@@ -108,8 +108,8 @@ class LDVImpl;
108108// / held by the same virtual register. The equivalence class is the transitive
109109// / closure of that relation.
110110class UserValue {
111- const MDNode *Variable; // /< The debug info variable we are part of.
112- const MDNode *Expression; // /< Any complex address expression.
111+ const DILocalVariable *Variable; // /< The debug info variable we are part of.
112+ const DIExpression *Expression; // /< Any complex address expression.
113113 bool IsIndirect; // /< true if this is a register-indirect+offset value.
114114 DebugLoc dl; // /< The debug location for the variable. This is
115115 // /< used by dwarf writer to find lexical scope.
@@ -127,8 +127,9 @@ class UserValue {
127127 SmallSet<SlotIndex, 2 > trimmedDefs;
128128
129129 // / insertDebugValue - Insert a DBG_VALUE into MBB at Idx for LocNo.
130- void insertDebugValue (MachineBasicBlock *MBB, SlotIndex Idx, unsigned LocNo,
131- LiveIntervals &LIS, const TargetInstrInfo &TII);
130+ void insertDebugValue (MachineBasicBlock *MBB, SlotIndex Idx,
131+ unsigned LocNo, bool Spilled, LiveIntervals &LIS,
132+ const TargetInstrInfo &TII);
132133
133134 // / splitLocation - Replace OldLocNo ranges with NewRegs ranges where NewRegs
134135 // / is live. Returns true if any changes were made.
@@ -137,8 +138,8 @@ class UserValue {
137138
138139public:
139140 // / UserValue - Create a new UserValue.
140- UserValue (const MDNode *var, const MDNode *expr, bool i, DebugLoc L ,
141- LocMap::Allocator &alloc)
141+ UserValue (const DILocalVariable *var, const DIExpression *expr, bool i,
142+ DebugLoc L, LocMap::Allocator &alloc)
142143 : Variable(var), Expression(expr), IsIndirect(i), dl(std::move(L)),
143144 leader (this ), locInts(alloc) {}
144145
@@ -154,8 +155,8 @@ class UserValue {
154155 UserValue *getNext () const { return next; }
155156
156157 // / match - Does this UserValue match the parameters?
157- bool match (const MDNode *Var, const MDNode *Expr, const DILocation *IA ,
158- bool indirect) const {
158+ bool match (const DILocalVariable *Var, const DIExpression *Expr,
159+ const DILocation *IA, bool indirect) const {
159160 return Var == Variable && Expr == Expression && dl->getInlinedAt () == IA &&
160161 indirect == IsIndirect;
161162 }
@@ -259,12 +260,14 @@ class UserValue {
259260 LiveIntervals &LIS);
260261
261262 // / rewriteLocations - Rewrite virtual register locations according to the
262- // / provided virtual register map.
263- void rewriteLocations (VirtRegMap &VRM, const TargetRegisterInfo &TRI);
263+ // / provided virtual register map. Record which locations were spilled.
264+ void rewriteLocations (VirtRegMap &VRM, const TargetRegisterInfo &TRI,
265+ BitVector &SpilledLocations);
264266
265267 // / emitDebugValues - Recreate DBG_VALUE instruction from data structures.
266- void emitDebugValues (VirtRegMap *VRM,
267- LiveIntervals &LIS, const TargetInstrInfo &TRI);
268+ void emitDebugValues (VirtRegMap *VRM, LiveIntervals &LIS,
269+ const TargetInstrInfo &TRI,
270+ const BitVector &SpilledLocations);
268271
269272 // / getDebugLoc - Return DebugLoc of this UserValue.
270273 DebugLoc getDebugLoc () { return dl;}
@@ -294,11 +297,11 @@ class LDVImpl {
294297 VRMap virtRegToEqClass;
295298
296299 // / Map user variable to eq class leader.
297- using UVMap = DenseMap<const MDNode *, UserValue *>;
300+ using UVMap = DenseMap<const DILocalVariable *, UserValue *>;
298301 UVMap userVarMap;
299302
300303 // / getUserValue - Find or create a UserValue.
301- UserValue *getUserValue (const MDNode *Var, const MDNode *Expr,
304+ UserValue *getUserValue (const DILocalVariable *Var, const DIExpression *Expr,
302305 bool IsIndirect, const DebugLoc &DL);
303306
304307 // / lookupVirtReg - Find the EC leader for VirtReg or null.
@@ -423,8 +426,9 @@ void UserValue::mapVirtRegs(LDVImpl *LDV) {
423426 LDV->mapVirtReg (locations[i].getReg (), this );
424427}
425428
426- UserValue *LDVImpl::getUserValue (const MDNode *Var, const MDNode *Expr,
427- bool IsIndirect, const DebugLoc &DL) {
429+ UserValue *LDVImpl::getUserValue (const DILocalVariable *Var,
430+ const DIExpression *Expr, bool IsIndirect,
431+ const DebugLoc &DL) {
428432 UserValue *&Leader = userVarMap[Var];
429433 if (Leader) {
430434 UserValue *UV = Leader->getLeader ();
@@ -463,11 +467,11 @@ bool LDVImpl::handleDebugValue(MachineInstr &MI, SlotIndex Idx) {
463467 }
464468
465469 // Get or create the UserValue for (variable,offset).
466- bool IsIndirect = MI.isIndirectDebugValue ();
470+ bool IsIndirect = MI.getOperand ( 1 ). isImm ();
467471 if (IsIndirect)
468472 assert (MI.getOperand (1 ).getImm () == 0 && " DBG_VALUE with nonzero offset" );
469- const MDNode *Var = MI.getDebugVariable ();
470- const MDNode *Expr = MI.getDebugExpression ();
473+ const DILocalVariable *Var = MI.getDebugVariable ();
474+ const DIExpression *Expr = MI.getDebugExpression ();
471475 // here.
472476 UserValue *UV = getUserValue (Var, Expr, IsIndirect, MI.getDebugLoc ());
473477 UV->addDef (Idx, MI.getOperand (0 ));
@@ -940,15 +944,20 @@ splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs, LiveIntervals &LIS) {
940944 static_cast <LDVImpl*>(pImpl)->splitRegister (OldReg, NewRegs);
941945}
942946
943- void UserValue::rewriteLocations (VirtRegMap &VRM,
944- const TargetRegisterInfo &TRI ) {
947+ void UserValue::rewriteLocations (VirtRegMap &VRM, const TargetRegisterInfo &TRI,
948+ BitVector &SpilledLocations ) {
945949 // Build a set of new locations with new numbers so we can coalesce our
946950 // IntervalMap if two vreg intervals collapse to the same physical location.
947951 // Use MapVector instead of SetVector because MapVector::insert returns the
948- // position of the previously or newly inserted element.
952+ // position of the previously or newly inserted element. The boolean value
953+ // tracks if the location was produced by a spill.
954+ // FIXME: This will be problematic if we ever support direct and indirect
955+ // frame index locations, i.e. expressing both variables in memory and
956+ // 'int x, *px = &x'. The "spilled" bit must become part of the location.
949957 MapVector<MachineOperand, bool > NewLocations;
950958 SmallVector<unsigned , 4 > LocNoMap (locations.size ());
951959 for (unsigned I = 0 , E = locations.size (); I != E; ++I) {
960+ bool Spilled = false ;
952961 MachineOperand Loc = locations[I];
953962 // Only virtual registers are rewritten.
954963 if (Loc.isReg () && Loc.getReg () &&
@@ -963,6 +972,7 @@ void UserValue::rewriteLocations(VirtRegMap &VRM,
963972 } else if (VRM.getStackSlot (VirtReg) != VirtRegMap::NO_STACK_SLOT) {
964973 // FIXME: Translate SubIdx to a stackslot offset.
965974 Loc = MachineOperand::CreateFI (VRM.getStackSlot (VirtReg));
975+ Spilled = true ;
966976 } else {
967977 Loc.setReg (0 );
968978 Loc.setSubReg (0 );
@@ -971,14 +981,22 @@ void UserValue::rewriteLocations(VirtRegMap &VRM,
971981
972982 // Insert this location if it doesn't already exist and record a mapping
973983 // from the old number to the new number.
974- auto InsertResult = NewLocations.insert ({Loc, false });
975- LocNoMap[I] = std::distance (NewLocations.begin (), InsertResult.first );
984+ auto InsertResult = NewLocations.insert ({Loc, Spilled});
985+ unsigned NewLocNo = std::distance (NewLocations.begin (), InsertResult.first );
986+ LocNoMap[I] = NewLocNo;
976987 }
977988
978- // Rewrite the locations.
989+ // Rewrite the locations and record which ones were spill slots .
979990 locations.clear ();
980- for (const auto &Pair : NewLocations)
991+ SpilledLocations.clear ();
992+ SpilledLocations.resize (NewLocations.size ());
993+ for (auto &Pair : NewLocations) {
981994 locations.push_back (Pair.first );
995+ if (Pair.second ) {
996+ unsigned NewLocNo = std::distance (&*NewLocations.begin (), &Pair);
997+ SpilledLocations.set (NewLocNo);
998+ }
999+ }
9821000
9831001 // Update the interval map, but only coalesce left, since intervals to the
9841002 // right use the old location numbers. This should merge two contiguous
@@ -1016,7 +1034,7 @@ findInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx,
10161034}
10171035
10181036void UserValue::insertDebugValue (MachineBasicBlock *MBB, SlotIndex Idx,
1019- unsigned LocNo,
1037+ unsigned LocNo, bool Spilled,
10201038 LiveIntervals &LIS,
10211039 const TargetInstrInfo &TII) {
10221040 MachineBasicBlock::iterator I = findInsertLocation (MBB, Idx, LIS);
@@ -1026,25 +1044,38 @@ void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx,
10261044 assert (cast<DILocalVariable>(Variable)
10271045 ->isValidLocationForIntrinsic (getDebugLoc ()) &&
10281046 " Expected inlined-at fields to agree" );
1029- if (Loc.isReg ())
1030- BuildMI (*MBB, I, getDebugLoc (), TII.get (TargetOpcode::DBG_VALUE),
1031- IsIndirect, Loc.getReg (), Variable, Expression);
1047+
1048+ // If the location was spilled, the new DBG_VALUE will be indirect. If the
1049+ // original DBG_VALUE was indirect, we need to add DW_OP_deref to indicate
1050+ // that the original virtual register was a pointer.
1051+ bool NewIndirect = IsIndirect || Spilled;
1052+ const DIExpression *Expr = Expression;
1053+ if (Spilled && IsIndirect)
1054+ Expr = DIExpression::prepend (Expr, DIExpression::WithDeref);
1055+
1056+ assert ((!Spilled || Loc.isFI ()) &&
1057+ " a spilled location must be a frame index" );
1058+
1059+ MachineInstrBuilder MIB =
1060+ BuildMI (*MBB, I, getDebugLoc (), TII.get (TargetOpcode::DBG_VALUE))
1061+ .add (Loc);
1062+ if (NewIndirect)
1063+ MIB.addImm (0U );
10321064 else
1033- BuildMI (*MBB, I, getDebugLoc (), TII.get (TargetOpcode::DBG_VALUE))
1034- .add (Loc)
1035- .addImm (0U )
1036- .addMetadata (Variable)
1037- .addMetadata (Expression);
1065+ MIB.addReg (0U , RegState::Debug);
1066+ MIB.addMetadata (Variable).addMetadata (Expr);
10381067}
10391068
10401069void UserValue::emitDebugValues (VirtRegMap *VRM, LiveIntervals &LIS,
1041- const TargetInstrInfo &TII) {
1070+ const TargetInstrInfo &TII,
1071+ const BitVector &SpilledLocations) {
10421072 MachineFunction::iterator MFEnd = VRM->getMachineFunction ().end ();
10431073
10441074 for (LocMap::const_iterator I = locInts.begin (); I.valid ();) {
10451075 SlotIndex Start = I.start ();
10461076 SlotIndex Stop = I.stop ();
10471077 unsigned LocNo = I.value ();
1078+ bool Spilled = LocNo != UndefLocNo ? SpilledLocations.test (LocNo) : false ;
10481079
10491080 // If the interval start was trimmed to the lexical scope insert the
10501081 // DBG_VALUE at the previous index (otherwise it appears after the
@@ -1057,7 +1088,7 @@ void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
10571088 SlotIndex MBBEnd = LIS.getMBBEndIdx (&*MBB);
10581089
10591090 DEBUG (dbgs () << " BB#" << MBB->getNumber () << ' -' << MBBEnd);
1060- insertDebugValue (&*MBB, Start, LocNo, LIS, TII);
1091+ insertDebugValue (&*MBB, Start, LocNo, Spilled, LIS, TII);
10611092 // This interval may span multiple basic blocks.
10621093 // Insert a DBG_VALUE into each one.
10631094 while (Stop > MBBEnd) {
@@ -1067,7 +1098,7 @@ void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
10671098 break ;
10681099 MBBEnd = LIS.getMBBEndIdx (&*MBB);
10691100 DEBUG (dbgs () << " BB#" << MBB->getNumber () << ' -' << MBBEnd);
1070- insertDebugValue (&*MBB, Start, LocNo, LIS, TII);
1101+ insertDebugValue (&*MBB, Start, LocNo, Spilled, LIS, TII);
10711102 }
10721103 DEBUG (dbgs () << ' \n ' );
10731104 if (MBB == MFEnd)
@@ -1082,10 +1113,11 @@ void LDVImpl::emitDebugValues(VirtRegMap *VRM) {
10821113 if (!MF)
10831114 return ;
10841115 const TargetInstrInfo *TII = MF->getSubtarget ().getInstrInfo ();
1116+ BitVector SpilledLocations;
10851117 for (unsigned i = 0 , e = userValues.size (); i != e; ++i) {
10861118 DEBUG (userValues[i]->print (dbgs (), TRI));
1087- userValues[i]->rewriteLocations (*VRM, *TRI);
1088- userValues[i]->emitDebugValues (VRM, *LIS, *TII);
1119+ userValues[i]->rewriteLocations (*VRM, *TRI, SpilledLocations );
1120+ userValues[i]->emitDebugValues (VRM, *LIS, *TII, SpilledLocations );
10891121 }
10901122 EmitDone = true ;
10911123}
0 commit comments