Home | History | Annotate | Download | only in VMCore
      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 "llvm/LLVMContext.h"
     19 #include "ConstantsContext.h"
     20 #include "LeaksContext.h"
     21 #include "llvm/Constants.h"
     22 #include "llvm/DerivedTypes.h"
     23 #include "llvm/Metadata.h"
     24 #include "llvm/Support/ValueHandle.h"
     25 #include "llvm/ADT/APFloat.h"
     26 #include "llvm/ADT/APInt.h"
     27 #include "llvm/ADT/ArrayRef.h"
     28 #include "llvm/ADT/DenseMap.h"
     29 #include "llvm/ADT/FoldingSet.h"
     30 #include "llvm/ADT/SmallPtrSet.h"
     31 #include "llvm/ADT/StringMap.h"
     32 #include <vector>
     33 
     34 namespace llvm {
     35 
     36 class ConstantInt;
     37 class ConstantFP;
     38 class LLVMContext;
     39 class Type;
     40 class Value;
     41 
     42 struct DenseMapAPIntKeyInfo {
     43   struct KeyTy {
     44     APInt val;
     45     Type* type;
     46     KeyTy(const APInt& V, Type* Ty) : val(V), type(Ty) {}
     47     KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
     48     bool operator==(const KeyTy& that) const {
     49       return type == that.type && this->val == that.val;
     50     }
     51     bool operator!=(const KeyTy& that) const {
     52       return !this->operator==(that);
     53     }
     54   };
     55   static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
     56   static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
     57   static unsigned getHashValue(const KeyTy &Key) {
     58     return DenseMapInfo<void*>::getHashValue(Key.type) ^
     59       Key.val.getHashValue();
     60   }
     61   static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
     62     return LHS == RHS;
     63   }
     64 };
     65 
     66 struct DenseMapAPFloatKeyInfo {
     67   struct KeyTy {
     68     APFloat val;
     69     KeyTy(const APFloat& V) : val(V){}
     70     KeyTy(const KeyTy& that) : val(that.val) {}
     71     bool operator==(const KeyTy& that) const {
     72       return this->val.bitwiseIsEqual(that.val);
     73     }
     74     bool operator!=(const KeyTy& that) const {
     75       return !this->operator==(that);
     76     }
     77   };
     78   static inline KeyTy getEmptyKey() {
     79     return KeyTy(APFloat(APFloat::Bogus,1));
     80   }
     81   static inline KeyTy getTombstoneKey() {
     82     return KeyTy(APFloat(APFloat::Bogus,2));
     83   }
     84   static unsigned getHashValue(const KeyTy &Key) {
     85     return Key.val.getHashValue();
     86   }
     87   static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
     88     return LHS == RHS;
     89   }
     90 };
     91 
     92 /// DebugRecVH - This is a CallbackVH used to keep the Scope -> index maps
     93 /// up to date as MDNodes mutate.  This class is implemented in DebugLoc.cpp.
     94 class DebugRecVH : public CallbackVH {
     95   /// Ctx - This is the LLVM Context being referenced.
     96   LLVMContextImpl *Ctx;
     97 
     98   /// Idx - The index into either ScopeRecordIdx or ScopeInlinedAtRecords that
     99   /// this reference lives in.  If this is zero, then it represents a
    100   /// non-canonical entry that has no DenseMap value.  This can happen due to
    101   /// RAUW.
    102   int Idx;
    103 public:
    104   DebugRecVH(MDNode *n, LLVMContextImpl *ctx, int idx)
    105     : CallbackVH(n), Ctx(ctx), Idx(idx) {}
    106 
    107   MDNode *get() const {
    108     return cast_or_null<MDNode>(getValPtr());
    109   }
    110 
    111   virtual void deleted();
    112   virtual void allUsesReplacedWith(Value *VNew);
    113 };
    114 
    115 class LLVMContextImpl {
    116 public:
    117   /// OwnedModules - The set of modules instantiated in this context, and which
    118   /// will be automatically deleted if this context is deleted.
    119   SmallPtrSet<Module*, 4> OwnedModules;
    120 
    121   LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
    122   void *InlineAsmDiagContext;
    123 
    124   typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
    125                          DenseMapAPIntKeyInfo> IntMapTy;
    126   IntMapTy IntConstants;
    127 
    128   typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
    129                          DenseMapAPFloatKeyInfo> FPMapTy;
    130   FPMapTy FPConstants;
    131 
    132   StringMap<MDString*> MDStringCache;
    133 
    134   FoldingSet<MDNode> MDNodeSet;
    135   // MDNodes may be uniqued or not uniqued.  When they're not uniqued, they
    136   // aren't in the MDNodeSet, but they're still shared between objects, so no
    137   // one object can destroy them.  This set allows us to at least destroy them
    138   // on Context destruction.
    139   SmallPtrSet<MDNode*, 1> NonUniquedMDNodes;
    140 
    141   ConstantUniqueMap<char, char, Type, ConstantAggregateZero> AggZeroConstants;
    142 
    143   typedef ConstantUniqueMap<std::vector<Constant*>, ArrayRef<Constant*>,
    144     ArrayType, ConstantArray, true /*largekey*/> ArrayConstantsTy;
    145   ArrayConstantsTy ArrayConstants;
    146 
    147   typedef ConstantUniqueMap<std::vector<Constant*>, ArrayRef<Constant*>,
    148     StructType, ConstantStruct, true /*largekey*/> StructConstantsTy;
    149   StructConstantsTy StructConstants;
    150 
    151   typedef ConstantUniqueMap<std::vector<Constant*>, ArrayRef<Constant*>,
    152                             VectorType, ConstantVector> VectorConstantsTy;
    153   VectorConstantsTy VectorConstants;
    154 
    155   ConstantUniqueMap<char, char, PointerType, ConstantPointerNull>
    156     NullPtrConstants;
    157   ConstantUniqueMap<char, char, Type, UndefValue> UndefValueConstants;
    158 
    159   DenseMap<std::pair<Function*, BasicBlock*> , BlockAddress*> BlockAddresses;
    160   ConstantUniqueMap<ExprMapKeyType, const ExprMapKeyType&, Type, ConstantExpr>
    161     ExprConstants;
    162 
    163   ConstantUniqueMap<InlineAsmKeyType, const InlineAsmKeyType&, PointerType,
    164                     InlineAsm> InlineAsms;
    165 
    166   ConstantInt *TheTrueVal;
    167   ConstantInt *TheFalseVal;
    168 
    169   LeakDetectorImpl<Value> LLVMObjects;
    170 
    171   // Basic type instances.
    172   Type VoidTy, LabelTy, FloatTy, DoubleTy, MetadataTy;
    173   Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy;
    174   IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty;
    175 
    176 
    177   /// TypeAllocator - All dynamically allocated types are allocated from this.
    178   /// They live forever until the context is torn down.
    179   BumpPtrAllocator TypeAllocator;
    180 
    181   DenseMap<unsigned, IntegerType*> IntegerTypes;
    182 
    183   // TODO: Optimize FunctionTypes/AnonStructTypes!
    184   std::map<std::vector<Type*>, FunctionType*> FunctionTypes;
    185   std::map<std::vector<Type*>, StructType*> AnonStructTypes;
    186   StringMap<StructType*> NamedStructTypes;
    187   unsigned NamedStructTypesUniqueID;
    188 
    189   DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
    190   DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes;
    191   DenseMap<Type*, PointerType*> PointerTypes;  // Pointers in AddrSpace = 0
    192   DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;
    193 
    194 
    195   /// ValueHandles - This map keeps track of all of the value handles that are
    196   /// watching a Value*.  The Value::HasValueHandle bit is used to know
    197   // whether or not a value has an entry in this map.
    198   typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
    199   ValueHandlesTy ValueHandles;
    200 
    201   /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
    202   StringMap<unsigned> CustomMDKindNames;
    203 
    204   typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
    205   typedef SmallVector<MDPairTy, 2> MDMapTy;
    206 
    207   /// MetadataStore - Collection of per-instruction metadata used in this
    208   /// context.
    209   DenseMap<const Instruction *, MDMapTy> MetadataStore;
    210 
    211   /// ScopeRecordIdx - This is the index in ScopeRecords for an MDNode scope
    212   /// entry with no "inlined at" element.
    213   DenseMap<MDNode*, int> ScopeRecordIdx;
    214 
    215   /// ScopeRecords - These are the actual mdnodes (in a value handle) for an
    216   /// index.  The ValueHandle ensures that ScopeRecordIdx stays up to date if
    217   /// the MDNode is RAUW'd.
    218   std::vector<DebugRecVH> ScopeRecords;
    219 
    220   /// ScopeInlinedAtIdx - This is the index in ScopeInlinedAtRecords for an
    221   /// scope/inlined-at pair.
    222   DenseMap<std::pair<MDNode*, MDNode*>, int> ScopeInlinedAtIdx;
    223 
    224   /// ScopeInlinedAtRecords - These are the actual mdnodes (in value handles)
    225   /// for an index.  The ValueHandle ensures that ScopeINlinedAtIdx stays up
    226   /// to date.
    227   std::vector<std::pair<DebugRecVH, DebugRecVH> > ScopeInlinedAtRecords;
    228 
    229   int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
    230   int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
    231 
    232   LLVMContextImpl(LLVMContext &C);
    233   ~LLVMContextImpl();
    234 };
    235 
    236 }
    237 
    238 #endif
    239