Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===-- llvm/CodeGen/MachineModuleInfo.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 // Collect meta information for a module.  This information should be in a
0010 // neutral form that can be used by different debugging and exception handling
0011 // schemes.
0012 //
0013 // The organization of information is primarily clustered around the source
0014 // compile units.  The main exception is source line correspondence where
0015 // inlining may interleave code from various compile units.
0016 //
0017 // The following information can be retrieved from the MachineModuleInfo.
0018 //
0019 //  -- Source directories - Directories are uniqued based on their canonical
0020 //     string and assigned a sequential numeric ID (base 1.)
0021 //  -- Source files - Files are also uniqued based on their name and directory
0022 //     ID.  A file ID is sequential number (base 1.)
0023 //  -- Source line correspondence - A vector of file ID, line#, column# triples.
0024 //     A DEBUG_LOCATION instruction is generated  by the DAG Legalizer
0025 //     corresponding to each entry in the source line list.  This allows a debug
0026 //     emitter to generate labels referenced by debug information tables.
0027 //
0028 //===----------------------------------------------------------------------===//
0029 
0030 #ifndef LLVM_CODEGEN_MACHINEMODULEINFO_H
0031 #define LLVM_CODEGEN_MACHINEMODULEINFO_H
0032 
0033 #include "llvm/ADT/DenseMap.h"
0034 #include "llvm/ADT/PointerIntPair.h"
0035 #include "llvm/IR/PassManager.h"
0036 #include "llvm/MC/MCContext.h"
0037 #include "llvm/MC/MCSymbol.h"
0038 #include "llvm/Pass.h"
0039 #include <memory>
0040 #include <utility>
0041 #include <vector>
0042 
0043 namespace llvm {
0044 
0045 class Function;
0046 class TargetMachine;
0047 class MachineFunction;
0048 class Module;
0049 
0050 //===----------------------------------------------------------------------===//
0051 /// This class can be derived from and used by targets to hold private
0052 /// target-specific information for each Module.  Objects of type are
0053 /// accessed/created with MachineModuleInfo::getObjFileInfo and destroyed when
0054 /// the MachineModuleInfo is destroyed.
0055 ///
0056 class MachineModuleInfoImpl {
0057 public:
0058   using StubValueTy = PointerIntPair<MCSymbol *, 1, bool>;
0059   using SymbolListTy = std::vector<std::pair<MCSymbol *, StubValueTy>>;
0060 
0061   /// A variant of SymbolListTy where the stub is a generalized MCExpr.
0062   using ExprStubListTy = std::vector<std::pair<MCSymbol *, const MCExpr *>>;
0063 
0064   virtual ~MachineModuleInfoImpl();
0065 
0066 protected:
0067   /// Return the entries from a DenseMap in a deterministic sorted orer.
0068   /// Clears the map.
0069   static SymbolListTy getSortedStubs(DenseMap<MCSymbol*, StubValueTy>&);
0070 
0071   /// Return the entries from a DenseMap in a deterministic sorted orer.
0072   /// Clears the map.
0073   static ExprStubListTy
0074   getSortedExprStubs(DenseMap<MCSymbol *, const MCExpr *> &);
0075 };
0076 
0077 //===----------------------------------------------------------------------===//
0078 /// This class contains meta information specific to a module.  Queries can be
0079 /// made by different debugging and exception handling schemes and reformated
0080 /// for specific use.
0081 ///
0082 class MachineModuleInfo {
0083   friend class MachineModuleInfoWrapperPass;
0084   friend class MachineModuleAnalysis;
0085 
0086   const TargetMachine &TM;
0087 
0088   /// This is the MCContext used for the entire code generator.
0089   MCContext Context;
0090   // This is an external context, that if assigned, will be used instead of the
0091   // internal context.
0092   MCContext *ExternalContext = nullptr;
0093 
0094   /// This is the LLVM Module being worked on.
0095   const Module *TheModule = nullptr;
0096 
0097   /// This is the object-file-format-specific implementation of
0098   /// MachineModuleInfoImpl, which lets targets accumulate whatever info they
0099   /// want.
0100   MachineModuleInfoImpl *ObjFileMMI;
0101 
0102   /// Maps IR Functions to their corresponding MachineFunctions.
0103   DenseMap<const Function*, std::unique_ptr<MachineFunction>> MachineFunctions;
0104   /// Next unique number available for a MachineFunction.
0105   unsigned NextFnNum = 0;
0106   const Function *LastRequest = nullptr; ///< Used for shortcut/cache.
0107   MachineFunction *LastResult = nullptr; ///< Used for shortcut/cache.
0108 
0109   MachineModuleInfo &operator=(MachineModuleInfo &&MMII) = delete;
0110 
0111 public:
0112   explicit MachineModuleInfo(const TargetMachine *TM = nullptr);
0113 
0114   explicit MachineModuleInfo(const TargetMachine *TM, MCContext *ExtContext);
0115 
0116   MachineModuleInfo(MachineModuleInfo &&MMII);
0117 
0118   ~MachineModuleInfo();
0119 
0120   void initialize();
0121   void finalize();
0122 
0123   const TargetMachine &getTarget() const { return TM; }
0124 
0125   const MCContext &getContext() const {
0126     return ExternalContext ? *ExternalContext : Context;
0127   }
0128   MCContext &getContext() {
0129     return ExternalContext ? *ExternalContext : Context;
0130   }
0131 
0132   const Module *getModule() const { return TheModule; }
0133 
0134   /// Returns the MachineFunction constructed for the IR function \p F.
0135   /// Creates a new MachineFunction if none exists yet.
0136   /// NOTE: New pass manager clients shall not use this method to get
0137   /// the `MachineFunction`, use `MachineFunctionAnalysis` instead.
0138   MachineFunction &getOrCreateMachineFunction(Function &F);
0139 
0140   /// \brief Returns the MachineFunction associated to IR function \p F if there
0141   /// is one, otherwise nullptr.
0142   /// NOTE: New pass manager clients shall not use this method to get
0143   /// the `MachineFunction`, use `MachineFunctionAnalysis` instead.
0144   MachineFunction *getMachineFunction(const Function &F) const;
0145 
0146   /// Delete the MachineFunction \p MF and reset the link in the IR Function to
0147   /// Machine Function map.
0148   void deleteMachineFunctionFor(Function &F);
0149 
0150   /// Add an externally created MachineFunction \p MF for \p F.
0151   void insertFunction(const Function &F, std::unique_ptr<MachineFunction> &&MF);
0152 
0153   /// Keep track of various per-module pieces of information for backends
0154   /// that would like to do so.
0155   template<typename Ty>
0156   Ty &getObjFileInfo() {
0157     if (ObjFileMMI == nullptr)
0158       ObjFileMMI = new Ty(*this);
0159     return *static_cast<Ty*>(ObjFileMMI);
0160   }
0161 
0162   template<typename Ty>
0163   const Ty &getObjFileInfo() const {
0164     return const_cast<MachineModuleInfo*>(this)->getObjFileInfo<Ty>();
0165   }
0166 
0167   /// \}
0168 }; // End class MachineModuleInfo
0169 
0170 class MachineModuleInfoWrapperPass : public ImmutablePass {
0171   MachineModuleInfo MMI;
0172 
0173 public:
0174   static char ID; // Pass identification, replacement for typeid
0175   explicit MachineModuleInfoWrapperPass(const TargetMachine *TM = nullptr);
0176 
0177   explicit MachineModuleInfoWrapperPass(const TargetMachine *TM,
0178                                         MCContext *ExtContext);
0179 
0180   // Initialization and Finalization
0181   bool doInitialization(Module &) override;
0182   bool doFinalization(Module &) override;
0183 
0184   MachineModuleInfo &getMMI() { return MMI; }
0185   const MachineModuleInfo &getMMI() const { return MMI; }
0186 };
0187 
0188 /// An analysis that produces \c MachineModuleInfo for a module.
0189 /// This does not produce its own MachineModuleInfo because we need a consistent
0190 /// MachineModuleInfo to keep ownership of MachineFunctions regardless of
0191 /// analysis invalidation/clearing. So something outside the analysis
0192 /// infrastructure must own the MachineModuleInfo.
0193 class MachineModuleAnalysis : public AnalysisInfoMixin<MachineModuleAnalysis> {
0194   friend AnalysisInfoMixin<MachineModuleAnalysis>;
0195   static AnalysisKey Key;
0196 
0197   MachineModuleInfo &MMI;
0198 
0199 public:
0200   class Result {
0201     MachineModuleInfo &MMI;
0202     Result(MachineModuleInfo &MMI) : MMI(MMI) {}
0203     friend class MachineModuleAnalysis;
0204 
0205   public:
0206     MachineModuleInfo &getMMI() { return MMI; }
0207 
0208     // MMI owes MCContext. It should never be invalidated.
0209     bool invalidate(Module &, const PreservedAnalyses &,
0210                     ModuleAnalysisManager::Invalidator &) {
0211       return false;
0212     }
0213   };
0214 
0215   MachineModuleAnalysis(MachineModuleInfo &MMI) : MMI(MMI) {}
0216 
0217   /// Run the analysis pass and produce machine module information.
0218   Result run(Module &M, ModuleAnalysisManager &);
0219 };
0220 
0221 } // end namespace llvm
0222 
0223 #endif // LLVM_CODEGEN_MACHINEMODULEINFO_H