Index: llvm/lib/CodeGen/StackSlotColoring.cpp =================================================================== --- llvm/lib/CodeGen/StackSlotColoring.cpp +++ llvm/lib/CodeGen/StackSlotColoring.cpp @@ -60,6 +60,7 @@ class StackSlotColoring : public MachineFunctionPass { LiveStacks* LS; + SlotIndexes *Indexes; MachineFrameInfo *MFI; const TargetInstrInfo *TII; const MachineBlockFrequencyInfo *MBFI; @@ -72,6 +73,8 @@ // to be careful that renames like [FI0, FI1] -> [FI1, FI2] do not // become FI0 -> FI1 -> FI2. SmallVector, 16> SSRefs; + DenseMap> SS2DbgValues; + DenseMap SS2LI; // OrigAlignments - Alignments of stack objects before coloring. SmallVector OrigAlignments; @@ -123,6 +126,7 @@ void RewriteInstruction(MachineInstr &MI, SmallVectorImpl &SlotMapping, MachineFunction &MF); bool RemoveDeadStores(MachineBasicBlock* MBB); + void TerminateDbgForSlot(unsigned FI, LiveInterval *LI); }; } // end anonymous namespace @@ -303,6 +307,9 @@ Assignments[Color].push_back(li); LLVM_DEBUG(dbgs() << "Assigning fi#" << FI << " to fi#" << Color << "\n"); + if (FI != Color) + TerminateDbgForSlot(Color, SS2LI[Color]); + // Change size and alignment of the allocated slot. If there are multiple // objects sharing the same slot, then make sure the size and alignment // are large enough for all. @@ -476,6 +483,24 @@ return changed; } +/// Terminate the DBG_VALUE(s) of variables residing in stack-slot with +/// frame-index FI at the Segment.end points of LI. +void StackSlotColoring::TerminateDbgForSlot(unsigned FI, LiveInterval *LI) { + for (auto &Seg : *LI) { + if (auto *SegEndMI = Indexes->getInstructionFromIndex(Seg.end)) { + for (auto *DbgValue : SS2DbgValues[FI]) { + auto ItMI = SegEndMI->getIterator(); + BuildMI(*SegEndMI->getParent(), ItMI, DbgValue->getDebugLoc(), + TII->get(TargetOpcode::DBG_VALUE)) + .addReg(0) // $noreg + .add(DbgValue->getOperand(1)) + .add(DbgValue->getOperand(2)) + .add(DbgValue->getOperand(3)); + } + } + } +} + bool StackSlotColoring::runOnMachineFunction(MachineFunction &MF) { LLVM_DEBUG({ dbgs() << "********** Stack Slot Coloring **********\n" @@ -489,6 +514,7 @@ TII = MF.getSubtarget().getInstrInfo(); LS = &getAnalysis(); MBFI = &getAnalysis(); + Indexes = &getAnalysis(); bool Changed = false; @@ -507,6 +533,24 @@ // Gather spill slot references ScanForSpillSlotRefs(MF); InitializeSlots(); + + // Create mapping of SS -> LiveInterval. + for (unsigned i = 0; i < SSIntervals.size(); i++) { + LiveInterval *li = SSIntervals[i]; + int SS = Register::stackSlot2Index(li->reg()); + SS2LI[SS] = li; + } + + // Create mapping of SS -> DBG_VALUE(s). + for (MachineBasicBlock &MBB : MF) { + for (MachineInstr &MI : MBB) { + if (MI.isDebugValue() && MI.getOperand(0).isFI()) { + auto SS = MI.getOperand(0).getIndex(); + SS2DbgValues[SS].push_back(&MI); + } + } + } + Changed = ColorSlots(MF); for (int &Next : NextColors)