LowerSwitch: Use ConstantInt for CaseRange::{Low,High}

Case values are always ConstantInt. This allows us to remove
a bunch of casts. NFC.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@228312 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Hans Wennborg 2015-02-05 16:58:10 +00:00
parent 5243154a6a
commit 2cdfb4b5da

View File

@ -67,11 +67,11 @@ namespace {
}
struct CaseRange {
Constant* Low;
Constant* High;
ConstantInt* Low;
ConstantInt* High;
BasicBlock* BB;
CaseRange(Constant *low, Constant *high, BasicBlock *bb)
CaseRange(ConstantInt *low, ConstantInt *high, BasicBlock *bb)
: Low(low), High(high), BB(bb) {}
};
@ -220,14 +220,14 @@ LowerSwitch::switchConvert(CaseItr Begin, CaseItr End, ConstantInt *LowerBound,
CaseRange &Pivot = *(Begin + Mid);
DEBUG(dbgs() << "Pivot ==> "
<< cast<ConstantInt>(Pivot.Low)->getValue()
<< " -" << cast<ConstantInt>(Pivot.High)->getValue() << "\n");
<< Pivot.Low->getValue()
<< " -" << Pivot.High->getValue() << "\n");
// NewLowerBound here should never be the integer minimal value.
// This is because it is computed from a case range that is never
// the smallest, so there is always a case range that has at least
// a smaller value.
ConstantInt *NewLowerBound = cast<ConstantInt>(Pivot.Low);
ConstantInt *NewLowerBound = Pivot.Low;
// Because NewLowerBound is never the smallest representable integer
// it is safe here to subtract one.
@ -236,16 +236,16 @@ LowerSwitch::switchConvert(CaseItr Begin, CaseItr End, ConstantInt *LowerBound,
if (!UnreachableRanges.empty()) {
// Check if the gap between LHS's highest and NewLowerBound is unreachable.
int64_t GapLow = cast<ConstantInt>(LHS.back().High)->getSExtValue() + 1;
int64_t GapLow = LHS.back().High->getSExtValue() + 1;
int64_t GapHigh = NewLowerBound->getSExtValue() - 1;
IntRange Gap = { GapLow, GapHigh };
if (GapHigh >= GapLow && IsInRanges(Gap, UnreachableRanges))
NewUpperBound = cast<ConstantInt>(LHS.back().High);
NewUpperBound = LHS.back().High;
}
DEBUG(dbgs() << "LHS Bounds ==> ";
if (LowerBound) {
dbgs() << cast<ConstantInt>(LowerBound)->getSExtValue();
dbgs() << LowerBound->getSExtValue();
} else {
dbgs() << "NONE";
}
@ -253,7 +253,7 @@ LowerSwitch::switchConvert(CaseItr Begin, CaseItr End, ConstantInt *LowerBound,
dbgs() << "RHS Bounds ==> ";
dbgs() << NewLowerBound->getSExtValue() << " - ";
if (UpperBound) {
dbgs() << cast<ConstantInt>(UpperBound)->getSExtValue() << "\n";
dbgs() << UpperBound->getSExtValue() << "\n";
} else {
dbgs() << "NONE\n";
});
@ -304,11 +304,11 @@ BasicBlock* LowerSwitch::newLeafBlock(CaseRange& Leaf, Value* Val,
Leaf.Low, "SwitchLeaf");
} else {
// Make range comparison
if (cast<ConstantInt>(Leaf.Low)->isMinValue(true /*isSigned*/)) {
if (Leaf.Low->isMinValue(true /*isSigned*/)) {
// Val >= Min && Val <= Hi --> Val <= Hi
Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_SLE, Val, Leaf.High,
"SwitchLeaf");
} else if (cast<ConstantInt>(Leaf.Low)->isZero()) {
} else if (Leaf.Low->isZero()) {
// Val >= 0 && Val <= Hi --> Val <=u Hi
Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_ULE, Val, Leaf.High,
"SwitchLeaf");
@ -333,8 +333,8 @@ BasicBlock* LowerSwitch::newLeafBlock(CaseRange& Leaf, Value* Val,
for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
PHINode* PN = cast<PHINode>(I);
// Remove all but one incoming entries from the cluster
uint64_t Range = cast<ConstantInt>(Leaf.High)->getSExtValue() -
cast<ConstantInt>(Leaf.Low)->getSExtValue();
uint64_t Range = Leaf.High->getSExtValue() -
Leaf.Low->getSExtValue();
for (uint64_t j = 0; j < Range; ++j) {
PN->removeIncomingValue(OrigBlock);
}
@ -362,8 +362,8 @@ unsigned LowerSwitch::Clusterify(CaseVector& Cases, SwitchInst *SI) {
if (Cases.size()>=2)
for (CaseItr I = Cases.begin(), J = std::next(Cases.begin());
J != Cases.end();) {
int64_t nextValue = cast<ConstantInt>(J->Low)->getSExtValue();
int64_t currentValue = cast<ConstantInt>(I->High)->getSExtValue();
int64_t nextValue = J->Low->getSExtValue();
int64_t currentValue = I->High->getSExtValue();
BasicBlock* nextBB = J->BB;
BasicBlock* currentBB = I->BB;
@ -420,8 +420,8 @@ void LowerSwitch::processSwitchInst(SwitchInst *SI) {
// know that the value passed to the switch must be exactly one of the case
// values.
assert(!Cases.empty());
LowerBound = cast<ConstantInt>(Cases.front().Low);
UpperBound = cast<ConstantInt>(Cases.back().High);
LowerBound = Cases.front().Low;
UpperBound = Cases.back().High;
DenseMap<BasicBlock *, unsigned> Popularity;
unsigned MaxPop = 0;
@ -430,8 +430,8 @@ void LowerSwitch::processSwitchInst(SwitchInst *SI) {
IntRange R = { INT64_MIN, INT64_MAX };
UnreachableRanges.push_back(R);
for (const auto &I : Cases) {
int64_t Low = cast<ConstantInt>(I.Low)->getSExtValue();
int64_t High = cast<ConstantInt>(I.High)->getSExtValue();
int64_t Low = I.Low->getSExtValue();
int64_t High = I.High->getSExtValue();
IntRange &LastRange = UnreachableRanges.back();
if (LastRange.Low == Low) {