Classify functions by EH personality type rather than using the triple
[oota-llvm.git] / 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/Metadata.h"
40 #include "llvm/IR/ValueHandle.h"
41 #include "llvm/MC/MCContext.h"
42 #include "llvm/MC/MachineLocation.h"
43 #include "llvm/Pass.h"
44 #include "llvm/Support/DataTypes.h"
45 #include "llvm/Support/Dwarf.h"
46
47 namespace llvm {
48
49 /// Different personality functions used by a function.
50 enum class EHPersonality {
51   None,     /// No exception handling
52   Itanium,  /// An Itanium C++ EH personality like __gxx_personality_seh0
53   Win64SEH, /// x86_64 SEH, uses __C_specific_handler
54 };
55
56 //===----------------------------------------------------------------------===//
57 // Forward declarations.
58 class Constant;
59 class GlobalVariable;
60 class MDNode;
61 class MMIAddrLabelMap;
62 class MachineBasicBlock;
63 class MachineFunction;
64 class Module;
65 class PointerType;
66 class StructType;
67
68 //===----------------------------------------------------------------------===//
69 /// LandingPadInfo - This structure is used to retain landing pad info for
70 /// the current function.
71 ///
72 struct LandingPadInfo {
73   MachineBasicBlock *LandingPadBlock;    // Landing pad block.
74   SmallVector<MCSymbol*, 1> BeginLabels; // Labels prior to invoke.
75   SmallVector<MCSymbol*, 1> EndLabels;   // Labels after invoke.
76   SmallVector<MCSymbol*, 1> ClauseLabels; // Labels for each clause.
77   MCSymbol *LandingPadLabel;             // Label at beginning of landing pad.
78   const Function *Personality;           // Personality function.
79   std::vector<int> TypeIds;              // List of type ids (filters negative)
80
81   explicit LandingPadInfo(MachineBasicBlock *MBB)
82     : LandingPadBlock(MBB), LandingPadLabel(nullptr), Personality(nullptr) {}
83 };
84
85 //===----------------------------------------------------------------------===//
86 /// MachineModuleInfoImpl - This class can be derived from and used by targets
87 /// to hold private target-specific information for each Module.  Objects of
88 /// type are accessed/created with MMI::getInfo and destroyed when the
89 /// MachineModuleInfo is destroyed.
90 /// 
91 class MachineModuleInfoImpl {
92 public:
93   typedef PointerIntPair<MCSymbol*, 1, bool> StubValueTy;
94   virtual ~MachineModuleInfoImpl();
95   typedef std::vector<std::pair<MCSymbol*, StubValueTy> > SymbolListTy;
96 protected:
97   static SymbolListTy GetSortedStubs(const DenseMap<MCSymbol*, StubValueTy>&);
98 };
99
100 //===----------------------------------------------------------------------===//
101 /// MachineModuleInfo - This class contains meta information specific to a
102 /// module.  Queries can be made by different debugging and exception handling
103 /// schemes and reformated for specific use.
104 ///
105 class MachineModuleInfo : public ImmutablePass {
106   /// Context - This is the MCContext used for the entire code generator.
107   MCContext Context;
108
109   /// TheModule - This is the LLVM Module being worked on.
110   const Module *TheModule;
111
112   /// ObjFileMMI - This is the object-file-format-specific implementation of
113   /// MachineModuleInfoImpl, which lets targets accumulate whatever info they
114   /// want.
115   MachineModuleInfoImpl *ObjFileMMI;
116
117   /// List of moves done by a function's prolog.  Used to construct frame maps
118   /// by debug and exception handling consumers.
119   std::vector<MCCFIInstruction> FrameInstructions;
120
121   /// LandingPads - List of LandingPadInfo describing the landing pad
122   /// information in the current function.
123   std::vector<LandingPadInfo> LandingPads;
124
125   /// LPadToCallSiteMap - Map a landing pad's EH symbol to the call site
126   /// indexes.
127   DenseMap<MCSymbol*, SmallVector<unsigned, 4> > LPadToCallSiteMap;
128
129   /// CallSiteMap - Map of invoke call site index values to associated begin
130   /// EH_LABEL for the current function.
131   DenseMap<MCSymbol*, unsigned> CallSiteMap;
132
133   /// CurCallSite - The current call site index being processed, if any. 0 if
134   /// none.
135   unsigned CurCallSite;
136
137   /// TypeInfos - List of C++ TypeInfo used in the current function.
138   std::vector<const GlobalValue *> TypeInfos;
139
140   /// FilterIds - List of typeids encoding filters used in the current function.
141   std::vector<unsigned> FilterIds;
142
143   /// FilterEnds - List of the indices in FilterIds corresponding to filter
144   /// terminators.
145   std::vector<unsigned> FilterEnds;
146
147   /// Personalities - Vector of all personality functions ever seen. Used to
148   /// emit common EH frames.
149   std::vector<const Function *> Personalities;
150
151   /// UsedFunctions - The functions in the @llvm.used list in a more easily
152   /// searchable format.  This does not include the functions in
153   /// llvm.compiler.used.
154   SmallPtrSet<const Function *, 32> UsedFunctions;
155
156   /// AddrLabelSymbols - This map keeps track of which symbol is being used for
157   /// the specified basic block's address of label.
158   MMIAddrLabelMap *AddrLabelSymbols;
159
160   bool CallsEHReturn;
161   bool CallsUnwindInit;
162
163   /// DbgInfoAvailable - True if debugging information is available
164   /// in this module.
165   bool DbgInfoAvailable;
166
167   /// UsesVAFloatArgument - True if this module calls VarArg function with
168   /// floating-point arguments.  This is used to emit an undefined reference
169   /// to _fltused on Windows targets.
170   bool UsesVAFloatArgument;
171
172   /// UsesMorestackAddr - True if the module calls the __morestack function
173   /// indirectly, as is required under the large code model on x86. This is used
174   /// to emit a definition of a symbol, __morestack_addr, containing the
175   /// address. See comments in lib/Target/X86/X86FrameLowering.cpp for more
176   /// details.
177   bool UsesMorestackAddr;
178
179   EHPersonality PersonalityTypeCache;
180
181   EHPersonality getPersonalityTypeSlow();
182
183 public:
184   static char ID; // Pass identification, replacement for typeid
185
186   struct VariableDbgInfo {
187     TrackingMDNodeRef Var;
188     TrackingMDNodeRef Expr;
189     unsigned Slot;
190     DebugLoc Loc;
191
192     VariableDbgInfo(MDNode *Var, MDNode *Expr, unsigned Slot, DebugLoc Loc)
193         : Var(Var), Expr(Expr), Slot(Slot), Loc(Loc) {}
194   };
195   typedef SmallVector<VariableDbgInfo, 4> VariableDbgInfoMapTy;
196   VariableDbgInfoMapTy VariableDbgInfos;
197
198   MachineModuleInfo();  // DUMMY CONSTRUCTOR, DO NOT CALL.
199   // Real constructor.
200   MachineModuleInfo(const MCAsmInfo &MAI, const MCRegisterInfo &MRI,
201                     const MCObjectFileInfo *MOFI);
202   ~MachineModuleInfo();
203
204   // Initialization and Finalization
205   bool doInitialization(Module &) override;
206   bool doFinalization(Module &) override;
207
208   /// EndFunction - Discard function meta information.
209   ///
210   void EndFunction();
211
212   const MCContext &getContext() const { return Context; }
213   MCContext &getContext() { return Context; }
214
215   void setModule(const Module *M) { TheModule = M; }
216   const Module *getModule() const { return TheModule; }
217
218   /// getInfo - Keep track of various per-function pieces of information for
219   /// backends that would like to do so.
220   ///
221   template<typename Ty>
222   Ty &getObjFileInfo() {
223     if (ObjFileMMI == nullptr)
224       ObjFileMMI = new Ty(*this);
225     return *static_cast<Ty*>(ObjFileMMI);
226   }
227
228   template<typename Ty>
229   const Ty &getObjFileInfo() const {
230     return const_cast<MachineModuleInfo*>(this)->getObjFileInfo<Ty>();
231   }
232
233   /// AnalyzeModule - Scan the module for global debug information.
234   ///
235   void AnalyzeModule(const Module &M);
236
237   /// hasDebugInfo - Returns true if valid debug info is present.
238   ///
239   bool hasDebugInfo() const { return DbgInfoAvailable; }
240   void setDebugInfoAvailability(bool avail) { DbgInfoAvailable = avail; }
241
242   bool callsEHReturn() const { return CallsEHReturn; }
243   void setCallsEHReturn(bool b) { CallsEHReturn = b; }
244
245   bool callsUnwindInit() const { return CallsUnwindInit; }
246   void setCallsUnwindInit(bool b) { CallsUnwindInit = b; }
247
248   bool usesVAFloatArgument() const {
249     return UsesVAFloatArgument;
250   }
251
252   void setUsesVAFloatArgument(bool b) {
253     UsesVAFloatArgument = b;
254   }
255
256   bool usesMorestackAddr() const {
257     return UsesMorestackAddr;
258   }
259
260   void setUsesMorestackAddr(bool b) {
261     UsesMorestackAddr = b;
262   }
263
264   /// \brief Returns a reference to a list of cfi instructions in the current
265   /// function's prologue.  Used to construct frame maps for debug and exception
266   /// handling comsumers.
267   const std::vector<MCCFIInstruction> &getFrameInstructions() const {
268     return FrameInstructions;
269   }
270
271   unsigned LLVM_ATTRIBUTE_UNUSED_RESULT
272   addFrameInst(const MCCFIInstruction &Inst) {
273     FrameInstructions.push_back(Inst);
274     return FrameInstructions.size() - 1;
275   }
276
277   /// getAddrLabelSymbol - Return the symbol to be used for the specified basic
278   /// block when its address is taken.  This cannot be its normal LBB label
279   /// because the block may be accessed outside its containing function.
280   MCSymbol *getAddrLabelSymbol(const BasicBlock *BB);
281
282   /// getAddrLabelSymbolToEmit - Return the symbol to be used for the specified
283   /// basic block when its address is taken.  If other blocks were RAUW'd to
284   /// this one, we may have to emit them as well, return the whole set.
285   std::vector<MCSymbol*> getAddrLabelSymbolToEmit(const BasicBlock *BB);
286
287   /// takeDeletedSymbolsForFunction - If the specified function has had any
288   /// references to address-taken blocks generated, but the block got deleted,
289   /// return the symbol now so we can emit it.  This prevents emitting a
290   /// reference to a symbol that has no definition.
291   void takeDeletedSymbolsForFunction(const Function *F,
292                                      std::vector<MCSymbol*> &Result);
293
294
295   //===- EH ---------------------------------------------------------------===//
296
297   /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
298   /// specified MachineBasicBlock.
299   LandingPadInfo &getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad);
300
301   /// addInvoke - Provide the begin and end labels of an invoke style call and
302   /// associate it with a try landing pad block.
303   void addInvoke(MachineBasicBlock *LandingPad,
304                  MCSymbol *BeginLabel, MCSymbol *EndLabel);
305
306   /// addLandingPad - Add a new panding pad.  Returns the label ID for the
307   /// landing pad entry.
308   MCSymbol *addLandingPad(MachineBasicBlock *LandingPad);
309
310   /// addPersonality - Provide the personality function for the exception
311   /// information.
312   void addPersonality(MachineBasicBlock *LandingPad,
313                       const Function *Personality);
314
315   /// getPersonalityIndex - Get index of the current personality function inside
316   /// Personalitites array
317   unsigned getPersonalityIndex() const;
318
319   /// getPersonalities - Return array of personality functions ever seen.
320   const std::vector<const Function *>& getPersonalities() const {
321     return Personalities;
322   }
323
324   /// isUsedFunction - Return true if the functions in the llvm.used list.  This
325   /// does not return true for things in llvm.compiler.used unless they are also
326   /// in llvm.used.
327   bool isUsedFunction(const Function *F) const {
328     return UsedFunctions.count(F);
329   }
330
331   /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
332   ///
333   void addCatchTypeInfo(MachineBasicBlock *LandingPad,
334                         ArrayRef<const GlobalValue *> TyInfo);
335
336   /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
337   ///
338   void addFilterTypeInfo(MachineBasicBlock *LandingPad,
339                          ArrayRef<const GlobalValue *> TyInfo);
340
341   /// addCleanup - Add a cleanup action for a landing pad.
342   ///
343   void addCleanup(MachineBasicBlock *LandingPad);
344
345   /// Add a clause for a landing pad. Returns a new label for the clause. This
346   /// is used by EH schemes that have more than one landing pad. In this case,
347   /// each clause gets its own basic block.
348   MCSymbol *addClauseForLandingPad(MachineBasicBlock *LandingPad);
349
350   /// getTypeIDFor - Return the type id for the specified typeinfo.  This is
351   /// function wide.
352   unsigned getTypeIDFor(const GlobalValue *TI);
353
354   /// getFilterIDFor - Return the id of the filter encoded by TyIds.  This is
355   /// function wide.
356   int getFilterIDFor(std::vector<unsigned> &TyIds);
357
358   /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
359   /// pads.
360   void TidyLandingPads(DenseMap<MCSymbol*, uintptr_t> *LPMap = nullptr);
361
362   /// getLandingPads - Return a reference to the landing pad info for the
363   /// current function.
364   const std::vector<LandingPadInfo> &getLandingPads() const {
365     return LandingPads;
366   }
367
368   /// setCallSiteLandingPad - Map the landing pad's EH symbol to the call
369   /// site indexes.
370   void setCallSiteLandingPad(MCSymbol *Sym, ArrayRef<unsigned> Sites);
371
372   /// getCallSiteLandingPad - Get the call site indexes for a landing pad EH
373   /// symbol.
374   SmallVectorImpl<unsigned> &getCallSiteLandingPad(MCSymbol *Sym) {
375     assert(hasCallSiteLandingPad(Sym) &&
376            "missing call site number for landing pad!");
377     return LPadToCallSiteMap[Sym];
378   }
379
380   /// hasCallSiteLandingPad - Return true if the landing pad Eh symbol has an
381   /// associated call site.
382   bool hasCallSiteLandingPad(MCSymbol *Sym) {
383     return !LPadToCallSiteMap[Sym].empty();
384   }
385
386   /// setCallSiteBeginLabel - Map the begin label for a call site.
387   void setCallSiteBeginLabel(MCSymbol *BeginLabel, unsigned Site) {
388     CallSiteMap[BeginLabel] = Site;
389   }
390
391   /// getCallSiteBeginLabel - Get the call site number for a begin label.
392   unsigned getCallSiteBeginLabel(MCSymbol *BeginLabel) {
393     assert(hasCallSiteBeginLabel(BeginLabel) &&
394            "Missing call site number for EH_LABEL!");
395     return CallSiteMap[BeginLabel];
396   }
397
398   /// hasCallSiteBeginLabel - Return true if the begin label has a call site
399   /// number associated with it.
400   bool hasCallSiteBeginLabel(MCSymbol *BeginLabel) {
401     return CallSiteMap[BeginLabel] != 0;
402   }
403
404   /// setCurrentCallSite - Set the call site currently being processed.
405   void setCurrentCallSite(unsigned Site) { CurCallSite = Site; }
406
407   /// getCurrentCallSite - Get the call site currently being processed, if any.
408   /// return zero if none.
409   unsigned getCurrentCallSite() { return CurCallSite; }
410
411   /// getTypeInfos - Return a reference to the C++ typeinfo for the current
412   /// function.
413   const std::vector<const GlobalValue *> &getTypeInfos() const {
414     return TypeInfos;
415   }
416
417   /// getFilterIds - Return a reference to the typeids encoding filters used in
418   /// the current function.
419   const std::vector<unsigned> &getFilterIds() const {
420     return FilterIds;
421   }
422
423   /// getPersonality - Return a personality function if available.  The presence
424   /// of one is required to emit exception handling info.
425   const Function *getPersonality() const;
426
427   /// Classify the personality function amongst known EH styles.
428   EHPersonality getPersonalityType() {
429     if (PersonalityTypeCache != EHPersonality::None)
430       return PersonalityTypeCache;
431     return getPersonalityTypeSlow();
432   }
433
434   /// setVariableDbgInfo - Collect information used to emit debugging
435   /// information of a variable.
436   void setVariableDbgInfo(MDNode *Var, MDNode *Expr, unsigned Slot,
437                           DebugLoc Loc) {
438     VariableDbgInfos.emplace_back(Var, Expr, Slot, Loc);
439   }
440
441   VariableDbgInfoMapTy &getVariableDbgInfo() { return VariableDbgInfos; }
442
443 }; // End class MachineModuleInfo
444
445 } // End llvm namespace
446
447 #endif