[llvm] b5dfea0 - [ARM] Fix Machine Outliner crash when tBLXr uses non-tcGPR register (#200684)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 22 02:14:47 PDT 2026


Author: Deepak Shirke
Date: 2026-06-22T10:14:41+01:00
New Revision: b5dfea08588fc64b0ce5cea685b275ed3de400de

URL: https://github.com/llvm/llvm-project/commit/b5dfea08588fc64b0ce5cea685b275ed3de400de
DIFF: https://github.com/llvm/llvm-project/commit/b5dfea08588fc64b0ce5cea685b275ed3de400de.diff

LOG: [ARM] Fix Machine Outliner crash when tBLXr uses non-tcGPR register  (#200684)

When the Machine Outliner selects MachineOutlinerThunk mode for a
sequence ending in tBLXr/tBLXr_noip, it converts the indirect call to
tTAILJMPr in buildOutlinedFrame. However tTAILJMPr requires its operand
to be in tcGPR (R0-R3, R12), while tBLXr accepts any GPR.

If the register is callee-saved (e.g. r4), the Machine Verifier crashes
with 'Illegal physical register for instruction'.

Fixes #188076

Added: 
    llvm/test/CodeGen/ARM/machine-outliner-thunk-tcgpr.mir

Modified: 
    llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp
    llvm/lib/Target/ARM/ARMRegisterInfo.td

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp b/llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp
index 3b1b8673e56a0..01a1b4288d569 100644
--- a/llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp
+++ b/llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp
@@ -5827,9 +5827,11 @@ ARMBaseInstrInfo::getOutliningCandidateInfo(
     SetCandidateCallInfo(MachineOutlinerTailCall, Costs.CallTailCall);
   } else if (LastInstrOpcode == ARM::BL || LastInstrOpcode == ARM::BLX ||
              LastInstrOpcode == ARM::BLX_noip || LastInstrOpcode == ARM::tBL ||
-             LastInstrOpcode == ARM::tBLXr ||
-             LastInstrOpcode == ARM::tBLXr_noip ||
-             LastInstrOpcode == ARM::tBLXi) {
+             LastInstrOpcode == ARM::tBLXi ||
+             ((LastInstrOpcode == ARM::tBLXr ||
+               LastInstrOpcode == ARM::tBLXr_noip) &&
+              ARM::tcGPRRegClass.contains(
+                  RepeatedSequenceLocs[0].back().getOperand(2).getReg()))) {
     FrameID = MachineOutlinerThunk;
     NumBytesToCreateFrame = Costs.FrameThunk;
     SetCandidateCallInfo(MachineOutlinerThunk, Costs.CallThunk);

diff  --git a/llvm/lib/Target/ARM/ARMRegisterInfo.td b/llvm/lib/Target/ARM/ARMRegisterInfo.td
index de4219577c78c..e8c2b3b822e65 100644
--- a/llvm/lib/Target/ARM/ARMRegisterInfo.td
+++ b/llvm/lib/Target/ARM/ARMRegisterInfo.td
@@ -366,8 +366,9 @@ def hGPR : RegisterClass<"ARM", [i32], 32, (sub GPR, tGPR)> {
   let DiagnosticString = "operand must be a register in range [r8, r15]";
 }
 
-// For tail calls, we can't use callee-saved registers, as they are restored
-// to the saved value before the tail call, which would clobber a call address.
+// For tail calls, we can't use callee-saved registers. When the frame
+// is destroyed before the tail call, callee-saved registers are restored
+// to the caller's values, clobbering any call address stored in them.
 // Note, getMinimalPhysRegClass(R0) returns tGPR because of the names of
 // this class and the preceding one(!)  This is what we want.
 def tcGPR : RegisterClass<"ARM", [i32], 32, (add R0, R1, R2, R3, R12)> {

diff  --git a/llvm/test/CodeGen/ARM/machine-outliner-thunk-tcgpr.mir b/llvm/test/CodeGen/ARM/machine-outliner-thunk-tcgpr.mir
new file mode 100644
index 0000000000000..b2638a7ff300d
--- /dev/null
+++ b/llvm/test/CodeGen/ARM/machine-outliner-thunk-tcgpr.mir
@@ -0,0 +1,40 @@
+# RUN: llc -run-pass=machine-outliner -verify-machineinstrs %s -o - | FileCheck %s
+# CHECK-NOT: tTAILJMPr {{.*}}$r4
+
+--- |
+  target datalayout = "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64"
+  target triple = "thumbv7m-unknown-none-eabihf"
+
+  define void @test1() #0 { ret void }
+  define void @test2() #0 { ret void }
+
+  attributes #0 = { minsize }
+...
+---
+name:            test1
+tracksRegLiveness: true
+body:             |
+  bb.0:
+    liveins: $r4, $lr
+    $r0, dead $cpsr = tMOVi8 0, 14, $noreg
+    $r1, dead $cpsr = tMOVi8 0, 14, $noreg
+    tBLXr 14, $noreg, $r4, implicit-def dead $lr, implicit $sp
+    $r0, dead $cpsr = tMOVi8 0, 14, $noreg
+    $r1, dead $cpsr = tMOVi8 0, 14, $noreg
+    tBLXr 14, $noreg, $r4, implicit-def dead $lr, implicit $sp
+    tBX_RET 14, $noreg
+...
+---
+name:            test2
+tracksRegLiveness: true
+body:             |
+  bb.0:
+    liveins: $r4, $lr
+    $r0, dead $cpsr = tMOVi8 0, 14, $noreg
+    $r1, dead $cpsr = tMOVi8 0, 14, $noreg
+    tBLXr 14, $noreg, $r4, implicit-def dead $lr, implicit $sp
+    $r0, dead $cpsr = tMOVi8 0, 14, $noreg
+    $r1, dead $cpsr = tMOVi8 0, 14, $noreg
+    tBLXr 14, $noreg, $r4, implicit-def dead $lr, implicit $sp
+    tBX_RET 14, $noreg
+...
\ No newline at end of file


        


More information about the llvm-commits mailing list