Skip to content

Commit b3d9622

Browse files
committed
[EntryExitInstrumenter][AArch64][RISCV][LoongArch] Pass __builtin_return_address(0) into _mcount
1 parent 9423961 commit b3d9622

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

llvm/lib/Transforms/Utils/EntryExitInstrumenter.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,21 @@ static void insertCall(Function &CurFn, StringRef Func,
4848
/*isVarArg=*/false)),
4949
{GV}, "", InsertionPt);
5050
Call->setDebugLoc(DL);
51+
} else if (TargetTriple.isRISCV() || TargetTriple.isAArch64() ||
52+
TargetTriple.isLoongArch()) {
53+
// On RISC-V, AArch64, and LoongArch, the `_mcount` function takes
54+
// `__builtin_return_address(0)` as an argument since
55+
// `__builtin_return_address(1)` is not available on these platforms.
56+
Instruction *RetAddr = CallInst::Create(
57+
Intrinsic::getOrInsertDeclaration(&M, Intrinsic::returnaddress),
58+
ConstantInt::get(Type::getInt32Ty(C), 0), "", InsertionPt);
59+
RetAddr->setDebugLoc(DL);
60+
61+
FunctionCallee Fn = M.getOrInsertFunction(
62+
Func, FunctionType::get(Type::getVoidTy(C), PointerType::getUnqual(C),
63+
false));
64+
CallInst *Call = CallInst::Create(Fn, RetAddr, "", InsertionPt);
65+
Call->setDebugLoc(DL);
5166
} else {
5267
FunctionCallee Fn = M.getOrInsertFunction(Func, Type::getVoidTy(C));
5368
CallInst *Call = CallInst::Create(Fn, "", InsertionPt);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -mtriple=riscv64 -passes=ee-instrument -S < %s | FileCheck %s --check-prefixes=CHECK,RISCV64
3+
; RUN: opt -mtriple=riscv32 -passes=ee-instrument -S < %s | FileCheck %s --check-prefixes=CHECK,RISCV32
4+
; RUN: opt -mtriple=loongarch64 -passes=ee-instrument -S < %s | FileCheck %s --check-prefixes=CHECK,LOONGARCH64
5+
; RUN: opt -mtriple=loongarch32 -passes=ee-instrument -S < %s | FileCheck %s --check-prefixes=CHECK,LOONGARCH32
6+
; RUN: opt -mtriple=aarch64 -passes=ee-instrument -S < %s | FileCheck %s --check-prefixes=CHECK,AARCH64
7+
; RUN: opt -mtriple=aarch64_be -passes=ee-instrument -S < %s | FileCheck %s --check-prefixes=CHECK,AARCH64_BE
8+
; RUN: opt -mtriple=aarch64_32 -passes=ee-instrument -S < %s | FileCheck %s --check-prefixes=CHECK,AARCH64_32
9+
10+
define void @f1() "instrument-function-entry-inlined"="_mcount" {
11+
; CHECK-LABEL: define void @f1(
12+
; CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
13+
; CHECK-NEXT: ret void
14+
;
15+
ret void
16+
}
17+
;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
18+
; AARCH64: {{.*}}
19+
; AARCH64_32: {{.*}}
20+
; AARCH64_BE: {{.*}}
21+
; LOONGARCH32: {{.*}}
22+
; LOONGARCH64: {{.*}}
23+
; RISCV32: {{.*}}
24+
; RISCV64: {{.*}}

0 commit comments

Comments
 (0)