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/FoldingSet.h"
00026 #include "llvm/ADT/Hashing.h"
00027 #include "llvm/ADT/SmallPtrSet.h"
00028 #include "llvm/ADT/StringMap.h"
00029 #include "llvm/IR/Constants.h"
00030 #include "llvm/IR/DerivedTypes.h"
00031 #include "llvm/IR/LLVMContext.h"
00032 #include "llvm/IR/Metadata.h"
00033 #include "llvm/IR/ValueHandle.h"
00034 #include <vector>
00035 
00036 namespace llvm {
00037 
00038 class ConstantInt;
00039 class ConstantFP;
00040 class DiagnosticInfoOptimizationRemark;
00041 class DiagnosticInfoOptimizationRemarkMissed;
00042 class DiagnosticInfoOptimizationRemarkAnalysis;
00043 class LLVMContext;
00044 class Type;
00045 class Value;
00046 
00047 struct DenseMapAPIntKeyInfo {
00048   struct KeyTy {
00049     APInt val;
00050     Type* type;
00051     KeyTy(const APInt& V, Type* Ty) : val(V), type(Ty) {}
00052     bool operator==(const KeyTy& that) const {
00053       return type == that.type && this->val == that.val;
00054     }
00055     bool operator!=(const KeyTy& that) const {
00056       return !this->operator==(that);
00057     }
00058     friend hash_code hash_value(const KeyTy &Key) {
00059       return hash_combine(Key.type, Key.val);
00060     }
00061   };
00062   static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), nullptr); }
00063   static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), nullptr); }
00064   static unsigned getHashValue(const KeyTy &Key) {
00065     return static_cast<unsigned>(hash_value(Key));
00066   }
00067   static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
00068     return LHS == RHS;
00069   }
00070 };
00071 
00072 struct DenseMapAPFloatKeyInfo {
00073   struct KeyTy {
00074     APFloat val;
00075     KeyTy(const APFloat& V) : val(V){}
00076     bool operator==(const KeyTy& that) const {
00077       return this->val.bitwiseIsEqual(that.val);
00078     }
00079     bool operator!=(const KeyTy& that) const {
00080       return !this->operator==(that);
00081     }
00082     friend hash_code hash_value(const KeyTy &Key) {
00083       return hash_combine(Key.val);
00084     }
00085   };
00086   static inline KeyTy getEmptyKey() { 
00087     return KeyTy(APFloat(APFloat::Bogus,1));
00088   }
00089   static inline KeyTy getTombstoneKey() { 
00090     return KeyTy(APFloat(APFloat::Bogus,2)); 
00091   }
00092   static unsigned getHashValue(const KeyTy &Key) {
00093     return static_cast<unsigned>(hash_value(Key));
00094   }
00095   static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
00096     return LHS == RHS;
00097   }
00098 };
00099 
00100 struct AnonStructTypeKeyInfo {
00101   struct KeyTy {
00102     ArrayRef<Type*> ETypes;
00103     bool isPacked;
00104     KeyTy(const ArrayRef<Type*>& E, bool P) :
00105       ETypes(E), isPacked(P) {}
00106     KeyTy(const StructType* ST) :
00107       ETypes(ArrayRef<Type*>(ST->element_begin(), ST->element_end())),
00108       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()),
00153       Params(makeArrayRef(FT->param_begin(), FT->param_end())),
00154       isVarArg(FT->isVarArg()) {}
00155     bool operator==(const KeyTy& that) const {
00156       if (ReturnType != that.ReturnType)
00157         return false;
00158       if (isVarArg != that.isVarArg)
00159         return false;
00160       if (Params != that.Params)
00161         return false;
00162       return true;
00163     }
00164     bool operator!=(const KeyTy& that) const {
00165       return !this->operator==(that);
00166     }
00167   };
00168   static inline FunctionType* getEmptyKey() {
00169     return DenseMapInfo<FunctionType*>::getEmptyKey();
00170   }
00171   static inline FunctionType* getTombstoneKey() {
00172     return DenseMapInfo<FunctionType*>::getTombstoneKey();
00173   }
00174   static unsigned getHashValue(const KeyTy& Key) {
00175     return hash_combine(Key.ReturnType,
00176                         hash_combine_range(Key.Params.begin(),
00177                                            Key.Params.end()),
00178                         Key.isVarArg);
00179   }
00180   static unsigned getHashValue(const FunctionType *FT) {
00181     return getHashValue(KeyTy(FT));
00182   }
00183   static bool isEqual(const KeyTy& LHS, const FunctionType *RHS) {
00184     if (RHS == getEmptyKey() || RHS == getTombstoneKey())
00185       return false;
00186     return LHS == KeyTy(RHS);
00187   }
00188   static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) {
00189     return LHS == RHS;
00190   }
00191 };
00192 
00193 // Provide a FoldingSetTrait::Equals specialization for MDNode that can use a
00194 // shortcut to avoid comparing all operands.
00195 template<> struct FoldingSetTrait<MDNode> : DefaultFoldingSetTrait<MDNode> {
00196   static bool Equals(const MDNode &X, const FoldingSetNodeID &ID,
00197                      unsigned IDHash, FoldingSetNodeID &TempID) {
00198     assert(!X.isNotUniqued() && "Non-uniqued MDNode in FoldingSet?");
00199     // First, check if the cached hashes match.  If they don't we can skip the
00200     // expensive operand walk.
00201     if (X.Hash != IDHash)
00202       return false;
00203 
00204     // If they match we have to compare the operands.
00205     X.Profile(TempID);
00206     return TempID == ID;
00207   }
00208   static unsigned ComputeHash(const MDNode &X, FoldingSetNodeID &) {
00209     return X.Hash; // Return cached hash.
00210   }
00211 };
00212 
00213 /// DebugRecVH - This is a CallbackVH used to keep the Scope -> index maps
00214 /// up to date as MDNodes mutate.  This class is implemented in DebugLoc.cpp.
00215 class DebugRecVH : public CallbackVH {
00216   /// Ctx - This is the LLVM Context being referenced.
00217   LLVMContextImpl *Ctx;
00218   
00219   /// Idx - The index into either ScopeRecordIdx or ScopeInlinedAtRecords that
00220   /// this reference lives in.  If this is zero, then it represents a
00221   /// non-canonical entry that has no DenseMap value.  This can happen due to
00222   /// RAUW.
00223   int Idx;
00224 public:
00225   DebugRecVH(MDNode *n, LLVMContextImpl *ctx, int idx)
00226     : CallbackVH(n), Ctx(ctx), Idx(idx) {}
00227   
00228   MDNode *get() const {
00229     return cast_or_null<MDNode>(getValPtr());
00230   }
00231 
00232   void deleted() override;
00233   void allUsesReplacedWith(Value *VNew) override;
00234 };
00235   
00236 class LLVMContextImpl {
00237 public:
00238   /// OwnedModules - The set of modules instantiated in this context, and which
00239   /// will be automatically deleted if this context is deleted.
00240   SmallPtrSet<Module*, 4> OwnedModules;
00241   
00242   LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
00243   void *InlineAsmDiagContext;
00244 
00245   LLVMContext::DiagnosticHandlerTy DiagnosticHandler;
00246   void *DiagnosticContext;
00247 
00248   LLVMContext::YieldCallbackTy YieldCallback;
00249   void *YieldOpaqueHandle;
00250 
00251   typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt *,
00252                    DenseMapAPIntKeyInfo> IntMapTy;
00253   IntMapTy IntConstants;
00254   
00255   typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*, 
00256                          DenseMapAPFloatKeyInfo> FPMapTy;
00257   FPMapTy FPConstants;
00258 
00259   FoldingSet<AttributeImpl> AttrsSet;
00260   FoldingSet<AttributeSetImpl> AttrsLists;
00261   FoldingSet<AttributeSetNode> AttrsSetNodes;
00262 
00263   StringMap<Value*> MDStringCache;
00264 
00265   FoldingSet<MDNode> MDNodeSet;
00266 
00267   // MDNodes may be uniqued or not uniqued.  When they're not uniqued, they
00268   // aren't in the MDNodeSet, but they're still shared between objects, so no
00269   // one object can destroy them.  This set allows us to at least destroy them
00270   // on Context destruction.
00271   SmallPtrSet<MDNode*, 1> NonUniquedMDNodes;
00272   
00273   DenseMap<Type*, ConstantAggregateZero*> CAZConstants;
00274 
00275   typedef ConstantUniqueMap<ConstantArray> ArrayConstantsTy;
00276   ArrayConstantsTy ArrayConstants;
00277   
00278   typedef ConstantUniqueMap<ConstantStruct> StructConstantsTy;
00279   StructConstantsTy StructConstants;
00280   
00281   typedef ConstantUniqueMap<ConstantVector> VectorConstantsTy;
00282   VectorConstantsTy VectorConstants;
00283   
00284   DenseMap<PointerType*, ConstantPointerNull*> CPNConstants;
00285 
00286   DenseMap<Type*, UndefValue*> UVConstants;
00287   
00288   StringMap<ConstantDataSequential*> CDSConstants;
00289 
00290   DenseMap<std::pair<const Function *, const BasicBlock *>, BlockAddress *>
00291     BlockAddresses;
00292   ConstantUniqueMap<ConstantExpr> ExprConstants;
00293 
00294   ConstantUniqueMap<InlineAsm> InlineAsms;
00295 
00296   ConstantInt *TheTrueVal;
00297   ConstantInt *TheFalseVal;
00298   
00299   LeakDetectorImpl<Value> LLVMObjects;
00300   
00301   // Basic type instances.
00302   Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy;
00303   Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy;
00304   IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty;
00305 
00306   
00307   /// TypeAllocator - All dynamically allocated types are allocated from this.
00308   /// They live forever until the context is torn down.
00309   BumpPtrAllocator TypeAllocator;
00310   
00311   DenseMap<unsigned, IntegerType*> IntegerTypes;
00312   
00313   typedef DenseMap<FunctionType*, bool, FunctionTypeKeyInfo> FunctionTypeMap;
00314   FunctionTypeMap FunctionTypes;
00315   typedef DenseMap<StructType*, bool, AnonStructTypeKeyInfo> StructTypeMap;
00316   StructTypeMap AnonStructTypes;
00317   StringMap<StructType*> NamedStructTypes;
00318   unsigned NamedStructTypesUniqueID;
00319     
00320   DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
00321   DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes;
00322   DenseMap<Type*, PointerType*> PointerTypes;  // Pointers in AddrSpace = 0
00323   DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;
00324 
00325 
00326   /// ValueHandles - This map keeps track of all of the value handles that are
00327   /// watching a Value*.  The Value::HasValueHandle bit is used to know
00328   /// whether or not a value has an entry in this map.
00329   typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
00330   ValueHandlesTy ValueHandles;
00331   
00332   /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
00333   StringMap<unsigned> CustomMDKindNames;
00334   
00335   typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
00336   typedef SmallVector<MDPairTy, 2> MDMapTy;
00337 
00338   /// MetadataStore - Collection of per-instruction metadata used in this
00339   /// context.
00340   DenseMap<const Instruction *, MDMapTy> MetadataStore;
00341   
00342   /// ScopeRecordIdx - This is the index in ScopeRecords for an MDNode scope
00343   /// entry with no "inlined at" element.
00344   DenseMap<MDNode*, int> ScopeRecordIdx;
00345   
00346   /// ScopeRecords - These are the actual mdnodes (in a value handle) for an
00347   /// index.  The ValueHandle ensures that ScopeRecordIdx stays up to date if
00348   /// the MDNode is RAUW'd.
00349   std::vector<DebugRecVH> ScopeRecords;
00350   
00351   /// ScopeInlinedAtIdx - This is the index in ScopeInlinedAtRecords for an
00352   /// scope/inlined-at pair.
00353   DenseMap<std::pair<MDNode*, MDNode*>, int> ScopeInlinedAtIdx;
00354   
00355   /// ScopeInlinedAtRecords - These are the actual mdnodes (in value handles)
00356   /// for an index.  The ValueHandle ensures that ScopeINlinedAtIdx stays up
00357   /// to date.
00358   std::vector<std::pair<DebugRecVH, DebugRecVH> > ScopeInlinedAtRecords;
00359 
00360   /// DiscriminatorTable - This table maps file:line locations to an
00361   /// integer representing the next DWARF path discriminator to assign to
00362   /// instructions in different blocks at the same location.
00363   DenseMap<std::pair<const char *, unsigned>, unsigned> DiscriminatorTable;
00364 
00365   /// IntrinsicIDCache - Cache of intrinsic name (string) to numeric ID mappings
00366   /// requested in this context
00367   typedef DenseMap<const Function*, unsigned> IntrinsicIDCacheTy;
00368   IntrinsicIDCacheTy IntrinsicIDCache;
00369 
00370   /// \brief Mapping from a function to its prefix data, which is stored as the
00371   /// operand of an unparented ReturnInst so that the prefix data has a Use.
00372   typedef DenseMap<const Function *, ReturnInst *> PrefixDataMapTy;
00373   PrefixDataMapTy PrefixDataMap;
00374 
00375   int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
00376   int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
00377   
00378   LLVMContextImpl(LLVMContext &C);
00379   ~LLVMContextImpl();
00380 };
00381 
00382 }
00383 
00384 #endif