From: Owen Anderson Date: Tue, 31 Aug 2010 20:26:04 +0000 (+0000) Subject: More cleanups of my JumpThreading transforms, including extracting some duplicated... X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=0eb355ab6be61ebd7adb407e02a3604be032b99e;p=oota-llvm.git More cleanups of my JumpThreading transforms, including extracting some duplicated code into a helper function. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@112634 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/Scalar/JumpThreading.cpp b/lib/Transforms/Scalar/JumpThreading.cpp index 5ff5d7de016..abf98866aec 100644 --- a/lib/Transforms/Scalar/JumpThreading.cpp +++ b/lib/Transforms/Scalar/JumpThreading.cpp @@ -276,6 +276,17 @@ void JumpThreading::FindLoopHeaders(Function &F) { LoopHeaders.insert(const_cast(Edges[i].second)); } +// Helper method for ComputeValueKnownInPredecessors. If Value is a +// ConstantInt, push it. If it's an undef, push 0. Otherwise, do nothing. +static void PushConstantIntOrUndef(SmallVectorImpl > &Result, + Constant *Value, BasicBlock* BB){ + if (ConstantInt *FoldedCInt = dyn_cast(Value)) + Result.push_back(std::make_pair(FoldedCInt, BB)); + else if (isa(Value)) + Result.push_back(std::make_pair((ConstantInt*)0, BB)); +} + /// ComputeValueKnownInPredecessors - Given a basic block BB and a value V, see /// if we can infer that the value is a known ConstantInt in any of our /// predecessors. If so, return the known list of value and pred BB in the @@ -355,11 +366,7 @@ ComputeValueKnownInPredecessors(Value *V, BasicBlock *BB,PredValueInfo &Result){ PN->getIncomingBlock(i), BB); // LVI returns null is no value could be determined. if (!CI) continue; - if (ConstantInt *CInt = dyn_cast(CI)) - Result.push_back(std::make_pair(CInt, PN->getIncomingBlock(i))); - else if (isa(CI)) - Result.push_back(std::make_pair((ConstantInt*)0, - PN->getIncomingBlock(i))); + PushConstantIntOrUndef(Result, CI, PN->getIncomingBlock(i)); } } @@ -428,26 +435,17 @@ ComputeValueKnownInPredecessors(Value *V, BasicBlock *BB,PredValueInfo &Result){ // Try to simplify some other binary operator values. } else if (BinaryOperator *BO = dyn_cast(I)) { - ConstantInt *CI = dyn_cast(BO->getOperand(1)); - if (CI) { + if (ConstantInt *CI = dyn_cast(BO->getOperand(1))) { SmallVector, 8> LHSVals; ComputeValueKnownInPredecessors(BO->getOperand(0), BB, LHSVals); // Try to use constant folding to simplify the binary operator. for (unsigned i = 0, e = LHSVals.size(); i != e; ++i) { - Constant *Folded = 0; - if (LHSVals[i].first == 0) { - Folded = ConstantExpr::get(BO->getOpcode(), - UndefValue::get(BO->getType()), - CI); - } else { - Folded = ConstantExpr::get(BO->getOpcode(), LHSVals[i].first, CI); - } + Constant *V = LHSVals[i].first ? LHSVals[i].first : + cast(UndefValue::get(BO->getType())); + Constant *Folded = ConstantExpr::get(BO->getOpcode(), V, CI); - if (ConstantInt *FoldedCInt = dyn_cast(Folded)) - Result.push_back(std::make_pair(FoldedCInt, LHSVals[i].second)); - else if (isa(Folded)) - Result.push_back(std::make_pair((ConstantInt*)0, LHSVals[i].second)); + PushConstantIntOrUndef(Result, Folded, LHSVals[i].second); } } @@ -478,10 +476,8 @@ ComputeValueKnownInPredecessors(Value *V, BasicBlock *BB,PredValueInfo &Result){ Res = ConstantInt::get(Type::getInt1Ty(LHS->getContext()), ResT); } - if (isa(Res)) - Result.push_back(std::make_pair((ConstantInt*)0, PredBB)); - else if (ConstantInt *CI = dyn_cast(Res)) - Result.push_back(std::make_pair(CI, PredBB)); + if (Constant *ConstRes = dyn_cast(Res)) + PushConstantIntOrUndef(Result, ConstRes, PredBB); } return !Result.empty(); @@ -520,18 +516,11 @@ ComputeValueKnownInPredecessors(Value *V, BasicBlock *BB,PredValueInfo &Result){ ComputeValueKnownInPredecessors(I->getOperand(0), BB, LHSVals); for (unsigned i = 0, e = LHSVals.size(); i != e; ++i) { - Constant * Folded = 0; - if (LHSVals[i].first == 0) - Folded = ConstantExpr::getCompare(Cmp->getPredicate(), - UndefValue::get(CmpConst->getType()), CmpConst); - else - Folded = ConstantExpr::getCompare(Cmp->getPredicate(), - LHSVals[i].first, CmpConst); - - if (ConstantInt *FoldedCInt = dyn_cast(Folded)) - Result.push_back(std::make_pair(FoldedCInt, LHSVals[i].second)); - else if (isa(Folded)) - Result.push_back(std::make_pair((ConstantInt*)0,LHSVals[i].second)); + Constant *V = LHSVals[i].first ? LHSVals[i].first : + cast(UndefValue::get(CmpConst->getType())); + Constant *Folded = ConstantExpr::getCompare(Cmp->getPredicate(), + V, CmpConst); + PushConstantIntOrUndef(Result, Folded, LHSVals[i].second); } return !Result.empty(); @@ -1168,9 +1157,9 @@ bool JumpThreading::ProcessThreadableEdges(Value *Cond, BasicBlock *BB) { return false; SmallVector, 8> PredValues; - if (!ComputeValueKnownInPredecessors(Cond, BB, PredValues)) { + if (!ComputeValueKnownInPredecessors(Cond, BB, PredValues)) return false; - } + assert(!PredValues.empty() && "ComputeValueKnownInPredecessors returned true with no values");