10
10
#include " llvm/Transforms/Utils/EntryExitInstrumenter.h"
11
11
#include " llvm/Analysis/GlobalsModRef.h"
12
12
#include " llvm/CodeGen/Passes.h"
13
+ #include " llvm/IR/DebugInfoMetadata.h"
13
14
#include " llvm/IR/Function.h"
14
15
#include " llvm/IR/Instructions.h"
15
16
#include " llvm/IR/Module.h"
19
20
using namespace llvm ;
20
21
21
22
static void insertCall (Function &CurFn, StringRef Func,
22
- Instruction *InsertionPt) {
23
+ Instruction *InsertionPt, DebugLoc DL ) {
23
24
Module &M = *InsertionPt->getParent ()->getParent ()->getParent ();
24
25
LLVMContext &C = InsertionPt->getParent ()->getContext ();
25
26
@@ -32,7 +33,8 @@ static void insertCall(Function &CurFn, StringRef Func,
32
33
Func == " _mcount" ||
33
34
Func == " __cyg_profile_func_enter_bare" ) {
34
35
Constant *Fn = M.getOrInsertFunction (Func, Type::getVoidTy (C));
35
- CallInst::Create (Fn, " " , InsertionPt);
36
+ CallInst *Call = CallInst::Create (Fn, " " , InsertionPt);
37
+ Call->setDebugLoc (DL);
36
38
return ;
37
39
}
38
40
@@ -46,11 +48,14 @@ static void insertCall(Function &CurFn, StringRef Func,
46
48
Intrinsic::getDeclaration (&M, Intrinsic::returnaddress),
47
49
ArrayRef<Value *>(ConstantInt::get (Type::getInt32Ty (C), 0 )), " " ,
48
50
InsertionPt);
51
+ RetAddr->setDebugLoc (DL);
49
52
50
53
Value *Args[] = {ConstantExpr::getBitCast (&CurFn, Type::getInt8PtrTy (C)),
51
54
RetAddr};
52
55
53
- CallInst::Create (Fn, ArrayRef<Value *>(Args), " " , InsertionPt);
56
+ CallInst *Call =
57
+ CallInst::Create (Fn, ArrayRef<Value *>(Args), " " , InsertionPt);
58
+ Call->setDebugLoc (DL);
54
59
return ;
55
60
}
56
61
@@ -76,16 +81,26 @@ static bool runOnFunction(Function &F, bool PostInlining) {
76
81
// run later for some reason.
77
82
78
83
if (!EntryFunc.empty ()) {
79
- insertCall (F, EntryFunc, &*F.begin ()->getFirstInsertionPt ());
84
+ DebugLoc DL;
85
+ if (auto SP = F.getSubprogram ())
86
+ DL = DebugLoc::get (SP->getScopeLine (), 0 , SP);
87
+
88
+ insertCall (F, EntryFunc, &*F.begin ()->getFirstInsertionPt (), DL);
80
89
Changed = true ;
81
90
F.removeAttribute (AttributeList::FunctionIndex, EntryAttr);
82
91
}
83
92
84
93
if (!ExitFunc.empty ()) {
85
94
for (BasicBlock &BB : F) {
86
95
TerminatorInst *T = BB.getTerminator ();
96
+ DebugLoc DL;
97
+ if (DebugLoc TerminatorDL = T->getDebugLoc ())
98
+ DL = TerminatorDL;
99
+ else if (auto SP = F.getSubprogram ())
100
+ DL = DebugLoc::get (0 , 0 , SP);
101
+
87
102
if (isa<ReturnInst>(T)) {
88
- insertCall (F, ExitFunc, T);
103
+ insertCall (F, ExitFunc, T, DL );
89
104
Changed = true ;
90
105
}
91
106
}
0 commit comments