xref: /freebsd-src/contrib/llvm-project/llvm/include/llvm/CodeGen/MachineModuleInfo.h (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
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/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 
43 namespace llvm {
44 
45 class BasicBlock;
46 class CallInst;
47 class Function;
48 class LLVMTargetMachine;
49 class MMIAddrLabelMap;
50 class MachineFunction;
51 class 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 MMI::getInfo and destroyed when the MachineModuleInfo
57 /// is destroyed.
58 ///
59 class MachineModuleInfoImpl {
60 public:
61   using StubValueTy = PointerIntPair<MCSymbol *, 1, bool>;
62   using SymbolListTy = std::vector<std::pair<MCSymbol *, StubValueTy>>;
63 
64   virtual ~MachineModuleInfoImpl();
65 
66 protected:
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 ///
77 class MachineModuleInfo : public ImmutablePass {
78   const LLVMTargetMachine &TM;
79 
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 
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.
112 
113   /// True if debugging information is available in this module.
114   bool DbgInfoAvailable;
115 
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;
119 
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 
143 public:
144   static char ID; // Pass identification, replacement for typeid
145 
146   explicit MachineModuleInfo(const LLVMTargetMachine *TM = nullptr);
147   ~MachineModuleInfo() override;
148 
149   // Initialization and Finalization
150   bool doInitialization(Module &) override;
151   bool doFinalization(Module &) override;
152 
153   const LLVMTargetMachine &getTarget() const { return TM; }
154 
155   const MCContext &getContext() const { return Context; }
156   MCContext &getContext() { return Context; }
157 
158   const Module *getModule() const { return TheModule; }
159 
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);
163 
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;
167 
168   /// Delete the MachineFunction \p MF and reset the link in the IR Function to
169   /// Machine Function map.
170   void deleteMachineFunctionFor(Function &F);
171 
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);
179   }
180 
181   template<typename Ty>
182   const Ty &getObjFileInfo() const {
183     return const_cast<MachineModuleInfo*>(this)->getObjFileInfo<Ty>();
184   }
185 
186   /// Returns true if valid debug info is present.
187   bool hasDebugInfo() const { return DbgInfoAvailable; }
188   void setDebugInfoAvailability(bool avail) { DbgInfoAvailable = avail; }
189 
190   bool usesMSVCFloatingPoint() const { return UsesMSVCFloatingPoint; }
191 
192   void setUsesMSVCFloatingPoint(bool b) { UsesMSVCFloatingPoint = b; }
193 
194   bool usesMorestackAddr() const {
195     return UsesMorestackAddr;
196   }
197 
198   void setUsesMorestackAddr(bool b) {
199     UsesMorestackAddr = b;
200   }
201 
202   bool hasSplitStack() const {
203     return HasSplitStack;
204   }
205 
206   void setHasSplitStack(bool b) {
207     HasSplitStack = b;
208   }
209 
210   bool hasNosplitStack() const {
211     return HasNosplitStack;
212   }
213 
214   void setHasNosplitStack(bool b) {
215     HasNosplitStack = b;
216   }
217 
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();
223   }
224 
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);
229 
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
233   /// definition.
234   void takeDeletedSymbolsForFunction(const Function *F,
235                                      std::vector<MCSymbol*> &Result);
236 
237   /// \name Exception Handling
238   /// \{
239 
240   /// Set the call site currently being processed.
241   void setCurrentCallSite(unsigned Site) { CurCallSite = Site; }
242 
243   /// Get the call site currently being processed, if any.  return zero if
244   /// none.
245   unsigned getCurrentCallSite() { return CurCallSite; }
246 
247   /// Provide the personality function for the exception information.
248   void addPersonality(const Function *Personality);
249 
250   /// Return array of personality functions ever seen.
251   const std::vector<const Function *>& getPersonalities() const {
252     return Personalities;
253   }
254   /// \}
255 }; // End class MachineModuleInfo
256 
257 } // end namespace llvm
258 
259 #endif // LLVM_CODEGEN_MACHINEMODULEINFO_H
260