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   struct KeyTy {
00050     APInt val;
00051     Type* type;
00052     KeyTy(const APInt& V, Type* Ty) : val(V), type(Ty) {}
00053     bool operator==(const KeyTy& that) const {
00054       return type == that.type && this->val == that.val;
00055     }
00056     bool operator!=(const KeyTy& that) const {
00057       return !this->operator==(that);
00058     }
00059     friend hash_code hash_value(const KeyTy &Key) {
00060       return hash_combine(Key.type, Key.val);
00061     }
00062   };
00063   static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), nullptr); }
00064   static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), nullptr); }
00065   static unsigned getHashValue(const KeyTy &Key) {
00066     return static_cast<unsigned>(hash_value(Key));
00067   }
00068   static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
00069     return LHS == RHS;
00070   }
00071 };
00072 
00073 struct DenseMapAPFloatKeyInfo {
00074   struct KeyTy {
00075     APFloat val;
00076     KeyTy(const APFloat& V) : val(V){}
00077     bool operator==(const KeyTy& that) const {
00078       return this->val.bitwiseIsEqual(that.val);
00079     }
00080     bool operator!=(const KeyTy& that) const {
00081       return !this->operator==(that);
00082     }
00083     friend hash_code hash_value(const KeyTy &Key) {
00084       return hash_combine(Key.val);
00085     }
00086   };
00087   static inline KeyTy getEmptyKey() { 
00088     return KeyTy(APFloat(APFloat::Bogus,1));
00089   }
00090   static inline KeyTy getTombstoneKey() { 
00091     return KeyTy(APFloat(APFloat::Bogus,2)); 
00092   }
00093   static unsigned getHashValue(const KeyTy &Key) {
00094     return static_cast<unsigned>(hash_value(Key));
00095   }
00096   static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
00097     return LHS == RHS;
00098   }
00099 };
00100 
00101 struct AnonStructTypeKeyInfo {
00102   struct KeyTy {
00103     ArrayRef<Type*> ETypes;
00104     bool isPacked;
00105     KeyTy(const ArrayRef<Type*>& E, bool P) :
00106       ETypes(E), isPacked(P) {}
00107     KeyTy(const StructType *ST)
00108         : ETypes(ST->elements()), isPacked(ST->isPacked()) {}
00109     bool operator==(const KeyTy& that) const {
00110       if (isPacked != that.isPacked)
00111         return false;
00112       if (ETypes != that.ETypes)
00113         return false;
00114       return true;
00115     }
00116     bool operator!=(const KeyTy& that) const {
00117       return !this->operator==(that);
00118     }
00119   };
00120   static inline StructType* getEmptyKey() {
00121     return DenseMapInfo<StructType*>::getEmptyKey();
00122   }
00123   static inline StructType* getTombstoneKey() {
00124     return DenseMapInfo<StructType*>::getTombstoneKey();
00125   }
00126   static unsigned getHashValue(const KeyTy& Key) {
00127     return hash_combine(hash_combine_range(Key.ETypes.begin(),
00128                                            Key.ETypes.end()),
00129                         Key.isPacked);
00130   }
00131   static unsigned getHashValue(const StructType *ST) {
00132     return getHashValue(KeyTy(ST));
00133   }
00134   static bool isEqual(const KeyTy& LHS, const StructType *RHS) {
00135     if (RHS == getEmptyKey() || RHS == getTombstoneKey())
00136       return false;
00137     return LHS == KeyTy(RHS);
00138   }
00139   static bool isEqual(const StructType *LHS, const StructType *RHS) {
00140     return LHS == RHS;
00141   }
00142 };
00143 
00144 struct FunctionTypeKeyInfo {
00145   struct KeyTy {
00146     const Type *ReturnType;
00147     ArrayRef<Type*> Params;
00148     bool isVarArg;
00149     KeyTy(const Type* R, const ArrayRef<Type*>& P, bool V) :
00150       ReturnType(R), Params(P), isVarArg(V) {}
00151     KeyTy(const FunctionType *FT)
00152         : ReturnType(FT->getReturnType()), Params(FT->params()),
00153           isVarArg(FT->isVarArg()) {}
00154     bool operator==(const KeyTy& that) const {
00155       if (ReturnType != that.ReturnType)
00156         return false;
00157       if (isVarArg != that.isVarArg)
00158         return false;
00159       if (Params != that.Params)
00160         return false;
00161       return true;
00162     }
00163     bool operator!=(const KeyTy& that) const {
00164       return !this->operator==(that);
00165     }
00166   };
00167   static inline FunctionType* getEmptyKey() {
00168     return DenseMapInfo<FunctionType*>::getEmptyKey();
00169   }
00170   static inline FunctionType* getTombstoneKey() {
00171     return DenseMapInfo<FunctionType*>::getTombstoneKey();
00172   }
00173   static unsigned getHashValue(const KeyTy& Key) {
00174     return hash_combine(Key.ReturnType,
00175                         hash_combine_range(Key.Params.begin(),
00176                                            Key.Params.end()),
00177                         Key.isVarArg);
00178   }
00179   static unsigned getHashValue(const FunctionType *FT) {
00180     return getHashValue(KeyTy(FT));
00181   }
00182   static bool isEqual(const KeyTy& LHS, const FunctionType *RHS) {
00183     if (RHS == getEmptyKey() || RHS == getTombstoneKey())
00184       return false;
00185     return LHS == KeyTy(RHS);
00186   }
00187   static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) {
00188     return LHS == RHS;
00189   }
00190 };
00191 
00192 /// \brief DenseMapInfo for GenericMDNode.
00193 ///
00194 /// Note that we don't need the is-function-local bit, since that's implicit in
00195 /// the operands.
00196 struct GenericMDNodeInfo {
00197   struct KeyTy {
00198     ArrayRef<Value *> Ops;
00199     unsigned Hash;
00200 
00201     KeyTy(ArrayRef<Value *> Ops)
00202         : Ops(Ops), Hash(hash_combine_range(Ops.begin(), Ops.end())) {}
00203 
00204     KeyTy(GenericMDNode *N, SmallVectorImpl<Value *> &Storage) {
00205       Storage.resize(N->getNumOperands());
00206       for (unsigned I = 0, E = N->getNumOperands(); I != E; ++I)
00207         Storage[I] = N->getOperand(I);
00208       Ops = Storage;
00209       Hash = hash_combine_range(Ops.begin(), Ops.end());
00210     }
00211 
00212     bool operator==(const GenericMDNode *RHS) const {
00213       if (RHS == getEmptyKey() || RHS == getTombstoneKey())
00214         return false;
00215       if (Hash != RHS->getHash() || Ops.size() != RHS->getNumOperands())
00216         return false;
00217       for (unsigned I = 0, E = Ops.size(); I != E; ++I)
00218         if (Ops[I] != RHS->getOperand(I))
00219           return false;
00220       return true;
00221     }
00222   };
00223   static inline GenericMDNode *getEmptyKey() {
00224     return DenseMapInfo<GenericMDNode *>::getEmptyKey();
00225   }
00226   static inline GenericMDNode *getTombstoneKey() {
00227     return DenseMapInfo<GenericMDNode *>::getTombstoneKey();
00228   }
00229   static unsigned getHashValue(const KeyTy &Key) { return Key.Hash; }
00230   static unsigned getHashValue(const GenericMDNode *U) {
00231     return U->getHash();
00232   }
00233   static bool isEqual(const KeyTy &LHS, const GenericMDNode *RHS) {
00234     return LHS == RHS;
00235   }
00236   static bool isEqual(const GenericMDNode *LHS, const GenericMDNode *RHS) {
00237     return LHS == RHS;
00238   }
00239 };
00240 
00241 /// DebugRecVH - This is a CallbackVH used to keep the Scope -> index maps
00242 /// up to date as MDNodes mutate.  This class is implemented in DebugLoc.cpp.
00243 class DebugRecVH : public CallbackVH {
00244   /// Ctx - This is the LLVM Context being referenced.
00245   LLVMContextImpl *Ctx;
00246   
00247   /// Idx - The index into either ScopeRecordIdx or ScopeInlinedAtRecords that
00248   /// this reference lives in.  If this is zero, then it represents a
00249   /// non-canonical entry that has no DenseMap value.  This can happen due to
00250   /// RAUW.
00251   int Idx;
00252 public:
00253   DebugRecVH(MDNode *n, LLVMContextImpl *ctx, int idx)
00254     : CallbackVH(n), Ctx(ctx), Idx(idx) {}
00255   
00256   MDNode *get() const {
00257     return cast_or_null<MDNode>(getValPtr());
00258   }
00259 
00260   void deleted() override;
00261   void allUsesReplacedWith(Value *VNew) override;
00262 };
00263   
00264 class LLVMContextImpl {
00265 public:
00266   /// OwnedModules - The set of modules instantiated in this context, and which
00267   /// will be automatically deleted if this context is deleted.
00268   SmallPtrSet<Module*, 4> OwnedModules;
00269   
00270   LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
00271   void *InlineAsmDiagContext;
00272 
00273   LLVMContext::DiagnosticHandlerTy DiagnosticHandler;
00274   void *DiagnosticContext;
00275   bool RespectDiagnosticFilters;
00276 
00277   LLVMContext::YieldCallbackTy YieldCallback;
00278   void *YieldOpaqueHandle;
00279 
00280   typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt *,
00281                    DenseMapAPIntKeyInfo> IntMapTy;
00282   IntMapTy IntConstants;
00283   
00284   typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*, 
00285                          DenseMapAPFloatKeyInfo> FPMapTy;
00286   FPMapTy FPConstants;
00287 
00288   FoldingSet<AttributeImpl> AttrsSet;
00289   FoldingSet<AttributeSetImpl> AttrsLists;
00290   FoldingSet<AttributeSetNode> AttrsSetNodes;
00291 
00292   StringMap<MDString> MDStringCache;
00293 
00294   DenseSet<GenericMDNode *, GenericMDNodeInfo> MDNodeSet;
00295 
00296   // MDNodes may be uniqued or not uniqued.  When they're not uniqued, they
00297   // aren't in the MDNodeSet, but they're still shared between objects, so no
00298   // one object can destroy them.  This set allows us to at least destroy them
00299   // on Context destruction.
00300   SmallPtrSet<GenericMDNode *, 1> NonUniquedMDNodes;
00301 
00302   DenseMap<Type*, ConstantAggregateZero*> CAZConstants;
00303 
00304   typedef ConstantUniqueMap<ConstantArray> ArrayConstantsTy;
00305   ArrayConstantsTy ArrayConstants;
00306   
00307   typedef ConstantUniqueMap<ConstantStruct> StructConstantsTy;
00308   StructConstantsTy StructConstants;
00309   
00310   typedef ConstantUniqueMap<ConstantVector> VectorConstantsTy;
00311   VectorConstantsTy VectorConstants;
00312   
00313   DenseMap<PointerType*, ConstantPointerNull*> CPNConstants;
00314 
00315   DenseMap<Type*, UndefValue*> UVConstants;
00316   
00317   StringMap<ConstantDataSequential*> CDSConstants;
00318 
00319   DenseMap<std::pair<const Function *, const BasicBlock *>, BlockAddress *>
00320     BlockAddresses;
00321   ConstantUniqueMap<ConstantExpr> ExprConstants;
00322 
00323   ConstantUniqueMap<InlineAsm> InlineAsms;
00324 
00325   ConstantInt *TheTrueVal;
00326   ConstantInt *TheFalseVal;
00327   
00328   LeakDetectorImpl<Value> LLVMObjects;
00329   
00330   // Basic type instances.
00331   Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy;
00332   Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy;
00333   IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty;
00334 
00335   
00336   /// TypeAllocator - All dynamically allocated types are allocated from this.
00337   /// They live forever until the context is torn down.
00338   BumpPtrAllocator TypeAllocator;
00339   
00340   DenseMap<unsigned, IntegerType*> IntegerTypes;
00341   
00342   typedef DenseMap<FunctionType*, bool, FunctionTypeKeyInfo> FunctionTypeMap;
00343   FunctionTypeMap FunctionTypes;
00344   typedef DenseMap<StructType*, bool, AnonStructTypeKeyInfo> StructTypeMap;
00345   StructTypeMap AnonStructTypes;
00346   StringMap<StructType*> NamedStructTypes;
00347   unsigned NamedStructTypesUniqueID;
00348     
00349   DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
00350   DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes;
00351   DenseMap<Type*, PointerType*> PointerTypes;  // Pointers in AddrSpace = 0
00352   DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;
00353 
00354 
00355   /// ValueHandles - This map keeps track of all of the value handles that are
00356   /// watching a Value*.  The Value::HasValueHandle bit is used to know
00357   /// whether or not a value has an entry in this map.
00358   typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
00359   ValueHandlesTy ValueHandles;
00360   
00361   /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
00362   StringMap<unsigned> CustomMDKindNames;
00363   
00364   typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
00365   typedef SmallVector<MDPairTy, 2> MDMapTy;
00366 
00367   /// MetadataStore - Collection of per-instruction metadata used in this
00368   /// context.
00369   DenseMap<const Instruction *, MDMapTy> MetadataStore;
00370   
00371   /// ScopeRecordIdx - This is the index in ScopeRecords for an MDNode scope
00372   /// entry with no "inlined at" element.
00373   DenseMap<MDNode*, int> ScopeRecordIdx;
00374   
00375   /// ScopeRecords - These are the actual mdnodes (in a value handle) for an
00376   /// index.  The ValueHandle ensures that ScopeRecordIdx stays up to date if
00377   /// the MDNode is RAUW'd.
00378   std::vector<DebugRecVH> ScopeRecords;
00379   
00380   /// ScopeInlinedAtIdx - This is the index in ScopeInlinedAtRecords for an
00381   /// scope/inlined-at pair.
00382   DenseMap<std::pair<MDNode*, MDNode*>, int> ScopeInlinedAtIdx;
00383   
00384   /// ScopeInlinedAtRecords - These are the actual mdnodes (in value handles)
00385   /// for an index.  The ValueHandle ensures that ScopeINlinedAtIdx stays up
00386   /// to date.
00387   std::vector<std::pair<DebugRecVH, DebugRecVH> > ScopeInlinedAtRecords;
00388 
00389   /// DiscriminatorTable - This table maps file:line locations to an
00390   /// integer representing the next DWARF path discriminator to assign to
00391   /// instructions in different blocks at the same location.
00392   DenseMap<std::pair<const char *, unsigned>, unsigned> DiscriminatorTable;
00393 
00394   /// IntrinsicIDCache - Cache of intrinsic name (string) to numeric ID mappings
00395   /// requested in this context
00396   typedef DenseMap<const Function*, unsigned> IntrinsicIDCacheTy;
00397   IntrinsicIDCacheTy IntrinsicIDCache;
00398 
00399   /// \brief Mapping from a function to its prefix data, which is stored as the
00400   /// operand of an unparented ReturnInst so that the prefix data has a Use.
00401   typedef DenseMap<const Function *, ReturnInst *> PrefixDataMapTy;
00402   PrefixDataMapTy PrefixDataMap;
00403 
00404   int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
00405   int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
00406   
00407   LLVMContextImpl(LLVMContext &C);
00408   ~LLVMContextImpl();
00409 };
00410 
00411 }
00412 
00413 #endif