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