[llvm] ARM: Don't duplicate the LR def when expanding call pseudos (PR #225062)

David Green via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 21 11:46:00 PDT 2026


================
@@ -3249,29 +3262,32 @@ bool ARMExpandPseudo::ExpandMI(MachineBasicBlock &MBB,
       const bool Thumb = Opcode == ARM::tBL_PUSHLR;
       Register Reg = MI.getOperand(0).getReg();
       assert(Reg == ARM::LR && "expect LR register!");
-      MachineInstrBuilder MIB;
+      MachineFunction &MF = *MBB.getParent();
+      unsigned CallOpc = Thumb ? ARM::tBL : ARM::BL;
       if (Thumb) {
         // push {lr}
         BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(ARM::tPUSH))
             .add(predOps(ARMCC::AL))
             .addReg(Reg);
-
-        // bl __gnu_mcount_nc
-        MIB = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(ARM::tBL));
       } else {
         // stmdb   sp!, {lr}
         BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(ARM::STMDB_UPD))
             .addReg(ARM::SP, RegState::Define)
             .addReg(ARM::SP)
             .add(predOps(ARMCC::AL))
             .addReg(Reg);
-
-        // bl __gnu_mcount_nc
-        MIB = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(ARM::BL));
       }
+
+      // bl __gnu_mcount_nc. Be careful not to duplicate the LR-def the original
+      // instruction already has.
+      MachineInstr *Call =
+          MF.CreateMachineInstr(TII->get(CallOpc), MI.getDebugLoc(),
+                                /*NoImplicit=*/true);
+      MBB.insert(MBBI, Call);
+      MachineInstrBuilder MIB(MF, Call);
       MIB.cloneMemRefs(MI);
       for (const MachineOperand &MO : llvm::drop_begin(MI.operands()))
-        MIB.add(MO);
+        Call->addOperand(MO);
----------------
davemgreen wrote:

MIB.add(MO); is OK?

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


More information about the llvm-commits mailing list