- else {
- std::set<unsigned> toSpill;
- toSpill.insert(minReg);
- for (const unsigned* as = mri_->getAliasSet(minReg); *as; ++as)
- toSpill.insert(*as);
-
- std::vector<unsigned> spilled;
- for (IntervalPtrs::iterator i = active_.begin();
- i != active_.end(); ) {
- unsigned reg = (*i)->reg;
- if (reg >= MRegisterInfo::FirstVirtualRegister &&
- toSpill.find(v2pMap_[reg]) != toSpill.end() &&
- cur->overlaps(**i)) {
- spilled.push_back(v2pMap_[reg]);
- DEBUG(std::cerr << "\t\t\t\tspilling : " << **i << '\n');
- assignVirt2StackSlot(reg);
- i = active_.erase(i);
+
+ // 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(MRegisterInfo::isPhysicalRegister(minReg) &&
+ "did not choose a register to spill?");
+ std::vector<bool> toSpill(mri_->getNumRegs(), false);
+ // we are going to spill minReg and all its aliases
+ toSpill[minReg] = true;
+ for (const unsigned* as = mri_->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->start();
+
+ // set of spilled vregs (used later to rollback properly)
+ std::set<unsigned> 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)->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);
+ }
+ }
+
+ DEBUG(std::cerr << "\t\trolling back to: " << earliestStart << '\n');
+ // scan handled in reverse order up to the earliaset start of a
+ // spilled live interval and undo each one, restoring the state of
+ // unhandled
+ while (!handled_.empty()) {
+ LiveInterval* i = handled_.back();
+ // if this interval starts before t we are done
+ if (i->start() < earliestStart)
+ break;
+ DEBUG(std::cerr << "\t\t\tundo changes for: " << *i << '\n');
+ handled_.pop_back();
+ // when undoing a live interval allocation we must know if it
+ // is active or inactive to properly update the PhysRegTracker
+ // and the VirtRegMap
+ IntervalPtrs::iterator it;
+ if ((it = find(active_.begin(), active_.end(), i)) != active_.end()) {
+ active_.erase(it);
+ if (MRegisterInfo::isPhysicalRegister(i->reg)) {
+ prt_->delRegUse(i->reg);
+ unhandled_.push(i);