LLVM 24.0.0git
MachineModuleInfo.h
Go to the documentation of this file.
1//===-- llvm/CodeGen/MachineModuleInfo.h ------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Collect meta information for a module. This information should be in a
10// neutral form that can be used by different debugging and exception handling
11// schemes.
12//
13// The organization of information is primarily clustered around the source
14// compile units. The main exception is source line correspondence where
15// inlining may interleave code from various compile units.
16//
17// The following information can be retrieved from the MachineModuleInfo.
18//
19// -- Source directories - Directories are uniqued based on their canonical
20// string and assigned a sequential numeric ID (base 1.)
21// -- Source files - Files are also uniqued based on their name and directory
22// ID. A file ID is sequential number (base 1.)
23// -- Source line correspondence - A vector of file ID, line#, column# triples.
24// A DEBUG_LOCATION instruction is generated by the DAG Legalizer
25// corresponding to each entry in the source line list. This allows a debug
26// emitter to generate labels referenced by debug information tables.
27//
28//===----------------------------------------------------------------------===//
29
30#ifndef LLVM_CODEGEN_MACHINEMODULEINFO_H
31#define LLVM_CODEGEN_MACHINEMODULEINFO_H
32
33#include "llvm/ADT/DenseMap.h"
34#include "llvm/ADT/DenseSet.h"
37#include "llvm/IR/PassManager.h"
38#include "llvm/MC/MCContext.h"
39#include "llvm/MC/MCSymbol.h"
40#include "llvm/Pass.h"
42#include <memory>
43#include <utility>
44#include <vector>
45
46namespace llvm {
47
48class Function;
49class TargetMachine;
50class MachineFunction;
51class Module;
52
53//===----------------------------------------------------------------------===//
54/// This class can be derived from and used by targets to hold private
55/// target-specific information for each Module. Objects of type are
56/// accessed/created with MachineModuleInfo::getObjFileInfo and destroyed when
57/// the MachineModuleInfo is destroyed.
58///
60public:
62 using SymbolListTy = std::vector<std::pair<MCSymbol *, StubValueTy>>;
63
64 /// A variant of SymbolListTy where the stub is a generalized MCExpr.
65 using ExprStubListTy = std::vector<std::pair<MCSymbol *, const MCExpr *>>;
66
68
69protected:
70 /// Return the entries from a DenseMap in a deterministic sorted orer.
71 /// Clears the map.
73
74 /// Return the entries from a DenseMap in a deterministic sorted orer.
75 /// Clears the map.
76 static ExprStubListTy
78};
79
80//===----------------------------------------------------------------------===//
81/// This class contains meta information specific to a module. Queries can be
82/// made by different debugging and exception handling schemes and reformated
83/// for specific use.
84///
88
89 const TargetMachine &TM;
90
91 /// This is the MCContext used for the entire code generator.
92 MCContext Context;
93 // This is an external context, that if assigned, will be used instead of the
94 // internal context.
95 MCContext *ExternalContext = nullptr;
96
97 /// This is the LLVM Module being worked on.
98 const Module *TheModule = nullptr;
99
100 /// This is the object-file-format-specific implementation of
101 /// MachineModuleInfoImpl, which lets targets accumulate whatever info they
102 /// want.
103 MachineModuleInfoImpl *ObjFileMMI;
104
105 /// Maps IR Functions to their corresponding MachineFunctions.
107 /// Next unique number available for a MachineFunction.
108 unsigned NextFnNum = 0;
109 const Function *LastRequest = nullptr; ///< Used for shortcut/cache.
110 MachineFunction *LastResult = nullptr; ///< Used for shortcut/cache.
111
112 // MachineFunctions are freed only when all the functions in the same
113 // deletion grouping have been finalized.
114 EquivalenceClasses<const Function *> MFDeletionGrouping;
115 // Add to this set once a function has been fully processed.
116 DenseSet<const Function *> FinalizedMFs;
117
118 MachineModuleInfo &operator=(MachineModuleInfo &&MMII) = delete;
119
120public:
121 LLVM_ABI explicit MachineModuleInfo(const TargetMachine *TM = nullptr);
122
123 LLVM_ABI explicit MachineModuleInfo(const TargetMachine *TM,
124 MCContext *ExtContext);
125
127
129
130 LLVM_ABI void initialize();
131 LLVM_ABI void finalize();
132
133 const TargetMachine &getTarget() const { return TM; }
134
135 const MCContext &getContext() const {
136 return ExternalContext ? *ExternalContext : Context;
137 }
139 return ExternalContext ? *ExternalContext : Context;
140 }
141
142 const Module *getModule() const { return TheModule; }
143
144 /// Returns the MachineFunction constructed for the IR function \p F.
145 /// Creates a new MachineFunction if none exists yet.
146 /// NOTE: New pass manager clients shall not use this method to get
147 /// the `MachineFunction`, use `MachineFunctionAnalysis` instead.
149
150 /// \brief Returns the MachineFunction associated to IR function \p F if there
151 /// is one, otherwise nullptr.
152 /// NOTE: New pass manager clients shall not use this method to get
153 /// the `MachineFunction`, use `MachineFunctionAnalysis` instead.
155
156 /// Group two IR functions so their MachineFunctions are deleted together once
157 /// both functions have been finalized.
159 const Function &F2) {
160 if (FinalizedMFs.count(&F1) || FinalizedMFs.count(&F2))
161 return;
162 MFDeletionGrouping.unionSets(&F1, &F2);
163 }
164
165 /// Delete the MachineFunction \p MF and reset the link in the IR Function to
166 /// Machine Function map. When a function is not grouped with any other
167 /// function, its MF gets deleted right away. When a function is grouped with
168 /// other functions, its MF gets deleted when all functions in the same group
169 /// have been finalized.
171
172 /// Add an externally created MachineFunction \p MF for \p F.
173 LLVM_ABI void insertFunction(const Function &F,
174 std::unique_ptr<MachineFunction> &&MF);
175
176 /// Keep track of various per-module pieces of information for backends
177 /// that would like to do so.
178 template<typename Ty>
180 if (ObjFileMMI == nullptr)
181 ObjFileMMI = new Ty(*this);
182 return *static_cast<Ty*>(ObjFileMMI);
183 }
184
185 template<typename Ty>
186 const Ty &getObjFileInfo() const {
187 return const_cast<MachineModuleInfo*>(this)->getObjFileInfo<Ty>();
188 }
189
190 /// \}
191}; // End class MachineModuleInfo
192
195
196public:
197 static char ID; // Pass identification, replacement for typeid
198 explicit MachineModuleInfoWrapperPass(const TargetMachine *TM = nullptr);
199
201 MCContext *ExtContext);
202
203 // Initialization and Finalization
204 bool doInitialization(Module &) override;
205 bool doFinalization(Module &) override;
206
207 MachineModuleInfo &getMMI() { return MMI; }
208 const MachineModuleInfo &getMMI() const { return MMI; }
209};
210
211/// An analysis that produces \c MachineModuleInfo for a module.
212/// This does not produce its own MachineModuleInfo because we need a consistent
213/// MachineModuleInfo to keep ownership of MachineFunctions regardless of
214/// analysis invalidation/clearing. So something outside the analysis
215/// infrastructure must own the MachineModuleInfo.
216class MachineModuleAnalysis : public AnalysisInfoMixin<MachineModuleAnalysis> {
218 LLVM_ABI static AnalysisKey Key;
219
221
222public:
223 class Result {
225 Result(MachineModuleInfo &MMI) : MMI(MMI) {}
227
228 public:
229 MachineModuleInfo &getMMI() { return MMI; }
230
231 // MMI owes MCContext. It should never be invalidated.
233 ModuleAnalysisManager::Invalidator &) {
234 return false;
235 }
236 };
237
239
240 /// Run the analysis pass and produce machine module information.
242};
243
244} // end namespace llvm
245
246#endif // LLVM_CODEGEN_MACHINEMODULEINFO_H
#define LLVM_ABI
Definition Compiler.h:215
This file defines the DenseMap class.
This file defines the DenseSet and SmallDenseSet classes.
Generic implementation of equivalence classes through the use Tarjan's efficient union-find algorithm...
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:54
This file defines the PointerIntPair class.
Implements a dense probed hash-table based set.
Definition DenseSet.h:281
This represents a collection of equivalence classes and supports three efficient operations: insert a...
ImmutablePass(char &pid)
Definition Pass.h:287
Context object for machine code objects.
Definition MCContext.h:83
bool invalidate(Module &, const PreservedAnalyses &, ModuleAnalysisManager::Invalidator &)
LLVM_ABI Result run(Module &M, ModuleAnalysisManager &)
Run the analysis pass and produce machine module information.
MachineModuleAnalysis(MachineModuleInfo &MMI)
This class can be derived from and used by targets to hold private target-specific information for ea...
std::vector< std::pair< MCSymbol *, StubValueTy > > SymbolListTy
static SymbolListTy getSortedStubs(DenseMap< MCSymbol *, StubValueTy > &)
Return the entries from a DenseMap in a deterministic sorted orer.
std::vector< std::pair< MCSymbol *, const MCExpr * > > ExprStubListTy
A variant of SymbolListTy where the stub is a generalized MCExpr.
PointerIntPair< MCSymbol *, 1, bool > StubValueTy
static ExprStubListTy getSortedExprStubs(DenseMap< MCSymbol *, const MCExpr * > &)
Return the entries from a DenseMap in a deterministic sorted orer.
const MachineModuleInfo & getMMI() const
bool doFinalization(Module &) override
doFinalization - Virtual method overriden by subclasses to do any necessary clean up after all passes...
MachineModuleInfoWrapperPass(const TargetMachine *TM=nullptr)
bool doInitialization(Module &) override
doInitialization - Virtual method overridden by subclasses to do any necessary initialization before ...
This class contains meta information specific to a module.
LLVM_ABI MachineModuleInfo(const TargetMachine *TM=nullptr)
LLVM_ABI void insertFunction(const Function &F, std::unique_ptr< MachineFunction > &&MF)
Add an externally created MachineFunction MF for F.
const Ty & getObjFileInfo() const
const MCContext & getContext() const
const Module * getModule() const
friend class MachineModuleInfoWrapperPass
LLVM_ABI MachineFunction & getOrCreateMachineFunction(Function &F)
Returns the MachineFunction constructed for the IR function F.
Ty & getObjFileInfo()
Keep track of various per-module pieces of information for backends that would like to do so.
const TargetMachine & getTarget() const
LLVM_ABI MachineFunction * getMachineFunction(const Function &F) const
Returns the MachineFunction associated to IR function F if there is one, otherwise nullptr.
void groupMachineFunctionsForDeletion(const Function &F1, const Function &F2)
Group two IR functions so their MachineFunctions are deleted together once both functions have been f...
LLVM_ABI void deleteMachineFunctionFor(Function &F)
Delete the MachineFunction MF and reset the link in the IR Function to Machine Function map.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
PointerIntPair - This class implements a pair of a pointer and small integer.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
Primary interface to the complete machine description for the target machine.
This is an optimization pass for GlobalISel generic memory operations.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
A CRTP mix-in that provides informational APIs needed for analysis passes.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29