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 DenseMapInfo for MDTuple.
172 /// Note that we don't need the is-function-local bit, since that's implicit in
176 ArrayRef<Metadata *> RawOps;
177 ArrayRef<MDOperand> Ops;
180 KeyTy(ArrayRef<Metadata *> Ops)
181 : RawOps(Ops), Hash(hash_combine_range(Ops.begin(), Ops.end())) {}
184 : Ops(N->op_begin(), N->op_end()), Hash(N->getHash()) {}
186 bool operator==(const MDTuple *RHS) const {
187 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
189 if (Hash != RHS->getHash())
191 assert((RawOps.empty() || Ops.empty()) && "Two sets of operands?");
192 return RawOps.empty() ? compareOps(Ops, RHS) : compareOps(RawOps, RHS);
195 static bool compareOps(ArrayRef<T> Ops, const MDTuple *RHS) {
196 if (Ops.size() != RHS->getNumOperands())
198 return std::equal(Ops.begin(), Ops.end(), RHS->op_begin());
201 static inline MDTuple *getEmptyKey() {
202 return DenseMapInfo<MDTuple *>::getEmptyKey();
204 static inline MDTuple *getTombstoneKey() {
205 return DenseMapInfo<MDTuple *>::getTombstoneKey();
207 static unsigned getHashValue(const KeyTy &Key) { return Key.Hash; }
208 static unsigned getHashValue(const MDTuple *U) {
211 static bool isEqual(const KeyTy &LHS, const MDTuple *RHS) {
214 static bool isEqual(const MDTuple *LHS, const MDTuple *RHS) {
219 /// \brief DenseMapInfo for MDLocation.
220 struct MDLocationInfo {
227 KeyTy(unsigned Line, unsigned Column, Metadata *Scope, Metadata *InlinedAt)
228 : Line(Line), Column(Column), Scope(Scope), InlinedAt(InlinedAt) {}
230 KeyTy(const MDLocation *L)
231 : Line(L->getLine()), Column(L->getColumn()), Scope(L->getScope()),
232 InlinedAt(L->getInlinedAt()) {}
234 bool operator==(const MDLocation *RHS) const {
235 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
237 return Line == RHS->getLine() && Column == RHS->getColumn() &&
238 Scope == RHS->getScope() && InlinedAt == RHS->getInlinedAt();
241 static inline MDLocation *getEmptyKey() {
242 return DenseMapInfo<MDLocation *>::getEmptyKey();
244 static inline MDLocation *getTombstoneKey() {
245 return DenseMapInfo<MDLocation *>::getTombstoneKey();
247 static unsigned getHashValue(const KeyTy &Key) {
248 return hash_combine(Key.Line, Key.Column, Key.Scope, Key.InlinedAt);
250 static unsigned getHashValue(const MDLocation *U) {
251 return getHashValue(KeyTy(U));
253 static bool isEqual(const KeyTy &LHS, const MDLocation *RHS) {
256 static bool isEqual(const MDLocation *LHS, const MDLocation *RHS) {
261 class LLVMContextImpl {
263 /// OwnedModules - The set of modules instantiated in this context, and which
264 /// will be automatically deleted if this context is deleted.
265 SmallPtrSet<Module*, 4> OwnedModules;
267 LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
268 void *InlineAsmDiagContext;
270 LLVMContext::DiagnosticHandlerTy DiagnosticHandler;
271 void *DiagnosticContext;
272 bool RespectDiagnosticFilters;
274 LLVMContext::YieldCallbackTy YieldCallback;
275 void *YieldOpaqueHandle;
277 typedef DenseMap<APInt, ConstantInt *, DenseMapAPIntKeyInfo> IntMapTy;
278 IntMapTy IntConstants;
280 typedef DenseMap<APFloat, ConstantFP *, DenseMapAPFloatKeyInfo> FPMapTy;
283 FoldingSet<AttributeImpl> AttrsSet;
284 FoldingSet<AttributeSetImpl> AttrsLists;
285 FoldingSet<AttributeSetNode> AttrsSetNodes;
287 StringMap<MDString> MDStringCache;
288 DenseMap<Value *, ValueAsMetadata *> ValuesAsMetadata;
289 DenseMap<Metadata *, MetadataAsValue *> MetadataAsValues;
291 DenseSet<MDTuple *, MDTupleInfo> MDTuples;
292 DenseSet<MDLocation *, MDLocationInfo> MDLocations;
294 // MDNodes may be uniqued or not uniqued. When they're not uniqued, they
295 // aren't in the MDNodeSet, but they're still shared between objects, so no
296 // one object can destroy them. This set allows us to at least destroy them
297 // on Context destruction.
298 SmallPtrSet<UniquableMDNode *, 1> DistinctMDNodes;
300 DenseMap<Type*, ConstantAggregateZero*> CAZConstants;
302 typedef ConstantUniqueMap<ConstantArray> ArrayConstantsTy;
303 ArrayConstantsTy ArrayConstants;
305 typedef ConstantUniqueMap<ConstantStruct> StructConstantsTy;
306 StructConstantsTy StructConstants;
308 typedef ConstantUniqueMap<ConstantVector> VectorConstantsTy;
309 VectorConstantsTy VectorConstants;
311 DenseMap<PointerType*, ConstantPointerNull*> CPNConstants;
313 DenseMap<Type*, UndefValue*> UVConstants;
315 StringMap<ConstantDataSequential*> CDSConstants;
317 DenseMap<std::pair<const Function *, const BasicBlock *>, BlockAddress *>
319 ConstantUniqueMap<ConstantExpr> ExprConstants;
321 ConstantUniqueMap<InlineAsm> InlineAsms;
323 ConstantInt *TheTrueVal;
324 ConstantInt *TheFalseVal;
326 LeakDetectorImpl<Value> LLVMObjects;
327 LeakDetectorImpl<Metadata> LLVMMDObjects;
329 // Basic type instances.
330 Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy;
331 Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy;
332 IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty;
335 /// TypeAllocator - All dynamically allocated types are allocated from this.
336 /// They live forever until the context is torn down.
337 BumpPtrAllocator TypeAllocator;
339 DenseMap<unsigned, IntegerType*> IntegerTypes;
341 typedef DenseSet<FunctionType *, FunctionTypeKeyInfo> FunctionTypeSet;
342 FunctionTypeSet FunctionTypes;
343 typedef DenseSet<StructType *, AnonStructTypeKeyInfo> StructTypeSet;
344 StructTypeSet AnonStructTypes;
345 StringMap<StructType*> NamedStructTypes;
346 unsigned NamedStructTypesUniqueID;
348 DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
349 DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes;
350 DenseMap<Type*, PointerType*> PointerTypes; // Pointers in AddrSpace = 0
351 DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;
354 /// ValueHandles - This map keeps track of all of the value handles that are
355 /// watching a Value*. The Value::HasValueHandle bit is used to know
356 /// whether or not a value has an entry in this map.
357 typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
358 ValueHandlesTy ValueHandles;
360 /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
361 StringMap<unsigned> CustomMDKindNames;
363 typedef std::pair<unsigned, TrackingMDNodeRef> MDPairTy;
364 typedef SmallVector<MDPairTy, 2> MDMapTy;
366 /// MetadataStore - Collection of per-instruction metadata used in this
368 DenseMap<const Instruction *, MDMapTy> MetadataStore;
370 /// DiscriminatorTable - This table maps file:line locations to an
371 /// integer representing the next DWARF path discriminator to assign to
372 /// instructions in different blocks at the same location.
373 DenseMap<std::pair<const char *, unsigned>, unsigned> DiscriminatorTable;
375 /// IntrinsicIDCache - Cache of intrinsic name (string) to numeric ID mappings
376 /// requested in this context
377 typedef DenseMap<const Function*, unsigned> IntrinsicIDCacheTy;
378 IntrinsicIDCacheTy IntrinsicIDCache;
380 /// \brief Mapping from a function to its prefix data, which is stored as the
381 /// operand of an unparented ReturnInst so that the prefix data has a Use.
382 typedef DenseMap<const Function *, ReturnInst *> PrefixDataMapTy;
383 PrefixDataMapTy PrefixDataMap;
385 /// \brief Mapping from a function to its prologue data, which is stored as
386 /// the operand of an unparented ReturnInst so that the prologue data has a
388 typedef DenseMap<const Function *, ReturnInst *> PrologueDataMapTy;
389 PrologueDataMapTy PrologueDataMap;
391 int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
392 int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
394 /// An owning list of all GCStrategies which have been created
395 SmallVector<std::unique_ptr<GCStrategy>, 1> GCStrategyList;
396 /// A helper map to speedup lookups into the above list
397 StringMap<GCStrategy*> GCStrategyMap;
399 /// Lookup the GCStrategy object associated with the given gc name. If one
400 /// can't be found, returns nullptr. The lifetime of the returned objects
401 /// is dictated by the lifetime of the associated context. No caller should
402 /// attempt to delete the returned objects.
403 GCStrategy *getGCStrategy(const StringRef Name);
405 LLVMContextImpl(LLVMContext &C);