[llvm] [InstrRef] Preserve debug instr num in aarch64-ldst-opt. (PR #136009)

Felipe de Azevedo Piovezan via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 17 18:10:47 PDT 2025


================
@@ -965,6 +965,40 @@ static void updateDefinedRegisters(MachineInstr &MI, LiveRegUnits &Units,
       Units.addReg(MOP.getReg());
 }
 
+/// Find the DBG_INSTR_REF instruction that references the \p InstrNum
+static std::optional<MachineInstr *> findDebugInstrRef(MachineBasicBlock *MBB,
+                                                       unsigned InstrNum) {
+
+  for (auto &MI : *MBB) {
+    if (MI.isDebugRef())
+      if (MI.getOperand(2).getInstrRefInstrIndex() == InstrNum)
+        return &MI;
+  }
+  return std::nullopt;
+}
+
+/// Set the correct debug-instr-number and the operand index in case of a merge.
+static void setDebugInstrNum(MachineBasicBlock::iterator OriginalInstr,
+                             MachineInstrBuilder &MIB, unsigned InstrNumToFind,
+                             unsigned InstrNumToSet, MachineBasicBlock *MBB) {
+
+  auto InstrRefMI = findDebugInstrRef(MBB, InstrNumToFind);
+  if (InstrRefMI) {
+    auto *MI = *InstrRefMI;
+    MI->getOperand(2).setInstrRefInstrIndex(InstrNumToSet);
+    // Set the Instruction Reference Op Index to be the same as the operand of
+    // the new merged pair instruction.
+    auto Reg = OriginalInstr->getOperand(0).getReg();
+    unsigned OperandNo = 0;
+    for (auto Op : MIB->operands()) {
+      if (Op.getReg() == Reg)
+        break;
+      OperandNo++;
----------------
felipepiovezan wrote:

MachineOperand has a [getOperandNo](https://llvm.org/doxygen/classllvm_1_1MachineOperand.html#a0c893675dfd5d1b1e4aea1e8211217c7) method, which simplifies this loop

https://github.com/llvm/llvm-project/pull/136009


More information about the llvm-commits mailing list