Commit 541ba667 by Jim Stichnoth

Subzero: Improve regalloc performance by optimizing UnhandledPrecolored.

A lot of time was being spent in the two loops that check precolored ranges in the Unhandled set, specifically in the endsBefore() check. Solve this by keeping a shadow copy of Unhandled, restricted to the ranges that are precolored. BUG= none R=jvoung@chromium.org Review URL: https://codereview.chromium.org/622553003
parent 0795ba01
...@@ -68,6 +68,7 @@ void LinearScan::scan(const llvm::SmallBitVector &RegMaskFull) { ...@@ -68,6 +68,7 @@ void LinearScan::scan(const llvm::SmallBitVector &RegMaskFull) {
TimerMarker T(IDscan, Func->getContext()); TimerMarker T(IDscan, Func->getContext());
assert(RegMaskFull.any()); // Sanity check assert(RegMaskFull.any()); // Sanity check
Unhandled.clear(); Unhandled.clear();
UnhandledPrecolored.clear();
Handled.clear(); Handled.clear();
Inactive.clear(); Inactive.clear();
Active.clear(); Active.clear();
...@@ -97,10 +98,12 @@ void LinearScan::scan(const llvm::SmallBitVector &RegMaskFull) { ...@@ -97,10 +98,12 @@ void LinearScan::scan(const llvm::SmallBitVector &RegMaskFull) {
// it was never referenced. // it was never referenced.
if (Var->getLiveRange().isEmpty()) if (Var->getLiveRange().isEmpty())
continue; continue;
Unhandled.insert(LiveRangeWrapper(Var)); LiveRangeWrapper R(Var);
Unhandled.insert(R);
if (Var->hasReg()) { if (Var->hasReg()) {
Var->setRegNumTmp(Var->getRegNum()); Var->setRegNumTmp(Var->getRegNum());
Var->setLiveRangeInfiniteWeight(); Var->setLiveRangeInfiniteWeight();
UnhandledPrecolored.insert(R);
} }
} }
} }
...@@ -145,6 +148,9 @@ void LinearScan::scan(const llvm::SmallBitVector &RegMaskFull) { ...@@ -145,6 +148,9 @@ void LinearScan::scan(const llvm::SmallBitVector &RegMaskFull) {
Active.push_back(Cur); Active.push_back(Cur);
assert(RegUses[RegNum] >= 0); assert(RegUses[RegNum] >= 0);
++RegUses[RegNum]; ++RegUses[RegNum];
assert(!UnhandledPrecolored.empty());
assert(UnhandledPrecolored.begin()->Var == Cur.Var);
UnhandledPrecolored.erase(UnhandledPrecolored.begin());
continue; continue;
} }
...@@ -306,19 +312,25 @@ void LinearScan::scan(const llvm::SmallBitVector &RegMaskFull) { ...@@ -306,19 +312,25 @@ void LinearScan::scan(const llvm::SmallBitVector &RegMaskFull) {
} }
} }
// Remove registers from the Free[] list where an Unhandled range std::vector<RegWeight> Weights(RegMask.size());
// overlaps with the current range and is precolored.
// Cur.endsBefore(Item) is an early exit check that turns a // Remove registers from the Free[] list where an Unhandled
// guaranteed O(N^2) algorithm into expected linear complexity. // precolored range overlaps with the current range, and set those
llvm::SmallBitVector PrecoloredUnhandled(RegMask.size()); // registers to infinite weight so that they aren't candidates for
// Note: PrecoloredUnhandled is only used for dumping. // eviction. Cur.endsBefore(Item) is an early exit check that
for (const LiveRangeWrapper &Item : Unhandled) { // turns a guaranteed O(N^2) algorithm into expected linear
// complexity.
llvm::SmallBitVector PrecoloredUnhandledMask(RegMask.size());
// Note: PrecoloredUnhandledMask is only used for dumping.
for (const LiveRangeWrapper &Item : UnhandledPrecolored) {
assert(Item.Var->hasReg());
if (Cur.endsBefore(Item)) if (Cur.endsBefore(Item))
break; break;
if (Item.Var->hasReg() && Item.overlaps(Cur)) { if (Item.overlaps(Cur)) {
int32_t ItemReg = Item.Var->getRegNum(); // Note: not getRegNumTmp() int32_t ItemReg = Item.Var->getRegNum(); // Note: not getRegNumTmp()
Weights[ItemReg].setWeight(RegWeight::Inf);
Free[ItemReg] = false; Free[ItemReg] = false;
PrecoloredUnhandled[ItemReg] = true; PrecoloredUnhandledMask[ItemReg] = true;
// Disable AllowOverlap if the preferred register is one of // Disable AllowOverlap if the preferred register is one of
// these precolored unhandled overlapping ranges. // these precolored unhandled overlapping ranges.
if (AllowOverlap && ItemReg == PreferReg) { if (AllowOverlap && ItemReg == PreferReg) {
...@@ -334,7 +346,7 @@ void LinearScan::scan(const llvm::SmallBitVector &RegMaskFull) { ...@@ -334,7 +346,7 @@ void LinearScan::scan(const llvm::SmallBitVector &RegMaskFull) {
if (RegMask[i]) { if (RegMask[i]) {
Str << Func->getTarget()->getRegName(i, IceType_i32) Str << Func->getTarget()->getRegName(i, IceType_i32)
<< "(U=" << RegUses[i] << ",F=" << Free[i] << "(U=" << RegUses[i] << ",F=" << Free[i]
<< ",P=" << PrecoloredUnhandled[i] << ") "; << ",P=" << PrecoloredUnhandledMask[i] << ") ";
} }
} }
Str << "\n"; Str << "\n";
...@@ -369,7 +381,6 @@ void LinearScan::scan(const llvm::SmallBitVector &RegMaskFull) { ...@@ -369,7 +381,6 @@ void LinearScan::scan(const llvm::SmallBitVector &RegMaskFull) {
} else { } else {
// Fallback: there are no free registers, so we look for the // Fallback: there are no free registers, so we look for the
// lowest-weight register and see if Cur has higher weight. // lowest-weight register and see if Cur has higher weight.
std::vector<RegWeight> Weights(RegMask.size());
// Check Active ranges. // Check Active ranges.
for (const LiveRangeWrapper &Item : Active) { for (const LiveRangeWrapper &Item : Active) {
assert(Item.overlaps(Cur)); assert(Item.overlaps(Cur));
...@@ -384,18 +395,6 @@ void LinearScan::scan(const llvm::SmallBitVector &RegMaskFull) { ...@@ -384,18 +395,6 @@ void LinearScan::scan(const llvm::SmallBitVector &RegMaskFull) {
if (Item.overlaps(Cur)) if (Item.overlaps(Cur))
Weights[RegNum].addWeight(Item.range().getWeight()); Weights[RegNum].addWeight(Item.range().getWeight());
} }
// Check Unhandled ranges that overlap Cur and are precolored.
// Cur.endsBefore(*I) is an early exit check that turns a
// guaranteed O(N^2) algorithm into expected linear complexity.
for (const LiveRangeWrapper &Item : Unhandled) {
if (Cur.endsBefore(Item))
break;
int32_t RegNum = Item.Var->getRegNumTmp();
if (RegNum < 0)
continue;
if (Item.overlaps(Cur))
Weights[RegNum].setWeight(RegWeight::Inf);
}
// All the weights are now calculated. Find the register with // All the weights are now calculated. Find the register with
// smallest weight. // smallest weight.
......
...@@ -71,6 +71,9 @@ private: ...@@ -71,6 +71,9 @@ private:
typedef std::set<LiveRangeWrapper, RangeCompare> OrderedRanges; typedef std::set<LiveRangeWrapper, RangeCompare> OrderedRanges;
typedef std::list<LiveRangeWrapper> UnorderedRanges; typedef std::list<LiveRangeWrapper> UnorderedRanges;
OrderedRanges Unhandled; OrderedRanges Unhandled;
// UnhandledPrecolored is a subset of Unhandled, specially collected
// for faster processing.
OrderedRanges UnhandledPrecolored;
UnorderedRanges Active, Inactive, Handled; UnorderedRanges Active, Inactive, Handled;
LinearScan(const LinearScan &) = delete; LinearScan(const LinearScan &) = delete;
LinearScan &operator=(const LinearScan &) = delete; LinearScan &operator=(const LinearScan &) = delete;
......
...@@ -4305,6 +4305,8 @@ Variable *TargetX8632::makeReg(Type Type, int32_t RegNum) { ...@@ -4305,6 +4305,8 @@ Variable *TargetX8632::makeReg(Type Type, int32_t RegNum) {
void TargetX8632::postLower() { void TargetX8632::postLower() {
if (Ctx->getOptLevel() != Opt_m1) if (Ctx->getOptLevel() != Opt_m1)
return; return;
static TimerIdT IDpostLower = GlobalContext::getTimerID("postLower");
TimerMarker T(IDpostLower, Ctx);
// TODO: Avoid recomputing WhiteList every instruction. // TODO: Avoid recomputing WhiteList every instruction.
RegSetMask RegInclude = RegSet_All; RegSetMask RegInclude = RegSet_All;
RegSetMask RegExclude = RegSet_StackPointer; RegSetMask RegExclude = RegSet_StackPointer;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment