From 0028bf5b31010482221ac3ff05a7dbca02ded06a Mon Sep 17 00:00:00 2001 From: Daniel Sanders Date: Sat, 28 Jan 2017 11:22:05 +0000 Subject: [PATCH] stripDebugInfo() should remove DILocation's found in !llvm.loop metadata Summary: Patch by Michele Scandale (with a small tweak to 'CHECK-NOT' the last DILocation in the test) Subscribers: bogner, llvm-commits Differential Revision: https://reviews.llvm.org/D27980 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@293377 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/IR/DebugInfo.cpp | 39 +++++++++++++++ test/DebugInfo/strip-loop-metadata.ll | 71 +++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 test/DebugInfo/strip-loop-metadata.ll diff --git a/lib/IR/DebugInfo.cpp b/lib/IR/DebugInfo.cpp index 6b9bc689a44..3bdd5a6bd95 100644 --- a/lib/IR/DebugInfo.cpp +++ b/lib/IR/DebugInfo.cpp @@ -239,6 +239,35 @@ bool DebugInfoFinder::addScope(DIScope *Scope) { return true; } +static llvm::MDNode *stripDebugLocFromLoopID(llvm::MDNode *N) { + assert(N->op_begin() != N->op_end() && "Missing self reference?"); + auto DebugLocOp = + std::find_if(N->op_begin() + 1, N->op_end(), [](const MDOperand &Op) { + return isa(Op.get()); + }); + + // No debug location, we do not have to rewrite this MDNode. + if (DebugLocOp == N->op_end()) + return N; + + // There is only the debug location without any actual loop metadata, hence we + // can remove the metadata. + if (N->getNumOperands() == 2) + return nullptr; + + SmallVector Args; + // Reserve operand 0 for loop id self reference. + auto TempNode = MDNode::getTemporary(N->getContext(), None); + Args.push_back(TempNode.get()); + Args.append(N->op_begin() + 1, DebugLocOp); + Args.append(DebugLocOp + 1, N->op_end()); + + // Set the first operand to itself. + MDNode *LoopID = MDNode::get(N->getContext(), Args); + LoopID->replaceOperandWith(0, LoopID); + return LoopID; +} + bool llvm::stripDebugInfo(Function &F) { bool Changed = false; if (F.getSubprogram()) { @@ -246,6 +275,7 @@ bool llvm::stripDebugInfo(Function &F) { F.setSubprogram(nullptr); } + llvm::DenseMap LoopIDsMap; for (BasicBlock &BB : F) { for (auto II = BB.begin(), End = BB.end(); II != End;) { Instruction &I = *II++; // We may delete the instruction, increment now. @@ -259,6 +289,15 @@ bool llvm::stripDebugInfo(Function &F) { I.setDebugLoc(DebugLoc()); } } + + auto *TermInst = BB.getTerminator(); + if (auto *LoopID = TermInst->getMetadata(LLVMContext::MD_loop)) { + auto *NewLoopID = LoopIDsMap.lookup(LoopID); + if (!NewLoopID) + NewLoopID = LoopIDsMap[LoopID] = stripDebugLocFromLoopID(LoopID); + if (NewLoopID != LoopID) + TermInst->setMetadata(LLVMContext::MD_loop, NewLoopID); + } } return Changed; } diff --git a/test/DebugInfo/strip-loop-metadata.ll b/test/DebugInfo/strip-loop-metadata.ll new file mode 100644 index 00000000000..1b1c9cac4e0 --- /dev/null +++ b/test/DebugInfo/strip-loop-metadata.ll @@ -0,0 +1,71 @@ +; RUN: opt -S -strip-debug < %s | FileCheck %s + +target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-apple-macosx10.12.0" + +; CHECK-LABEL: _Z5test1v +; CHECK-NOT: br {{.*}} !llvm.loop +define void @_Z5test1v() !dbg !7 { +entry: + br label %while.body, !dbg !9 + +while.body: + call void @_Z3barv(), !dbg !10 + br label %while.body, !dbg !11, !llvm.loop !13 + +return: + ret void, !dbg !14 +} + +declare void @_Z3barv() + +; CHECK-LABEL: _Z5test2v +; CHECK: br {{.*}} !llvm.loop [[LOOP:![0-9]+]] +define void @_Z5test2v() !dbg !15 { +entry: + br label %while.body, !dbg !16 + +while.body: + call void @_Z3barv(), !dbg !17 + br label %while.body, !dbg !18, !llvm.loop !19 + +return: + ret void, !dbg !21 +} + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!3, !4, !5} +!llvm.ident = !{!6} + +!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 4.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: LineTablesOnly, enums: !2) +!1 = !DIFile(filename: "test.cpp", directory: "/tmp") +!2 = !{} +!3 = !{i32 2, !"Dwarf Version", i32 4} +!4 = !{i32 2, !"Debug Info Version", i32 3} +!5 = !{i32 1, !"PIC Level", i32 2} +!6 = !{!"clang version 4.0.0"} +!7 = distinct !DISubprogram(name: "test1", scope: !1, file: !1, line: 3, type: !8, isLocal: false, isDefinition: true, scopeLine: 3, flags: DIFlagPrototyped, isOptimized: false, unit: !0, variables: !2) +!8 = !DISubroutineType(types: !2) +!9 = !DILocation(line: 4, column: 3, scope: !7) +!10 = !DILocation(line: 5, column: 5, scope: !7) +!11 = !DILocation(line: 4, column: 3, scope: !12) +!12 = !DILexicalBlockFile(scope: !7, file: !1, discriminator: 1) +!13 = distinct !{!13, !9} +!14 = !DILocation(line: 6, column: 1, scope: !7) +!15 = distinct !DISubprogram(name: "test2", scope: !1, file: !1, line: 8, type: !8, isLocal: false, isDefinition: true, scopeLine: 8, flags: DIFlagPrototyped, isOptimized: false, unit: !0, variables: !2) +!16 = !DILocation(line: 8, column: 14, scope: !15) +!17 = !DILocation(line: 11, column: 5, scope: !15) +!18 = !DILocation(line: 10, column: 3, scope: !15) +!19 = distinct !{!19, !16, !20} +!20 = !{!"llvm.loop.unroll.enable"} +!21 = !DILocation(line: 12, column: 1, scope: !15) + +; CHECK-NOT: !DICompileUnit +; CHECK-NOT: !DIFile +; CHECK-NOT: !DISubprogram +; CHECK-NOT: !DISubroutineType +; CHECK-NOT: !DILocation +; CHECK-NOT: !DILexicalBlockFile +; CHECK: [[LOOP]] = distinct !{[[LOOP]], [[LOOP_UNROLL:![0-9]+]]} +; CHECK-NEXT: [[LOOP_UNROLL]] = !{!"llvm.loop.unroll.enable"} +; CHECK-NOT: !DILocation -- 2.40.0