- // push the current interval back to unhandled since we are going
- // to re-run at least this iteration. Since we didn't modify it it
- // should go back right in the front of the list
- unhandled_.push_front(cur);
-
- // otherwise we spill all intervals aliasing the register with
- // minimum weight, rollback to the interval with the earliest
- // start point and let the linear scan algorithm run again
- std::vector<LiveInterval*> added;
- assert(MRegisterInfo::isPhysicalRegister(minReg) &&
- "did not choose a register to spill?");
- std::vector<bool> toSpill(mri_->getNumRegs(), false);
- toSpill[minReg] = true;
- for (const unsigned* as = mri_->getAliasSet(minReg); *as; ++as)
- toSpill[*as] = true;
- unsigned earliestStart = cur->start();
-
- std::set<unsigned> spilled;
-
- for (IntervalPtrs::iterator i = active_.begin(); i != active_.end(); ++i) {
- unsigned reg = (*i)->reg;
- if (MRegisterInfo::isVirtualRegister(reg) &&
- toSpill[vrm_->getPhys(reg)] &&
- cur->overlaps(**i)) {
- DEBUG(std::cerr << "\t\t\tspilling(a): " << **i << '\n');
- earliestStart = std::min(earliestStart, (*i)->start());
- int slot = vrm_->assignVirt2StackSlot((*i)->reg);
- std::vector<LiveInterval*> newIs =
- li_->addIntervalsForSpills(**i, *vrm_, slot);
- std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
- spilled.insert(reg);
- }
- }
- for (IntervalPtrs::iterator i = inactive_.begin();
- i != inactive_.end(); ++i) {
- unsigned reg = (*i)->reg;
- if (MRegisterInfo::isVirtualRegister(reg) &&
- toSpill[vrm_->getPhys(reg)] &&
- cur->overlaps(**i)) {
- DEBUG(std::cerr << "\t\t\tspilling(i): " << **i << '\n');
- earliestStart = std::min(earliestStart, (*i)->start());
- int slot = vrm_->assignVirt2StackSlot((*i)->reg);
- std::vector<LiveInterval*> newIs =
- li_->addIntervalsForSpills(**i, *vrm_, slot);
- std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
- spilled.insert(reg);
- }
+ // All registers must have inf weight. Just grab one!
+ if (!minReg)
+ minReg = *RC->allocation_order_begin(*mf_);
+ }
+
+ DOUT << "\t\tregister with min weight: "
+ << tri_->getName(minReg) << " (" << minWeight << ")\n";
+
+ // if the current has the minimum weight, we need to spill it and
+ // add any added intervals back to unhandled, and restart
+ // linearscan.
+ if (cur->weight != HUGE_VALF && cur->weight <= minWeight) {
+ DOUT << "\t\t\tspilling(c): " << *cur << '\n';
+ std::vector<LiveInterval*> added =
+ li_->addIntervalsForSpills(*cur, loopInfo, *vrm_);
+ if (added.empty())
+ return; // Early exit if all spills were folded.
+
+ // Merge added with unhandled. Note that we know that
+ // addIntervalsForSpills returns intervals sorted by their starting
+ // point.
+ for (unsigned i = 0, e = added.size(); i != e; ++i)
+ unhandled_.push(added[i]);
+ return;
+ }
+
+ ++NumBacktracks;
+
+ // push the current interval back to unhandled since we are going
+ // to re-run at least this iteration. Since we didn't modify it it
+ // should go back right in the front of the list
+ unhandled_.push(cur);
+
+ // otherwise we spill all intervals aliasing the register with
+ // minimum weight, rollback to the interval with the earliest
+ // start point and let the linear scan algorithm run again
+ std::vector<LiveInterval*> added;
+ assert(TargetRegisterInfo::isPhysicalRegister(minReg) &&
+ "did not choose a register to spill?");
+ BitVector toSpill(tri_->getNumRegs());
+
+ // We are going to spill minReg and all its aliases.
+ toSpill[minReg] = true;
+ for (const unsigned* as = tri_->getAliasSet(minReg); *as; ++as)
+ toSpill[*as] = true;
+
+ // the earliest start of a spilled interval indicates up to where
+ // in handled we need to roll back
+ unsigned earliestStart = cur->beginNumber();
+
+ // set of spilled vregs (used later to rollback properly)
+ SmallSet<unsigned, 32> spilled;
+
+ // spill live intervals of virtual regs mapped to the physical register we
+ // want to clear (and its aliases). We only spill those that overlap with the
+ // current interval as the rest do not affect its allocation. we also keep
+ // track of the earliest start of all spilled live intervals since this will
+ // mark our rollback point.
+ for (IntervalPtrs::iterator i = active_.begin(); i != active_.end(); ++i) {
+ unsigned reg = i->first->reg;
+ if (//TargetRegisterInfo::isVirtualRegister(reg) &&
+ toSpill[vrm_->getPhys(reg)] &&
+ cur->overlapsFrom(*i->first, i->second)) {
+ DOUT << "\t\t\tspilling(a): " << *i->first << '\n';
+ earliestStart = std::min(earliestStart, i->first->beginNumber());
+ std::vector<LiveInterval*> newIs =
+ li_->addIntervalsForSpills(*i->first, loopInfo, *vrm_);
+ std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
+ spilled.insert(reg);