[llvm-commits] [llvm] r46427 - in /llvm/trunk: lib/Transforms/Scalar/SCCP.cpp test/Transforms/SCCP/2008-01-27-UndefCorrelate.ll

Chris Lattner sabre at nondot.org
Sun Jan 27 16:32:30 PST 2008


Author: lattner
Date: Sun Jan 27 18:32:30 2008
New Revision: 46427

URL: http://llvm.org/viewvc/llvm-project?rev=46427&view=rev
Log:
Fix PR1938 by forcing the code that uses an undefined value to branch one
way or the other.  Rewriting the code itself prevents subsequent analysis
passes from making contradictory conclusions about the code that could 
cause an infeasible path to be made feasible.

Added:
    llvm/trunk/test/Transforms/SCCP/2008-01-27-UndefCorrelate.ll
Modified:
    llvm/trunk/lib/Transforms/Scalar/SCCP.cpp

Modified: llvm/trunk/lib/Transforms/Scalar/SCCP.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/SCCP.cpp?rev=46427&r1=46426&r2=46427&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/SCCP.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/SCCP.cpp Sun Jan 27 18:32:30 2008
@@ -1313,15 +1313,30 @@
       continue;
     }
     
-    // If the edge to the first successor isn't thought to be feasible yet, mark
-    // it so now.
-    if (KnownFeasibleEdges.count(Edge(BB, TI->getSuccessor(0))))
+    // If the edge to the second successor isn't thought to be feasible yet,
+    // mark it so now.  We pick the second one so that this goes to some
+    // enumerated value in a switch instead of going to the default destination.
+    if (KnownFeasibleEdges.count(Edge(BB, TI->getSuccessor(1))))
       continue;
     
     // Otherwise, it isn't already thought to be feasible.  Mark it as such now
     // and return.  This will make other blocks reachable, which will allow new
     // values to be discovered and existing ones to be moved in the lattice.
-    markEdgeExecutable(BB, TI->getSuccessor(0));
+    markEdgeExecutable(BB, TI->getSuccessor(1));
+    
+    // This must be a conditional branch of switch on undef.  At this point,
+    // force the old terminator to branch to the first successor.  This is
+    // required because we are now influencing the dataflow of the function with
+    // the assumption that this edge is taken.  If we leave the branch condition
+    // as undef, then further analysis could think the undef went another way
+    // leading to an inconsistent set of conclusions.
+    if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
+      BI->setCondition(ConstantInt::getFalse());
+    } else {
+      SwitchInst *SI = cast<SwitchInst>(TI);
+      SI->setCondition(SI->getCaseValue(1));
+    }
+    
     return true;
   }
 

Added: llvm/trunk/test/Transforms/SCCP/2008-01-27-UndefCorrelate.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SCCP/2008-01-27-UndefCorrelate.ll?rev=46427&view=auto

==============================================================================
--- llvm/trunk/test/Transforms/SCCP/2008-01-27-UndefCorrelate.ll (added)
+++ llvm/trunk/test/Transforms/SCCP/2008-01-27-UndefCorrelate.ll Sun Jan 27 18:32:30 2008
@@ -0,0 +1,36 @@
+; RUN: llvm-as < %s | opt -sccp | llvm-dis | grep undef | count 1
+; PR1938
+
+define i32 @main() {
+entry:
+	br label %bb
+
+bb:
+	%indvar = phi i32 [ 0, %entry ], [ %k, %bb.backedge ]
+	%k = add i32 %indvar, 1
+	br i1 undef, label %cond_true, label %cond_false
+
+cond_true:
+	%tmp97 = icmp slt i32 %k, 10
+	br i1 %tmp97, label %bb.backedge, label %bb12
+
+bb.backedge:
+	br label %bb
+
+cond_false:
+	%tmp9 = icmp slt i32 %k, 10
+	br i1 %tmp9, label %bb.backedge, label %bb12
+
+bb12:
+	%tmp14 = icmp eq i32 %k, 10
+	br i1 %tmp14, label %cond_next18, label %cond_true17
+
+cond_true17:
+	tail call void @abort( )
+	unreachable
+
+cond_next18:
+	ret i32 0
+}
+
+declare void @abort()





More information about the llvm-commits mailing list