1 //===-- llvm/CodeGen/MachineModuleInfo.h ------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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
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.
17 // The following information can be retrieved from the MachineModuleInfo.
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.
28 //===----------------------------------------------------------------------===//
30 #ifndef LLVM_CODEGEN_MACHINEMODULEINFO_H
31 #define LLVM_CODEGEN_MACHINEMODULEINFO_H
33 #include "llvm/ADT/ArrayRef.h"
34 #include "llvm/ADT/DenseMap.h"
35 #include "llvm/ADT/PointerIntPair.h"
36 #include "llvm/MC/MCContext.h"
37 #include "llvm/MC/MCSymbol.h"
38 #include "llvm/Pass.h"
48 class LLVMTargetMachine
;
49 class MMIAddrLabelMap
;
50 class MachineFunction
;
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 MMI::getInfo and destroyed when the MachineModuleInfo
59 class MachineModuleInfoImpl
{
61 using StubValueTy
= PointerIntPair
<MCSymbol
*, 1, bool>;
62 using SymbolListTy
= std::vector
<std::pair
<MCSymbol
*, StubValueTy
>>;
64 virtual ~MachineModuleInfoImpl();
67 /// Return the entries from a DenseMap in a deterministic sorted orer.
69 static SymbolListTy
getSortedStubs(DenseMap
<MCSymbol
*, StubValueTy
>&);
72 //===----------------------------------------------------------------------===//
73 /// This class contains meta information specific to a module. Queries can be
74 /// made by different debugging and exception handling schemes and reformated
77 class MachineModuleInfo
: public ImmutablePass
{
78 const LLVMTargetMachine
&TM
;
80 /// This is the MCContext used for the entire code generator.
83 /// This is the LLVM Module being worked on.
84 const Module
*TheModule
;
86 /// This is the object-file-format-specific implementation of
87 /// MachineModuleInfoImpl, which lets targets accumulate whatever info they
89 MachineModuleInfoImpl
*ObjFileMMI
;
91 /// \name Exception Handling
94 /// Vector of all personality functions ever seen. Used to emit common EH
96 std::vector
<const Function
*> Personalities
;
98 /// The current call site index being processed, if any. 0 if none.
103 /// This map keeps track of which symbol is being used for the specified
104 /// basic block's address of label.
105 MMIAddrLabelMap
*AddrLabelSymbols
;
107 // TODO: Ideally, what we'd like is to have a switch that allows emitting
108 // synchronous (precise at call-sites only) CFA into .eh_frame. However,
109 // even under this switch, we'd like .debug_frame to be precise when using
110 // -g. At this moment, there's no way to specify that some CFI directives
111 // go into .eh_frame only, while others go into .debug_frame only.
113 /// True if debugging information is available in this module.
114 bool DbgInfoAvailable
;
116 /// True if this module is being built for windows/msvc, and uses floating
117 /// point. This is used to emit an undefined reference to _fltused.
118 bool UsesMSVCFloatingPoint
;
120 /// True if the module calls the __morestack function indirectly, as is
121 /// required under the large code model on x86. This is used to emit
122 /// a definition of a symbol, __morestack_addr, containing the address. See
123 /// comments in lib/Target/X86/X86FrameLowering.cpp for more details.
124 bool UsesMorestackAddr
;
126 /// True if the module contains split-stack functions. This is used to
127 /// emit .note.GNU-split-stack section as required by the linker for
128 /// special handling split-stack function calling no-split-stack function.
131 /// True if the module contains no-split-stack functions. This is used to
132 /// emit .note.GNU-no-split-stack section when it also contains split-stack
134 bool HasNosplitStack
;
136 /// Maps IR Functions to their corresponding MachineFunctions.
137 DenseMap
<const Function
*, std::unique_ptr
<MachineFunction
>> MachineFunctions
;
138 /// Next unique number available for a MachineFunction.
139 unsigned NextFnNum
= 0;
140 const Function
*LastRequest
= nullptr; ///< Used for shortcut/cache.
141 MachineFunction
*LastResult
= nullptr; ///< Used for shortcut/cache.
144 static char ID
; // Pass identification, replacement for typeid
146 explicit MachineModuleInfo(const LLVMTargetMachine
*TM
= nullptr);
147 ~MachineModuleInfo() override
;
149 // Initialization and Finalization
150 bool doInitialization(Module
&) override
;
151 bool doFinalization(Module
&) override
;
153 const LLVMTargetMachine
&getTarget() const { return TM
; }
155 const MCContext
&getContext() const { return Context
; }
156 MCContext
&getContext() { return Context
; }
158 const Module
*getModule() const { return TheModule
; }
160 /// Returns the MachineFunction constructed for the IR function \p F.
161 /// Creates a new MachineFunction if none exists yet.
162 MachineFunction
&getOrCreateMachineFunction(const Function
&F
);
164 /// \bried Returns the MachineFunction associated to IR function \p F if there
165 /// is one, otherwise nullptr.
166 MachineFunction
*getMachineFunction(const Function
&F
) const;
168 /// Delete the MachineFunction \p MF and reset the link in the IR Function to
169 /// Machine Function map.
170 void deleteMachineFunctionFor(Function
&F
);
172 /// Keep track of various per-function pieces of information for backends
173 /// that would like to do so.
174 template<typename Ty
>
175 Ty
&getObjFileInfo() {
176 if (ObjFileMMI
== nullptr)
177 ObjFileMMI
= new Ty(*this);
178 return *static_cast<Ty
*>(ObjFileMMI
);
181 template<typename Ty
>
182 const Ty
&getObjFileInfo() const {
183 return const_cast<MachineModuleInfo
*>(this)->getObjFileInfo
<Ty
>();
186 /// Returns true if valid debug info is present.
187 bool hasDebugInfo() const { return DbgInfoAvailable
; }
188 void setDebugInfoAvailability(bool avail
) { DbgInfoAvailable
= avail
; }
190 bool usesMSVCFloatingPoint() const { return UsesMSVCFloatingPoint
; }
192 void setUsesMSVCFloatingPoint(bool b
) { UsesMSVCFloatingPoint
= b
; }
194 bool usesMorestackAddr() const {
195 return UsesMorestackAddr
;
198 void setUsesMorestackAddr(bool b
) {
199 UsesMorestackAddr
= b
;
202 bool hasSplitStack() const {
203 return HasSplitStack
;
206 void setHasSplitStack(bool b
) {
210 bool hasNosplitStack() const {
211 return HasNosplitStack
;
214 void setHasNosplitStack(bool b
) {
218 /// Return the symbol to be used for the specified basic block when its
219 /// address is taken. This cannot be its normal LBB label because the block
220 /// may be accessed outside its containing function.
221 MCSymbol
*getAddrLabelSymbol(const BasicBlock
*BB
) {
222 return getAddrLabelSymbolToEmit(BB
).front();
225 /// Return the symbol to be used for the specified basic block when its
226 /// address is taken. If other blocks were RAUW'd to this one, we may have
227 /// to emit them as well, return the whole set.
228 ArrayRef
<MCSymbol
*> getAddrLabelSymbolToEmit(const BasicBlock
*BB
);
230 /// If the specified function has had any references to address-taken blocks
231 /// generated, but the block got deleted, return the symbol now so we can
232 /// emit it. This prevents emitting a reference to a symbol that has no
234 void takeDeletedSymbolsForFunction(const Function
*F
,
235 std::vector
<MCSymbol
*> &Result
);
237 /// \name Exception Handling
240 /// Set the call site currently being processed.
241 void setCurrentCallSite(unsigned Site
) { CurCallSite
= Site
; }
243 /// Get the call site currently being processed, if any. return zero if
245 unsigned getCurrentCallSite() { return CurCallSite
; }
247 /// Provide the personality function for the exception information.
248 void addPersonality(const Function
*Personality
);
250 /// Return array of personality functions ever seen.
251 const std::vector
<const Function
*>& getPersonalities() const {
252 return Personalities
;
255 }; // End class MachineModuleInfo
257 } // end namespace llvm
259 #endif // LLVM_CODEGEN_MACHINEMODULEINFO_H