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
This commit is contained in:
Owen Anderson 2010-08-31 20:26:04 +00:00
parent 9d4b51b696
commit 0eb355ab6b

View File

@ -276,6 +276,17 @@ void JumpThreading::FindLoopHeaders(Function &F) {
LoopHeaders.insert(const_cast<BasicBlock*>(Edges[i].second)); LoopHeaders.insert(const_cast<BasicBlock*>(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<std::pair<ConstantInt*,
BasicBlock*> > &Result,
Constant *Value, BasicBlock* BB){
if (ConstantInt *FoldedCInt = dyn_cast<ConstantInt>(Value))
Result.push_back(std::make_pair(FoldedCInt, BB));
else if (isa<UndefValue>(Value))
Result.push_back(std::make_pair((ConstantInt*)0, BB));
}
/// ComputeValueKnownInPredecessors - Given a basic block BB and a value V, see /// 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 /// 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 /// 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); PN->getIncomingBlock(i), BB);
// LVI returns null is no value could be determined. // LVI returns null is no value could be determined.
if (!CI) continue; if (!CI) continue;
if (ConstantInt *CInt = dyn_cast<ConstantInt>(CI)) PushConstantIntOrUndef(Result, CI, PN->getIncomingBlock(i));
Result.push_back(std::make_pair(CInt, PN->getIncomingBlock(i)));
else if (isa<UndefValue>(CI))
Result.push_back(std::make_pair((ConstantInt*)0,
PN->getIncomingBlock(i)));
} }
} }
@ -428,26 +435,17 @@ ComputeValueKnownInPredecessors(Value *V, BasicBlock *BB,PredValueInfo &Result){
// Try to simplify some other binary operator values. // Try to simplify some other binary operator values.
} else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) { } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) {
ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1)); if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1))) {
if (CI) {
SmallVector<std::pair<ConstantInt*, BasicBlock*>, 8> LHSVals; SmallVector<std::pair<ConstantInt*, BasicBlock*>, 8> LHSVals;
ComputeValueKnownInPredecessors(BO->getOperand(0), BB, LHSVals); ComputeValueKnownInPredecessors(BO->getOperand(0), BB, LHSVals);
// Try to use constant folding to simplify the binary operator. // Try to use constant folding to simplify the binary operator.
for (unsigned i = 0, e = LHSVals.size(); i != e; ++i) { for (unsigned i = 0, e = LHSVals.size(); i != e; ++i) {
Constant *Folded = 0; Constant *V = LHSVals[i].first ? LHSVals[i].first :
if (LHSVals[i].first == 0) { cast<Constant>(UndefValue::get(BO->getType()));
Folded = ConstantExpr::get(BO->getOpcode(), Constant *Folded = ConstantExpr::get(BO->getOpcode(), V, CI);
UndefValue::get(BO->getType()),
CI);
} else {
Folded = ConstantExpr::get(BO->getOpcode(), LHSVals[i].first, CI);
}
if (ConstantInt *FoldedCInt = dyn_cast<ConstantInt>(Folded)) PushConstantIntOrUndef(Result, Folded, LHSVals[i].second);
Result.push_back(std::make_pair(FoldedCInt, LHSVals[i].second));
else if (isa<UndefValue>(Folded))
Result.push_back(std::make_pair((ConstantInt*)0, LHSVals[i].second));
} }
} }
@ -478,10 +476,8 @@ ComputeValueKnownInPredecessors(Value *V, BasicBlock *BB,PredValueInfo &Result){
Res = ConstantInt::get(Type::getInt1Ty(LHS->getContext()), ResT); Res = ConstantInt::get(Type::getInt1Ty(LHS->getContext()), ResT);
} }
if (isa<UndefValue>(Res)) if (Constant *ConstRes = dyn_cast<Constant>(Res))
Result.push_back(std::make_pair((ConstantInt*)0, PredBB)); PushConstantIntOrUndef(Result, ConstRes, PredBB);
else if (ConstantInt *CI = dyn_cast<ConstantInt>(Res))
Result.push_back(std::make_pair(CI, PredBB));
} }
return !Result.empty(); return !Result.empty();
@ -520,18 +516,11 @@ ComputeValueKnownInPredecessors(Value *V, BasicBlock *BB,PredValueInfo &Result){
ComputeValueKnownInPredecessors(I->getOperand(0), BB, LHSVals); ComputeValueKnownInPredecessors(I->getOperand(0), BB, LHSVals);
for (unsigned i = 0, e = LHSVals.size(); i != e; ++i) { for (unsigned i = 0, e = LHSVals.size(); i != e; ++i) {
Constant * Folded = 0; Constant *V = LHSVals[i].first ? LHSVals[i].first :
if (LHSVals[i].first == 0) cast<Constant>(UndefValue::get(CmpConst->getType()));
Folded = ConstantExpr::getCompare(Cmp->getPredicate(), Constant *Folded = ConstantExpr::getCompare(Cmp->getPredicate(),
UndefValue::get(CmpConst->getType()), CmpConst); V, CmpConst);
else PushConstantIntOrUndef(Result, Folded, LHSVals[i].second);
Folded = ConstantExpr::getCompare(Cmp->getPredicate(),
LHSVals[i].first, CmpConst);
if (ConstantInt *FoldedCInt = dyn_cast<ConstantInt>(Folded))
Result.push_back(std::make_pair(FoldedCInt, LHSVals[i].second));
else if (isa<UndefValue>(Folded))
Result.push_back(std::make_pair((ConstantInt*)0,LHSVals[i].second));
} }
return !Result.empty(); return !Result.empty();
@ -1168,9 +1157,9 @@ bool JumpThreading::ProcessThreadableEdges(Value *Cond, BasicBlock *BB) {
return false; return false;
SmallVector<std::pair<ConstantInt*, BasicBlock*>, 8> PredValues; SmallVector<std::pair<ConstantInt*, BasicBlock*>, 8> PredValues;
if (!ComputeValueKnownInPredecessors(Cond, BB, PredValues)) { if (!ComputeValueKnownInPredecessors(Cond, BB, PredValues))
return false; return false;
}
assert(!PredValues.empty() && assert(!PredValues.empty() &&
"ComputeValueKnownInPredecessors returned true with no values"); "ComputeValueKnownInPredecessors returned true with no values");