[llvm] r319199 - EntryExitInstrumenter: set DebugLocs on the inserted call instructions (PR35412)

Hans Wennborg via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 28 10:44:27 PST 2017


Author: hans
Date: Tue Nov 28 10:44:26 2017
New Revision: 319199

URL: http://llvm.org/viewvc/llvm-project?rev=319199&view=rev
Log:
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.

Added:
    llvm/trunk/test/Transforms/EntryExitInstrumenter/debug-info.ll
Modified:
    llvm/trunk/lib/Transforms/Utils/EntryExitInstrumenter.cpp

Modified: llvm/trunk/lib/Transforms/Utils/EntryExitInstrumenter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/EntryExitInstrumenter.cpp?rev=319199&r1=319198&r2=319199&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/EntryExitInstrumenter.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/EntryExitInstrumenter.cpp Tue Nov 28 10:44:26 2017
@@ -10,6 +10,7 @@
 #include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
 #include "llvm/Analysis/GlobalsModRef.h"
 #include "llvm/CodeGen/Passes.h"
+#include "llvm/IR/DebugInfoMetadata.h"
 #include "llvm/IR/Function.h"
 #include "llvm/IR/Instructions.h"
 #include "llvm/IR/Module.h"
@@ -19,7 +20,7 @@
 using namespace llvm;
 
 static void insertCall(Function &CurFn, StringRef Func,
-                       Instruction *InsertionPt) {
+                       Instruction *InsertionPt, DebugLoc DL) {
   Module &M = *InsertionPt->getParent()->getParent()->getParent();
   LLVMContext &C = InsertionPt->getParent()->getContext();
 
@@ -32,7 +33,8 @@ static void insertCall(Function &CurFn,
       Func == "_mcount" ||
       Func == "__cyg_profile_func_enter_bare") {
     Constant *Fn = M.getOrInsertFunction(Func, Type::getVoidTy(C));
-    CallInst::Create(Fn, "", InsertionPt);
+    CallInst *Call = CallInst::Create(Fn, "", InsertionPt);
+    Call->setDebugLoc(DL);
     return;
   }
 
@@ -46,11 +48,14 @@ static void insertCall(Function &CurFn,
         Intrinsic::getDeclaration(&M, Intrinsic::returnaddress),
         ArrayRef<Value *>(ConstantInt::get(Type::getInt32Ty(C), 0)), "",
         InsertionPt);
+    RetAddr->setDebugLoc(DL);
 
     Value *Args[] = {ConstantExpr::getBitCast(&CurFn, Type::getInt8PtrTy(C)),
                      RetAddr};
 
-    CallInst::Create(Fn, ArrayRef<Value *>(Args), "", InsertionPt);
+    CallInst *Call =
+        CallInst::Create(Fn, ArrayRef<Value *>(Args), "", InsertionPt);
+    Call->setDebugLoc(DL);
     return;
   }
 
@@ -76,7 +81,11 @@ static bool runOnFunction(Function &F, b
   // run later for some reason.
 
   if (!EntryFunc.empty()) {
-    insertCall(F, EntryFunc, &*F.begin()->getFirstInsertionPt());
+    DebugLoc DL;
+    if (auto SP = F.getSubprogram())
+      DL = DebugLoc::get(SP->getScopeLine(), 0, SP);
+
+    insertCall(F, EntryFunc, &*F.begin()->getFirstInsertionPt(), DL);
     Changed = true;
     F.removeAttribute(AttributeList::FunctionIndex, EntryAttr);
   }
@@ -84,8 +93,14 @@ static bool runOnFunction(Function &F, b
   if (!ExitFunc.empty()) {
     for (BasicBlock &BB : F) {
       TerminatorInst *T = BB.getTerminator();
+      DebugLoc DL;
+      if (DebugLoc TerminatorDL = T->getDebugLoc())
+        DL = TerminatorDL;
+      else if (auto SP = F.getSubprogram())
+        DL = DebugLoc::get(0, 0, SP);
+
       if (isa<ReturnInst>(T)) {
-        insertCall(F, ExitFunc, T);
+        insertCall(F, ExitFunc, T, DL);
         Changed = true;
       }
     }

Added: llvm/trunk/test/Transforms/EntryExitInstrumenter/debug-info.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/EntryExitInstrumenter/debug-info.ll?rev=319199&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/EntryExitInstrumenter/debug-info.ll (added)
+++ llvm/trunk/test/Transforms/EntryExitInstrumenter/debug-info.ll Tue Nov 28 10:44:26 2017
@@ -0,0 +1,43 @@
+; RUN: opt -passes="function(ee-instrument),cgscc(inline),function(post-inline-ee-instrument)" -S < %s | FileCheck %s
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define i32 @f(i32 %x) #0 !dbg !7 {
+entry:
+  %x.addr = alloca i32, align 4
+  store i32 %x, i32* %x.addr, align 4
+  ret i32 42, !dbg !12
+
+; CHECK-LABEL: define i32 @f(i32 %x)
+; CHECK: call i8* @llvm.returnaddress(i32 0), !dbg ![[ENTRYLOC:[0-9]+]]
+; CHECK: call void @__cyg_profile_func_enter{{.*}}, !dbg ![[ENTRYLOC]]
+
+; CHECK: call i8* @llvm.returnaddress(i32 0), !dbg ![[EXITLOC:[0-9]+]]
+; CHECK: call void @__cyg_profile_func_exit{{.*}}, !dbg ![[EXITLOC]]
+; CHECK: ret i32 42, !dbg ![[EXITLOC]]
+}
+
+; CHECK: ![[SP:[0-9]+]] = distinct !DISubprogram(name: "f"
+; CHECK: ![[ENTRYLOC]] = !DILocation(line: 2, scope: ![[SP]])
+; CHECK: ![[EXITLOC]] = !DILocation(line: 4, column: 3, scope: ![[SP]])
+
+attributes #0 = { "instrument-function-entry"="__cyg_profile_func_enter" "instrument-function-exit"="__cyg_profile_func_exit" }
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!3, !4, !5}
+!llvm.ident = !{!6}
+
+!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)
+!1 = !DIFile(filename: "a.c", directory: "/tmp")
+!2 = !{}
+!3 = !{i32 2, !"Dwarf Version", i32 4}
+!4 = !{i32 2, !"Debug Info Version", i32 3}
+!5 = !{i32 1, !"wchar_size", i32 4}
+!6 = !{!"clang version 6.0.0 (trunk 319007) (llvm/trunk 319050)"}
+!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)
+!8 = !DISubroutineType(types: !9)
+!9 = !{!10, !10}
+!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+!11 = !DILocalVariable(name: "x", arg: 1, scope: !7, file: !1, line: 2, type: !10)
+!12 = !DILocation(line: 4, column: 3, scope: !7)




More information about the llvm-commits mailing list