llvm.org GIT mirror llvm / 7860aaa
[Mem2Reg] Create merged debug locations for inserted phis Track the debug locations of the incoming values to newly-created phis, and apply merged debug locations to the phis. A merged location will be on line 0, but will have the correct scope set. This improves crash reporting when an inlined instruction with a merged location triggers a machine exception. A debugger will be able to narrow down the crash to the correct inlined scope, instead of simply pointing to the outer scope of the caller. Taken together with a change allows generating merged line-0 locations for instructions which aren't calls, this results in a 0.5% increase in the uncompressed size of the .debug_line section of a stage2+Release build of clang (-O3 -g). rdar://33858697 Differential Revision: https://reviews.llvm.org/D45397 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@330227 91177308-0d34-0410-b5e6-96231b3b80d8 Vedant Kumar 1 year, 6 months ago
2 changed file(s) with 143 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
166166 /// Data package used by RenamePass().
167167 struct RenamePassData {
168168 using ValVector = std::vector;
169
170 RenamePassData(BasicBlock *B, BasicBlock *P, ValVector V)
171 : BB(B), Pred(P), Values(std::move(V)) {}
169 using LocationVector = std::vector;
170
171 RenamePassData(BasicBlock *B, BasicBlock *P, ValVector V, LocationVector L)
172 : BB(B), Pred(P), Values(std::move(V)), Locations(std::move(L)) {}
172173
173174 BasicBlock *BB;
174175 BasicBlock *Pred;
175176 ValVector Values;
177 LocationVector Locations;
176178 };
177179
178180 /// \brief This assigns and keeps a per-bb relative ordering of load/store
301303 SmallPtrSetImpl &LiveInBlocks);
302304 void RenamePass(BasicBlock *BB, BasicBlock *Pred,
303305 RenamePassData::ValVector &IncVals,
306 RenamePassData::LocationVector &IncLocs,
304307 std::vector &Worklist);
305308 bool QueuePhiNode(BasicBlock *BB, unsigned AllocaIdx, unsigned &Version);
306309 };
651654 for (unsigned i = 0, e = Allocas.size(); i != e; ++i)
652655 Values[i] = UndefValue::get(Allocas[i]->getAllocatedType());
653656
657 // When handling debug info, treat all incoming values as if they have unknown
658 // locations until proven otherwise.
659 RenamePassData::LocationVector Locations(Allocas.size());
660
654661 // Walks all basic blocks in the function performing the SSA rename algorithm
655662 // and inserting the phi nodes we marked as necessary
656663 std::vector RenamePassWorkList;
657 RenamePassWorkList.emplace_back(&F.front(), nullptr, std::move(Values));
664 RenamePassWorkList.emplace_back(&F.front(), nullptr, std::move(Values),
665 std::move(Locations));
658666 do {
659667 RenamePassData RPD = std::move(RenamePassWorkList.back());
660668 RenamePassWorkList.pop_back();
661669 // RenamePass may add new worklist entries.
662 RenamePass(RPD.BB, RPD.Pred, RPD.Values, RenamePassWorkList);
670 RenamePass(RPD.BB, RPD.Pred, RPD.Values, RPD.Locations, RenamePassWorkList);
663671 } while (!RenamePassWorkList.empty());
664672
665673 // The renamer uses the Visited set to avoid infinite loops. Clear it now.
866874 return true;
867875 }
868876
877 /// Update the debug location of a phi. \p ApplyMergedLoc indicates whether to
878 /// create a merged location incorporating \p DL, or to set \p DL directly.
879 static void updateForIncomingValueLocation(PHINode *PN, DebugLoc DL,
880 bool ApplyMergedLoc) {
881 if (ApplyMergedLoc)
882 PN->applyMergedLocation(PN->getDebugLoc(), DL);
883 else
884 PN->setDebugLoc(DL);
885 }
886
869887 /// \brief Recursively traverse the CFG of the function, renaming loads and
870888 /// stores to the allocas which we are promoting.
871889 ///
873891 /// predecessor block Pred.
874892 void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred,
875893 RenamePassData::ValVector &IncomingVals,
894 RenamePassData::LocationVector &IncomingLocs,
876895 std::vector &Worklist) {
877896 NextIteration:
878897 // If we are inserting any phi nodes into this BB, they will already be in the
897916 do {
898917 unsigned AllocaNo = PhiToAllocaMap[APN];
899918
919 // Update the location of the phi node.
920 updateForIncomingValueLocation(APN, IncomingLocs[AllocaNo],
921 APN->getNumIncomingValues() > 0);
922
900923 // Add N incoming values to the PHI node.
901924 for (unsigned i = 0; i != NumEdges; ++i)
902925 APN->addIncoming(IncomingVals[AllocaNo], Pred);
958981 continue;
959982
960983 // what value were we writing?
961 IncomingVals[ai->second] = SI->getOperand(0);
984 unsigned AllocaNo = ai->second;
985 IncomingVals[AllocaNo] = SI->getOperand(0);
986
962987 // Record debuginfo for the store before removing it.
988 IncomingLocs[AllocaNo] = SI->getDebugLoc();
963989 for (DbgInfoIntrinsic *DII : AllocaDbgDeclares[ai->second])
964990 ConvertDebugDeclareToDebugValue(DII, SI, DIB);
965991 BB->getInstList().erase(SI);
9821008
9831009 for (; I != E; ++I)
9841010 if (VisitedSuccs.insert(*I).second)
985 Worklist.emplace_back(*I, Pred, IncomingVals);
1011 Worklist.emplace_back(*I, Pred, IncomingVals, IncomingLocs);
9861012
9871013 goto NextIteration;
9881014 }
0 ; RUN: opt -S < %s -mem2reg -verify | FileCheck %s
1
2 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
3 target triple = "x86_64-apple-macosx10.13.0"
4
5 ; Original source (with some whitespace removed):
6 ;
7 ; extern int *getp();
8 ; extern int cond();
9 ; int get1() { return *getp(); }
10 ; int get2(int *p) { return *p; }
11 ; int bug(int *p) {
12 ; if (cond()) return get1();
13 ; else return get2(p);
14 ; }
15
16 define i32 @get1() !dbg !8 {
17 %1 = call i32* (...) @getp(), !dbg !12
18 %2 = load i32, i32* %1, align 4, !dbg !13
19 ret i32 %2, !dbg !14
20 }
21
22 declare i32* @getp(...)
23
24 define i32 @get2(i32*) !dbg !15 {
25 %2 = alloca i32*, align 8
26 store i32* %0, i32** %2, align 8
27 call void @llvm.dbg.declare(metadata i32** %2, metadata !19, metadata !DIExpression()), !dbg !20
28 %3 = load i32*, i32** %2, align 8, !dbg !21
29 %4 = load i32, i32* %3, align 4, !dbg !22
30 ret i32 %4, !dbg !23
31 }
32
33 declare void @llvm.dbg.declare(metadata, metadata, metadata)
34
35 ; CHECK-LABEL: define i32 @bug
36 define i32 @bug(i32*) !dbg !24 {
37 %2 = alloca i32, align 4
38 %3 = alloca i32*, align 8
39 store i32* %0, i32** %3, align 8
40 call void @llvm.dbg.declare(metadata i32** %3, metadata !25, metadata !DIExpression()), !dbg !26
41 %4 = call i32 (...) @cond(), !dbg !27
42 %5 = icmp ne i32 %4, 0, !dbg !27
43 br i1 %5, label %6, label %8, !dbg !29
44
45 ;
46 %7 = call i32 @get1(), !dbg !30
47 store i32 %7, i32* %2, align 4, !dbg !31
48 br label %11, !dbg !31
49
50 ;
51 %9 = load i32*, i32** %3, align 8, !dbg !32
52 %10 = call i32 @get2(i32* %9), !dbg !33
53 store i32 %10, i32* %2, align 4, !dbg !34
54 br label %11, !dbg !34
55
56 ;
57 %12 = load i32, i32* %2, align 4, !dbg !35
58 ret i32 %12, !dbg !35
59
60 ; CHECK: [[phi:%.*]] = phi i32 [ {{.*}} ], [ {{.*}} ], !dbg [[mergedLoc:![0-9]+]]
61 ; CHECK-NEXT: ret i32 [[phi]], !dbg [[retLoc:![0-9]+]]
62 }
63
64 ; CHECK: [[commonScope:![0-9]+]] = distinct !DILexicalBlock(scope: {{.*}}, file: !1, line: 15, column: 7)
65 ; CHECK: [[mergedLoc]] = !DILocation(line: 0, scope: [[commonScope]])
66 ; CHECK: [[retLoc]] = !DILocation(line: 23, column: 1
67
68 declare i32 @cond(...)
69
70 !llvm.dbg.cu = !{!0}
71 !llvm.module.flags = !{!3, !4, !5, !6}
72 !llvm.ident = !{!7}
73
74 !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "Apple LLVM version 9.1.0 (clang-902.2.37.2)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
75 !1 = !DIFile(filename: "bug.c", directory: "/bug")
76 !2 = !{}
77 !3 = !{i32 2, !"Dwarf Version", i32 4}
78 !4 = !{i32 2, !"Debug Info Version", i32 3}
79 !5 = !{i32 1, !"wchar_size", i32 4}
80 !6 = !{i32 7, !"PIC Level", i32 2}
81 !7 = !{!"Apple LLVM version 9.1.0 (clang-902.2.37.2)"}
82 !8 = distinct !DISubprogram(name: "get1", scope: !1, file: !1, line: 6, type: !9, isLocal: false, isDefinition: true, scopeLine: 6, isOptimized: false, unit: !0, variables: !2)
83 !9 = !DISubroutineType(types: !10)
84 !10 = !{!11}
85 !11 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
86 !12 = !DILocation(line: 7, column: 11, scope: !8)
87 !13 = !DILocation(line: 7, column: 10, scope: !8)
88 !14 = !DILocation(line: 7, column: 3, scope: !8)
89 !15 = distinct !DISubprogram(name: "get2", scope: !1, file: !1, line: 10, type: !16, isLocal: false, isDefinition: true, scopeLine: 10, flags: DIFlagPrototyped, isOptimized: false, unit: !0, variables: !2)
90 !16 = !DISubroutineType(types: !17)
91 !17 = !{!11, !18}
92 !18 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !11, size: 64)
93 !19 = !DILocalVariable(name: "p", arg: 1, scope: !15, file: !1, line: 10, type: !18)
94 !20 = !DILocation(line: 10, column: 15, scope: !15)
95 !21 = !DILocation(line: 11, column: 11, scope: !15)
96 !22 = !DILocation(line: 11, column: 10, scope: !15)
97 !23 = !DILocation(line: 11, column: 3, scope: !15)
98 !24 = distinct !DISubprogram(name: "bug", scope: !1, file: !1, line: 14, type: !16, isLocal: false, isDefinition: true, scopeLine: 14, flags: DIFlagPrototyped, isOptimized: false, unit: !0, variables: !2)
99 !25 = !DILocalVariable(name: "p", arg: 1, scope: !24, file: !1, line: 14, type: !18)
100 !26 = !DILocation(line: 14, column: 14, scope: !24)
101 !27 = !DILocation(line: 15, column: 7, scope: !28)
102 !28 = distinct !DILexicalBlock(scope: !24, file: !1, line: 15, column: 7)
103 !29 = !DILocation(line: 15, column: 7, scope: !24)
104 !30 = !DILocation(line: 16, column: 12, scope: !28)
105 !31 = !DILocation(line: 16, column: 5, scope: !28)
106 !32 = !DILocation(line: 18, column: 17, scope: !28)
107 !33 = !DILocation(line: 18, column: 12, scope: !28)
108 !34 = !DILocation(line: 18, column: 5, scope: !28)
109 !35 = !DILocation(line: 23, column: 1, scope: !24)