blob: 9b81dc6a45e5263478fade9a97a6b86a07dfda23 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===-- llvm/CodeGen/MachineModuleInfo.h ------------------------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
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/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"
39#include <memory>
40#include <utility>
41#include <vector>
42
43namespace llvm {
44
45class BasicBlock;
46class CallInst;
47class Function;
Andrew Walbran16937d02019-10-22 13:54:20 +010048class LLVMTargetMachine;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010049class MMIAddrLabelMap;
Andrew Walbran16937d02019-10-22 13:54:20 +010050class MachineFunction;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010051class Module;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010052
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
57/// is destroyed.
58///
59class MachineModuleInfoImpl {
60public:
61 using StubValueTy = PointerIntPair<MCSymbol *, 1, bool>;
62 using SymbolListTy = std::vector<std::pair<MCSymbol *, StubValueTy>>;
63
64 virtual ~MachineModuleInfoImpl();
65
66protected:
67 /// Return the entries from a DenseMap in a deterministic sorted orer.
68 /// Clears the map.
69 static SymbolListTy getSortedStubs(DenseMap<MCSymbol*, StubValueTy>&);
70};
71
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
75/// for specific use.
76///
77class MachineModuleInfo : public ImmutablePass {
Andrew Walbran16937d02019-10-22 13:54:20 +010078 const LLVMTargetMachine &TM;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010079
80 /// This is the MCContext used for the entire code generator.
81 MCContext Context;
82
83 /// This is the LLVM Module being worked on.
84 const Module *TheModule;
85
86 /// This is the object-file-format-specific implementation of
87 /// MachineModuleInfoImpl, which lets targets accumulate whatever info they
88 /// want.
89 MachineModuleInfoImpl *ObjFileMMI;
90
91 /// \name Exception Handling
92 /// \{
93
94 /// Vector of all personality functions ever seen. Used to emit common EH
95 /// frames.
96 std::vector<const Function *> Personalities;
97
98 /// The current call site index being processed, if any. 0 if none.
99 unsigned CurCallSite;
100
101 /// \}
102
103 /// This map keeps track of which symbol is being used for the specified
104 /// basic block's address of label.
105 MMIAddrLabelMap *AddrLabelSymbols;
106
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100107 // TODO: Ideally, what we'd like is to have a switch that allows emitting
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100108 // 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.
112
113 /// True if debugging information is available in this module.
114 bool DbgInfoAvailable;
115
Andrew Walbran16937d02019-10-22 13:54:20 +0100116 /// 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;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100119
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;
125
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.
129 bool HasSplitStack;
130
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
133 /// functions.
134 bool HasNosplitStack;
135
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.
142
143public:
144 static char ID; // Pass identification, replacement for typeid
145
Andrew Walbran16937d02019-10-22 13:54:20 +0100146 explicit MachineModuleInfo(const LLVMTargetMachine *TM = nullptr);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100147 ~MachineModuleInfo() override;
148
149 // Initialization and Finalization
150 bool doInitialization(Module &) override;
151 bool doFinalization(Module &) override;
152
153 const MCContext &getContext() const { return Context; }
154 MCContext &getContext() { return Context; }
155
156 const Module *getModule() const { return TheModule; }
157
158 /// Returns the MachineFunction constructed for the IR function \p F.
159 /// Creates a new MachineFunction if none exists yet.
160 MachineFunction &getOrCreateMachineFunction(const Function &F);
161
162 /// \bried Returns the MachineFunction associated to IR function \p F if there
163 /// is one, otherwise nullptr.
164 MachineFunction *getMachineFunction(const Function &F) const;
165
166 /// Delete the MachineFunction \p MF and reset the link in the IR Function to
167 /// Machine Function map.
168 void deleteMachineFunctionFor(Function &F);
169
170 /// Keep track of various per-function pieces of information for backends
171 /// that would like to do so.
172 template<typename Ty>
173 Ty &getObjFileInfo() {
174 if (ObjFileMMI == nullptr)
175 ObjFileMMI = new Ty(*this);
176 return *static_cast<Ty*>(ObjFileMMI);
177 }
178
179 template<typename Ty>
180 const Ty &getObjFileInfo() const {
181 return const_cast<MachineModuleInfo*>(this)->getObjFileInfo<Ty>();
182 }
183
184 /// Returns true if valid debug info is present.
185 bool hasDebugInfo() const { return DbgInfoAvailable; }
186 void setDebugInfoAvailability(bool avail) { DbgInfoAvailable = avail; }
187
Andrew Walbran16937d02019-10-22 13:54:20 +0100188 bool usesMSVCFloatingPoint() const { return UsesMSVCFloatingPoint; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100189
Andrew Walbran16937d02019-10-22 13:54:20 +0100190 void setUsesMSVCFloatingPoint(bool b) { UsesMSVCFloatingPoint = b; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100191
192 bool usesMorestackAddr() const {
193 return UsesMorestackAddr;
194 }
195
196 void setUsesMorestackAddr(bool b) {
197 UsesMorestackAddr = b;
198 }
199
200 bool hasSplitStack() const {
201 return HasSplitStack;
202 }
203
204 void setHasSplitStack(bool b) {
205 HasSplitStack = b;
206 }
207
208 bool hasNosplitStack() const {
209 return HasNosplitStack;
210 }
211
212 void setHasNosplitStack(bool b) {
213 HasNosplitStack = b;
214 }
215
216 /// Return the symbol to be used for the specified basic block when its
217 /// address is taken. This cannot be its normal LBB label because the block
218 /// may be accessed outside its containing function.
219 MCSymbol *getAddrLabelSymbol(const BasicBlock *BB) {
220 return getAddrLabelSymbolToEmit(BB).front();
221 }
222
223 /// Return the symbol to be used for the specified basic block when its
224 /// address is taken. If other blocks were RAUW'd to this one, we may have
225 /// to emit them as well, return the whole set.
226 ArrayRef<MCSymbol *> getAddrLabelSymbolToEmit(const BasicBlock *BB);
227
228 /// If the specified function has had any references to address-taken blocks
229 /// generated, but the block got deleted, return the symbol now so we can
230 /// emit it. This prevents emitting a reference to a symbol that has no
231 /// definition.
232 void takeDeletedSymbolsForFunction(const Function *F,
233 std::vector<MCSymbol*> &Result);
234
235 /// \name Exception Handling
236 /// \{
237
238 /// Set the call site currently being processed.
239 void setCurrentCallSite(unsigned Site) { CurCallSite = Site; }
240
241 /// Get the call site currently being processed, if any. return zero if
242 /// none.
243 unsigned getCurrentCallSite() { return CurCallSite; }
244
245 /// Provide the personality function for the exception information.
246 void addPersonality(const Function *Personality);
247
248 /// Return array of personality functions ever seen.
249 const std::vector<const Function *>& getPersonalities() const {
250 return Personalities;
251 }
252 /// \}
253}; // End class MachineModuleInfo
254
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100255} // end namespace llvm
256
257#endif // LLVM_CODEGEN_MACHINEMODULEINFO_H