LLVM API Documentation

LLVMContextImpl.h
Go to the documentation of this file.
00001 //===-- LLVMContextImpl.h - The LLVMContextImpl opaque class ----*- 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 //  This file declares LLVMContextImpl, the opaque implementation 
00011 //  of LLVMContext.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_LIB_IR_LLVMCONTEXTIMPL_H
00016 #define LLVM_LIB_IR_LLVMCONTEXTIMPL_H
00017 
00018 #include "AttributeImpl.h"
00019 #include "ConstantsContext.h"
00020 #include "LeaksContext.h"
00021 #include "llvm/ADT/APFloat.h"
00022 #include "llvm/ADT/APInt.h"
00023 #include "llvm/ADT/ArrayRef.h"
00024 #include "llvm/ADT/DenseMap.h"
00025 #include "llvm/ADT/DenseSet.h"
00026 #include "llvm/ADT/FoldingSet.h"
00027 #include "llvm/ADT/Hashing.h"
00028 #include "llvm/ADT/SmallPtrSet.h"
00029 #include "llvm/ADT/StringMap.h"
00030 #include "llvm/IR/Constants.h"
00031 #include "llvm/IR/DerivedTypes.h"
00032 #include "llvm/IR/LLVMContext.h"
00033 #include "llvm/IR/Metadata.h"
00034 #include "llvm/IR/ValueHandle.h"
00035 #include <vector>
00036 
00037 namespace llvm {
00038 
00039 class ConstantInt;
00040 class ConstantFP;
00041 class DiagnosticInfoOptimizationRemark;
00042 class DiagnosticInfoOptimizationRemarkMissed;
00043 class DiagnosticInfoOptimizationRemarkAnalysis;
00044 class LLVMContext;
00045 class Type;
00046 class Value;
00047 
00048 struct DenseMapAPIntKeyInfo {
00049   static inline APInt getEmptyKey() {
00050     APInt V(nullptr, 0);
00051     V.VAL = 0;
00052     return V;
00053   }
00054   static inline APInt getTombstoneKey() {
00055     APInt V(nullptr, 0);
00056     V.VAL = 1;
00057     return V;
00058   }
00059   static unsigned getHashValue(const APInt &Key) {
00060     return static_cast<unsigned>(hash_value(Key));
00061   }
00062   static bool isEqual(const APInt &LHS, const APInt &RHS) {
00063     return LHS.getBitWidth() == RHS.getBitWidth() && LHS == RHS;
00064   }
00065 };
00066 
00067 struct DenseMapAPFloatKeyInfo {
00068   static inline APFloat getEmptyKey() { return APFloat(APFloat::Bogus, 1); }
00069   static inline APFloat getTombstoneKey() { return APFloat(APFloat::Bogus, 2); }
00070   static unsigned getHashValue(const APFloat &Key) {
00071     return static_cast<unsigned>(hash_value(Key));
00072   }
00073   static bool isEqual(const APFloat &LHS, const APFloat &RHS) {
00074     return LHS.bitwiseIsEqual(RHS);
00075   }
00076 };
00077 
00078 struct AnonStructTypeKeyInfo {
00079   struct KeyTy {
00080     ArrayRef<Type*> ETypes;
00081     bool isPacked;
00082     KeyTy(const ArrayRef<Type*>& E, bool P) :
00083       ETypes(E), isPacked(P) {}
00084     KeyTy(const StructType *ST)
00085         : ETypes(ST->elements()), isPacked(ST->isPacked()) {}
00086     bool operator==(const KeyTy& that) const {
00087       if (isPacked != that.isPacked)
00088         return false;
00089       if (ETypes != that.ETypes)
00090         return false;
00091       return true;
00092     }
00093     bool operator!=(const KeyTy& that) const {
00094       return !this->operator==(that);
00095     }
00096   };
00097   static inline StructType* getEmptyKey() {
00098     return DenseMapInfo<StructType*>::getEmptyKey();
00099   }
00100   static inline StructType* getTombstoneKey() {
00101     return DenseMapInfo<StructType*>::getTombstoneKey();
00102   }
00103   static unsigned getHashValue(const KeyTy& Key) {
00104     return hash_combine(hash_combine_range(Key.ETypes.begin(),
00105                                            Key.ETypes.end()),
00106                         Key.isPacked);
00107   }
00108   static unsigned getHashValue(const StructType *ST) {
00109     return getHashValue(KeyTy(ST));
00110   }
00111   static bool isEqual(const KeyTy& LHS, const StructType *RHS) {
00112     if (RHS == getEmptyKey() || RHS == getTombstoneKey())
00113       return false;
00114     return LHS == KeyTy(RHS);
00115   }
00116   static bool isEqual(const StructType *LHS, const StructType *RHS) {
00117     return LHS == RHS;
00118   }
00119 };
00120 
00121 struct FunctionTypeKeyInfo {
00122   struct KeyTy {
00123     const Type *ReturnType;
00124     ArrayRef<Type*> Params;
00125     bool isVarArg;
00126     KeyTy(const Type* R, const ArrayRef<Type*>& P, bool V) :
00127       ReturnType(R), Params(P), isVarArg(V) {}
00128     KeyTy(const FunctionType *FT)
00129         : ReturnType(FT->getReturnType()), Params(FT->params()),
00130           isVarArg(FT->isVarArg()) {}
00131     bool operator==(const KeyTy& that) const {
00132       if (ReturnType != that.ReturnType)
00133         return false;
00134       if (isVarArg != that.isVarArg)
00135         return false;
00136       if (Params != that.Params)
00137         return false;
00138       return true;
00139     }
00140     bool operator!=(const KeyTy& that) const {
00141       return !this->operator==(that);
00142     }
00143   };
00144   static inline FunctionType* getEmptyKey() {
00145     return DenseMapInfo<FunctionType*>::getEmptyKey();
00146   }
00147   static inline FunctionType* getTombstoneKey() {
00148     return DenseMapInfo<FunctionType*>::getTombstoneKey();
00149   }
00150   static unsigned getHashValue(const KeyTy& Key) {
00151     return hash_combine(Key.ReturnType,
00152                         hash_combine_range(Key.Params.begin(),
00153                                            Key.Params.end()),
00154                         Key.isVarArg);
00155   }
00156   static unsigned getHashValue(const FunctionType *FT) {
00157     return getHashValue(KeyTy(FT));
00158   }
00159   static bool isEqual(const KeyTy& LHS, const FunctionType *RHS) {
00160     if (RHS == getEmptyKey() || RHS == getTombstoneKey())
00161       return false;
00162     return LHS == KeyTy(RHS);
00163   }
00164   static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) {
00165     return LHS == RHS;
00166   }
00167 };
00168 
00169 /// \brief DenseMapInfo for GenericMDNode.
00170 ///
00171 /// Note that we don't need the is-function-local bit, since that's implicit in
00172 /// the operands.
00173 struct GenericMDNodeInfo {
00174   struct KeyTy {
00175     ArrayRef<Metadata *> RawOps;
00176     ArrayRef<MDOperand> Ops;
00177     unsigned Hash;
00178 
00179     KeyTy(ArrayRef<Metadata *> Ops)
00180         : RawOps(Ops), Hash(hash_combine_range(Ops.begin(), Ops.end())) {}
00181 
00182     KeyTy(GenericMDNode *N)
00183         : Ops(N->op_begin(), N->op_end()), Hash(N->getHash()) {}
00184 
00185     bool operator==(const GenericMDNode *RHS) const {
00186       if (RHS == getEmptyKey() || RHS == getTombstoneKey())
00187         return false;
00188       if (Hash != RHS->getHash())
00189         return false;
00190       assert((RawOps.empty() || Ops.empty()) && "Two sets of operands?");
00191       return RawOps.empty() ? compareOps(Ops, RHS) : compareOps(RawOps, RHS);
00192     }
00193     template <class T>
00194     static bool compareOps(ArrayRef<T> Ops, const GenericMDNode *RHS) {
00195       if (Ops.size() != RHS->getNumOperands())
00196         return false;
00197       return std::equal(Ops.begin(), Ops.end(), RHS->op_begin());
00198     }
00199   };
00200   static inline GenericMDNode *getEmptyKey() {
00201     return DenseMapInfo<GenericMDNode *>::getEmptyKey();
00202   }
00203   static inline GenericMDNode *getTombstoneKey() {
00204     return DenseMapInfo<GenericMDNode *>::getTombstoneKey();
00205   }
00206   static unsigned getHashValue(const KeyTy &Key) { return Key.Hash; }
00207   static unsigned getHashValue(const GenericMDNode *U) {
00208     return U->getHash();
00209   }
00210   static bool isEqual(const KeyTy &LHS, const GenericMDNode *RHS) {
00211     return LHS == RHS;
00212   }
00213   static bool isEqual(const GenericMDNode *LHS, const GenericMDNode *RHS) {
00214     return LHS == RHS;
00215   }
00216 };
00217 
00218 class LLVMContextImpl {
00219 public:
00220   /// OwnedModules - The set of modules instantiated in this context, and which
00221   /// will be automatically deleted if this context is deleted.
00222   SmallPtrSet<Module*, 4> OwnedModules;
00223   
00224   LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
00225   void *InlineAsmDiagContext;
00226 
00227   LLVMContext::DiagnosticHandlerTy DiagnosticHandler;
00228   void *DiagnosticContext;
00229   bool RespectDiagnosticFilters;
00230 
00231   LLVMContext::YieldCallbackTy YieldCallback;
00232   void *YieldOpaqueHandle;
00233 
00234   typedef DenseMap<APInt, ConstantInt *, DenseMapAPIntKeyInfo> IntMapTy;
00235   IntMapTy IntConstants;
00236 
00237   typedef DenseMap<APFloat, ConstantFP *, DenseMapAPFloatKeyInfo> FPMapTy;
00238   FPMapTy FPConstants;
00239 
00240   FoldingSet<AttributeImpl> AttrsSet;
00241   FoldingSet<AttributeSetImpl> AttrsLists;
00242   FoldingSet<AttributeSetNode> AttrsSetNodes;
00243 
00244   StringMap<MDString> MDStringCache;
00245   DenseMap<Value *, ValueAsMetadata *> ValuesAsMetadata;
00246   DenseMap<Metadata *, MetadataAsValue *> MetadataAsValues;
00247 
00248   DenseSet<GenericMDNode *, GenericMDNodeInfo> MDNodeSet;
00249 
00250   // MDNodes may be uniqued or not uniqued.  When they're not uniqued, they
00251   // aren't in the MDNodeSet, but they're still shared between objects, so no
00252   // one object can destroy them.  This set allows us to at least destroy them
00253   // on Context destruction.
00254   SmallPtrSet<GenericMDNode *, 1> NonUniquedMDNodes;
00255 
00256   DenseMap<Type*, ConstantAggregateZero*> CAZConstants;
00257 
00258   typedef ConstantUniqueMap<ConstantArray> ArrayConstantsTy;
00259   ArrayConstantsTy ArrayConstants;
00260   
00261   typedef ConstantUniqueMap<ConstantStruct> StructConstantsTy;
00262   StructConstantsTy StructConstants;
00263   
00264   typedef ConstantUniqueMap<ConstantVector> VectorConstantsTy;
00265   VectorConstantsTy VectorConstants;
00266   
00267   DenseMap<PointerType*, ConstantPointerNull*> CPNConstants;
00268 
00269   DenseMap<Type*, UndefValue*> UVConstants;
00270   
00271   StringMap<ConstantDataSequential*> CDSConstants;
00272 
00273   DenseMap<std::pair<const Function *, const BasicBlock *>, BlockAddress *>
00274     BlockAddresses;
00275   ConstantUniqueMap<ConstantExpr> ExprConstants;
00276 
00277   ConstantUniqueMap<InlineAsm> InlineAsms;
00278 
00279   ConstantInt *TheTrueVal;
00280   ConstantInt *TheFalseVal;
00281   
00282   LeakDetectorImpl<Value> LLVMObjects;
00283   LeakDetectorImpl<Metadata> LLVMMDObjects;
00284 
00285   // Basic type instances.
00286   Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy;
00287   Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy;
00288   IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty;
00289 
00290   
00291   /// TypeAllocator - All dynamically allocated types are allocated from this.
00292   /// They live forever until the context is torn down.
00293   BumpPtrAllocator TypeAllocator;
00294   
00295   DenseMap<unsigned, IntegerType*> IntegerTypes;
00296 
00297   typedef DenseSet<FunctionType *, FunctionTypeKeyInfo> FunctionTypeSet;
00298   FunctionTypeSet FunctionTypes;
00299   typedef DenseSet<StructType *, AnonStructTypeKeyInfo> StructTypeSet;
00300   StructTypeSet AnonStructTypes;
00301   StringMap<StructType*> NamedStructTypes;
00302   unsigned NamedStructTypesUniqueID;
00303     
00304   DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
00305   DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes;
00306   DenseMap<Type*, PointerType*> PointerTypes;  // Pointers in AddrSpace = 0
00307   DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;
00308 
00309 
00310   /// ValueHandles - This map keeps track of all of the value handles that are
00311   /// watching a Value*.  The Value::HasValueHandle bit is used to know
00312   /// whether or not a value has an entry in this map.
00313   typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
00314   ValueHandlesTy ValueHandles;
00315   
00316   /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
00317   StringMap<unsigned> CustomMDKindNames;
00318 
00319   typedef std::pair<unsigned, TrackingMDNodeRef> MDPairTy;
00320   typedef SmallVector<MDPairTy, 2> MDMapTy;
00321 
00322   /// MetadataStore - Collection of per-instruction metadata used in this
00323   /// context.
00324   DenseMap<const Instruction *, MDMapTy> MetadataStore;
00325   
00326   /// DiscriminatorTable - This table maps file:line locations to an
00327   /// integer representing the next DWARF path discriminator to assign to
00328   /// instructions in different blocks at the same location.
00329   DenseMap<std::pair<const char *, unsigned>, unsigned> DiscriminatorTable;
00330 
00331   /// IntrinsicIDCache - Cache of intrinsic name (string) to numeric ID mappings
00332   /// requested in this context
00333   typedef DenseMap<const Function*, unsigned> IntrinsicIDCacheTy;
00334   IntrinsicIDCacheTy IntrinsicIDCache;
00335 
00336   /// \brief Mapping from a function to its prefix data, which is stored as the
00337   /// operand of an unparented ReturnInst so that the prefix data has a Use.
00338   typedef DenseMap<const Function *, ReturnInst *> PrefixDataMapTy;
00339   PrefixDataMapTy PrefixDataMap;
00340 
00341   /// \brief Mapping from a function to its prologue data, which is stored as
00342   /// the operand of an unparented ReturnInst so that the prologue data has a
00343   /// Use.
00344   typedef DenseMap<const Function *, ReturnInst *> PrologueDataMapTy;
00345   PrologueDataMapTy PrologueDataMap;
00346 
00347   int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
00348   int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
00349   
00350   LLVMContextImpl(LLVMContext &C);
00351   ~LLVMContextImpl();
00352 };
00353 
00354 }
00355 
00356 #endif