[llvm] 8f3fd26 - [Reassociate] Use getInsertionPointerAfterDef()

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 31 02:10:38 PDT 2022


Author: Nikita Popov
Date: 2022-08-31T11:10:24+02:00
New Revision: 8f3fd26b74a7f5eb9892556511bae8f70a3597dc

URL: https://github.com/llvm/llvm-project/commit/8f3fd26b74a7f5eb9892556511bae8f70a3597dc
DIFF: https://github.com/llvm/llvm-project/commit/8f3fd26b74a7f5eb9892556511bae8f70a3597dc.diff

LOG: [Reassociate] Use getInsertionPointerAfterDef()

This simplifies the code and fixes handling for the callbr case,
where the instruction needs to be inserted in the normal
destination, rather than after the terminator.

Originally part of D129660.

Added: 
    llvm/test/Transforms/Reassociate/callbr.ll

Modified: 
    llvm/lib/Transforms/Scalar/Reassociate.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/Reassociate.cpp b/llvm/lib/Transforms/Scalar/Reassociate.cpp
index 87546a7496ecd..3bd1234bb9083 100644
--- a/llvm/lib/Transforms/Scalar/Reassociate.cpp
+++ b/llvm/lib/Transforms/Scalar/Reassociate.cpp
@@ -886,40 +886,16 @@ static Value *NegateValue(Value *V, Instruction *BI,
     if (TheNeg->getParent()->getParent() != BI->getParent()->getParent())
       continue;
 
-    bool FoundCatchSwitch = false;
-
-    BasicBlock::iterator InsertPt;
+    Instruction *InsertPt;
     if (Instruction *InstInput = dyn_cast<Instruction>(V)) {
-      if (InvokeInst *II = dyn_cast<InvokeInst>(InstInput)) {
-        InsertPt = II->getNormalDest()->begin();
-      } else {
-        InsertPt = ++InstInput->getIterator();
-      }
-
-      const BasicBlock *BB = InsertPt->getParent();
-
-      // Make sure we don't move anything before PHIs or exception
-      // handling pads.
-      while (InsertPt != BB->end() && (isa<PHINode>(InsertPt) ||
-                                       InsertPt->isEHPad())) {
-        if (isa<CatchSwitchInst>(InsertPt))
-          // A catchswitch cannot have anything in the block except
-          // itself and PHIs.  We'll bail out below.
-          FoundCatchSwitch = true;
-        ++InsertPt;
-      }
+      InsertPt = InstInput->getInsertionPointAfterDef();
+      if (!InsertPt)
+        continue;
     } else {
-      InsertPt = TheNeg->getParent()->getParent()->getEntryBlock().begin();
+      InsertPt = &*TheNeg->getFunction()->getEntryBlock().begin();
     }
 
-    // We found a catchswitch in the block where we want to move the
-    // neg.  We cannot move anything into that block.  Bail and just
-    // create the neg before BI, as if we hadn't found an existing
-    // neg.
-    if (FoundCatchSwitch)
-      break;
-
-    TheNeg->moveBefore(&*InsertPt);
+    TheNeg->moveBefore(InsertPt);
     if (TheNeg->getOpcode() == Instruction::Sub) {
       TheNeg->setHasNoUnsignedWrap(false);
       TheNeg->setHasNoSignedWrap(false);

diff  --git a/llvm/test/Transforms/Reassociate/callbr.ll b/llvm/test/Transforms/Reassociate/callbr.ll
new file mode 100644
index 0000000000000..26cb7d51dc338
--- /dev/null
+++ b/llvm/test/Transforms/Reassociate/callbr.ll
@@ -0,0 +1,25 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -S -reassociate < %s | FileCheck %s
+
+define i32 @test(i1 %b) {
+; CHECK-LABEL: @test(
+; CHECK-NEXT:    [[RES:%.*]] = callbr i32 asm "", "=r,!i"()
+; CHECK-NEXT:    to label [[NORMAL:%.*]] [label %abnormal]
+; CHECK:       normal:
+; CHECK-NEXT:    [[FACTOR:%.*]] = mul i32 [[RES]], -2
+; CHECK-NEXT:    [[SUB2:%.*]] = add i32 [[FACTOR]], 5
+; CHECK-NEXT:    ret i32 [[SUB2]]
+; CHECK:       abnormal:
+; CHECK-NEXT:    ret i32 0
+;
+  %res = callbr i32 asm "", "=r,!i"()
+  to label %normal [label %abnormal]
+
+normal:
+  %sub1 = sub nsw i32 5, %res
+  %sub2 = sub nsw i32 %sub1, %res
+  ret i32 %sub2
+
+abnormal:
+  ret i32 0
+}


        


More information about the llvm-commits mailing list