@@ -965,38 +965,26 @@ static void updateDefinedRegisters(MachineInstr &MI, LiveRegUnits &Units,
965965 Units.addReg (MOP.getReg ());
966966}
967967
968- // / Find the DBG_INSTR_REF instruction that references the \p InstrNum
969- static std::optional<MachineInstr *> findDebugInstrRef (MachineBasicBlock *MBB,
970- unsigned InstrNum) {
971-
972- for (auto &MI : *MBB) {
973- if (MI.isDebugRef ())
974- if (MI.getOperand (2 ).getInstrRefInstrIndex () == InstrNum)
975- return &MI;
968+ // / This function will add a new entry into the debugValueSubstitutions table
969+ // / when two instruction have been merged into a new one represented by \p
970+ // / MergedInstr.
971+ static void addDebugSubstitutionsToTable (MachineFunction *MF,
972+ unsigned InstrNumToSet,
973+ MachineInstr &OriginalInstr,
974+ MachineInstr &MergedInstr) {
975+
976+ // Figure out the Operand Index of the destination register of the
977+ // OriginalInstr in the new MergedInstr.
978+ auto Reg = OriginalInstr.getOperand (0 ).getReg ();
979+ unsigned OperandNo = 0 ;
980+ for (auto Op : MergedInstr.operands ()) {
981+ if (Op.getReg () == Reg)
982+ break ;
983+ OperandNo++;
976984 }
977- return std::nullopt ;
978- }
979985
980- // / Set the correct debug-instr-number and the operand index in case of a merge.
981- static void setDebugInstrNum (MachineBasicBlock::iterator OriginalInstr,
982- MachineInstrBuilder &MIB, unsigned InstrNumToFind,
983- unsigned InstrNumToSet, MachineBasicBlock *MBB) {
984-
985- auto InstrRefMI = findDebugInstrRef (MBB, InstrNumToFind);
986- if (InstrRefMI) {
987- auto *MI = *InstrRefMI;
988- MI->getOperand (2 ).setInstrRefInstrIndex (InstrNumToSet);
989- // Set the Instruction Reference Op Index to be the same as the operand of
990- // the new merged pair instruction.
991- auto Reg = OriginalInstr->getOperand (0 ).getReg ();
992- unsigned OperandNo = 0 ;
993- for (auto Op : MIB->operands ()) {
994- if (Op.getReg () == Reg)
995- break ;
996- OperandNo++;
997- }
998- MI->getOperand (2 ).setInstrRefOpIndex (OperandNo);
999- }
986+ MF->makeDebugValueSubstitution ({OriginalInstr.peekDebugInstrNum (), 0 },
987+ {InstrNumToSet, OperandNo});
1000988}
1001989
1002990MachineBasicBlock::iterator
@@ -1262,26 +1250,75 @@ AArch64LoadStoreOpt::mergePairedInsns(MachineBasicBlock::iterator I,
12621250 .addImm (31 );
12631251 (void )MIBSXTW;
12641252
1253+ // In the case of a sign-extend, where we have something like:
1254+ // debugValueSubstitutions:[]
1255+ // $w1 = LDRWui $x0, 1, debug-instr-number 1
1256+ // DBG_INSTR_REF !7, dbg-instr-ref(1, 0), debug-location !9
1257+ // $x0 = LDRSWui $x0, 0, debug-instr-number 2
1258+ // DBG_INSTR_REF !8, dbg-instr-ref(2, 0), debug-location !9
1259+
1260+ // It will be converted to:
1261+ // debugValueSubstitutions:[]
1262+ // $w0, $w1 = LDPWi $x0, 0
1263+ // $w0 = KILL $w0, implicit-def $x0
1264+ // $x0 = SBFMXri $x0, 0, 31
1265+ // DBG_INSTR_REF !7, dbg-instr-ref(1, 0), debug-location !9
1266+ // DBG_INSTR_REF !8, dbg-instr-ref(2, 0), debug-location !9
1267+
1268+ // $x0 is where the final value is stored, so the sign extend (SBFMXri)
1269+ // instruction contains the final value we care about we give it a new
1270+ // debug-instr-number 3. Whereas, $w1 contains the final value that we care
1271+ // about, therefore the LDP instruction is also given a new
1272+ // debug-instr-number 4. We have to add these subsitutions to the
1273+ // debugValueSubstitutions table. However, we also have to ensure that the
1274+ // OpIndex that pointed to debug-instr-number 1 gets updated to 1, because
1275+ // $w1 is the second operand of the LDP instruction.
1276+
1277+ // We want the final result to look like:
1278+ // debugValueSubstitutions:
1279+ // - { srcinst: 1, srcop: 0, dstinst: 4, dstop: 1, subreg: 0 }
1280+ // - { srcinst: 2, srcop: 0, dstinst: 3, dstop: 0, subreg: 0 }
1281+ // $w0, $w1 = LDPWi $x0, 0, debug-instr-number 4
1282+ // $w0 = KILL $w0, implicit-def $x0
1283+ // $x0 = SBFMXri $x0, 0, 31, debug-instr-number 3
1284+ // DBG_INSTR_REF !7, dbg-instr-ref(1, 0), debug-location !9
1285+ // DBG_INSTR_REF !8, dbg-instr-ref(2, 0), debug-location !9
1286+
12651287 if (I->peekDebugInstrNum ()) {
1266- auto InstrNum = I->peekDebugInstrNum ();
1267- // If I is the instruction which got sign extended, restore the debug
1268- // instruction number from I to the SBFMXri instruction
1269- if (DstRegX == I->getOperand (0 ).getReg ())
1270- MIBSXTW->setDebugInstrNum (InstrNum);
1271- else {
1272- MIB->setDebugInstrNum (InstrNum);
1273- setDebugInstrNum (I, MIB, InstrNum, InstrNum, MBB);
1288+ // If I is the instruction which got sign extended and has a
1289+ // debug-instr-number, give the SBFMXri instruction a new
1290+ // debug-instr-number, and update the debugValueSubstitutions table with
1291+ // the new debug-instr-number and OpIndex pair. Otherwise, give the Merged
1292+ // instruction a new debug-instr-number, and update the
1293+ // debugValueSubstitutions table with the new debug-instr-number and
1294+ // OpIndex pair.
1295+ unsigned NewInstrNum;
1296+ if (DstRegX == I->getOperand (0 ).getReg ()) {
1297+ NewInstrNum = MIBSXTW->getDebugInstrNum ();
1298+ addDebugSubstitutionsToTable (MBB->getParent (), NewInstrNum, *I,
1299+ *MIBSXTW);
1300+ } else {
1301+ NewInstrNum = MIB->getDebugInstrNum ();
1302+ addDebugSubstitutionsToTable (MBB->getParent (), NewInstrNum, *I, *MIB);
12741303 }
12751304 }
12761305 if (Paired->peekDebugInstrNum ()) {
1277- auto InstrNum = Paired->peekDebugInstrNum ();
1278- // If Paired is the instruction which got sign extended, restore the debug
1279- // instruction number from Paired to the SBFMXri instruction
1280- if (DstRegX == Paired->getOperand (0 ).getReg ())
1281- MIBSXTW->setDebugInstrNum (InstrNum);
1282- else {
1283- MIB->setDebugInstrNum (InstrNum);
1284- setDebugInstrNum (Paired, MIB, InstrNum, InstrNum, MBB);
1306+ // If Paired is the instruction which got sign extended and has a
1307+ // debug-instr-number, give the SBFMXri instruction a new
1308+ // debug-instr-number, and update the debugValueSubstitutions table with
1309+ // the new debug-instr-number and OpIndex pair. Otherwise, give the Merged
1310+ // instruction a new debug-instr-number, and update the
1311+ // debugValueSubstitutions table with the new debug-instr-number and
1312+ // OpIndex pair.
1313+ unsigned NewInstrNum;
1314+ if (DstRegX == Paired->getOperand (0 ).getReg ()) {
1315+ NewInstrNum = MIBSXTW->getDebugInstrNum ();
1316+ addDebugSubstitutionsToTable (MBB->getParent (), NewInstrNum, *Paired,
1317+ *MIBSXTW);
1318+ } else {
1319+ NewInstrNum = MIB->getDebugInstrNum ();
1320+ addDebugSubstitutionsToTable (MBB->getParent (), NewInstrNum, *Paired,
1321+ *MIB);
12851322 }
12861323 }
12871324
@@ -1299,27 +1336,44 @@ AArch64LoadStoreOpt::mergePairedInsns(MachineBasicBlock::iterator I,
12991336 LLVM_DEBUG (((MachineInstr *)MIB)->print (dbgs ()));
13001337 } else {
13011338
1302- // Restore the debug instruction numbers to the merged instruction.
1303- bool UseNewDebugInstrNum = false ;
1304- unsigned NewDebugInstrNum;
1305- if (I->peekDebugInstrNum () && Paired->peekDebugInstrNum ()) {
1306- // Both instructions contain debug instruction numbers. We need to set a
1307- // new instruction number for the paired instruction and update the
1308- // DBG_INSTR_REFs that reference the instruction numbers of both
1309- // instructions and update them.
1310- NewDebugInstrNum = MIB->getDebugInstrNum ();
1311- UseNewDebugInstrNum = true ;
1339+ // In the case that the merge doesn't result in a sign-extend, if we have
1340+ // something like:
1341+ // debugValueSubstitutions:[]
1342+ // $x1 = LDRXui $x0, 1, debug-instr-number 1
1343+ // DBG_INSTR_REF !13, dbg-instr-ref(1, 0), debug-location !11
1344+ // $x0 = LDRXui killed $x0, 0, debug-instr-number 2
1345+ // DBG_INSTR_REF !14, dbg-instr-ref(2, 0), debug-location !11
1346+
1347+ // It will be converted to:
1348+ // debugValueSubstitutions: []
1349+ // $x0, $x1 = LDPXi $x0, 0
1350+ // DBG_INSTR_REF !12, dbg-instr-ref(1, 0), debug-location !14
1351+ // DBG_INSTR_REF !13, dbg-instr-ref(2, 0), debug-location !14
1352+
1353+ // Here all that needs to be done is, that the LDP instruction needs to be
1354+ // updated with a new debug-instr-number, we then need to add entries into
1355+ // the debugSubstitutions table to map the old instr-refs to the new ones.
1356+
1357+ // We want the final result to look like:
1358+ // debugValueSubstitutions:
1359+ // - { srcinst: 1, srcop: 0, dstinst: 3, dstop: 1, subreg: 0 }
1360+ // - { srcinst: 2, srcop: 0, dstinst: 3, dstop: 0, subreg: 0 }
1361+ // $x0, $x1 = LDPXi $x0, 0, debug-instr-number 3
1362+ // DBG_INSTR_REF !12, dbg-instr-ref(1, 0), debug-location !14
1363+ // DBG_INSTR_REF !12, dbg-instr-ref(2, 0), debug-location !14
1364+
1365+ // Assign new DebugInstrNum to the Paired instruction.
1366+
1367+ if (I->peekDebugInstrNum ()) {
1368+ unsigned NewDebugInstrNum = MIB->getDebugInstrNum ();
1369+ addDebugSubstitutionsToTable (MBB->getParent (), NewDebugInstrNum, *I,
1370+ *MIB);
1371+ }
1372+ if (Paired->peekDebugInstrNum ()) {
1373+ unsigned NewDebugInstrNum = MIB->getDebugInstrNum ();
1374+ addDebugSubstitutionsToTable (MBB->getParent (), NewDebugInstrNum, *Paired,
1375+ *MIB);
13121376 }
1313- if (I->peekDebugInstrNum ())
1314- setDebugInstrNum (
1315- I, MIB, I->peekDebugInstrNum (),
1316- UseNewDebugInstrNum ? NewDebugInstrNum : I->peekDebugInstrNum (), MBB);
1317-
1318- if (Paired->peekDebugInstrNum ())
1319- setDebugInstrNum (Paired, MIB, Paired->peekDebugInstrNum (),
1320- UseNewDebugInstrNum ? NewDebugInstrNum
1321- : Paired->peekDebugInstrNum (),
1322- MBB);
13231377
13241378 LLVM_DEBUG (((MachineInstr *)MIB)->print (dbgs ()));
13251379 }
0 commit comments