[clang] 6cfa183 - [MSP430] Define the registers of the exception pointer and selector

via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 12 08:56:15 PST 2026


Author: Mintsuki
Date: 2026-11-12T08:56:09-08:00
New Revision: 6cfa183986ae2b79a1e0e35aab346ae62e321a94

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

LOG: [MSP430] Define the registers of the exception pointer and selector

GCC passes the exception pointer and the selector in R12 and R13: its
`__builtin_eh_return_data_regno` gives 12, 13 and 14 for 0, 1 and 2. Use
R12 and R13, whose DWARF numbers are 12 and 13 in LLVM too, so that an
unwinder that sets them with `_Unwind_SetGR` reaches them, and make
Clang's `__builtin_eh_return_data_regno` return those numbers for
MSP430, where it gave -1.

Assisted-by: Claude Code (Claude Opus 5.5)
Signed-off-by: Mintsuki <mintsuki at protonmail.com>

Added: 
    llvm/test/CodeGen/MSP430/landingpad.ll

Modified: 
    clang/lib/Basic/Targets/MSP430.h
    clang/test/CodeGen/builtins-msp430.c
    llvm/lib/Target/MSP430/MSP430ISelLowering.cpp
    llvm/lib/Target/MSP430/MSP430ISelLowering.h

Removed: 
    


################################################################################
diff  --git a/clang/lib/Basic/Targets/MSP430.h b/clang/lib/Basic/Targets/MSP430.h
index 02896cc2c73f9..ea59bf7c4e151 100644
--- a/clang/lib/Basic/Targets/MSP430.h
+++ b/clang/lib/Basic/Targets/MSP430.h
@@ -94,6 +94,16 @@ class LLVM_LIBRARY_VISIBILITY MSP430TargetInfo : public TargetInfo {
     return "";
   }
 
