blob: 9aba73781c4e4d111edb578e04cc35a8c2afde45 [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"
Owen Andersonafd0c4c2009-08-04 22:41:48 +000019#include "ConstantsContext.h"
Owen Anderson6d549d62009-08-19 17:07:46 +000020#include "LeaksContext.h"
Owen Andersonedb4a702009-07-24 23:12:02 +000021#include "llvm/Constants.h"
Owen Anderson2ad52172009-07-21 02:47:59 +000022#include "llvm/DerivedTypes.h"
Jeffrey Yasskincd3706c2010-03-21 22:08:41 +000023#include "llvm/Metadata.h"
Chris Lattnera0566972009-12-29 09:01:33 +000024#include "llvm/Support/ValueHandle.h"
Owen Andersonc277dc42009-07-16 19:05:41 +000025#include "llvm/ADT/APFloat.h"
Owen Anderson20b34ac2009-07-16 18:04:31 +000026#include "llvm/ADT/APInt.h"
Jay Foadc365eea2011-06-22 08:50:06 +000027#include "llvm/ADT/ArrayRef.h"
Owen Anderson20b34ac2009-07-16 18:04:31 +000028#include "llvm/ADT/DenseMap.h"
Owen Anderson4118dde2009-07-16 23:44:30 +000029#include "llvm/ADT/FoldingSet.h"
Jeffrey Yasskin28f24482009-12-17 19:55:06 +000030#include "llvm/ADT/SmallPtrSet.h"
Owen Anderson69ab4162009-07-16 22:11:26 +000031#include "llvm/ADT/StringMap.h"
Jay Foad529776c2012-02-23 09:17:40 +000032#include "llvm/ADT/Hashing.h"
Owen Anderson909f6002009-07-23 23:25:33 +000033#include <vector>
Owen Anderson39ede7b2009-07-21 20:13:12 +000034
Owen Anderson20b34ac2009-07-16 18:04:31 +000035namespace llvm {
Owen Andersonedb4a702009-07-24 23:12:02 +000036
Owen Anderson20b34ac2009-07-16 18:04:31 +000037class ConstantInt;
Owen Andersonc277dc42009-07-16 19:05:41 +000038class ConstantFP;
Benjamin Kramer78c3bcb2009-08-11 17:45:13 +000039class LLVMContext;
Owen Anderson20b34ac2009-07-16 18:04:31 +000040class Type;
Owen Anderson4118dde2009-07-16 23:44:30 +000041class Value;
Owen Anderson20b34ac2009-07-16 18:04:31 +000042
43struct DenseMapAPIntKeyInfo {
44 struct KeyTy {
45 APInt val;
Chris Lattner229907c2011-07-18 04:54:35 +000046 Type* type;
47 KeyTy(const APInt& V, Type* Ty) : val(V), type(Ty) {}
Owen Anderson20b34ac2009-07-16 18:04:31 +000048 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
49 bool operator==(const KeyTy& that) const {
50 return type == that.type && this->val == that.val;
51 }
52 bool operator!=(const KeyTy& that) const {
53 return !this->operator==(that);
54 }
55 };
56 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
57 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
58 static unsigned getHashValue(const KeyTy &Key) {
59 return DenseMapInfo<void*>::getHashValue(Key.type) ^
60 Key.val.getHashValue();
61 }
62 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
63 return LHS == RHS;
64 }
Owen Anderson20b34ac2009-07-16 18:04:31 +000065};
66
Owen Andersonc277dc42009-07-16 19:05:41 +000067struct DenseMapAPFloatKeyInfo {
68 struct KeyTy {
69 APFloat val;
70 KeyTy(const APFloat& V) : val(V){}
71 KeyTy(const KeyTy& that) : val(that.val) {}
72 bool operator==(const KeyTy& that) const {
73 return this->val.bitwiseIsEqual(that.val);
74 }
75 bool operator!=(const KeyTy& that) const {
76 return !this->operator==(that);
77 }
78 };
79 static inline KeyTy getEmptyKey() {
80 return KeyTy(APFloat(APFloat::Bogus,1));
81 }
82 static inline KeyTy getTombstoneKey() {
83 return KeyTy(APFloat(APFloat::Bogus,2));
84 }
85 static unsigned getHashValue(const KeyTy &Key) {
86 return Key.val.getHashValue();
87 }
88 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
89 return LHS == RHS;
90 }
Owen Andersonc277dc42009-07-16 19:05:41 +000091};
92
Jay Foad529776c2012-02-23 09:17:40 +000093struct AnonStructTypeKeyInfo {
94 struct KeyTy {
95 ArrayRef<Type*> ETypes;
96 bool isPacked;
97 KeyTy(const ArrayRef<Type*>& E, bool P) :
98 ETypes(E), isPacked(P) {}
99 KeyTy(const KeyTy& that) :
100 ETypes(that.ETypes), isPacked(that.isPacked) {}
101 KeyTy(const StructType* ST) :
102 ETypes(ArrayRef<Type*>(ST->element_begin(), ST->element_end())),
103 isPacked(ST->isPacked()) {}
104 bool operator==(const KeyTy& that) const {
105 if (isPacked != that.isPacked)
106 return false;
107 if (ETypes != that.ETypes)
108 return false;
109 return true;
110 }
111 bool operator!=(const KeyTy& that) const {
112 return !this->operator==(that);
113 }
114 };
115 static inline StructType* getEmptyKey() {
116 return DenseMapInfo<StructType*>::getEmptyKey();
117 }
118 static inline StructType* getTombstoneKey() {
119 return DenseMapInfo<StructType*>::getTombstoneKey();
120 }
121 static unsigned getHashValue(const KeyTy& Key) {
122 GeneralHash Hash;
123 Hash.add(Key.ETypes);
124 Hash.add(Key.isPacked);
125 return Hash.finish();
126 }
127 static unsigned getHashValue(const StructType *ST) {
128 return getHashValue(KeyTy(ST));
129 }
130 static bool isEqual(const KeyTy& LHS, const StructType *RHS) {
131 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
132 return false;
133 return LHS == KeyTy(RHS);
134 }
135 static bool isEqual(const StructType *LHS, const StructType *RHS) {
136 return LHS == RHS;
137 }
138};
139
140struct FunctionTypeKeyInfo {
141 struct KeyTy {
142 const Type *ReturnType;
143 ArrayRef<Type*> Params;
144 bool isVarArg;
145 KeyTy(const Type* R, const ArrayRef<Type*>& P, bool V) :
146 ReturnType(R), Params(P), isVarArg(V) {}
147 KeyTy(const KeyTy& that) :
148 ReturnType(that.ReturnType),
149 Params(that.Params),
150 isVarArg(that.isVarArg) {}
151 KeyTy(const FunctionType* FT) :
152 ReturnType(FT->getReturnType()),
153 Params(ArrayRef<Type*>(FT->param_begin(), FT->param_end())),
154 isVarArg(FT->isVarArg()) {}
155 bool operator==(const KeyTy& that) const {
156 if (ReturnType != that.ReturnType)
157 return false;
158 if (isVarArg != that.isVarArg)
159 return false;
160 if (Params != that.Params)
161 return false;
162 return true;
163 }
164 bool operator!=(const KeyTy& that) const {
165 return !this->operator==(that);
166 }
167 };
168 static inline FunctionType* getEmptyKey() {
169 return DenseMapInfo<FunctionType*>::getEmptyKey();
170 }
171 static inline FunctionType* getTombstoneKey() {
172 return DenseMapInfo<FunctionType*>::getTombstoneKey();
173 }
174 static unsigned getHashValue(const KeyTy& Key) {
175 GeneralHash Hash;
176 Hash.add(Key.ReturnType);
177 Hash.add(Key.Params);
178 Hash.add(Key.isVarArg);
179 return Hash.finish();
180 }
181 static unsigned getHashValue(const FunctionType *FT) {
182 return getHashValue(KeyTy(FT));
183 }
184 static bool isEqual(const KeyTy& LHS, const FunctionType *RHS) {
185 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
186 return false;
187 return LHS == KeyTy(RHS);
188 }
189 static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) {
190 return LHS == RHS;
191 }
192};
193
Chris Lattner8cb2aeb2010-04-01 00:37:44 +0000194/// DebugRecVH - This is a CallbackVH used to keep the Scope -> index maps
195/// up to date as MDNodes mutate. This class is implemented in DebugLoc.cpp.
196class DebugRecVH : public CallbackVH {
197 /// Ctx - This is the LLVM Context being referenced.
198 LLVMContextImpl *Ctx;
199
200 /// Idx - The index into either ScopeRecordIdx or ScopeInlinedAtRecords that
201 /// this reference lives in. If this is zero, then it represents a
202 /// non-canonical entry that has no DenseMap value. This can happen due to
203 /// RAUW.
204 int Idx;
205public:
206 DebugRecVH(MDNode *n, LLVMContextImpl *ctx, int idx)
207 : CallbackVH(n), Ctx(ctx), Idx(idx) {}
208
209 MDNode *get() const {
210 return cast_or_null<MDNode>(getValPtr());
211 }
212
213 virtual void deleted();
214 virtual void allUsesReplacedWith(Value *VNew);
215};
216
Benjamin Kramer78c3bcb2009-08-11 17:45:13 +0000217class LLVMContextImpl {
218public:
Owen Anderson8e89e412010-09-08 18:03:32 +0000219 /// OwnedModules - The set of modules instantiated in this context, and which
220 /// will be automatically deleted if this context is deleted.
221 SmallPtrSet<Module*, 4> OwnedModules;
222
Chris Lattnerb0e36082010-11-17 08:13:01 +0000223 LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
224 void *InlineAsmDiagContext;
Chris Lattner60955d42010-04-06 00:44:45 +0000225
Owen Anderson20b34ac2009-07-16 18:04:31 +0000226 typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
Owen Andersonafd0c4c2009-08-04 22:41:48 +0000227 DenseMapAPIntKeyInfo> IntMapTy;
Owen Anderson20b34ac2009-07-16 18:04:31 +0000228 IntMapTy IntConstants;
229
Owen Andersonc277dc42009-07-16 19:05:41 +0000230 typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
Owen Andersonafd0c4c2009-08-04 22:41:48 +0000231 DenseMapAPFloatKeyInfo> FPMapTy;
Owen Andersonc277dc42009-07-16 19:05:41 +0000232 FPMapTy FPConstants;
233
Owen Anderson69ab4162009-07-16 22:11:26 +0000234 StringMap<MDString*> MDStringCache;
235
Devang Patelf7188322009-09-03 01:39:20 +0000236 FoldingSet<MDNode> MDNodeSet;
Jeffrey Yasskin2cc24762010-03-13 01:26:15 +0000237 // MDNodes may be uniqued or not uniqued. When they're not uniqued, they
238 // aren't in the MDNodeSet, but they're still shared between objects, so no
239 // one object can destroy them. This set allows us to at least destroy them
240 // on Context destruction.
241 SmallPtrSet<MDNode*, 1> NonUniquedMDNodes;
Devang Patelf7188322009-09-03 01:39:20 +0000242
Chris Lattnerc7f9fd42012-01-23 15:20:12 +0000243 DenseMap<Type*, ConstantAggregateZero*> CAZConstants;
Owen Anderson13234f82009-08-10 18:16:08 +0000244
Talin46e9b442012-02-05 20:54:10 +0000245 typedef ConstantAggrUniqueMap<ArrayType, ConstantArray> ArrayConstantsTy;
Owen Andersonedb4a702009-07-24 23:12:02 +0000246 ArrayConstantsTy ArrayConstants;
Owen Anderson39ede7b2009-07-21 20:13:12 +0000247
Talin46e9b442012-02-05 20:54:10 +0000248 typedef ConstantAggrUniqueMap<StructType, ConstantStruct> StructConstantsTy;
Owen Andersonedb4a702009-07-24 23:12:02 +0000249 StructConstantsTy StructConstants;
Owen Anderson909f6002009-07-23 23:25:33 +0000250
Talin46e9b442012-02-05 20:54:10 +0000251 typedef ConstantAggrUniqueMap<VectorType, ConstantVector> VectorConstantsTy;
Owen Andersonedb4a702009-07-24 23:12:02 +0000252 VectorConstantsTy VectorConstants;
Owen Anderson0348a132009-07-24 00:36:24 +0000253
Chris Lattnerc7f9fd42012-01-23 15:20:12 +0000254 DenseMap<PointerType*, ConstantPointerNull*> CPNConstants;
255
256 DenseMap<Type*, UndefValue*> UVConstants;
Owen Andersonc8c30262009-07-31 22:45:43 +0000257
Chris Lattner3756b912012-01-23 22:57:10 +0000258 StringMap<ConstantDataSequential*> CDSConstants;
259
260
Chris Lattner31b132c2009-10-28 00:01:44 +0000261 DenseMap<std::pair<Function*, BasicBlock*> , BlockAddress*> BlockAddresses;
Jay Foadc365eea2011-06-22 08:50:06 +0000262 ConstantUniqueMap<ExprMapKeyType, const ExprMapKeyType&, Type, ConstantExpr>
263 ExprConstants;
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000264
Jay Foadc365eea2011-06-22 08:50:06 +0000265 ConstantUniqueMap<InlineAsmKeyType, const InlineAsmKeyType&, PointerType,
266 InlineAsm> InlineAsms;
Owen Anderson1584a292009-08-04 20:25:11 +0000267
Owen Anderson2ad52172009-07-21 02:47:59 +0000268 ConstantInt *TheTrueVal;
269 ConstantInt *TheFalseVal;
270
Owen Anderson6d549d62009-08-19 17:07:46 +0000271 LeakDetectorImpl<Value> LLVMObjects;
272
Dan Gohman97d2cb82009-08-25 16:00:35 +0000273 // Basic type instances.
Dan Gohman518cda42011-12-17 00:04:22 +0000274 Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000275 Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy;
276 IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty;
Dan Gohman97d2cb82009-08-25 16:00:35 +0000277
Chris Lattner07bd69c2011-07-15 05:49:15 +0000278
279 /// TypeAllocator - All dynamically allocated types are allocated from this.
280 /// They live forever until the context is torn down.
281 BumpPtrAllocator TypeAllocator;
282
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000283 DenseMap<unsigned, IntegerType*> IntegerTypes;
284
Jay Foad529776c2012-02-23 09:17:40 +0000285 typedef DenseMap<FunctionType*, bool, FunctionTypeKeyInfo> FunctionTypeMap;
286 FunctionTypeMap FunctionTypes;
287 typedef DenseMap<StructType*, bool, AnonStructTypeKeyInfo> StructTypeMap;
288 StructTypeMap AnonStructTypes;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000289 StringMap<StructType*> NamedStructTypes;
290 unsigned NamedStructTypesUniqueID;
291
292 DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
293 DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes;
294 DenseMap<Type*, PointerType*> PointerTypes; // Pointers in AddrSpace = 0
295 DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;
Jeffrey Yasskinc660b232010-02-11 06:41:30 +0000296
Jeffrey Yasskin28f24482009-12-17 19:55:06 +0000297
Owen Andersone8f21852009-08-18 18:28:58 +0000298 /// ValueHandles - This map keeps track of all of the value handles that are
299 /// watching a Value*. The Value::HasValueHandle bit is used to know
300 // whether or not a value has an entry in this map.
301 typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
302 ValueHandlesTy ValueHandles;
303
Chris Lattnera0566972009-12-29 09:01:33 +0000304 /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
305 StringMap<unsigned> CustomMDKindNames;
306
307 typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
308 typedef SmallVector<MDPairTy, 2> MDMapTy;
309
310 /// MetadataStore - Collection of per-instruction metadata used in this
311 /// context.
312 DenseMap<const Instruction *, MDMapTy> MetadataStore;
313
Chris Lattner8cb2aeb2010-04-01 00:37:44 +0000314 /// ScopeRecordIdx - This is the index in ScopeRecords for an MDNode scope
315 /// entry with no "inlined at" element.
316 DenseMap<MDNode*, int> ScopeRecordIdx;
317
318 /// ScopeRecords - These are the actual mdnodes (in a value handle) for an
319 /// index. The ValueHandle ensures that ScopeRecordIdx stays up to date if
320 /// the MDNode is RAUW'd.
321 std::vector<DebugRecVH> ScopeRecords;
322
323 /// ScopeInlinedAtIdx - This is the index in ScopeInlinedAtRecords for an
324 /// scope/inlined-at pair.
325 DenseMap<std::pair<MDNode*, MDNode*>, int> ScopeInlinedAtIdx;
326
327 /// ScopeInlinedAtRecords - These are the actual mdnodes (in value handles)
328 /// for an index. The ValueHandle ensures that ScopeINlinedAtIdx stays up
329 /// to date.
330 std::vector<std::pair<DebugRecVH, DebugRecVH> > ScopeInlinedAtRecords;
331
332 int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
333 int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
334
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000335 LLVMContextImpl(LLVMContext &C);
336 ~LLVMContextImpl();
Owen Anderson8e66e0b2009-06-30 00:48:55 +0000337};
338
339}
340
Owen Anderson36f62e52009-06-30 17:06:46 +0000341#endif