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