[llvm-dev] InstCombine doesn't delete instructions with token

Eli Friedman via llvm-dev llvm-dev at lists.llvm.org
Tue Jun 16 19:55:48 PDT 2020


In general, we have to RAUW before we erase an instruction in dead code; even if we know the instruction is dead, it could still have uses in other dead code.  If an instruction still has uses, we can’t erase it.

-Eli

From: llvm-dev <llvm-dev-bounces at lists.llvm.org> On Behalf Of Alexandre Isoard via llvm-dev
Sent: Tuesday, June 16, 2020 7:33 PM
To: David Majnemer <david.majnemer at gmail.com>; llvm-dev <llvm-dev at lists.llvm.org>
Subject: [EXT] [llvm-dev] InstCombine doesn't delete instructions with token

Hello David,

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.

The source of the issue is coming from an old patch of yours:

commit 7204cff0a121ebc770cf81f7f94679ae7324daae
Author: David Majnemer <david.majnemer at gmail.com<mailto:david.majnemer at gmail.com>>
Date:   Fri Nov 6 21:26:32 2015 +0000

    [InstCombine] Don't RAUW tokens with undef

    Let SimplifyCFG remove unreachable BBs which define token instructions.

    llvm-svn: 252343

--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -3012,7 +3012,7 @@ static bool prepareICWorklistFromFunction(Function &F, const DataLayout &DL,
     while (EndInst != BB->begin()) {
       // Delete the next to last instruction.
       Instruction *Inst = &*--EndInst->getIterator();
-      if (!Inst->use_empty())
+      if (!Inst->use_empty() && !Inst->getType()->isTokenTy())
         Inst->replaceAllUsesWith(UndefValue::get(Inst->getType()));
-      if (Inst->isEHPad()) {
+      if (Inst->isEHPad() || Inst->getType()->isTokenTy()) {
         EndInst = Inst;
@@ -3022,7 +3022,8 @@ static bool prepareICWorklistFromFunction(Function &F, const DataLayout &DL,
         ++NumDeadInst;
         MadeIRChange = true;
       }
       Inst->eraseFromParent();
     }
   }

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.

In which case we could instead do:

     while (EndInst != BB->begin()) {
       // Delete the next to last instruction.
       Instruction *Inst = &*--EndInst->getIterator();
-      if (!Inst->use_empty())
+      if (!Inst->use_empty() && !Inst->isEHPad())
         Inst->replaceAllUsesWith(UndefValue::get(Inst->getType()));
       if (Inst->isEHPad()) {
         EndInst = Inst;
@@ -3022,7 +3022,8 @@ static bool prepareICWorklistFromFunction(Function &F, const DataLayout &DL,
         ++NumDeadInst;
         MadeIRChange = true;
       }
       Inst->eraseFromParent();
     }

Is my assumption correct?

Note that the code is now in llvm::removeAllNonTerminatorAndEHPadInstructions of llvm/lib/Transforms/Utils/Local.cpp

--
Alexandre Isoard
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200617/a60d4c8f/attachment.html>


More information about the llvm-dev mailing list