LLVM API Documentation

MachineModuleInfo.h
Go to the documentation of this file.
00001 //===-- llvm/CodeGen/MachineModuleInfo.h ------------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // Collect meta information for a module.  This information should be in a
00011 // neutral form that can be used by different debugging and exception handling
00012 // schemes.
00013 //
00014 // The organization of information is primarily clustered around the source
00015 // compile units.  The main exception is source line correspondence where
00016 // inlining may interleave code from various compile units.
00017 //
00018 // The following information can be retrieved from the MachineModuleInfo.
00019 //
00020 //  -- Source directories - Directories are uniqued based on their canonical
00021 //     string and assigned a sequential numeric ID (base 1.)
00022 //  -- Source files - Files are also uniqued based on their name and directory
00023 //     ID.  A file ID is sequential number (base 1.)
00024 //  -- Source line correspondence - A vector of file ID, line#, column# triples.
00025 //     A DEBUG_LOCATION instruction is generated  by the DAG Legalizer
00026 //     corresponding to each entry in the source line list.  This allows a debug
00027 //     emitter to generate labels referenced by debug information tables.
00028 //
00029 //===----------------------------------------------------------------------===//
00030 
00031 #ifndef LLVM_CODEGEN_MACHINEMODULEINFO_H
00032 #define LLVM_CODEGEN_MACHINEMODULEINFO_H
00033 
00034 #include "llvm/ADT/DenseMap.h"
00035 #include "llvm/ADT/PointerIntPair.h"
00036 #include "llvm/ADT/SmallPtrSet.h"
00037 #include "llvm/ADT/SmallVector.h"
00038 #include "llvm/IR/DebugLoc.h"
00039 #include "llvm/IR/Metadata.h"
00040 #include "llvm/IR/ValueHandle.h"
00041 #include "llvm/MC/MCContext.h"
00042 #include "llvm/MC/MachineLocation.h"
00043 #include "llvm/Pass.h"
00044 #include "llvm/Support/DataTypes.h"
00045 #include "llvm/Support/Dwarf.h"
00046 
00047 namespace llvm {
00048 
00049 //===----------------------------------------------------------------------===//
00050 // Forward declarations.
00051 class Constant;
00052 class GlobalVariable;
00053 class MDNode;
00054 class MMIAddrLabelMap;
00055 class MachineBasicBlock;
00056 class MachineFunction;
00057 class Module;
00058 class PointerType;
00059 class StructType;
00060 
00061 //===----------------------------------------------------------------------===//
00062 /// LandingPadInfo - This structure is used to retain landing pad info for
00063 /// the current function.
00064 ///
00065 struct LandingPadInfo {
00066   MachineBasicBlock *LandingPadBlock;    // Landing pad block.
00067   SmallVector<MCSymbol*, 1> BeginLabels; // Labels prior to invoke.
00068   SmallVector<MCSymbol*, 1> EndLabels;   // Labels after invoke.
00069   MCSymbol *LandingPadLabel;             // Label at beginning of landing pad.
00070   const Function *Personality;           // Personality function.
00071   std::vector<int> TypeIds;              // List of type ids (filters negative)
00072 
00073   explicit LandingPadInfo(MachineBasicBlock *MBB)
00074     : LandingPadBlock(MBB), LandingPadLabel(nullptr), Personality(nullptr) {}
00075 };
00076 
00077 //===----------------------------------------------------------------------===//
00078 /// MachineModuleInfoImpl - This class can be derived from and used by targets
00079 /// to hold private target-specific information for each Module.  Objects of
00080 /// type are accessed/created with MMI::getInfo and destroyed when the
00081 /// MachineModuleInfo is destroyed.
00082 /// 
00083 class MachineModuleInfoImpl {
00084 public:
00085   typedef PointerIntPair<MCSymbol*, 1, bool> StubValueTy;
00086   virtual ~MachineModuleInfoImpl();
00087   typedef std::vector<std::pair<MCSymbol*, StubValueTy> > SymbolListTy;
00088 protected:
00089   static SymbolListTy GetSortedStubs(const DenseMap<MCSymbol*, StubValueTy>&);
00090 };
00091 
00092 //===----------------------------------------------------------------------===//
00093 /// MachineModuleInfo - This class contains meta information specific to a
00094 /// module.  Queries can be made by different debugging and exception handling
00095 /// schemes and reformated for specific use.
00096 ///
00097 class MachineModuleInfo : public ImmutablePass {
00098   /// Context - This is the MCContext used for the entire code generator.
00099   MCContext Context;
00100 
00101   /// TheModule - This is the LLVM Module being worked on.
00102   const Module *TheModule;
00103 
00104   /// ObjFileMMI - This is the object-file-format-specific implementation of
00105   /// MachineModuleInfoImpl, which lets targets accumulate whatever info they
00106   /// want.
00107   MachineModuleInfoImpl *ObjFileMMI;
00108 
00109   /// List of moves done by a function's prolog.  Used to construct frame maps
00110   /// by debug and exception handling consumers.
00111   std::vector<MCCFIInstruction> FrameInstructions;
00112 
00113   /// LandingPads - List of LandingPadInfo describing the landing pad
00114   /// information in the current function.
00115   std::vector<LandingPadInfo> LandingPads;
00116 
00117   /// LPadToCallSiteMap - Map a landing pad's EH symbol to the call site
00118   /// indexes.
00119   DenseMap<MCSymbol*, SmallVector<unsigned, 4> > LPadToCallSiteMap;
00120 
00121   /// CallSiteMap - Map of invoke call site index values to associated begin
00122   /// EH_LABEL for the current function.
00123   DenseMap<MCSymbol*, unsigned> CallSiteMap;
00124 
00125   /// CurCallSite - The current call site index being processed, if any. 0 if
00126   /// none.
00127   unsigned CurCallSite;
00128 
00129   /// TypeInfos - List of C++ TypeInfo used in the current function.
00130   std::vector<const GlobalVariable *> TypeInfos;
00131 
00132   /// FilterIds - List of typeids encoding filters used in the current function.
00133   std::vector<unsigned> FilterIds;
00134 
00135   /// FilterEnds - List of the indices in FilterIds corresponding to filter
00136   /// terminators.
00137   std::vector<unsigned> FilterEnds;
00138 
00139   /// Personalities - Vector of all personality functions ever seen. Used to
00140   /// emit common EH frames.
00141   std::vector<const Function *> Personalities;
00142 
00143   /// UsedFunctions - The functions in the @llvm.used list in a more easily
00144   /// searchable format.  This does not include the functions in
00145   /// llvm.compiler.used.
00146   SmallPtrSet<const Function *, 32> UsedFunctions;
00147 
00148   /// AddrLabelSymbols - This map keeps track of which symbol is being used for
00149   /// the specified basic block's address of label.
00150   MMIAddrLabelMap *AddrLabelSymbols;
00151 
00152   bool CallsEHReturn;
00153   bool CallsUnwindInit;
00154 
00155   /// DbgInfoAvailable - True if debugging information is available
00156   /// in this module.
00157   bool DbgInfoAvailable;
00158 
00159   /// UsesVAFloatArgument - True if this module calls VarArg function with
00160   /// floating-point arguments.  This is used to emit an undefined reference
00161   /// to _fltused on Windows targets.
00162   bool UsesVAFloatArgument;
00163 
00164 public:
00165   static char ID; // Pass identification, replacement for typeid
00166 
00167   struct VariableDbgInfo {
00168     TrackingVH<MDNode> Var;
00169     unsigned Slot;
00170     DebugLoc Loc;
00171   };
00172   typedef SmallVector<VariableDbgInfo, 4> VariableDbgInfoMapTy;
00173   VariableDbgInfoMapTy VariableDbgInfos;
00174 
00175   MachineModuleInfo();  // DUMMY CONSTRUCTOR, DO NOT CALL.
00176   // Real constructor.
00177   MachineModuleInfo(const MCAsmInfo &MAI, const MCRegisterInfo &MRI,
00178                     const MCObjectFileInfo *MOFI);
00179   ~MachineModuleInfo();
00180 
00181   // Initialization and Finalization
00182   bool doInitialization(Module &) override;
00183   bool doFinalization(Module &) override;
00184 
00185   /// EndFunction - Discard function meta information.
00186   ///
00187   void EndFunction();
00188 
00189   const MCContext &getContext() const { return Context; }
00190   MCContext &getContext() { return Context; }
00191 
00192   void setModule(const Module *M) { TheModule = M; }
00193   const Module *getModule() const { return TheModule; }
00194 
00195   /// getInfo - Keep track of various per-function pieces of information for
00196   /// backends that would like to do so.
00197   ///
00198   template<typename Ty>
00199   Ty &getObjFileInfo() {
00200     if (ObjFileMMI == nullptr)
00201       ObjFileMMI = new Ty(*this);
00202     return *static_cast<Ty*>(ObjFileMMI);
00203   }
00204 
00205   template<typename Ty>
00206   const Ty &getObjFileInfo() const {
00207     return const_cast<MachineModuleInfo*>(this)->getObjFileInfo<Ty>();
00208   }
00209 
00210   /// AnalyzeModule - Scan the module for global debug information.
00211   ///
00212   void AnalyzeModule(const Module &M);
00213 
00214   /// hasDebugInfo - Returns true if valid debug info is present.
00215   ///
00216   bool hasDebugInfo() const { return DbgInfoAvailable; }
00217   void setDebugInfoAvailability(bool avail) { DbgInfoAvailable = avail; }
00218 
00219   bool callsEHReturn() const { return CallsEHReturn; }
00220   void setCallsEHReturn(bool b) { CallsEHReturn = b; }
00221 
00222   bool callsUnwindInit() const { return CallsUnwindInit; }
00223   void setCallsUnwindInit(bool b) { CallsUnwindInit = b; }
00224 
00225   bool usesVAFloatArgument() const {
00226     return UsesVAFloatArgument;
00227   }
00228 
00229   void setUsesVAFloatArgument(bool b) {
00230     UsesVAFloatArgument = b;
00231   }
00232 
00233   /// \brief Returns a reference to a list of cfi instructions in the current
00234   /// function's prologue.  Used to construct frame maps for debug and exception
00235   /// handling comsumers.
00236   const std::vector<MCCFIInstruction> &getFrameInstructions() const {
00237     return FrameInstructions;
00238   }
00239 
00240   unsigned LLVM_ATTRIBUTE_UNUSED_RESULT
00241   addFrameInst(const MCCFIInstruction &Inst) {
00242     FrameInstructions.push_back(Inst);
00243     return FrameInstructions.size() - 1;
00244   }
00245 
00246   /// getAddrLabelSymbol - Return the symbol to be used for the specified basic
00247   /// block when its address is taken.  This cannot be its normal LBB label
00248   /// because the block may be accessed outside its containing function.
00249   MCSymbol *getAddrLabelSymbol(const BasicBlock *BB);
00250 
00251   /// getAddrLabelSymbolToEmit - Return the symbol to be used for the specified
00252   /// basic block when its address is taken.  If other blocks were RAUW'd to
00253   /// this one, we may have to emit them as well, return the whole set.
00254   std::vector<MCSymbol*> getAddrLabelSymbolToEmit(const BasicBlock *BB);
00255 
00256   /// takeDeletedSymbolsForFunction - If the specified function has had any
00257   /// references to address-taken blocks generated, but the block got deleted,
00258   /// return the symbol now so we can emit it.  This prevents emitting a
00259   /// reference to a symbol that has no definition.
00260   void takeDeletedSymbolsForFunction(const Function *F,
00261                                      std::vector<MCSymbol*> &Result);
00262 
00263 
00264   //===- EH ---------------------------------------------------------------===//
00265 
00266   /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
00267   /// specified MachineBasicBlock.
00268   LandingPadInfo &getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad);
00269 
00270   /// addInvoke - Provide the begin and end labels of an invoke style call and
00271   /// associate it with a try landing pad block.
00272   void addInvoke(MachineBasicBlock *LandingPad,
00273                  MCSymbol *BeginLabel, MCSymbol *EndLabel);
00274 
00275   /// addLandingPad - Add a new panding pad.  Returns the label ID for the
00276   /// landing pad entry.
00277   MCSymbol *addLandingPad(MachineBasicBlock *LandingPad);
00278 
00279   /// addPersonality - Provide the personality function for the exception
00280   /// information.
00281   void addPersonality(MachineBasicBlock *LandingPad,
00282                       const Function *Personality);
00283 
00284   /// getPersonalityIndex - Get index of the current personality function inside
00285   /// Personalitites array
00286   unsigned getPersonalityIndex() const;
00287 
00288   /// getPersonalities - Return array of personality functions ever seen.
00289   const std::vector<const Function *>& getPersonalities() const {
00290     return Personalities;
00291   }
00292 
00293   /// isUsedFunction - Return true if the functions in the llvm.used list.  This
00294   /// does not return true for things in llvm.compiler.used unless they are also
00295   /// in llvm.used.
00296   bool isUsedFunction(const Function *F) const {
00297     return UsedFunctions.count(F);
00298   }
00299 
00300   /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
00301   ///
00302   void addCatchTypeInfo(MachineBasicBlock *LandingPad,
00303                         ArrayRef<const GlobalVariable *> TyInfo);
00304 
00305   /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
00306   ///
00307   void addFilterTypeInfo(MachineBasicBlock *LandingPad,
00308                          ArrayRef<const GlobalVariable *> TyInfo);
00309 
00310   /// addCleanup - Add a cleanup action for a landing pad.
00311   ///
00312   void addCleanup(MachineBasicBlock *LandingPad);
00313 
00314   /// getTypeIDFor - Return the type id for the specified typeinfo.  This is
00315   /// function wide.
00316   unsigned getTypeIDFor(const GlobalVariable *TI);
00317 
00318   /// getFilterIDFor - Return the id of the filter encoded by TyIds.  This is
00319   /// function wide.
00320   int getFilterIDFor(std::vector<unsigned> &TyIds);
00321 
00322   /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
00323   /// pads.
00324   void TidyLandingPads(DenseMap<MCSymbol*, uintptr_t> *LPMap = nullptr);
00325 
00326   /// getLandingPads - Return a reference to the landing pad info for the
00327   /// current function.
00328   const std::vector<LandingPadInfo> &getLandingPads() const {
00329     return LandingPads;
00330   }
00331 
00332   /// setCallSiteLandingPad - Map the landing pad's EH symbol to the call
00333   /// site indexes.
00334   void setCallSiteLandingPad(MCSymbol *Sym, ArrayRef<unsigned> Sites);
00335 
00336   /// getCallSiteLandingPad - Get the call site indexes for a landing pad EH
00337   /// symbol.
00338   SmallVectorImpl<unsigned> &getCallSiteLandingPad(MCSymbol *Sym) {
00339     assert(hasCallSiteLandingPad(Sym) &&
00340            "missing call site number for landing pad!");
00341     return LPadToCallSiteMap[Sym];
00342   }
00343 
00344   /// hasCallSiteLandingPad - Return true if the landing pad Eh symbol has an
00345   /// associated call site.
00346   bool hasCallSiteLandingPad(MCSymbol *Sym) {
00347     return !LPadToCallSiteMap[Sym].empty();
00348   }
00349 
00350   /// setCallSiteBeginLabel - Map the begin label for a call site.
00351   void setCallSiteBeginLabel(MCSymbol *BeginLabel, unsigned Site) {
00352     CallSiteMap[BeginLabel] = Site;
00353   }
00354 
00355   /// getCallSiteBeginLabel - Get the call site number for a begin label.
00356   unsigned getCallSiteBeginLabel(MCSymbol *BeginLabel) {
00357     assert(hasCallSiteBeginLabel(BeginLabel) &&
00358            "Missing call site number for EH_LABEL!");
00359     return CallSiteMap[BeginLabel];
00360   }
00361 
00362   /// hasCallSiteBeginLabel - Return true if the begin label has a call site
00363   /// number associated with it.
00364   bool hasCallSiteBeginLabel(MCSymbol *BeginLabel) {
00365     return CallSiteMap[BeginLabel] != 0;
00366   }
00367 
00368   /// setCurrentCallSite - Set the call site currently being processed.
00369   void setCurrentCallSite(unsigned Site) { CurCallSite = Site; }
00370 
00371   /// getCurrentCallSite - Get the call site currently being processed, if any.
00372   /// return zero if none.
00373   unsigned getCurrentCallSite() { return CurCallSite; }
00374 
00375   /// getTypeInfos - Return a reference to the C++ typeinfo for the current
00376   /// function.
00377   const std::vector<const GlobalVariable *> &getTypeInfos() const {
00378     return TypeInfos;
00379   }
00380 
00381   /// getFilterIds - Return a reference to the typeids encoding filters used in
00382   /// the current function.
00383   const std::vector<unsigned> &getFilterIds() const {
00384     return FilterIds;
00385   }
00386 
00387   /// getPersonality - Return a personality function if available.  The presence
00388   /// of one is required to emit exception handling info.
00389   const Function *getPersonality() const;
00390 
00391   /// setVariableDbgInfo - Collect information used to emit debugging
00392   /// information of a variable.
00393   void setVariableDbgInfo(MDNode *N, unsigned Slot, DebugLoc Loc) {
00394     VariableDbgInfo Info = { N, Slot, Loc };
00395     VariableDbgInfos.push_back(std::move(Info));
00396   }
00397 
00398   VariableDbgInfoMapTy &getVariableDbgInfo() { return VariableDbgInfos; }
00399 
00400 }; // End class MachineModuleInfo
00401 
00402 } // End llvm namespace
00403 
00404 #endif