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_LIB_IR_LLVMCONTEXTIMPL_H 16 #define LLVM_LIB_IR_LLVMCONTEXTIMPL_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/DenseSet.h" 26 #include "llvm/ADT/FoldingSet.h" 27 #include "llvm/ADT/Hashing.h" 28 #include "llvm/ADT/SmallPtrSet.h" 29 #include "llvm/ADT/StringMap.h" 30 #include "llvm/IR/Constants.h" 31 #include "llvm/IR/DerivedTypes.h" 32 #include "llvm/IR/LLVMContext.h" 33 #include "llvm/IR/Metadata.h" 34 #include "llvm/IR/ValueHandle.h" 35 #include <vector> 36 37 namespace llvm { 38 39 class ConstantInt; 40 class ConstantFP; 41 class DiagnosticInfoOptimizationRemark; 42 class DiagnosticInfoOptimizationRemarkMissed; 43 class DiagnosticInfoOptimizationRemarkAnalysis; 44 class LLVMContext; 45 class Type; 46 class Value; 47 48 struct DenseMapAPIntKeyInfo { getEmptyKeyDenseMapAPIntKeyInfo49 static inline APInt getEmptyKey() { 50 APInt V(nullptr, 0); 51 V.VAL = 0; 52 return V; 53 } getTombstoneKeyDenseMapAPIntKeyInfo54 static inline APInt getTombstoneKey() { 55 APInt V(nullptr, 0); 56 V.VAL = 1; 57 return V; 58 } getHashValueDenseMapAPIntKeyInfo59 static unsigned getHashValue(const APInt &Key) { 60 return static_cast<unsigned>(hash_value(Key)); 61 } isEqualDenseMapAPIntKeyInfo62 static bool isEqual(const APInt &LHS, const APInt &RHS) { 63 return LHS.getBitWidth() == RHS.getBitWidth() && LHS == RHS; 64 } 65 }; 66 67 struct DenseMapAPFloatKeyInfo { getEmptyKeyDenseMapAPFloatKeyInfo68 static inline APFloat getEmptyKey() { return APFloat(APFloat::Bogus, 1); } getTombstoneKeyDenseMapAPFloatKeyInfo69 static inline APFloat getTombstoneKey() { return APFloat(APFloat::Bogus, 2); } getHashValueDenseMapAPFloatKeyInfo70 static unsigned getHashValue(const APFloat &Key) { 71 return static_cast<unsigned>(hash_value(Key)); 72 } isEqualDenseMapAPFloatKeyInfo73 static bool isEqual(const APFloat &LHS, const APFloat &RHS) { 74 return LHS.bitwiseIsEqual(RHS); 75 } 76 }; 77 78 struct AnonStructTypeKeyInfo { 79 struct KeyTy { 80 ArrayRef<Type*> ETypes; 81 bool isPacked; KeyTyAnonStructTypeKeyInfo::KeyTy82 KeyTy(const ArrayRef<Type*>& E, bool P) : 83 ETypes(E), isPacked(P) {} KeyTyAnonStructTypeKeyInfo::KeyTy84 KeyTy(const StructType *ST) 85 : ETypes(ST->elements()), isPacked(ST->isPacked()) {} 86 bool operator==(const KeyTy& that) const { 87 if (isPacked != that.isPacked) 88 return false; 89 if (ETypes != that.ETypes) 90 return false; 91 return true; 92 } 93 bool operator!=(const KeyTy& that) const { 94 return !this->operator==(that); 95 } 96 }; getEmptyKeyAnonStructTypeKeyInfo97 static inline StructType* getEmptyKey() { 98 return DenseMapInfo<StructType*>::getEmptyKey(); 99 } getTombstoneKeyAnonStructTypeKeyInfo100 static inline StructType* getTombstoneKey() { 101 return DenseMapInfo<StructType*>::getTombstoneKey(); 102 } getHashValueAnonStructTypeKeyInfo103 static unsigned getHashValue(const KeyTy& Key) { 104 return hash_combine(hash_combine_range(Key.ETypes.begin(), 105 Key.ETypes.end()), 106 Key.isPacked); 107 } getHashValueAnonStructTypeKeyInfo108 static unsigned getHashValue(const StructType *ST) { 109 return getHashValue(KeyTy(ST)); 110 } isEqualAnonStructTypeKeyInfo111 static bool isEqual(const KeyTy& LHS, const StructType *RHS) { 112 if (RHS == getEmptyKey() || RHS == getTombstoneKey()) 113 return false; 114 return LHS == KeyTy(RHS); 115 } isEqualAnonStructTypeKeyInfo116 static bool isEqual(const StructType *LHS, const StructType *RHS) { 117 return LHS == RHS; 118 } 119 }; 120 121 struct FunctionTypeKeyInfo { 122 struct KeyTy { 123 const Type *ReturnType; 124 ArrayRef<Type*> Params; 125 bool isVarArg; KeyTyFunctionTypeKeyInfo::KeyTy126 KeyTy(const Type* R, const ArrayRef<Type*>& P, bool V) : 127 ReturnType(R), Params(P), isVarArg(V) {} KeyTyFunctionTypeKeyInfo::KeyTy128 KeyTy(const FunctionType *FT) 129 : ReturnType(FT->getReturnType()), Params(FT->params()), 130 isVarArg(FT->isVarArg()) {} 131 bool operator==(const KeyTy& that) const { 132 if (ReturnType != that.ReturnType) 133 return false; 134 if (isVarArg != that.isVarArg) 135 return false; 136 if (Params != that.Params) 137 return false; 138 return true; 139 } 140 bool operator!=(const KeyTy& that) const { 141 return !this->operator==(that); 142 } 143 }; getEmptyKeyFunctionTypeKeyInfo144 static inline FunctionType* getEmptyKey() { 145 return DenseMapInfo<FunctionType*>::getEmptyKey(); 146 } getTombstoneKeyFunctionTypeKeyInfo147 static inline FunctionType* getTombstoneKey() { 148 return DenseMapInfo<FunctionType*>::getTombstoneKey(); 149 } getHashValueFunctionTypeKeyInfo150 static unsigned getHashValue(const KeyTy& Key) { 151 return hash_combine(Key.ReturnType, 152 hash_combine_range(Key.Params.begin(), 153 Key.Params.end()), 154 Key.isVarArg); 155 } getHashValueFunctionTypeKeyInfo156 static unsigned getHashValue(const FunctionType *FT) { 157 return getHashValue(KeyTy(FT)); 158 } isEqualFunctionTypeKeyInfo159 static bool isEqual(const KeyTy& LHS, const FunctionType *RHS) { 160 if (RHS == getEmptyKey() || RHS == getTombstoneKey()) 161 return false; 162 return LHS == KeyTy(RHS); 163 } isEqualFunctionTypeKeyInfo164 static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) { 165 return LHS == RHS; 166 } 167 }; 168 169 /// \brief DenseMapInfo for MDTuple. 170 /// 171 /// Note that we don't need the is-function-local bit, since that's implicit in 172 /// the operands. 173 struct MDTupleInfo { 174 struct KeyTy { 175 ArrayRef<Metadata *> RawOps; 176 ArrayRef<MDOperand> Ops; 177 unsigned Hash; 178 KeyTyMDTupleInfo::KeyTy179 KeyTy(ArrayRef<Metadata *> Ops) 180 : RawOps(Ops), Hash(hash_combine_range(Ops.begin(), Ops.end())) {} 181 KeyTyMDTupleInfo::KeyTy182 KeyTy(MDTuple *N) 183 : Ops(N->op_begin(), N->op_end()), Hash(N->getHash()) {} 184 185 bool operator==(const MDTuple *RHS) const { 186 if (RHS == getEmptyKey() || RHS == getTombstoneKey()) 187 return false; 188 if (Hash != RHS->getHash()) 189 return false; 190 assert((RawOps.empty() || Ops.empty()) && "Two sets of operands?"); 191 return RawOps.empty() ? compareOps(Ops, RHS) : compareOps(RawOps, RHS); 192 } 193 template <class T> compareOpsMDTupleInfo::KeyTy194 static bool compareOps(ArrayRef<T> Ops, const MDTuple *RHS) { 195 if (Ops.size() != RHS->getNumOperands()) 196 return false; 197 return std::equal(Ops.begin(), Ops.end(), RHS->op_begin()); 198 } 199 }; getEmptyKeyMDTupleInfo200 static inline MDTuple *getEmptyKey() { 201 return DenseMapInfo<MDTuple *>::getEmptyKey(); 202 } getTombstoneKeyMDTupleInfo203 static inline MDTuple *getTombstoneKey() { 204 return DenseMapInfo<MDTuple *>::getTombstoneKey(); 205 } getHashValueMDTupleInfo206 static unsigned getHashValue(const KeyTy &Key) { return Key.Hash; } getHashValueMDTupleInfo207 static unsigned getHashValue(const MDTuple *U) { 208 return U->getHash(); 209 } isEqualMDTupleInfo210 static bool isEqual(const KeyTy &LHS, const MDTuple *RHS) { 211 return LHS == RHS; 212 } isEqualMDTupleInfo213 static bool isEqual(const MDTuple *LHS, const MDTuple *RHS) { 214 return LHS == RHS; 215 } 216 }; 217 218 /// \brief DenseMapInfo for MDLocation. 219 struct MDLocationInfo { 220 struct KeyTy { 221 unsigned Line; 222 unsigned Column; 223 Metadata *Scope; 224 Metadata *InlinedAt; 225 KeyTyMDLocationInfo::KeyTy226 KeyTy(unsigned Line, unsigned Column, Metadata *Scope, Metadata *InlinedAt) 227 : Line(Line), Column(Column), Scope(Scope), InlinedAt(InlinedAt) {} 228 KeyTyMDLocationInfo::KeyTy229 KeyTy(const MDLocation *L) 230 : Line(L->getLine()), Column(L->getColumn()), Scope(L->getScope()), 231 InlinedAt(L->getInlinedAt()) {} 232 233 bool operator==(const MDLocation *RHS) const { 234 if (RHS == getEmptyKey() || RHS == getTombstoneKey()) 235 return false; 236 return Line == RHS->getLine() && Column == RHS->getColumn() && 237 Scope == RHS->getScope() && InlinedAt == RHS->getInlinedAt(); 238 } 239 }; getEmptyKeyMDLocationInfo240 static inline MDLocation *getEmptyKey() { 241 return DenseMapInfo<MDLocation *>::getEmptyKey(); 242 } getTombstoneKeyMDLocationInfo243 static inline MDLocation *getTombstoneKey() { 244 return DenseMapInfo<MDLocation *>::getTombstoneKey(); 245 } getHashValueMDLocationInfo246 static unsigned getHashValue(const KeyTy &Key) { 247 return hash_combine(Key.Line, Key.Column, Key.Scope, Key.InlinedAt); 248 } getHashValueMDLocationInfo249 static unsigned getHashValue(const MDLocation *U) { 250 return getHashValue(KeyTy(U)); 251 } isEqualMDLocationInfo252 static bool isEqual(const KeyTy &LHS, const MDLocation *RHS) { 253 return LHS == RHS; 254 } isEqualMDLocationInfo255 static bool isEqual(const MDLocation *LHS, const MDLocation *RHS) { 256 return LHS == RHS; 257 } 258 }; 259 260 class LLVMContextImpl { 261 public: 262 /// OwnedModules - The set of modules instantiated in this context, and which 263 /// will be automatically deleted if this context is deleted. 264 SmallPtrSet<Module*, 4> OwnedModules; 265 266 LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler; 267 void *InlineAsmDiagContext; 268 269 LLVMContext::DiagnosticHandlerTy DiagnosticHandler; 270 void *DiagnosticContext; 271 bool RespectDiagnosticFilters; 272 273 LLVMContext::YieldCallbackTy YieldCallback; 274 void *YieldOpaqueHandle; 275 276 typedef DenseMap<APInt, ConstantInt *, DenseMapAPIntKeyInfo> IntMapTy; 277 IntMapTy IntConstants; 278 279 typedef DenseMap<APFloat, ConstantFP *, DenseMapAPFloatKeyInfo> FPMapTy; 280 FPMapTy FPConstants; 281 282 FoldingSet<AttributeImpl> AttrsSet; 283 FoldingSet<AttributeSetImpl> AttrsLists; 284 FoldingSet<AttributeSetNode> AttrsSetNodes; 285 286 StringMap<MDString> MDStringCache; 287 DenseMap<Value *, ValueAsMetadata *> ValuesAsMetadata; 288 DenseMap<Metadata *, MetadataAsValue *> MetadataAsValues; 289 290 DenseSet<MDTuple *, MDTupleInfo> MDTuples; 291 DenseSet<MDLocation *, MDLocationInfo> MDLocations; 292 293 // MDNodes may be uniqued or not uniqued. When they're not uniqued, they 294 // aren't in the MDNodeSet, but they're still shared between objects, so no 295 // one object can destroy them. This set allows us to at least destroy them 296 // on Context destruction. 297 SmallPtrSet<UniquableMDNode *, 1> DistinctMDNodes; 298 299 DenseMap<Type*, ConstantAggregateZero*> CAZConstants; 300 301 typedef ConstantUniqueMap<ConstantArray> ArrayConstantsTy; 302 ArrayConstantsTy ArrayConstants; 303 304 typedef ConstantUniqueMap<ConstantStruct> StructConstantsTy; 305 StructConstantsTy StructConstants; 306 307 typedef ConstantUniqueMap<ConstantVector> VectorConstantsTy; 308 VectorConstantsTy VectorConstants; 309 310 DenseMap<PointerType*, ConstantPointerNull*> CPNConstants; 311 312 DenseMap<Type*, UndefValue*> UVConstants; 313 314 StringMap<ConstantDataSequential*> CDSConstants; 315 316 DenseMap<std::pair<const Function *, const BasicBlock *>, BlockAddress *> 317 BlockAddresses; 318 ConstantUniqueMap<ConstantExpr> ExprConstants; 319 320 ConstantUniqueMap<InlineAsm> InlineAsms; 321 322 ConstantInt *TheTrueVal; 323 ConstantInt *TheFalseVal; 324 325 LeakDetectorImpl<Value> LLVMObjects; 326 LeakDetectorImpl<Metadata> LLVMMDObjects; 327 328 // Basic type instances. 329 Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy; 330 Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy; 331 IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty; 332 333 334 /// TypeAllocator - All dynamically allocated types are allocated from this. 335 /// They live forever until the context is torn down. 336 BumpPtrAllocator TypeAllocator; 337 338 DenseMap<unsigned, IntegerType*> IntegerTypes; 339 340 typedef DenseSet<FunctionType *, FunctionTypeKeyInfo> FunctionTypeSet; 341 FunctionTypeSet FunctionTypes; 342 typedef DenseSet<StructType *, AnonStructTypeKeyInfo> StructTypeSet; 343 StructTypeSet AnonStructTypes; 344 StringMap<StructType*> NamedStructTypes; 345 unsigned NamedStructTypesUniqueID; 346 347 DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes; 348 DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes; 349 DenseMap<Type*, PointerType*> PointerTypes; // Pointers in AddrSpace = 0 350 DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes; 351 352 353 /// ValueHandles - This map keeps track of all of the value handles that are 354 /// watching a Value*. The Value::HasValueHandle bit is used to know 355 /// whether or not a value has an entry in this map. 356 typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy; 357 ValueHandlesTy ValueHandles; 358 359 /// CustomMDKindNames - Map to hold the metadata string to ID mapping. 360 StringMap<unsigned> CustomMDKindNames; 361 362 typedef std::pair<unsigned, TrackingMDNodeRef> MDPairTy; 363 typedef SmallVector<MDPairTy, 2> MDMapTy; 364 365 /// MetadataStore - Collection of per-instruction metadata used in this 366 /// context. 367 DenseMap<const Instruction *, MDMapTy> MetadataStore; 368 369 /// DiscriminatorTable - This table maps file:line locations to an 370 /// integer representing the next DWARF path discriminator to assign to 371 /// instructions in different blocks at the same location. 372 DenseMap<std::pair<const char *, unsigned>, unsigned> DiscriminatorTable; 373 374 /// IntrinsicIDCache - Cache of intrinsic name (string) to numeric ID mappings 375 /// requested in this context 376 typedef DenseMap<const Function*, unsigned> IntrinsicIDCacheTy; 377 IntrinsicIDCacheTy IntrinsicIDCache; 378 379 /// \brief Mapping from a function to its prefix data, which is stored as the 380 /// operand of an unparented ReturnInst so that the prefix data has a Use. 381 typedef DenseMap<const Function *, ReturnInst *> PrefixDataMapTy; 382 PrefixDataMapTy PrefixDataMap; 383 384 /// \brief Mapping from a function to its prologue data, which is stored as 385 /// the operand of an unparented ReturnInst so that the prologue data has a 386 /// Use. 387 typedef DenseMap<const Function *, ReturnInst *> PrologueDataMapTy; 388 PrologueDataMapTy PrologueDataMap; 389 390 int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx); 391 int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx); 392 393 LLVMContextImpl(LLVMContext &C); 394 ~LLVMContextImpl(); 395 }; 396 397 } 398 399 #endif 400