blob: ee31814c055741da12605d723d319f80469462ba [file] [log] [blame]
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +00001//===-- LLVMContextImpl.h - The LLVMContextImpl opaque class ----*- C++ -*-===//
Owen Anderson8e66e0b2009-06-30 00:48:55 +00002//
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//===----------------------------------------------------------------------===//
Owen Anderson36f62e52009-06-30 17:06:46 +00009//
10// This file declares LLVMContextImpl, the opaque implementation
11// of LLVMContext.
12//
13//===----------------------------------------------------------------------===//
Owen Anderson8e66e0b2009-06-30 00:48:55 +000014
15#ifndef LLVM_LLVMCONTEXT_IMPL_H
16#define LLVM_LLVMCONTEXT_IMPL_H
17
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000018#include "llvm/LLVMContext.h"
Bill Wendlinge38b8042012-09-26 21:07:29 +000019#include "AttributesImpl.h"
Owen Andersonafd0c4c2009-08-04 22:41:48 +000020#include "ConstantsContext.h"
Owen Anderson6d549d62009-08-19 17:07:46 +000021#include "LeaksContext.h"
Owen Andersonedb4a702009-07-24 23:12:02 +000022#include "llvm/Constants.h"
Owen Anderson2ad52172009-07-21 02:47:59 +000023#include "llvm/DerivedTypes.h"
Jeffrey Yasskincd3706c2010-03-21 22:08:41 +000024#include "llvm/Metadata.h"
Chris Lattnera0566972009-12-29 09:01:33 +000025#include "llvm/Support/ValueHandle.h"
Owen Andersonc277dc42009-07-16 19:05:41 +000026#include "llvm/ADT/APFloat.h"
Owen Anderson20b34ac2009-07-16 18:04:31 +000027#include "llvm/ADT/APInt.h"
Jay Foadc365eea2011-06-22 08:50:06 +000028#include "llvm/ADT/ArrayRef.h"
Owen Anderson20b34ac2009-07-16 18:04:31 +000029#include "llvm/ADT/DenseMap.h"
Owen Anderson4118dde2009-07-16 23:44:30 +000030#include "llvm/ADT/FoldingSet.h"
Jeffrey Yasskin28f24482009-12-17 19:55:06 +000031#include "llvm/ADT/SmallPtrSet.h"
Owen Anderson69ab4162009-07-16 22:11:26 +000032#include "llvm/ADT/StringMap.h"
Jay Foad529776c2012-02-23 09:17:40 +000033#include "llvm/ADT/Hashing.h"
Owen Anderson909f6002009-07-23 23:25:33 +000034#include <vector>
Owen Anderson39ede7b2009-07-21 20:13:12 +000035
Owen Anderson20b34ac2009-07-16 18:04:31 +000036namespace llvm {
Owen Andersonedb4a702009-07-24 23:12:02 +000037
Owen Anderson20b34ac2009-07-16 18:04:31 +000038class ConstantInt;
Owen Andersonc277dc42009-07-16 19:05:41 +000039class ConstantFP;
Benjamin Kramer78c3bcb2009-08-11 17:45:13 +000040class LLVMContext;
Owen Anderson20b34ac2009-07-16 18:04:31 +000041class Type;
Owen Anderson4118dde2009-07-16 23:44:30 +000042class Value;
Owen Anderson20b34ac2009-07-16 18:04:31 +000043
44struct DenseMapAPIntKeyInfo {
45 struct KeyTy {
46 APInt val;
Chris Lattner229907c2011-07-18 04:54:35 +000047 Type* type;
48 KeyTy(const APInt& V, Type* Ty) : val(V), type(Ty) {}
Owen Anderson20b34ac2009-07-16 18:04:31 +000049 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
50 bool operator==(const KeyTy& that) const {
51 return type == that.type && this->val == that.val;
52 }
53 bool operator!=(const KeyTy& that) const {
54 return !this->operator==(that);
55 }
Chandler Carruth71bd7d12012-03-04 12:02:57 +000056 friend hash_code hash_value(const KeyTy &Key) {
57 return hash_combine(Key.type, Key.val);
58 }
Owen Anderson20b34ac2009-07-16 18:04:31 +000059 };
60 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
61 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
62 static unsigned getHashValue(const KeyTy &Key) {
Chandler Carruth71bd7d12012-03-04 12:02:57 +000063 return static_cast<unsigned>(hash_value(Key));
Owen Anderson20b34ac2009-07-16 18:04:31 +000064 }
65 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
66 return LHS == RHS;
67 }
Owen Anderson20b34ac2009-07-16 18:04:31 +000068};
69
Owen Andersonc277dc42009-07-16 19:05:41 +000070struct DenseMapAPFloatKeyInfo {
71 struct KeyTy {
72 APFloat val;
73 KeyTy(const APFloat& V) : val(V){}
74 KeyTy(const KeyTy& that) : val(that.val) {}
75 bool operator==(const KeyTy& that) const {
76 return this->val.bitwiseIsEqual(that.val);
77 }
78 bool operator!=(const KeyTy& that) const {
79 return !this->operator==(that);
80 }
Chandler Carruth71bd7d12012-03-04 12:02:57 +000081 friend hash_code hash_value(const KeyTy &Key) {
82 return hash_combine(Key.val);
83 }
Owen Andersonc277dc42009-07-16 19:05:41 +000084 };
85 static inline KeyTy getEmptyKey() {
86 return KeyTy(APFloat(APFloat::Bogus,1));
87 }
88 static inline KeyTy getTombstoneKey() {
89 return KeyTy(APFloat(APFloat::Bogus,2));
90 }
91 static unsigned getHashValue(const KeyTy &Key) {
Chandler Carruth71bd7d12012-03-04 12:02:57 +000092 return static_cast<unsigned>(hash_value(Key));
Owen Andersonc277dc42009-07-16 19:05:41 +000093 }
94 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
95 return LHS == RHS;
96 }
Owen Andersonc277dc42009-07-16 19:05:41 +000097};
98
Jay Foad529776c2012-02-23 09:17:40 +000099struct AnonStructTypeKeyInfo {
100 struct KeyTy {
101 ArrayRef<Type*> ETypes;
102 bool isPacked;
103 KeyTy(const ArrayRef<Type*>& E, bool P) :
104 ETypes(E), isPacked(P) {}
105 KeyTy(const KeyTy& that) :
106 ETypes(that.ETypes), isPacked(that.isPacked) {}
107 KeyTy(const StructType* ST) :
108 ETypes(ArrayRef<Type*>(ST->element_begin(), ST->element_end())),
109 isPacked(ST->isPacked()) {}
110 bool operator==(const KeyTy& that) const {
111 if (isPacked != that.isPacked)
112 return false;
113 if (ETypes != that.ETypes)
114 return false;
115 return true;
116 }
117 bool operator!=(const KeyTy& that) const {
118 return !this->operator==(that);
119 }
120 };
121 static inline StructType* getEmptyKey() {
122 return DenseMapInfo<StructType*>::getEmptyKey();
123 }
124 static inline StructType* getTombstoneKey() {
125 return DenseMapInfo<StructType*>::getTombstoneKey();
126 }
127 static unsigned getHashValue(const KeyTy& Key) {
Chandler Carruth1d03a3b2012-03-01 18:55:25 +0000128 return hash_combine(hash_combine_range(Key.ETypes.begin(),
129 Key.ETypes.end()),
130 Key.isPacked);
Jay Foad529776c2012-02-23 09:17:40 +0000131 }
132 static unsigned getHashValue(const StructType *ST) {
133 return getHashValue(KeyTy(ST));
134 }
135 static bool isEqual(const KeyTy& LHS, const StructType *RHS) {
136 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
137 return false;
138 return LHS == KeyTy(RHS);
139 }
140 static bool isEqual(const StructType *LHS, const StructType *RHS) {
141 return LHS == RHS;
142 }
143};
144
145struct FunctionTypeKeyInfo {
146 struct KeyTy {
147 const Type *ReturnType;
148 ArrayRef<Type*> Params;
149 bool isVarArg;
150 KeyTy(const Type* R, const ArrayRef<Type*>& P, bool V) :
151 ReturnType(R), Params(P), isVarArg(V) {}
152 KeyTy(const KeyTy& that) :
153 ReturnType(that.ReturnType),
154 Params(that.Params),
155 isVarArg(that.isVarArg) {}
156 KeyTy(const FunctionType* FT) :
157 ReturnType(FT->getReturnType()),
158 Params(ArrayRef<Type*>(FT->param_begin(), FT->param_end())),
159 isVarArg(FT->isVarArg()) {}
160 bool operator==(const KeyTy& that) const {
161 if (ReturnType != that.ReturnType)
162 return false;
163 if (isVarArg != that.isVarArg)
164 return false;
165 if (Params != that.Params)
166 return false;
167 return true;
168 }
169 bool operator!=(const KeyTy& that) const {
170 return !this->operator==(that);
171 }
172 };
173 static inline FunctionType* getEmptyKey() {
174 return DenseMapInfo<FunctionType*>::getEmptyKey();
175 }
176 static inline FunctionType* getTombstoneKey() {
177 return DenseMapInfo<FunctionType*>::getTombstoneKey();
178 }
179 static unsigned getHashValue(const KeyTy& Key) {
Chandler Carruth1d03a3b2012-03-01 18:55:25 +0000180 return hash_combine(Key.ReturnType,
181 hash_combine_range(Key.Params.begin(),
182 Key.Params.end()),
183 Key.isVarArg);
Jay Foad529776c2012-02-23 09:17:40 +0000184 }
185 static unsigned getHashValue(const FunctionType *FT) {
186 return getHashValue(KeyTy(FT));
187 }
188 static bool isEqual(const KeyTy& LHS, const FunctionType *RHS) {
189 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
190 return false;
191 return LHS == KeyTy(RHS);
192 }
193 static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) {
194 return LHS == RHS;
195 }
196};
197
Benjamin Kramer2335a5c2012-04-11 14:06:54 +0000198// Provide a FoldingSetTrait::Equals specialization for MDNode that can use a
199// shortcut to avoid comparing all operands.
200template<> struct FoldingSetTrait<MDNode> : DefaultFoldingSetTrait<MDNode> {
201 static bool Equals(const MDNode &X, const FoldingSetNodeID &ID,
202 unsigned IDHash, FoldingSetNodeID &TempID) {
203 assert(!X.isNotUniqued() && "Non-uniqued MDNode in FoldingSet?");
204 // First, check if the cached hashes match. If they don't we can skip the
205 // expensive operand walk.
206 if (X.Hash != IDHash)
207 return false;
208
209 // If they match we have to compare the operands.
210 X.Profile(TempID);
211 return TempID == ID;
212 }
213 static unsigned ComputeHash(const MDNode &X, FoldingSetNodeID &) {
214 return X.Hash; // Return cached hash.
215 }
216};
217
Chris Lattner8cb2aeb2010-04-01 00:37:44 +0000218/// DebugRecVH - This is a CallbackVH used to keep the Scope -> index maps
219/// up to date as MDNodes mutate. This class is implemented in DebugLoc.cpp.
220class DebugRecVH : public CallbackVH {
221 /// Ctx - This is the LLVM Context being referenced.
222 LLVMContextImpl *Ctx;
223
224 /// Idx - The index into either ScopeRecordIdx or ScopeInlinedAtRecords that
225 /// this reference lives in. If this is zero, then it represents a
226 /// non-canonical entry that has no DenseMap value. This can happen due to
227 /// RAUW.
228 int Idx;
229public:
230 DebugRecVH(MDNode *n, LLVMContextImpl *ctx, int idx)
231 : CallbackVH(n), Ctx(ctx), Idx(idx) {}
232
233 MDNode *get() const {
234 return cast_or_null<MDNode>(getValPtr());
235 }
236
237 virtual void deleted();
238 virtual void allUsesReplacedWith(Value *VNew);
239};
240
Benjamin Kramer78c3bcb2009-08-11 17:45:13 +0000241class LLVMContextImpl {
242public:
Owen Anderson8e89e412010-09-08 18:03:32 +0000243 /// OwnedModules - The set of modules instantiated in this context, and which
244 /// will be automatically deleted if this context is deleted.
245 SmallPtrSet<Module*, 4> OwnedModules;
246
Chris Lattnerb0e36082010-11-17 08:13:01 +0000247 LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
248 void *InlineAsmDiagContext;
Chris Lattner60955d42010-04-06 00:44:45 +0000249
Owen Anderson20b34ac2009-07-16 18:04:31 +0000250 typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
Owen Andersonafd0c4c2009-08-04 22:41:48 +0000251 DenseMapAPIntKeyInfo> IntMapTy;
Owen Anderson20b34ac2009-07-16 18:04:31 +0000252 IntMapTy IntConstants;
253
Owen Andersonc277dc42009-07-16 19:05:41 +0000254 typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
Owen Andersonafd0c4c2009-08-04 22:41:48 +0000255 DenseMapAPFloatKeyInfo> FPMapTy;
Owen Andersonc277dc42009-07-16 19:05:41 +0000256 FPMapTy FPConstants;
Bill Wendlinge38b8042012-09-26 21:07:29 +0000257
258 FoldingSet<AttributesImpl> AttrsSet;
Owen Andersonc277dc42009-07-16 19:05:41 +0000259
Bill Wendlingc4c568b2012-04-10 20:12:16 +0000260 StringMap<Value*> MDStringCache;
Bill Wendlinge38b8042012-09-26 21:07:29 +0000261
Devang Patelf7188322009-09-03 01:39:20 +0000262 FoldingSet<MDNode> MDNodeSet;
Bill Wendlinge38b8042012-09-26 21:07:29 +0000263
Jeffrey Yasskin2cc24762010-03-13 01:26:15 +0000264 // MDNodes may be uniqued or not uniqued. When they're not uniqued, they
265 // aren't in the MDNodeSet, but they're still shared between objects, so no
266 // one object can destroy them. This set allows us to at least destroy them
267 // on Context destruction.
268 SmallPtrSet<MDNode*, 1> NonUniquedMDNodes;
Devang Patelf7188322009-09-03 01:39:20 +0000269
Chris Lattnerc7f9fd42012-01-23 15:20:12 +0000270 DenseMap<Type*, ConstantAggregateZero*> CAZConstants;
Owen Anderson13234f82009-08-10 18:16:08 +0000271
Talin46e9b442012-02-05 20:54:10 +0000272 typedef ConstantAggrUniqueMap<ArrayType, ConstantArray> ArrayConstantsTy;
Owen Andersonedb4a702009-07-24 23:12:02 +0000273 ArrayConstantsTy ArrayConstants;
Owen Anderson39ede7b2009-07-21 20:13:12 +0000274
Talin46e9b442012-02-05 20:54:10 +0000275 typedef ConstantAggrUniqueMap<StructType, ConstantStruct> StructConstantsTy;
Owen Andersonedb4a702009-07-24 23:12:02 +0000276 StructConstantsTy StructConstants;
Owen Anderson909f6002009-07-23 23:25:33 +0000277
Talin46e9b442012-02-05 20:54:10 +0000278 typedef ConstantAggrUniqueMap<VectorType, ConstantVector> VectorConstantsTy;
Owen Andersonedb4a702009-07-24 23:12:02 +0000279 VectorConstantsTy VectorConstants;
Owen Anderson0348a132009-07-24 00:36:24 +0000280
Chris Lattnerc7f9fd42012-01-23 15:20:12 +0000281 DenseMap<PointerType*, ConstantPointerNull*> CPNConstants;
282
283 DenseMap<Type*, UndefValue*> UVConstants;
Owen Andersonc8c30262009-07-31 22:45:43 +0000284
Chris Lattner3756b912012-01-23 22:57:10 +0000285 StringMap<ConstantDataSequential*> CDSConstants;
286
287
Chris Lattner31b132c2009-10-28 00:01:44 +0000288 DenseMap<std::pair<Function*, BasicBlock*> , BlockAddress*> BlockAddresses;
Jay Foadc365eea2011-06-22 08:50:06 +0000289 ConstantUniqueMap<ExprMapKeyType, const ExprMapKeyType&, Type, ConstantExpr>
290 ExprConstants;
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000291
Jay Foadc365eea2011-06-22 08:50:06 +0000292 ConstantUniqueMap<InlineAsmKeyType, const InlineAsmKeyType&, PointerType,
293 InlineAsm> InlineAsms;
Owen Anderson1584a292009-08-04 20:25:11 +0000294
Owen Anderson2ad52172009-07-21 02:47:59 +0000295 ConstantInt *TheTrueVal;
296 ConstantInt *TheFalseVal;
297
Owen Anderson6d549d62009-08-19 17:07:46 +0000298 LeakDetectorImpl<Value> LLVMObjects;
299
Dan Gohman97d2cb82009-08-25 16:00:35 +0000300 // Basic type instances.
Dan Gohman518cda42011-12-17 00:04:22 +0000301 Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000302 Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy;
303 IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty;
Dan Gohman97d2cb82009-08-25 16:00:35 +0000304
Chris Lattner07bd69c2011-07-15 05:49:15 +0000305
306 /// TypeAllocator - All dynamically allocated types are allocated from this.
307 /// They live forever until the context is torn down.
308 BumpPtrAllocator TypeAllocator;
309
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000310 DenseMap<unsigned, IntegerType*> IntegerTypes;
311
Jay Foad529776c2012-02-23 09:17:40 +0000312 typedef DenseMap<FunctionType*, bool, FunctionTypeKeyInfo> FunctionTypeMap;
313 FunctionTypeMap FunctionTypes;
314 typedef DenseMap<StructType*, bool, AnonStructTypeKeyInfo> StructTypeMap;
315 StructTypeMap AnonStructTypes;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000316 StringMap<StructType*> NamedStructTypes;
317 unsigned NamedStructTypesUniqueID;
318
319 DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
320 DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes;
321 DenseMap<Type*, PointerType*> PointerTypes; // Pointers in AddrSpace = 0
322 DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;
Jeffrey Yasskinc660b232010-02-11 06:41:30 +0000323
Jeffrey Yasskin28f24482009-12-17 19:55:06 +0000324
Owen Andersone8f21852009-08-18 18:28:58 +0000325 /// ValueHandles - This map keeps track of all of the value handles that are
326 /// watching a Value*. The Value::HasValueHandle bit is used to know
327 // whether or not a value has an entry in this map.
328 typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
329 ValueHandlesTy ValueHandles;
330
Chris Lattnera0566972009-12-29 09:01:33 +0000331 /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
332 StringMap<unsigned> CustomMDKindNames;
333
334 typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
335 typedef SmallVector<MDPairTy, 2> MDMapTy;
336
337 /// MetadataStore - Collection of per-instruction metadata used in this
338 /// context.
339 DenseMap<const Instruction *, MDMapTy> MetadataStore;
340
Chris Lattner8cb2aeb2010-04-01 00:37:44 +0000341 /// ScopeRecordIdx - This is the index in ScopeRecords for an MDNode scope
342 /// entry with no "inlined at" element.
343 DenseMap<MDNode*, int> ScopeRecordIdx;
344
345 /// ScopeRecords - These are the actual mdnodes (in a value handle) for an
346 /// index. The ValueHandle ensures that ScopeRecordIdx stays up to date if
347 /// the MDNode is RAUW'd.
348 std::vector<DebugRecVH> ScopeRecords;
349
350 /// ScopeInlinedAtIdx - This is the index in ScopeInlinedAtRecords for an
351 /// scope/inlined-at pair.
352 DenseMap<std::pair<MDNode*, MDNode*>, int> ScopeInlinedAtIdx;
353
354 /// ScopeInlinedAtRecords - These are the actual mdnodes (in value handles)
355 /// for an index. The ValueHandle ensures that ScopeINlinedAtIdx stays up
356 /// to date.
357 std::vector<std::pair<DebugRecVH, DebugRecVH> > ScopeInlinedAtRecords;
358
359 int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
360 int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
361
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000362 LLVMContextImpl(LLVMContext &C);
363 ~LLVMContextImpl();
Owen Anderson8e66e0b2009-06-30 00:48:55 +0000364};
365
366}
367
Owen Anderson36f62e52009-06-30 17:06:46 +0000368#endif