1193323Sed//===-- llvm/CodeGen/MachineModuleInfo.h ------------------------*- C++ -*-===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// Collect meta information for a module.  This information should be in a
11193323Sed// neutral form that can be used by different debugging and exception handling
12193323Sed// schemes.
13193323Sed//
14193323Sed// The organization of information is primarily clustered around the source
15193323Sed// compile units.  The main exception is source line correspondence where
16193323Sed// inlining may interleave code from various compile units.
17193323Sed//
18193323Sed// The following information can be retrieved from the MachineModuleInfo.
19193323Sed//
20193323Sed//  -- Source directories - Directories are uniqued based on their canonical
21193323Sed//     string and assigned a sequential numeric ID (base 1.)
22193323Sed//  -- Source files - Files are also uniqued based on their name and directory
23193323Sed//     ID.  A file ID is sequential number (base 1.)
24193323Sed//  -- Source line correspondence - A vector of file ID, line#, column# triples.
25193323Sed//     A DEBUG_LOCATION instruction is generated  by the DAG Legalizer
26193323Sed//     corresponding to each entry in the source line list.  This allows a debug
27193323Sed//     emitter to generate labels referenced by debug information tables.
28193323Sed//
29193323Sed//===----------------------------------------------------------------------===//
30193323Sed
31193323Sed#ifndef LLVM_CODEGEN_MACHINEMODULEINFO_H
32193323Sed#define LLVM_CODEGEN_MACHINEMODULEINFO_H
33193323Sed
34193323Sed#include "llvm/ADT/DenseMap.h"
35205218Srdivacky#include "llvm/ADT/PointerIntPair.h"
36193323Sed#include "llvm/ADT/SmallPtrSet.h"
37205218Srdivacky#include "llvm/ADT/SmallVector.h"
38249423Sdim#include "llvm/IR/Metadata.h"
39249423Sdim#include "llvm/MC/MCContext.h"
40249423Sdim#include "llvm/MC/MachineLocation.h"
41249423Sdim#include "llvm/Pass.h"
42249423Sdim#include "llvm/Support/DataTypes.h"
43249423Sdim#include "llvm/Support/DebugLoc.h"
44249423Sdim#include "llvm/Support/Dwarf.h"
45249423Sdim#include "llvm/Support/ValueHandle.h"
46193323Sed
47193323Sednamespace llvm {
48193323Sed
49193323Sed//===----------------------------------------------------------------------===//
50193323Sed// Forward declarations.
51193323Sedclass Constant;
52205218Srdivackyclass GlobalVariable;
53198090Srdivackyclass MDNode;
54223017Sdimclass MMIAddrLabelMap;
55193323Sedclass MachineBasicBlock;
56193323Sedclass MachineFunction;
57193323Sedclass Module;
58193323Sedclass PointerType;
59193323Sedclass StructType;
60218893Sdim
61193323Sed//===----------------------------------------------------------------------===//
62193323Sed/// LandingPadInfo - This structure is used to retain landing pad info for
63193323Sed/// the current function.
64193323Sed///
65193323Sedstruct LandingPadInfo {
66205218Srdivacky  MachineBasicBlock *LandingPadBlock;    // Landing pad block.
67205218Srdivacky  SmallVector<MCSymbol*, 1> BeginLabels; // Labels prior to invoke.
68205218Srdivacky  SmallVector<MCSymbol*, 1> EndLabels;   // Labels after invoke.
69205218Srdivacky  MCSymbol *LandingPadLabel;             // Label at beginning of landing pad.
70207618Srdivacky  const Function *Personality;           // Personality function.
71205218Srdivacky  std::vector<int> TypeIds;              // List of type ids (filters negative)
72193323Sed
73193323Sed  explicit LandingPadInfo(MachineBasicBlock *MBB)
74205218Srdivacky    : LandingPadBlock(MBB), LandingPadLabel(0), Personality(0) {}
75193323Sed};
76193323Sed
77223017Sdim//===----------------------------------------------------------------------===//
78223017Sdim/// MachineModuleInfoImpl - This class can be derived from and used by targets
79223017Sdim/// to hold private target-specific information for each Module.  Objects of
80223017Sdim/// type are accessed/created with MMI::getInfo and destroyed when the
81223017Sdim/// MachineModuleInfo is destroyed.
82223017Sdim///
83223017Sdimclass MachineModuleInfoImpl {
84223017Sdimpublic:
85223017Sdim  typedef PointerIntPair<MCSymbol*, 1, bool> StubValueTy;
86223017Sdim  virtual ~MachineModuleInfoImpl();
87223017Sdim  typedef std::vector<std::pair<MCSymbol*, StubValueTy> > SymbolListTy;
88223017Sdimprotected:
89223017Sdim  static SymbolListTy GetSortedStubs(const DenseMap<MCSymbol*, StubValueTy>&);
90223017Sdim};
91218893Sdim
92193323Sed//===----------------------------------------------------------------------===//
93193323Sed/// MachineModuleInfo - This class contains meta information specific to a
94218893Sdim/// module.  Queries can be made by different debugging and exception handling
95193323Sed/// schemes and reformated for specific use.
96193323Sed///
97193323Sedclass MachineModuleInfo : public ImmutablePass {
98205218Srdivacky  /// Context - This is the MCContext used for the entire code generator.
99205218Srdivacky  MCContext Context;
100218893Sdim
101206274Srdivacky  /// TheModule - This is the LLVM Module being worked on.
102207618Srdivacky  const Module *TheModule;
103218893Sdim
104198090Srdivacky  /// ObjFileMMI - This is the object-file-format-specific implementation of
105198090Srdivacky  /// MachineModuleInfoImpl, which lets targets accumulate whatever info they
106198090Srdivacky  /// want.
107198090Srdivacky  MachineModuleInfoImpl *ObjFileMMI;
108198090Srdivacky
109263508Sdim  /// List of moves done by a function's prolog.  Used to construct frame maps
110263508Sdim  /// by debug and exception handling consumers.
111263508Sdim  std::vector<MCCFIInstruction> FrameInstructions;
112218893Sdim
113226633Sdim  /// CompactUnwindEncoding - If the target supports it, this is the compact
114226633Sdim  /// unwind encoding. It replaces a function's CIE and FDE.
115226633Sdim  uint32_t CompactUnwindEncoding;
116226633Sdim
117226633Sdim  /// LandingPads - List of LandingPadInfo describing the landing pad
118226633Sdim  /// information in the current function.
119193323Sed  std::vector<LandingPadInfo> LandingPads;
120203954Srdivacky
121226633Sdim  /// LPadToCallSiteMap - Map a landing pad's EH symbol to the call site
122226633Sdim  /// indexes.
123226633Sdim  DenseMap<MCSymbol*, SmallVector<unsigned, 4> > LPadToCallSiteMap;
124226633Sdim
125226633Sdim  /// CallSiteMap - Map of invoke call site index values to associated begin
126226633Sdim  /// EH_LABEL for the current function.
127205218Srdivacky  DenseMap<MCSymbol*, unsigned> CallSiteMap;
128203954Srdivacky
129226633Sdim  /// CurCallSite - The current call site index being processed, if any. 0 if
130226633Sdim  /// none.
131203954Srdivacky  unsigned CurCallSite;
132203954Srdivacky
133226633Sdim  /// TypeInfos - List of C++ TypeInfo used in the current function.
134207618Srdivacky  std::vector<const GlobalVariable *> TypeInfos;
135193323Sed
136226633Sdim  /// FilterIds - List of typeids encoding filters used in the current function.
137193323Sed  std::vector<unsigned> FilterIds;
138193323Sed
139226633Sdim  /// FilterEnds - List of the indices in FilterIds corresponding to filter
140226633Sdim  /// terminators.
141193323Sed  std::vector<unsigned> FilterEnds;
142193323Sed
143226633Sdim  /// Personalities - Vector of all personality functions ever seen. Used to
144226633Sdim  /// emit common EH frames.
145207618Srdivacky  std::vector<const Function *> Personalities;
146193323Sed
147198090Srdivacky  /// UsedFunctions - The functions in the @llvm.used list in a more easily
148198090Srdivacky  /// searchable format.  This does not include the functions in
149198090Srdivacky  /// llvm.compiler.used.
150193323Sed  SmallPtrSet<const Function *, 32> UsedFunctions;
151193323Sed
152205218Srdivacky  /// AddrLabelSymbols - This map keeps track of which symbol is being used for
153205218Srdivacky  /// the specified basic block's address of label.
154205218Srdivacky  MMIAddrLabelMap *AddrLabelSymbols;
155218893Sdim
156193323Sed  bool CallsEHReturn;
157193323Sed  bool CallsUnwindInit;
158218893Sdim
159193323Sed  /// DbgInfoAvailable - True if debugging information is available
160193323Sed  /// in this module.
161193323Sed  bool DbgInfoAvailable;
162198090Srdivacky
163234353Sdim  /// UsesVAFloatArgument - True if this module calls VarArg function with
164234353Sdim  /// floating-point arguments.  This is used to emit an undefined reference
165234353Sdim  /// to _fltused on Windows targets.
166234353Sdim  bool UsesVAFloatArgument;
167218893Sdim
168193323Sedpublic:
169193323Sed  static char ID; // Pass identification, replacement for typeid
170193323Sed
171206124Srdivacky  typedef std::pair<unsigned, DebugLoc> UnsignedDebugLocPair;
172206124Srdivacky  typedef SmallVector<std::pair<TrackingVH<MDNode>, UnsignedDebugLocPair>, 4>
173198396Srdivacky    VariableDbgInfoMapTy;
174198090Srdivacky  VariableDbgInfoMapTy VariableDbgInfo;
175198090Srdivacky
176205218Srdivacky  MachineModuleInfo();  // DUMMY CONSTRUCTOR, DO NOT CALL.
177218893Sdim  // Real constructor.
178226633Sdim  MachineModuleInfo(const MCAsmInfo &MAI, const MCRegisterInfo &MRI,
179226633Sdim                    const MCObjectFileInfo *MOFI);
180193323Sed  ~MachineModuleInfo();
181218893Sdim
182249423Sdim  // Initialization and Finalization
183249423Sdim  virtual bool doInitialization(Module &);
184249423Sdim  virtual bool doFinalization(Module &);
185198090Srdivacky
186193323Sed  /// EndFunction - Discard function meta information.
187193323Sed  ///
188193323Sed  void EndFunction();
189218893Sdim
190205218Srdivacky  const MCContext &getContext() const { return Context; }
191205218Srdivacky  MCContext &getContext() { return Context; }
192193323Sed
193207618Srdivacky  void setModule(const Module *M) { TheModule = M; }
194207618Srdivacky  const Module *getModule() const { return TheModule; }
195218893Sdim
196198090Srdivacky  /// getInfo - Keep track of various per-function pieces of information for
197198090Srdivacky  /// backends that would like to do so.
198198090Srdivacky  ///
199198090Srdivacky  template<typename Ty>
200198090Srdivacky  Ty &getObjFileInfo() {
201198090Srdivacky    if (ObjFileMMI == 0)
202198090Srdivacky      ObjFileMMI = new Ty(*this);
203198090Srdivacky    return *static_cast<Ty*>(ObjFileMMI);
204198090Srdivacky  }
205218893Sdim
206198090Srdivacky  template<typename Ty>
207198090Srdivacky  const Ty &getObjFileInfo() const {
208198090Srdivacky    return const_cast<MachineModuleInfo*>(this)->getObjFileInfo<Ty>();
209198090Srdivacky  }
210218893Sdim
211193323Sed  /// AnalyzeModule - Scan the module for global debug information.
212193323Sed  ///
213207618Srdivacky  void AnalyzeModule(const Module &M);
214218893Sdim
215193323Sed  /// hasDebugInfo - Returns true if valid debug info is present.
216193323Sed  ///
217193323Sed  bool hasDebugInfo() const { return DbgInfoAvailable; }
218218893Sdim  void setDebugInfoAvailability(bool avail) { DbgInfoAvailable = avail; }
219193323Sed
220193323Sed  bool callsEHReturn() const { return CallsEHReturn; }
221193323Sed  void setCallsEHReturn(bool b) { CallsEHReturn = b; }
222193323Sed
223193323Sed  bool callsUnwindInit() const { return CallsUnwindInit; }
224193323Sed  void setCallsUnwindInit(bool b) { CallsUnwindInit = b; }
225218893Sdim
226234353Sdim  bool usesVAFloatArgument() const {
227234353Sdim    return UsesVAFloatArgument;
228218893Sdim  }
229218893Sdim
230234353Sdim  void setUsesVAFloatArgument(bool b) {
231234353Sdim    UsesVAFloatArgument = b;
232218893Sdim  }
233218893Sdim
234263508Sdim  /// \brief Returns a reference to a list of cfi instructions in the current
235193323Sed  /// function's prologue.  Used to construct frame maps for debug and exception
236193323Sed  /// handling comsumers.
237263508Sdim  const std::vector<MCCFIInstruction> &getFrameInstructions() const {
238263508Sdim    return FrameInstructions;
239263508Sdim  }
240218893Sdim
241263508Sdim  void addFrameInst(const MCCFIInstruction &Inst) {
242263508Sdim    FrameInstructions.push_back(Inst);
243263508Sdim  }
244263508Sdim
245226633Sdim  /// getCompactUnwindEncoding - Returns the compact unwind encoding for a
246226633Sdim  /// function if the target supports the encoding. This encoding replaces a
247226633Sdim  /// function's CIE and FDE.
248226633Sdim  uint32_t getCompactUnwindEncoding() const { return CompactUnwindEncoding; }
249226633Sdim
250226633Sdim  /// setCompactUnwindEncoding - Set the compact unwind encoding for a function
251226633Sdim  /// if the target supports the encoding.
252226633Sdim  void setCompactUnwindEncoding(uint32_t Enc) { CompactUnwindEncoding = Enc; }
253226633Sdim
254205218Srdivacky  /// getAddrLabelSymbol - Return the symbol to be used for the specified basic
255205218Srdivacky  /// block when its address is taken.  This cannot be its normal LBB label
256205218Srdivacky  /// because the block may be accessed outside its containing function.
257205218Srdivacky  MCSymbol *getAddrLabelSymbol(const BasicBlock *BB);
258193323Sed
259205218Srdivacky  /// getAddrLabelSymbolToEmit - Return the symbol to be used for the specified
260205218Srdivacky  /// basic block when its address is taken.  If other blocks were RAUW'd to
261205218Srdivacky  /// this one, we may have to emit them as well, return the whole set.
262205218Srdivacky  std::vector<MCSymbol*> getAddrLabelSymbolToEmit(const BasicBlock *BB);
263218893Sdim
264205218Srdivacky  /// takeDeletedSymbolsForFunction - If the specified function has had any
265205218Srdivacky  /// references to address-taken blocks generated, but the block got deleted,
266205218Srdivacky  /// return the symbol now so we can emit it.  This prevents emitting a
267205218Srdivacky  /// reference to a symbol that has no definition.
268218893Sdim  void takeDeletedSymbolsForFunction(const Function *F,
269205218Srdivacky                                     std::vector<MCSymbol*> &Result);
270205218Srdivacky
271218893Sdim
272205218Srdivacky  //===- EH ---------------------------------------------------------------===//
273205218Srdivacky
274193323Sed  /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
275193323Sed  /// specified MachineBasicBlock.
276193323Sed  LandingPadInfo &getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad);
277193323Sed
278193323Sed  /// addInvoke - Provide the begin and end labels of an invoke style call and
279193323Sed  /// associate it with a try landing pad block.
280205218Srdivacky  void addInvoke(MachineBasicBlock *LandingPad,
281205218Srdivacky                 MCSymbol *BeginLabel, MCSymbol *EndLabel);
282218893Sdim
283218893Sdim  /// addLandingPad - Add a new panding pad.  Returns the label ID for the
284193323Sed  /// landing pad entry.
285205218Srdivacky  MCSymbol *addLandingPad(MachineBasicBlock *LandingPad);
286218893Sdim
287193323Sed  /// addPersonality - Provide the personality function for the exception
288193323Sed  /// information.
289207618Srdivacky  void addPersonality(MachineBasicBlock *LandingPad,
290207618Srdivacky                      const Function *Personality);
291193323Sed
292193323Sed  /// getPersonalityIndex - Get index of the current personality function inside
293193323Sed  /// Personalitites array
294193323Sed  unsigned getPersonalityIndex() const;
295193323Sed
296193323Sed  /// getPersonalities - Return array of personality functions ever seen.
297207618Srdivacky  const std::vector<const Function *>& getPersonalities() const {
298193323Sed    return Personalities;
299193323Sed  }
300193323Sed
301198090Srdivacky  /// isUsedFunction - Return true if the functions in the llvm.used list.  This
302198090Srdivacky  /// does not return true for things in llvm.compiler.used unless they are also
303198090Srdivacky  /// in llvm.used.
304249423Sdim  bool isUsedFunction(const Function *F) const {
305198090Srdivacky    return UsedFunctions.count(F);
306193323Sed  }
307193323Sed
308193323Sed  /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
309193323Sed  ///
310193323Sed  void addCatchTypeInfo(MachineBasicBlock *LandingPad,
311226633Sdim                        ArrayRef<const GlobalVariable *> TyInfo);
312193323Sed
313193323Sed  /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
314193323Sed  ///
315193323Sed  void addFilterTypeInfo(MachineBasicBlock *LandingPad,
316226633Sdim                         ArrayRef<const GlobalVariable *> TyInfo);
317193323Sed
318193323Sed  /// addCleanup - Add a cleanup action for a landing pad.
319193323Sed  ///
320193323Sed  void addCleanup(MachineBasicBlock *LandingPad);
321193323Sed
322218893Sdim  /// getTypeIDFor - Return the type id for the specified typeinfo.  This is
323193323Sed  /// function wide.
324207618Srdivacky  unsigned getTypeIDFor(const GlobalVariable *TI);
325193323Sed
326193323Sed  /// getFilterIDFor - Return the id of the filter encoded by TyIds.  This is
327193323Sed  /// function wide.
328193323Sed  int getFilterIDFor(std::vector<unsigned> &TyIds);
329193323Sed
330193323Sed  /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
331193323Sed  /// pads.
332207618Srdivacky  void TidyLandingPads(DenseMap<MCSymbol*, uintptr_t> *LPMap = 0);
333218893Sdim
334193323Sed  /// getLandingPads - Return a reference to the landing pad info for the
335193323Sed  /// current function.
336193323Sed  const std::vector<LandingPadInfo> &getLandingPads() const {
337193323Sed    return LandingPads;
338193323Sed  }
339203954Srdivacky
340226633Sdim  /// setCallSiteLandingPad - Map the landing pad's EH symbol to the call
341226633Sdim  /// site indexes.
342226633Sdim  void setCallSiteLandingPad(MCSymbol *Sym, ArrayRef<unsigned> Sites);
343226633Sdim
344226633Sdim  /// getCallSiteLandingPad - Get the call site indexes for a landing pad EH
345226633Sdim  /// symbol.
346226633Sdim  SmallVectorImpl<unsigned> &getCallSiteLandingPad(MCSymbol *Sym) {
347226633Sdim    assert(hasCallSiteLandingPad(Sym) &&
348226633Sdim           "missing call site number for landing pad!");
349226633Sdim    return LPadToCallSiteMap[Sym];
350226633Sdim  }
351226633Sdim
352226633Sdim  /// hasCallSiteLandingPad - Return true if the landing pad Eh symbol has an
353226633Sdim  /// associated call site.
354226633Sdim  bool hasCallSiteLandingPad(MCSymbol *Sym) {
355226633Sdim    return !LPadToCallSiteMap[Sym].empty();
356226633Sdim  }
357226633Sdim
358226633Sdim  /// setCallSiteBeginLabel - Map the begin label for a call site.
359205218Srdivacky  void setCallSiteBeginLabel(MCSymbol *BeginLabel, unsigned Site) {
360203954Srdivacky    CallSiteMap[BeginLabel] = Site;
361203954Srdivacky  }
362203954Srdivacky
363226633Sdim  /// getCallSiteBeginLabel - Get the call site number for a begin label.
364205218Srdivacky  unsigned getCallSiteBeginLabel(MCSymbol *BeginLabel) {
365226633Sdim    assert(hasCallSiteBeginLabel(BeginLabel) &&
366203954Srdivacky           "Missing call site number for EH_LABEL!");
367203954Srdivacky    return CallSiteMap[BeginLabel];
368203954Srdivacky  }
369203954Srdivacky
370226633Sdim  /// hasCallSiteBeginLabel - Return true if the begin label has a call site
371226633Sdim  /// number associated with it.
372226633Sdim  bool hasCallSiteBeginLabel(MCSymbol *BeginLabel) {
373226633Sdim    return CallSiteMap[BeginLabel] != 0;
374226633Sdim  }
375226633Sdim
376203954Srdivacky  /// setCurrentCallSite - Set the call site currently being processed.
377203954Srdivacky  void setCurrentCallSite(unsigned Site) { CurCallSite = Site; }
378203954Srdivacky
379203954Srdivacky  /// getCurrentCallSite - Get the call site currently being processed, if any.
380203954Srdivacky  /// return zero if none.
381249423Sdim  unsigned getCurrentCallSite() { return CurCallSite; }
382203954Srdivacky
383193323Sed  /// getTypeInfos - Return a reference to the C++ typeinfo for the current
384193323Sed  /// function.
385207618Srdivacky  const std::vector<const GlobalVariable *> &getTypeInfos() const {
386193323Sed    return TypeInfos;
387193323Sed  }
388193323Sed
389193323Sed  /// getFilterIds - Return a reference to the typeids encoding filters used in
390193323Sed  /// the current function.
391193323Sed  const std::vector<unsigned> &getFilterIds() const {
392193323Sed    return FilterIds;
393193323Sed  }
394193323Sed
395193323Sed  /// getPersonality - Return a personality function if available.  The presence
396193323Sed  /// of one is required to emit exception handling info.
397207618Srdivacky  const Function *getPersonality() const;
398193323Sed
399206124Srdivacky  /// setVariableDbgInfo - Collect information used to emit debugging
400206124Srdivacky  /// information of a variable.
401206124Srdivacky  void setVariableDbgInfo(MDNode *N, unsigned Slot, DebugLoc Loc) {
402206124Srdivacky    VariableDbgInfo.push_back(std::make_pair(N, std::make_pair(Slot, Loc)));
403198090Srdivacky  }
404198090Srdivacky
405212904Sdim  VariableDbgInfoMapTy &getVariableDbgInfo() { return VariableDbgInfo; }
406198090Srdivacky
407193323Sed}; // End class MachineModuleInfo
408193323Sed
409193323Sed} // End llvm namespace
410193323Sed
411193323Sed#endif
412