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