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