1249259Sdim//===-- LLVMContextImpl.h - The LLVMContextImpl opaque class ----*- C++ -*-===// 2249259Sdim// 3249259Sdim// The LLVM Compiler Infrastructure 4249259Sdim// 5249259Sdim// This file is distributed under the University of Illinois Open Source 6249259Sdim// License. See LICENSE.TXT for details. 7249259Sdim// 8249259Sdim//===----------------------------------------------------------------------===// 9249259Sdim// 10249259Sdim// This file declares LLVMContextImpl, the opaque implementation 11249259Sdim// of LLVMContext. 12249259Sdim// 13249259Sdim//===----------------------------------------------------------------------===// 14249259Sdim 15249259Sdim#ifndef LLVM_LLVMCONTEXT_IMPL_H 16249259Sdim#define LLVM_LLVMCONTEXT_IMPL_H 17249259Sdim 18249259Sdim#include "AttributeImpl.h" 19249259Sdim#include "ConstantsContext.h" 20249259Sdim#include "LeaksContext.h" 21249259Sdim#include "llvm/ADT/APFloat.h" 22249259Sdim#include "llvm/ADT/APInt.h" 23249259Sdim#include "llvm/ADT/ArrayRef.h" 24249259Sdim#include "llvm/ADT/DenseMap.h" 25249259Sdim#include "llvm/ADT/FoldingSet.h" 26249259Sdim#include "llvm/ADT/Hashing.h" 27249259Sdim#include "llvm/ADT/SmallPtrSet.h" 28249259Sdim#include "llvm/ADT/StringMap.h" 29249259Sdim#include "llvm/IR/Constants.h" 30249259Sdim#include "llvm/IR/DerivedTypes.h" 31249259Sdim#include "llvm/IR/LLVMContext.h" 32249259Sdim#include "llvm/IR/Metadata.h" 33249259Sdim#include "llvm/Support/ValueHandle.h" 34249259Sdim#include <vector> 35249259Sdim 36249259Sdimnamespace llvm { 37249259Sdim 38249259Sdimclass ConstantInt; 39249259Sdimclass ConstantFP; 40249259Sdimclass LLVMContext; 41249259Sdimclass Type; 42249259Sdimclass Value; 43249259Sdim 44249259Sdimstruct DenseMapAPIntKeyInfo { 45249259Sdim struct KeyTy { 46249259Sdim APInt val; 47249259Sdim Type* type; 48249259Sdim KeyTy(const APInt& V, Type* Ty) : val(V), type(Ty) {} 49249259Sdim bool operator==(const KeyTy& that) const { 50249259Sdim return type == that.type && this->val == that.val; 51249259Sdim } 52249259Sdim bool operator!=(const KeyTy& that) const { 53249259Sdim return !this->operator==(that); 54249259Sdim } 55249259Sdim friend hash_code hash_value(const KeyTy &Key) { 56249259Sdim return hash_combine(Key.type, Key.val); 57249259Sdim } 58249259Sdim }; 59249259Sdim static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); } 60249259Sdim static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); } 61249259Sdim static unsigned getHashValue(const KeyTy &Key) { 62249259Sdim return static_cast<unsigned>(hash_value(Key)); 63249259Sdim } 64249259Sdim static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) { 65249259Sdim return LHS == RHS; 66249259Sdim } 67249259Sdim}; 68249259Sdim 69249259Sdimstruct DenseMapAPFloatKeyInfo { 70249259Sdim struct KeyTy { 71249259Sdim APFloat val; 72249259Sdim KeyTy(const APFloat& V) : val(V){} 73249259Sdim bool operator==(const KeyTy& that) const { 74249259Sdim return this->val.bitwiseIsEqual(that.val); 75249259Sdim } 76249259Sdim bool operator!=(const KeyTy& that) const { 77249259Sdim return !this->operator==(that); 78249259Sdim } 79249259Sdim friend hash_code hash_value(const KeyTy &Key) { 80249259Sdim return hash_combine(Key.val); 81249259Sdim } 82249259Sdim }; 83249259Sdim static inline KeyTy getEmptyKey() { 84249259Sdim return KeyTy(APFloat(APFloat::Bogus,1)); 85249259Sdim } 86249259Sdim static inline KeyTy getTombstoneKey() { 87249259Sdim return KeyTy(APFloat(APFloat::Bogus,2)); 88249259Sdim } 89249259Sdim static unsigned getHashValue(const KeyTy &Key) { 90249259Sdim return static_cast<unsigned>(hash_value(Key)); 91249259Sdim } 92249259Sdim static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) { 93249259Sdim return LHS == RHS; 94249259Sdim } 95249259Sdim}; 96249259Sdim 97249259Sdimstruct AnonStructTypeKeyInfo { 98249259Sdim struct KeyTy { 99249259Sdim ArrayRef<Type*> ETypes; 100249259Sdim bool isPacked; 101249259Sdim KeyTy(const ArrayRef<Type*>& E, bool P) : 102249259Sdim ETypes(E), isPacked(P) {} 103249259Sdim KeyTy(const StructType* ST) : 104249259Sdim ETypes(ArrayRef<Type*>(ST->element_begin(), ST->element_end())), 105249259Sdim isPacked(ST->isPacked()) {} 106249259Sdim bool operator==(const KeyTy& that) const { 107249259Sdim if (isPacked != that.isPacked) 108249259Sdim return false; 109249259Sdim if (ETypes != that.ETypes) 110249259Sdim return false; 111249259Sdim return true; 112249259Sdim } 113249259Sdim bool operator!=(const KeyTy& that) const { 114249259Sdim return !this->operator==(that); 115249259Sdim } 116249259Sdim }; 117249259Sdim static inline StructType* getEmptyKey() { 118249259Sdim return DenseMapInfo<StructType*>::getEmptyKey(); 119249259Sdim } 120249259Sdim static inline StructType* getTombstoneKey() { 121249259Sdim return DenseMapInfo<StructType*>::getTombstoneKey(); 122249259Sdim } 123249259Sdim static unsigned getHashValue(const KeyTy& Key) { 124249259Sdim return hash_combine(hash_combine_range(Key.ETypes.begin(), 125249259Sdim Key.ETypes.end()), 126249259Sdim Key.isPacked); 127249259Sdim } 128249259Sdim static unsigned getHashValue(const StructType *ST) { 129249259Sdim return getHashValue(KeyTy(ST)); 130249259Sdim } 131249259Sdim static bool isEqual(const KeyTy& LHS, const StructType *RHS) { 132249259Sdim if (RHS == getEmptyKey() || RHS == getTombstoneKey()) 133249259Sdim return false; 134249259Sdim return LHS == KeyTy(RHS); 135249259Sdim } 136249259Sdim static bool isEqual(const StructType *LHS, const StructType *RHS) { 137249259Sdim return LHS == RHS; 138249259Sdim } 139249259Sdim}; 140249259Sdim 141249259Sdimstruct FunctionTypeKeyInfo { 142249259Sdim struct KeyTy { 143249259Sdim const Type *ReturnType; 144249259Sdim ArrayRef<Type*> Params; 145249259Sdim bool isVarArg; 146249259Sdim KeyTy(const Type* R, const ArrayRef<Type*>& P, bool V) : 147249259Sdim ReturnType(R), Params(P), isVarArg(V) {} 148249259Sdim KeyTy(const FunctionType* FT) : 149249259Sdim ReturnType(FT->getReturnType()), 150249259Sdim Params(ArrayRef<Type*>(FT->param_begin(), FT->param_end())), 151249259Sdim isVarArg(FT->isVarArg()) {} 152249259Sdim bool operator==(const KeyTy& that) const { 153249259Sdim if (ReturnType != that.ReturnType) 154249259Sdim return false; 155249259Sdim if (isVarArg != that.isVarArg) 156249259Sdim return false; 157249259Sdim if (Params != that.Params) 158249259Sdim return false; 159249259Sdim return true; 160249259Sdim } 161249259Sdim bool operator!=(const KeyTy& that) const { 162249259Sdim return !this->operator==(that); 163249259Sdim } 164249259Sdim }; 165249259Sdim static inline FunctionType* getEmptyKey() { 166249259Sdim return DenseMapInfo<FunctionType*>::getEmptyKey(); 167249259Sdim } 168249259Sdim static inline FunctionType* getTombstoneKey() { 169249259Sdim return DenseMapInfo<FunctionType*>::getTombstoneKey(); 170249259Sdim } 171249259Sdim static unsigned getHashValue(const KeyTy& Key) { 172249259Sdim return hash_combine(Key.ReturnType, 173249259Sdim hash_combine_range(Key.Params.begin(), 174249259Sdim Key.Params.end()), 175249259Sdim Key.isVarArg); 176249259Sdim } 177249259Sdim static unsigned getHashValue(const FunctionType *FT) { 178249259Sdim return getHashValue(KeyTy(FT)); 179249259Sdim } 180249259Sdim static bool isEqual(const KeyTy& LHS, const FunctionType *RHS) { 181249259Sdim if (RHS == getEmptyKey() || RHS == getTombstoneKey()) 182249259Sdim return false; 183249259Sdim return LHS == KeyTy(RHS); 184249259Sdim } 185249259Sdim static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) { 186249259Sdim return LHS == RHS; 187249259Sdim } 188249259Sdim}; 189249259Sdim 190249259Sdim// Provide a FoldingSetTrait::Equals specialization for MDNode that can use a 191249259Sdim// shortcut to avoid comparing all operands. 192249259Sdimtemplate<> struct FoldingSetTrait<MDNode> : DefaultFoldingSetTrait<MDNode> { 193249259Sdim static bool Equals(const MDNode &X, const FoldingSetNodeID &ID, 194249259Sdim unsigned IDHash, FoldingSetNodeID &TempID) { 195249259Sdim assert(!X.isNotUniqued() && "Non-uniqued MDNode in FoldingSet?"); 196249259Sdim // First, check if the cached hashes match. If they don't we can skip the 197249259Sdim // expensive operand walk. 198249259Sdim if (X.Hash != IDHash) 199249259Sdim return false; 200249259Sdim 201249259Sdim // If they match we have to compare the operands. 202249259Sdim X.Profile(TempID); 203249259Sdim return TempID == ID; 204249259Sdim } 205249259Sdim static unsigned ComputeHash(const MDNode &X, FoldingSetNodeID &) { 206249259Sdim return X.Hash; // Return cached hash. 207249259Sdim } 208249259Sdim}; 209249259Sdim 210249259Sdim/// DebugRecVH - This is a CallbackVH used to keep the Scope -> index maps 211249259Sdim/// up to date as MDNodes mutate. This class is implemented in DebugLoc.cpp. 212249259Sdimclass DebugRecVH : public CallbackVH { 213249259Sdim /// Ctx - This is the LLVM Context being referenced. 214249259Sdim LLVMContextImpl *Ctx; 215249259Sdim 216249259Sdim /// Idx - The index into either ScopeRecordIdx or ScopeInlinedAtRecords that 217249259Sdim /// this reference lives in. If this is zero, then it represents a 218249259Sdim /// non-canonical entry that has no DenseMap value. This can happen due to 219249259Sdim /// RAUW. 220249259Sdim int Idx; 221249259Sdimpublic: 222249259Sdim DebugRecVH(MDNode *n, LLVMContextImpl *ctx, int idx) 223249259Sdim : CallbackVH(n), Ctx(ctx), Idx(idx) {} 224249259Sdim 225249259Sdim MDNode *get() const { 226249259Sdim return cast_or_null<MDNode>(getValPtr()); 227249259Sdim } 228249259Sdim 229249259Sdim virtual void deleted(); 230249259Sdim virtual void allUsesReplacedWith(Value *VNew); 231249259Sdim}; 232249259Sdim 233249259Sdimclass LLVMContextImpl { 234249259Sdimpublic: 235249259Sdim /// OwnedModules - The set of modules instantiated in this context, and which 236249259Sdim /// will be automatically deleted if this context is deleted. 237249259Sdim SmallPtrSet<Module*, 4> OwnedModules; 238249259Sdim 239249259Sdim LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler; 240249259Sdim void *InlineAsmDiagContext; 241249259Sdim 242249259Sdim typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*, 243249259Sdim DenseMapAPIntKeyInfo> IntMapTy; 244249259Sdim IntMapTy IntConstants; 245249259Sdim 246249259Sdim typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*, 247249259Sdim DenseMapAPFloatKeyInfo> FPMapTy; 248249259Sdim FPMapTy FPConstants; 249249259Sdim 250249259Sdim FoldingSet<AttributeImpl> AttrsSet; 251249259Sdim FoldingSet<AttributeSetImpl> AttrsLists; 252249259Sdim FoldingSet<AttributeSetNode> AttrsSetNodes; 253249259Sdim 254249259Sdim StringMap<Value*> MDStringCache; 255249259Sdim 256249259Sdim FoldingSet<MDNode> MDNodeSet; 257249259Sdim 258249259Sdim // MDNodes may be uniqued or not uniqued. When they're not uniqued, they 259249259Sdim // aren't in the MDNodeSet, but they're still shared between objects, so no 260249259Sdim // one object can destroy them. This set allows us to at least destroy them 261249259Sdim // on Context destruction. 262249259Sdim SmallPtrSet<MDNode*, 1> NonUniquedMDNodes; 263249259Sdim 264249259Sdim DenseMap<Type*, ConstantAggregateZero*> CAZConstants; 265249259Sdim 266249259Sdim typedef ConstantAggrUniqueMap<ArrayType, ConstantArray> ArrayConstantsTy; 267249259Sdim ArrayConstantsTy ArrayConstants; 268249259Sdim 269249259Sdim typedef ConstantAggrUniqueMap<StructType, ConstantStruct> StructConstantsTy; 270249259Sdim StructConstantsTy StructConstants; 271249259Sdim 272249259Sdim typedef ConstantAggrUniqueMap<VectorType, ConstantVector> VectorConstantsTy; 273249259Sdim VectorConstantsTy VectorConstants; 274249259Sdim 275249259Sdim DenseMap<PointerType*, ConstantPointerNull*> CPNConstants; 276249259Sdim 277249259Sdim DenseMap<Type*, UndefValue*> UVConstants; 278249259Sdim 279249259Sdim StringMap<ConstantDataSequential*> CDSConstants; 280249259Sdim 281249259Sdim 282249259Sdim DenseMap<std::pair<Function*, BasicBlock*> , BlockAddress*> BlockAddresses; 283249259Sdim ConstantUniqueMap<ExprMapKeyType, const ExprMapKeyType&, Type, ConstantExpr> 284249259Sdim ExprConstants; 285249259Sdim 286249259Sdim ConstantUniqueMap<InlineAsmKeyType, const InlineAsmKeyType&, PointerType, 287249259Sdim InlineAsm> InlineAsms; 288249259Sdim 289249259Sdim ConstantInt *TheTrueVal; 290249259Sdim ConstantInt *TheFalseVal; 291249259Sdim 292249259Sdim LeakDetectorImpl<Value> LLVMObjects; 293249259Sdim 294249259Sdim // Basic type instances. 295249259Sdim Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy; 296249259Sdim Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy; 297249259Sdim IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty; 298249259Sdim 299249259Sdim 300249259Sdim /// TypeAllocator - All dynamically allocated types are allocated from this. 301249259Sdim /// They live forever until the context is torn down. 302249259Sdim BumpPtrAllocator TypeAllocator; 303249259Sdim 304249259Sdim DenseMap<unsigned, IntegerType*> IntegerTypes; 305249259Sdim 306249259Sdim typedef DenseMap<FunctionType*, bool, FunctionTypeKeyInfo> FunctionTypeMap; 307249259Sdim FunctionTypeMap FunctionTypes; 308249259Sdim typedef DenseMap<StructType*, bool, AnonStructTypeKeyInfo> StructTypeMap; 309249259Sdim StructTypeMap AnonStructTypes; 310249259Sdim StringMap<StructType*> NamedStructTypes; 311249259Sdim unsigned NamedStructTypesUniqueID; 312249259Sdim 313249259Sdim DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes; 314249259Sdim DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes; 315249259Sdim DenseMap<Type*, PointerType*> PointerTypes; // Pointers in AddrSpace = 0 316249259Sdim DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes; 317249259Sdim 318249259Sdim 319249259Sdim /// ValueHandles - This map keeps track of all of the value handles that are 320249259Sdim /// watching a Value*. The Value::HasValueHandle bit is used to know 321249259Sdim /// whether or not a value has an entry in this map. 322249259Sdim typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy; 323249259Sdim ValueHandlesTy ValueHandles; 324249259Sdim 325249259Sdim /// CustomMDKindNames - Map to hold the metadata string to ID mapping. 326249259Sdim StringMap<unsigned> CustomMDKindNames; 327249259Sdim 328249259Sdim typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy; 329249259Sdim typedef SmallVector<MDPairTy, 2> MDMapTy; 330249259Sdim 331249259Sdim /// MetadataStore - Collection of per-instruction metadata used in this 332249259Sdim /// context. 333249259Sdim DenseMap<const Instruction *, MDMapTy> MetadataStore; 334249259Sdim 335249259Sdim /// ScopeRecordIdx - This is the index in ScopeRecords for an MDNode scope 336249259Sdim /// entry with no "inlined at" element. 337249259Sdim DenseMap<MDNode*, int> ScopeRecordIdx; 338249259Sdim 339249259Sdim /// ScopeRecords - These are the actual mdnodes (in a value handle) for an 340249259Sdim /// index. The ValueHandle ensures that ScopeRecordIdx stays up to date if 341249259Sdim /// the MDNode is RAUW'd. 342249259Sdim std::vector<DebugRecVH> ScopeRecords; 343249259Sdim 344249259Sdim /// ScopeInlinedAtIdx - This is the index in ScopeInlinedAtRecords for an 345249259Sdim /// scope/inlined-at pair. 346249259Sdim DenseMap<std::pair<MDNode*, MDNode*>, int> ScopeInlinedAtIdx; 347249259Sdim 348249259Sdim /// ScopeInlinedAtRecords - These are the actual mdnodes (in value handles) 349249259Sdim /// for an index. The ValueHandle ensures that ScopeINlinedAtIdx stays up 350249259Sdim /// to date. 351249259Sdim std::vector<std::pair<DebugRecVH, DebugRecVH> > ScopeInlinedAtRecords; 352249259Sdim 353249259Sdim /// IntrinsicIDCache - Cache of intrinsic name (string) to numeric ID mappings 354249259Sdim /// requested in this context 355249259Sdim typedef DenseMap<const Function*, unsigned> IntrinsicIDCacheTy; 356249259Sdim IntrinsicIDCacheTy IntrinsicIDCache; 357249259Sdim 358263508Sdim /// \brief Mapping from a function to its prefix data, which is stored as the 359263508Sdim /// operand of an unparented ReturnInst so that the prefix data has a Use. 360263508Sdim typedef DenseMap<const Function *, ReturnInst *> PrefixDataMapTy; 361263508Sdim PrefixDataMapTy PrefixDataMap; 362263508Sdim 363249259Sdim int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx); 364249259Sdim int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx); 365249259Sdim 366249259Sdim LLVMContextImpl(LLVMContext &C); 367249259Sdim ~LLVMContextImpl(); 368249259Sdim}; 369249259Sdim 370249259Sdim} 371249259Sdim 372249259Sdim#endif 373