Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:28

0001 //===-- llvm/CodeGen/DebugHandlerBase.h -----------------------*- C++ -*--===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // Common functionality for different debug information format backends.
0010 // LLVM currently supports DWARF and CodeView.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_CODEGEN_DEBUGHANDLERBASE_H
0015 #define LLVM_CODEGEN_DEBUGHANDLERBASE_H
0016 
0017 #include "llvm/CodeGen/AsmPrinterHandler.h"
0018 #include "llvm/CodeGen/DbgEntityHistoryCalculator.h"
0019 #include "llvm/CodeGen/LexicalScopes.h"
0020 #include "llvm/IR/DebugInfoMetadata.h"
0021 #include "llvm/IR/DebugLoc.h"
0022 #include <optional>
0023 
0024 namespace llvm {
0025 
0026 class AsmPrinter;
0027 class MachineInstr;
0028 class MachineModuleInfo;
0029 
0030 /// Represents the location at which a variable is stored.
0031 struct DbgVariableLocation {
0032   /// Base register.
0033   unsigned Register;
0034 
0035   /// Chain of offsetted loads necessary to load the value if it lives in
0036   /// memory. Every load except for the last is pointer-sized.
0037   SmallVector<int64_t, 1> LoadChain;
0038 
0039   /// Present if the location is part of a larger variable.
0040   std::optional<llvm::DIExpression::FragmentInfo> FragmentInfo;
0041 
0042   /// Extract a VariableLocation from a MachineInstr.
0043   /// This will only work if Instruction is a debug value instruction
0044   /// and the associated DIExpression is in one of the supported forms.
0045   /// If these requirements are not met, the returned Optional will not
0046   /// have a value.
0047   static std::optional<DbgVariableLocation>
0048   extractFromMachineInstruction(const MachineInstr &Instruction);
0049 };
0050 
0051 /// Base class for debug information backends. Common functionality related to
0052 /// tracking which variables and scopes are alive at a given PC live here.
0053 class DebugHandlerBase : public AsmPrinterHandler {
0054 protected:
0055   DebugHandlerBase(AsmPrinter *A);
0056 
0057   /// Target of debug info emission.
0058   AsmPrinter *Asm = nullptr;
0059 
0060   /// Collected machine module information.
0061   MachineModuleInfo *MMI = nullptr;
0062 
0063   /// Previous instruction's location information. This is used to
0064   /// determine label location to indicate scope boundaries in debug info.
0065   /// We track the previous instruction's source location (if not line 0),
0066   /// whether it was a label, and its parent BB.
0067   DebugLoc PrevInstLoc;
0068   MCSymbol *PrevLabel = nullptr;
0069   const MachineBasicBlock *PrevInstBB = nullptr;
0070 
0071   /// This location indicates end of function prologue and beginning of
0072   /// function body.
0073   const MachineInstr *PrologEndLoc;
0074 
0075   /// This block includes epilogue instructions.
0076   const MachineBasicBlock *EpilogBeginBlock = nullptr;
0077 
0078   /// If nonnull, stores the current machine instruction we're processing.
0079   const MachineInstr *CurMI = nullptr;
0080 
0081   LexicalScopes LScopes;
0082 
0083   /// History of DBG_VALUE and clobber instructions for each user
0084   /// variable.  Variables are listed in order of appearance.
0085   DbgValueHistoryMap DbgValues;
0086 
0087   /// Mapping of inlined labels and DBG_LABEL machine instruction.
0088   DbgLabelInstrMap DbgLabels;
0089 
0090   /// Maps instruction with label emitted before instruction.
0091   /// FIXME: Make this private from DwarfDebug, we have the necessary accessors
0092   /// for it.
0093   DenseMap<const MachineInstr *, MCSymbol *> LabelsBeforeInsn;
0094 
0095   /// Maps instruction with label emitted after instruction.
0096   DenseMap<const MachineInstr *, MCSymbol *> LabelsAfterInsn;
0097 
0098   /// Indentify instructions that are marking the beginning of or
0099   /// ending of a scope.
0100   void identifyScopeMarkers();
0101 
0102   /// Ensure that a label will be emitted before MI.
0103   void requestLabelBeforeInsn(const MachineInstr *MI) {
0104     LabelsBeforeInsn.insert(std::make_pair(MI, nullptr));
0105   }
0106 
0107   /// Ensure that a label will be emitted after MI.
0108   void requestLabelAfterInsn(const MachineInstr *MI) {
0109     LabelsAfterInsn.insert(std::make_pair(MI, nullptr));
0110   }
0111 
0112   virtual void beginFunctionImpl(const MachineFunction *MF) = 0;
0113   virtual void endFunctionImpl(const MachineFunction *MF) = 0;
0114   virtual void skippedNonDebugFunction() {}
0115 
0116 private:
0117   InstructionOrdering InstOrdering;
0118 
0119   // AsmPrinterHandler overrides.
0120 public:
0121   virtual ~DebugHandlerBase() override;
0122 
0123   void beginModule(Module *M) override;
0124 
0125   void beginInstruction(const MachineInstr *MI) override;
0126   void endInstruction() override;
0127 
0128   void beginFunction(const MachineFunction *MF) override;
0129   void endFunction(const MachineFunction *MF) override;
0130 
0131   void beginBasicBlockSection(const MachineBasicBlock &MBB) override;
0132   void endBasicBlockSection(const MachineBasicBlock &MBB) override;
0133 
0134   /// Return Label preceding the instruction.
0135   MCSymbol *getLabelBeforeInsn(const MachineInstr *MI);
0136 
0137   /// Return Label immediately following the instruction.
0138   MCSymbol *getLabelAfterInsn(const MachineInstr *MI);
0139 
0140   /// If this type is derived from a base type then return base type size.
0141   static uint64_t getBaseTypeSize(const DIType *Ty);
0142 
0143   /// Return true if type encoding is unsigned.
0144   static bool isUnsignedDIType(const DIType *Ty);
0145 
0146   const InstructionOrdering &getInstOrdering() const { return InstOrdering; }
0147 };
0148 
0149 } // namespace llvm
0150 
0151 #endif