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" |
Bob Haarman | 1a4cbbe | 2017-08-30 17:50:21 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Optional.h" |
| 17 | #include "llvm/ADT/Twine.h" |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/AsmPrinter.h" |
| 19 | #include "llvm/CodeGen/MachineFunction.h" |
| 20 | #include "llvm/CodeGen/MachineInstr.h" |
| 21 | #include "llvm/CodeGen/MachineModuleInfo.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Reid Kleckner | 2886580 | 2016-04-14 18:29:59 +0000 | [diff] [blame] | 23 | #include "llvm/IR/DebugInfo.h" |
Reid Kleckner | a5b1eef | 2016-08-26 17:58:37 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCStreamer.h" |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace llvm; |
| 27 | |
Bob Haarman | 1a4cbbe | 2017-08-30 17:50:21 +0000 | [diff] [blame] | 28 | Optional<DbgVariableLocation> |
| 29 | DbgVariableLocation::extractFromMachineInstruction( |
| 30 | const MachineInstr &Instruction) { |
| 31 | DbgVariableLocation Location; |
Bob Haarman | 223303c | 2017-08-29 20:59:25 +0000 | [diff] [blame] | 32 | if (!Instruction.isDebugValue()) |
Bob Haarman | 1a4cbbe | 2017-08-30 17:50:21 +0000 | [diff] [blame] | 33 | return None; |
Bob Haarman | 223303c | 2017-08-29 20:59:25 +0000 | [diff] [blame] | 34 | if (!Instruction.getOperand(0).isReg()) |
Bob Haarman | 1a4cbbe | 2017-08-30 17:50:21 +0000 | [diff] [blame] | 35 | return None; |
Bob Haarman | 223303c | 2017-08-29 20:59:25 +0000 | [diff] [blame] | 36 | Location.Register = Instruction.getOperand(0).getReg(); |
Bob Haarman | 223303c | 2017-08-29 20:59:25 +0000 | [diff] [blame] | 37 | Location.FragmentInfo.reset(); |
| 38 | // We only handle expressions generated by DIExpression::appendOffset, |
| 39 | // which doesn't require a full stack machine. |
| 40 | int64_t Offset = 0; |
| 41 | const DIExpression *DIExpr = Instruction.getDebugExpression(); |
| 42 | auto Op = DIExpr->expr_op_begin(); |
| 43 | while (Op != DIExpr->expr_op_end()) { |
| 44 | switch (Op->getOp()) { |
| 45 | case dwarf::DW_OP_constu: { |
| 46 | int Value = Op->getArg(0); |
| 47 | ++Op; |
| 48 | if (Op != DIExpr->expr_op_end()) { |
| 49 | switch (Op->getOp()) { |
| 50 | case dwarf::DW_OP_minus: |
| 51 | Offset -= Value; |
| 52 | break; |
| 53 | case dwarf::DW_OP_plus: |
| 54 | Offset += Value; |
Bob Haarman | 68e4601 | 2017-08-29 22:54:31 +0000 | [diff] [blame] | 55 | break; |
Bob Haarman | 223303c | 2017-08-29 20:59:25 +0000 | [diff] [blame] | 56 | default: |
| 57 | continue; |
| 58 | } |
| 59 | } |
| 60 | } break; |
| 61 | case dwarf::DW_OP_plus_uconst: |
| 62 | Offset += Op->getArg(0); |
| 63 | break; |
| 64 | case dwarf::DW_OP_LLVM_fragment: |
| 65 | Location.FragmentInfo = {Op->getArg(1), Op->getArg(0)}; |
| 66 | break; |
| 67 | case dwarf::DW_OP_deref: |
Reid Kleckner | 08f5fd5 | 2017-08-31 15:56:49 +0000 | [diff] [blame] | 68 | Location.LoadChain.push_back(Offset); |
| 69 | Offset = 0; |
Bob Haarman | 223303c | 2017-08-29 20:59:25 +0000 | [diff] [blame] | 70 | break; |
| 71 | default: |
Bob Haarman | 1a4cbbe | 2017-08-30 17:50:21 +0000 | [diff] [blame] | 72 | return None; |
Bob Haarman | 223303c | 2017-08-29 20:59:25 +0000 | [diff] [blame] | 73 | } |
| 74 | ++Op; |
| 75 | } |
| 76 | |
Reid Kleckner | 08f5fd5 | 2017-08-31 15:56:49 +0000 | [diff] [blame] | 77 | // Do one final implicit DW_OP_deref if this was an indirect DBG_VALUE |
| 78 | // instruction. |
| 79 | // FIXME: Replace these with DIExpression. |
| 80 | if (Instruction.isIndirectDebugValue()) |
| 81 | Location.LoadChain.push_back(Offset); |
| 82 | |
Bob Haarman | 1a4cbbe | 2017-08-30 17:50:21 +0000 | [diff] [blame] | 83 | return Location; |
Bob Haarman | 223303c | 2017-08-29 20:59:25 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 86 | DebugHandlerBase::DebugHandlerBase(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {} |
| 87 | |
| 88 | // Each LexicalScope has first instruction and last instruction to mark |
| 89 | // beginning and end of a scope respectively. Create an inverse map that list |
| 90 | // scopes starts (and ends) with an instruction. One instruction may start (or |
| 91 | // end) multiple scopes. Ignore scopes that are not reachable. |
| 92 | void DebugHandlerBase::identifyScopeMarkers() { |
| 93 | SmallVector<LexicalScope *, 4> WorkList; |
| 94 | WorkList.push_back(LScopes.getCurrentFunctionScope()); |
| 95 | while (!WorkList.empty()) { |
| 96 | LexicalScope *S = WorkList.pop_back_val(); |
| 97 | |
| 98 | const SmallVectorImpl<LexicalScope *> &Children = S->getChildren(); |
| 99 | if (!Children.empty()) |
| 100 | WorkList.append(Children.begin(), Children.end()); |
| 101 | |
| 102 | if (S->isAbstractScope()) |
| 103 | continue; |
| 104 | |
| 105 | for (const InsnRange &R : S->getRanges()) { |
| 106 | assert(R.first && "InsnRange does not have first instruction!"); |
| 107 | assert(R.second && "InsnRange does not have second instruction!"); |
| 108 | requestLabelBeforeInsn(R.first); |
| 109 | requestLabelAfterInsn(R.second); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // Return Label preceding the instruction. |
| 115 | MCSymbol *DebugHandlerBase::getLabelBeforeInsn(const MachineInstr *MI) { |
| 116 | MCSymbol *Label = LabelsBeforeInsn.lookup(MI); |
| 117 | assert(Label && "Didn't insert label before instruction"); |
| 118 | return Label; |
| 119 | } |
| 120 | |
| 121 | // Return Label immediately following the instruction. |
| 122 | MCSymbol *DebugHandlerBase::getLabelAfterInsn(const MachineInstr *MI) { |
| 123 | return LabelsAfterInsn.lookup(MI); |
| 124 | } |
| 125 | |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 126 | int DebugHandlerBase::fragmentCmp(const DIExpression *P1, |
| 127 | const DIExpression *P2) { |
Adrian Prantl | 49797ca | 2016-12-22 05:27:12 +0000 | [diff] [blame] | 128 | auto Fragment1 = *P1->getFragmentInfo(); |
| 129 | auto Fragment2 = *P2->getFragmentInfo(); |
| 130 | unsigned l1 = Fragment1.OffsetInBits; |
| 131 | unsigned l2 = Fragment2.OffsetInBits; |
| 132 | unsigned r1 = l1 + Fragment1.SizeInBits; |
| 133 | unsigned r2 = l2 + Fragment2.SizeInBits; |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 134 | if (r1 <= l2) |
| 135 | return -1; |
| 136 | else if (r2 <= l1) |
| 137 | return 1; |
| 138 | else |
| 139 | return 0; |
| 140 | } |
| 141 | |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 142 | bool DebugHandlerBase::fragmentsOverlap(const DIExpression *P1, |
| 143 | const DIExpression *P2) { |
| 144 | if (!P1->isFragment() || !P2->isFragment()) |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 145 | return true; |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 146 | return fragmentCmp(P1, P2) == 0; |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Amjad Aboud | acee568 | 2016-07-12 12:06:34 +0000 | [diff] [blame] | 149 | /// If this type is derived from a base type then return base type size. |
| 150 | uint64_t DebugHandlerBase::getBaseTypeSize(const DITypeRef TyRef) { |
| 151 | DIType *Ty = TyRef.resolve(); |
| 152 | assert(Ty); |
| 153 | DIDerivedType *DDTy = dyn_cast<DIDerivedType>(Ty); |
| 154 | if (!DDTy) |
| 155 | return Ty->getSizeInBits(); |
| 156 | |
| 157 | unsigned Tag = DDTy->getTag(); |
| 158 | |
| 159 | if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef && |
| 160 | Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type && |
Victor Leschuk | e1156c2 | 2016-10-31 19:09:38 +0000 | [diff] [blame] | 161 | Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_atomic_type) |
Amjad Aboud | acee568 | 2016-07-12 12:06:34 +0000 | [diff] [blame] | 162 | return DDTy->getSizeInBits(); |
| 163 | |
| 164 | DIType *BaseType = DDTy->getBaseType().resolve(); |
| 165 | |
| 166 | assert(BaseType && "Unexpected invalid base type"); |
| 167 | |
| 168 | // If this is a derived type, go ahead and get the base type, unless it's a |
| 169 | // reference then it's just the size of the field. Pointer types have no need |
| 170 | // of this since they're a different type of qualification on the type. |
| 171 | if (BaseType->getTag() == dwarf::DW_TAG_reference_type || |
| 172 | BaseType->getTag() == dwarf::DW_TAG_rvalue_reference_type) |
| 173 | return Ty->getSizeInBits(); |
| 174 | |
| 175 | return getBaseTypeSize(BaseType); |
| 176 | } |
| 177 | |
Benjamin Kramer | debb3c3 | 2017-05-26 20:09:00 +0000 | [diff] [blame] | 178 | static bool hasDebugInfo(const MachineModuleInfo *MMI, |
| 179 | const MachineFunction *MF) { |
David Blaikie | b2fbb4b | 2017-02-16 18:48:33 +0000 | [diff] [blame] | 180 | if (!MMI->hasDebugInfo()) |
| 181 | return false; |
| 182 | auto *SP = MF->getFunction()->getSubprogram(); |
| 183 | if (!SP) |
| 184 | return false; |
| 185 | assert(SP->getUnit()); |
| 186 | auto EK = SP->getUnit()->getEmissionKind(); |
| 187 | if (EK == DICompileUnit::NoDebug) |
| 188 | return false; |
| 189 | return true; |
| 190 | } |
| 191 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 192 | void DebugHandlerBase::beginFunction(const MachineFunction *MF) { |
Adrian Prantl | 8f33379 | 2017-03-23 20:23:42 +0000 | [diff] [blame] | 193 | PrevInstBB = nullptr; |
David Blaikie | b2fbb4b | 2017-02-16 18:48:33 +0000 | [diff] [blame] | 194 | |
Reid Kleckner | 5bc8543 | 2017-05-12 17:02:40 +0000 | [diff] [blame] | 195 | if (!Asm || !hasDebugInfo(MMI, MF)) { |
David Blaikie | b2fbb4b | 2017-02-16 18:48:33 +0000 | [diff] [blame] | 196 | skippedNonDebugFunction(); |
| 197 | return; |
| 198 | } |
| 199 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 200 | // Grab the lexical scopes for the function, if we don't have any of those |
| 201 | // then we're not going to be able to do anything. |
| 202 | LScopes.initialize(*MF); |
David Blaikie | b2fbb4b | 2017-02-16 18:48:33 +0000 | [diff] [blame] | 203 | if (LScopes.empty()) { |
| 204 | beginFunctionImpl(MF); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 205 | return; |
David Blaikie | b2fbb4b | 2017-02-16 18:48:33 +0000 | [diff] [blame] | 206 | } |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 207 | |
| 208 | // Make sure that each lexical scope will have a begin/end label. |
| 209 | identifyScopeMarkers(); |
| 210 | |
| 211 | // Calculate history for local variables. |
| 212 | assert(DbgValues.empty() && "DbgValues map wasn't cleaned!"); |
| 213 | calculateDbgValueHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(), |
| 214 | DbgValues); |
| 215 | |
| 216 | // Request labels for the full history. |
| 217 | for (const auto &I : DbgValues) { |
| 218 | const auto &Ranges = I.second; |
| 219 | if (Ranges.empty()) |
| 220 | continue; |
| 221 | |
| 222 | // The first mention of a function argument gets the CurrentFnBegin |
| 223 | // label, so arguments are visible when breaking at function entry. |
| 224 | const DILocalVariable *DIVar = Ranges.front().first->getDebugVariable(); |
| 225 | if (DIVar->isParameter() && |
| 226 | getDISubprogram(DIVar->getScope())->describes(MF->getFunction())) { |
| 227 | LabelsBeforeInsn[Ranges.front().first] = Asm->getFunctionBegin(); |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 228 | if (Ranges.front().first->getDebugExpression()->isFragment()) { |
| 229 | // Mark all non-overlapping initial fragments. |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 230 | for (auto I = Ranges.begin(); I != Ranges.end(); ++I) { |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 231 | const DIExpression *Fragment = I->first->getDebugExpression(); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 232 | if (std::all_of(Ranges.begin(), I, |
| 233 | [&](DbgValueHistoryMap::InstrRange Pred) { |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 234 | return !fragmentsOverlap( |
| 235 | Fragment, Pred.first->getDebugExpression()); |
| 236 | })) |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 237 | LabelsBeforeInsn[I->first] = Asm->getFunctionBegin(); |
| 238 | else |
| 239 | break; |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | for (const auto &Range : Ranges) { |
| 245 | requestLabelBeforeInsn(Range.first); |
| 246 | if (Range.second) |
| 247 | requestLabelAfterInsn(Range.second); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | PrevInstLoc = DebugLoc(); |
| 252 | PrevLabel = Asm->getFunctionBegin(); |
David Blaikie | b2fbb4b | 2017-02-16 18:48:33 +0000 | [diff] [blame] | 253 | beginFunctionImpl(MF); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | void DebugHandlerBase::beginInstruction(const MachineInstr *MI) { |
| 257 | if (!MMI->hasDebugInfo()) |
| 258 | return; |
| 259 | |
| 260 | assert(CurMI == nullptr); |
| 261 | CurMI = MI; |
| 262 | |
| 263 | // Insert labels where requested. |
| 264 | DenseMap<const MachineInstr *, MCSymbol *>::iterator I = |
| 265 | LabelsBeforeInsn.find(MI); |
| 266 | |
| 267 | // No label needed. |
| 268 | if (I == LabelsBeforeInsn.end()) |
| 269 | return; |
| 270 | |
| 271 | // Label already assigned. |
| 272 | if (I->second) |
| 273 | return; |
| 274 | |
| 275 | if (!PrevLabel) { |
| 276 | PrevLabel = MMI->getContext().createTempSymbol(); |
| 277 | Asm->OutStreamer->EmitLabel(PrevLabel); |
| 278 | } |
| 279 | I->second = PrevLabel; |
| 280 | } |
| 281 | |
| 282 | void DebugHandlerBase::endInstruction() { |
| 283 | if (!MMI->hasDebugInfo()) |
| 284 | return; |
| 285 | |
| 286 | assert(CurMI != nullptr); |
Adrian Prantl | fb31da1 | 2017-05-22 20:47:09 +0000 | [diff] [blame] | 287 | // Don't create a new label after DBG_VALUE and other instructions that don't |
| 288 | // generate code. |
| 289 | if (!CurMI->isMetaInstruction()) { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 290 | PrevLabel = nullptr; |
Paul Robinson | ac7fe5e | 2016-12-12 20:49:11 +0000 | [diff] [blame] | 291 | PrevInstBB = CurMI->getParent(); |
| 292 | } |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 293 | |
| 294 | DenseMap<const MachineInstr *, MCSymbol *>::iterator I = |
| 295 | LabelsAfterInsn.find(CurMI); |
| 296 | CurMI = nullptr; |
| 297 | |
| 298 | // No label needed. |
| 299 | if (I == LabelsAfterInsn.end()) |
| 300 | return; |
| 301 | |
| 302 | // Label already assigned. |
| 303 | if (I->second) |
| 304 | return; |
| 305 | |
| 306 | // We need a label after this instruction. |
| 307 | if (!PrevLabel) { |
| 308 | PrevLabel = MMI->getContext().createTempSymbol(); |
| 309 | Asm->OutStreamer->EmitLabel(PrevLabel); |
| 310 | } |
| 311 | I->second = PrevLabel; |
| 312 | } |
| 313 | |
| 314 | void DebugHandlerBase::endFunction(const MachineFunction *MF) { |
David Blaikie | b2fbb4b | 2017-02-16 18:48:33 +0000 | [diff] [blame] | 315 | if (hasDebugInfo(MMI, MF)) |
| 316 | endFunctionImpl(MF); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 317 | DbgValues.clear(); |
| 318 | LabelsBeforeInsn.clear(); |
| 319 | LabelsAfterInsn.clear(); |
| 320 | } |