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;
48 struct DenseMapAPIntKeyInfo {
49 static inline APInt getEmptyKey() {
54 static inline APInt getTombstoneKey() {
59 static unsigned getHashValue(const APInt &Key) {
60 return static_cast<unsigned>(hash_value(Key));
62 static bool isEqual(const APInt &LHS, const APInt &RHS) {
63 return LHS.getBitWidth() == RHS.getBitWidth() && LHS == RHS;
67 struct DenseMapAPFloatKeyInfo {
68 static inline APFloat getEmptyKey() { return APFloat(APFloat::Bogus, 1); }
69 static inline APFloat getTombstoneKey() { return APFloat(APFloat::Bogus, 2); }
70 static unsigned getHashValue(const APFloat &Key) {
71 return static_cast<unsigned>(hash_value(Key));
73 static bool isEqual(const APFloat &LHS, const APFloat &RHS) {
74 return LHS.bitwiseIsEqual(RHS);
78 struct AnonStructTypeKeyInfo {
80 ArrayRef<Type*> ETypes;
82 KeyTy(const ArrayRef<Type*>& E, bool P) :
83 ETypes(E), isPacked(P) {}
84 KeyTy(const StructType *ST)
85 : ETypes(ST->elements()), isPacked(ST->isPacked()) {}
86 bool operator==(const KeyTy& that) const {
87 if (isPacked != that.isPacked)
89 if (ETypes != that.ETypes)
93 bool operator!=(const KeyTy& that) const {
94 return !this->operator==(that);
97 static inline StructType* getEmptyKey() {
98 return DenseMapInfo<StructType*>::getEmptyKey();
100 static inline StructType* getTombstoneKey() {
101 return DenseMapInfo<StructType*>::getTombstoneKey();
103 static unsigned getHashValue(const KeyTy& Key) {
104 return hash_combine(hash_combine_range(Key.ETypes.begin(),
108 static unsigned getHashValue(const StructType *ST) {
109 return getHashValue(KeyTy(ST));
111 static bool isEqual(const KeyTy& LHS, const StructType *RHS) {
112 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
114 return LHS == KeyTy(RHS);
116 static bool isEqual(const StructType *LHS, const StructType *RHS) {
121 struct FunctionTypeKeyInfo {
123 const Type *ReturnType;
124 ArrayRef<Type*> Params;
126 KeyTy(const Type* R, const ArrayRef<Type*>& P, bool V) :
127 ReturnType(R), Params(P), isVarArg(V) {}
128 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)
134 if (isVarArg != that.isVarArg)
136 if (Params != that.Params)
140 bool operator!=(const KeyTy& that) const {
141 return !this->operator==(that);
144 static inline FunctionType* getEmptyKey() {
145 return DenseMapInfo<FunctionType*>::getEmptyKey();
147 static inline FunctionType* getTombstoneKey() {
148 return DenseMapInfo<FunctionType*>::getTombstoneKey();
150 static unsigned getHashValue(const KeyTy& Key) {
151 return hash_combine(Key.ReturnType,
152 hash_combine_range(Key.Params.begin(),
156 static unsigned getHashValue(const FunctionType *FT) {
157 return getHashValue(KeyTy(FT));
159 static bool isEqual(const KeyTy& LHS, const FunctionType *RHS) {
160 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
162 return LHS == KeyTy(RHS);
164 static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) {
169 /// \brief DenseMapInfo for MDTuple.
171 /// Note that we don't need the is-function-local bit, since that's implicit in
175 ArrayRef<Metadata *> RawOps;
176 ArrayRef<MDOperand> Ops;
179 KeyTy(ArrayRef<Metadata *> Ops)
180 : RawOps(Ops), Hash(hash_combine_range(Ops.begin(), Ops.end())) {}
183 : Ops(N->op_begin(), N->op_end()), Hash(N->getHash()) {}
185 bool operator==(const MDTuple *RHS) const {
186 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
188 if (Hash != RHS->getHash())
190 assert((RawOps.empty() || Ops.empty()) && "Two sets of operands?");
191 return RawOps.empty() ? compareOps(Ops, RHS) : compareOps(RawOps, RHS);
194 static bool compareOps(ArrayRef<T> Ops, const MDTuple *RHS) {
195 if (Ops.size() != RHS->getNumOperands())
197 return std::equal(Ops.begin(), Ops.end(), RHS->op_begin());
200 static inline MDTuple *getEmptyKey() {
201 return DenseMapInfo<MDTuple *>::getEmptyKey();
203 static inline MDTuple *getTombstoneKey() {
204 return DenseMapInfo<MDTuple *>::getTombstoneKey();
206 static unsigned getHashValue(const KeyTy &Key) { return Key.Hash; }
207 static unsigned getHashValue(const MDTuple *U) {
210 static bool isEqual(const KeyTy &LHS, const MDTuple *RHS) {
213 static bool isEqual(const MDTuple *LHS, const MDTuple *RHS) {
218 class LLVMContextImpl {
220 /// OwnedModules - The set of modules instantiated in this context, and which
221 /// will be automatically deleted if this context is deleted.
222 SmallPtrSet<Module*, 4> OwnedModules;
224 LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
225 void *InlineAsmDiagContext;
227 LLVMContext::DiagnosticHandlerTy DiagnosticHandler;
228 void *DiagnosticContext;
229 bool RespectDiagnosticFilters;
231 LLVMContext::YieldCallbackTy YieldCallback;
232 void *YieldOpaqueHandle;
234 typedef DenseMap<APInt, ConstantInt *, DenseMapAPIntKeyInfo> IntMapTy;
235 IntMapTy IntConstants;
237 typedef DenseMap<APFloat, ConstantFP *, DenseMapAPFloatKeyInfo> FPMapTy;
240 FoldingSet<AttributeImpl> AttrsSet;
241 FoldingSet<AttributeSetImpl> AttrsLists;
242 FoldingSet<AttributeSetNode> AttrsSetNodes;
244 StringMap<MDString> MDStringCache;
245 DenseMap<Value *, ValueAsMetadata *> ValuesAsMetadata;
246 DenseMap<Metadata *, MetadataAsValue *> MetadataAsValues;
248 DenseSet<MDTuple *, MDTupleInfo> MDTuples;
250 // MDNodes may be uniqued or not uniqued. When they're not uniqued, they
251 // aren't in the MDNodeSet, but they're still shared between objects, so no
252 // one object can destroy them. This set allows us to at least destroy them
253 // on Context destruction.
254 SmallPtrSet<UniquableMDNode *, 1> DistinctMDNodes;
256 DenseMap<Type*, ConstantAggregateZero*> CAZConstants;
258 typedef ConstantUniqueMap<ConstantArray> ArrayConstantsTy;
259 ArrayConstantsTy ArrayConstants;
261 typedef ConstantUniqueMap<ConstantStruct> StructConstantsTy;
262 StructConstantsTy StructConstants;
264 typedef ConstantUniqueMap<ConstantVector> VectorConstantsTy;
265 VectorConstantsTy VectorConstants;
267 DenseMap<PointerType*, ConstantPointerNull*> CPNConstants;
269 DenseMap<Type*, UndefValue*> UVConstants;
271 StringMap<ConstantDataSequential*> CDSConstants;
273 DenseMap<std::pair<const Function *, const BasicBlock *>, BlockAddress *>
275 ConstantUniqueMap<ConstantExpr> ExprConstants;
277 ConstantUniqueMap<InlineAsm> InlineAsms;
279 ConstantInt *TheTrueVal;
280 ConstantInt *TheFalseVal;
282 LeakDetectorImpl<Value> LLVMObjects;
283 LeakDetectorImpl<Metadata> LLVMMDObjects;
285 // Basic type instances.
286 Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy;
287 Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy;
288 IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty;
291 /// TypeAllocator - All dynamically allocated types are allocated from this.
292 /// They live forever until the context is torn down.
293 BumpPtrAllocator TypeAllocator;
295 DenseMap<unsigned, IntegerType*> IntegerTypes;
297 typedef DenseSet<FunctionType *, FunctionTypeKeyInfo> FunctionTypeSet;
298 FunctionTypeSet FunctionTypes;
299 typedef DenseSet<StructType *, AnonStructTypeKeyInfo> StructTypeSet;
300 StructTypeSet AnonStructTypes;
301 StringMap<StructType*> NamedStructTypes;
302 unsigned NamedStructTypesUniqueID;
304 DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
305 DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes;
306 DenseMap<Type*, PointerType*> PointerTypes; // Pointers in AddrSpace = 0
307 DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;
310 /// ValueHandles - This map keeps track of all of the value handles that are
311 /// watching a Value*. The Value::HasValueHandle bit is used to know
312 /// whether or not a value has an entry in this map.
313 typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
314 ValueHandlesTy ValueHandles;
316 /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
317 StringMap<unsigned> CustomMDKindNames;
319 typedef std::pair<unsigned, TrackingMDNodeRef> MDPairTy;
320 typedef SmallVector<MDPairTy, 2> MDMapTy;
322 /// MetadataStore - Collection of per-instruction metadata used in this
324 DenseMap<const Instruction *, MDMapTy> MetadataStore;
326 /// DiscriminatorTable - This table maps file:line locations to an
327 /// integer representing the next DWARF path discriminator to assign to
328 /// instructions in different blocks at the same location.
329 DenseMap<std::pair<const char *, unsigned>, unsigned> DiscriminatorTable;
331 /// IntrinsicIDCache - Cache of intrinsic name (string) to numeric ID mappings
332 /// requested in this context
333 typedef DenseMap<const Function*, unsigned> IntrinsicIDCacheTy;
334 IntrinsicIDCacheTy IntrinsicIDCache;
336 /// \brief Mapping from a function to its prefix data, which is stored as the
337 /// operand of an unparented ReturnInst so that the prefix data has a Use.
338 typedef DenseMap<const Function *, ReturnInst *> PrefixDataMapTy;
339 PrefixDataMapTy PrefixDataMap;
341 /// \brief Mapping from a function to its prologue data, which is stored as
342 /// the operand of an unparented ReturnInst so that the prologue data has a
344 typedef DenseMap<const Function *, ReturnInst *> PrologueDataMapTy;
345 PrologueDataMapTy PrologueDataMap;
347 int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
348 int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
350 LLVMContextImpl(LLVMContext &C);