1 //===----------------- LLVMContextImpl.h - 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 declares LLVMContextImpl, the opaque implementation
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_LLVMCONTEXT_IMPL_H
16 #define LLVM_LLVMCONTEXT_IMPL_H
18 #include "llvm/LLVMContext.h"
19 #include "llvm/Constants.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Instructions.h"
22 #include "llvm/Operator.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/System/Mutex.h"
26 #include "llvm/System/RWMutex.h"
27 #include "llvm/ADT/APFloat.h"
28 #include "llvm/ADT/APInt.h"
29 #include "llvm/ADT/DenseMap.h"
30 #include "llvm/ADT/FoldingSet.h"
31 #include "llvm/ADT/StringMap.h"
36 template<class ValType>
37 struct ConstantTraits;
40 /// UnaryConstantExpr - This class is private to Constants.cpp, and is used
41 /// behind the scenes to implement unary constant exprs.
42 class UnaryConstantExpr : public ConstantExpr {
43 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
45 // allocate space for exactly one operand
46 void *operator new(size_t s) {
47 return User::operator new(s, 1);
49 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
50 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
53 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
56 /// BinaryConstantExpr - This class is private to Constants.cpp, and is used
57 /// behind the scenes to implement binary constant exprs.
58 class BinaryConstantExpr : public ConstantExpr {
59 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
61 // allocate space for exactly two operands
62 void *operator new(size_t s) {
63 return User::operator new(s, 2);
65 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
66 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
70 /// Transparently provide more efficient getOperand methods.
71 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
74 /// SelectConstantExpr - This class is private to Constants.cpp, and is used
75 /// behind the scenes to implement select constant exprs.
76 class SelectConstantExpr : public ConstantExpr {
77 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
79 // allocate space for exactly three operands
80 void *operator new(size_t s) {
81 return User::operator new(s, 3);
83 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
84 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
89 /// Transparently provide more efficient getOperand methods.
90 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
93 /// ExtractElementConstantExpr - This class is private to
94 /// Constants.cpp, and is used behind the scenes to implement
95 /// extractelement constant exprs.
96 class ExtractElementConstantExpr : public ConstantExpr {
97 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
99 // allocate space for exactly two operands
100 void *operator new(size_t s) {
101 return User::operator new(s, 2);
103 ExtractElementConstantExpr(Constant *C1, Constant *C2)
104 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
105 Instruction::ExtractElement, &Op<0>(), 2) {
109 /// Transparently provide more efficient getOperand methods.
110 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
113 /// InsertElementConstantExpr - This class is private to
114 /// Constants.cpp, and is used behind the scenes to implement
115 /// insertelement constant exprs.
116 class InsertElementConstantExpr : public ConstantExpr {
117 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
119 // allocate space for exactly three operands
120 void *operator new(size_t s) {
121 return User::operator new(s, 3);
123 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
124 : ConstantExpr(C1->getType(), Instruction::InsertElement,
130 /// Transparently provide more efficient getOperand methods.
131 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
134 /// ShuffleVectorConstantExpr - This class is private to
135 /// Constants.cpp, and is used behind the scenes to implement
136 /// shufflevector constant exprs.
137 class ShuffleVectorConstantExpr : public ConstantExpr {
138 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
140 // allocate space for exactly three operands
141 void *operator new(size_t s) {
142 return User::operator new(s, 3);
144 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
145 : ConstantExpr(VectorType::get(
146 cast<VectorType>(C1->getType())->getElementType(),
147 cast<VectorType>(C3->getType())->getNumElements()),
148 Instruction::ShuffleVector,
154 /// Transparently provide more efficient getOperand methods.
155 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
158 /// ExtractValueConstantExpr - This class is private to
159 /// Constants.cpp, and is used behind the scenes to implement
160 /// extractvalue constant exprs.
161 class ExtractValueConstantExpr : public ConstantExpr {
162 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
164 // allocate space for exactly one operand
165 void *operator new(size_t s) {
166 return User::operator new(s, 1);
168 ExtractValueConstantExpr(Constant *Agg,
169 const SmallVector<unsigned, 4> &IdxList,
171 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
176 /// Indices - These identify which value to extract.
177 const SmallVector<unsigned, 4> Indices;
179 /// Transparently provide more efficient getOperand methods.
180 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
183 /// InsertValueConstantExpr - This class is private to
184 /// Constants.cpp, and is used behind the scenes to implement
185 /// insertvalue constant exprs.
186 class InsertValueConstantExpr : public ConstantExpr {
187 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
189 // allocate space for exactly one operand
190 void *operator new(size_t s) {
191 return User::operator new(s, 2);
193 InsertValueConstantExpr(Constant *Agg, Constant *Val,
194 const SmallVector<unsigned, 4> &IdxList,
196 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
202 /// Indices - These identify the position for the insertion.
203 const SmallVector<unsigned, 4> Indices;
205 /// Transparently provide more efficient getOperand methods.
206 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
210 /// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
211 /// used behind the scenes to implement getelementpr constant exprs.
212 class GetElementPtrConstantExpr : public ConstantExpr {
213 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
216 static GetElementPtrConstantExpr *Create(Constant *C,
217 const std::vector<Constant*>&IdxList,
218 const Type *DestTy) {
220 new(IdxList.size() + 1) GetElementPtrConstantExpr(C, IdxList, DestTy);
222 /// Transparently provide more efficient getOperand methods.
223 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
226 // CompareConstantExpr - This class is private to Constants.cpp, and is used
227 // behind the scenes to implement ICmp and FCmp constant expressions. This is
228 // needed in order to store the predicate value for these instructions.
229 struct CompareConstantExpr : public ConstantExpr {
230 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
231 // allocate space for exactly two operands
232 void *operator new(size_t s) {
233 return User::operator new(s, 2);
235 unsigned short predicate;
236 CompareConstantExpr(const Type *ty, Instruction::OtherOps opc,
237 unsigned short pred, Constant* LHS, Constant* RHS)
238 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
242 /// Transparently provide more efficient getOperand methods.
243 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
247 struct OperandTraits<UnaryConstantExpr> : FixedNumOperandTraits<1> {
249 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
252 struct OperandTraits<BinaryConstantExpr> : FixedNumOperandTraits<2> {
254 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
257 struct OperandTraits<SelectConstantExpr> : FixedNumOperandTraits<3> {
259 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
262 struct OperandTraits<ExtractElementConstantExpr> : FixedNumOperandTraits<2> {
264 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
267 struct OperandTraits<InsertElementConstantExpr> : FixedNumOperandTraits<3> {
269 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
272 struct OperandTraits<ShuffleVectorConstantExpr> : FixedNumOperandTraits<3> {
274 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
277 struct OperandTraits<ExtractValueConstantExpr> : FixedNumOperandTraits<1> {
279 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
282 struct OperandTraits<InsertValueConstantExpr> : FixedNumOperandTraits<2> {
284 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
287 struct OperandTraits<GetElementPtrConstantExpr> : VariadicOperandTraits<1> {
290 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
294 struct OperandTraits<CompareConstantExpr> : FixedNumOperandTraits<2> {
296 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
298 struct ExprMapKeyType {
299 typedef SmallVector<unsigned, 4> IndexList;
301 ExprMapKeyType(unsigned opc,
302 const std::vector<Constant*> &ops,
303 unsigned short pred = 0,
304 const IndexList &inds = IndexList())
305 : opcode(opc), predicate(pred), operands(ops), indices(inds) {}
308 std::vector<Constant*> operands;
310 bool operator==(const ExprMapKeyType& that) const {
311 return this->opcode == that.opcode &&
312 this->predicate == that.predicate &&
313 this->operands == that.operands &&
314 this->indices == that.indices;
316 bool operator<(const ExprMapKeyType & that) const {
317 return this->opcode < that.opcode ||
318 (this->opcode == that.opcode && this->predicate < that.predicate) ||
319 (this->opcode == that.opcode && this->predicate == that.predicate &&
320 this->operands < that.operands) ||
321 (this->opcode == that.opcode && this->predicate == that.predicate &&
322 this->operands == that.operands && this->indices < that.indices);
325 bool operator!=(const ExprMapKeyType& that) const {
326 return !(*this == that);
330 // The number of operands for each ConstantCreator::create method is
331 // determined by the ConstantTraits template.
332 // ConstantCreator - A class that is used to create constants by
333 // ValueMap*. This class should be partially specialized if there is
334 // something strange that needs to be done to interface to the ctor for the
337 template<typename T, typename Alloc>
338 struct ConstantTraits< std::vector<T, Alloc> > {
339 static unsigned uses(const std::vector<T, Alloc>& v) {
344 template<class ConstantClass, class TypeClass, class ValType>
345 struct ConstantCreator {
346 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
347 return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V);
351 template<class ConstantClass, class TypeClass>
352 struct ConvertConstantType {
353 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
354 llvm_unreachable("This type cannot be converted!");
359 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
360 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
361 unsigned short pred = 0) {
362 if (Instruction::isCast(V.opcode))
363 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
364 if ((V.opcode >= Instruction::BinaryOpsBegin &&
365 V.opcode < Instruction::BinaryOpsEnd))
366 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
367 if (V.opcode == Instruction::Select)
368 return new SelectConstantExpr(V.operands[0], V.operands[1],
370 if (V.opcode == Instruction::ExtractElement)
371 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
372 if (V.opcode == Instruction::InsertElement)
373 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
375 if (V.opcode == Instruction::ShuffleVector)
376 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
378 if (V.opcode == Instruction::InsertValue)
379 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
381 if (V.opcode == Instruction::ExtractValue)
382 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
383 if (V.opcode == Instruction::GetElementPtr) {
384 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
385 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty);
388 // The compare instructions are weird. We have to encode the predicate
389 // value and it is combined with the instruction opcode by multiplying
390 // the opcode by one hundred. We must decode this to get the predicate.
391 if (V.opcode == Instruction::ICmp)
392 return new CompareConstantExpr(Ty, Instruction::ICmp, V.predicate,
393 V.operands[0], V.operands[1]);
394 if (V.opcode == Instruction::FCmp)
395 return new CompareConstantExpr(Ty, Instruction::FCmp, V.predicate,
396 V.operands[0], V.operands[1]);
397 llvm_unreachable("Invalid ConstantExpr!");
403 struct ConvertConstantType<ConstantExpr, Type> {
404 static void convert(ConstantExpr *OldC, const Type *NewTy) {
406 switch (OldC->getOpcode()) {
407 case Instruction::Trunc:
408 case Instruction::ZExt:
409 case Instruction::SExt:
410 case Instruction::FPTrunc:
411 case Instruction::FPExt:
412 case Instruction::UIToFP:
413 case Instruction::SIToFP:
414 case Instruction::FPToUI:
415 case Instruction::FPToSI:
416 case Instruction::PtrToInt:
417 case Instruction::IntToPtr:
418 case Instruction::BitCast:
419 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
422 case Instruction::Select:
423 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
425 OldC->getOperand(2));
428 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
429 OldC->getOpcode() < Instruction::BinaryOpsEnd);
430 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
431 OldC->getOperand(1));
433 case Instruction::GetElementPtr:
434 // Make everyone now use a constant of the new type...
435 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
436 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
437 &Idx[0], Idx.size());
441 assert(New != OldC && "Didn't replace constant??");
442 OldC->uncheckedReplaceAllUsesWith(New);
443 OldC->destroyConstant(); // This constant is now dead, destroy it.
447 // ConstantAggregateZero does not take extra "value" argument...
448 template<class ValType>
449 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
450 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
451 return new ConstantAggregateZero(Ty);
456 struct ConvertConstantType<ConstantVector, VectorType> {
457 static void convert(ConstantVector *OldC, const VectorType *NewTy) {
458 // Make everyone now use a constant of the new type...
459 std::vector<Constant*> C;
460 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
461 C.push_back(cast<Constant>(OldC->getOperand(i)));
462 Constant *New = ConstantVector::get(NewTy, C);
463 assert(New != OldC && "Didn't replace constant??");
464 OldC->uncheckedReplaceAllUsesWith(New);
465 OldC->destroyConstant(); // This constant is now dead, destroy it.
470 struct ConvertConstantType<ConstantAggregateZero, Type> {
471 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
472 // Make everyone now use a constant of the new type...
473 Constant *New = ConstantAggregateZero::get(NewTy);
474 assert(New != OldC && "Didn't replace constant??");
475 OldC->uncheckedReplaceAllUsesWith(New);
476 OldC->destroyConstant(); // This constant is now dead, destroy it.
481 struct ConvertConstantType<ConstantArray, ArrayType> {
482 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
483 // Make everyone now use a constant of the new type...
484 std::vector<Constant*> C;
485 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
486 C.push_back(cast<Constant>(OldC->getOperand(i)));
487 Constant *New = ConstantArray::get(NewTy, C);
488 assert(New != OldC && "Didn't replace constant??");
489 OldC->uncheckedReplaceAllUsesWith(New);
490 OldC->destroyConstant(); // This constant is now dead, destroy it.
495 struct ConvertConstantType<ConstantStruct, StructType> {
496 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
497 // Make everyone now use a constant of the new type...
498 std::vector<Constant*> C;
499 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
500 C.push_back(cast<Constant>(OldC->getOperand(i)));
501 Constant *New = ConstantStruct::get(NewTy, C);
502 assert(New != OldC && "Didn't replace constant??");
504 OldC->uncheckedReplaceAllUsesWith(New);
505 OldC->destroyConstant(); // This constant is now dead, destroy it.
509 // ConstantPointerNull does not take extra "value" argument...
510 template<class ValType>
511 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
512 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
513 return new ConstantPointerNull(Ty);
518 struct ConvertConstantType<ConstantPointerNull, PointerType> {
519 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
520 // Make everyone now use a constant of the new type...
521 Constant *New = ConstantPointerNull::get(NewTy);
522 assert(New != OldC && "Didn't replace constant??");
523 OldC->uncheckedReplaceAllUsesWith(New);
524 OldC->destroyConstant(); // This constant is now dead, destroy it.
528 // UndefValue does not take extra "value" argument...
529 template<class ValType>
530 struct ConstantCreator<UndefValue, Type, ValType> {
531 static UndefValue *create(const Type *Ty, const ValType &V) {
532 return new UndefValue(Ty);
537 struct ConvertConstantType<UndefValue, Type> {
538 static void convert(UndefValue *OldC, const Type *NewTy) {
539 // Make everyone now use a constant of the new type.
540 Constant *New = UndefValue::get(NewTy);
541 assert(New != OldC && "Didn't replace constant??");
542 OldC->uncheckedReplaceAllUsesWith(New);
543 OldC->destroyConstant(); // This constant is now dead, destroy it.
547 template<class ValType, class TypeClass, class ConstantClass,
548 bool HasLargeKey = false /*true for arrays and structs*/ >
549 class ValueMap : public AbstractTypeUser {
551 typedef std::pair<const Type*, ValType> MapKey;
552 typedef std::map<MapKey, Constant *> MapTy;
553 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
554 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
556 /// Map - This is the main map from the element descriptor to the Constants.
557 /// This is the primary way we avoid creating two of the same shape
561 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
562 /// from the constants to their element in Map. This is important for
563 /// removal of constants from the array, which would otherwise have to scan
564 /// through the map with very large keys.
565 InverseMapTy InverseMap;
567 /// AbstractTypeMap - Map for abstract type constants.
569 AbstractTypeMapTy AbstractTypeMap;
571 /// ValueMapLock - Mutex for this map.
572 sys::SmartMutex<true> ValueMapLock;
575 // NOTE: This function is not locked. It is the caller's responsibility
576 // to enforce proper synchronization.
577 typename MapTy::iterator map_end() { return Map.end(); }
579 /// InsertOrGetItem - Return an iterator for the specified element.
580 /// If the element exists in the map, the returned iterator points to the
581 /// entry and Exists=true. If not, the iterator points to the newly
582 /// inserted entry and returns Exists=false. Newly inserted entries have
583 /// I->second == 0, and should be filled in.
584 /// NOTE: This function is not locked. It is the caller's responsibility
585 // to enforce proper synchronization.
586 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
589 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
595 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
597 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
598 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
599 IMI->second->second == CP &&
600 "InverseMap corrupt!");
604 typename MapTy::iterator I =
605 Map.find(MapKey(static_cast<const TypeClass*>(CP->getRawType()),
607 if (I == Map.end() || I->second != CP) {
608 // FIXME: This should not use a linear scan. If this gets to be a
609 // performance problem, someone should look at this.
610 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
616 ConstantClass* Create(const TypeClass *Ty, const ValType &V,
617 typename MapTy::iterator I) {
618 ConstantClass* Result =
619 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
621 assert(Result->getType() == Ty && "Type specified is not correct!");
622 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
624 if (HasLargeKey) // Remember the reverse mapping if needed.
625 InverseMap.insert(std::make_pair(Result, I));
627 // If the type of the constant is abstract, make sure that an entry
628 // exists for it in the AbstractTypeMap.
629 if (Ty->isAbstract()) {
630 typename AbstractTypeMapTy::iterator TI =
631 AbstractTypeMap.find(Ty);
633 if (TI == AbstractTypeMap.end()) {
634 // Add ourselves to the ATU list of the type.
635 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
637 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
645 /// getOrCreate - Return the specified constant from the map, creating it if
647 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
648 sys::SmartScopedLock<true> Lock(ValueMapLock);
649 MapKey Lookup(Ty, V);
650 ConstantClass* Result = 0;
652 typename MapTy::iterator I = Map.find(Lookup);
655 Result = static_cast<ConstantClass *>(I->second);
658 // If no preexisting value, create one now...
659 Result = Create(Ty, V, I);
665 void remove(ConstantClass *CP) {
666 sys::SmartScopedLock<true> Lock(ValueMapLock);
667 typename MapTy::iterator I = FindExistingElement(CP);
668 assert(I != Map.end() && "Constant not found in constant table!");
669 assert(I->second == CP && "Didn't find correct element?");
671 if (HasLargeKey) // Remember the reverse mapping if needed.
672 InverseMap.erase(CP);
674 // Now that we found the entry, make sure this isn't the entry that
675 // the AbstractTypeMap points to.
676 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
677 if (Ty->isAbstract()) {
678 assert(AbstractTypeMap.count(Ty) &&
679 "Abstract type not in AbstractTypeMap?");
680 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
681 if (ATMEntryIt == I) {
682 // Yes, we are removing the representative entry for this type.
683 // See if there are any other entries of the same type.
684 typename MapTy::iterator TmpIt = ATMEntryIt;
686 // First check the entry before this one...
687 if (TmpIt != Map.begin()) {
689 if (TmpIt->first.first != Ty) // Not the same type, move back...
693 // If we didn't find the same type, try to move forward...
694 if (TmpIt == ATMEntryIt) {
696 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
697 --TmpIt; // No entry afterwards with the same type
700 // If there is another entry in the map of the same abstract type,
701 // update the AbstractTypeMap entry now.
702 if (TmpIt != ATMEntryIt) {
705 // Otherwise, we are removing the last instance of this type
706 // from the table. Remove from the ATM, and from user list.
707 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
708 AbstractTypeMap.erase(Ty);
717 /// MoveConstantToNewSlot - If we are about to change C to be the element
718 /// specified by I, update our internal data structures to reflect this
720 /// NOTE: This function is not locked. It is the responsibility of the
721 /// caller to enforce proper synchronization if using this method.
722 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
723 // First, remove the old location of the specified constant in the map.
724 typename MapTy::iterator OldI = FindExistingElement(C);
725 assert(OldI != Map.end() && "Constant not found in constant table!");
726 assert(OldI->second == C && "Didn't find correct element?");
728 // If this constant is the representative element for its abstract type,
729 // update the AbstractTypeMap so that the representative element is I.
730 if (C->getType()->isAbstract()) {
731 typename AbstractTypeMapTy::iterator ATI =
732 AbstractTypeMap.find(C->getType());
733 assert(ATI != AbstractTypeMap.end() &&
734 "Abstract type not in AbstractTypeMap?");
735 if (ATI->second == OldI)
739 // Remove the old entry from the map.
742 // Update the inverse map so that we know that this constant is now
743 // located at descriptor I.
745 assert(I->second == C && "Bad inversemap entry!");
750 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
751 sys::SmartScopedLock<true> Lock(ValueMapLock);
752 typename AbstractTypeMapTy::iterator I =
753 AbstractTypeMap.find(cast<Type>(OldTy));
755 assert(I != AbstractTypeMap.end() &&
756 "Abstract type not in AbstractTypeMap?");
758 // Convert a constant at a time until the last one is gone. The last one
759 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
760 // eliminated eventually.
762 ConvertConstantType<ConstantClass,
764 static_cast<ConstantClass *>(I->second->second),
765 cast<TypeClass>(NewTy));
767 I = AbstractTypeMap.find(cast<Type>(OldTy));
768 } while (I != AbstractTypeMap.end());
771 // If the type became concrete without being refined to any other existing
772 // type, we just remove ourselves from the ATU list.
773 void typeBecameConcrete(const DerivedType *AbsTy) {
774 AbsTy->removeAbstractTypeUser(this);
778 DOUT << "Constant.cpp: ValueMap\n";
791 struct DenseMapAPIntKeyInfo {
795 KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
796 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
797 bool operator==(const KeyTy& that) const {
798 return type == that.type && this->val == that.val;
800 bool operator!=(const KeyTy& that) const {
801 return !this->operator==(that);
804 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
805 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
806 static unsigned getHashValue(const KeyTy &Key) {
807 return DenseMapInfo<void*>::getHashValue(Key.type) ^
808 Key.val.getHashValue();
810 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
813 static bool isPod() { return false; }
816 struct DenseMapAPFloatKeyInfo {
819 KeyTy(const APFloat& V) : val(V){}
820 KeyTy(const KeyTy& that) : val(that.val) {}
821 bool operator==(const KeyTy& that) const {
822 return this->val.bitwiseIsEqual(that.val);
824 bool operator!=(const KeyTy& that) const {
825 return !this->operator==(that);
828 static inline KeyTy getEmptyKey() {
829 return KeyTy(APFloat(APFloat::Bogus,1));
831 static inline KeyTy getTombstoneKey() {
832 return KeyTy(APFloat(APFloat::Bogus,2));
834 static unsigned getHashValue(const KeyTy &Key) {
835 return Key.val.getHashValue();
837 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
840 static bool isPod() { return false; }
843 struct LLVMContextImpl {
844 sys::SmartRWMutex<true> ConstantsLock;
846 typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
847 DenseMapAPIntKeyInfo> IntMapTy;
848 IntMapTy IntConstants;
850 typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
851 DenseMapAPFloatKeyInfo> FPMapTy;
854 StringMap<MDString*> MDStringCache;
856 FoldingSet<MDNode> MDNodeSet;
858 ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
860 typedef ValueMap<std::vector<Constant*>, ArrayType,
861 ConstantArray, true /*largekey*/> ArrayConstantsTy;
862 ArrayConstantsTy ArrayConstants;
864 typedef ValueMap<std::vector<Constant*>, StructType,
865 ConstantStruct, true /*largekey*/> StructConstantsTy;
866 StructConstantsTy StructConstants;
868 typedef ValueMap<std::vector<Constant*>, VectorType,
869 ConstantVector> VectorConstantsTy;
870 VectorConstantsTy VectorConstants;
872 ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
874 ValueMap<char, Type, UndefValue> UndefValueConstants;
876 ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
878 LLVMContext &Context;
879 ConstantInt *TheTrueVal;
880 ConstantInt *TheFalseVal;
882 LLVMContextImpl(LLVMContext &C);
885 LLVMContextImpl(const LLVMContextImpl&);