blob: 7a29a8421f4eb5886cbfe8dbf0eb440c3285a34f [file] [log] [blame]
Owen Anderson20b34ac2009-07-16 18:04:31 +00001//===----------------- LLVMContextImpl.h - Implementation ------*- 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 Anderson2ad52172009-07-21 02:47:59 +000018#include "llvm/LLVMContext.h"
Owen Andersonedb4a702009-07-24 23:12:02 +000019#include "llvm/Constants.h"
Owen Anderson2ad52172009-07-21 02:47:59 +000020#include "llvm/DerivedTypes.h"
Owen Anderson39ede7b2009-07-21 20:13:12 +000021#include "llvm/Support/Debug.h"
22#include "llvm/Support/ErrorHandling.h"
23#include "llvm/System/Mutex.h"
Owen Anderson20b34ac2009-07-16 18:04:31 +000024#include "llvm/System/RWMutex.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"
27#include "llvm/ADT/DenseMap.h"
Owen Anderson4118dde2009-07-16 23:44:30 +000028#include "llvm/ADT/FoldingSet.h"
Owen Anderson69ab4162009-07-16 22:11:26 +000029#include "llvm/ADT/StringMap.h"
Owen Anderson39ede7b2009-07-21 20:13:12 +000030#include <map>
Owen Anderson909f6002009-07-23 23:25:33 +000031#include <vector>
Owen Anderson39ede7b2009-07-21 20:13:12 +000032
Owen Anderson20b34ac2009-07-16 18:04:31 +000033namespace llvm {
Owen Anderson39ede7b2009-07-21 20:13:12 +000034template<class ValType>
35struct ConstantTraits;
Owen Anderson20b34ac2009-07-16 18:04:31 +000036
Owen Andersonedb4a702009-07-24 23:12:02 +000037// The number of operands for each ConstantCreator::create method is
38// determined by the ConstantTraits template.
39// ConstantCreator - A class that is used to create constants by
40// ValueMap*. This class should be partially specialized if there is
41// something strange that needs to be done to interface to the ctor for the
42// constant.
43//
44template<typename T, typename Alloc>
45struct VISIBILITY_HIDDEN ConstantTraits< std::vector<T, Alloc> > {
46 static unsigned uses(const std::vector<T, Alloc>& v) {
47 return v.size();
48 }
49};
50
51template<class ConstantClass, class TypeClass, class ValType>
52struct VISIBILITY_HIDDEN ConstantCreator {
53 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
54 return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V);
55 }
56};
57
58template<class ConstantClass, class TypeClass>
59struct VISIBILITY_HIDDEN ConvertConstantType {
60 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
61 llvm_unreachable("This type cannot be converted!");
62 }
63};
64
65// ConstantAggregateZero does not take extra "value" argument...
66template<class ValType>
67struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
68 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
69 return new ConstantAggregateZero(Ty);
70 }
71};
72
73template<>
74struct ConvertConstantType<ConstantAggregateZero, Type> {
75 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
76 // Make everyone now use a constant of the new type...
Owen Andersonb292b8c2009-07-30 23:03:37 +000077 Constant *New = ConstantAggregateZero::get(NewTy);
Owen Andersonedb4a702009-07-24 23:12:02 +000078 assert(New != OldC && "Didn't replace constant??");
79 OldC->uncheckedReplaceAllUsesWith(New);
80 OldC->destroyConstant(); // This constant is now dead, destroy it.
81 }
82};
83
84template<>
85struct ConvertConstantType<ConstantArray, ArrayType> {
86 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
87 // Make everyone now use a constant of the new type...
88 std::vector<Constant*> C;
89 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
90 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Andersonc2c79322009-07-28 18:32:17 +000091 Constant *New = ConstantArray::get(NewTy, C);
Owen Andersonedb4a702009-07-24 23:12:02 +000092 assert(New != OldC && "Didn't replace constant??");
93 OldC->uncheckedReplaceAllUsesWith(New);
94 OldC->destroyConstant(); // This constant is now dead, destroy it.
95 }
96};
97
98template<>
99struct ConvertConstantType<ConstantStruct, StructType> {
100 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
101 // Make everyone now use a constant of the new type...
102 std::vector<Constant*> C;
103 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
104 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson45308b52009-07-27 22:29:26 +0000105 Constant *New = ConstantStruct::get(NewTy, C);
Owen Andersonedb4a702009-07-24 23:12:02 +0000106 assert(New != OldC && "Didn't replace constant??");
107
108 OldC->uncheckedReplaceAllUsesWith(New);
109 OldC->destroyConstant(); // This constant is now dead, destroy it.
110 }
111};
112
113template<>
114struct ConvertConstantType<ConstantVector, VectorType> {
115 static void convert(ConstantVector *OldC, const VectorType *NewTy) {
116 // Make everyone now use a constant of the new type...
117 std::vector<Constant*> C;
118 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
119 C.push_back(cast<Constant>(OldC->getOperand(i)));
Owen Anderson4aa32952009-07-28 21:19:26 +0000120 Constant *New = ConstantVector::get(NewTy, C);
Owen Andersonedb4a702009-07-24 23:12:02 +0000121 assert(New != OldC && "Didn't replace constant??");
122 OldC->uncheckedReplaceAllUsesWith(New);
123 OldC->destroyConstant(); // This constant is now dead, destroy it.
124 }
125};
126
127template<class ValType, class TypeClass, class ConstantClass,
128 bool HasLargeKey = false /*true for arrays and structs*/ >
129class ValueMap : public AbstractTypeUser {
130public:
131 typedef std::pair<const Type*, ValType> MapKey;
132 typedef std::map<MapKey, Constant *> MapTy;
133 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
134 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
135private:
136 /// Map - This is the main map from the element descriptor to the Constants.
137 /// This is the primary way we avoid creating two of the same shape
138 /// constant.
139 MapTy Map;
140
141 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
142 /// from the constants to their element in Map. This is important for
143 /// removal of constants from the array, which would otherwise have to scan
144 /// through the map with very large keys.
145 InverseMapTy InverseMap;
146
147 /// AbstractTypeMap - Map for abstract type constants.
148 ///
149 AbstractTypeMapTy AbstractTypeMap;
150
151 /// ValueMapLock - Mutex for this map.
152 sys::SmartMutex<true> ValueMapLock;
153
154public:
155 // NOTE: This function is not locked. It is the caller's responsibility
156 // to enforce proper synchronization.
157 typename MapTy::iterator map_end() { return Map.end(); }
158
159 /// InsertOrGetItem - Return an iterator for the specified element.
160 /// If the element exists in the map, the returned iterator points to the
161 /// entry and Exists=true. If not, the iterator points to the newly
162 /// inserted entry and returns Exists=false. Newly inserted entries have
163 /// I->second == 0, and should be filled in.
164 /// NOTE: This function is not locked. It is the caller's responsibility
165 // to enforce proper synchronization.
166 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
167 &InsertVal,
168 bool &Exists) {
169 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
170 Exists = !IP.second;
171 return IP.first;
172 }
173
174private:
175 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
176 if (HasLargeKey) {
177 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
178 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
179 IMI->second->second == CP &&
180 "InverseMap corrupt!");
181 return IMI->second;
182 }
183
184 typename MapTy::iterator I =
185 Map.find(MapKey(static_cast<const TypeClass*>(CP->getRawType()),
186 getValType(CP)));
187 if (I == Map.end() || I->second != CP) {
188 // FIXME: This should not use a linear scan. If this gets to be a
189 // performance problem, someone should look at this.
190 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
191 /* empty */;
192 }
193 return I;
194 }
195
196 ConstantClass* Create(const TypeClass *Ty, const ValType &V,
197 typename MapTy::iterator I) {
198 ConstantClass* Result =
199 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
200
201 assert(Result->getType() == Ty && "Type specified is not correct!");
202 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
203
204 if (HasLargeKey) // Remember the reverse mapping if needed.
205 InverseMap.insert(std::make_pair(Result, I));
206
207 // If the type of the constant is abstract, make sure that an entry
208 // exists for it in the AbstractTypeMap.
209 if (Ty->isAbstract()) {
210 typename AbstractTypeMapTy::iterator TI =
211 AbstractTypeMap.find(Ty);
212
213 if (TI == AbstractTypeMap.end()) {
214 // Add ourselves to the ATU list of the type.
215 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
216
217 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
218 }
219 }
220
221 return Result;
222 }
223public:
224
225 /// getOrCreate - Return the specified constant from the map, creating it if
226 /// necessary.
227 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
228 sys::SmartScopedLock<true> Lock(ValueMapLock);
229 MapKey Lookup(Ty, V);
230 ConstantClass* Result = 0;
231
232 typename MapTy::iterator I = Map.find(Lookup);
233 // Is it in the map?
234 if (I != Map.end())
235 Result = static_cast<ConstantClass *>(I->second);
236
237 if (!Result) {
238 // If no preexisting value, create one now...
239 Result = Create(Ty, V, I);
240 }
241
242 return Result;
243 }
244
245 void remove(ConstantClass *CP) {
246 sys::SmartScopedLock<true> Lock(ValueMapLock);
247 typename MapTy::iterator I = FindExistingElement(CP);
248 assert(I != Map.end() && "Constant not found in constant table!");
249 assert(I->second == CP && "Didn't find correct element?");
250
251 if (HasLargeKey) // Remember the reverse mapping if needed.
252 InverseMap.erase(CP);
253
254 // Now that we found the entry, make sure this isn't the entry that
255 // the AbstractTypeMap points to.
256 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
257 if (Ty->isAbstract()) {
258 assert(AbstractTypeMap.count(Ty) &&
259 "Abstract type not in AbstractTypeMap?");
260 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
261 if (ATMEntryIt == I) {
262 // Yes, we are removing the representative entry for this type.
263 // See if there are any other entries of the same type.
264 typename MapTy::iterator TmpIt = ATMEntryIt;
265
266 // First check the entry before this one...
267 if (TmpIt != Map.begin()) {
268 --TmpIt;
269 if (TmpIt->first.first != Ty) // Not the same type, move back...
270 ++TmpIt;
271 }
272
273 // If we didn't find the same type, try to move forward...
274 if (TmpIt == ATMEntryIt) {
275 ++TmpIt;
276 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
277 --TmpIt; // No entry afterwards with the same type
278 }
279
280 // If there is another entry in the map of the same abstract type,
281 // update the AbstractTypeMap entry now.
282 if (TmpIt != ATMEntryIt) {
283 ATMEntryIt = TmpIt;
284 } else {
285 // Otherwise, we are removing the last instance of this type
286 // from the table. Remove from the ATM, and from user list.
287 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
288 AbstractTypeMap.erase(Ty);
289 }
290 }
291 }
292
293 Map.erase(I);
294 }
295
296
297 /// MoveConstantToNewSlot - If we are about to change C to be the element
298 /// specified by I, update our internal data structures to reflect this
299 /// fact.
300 /// NOTE: This function is not locked. It is the responsibility of the
301 /// caller to enforce proper synchronization if using this method.
302 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
303 // First, remove the old location of the specified constant in the map.
304 typename MapTy::iterator OldI = FindExistingElement(C);
305 assert(OldI != Map.end() && "Constant not found in constant table!");
306 assert(OldI->second == C && "Didn't find correct element?");
307
308 // If this constant is the representative element for its abstract type,
309 // update the AbstractTypeMap so that the representative element is I.
310 if (C->getType()->isAbstract()) {
311 typename AbstractTypeMapTy::iterator ATI =
312 AbstractTypeMap.find(C->getType());
313 assert(ATI != AbstractTypeMap.end() &&
314 "Abstract type not in AbstractTypeMap?");
315 if (ATI->second == OldI)
316 ATI->second = I;
317 }
318
319 // Remove the old entry from the map.
320 Map.erase(OldI);
321
322 // Update the inverse map so that we know that this constant is now
323 // located at descriptor I.
324 if (HasLargeKey) {
325 assert(I->second == C && "Bad inversemap entry!");
326 InverseMap[C] = I;
327 }
328 }
329
330 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
331 sys::SmartScopedLock<true> Lock(ValueMapLock);
332 typename AbstractTypeMapTy::iterator I =
333 AbstractTypeMap.find(cast<Type>(OldTy));
334
335 assert(I != AbstractTypeMap.end() &&
336 "Abstract type not in AbstractTypeMap?");
337
338 // Convert a constant at a time until the last one is gone. The last one
339 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
340 // eliminated eventually.
341 do {
342 ConvertConstantType<ConstantClass,
343 TypeClass>::convert(
344 static_cast<ConstantClass *>(I->second->second),
345 cast<TypeClass>(NewTy));
346
347 I = AbstractTypeMap.find(cast<Type>(OldTy));
348 } while (I != AbstractTypeMap.end());
349 }
350
351 // If the type became concrete without being refined to any other existing
352 // type, we just remove ourselves from the ATU list.
353 void typeBecameConcrete(const DerivedType *AbsTy) {
354 AbsTy->removeAbstractTypeUser(this);
355 }
356
357 void dump() const {
358 DOUT << "Constant.cpp: ValueMap\n";
359 }
360};
361
362
Owen Anderson20b34ac2009-07-16 18:04:31 +0000363class ConstantInt;
Owen Andersonc277dc42009-07-16 19:05:41 +0000364class ConstantFP;
Owen Anderson69ab4162009-07-16 22:11:26 +0000365class MDString;
Owen Anderson4118dde2009-07-16 23:44:30 +0000366class MDNode;
Owen Anderson20b34ac2009-07-16 18:04:31 +0000367class LLVMContext;
368class Type;
Owen Anderson4118dde2009-07-16 23:44:30 +0000369class Value;
Owen Anderson20b34ac2009-07-16 18:04:31 +0000370
371struct DenseMapAPIntKeyInfo {
372 struct KeyTy {
373 APInt val;
374 const Type* type;
375 KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
376 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
377 bool operator==(const KeyTy& that) const {
378 return type == that.type && this->val == that.val;
379 }
380 bool operator!=(const KeyTy& that) const {
381 return !this->operator==(that);
382 }
383 };
384 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
385 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
386 static unsigned getHashValue(const KeyTy &Key) {
387 return DenseMapInfo<void*>::getHashValue(Key.type) ^
388 Key.val.getHashValue();
389 }
390 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
391 return LHS == RHS;
392 }
393 static bool isPod() { return false; }
394};
395
Owen Andersonc277dc42009-07-16 19:05:41 +0000396struct DenseMapAPFloatKeyInfo {
397 struct KeyTy {
398 APFloat val;
399 KeyTy(const APFloat& V) : val(V){}
400 KeyTy(const KeyTy& that) : val(that.val) {}
401 bool operator==(const KeyTy& that) const {
402 return this->val.bitwiseIsEqual(that.val);
403 }
404 bool operator!=(const KeyTy& that) const {
405 return !this->operator==(that);
406 }
407 };
408 static inline KeyTy getEmptyKey() {
409 return KeyTy(APFloat(APFloat::Bogus,1));
410 }
411 static inline KeyTy getTombstoneKey() {
412 return KeyTy(APFloat(APFloat::Bogus,2));
413 }
414 static unsigned getHashValue(const KeyTy &Key) {
415 return Key.val.getHashValue();
416 }
417 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
418 return LHS == RHS;
419 }
420 static bool isPod() { return false; }
421};
422
Owen Anderson20b34ac2009-07-16 18:04:31 +0000423class LLVMContextImpl {
424 sys::SmartRWMutex<true> ConstantsLock;
425
426 typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
427 DenseMapAPIntKeyInfo> IntMapTy;
428 IntMapTy IntConstants;
429
Owen Andersonc277dc42009-07-16 19:05:41 +0000430 typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
431 DenseMapAPFloatKeyInfo> FPMapTy;
432 FPMapTy FPConstants;
433
Owen Anderson69ab4162009-07-16 22:11:26 +0000434 StringMap<MDString*> MDStringCache;
435
Owen Anderson4118dde2009-07-16 23:44:30 +0000436 FoldingSet<MDNode> MDNodeSet;
437
Owen Andersonedb4a702009-07-24 23:12:02 +0000438 ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
Owen Anderson3d344922009-07-21 20:55:28 +0000439
440 typedef ValueMap<std::vector<Constant*>, ArrayType,
441 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Owen Andersonedb4a702009-07-24 23:12:02 +0000442 ArrayConstantsTy ArrayConstants;
Owen Anderson39ede7b2009-07-21 20:13:12 +0000443
Owen Anderson909f6002009-07-23 23:25:33 +0000444 typedef ValueMap<std::vector<Constant*>, StructType,
445 ConstantStruct, true /*largekey*/> StructConstantsTy;
Owen Andersonedb4a702009-07-24 23:12:02 +0000446 StructConstantsTy StructConstants;
Owen Anderson909f6002009-07-23 23:25:33 +0000447
Owen Anderson0348a132009-07-24 00:36:24 +0000448 typedef ValueMap<std::vector<Constant*>, VectorType,
449 ConstantVector> VectorConstantsTy;
Owen Andersonedb4a702009-07-24 23:12:02 +0000450 VectorConstantsTy VectorConstants;
Owen Anderson0348a132009-07-24 00:36:24 +0000451
Owen Anderson20b34ac2009-07-16 18:04:31 +0000452 LLVMContext &Context;
Owen Anderson2ad52172009-07-21 02:47:59 +0000453 ConstantInt *TheTrueVal;
454 ConstantInt *TheFalseVal;
455
Owen Anderson20b34ac2009-07-16 18:04:31 +0000456 LLVMContextImpl();
457 LLVMContextImpl(const LLVMContextImpl&);
Owen Andersonedb4a702009-07-24 23:12:02 +0000458
459 friend class ConstantInt;
Owen Anderson69c464d2009-07-27 20:59:43 +0000460 friend class ConstantFP;
Owen Anderson45308b52009-07-27 22:29:26 +0000461 friend class ConstantStruct;
Owen Andersonc2c79322009-07-28 18:32:17 +0000462 friend class ConstantArray;
Owen Anderson4aa32952009-07-28 21:19:26 +0000463 friend class ConstantVector;
Owen Andersonb292b8c2009-07-30 23:03:37 +0000464 friend class ConstantAggregateZero;
Owen Anderson0087fe62009-07-31 21:35:40 +0000465 friend class MDNode;
466 friend class MDString;
Owen Anderson20b34ac2009-07-16 18:04:31 +0000467public:
Owen Anderson39ede7b2009-07-21 20:13:12 +0000468 LLVMContextImpl(LLVMContext &C);
Owen Anderson8e66e0b2009-06-30 00:48:55 +0000469};
470
471}
472
Owen Anderson36f62e52009-06-30 17:06:46 +0000473#endif