]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/CodeGen/MachineModuleInfo.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r302418, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / CodeGen / MachineModuleInfo.h
1 //===-- llvm/CodeGen/MachineModuleInfo.h ------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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
12 // schemes.
13 //
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.
17 //
18 // The following information can be retrieved from the MachineModuleInfo.
19 //
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.
28 //
29 //===----------------------------------------------------------------------===//
30
31 #ifndef LLVM_CODEGEN_MACHINEMODULEINFO_H
32 #define LLVM_CODEGEN_MACHINEMODULEINFO_H
33
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"
45
46 namespace llvm {
47
48 //===----------------------------------------------------------------------===//
49 // Forward declarations.
50 class BlockAddress;
51 class CallInst;
52 class Constant;
53 class GlobalVariable;
54 class LandingPadInst;
55 class MDNode;
56 class MMIAddrLabelMap;
57 class MachineBasicBlock;
58 class MachineFunction;
59 class MachineFunctionInitializer;
60 class Module;
61 class PointerType;
62 class StructType;
63
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
68 /// is destroyed.
69 ///
70 class MachineModuleInfoImpl {
71 public:
72   typedef PointerIntPair<MCSymbol*, 1, bool> StubValueTy;
73   virtual ~MachineModuleInfoImpl();
74   typedef std::vector<std::pair<MCSymbol*, StubValueTy> > SymbolListTy;
75 protected:
76
77   /// Return the entries from a DenseMap in a deterministic sorted orer.
78   /// Clears the map.
79   static SymbolListTy getSortedStubs(DenseMap<MCSymbol*, StubValueTy>&);
80 };
81
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
85 /// for specific use.
86 ///
87 class MachineModuleInfo : public ImmutablePass {
88   const TargetMachine &TM;
89
90   /// This is the MCContext used for the entire code generator.
91   MCContext Context;
92
93   /// This is the LLVM Module being worked on.
94   const Module *TheModule;
95
96   /// This is the object-file-format-specific implementation of
97   /// MachineModuleInfoImpl, which lets targets accumulate whatever info they
98   /// want.
99   MachineModuleInfoImpl *ObjFileMMI;
100
101   /// \name Exception Handling
102   /// \{
103
104   /// Vector of all personality functions ever seen. Used to emit common EH
105   /// frames.
106   std::vector<const Function *> Personalities;
107
108   /// The current call site index being processed, if any. 0 if none.
109   unsigned CurCallSite;
110
111   /// \}
112
113   /// This map keeps track of which symbol is being used for the specified
114   /// basic block's address of label.
115   MMIAddrLabelMap *AddrLabelSymbols;
116
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.
122
123   /// True if debugging information is available in this module.
124   bool DbgInfoAvailable;
125
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
128   /// targets.
129   bool UsesVAFloatArgument;
130
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;
136
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.
144
145 public:
146   static char ID; // Pass identification, replacement for typeid
147
148   explicit MachineModuleInfo(const TargetMachine *TM = nullptr);
149   ~MachineModuleInfo() override;
150
151   // Initialization and Finalization
152   bool doInitialization(Module &) override;
153   bool doFinalization(Module &) override;
154
155   const MCContext &getContext() const { return Context; }
156   MCContext &getContext() { return Context; }
157
158   void setModule(const Module *M) { TheModule = M; }
159   const Module *getModule() const { return TheModule; }
160
161   void setMachineFunctionInitializer(MachineFunctionInitializer *MFInit) {
162     MFInitializer = MFInit;
163   }
164
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);
169
170   /// Delete the MachineFunction \p MF and reset the link in the IR Function to
171   /// Machine Function map.
172   void deleteMachineFunctionFor(Function &F);
173
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);
181   }
182
183   template<typename Ty>
184   const Ty &getObjFileInfo() const {
185     return const_cast<MachineModuleInfo*>(this)->getObjFileInfo<Ty>();
186   }
187
188   /// Returns true if valid debug info is present.
189   bool hasDebugInfo() const { return DbgInfoAvailable; }
190   void setDebugInfoAvailability(bool avail) { DbgInfoAvailable = avail; }
191
192   bool usesVAFloatArgument() const {
193     return UsesVAFloatArgument;
194   }
195
196   void setUsesVAFloatArgument(bool b) {
197     UsesVAFloatArgument = b;
198   }
199
200   bool usesMorestackAddr() const {
201     return UsesMorestackAddr;
202   }
203
204   void setUsesMorestackAddr(bool b) {
205     UsesMorestackAddr = b;
206   }
207
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();
213   }
214
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);
219
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
223   /// definition.
224   void takeDeletedSymbolsForFunction(const Function *F,
225                                      std::vector<MCSymbol*> &Result);
226
227   /// \name Exception Handling
228   /// \{
229
230   /// Set the call site currently being processed.
231   void setCurrentCallSite(unsigned Site) { CurCallSite = Site; }
232
233   /// Get the call site currently being processed, if any.  return zero if
234   /// none.
235   unsigned getCurrentCallSite() { return CurCallSite; }
236
237   /// Provide the personality function for the exception information.
238   void addPersonality(const Function *Personality);
239
240   /// Return array of personality functions ever seen.
241   const std::vector<const Function *>& getPersonalities() const {
242     return Personalities;
243   }
244   /// \}
245 }; // End class MachineModuleInfo
246
247 //===- MMI building helpers -----------------------------------------------===//
248
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);
254
255 } // End llvm namespace
256
257 #endif