MachineModuleInfo.h revision 263508
11590Srgrimes//===-- llvm/CodeGen/MachineModuleInfo.h ------------------------*- C++ -*-===//
21590Srgrimes//
31590Srgrimes//                     The LLVM Compiler Infrastructure
41590Srgrimes//
51590Srgrimes// This file is distributed under the University of Illinois Open Source
61590Srgrimes// License. See LICENSE.TXT for details.
71590Srgrimes//
81590Srgrimes//===----------------------------------------------------------------------===//
91590Srgrimes//
101590Srgrimes// Collect meta information for a module.  This information should be in a
111590Srgrimes// neutral form that can be used by different debugging and exception handling
121590Srgrimes// schemes.
131590Srgrimes//
141590Srgrimes// The organization of information is primarily clustered around the source
151590Srgrimes// compile units.  The main exception is source line correspondence where
161590Srgrimes// inlining may interleave code from various compile units.
171590Srgrimes//
181590Srgrimes// The following information can be retrieved from the MachineModuleInfo.
191590Srgrimes//
201590Srgrimes//  -- Source directories - Directories are uniqued based on their canonical
211590Srgrimes//     string and assigned a sequential numeric ID (base 1.)
221590Srgrimes//  -- Source files - Files are also uniqued based on their name and directory
231590Srgrimes//     ID.  A file ID is sequential number (base 1.)
241590Srgrimes//  -- Source line correspondence - A vector of file ID, line#, column# triples.
251590Srgrimes//     A DEBUG_LOCATION instruction is generated  by the DAG Legalizer
261590Srgrimes//     corresponding to each entry in the source line list.  This allows a debug
271590Srgrimes//     emitter to generate labels referenced by debug information tables.
281590Srgrimes//
291590Srgrimes//===----------------------------------------------------------------------===//
301590Srgrimes
3127648Scharnier#ifndef LLVM_CODEGEN_MACHINEMODULEINFO_H
321590Srgrimes#define LLVM_CODEGEN_MACHINEMODULEINFO_H
331590Srgrimes
341590Srgrimes#include "llvm/ADT/DenseMap.h"
351590Srgrimes#include "llvm/ADT/PointerIntPair.h"
361590Srgrimes#include "llvm/ADT/SmallPtrSet.h"
3727648Scharnier#include "llvm/ADT/SmallVector.h"
381590Srgrimes#include "llvm/IR/Metadata.h"
3927648Scharnier#include "llvm/MC/MCContext.h"
401590Srgrimes#include "llvm/MC/MachineLocation.h"
411590Srgrimes#include "llvm/Pass.h"
4294503Scharnier#include "llvm/Support/DataTypes.h"
4394503Scharnier#include "llvm/Support/DebugLoc.h"
4494503Scharnier#include "llvm/Support/Dwarf.h"
4527648Scharnier#include "llvm/Support/ValueHandle.h"
46106293Stjr
471590Srgrimesnamespace llvm {
4827648Scharnier
4933648Sjb//===----------------------------------------------------------------------===//
501590Srgrimes// Forward declarations.
511590Srgrimesclass Constant;
521590Srgrimesclass GlobalVariable;
531590Srgrimesclass MDNode;
541590Srgrimesclass MMIAddrLabelMap;
551590Srgrimesclass MachineBasicBlock;
561590Srgrimesclass MachineFunction;
571590Srgrimesclass Module;
581590Srgrimesclass PointerType;
591590Srgrimesclass StructType;
601590Srgrimes
611590Srgrimes//===----------------------------------------------------------------------===//
621590Srgrimes/// LandingPadInfo - This structure is used to retain landing pad info for
631590Srgrimes/// the current function.
641590Srgrimes///
651590Srgrimesstruct LandingPadInfo {
661590Srgrimes  MachineBasicBlock *LandingPadBlock;    // Landing pad block.
671590Srgrimes  SmallVector<MCSymbol*, 1> BeginLabels; // Labels prior to invoke.
681590Srgrimes  SmallVector<MCSymbol*, 1> EndLabels;   // Labels after invoke.
691590Srgrimes  MCSymbol *LandingPadLabel;             // Label at beginning of landing pad.
701590Srgrimes  const Function *Personality;           // Personality function.
711590Srgrimes  std::vector<int> TypeIds;              // List of type ids (filters negative)
721590Srgrimes
731590Srgrimes  explicit LandingPadInfo(MachineBasicBlock *MBB)
741590Srgrimes    : LandingPadBlock(MBB), LandingPadLabel(0), Personality(0) {}
751590Srgrimes};
761590Srgrimes
771590Srgrimes//===----------------------------------------------------------------------===//
781590Srgrimes/// MachineModuleInfoImpl - This class can be derived from and used by targets
791590Srgrimes/// to hold private target-specific information for each Module.  Objects of
801590Srgrimes/// type are accessed/created with MMI::getInfo and destroyed when the
811590Srgrimes/// MachineModuleInfo is destroyed.
8292921Simp///
8392921Simpclass MachineModuleInfoImpl {
8495642Smarkmpublic:
8592921Simp  typedef PointerIntPair<MCSymbol*, 1, bool> StubValueTy;
8692921Simp  virtual ~MachineModuleInfoImpl();
8792921Simp  typedef std::vector<std::pair<MCSymbol*, StubValueTy> > SymbolListTy;
8892921Simpprotected:
8995642Smarkm  static SymbolListTy GetSortedStubs(const DenseMap<MCSymbol*, StubValueTy>&);
9027648Scharnier};
9127648Scharnier
9295642Smarkm//===----------------------------------------------------------------------===//
931590Srgrimes/// MachineModuleInfo - This class contains meta information specific to a
941590Srgrimes/// module.  Queries can be made by different debugging and exception handling
95106293Stjr/// schemes and reformated for specific use.
961590Srgrimes///
9727648Scharnierclass MachineModuleInfo : public ImmutablePass {
981590Srgrimes  /// Context - This is the MCContext used for the entire code generator.
991590Srgrimes  MCContext Context;
1001590Srgrimes
10127648Scharnier  /// TheModule - This is the LLVM Module being worked on.
1021590Srgrimes  const Module *TheModule;
1031590Srgrimes
10427648Scharnier  /// ObjFileMMI - This is the object-file-format-specific implementation of
1051590Srgrimes  /// MachineModuleInfoImpl, which lets targets accumulate whatever info they
1061590Srgrimes  /// want.
10727648Scharnier  MachineModuleInfoImpl *ObjFileMMI;
1081590Srgrimes
1091590Srgrimes  /// List of moves done by a function's prolog.  Used to construct frame maps
110106293Stjr  /// by debug and exception handling consumers.
111106293Stjr  std::vector<MCCFIInstruction> FrameInstructions;
112106293Stjr
113106293Stjr  /// CompactUnwindEncoding - If the target supports it, this is the compact
114106293Stjr  /// unwind encoding. It replaces a function's CIE and FDE.
115106293Stjr  uint32_t CompactUnwindEncoding;
1161590Srgrimes
1171590Srgrimes  /// LandingPads - List of LandingPadInfo describing the landing pad
118106293Stjr  /// information in the current function.
119106293Stjr  std::vector<LandingPadInfo> LandingPads;
120106293Stjr
121106293Stjr  /// LPadToCallSiteMap - Map a landing pad's EH symbol to the call site
122106293Stjr  /// indexes.
1231590Srgrimes  DenseMap<MCSymbol*, SmallVector<unsigned, 4> > LPadToCallSiteMap;
12427648Scharnier
1251590Srgrimes  /// CallSiteMap - Map of invoke call site index values to associated begin
12627648Scharnier  /// EH_LABEL for the current function.
1271590Srgrimes  DenseMap<MCSymbol*, unsigned> CallSiteMap;
1281590Srgrimes
1291590Srgrimes  /// CurCallSite - The current call site index being processed, if any. 0 if
1301590Srgrimes  /// none.
1311590Srgrimes  unsigned CurCallSite;
1321590Srgrimes
13395642Smarkm  /// TypeInfos - List of C++ TypeInfo used in the current function.
13495642Smarkm  std::vector<const GlobalVariable *> TypeInfos;
13527648Scharnier
13694503Scharnier  /// FilterIds - List of typeids encoding filters used in the current function.
13727648Scharnier  std::vector<unsigned> FilterIds;
13827648Scharnier
13927648Scharnier  /// FilterEnds - List of the indices in FilterIds corresponding to filter
14027648Scharnier  /// terminators.
14195642Smarkm  std::vector<unsigned> FilterEnds;
1421590Srgrimes
14387289Sdwmalone  /// Personalities - Vector of all personality functions ever seen. Used to
1441590Srgrimes  /// emit common EH frames.
1451590Srgrimes  std::vector<const Function *> Personalities;
1461590Srgrimes
1471590Srgrimes  /// UsedFunctions - The functions in the @llvm.used list in a more easily
1481590Srgrimes  /// searchable format.  This does not include the functions in
1491590Srgrimes  /// llvm.compiler.used.
1501590Srgrimes  SmallPtrSet<const Function *, 32> UsedFunctions;
1511590Srgrimes
1521590Srgrimes  /// AddrLabelSymbols - This map keeps track of which symbol is being used for
1531590Srgrimes  /// the specified basic block's address of label.
1541590Srgrimes  MMIAddrLabelMap *AddrLabelSymbols;
1551590Srgrimes
1561590Srgrimes  bool CallsEHReturn;
1571590Srgrimes  bool CallsUnwindInit;
1581590Srgrimes
1591590Srgrimes  /// DbgInfoAvailable - True if debugging information is available
1601590Srgrimes  /// in this module.
1611590Srgrimes  bool DbgInfoAvailable;
1621590Srgrimes
1631590Srgrimes  /// UsesVAFloatArgument - True if this module calls VarArg function with
16427648Scharnier  /// floating-point arguments.  This is used to emit an undefined reference
16595642Smarkm  /// to _fltused on Windows targets.
1661590Srgrimes  bool UsesVAFloatArgument;
16787289Sdwmalone
16887289Sdwmalonepublic:
1691590Srgrimes  static char ID; // Pass identification, replacement for typeid
1701590Srgrimes
1711590Srgrimes  typedef std::pair<unsigned, DebugLoc> UnsignedDebugLocPair;
1721590Srgrimes  typedef SmallVector<std::pair<TrackingVH<MDNode>, UnsignedDebugLocPair>, 4>
1731590Srgrimes    VariableDbgInfoMapTy;
1741590Srgrimes  VariableDbgInfoMapTy VariableDbgInfo;
1751590Srgrimes
1761590Srgrimes  MachineModuleInfo();  // DUMMY CONSTRUCTOR, DO NOT CALL.
1771590Srgrimes  // Real constructor.
1781590Srgrimes  MachineModuleInfo(const MCAsmInfo &MAI, const MCRegisterInfo &MRI,
1791590Srgrimes                    const MCObjectFileInfo *MOFI);
1801590Srgrimes  ~MachineModuleInfo();
1811590Srgrimes
18227648Scharnier  // Initialization and Finalization
18395642Smarkm  virtual bool doInitialization(Module &);
1841590Srgrimes  virtual bool doFinalization(Module &);
18587289Sdwmalone
1861590Srgrimes  /// EndFunction - Discard function meta information.
18795642Smarkm  ///
1881590Srgrimes  void EndFunction();
1891590Srgrimes
190106294Stjr  const MCContext &getContext() const { return Context; }
191106294Stjr  MCContext &getContext() { return Context; }
1921590Srgrimes
1931590Srgrimes  void setModule(const Module *M) { TheModule = M; }
1941590Srgrimes  const Module *getModule() const { return TheModule; }
1951590Srgrimes
1961590Srgrimes  /// getInfo - Keep track of various per-function pieces of information for
1971590Srgrimes  /// backends that would like to do so.
1981590Srgrimes  ///
1991590Srgrimes  template<typename Ty>
2001590Srgrimes  Ty &getObjFileInfo() {
2011590Srgrimes    if (ObjFileMMI == 0)
2021590Srgrimes      ObjFileMMI = new Ty(*this);
2031590Srgrimes    return *static_cast<Ty*>(ObjFileMMI);
2041590Srgrimes  }
2051590Srgrimes
2061590Srgrimes  template<typename Ty>
2071590Srgrimes  const Ty &getObjFileInfo() const {
2081590Srgrimes    return const_cast<MachineModuleInfo*>(this)->getObjFileInfo<Ty>();
2091590Srgrimes  }
2101590Srgrimes
2111590Srgrimes  /// AnalyzeModule - Scan the module for global debug information.
2121590Srgrimes  ///
2131590Srgrimes  void AnalyzeModule(const Module &M);
2141590Srgrimes
2151590Srgrimes  /// hasDebugInfo - Returns true if valid debug info is present.
2161590Srgrimes  ///
2171590Srgrimes  bool hasDebugInfo() const { return DbgInfoAvailable; }
2181590Srgrimes  void setDebugInfoAvailability(bool avail) { DbgInfoAvailable = avail; }
2191590Srgrimes
2201590Srgrimes  bool callsEHReturn() const { return CallsEHReturn; }
2211590Srgrimes  void setCallsEHReturn(bool b) { CallsEHReturn = b; }
2221590Srgrimes
2231590Srgrimes  bool callsUnwindInit() const { return CallsUnwindInit; }
2241590Srgrimes  void setCallsUnwindInit(bool b) { CallsUnwindInit = b; }
2251590Srgrimes
2261590Srgrimes  bool usesVAFloatArgument() const {
2271590Srgrimes    return UsesVAFloatArgument;
2281590Srgrimes  }
2291590Srgrimes
2301590Srgrimes  void setUsesVAFloatArgument(bool b) {
2311590Srgrimes    UsesVAFloatArgument = b;
2321590Srgrimes  }
2331590Srgrimes
2341590Srgrimes  /// \brief Returns a reference to a list of cfi instructions in the current
2351590Srgrimes  /// function's prologue.  Used to construct frame maps for debug and exception
2361590Srgrimes  /// handling comsumers.
2371590Srgrimes  const std::vector<MCCFIInstruction> &getFrameInstructions() const {
2381590Srgrimes    return FrameInstructions;
2391590Srgrimes  }
2401590Srgrimes
2411590Srgrimes  void addFrameInst(const MCCFIInstruction &Inst) {
2421590Srgrimes    FrameInstructions.push_back(Inst);
2431590Srgrimes  }
2441590Srgrimes
245126960Sbde  /// getCompactUnwindEncoding - Returns the compact unwind encoding for a
2461590Srgrimes  /// function if the target supports the encoding. This encoding replaces a
2471590Srgrimes  /// function's CIE and FDE.
24827648Scharnier  uint32_t getCompactUnwindEncoding() const { return CompactUnwindEncoding; }
24995642Smarkm
2501590Srgrimes  /// setCompactUnwindEncoding - Set the compact unwind encoding for a function
2511590Srgrimes  /// if the target supports the encoding.
2521590Srgrimes  void setCompactUnwindEncoding(uint32_t Enc) { CompactUnwindEncoding = Enc; }
2531590Srgrimes
2541590Srgrimes  /// getAddrLabelSymbol - Return the symbol to be used for the specified basic
25527648Scharnier  /// block when its address is taken.  This cannot be its normal LBB label
25695642Smarkm  /// because the block may be accessed outside its containing function.
2571590Srgrimes  MCSymbol *getAddrLabelSymbol(const BasicBlock *BB);
2581590Srgrimes
2591590Srgrimes  /// getAddrLabelSymbolToEmit - Return the symbol to be used for the specified
2601590Srgrimes  /// basic block when its address is taken.  If other blocks were RAUW'd to
2611590Srgrimes  /// this one, we may have to emit them as well, return the whole set.
26227648Scharnier  std::vector<MCSymbol*> getAddrLabelSymbolToEmit(const BasicBlock *BB);
2631590Srgrimes
2641590Srgrimes  /// takeDeletedSymbolsForFunction - If the specified function has had any
2651590Srgrimes  /// references to address-taken blocks generated, but the block got deleted,
2661590Srgrimes  /// return the symbol now so we can emit it.  This prevents emitting a
2671590Srgrimes  /// reference to a symbol that has no definition.
2681590Srgrimes  void takeDeletedSymbolsForFunction(const Function *F,
2691590Srgrimes                                     std::vector<MCSymbol*> &Result);
2701590Srgrimes
2711590Srgrimes
2721590Srgrimes  //===- EH ---------------------------------------------------------------===//
2731590Srgrimes
2741590Srgrimes  /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
2751590Srgrimes  /// specified MachineBasicBlock.
27627648Scharnier  LandingPadInfo &getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad);
27795642Smarkm
2781590Srgrimes  /// addInvoke - Provide the begin and end labels of an invoke style call and
2791590Srgrimes  /// associate it with a try landing pad block.
28095642Smarkm  void addInvoke(MachineBasicBlock *LandingPad,
2811590Srgrimes                 MCSymbol *BeginLabel, MCSymbol *EndLabel);
2821590Srgrimes
28395642Smarkm  /// addLandingPad - Add a new panding pad.  Returns the label ID for the
2841590Srgrimes  /// landing pad entry.
2851590Srgrimes  MCSymbol *addLandingPad(MachineBasicBlock *LandingPad);
2861590Srgrimes
2871590Srgrimes  /// addPersonality - Provide the personality function for the exception
2881590Srgrimes  /// information.
2891590Srgrimes  void addPersonality(MachineBasicBlock *LandingPad,
2901590Srgrimes                      const Function *Personality);
2911590Srgrimes
2921590Srgrimes  /// getPersonalityIndex - Get index of the current personality function inside
2931590Srgrimes  /// Personalitites array
2941590Srgrimes  unsigned getPersonalityIndex() const;
2951590Srgrimes
2961590Srgrimes  /// getPersonalities - Return array of personality functions ever seen.
2971590Srgrimes  const std::vector<const Function *>& getPersonalities() const {
2981590Srgrimes    return Personalities;
2991590Srgrimes  }
3001590Srgrimes
3011590Srgrimes  /// isUsedFunction - Return true if the functions in the llvm.used list.  This
3021590Srgrimes  /// does not return true for things in llvm.compiler.used unless they are also
3031590Srgrimes  /// in llvm.used.
3041590Srgrimes  bool isUsedFunction(const Function *F) const {
30594503Scharnier    return UsedFunctions.count(F);
30694503Scharnier  }
3071590Srgrimes
3081590Srgrimes  /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
3091590Srgrimes  ///
3101590Srgrimes  void addCatchTypeInfo(MachineBasicBlock *LandingPad,
3111590Srgrimes                        ArrayRef<const GlobalVariable *> TyInfo);
3121590Srgrimes
3131590Srgrimes  /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
3141590Srgrimes  ///
3151590Srgrimes  void addFilterTypeInfo(MachineBasicBlock *LandingPad,
3161590Srgrimes                         ArrayRef<const GlobalVariable *> TyInfo);
3171590Srgrimes
3181590Srgrimes  /// addCleanup - Add a cleanup action for a landing pad.
3191590Srgrimes  ///
3201590Srgrimes  void addCleanup(MachineBasicBlock *LandingPad);
3211590Srgrimes
32227648Scharnier  /// getTypeIDFor - Return the type id for the specified typeinfo.  This is
32395642Smarkm  /// function wide.
3241590Srgrimes  unsigned getTypeIDFor(const GlobalVariable *TI);
32587289Sdwmalone
32695642Smarkm  /// getFilterIDFor - Return the id of the filter encoded by TyIds.  This is
3271590Srgrimes  /// function wide.
3281590Srgrimes  int getFilterIDFor(std::vector<unsigned> &TyIds);
3291590Srgrimes
3301590Srgrimes  /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
3311590Srgrimes  /// pads.
33227648Scharnier  void TidyLandingPads(DenseMap<MCSymbol*, uintptr_t> *LPMap = 0);
3331590Srgrimes
334  /// getLandingPads - Return a reference to the landing pad info for the
335  /// current function.
336  const std::vector<LandingPadInfo> &getLandingPads() const {
337    return LandingPads;
338  }
339
340  /// setCallSiteLandingPad - Map the landing pad's EH symbol to the call
341  /// site indexes.
342  void setCallSiteLandingPad(MCSymbol *Sym, ArrayRef<unsigned> Sites);
343
344  /// getCallSiteLandingPad - Get the call site indexes for a landing pad EH
345  /// symbol.
346  SmallVectorImpl<unsigned> &getCallSiteLandingPad(MCSymbol *Sym) {
347    assert(hasCallSiteLandingPad(Sym) &&
348           "missing call site number for landing pad!");
349    return LPadToCallSiteMap[Sym];
350  }
351
352  /// hasCallSiteLandingPad - Return true if the landing pad Eh symbol has an
353  /// associated call site.
354  bool hasCallSiteLandingPad(MCSymbol *Sym) {
355    return !LPadToCallSiteMap[Sym].empty();
356  }
357
358  /// setCallSiteBeginLabel - Map the begin label for a call site.
359  void setCallSiteBeginLabel(MCSymbol *BeginLabel, unsigned Site) {
360    CallSiteMap[BeginLabel] = Site;
361  }
362
363  /// getCallSiteBeginLabel - Get the call site number for a begin label.
364  unsigned getCallSiteBeginLabel(MCSymbol *BeginLabel) {
365    assert(hasCallSiteBeginLabel(BeginLabel) &&
366           "Missing call site number for EH_LABEL!");
367    return CallSiteMap[BeginLabel];
368  }
369
370  /// hasCallSiteBeginLabel - Return true if the begin label has a call site
371  /// number associated with it.
372  bool hasCallSiteBeginLabel(MCSymbol *BeginLabel) {
373    return CallSiteMap[BeginLabel] != 0;
374  }
375
376  /// setCurrentCallSite - Set the call site currently being processed.
377  void setCurrentCallSite(unsigned Site) { CurCallSite = Site; }
378
379  /// getCurrentCallSite - Get the call site currently being processed, if any.
380  /// return zero if none.
381  unsigned getCurrentCallSite() { return CurCallSite; }
382
383  /// getTypeInfos - Return a reference to the C++ typeinfo for the current
384  /// function.
385  const std::vector<const GlobalVariable *> &getTypeInfos() const {
386    return TypeInfos;
387  }
388
389  /// getFilterIds - Return a reference to the typeids encoding filters used in
390  /// the current function.
391  const std::vector<unsigned> &getFilterIds() const {
392    return FilterIds;
393  }
394
395  /// getPersonality - Return a personality function if available.  The presence
396  /// of one is required to emit exception handling info.
397  const Function *getPersonality() const;
398
399  /// setVariableDbgInfo - Collect information used to emit debugging
400  /// information of a variable.
401  void setVariableDbgInfo(MDNode *N, unsigned Slot, DebugLoc Loc) {
402    VariableDbgInfo.push_back(std::make_pair(N, std::make_pair(Slot, Loc)));
403  }
404
405  VariableDbgInfoMapTy &getVariableDbgInfo() { return VariableDbgInfo; }
406
407}; // End class MachineModuleInfo
408
409} // End llvm namespace
410
411#endif
412