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/LibCallSemantics.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 MDNode;
     55 class MMIAddrLabelMap;
     56 class MachineBasicBlock;
     57 class MachineFunction;
     58 class Module;
     59 class PointerType;
     60 class StructType;
     61 struct WinEHFuncInfo;
     62 
     63 //===----------------------------------------------------------------------===//
     64 /// LandingPadInfo - This structure is used to retain landing pad info for
     65 /// the current function.
     66 ///
     67 struct LandingPadInfo {
     68   MachineBasicBlock *LandingPadBlock;      // Landing pad block.
     69   SmallVector<MCSymbol *, 1> BeginLabels;  // Labels prior to invoke.
     70   SmallVector<MCSymbol *, 1> EndLabels;    // Labels after invoke.
     71   SmallVector<MCSymbol *, 1> ClauseLabels; // Labels for each clause.
     72   MCSymbol *LandingPadLabel;               // Label at beginning of landing pad.
     73   const Function *Personality;             // Personality function.
     74   std::vector<int> TypeIds;               // List of type ids (filters negative).
     75   int WinEHState;                         // WinEH specific state number.
     76 
     77   explicit LandingPadInfo(MachineBasicBlock *MBB)
     78       : LandingPadBlock(MBB), LandingPadLabel(nullptr), Personality(nullptr),
     79         WinEHState(-1) {}
     80 };
     81 
     82 //===----------------------------------------------------------------------===//
     83 /// MachineModuleInfoImpl - This class can be derived from and used by targets
     84 /// to hold private target-specific information for each Module.  Objects of
     85 /// type are accessed/created with MMI::getInfo and destroyed when the
     86 /// MachineModuleInfo is destroyed.
     87 ///
     88 class MachineModuleInfoImpl {
     89 public:
     90   typedef PointerIntPair<MCSymbol*, 1, bool> StubValueTy;
     91   virtual ~MachineModuleInfoImpl();
     92   typedef std::vector<std::pair<MCSymbol*, StubValueTy> > SymbolListTy;
     93 protected:
     94 
     95   /// Return the entries from a DenseMap in a deterministic sorted orer.
     96   /// Clears the map.
     97   static SymbolListTy getSortedStubs(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   DenseMap<const Function *, std::unique_ptr<WinEHFuncInfo>> FuncInfoMap;
    182 
    183 public:
    184   static char ID; // Pass identification, replacement for typeid
    185 
    186   struct VariableDbgInfo {
    187     const MDLocalVariable *Var;
    188     const MDExpression *Expr;
    189     unsigned Slot;
    190     const MDLocation *Loc;
    191 
    192     VariableDbgInfo(const MDLocalVariable *Var, const MDExpression *Expr,
    193                     unsigned Slot, const MDLocation *Loc)
    194         : Var(Var), Expr(Expr), Slot(Slot), Loc(Loc) {}
    195   };
    196   typedef SmallVector<VariableDbgInfo, 4> VariableDbgInfoMapTy;
    197   VariableDbgInfoMapTy VariableDbgInfos;
    198 
    199   MachineModuleInfo();  // DUMMY CONSTRUCTOR, DO NOT CALL.
    200   // Real constructor.
    201   MachineModuleInfo(const MCAsmInfo &MAI, const MCRegisterInfo &MRI,
    202                     const MCObjectFileInfo *MOFI);
    203   ~MachineModuleInfo() override;
    204 
    205   // Initialization and Finalization
    206   bool doInitialization(Module &) override;
    207   bool doFinalization(Module &) override;
    208 
    209   /// EndFunction - Discard function meta information.
    210   ///
    211   void EndFunction();
    212 
    213   const MCContext &getContext() const { return Context; }
    214   MCContext &getContext() { return Context; }
    215 
    216   void setModule(const Module *M) { TheModule = M; }
    217   const Module *getModule() const { return TheModule; }
    218 
    219   const Function *getWinEHParent(const Function *F) const;
    220   WinEHFuncInfo &getWinEHFuncInfo(const Function *F);
    221   bool hasWinEHFuncInfo(const Function *F) const {
    222     return FuncInfoMap.count(getWinEHParent(F)) > 0;
    223   }
    224 
    225   /// getInfo - Keep track of various per-function pieces of information for
    226   /// backends that would like to do so.
    227   ///
    228   template<typename Ty>
    229   Ty &getObjFileInfo() {
    230     if (ObjFileMMI == nullptr)
    231       ObjFileMMI = new Ty(*this);
    232     return *static_cast<Ty*>(ObjFileMMI);
    233   }
    234 
    235   template<typename Ty>
    236   const Ty &getObjFileInfo() const {
    237     return const_cast<MachineModuleInfo*>(this)->getObjFileInfo<Ty>();
    238   }
    239 
    240   /// AnalyzeModule - Scan the module for global debug information.
    241   ///
    242   void AnalyzeModule(const Module &M);
    243 
    244   /// hasDebugInfo - Returns true if valid debug info is present.
    245   ///
    246   bool hasDebugInfo() const { return DbgInfoAvailable; }
    247   void setDebugInfoAvailability(bool avail) { DbgInfoAvailable = avail; }
    248 
    249   bool callsEHReturn() const { return CallsEHReturn; }
    250   void setCallsEHReturn(bool b) { CallsEHReturn = b; }
    251 
    252   bool callsUnwindInit() const { return CallsUnwindInit; }
    253   void setCallsUnwindInit(bool b) { CallsUnwindInit = b; }
    254 
    255   bool usesVAFloatArgument() const {
    256     return UsesVAFloatArgument;
    257   }
    258 
    259   void setUsesVAFloatArgument(bool b) {
    260     UsesVAFloatArgument = b;
    261   }
    262 
    263   bool usesMorestackAddr() const {
    264     return UsesMorestackAddr;
    265   }
    266 
    267   void setUsesMorestackAddr(bool b) {
    268     UsesMorestackAddr = b;
    269   }
    270 
    271   /// \brief Returns a reference to a list of cfi instructions in the current
    272   /// function's prologue.  Used to construct frame maps for debug and exception
    273   /// handling comsumers.
    274   const std::vector<MCCFIInstruction> &getFrameInstructions() const {
    275     return FrameInstructions;
    276   }
    277 
    278   unsigned LLVM_ATTRIBUTE_UNUSED_RESULT
    279   addFrameInst(const MCCFIInstruction &Inst) {
    280     FrameInstructions.push_back(Inst);
    281     return FrameInstructions.size() - 1;
    282   }
    283 
    284   /// getAddrLabelSymbol - Return the symbol to be used for the specified basic
    285   /// block when its address is taken.  This cannot be its normal LBB label
    286   /// because the block may be accessed outside its containing function.
    287   MCSymbol *getAddrLabelSymbol(const BasicBlock *BB);
    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   std::vector<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(MachineBasicBlock *LandingPad,
    320                       const Function *Personality);
    321 
    322   void addWinEHState(MachineBasicBlock *LandingPad, int State);
    323 
    324   /// getPersonalityIndex - Get index of the current personality function inside
    325   /// Personalitites array
    326   unsigned getPersonalityIndex() const;
    327 
    328   /// getPersonalities - Return array of personality functions ever seen.
    329   const std::vector<const Function *>& getPersonalities() const {
    330     return Personalities;
    331   }
    332 
    333   /// isUsedFunction - Return true if the functions in the llvm.used list.  This
    334   /// does not return true for things in llvm.compiler.used unless they are also
    335   /// in llvm.used.
    336   bool isUsedFunction(const Function *F) const {
    337     return UsedFunctions.count(F);
    338   }
    339 
    340   /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
    341   ///
    342   void addCatchTypeInfo(MachineBasicBlock *LandingPad,
    343                         ArrayRef<const GlobalValue *> TyInfo);
    344 
    345   /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
    346   ///
    347   void addFilterTypeInfo(MachineBasicBlock *LandingPad,
    348                          ArrayRef<const GlobalValue *> TyInfo);
    349 
    350   /// addCleanup - Add a cleanup action for a landing pad.
    351   ///
    352   void addCleanup(MachineBasicBlock *LandingPad);
    353 
    354   /// Add a clause for a landing pad. Returns a new label for the clause. This
    355   /// is used by EH schemes that have more than one landing pad. In this case,
    356   /// each clause gets its own basic block.
    357   MCSymbol *addClauseForLandingPad(MachineBasicBlock *LandingPad);
    358 
    359   /// getTypeIDFor - Return the type id for the specified typeinfo.  This is
    360   /// function wide.
    361   unsigned getTypeIDFor(const GlobalValue *TI);
    362 
    363   /// getFilterIDFor - Return the id of the filter encoded by TyIds.  This is
    364   /// function wide.
    365   int getFilterIDFor(std::vector<unsigned> &TyIds);
    366 
    367   /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
    368   /// pads.
    369   void TidyLandingPads(DenseMap<MCSymbol*, uintptr_t> *LPMap = nullptr);
    370 
    371   /// getLandingPads - Return a reference to the landing pad info for the
    372   /// current function.
    373   const std::vector<LandingPadInfo> &getLandingPads() const {
    374     return LandingPads;
    375   }
    376 
    377   /// setCallSiteLandingPad - Map the landing pad's EH symbol to the call
    378   /// site indexes.
    379   void setCallSiteLandingPad(MCSymbol *Sym, ArrayRef<unsigned> Sites);
    380 
    381   /// getCallSiteLandingPad - Get the call site indexes for a landing pad EH
    382   /// symbol.
    383   SmallVectorImpl<unsigned> &getCallSiteLandingPad(MCSymbol *Sym) {
    384     assert(hasCallSiteLandingPad(Sym) &&
    385            "missing call site number for landing pad!");
    386     return LPadToCallSiteMap[Sym];
    387   }
    388 
    389   /// hasCallSiteLandingPad - Return true if the landing pad Eh symbol has an
    390   /// associated call site.
    391   bool hasCallSiteLandingPad(MCSymbol *Sym) {
    392     return !LPadToCallSiteMap[Sym].empty();
    393   }
    394 
    395   /// setCallSiteBeginLabel - Map the begin label for a call site.
    396   void setCallSiteBeginLabel(MCSymbol *BeginLabel, unsigned Site) {
    397     CallSiteMap[BeginLabel] = Site;
    398   }
    399 
    400   /// getCallSiteBeginLabel - Get the call site number for a begin label.
    401   unsigned getCallSiteBeginLabel(MCSymbol *BeginLabel) {
    402     assert(hasCallSiteBeginLabel(BeginLabel) &&
    403            "Missing call site number for EH_LABEL!");
    404     return CallSiteMap[BeginLabel];
    405   }
    406 
    407   /// hasCallSiteBeginLabel - Return true if the begin label has a call site
    408   /// number associated with it.
    409   bool hasCallSiteBeginLabel(MCSymbol *BeginLabel) {
    410     return CallSiteMap[BeginLabel] != 0;
    411   }
    412 
    413   /// setCurrentCallSite - Set the call site currently being processed.
    414   void setCurrentCallSite(unsigned Site) { CurCallSite = Site; }
    415 
    416   /// getCurrentCallSite - Get the call site currently being processed, if any.
    417   /// return zero if none.
    418   unsigned getCurrentCallSite() { return CurCallSite; }
    419 
    420   /// getTypeInfos - Return a reference to the C++ typeinfo for the current
    421   /// function.
    422   const std::vector<const GlobalValue *> &getTypeInfos() const {
    423     return TypeInfos;
    424   }
    425 
    426   /// getFilterIds - Return a reference to the typeids encoding filters used in
    427   /// the current function.
    428   const std::vector<unsigned> &getFilterIds() const {
    429     return FilterIds;
    430   }
    431 
    432   /// getPersonality - Return a personality function if available.  The presence
    433   /// of one is required to emit exception handling info.
    434   const Function *getPersonality() const;
    435 
    436   /// Classify the personality function amongst known EH styles.
    437   EHPersonality getPersonalityType();
    438 
    439   /// setVariableDbgInfo - Collect information used to emit debugging
    440   /// information of a variable.
    441   void setVariableDbgInfo(const MDLocalVariable *Var, const MDExpression *Expr,
    442                           unsigned Slot, const MDLocation *Loc) {
    443     VariableDbgInfos.emplace_back(Var, Expr, Slot, Loc);
    444   }
    445 
    446   VariableDbgInfoMapTy &getVariableDbgInfo() { return VariableDbgInfos; }
    447 
    448 }; // End class MachineModuleInfo
    449 
    450 } // End llvm namespace
    451 
    452 #endif
    453