1 //===-- LLVMContextImpl.h - The LLVMContextImpl opaque class ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares LLVMContextImpl, the opaque implementation
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_LIB_IR_LLVMCONTEXTIMPL_H
16 #define LLVM_LIB_IR_LLVMCONTEXTIMPL_H
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"
41 class DiagnosticInfoOptimizationRemark;
42 class DiagnosticInfoOptimizationRemarkMissed;
43 class DiagnosticInfoOptimizationRemarkAnalysis;
49 struct DenseMapAPIntKeyInfo {
50 static inline APInt getEmptyKey() {
55 static inline APInt getTombstoneKey() {
60 static unsigned getHashValue(const APInt &Key) {
61 return static_cast<unsigned>(hash_value(Key));
63 static bool isEqual(const APInt &LHS, const APInt &RHS) {
64 return LHS.getBitWidth() == RHS.getBitWidth() && LHS == RHS;
68 struct DenseMapAPFloatKeyInfo {
69 static inline APFloat getEmptyKey() { return APFloat(APFloat::Bogus, 1); }
70 static inline APFloat getTombstoneKey() { return APFloat(APFloat::Bogus, 2); }
71 static unsigned getHashValue(const APFloat &Key) {
72 return static_cast<unsigned>(hash_value(Key));
74 static bool isEqual(const APFloat &LHS, const APFloat &RHS) {
75 return LHS.bitwiseIsEqual(RHS);
79 struct AnonStructTypeKeyInfo {
81 ArrayRef<Type*> ETypes;
83 KeyTy(const ArrayRef<Type*>& E, bool P) :
84 ETypes(E), isPacked(P) {}
85 KeyTy(const StructType *ST)
86 : ETypes(ST->elements()), isPacked(ST->isPacked()) {}
87 bool operator==(const KeyTy& that) const {
88 if (isPacked != that.isPacked)
90 if (ETypes != that.ETypes)
94 bool operator!=(const KeyTy& that) const {
95 return !this->operator==(that);
98 static inline StructType* getEmptyKey() {
99 return DenseMapInfo<StructType*>::getEmptyKey();
101 static inline StructType* getTombstoneKey() {
102 return DenseMapInfo<StructType*>::getTombstoneKey();
104 static unsigned getHashValue(const KeyTy& Key) {
105 return hash_combine(hash_combine_range(Key.ETypes.begin(),
109 static unsigned getHashValue(const StructType *ST) {
110 return getHashValue(KeyTy(ST));
112 static bool isEqual(const KeyTy& LHS, const StructType *RHS) {
113 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
115 return LHS == KeyTy(RHS);
117 static bool isEqual(const StructType *LHS, const StructType *RHS) {
122 struct FunctionTypeKeyInfo {
124 const Type *ReturnType;
125 ArrayRef<Type*> Params;
127 KeyTy(const Type* R, const ArrayRef<Type*>& P, bool V) :
128 ReturnType(R), Params(P), isVarArg(V) {}
129 KeyTy(const FunctionType *FT)
130 : ReturnType(FT->getReturnType()), Params(FT->params()),
131 isVarArg(FT->isVarArg()) {}
132 bool operator==(const KeyTy& that) const {
133 if (ReturnType != that.ReturnType)
135 if (isVarArg != that.isVarArg)
137 if (Params != that.Params)
141 bool operator!=(const KeyTy& that) const {
142 return !this->operator==(that);
145 static inline FunctionType* getEmptyKey() {
146 return DenseMapInfo<FunctionType*>::getEmptyKey();
148 static inline FunctionType* getTombstoneKey() {
149 return DenseMapInfo<FunctionType*>::getTombstoneKey();
151 static unsigned getHashValue(const KeyTy& Key) {
152 return hash_combine(Key.ReturnType,
153 hash_combine_range(Key.Params.begin(),
157 static unsigned getHashValue(const FunctionType *FT) {
158 return getHashValue(KeyTy(FT));
160 static bool isEqual(const KeyTy& LHS, const FunctionType *RHS) {
161 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
163 return LHS == KeyTy(RHS);
165 static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) {
170 /// \brief Structure for hashing arbitrary MDNode operands.
172 ArrayRef<Metadata *> RawOps;
173 ArrayRef<MDOperand> Ops;
178 MDNodeOpsKey(ArrayRef<Metadata *> Ops)
179 : RawOps(Ops), Hash(calculateHash(Ops)) {}
181 template <class NodeTy>
182 MDNodeOpsKey(NodeTy *N, unsigned Offset = 0)
183 : Ops(N->op_begin() + Offset, N->op_end()), Hash(N->getHash()) {}
185 template <class NodeTy>
186 bool compareOps(const NodeTy *RHS, unsigned Offset = 0) const {
187 if (getHash() != RHS->getHash())
190 assert((RawOps.empty() || Ops.empty()) && "Two sets of operands?");
191 return RawOps.empty() ? compareOps(Ops, RHS, Offset)
192 : compareOps(RawOps, RHS, Offset);
195 static unsigned calculateHash(MDNode *N, unsigned Offset = 0);
199 static bool compareOps(ArrayRef<T> Ops, const MDNode *RHS, unsigned Offset) {
200 if (Ops.size() != RHS->getNumOperands() - Offset)
202 return std::equal(Ops.begin(), Ops.end(), RHS->op_begin() + Offset);
205 static unsigned calculateHash(ArrayRef<Metadata *> Ops);
208 unsigned getHash() const { return Hash; }
211 /// \brief DenseMapInfo for MDTuple.
213 /// Note that we don't need the is-function-local bit, since that's implicit in
216 struct KeyTy : MDNodeOpsKey {
217 KeyTy(ArrayRef<Metadata *> Ops) : MDNodeOpsKey(Ops) {}
218 KeyTy(MDTuple *N) : MDNodeOpsKey(N) {}
220 bool operator==(const MDTuple *RHS) const {
221 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
223 return compareOps(RHS);
226 static unsigned calculateHash(MDTuple *N) {
227 return MDNodeOpsKey::calculateHash(N);
230 static inline MDTuple *getEmptyKey() {
231 return DenseMapInfo<MDTuple *>::getEmptyKey();
233 static inline MDTuple *getTombstoneKey() {
234 return DenseMapInfo<MDTuple *>::getTombstoneKey();
236 static unsigned getHashValue(const KeyTy &Key) { return Key.getHash(); }
237 static unsigned getHashValue(const MDTuple *U) { return U->getHash(); }
238 static bool isEqual(const KeyTy &LHS, const MDTuple *RHS) {
241 static bool isEqual(const MDTuple *LHS, const MDTuple *RHS) {
246 /// \brief DenseMapInfo for MDLocation.
247 struct MDLocationInfo {
254 KeyTy(unsigned Line, unsigned Column, Metadata *Scope, Metadata *InlinedAt)
255 : Line(Line), Column(Column), Scope(Scope), InlinedAt(InlinedAt) {}
257 KeyTy(const MDLocation *L)
258 : Line(L->getLine()), Column(L->getColumn()), Scope(L->getScope()),
259 InlinedAt(L->getInlinedAt()) {}
261 bool operator==(const MDLocation *RHS) const {
262 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
264 return Line == RHS->getLine() && Column == RHS->getColumn() &&
265 Scope == RHS->getScope() && InlinedAt == RHS->getInlinedAt();
268 static inline MDLocation *getEmptyKey() {
269 return DenseMapInfo<MDLocation *>::getEmptyKey();
271 static inline MDLocation *getTombstoneKey() {
272 return DenseMapInfo<MDLocation *>::getTombstoneKey();
274 static unsigned getHashValue(const KeyTy &Key) {
275 return hash_combine(Key.Line, Key.Column, Key.Scope, Key.InlinedAt);
277 static unsigned getHashValue(const MDLocation *U) {
278 return getHashValue(KeyTy(U));
280 static bool isEqual(const KeyTy &LHS, const MDLocation *RHS) {
283 static bool isEqual(const MDLocation *LHS, const MDLocation *RHS) {
288 /// \brief DenseMapInfo for GenericDwarfNode.
289 struct GenericDwarfNodeInfo {
290 struct KeyTy : MDNodeOpsKey {
293 KeyTy(unsigned Tag, MDString *Header, ArrayRef<Metadata *> DwarfOps)
294 : MDNodeOpsKey(DwarfOps), Tag(Tag), Header(Header) {}
295 KeyTy(GenericDwarfNode *N)
296 : MDNodeOpsKey(N, 1), Tag(N->getTag()), Header(N->getHeader()) {}
298 bool operator==(const GenericDwarfNode *RHS) const {
299 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
301 return Tag == RHS->getTag() && Header == RHS->getHeader() &&
305 static unsigned calculateHash(GenericDwarfNode *N) {
306 return MDNodeOpsKey::calculateHash(N, 1);
309 static inline GenericDwarfNode *getEmptyKey() {
310 return DenseMapInfo<GenericDwarfNode *>::getEmptyKey();
312 static inline GenericDwarfNode *getTombstoneKey() {
313 return DenseMapInfo<GenericDwarfNode *>::getTombstoneKey();
315 static unsigned getHashValue(const KeyTy &Key) {
316 return hash_combine(Key.getHash(), Key.Tag, Key.Header);
318 static unsigned getHashValue(const GenericDwarfNode *U) {
319 return hash_combine(U->getHash(), U->getTag(), U->getHeader());
321 static bool isEqual(const KeyTy &LHS, const GenericDwarfNode *RHS) {
324 static bool isEqual(const GenericDwarfNode *LHS,
325 const GenericDwarfNode *RHS) {
330 class LLVMContextImpl {
332 /// OwnedModules - The set of modules instantiated in this context, and which
333 /// will be automatically deleted if this context is deleted.
334 SmallPtrSet<Module*, 4> OwnedModules;
336 LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
337 void *InlineAsmDiagContext;
339 LLVMContext::DiagnosticHandlerTy DiagnosticHandler;
340 void *DiagnosticContext;
341 bool RespectDiagnosticFilters;
343 LLVMContext::YieldCallbackTy YieldCallback;
344 void *YieldOpaqueHandle;
346 typedef DenseMap<APInt, ConstantInt *, DenseMapAPIntKeyInfo> IntMapTy;
347 IntMapTy IntConstants;
349 typedef DenseMap<APFloat, ConstantFP *, DenseMapAPFloatKeyInfo> FPMapTy;
352 FoldingSet<AttributeImpl> AttrsSet;
353 FoldingSet<AttributeSetImpl> AttrsLists;
354 FoldingSet<AttributeSetNode> AttrsSetNodes;
356 StringMap<MDString> MDStringCache;
357 DenseMap<Value *, ValueAsMetadata *> ValuesAsMetadata;
358 DenseMap<Metadata *, MetadataAsValue *> MetadataAsValues;
360 DenseSet<MDTuple *, MDTupleInfo> MDTuples;
361 DenseSet<MDLocation *, MDLocationInfo> MDLocations;
362 DenseSet<GenericDwarfNode *, GenericDwarfNodeInfo> GenericDwarfNodes;
364 // MDNodes may be uniqued or not uniqued. When they're not uniqued, they
365 // aren't in the MDNodeSet, but they're still shared between objects, so no
366 // one object can destroy them. This set allows us to at least destroy them
367 // on Context destruction.
368 SmallPtrSet<MDNode *, 1> DistinctMDNodes;
370 DenseMap<Type*, ConstantAggregateZero*> CAZConstants;
372 typedef ConstantUniqueMap<ConstantArray> ArrayConstantsTy;
373 ArrayConstantsTy ArrayConstants;
375 typedef ConstantUniqueMap<ConstantStruct> StructConstantsTy;
376 StructConstantsTy StructConstants;
378 typedef ConstantUniqueMap<ConstantVector> VectorConstantsTy;
379 VectorConstantsTy VectorConstants;
381 DenseMap<PointerType*, ConstantPointerNull*> CPNConstants;
383 DenseMap<Type*, UndefValue*> UVConstants;
385 StringMap<ConstantDataSequential*> CDSConstants;
387 DenseMap<std::pair<const Function *, const BasicBlock *>, BlockAddress *>
389 ConstantUniqueMap<ConstantExpr> ExprConstants;
391 ConstantUniqueMap<InlineAsm> InlineAsms;
393 ConstantInt *TheTrueVal;
394 ConstantInt *TheFalseVal;
396 LeakDetectorImpl<Value> LLVMObjects;
397 LeakDetectorImpl<Metadata> LLVMMDObjects;
399 // Basic type instances.
400 Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy;
401 Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy;
402 IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty;
405 /// TypeAllocator - All dynamically allocated types are allocated from this.
406 /// They live forever until the context is torn down.
407 BumpPtrAllocator TypeAllocator;
409 DenseMap<unsigned, IntegerType*> IntegerTypes;
411 typedef DenseSet<FunctionType *, FunctionTypeKeyInfo> FunctionTypeSet;
412 FunctionTypeSet FunctionTypes;
413 typedef DenseSet<StructType *, AnonStructTypeKeyInfo> StructTypeSet;
414 StructTypeSet AnonStructTypes;
415 StringMap<StructType*> NamedStructTypes;
416 unsigned NamedStructTypesUniqueID;
418 DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
419 DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes;
420 DenseMap<Type*, PointerType*> PointerTypes; // Pointers in AddrSpace = 0
421 DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;
424 /// ValueHandles - This map keeps track of all of the value handles that are
425 /// watching a Value*. The Value::HasValueHandle bit is used to know
426 /// whether or not a value has an entry in this map.
427 typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
428 ValueHandlesTy ValueHandles;
430 /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
431 StringMap<unsigned> CustomMDKindNames;
433 typedef std::pair<unsigned, TrackingMDNodeRef> MDPairTy;
434 typedef SmallVector<MDPairTy, 2> MDMapTy;
436 /// MetadataStore - Collection of per-instruction metadata used in this
438 DenseMap<const Instruction *, MDMapTy> MetadataStore;
440 /// DiscriminatorTable - This table maps file:line locations to an
441 /// integer representing the next DWARF path discriminator to assign to
442 /// instructions in different blocks at the same location.
443 DenseMap<std::pair<const char *, unsigned>, unsigned> DiscriminatorTable;
445 /// IntrinsicIDCache - Cache of intrinsic name (string) to numeric ID mappings
446 /// requested in this context
447 typedef DenseMap<const Function*, unsigned> IntrinsicIDCacheTy;
448 IntrinsicIDCacheTy IntrinsicIDCache;
450 /// \brief Mapping from a function to its prefix data, which is stored as the
451 /// operand of an unparented ReturnInst so that the prefix data has a Use.
452 typedef DenseMap<const Function *, ReturnInst *> PrefixDataMapTy;
453 PrefixDataMapTy PrefixDataMap;
455 /// \brief Mapping from a function to its prologue data, which is stored as
456 /// the operand of an unparented ReturnInst so that the prologue data has a
458 typedef DenseMap<const Function *, ReturnInst *> PrologueDataMapTy;
459 PrologueDataMapTy PrologueDataMap;
461 int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
462 int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
464 /// An owning list of all GCStrategies which have been created
465 SmallVector<std::unique_ptr<GCStrategy>, 1> GCStrategyList;
466 /// A helper map to speedup lookups into the above list
467 StringMap<GCStrategy*> GCStrategyMap;
469 /// Lookup the GCStrategy object associated with the given gc name. If one
470 /// can't be found, returns nullptr. The lifetime of the returned objects
471 /// is dictated by the lifetime of the associated context. No caller should
472 /// attempt to delete the returned objects.
473 GCStrategy *getGCStrategy(const StringRef Name);
475 LLVMContextImpl(LLVMContext &C);