Skip to content

Commit dd74d55

Browse files
committed
EntryExitInstrumenter: set DebugLocs on the inserted call instructions (PR35412)
Apparently the verifier requires that inlineable calls in a function with debug info have debug locations. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@319199 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent ab45c06 commit dd74d55

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed

lib/Transforms/Utils/EntryExitInstrumenter.cpp

+20-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
1111
#include "llvm/Analysis/GlobalsModRef.h"
1212
#include "llvm/CodeGen/Passes.h"
13+
#include "llvm/IR/DebugInfoMetadata.h"
1314
#include "llvm/IR/Function.h"
1415
#include "llvm/IR/Instructions.h"
1516
#include "llvm/IR/Module.h"
@@ -19,7 +20,7 @@
1920
using namespace llvm;
2021

2122
static void insertCall(Function &CurFn, StringRef Func,
22-
Instruction *InsertionPt) {
23+
Instruction *InsertionPt, DebugLoc DL) {
2324
Module &M = *InsertionPt->getParent()->getParent()->getParent();
2425
LLVMContext &C = InsertionPt->getParent()->getContext();
2526

@@ -32,7 +33,8 @@ static void insertCall(Function &CurFn, StringRef Func,
3233
Func == "_mcount" ||
3334
Func == "__cyg_profile_func_enter_bare") {
3435
Constant *Fn = M.getOrInsertFunction(Func, Type::getVoidTy(C));
35-
CallInst::Create(Fn, "", InsertionPt);
36+
CallInst *Call = CallInst::Create(Fn, "", InsertionPt);
37+
Call->setDebugLoc(DL);
3638
return;
3739
}
3840

@@ -46,11 +48,14 @@ static void insertCall(Function &CurFn, StringRef Func,
4648
Intrinsic::getDeclaration(&M, Intrinsic::returnaddress),
4749
ArrayRef<Value *>(ConstantInt::get(Type::getInt32Ty(C), 0)), "",
4850
InsertionPt);
51+
RetAddr->setDebugLoc(DL);
4952

5053
Value *Args[] = {ConstantExpr::getBitCast(&CurFn, Type::getInt8PtrTy(C)),
5154
RetAddr};
5255

53-
CallInst::Create(Fn, ArrayRef<Value *>(Args), "", InsertionPt);
56+
CallInst *Call =
57+
CallInst::Create(Fn, ArrayRef<Value *>(Args), "", InsertionPt);
58+
Call->setDebugLoc(DL);
5459
return;
5560
}
5661

@@ -76,16 +81,26 @@ static bool runOnFunction(Function &F, bool PostInlining) {
7681
// run later for some reason.
7782

7883
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);
8089
Changed = true;
8190
F.removeAttribute(AttributeList::FunctionIndex, EntryAttr);
8291
}
8392

8493
if (!ExitFunc.empty()) {
8594
for (BasicBlock &BB : F) {
8695
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+
87102
if (isa<ReturnInst>(T)) {
88-
insertCall(F, ExitFunc, T);
103+
insertCall(F, ExitFunc, T, DL);
89104
Changed = true;
90105
}
91106
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
; RUN: opt -passes="function(ee-instrument),cgscc(inline),function(post-inline-ee-instrument)" -S < %s | FileCheck %s
2+
3+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
4+
target triple = "x86_64-unknown-linux-gnu"
5+
6+
define i32 @f(i32 %x) #0 !dbg !7 {
7+
entry:
8+
%x.addr = alloca i32, align 4
9+
store i32 %x, i32* %x.addr, align 4
10+
ret i32 42, !dbg !12
11+
12+
; CHECK-LABEL: define i32 @f(i32 %x)
13+
; CHECK: call i8* @llvm.returnaddress(i32 0), !dbg ![[ENTRYLOC:[0-9]+]]
14+
; CHECK: call void @__cyg_profile_func_enter{{.*}}, !dbg ![[ENTRYLOC]]
15+
16+
; CHECK: call i8* @llvm.returnaddress(i32 0), !dbg ![[EXITLOC:[0-9]+]]
17+
; CHECK: call void @__cyg_profile_func_exit{{.*}}, !dbg ![[EXITLOC]]
18+
; CHECK: ret i32 42, !dbg ![[EXITLOC]]
19+
}
20+
21+
; CHECK: ![[SP:[0-9]+]] = distinct !DISubprogram(name: "f"
22+
; CHECK: ![[ENTRYLOC]] = !DILocation(line: 2, scope: ![[SP]])
23+
; CHECK: ![[EXITLOC]] = !DILocation(line: 4, column: 3, scope: ![[SP]])
24+
25+
attributes #0 = { "instrument-function-entry"="__cyg_profile_func_enter" "instrument-function-exit"="__cyg_profile_func_exit" }
26+
27+
!llvm.dbg.cu = !{!0}
28+
!llvm.module.flags = !{!3, !4, !5}
29+
!llvm.ident = !{!6}
30+
31+
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 6.0.0 (trunk 319007) (llvm/trunk 319050)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
32+
!1 = !DIFile(filename: "a.c", directory: "/tmp")
33+
!2 = !{}
34+
!3 = !{i32 2, !"Dwarf Version", i32 4}
35+
!4 = !{i32 2, !"Debug Info Version", i32 3}
36+
!5 = !{i32 1, !"wchar_size", i32 4}
37+
!6 = !{!"clang version 6.0.0 (trunk 319007) (llvm/trunk 319050)"}
38+
!7 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 2, type: !8, isLocal: false, isDefinition: true, scopeLine: 2, flags: DIFlagPrototyped, isOptimized: false, unit: !0, variables: !2)
39+
!8 = !DISubroutineType(types: !9)
40+
!9 = !{!10, !10}
41+
!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
42+
!11 = !DILocalVariable(name: "x", arg: 1, scope: !7, file: !1, line: 2, type: !10)
43+
!12 = !DILocation(line: 4, column: 3, scope: !7)

0 commit comments

Comments
 (0)