[llvm-commits] [llvm] r50110 - in /llvm/trunk: lib/Transforms/Scalar/JumpThreading.cpp test/Transforms/JumpThreading/compare.ll
Chris Lattner
sabre at nondot.org
Tue Apr 22 14:40:39 PDT 2008
Author: lattner
Date: Tue Apr 22 16:40:39 2008
New Revision: 50110
URL: http://llvm.org/viewvc/llvm-project?rev=50110&view=rev
Log:
Start doing the significantly useful part of jump threading: handle cases
where a comparison has a phi input and that phi is a constant. For example,
stuff like:
Threading edge through bool from 'bb2149' to 'bb2231' with cost: 1, across block:
bb2237: ; preds = %bb2231, %bb2149
%tmp2328.rle = phi i32 [ %tmp2232, %bb2231 ], [ %tmp2232439, %bb2149 ] ; <i32> [#uses=2]
%done.0 = phi i32 [ %done.2, %bb2231 ], [ 0, %bb2149 ] ; <i32> [#uses=1]
%tmp2239 = icmp eq i32 %done.0, 0 ; <i1> [#uses=1]
br i1 %tmp2239, label %bb2231, label %bb2327
or
bb38.i298: ; preds = %bb33.i295, %bb1693
%tmp39.i296.rle = phi %struct.ibox* [ null, %bb1693 ], [ %tmp39.i296.rle1109, %bb33.i295 ] ; <%struct.ibox*> [#uses=2]
%minspan.1.i291.reg2mem.1 = phi i32 [ 32000, %bb1693 ], [ %minspan.0.i288, %bb33.i295 ] ; <i32> [#uses=1]
%tmp40.i297 = icmp eq %struct.ibox* %tmp39.i296.rle, null ; <i1> [#uses=1]
br i1 %tmp40.i297, label %implfeeds.exit311, label %bb43.i301
This triggers thousands of times in spec.
Added:
llvm/trunk/test/Transforms/JumpThreading/compare.ll
Modified:
llvm/trunk/lib/Transforms/Scalar/JumpThreading.cpp
Modified: llvm/trunk/lib/Transforms/Scalar/JumpThreading.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/JumpThreading.cpp?rev=50110&r1=50109&r2=50110&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/JumpThreading.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/JumpThreading.cpp Tue Apr 22 16:40:39 2008
@@ -61,6 +61,7 @@
bool ProcessJumpOnPHI(PHINode *PN);
bool ProcessBranchOnLogical(Value *V, BasicBlock *BB, bool isAnd);
+ bool ProcessBranchOnCompare(CmpInst *Cmp, BasicBlock *BB);
};
char JumpThreading::ID = 0;
RegisterPass<JumpThreading> X("jump-threading", "Jump Threading");
@@ -199,6 +200,14 @@
return true;
}
+ // If we have "br (phi != 42)" and the phi node has any constant values as
+ // operands, we can thread through this block.
+ if (CmpInst *CondCmp = dyn_cast<CmpInst>(Condition))
+ if (isa<PHINode>(CondCmp->getOperand(0)) &&
+ isa<Constant>(CondCmp->getOperand(1)) &&
+ ProcessBranchOnCompare(CondCmp, BB))
+ return true;
+
return false;
}
@@ -209,18 +218,14 @@
bool JumpThreading::ProcessJumpOnPHI(PHINode *PN) {
// See if the phi node has any constant values. If so, we can determine where
// the corresponding predecessor will branch.
- unsigned PredNo = ~0U;
ConstantInt *PredCst = 0;
- for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
- if ((PredCst = dyn_cast<ConstantInt>(PN->getIncomingValue(i)))) {
- PredNo = i;
+ for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
+ if ((PredCst = dyn_cast<ConstantInt>(PN->getIncomingValue(i))))
break;
- }
- }
// If no incoming value has a constant, we don't know the destination of any
// predecessors.
- if (PredNo == ~0U)
+ if (PredCst == 0)
return false;
// See if the cost of duplicating this block is low enough.
@@ -325,6 +330,77 @@
return true;
}
+/// ProcessBranchOnCompare - We found a branch on a comparison between a phi
+/// node and a constant. If the PHI node contains any constants as inputs, we
+/// can fold the compare for that edge and thread through it.
+bool JumpThreading::ProcessBranchOnCompare(CmpInst *Cmp, BasicBlock *BB) {
+ PHINode *PN = cast<PHINode>(Cmp->getOperand(0));
+ Constant *RHS = cast<Constant>(Cmp->getOperand(1));
+
+ // If the phi isn't in the current block, an incoming edge to this block
+ // doesn't control the destination.
+ if (PN->getParent() != BB)
+ return false;
+
+ // We can do this simplification if any comparisons fold to true or false.
+ // See if any do.
+ Constant *PredCst = 0;
+ bool TrueDirection = false;
+ for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
+ PredCst = dyn_cast<Constant>(PN->getIncomingValue(i));
+ if (PredCst == 0) continue;
+
+ Constant *Res;
+ if (ICmpInst *ICI = dyn_cast<ICmpInst>(Cmp))
+ Res = ConstantExpr::getICmp(ICI->getPredicate(), PredCst, RHS);
+ else
+ Res = ConstantExpr::getFCmp(cast<FCmpInst>(Cmp)->getPredicate(),
+ PredCst, RHS);
+ // If this folded to a constant expr, we can't do anything.
+ if (ConstantInt *ResC = dyn_cast<ConstantInt>(Res)) {
+ TrueDirection = ResC->getZExtValue();
+ break;
+ }
+ // If this folded to undef, just go the false way.
+ if (isa<UndefValue>(Res)) {
+ TrueDirection = false;
+ break;
+ }
+
+ // Otherwise, we can't fold this input.
+ PredCst = 0;
+ }
+
+ // If no match, bail out.
+ if (PredCst == 0)
+ return false;
+
+ // See if the cost of duplicating this block is low enough.
+ unsigned JumpThreadCost = getJumpThreadDuplicationCost(BB);
+ if (JumpThreadCost > Threshold) {
+ DOUT << " Not threading BB '" << BB->getNameStart()
+ << "' - Cost is too high: " << JumpThreadCost << "\n";
+ return false;
+ }
+
+ // If so, we can actually do this threading. Merge any common predecessors
+ // that will act the same.
+ BasicBlock *PredBB = FactorCommonPHIPreds(PN, PredCst);
+
+ // Next, get our successor.
+ BasicBlock *SuccBB = BB->getTerminator()->getSuccessor(!TrueDirection);
+
+ // And finally, do it!
+ DOUT << " Threading edge through bool from '" << PredBB->getNameStart()
+ << "' to '" << SuccBB->getNameStart() << "' with cost: "
+ << JumpThreadCost << ", across block:\n "
+ << *BB << "\n";
+
+ ThreadEdge(BB, PredBB, SuccBB);
+ ++NumThreads;
+ return true;
+}
+
/// ThreadEdge - We have decided that it is safe and profitable to thread an
/// edge from PredBB to SuccBB across BB. Transform the IR to reflect this
Added: llvm/trunk/test/Transforms/JumpThreading/compare.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/JumpThreading/compare.ll?rev=50110&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/JumpThreading/compare.ll (added)
+++ llvm/trunk/test/Transforms/JumpThreading/compare.ll Tue Apr 22 16:40:39 2008
@@ -0,0 +1,30 @@
+; There should be no phi nodes left.
+; RUN: llvm-as < %s | opt -jump-threading -simplifycfg -mem2reg | llvm-dis | not grep {phi i32}
+
+declare i32 @f1()
+declare i32 @f2()
+declare void @f3()
+
+define i32 @test(i1 %cond) {
+ br i1 %cond, label %T1, label %F1
+
+T1:
+ %v1 = call i32 @f1()
+ br label %Merge
+
+F1:
+ %v2 = call i32 @f2()
+ br label %Merge
+
+Merge:
+ %B = phi i32 [%v1, %T1], [12, %F1]
+ %A = icmp ne i32 %B, 42
+ br i1 %A, label %T2, label %F2
+
+T2:
+ call void @f3()
+ ret i32 1
+
+F2:
+ ret i32 0
+}
More information about the llvm-commits
mailing list