llvm.org GIT mirror llvm / 1f379ad
[CodeGen] Fix bugs in LiveDebugVariables when debug labels are generated. Remove DBG_LABELs in LiveDebugVariables and generate them in VirtRegRewriter. This bug is reported in https://bugs.chromium.org/p/chromium/issues/detail?id=898152. Differential Revision: https://reviews.llvm.org/D54465 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@351525 91177308-0d34-0410-b5e6-96231b3b80d8 Hsiangkai Wang 1 year, 10 months ago
2 changed file(s) with 266 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
7070 cl::desc("Enable the live debug variables pass"), cl::Hidden);
7171
7272 STATISTIC(NumInsertedDebugValues, "Number of DBG_VALUEs inserted");
73 STATISTIC(NumInsertedDebugLabels, "Number of DBG_LABELs inserted");
7374
7475 char LiveDebugVariables::ID = 0;
7576
338339 void print(raw_ostream &, const TargetRegisterInfo *);
339340 };
340341
342 /// A user label is a part of a debug info user label.
343 class UserLabel {
344 const DILabel *Label; ///< The debug info label we are part of.
345 DebugLoc dl; ///< The debug location for the label. This is
346 ///< used by dwarf writer to find lexical scope.
347 SlotIndex loc; ///< Slot used by the debug label.
348
349 /// Insert a DBG_LABEL into MBB at Idx.
350 void insertDebugLabel(MachineBasicBlock *MBB, SlotIndex Idx,
351 LiveIntervals &LIS, const TargetInstrInfo &TII);
352
353 public:
354 /// Create a new UserLabel.
355 UserLabel(const DILabel *label, DebugLoc L, SlotIndex Idx)
356 : Label(label), dl(std::move(L)), loc(Idx) {}
357
358 /// Does this UserLabel match the parameters?
359 bool match(const DILabel *L, const DILocation *IA,
360 const SlotIndex Index) const {
361 return Label == L && dl->getInlinedAt() == IA && loc == Index;
362 }
363
364 /// Recreate DBG_LABEL instruction from data structures.
365 void emitDebugLabel(LiveIntervals &LIS, const TargetInstrInfo &TII);
366
367 /// Return DebugLoc of this UserLabel.
368 DebugLoc getDebugLoc() { return dl; }
369
370 void print(raw_ostream &, const TargetRegisterInfo *);
371 };
372
341373 /// Implementation of the LiveDebugVariables pass.
342374 class LDVImpl {
343375 LiveDebugVariables &pass;
355387 /// All allocated UserValue instances.
356388 SmallVector, 8> userValues;
357389
390 /// All allocated UserLabel instances.
391 SmallVector, 2> userLabels;
392
358393 /// Map virtual register to eq class leader.
359394 using VRMap = DenseMap;
360395 VRMap virtRegToEqClass;
378413 /// \returns True if the DBG_VALUE instruction should be deleted.
379414 bool handleDebugValue(MachineInstr &MI, SlotIndex Idx);
380415
416 /// Add DBG_LABEL instruction to UserLabel.
417 ///
418 /// \param MI DBG_LABEL instruction
419 /// \param Idx Last valid SlotIndex before instruction.
420 ///
421 /// \returns True if the DBG_LABEL instruction should be deleted.
422 bool handleDebugLabel(MachineInstr &MI, SlotIndex Idx);
423
381424 /// Collect and erase all DBG_VALUE instructions, adding a UserValue def
382425 /// for each instruction.
383426 ///
399442 void clear() {
400443 MF = nullptr;
401444 userValues.clear();
445 userLabels.clear();
402446 virtRegToEqClass.clear();
403447 userVarMap.clear();
404448 // Make sure we call emitDebugValues if the machine function was modified.
444488 CommentOS << " ]";
445489 }
446490
447 static void printExtendedName(raw_ostream &OS, const DILocalVariable *V,
491 static void printExtendedName(raw_ostream &OS, const DINode *Node,
448492 const DILocation *DL) {
449 const LLVMContext &Ctx = V->getContext();
450 StringRef Res = V->getName();
493 const LLVMContext &Ctx = Node->getContext();
494 StringRef Res;
495 unsigned Line;
496 if (const auto *V = dyn_cast(Node)) {
497 Res = V->getName();
498 Line = V->getLine();
499 } else if (const auto *L = dyn_cast(Node)) {
500 Res = L->getName();
501 Line = L->getLine();
502 }
503
451504 if (!Res.empty())
452 OS << Res << "," << V->getLine();
505 OS << Res << "," << Line;
453506 if (auto *InlinedAt = DL->getInlinedAt()) {
454507 if (DebugLoc InlinedAtDL = InlinedAt) {
455508 OS << " @[";
460513 }
461514
462515 void UserValue::print(raw_ostream &OS, const TargetRegisterInfo *TRI) {
463 auto *DV = cast(Variable);
464516 OS << "!\"";
465 printExtendedName(OS, DV, dl);
517 printExtendedName(OS, Variable, dl);
466518
467519 OS << "\"\t";
468520 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) {
482534 OS << '\n';
483535 }
484536
537 void UserLabel::print(raw_ostream &OS, const TargetRegisterInfo *TRI) {
538 OS << "!\"";
539 printExtendedName(OS, Label, dl);
540
541 OS << "\"\t";
542 OS << loc;
543 OS << '\n';
544 }
545
485546 void LDVImpl::print(raw_ostream &OS) {
486547 OS << "********** DEBUG VARIABLES **********\n";
487 for (unsigned i = 0, e = userValues.size(); i != e; ++i)
488 userValues[i]->print(OS, TRI);
548 for (auto &userValue : userValues)
549 userValue->print(OS, TRI);
550 OS << "********** DEBUG LABELS **********\n";
551 for (auto &userLabel : userLabels)
552 userLabel->print(OS, TRI);
489553 }
490554 #endif
491555
586650 return true;
587651 }
588652
653 bool LDVImpl::handleDebugLabel(MachineInstr &MI, SlotIndex Idx) {
654 // DBG_LABEL label
655 if (MI.getNumOperands() != 1 || !MI.getOperand(0).isMetadata()) {
656 LLVM_DEBUG(dbgs() << "Can't handle " << MI);
657 return false;
658 }
659
660 // Get or create the UserLabel for label here.
661 const DILabel *Label = MI.getDebugLabel();
662 const DebugLoc &DL = MI.getDebugLoc();
663 bool Found = false;
664 for (auto const &L : userLabels) {
665 if (L->match(Label, DL->getInlinedAt(), Idx)) {
666 Found = true;
667 break;
668 }
669 }
670 if (!Found)
671 userLabels.push_back(llvm::make_unique(Label, DL, Idx));
672
673 return true;
674 }
675
589676 bool LDVImpl::collectDebugValues(MachineFunction &mf) {
590677 bool Changed = false;
591678 for (MachineFunction::iterator MFI = mf.begin(), MFE = mf.end(); MFI != MFE;
609696 do {
610697 // Only handle DBG_VALUE in handleDebugValue(). Skip all other
611698 // kinds of debug instructions.
612 if (MBBI->isDebugValue() && handleDebugValue(*MBBI, Idx)) {
699 if ((MBBI->isDebugValue() && handleDebugValue(*MBBI, Idx)) ||
700 (MBBI->isDebugLabel() && handleDebugLabel(*MBBI, Idx))) {
613701 MBBI = MBB->erase(MBBI);
614702 Changed = true;
615703 } else
12461334 } while (I != MBB->end());
12471335 }
12481336
1337 void UserLabel::insertDebugLabel(MachineBasicBlock *MBB, SlotIndex Idx,
1338 LiveIntervals &LIS,
1339 const TargetInstrInfo &TII) {
1340 MachineBasicBlock::iterator I = findInsertLocation(MBB, Idx, LIS);
1341 ++NumInsertedDebugLabels;
1342 BuildMI(*MBB, I, getDebugLoc(), TII.get(TargetOpcode::DBG_LABEL))
1343 .addMetadata(Label);
1344 }
1345
12491346 void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
12501347 const TargetInstrInfo &TII,
12511348 const TargetRegisterInfo &TRI,
12941391 }
12951392 }
12961393
1394 void UserLabel::emitDebugLabel(LiveIntervals &LIS, const TargetInstrInfo &TII) {
1395 LLVM_DEBUG(dbgs() << "\t" << loc);
1396 MachineFunction::iterator MBB = LIS.getMBBFromIndex(loc)->getIterator();
1397
1398 LLVM_DEBUG(dbgs() << ' ' << printMBBReference(*MBB));
1399 insertDebugLabel(&*MBB, loc, LIS, TII);
1400
1401 LLVM_DEBUG(dbgs() << '\n');
1402 }
1403
12971404 void LDVImpl::emitDebugValues(VirtRegMap *VRM) {
12981405 LLVM_DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n");
12991406 if (!MF)
13001407 return;
13011408 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
13021409 SpillOffsetMap SpillOffsets;
1303 for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
1304 LLVM_DEBUG(userValues[i]->print(dbgs(), TRI));
1305 userValues[i]->rewriteLocations(*VRM, *MF, *TII, *TRI, SpillOffsets);
1306 userValues[i]->emitDebugValues(VRM, *LIS, *TII, *TRI, SpillOffsets);
1410 for (auto &userValue : userValues) {
1411 LLVM_DEBUG(userValue->print(dbgs(), TRI));
1412 userValue->rewriteLocations(*VRM, *MF, *TII, *TRI, SpillOffsets);
1413 userValue->emitDebugValues(VRM, *LIS, *TII, *TRI, SpillOffsets);
1414 }
1415 LLVM_DEBUG(dbgs() << "********** EMITTING LIVE DEBUG LABELS **********\n");
1416 for (auto &userLabel : userLabels) {
1417 LLVM_DEBUG(userLabel->print(dbgs(), TRI));
1418 userLabel->emitDebugLabel(*LIS, *TII);
13071419 }
13081420 EmitDone = true;
13091421 }
0 ; RUN: llc < %s -stop-after=virtregrewriter -o - | FileCheck %s
1 ;
2 ; Generated with "clang++ -g -O1 -S -emit-llvm"
3 ;
4 ; inline bool bar(char c) {
5 ; return c >= '0' && c <= '9';
6 ; }
7 ;
8 ; unsigned foo(const char* data,
9 ; int length,
10 ; int* parsing_result) {
11 ; unsigned value = 0;
12 ; int result = 1;
13 ; bool overflow = 0;
14 ;
15 ; while (bar(*data)) {
16 ; if (value > 1) {
17 ; result = 2;
18 ; overflow = 1;
19 ; }
20 ;
21 ; if (!overflow)
22 ; value = value + 1;
23 ; }
24 ;
25 ; if (length == 0 || value) {
26 ; if (!overflow)
27 ; result = 0;
28 ; } else {
29 ; result = 1;
30 ; }
31 ; bye:
32 ; *parsing_result = result;
33 ; return result == 0 ? value : 0;
34 ; }
35 ;
36 ; CHECK: {{^body:}}
37 ; CHECK: bye.thread21:
38 ; CHECK: DBG_LABEL !14
39 ; CHECK: if.then5:
40 ; CHECK: DBG_LABEL !14
41 ; CHECK-NOT: DBG_LABEL !14
42
43 $_Z3barc = comdat any
44
45 ; Function Attrs: nounwind uwtable
46 define dso_local i32 @_Z3fooPKciPi(i8* nocapture readonly %data, i32 %length, i32* nocapture %parsing_result) local_unnamed_addr !dbg !4 {
47 entry:
48 %0 = load i8, i8* %data, align 1
49 %call23 = tail call zeroext i1 @_Z3barc(i8 signext %0), !dbg !15
50 br i1 %call23, label %while.body, label %while.end
51
52 while.body: ; preds = %entry, %while.body
53 %overflow.026 = phi i8 [ %spec.select18, %while.body ], [ 0, %entry ]
54 %result.025 = phi i32 [ %spec.select, %while.body ], [ 1, %entry ]
55 %value.024 = phi i32 [ %value.1, %while.body ], [ 0, %entry ]
56 %cmp = icmp ugt i32 %value.024, 1
57 %spec.select = select i1 %cmp, i32 2, i32 %result.025
58 %spec.select18 = select i1 %cmp, i8 1, i8 %overflow.026
59 %1 = and i8 %spec.select18, 1
60 %2 = xor i8 %1, 1
61 %3 = zext i8 %2 to i32
62 %value.1 = add i32 %value.024, %3
63 %4 = load i8, i8* %data, align 1
64 %call = tail call zeroext i1 @_Z3barc(i8 signext %4), !dbg !15
65 br i1 %call, label %while.body, label %while.end.loopexit
66
67 while.end.loopexit: ; preds = %while.body
68 %phitmp = and i8 %spec.select18, 1
69 br label %while.end
70
71 while.end: ; preds = %while.end.loopexit, %entry
72 %value.0.lcssa = phi i32 [ 0, %entry ], [ %value.1, %while.end.loopexit ]
73 %result.0.lcssa = phi i32 [ 1, %entry ], [ %spec.select, %while.end.loopexit ]
74 %overflow.0.lcssa = phi i8 [ 0, %entry ], [ %phitmp, %while.end.loopexit ]
75 %cmp3 = icmp eq i32 %length, 0
76 %tobool4 = icmp ne i32 %value.0.lcssa, 0
77 %or.cond = or i1 %cmp3, %tobool4
78 br i1 %or.cond, label %if.then5, label %bye.thread21
79
80 bye.thread21: ; preds = %while.end
81 call void @llvm.dbg.label(metadata !14), !dbg !16
82 store i32 1, i32* %parsing_result, align 4
83 br label %6
84
85 if.then5: ; preds = %while.end
86 %tobool6 = icmp eq i8 %overflow.0.lcssa, 0
87 call void @llvm.dbg.label(metadata !14), !dbg !16
88 call void @llvm.dbg.label(metadata !14), !dbg !16
89 br i1 %tobool6, label %bye.thread, label %bye
90
91 bye.thread: ; preds = %if.then5
92 store i32 0, i32* %parsing_result, align 4
93 br label %5
94
95 bye: ; preds = %if.then5
96 store i32 %result.0.lcssa, i32* %parsing_result, align 4
97 %cmp10 = icmp eq i32 %result.0.lcssa, 0
98 br i1 %cmp10, label %5, label %6
99
100 ;
101 br label %6
102
103 ;
104 %7 = phi i32 [ %value.0.lcssa, %5 ], [ 0, %bye ], [ 0, %bye.thread21 ]
105 ret i32 %7
106 }
107
108 ; Function Attrs: inlinehint nounwind uwtable
109 define linkonce_odr dso_local zeroext i1 @_Z3barc(i8 signext %c) local_unnamed_addr comdat {
110 entry:
111 %c.off = add i8 %c, -48
112 %0 = icmp ult i8 %c.off, 10
113 ret i1 %0
114 }
115
116 ; Function Attrs: nounwind readnone speculatable
117 declare void @llvm.dbg.label(metadata) #0
118
119 attributes #0 = { nounwind readnone speculatable }
120
121 !llvm.dbg.cu = !{!0}
122 !llvm.module.flags = !{!3}
123
124 !0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, nameTableKind: None)
125 !1 = !DIFile(filename: "live-debug-label.cc", directory: ".")
126 !2 = !{}
127 !3 = !{i32 2, !"Debug Info Version", i32 3}
128 !4 = distinct !DISubprogram(name: "foo", linkageName: "_Z3fooPKciPi", scope: !1, file: !1, line: 5, type: !5, isLocal: false, isDefinition: true, scopeLine: 7, flags: DIFlagPrototyped, isOptimized: true, unit: !0, retainedNodes: !13)
129 !5 = !DISubroutineType(types: !6)
130 !6 = !{!7, !8, !11, !12}
131 !7 = !DIBasicType(name: "unsigned int", size: 32, encoding: DW_ATE_unsigned)
132 !8 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !9, size: 64)
133 !9 = !DIDerivedType(tag: DW_TAG_const_type, baseType: !10)
134 !10 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char)
135 !11 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
136 !12 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !11, size: 64)
137 !13 = !{!14}
138 !14 = !DILabel(scope: !4, name: "bye", file: !1, line: 28)
139 !15 = !DILocation(line: 12, column: 10, scope: !4)
140 !16 = !DILocation(line: 28, column: 1, scope: !4)