1 //===-- llvm/lib/CodeGen/AsmPrinter/DebugHandlerBase.cpp -------*- C++ -*--===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // Common functionality for different debug information format backends. 10 // LLVM currently supports DWARF and CodeView. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/CodeGen/DebugHandlerBase.h" 15 #include "llvm/ADT/Optional.h" 16 #include "llvm/ADT/Twine.h" 17 #include "llvm/CodeGen/AsmPrinter.h" 18 #include "llvm/CodeGen/MachineFunction.h" 19 #include "llvm/CodeGen/MachineInstr.h" 20 #include "llvm/CodeGen/MachineModuleInfo.h" 21 #include "llvm/CodeGen/TargetSubtargetInfo.h" 22 #include "llvm/IR/DebugInfo.h" 23 #include "llvm/MC/MCStreamer.h" 24 25 using namespace llvm; 26 27 #define DEBUG_TYPE "dwarfdebug" 28 29 Optional<DbgVariableLocation> 30 DbgVariableLocation::extractFromMachineInstruction( 31 const MachineInstr &Instruction) { 32 DbgVariableLocation Location; 33 if (!Instruction.isDebugValue()) 34 return None; 35 if (!Instruction.getDebugOperand(0).isReg()) 36 return None; 37 Location.Register = Instruction.getDebugOperand(0).getReg(); 38 Location.FragmentInfo.reset(); 39 // We only handle expressions generated by DIExpression::appendOffset, 40 // which doesn't require a full stack machine. 41 int64_t Offset = 0; 42 const DIExpression *DIExpr = Instruction.getDebugExpression(); 43 auto Op = DIExpr->expr_op_begin(); 44 while (Op != DIExpr->expr_op_end()) { 45 switch (Op->getOp()) { 46 case dwarf::DW_OP_constu: { 47 int Value = Op->getArg(0); 48 ++Op; 49 if (Op != DIExpr->expr_op_end()) { 50 switch (Op->getOp()) { 51 case dwarf::DW_OP_minus: 52 Offset -= Value; 53 break; 54 case dwarf::DW_OP_plus: 55 Offset += Value; 56 break; 57 default: 58 continue; 59 } 60 } 61 } break; 62 case dwarf::DW_OP_plus_uconst: 63 Offset += Op->getArg(0); 64 break; 65 case dwarf::DW_OP_LLVM_fragment: 66 Location.FragmentInfo = {Op->getArg(1), Op->getArg(0)}; 67 break; 68 case dwarf::DW_OP_deref: 69 Location.LoadChain.push_back(Offset); 70 Offset = 0; 71 break; 72 default: 73 return None; 74 } 75 ++Op; 76 } 77 78 // Do one final implicit DW_OP_deref if this was an indirect DBG_VALUE 79 // instruction. 80 // FIXME: Replace these with DIExpression. 81 if (Instruction.isIndirectDebugValue()) 82 Location.LoadChain.push_back(Offset); 83 84 return Location; 85 } 86 87 DebugHandlerBase::DebugHandlerBase(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {} 88 89 // Each LexicalScope has first instruction and last instruction to mark 90 // beginning and end of a scope respectively. Create an inverse map that list 91 // scopes starts (and ends) with an instruction. One instruction may start (or 92 // end) multiple scopes. Ignore scopes that are not reachable. 93 void DebugHandlerBase::identifyScopeMarkers() { 94 SmallVector<LexicalScope *, 4> WorkList; 95 WorkList.push_back(LScopes.getCurrentFunctionScope()); 96 while (!WorkList.empty()) { 97 LexicalScope *S = WorkList.pop_back_val(); 98 99 const SmallVectorImpl<LexicalScope *> &Children = S->getChildren(); 100 if (!Children.empty()) 101 WorkList.append(Children.begin(), Children.end()); 102 103 if (S->isAbstractScope()) 104 continue; 105 106 for (const InsnRange &R : S->getRanges()) { 107 assert(R.first && "InsnRange does not have first instruction!"); 108 assert(R.second && "InsnRange does not have second instruction!"); 109 requestLabelBeforeInsn(R.first); 110 requestLabelAfterInsn(R.second); 111 } 112 } 113 } 114 115 // Return Label preceding the instruction. 116 MCSymbol *DebugHandlerBase::getLabelBeforeInsn(const MachineInstr *MI) { 117 MCSymbol *Label = LabelsBeforeInsn.lookup(MI); 118 assert(Label && "Didn't insert label before instruction"); 119 return Label; 120 } 121 122 // Return Label immediately following the instruction. 123 MCSymbol *DebugHandlerBase::getLabelAfterInsn(const MachineInstr *MI) { 124 return LabelsAfterInsn.lookup(MI); 125 } 126 127 /// If this type is derived from a base type then return base type size. 128 uint64_t DebugHandlerBase::getBaseTypeSize(const DIType *Ty) { 129 assert(Ty); 130 const DIDerivedType *DDTy = dyn_cast<DIDerivedType>(Ty); 131 if (!DDTy) 132 return Ty->getSizeInBits(); 133 134 unsigned Tag = DDTy->getTag(); 135 136 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef && 137 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type && 138 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_atomic_type) 139 return DDTy->getSizeInBits(); 140 141 DIType *BaseType = DDTy->getBaseType(); 142 143 if (!BaseType) 144 return 0; 145 146 // If this is a derived type, go ahead and get the base type, unless it's a 147 // reference then it's just the size of the field. Pointer types have no need 148 // of this since they're a different type of qualification on the type. 149 if (BaseType->getTag() == dwarf::DW_TAG_reference_type || 150 BaseType->getTag() == dwarf::DW_TAG_rvalue_reference_type) 151 return Ty->getSizeInBits(); 152 153 return getBaseTypeSize(BaseType); 154 } 155 156 static bool hasDebugInfo(const MachineModuleInfo *MMI, 157 const MachineFunction *MF) { 158 if (!MMI->hasDebugInfo()) 159 return false; 160 auto *SP = MF->getFunction().getSubprogram(); 161 if (!SP) 162 return false; 163 assert(SP->getUnit()); 164 auto EK = SP->getUnit()->getEmissionKind(); 165 if (EK == DICompileUnit::NoDebug) 166 return false; 167 return true; 168 } 169 170 void DebugHandlerBase::beginFunction(const MachineFunction *MF) { 171 PrevInstBB = nullptr; 172 173 if (!Asm || !hasDebugInfo(MMI, MF)) { 174 skippedNonDebugFunction(); 175 return; 176 } 177 178 // Grab the lexical scopes for the function, if we don't have any of those 179 // then we're not going to be able to do anything. 180 LScopes.initialize(*MF); 181 if (LScopes.empty()) { 182 beginFunctionImpl(MF); 183 return; 184 } 185 186 // Make sure that each lexical scope will have a begin/end label. 187 identifyScopeMarkers(); 188 189 // Calculate history for local variables. 190 assert(DbgValues.empty() && "DbgValues map wasn't cleaned!"); 191 assert(DbgLabels.empty() && "DbgLabels map wasn't cleaned!"); 192 calculateDbgEntityHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(), 193 DbgValues, DbgLabels); 194 LLVM_DEBUG(DbgValues.dump()); 195 196 // Request labels for the full history. 197 for (const auto &I : DbgValues) { 198 const auto &Entries = I.second; 199 if (Entries.empty()) 200 continue; 201 202 auto IsDescribedByReg = [](const MachineInstr *MI) { 203 return MI->getDebugOperand(0).isReg() && MI->getDebugOperand(0).getReg(); 204 }; 205 206 // The first mention of a function argument gets the CurrentFnBegin label, 207 // so arguments are visible when breaking at function entry. 208 // 209 // We do not change the label for values that are described by registers, 210 // as that could place them above their defining instructions. We should 211 // ideally not change the labels for constant debug values either, since 212 // doing that violates the ranges that are calculated in the history map. 213 // However, we currently do not emit debug values for constant arguments 214 // directly at the start of the function, so this code is still useful. 215 const DILocalVariable *DIVar = 216 Entries.front().getInstr()->getDebugVariable(); 217 if (DIVar->isParameter() && 218 getDISubprogram(DIVar->getScope())->describes(&MF->getFunction())) { 219 if (!IsDescribedByReg(Entries.front().getInstr())) 220 LabelsBeforeInsn[Entries.front().getInstr()] = Asm->getFunctionBegin(); 221 if (Entries.front().getInstr()->getDebugExpression()->isFragment()) { 222 // Mark all non-overlapping initial fragments. 223 for (auto I = Entries.begin(); I != Entries.end(); ++I) { 224 if (!I->isDbgValue()) 225 continue; 226 const DIExpression *Fragment = I->getInstr()->getDebugExpression(); 227 if (std::any_of(Entries.begin(), I, 228 [&](DbgValueHistoryMap::Entry Pred) { 229 return Pred.isDbgValue() && 230 Fragment->fragmentsOverlap( 231 Pred.getInstr()->getDebugExpression()); 232 })) 233 break; 234 // The code that generates location lists for DWARF assumes that the 235 // entries' start labels are monotonically increasing, and since we 236 // don't change the label for fragments that are described by 237 // registers, we must bail out when encountering such a fragment. 238 if (IsDescribedByReg(I->getInstr())) 239 break; 240 LabelsBeforeInsn[I->getInstr()] = Asm->getFunctionBegin(); 241 } 242 } 243 } 244 245 for (const auto &Entry : Entries) { 246 if (Entry.isDbgValue()) 247 requestLabelBeforeInsn(Entry.getInstr()); 248 else 249 requestLabelAfterInsn(Entry.getInstr()); 250 } 251 } 252 253 // Ensure there is a symbol before DBG_LABEL. 254 for (const auto &I : DbgLabels) { 255 const MachineInstr *MI = I.second; 256 requestLabelBeforeInsn(MI); 257 } 258 259 PrevInstLoc = DebugLoc(); 260 PrevLabel = Asm->getFunctionBegin(); 261 beginFunctionImpl(MF); 262 } 263 264 void DebugHandlerBase::beginInstruction(const MachineInstr *MI) { 265 if (!MMI->hasDebugInfo()) 266 return; 267 268 assert(CurMI == nullptr); 269 CurMI = MI; 270 271 // Insert labels where requested. 272 DenseMap<const MachineInstr *, MCSymbol *>::iterator I = 273 LabelsBeforeInsn.find(MI); 274 275 // No label needed. 276 if (I == LabelsBeforeInsn.end()) 277 return; 278 279 // Label already assigned. 280 if (I->second) 281 return; 282 283 if (!PrevLabel) { 284 PrevLabel = MMI->getContext().createTempSymbol(); 285 Asm->OutStreamer->emitLabel(PrevLabel); 286 } 287 I->second = PrevLabel; 288 } 289 290 void DebugHandlerBase::endInstruction() { 291 if (!MMI->hasDebugInfo()) 292 return; 293 294 assert(CurMI != nullptr); 295 // Don't create a new label after DBG_VALUE and other instructions that don't 296 // generate code. 297 if (!CurMI->isMetaInstruction()) { 298 PrevLabel = nullptr; 299 PrevInstBB = CurMI->getParent(); 300 } 301 302 DenseMap<const MachineInstr *, MCSymbol *>::iterator I = 303 LabelsAfterInsn.find(CurMI); 304 CurMI = nullptr; 305 306 // No label needed. 307 if (I == LabelsAfterInsn.end()) 308 return; 309 310 // Label already assigned. 311 if (I->second) 312 return; 313 314 // We need a label after this instruction. 315 if (!PrevLabel) { 316 PrevLabel = MMI->getContext().createTempSymbol(); 317 Asm->OutStreamer->emitLabel(PrevLabel); 318 } 319 I->second = PrevLabel; 320 } 321 322 void DebugHandlerBase::endFunction(const MachineFunction *MF) { 323 if (hasDebugInfo(MMI, MF)) 324 endFunctionImpl(MF); 325 DbgValues.clear(); 326 DbgLabels.clear(); 327 LabelsBeforeInsn.clear(); 328 LabelsAfterInsn.clear(); 329 } 330 331 void DebugHandlerBase::beginBasicBlock(const MachineBasicBlock &MBB) { 332 if (!MBB.isBeginSection()) 333 return; 334 335 PrevLabel = MBB.getSymbol(); 336 } 337 338 void DebugHandlerBase::endBasicBlock(const MachineBasicBlock &MBB) { 339 if (!MBB.isEndSection()) 340 return; 341 342 PrevLabel = nullptr; 343 } 344