Index: lib/CodeGen/InlineSpiller.cpp =================================================================== --- lib/CodeGen/InlineSpiller.cpp +++ lib/CodeGen/InlineSpiller.cpp @@ -72,8 +72,11 @@ InsertPointAnalysis IPA; - // Map from StackSlot to its original register. - DenseMap StackSlotToReg; + // Map from StackSlot to the LiveInterval of the original register. + // Note the LiveInterval of the original register may have been deleted + // after it is spilled. We keep a copy here to track the range where + // spills can be moved. + DenseMap> StackSlotToOrigLI; // Map from pair of (StackSlot and Original VNI) to a set of spills which // have the same stackslot and have equal values defined by Original VNI. // These spills are mergeable and are hoist candiates. @@ -86,8 +89,8 @@ /// sibling there and use it as the source of the new spill. DenseMap> Virt2SiblingsMap; - bool isSpillCandBB(unsigned OrigReg, VNInfo &OrigVNI, MachineBasicBlock &BB, - unsigned &LiveReg); + bool isSpillCandBB(LiveInterval &OrigLI, VNInfo &OrigVNI, + MachineBasicBlock &BB, unsigned &LiveReg); void rmRedundantSpills( SmallPtrSet &Spills, @@ -101,7 +104,7 @@ DenseMap &SpillsToKeep, DenseMap &SpillBBToSpill); - void runHoistSpills(unsigned OrigReg, VNInfo &OrigVNI, + void runHoistSpills(LiveInterval &OrigLI, VNInfo &OrigVNI, SmallPtrSet &Spills, SmallVectorImpl &SpillsToRm, DenseMap &SpillsToIns); @@ -1083,9 +1086,17 @@ /// void HoistSpillHelper::addToMergeableSpills(MachineInstr &Spill, int StackSlot, unsigned Original) { - StackSlotToReg[StackSlot] = Original; + BumpPtrAllocator &Allocator = LIS.getVNInfoAllocator(); + LiveInterval &OrigLI = LIS.getInterval(Original); + // save a copy of LiveInterval in StackSlotToOrigLI because the original + // LiveInterval may be cleared after all its references are spilled. + if (StackSlotToOrigLI.find(StackSlot) == StackSlotToOrigLI.end()) { + auto LI = llvm::make_unique(OrigLI.reg, OrigLI.weight); + LI->assign(OrigLI, Allocator); + StackSlotToOrigLI[StackSlot] = std::move(LI); + } SlotIndex Idx = LIS.getInstructionIndex(Spill); - VNInfo *OrigVNI = LIS.getInterval(Original).getVNInfoAt(Idx.getRegSlot()); + VNInfo *OrigVNI = StackSlotToOrigLI[StackSlot]->getVNInfoAt(Idx.getRegSlot()); std::pair MIdx = std::make_pair(StackSlot, OrigVNI); MergeableSpills[MIdx].insert(&Spill); } @@ -1095,11 +1106,11 @@ /// bool HoistSpillHelper::rmFromMergeableSpills(MachineInstr &Spill, int StackSlot) { - int Original = StackSlotToReg[StackSlot]; - if (!Original) + auto It = StackSlotToOrigLI.find(StackSlot); + if (It == StackSlotToOrigLI.end()) return false; SlotIndex Idx = LIS.getInstructionIndex(Spill); - VNInfo *OrigVNI = LIS.getInterval(Original).getVNInfoAt(Idx.getRegSlot()); + VNInfo *OrigVNI = It->second->getVNInfoAt(Idx.getRegSlot()); std::pair MIdx = std::make_pair(StackSlot, OrigVNI); return MergeableSpills[MIdx].erase(&Spill); } @@ -1107,18 +1118,17 @@ /// Check BB to see if it is a possible target BB to place a hoisted spill, /// i.e., there should be a living sibling of OrigReg at the insert point. /// -bool HoistSpillHelper::isSpillCandBB(unsigned OrigReg, VNInfo &OrigVNI, +bool HoistSpillHelper::isSpillCandBB(LiveInterval &OrigLI, VNInfo &OrigVNI, MachineBasicBlock &BB, unsigned &LiveReg) { SlotIndex Idx; - LiveInterval &OrigLI = LIS.getInterval(OrigReg); + unsigned OrigReg = OrigLI.reg; MachineBasicBlock::iterator MI = IPA.getLastInsertPointIter(OrigLI, BB); if (MI != BB.end()) Idx = LIS.getInstructionIndex(*MI); else Idx = LIS.getMBBEndIdx(&BB).getPrevSlot(); SmallSetVector &Siblings = Virt2SiblingsMap[OrigReg]; - assert((LIS.getInterval(OrigReg)).getVNInfoAt(Idx) == &OrigVNI && - "Unexpected VNI"); + assert(OrigLI.getVNInfoAt(Idx) == &OrigVNI && "Unexpected VNI"); for (auto const SibReg : Siblings) { LiveInterval &LI = LIS.getInterval(SibReg); @@ -1256,7 +1266,8 @@ /// \p SpillsToIns. /// void HoistSpillHelper::runHoistSpills( - unsigned OrigReg, VNInfo &OrigVNI, SmallPtrSet &Spills, + LiveInterval &OrigLI, VNInfo &OrigVNI, + SmallPtrSet &Spills, SmallVectorImpl &SpillsToRm, DenseMap &SpillsToIns) { // Visit order of dominator tree nodes. @@ -1331,7 +1342,7 @@ // Check whether Block is a possible candidate to insert spill. unsigned LiveReg = 0; - if (!isSpillCandBB(OrigReg, OrigVNI, *Block, LiveReg)) + if (!isSpillCandBB(OrigLI, OrigVNI, *Block, LiveReg)) continue; // If there are multiple spills that could be merged, bias a little @@ -1396,13 +1407,8 @@ SmallVector NewVRegs; LiveRangeEdit Edit(nullptr, NewVRegs, MF, LIS, &VRM, this); - // Save the mapping between stackslot and its original reg. - DenseMap SlotToOrigReg; for (unsigned i = 0, e = MRI.getNumVirtRegs(); i != e; ++i) { unsigned Reg = TargetRegisterInfo::index2VirtReg(i); - int Slot = VRM.getStackSlot(Reg); - if (Slot != VirtRegMap::NO_STACK_SLOT) - SlotToOrigReg[Slot] = VRM.getOriginal(Reg); unsigned Original = VRM.getPreSplitReg(Reg); if (!MRI.def_empty(Reg)) Virt2SiblingsMap[Original].insert(Reg); @@ -1411,8 +1417,7 @@ // Each entry in MergeableSpills contains a spill set with equal values. for (auto &Ent : MergeableSpills) { int Slot = Ent.first.first; - unsigned OrigReg = SlotToOrigReg[Slot]; - LiveInterval &OrigLI = LIS.getInterval(OrigReg); + LiveInterval &OrigLI = *StackSlotToOrigLI[Slot]; VNInfo *OrigVNI = Ent.first.second; SmallPtrSet &EqValSpills = Ent.second; if (Ent.second.empty()) @@ -1431,7 +1436,7 @@ // SpillsToIns is the spill set to be newly inserted after hoisting. DenseMap SpillsToIns; - runHoistSpills(OrigReg, *OrigVNI, EqValSpills, SpillsToRm, SpillsToIns); + runHoistSpills(OrigLI, *OrigVNI, EqValSpills, SpillsToRm, SpillsToIns); DEBUG({ dbgs() << "Finally inserted spills in BB: ";