1 //===-- llvm/CodeGen/MachineModuleInfo.h ------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Collect meta information for a module. This information should be in a
11 // neutral form that can be used by different debugging and exception handling
14 // The organization of information is primarily clustered around the source
15 // compile units. The main exception is source line correspondence where
16 // inlining may interleave code from various compile units.
18 // The following information can be retrieved from the MachineModuleInfo.
20 // -- Source directories - Directories are uniqued based on their canonical
21 // string and assigned a sequential numeric ID (base 1.)
22 // -- Source files - Files are also uniqued based on their name and directory
23 // ID. A file ID is sequential number (base 1.)
24 // -- Source line correspondence - A vector of file ID, line#, column# triples.
25 // A DEBUG_LOCATION instruction is generated by the DAG Legalizer
26 // corresponding to each entry in the source line list. This allows a debug
27 // emitter to generate labels referenced by debug information tables.
29 //===----------------------------------------------------------------------===//
31 #ifndef LLVM_CODEGEN_MACHINEMODULEINFO_H
32 #define LLVM_CODEGEN_MACHINEMODULEINFO_H
34 #include "llvm/ADT/DenseMap.h"
35 #include "llvm/ADT/PointerIntPair.h"
36 #include "llvm/ADT/SmallPtrSet.h"
37 #include "llvm/ADT/SmallVector.h"
38 #include "llvm/IR/DebugLoc.h"
39 #include "llvm/IR/ValueHandle.h"
40 #include "llvm/MC/MCContext.h"
41 #include "llvm/MC/MCSymbol.h"
42 #include "llvm/MC/MachineLocation.h"
43 #include "llvm/Pass.h"
44 #include "llvm/Support/DataTypes.h"
48 //===----------------------------------------------------------------------===//
49 // Forward declarations.
56 class MMIAddrLabelMap;
57 class MachineBasicBlock;
58 class MachineFunction;
59 class MachineFunctionInitializer;
64 //===----------------------------------------------------------------------===//
65 /// This class can be derived from and used by targets to hold private
66 /// target-specific information for each Module. Objects of type are
67 /// accessed/created with MMI::getInfo and destroyed when the MachineModuleInfo
70 class MachineModuleInfoImpl {
72 typedef PointerIntPair<MCSymbol*, 1, bool> StubValueTy;
73 virtual ~MachineModuleInfoImpl();
74 typedef std::vector<std::pair<MCSymbol*, StubValueTy> > SymbolListTy;
77 /// Return the entries from a DenseMap in a deterministic sorted orer.
79 static SymbolListTy getSortedStubs(DenseMap<MCSymbol*, StubValueTy>&);
82 //===----------------------------------------------------------------------===//
83 /// This class contains meta information specific to a module. Queries can be
84 /// made by different debugging and exception handling schemes and reformated
87 class MachineModuleInfo : public ImmutablePass {
88 const TargetMachine &TM;
90 /// This is the MCContext used for the entire code generator.
93 /// This is the LLVM Module being worked on.
94 const Module *TheModule;
96 /// This is the object-file-format-specific implementation of
97 /// MachineModuleInfoImpl, which lets targets accumulate whatever info they
99 MachineModuleInfoImpl *ObjFileMMI;
101 /// \name Exception Handling
104 /// Vector of all personality functions ever seen. Used to emit common EH
106 std::vector<const Function *> Personalities;
108 /// The current call site index being processed, if any. 0 if none.
109 unsigned CurCallSite;
113 /// This map keeps track of which symbol is being used for the specified
114 /// basic block's address of label.
115 MMIAddrLabelMap *AddrLabelSymbols;
117 // TODO: Ideally, what we'd like is to have a switch that allows emitting
118 // synchronous (precise at call-sites only) CFA into .eh_frame. However,
119 // even under this switch, we'd like .debug_frame to be precise when using
120 // -g. At this moment, there's no way to specify that some CFI directives
121 // go into .eh_frame only, while others go into .debug_frame only.
123 /// True if debugging information is available in this module.
124 bool DbgInfoAvailable;
126 /// True if this module calls VarArg function with floating-point arguments.
127 /// This is used to emit an undefined reference to _fltused on Windows
129 bool UsesVAFloatArgument;
131 /// True if the module calls the __morestack function indirectly, as is
132 /// required under the large code model on x86. This is used to emit
133 /// a definition of a symbol, __morestack_addr, containing the address. See
134 /// comments in lib/Target/X86/X86FrameLowering.cpp for more details.
135 bool UsesMorestackAddr;
137 MachineFunctionInitializer *MFInitializer;
138 /// Maps IR Functions to their corresponding MachineFunctions.
139 DenseMap<const Function*, std::unique_ptr<MachineFunction>> MachineFunctions;
140 /// Next unique number available for a MachineFunction.
141 unsigned NextFnNum = 0;
142 const Function *LastRequest = nullptr; ///< Used for shortcut/cache.
143 MachineFunction *LastResult = nullptr; ///< Used for shortcut/cache.
146 static char ID; // Pass identification, replacement for typeid
148 explicit MachineModuleInfo(const TargetMachine *TM = nullptr);
149 ~MachineModuleInfo() override;
151 // Initialization and Finalization
152 bool doInitialization(Module &) override;
153 bool doFinalization(Module &) override;
155 const MCContext &getContext() const { return Context; }
156 MCContext &getContext() { return Context; }
158 void setModule(const Module *M) { TheModule = M; }
159 const Module *getModule() const { return TheModule; }
161 void setMachineFunctionInitializer(MachineFunctionInitializer *MFInit) {
162 MFInitializer = MFInit;
165 /// Returns the MachineFunction constructed for the IR function \p F.
166 /// Creates a new MachineFunction and runs the MachineFunctionInitializer
167 /// if none exists yet.
168 MachineFunction &getMachineFunction(const Function &F);
170 /// Delete the MachineFunction \p MF and reset the link in the IR Function to
171 /// Machine Function map.
172 void deleteMachineFunctionFor(Function &F);
174 /// Keep track of various per-function pieces of information for backends
175 /// that would like to do so.
176 template<typename Ty>
177 Ty &getObjFileInfo() {
178 if (ObjFileMMI == nullptr)
179 ObjFileMMI = new Ty(*this);
180 return *static_cast<Ty*>(ObjFileMMI);
183 template<typename Ty>
184 const Ty &getObjFileInfo() const {
185 return const_cast<MachineModuleInfo*>(this)->getObjFileInfo<Ty>();
188 /// Returns true if valid debug info is present.
189 bool hasDebugInfo() const { return DbgInfoAvailable; }
190 void setDebugInfoAvailability(bool avail) { DbgInfoAvailable = avail; }
192 bool usesVAFloatArgument() const {
193 return UsesVAFloatArgument;
196 void setUsesVAFloatArgument(bool b) {
197 UsesVAFloatArgument = b;
200 bool usesMorestackAddr() const {
201 return UsesMorestackAddr;
204 void setUsesMorestackAddr(bool b) {
205 UsesMorestackAddr = b;
208 /// Return the symbol to be used for the specified basic block when its
209 /// address is taken. This cannot be its normal LBB label because the block
210 /// may be accessed outside its containing function.
211 MCSymbol *getAddrLabelSymbol(const BasicBlock *BB) {
212 return getAddrLabelSymbolToEmit(BB).front();
215 /// Return the symbol to be used for the specified basic block when its
216 /// address is taken. If other blocks were RAUW'd to this one, we may have
217 /// to emit them as well, return the whole set.
218 ArrayRef<MCSymbol *> getAddrLabelSymbolToEmit(const BasicBlock *BB);
220 /// If the specified function has had any references to address-taken blocks
221 /// generated, but the block got deleted, return the symbol now so we can
222 /// emit it. This prevents emitting a reference to a symbol that has no
224 void takeDeletedSymbolsForFunction(const Function *F,
225 std::vector<MCSymbol*> &Result);
227 /// \name Exception Handling
230 /// Set the call site currently being processed.
231 void setCurrentCallSite(unsigned Site) { CurCallSite = Site; }
233 /// Get the call site currently being processed, if any. return zero if
235 unsigned getCurrentCallSite() { return CurCallSite; }
237 /// Provide the personality function for the exception information.
238 void addPersonality(const Function *Personality);
240 /// Return array of personality functions ever seen.
241 const std::vector<const Function *>& getPersonalities() const {
242 return Personalities;
245 }; // End class MachineModuleInfo
247 //===- MMI building helpers -----------------------------------------------===//
249 /// Determine if any floating-point values are being passed to this variadic
250 /// function, and set the MachineModuleInfo's usesVAFloatArgument flag if so.
251 /// This flag is used to emit an undefined reference to _fltused on Windows,
252 /// which will link in MSVCRT's floating-point support.
253 void computeUsesVAFloatArgument(const CallInst &I, MachineModuleInfo &MMI);
255 } // End llvm namespace