Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 1 | //===-- llvm/lib/CodeGen/AsmPrinter/DebugHandlerBase.cpp -------*- C++ -*--===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Common functionality for different debug information format backends. |
| 11 | // LLVM currently supports DWARF and CodeView. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "DebugHandlerBase.h" |
| 16 | #include "llvm/CodeGen/AsmPrinter.h" |
| 17 | #include "llvm/CodeGen/MachineFunction.h" |
| 18 | #include "llvm/CodeGen/MachineInstr.h" |
| 19 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Reid Kleckner | 2886580 | 2016-04-14 18:29:59 +0000 | [diff] [blame] | 20 | #include "llvm/IR/DebugInfo.h" |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetSubtargetInfo.h" |
| 22 | |
| 23 | using namespace llvm; |
| 24 | |
| 25 | DebugHandlerBase::DebugHandlerBase(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {} |
| 26 | |
| 27 | // Each LexicalScope has first instruction and last instruction to mark |
| 28 | // beginning and end of a scope respectively. Create an inverse map that list |
| 29 | // scopes starts (and ends) with an instruction. One instruction may start (or |
| 30 | // end) multiple scopes. Ignore scopes that are not reachable. |
| 31 | void DebugHandlerBase::identifyScopeMarkers() { |
| 32 | SmallVector<LexicalScope *, 4> WorkList; |
| 33 | WorkList.push_back(LScopes.getCurrentFunctionScope()); |
| 34 | while (!WorkList.empty()) { |
| 35 | LexicalScope *S = WorkList.pop_back_val(); |
| 36 | |
| 37 | const SmallVectorImpl<LexicalScope *> &Children = S->getChildren(); |
| 38 | if (!Children.empty()) |
| 39 | WorkList.append(Children.begin(), Children.end()); |
| 40 | |
| 41 | if (S->isAbstractScope()) |
| 42 | continue; |
| 43 | |
| 44 | for (const InsnRange &R : S->getRanges()) { |
| 45 | assert(R.first && "InsnRange does not have first instruction!"); |
| 46 | assert(R.second && "InsnRange does not have second instruction!"); |
| 47 | requestLabelBeforeInsn(R.first); |
| 48 | requestLabelAfterInsn(R.second); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // Return Label preceding the instruction. |
| 54 | MCSymbol *DebugHandlerBase::getLabelBeforeInsn(const MachineInstr *MI) { |
| 55 | MCSymbol *Label = LabelsBeforeInsn.lookup(MI); |
| 56 | assert(Label && "Didn't insert label before instruction"); |
| 57 | return Label; |
| 58 | } |
| 59 | |
| 60 | // Return Label immediately following the instruction. |
| 61 | MCSymbol *DebugHandlerBase::getLabelAfterInsn(const MachineInstr *MI) { |
| 62 | return LabelsAfterInsn.lookup(MI); |
| 63 | } |
| 64 | |
| 65 | // Determine the relative position of the pieces described by P1 and P2. |
| 66 | // Returns -1 if P1 is entirely before P2, 0 if P1 and P2 overlap, |
| 67 | // 1 if P1 is entirely after P2. |
| 68 | int DebugHandlerBase::pieceCmp(const DIExpression *P1, const DIExpression *P2) { |
| 69 | unsigned l1 = P1->getBitPieceOffset(); |
| 70 | unsigned l2 = P2->getBitPieceOffset(); |
| 71 | unsigned r1 = l1 + P1->getBitPieceSize(); |
| 72 | unsigned r2 = l2 + P2->getBitPieceSize(); |
| 73 | if (r1 <= l2) |
| 74 | return -1; |
| 75 | else if (r2 <= l1) |
| 76 | return 1; |
| 77 | else |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | /// Determine whether two variable pieces overlap. |
| 82 | bool DebugHandlerBase::piecesOverlap(const DIExpression *P1, const DIExpression *P2) { |
| 83 | if (!P1->isBitPiece() || !P2->isBitPiece()) |
| 84 | return true; |
| 85 | return pieceCmp(P1, P2) == 0; |
| 86 | } |
| 87 | |
| 88 | void DebugHandlerBase::beginFunction(const MachineFunction *MF) { |
| 89 | // Grab the lexical scopes for the function, if we don't have any of those |
| 90 | // then we're not going to be able to do anything. |
| 91 | LScopes.initialize(*MF); |
| 92 | if (LScopes.empty()) |
| 93 | return; |
| 94 | |
| 95 | // Make sure that each lexical scope will have a begin/end label. |
| 96 | identifyScopeMarkers(); |
| 97 | |
| 98 | // Calculate history for local variables. |
| 99 | assert(DbgValues.empty() && "DbgValues map wasn't cleaned!"); |
| 100 | calculateDbgValueHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(), |
| 101 | DbgValues); |
| 102 | |
| 103 | // Request labels for the full history. |
| 104 | for (const auto &I : DbgValues) { |
| 105 | const auto &Ranges = I.second; |
| 106 | if (Ranges.empty()) |
| 107 | continue; |
| 108 | |
| 109 | // The first mention of a function argument gets the CurrentFnBegin |
| 110 | // label, so arguments are visible when breaking at function entry. |
| 111 | const DILocalVariable *DIVar = Ranges.front().first->getDebugVariable(); |
| 112 | if (DIVar->isParameter() && |
| 113 | getDISubprogram(DIVar->getScope())->describes(MF->getFunction())) { |
| 114 | LabelsBeforeInsn[Ranges.front().first] = Asm->getFunctionBegin(); |
| 115 | if (Ranges.front().first->getDebugExpression()->isBitPiece()) { |
| 116 | // Mark all non-overlapping initial pieces. |
| 117 | for (auto I = Ranges.begin(); I != Ranges.end(); ++I) { |
| 118 | const DIExpression *Piece = I->first->getDebugExpression(); |
| 119 | if (std::all_of(Ranges.begin(), I, |
| 120 | [&](DbgValueHistoryMap::InstrRange Pred) { |
| 121 | return !piecesOverlap(Piece, Pred.first->getDebugExpression()); |
| 122 | })) |
| 123 | LabelsBeforeInsn[I->first] = Asm->getFunctionBegin(); |
| 124 | else |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | for (const auto &Range : Ranges) { |
| 131 | requestLabelBeforeInsn(Range.first); |
| 132 | if (Range.second) |
| 133 | requestLabelAfterInsn(Range.second); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | PrevInstLoc = DebugLoc(); |
| 138 | PrevLabel = Asm->getFunctionBegin(); |
| 139 | } |
| 140 | |
| 141 | void DebugHandlerBase::beginInstruction(const MachineInstr *MI) { |
| 142 | if (!MMI->hasDebugInfo()) |
| 143 | return; |
| 144 | |
| 145 | assert(CurMI == nullptr); |
| 146 | CurMI = MI; |
| 147 | |
| 148 | // Insert labels where requested. |
| 149 | DenseMap<const MachineInstr *, MCSymbol *>::iterator I = |
| 150 | LabelsBeforeInsn.find(MI); |
| 151 | |
| 152 | // No label needed. |
| 153 | if (I == LabelsBeforeInsn.end()) |
| 154 | return; |
| 155 | |
| 156 | // Label already assigned. |
| 157 | if (I->second) |
| 158 | return; |
| 159 | |
| 160 | if (!PrevLabel) { |
| 161 | PrevLabel = MMI->getContext().createTempSymbol(); |
| 162 | Asm->OutStreamer->EmitLabel(PrevLabel); |
| 163 | } |
| 164 | I->second = PrevLabel; |
| 165 | } |
| 166 | |
| 167 | void DebugHandlerBase::endInstruction() { |
| 168 | if (!MMI->hasDebugInfo()) |
| 169 | return; |
| 170 | |
| 171 | assert(CurMI != nullptr); |
| 172 | // Don't create a new label after DBG_VALUE instructions. |
| 173 | // They don't generate code. |
| 174 | if (!CurMI->isDebugValue()) |
| 175 | PrevLabel = nullptr; |
| 176 | |
| 177 | DenseMap<const MachineInstr *, MCSymbol *>::iterator I = |
| 178 | LabelsAfterInsn.find(CurMI); |
| 179 | CurMI = nullptr; |
| 180 | |
| 181 | // No label needed. |
| 182 | if (I == LabelsAfterInsn.end()) |
| 183 | return; |
| 184 | |
| 185 | // Label already assigned. |
| 186 | if (I->second) |
| 187 | return; |
| 188 | |
| 189 | // We need a label after this instruction. |
| 190 | if (!PrevLabel) { |
| 191 | PrevLabel = MMI->getContext().createTempSymbol(); |
| 192 | Asm->OutStreamer->EmitLabel(PrevLabel); |
| 193 | } |
| 194 | I->second = PrevLabel; |
| 195 | } |
| 196 | |
| 197 | void DebugHandlerBase::endFunction(const MachineFunction *MF) { |
| 198 | DbgValues.clear(); |
| 199 | LabelsBeforeInsn.clear(); |
| 200 | LabelsAfterInsn.clear(); |
| 201 | } |