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