add a new ValueState::getConstantInt() helper, use it to

simplify some code.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@85783 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2009-11-02 03:21:36 +00:00
parent bd5dc45296
commit 36c9952b1b

View File

@ -129,6 +129,14 @@ public:
return true; return true;
} }
/// getConstantInt - If this is a constant with a ConstantInt value, return it
/// otherwise return null.
ConstantInt *getConstantInt() const {
if (isConstant())
return dyn_cast<ConstantInt>(getConstant());
return 0;
}
void markForcedConstant(Constant *V) { void markForcedConstant(Constant *V) {
assert(isUndefined() && "Can't force a defined value!"); assert(isUndefined() && "Can't force a defined value!");
Val.setInt(forcedconstant); Val.setInt(forcedconstant);
@ -255,7 +263,7 @@ public:
return TrackedGlobals; return TrackedGlobals;
} }
inline void markOverdefined(Value *V) { void markOverdefined(Value *V) {
markOverdefined(ValueState[V], V); markOverdefined(ValueState[V], V);
} }
@ -307,7 +315,7 @@ private:
} }
void mergeInValue(Value *V, LatticeVal &MergeWithV) { void mergeInValue(Value *V, LatticeVal &MergeWithV) {
return mergeInValue(ValueState[V], V, MergeWithV); mergeInValue(ValueState[V], V, MergeWithV);
} }
@ -321,17 +329,16 @@ private:
std::map<Value*, LatticeVal>::iterator I = ValueState.find(V); std::map<Value*, LatticeVal>::iterator I = ValueState.find(V);
if (I != ValueState.end()) return I->second; // Common case, in the map if (I != ValueState.end()) return I->second; // Common case, in the map
LatticeVal &LV = ValueState[V];
if (Constant *C = dyn_cast<Constant>(V)) { if (Constant *C = dyn_cast<Constant>(V)) {
if (isa<UndefValue>(V)) { // Undef values remain undefined.
// Nothing to do, remain undefined. if (!isa<UndefValue>(V))
} else {
LatticeVal &LV = ValueState[C];
LV.markConstant(C); // Constants are constant LV.markConstant(C); // Constants are constant
return LV;
}
} }
// All others are underdefined by default. // All others are underdefined by default.
return ValueState[V]; return LV;
} }
// markEdgeExecutable - Mark a basic block as executable, adding it to the BB // markEdgeExecutable - Mark a basic block as executable, adding it to the BB
@ -442,21 +449,21 @@ void SCCPSolver::getFeasibleSuccessors(TerminatorInst &TI,
} }
LatticeVal &BCValue = getValueState(BI->getCondition()); LatticeVal &BCValue = getValueState(BI->getCondition());
if (BCValue.isOverdefined() || ConstantInt *CI = BCValue.getConstantInt();
(BCValue.isConstant() && !isa<ConstantInt>(BCValue.getConstant()))) { if (CI == 0) {
// Overdefined condition variables, and branches on unfoldable constant // Overdefined condition variables, and branches on unfoldable constant
// conditions, mean the branch could go either way. // conditions, mean the branch could go either way.
Succs[0] = Succs[1] = true; if (!BCValue.isUndefined())
Succs[0] = Succs[1] = true;
return; return;
} }
// Constant condition variables mean the branch can only go a single way. // Constant condition variables mean the branch can only go a single way.
if (BCValue.isConstant()) Succs[CI->isZero()] = true;
Succs[cast<ConstantInt>(BCValue.getConstant())->isZero()] = true;
return; return;
} }
if (isa<InvokeInst>(&TI)) { if (isa<InvokeInst>(TI)) {
// Invoke instructions successors are always executable. // Invoke instructions successors are always executable.
Succs[0] = Succs[1] = true; Succs[0] = Succs[1] = true;
return; return;
@ -464,12 +471,16 @@ void SCCPSolver::getFeasibleSuccessors(TerminatorInst &TI,
if (SwitchInst *SI = dyn_cast<SwitchInst>(&TI)) { if (SwitchInst *SI = dyn_cast<SwitchInst>(&TI)) {
LatticeVal &SCValue = getValueState(SI->getCondition()); LatticeVal &SCValue = getValueState(SI->getCondition());
if (SCValue.isOverdefined() || // Overdefined condition? ConstantInt *CI = SCValue.getConstantInt();
(SCValue.isConstant() && !isa<ConstantInt>(SCValue.getConstant()))) {
if (CI == 0) { // Overdefined or undefined condition?
// All destinations are executable! // All destinations are executable!
Succs.assign(TI.getNumSuccessors(), true); if (!SCValue.isUndefined())
} else if (SCValue.isConstant()) Succs.assign(TI.getNumSuccessors(), true);
Succs[SI->findCaseValue(cast<ConstantInt>(SCValue.getConstant()))] = true; return;
}
Succs[SI->findCaseValue(CI)] = true;
return; return;
} }
@ -506,15 +517,12 @@ bool SCCPSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To) {
// Overdefined condition variables mean the branch could go either way, // Overdefined condition variables mean the branch could go either way,
// undef conditions mean that neither edge is feasible yet. // undef conditions mean that neither edge is feasible yet.
if (!BCValue.isConstant()) ConstantInt *CI = BCValue.getConstantInt();
return BCValue.isOverdefined(); if (CI == 0)
return !BCValue.isUndefined();
// Not branching on an evaluatable constant?
if (!isa<ConstantInt>(BCValue.getConstant())) return true;
// Constant condition variables mean the branch can only go a single way. // Constant condition variables mean the branch can only go a single way.
bool CondIsFalse = cast<ConstantInt>(BCValue.getConstant())->isZero(); return BI->getSuccessor(CI->isZero()) == To;
return BI->getSuccessor(CondIsFalse) == To;
} }
// Invoke instructions successors are always executable. // Invoke instructions successors are always executable.
@ -523,24 +531,19 @@ bool SCCPSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To) {
if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
LatticeVal &SCValue = getValueState(SI->getCondition()); LatticeVal &SCValue = getValueState(SI->getCondition());
if (SCValue.isOverdefined()) { // Overdefined condition? ConstantInt *CI = SCValue.getConstantInt();
// All destinations are executable!
return true; if (CI == 0)
} else if (SCValue.isConstant()) { return !SCValue.isUndefined();
Constant *CPV = SCValue.getConstant();
if (!isa<ConstantInt>(CPV))
return true; // not a foldable constant?
// Make sure to skip the "default value" which isn't a value // Make sure to skip the "default value" which isn't a value
for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i) for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i)
if (SI->getSuccessorValue(i) == CPV) // Found the taken branch. if (SI->getSuccessorValue(i) == CI) // Found the taken branch.
return SI->getSuccessor(i) == To; return SI->getSuccessor(i) == To;
// If the constant value is not equal to any of the branches, we must // If the constant value is not equal to any of the branches, we must
// execute default branch. // execute default branch.
return SI->getDefaultDest() == To; return SI->getDefaultDest() == To;
}
return false;
} }
// Just mark all destinations executable! // Just mark all destinations executable!
@ -775,12 +778,11 @@ void SCCPSolver::visitSelectInst(SelectInst &I) {
LatticeVal &CondValue = getValueState(I.getCondition()); LatticeVal &CondValue = getValueState(I.getCondition());
if (CondValue.isUndefined()) if (CondValue.isUndefined())
return; return;
if (CondValue.isConstant()) {
if (ConstantInt *CondCB = dyn_cast<ConstantInt>(CondValue.getConstant())){ if (ConstantInt *CondCB = CondValue.getConstantInt()) {
mergeInValue(&I, getValueState(CondCB->getZExtValue() ? I.getTrueValue() mergeInValue(&I, getValueState(CondCB->getZExtValue() ? I.getTrueValue()
: I.getFalseValue())); : I.getFalseValue()));
return; return;
}
} }
// Otherwise, the condition is overdefined or a constant we can't evaluate. // Otherwise, the condition is overdefined or a constant we can't evaluate.
@ -839,8 +841,7 @@ void SCCPSolver::visitBinaryOperator(Instruction &I) {
if (NonOverdefVal->getConstant()->isNullValue()) if (NonOverdefVal->getConstant()->isNullValue())
return markConstant(IV, &I, NonOverdefVal->getConstant()); return markConstant(IV, &I, NonOverdefVal->getConstant());
} else { } else {
if (ConstantInt *CI = if (ConstantInt *CI = NonOverdefVal->getConstantInt())
dyn_cast<ConstantInt>(NonOverdefVal->getConstant()))
if (CI->isAllOnesValue()) // X or -1 = -1 if (CI->isAllOnesValue()) // X or -1 = -1
return markConstant(IV, &I, NonOverdefVal->getConstant()); return markConstant(IV, &I, NonOverdefVal->getConstant());
} }