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