DebugHandlerBase.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/CodeGen/DebugHandlerBase.h -----------------------*- C++ -*--===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // Common functionality for different debug information format backends.
  15. // LLVM currently supports DWARF and CodeView.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CODEGEN_DEBUGHANDLERBASE_H
  19. #define LLVM_CODEGEN_DEBUGHANDLERBASE_H
  20. #include "llvm/ADT/Optional.h"
  21. #include "llvm/CodeGen/AsmPrinterHandler.h"
  22. #include "llvm/CodeGen/DbgEntityHistoryCalculator.h"
  23. #include "llvm/CodeGen/LexicalScopes.h"
  24. #include "llvm/IR/DebugInfoMetadata.h"
  25. #include "llvm/IR/DebugLoc.h"
  26. namespace llvm {
  27. class AsmPrinter;
  28. class MachineInstr;
  29. class MachineModuleInfo;
  30. /// Represents the location at which a variable is stored.
  31. struct DbgVariableLocation {
  32. /// Base register.
  33. unsigned Register;
  34. /// Chain of offsetted loads necessary to load the value if it lives in
  35. /// memory. Every load except for the last is pointer-sized.
  36. SmallVector<int64_t, 1> LoadChain;
  37. /// Present if the location is part of a larger variable.
  38. llvm::Optional<llvm::DIExpression::FragmentInfo> FragmentInfo;
  39. /// Extract a VariableLocation from a MachineInstr.
  40. /// This will only work if Instruction is a debug value instruction
  41. /// and the associated DIExpression is in one of the supported forms.
  42. /// If these requirements are not met, the returned Optional will not
  43. /// have a value.
  44. static Optional<DbgVariableLocation>
  45. extractFromMachineInstruction(const MachineInstr &Instruction);
  46. };
  47. /// Base class for debug information backends. Common functionality related to
  48. /// tracking which variables and scopes are alive at a given PC live here.
  49. class DebugHandlerBase : public AsmPrinterHandler {
  50. protected:
  51. DebugHandlerBase(AsmPrinter *A);
  52. /// Target of debug info emission.
  53. AsmPrinter *Asm;
  54. /// Collected machine module information.
  55. MachineModuleInfo *MMI;
  56. /// Previous instruction's location information. This is used to
  57. /// determine label location to indicate scope boundaries in debug info.
  58. /// We track the previous instruction's source location (if not line 0),
  59. /// whether it was a label, and its parent BB.
  60. DebugLoc PrevInstLoc;
  61. MCSymbol *PrevLabel = nullptr;
  62. const MachineBasicBlock *PrevInstBB = nullptr;
  63. /// This location indicates end of function prologue and beginning of
  64. /// function body.
  65. DebugLoc PrologEndLoc;
  66. /// If nonnull, stores the current machine instruction we're processing.
  67. const MachineInstr *CurMI = nullptr;
  68. LexicalScopes LScopes;
  69. /// History of DBG_VALUE and clobber instructions for each user
  70. /// variable. Variables are listed in order of appearance.
  71. DbgValueHistoryMap DbgValues;
  72. /// Mapping of inlined labels and DBG_LABEL machine instruction.
  73. DbgLabelInstrMap DbgLabels;
  74. /// Maps instruction with label emitted before instruction.
  75. /// FIXME: Make this private from DwarfDebug, we have the necessary accessors
  76. /// for it.
  77. DenseMap<const MachineInstr *, MCSymbol *> LabelsBeforeInsn;
  78. /// Maps instruction with label emitted after instruction.
  79. DenseMap<const MachineInstr *, MCSymbol *> LabelsAfterInsn;
  80. /// Indentify instructions that are marking the beginning of or
  81. /// ending of a scope.
  82. void identifyScopeMarkers();
  83. /// Ensure that a label will be emitted before MI.
  84. void requestLabelBeforeInsn(const MachineInstr *MI) {
  85. LabelsBeforeInsn.insert(std::make_pair(MI, nullptr));
  86. }
  87. /// Ensure that a label will be emitted after MI.
  88. void requestLabelAfterInsn(const MachineInstr *MI) {
  89. LabelsAfterInsn.insert(std::make_pair(MI, nullptr));
  90. }
  91. virtual void beginFunctionImpl(const MachineFunction *MF) = 0;
  92. virtual void endFunctionImpl(const MachineFunction *MF) = 0;
  93. virtual void skippedNonDebugFunction() {}
  94. private:
  95. InstructionOrdering InstOrdering;
  96. // AsmPrinterHandler overrides.
  97. public:
  98. void beginModule(Module *M) override;
  99. void beginInstruction(const MachineInstr *MI) override;
  100. void endInstruction() override;
  101. void beginFunction(const MachineFunction *MF) override;
  102. void endFunction(const MachineFunction *MF) override;
  103. void beginBasicBlock(const MachineBasicBlock &MBB) override;
  104. void endBasicBlock(const MachineBasicBlock &MBB) override;
  105. /// Return Label preceding the instruction.
  106. MCSymbol *getLabelBeforeInsn(const MachineInstr *MI);
  107. /// Return Label immediately following the instruction.
  108. MCSymbol *getLabelAfterInsn(const MachineInstr *MI);
  109. /// If this type is derived from a base type then return base type size.
  110. static uint64_t getBaseTypeSize(const DIType *Ty);
  111. /// Return true if type encoding is unsigned.
  112. static bool isUnsignedDIType(const DIType *Ty);
  113. const InstructionOrdering &getInstOrdering() const { return InstOrdering; }
  114. };
  115. } // namespace llvm
  116. #endif
  117. #ifdef __GNUC__
  118. #pragma GCC diagnostic pop
  119. #endif