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 | a5b1eef | 2016-08-26 17:58:37 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCStreamer.h" |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetSubtargetInfo.h" |
| 23 | |
| 24 | using namespace llvm; |
| 25 | |
| 26 | DebugHandlerBase::DebugHandlerBase(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {} |
| 27 | |
| 28 | // Each LexicalScope has first instruction and last instruction to mark |
| 29 | // beginning and end of a scope respectively. Create an inverse map that list |
| 30 | // scopes starts (and ends) with an instruction. One instruction may start (or |
| 31 | // end) multiple scopes. Ignore scopes that are not reachable. |
| 32 | void DebugHandlerBase::identifyScopeMarkers() { |
| 33 | SmallVector<LexicalScope *, 4> WorkList; |
| 34 | WorkList.push_back(LScopes.getCurrentFunctionScope()); |
| 35 | while (!WorkList.empty()) { |
| 36 | LexicalScope *S = WorkList.pop_back_val(); |
| 37 | |
| 38 | const SmallVectorImpl<LexicalScope *> &Children = S->getChildren(); |
| 39 | if (!Children.empty()) |
| 40 | WorkList.append(Children.begin(), Children.end()); |
| 41 | |
| 42 | if (S->isAbstractScope()) |
| 43 | continue; |
| 44 | |
| 45 | for (const InsnRange &R : S->getRanges()) { |
| 46 | assert(R.first && "InsnRange does not have first instruction!"); |
| 47 | assert(R.second && "InsnRange does not have second instruction!"); |
| 48 | requestLabelBeforeInsn(R.first); |
| 49 | requestLabelAfterInsn(R.second); |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // Return Label preceding the instruction. |
| 55 | MCSymbol *DebugHandlerBase::getLabelBeforeInsn(const MachineInstr *MI) { |
| 56 | MCSymbol *Label = LabelsBeforeInsn.lookup(MI); |
| 57 | assert(Label && "Didn't insert label before instruction"); |
| 58 | return Label; |
| 59 | } |
| 60 | |
| 61 | // Return Label immediately following the instruction. |
| 62 | MCSymbol *DebugHandlerBase::getLabelAfterInsn(const MachineInstr *MI) { |
| 63 | return LabelsAfterInsn.lookup(MI); |
| 64 | } |
| 65 | |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame^] | 66 | int DebugHandlerBase::fragmentCmp(const DIExpression *P1, |
| 67 | const DIExpression *P2) { |
| 68 | unsigned l1 = P1->getFragmentOffsetInBits(); |
| 69 | unsigned l2 = P2->getFragmentOffsetInBits(); |
| 70 | unsigned r1 = l1 + P1->getFragmentSizeInBits(); |
| 71 | unsigned r2 = l2 + P2->getFragmentSizeInBits(); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 72 | if (r1 <= l2) |
| 73 | return -1; |
| 74 | else if (r2 <= l1) |
| 75 | return 1; |
| 76 | else |
| 77 | return 0; |
| 78 | } |
| 79 | |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame^] | 80 | bool DebugHandlerBase::fragmentsOverlap(const DIExpression *P1, |
| 81 | const DIExpression *P2) { |
| 82 | if (!P1->isFragment() || !P2->isFragment()) |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 83 | return true; |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame^] | 84 | return fragmentCmp(P1, P2) == 0; |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Amjad Aboud | acee568 | 2016-07-12 12:06:34 +0000 | [diff] [blame] | 87 | /// If this type is derived from a base type then return base type size. |
| 88 | uint64_t DebugHandlerBase::getBaseTypeSize(const DITypeRef TyRef) { |
| 89 | DIType *Ty = TyRef.resolve(); |
| 90 | assert(Ty); |
| 91 | DIDerivedType *DDTy = dyn_cast<DIDerivedType>(Ty); |
| 92 | if (!DDTy) |
| 93 | return Ty->getSizeInBits(); |
| 94 | |
| 95 | unsigned Tag = DDTy->getTag(); |
| 96 | |
| 97 | if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef && |
| 98 | Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type && |
Victor Leschuk | e1156c2 | 2016-10-31 19:09:38 +0000 | [diff] [blame] | 99 | Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_atomic_type) |
Amjad Aboud | acee568 | 2016-07-12 12:06:34 +0000 | [diff] [blame] | 100 | return DDTy->getSizeInBits(); |
| 101 | |
| 102 | DIType *BaseType = DDTy->getBaseType().resolve(); |
| 103 | |
| 104 | assert(BaseType && "Unexpected invalid base type"); |
| 105 | |
| 106 | // If this is a derived type, go ahead and get the base type, unless it's a |
| 107 | // reference then it's just the size of the field. Pointer types have no need |
| 108 | // of this since they're a different type of qualification on the type. |
| 109 | if (BaseType->getTag() == dwarf::DW_TAG_reference_type || |
| 110 | BaseType->getTag() == dwarf::DW_TAG_rvalue_reference_type) |
| 111 | return Ty->getSizeInBits(); |
| 112 | |
| 113 | return getBaseTypeSize(BaseType); |
| 114 | } |
| 115 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 116 | void DebugHandlerBase::beginFunction(const MachineFunction *MF) { |
| 117 | // Grab the lexical scopes for the function, if we don't have any of those |
| 118 | // then we're not going to be able to do anything. |
| 119 | LScopes.initialize(*MF); |
| 120 | if (LScopes.empty()) |
| 121 | return; |
| 122 | |
| 123 | // Make sure that each lexical scope will have a begin/end label. |
| 124 | identifyScopeMarkers(); |
| 125 | |
| 126 | // Calculate history for local variables. |
| 127 | assert(DbgValues.empty() && "DbgValues map wasn't cleaned!"); |
| 128 | calculateDbgValueHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(), |
| 129 | DbgValues); |
| 130 | |
| 131 | // Request labels for the full history. |
| 132 | for (const auto &I : DbgValues) { |
| 133 | const auto &Ranges = I.second; |
| 134 | if (Ranges.empty()) |
| 135 | continue; |
| 136 | |
| 137 | // The first mention of a function argument gets the CurrentFnBegin |
| 138 | // label, so arguments are visible when breaking at function entry. |
| 139 | const DILocalVariable *DIVar = Ranges.front().first->getDebugVariable(); |
| 140 | if (DIVar->isParameter() && |
| 141 | getDISubprogram(DIVar->getScope())->describes(MF->getFunction())) { |
| 142 | LabelsBeforeInsn[Ranges.front().first] = Asm->getFunctionBegin(); |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame^] | 143 | if (Ranges.front().first->getDebugExpression()->isFragment()) { |
| 144 | // Mark all non-overlapping initial fragments. |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 145 | for (auto I = Ranges.begin(); I != Ranges.end(); ++I) { |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame^] | 146 | const DIExpression *Fragment = I->first->getDebugExpression(); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 147 | if (std::all_of(Ranges.begin(), I, |
| 148 | [&](DbgValueHistoryMap::InstrRange Pred) { |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame^] | 149 | return !fragmentsOverlap( |
| 150 | Fragment, Pred.first->getDebugExpression()); |
| 151 | })) |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 152 | LabelsBeforeInsn[I->first] = Asm->getFunctionBegin(); |
| 153 | else |
| 154 | break; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | for (const auto &Range : Ranges) { |
| 160 | requestLabelBeforeInsn(Range.first); |
| 161 | if (Range.second) |
| 162 | requestLabelAfterInsn(Range.second); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | PrevInstLoc = DebugLoc(); |
| 167 | PrevLabel = Asm->getFunctionBegin(); |
| 168 | } |
| 169 | |
| 170 | void DebugHandlerBase::beginInstruction(const MachineInstr *MI) { |
| 171 | if (!MMI->hasDebugInfo()) |
| 172 | return; |
| 173 | |
| 174 | assert(CurMI == nullptr); |
| 175 | CurMI = MI; |
| 176 | |
| 177 | // Insert labels where requested. |
| 178 | DenseMap<const MachineInstr *, MCSymbol *>::iterator I = |
| 179 | LabelsBeforeInsn.find(MI); |
| 180 | |
| 181 | // No label needed. |
| 182 | if (I == LabelsBeforeInsn.end()) |
| 183 | return; |
| 184 | |
| 185 | // Label already assigned. |
| 186 | if (I->second) |
| 187 | return; |
| 188 | |
| 189 | if (!PrevLabel) { |
| 190 | PrevLabel = MMI->getContext().createTempSymbol(); |
| 191 | Asm->OutStreamer->EmitLabel(PrevLabel); |
| 192 | } |
| 193 | I->second = PrevLabel; |
| 194 | } |
| 195 | |
| 196 | void DebugHandlerBase::endInstruction() { |
| 197 | if (!MMI->hasDebugInfo()) |
| 198 | return; |
| 199 | |
| 200 | assert(CurMI != nullptr); |
| 201 | // Don't create a new label after DBG_VALUE instructions. |
| 202 | // They don't generate code. |
Kostya Serebryany | b66cb88 | 2016-12-01 02:06:56 +0000 | [diff] [blame] | 203 | if (!CurMI->isDebugValue()) |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 204 | PrevLabel = nullptr; |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 205 | |
| 206 | DenseMap<const MachineInstr *, MCSymbol *>::iterator I = |
| 207 | LabelsAfterInsn.find(CurMI); |
| 208 | CurMI = nullptr; |
| 209 | |
| 210 | // No label needed. |
| 211 | if (I == LabelsAfterInsn.end()) |
| 212 | return; |
| 213 | |
| 214 | // Label already assigned. |
| 215 | if (I->second) |
| 216 | return; |
| 217 | |
| 218 | // We need a label after this instruction. |
| 219 | if (!PrevLabel) { |
| 220 | PrevLabel = MMI->getContext().createTempSymbol(); |
| 221 | Asm->OutStreamer->EmitLabel(PrevLabel); |
| 222 | } |
| 223 | I->second = PrevLabel; |
| 224 | } |
| 225 | |
| 226 | void DebugHandlerBase::endFunction(const MachineFunction *MF) { |
| 227 | DbgValues.clear(); |
| 228 | LabelsBeforeInsn.clear(); |
| 229 | LabelsAfterInsn.clear(); |
| 230 | } |