1 //===--------------- LLVMContextImpl.cpp - Implementation ------*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements LLVMContextImpl, the opaque implementation
13 //===----------------------------------------------------------------------===//
15 #include "LLVMContextImpl.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/LLVMContext.h"
19 #include "llvm/MDNode.h"
22 static char getValType(ConstantAggregateZero *CPZ) { return 0; }
24 static std::vector<Constant*> getValType(ConstantArray *CA) {
25 std::vector<Constant*> Elements;
26 Elements.reserve(CA->getNumOperands());
27 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
28 Elements.push_back(cast<Constant>(CA->getOperand(i)));
32 static std::vector<Constant*> getValType(ConstantStruct *CS) {
33 std::vector<Constant*> Elements;
34 Elements.reserve(CS->getNumOperands());
35 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
36 Elements.push_back(cast<Constant>(CS->getOperand(i)));
40 static std::vector<Constant*> getValType(ConstantVector *CP) {
41 std::vector<Constant*> Elements;
42 Elements.reserve(CP->getNumOperands());
43 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
44 Elements.push_back(CP->getOperand(i));
49 template<typename T, typename Alloc>
50 struct VISIBILITY_HIDDEN ConstantTraits< std::vector<T, Alloc> > {
51 static unsigned uses(const std::vector<T, Alloc>& v) {
56 template<class ConstantClass, class TypeClass, class ValType>
57 struct VISIBILITY_HIDDEN ConstantCreator {
58 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
59 return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V);
63 template<class ConstantClass, class TypeClass>
64 struct VISIBILITY_HIDDEN ConvertConstantType {
65 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
66 llvm_unreachable("This type cannot be converted!");
70 // ConstantAggregateZero does not take extra "value" argument...
71 template<class ValType>
72 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
73 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
74 return new ConstantAggregateZero(Ty);
79 struct ConvertConstantType<ConstantAggregateZero, Type> {
80 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
81 // Make everyone now use a constant of the new type...
82 Constant *New = NewTy->getContext().getConstantAggregateZero(NewTy);
83 assert(New != OldC && "Didn't replace constant??");
84 OldC->uncheckedReplaceAllUsesWith(New);
85 OldC->destroyConstant(); // This constant is now dead, destroy it.
90 struct ConvertConstantType<ConstantArray, ArrayType> {
91 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
92 // Make everyone now use a constant of the new type...
93 std::vector<Constant*> C;
94 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
95 C.push_back(cast<Constant>(OldC->getOperand(i)));
96 Constant *New = NewTy->getContext().getConstantArray(NewTy, C);
97 assert(New != OldC && "Didn't replace constant??");
98 OldC->uncheckedReplaceAllUsesWith(New);
99 OldC->destroyConstant(); // This constant is now dead, destroy it.
104 struct ConvertConstantType<ConstantStruct, StructType> {
105 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
106 // Make everyone now use a constant of the new type...
107 std::vector<Constant*> C;
108 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
109 C.push_back(cast<Constant>(OldC->getOperand(i)));
110 Constant *New = NewTy->getContext().getConstantStruct(NewTy, C);
111 assert(New != OldC && "Didn't replace constant??");
113 OldC->uncheckedReplaceAllUsesWith(New);
114 OldC->destroyConstant(); // This constant is now dead, destroy it.
119 struct ConvertConstantType<ConstantVector, VectorType> {
120 static void convert(ConstantVector *OldC, const VectorType *NewTy) {
121 // Make everyone now use a constant of the new type...
122 std::vector<Constant*> C;
123 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
124 C.push_back(cast<Constant>(OldC->getOperand(i)));
125 Constant *New = OldC->getContext().getConstantVector(NewTy, C);
126 assert(New != OldC && "Didn't replace constant??");
127 OldC->uncheckedReplaceAllUsesWith(New);
128 OldC->destroyConstant(); // This constant is now dead, destroy it.
133 template<class ValType, class TypeClass, class ConstantClass,
134 bool HasLargeKey /*true for arrays and structs*/ >
135 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
137 typedef std::pair<const Type*, ValType> MapKey;
138 typedef std::map<MapKey, Constant *> MapTy;
139 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
140 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
142 /// Map - This is the main map from the element descriptor to the Constants.
143 /// This is the primary way we avoid creating two of the same shape
147 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
148 /// from the constants to their element in Map. This is important for
149 /// removal of constants from the array, which would otherwise have to scan
150 /// through the map with very large keys.
151 InverseMapTy InverseMap;
153 /// AbstractTypeMap - Map for abstract type constants.
155 AbstractTypeMapTy AbstractTypeMap;
157 /// ValueMapLock - Mutex for this map.
158 sys::SmartMutex<true> ValueMapLock;
161 // NOTE: This function is not locked. It is the caller's responsibility
162 // to enforce proper synchronization.
163 typename MapTy::iterator map_end() { return Map.end(); }
165 /// InsertOrGetItem - Return an iterator for the specified element.
166 /// If the element exists in the map, the returned iterator points to the
167 /// entry and Exists=true. If not, the iterator points to the newly
168 /// inserted entry and returns Exists=false. Newly inserted entries have
169 /// I->second == 0, and should be filled in.
170 /// NOTE: This function is not locked. It is the caller's responsibility
171 // to enforce proper synchronization.
172 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
175 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
181 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
183 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
184 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
185 IMI->second->second == CP &&
186 "InverseMap corrupt!");
190 typename MapTy::iterator I =
191 Map.find(MapKey(static_cast<const TypeClass*>(CP->getRawType()),
193 if (I == Map.end() || I->second != CP) {
194 // FIXME: This should not use a linear scan. If this gets to be a
195 // performance problem, someone should look at this.
196 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
202 ConstantClass* Create(const TypeClass *Ty, const ValType &V,
203 typename MapTy::iterator I) {
204 ConstantClass* Result =
205 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
207 assert(Result->getType() == Ty && "Type specified is not correct!");
208 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
210 if (HasLargeKey) // Remember the reverse mapping if needed.
211 InverseMap.insert(std::make_pair(Result, I));
213 // If the type of the constant is abstract, make sure that an entry
214 // exists for it in the AbstractTypeMap.
215 if (Ty->isAbstract()) {
216 typename AbstractTypeMapTy::iterator TI =
217 AbstractTypeMap.find(Ty);
219 if (TI == AbstractTypeMap.end()) {
220 // Add ourselves to the ATU list of the type.
221 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
223 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
231 /// getOrCreate - Return the specified constant from the map, creating it if
233 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
234 sys::SmartScopedLock<true> Lock(ValueMapLock);
235 MapKey Lookup(Ty, V);
236 ConstantClass* Result = 0;
238 typename MapTy::iterator I = Map.find(Lookup);
241 Result = static_cast<ConstantClass *>(I->second);
244 // If no preexisting value, create one now...
245 Result = Create(Ty, V, I);
251 void remove(ConstantClass *CP) {
252 sys::SmartScopedLock<true> Lock(ValueMapLock);
253 typename MapTy::iterator I = FindExistingElement(CP);
254 assert(I != Map.end() && "Constant not found in constant table!");
255 assert(I->second == CP && "Didn't find correct element?");
257 if (HasLargeKey) // Remember the reverse mapping if needed.
258 InverseMap.erase(CP);
260 // Now that we found the entry, make sure this isn't the entry that
261 // the AbstractTypeMap points to.
262 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
263 if (Ty->isAbstract()) {
264 assert(AbstractTypeMap.count(Ty) &&
265 "Abstract type not in AbstractTypeMap?");
266 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
267 if (ATMEntryIt == I) {
268 // Yes, we are removing the representative entry for this type.
269 // See if there are any other entries of the same type.
270 typename MapTy::iterator TmpIt = ATMEntryIt;
272 // First check the entry before this one...
273 if (TmpIt != Map.begin()) {
275 if (TmpIt->first.first != Ty) // Not the same type, move back...
279 // If we didn't find the same type, try to move forward...
280 if (TmpIt == ATMEntryIt) {
282 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
283 --TmpIt; // No entry afterwards with the same type
286 // If there is another entry in the map of the same abstract type,
287 // update the AbstractTypeMap entry now.
288 if (TmpIt != ATMEntryIt) {
291 // Otherwise, we are removing the last instance of this type
292 // from the table. Remove from the ATM, and from user list.
293 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
294 AbstractTypeMap.erase(Ty);
303 /// MoveConstantToNewSlot - If we are about to change C to be the element
304 /// specified by I, update our internal data structures to reflect this
306 /// NOTE: This function is not locked. It is the responsibility of the
307 /// caller to enforce proper synchronization if using this method.
308 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
309 // First, remove the old location of the specified constant in the map.
310 typename MapTy::iterator OldI = FindExistingElement(C);
311 assert(OldI != Map.end() && "Constant not found in constant table!");
312 assert(OldI->second == C && "Didn't find correct element?");
314 // If this constant is the representative element for its abstract type,
315 // update the AbstractTypeMap so that the representative element is I.
316 if (C->getType()->isAbstract()) {
317 typename AbstractTypeMapTy::iterator ATI =
318 AbstractTypeMap.find(C->getType());
319 assert(ATI != AbstractTypeMap.end() &&
320 "Abstract type not in AbstractTypeMap?");
321 if (ATI->second == OldI)
325 // Remove the old entry from the map.
328 // Update the inverse map so that we know that this constant is now
329 // located at descriptor I.
331 assert(I->second == C && "Bad inversemap entry!");
336 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
337 sys::SmartScopedLock<true> Lock(ValueMapLock);
338 typename AbstractTypeMapTy::iterator I =
339 AbstractTypeMap.find(cast<Type>(OldTy));
341 assert(I != AbstractTypeMap.end() &&
342 "Abstract type not in AbstractTypeMap?");
344 // Convert a constant at a time until the last one is gone. The last one
345 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
346 // eliminated eventually.
348 ConvertConstantType<ConstantClass,
350 static_cast<ConstantClass *>(I->second->second),
351 cast<TypeClass>(NewTy));
353 I = AbstractTypeMap.find(cast<Type>(OldTy));
354 } while (I != AbstractTypeMap.end());
357 // If the type became concrete without being refined to any other existing
358 // type, we just remove ourselves from the ATU list.
359 void typeBecameConcrete(const DerivedType *AbsTy) {
360 AbsTy->removeAbstractTypeUser(this);
364 DOUT << "Constant.cpp: ValueMap\n";
368 LLVMContextImpl::LLVMContextImpl(LLVMContext &C) :
369 Context(C), TheTrueVal(0), TheFalseVal(0) {
370 AggZeroConstants = new ValueMap<char, Type, ConstantAggregateZero>();
371 ArrayConstants = new ArrayConstantsTy();
372 StructConstants = new StructConstantsTy();
373 VectorConstants = new VectorConstantsTy();
376 LLVMContextImpl::~LLVMContextImpl() {
377 delete AggZeroConstants;
378 delete ArrayConstants;
379 delete StructConstants;
380 delete VectorConstants;
383 // Get a ConstantInt from an APInt. Note that the value stored in the DenseMap
384 // as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the
385 // operator== and operator!= to ensure that the DenseMap doesn't attempt to
386 // compare APInt's of different widths, which would violate an APInt class
387 // invariant which generates an assertion.
388 ConstantInt *LLVMContextImpl::getConstantInt(const APInt& V) {
389 // Get the corresponding integer type for the bit width of the value.
390 const IntegerType *ITy = Context.getIntegerType(V.getBitWidth());
391 // get an existing value or the insertion position
392 DenseMapAPIntKeyInfo::KeyTy Key(V, ITy);
394 ConstantsLock.reader_acquire();
395 ConstantInt *&Slot = IntConstants[Key];
396 ConstantsLock.reader_release();
399 sys::SmartScopedWriter<true> Writer(ConstantsLock);
400 ConstantInt *&NewSlot = IntConstants[Key];
402 NewSlot = new ConstantInt(ITy, V);
411 ConstantFP *LLVMContextImpl::getConstantFP(const APFloat &V) {
412 DenseMapAPFloatKeyInfo::KeyTy Key(V);
414 ConstantsLock.reader_acquire();
415 ConstantFP *&Slot = FPConstants[Key];
416 ConstantsLock.reader_release();
419 sys::SmartScopedWriter<true> Writer(ConstantsLock);
420 ConstantFP *&NewSlot = FPConstants[Key];
423 if (&V.getSemantics() == &APFloat::IEEEsingle)
425 else if (&V.getSemantics() == &APFloat::IEEEdouble)
427 else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
428 Ty = Type::X86_FP80Ty;
429 else if (&V.getSemantics() == &APFloat::IEEEquad)
432 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble &&
433 "Unknown FP format");
434 Ty = Type::PPC_FP128Ty;
436 NewSlot = new ConstantFP(Ty, V);
445 MDString *LLVMContextImpl::getMDString(const char *StrBegin,
446 unsigned StrLength) {
447 sys::SmartScopedWriter<true> Writer(ConstantsLock);
448 StringMapEntry<MDString *> &Entry =
449 MDStringCache.GetOrCreateValue(StringRef(StrBegin, StrLength));
450 MDString *&S = Entry.getValue();
451 if (!S) S = new MDString(Entry.getKeyData(),
452 Entry.getKeyLength());
457 MDNode *LLVMContextImpl::getMDNode(Value*const* Vals, unsigned NumVals) {
459 for (unsigned i = 0; i != NumVals; ++i)
460 ID.AddPointer(Vals[i]);
462 ConstantsLock.reader_acquire();
464 MDNode *N = MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
465 ConstantsLock.reader_release();
468 sys::SmartScopedWriter<true> Writer(ConstantsLock);
469 N = MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
471 // InsertPoint will have been set by the FindNodeOrInsertPos call.
472 N = new MDNode(Vals, NumVals);
473 MDNodeSet.InsertNode(N, InsertPoint);
480 ConstantAggregateZero*
481 LLVMContextImpl::getConstantAggregateZero(const Type *Ty) {
482 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) &&
483 "Cannot create an aggregate zero of non-aggregate type!");
485 // Implicitly locked.
486 return AggZeroConstants->getOrCreate(Ty, 0);
489 Constant *LLVMContextImpl::getConstantArray(const ArrayType *Ty,
490 const std::vector<Constant*> &V) {
491 // If this is an all-zero array, return a ConstantAggregateZero object
494 if (!C->isNullValue()) {
495 // Implicitly locked.
496 return ArrayConstants->getOrCreate(Ty, V);
498 for (unsigned i = 1, e = V.size(); i != e; ++i)
500 // Implicitly locked.
501 return ArrayConstants->getOrCreate(Ty, V);
505 return Context.getConstantAggregateZero(Ty);
508 Constant *LLVMContextImpl::getConstantStruct(const StructType *Ty,
509 const std::vector<Constant*> &V) {
510 // Create a ConstantAggregateZero value if all elements are zeros...
511 for (unsigned i = 0, e = V.size(); i != e; ++i)
512 if (!V[i]->isNullValue())
513 // Implicitly locked.
514 return StructConstants->getOrCreate(Ty, V);
516 return Context.getConstantAggregateZero(Ty);
519 Constant *LLVMContextImpl::getConstantVector(const VectorType *Ty,
520 const std::vector<Constant*> &V) {
521 assert(!V.empty() && "Vectors can't be empty");
522 // If this is an all-undef or alll-zero vector, return a
523 // ConstantAggregateZero or UndefValue.
525 bool isZero = C->isNullValue();
526 bool isUndef = isa<UndefValue>(C);
528 if (isZero || isUndef) {
529 for (unsigned i = 1, e = V.size(); i != e; ++i)
531 isZero = isUndef = false;
537 return Context.getConstantAggregateZero(Ty);
539 return Context.getUndef(Ty);
541 // Implicitly locked.
542 return VectorConstants->getOrCreate(Ty, V);
545 // *** erase methods ***
547 void LLVMContextImpl::erase(MDString *M) {
548 sys::SmartScopedWriter<true> Writer(ConstantsLock);
549 MDStringCache.erase(MDStringCache.find(StringRef(M->StrBegin,
553 void LLVMContextImpl::erase(MDNode *M) {
554 sys::SmartScopedWriter<true> Writer(ConstantsLock);
555 MDNodeSet.RemoveNode(M);
558 void LLVMContextImpl::erase(ConstantAggregateZero *Z) {
559 AggZeroConstants->remove(Z);
562 void LLVMContextImpl::erase(ConstantArray *C) {
563 ArrayConstants->remove(C);
566 void LLVMContextImpl::erase(ConstantStruct *S) {
567 StructConstants->remove(S);
570 void LLVMContextImpl::erase(ConstantVector *V) {
571 VectorConstants->remove(V);
574 // *** RAUW helpers ***
576 Constant *LLVMContextImpl::replaceUsesOfWithOnConstant(ConstantArray *CA,
577 Value *From, Value *To, Use *U) {
578 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
579 Constant *ToC = cast<Constant>(To);
581 std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
582 Lookup.first.first = CA->getType();
585 std::vector<Constant*> &Values = Lookup.first.second;
586 Values.reserve(CA->getNumOperands()); // Build replacement array.
588 // Fill values with the modified operands of the constant array. Also,
589 // compute whether this turns into an all-zeros array.
590 bool isAllZeros = false;
591 unsigned NumUpdated = 0;
592 if (!ToC->isNullValue()) {
593 for (Use *O = CA->OperandList, *E = CA->OperandList + CA->getNumOperands();
595 Constant *Val = cast<Constant>(O->get());
600 Values.push_back(Val);
604 for (Use *O = CA->OperandList, *E = CA->OperandList + CA->getNumOperands();
606 Constant *Val = cast<Constant>(O->get());
611 Values.push_back(Val);
612 if (isAllZeros) isAllZeros = Val->isNullValue();
616 Constant *Replacement = 0;
618 Replacement = Context.getConstantAggregateZero(CA->getType());
620 // Check to see if we have this array type already.
621 sys::SmartScopedWriter<true> Writer(ConstantsLock);
623 ArrayConstantsTy::MapTy::iterator I =
624 ArrayConstants->InsertOrGetItem(Lookup, Exists);
627 Replacement = I->second;
629 // Okay, the new shape doesn't exist in the system yet. Instead of
630 // creating a new constant array, inserting it, replaceallusesof'ing the
631 // old with the new, then deleting the old... just update the current one
633 ArrayConstants->MoveConstantToNewSlot(CA, I);
635 // Update to the new value. Optimize for the case when we have a single
636 // operand that we're changing, but handle bulk updates efficiently.
637 if (NumUpdated == 1) {
638 unsigned OperandToUpdate = U - CA->OperandList;
639 assert(CA->getOperand(OperandToUpdate) == From &&
640 "ReplaceAllUsesWith broken!");
641 CA->setOperand(OperandToUpdate, ToC);
643 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
644 if (CA->getOperand(i) == From)
645 CA->setOperand(i, ToC);
654 Constant *LLVMContextImpl::replaceUsesOfWithOnConstant(ConstantStruct *CS,
655 Value *From, Value *To, Use *U) {
656 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
657 Constant *ToC = cast<Constant>(To);
659 unsigned OperandToUpdate = U - CS->OperandList;
660 assert(CS->getOperand(OperandToUpdate) == From &&
661 "ReplaceAllUsesWith broken!");
663 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
664 Lookup.first.first = CS->getType();
666 std::vector<Constant*> &Values = Lookup.first.second;
667 Values.reserve(CS->getNumOperands()); // Build replacement struct.
670 // Fill values with the modified operands of the constant struct. Also,
671 // compute whether this turns into an all-zeros struct.
672 bool isAllZeros = false;
673 if (!ToC->isNullValue()) {
674 for (Use *O = CS->OperandList, *E = CS->OperandList + CS->getNumOperands();
676 Values.push_back(cast<Constant>(O->get()));
679 for (Use *O = CS->OperandList, *E = CS->OperandList + CS->getNumOperands();
681 Constant *Val = cast<Constant>(O->get());
682 Values.push_back(Val);
683 if (isAllZeros) isAllZeros = Val->isNullValue();
686 Values[OperandToUpdate] = ToC;
688 Constant *Replacement = 0;
690 Replacement = Context.getConstantAggregateZero(CS->getType());
692 // Check to see if we have this array type already.
693 sys::SmartScopedWriter<true> Writer(ConstantsLock);
695 StructConstantsTy::MapTy::iterator I =
696 StructConstants->InsertOrGetItem(Lookup, Exists);
699 Replacement = I->second;
701 // Okay, the new shape doesn't exist in the system yet. Instead of
702 // creating a new constant struct, inserting it, replaceallusesof'ing the
703 // old with the new, then deleting the old... just update the current one
705 StructConstants->MoveConstantToNewSlot(CS, I);
707 // Update to the new value.
708 CS->setOperand(OperandToUpdate, ToC);
713 assert(Replacement != CS && "I didn't contain From!");