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 /// \brief DenseMapInfo for MDLocation.
219 struct MDLocationInfo {
226 KeyTy(unsigned Line, unsigned Column, Metadata *Scope, Metadata *InlinedAt)
227 : Line(Line), Column(Column), Scope(Scope), InlinedAt(InlinedAt) {}
229 KeyTy(const MDLocation *L)
230 : Line(L->getLine()), Column(L->getColumn()), Scope(L->getScope()),
231 InlinedAt(L->getInlinedAt()) {}
233 bool operator==(const MDLocation *RHS) const {
234 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
236 return Line == RHS->getLine() && Column == RHS->getColumn() &&
237 Scope == RHS->getScope() && InlinedAt == RHS->getInlinedAt();
240 static inline MDLocation *getEmptyKey() {
241 return DenseMapInfo<MDLocation *>::getEmptyKey();
243 static inline MDLocation *getTombstoneKey() {
244 return DenseMapInfo<MDLocation *>::getTombstoneKey();
246 static unsigned getHashValue(const KeyTy &Key) {
247 return hash_combine(Key.Line, Key.Column, Key.Scope, Key.InlinedAt);
249 static unsigned getHashValue(const MDLocation *U) {
250 return getHashValue(KeyTy(U));
252 static bool isEqual(const KeyTy &LHS, const MDLocation *RHS) {
255 static bool isEqual(const MDLocation *LHS, const MDLocation *RHS) {
260 class LLVMContextImpl {
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;
266 LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
267 void *InlineAsmDiagContext;
269 LLVMContext::DiagnosticHandlerTy DiagnosticHandler;
270 void *DiagnosticContext;
271 bool RespectDiagnosticFilters;
273 LLVMContext::YieldCallbackTy YieldCallback;
274 void *YieldOpaqueHandle;
276 typedef DenseMap<APInt, ConstantInt *, DenseMapAPIntKeyInfo> IntMapTy;
277 IntMapTy IntConstants;
279 typedef DenseMap<APFloat, ConstantFP *, DenseMapAPFloatKeyInfo> FPMapTy;
282 FoldingSet<AttributeImpl> AttrsSet;
283 FoldingSet<AttributeSetImpl> AttrsLists;
284 FoldingSet<AttributeSetNode> AttrsSetNodes;
286 StringMap<MDString> MDStringCache;
287 DenseMap<Value *, ValueAsMetadata *> ValuesAsMetadata;
288 DenseMap<Metadata *, MetadataAsValue *> MetadataAsValues;
290 DenseSet<MDTuple *, MDTupleInfo> MDTuples;
291 DenseSet<MDLocation *, MDLocationInfo> MDLocations;
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;
299 DenseMap<Type*, ConstantAggregateZero*> CAZConstants;
301 typedef ConstantUniqueMap<ConstantArray> ArrayConstantsTy;
302 ArrayConstantsTy ArrayConstants;
304 typedef ConstantUniqueMap<ConstantStruct> StructConstantsTy;
305 StructConstantsTy StructConstants;
307 typedef ConstantUniqueMap<ConstantVector> VectorConstantsTy;
308 VectorConstantsTy VectorConstants;
310 DenseMap<PointerType*, ConstantPointerNull*> CPNConstants;
312 DenseMap<Type*, UndefValue*> UVConstants;
314 StringMap<ConstantDataSequential*> CDSConstants;
316 DenseMap<std::pair<const Function *, const BasicBlock *>, BlockAddress *>
318 ConstantUniqueMap<ConstantExpr> ExprConstants;
320 ConstantUniqueMap<InlineAsm> InlineAsms;
322 ConstantInt *TheTrueVal;
323 ConstantInt *TheFalseVal;
325 LeakDetectorImpl<Value> LLVMObjects;
326 LeakDetectorImpl<Metadata> LLVMMDObjects;
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;
334 /// TypeAllocator - All dynamically allocated types are allocated from this.
335 /// They live forever until the context is torn down.
336 BumpPtrAllocator TypeAllocator;
338 DenseMap<unsigned, IntegerType*> IntegerTypes;
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;
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;
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;
359 /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
360 StringMap<unsigned> CustomMDKindNames;
362 typedef std::pair<unsigned, TrackingMDNodeRef> MDPairTy;
363 typedef SmallVector<MDPairTy, 2> MDMapTy;
365 /// MetadataStore - Collection of per-instruction metadata used in this
367 DenseMap<const Instruction *, MDMapTy> MetadataStore;
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;
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;
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;
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
387 typedef DenseMap<const Function *, ReturnInst *> PrologueDataMapTy;
388 PrologueDataMapTy PrologueDataMap;
390 int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
391 int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
393 LLVMContextImpl(LLVMContext &C);