blob: 23971aafa74d8f6c47b0ef2976ea64b3c2f0d653 [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
Owen Andersonafd0c4c2009-08-04 22:41:48 +000018#include "ConstantsContext.h"
Owen Anderson6d549d62009-08-19 17:07:46 +000019#include "LeaksContext.h"
Owen Anderson542cffc2009-08-04 23:33:01 +000020#include "TypesContext.h"
Owen Anderson2ad52172009-07-21 02:47:59 +000021#include "llvm/LLVMContext.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"
Owen Anderson98015602009-08-17 17:59:35 +000025#include "llvm/Assembly/Writer.h"
Chris Lattnera0566972009-12-29 09:01:33 +000026#include "llvm/Support/ValueHandle.h"
Owen Andersonc277dc42009-07-16 19:05:41 +000027#include "llvm/ADT/APFloat.h"
Owen Anderson20b34ac2009-07-16 18:04:31 +000028#include "llvm/ADT/APInt.h"
29#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"
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;
46 const Type* type;
47 KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
48 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
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000093/// DebugRecVH - This is a CallbackVH used to keep the Scope -> index maps
94/// up to date as MDNodes mutate. This class is implemented in DebugLoc.cpp.
95class DebugRecVH : public CallbackVH {
96 /// Ctx - This is the LLVM Context being referenced.
97 LLVMContextImpl *Ctx;
98
99 /// Idx - The index into either ScopeRecordIdx or ScopeInlinedAtRecords that
100 /// this reference lives in. If this is zero, then it represents a
101 /// non-canonical entry that has no DenseMap value. This can happen due to
102 /// RAUW.
103 int Idx;
104public:
105 DebugRecVH(MDNode *n, LLVMContextImpl *ctx, int idx)
106 : CallbackVH(n), Ctx(ctx), Idx(idx) {}
107
108 MDNode *get() const {
109 return cast_or_null<MDNode>(getValPtr());
110 }
111
112 virtual void deleted();
113 virtual void allUsesReplacedWith(Value *VNew);
114};
115
Benjamin Kramer78c3bcb2009-08-11 17:45:13 +0000116class LLVMContextImpl {
117public:
Owen Anderson8e89e412010-09-08 18:03:32 +0000118 /// OwnedModules - The set of modules instantiated in this context, and which
119 /// will be automatically deleted if this context is deleted.
120 SmallPtrSet<Module*, 4> OwnedModules;
121
Chris Lattnerb0e36082010-11-17 08:13:01 +0000122 LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
123 void *InlineAsmDiagContext;
Chris Lattner60955d42010-04-06 00:44:45 +0000124
Owen Anderson20b34ac2009-07-16 18:04:31 +0000125 typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
Owen Andersonafd0c4c2009-08-04 22:41:48 +0000126 DenseMapAPIntKeyInfo> IntMapTy;
Owen Anderson20b34ac2009-07-16 18:04:31 +0000127 IntMapTy IntConstants;
128
Owen Andersonc277dc42009-07-16 19:05:41 +0000129 typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
Owen Andersonafd0c4c2009-08-04 22:41:48 +0000130 DenseMapAPFloatKeyInfo> FPMapTy;
Owen Andersonc277dc42009-07-16 19:05:41 +0000131 FPMapTy FPConstants;
132
Owen Anderson69ab4162009-07-16 22:11:26 +0000133 StringMap<MDString*> MDStringCache;
134
Devang Patelf7188322009-09-03 01:39:20 +0000135 FoldingSet<MDNode> MDNodeSet;
Jeffrey Yasskin2cc24762010-03-13 01:26:15 +0000136 // MDNodes may be uniqued or not uniqued. When they're not uniqued, they
137 // aren't in the MDNodeSet, but they're still shared between objects, so no
138 // one object can destroy them. This set allows us to at least destroy them
139 // on Context destruction.
140 SmallPtrSet<MDNode*, 1> NonUniquedMDNodes;
Devang Patelf7188322009-09-03 01:39:20 +0000141
Jeffrey Yasskinf6ee7be2009-10-27 23:45:55 +0000142 ConstantUniqueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
Owen Anderson13234f82009-08-10 18:16:08 +0000143
Jeffrey Yasskinf6ee7be2009-10-27 23:45:55 +0000144 typedef ConstantUniqueMap<std::vector<Constant*>, ArrayType,
Owen Anderson3d344922009-07-21 20:55:28 +0000145 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Owen Andersonedb4a702009-07-24 23:12:02 +0000146 ArrayConstantsTy ArrayConstants;
Owen Anderson39ede7b2009-07-21 20:13:12 +0000147
Jeffrey Yasskinf6ee7be2009-10-27 23:45:55 +0000148 typedef ConstantUniqueMap<std::vector<Constant*>, StructType,
149 ConstantStruct, true /*largekey*/> StructConstantsTy;
Owen Andersonedb4a702009-07-24 23:12:02 +0000150 StructConstantsTy StructConstants;
Owen Anderson909f6002009-07-23 23:25:33 +0000151
Jeffrey Yasskinf6ee7be2009-10-27 23:45:55 +0000152 typedef ConstantUniqueMap<std::vector<Constant*>, VectorType,
153 ConstantVector> VectorConstantsTy;
Owen Andersonedb4a702009-07-24 23:12:02 +0000154 VectorConstantsTy VectorConstants;
Owen Anderson0348a132009-07-24 00:36:24 +0000155
Jeffrey Yasskinf6ee7be2009-10-27 23:45:55 +0000156 ConstantUniqueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
Jeffrey Yasskinf6ee7be2009-10-27 23:45:55 +0000157 ConstantUniqueMap<char, Type, UndefValue> UndefValueConstants;
Owen Andersonc8c30262009-07-31 22:45:43 +0000158
Chris Lattner31b132c2009-10-28 00:01:44 +0000159 DenseMap<std::pair<Function*, BasicBlock*> , BlockAddress*> BlockAddresses;
Jeffrey Yasskinf6ee7be2009-10-27 23:45:55 +0000160 ConstantUniqueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000161
162 ConstantUniqueMap<InlineAsmKeyType, PointerType, InlineAsm> InlineAsms;
Owen Anderson1584a292009-08-04 20:25:11 +0000163
Owen Anderson2ad52172009-07-21 02:47:59 +0000164 ConstantInt *TheTrueVal;
165 ConstantInt *TheFalseVal;
166
Owen Anderson6d549d62009-08-19 17:07:46 +0000167 LeakDetectorImpl<Value> LLVMObjects;
168
Dan Gohman97d2cb82009-08-25 16:00:35 +0000169 // Basic type instances.
170 const Type VoidTy;
171 const Type LabelTy;
172 const Type FloatTy;
173 const Type DoubleTy;
174 const Type MetadataTy;
175 const Type X86_FP80Ty;
176 const Type FP128Ty;
177 const Type PPC_FP128Ty;
Dale Johannesenbaa5d042010-09-10 20:55:01 +0000178 const Type X86_MMXTy;
Dan Gohman97d2cb82009-08-25 16:00:35 +0000179 const IntegerType Int1Ty;
180 const IntegerType Int8Ty;
181 const IntegerType Int16Ty;
182 const IntegerType Int32Ty;
183 const IntegerType Int64Ty;
184
Owen Anderson98015602009-08-17 17:59:35 +0000185 // Concrete/Abstract TypeDescriptions - We lazily calculate type descriptions
186 // for types as they are needed. Because resolution of types must invalidate
187 // all of the abstract type descriptions, we keep them in a seperate map to
188 // make this easy.
189 TypePrinting ConcreteTypeDescriptions;
190 TypePrinting AbstractTypeDescriptions;
191
Owen Anderson542cffc2009-08-04 23:33:01 +0000192 TypeMap<ArrayValType, ArrayType> ArrayTypes;
Owen Andersond9186492009-08-04 23:47:44 +0000193 TypeMap<VectorValType, VectorType> VectorTypes;
Owen Andersone5659952009-08-05 00:15:12 +0000194 TypeMap<PointerValType, PointerType> PointerTypes;
Owen Anderson4b5c7612009-08-05 18:13:27 +0000195 TypeMap<FunctionValType, FunctionType> FunctionTypes;
Owen Anderson03cb69f2009-08-05 23:16:16 +0000196 TypeMap<StructValType, StructType> StructTypes;
Owen Andersona42ac692009-08-13 23:27:32 +0000197 TypeMap<IntegerValType, IntegerType> IntegerTypes;
Dan Gohman97d2cb82009-08-25 16:00:35 +0000198
Jeffrey Yasskin28f24482009-12-17 19:55:06 +0000199 // Opaque types are not structurally uniqued, so don't use TypeMap.
200 typedef SmallPtrSet<const OpaqueType*, 8> OpaqueTypesTy;
201 OpaqueTypesTy OpaqueTypes;
Jeffrey Yasskinc660b232010-02-11 06:41:30 +0000202
203 /// Used as an abstract type that will never be resolved.
204 OpaqueType *const AlwaysOpaqueTy;
205
Jeffrey Yasskin28f24482009-12-17 19:55:06 +0000206
Owen Andersone8f21852009-08-18 18:28:58 +0000207 /// ValueHandles - This map keeps track of all of the value handles that are
208 /// watching a Value*. The Value::HasValueHandle bit is used to know
209 // whether or not a value has an entry in this map.
210 typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
211 ValueHandlesTy ValueHandles;
212
Chris Lattnera0566972009-12-29 09:01:33 +0000213 /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
214 StringMap<unsigned> CustomMDKindNames;
215
216 typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
217 typedef SmallVector<MDPairTy, 2> MDMapTy;
218
219 /// MetadataStore - Collection of per-instruction metadata used in this
220 /// context.
221 DenseMap<const Instruction *, MDMapTy> MetadataStore;
222
Chris Lattner8cb2aeb2010-04-01 00:37:44 +0000223 /// ScopeRecordIdx - This is the index in ScopeRecords for an MDNode scope
224 /// entry with no "inlined at" element.
225 DenseMap<MDNode*, int> ScopeRecordIdx;
226
227 /// ScopeRecords - These are the actual mdnodes (in a value handle) for an
228 /// index. The ValueHandle ensures that ScopeRecordIdx stays up to date if
229 /// the MDNode is RAUW'd.
230 std::vector<DebugRecVH> ScopeRecords;
231
232 /// ScopeInlinedAtIdx - This is the index in ScopeInlinedAtRecords for an
233 /// scope/inlined-at pair.
234 DenseMap<std::pair<MDNode*, MDNode*>, int> ScopeInlinedAtIdx;
235
236 /// ScopeInlinedAtRecords - These are the actual mdnodes (in value handles)
237 /// for an index. The ValueHandle ensures that ScopeINlinedAtIdx stays up
238 /// to date.
239 std::vector<std::pair<DebugRecVH, DebugRecVH> > ScopeInlinedAtRecords;
240
241 int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
242 int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
243
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000244 LLVMContextImpl(LLVMContext &C);
245 ~LLVMContextImpl();
Owen Anderson8e66e0b2009-06-30 00:48:55 +0000246};
247
248}
249
Owen Anderson36f62e52009-06-30 17:06:46 +0000250#endif