Fix a problem exposed by PR3354: simplifycfg was making a potentially
authorChris Lattner <sabre@nondot.org>
Mon, 19 Jan 2009 23:03:13 +0000 (23:03 +0000)
committerChris Lattner <sabre@nondot.org>
Mon, 19 Jan 2009 23:03:13 +0000 (23:03 +0000)
trapping instruction be executed unconditionally.

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

lib/Transforms/Utils/SimplifyCFG.cpp
test/Transforms/SimplifyCFG/2009-01-19-UnconditionalTrappingConstantExpr.ll [new file with mode: 0644]

index b0feadffadd3e6974ce6ab7211e84981a6635198..4abc2eb0422fb98985601e969543b0b762eeb715 100644 (file)
@@ -1423,11 +1423,21 @@ static bool FoldBranchToCommonDest(BranchInst *BI) {
   if ((!isa<CmpInst>(Cond) && !isa<BinaryOperator>(Cond)) ||
       Cond->getParent() != BB || &BB->front() != Cond || !Cond->hasOneUse())
     return false;
-      
+  
   // Make sure the instruction after the condition is the cond branch.
   BasicBlock::iterator CondIt = Cond; ++CondIt;
   if (&*CondIt != BI)
     return false;
+
+  // Cond is known to be a compare or binary operator.  Check to make sure that
+  // neither operand is a potentially-trapping constant expression.
+  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Cond->getOperand(0)))
+    if (CE->canTrap())
+      return false;
+  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Cond->getOperand(1)))
+    if (CE->canTrap())
+      return false;
+  
   
   // Finally, don't infinitely unroll conditional loops.
   BasicBlock *TrueDest  = BI->getSuccessor(0);
@@ -1438,6 +1448,7 @@ static bool FoldBranchToCommonDest(BranchInst *BI) {
   for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
     BasicBlock *PredBlock = *PI;
     BranchInst *PBI = dyn_cast<BranchInst>(PredBlock->getTerminator());
+    
     // Check that we have two conditional branches.  If there is a PHI node in
     // the common successor, verify that the same value flows in from both
     // blocks.
@@ -1459,6 +1470,8 @@ static bool FoldBranchToCommonDest(BranchInst *BI) {
     else
       continue;
 
+    DOUT << "FOLDING BRANCH TO COMMON DEST:\n" << *PBI << *BB;
+    
     // If we need to invert the condition in the pred block to match, do so now.
     if (InvertPredCond) {
       Value *NewCond =
diff --git a/test/Transforms/SimplifyCFG/2009-01-19-UnconditionalTrappingConstantExpr.ll b/test/Transforms/SimplifyCFG/2009-01-19-UnconditionalTrappingConstantExpr.ll
new file mode 100644 (file)
index 0000000..0bc6bdb
--- /dev/null
@@ -0,0 +1,20 @@
+; RUN: llvm-as < %s | opt -simplifycfg | llvm-dis | grep {br i1 } | count 2
+; PR3354
+; Do not merge bb1 into the entry block, it might trap.
+
+@G = extern_weak global i32
+
+define i32 @test(i32 %tmp21, i32 %tmp24) {
+       %tmp25 = icmp sle i32 %tmp21, %tmp24            
+       br i1 %tmp25, label %bb2, label %bb1    
+                                       
+bb1:           ; preds = %bb   
+       %tmp26 = icmp sgt i32 sdiv (i32 -32768, i32 ptrtoint (i32* @G to i32)), 0
+       br i1 %tmp26, label %bb6, label %bb2            
+bb2:
+       ret i32 42
+
+bb6:
+       unwind
+}
+