blob: f98526de650e9d06f6c4449bbd8cc39bcda0d918 [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 }
Chandler Carruth71bd7d12012-03-04 12:02:57 +000055 friend hash_code hash_value(const KeyTy &Key) {
56 return hash_combine(Key.type, Key.val);
57 }
Owen Anderson20b34ac2009-07-16 18:04:31 +000058 };
59 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
60 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
61 static unsigned getHashValue(const KeyTy &Key) {
Chandler Carruth71bd7d12012-03-04 12:02:57 +000062 return static_cast<unsigned>(hash_value(Key));
Owen Anderson20b34ac2009-07-16 18:04:31 +000063 }
64 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
65 return LHS == RHS;
66 }
Owen Anderson20b34ac2009-07-16 18:04:31 +000067};
68
Owen Andersonc277dc42009-07-16 19:05:41 +000069struct DenseMapAPFloatKeyInfo {
70 struct KeyTy {
71 APFloat val;
72 KeyTy(const APFloat& V) : val(V){}
73 KeyTy(const KeyTy& that) : val(that.val) {}
74 bool operator==(const KeyTy& that) const {
75 return this->val.bitwiseIsEqual(that.val);
76 }
77 bool operator!=(const KeyTy& that) const {
78 return !this->operator==(that);
79 }
Chandler Carruth71bd7d12012-03-04 12:02:57 +000080 friend hash_code hash_value(const KeyTy &Key) {
81 return hash_combine(Key.val);
82 }
Owen Andersonc277dc42009-07-16 19:05:41 +000083 };
84 static inline KeyTy getEmptyKey() {
85 return KeyTy(APFloat(APFloat::Bogus,1));
86 }
87 static inline KeyTy getTombstoneKey() {
88 return KeyTy(APFloat(APFloat::Bogus,2));
89 }
90 static unsigned getHashValue(const KeyTy &Key) {
Chandler Carruth71bd7d12012-03-04 12:02:57 +000091 return static_cast<unsigned>(hash_value(Key));
Owen Andersonc277dc42009-07-16 19:05:41 +000092 }
93 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
94 return LHS == RHS;
95 }
Owen Andersonc277dc42009-07-16 19:05:41 +000096};
97
Jay Foad529776c2012-02-23 09:17:40 +000098struct AnonStructTypeKeyInfo {
99 struct KeyTy {
100 ArrayRef<Type*> ETypes;
101 bool isPacked;
102 KeyTy(const ArrayRef<Type*>& E, bool P) :
103 ETypes(E), isPacked(P) {}
104 KeyTy(const KeyTy& that) :
105 ETypes(that.ETypes), isPacked(that.isPacked) {}
106 KeyTy(const StructType* ST) :
107 ETypes(ArrayRef<Type*>(ST->element_begin(), ST->element_end())),
108 isPacked(ST->isPacked()) {}
109 bool operator==(const KeyTy& that) const {
110 if (isPacked != that.isPacked)
111 return false;
112 if (ETypes != that.ETypes)
113 return false;
114 return true;
115 }
116 bool operator!=(const KeyTy& that) const {
117 return !this->operator==(that);
118 }
119 };
120 static inline StructType* getEmptyKey() {
121 return DenseMapInfo<StructType*>::getEmptyKey();
122 }
123 static inline StructType* getTombstoneKey() {
124 return DenseMapInfo<StructType*>::getTombstoneKey();
125 }
126 static unsigned getHashValue(const KeyTy& Key) {
Chandler Carruth1d03a3b2012-03-01 18:55:25 +0000127 return hash_combine(hash_combine_range(Key.ETypes.begin(),
128 Key.ETypes.end()),
129 Key.isPacked);
Jay Foad529776c2012-02-23 09:17:40 +0000130 }
131 static unsigned getHashValue(const StructType *ST) {
132 return getHashValue(KeyTy(ST));
133 }
134 static bool isEqual(const KeyTy& LHS, const StructType *RHS) {
135 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
136 return false;
137 return LHS == KeyTy(RHS);
138 }
139 static bool isEqual(const StructType *LHS, const StructType *RHS) {
140 return LHS == RHS;
141 }
142};
143
144struct FunctionTypeKeyInfo {
145 struct KeyTy {
146 const Type *ReturnType;
147 ArrayRef<Type*> Params;
148 bool isVarArg;
149 KeyTy(const Type* R, const ArrayRef<Type*>& P, bool V) :
150 ReturnType(R), Params(P), isVarArg(V) {}
151 KeyTy(const KeyTy& that) :
152 ReturnType(that.ReturnType),
153 Params(that.Params),
154 isVarArg(that.isVarArg) {}
155 KeyTy(const FunctionType* FT) :
156 ReturnType(FT->getReturnType()),
157 Params(ArrayRef<Type*>(FT->param_begin(), FT->param_end())),
158 isVarArg(FT->isVarArg()) {}
159 bool operator==(const KeyTy& that) const {
160 if (ReturnType != that.ReturnType)
161 return false;
162 if (isVarArg != that.isVarArg)
163 return false;
164 if (Params != that.Params)
165 return false;
166 return true;
167 }
168 bool operator!=(const KeyTy& that) const {
169 return !this->operator==(that);
170 }
171 };
172 static inline FunctionType* getEmptyKey() {
173 return DenseMapInfo<FunctionType*>::getEmptyKey();
174 }
175 static inline FunctionType* getTombstoneKey() {
176 return DenseMapInfo<FunctionType*>::getTombstoneKey();
177 }
178 static unsigned getHashValue(const KeyTy& Key) {
Chandler Carruth1d03a3b2012-03-01 18:55:25 +0000179 return hash_combine(Key.ReturnType,
180 hash_combine_range(Key.Params.begin(),
181 Key.Params.end()),
182 Key.isVarArg);
Jay Foad529776c2012-02-23 09:17:40 +0000183 }
184 static unsigned getHashValue(const FunctionType *FT) {
185 return getHashValue(KeyTy(FT));
186 }
187 static bool isEqual(const KeyTy& LHS, const FunctionType *RHS) {
188 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
189 return false;
190 return LHS == KeyTy(RHS);
191 }
192 static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) {
193 return LHS == RHS;
194 }
195};
196
Chris Lattner8cb2aeb2010-04-01 00:37:44 +0000197/// DebugRecVH - This is a CallbackVH used to keep the Scope -> index maps
198/// up to date as MDNodes mutate. This class is implemented in DebugLoc.cpp.
199class DebugRecVH : public CallbackVH {
200 /// Ctx - This is the LLVM Context being referenced.
201 LLVMContextImpl *Ctx;
202
203 /// Idx - The index into either ScopeRecordIdx or ScopeInlinedAtRecords that
204 /// this reference lives in. If this is zero, then it represents a
205 /// non-canonical entry that has no DenseMap value. This can happen due to
206 /// RAUW.
207 int Idx;
208public:
209 DebugRecVH(MDNode *n, LLVMContextImpl *ctx, int idx)
210 : CallbackVH(n), Ctx(ctx), Idx(idx) {}
211
212 MDNode *get() const {
213 return cast_or_null<MDNode>(getValPtr());
214 }
215
216 virtual void deleted();
217 virtual void allUsesReplacedWith(Value *VNew);
218};
219
Benjamin Kramer78c3bcb2009-08-11 17:45:13 +0000220class LLVMContextImpl {
221public:
Owen Anderson8e89e412010-09-08 18:03:32 +0000222 /// OwnedModules - The set of modules instantiated in this context, and which
223 /// will be automatically deleted if this context is deleted.
224 SmallPtrSet<Module*, 4> OwnedModules;
225
Chris Lattnerb0e36082010-11-17 08:13:01 +0000226 LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
227 void *InlineAsmDiagContext;
Chris Lattner60955d42010-04-06 00:44:45 +0000228
Owen Anderson20b34ac2009-07-16 18:04:31 +0000229 typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
Owen Andersonafd0c4c2009-08-04 22:41:48 +0000230 DenseMapAPIntKeyInfo> IntMapTy;
Owen Anderson20b34ac2009-07-16 18:04:31 +0000231 IntMapTy IntConstants;
232
Owen Andersonc277dc42009-07-16 19:05:41 +0000233 typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
Owen Andersonafd0c4c2009-08-04 22:41:48 +0000234 DenseMapAPFloatKeyInfo> FPMapTy;
Owen Andersonc277dc42009-07-16 19:05:41 +0000235 FPMapTy FPConstants;
236
Owen Anderson69ab4162009-07-16 22:11:26 +0000237 StringMap<MDString*> MDStringCache;
238
Devang Patelf7188322009-09-03 01:39:20 +0000239 FoldingSet<MDNode> MDNodeSet;
Jeffrey Yasskin2cc24762010-03-13 01:26:15 +0000240 // MDNodes may be uniqued or not uniqued. When they're not uniqued, they
241 // aren't in the MDNodeSet, but they're still shared between objects, so no
242 // one object can destroy them. This set allows us to at least destroy them
243 // on Context destruction.
244 SmallPtrSet<MDNode*, 1> NonUniquedMDNodes;
Devang Patelf7188322009-09-03 01:39:20 +0000245
Chris Lattnerc7f9fd42012-01-23 15:20:12 +0000246 DenseMap<Type*, ConstantAggregateZero*> CAZConstants;
Owen Anderson13234f82009-08-10 18:16:08 +0000247
Talin46e9b442012-02-05 20:54:10 +0000248 typedef ConstantAggrUniqueMap<ArrayType, ConstantArray> ArrayConstantsTy;
Owen Andersonedb4a702009-07-24 23:12:02 +0000249 ArrayConstantsTy ArrayConstants;
Owen Anderson39ede7b2009-07-21 20:13:12 +0000250
Talin46e9b442012-02-05 20:54:10 +0000251 typedef ConstantAggrUniqueMap<StructType, ConstantStruct> StructConstantsTy;
Owen Andersonedb4a702009-07-24 23:12:02 +0000252 StructConstantsTy StructConstants;
Owen Anderson909f6002009-07-23 23:25:33 +0000253
Talin46e9b442012-02-05 20:54:10 +0000254 typedef ConstantAggrUniqueMap<VectorType, ConstantVector> VectorConstantsTy;
Owen Andersonedb4a702009-07-24 23:12:02 +0000255 VectorConstantsTy VectorConstants;
Owen Anderson0348a132009-07-24 00:36:24 +0000256
Chris Lattnerc7f9fd42012-01-23 15:20:12 +0000257 DenseMap<PointerType*, ConstantPointerNull*> CPNConstants;
258
259 DenseMap<Type*, UndefValue*> UVConstants;
Owen Andersonc8c30262009-07-31 22:45:43 +0000260
Chris Lattner3756b912012-01-23 22:57:10 +0000261 StringMap<ConstantDataSequential*> CDSConstants;
262
263
Chris Lattner31b132c2009-10-28 00:01:44 +0000264 DenseMap<std::pair<Function*, BasicBlock*> , BlockAddress*> BlockAddresses;
Jay Foadc365eea2011-06-22 08:50:06 +0000265 ConstantUniqueMap<ExprMapKeyType, const ExprMapKeyType&, Type, ConstantExpr>
266 ExprConstants;
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000267
Jay Foadc365eea2011-06-22 08:50:06 +0000268 ConstantUniqueMap<InlineAsmKeyType, const InlineAsmKeyType&, PointerType,
269 InlineAsm> InlineAsms;
Owen Anderson1584a292009-08-04 20:25:11 +0000270
Owen Anderson2ad52172009-07-21 02:47:59 +0000271 ConstantInt *TheTrueVal;
272 ConstantInt *TheFalseVal;
273
Owen Anderson6d549d62009-08-19 17:07:46 +0000274 LeakDetectorImpl<Value> LLVMObjects;
275
Dan Gohman97d2cb82009-08-25 16:00:35 +0000276 // Basic type instances.
Dan Gohman518cda42011-12-17 00:04:22 +0000277 Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000278 Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy;
279 IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty;
Dan Gohman97d2cb82009-08-25 16:00:35 +0000280
Chris Lattner07bd69c2011-07-15 05:49:15 +0000281
282 /// TypeAllocator - All dynamically allocated types are allocated from this.
283 /// They live forever until the context is torn down.
284 BumpPtrAllocator TypeAllocator;
285
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000286 DenseMap<unsigned, IntegerType*> IntegerTypes;
287
Jay Foad529776c2012-02-23 09:17:40 +0000288 typedef DenseMap<FunctionType*, bool, FunctionTypeKeyInfo> FunctionTypeMap;
289 FunctionTypeMap FunctionTypes;
290 typedef DenseMap<StructType*, bool, AnonStructTypeKeyInfo> StructTypeMap;
291 StructTypeMap AnonStructTypes;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000292 StringMap<StructType*> NamedStructTypes;
293 unsigned NamedStructTypesUniqueID;
294
295 DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
296 DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes;
297 DenseMap<Type*, PointerType*> PointerTypes; // Pointers in AddrSpace = 0
298 DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;
Jeffrey Yasskinc660b232010-02-11 06:41:30 +0000299
Jeffrey Yasskin28f24482009-12-17 19:55:06 +0000300
Owen Andersone8f21852009-08-18 18:28:58 +0000301 /// ValueHandles - This map keeps track of all of the value handles that are
302 /// watching a Value*. The Value::HasValueHandle bit is used to know
303 // whether or not a value has an entry in this map.
304 typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
305 ValueHandlesTy ValueHandles;
306
Chris Lattnera0566972009-12-29 09:01:33 +0000307 /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
308 StringMap<unsigned> CustomMDKindNames;
309
310 typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
311 typedef SmallVector<MDPairTy, 2> MDMapTy;
312
313 /// MetadataStore - Collection of per-instruction metadata used in this
314 /// context.
315 DenseMap<const Instruction *, MDMapTy> MetadataStore;
316
Chris Lattner8cb2aeb2010-04-01 00:37:44 +0000317 /// ScopeRecordIdx - This is the index in ScopeRecords for an MDNode scope
318 /// entry with no "inlined at" element.
319 DenseMap<MDNode*, int> ScopeRecordIdx;
320
321 /// ScopeRecords - These are the actual mdnodes (in a value handle) for an
322 /// index. The ValueHandle ensures that ScopeRecordIdx stays up to date if
323 /// the MDNode is RAUW'd.
324 std::vector<DebugRecVH> ScopeRecords;
325
326 /// ScopeInlinedAtIdx - This is the index in ScopeInlinedAtRecords for an
327 /// scope/inlined-at pair.
328 DenseMap<std::pair<MDNode*, MDNode*>, int> ScopeInlinedAtIdx;
329
330 /// ScopeInlinedAtRecords - These are the actual mdnodes (in value handles)
331 /// for an index. The ValueHandle ensures that ScopeINlinedAtIdx stays up
332 /// to date.
333 std::vector<std::pair<DebugRecVH, DebugRecVH> > ScopeInlinedAtRecords;
334
335 int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
336 int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
337
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000338 LLVMContextImpl(LLVMContext &C);
339 ~LLVMContextImpl();
Owen Anderson8e66e0b2009-06-30 00:48:55 +0000340};
341
342}
343
Owen Anderson36f62e52009-06-30 17:06:46 +0000344#endif