[ScalarEvolutionExpander] PHI on a catchpad can be used on both edges
authorDavid Majnemer <david.majnemer@gmail.com>
Tue, 27 Oct 2015 19:48:28 +0000 (19:48 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Tue, 27 Oct 2015 19:48:28 +0000 (19:48 +0000)
A PHI on a catchpad might be used by both edges out of the catchpad,
feeding back into a loop.  In this case, just use the insertion point.
Anything more clever would require new basic blocks or PHI placement.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@251442 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/ScalarEvolutionExpander.cpp
test/Transforms/LoopStrengthReduce/funclet.ll

index 428d989285c66e369bafa2b0a74405a6119ebada..81316849847a0ca09a96366707d2ffd087db12de 100644 (file)
@@ -87,7 +87,6 @@ Value *SCEVExpander::ReuseOrCreateCast(Value *V, Type *Ty,
 }
 
 static BasicBlock::iterator findInsertPointAfter(Instruction *I,
-                                                 DominatorTree &DT,
                                                  BasicBlock *MustDominate) {
   BasicBlock::iterator IP = ++I->getIterator();
   if (auto *II = dyn_cast<InvokeInst>(I))
@@ -107,12 +106,8 @@ static BasicBlock::iterator findInsertPointAfter(Instruction *I,
       IP = CEPI->getUnwindDest()->getFirstNonPHI();
     } else if (auto *CEPI = dyn_cast<CleanupEndPadInst>(IP)) {
       IP = CEPI->getUnwindDest()->getFirstNonPHI();
-    } else if (auto *CPI = dyn_cast<CatchPadInst>(IP)) {
-      BasicBlock *NormalDest = CPI->getNormalDest();
-      if (NormalDest == MustDominate || DT.dominates(NormalDest, MustDominate))
-        IP = NormalDest->getFirstNonPHI();
-      else
-        IP = CPI->getUnwindDest()->getFirstNonPHI();
+    } else if (isa<CatchPadInst>(IP)) {
+      IP = MustDominate->getFirstInsertionPt();
     } else {
       llvm_unreachable("unexpected eh pad!");
     }
@@ -177,8 +172,7 @@ Value *SCEVExpander::InsertNoopCastOfTo(Value *V, Type *Ty) {
 
   // Cast the instruction immediately after the instruction.
   Instruction *I = cast<Instruction>(V);
-  BasicBlock::iterator IP =
-      findInsertPointAfter(I, SE.DT, Builder.GetInsertBlock());
+  BasicBlock::iterator IP = findInsertPointAfter(I, Builder.GetInsertBlock());
   return ReuseOrCreateCast(I, Ty, Op, IP);
 }
 
@@ -1423,8 +1417,8 @@ Value *SCEVExpander::visitAddRecExpr(const SCEVAddRecExpr *S) {
       NewOps[i] = SE.getAnyExtendExpr(S->op_begin()[i], CanonicalIV->getType());
     Value *V = expand(SE.getAddRecExpr(NewOps, S->getLoop(),
                                        S->getNoWrapFlags(SCEV::FlagNW)));
-    BasicBlock::iterator NewInsertPt = findInsertPointAfter(
-        cast<Instruction>(V), SE.DT, Builder.GetInsertBlock());
+    BasicBlock::iterator NewInsertPt =
+        findInsertPointAfter(cast<Instruction>(V), Builder.GetInsertBlock());
     V = expandCodeFor(SE.getTruncateExpr(SE.getUnknown(V), Ty), nullptr,
                       &*NewInsertPt);
     return V;
index 60bec76e7e636fa4612c591ca2d0ece26cfdf75e..ba2234f31050ea31bdae9dce08fe2fb284701c2d 100644 (file)
@@ -146,3 +146,45 @@ iter:                                             ; preds = %loop_body
 
 ; CHECK: blah2:
 ; CHECK-NEXT: ptrtoint i8* %phi2 to i32
+
+define void @i() personality i32 (...)* @_except_handler3 {
+entry:
+  br label %throw
+
+throw:                                            ; preds = %throw, %entry
+  %tmp96 = getelementptr inbounds i8, i8* undef, i32 1
+  invoke void @reserve()
+          to label %throw unwind label %catchpad
+
+catchpad:                                              ; preds = %throw
+  %phi2 = phi i8* [ %tmp96, %throw ]
+  catchpad [] to label %cp_body unwind label %catchendpad
+
+cp_body:
+  br label %loop_head
+
+catchendpad:
+  catchendpad unwind label %cleanuppad
+
+cleanuppad:
+  cleanuppad []
+  br label %loop_head
+
+loop_head:
+  br label %loop_body
+
+loop_body:                                        ; preds = %iter, %catchpad
+  %tmp99 = phi i8* [ %tmp101, %iter ], [ %phi2, %loop_head ]
+  %tmp100 = icmp eq i8* %tmp99, undef
+  br i1 %tmp100, label %unwind_out, label %iter
+
+iter:                                             ; preds = %loop_body
+  %tmp101 = getelementptr inbounds i8, i8* %tmp99, i32 1
+  br i1 undef, label %unwind_out, label %loop_body
+
+unwind_out:                                       ; preds = %iter, %loop_body
+  unreachable
+}
+
+; CHECK-LABEL: define void @i(
+; CHECK: ptrtoint i8* %phi2 to i32