[llvm-commits] [llvm] r53533 - in /llvm/trunk: lib/Transforms/Utils/SimplifyCFG.cpp test/Transforms/SimplifyCFG/2008-07-13-InfLoopMiscompile.ll

Chris Lattner sabre at nondot.org
Sun Jul 13 15:23:11 PDT 2008


Author: lattner
Date: Sun Jul 13 17:23:11 2008
New Revision: 53533

URL: http://llvm.org/viewvc/llvm-project?rev=53533&view=rev
Log:
Fix mishandling of the infinite loop case when merging two blocks.  This
fixes PR2540.

Added:
    llvm/trunk/test/Transforms/SimplifyCFG/2008-07-13-InfLoopMiscompile.ll
Modified:
    llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp

Modified: llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp?rev=53533&r1=53532&r2=53533&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp Sun Jul 13 17:23:11 2008
@@ -68,11 +68,10 @@
          succ_end(ExistPred) && "ExistPred is not a predecessor of Succ!");
   if (!isa<PHINode>(Succ->begin())) return; // Quick exit if nothing to do
   
-  for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
-    PHINode *PN = cast<PHINode>(I);
-    Value *V = PN->getIncomingValueForBlock(ExistPred);
-    PN->addIncoming(V, NewPred);
-  }
+  PHINode *PN;
+  for (BasicBlock::iterator I = Succ->begin();
+       (PN = dyn_cast<PHINode>(I)); ++I)
+    PN->addIncoming(PN->getIncomingValueForBlock(ExistPred), NewPred);
 }
 
 // CanPropagatePredecessorsForPHIs - Return true if we can fold BB, an
@@ -860,7 +859,7 @@
       for (unsigned i = 0, e = NewSI->getNumSuccessors(); i != e; ++i)
         if (NewSI->getSuccessor(i) == BB) {
           if (InfLoopBlock == 0) {
-            // Insert it at the end of the loop, because it's either code,
+            // Insert it at the end of the function, because it's either code,
             // or it won't matter if it's hot. :)
             InfLoopBlock = BasicBlock::Create("infloop", BB->getParent());
             BranchInst::Create(InfLoopBlock, InfLoopBlock);
@@ -1430,11 +1429,11 @@
 /// setcc into the predecessor and use logical operations to pick the right
 /// destination.
 static bool FoldBranchToCommonDest(BranchInst *BI) {
+  BasicBlock *BB = BI->getParent();
   Instruction *Cond = dyn_cast<Instruction>(BI->getCondition());
   if (Cond == 0) return false;
 
-  BasicBlock *BB = BI->getParent();
-
+  
   // Only allow this if the condition is a simple instruction that can be
   // executed unconditionally.  It must be in the same block as the branch, and
   // must be at the front of the block.
@@ -1456,6 +1455,9 @@
   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.
     if (PBI == 0 || PBI->isUnconditional() ||
         !SafeToMergeTerminators(BI, PBI))
       continue;
@@ -1596,18 +1598,25 @@
   // Finally, if everything is ok, fold the branches to logical ops.
   BasicBlock *OtherDest  = BI->getSuccessor(BIOp ^ 1);
   
-  // If OtherDest *is* BB, then this is a basic block with just
-  // a conditional branch in it, where one edge (OtherDesg) goes
-  // back to the block.  We know that the program doesn't get
-  // stuck in the infinite loop, so the condition must be such
-  // that OtherDest isn't branched through. Forward to CommonDest,
-  // and avoid an infinite loop at optimizer time.
-  if (OtherDest == BB)
-    OtherDest = CommonDest;
-  
   DOUT << "FOLDING BRs:" << *PBI->getParent()
        << "AND: " << *BI->getParent();
   
+  
+  // If OtherDest *is* BB, then BB is a basic block with a single conditional
+  // branch in it, where one edge (OtherDest) goes back to itself but the other
+  // exits.  We don't *know* that the program avoids the infinite loop
+  // (even though that seems likely).  If we do this xform naively, we'll end up
+  // recursively unpeeling the loop.  Since we know that (after the xform is
+  // done) that the block *is* infinite if reached, we just make it an obviously
+  // infinite loop with no cond branch.
+  if (OtherDest == BB) {
+    // Insert it at the end of the function, because it's either code,
+    // or it won't matter if it's hot. :)
+    BasicBlock *InfLoopBlock = BasicBlock::Create("infloop", BB->getParent());
+    BranchInst::Create(InfLoopBlock, InfLoopBlock);
+    OtherDest = InfLoopBlock;
+  }  
+  
   DOUT << *PBI->getParent()->getParent();
   
   // BI may have other predecessors.  Because of this, we leave

Added: llvm/trunk/test/Transforms/SimplifyCFG/2008-07-13-InfLoopMiscompile.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SimplifyCFG/2008-07-13-InfLoopMiscompile.ll?rev=53533&view=auto

==============================================================================
--- llvm/trunk/test/Transforms/SimplifyCFG/2008-07-13-InfLoopMiscompile.ll (added)
+++ llvm/trunk/test/Transforms/SimplifyCFG/2008-07-13-InfLoopMiscompile.ll Sun Jul 13 17:23:11 2008
@@ -0,0 +1,36 @@
+; RUN: llvm-as < %s | opt -simplifycfg | llvm-dis | grep {%outval = phi i32 .*mux}
+; PR2540
+; Outval should end up with a select from 0/2, not all constants.
+
+target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32"
+target triple = "i386-pc-linux-gnu"
+ at g_37 = common global i32 0		; <i32*> [#uses=1]
+ at .str = internal constant [4 x i8] c"%d\0A\00"		; <[4 x i8]*> [#uses=1]
+
+define i32 @main() nounwind  {
+entry:
+	%l = load i32* @g_37, align 4		; <i32> [#uses=1]
+	%cmpa = icmp ne i32 %l, 0		; <i1> [#uses=3]
+	br i1 %cmpa, label %func_1.exit, label %mooseblock
+
+mooseblock:		; preds = %entry
+	%cmpb = icmp eq i1 %cmpa, false		; <i1> [#uses=2]
+	br i1 %cmpb, label %monkeyblock, label %beeblock
+
+monkeyblock:		; preds = %monkeyblock, %mooseblock
+	br i1 %cmpb, label %cowblock, label %monkeyblock
+
+beeblock:		; preds = %beeblock, %mooseblock
+	br i1 %cmpa, label %cowblock, label %beeblock
+
+cowblock:		; preds = %beeblock, %monkeyblock
+	%cowval = phi i32 [ 2, %beeblock ], [ 0, %monkeyblock ]		; <i32> [#uses=1]
+	br label %func_1.exit
+
+func_1.exit:		; preds = %cowblock, %entry
+	%outval = phi i32 [ %cowval, %cowblock ], [ 1, %entry ]		; <i32> [#uses=1]
+	%pout = tail call i32 (i8*, ...)* @printf( i8* noalias  getelementptr ([4 x i8]* @.str, i32 0, i32 0), i32 %outval ) nounwind 		; <i32> [#uses=0]
+	ret i32 0
+}
+
+declare i32 @printf(i8*, ...) nounwind 





More information about the llvm-commits mailing list