Home | History | Annotate | Download | only in IR
      1 //===-- LLVMContextImpl.h - The LLVMContextImpl opaque class ----*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 //  This file declares LLVMContextImpl, the opaque implementation
     11 //  of LLVMContext.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #ifndef LLVM_LLVMCONTEXT_IMPL_H
     16 #define LLVM_LLVMCONTEXT_IMPL_H
     17 
     18 #include "AttributeImpl.h"
     19 #include "ConstantsContext.h"
     20 #include "LeaksContext.h"
     21 #include "llvm/ADT/APFloat.h"
     22 #include "llvm/ADT/APInt.h"
     23 #include "llvm/ADT/ArrayRef.h"
     24 #include "llvm/ADT/DenseMap.h"
     25 #include "llvm/ADT/FoldingSet.h"
     26 #include "llvm/ADT/Hashing.h"
     27 #include "llvm/ADT/SmallPtrSet.h"
     28 #include "llvm/ADT/StringMap.h"
     29 #include "llvm/IR/Constants.h"
     30 #include "llvm/IR/DerivedTypes.h"
     31 #include "llvm/IR/LLVMContext.h"
     32 #include "llvm/IR/Metadata.h"
     33 #include "llvm/Support/ValueHandle.h"
     34 #include <vector>
     35 
     36 namespace llvm {
     37 
     38 class ConstantInt;
     39 class ConstantFP;
     40 class LLVMContext;
     41 class Type;
     42 class Value;
     43 
     44 struct DenseMapAPIntKeyInfo {
     45   struct KeyTy {
     46     APInt val;
     47     Type* type;
     48     KeyTy(const APInt& V, Type* Ty) : val(V), type(Ty) {}
     49     bool operator==(const KeyTy& that) const {
     50       return type == that.type && this->val == that.val;
     51     }
     52     bool operator!=(const KeyTy& that) const {
     53       return !this->operator==(that);
     54     }
     55     friend hash_code hash_value(const KeyTy &Key) {
     56       return hash_combine(Key.type, Key.val);
     57     }
     58   };
     59   static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
     60   static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
     61   static unsigned getHashValue(const KeyTy &Key) {
     62     return static_cast<unsigned>(hash_value(Key));
     63   }
     64   static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
     65     return LHS == RHS;
     66   }
     67 };
     68 
     69 struct DenseMapAPFloatKeyInfo {
     70   struct KeyTy {
     71     APFloat val;
     72     KeyTy(const APFloat& V) : val(V){}
     73     bool operator==(const KeyTy& that) const {
     74       return this->val.bitwiseIsEqual(that.val);
     75     }
     76     bool operator!=(const KeyTy& that) const {
     77       return !this->operator==(that);
     78     }
     79     friend hash_code hash_value(const KeyTy &Key) {
     80       return hash_combine(Key.val);
     81     }
     82   };
     83   static inline KeyTy getEmptyKey() {
     84     return KeyTy(APFloat(APFloat::Bogus,1));
     85   }
     86   static inline KeyTy getTombstoneKey() {
     87     return KeyTy(APFloat(APFloat::Bogus,2));
     88   }
     89   static unsigned getHashValue(const KeyTy &Key) {
     90     return static_cast<unsigned>(hash_value(Key));
     91   }
     92   static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
     93     return LHS == RHS;
     94   }
     95 };
     96 
     97 struct AnonStructTypeKeyInfo {
     98   struct KeyTy {
     99     ArrayRef<Type*> ETypes;
    100     bool isPacked;
    101     KeyTy(const ArrayRef<Type*>& E, bool P) :
    102       ETypes(E), isPacked(P) {}
    103     KeyTy(const StructType* ST) :
    104       ETypes(ArrayRef<Type*>(ST->element_begin(), ST->element_end())),
    105       isPacked(ST->isPacked()) {}
    106     bool operator==(const KeyTy& that) const {
    107       if (isPacked != that.isPacked)
    108         return false;
    109       if (ETypes != that.ETypes)
    110         return false;
    111       return true;
    112     }
    113     bool operator!=(const KeyTy& that) const {
    114       return !this->operator==(that);
    115     }
    116   };
    117   static inline StructType* getEmptyKey() {
    118     return DenseMapInfo<StructType*>::getEmptyKey();
    119   }
    120   static inline StructType* getTombstoneKey() {
    121     return DenseMapInfo<StructType*>::getTombstoneKey();
    122   }
    123   static unsigned getHashValue(const KeyTy& Key) {
    124     return hash_combine(hash_combine_range(Key.ETypes.begin(),
    125                                            Key.ETypes.end()),
    126                         Key.isPacked);
    127   }
    128   static unsigned getHashValue(const StructType *ST) {
    129     return getHashValue(KeyTy(ST));
    130   }
    131   static bool isEqual(const KeyTy& LHS, const StructType *RHS) {
    132     if (RHS == getEmptyKey() || RHS == getTombstoneKey())
    133       return false;
    134     return LHS == KeyTy(RHS);
    135   }
    136   static bool isEqual(const StructType *LHS, const StructType *RHS) {
    137     return LHS == RHS;
    138   }
    139 };
    140 
    141 struct FunctionTypeKeyInfo {
    142   struct KeyTy {
    143     const Type *ReturnType;
    144     ArrayRef<Type*> Params;
    145     bool isVarArg;
    146     KeyTy(const Type* R, const ArrayRef<Type*>& P, bool V) :
    147       ReturnType(R), Params(P), isVarArg(V) {}
    148     KeyTy(const FunctionType* FT) :
    149       ReturnType(FT->getReturnType()),
    150       Params(ArrayRef<Type*>(FT->param_begin(), FT->param_end())),
    151       isVarArg(FT->isVarArg()) {}
    152     bool operator==(const KeyTy& that) const {
    153       if (ReturnType != that.ReturnType)
    154         return false;
    155       if (isVarArg != that.isVarArg)
    156         return false;
    157       if (Params != that.Params)
    158         return false;
    159       return true;
    160     }
    161     bool operator!=(const KeyTy& that) const {
    162       return !this->operator==(that);
    163     }
    164   };
    165   static inline FunctionType* getEmptyKey() {
    166     return DenseMapInfo<FunctionType*>::getEmptyKey();
    167   }
    168   static inline FunctionType* getTombstoneKey() {
    169     return DenseMapInfo<FunctionType*>::getTombstoneKey();
    170   }
    171   static unsigned getHashValue(const KeyTy& Key) {
    172     return hash_combine(Key.ReturnType,
    173                         hash_combine_range(Key.Params.begin(),
    174                                            Key.Params.end()),
    175                         Key.isVarArg);
    176   }
    177   static unsigned getHashValue(const FunctionType *FT) {
    178     return getHashValue(KeyTy(FT));
    179   }
    180   static bool isEqual(const KeyTy& LHS, const FunctionType *RHS) {
    181     if (RHS == getEmptyKey() || RHS == getTombstoneKey())
    182       return false;
    183     return LHS == KeyTy(RHS);
    184   }
    185   static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) {
    186     return LHS == RHS;
    187   }
    188 };
    189 
    190 // Provide a FoldingSetTrait::Equals specialization for MDNode that can use a
    191 // shortcut to avoid comparing all operands.
    192 template<> struct FoldingSetTrait<MDNode> : DefaultFoldingSetTrait<MDNode> {
    193   static bool Equals(const MDNode &X, const FoldingSetNodeID &ID,
    194                      unsigned IDHash, FoldingSetNodeID &TempID) {
    195     assert(!X.isNotUniqued() && "Non-uniqued MDNode in FoldingSet?");
    196     // First, check if the cached hashes match.  If they don't we can skip the
    197     // expensive operand walk.
    198     if (X.Hash != IDHash)
    199       return false;
    200 
    201     // If they match we have to compare the operands.
    202     X.Profile(TempID);
    203     return TempID == ID;
    204   }
    205   static unsigned ComputeHash(const MDNode &X, FoldingSetNodeID &) {
    206     return X.Hash; // Return cached hash.
    207   }
    208 };
    209 
    210 /// DebugRecVH - This is a CallbackVH used to keep the Scope -> index maps
    211 /// up to date as MDNodes mutate.  This class is implemented in DebugLoc.cpp.
    212 class DebugRecVH : public CallbackVH {
    213   /// Ctx - This is the LLVM Context being referenced.
    214   LLVMContextImpl *Ctx;
    215 
    216   /// Idx - The index into either ScopeRecordIdx or ScopeInlinedAtRecords that
    217   /// this reference lives in.  If this is zero, then it represents a
    218   /// non-canonical entry that has no DenseMap value.  This can happen due to
    219   /// RAUW.
    220   int Idx;
    221 public:
    222   DebugRecVH(MDNode *n, LLVMContextImpl *ctx, int idx)
    223     : CallbackVH(n), Ctx(ctx), Idx(idx) {}
    224 
    225   MDNode *get() const {
    226     return cast_or_null<MDNode>(getValPtr());
    227   }
    228 
    229   virtual void deleted();
    230   virtual void allUsesReplacedWith(Value *VNew);
    231 };
    232 
    233 class LLVMContextImpl {
    234 public:
    235   /// OwnedModules - The set of modules instantiated in this context, and which
    236   /// will be automatically deleted if this context is deleted.
    237   SmallPtrSet<Module*, 4> OwnedModules;
    238 
    239   LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
    240   void *InlineAsmDiagContext;
    241 
    242   typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
    243                          DenseMapAPIntKeyInfo> IntMapTy;
    244   IntMapTy IntConstants;
    245 
    246   typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
    247                          DenseMapAPFloatKeyInfo> FPMapTy;
    248   FPMapTy FPConstants;
    249 
    250   FoldingSet<AttributeImpl> AttrsSet;
    251   FoldingSet<AttributeSetImpl> AttrsLists;
    252   FoldingSet<AttributeSetNode> AttrsSetNodes;
    253 
    254   StringMap<Value*> MDStringCache;
    255 
    256   FoldingSet<MDNode> MDNodeSet;
    257 
    258   // MDNodes may be uniqued or not uniqued.  When they're not uniqued, they
    259   // aren't in the MDNodeSet, but they're still shared between objects, so no
    260   // one object can destroy them.  This set allows us to at least destroy them
    261   // on Context destruction.
    262   SmallPtrSet<MDNode*, 1> NonUniquedMDNodes;
    263 
    264   DenseMap<Type*, ConstantAggregateZero*> CAZConstants;
    265 
    266   typedef ConstantAggrUniqueMap<ArrayType, ConstantArray> ArrayConstantsTy;
    267   ArrayConstantsTy ArrayConstants;
    268 
    269   typedef ConstantAggrUniqueMap<StructType, ConstantStruct> StructConstantsTy;
    270   StructConstantsTy StructConstants;
    271 
    272   typedef ConstantAggrUniqueMap<VectorType, ConstantVector> VectorConstantsTy;
    273   VectorConstantsTy VectorConstants;
    274 
    275   DenseMap<PointerType*, ConstantPointerNull*> CPNConstants;
    276 
    277   DenseMap<Type*, UndefValue*> UVConstants;
    278 
    279   StringMap<ConstantDataSequential*> CDSConstants;
    280 
    281 
    282   DenseMap<std::pair<Function*, BasicBlock*> , BlockAddress*> BlockAddresses;
    283   ConstantUniqueMap<ExprMapKeyType, const ExprMapKeyType&, Type, ConstantExpr>
    284     ExprConstants;
    285 
    286   ConstantUniqueMap<InlineAsmKeyType, const InlineAsmKeyType&, PointerType,
    287                     InlineAsm> InlineAsms;
    288 
    289   ConstantInt *TheTrueVal;
    290   ConstantInt *TheFalseVal;
    291 
    292   LeakDetectorImpl<Value> LLVMObjects;
    293 
    294   // Basic type instances.
    295   Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy;
    296   Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy;
    297   IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty;
    298 
    299 
    300   /// TypeAllocator - All dynamically allocated types are allocated from this.
    301   /// They live forever until the context is torn down.
    302   BumpPtrAllocator TypeAllocator;
    303 
    304   DenseMap<unsigned, IntegerType*> IntegerTypes;
    305 
    306   typedef DenseMap<FunctionType*, bool, FunctionTypeKeyInfo> FunctionTypeMap;
    307   FunctionTypeMap FunctionTypes;
    308   typedef DenseMap<StructType*, bool, AnonStructTypeKeyInfo> StructTypeMap;
    309   StructTypeMap AnonStructTypes;
    310   StringMap<StructType*> NamedStructTypes;
    311   unsigned NamedStructTypesUniqueID;
    312 
    313   DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
    314   DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes;
    315   DenseMap<Type*, PointerType*> PointerTypes;  // Pointers in AddrSpace = 0
    316   DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;
    317 
    318 
    319   /// ValueHandles - This map keeps track of all of the value handles that are
    320   /// watching a Value*.  The Value::HasValueHandle bit is used to know
    321   /// whether or not a value has an entry in this map.
    322   typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
    323   ValueHandlesTy ValueHandles;
    324 
    325   /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
    326   StringMap<unsigned> CustomMDKindNames;
    327 
    328   typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
    329   typedef SmallVector<MDPairTy, 2> MDMapTy;
    330 
    331   /// MetadataStore - Collection of per-instruction metadata used in this
    332   /// context.
    333   DenseMap<const Instruction *, MDMapTy> MetadataStore;
    334 
    335   /// ScopeRecordIdx - This is the index in ScopeRecords for an MDNode scope
    336   /// entry with no "inlined at" element.
    337   DenseMap<MDNode*, int> ScopeRecordIdx;
    338 
    339   /// ScopeRecords - These are the actual mdnodes (in a value handle) for an
    340   /// index.  The ValueHandle ensures that ScopeRecordIdx stays up to date if
    341   /// the MDNode is RAUW'd.
    342   std::vector<DebugRecVH> ScopeRecords;
    343 
    344   /// ScopeInlinedAtIdx - This is the index in ScopeInlinedAtRecords for an
    345   /// scope/inlined-at pair.
    346   DenseMap<std::pair<MDNode*, MDNode*>, int> ScopeInlinedAtIdx;
    347 
    348   /// ScopeInlinedAtRecords - These are the actual mdnodes (in value handles)
    349   /// for an index.  The ValueHandle ensures that ScopeINlinedAtIdx stays up
    350   /// to date.
    351   std::vector<std::pair<DebugRecVH, DebugRecVH> > ScopeInlinedAtRecords;
    352 
    353   /// IntrinsicIDCache - Cache of intrinsic name (string) to numeric ID mappings
    354   /// requested in this context
    355   typedef DenseMap<const Function*, unsigned> IntrinsicIDCacheTy;
    356   IntrinsicIDCacheTy IntrinsicIDCache;
    357 
    358   int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
    359   int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
    360 
    361   LLVMContextImpl(LLVMContext &C);
    362   ~LLVMContextImpl();
    363 };
    364 
    365 }
    366 
    367 #endif
    368