[PATCH] D132105: [InstCombine] Fix freeze instruction getting inserted before landingpad

Danila Malyutin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 18 07:44:10 PDT 2022


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG4a9ff289fbaa: [InstCombine] Fix freeze instruction getting inserted before landingpad (authored by danilaml).

Changed prior to commit:
  https://reviews.llvm.org/D132105?vs=453630&id=453661#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D132105/new/

https://reviews.llvm.org/D132105

Files:
  llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
  llvm/test/Transforms/InstCombine/freeze-landingpad.ll


Index: llvm/test/Transforms/InstCombine/freeze-landingpad.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/InstCombine/freeze-landingpad.ll
@@ -0,0 +1,53 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; Check that no freeze instruction gets inserted before landingpad in a basic block
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+define i32 @propagate_freeze_in_landingpad() personality i32* ()* null {
+; CHECK-LABEL: @propagate_freeze_in_landingpad(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    br label [[INVOKE_BB1:%.*]]
+; CHECK:       invoke.bb1:
+; CHECK-NEXT:    [[X:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[INC:%.*]], [[NORMAL_RETURN:%.*]] ]
+; CHECK-NEXT:    [[RES0:%.*]] = invoke i32 @foo()
+; CHECK-NEXT:    to label [[INVOKE_BB2:%.*]] unwind label [[EXCEPTIONAL_RETURN:%.*]]
+; CHECK:       invoke.bb2:
+; CHECK-NEXT:    [[RES1:%.*]] = invoke i32 @foo()
+; CHECK-NEXT:    to label [[NORMAL_RETURN]] unwind label [[EXCEPTIONAL_RETURN]]
+; CHECK:       normal_return:
+; CHECK-NEXT:    [[INC]] = add nuw nsw i32 [[X]], 1
+; CHECK-NEXT:    br label [[INVOKE_BB1]]
+; CHECK:       exceptional_return:
+; CHECK-NEXT:    [[PHI:%.*]] = phi i32 [ [[X]], [[INVOKE_BB1]] ], [ 0, [[INVOKE_BB2]] ]
+; CHECK-NEXT:    [[LANDING_PAD:%.*]] = landingpad { i8*, i32 }
+; CHECK-NEXT:    cleanup
+; CHECK-NEXT:    [[FR:%.*]] = freeze i32 [[PHI]]
+; CHECK-NEXT:    [[RES:%.*]] = shl i32 [[FR]], 1
+; CHECK-NEXT:    ret i32 [[RES]]
+;
+entry:
+  br label %invoke.bb1
+
+invoke.bb1:                                 ; preds = %normal_return, %entry
+  %x = phi i32 [ 0, %entry ], [ %inc, %normal_return ]
+  %res0 = invoke i32 @foo()
+  to label %invoke.bb2 unwind label %exceptional_return
+
+invoke.bb2:                                  ; preds = %invoke.bb1
+  %res1 = invoke i32 @foo()
+  to label %normal_return unwind label %exceptional_return
+
+normal_return:                                ; preds = %invoke.bb2
+  %inc = add nuw nsw i32 %x, 1
+  br label %invoke.bb1
+
+exceptional_return:                             ; preds = %invoke.bb2, %invoke.bb1
+  %phi = phi i32 [ %x, %invoke.bb1 ], [ 0, %invoke.bb2 ]
+  %landing_pad = landingpad { i8*, i32 }
+  cleanup
+  %fr = freeze i32 %phi
+  %res = add i32 %fr, %phi
+  ret i32 %res
+}
+
+declare i32 @foo()
+
Index: llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -3897,26 +3897,26 @@
   // *all* uses if the operand is an invoke/callbr and the use is in a phi on
   // the normal/default destination. This is why the domination check in the
   // replacement below is still necessary.
-  Instruction *MoveBefore = nullptr;
+  BasicBlock::iterator MoveBefore;
   if (isa<Argument>(Op)) {
-    MoveBefore = &FI.getFunction()->getEntryBlock().front();
-    while (isa<AllocaInst>(MoveBefore))
-      MoveBefore = MoveBefore->getNextNode();
+    MoveBefore = FI.getFunction()->getEntryBlock().begin();
+    while (isa<AllocaInst>(*MoveBefore))
+      ++MoveBefore;
   } else if (auto *PN = dyn_cast<PHINode>(Op)) {
-    MoveBefore = PN->getParent()->getFirstNonPHI();
+    MoveBefore = PN->getParent()->getFirstInsertionPt();
   } else if (auto *II = dyn_cast<InvokeInst>(Op)) {
-    MoveBefore = II->getNormalDest()->getFirstNonPHI();
+    MoveBefore = II->getNormalDest()->getFirstInsertionPt();
   } else if (auto *CB = dyn_cast<CallBrInst>(Op)) {
-    MoveBefore = CB->getDefaultDest()->getFirstNonPHI();
+    MoveBefore = CB->getDefaultDest()->getFirstInsertionPt();
   } else {
     auto *I = cast<Instruction>(Op);
     assert(!I->isTerminator() && "Cannot be a terminator");
-    MoveBefore = I->getNextNode();
+    MoveBefore = std::next(I->getIterator());
   }
 
   bool Changed = false;
-  if (&FI != MoveBefore) {
-    FI.moveBefore(MoveBefore);
+  if (FI.getIterator() != MoveBefore) {
+    FI.moveBefore(*MoveBefore->getParent(), MoveBefore);
     Changed = true;
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D132105.453661.patch
Type: text/x-patch
Size: 4137 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220818/3f187e56/attachment.bin>


More information about the llvm-commits mailing list