Skip to content

Commit 7ecb514

Browse files
committed
[COFF, ARM64] Fix encoding of debugtrap for Windows
On Windows ARM64, intrinsic __debugbreak is compiled into brk #0xF000 which is mapped to llvm.debugtrap in Clang. Instruction brk #F000 is the defined break point instruction on ARM64 which is recognized by Windows debugger and exception handling code, so llvm.debugtrap should map to it instead of redirecting to llvm.trap (brk #1) as the default implementation. Differential Revision: https://reviews.llvm.org/D63635 llvm-svn: 364115
1 parent 51a741c commit 7ecb514

File tree

5 files changed

+30
-0
lines changed

5 files changed

+30
-0
lines changed

llvm/lib/Target/AArch64/AArch64FastISel.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3604,6 +3604,14 @@ bool AArch64FastISel::fastLowerIntrinsicCall(const IntrinsicInst *II) {
36043604
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::BRK))
36053605
.addImm(1);
36063606
return true;
3607+
case Intrinsic::debugtrap: {
3608+
if (Subtarget->isTargetWindows()) {
3609+
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::BRK))
3610+
.addImm(0xF000);
3611+
return true;
3612+
}
3613+
break;
3614+
}
36073615

36083616
case Intrinsic::sqrt: {
36093617
Type *RetTy = II->getCalledFunction()->getReturnType();

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,8 @@ AArch64TargetLowering::AArch64TargetLowering(const TargetMachine &TM,
551551

552552
// Trap.
553553
setOperationAction(ISD::TRAP, MVT::Other, Legal);
554+
if (Subtarget->isTargetWindows())
555+
setOperationAction(ISD::DEBUGTRAP, MVT::Other, Legal);
554556

555557
// We combine OR nodes for bitfield operations.
556558
setTargetDAGCombine(ISD::OR);

llvm/lib/Target/AArch64/AArch64InstrInfo.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ def HasMTE : Predicate<"Subtarget->hasMTE()">,
135135
AssemblerPredicate<"FeatureMTE", "mte">;
136136
def IsLE : Predicate<"Subtarget->isLittleEndian()">;
137137
def IsBE : Predicate<"!Subtarget->isLittleEndian()">;
138+
def IsWindows : Predicate<"Subtarget->isTargetWindows()">;
138139
def UseAlternateSExtLoadCVTF32
139140
: Predicate<"Subtarget->useAlternateSExtLoadCVTF32Pattern()">;
140141

@@ -6116,6 +6117,7 @@ def : Pat<(i32 (trunc GPR64sp:$src)),
61166117

61176118
// __builtin_trap() uses the BRK instruction on AArch64.
61186119
def : Pat<(trap), (BRK 1)>;
6120+
def : Pat<(debugtrap), (BRK 0xF000)>, Requires<[IsWindows]>;
61196121

61206122
// Multiply high patterns which multiply the lower subvector using smull/umull
61216123
// and the upper subvector with smull2/umull2. Then shuffle the high the high

llvm/lib/Target/AArch64/AArch64InstructionSelector.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3525,6 +3525,11 @@ bool AArch64InstructionSelector::selectIntrinsicWithSideEffects(
35253525
case Intrinsic::trap:
35263526
MIRBuilder.buildInstr(AArch64::BRK, {}, {}).addImm(1);
35273527
break;
3528+
case Intrinsic::debugtrap:
3529+
if (!STI.isTargetWindows())
3530+
return false;
3531+
MIRBuilder.buildInstr(AArch64::BRK, {}, {}).addImm(0xF000);
3532+
break;
35283533
case Intrinsic::aarch64_stlxr:
35293534
unsigned StatReg = I.getOperand(0).getReg();
35303535
assert(RBI.getSizeInBits(StatReg, MRI, TRI) == 32 &&
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
; RUN: llc -mtriple=aarch64-windows %s -o -| FileCheck %s
2+
; RUN: llc -mtriple=aarch64-windows -fast-isel %s -o - | FileCheck %s
3+
; RUN: llc -mtriple=aarch64-windows -global-isel %s -o - | FileCheck %s
4+
5+
; CHECK-LABEL: test1:
6+
; CHECK: brk #0xf000
7+
define void @test1() noreturn nounwind {
8+
entry:
9+
tail call void @llvm.debugtrap( )
10+
ret void
11+
}
12+
13+
declare void @llvm.debugtrap() nounwind

0 commit comments

Comments
 (0)