<div dir="ltr">Hello David,<div><br></div><div>I am having an issue with some custom intrinsics that return a token value: InstCombine deletes the users of the token but not the instruction that creates the token itself. The IR is still valid but it's wasted.</div><div><br></div><div>The source of the issue is coming from an old patch of yours:</div><div><div><br></div><div>commit 7204cff0a121ebc770cf81f7f94679ae7324daae<br>Author: David Majnemer <<a href="mailto:david.majnemer@gmail.com">david.majnemer@gmail.com</a>><br>Date:   Fri Nov 6 21:26:32 2015 +0000<br><br>    [InstCombine] Don't RAUW tokens with undef<br>    <br>    Let SimplifyCFG remove unreachable BBs which define token instructions.<br>    <br>    llvm-svn: 252343<br><br>--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp<br>+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp<br>@@ -3012,7 +3012,7 @@ static bool prepareICWorklistFromFunction(Function &F, const DataLayout &DL,<br>     while (EndInst != BB->begin()) {<br>       // Delete the next to last instruction.<br>       Instruction *Inst = &*--EndInst->getIterator();<br>-      if (!Inst->use_empty())<br>+      if (!Inst->use_empty() && !Inst->getType()->isTokenTy())<br>         Inst->replaceAllUsesWith(UndefValue::get(Inst->getType()));

<br>-      if (Inst->isEHPad()) { </div><div>+      if (Inst->isEHPad() || Inst->getType()->isTokenTy()) { <br>         EndInst = Inst;<br>@@ -3022,7 +3022,8 @@ static bool prepareICWorklistFromFunction(Function &F, const DataLayout &DL,<br>         ++NumDeadInst;<br>         MadeIRChange = true;<br>       }<br>       Inst->eraseFromParent();<br>     }<br>   }<br></div><div><br></div><div>I believe the goal was to avoid RAUW the EHPad (as we don't delete the associated EHRet) and not skip all of the token instructions. At least you only test on EHPad in the associated unit test.</div><div><br></div><div>In which case we could instead do:</div><div><br></div><div><div>     while (EndInst != BB->begin()) {<br>       // Delete the next to last instruction.<br>       Instruction *Inst = &*--EndInst->getIterator();<br>-      if (!Inst->use_empty())<br>+      if (!Inst->use_empty() && !Inst->isEHPad())<br>         Inst->replaceAllUsesWith(UndefValue::get(Inst->getType()));

<br>       if (Inst->isEHPad()) { </div><div>         EndInst = Inst;<br>@@ -3022,7 +3022,8 @@ static bool prepareICWorklistFromFunction(Function &F, const DataLayout &DL,<br>         ++NumDeadInst;<br>         MadeIRChange = true;<br>       }<br>       Inst->eraseFromParent();<br>     }</div></div><div><br></div><div>Is my assumption correct?</div><div><br></div><div>Note that the code is now in llvm::removeAllNonTerminatorAndEHPadInstructions of llvm/lib/Transforms/Utils/Local.cpp</div><div><br></div>-- <br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><b>Alexandre Isoard</b><br></div></div></div></div>