llvm.org GIT mirror llvm / 0a7baaf
Reimplement discriminator assignment algorithm. Summary: The new algorithm is more efficient (O(n), n is number of basic blocks). And it is guaranteed to cover all cases of multiple BB mapped to same line. Reviewers: dblaikie, davidxl, dnovillo Subscribers: llvm-commits Differential Revision: http://reviews.llvm.org/D14738 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@253594 91177308-0d34-0410-b5e6-96231b3b80d8 Dehao Chen 3 years ago
3 changed file(s) with 114 addition(s) and 57 deletion(s). Raw diff Collapse all Expand all
5151 // http://wiki.dwarfstd.org/index.php?title=Path_Discriminators
5252 //===----------------------------------------------------------------------===//
5353
54 #include "llvm/Transforms/Scalar.h"
54 #include "llvm/ADT/DenseMap.h"
5555 #include "llvm/IR/BasicBlock.h"
5656 #include "llvm/IR/Constants.h"
5757 #include "llvm/IR/DIBuilder.h"
6464 #include "llvm/Support/CommandLine.h"
6565 #include "llvm/Support/Debug.h"
6666 #include "llvm/Support/raw_ostream.h"
67 #include "llvm/Transforms/Scalar.h"
6768
6869 using namespace llvm;
6970
168169 LLVMContext &Ctx = M->getContext();
169170 DIBuilder Builder(*M, /*AllowUnresolved*/ false);
170171
171 // Traverse all the blocks looking for instructions in different
172 // blocks that are at the same file:line location.
172 typedef std::pair Location;
173 typedef DenseMap BBScopeMap;
174 typedef DenseMap LocationBBMap;
175
176 LocationBBMap LBM;
177
178 // Traverse all instructions in the function. If the source line location
179 // of the instruction appears in other basic block, assign a new
180 // discriminator for this instruction.
173181 for (BasicBlock &B : F) {
174 TerminatorInst *Last = B.getTerminator();
175 const DILocation *LastDIL = Last->getDebugLoc();
176 if (!LastDIL)
177 continue;
178
179 for (unsigned I = 0; I < Last->getNumSuccessors(); ++I) {
180 BasicBlock *Succ = Last->getSuccessor(I);
181 Instruction *First = Succ->getFirstNonPHIOrDbgOrLifetime();
182 const DILocation *FirstDIL = First->getDebugLoc();
183 if (!FirstDIL || FirstDIL->getDiscriminator())
184 continue;
185
186 // If the first instruction (First) of Succ is at the same file
187 // location as B's last instruction (Last), add a new
188 // discriminator for First's location and all the instructions
189 // in Succ that share the same location with First.
190 if (!FirstDIL->canDiscriminate(*LastDIL)) {
191 // Create a new lexical scope and compute a new discriminator
192 // number for it.
193 StringRef Filename = FirstDIL->getFilename();
194 auto *Scope = FirstDIL->getScope();
195 auto *File = Builder.createFile(Filename, Scope->getDirectory());
196
197 // FIXME: Calculate the discriminator here, based on local information,
198 // and delete DILocation::computeNewDiscriminator(). The current
199 // solution gives different results depending on other modules in the
200 // same context. All we really need is to discriminate between
201 // FirstDIL and LastDIL -- a local map would suffice.
202 unsigned Discriminator = FirstDIL->computeNewDiscriminator();
203 auto *NewScope =
204 Builder.createLexicalBlockFile(Scope, File, Discriminator);
205
206 // Attach this new debug location to First and every
207 // instruction following First that shares the same location.
208 for (BasicBlock::iterator I1(*First), E1 = Succ->end(); I1 != E1;
209 ++I1) {
210 const DILocation *CurrentDIL = I1->getDebugLoc();
211 if (CurrentDIL && CurrentDIL->getLine() == FirstDIL->getLine() &&
212 CurrentDIL->getFilename() == FirstDIL->getFilename()) {
213 I1->setDebugLoc(DILocation::get(Ctx, CurrentDIL->getLine(),
214 CurrentDIL->getColumn(), NewScope,
215 CurrentDIL->getInlinedAt()));
216 DEBUG(dbgs() << CurrentDIL->getFilename() << ":"
217 << CurrentDIL->getLine() << ":"
218 << CurrentDIL->getColumn() << ":"
219 << CurrentDIL->getDiscriminator() << *I1 << "\n");
220 }
221 }
222 DEBUG(dbgs() << "\n");
223 Changed = true;
182 for (auto &I : B.getInstList()) {
183 if (isa(&I))
184 continue;
185 const DILocation *DIL = I.getDebugLoc();
186 if (!DIL)
187 continue;
188 Location L = std::make_pair(DIL->getFilename(), DIL->getLine());
189 auto &BBMap = LBM[L];
190 auto R = BBMap.insert(std::make_pair(&B, (Metadata *)nullptr));
191 if (BBMap.size() == 1)
192 continue;
193 bool InsertSuccess = R.second;
194 Metadata *&NewScope = R.first->second;
195 // If we could insert a different block in the same location, a
196 // discriminator is needed to distinguish both instructions.
197 if (InsertSuccess) {
198 auto *Scope = DIL->getScope();
199 auto *File =
200 Builder.createFile(DIL->getFilename(), Scope->getDirectory());
201 NewScope = Builder.createLexicalBlockFile(
202 Scope, File, DIL->computeNewDiscriminator());
224203 }
204 I.setDebugLoc(DILocation::get(Ctx, DIL->getLine(), DIL->getColumn(),
205 NewScope, DIL->getInlinedAt()));
206 DEBUG(dbgs() << DIL->getFilename() << ":" << DIL->getLine() << ":"
207 << DIL->getColumn() << ":" << NewScope->getDiscriminator()
208 << I << "\n");
209 Changed = true;
225210 }
226211 }
227212
0 ; RUN: opt < %s -add-discriminators -S | FileCheck %s
1
2 ; Discriminator support for diamond-shaped CFG.:
3 ; #1 void bar(int);
4 ; #2
5 ; #3 void foo(int i) {
6 ; #4 if (i > 10)
7 ; #5 bar(5); else bar(3);
8 ; #6 }
9
10 ; bar(5): discriminator 0
11 ; bar(3): discriminator 1
12
13 ; Function Attrs: uwtable
14 define void @_Z3fooi(i32 %i) #0 !dbg !4 {
15 %1 = alloca i32, align 4
16 store i32 %i, i32* %1, align 4
17 call void @llvm.dbg.declare(metadata i32* %1, metadata !11, metadata !12), !dbg !13
18 %2 = load i32, i32* %1, align 4, !dbg !14
19 %3 = icmp sgt i32 %2, 10, !dbg !16
20 br i1 %3, label %4, label %5, !dbg !17
21
22 ;
23 call void @_Z3bari(i32 5), !dbg !18
24 br label %6, !dbg !18
25
26 ;
27 call void @_Z3bari(i32 3), !dbg !19
28 ; CHECK: call void @_Z3bari(i32 3), !dbg ![[ELSE:[0-9]+]]
29 br label %6
30
31 ;
32 ret void, !dbg !20
33 }
34
35 ; Function Attrs: nounwind readnone
36 declare void @llvm.dbg.declare(metadata, metadata, metadata) #1
37
38 declare void @_Z3bari(i32) #2
39
40 attributes #0 = { uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" }
41 attributes #1 = { nounwind readnone }
42 attributes #2 = { "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" }
43
44 !llvm.dbg.cu = !{!0}
45 !llvm.module.flags = !{!8, !9}
46 !llvm.ident = !{!10}
47
48 !0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 3.8.0 (trunk 253273)", isOptimized: false, runtimeVersion: 0, emissionKind: 1, enums: !2, subprograms: !3)
49 !1 = !DIFile(filename: "a.cc", directory: "/tmp")
50 !2 = !{}
51 !3 = !{!4}
52 !4 = distinct !DISubprogram(name: "foo", linkageName: "_Z3fooi", scope: !1, file: !1, line: 3, type: !5, isLocal: false, isDefinition: true, scopeLine: 3, flags: DIFlagPrototyped, isOptimized: false, variables: !2)
53 !5 = !DISubroutineType(types: !6)
54 !6 = !{null, !7}
55 !7 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed)
56 !8 = !{i32 2, !"Dwarf Version", i32 4}
57 !9 = !{i32 2, !"Debug Info Version", i32 3}
58 !10 = !{!"clang version 3.8.0 (trunk 253273)"}
59 !11 = !DILocalVariable(name: "i", arg: 1, scope: !4, file: !1, line: 3, type: !7)
60 !12 = !DIExpression()
61 !13 = !DILocation(line: 3, column: 14, scope: !4)
62 !14 = !DILocation(line: 4, column: 7, scope: !15)
63 !15 = distinct !DILexicalBlock(scope: !4, file: !1, line: 4, column: 7)
64 !16 = !DILocation(line: 4, column: 9, scope: !15)
65 !17 = !DILocation(line: 4, column: 7, scope: !4)
66 !18 = !DILocation(line: 5, column: 5, scope: !15)
67 !19 = !DILocation(line: 5, column: 18, scope: !15)
68 !20 = !DILocation(line: 6, column: 1, scope: !4)
69
70 ; CHECK: ![[ELSE]] = !DILocation(line: 5, column: 18, scope: ![[ELSEBLOCK:[0-9]+]])
71 ; CHECK: ![[ELSEBLOCK]] = !DILexicalBlockFile({{.*}} discriminator: 1)
55 ; #3 }
66
77 ; i == 3: discriminator 0
8 ; i == 5: discriminator 2
9 ; return 100: discriminator 1
8 ; i == 5: discriminator 1
9 ; return 100: discriminator 2
1010 ; return 99: discriminator 3
1111
1212 define i32 @_Z3fooi(i32 %i) #0 !dbg !4 {
9292 !28 = !DILocation(line: 3, column: 1, scope: !4)
9393
9494 ; CHECK: ![[THEN1]] = !DILocation(line: 2, column: 17, scope: ![[THENBLOCK:[0-9]+]])
95 ; CHECK: ![[THENBLOCK]] = !DILexicalBlockFile({{.*}} discriminator: 2)
95 ; CHECK: ![[THENBLOCK]] = !DILexicalBlockFile({{.*}} discriminator: 1)
9696 ; CHECK: ![[THEN2]] = !DILocation(line: 2, column: 19, scope: ![[THENBLOCK]])
9797 ; CHECK: ![[THEN3]] = !DILocation(line: 2, column: 7, scope: ![[THENBLOCK]])
9898 ; CHECK: ![[ELSE]] = !DILocation(line: 2, column: 25, scope: ![[ELSEBLOCK:[0-9]+]])
99 ; CHECK: ![[ELSEBLOCK]] = !DILexicalBlockFile({{.*}} discriminator: 1)
99 ; CHECK: ![[ELSEBLOCK]] = !DILexicalBlockFile({{.*}} discriminator: 2)
100100 ; CHECK: ![[COMBINE]] = !DILocation(line: 2, column: 42, scope: ![[COMBINEBLOCK:[0-9]+]])
101101 ; CHECK: ![[COMBINEBLOCK]] = !DILexicalBlockFile({{.*}} discriminator: 3)