1 //===-- ConstantsContext.h - Constants-related Context Interals -----------===//
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 defines various helper methods and classes used by
11 // LLVMContextImpl for creating and managing constants.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CONSTANTSCONTEXT_H
16 #define LLVM_CONSTANTSCONTEXT_H
18 #include "llvm/Instructions.h"
19 #include "llvm/Operator.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/System/Mutex.h"
24 #include "llvm/System/RWMutex.h"
28 template<class ValType
>
29 struct ConstantTraits
;
31 /// UnaryConstantExpr - This class is private to Constants.cpp, and is used
32 /// behind the scenes to implement unary constant exprs.
33 class UnaryConstantExpr
: public ConstantExpr
{
34 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
36 // allocate space for exactly one operand
37 void *operator new(size_t s
) {
38 return User::operator new(s
, 1);
40 UnaryConstantExpr(unsigned Opcode
, Constant
*C
, const Type
*Ty
)
41 : ConstantExpr(Ty
, Opcode
, &Op
<0>(), 1) {
44 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value
);
47 /// BinaryConstantExpr - This class is private to Constants.cpp, and is used
48 /// behind the scenes to implement binary constant exprs.
49 class BinaryConstantExpr
: public ConstantExpr
{
50 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
52 // allocate space for exactly two operands
53 void *operator new(size_t s
) {
54 return User::operator new(s
, 2);
56 BinaryConstantExpr(unsigned Opcode
, Constant
*C1
, Constant
*C2
,
58 : ConstantExpr(C1
->getType(), Opcode
, &Op
<0>(), 2) {
61 SubclassOptionalData
= Flags
;
63 /// Transparently provide more efficient getOperand methods.
64 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value
);
67 /// SelectConstantExpr - This class is private to Constants.cpp, and is used
68 /// behind the scenes to implement select constant exprs.
69 class SelectConstantExpr
: public ConstantExpr
{
70 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
72 // allocate space for exactly three operands
73 void *operator new(size_t s
) {
74 return User::operator new(s
, 3);
76 SelectConstantExpr(Constant
*C1
, Constant
*C2
, Constant
*C3
)
77 : ConstantExpr(C2
->getType(), Instruction::Select
, &Op
<0>(), 3) {
82 /// Transparently provide more efficient getOperand methods.
83 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value
);
86 /// ExtractElementConstantExpr - This class is private to
87 /// Constants.cpp, and is used behind the scenes to implement
88 /// extractelement constant exprs.
89 class ExtractElementConstantExpr
: public ConstantExpr
{
90 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
92 // allocate space for exactly two operands
93 void *operator new(size_t s
) {
94 return User::operator new(s
, 2);
96 ExtractElementConstantExpr(Constant
*C1
, Constant
*C2
)
97 : ConstantExpr(cast
<VectorType
>(C1
->getType())->getElementType(),
98 Instruction::ExtractElement
, &Op
<0>(), 2) {
102 /// Transparently provide more efficient getOperand methods.
103 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value
);
106 /// InsertElementConstantExpr - This class is private to
107 /// Constants.cpp, and is used behind the scenes to implement
108 /// insertelement constant exprs.
109 class InsertElementConstantExpr
: public ConstantExpr
{
110 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
112 // allocate space for exactly three operands
113 void *operator new(size_t s
) {
114 return User::operator new(s
, 3);
116 InsertElementConstantExpr(Constant
*C1
, Constant
*C2
, Constant
*C3
)
117 : ConstantExpr(C1
->getType(), Instruction::InsertElement
,
123 /// Transparently provide more efficient getOperand methods.
124 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value
);
127 /// ShuffleVectorConstantExpr - This class is private to
128 /// Constants.cpp, and is used behind the scenes to implement
129 /// shufflevector constant exprs.
130 class ShuffleVectorConstantExpr
: public ConstantExpr
{
131 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
133 // allocate space for exactly three operands
134 void *operator new(size_t s
) {
135 return User::operator new(s
, 3);
137 ShuffleVectorConstantExpr(Constant
*C1
, Constant
*C2
, Constant
*C3
)
138 : ConstantExpr(VectorType::get(
139 cast
<VectorType
>(C1
->getType())->getElementType(),
140 cast
<VectorType
>(C3
->getType())->getNumElements()),
141 Instruction::ShuffleVector
,
147 /// Transparently provide more efficient getOperand methods.
148 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value
);
151 /// ExtractValueConstantExpr - This class is private to
152 /// Constants.cpp, and is used behind the scenes to implement
153 /// extractvalue constant exprs.
154 class ExtractValueConstantExpr
: public ConstantExpr
{
155 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
157 // allocate space for exactly one operand
158 void *operator new(size_t s
) {
159 return User::operator new(s
, 1);
161 ExtractValueConstantExpr(Constant
*Agg
,
162 const SmallVector
<unsigned, 4> &IdxList
,
164 : ConstantExpr(DestTy
, Instruction::ExtractValue
, &Op
<0>(), 1),
169 /// Indices - These identify which value to extract.
170 const SmallVector
<unsigned, 4> Indices
;
172 /// Transparently provide more efficient getOperand methods.
173 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value
);
176 /// InsertValueConstantExpr - This class is private to
177 /// Constants.cpp, and is used behind the scenes to implement
178 /// insertvalue constant exprs.
179 class InsertValueConstantExpr
: public ConstantExpr
{
180 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
182 // allocate space for exactly one operand
183 void *operator new(size_t s
) {
184 return User::operator new(s
, 2);
186 InsertValueConstantExpr(Constant
*Agg
, Constant
*Val
,
187 const SmallVector
<unsigned, 4> &IdxList
,
189 : ConstantExpr(DestTy
, Instruction::InsertValue
, &Op
<0>(), 2),
195 /// Indices - These identify the position for the insertion.
196 const SmallVector
<unsigned, 4> Indices
;
198 /// Transparently provide more efficient getOperand methods.
199 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value
);
203 /// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
204 /// used behind the scenes to implement getelementpr constant exprs.
205 class GetElementPtrConstantExpr
: public ConstantExpr
{
206 GetElementPtrConstantExpr(Constant
*C
, const std::vector
<Constant
*> &IdxList
,
209 static GetElementPtrConstantExpr
*Create(Constant
*C
,
210 const std::vector
<Constant
*>&IdxList
,
213 GetElementPtrConstantExpr
*Result
=
214 new(IdxList
.size() + 1) GetElementPtrConstantExpr(C
, IdxList
, DestTy
);
215 Result
->SubclassOptionalData
= Flags
;
218 /// Transparently provide more efficient getOperand methods.
219 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value
);
222 // CompareConstantExpr - This class is private to Constants.cpp, and is used
223 // behind the scenes to implement ICmp and FCmp constant expressions. This is
224 // needed in order to store the predicate value for these instructions.
225 struct CompareConstantExpr
: public ConstantExpr
{
226 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
227 // allocate space for exactly two operands
228 void *operator new(size_t s
) {
229 return User::operator new(s
, 2);
231 unsigned short predicate
;
232 CompareConstantExpr(const Type
*ty
, Instruction::OtherOps opc
,
233 unsigned short pred
, Constant
* LHS
, Constant
* RHS
)
234 : ConstantExpr(ty
, opc
, &Op
<0>(), 2), predicate(pred
) {
238 /// Transparently provide more efficient getOperand methods.
239 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value
);
243 struct OperandTraits
<UnaryConstantExpr
> : public FixedNumOperandTraits
<1> {
245 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr
, Value
)
248 struct OperandTraits
<BinaryConstantExpr
> : public FixedNumOperandTraits
<2> {
250 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr
, Value
)
253 struct OperandTraits
<SelectConstantExpr
> : public FixedNumOperandTraits
<3> {
255 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr
, Value
)
258 struct OperandTraits
<ExtractElementConstantExpr
> : public FixedNumOperandTraits
<2> {
260 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr
, Value
)
263 struct OperandTraits
<InsertElementConstantExpr
> : public FixedNumOperandTraits
<3> {
265 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr
, Value
)
268 struct OperandTraits
<ShuffleVectorConstantExpr
> : public FixedNumOperandTraits
<3> {
270 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr
, Value
)
273 struct OperandTraits
<ExtractValueConstantExpr
> : public FixedNumOperandTraits
<1> {
275 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr
, Value
)
278 struct OperandTraits
<InsertValueConstantExpr
> : public FixedNumOperandTraits
<2> {
280 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr
, Value
)
283 struct OperandTraits
<GetElementPtrConstantExpr
> : public VariadicOperandTraits
<1> {
286 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr
, Value
)
290 struct OperandTraits
<CompareConstantExpr
> : public FixedNumOperandTraits
<2> {
292 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr
, Value
)
294 struct ExprMapKeyType
{
295 typedef SmallVector
<unsigned, 4> IndexList
;
297 ExprMapKeyType(unsigned opc
,
298 const std::vector
<Constant
*> &ops
,
299 unsigned short flags
= 0,
300 unsigned short optionalflags
= 0,
301 const IndexList
&inds
= IndexList())
302 : opcode(opc
), subclassoptionaldata(optionalflags
), subclassdata(flags
),
303 operands(ops
), indices(inds
) {}
305 uint8_t subclassoptionaldata
;
306 uint16_t subclassdata
;
307 std::vector
<Constant
*> operands
;
309 bool operator==(const ExprMapKeyType
& that
) const {
310 return this->opcode
== that
.opcode
&&
311 this->subclassdata
== that
.subclassdata
&&
312 this->subclassoptionaldata
== that
.subclassoptionaldata
&&
313 this->operands
== that
.operands
&&
314 this->indices
== that
.indices
;
316 bool operator<(const ExprMapKeyType
& that
) const {
317 if (this->opcode
!= that
.opcode
) return this->opcode
< that
.opcode
;
318 if (this->operands
!= that
.operands
) return this->operands
< that
.operands
;
319 if (this->subclassdata
!= that
.subclassdata
)
320 return this->subclassdata
< that
.subclassdata
;
321 if (this->subclassoptionaldata
!= that
.subclassoptionaldata
)
322 return this->subclassoptionaldata
< that
.subclassoptionaldata
;
323 if (this->indices
!= that
.indices
) return this->indices
< that
.indices
;
327 bool operator!=(const ExprMapKeyType
& that
) const {
328 return !(*this == that
);
332 // The number of operands for each ConstantCreator::create method is
333 // determined by the ConstantTraits template.
334 // ConstantCreator - A class that is used to create constants by
335 // ValueMap*. This class should be partially specialized if there is
336 // something strange that needs to be done to interface to the ctor for the
339 template<typename T
, typename Alloc
>
340 struct ConstantTraits
< std::vector
<T
, Alloc
> > {
341 static unsigned uses(const std::vector
<T
, Alloc
>& v
) {
346 template<class ConstantClass
, class TypeClass
, class ValType
>
347 struct ConstantCreator
{
348 static ConstantClass
*create(const TypeClass
*Ty
, const ValType
&V
) {
349 return new(ConstantTraits
<ValType
>::uses(V
)) ConstantClass(Ty
, V
);
353 template<class ConstantClass
>
354 struct ConstantKeyData
{
355 typedef void ValType
;
356 static ValType
getValType(ConstantClass
*C
) {
357 llvm_unreachable("Unknown Constant type!");
362 struct ConstantCreator
<ConstantExpr
, Type
, ExprMapKeyType
> {
363 static ConstantExpr
*create(const Type
*Ty
, const ExprMapKeyType
&V
,
364 unsigned short pred
= 0) {
365 if (Instruction::isCast(V
.opcode
))
366 return new UnaryConstantExpr(V
.opcode
, V
.operands
[0], Ty
);
367 if ((V
.opcode
>= Instruction::BinaryOpsBegin
&&
368 V
.opcode
< Instruction::BinaryOpsEnd
))
369 return new BinaryConstantExpr(V
.opcode
, V
.operands
[0], V
.operands
[1],
370 V
.subclassoptionaldata
);
371 if (V
.opcode
== Instruction::Select
)
372 return new SelectConstantExpr(V
.operands
[0], V
.operands
[1],
374 if (V
.opcode
== Instruction::ExtractElement
)
375 return new ExtractElementConstantExpr(V
.operands
[0], V
.operands
[1]);
376 if (V
.opcode
== Instruction::InsertElement
)
377 return new InsertElementConstantExpr(V
.operands
[0], V
.operands
[1],
379 if (V
.opcode
== Instruction::ShuffleVector
)
380 return new ShuffleVectorConstantExpr(V
.operands
[0], V
.operands
[1],
382 if (V
.opcode
== Instruction::InsertValue
)
383 return new InsertValueConstantExpr(V
.operands
[0], V
.operands
[1],
385 if (V
.opcode
== Instruction::ExtractValue
)
386 return new ExtractValueConstantExpr(V
.operands
[0], V
.indices
, Ty
);
387 if (V
.opcode
== Instruction::GetElementPtr
) {
388 std::vector
<Constant
*> IdxList(V
.operands
.begin()+1, V
.operands
.end());
389 return GetElementPtrConstantExpr::Create(V
.operands
[0], IdxList
, Ty
,
390 V
.subclassoptionaldata
);
393 // The compare instructions are weird. We have to encode the predicate
394 // value and it is combined with the instruction opcode by multiplying
395 // the opcode by one hundred. We must decode this to get the predicate.
396 if (V
.opcode
== Instruction::ICmp
)
397 return new CompareConstantExpr(Ty
, Instruction::ICmp
, V
.subclassdata
,
398 V
.operands
[0], V
.operands
[1]);
399 if (V
.opcode
== Instruction::FCmp
)
400 return new CompareConstantExpr(Ty
, Instruction::FCmp
, V
.subclassdata
,
401 V
.operands
[0], V
.operands
[1]);
402 llvm_unreachable("Invalid ConstantExpr!");
408 struct ConstantKeyData
<ConstantExpr
> {
409 typedef ExprMapKeyType ValType
;
410 static ValType
getValType(ConstantExpr
*CE
) {
411 std::vector
<Constant
*> Operands
;
412 Operands
.reserve(CE
->getNumOperands());
413 for (unsigned i
= 0, e
= CE
->getNumOperands(); i
!= e
; ++i
)
414 Operands
.push_back(cast
<Constant
>(CE
->getOperand(i
)));
415 return ExprMapKeyType(CE
->getOpcode(), Operands
,
416 CE
->isCompare() ? CE
->getPredicate() : 0,
417 CE
->getRawSubclassOptionalData(),
419 CE
->getIndices() : SmallVector
<unsigned, 4>());
423 // ConstantAggregateZero does not take extra "value" argument...
424 template<class ValType
>
425 struct ConstantCreator
<ConstantAggregateZero
, Type
, ValType
> {
426 static ConstantAggregateZero
*create(const Type
*Ty
, const ValType
&V
){
427 return new ConstantAggregateZero(Ty
);
432 struct ConstantKeyData
<ConstantVector
> {
433 typedef std::vector
<Constant
*> ValType
;
434 static ValType
getValType(ConstantVector
*CP
) {
435 std::vector
<Constant
*> Elements
;
436 Elements
.reserve(CP
->getNumOperands());
437 for (unsigned i
= 0, e
= CP
->getNumOperands(); i
!= e
; ++i
)
438 Elements
.push_back(CP
->getOperand(i
));
444 struct ConstantKeyData
<ConstantAggregateZero
> {
445 typedef char ValType
;
446 static ValType
getValType(ConstantAggregateZero
*C
) {
452 struct ConstantKeyData
<ConstantArray
> {
453 typedef std::vector
<Constant
*> ValType
;
454 static ValType
getValType(ConstantArray
*CA
) {
455 std::vector
<Constant
*> Elements
;
456 Elements
.reserve(CA
->getNumOperands());
457 for (unsigned i
= 0, e
= CA
->getNumOperands(); i
!= e
; ++i
)
458 Elements
.push_back(cast
<Constant
>(CA
->getOperand(i
)));
464 struct ConstantKeyData
<ConstantStruct
> {
465 typedef std::vector
<Constant
*> ValType
;
466 static ValType
getValType(ConstantStruct
*CS
) {
467 std::vector
<Constant
*> Elements
;
468 Elements
.reserve(CS
->getNumOperands());
469 for (unsigned i
= 0, e
= CS
->getNumOperands(); i
!= e
; ++i
)
470 Elements
.push_back(cast
<Constant
>(CS
->getOperand(i
)));
475 // ConstantPointerNull does not take extra "value" argument...
476 template<class ValType
>
477 struct ConstantCreator
<ConstantPointerNull
, PointerType
, ValType
> {
478 static ConstantPointerNull
*create(const PointerType
*Ty
, const ValType
&V
){
479 return new ConstantPointerNull(Ty
);
484 struct ConstantKeyData
<ConstantPointerNull
> {
485 typedef char ValType
;
486 static ValType
getValType(ConstantPointerNull
*C
) {
491 // UndefValue does not take extra "value" argument...
492 template<class ValType
>
493 struct ConstantCreator
<UndefValue
, Type
, ValType
> {
494 static UndefValue
*create(const Type
*Ty
, const ValType
&V
) {
495 return new UndefValue(Ty
);
500 struct ConstantKeyData
<UndefValue
> {
501 typedef char ValType
;
502 static ValType
getValType(UndefValue
*C
) {
507 template<class ValType
, class TypeClass
, class ConstantClass
,
508 bool HasLargeKey
= false /*true for arrays and structs*/ >
509 class ValueMap
: public AbstractTypeUser
{
511 typedef std::pair
<const TypeClass
*, ValType
> MapKey
;
512 typedef std::map
<MapKey
, ConstantClass
*> MapTy
;
513 typedef std::map
<ConstantClass
*, typename
MapTy::iterator
> InverseMapTy
;
514 typedef std::map
<const DerivedType
*, typename
MapTy::iterator
>
517 /// Map - This is the main map from the element descriptor to the Constants.
518 /// This is the primary way we avoid creating two of the same shape
522 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
523 /// from the constants to their element in Map. This is important for
524 /// removal of constants from the array, which would otherwise have to scan
525 /// through the map with very large keys.
526 InverseMapTy InverseMap
;
528 /// AbstractTypeMap - Map for abstract type constants.
530 AbstractTypeMapTy AbstractTypeMap
;
532 /// ValueMapLock - Mutex for this map.
533 sys::SmartMutex
<true> ValueMapLock
;
536 // NOTE: This function is not locked. It is the caller's responsibility
537 // to enforce proper synchronization.
538 typename
MapTy::iterator
map_begin() { return Map
.begin(); }
539 typename
MapTy::iterator
map_end() { return Map
.end(); }
541 void freeConstants() {
542 for (typename
MapTy::iterator I
=Map
.begin(), E
=Map
.end();
544 if (I
->second
->use_empty())
549 /// InsertOrGetItem - Return an iterator for the specified element.
550 /// If the element exists in the map, the returned iterator points to the
551 /// entry and Exists=true. If not, the iterator points to the newly
552 /// inserted entry and returns Exists=false. Newly inserted entries have
553 /// I->second == 0, and should be filled in.
554 /// NOTE: This function is not locked. It is the caller's responsibility
555 // to enforce proper synchronization.
556 typename
MapTy::iterator
InsertOrGetItem(std::pair
<MapKey
, ConstantClass
*>
559 std::pair
<typename
MapTy::iterator
, bool> IP
= Map
.insert(InsertVal
);
565 typename
MapTy::iterator
FindExistingElement(ConstantClass
*CP
) {
567 typename
InverseMapTy::iterator IMI
= InverseMap
.find(CP
);
568 assert(IMI
!= InverseMap
.end() && IMI
->second
!= Map
.end() &&
569 IMI
->second
->second
== CP
&&
570 "InverseMap corrupt!");
574 typename
MapTy::iterator I
=
575 Map
.find(MapKey(static_cast<const TypeClass
*>(CP
->getRawType()),
576 ConstantKeyData
<ConstantClass
>::getValType(CP
)));
577 if (I
== Map
.end() || I
->second
!= CP
) {
578 // FIXME: This should not use a linear scan. If this gets to be a
579 // performance problem, someone should look at this.
580 for (I
= Map
.begin(); I
!= Map
.end() && I
->second
!= CP
; ++I
)
586 void AddAbstractTypeUser(const Type
*Ty
, typename
MapTy::iterator I
) {
587 // If the type of the constant is abstract, make sure that an entry
588 // exists for it in the AbstractTypeMap.
589 if (Ty
->isAbstract()) {
590 const DerivedType
*DTy
= static_cast<const DerivedType
*>(Ty
);
591 typename
AbstractTypeMapTy::iterator TI
= AbstractTypeMap
.find(DTy
);
593 if (TI
== AbstractTypeMap
.end()) {
594 // Add ourselves to the ATU list of the type.
595 cast
<DerivedType
>(DTy
)->addAbstractTypeUser(this);
597 AbstractTypeMap
.insert(TI
, std::make_pair(DTy
, I
));
602 ConstantClass
* Create(const TypeClass
*Ty
, const ValType
&V
,
603 typename
MapTy::iterator I
) {
604 ConstantClass
* Result
=
605 ConstantCreator
<ConstantClass
,TypeClass
,ValType
>::create(Ty
, V
);
607 assert(Result
->getType() == Ty
&& "Type specified is not correct!");
608 I
= Map
.insert(I
, std::make_pair(MapKey(Ty
, V
), Result
));
610 if (HasLargeKey
) // Remember the reverse mapping if needed.
611 InverseMap
.insert(std::make_pair(Result
, I
));
613 AddAbstractTypeUser(Ty
, I
);
619 /// getOrCreate - Return the specified constant from the map, creating it if
621 ConstantClass
*getOrCreate(const TypeClass
*Ty
, const ValType
&V
) {
622 sys::SmartScopedLock
<true> Lock(ValueMapLock
);
623 MapKey
Lookup(Ty
, V
);
624 ConstantClass
* Result
= 0;
626 typename
MapTy::iterator I
= Map
.find(Lookup
);
632 // If no preexisting value, create one now...
633 Result
= Create(Ty
, V
, I
);
639 void UpdateAbstractTypeMap(const DerivedType
*Ty
,
640 typename
MapTy::iterator I
) {
641 assert(AbstractTypeMap
.count(Ty
) &&
642 "Abstract type not in AbstractTypeMap?");
643 typename
MapTy::iterator
&ATMEntryIt
= AbstractTypeMap
[Ty
];
644 if (ATMEntryIt
== I
) {
645 // Yes, we are removing the representative entry for this type.
646 // See if there are any other entries of the same type.
647 typename
MapTy::iterator TmpIt
= ATMEntryIt
;
649 // First check the entry before this one...
650 if (TmpIt
!= Map
.begin()) {
652 if (TmpIt
->first
.first
!= Ty
) // Not the same type, move back...
656 // If we didn't find the same type, try to move forward...
657 if (TmpIt
== ATMEntryIt
) {
659 if (TmpIt
== Map
.end() || TmpIt
->first
.first
!= Ty
)
660 --TmpIt
; // No entry afterwards with the same type
663 // If there is another entry in the map of the same abstract type,
664 // update the AbstractTypeMap entry now.
665 if (TmpIt
!= ATMEntryIt
) {
668 // Otherwise, we are removing the last instance of this type
669 // from the table. Remove from the ATM, and from user list.
670 cast
<DerivedType
>(Ty
)->removeAbstractTypeUser(this);
671 AbstractTypeMap
.erase(Ty
);
676 void remove(ConstantClass
*CP
) {
677 sys::SmartScopedLock
<true> Lock(ValueMapLock
);
678 typename
MapTy::iterator I
= FindExistingElement(CP
);
679 assert(I
!= Map
.end() && "Constant not found in constant table!");
680 assert(I
->second
== CP
&& "Didn't find correct element?");
682 if (HasLargeKey
) // Remember the reverse mapping if needed.
683 InverseMap
.erase(CP
);
685 // Now that we found the entry, make sure this isn't the entry that
686 // the AbstractTypeMap points to.
687 const TypeClass
*Ty
= I
->first
.first
;
688 if (Ty
->isAbstract())
689 UpdateAbstractTypeMap(static_cast<const DerivedType
*>(Ty
), I
);
694 /// MoveConstantToNewSlot - If we are about to change C to be the element
695 /// specified by I, update our internal data structures to reflect this
697 /// NOTE: This function is not locked. It is the responsibility of the
698 /// caller to enforce proper synchronization if using this method.
699 void MoveConstantToNewSlot(ConstantClass
*C
, typename
MapTy::iterator I
) {
700 // First, remove the old location of the specified constant in the map.
701 typename
MapTy::iterator OldI
= FindExistingElement(C
);
702 assert(OldI
!= Map
.end() && "Constant not found in constant table!");
703 assert(OldI
->second
== C
&& "Didn't find correct element?");
705 // If this constant is the representative element for its abstract type,
706 // update the AbstractTypeMap so that the representative element is I.
707 if (C
->getType()->isAbstract()) {
708 typename
AbstractTypeMapTy::iterator ATI
=
709 AbstractTypeMap
.find(C
->getType());
710 assert(ATI
!= AbstractTypeMap
.end() &&
711 "Abstract type not in AbstractTypeMap?");
712 if (ATI
->second
== OldI
)
716 // Remove the old entry from the map.
719 // Update the inverse map so that we know that this constant is now
720 // located at descriptor I.
722 assert(I
->second
== C
&& "Bad inversemap entry!");
727 void refineAbstractType(const DerivedType
*OldTy
, const Type
*NewTy
) {
728 sys::SmartScopedLock
<true> Lock(ValueMapLock
);
729 typename
AbstractTypeMapTy::iterator I
= AbstractTypeMap
.find(OldTy
);
731 assert(I
!= AbstractTypeMap
.end() &&
732 "Abstract type not in AbstractTypeMap?");
734 // Convert a constant at a time until the last one is gone. The last one
735 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
736 // eliminated eventually.
738 ConstantClass
*C
= I
->second
->second
;
739 MapKey
Key(cast
<TypeClass
>(NewTy
),
740 ConstantKeyData
<ConstantClass
>::getValType(C
));
742 std::pair
<typename
MapTy::iterator
, bool> IP
=
743 Map
.insert(std::make_pair(Key
, C
));
745 // The map didn't previously have an appropriate constant in the
748 // Remove the old entry.
749 typename
MapTy::iterator OldI
=
750 Map
.find(MapKey(cast
<TypeClass
>(OldTy
), IP
.first
->first
.second
));
751 assert(OldI
!= Map
.end() && "Constant not in map!");
752 UpdateAbstractTypeMap(OldTy
, OldI
);
755 // Set the constant's type. This is done in place!
758 // Update the inverse map so that we know that this constant is now
759 // located at descriptor I.
761 InverseMap
[C
] = IP
.first
;
763 AddAbstractTypeUser(NewTy
, IP
.first
);
765 // The map already had an appropriate constant in the new type, so
766 // there's no longer a need for the old constant.
767 C
->uncheckedReplaceAllUsesWith(IP
.first
->second
);
768 C
->destroyConstant(); // This constant is now dead, destroy it.
770 I
= AbstractTypeMap
.find(OldTy
);
771 } while (I
!= AbstractTypeMap
.end());
774 // If the type became concrete without being refined to any other existing
775 // type, we just remove ourselves from the ATU list.
776 void typeBecameConcrete(const DerivedType
*AbsTy
) {
777 AbsTy
->removeAbstractTypeUser(this);
781 DEBUG(errs() << "Constant.cpp: ValueMap\n");