+  // The registers that receive the exception pointer and the selector, R12
+  // and R13, as in GCC.
+  int getEHDataRegisterNumber(unsigned RegNo) const override {
+    if (RegNo == 0)
+      return 12;
+    if (RegNo == 1)
+      return 13;
+    return -1;
+  }
+
   BuiltinVaListKind getBuiltinVaListKind() const override {
     // FIXME: implement
     return TargetInfo::CharPtrBuiltinVaList;

diff  --git a/clang/test/CodeGen/builtins-msp430.c b/clang/test/CodeGen/builtins-msp430.c
index c9d7615014b78..a08f663bc0713 100644
--- a/clang/test/CodeGen/builtins-msp430.c
+++ b/clang/test/CodeGen/builtins-msp430.c
@@ -8,3 +8,11 @@ int test_builtin_flt_rounds() {
   return __builtin_flt_rounds();
 }
 
+void test_eh_return_data_regno(void) {
+  volatile int res;
+  // CHECK-LABEL: @test_eh_return_data_regno
+  // CHECK: store volatile i16 12
+  // CHECK: store volatile i16 13
+  res = __builtin_eh_return_data_regno(0);
+  res = __builtin_eh_return_data_regno(1);
+}

diff  --git a/llvm/lib/Target/MSP430/MSP430ISelLowering.cpp b/llvm/lib/Target/MSP430/MSP430ISelLowering.cpp
index efb344a0bd5de..782b5f673770d 100644
--- a/llvm/lib/Target/MSP430/MSP430ISelLowering.cpp
+++ b/llvm/lib/Target/MSP430/MSP430ISelLowering.cpp
@@ -183,6 +183,18 @@ bool MSP430TargetLowering::isLegalICmpImmediate(int64_t Immed) const {
   return TargetLowering::isLegalICmpImmediate(Immed);
 }
 
+// The unwinder passes the exception pointer and the selector in R12 and R13,
+// as GCC's EH_RETURN_DATA_REGNO has it.
+Register MSP430TargetLowering::getExceptionPointerRegister(
+    ExceptionHandling EH, const Constant *PersonalityFn) const {
+  return MSP430::R12;
+}
+
+Register MSP430TargetLowering::getExceptionSelectorRegister(
+    ExceptionHandling EH, const Constant *PersonalityFn) const {
+  return MSP430::R13;
+}
+
 //===----------------------------------------------------------------------===//
 //                       MSP430 Inline Assembly Support
 //===----------------------------------------------------------------------===//

diff  --git a/llvm/lib/Target/MSP430/MSP430ISelLowering.h b/llvm/lib/Target/MSP430/MSP430ISelLowering.h
index 828d630e1bf54..a5a2cb407af39 100644
--- a/llvm/lib/Target/MSP430/MSP430ISelLowering.h
+++ b/llvm/lib/Target/MSP430/MSP430ISelLowering.h
@@ -77,6 +77,18 @@ namespace llvm {
     bool isZExtFree(EVT VT1, EVT VT2) const override;
 
     bool isLegalICmpImmediate(int64_t) const override;
+
+    /// If a physical register, this returns the register that receives the
+    /// exception address on entry to an EH pad.
+    Register
+    getExceptionPointerRegister(ExceptionHandling EH,
+                                const Constant *PersonalityFn) const override;
+
+    /// If a physical register, this returns the register that receives the
+    /// exception typeid on entry to a landing pad.
+    Register
+    getExceptionSelectorRegister(ExceptionHandling EH,
+                                 const Constant *PersonalityFn) const override;
     bool shouldAvoidTransformToShift(EVT VT, unsigned Amount) const override;
 
     MachineBasicBlock *

diff  --git a/llvm/test/CodeGen/MSP430/landingpad.ll b/llvm/test/CodeGen/MSP430/landingpad.ll
new file mode 100644
index 0000000000000..b822bf2a95922
--- /dev/null
+++ b/llvm/test/CodeGen/MSP430/landingpad.ll
@@ -0,0 +1,155 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -mtriple=msp430 -O0 -verify-machineinstrs < %s | FileCheck %s --check-prefix=O0
+; RUN: llc -mtriple=msp430 -O2 -verify-machineinstrs < %s | FileCheck %s --check-prefix=O2
+
+; The exception pointer comes in r12, and the selector in r13.
+
+ at _ZTIi = external constant ptr
+
+declare void @may_throw()
+declare void @cleanup()
+declare i32 @__gxx_personality_v0(...)
+declare ptr @__cxa_begin_catch(ptr)
+declare void @__cxa_end_catch()
+declare i32 @llvm.eh.typeid.for(ptr)
+
+define void @resume() personality ptr @__gxx_personality_v0 {
+; O0-LABEL: resume:
+; O0:       .Lfunc_begin0:
+; O0-NEXT:    .cfi_startproc
+; O0-NEXT:    .cfi_personality 0, __gxx_personality_v0
+; O0-NEXT:    .cfi_lsda 0, .Lexception0
+; O0-NEXT:  ; %bb.0: ; %entry
+; O0-NEXT:    sub #2, r1
+; O0-NEXT:    .cfi_def_cfa_offset 4
+; O0-NEXT:  .Ltmp0: ; EH_LABEL
+; O0-NEXT:    call #may_throw
+; O0-NEXT:  .Ltmp1: ; EH_LABEL
+; O0-NEXT:    jmp .LBB0_1
+; O0-NEXT:  .LBB0_1: ; %done
+; O0-NEXT:    add #2, r1
+; O0-NEXT:    .cfi_def_cfa_offset 2
+; O0-NEXT:    ret
+; O0-NEXT:  .LBB0_2: ; %lpad
+; O0-NEXT:  .Ltmp2: ; EH_LABEL
+; O0-NEXT:    mov r12, 0(r1) ; 2-byte Folded Spill
+; O0-NEXT:    call #cleanup
+; O0-NEXT:    mov 0(r1), r12 ; 2-byte Folded Reload
+; O0-NEXT:    call #_Unwind_Resume
+;
+; O2-LABEL: resume:
+; O2:       .Lfunc_begin0:
+; O2-NEXT:    .cfi_startproc
+; O2-NEXT:    .cfi_personality 0, __gxx_personality_v0
+; O2-NEXT:    .cfi_lsda 0, .Lexception0
+; O2-NEXT:  ; %bb.0: ; %entry
+; O2-NEXT:    push r10
+; O2-NEXT:    .cfi_def_cfa_offset 4
+; O2-NEXT:    .cfi_offset r10, -4
+; O2-NEXT:  .Ltmp0: ; EH_LABEL
+; O2-NEXT:    call #may_throw
+; O2-NEXT:  .Ltmp1: ; EH_LABEL
+; O2-NEXT:  ; %bb.1: ; %done
+; O2-NEXT:    pop r10
+; O2-NEXT:    .cfi_def_cfa_offset 2
+; O2-NEXT:    .cfi_restore r10
+; O2-NEXT:    ret
+; O2-NEXT:  .LBB0_2: ; %lpad
+; O2-NEXT:  .Ltmp2: ; EH_LABEL
+; O2-NEXT:    mov r12, r10
+; O2-NEXT:    call #cleanup
+; O2-NEXT:    mov r10, r12
+; O2-NEXT:    call #_Unwind_Resume
+entry:
+  invoke void @may_throw()
+          to label %done unwind label %lpad
+
+done:
+  ret void
+
+lpad:
+  %lp = landingpad { ptr, i32 }
+          cleanup
+  call void @cleanup()
+  resume { ptr, i32 } %lp
+}
+
+define i16 @catch_int() personality ptr @__gxx_personality_v0 {
+; O0-LABEL: catch_int:
+; O0:       .Lfunc_begin1:
+; O0-NEXT:    .cfi_startproc
+; O0-NEXT:    .cfi_personality 0, __gxx_personality_v0
+; O0-NEXT:    .cfi_lsda 0, .Lexception1
+; O0-NEXT:  ; %bb.0: ; %entry
+; O0-NEXT:    sub #2, r1
+; O0-NEXT:    .cfi_def_cfa_offset 4
+; O0-NEXT:  .Ltmp3: ; EH_LABEL
+; O0-NEXT:    call #may_throw
+; O0-NEXT:  .Ltmp4: ; EH_LABEL
+; O0-NEXT:    jmp .LBB1_1
+; O0-NEXT:  .LBB1_1: ; %done
+; O0-NEXT:    clr r12
+; O0-NEXT:    add #2, r1
+; O0-NEXT:    .cfi_def_cfa_offset 2
+; O0-NEXT:    ret
+; O0-NEXT:  .LBB1_2: ; %lpad
+; O0-NEXT:  .Ltmp5: ; EH_LABEL
+; O0-NEXT:    clr r14
+; O0-NEXT:    mov r12, 0(r1) ; 2-byte Folded Spill
+; O0-NEXT:    cmp #1, r13
+; O0-NEXT:    jne .LBB1_4
+; O0-NEXT:    jmp .LBB1_3
+; O0-NEXT:  .LBB1_3: ; %caught
+; O0-NEXT:    mov 0(r1), r12 ; 2-byte Folded Reload
+; O0-NEXT:    call #__cxa_begin_catch
+; O0-NEXT:    call #__cxa_end_catch
+; O0-NEXT:    mov #1, r12
+; O0-NEXT:    add #2, r1
+; O0-NEXT:    .cfi_def_cfa_offset 2
+; O0-NEXT:    ret
+; O0-NEXT:  .LBB1_4: ; %rethrow
+; O0-NEXT:    mov 0(r1), r12 ; 2-byte Folded Reload
+; O0-NEXT:    call #_Unwind_Resume
+;
+; O2-LABEL: catch_int:
+; O2:       .Lfunc_begin1:
+; O2-NEXT:    .cfi_startproc
+; O2-NEXT:    .cfi_personality 0, __gxx_personality_v0
+; O2-NEXT:    .cfi_lsda 0, .Lexception1
+; O2-NEXT:  ; %bb.0: ; %entry
+; O2-NEXT:  .Ltmp3: ; EH_LABEL
+; O2-NEXT:    call #may_throw
+; O2-NEXT:  .Ltmp4: ; EH_LABEL
+; O2-NEXT:  ; %bb.1: ; %done
+; O2-NEXT:    clr r12
+; O2-NEXT:    ret
+; O2-NEXT:  .LBB1_2: ; %lpad
+; O2-NEXT:  .Ltmp5: ; EH_LABEL
+; O2-NEXT:    call #__cxa_begin_catch
+; O2-NEXT:    call #__cxa_end_catch
+; O2-NEXT:    mov #1, r12
+; O2-NEXT:    ret
+entry:
+  invoke void @may_throw()
+          to label %done unwind label %lpad
+
+done:
+  ret i16 0
+
+lpad:
+  %lp = landingpad { ptr, i32 }
+          catch ptr @_ZTIi
+  %sel = extractvalue { ptr, i32 } %lp, 1
+  %id = call i32 @llvm.eh.typeid.for(ptr @_ZTIi)
+  %match = icmp eq i32 %sel, %id
+  br i1 %match, label %caught, label %rethrow
+
+caught:
+  %exn = extractvalue { ptr, i32 } %lp, 0
+  %obj = call ptr @__cxa_begin_catch(ptr %exn)
+  call void @__cxa_end_catch()
+  ret i16 1
+
+rethrow:
+  resume { ptr, i32 } %lp
+}


        


More information about the cfe-commits mailing list