blob: cef770c8bf81c33432991e636e13aeab472f72c0 [file] [log] [blame]
Owen Anderson3fb4aab2009-08-23 04:24:24 +00001//===-- LLVMContextImpl.h - The LLVMContextImpl opaque class --------------===//
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"
Devang Patelf7188322009-09-03 01:39:20 +000022#include "llvm/Metadata.h"
Owen Andersonedb4a702009-07-24 23:12:02 +000023#include "llvm/Constants.h"
Owen Anderson2ad52172009-07-21 02:47:59 +000024#include "llvm/DerivedTypes.h"
Owen Anderson286b3af2009-08-17 17:34:27 +000025#include "llvm/System/Mutex.h"
Owen Anderson20b34ac2009-07-16 18:04:31 +000026#include "llvm/System/RWMutex.h"
Owen Anderson98015602009-08-17 17:59:35 +000027#include "llvm/Assembly/Writer.h"
Owen Andersonc277dc42009-07-16 19:05:41 +000028#include "llvm/ADT/APFloat.h"
Owen Anderson20b34ac2009-07-16 18:04:31 +000029#include "llvm/ADT/APInt.h"
30#include "llvm/ADT/DenseMap.h"
Owen Anderson4118dde2009-07-16 23:44:30 +000031#include "llvm/ADT/FoldingSet.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;
Owen Anderson69ab4162009-07-16 22:11:26 +000039class MDString;
Owen Anderson4118dde2009-07-16 23:44:30 +000040class MDNode;
Benjamin Kramer78c3bcb2009-08-11 17:45:13 +000041class LLVMContext;
Owen Anderson20b34ac2009-07-16 18:04:31 +000042class Type;
Owen Anderson4118dde2009-07-16 23:44:30 +000043class Value;
Owen Anderson20b34ac2009-07-16 18:04:31 +000044
45struct DenseMapAPIntKeyInfo {
46 struct KeyTy {
47 APInt val;
48 const Type* type;
49 KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
50 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
51 bool operator==(const KeyTy& that) const {
52 return type == that.type && this->val == that.val;
53 }
54 bool operator!=(const KeyTy& that) const {
55 return !this->operator==(that);
56 }
57 };
58 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
59 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
60 static unsigned getHashValue(const KeyTy &Key) {
61 return DenseMapInfo<void*>::getHashValue(Key.type) ^
62 Key.val.getHashValue();
63 }
64 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
65 return LHS == RHS;
66 }
67 static bool isPod() { return false; }
68};
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 }
81 };
82 static inline KeyTy getEmptyKey() {
83 return KeyTy(APFloat(APFloat::Bogus,1));
84 }
85 static inline KeyTy getTombstoneKey() {
86 return KeyTy(APFloat(APFloat::Bogus,2));
87 }
88 static unsigned getHashValue(const KeyTy &Key) {
89 return Key.val.getHashValue();
90 }
91 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
92 return LHS == RHS;
93 }
94 static bool isPod() { return false; }
95};
96
Benjamin Kramer78c3bcb2009-08-11 17:45:13 +000097class LLVMContextImpl {
98public:
Owen Anderson20b34ac2009-07-16 18:04:31 +000099 sys::SmartRWMutex<true> ConstantsLock;
Owen Anderson20b34ac2009-07-16 18:04:31 +0000100 typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
Owen Andersonafd0c4c2009-08-04 22:41:48 +0000101 DenseMapAPIntKeyInfo> IntMapTy;
Owen Anderson20b34ac2009-07-16 18:04:31 +0000102 IntMapTy IntConstants;
103
Owen Andersonc277dc42009-07-16 19:05:41 +0000104 typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
Owen Andersonafd0c4c2009-08-04 22:41:48 +0000105 DenseMapAPFloatKeyInfo> FPMapTy;
Owen Andersonc277dc42009-07-16 19:05:41 +0000106 FPMapTy FPConstants;
107
Owen Anderson69ab4162009-07-16 22:11:26 +0000108 StringMap<MDString*> MDStringCache;
109
Devang Patelf7188322009-09-03 01:39:20 +0000110 FoldingSet<MDNode> MDNodeSet;
111
Owen Andersonedb4a702009-07-24 23:12:02 +0000112 ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
Owen Anderson13234f82009-08-10 18:16:08 +0000113
Owen Anderson3d344922009-07-21 20:55:28 +0000114 typedef ValueMap<std::vector<Constant*>, ArrayType,
115 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Owen Andersonedb4a702009-07-24 23:12:02 +0000116 ArrayConstantsTy ArrayConstants;
Owen Anderson39ede7b2009-07-21 20:13:12 +0000117
Owen Anderson909f6002009-07-23 23:25:33 +0000118 typedef ValueMap<std::vector<Constant*>, StructType,
119 ConstantStruct, true /*largekey*/> StructConstantsTy;
Owen Andersonedb4a702009-07-24 23:12:02 +0000120 StructConstantsTy StructConstants;
Owen Anderson909f6002009-07-23 23:25:33 +0000121
Owen Anderson0348a132009-07-24 00:36:24 +0000122 typedef ValueMap<std::vector<Constant*>, VectorType,
123 ConstantVector> VectorConstantsTy;
Owen Andersonedb4a702009-07-24 23:12:02 +0000124 VectorConstantsTy VectorConstants;
Owen Anderson0348a132009-07-24 00:36:24 +0000125
Owen Andersonc8c30262009-07-31 22:45:43 +0000126 ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
127
128 ValueMap<char, Type, UndefValue> UndefValueConstants;
129
Owen Anderson1584a292009-08-04 20:25:11 +0000130 ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
131
Owen Anderson2ad52172009-07-21 02:47:59 +0000132 ConstantInt *TheTrueVal;
133 ConstantInt *TheFalseVal;
134
Owen Anderson6d549d62009-08-19 17:07:46 +0000135 // Lock used for guarding access to the leak detector
136 sys::SmartMutex<true> LLVMObjectsLock;
137 LeakDetectorImpl<Value> LLVMObjects;
138
Owen Anderson286b3af2009-08-17 17:34:27 +0000139 // Lock used for guarding access to the type maps.
140 sys::SmartMutex<true> TypeMapLock;
141
Owen Anderson98015602009-08-17 17:59:35 +0000142 // Recursive lock used for guarding access to AbstractTypeUsers.
143 // NOTE: The true template parameter means this will no-op when we're not in
144 // multithreaded mode.
145 sys::SmartMutex<true> AbstractTypeUsersLock;
146
Dan Gohman97d2cb82009-08-25 16:00:35 +0000147 // Basic type instances.
148 const Type VoidTy;
149 const Type LabelTy;
150 const Type FloatTy;
151 const Type DoubleTy;
152 const Type MetadataTy;
153 const Type X86_FP80Ty;
154 const Type FP128Ty;
155 const Type PPC_FP128Ty;
156 const IntegerType Int1Ty;
157 const IntegerType Int8Ty;
158 const IntegerType Int16Ty;
159 const IntegerType Int32Ty;
160 const IntegerType Int64Ty;
161
Owen Anderson98015602009-08-17 17:59:35 +0000162 // Concrete/Abstract TypeDescriptions - We lazily calculate type descriptions
163 // for types as they are needed. Because resolution of types must invalidate
164 // all of the abstract type descriptions, we keep them in a seperate map to
165 // make this easy.
166 TypePrinting ConcreteTypeDescriptions;
167 TypePrinting AbstractTypeDescriptions;
168
Owen Anderson542cffc2009-08-04 23:33:01 +0000169 TypeMap<ArrayValType, ArrayType> ArrayTypes;
Owen Andersond9186492009-08-04 23:47:44 +0000170 TypeMap<VectorValType, VectorType> VectorTypes;
Owen Andersone5659952009-08-05 00:15:12 +0000171 TypeMap<PointerValType, PointerType> PointerTypes;
Owen Anderson4b5c7612009-08-05 18:13:27 +0000172 TypeMap<FunctionValType, FunctionType> FunctionTypes;
Owen Anderson03cb69f2009-08-05 23:16:16 +0000173 TypeMap<StructValType, StructType> StructTypes;
Owen Andersona42ac692009-08-13 23:27:32 +0000174 TypeMap<IntegerValType, IntegerType> IntegerTypes;
Dan Gohman97d2cb82009-08-25 16:00:35 +0000175
Owen Andersone8f21852009-08-18 18:28:58 +0000176 /// ValueHandles - This map keeps track of all of the value handles that are
177 /// watching a Value*. The Value::HasValueHandle bit is used to know
178 // whether or not a value has an entry in this map.
179 typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
180 ValueHandlesTy ValueHandles;
181
Owen Andersona42ac692009-08-13 23:27:32 +0000182 LLVMContextImpl(LLVMContext &C) : TheTrueVal(0), TheFalseVal(0),
Dan Gohman97d2cb82009-08-25 16:00:35 +0000183 VoidTy(C, Type::VoidTyID),
184 LabelTy(C, Type::LabelTyID),
185 FloatTy(C, Type::FloatTyID),
186 DoubleTy(C, Type::DoubleTyID),
187 MetadataTy(C, Type::MetadataTyID),
188 X86_FP80Ty(C, Type::X86_FP80TyID),
189 FP128Ty(C, Type::FP128TyID),
190 PPC_FP128Ty(C, Type::PPC_FP128TyID),
191 Int1Ty(C, 1),
192 Int8Ty(C, 8),
193 Int16Ty(C, 16),
194 Int32Ty(C, 32),
195 Int64Ty(C, 64) { }
Torok Edwind18e6682009-08-31 16:14:59 +0000196
197 ~LLVMContextImpl()
198 {
199 ExprConstants.freeConstants();
200 ArrayConstants.freeConstants();
201 StructConstants.freeConstants();
202 VectorConstants.freeConstants();
Torok Edwind18e6682009-08-31 16:14:59 +0000203 AggZeroConstants.freeConstants();
204 NullPtrConstants.freeConstants();
205 UndefValueConstants.freeConstants();
Devang Patel5ffc3852009-09-10 22:36:12 +0000206 for (FoldingSet<MDNode>::iterator I = MDNodeSet.begin(),
207 E = MDNodeSet.end(); I != E; ++I)
Devang Patel93056ec2009-09-09 17:07:07 +0000208 I->dropAllReferences();
Devang Patel5ffc3852009-09-10 22:36:12 +0000209 for (IntMapTy::iterator I = IntConstants.begin(), E = IntConstants.end();
Torok Edwind18e6682009-08-31 16:14:59 +0000210 I != E; ++I) {
211 if (I->second->use_empty())
212 delete I->second;
213 }
Devang Patel5ffc3852009-09-10 22:36:12 +0000214 for (FPMapTy::iterator I = FPConstants.begin(), E = FPConstants.end();
Torok Edwind18e6682009-08-31 16:14:59 +0000215 I != E; ++I) {
216 if (I->second->use_empty())
217 delete I->second;
218 }
219 }
Owen Anderson8e66e0b2009-06-30 00:48:55 +0000220};
221
222}
223
Owen Anderson36f62e52009-06-30 17:06:46 +0000224#endif