DebugHandlerBase.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. #include "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. using namespace llvm;
  25. #define DEBUG_TYPE "dwarfdebug"
  26. Optional<DbgVariableLocation>
  27. DbgVariableLocation::extractFromMachineInstruction(
  28. const MachineInstr &Instruction) {
  29. DbgVariableLocation Location;
  30. if (!Instruction.isDebugValue())
  31. return None;
  32. if (!Instruction.getOperand(0).isReg())
  33. return None;
  34. Location.Register = Instruction.getOperand(0).getReg();
  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.LoadChain.push_back(Offset);
  67. Offset = 0;
  68. break;
  69. default:
  70. return None;
  71. }
  72. ++Op;
  73. }
  74. // Do one final implicit DW_OP_deref if this was an indirect DBG_VALUE
  75. // instruction.
  76. // FIXME: Replace these with DIExpression.
  77. if (Instruction.isIndirectDebugValue())
  78. Location.LoadChain.push_back(Offset);
  79. return Location;
  80. }
  81. DebugHandlerBase::DebugHandlerBase(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {}
  82. // Each LexicalScope has first instruction and last instruction to mark
  83. // beginning and end of a scope respectively. Create an inverse map that list
  84. // scopes starts (and ends) with an instruction. One instruction may start (or
  85. // end) multiple scopes. Ignore scopes that are not reachable.
  86. void DebugHandlerBase::identifyScopeMarkers() {
  87. SmallVector<LexicalScope *, 4> WorkList;
  88. WorkList.push_back(LScopes.getCurrentFunctionScope());
  89. while (!WorkList.empty()) {
  90. LexicalScope *S = WorkList.pop_back_val();
  91. const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
  92. if (!Children.empty())
  93. WorkList.append(Children.begin(), Children.end());
  94. if (S->isAbstractScope())
  95. continue;
  96. for (const InsnRange &R : S->getRanges()) {
  97. assert(R.first && "InsnRange does not have first instruction!");
  98. assert(R.second && "InsnRange does not have second instruction!");
  99. requestLabelBeforeInsn(R.first);
  100. requestLabelAfterInsn(R.second);
  101. }
  102. }
  103. }
  104. // Return Label preceding the instruction.
  105. MCSymbol *DebugHandlerBase::getLabelBeforeInsn(const MachineInstr *MI) {
  106. MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
  107. assert(Label && "Didn't insert label before instruction");
  108. return Label;
  109. }
  110. // Return Label immediately following the instruction.
  111. MCSymbol *DebugHandlerBase::getLabelAfterInsn(const MachineInstr *MI) {
  112. return LabelsAfterInsn.lookup(MI);
  113. }
  114. /// If this type is derived from a base type then return base type size.
  115. uint64_t DebugHandlerBase::getBaseTypeSize(const DITypeRef TyRef) {
  116. DIType *Ty = TyRef.resolve();
  117. assert(Ty);
  118. DIDerivedType *DDTy = dyn_cast<DIDerivedType>(Ty);
  119. if (!DDTy)
  120. return Ty->getSizeInBits();
  121. unsigned Tag = DDTy->getTag();
  122. if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
  123. Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
  124. Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_atomic_type)
  125. return DDTy->getSizeInBits();
  126. DIType *BaseType = DDTy->getBaseType().resolve();
  127. if (!BaseType)
  128. return 0;
  129. // If this is a derived type, go ahead and get the base type, unless it's a
  130. // reference then it's just the size of the field. Pointer types have no need
  131. // of this since they're a different type of qualification on the type.
  132. if (BaseType->getTag() == dwarf::DW_TAG_reference_type ||
  133. BaseType->getTag() == dwarf::DW_TAG_rvalue_reference_type)
  134. return Ty->getSizeInBits();
  135. return getBaseTypeSize(BaseType);
  136. }
  137. static bool hasDebugInfo(const MachineModuleInfo *MMI,
  138. const MachineFunction *MF) {
  139. if (!MMI->hasDebugInfo())
  140. return false;
  141. auto *SP = MF->getFunction().getSubprogram();
  142. if (!SP)
  143. return false;
  144. assert(SP->getUnit());
  145. auto EK = SP->getUnit()->getEmissionKind();
  146. if (EK == DICompileUnit::NoDebug)
  147. return false;
  148. return true;
  149. }
  150. void DebugHandlerBase::beginFunction(const MachineFunction *MF) {
  151. PrevInstBB = nullptr;
  152. if (!Asm || !hasDebugInfo(MMI, MF)) {
  153. skippedNonDebugFunction();
  154. return;
  155. }
  156. // Grab the lexical scopes for the function, if we don't have any of those
  157. // then we're not going to be able to do anything.
  158. LScopes.initialize(*MF);
  159. if (LScopes.empty()) {
  160. beginFunctionImpl(MF);
  161. return;
  162. }
  163. // Make sure that each lexical scope will have a begin/end label.
  164. identifyScopeMarkers();
  165. // Calculate history for local variables.
  166. assert(DbgValues.empty() && "DbgValues map wasn't cleaned!");
  167. calculateDbgValueHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(),
  168. DbgValues);
  169. LLVM_DEBUG(DbgValues.dump());
  170. // Request labels for the full history.
  171. for (const auto &I : DbgValues) {
  172. const auto &Ranges = I.second;
  173. if (Ranges.empty())
  174. continue;
  175. // The first mention of a function argument gets the CurrentFnBegin
  176. // label, so arguments are visible when breaking at function entry.
  177. const DILocalVariable *DIVar = Ranges.front().first->getDebugVariable();
  178. if (DIVar->isParameter() &&
  179. getDISubprogram(DIVar->getScope())->describes(&MF->getFunction())) {
  180. LabelsBeforeInsn[Ranges.front().first] = Asm->getFunctionBegin();
  181. if (Ranges.front().first->getDebugExpression()->isFragment()) {
  182. // Mark all non-overlapping initial fragments.
  183. for (auto I = Ranges.begin(); I != Ranges.end(); ++I) {
  184. const DIExpression *Fragment = I->first->getDebugExpression();
  185. if (std::all_of(Ranges.begin(), I,
  186. [&](DbgValueHistoryMap::InstrRange Pred) {
  187. return !Fragment->fragmentsOverlap(
  188. Pred.first->getDebugExpression());
  189. }))
  190. LabelsBeforeInsn[I->first] = Asm->getFunctionBegin();
  191. else
  192. break;
  193. }
  194. }
  195. }
  196. for (const auto &Range : Ranges) {
  197. requestLabelBeforeInsn(Range.first);
  198. if (Range.second)
  199. requestLabelAfterInsn(Range.second);
  200. }
  201. }
  202. PrevInstLoc = DebugLoc();
  203. PrevLabel = Asm->getFunctionBegin();
  204. beginFunctionImpl(MF);
  205. }
  206. void DebugHandlerBase::beginInstruction(const MachineInstr *MI) {
  207. if (!MMI->hasDebugInfo())
  208. return;
  209. assert(CurMI == nullptr);
  210. CurMI = MI;
  211. // Insert labels where requested.
  212. DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
  213. LabelsBeforeInsn.find(MI);
  214. // No label needed.
  215. if (I == LabelsBeforeInsn.end())
  216. return;
  217. // Label already assigned.
  218. if (I->second)
  219. return;
  220. if (!PrevLabel) {
  221. PrevLabel = MMI->getContext().createTempSymbol();
  222. Asm->OutStreamer->EmitLabel(PrevLabel);
  223. }
  224. I->second = PrevLabel;
  225. }
  226. void DebugHandlerBase::endInstruction() {
  227. if (!MMI->hasDebugInfo())
  228. return;
  229. assert(CurMI != nullptr);
  230. // Don't create a new label after DBG_VALUE and other instructions that don't
  231. // generate code.
  232. if (!CurMI->isMetaInstruction()) {
  233. PrevLabel = nullptr;
  234. PrevInstBB = CurMI->getParent();
  235. }
  236. DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
  237. LabelsAfterInsn.find(CurMI);
  238. CurMI = nullptr;
  239. // No label needed.
  240. if (I == LabelsAfterInsn.end())
  241. return;
  242. // Label already assigned.
  243. if (I->second)
  244. return;
  245. // We need a label after this instruction.
  246. if (!PrevLabel) {
  247. PrevLabel = MMI->getContext().createTempSymbol();
  248. Asm->OutStreamer->EmitLabel(PrevLabel);
  249. }
  250. I->second = PrevLabel;
  251. }
  252. void DebugHandlerBase::endFunction(const MachineFunction *MF) {
  253. if (hasDebugInfo(MMI, MF))
  254. endFunctionImpl(MF);
  255. DbgValues.clear();
  256. LabelsBeforeInsn.clear();
  257. LabelsAfterInsn.clear();
  258. }