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/ADT/Optional.h" 17 #include "llvm/ADT/Twine.h" 18 #include "llvm/CodeGen/AsmPrinter.h" 19 #include "llvm/CodeGen/MachineFunction.h" 20 #include "llvm/CodeGen/MachineInstr.h" 21 #include "llvm/CodeGen/MachineModuleInfo.h" 22 #include "llvm/CodeGen/TargetSubtargetInfo.h" 23 #include "llvm/IR/DebugInfo.h" 24 #include "llvm/MC/MCStreamer.h" 25 26 using namespace llvm; 27 28 Optional<DbgVariableLocation> 29 DbgVariableLocation::extractFromMachineInstruction( 30 const MachineInstr &Instruction) { 31 DbgVariableLocation Location; 32 if (!Instruction.isDebugValue()) 33 return None; 34 if (!Instruction.getOperand(0).isReg()) 35 return None; 36 Location.Register = Instruction.getOperand(0).getReg(); 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; 55 break; 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: 68 Location.LoadChain.push_back(Offset); 69 Offset = 0; 70 break; 71 default: 72 return None; 73 } 74 ++Op; 75 } 76 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 83 return Location; 84 } 85 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 126 /// If this type is derived from a base type then return base type size. 127 uint64_t DebugHandlerBase::getBaseTypeSize(const DITypeRef TyRef) { 128 DIType *Ty = TyRef.resolve(); 129 assert(Ty); 130 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().resolve(); 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 calculateDbgValueHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(), 192 DbgValues); 193 194 // Request labels for the full history. 195 for (const auto &I : DbgValues) { 196 const auto &Ranges = I.second; 197 if (Ranges.empty()) 198 continue; 199 200 // The first mention of a function argument gets the CurrentFnBegin 201 // label, so arguments are visible when breaking at function entry. 202 const DILocalVariable *DIVar = Ranges.front().first->getDebugVariable(); 203 if (DIVar->isParameter() && 204 getDISubprogram(DIVar->getScope())->describes(&MF->getFunction())) { 205 LabelsBeforeInsn[Ranges.front().first] = Asm->getFunctionBegin(); 206 if (Ranges.front().first->getDebugExpression()->isFragment()) { 207 // Mark all non-overlapping initial fragments. 208 for (auto I = Ranges.begin(); I != Ranges.end(); ++I) { 209 const DIExpression *Fragment = I->first->getDebugExpression(); 210 if (std::all_of(Ranges.begin(), I, 211 [&](DbgValueHistoryMap::InstrRange Pred) { 212 return !Fragment->fragmentsOverlap( 213 Pred.first->getDebugExpression()); 214 })) 215 LabelsBeforeInsn[I->first] = Asm->getFunctionBegin(); 216 else 217 break; 218 } 219 } 220 } 221 222 for (const auto &Range : Ranges) { 223 requestLabelBeforeInsn(Range.first); 224 if (Range.second) 225 requestLabelAfterInsn(Range.second); 226 } 227 } 228 229 PrevInstLoc = DebugLoc(); 230 PrevLabel = Asm->getFunctionBegin(); 231 beginFunctionImpl(MF); 232 } 233 234 void DebugHandlerBase::beginInstruction(const MachineInstr *MI) { 235 if (!MMI->hasDebugInfo()) 236 return; 237 238 assert(CurMI == nullptr); 239 CurMI = MI; 240 241 // Insert labels where requested. 242 DenseMap<const MachineInstr *, MCSymbol *>::iterator I = 243 LabelsBeforeInsn.find(MI); 244 245 // No label needed. 246 if (I == LabelsBeforeInsn.end()) 247 return; 248 249 // Label already assigned. 250 if (I->second) 251 return; 252 253 if (!PrevLabel) { 254 PrevLabel = MMI->getContext().createTempSymbol(); 255 Asm->OutStreamer->EmitLabel(PrevLabel); 256 } 257 I->second = PrevLabel; 258 } 259 260 void DebugHandlerBase::endInstruction() { 261 if (!MMI->hasDebugInfo()) 262 return; 263 264 assert(CurMI != nullptr); 265 // Don't create a new label after DBG_VALUE and other instructions that don't 266 // generate code. 267 if (!CurMI->isMetaInstruction()) { 268 PrevLabel = nullptr; 269 PrevInstBB = CurMI->getParent(); 270 } 271 272 DenseMap<const MachineInstr *, MCSymbol *>::iterator I = 273 LabelsAfterInsn.find(CurMI); 274 CurMI = nullptr; 275 276 // No label needed. 277 if (I == LabelsAfterInsn.end()) 278 return; 279 280 // Label already assigned. 281 if (I->second) 282 return; 283 284 // We need a label after this instruction. 285 if (!PrevLabel) { 286 PrevLabel = MMI->getContext().createTempSymbol(); 287 Asm->OutStreamer->EmitLabel(PrevLabel); 288 } 289 I->second = PrevLabel; 290 } 291 292 void DebugHandlerBase::endFunction(const MachineFunction *MF) { 293 if (hasDebugInfo(MMI, MF)) 294 endFunctionImpl(MF); 295 DbgValues.clear(); 296 LabelsBeforeInsn.clear(); 297 LabelsAfterInsn.clear(); 298 } 299