SCCP doesn't actually preserve the CFG. It will delete and insert terminator
[llvm.git] / lib / VMCore / ConstantsContext.h
blobffc673fac0daed82b9e6a1f0fda8a0e7c2552b75
1 //===-- ConstantsContext.h - Constants-related Context Interals -----------===//
2 //
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 //===----------------------------------------------------------------------===//
9 //
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/InlineAsm.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/Operator.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include <map>
26 namespace llvm {
27 template<class ValType>
28 struct ConstantTraits;
30 /// UnaryConstantExpr - This class is private to Constants.cpp, and is used
31 /// behind the scenes to implement unary constant exprs.
32 class UnaryConstantExpr : public ConstantExpr {
33 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
34 public:
35 // allocate space for exactly one operand
36 void *operator new(size_t s) {
37 return User::operator new(s, 1);
39 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
40 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
41 Op<0>() = C;
43 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
46 /// BinaryConstantExpr - This class is private to Constants.cpp, and is used
47 /// behind the scenes to implement binary constant exprs.
48 class BinaryConstantExpr : public ConstantExpr {
49 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
50 public:
51 // allocate space for exactly two operands
52 void *operator new(size_t s) {
53 return User::operator new(s, 2);
55 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2,
56 unsigned Flags)
57 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
58 Op<0>() = C1;
59 Op<1>() = C2;
60 SubclassOptionalData = Flags;
62 /// Transparently provide more efficient getOperand methods.
63 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
66 /// SelectConstantExpr - This class is private to Constants.cpp, and is used
67 /// behind the scenes to implement select constant exprs.
68 class SelectConstantExpr : public ConstantExpr {
69 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
70 public:
71 // allocate space for exactly three operands
72 void *operator new(size_t s) {
73 return User::operator new(s, 3);
75 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
76 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
77 Op<0>() = C1;
78 Op<1>() = C2;
79 Op<2>() = C3;
81 /// Transparently provide more efficient getOperand methods.
82 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
85 /// ExtractElementConstantExpr - This class is private to
86 /// Constants.cpp, and is used behind the scenes to implement
87 /// extractelement constant exprs.
88 class ExtractElementConstantExpr : public ConstantExpr {
89 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
90 public:
91 // allocate space for exactly two operands
92 void *operator new(size_t s) {
93 return User::operator new(s, 2);
95 ExtractElementConstantExpr(Constant *C1, Constant *C2)
96 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
97 Instruction::ExtractElement, &Op<0>(), 2) {
98 Op<0>() = C1;
99 Op<1>() = C2;
101 /// Transparently provide more efficient getOperand methods.
102 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
105 /// InsertElementConstantExpr - This class is private to
106 /// Constants.cpp, and is used behind the scenes to implement
107 /// insertelement constant exprs.
108 class InsertElementConstantExpr : public ConstantExpr {
109 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
110 public:
111 // allocate space for exactly three operands
112 void *operator new(size_t s) {
113 return User::operator new(s, 3);
115 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
116 : ConstantExpr(C1->getType(), Instruction::InsertElement,
117 &Op<0>(), 3) {
118 Op<0>() = C1;
119 Op<1>() = C2;
120 Op<2>() = C3;
122 /// Transparently provide more efficient getOperand methods.
123 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
126 /// ShuffleVectorConstantExpr - This class is private to
127 /// Constants.cpp, and is used behind the scenes to implement
128 /// shufflevector constant exprs.
129 class ShuffleVectorConstantExpr : public ConstantExpr {
130 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
131 public:
132 // allocate space for exactly three operands
133 void *operator new(size_t s) {
134 return User::operator new(s, 3);
136 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
137 : ConstantExpr(VectorType::get(
138 cast<VectorType>(C1->getType())->getElementType(),
139 cast<VectorType>(C3->getType())->getNumElements()),
140 Instruction::ShuffleVector,
141 &Op<0>(), 3) {
142 Op<0>() = C1;
143 Op<1>() = C2;
144 Op<2>() = C3;
146 /// Transparently provide more efficient getOperand methods.
147 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
150 /// ExtractValueConstantExpr - This class is private to
151 /// Constants.cpp, and is used behind the scenes to implement
152 /// extractvalue constant exprs.
153 class ExtractValueConstantExpr : public ConstantExpr {
154 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
155 public:
156 // allocate space for exactly one operand
157 void *operator new(size_t s) {
158 return User::operator new(s, 1);
160 ExtractValueConstantExpr(Constant *Agg,
161 const SmallVector<unsigned, 4> &IdxList,
162 const Type *DestTy)
163 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
164 Indices(IdxList) {
165 Op<0>() = Agg;
168 /// Indices - These identify which value to extract.
169 const SmallVector<unsigned, 4> Indices;
171 /// Transparently provide more efficient getOperand methods.
172 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
175 /// InsertValueConstantExpr - This class is private to
176 /// Constants.cpp, and is used behind the scenes to implement
177 /// insertvalue constant exprs.
178 class InsertValueConstantExpr : public ConstantExpr {
179 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
180 public:
181 // allocate space for exactly one operand
182 void *operator new(size_t s) {
183 return User::operator new(s, 2);
185 InsertValueConstantExpr(Constant *Agg, Constant *Val,
186 const SmallVector<unsigned, 4> &IdxList,
187 const Type *DestTy)
188 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
189 Indices(IdxList) {
190 Op<0>() = Agg;
191 Op<1>() = Val;
194 /// Indices - These identify the position for the insertion.
195 const SmallVector<unsigned, 4> Indices;
197 /// Transparently provide more efficient getOperand methods.
198 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
202 /// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
203 /// used behind the scenes to implement getelementpr constant exprs.
204 class GetElementPtrConstantExpr : public ConstantExpr {
205 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
206 const Type *DestTy);
207 public:
208 static GetElementPtrConstantExpr *Create(Constant *C,
209 const std::vector<Constant*>&IdxList,
210 const Type *DestTy,
211 unsigned Flags) {
212 GetElementPtrConstantExpr *Result =
213 new(IdxList.size() + 1) GetElementPtrConstantExpr(C, IdxList, DestTy);
214 Result->SubclassOptionalData = Flags;
215 return Result;
217 /// Transparently provide more efficient getOperand methods.
218 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
221 // CompareConstantExpr - This class is private to Constants.cpp, and is used
222 // behind the scenes to implement ICmp and FCmp constant expressions. This is
223 // needed in order to store the predicate value for these instructions.
224 struct CompareConstantExpr : public ConstantExpr {
225 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
226 // allocate space for exactly two operands
227 void *operator new(size_t s) {
228 return User::operator new(s, 2);
230 unsigned short predicate;
231 CompareConstantExpr(const Type *ty, Instruction::OtherOps opc,
232 unsigned short pred, Constant* LHS, Constant* RHS)
233 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
234 Op<0>() = LHS;
235 Op<1>() = RHS;
237 /// Transparently provide more efficient getOperand methods.
238 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
241 template <>
242 struct OperandTraits<UnaryConstantExpr> :
243 public FixedNumOperandTraits<UnaryConstantExpr, 1> {
245 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
247 template <>
248 struct OperandTraits<BinaryConstantExpr> :
249 public FixedNumOperandTraits<BinaryConstantExpr, 2> {
251 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
253 template <>
254 struct OperandTraits<SelectConstantExpr> :
255 public FixedNumOperandTraits<SelectConstantExpr, 3> {
257 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
259 template <>
260 struct OperandTraits<ExtractElementConstantExpr> :
261 public FixedNumOperandTraits<ExtractElementConstantExpr, 2> {
263 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
265 template <>
266 struct OperandTraits<InsertElementConstantExpr> :
267 public FixedNumOperandTraits<InsertElementConstantExpr, 3> {
269 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
271 template <>
272 struct OperandTraits<ShuffleVectorConstantExpr> :
273 public FixedNumOperandTraits<ShuffleVectorConstantExpr, 3> {
275 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
277 template <>
278 struct OperandTraits<ExtractValueConstantExpr> :
279 public FixedNumOperandTraits<ExtractValueConstantExpr, 1> {
281 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
283 template <>
284 struct OperandTraits<InsertValueConstantExpr> :
285 public FixedNumOperandTraits<InsertValueConstantExpr, 2> {
287 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
289 template <>
290 struct OperandTraits<GetElementPtrConstantExpr> :
291 public VariadicOperandTraits<GetElementPtrConstantExpr, 1> {
294 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
297 template <>
298 struct OperandTraits<CompareConstantExpr> :
299 public FixedNumOperandTraits<CompareConstantExpr, 2> {
301 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
303 struct ExprMapKeyType {
304 typedef SmallVector<unsigned, 4> IndexList;
306 ExprMapKeyType(unsigned opc,
307 const std::vector<Constant*> &ops,
308 unsigned short flags = 0,
309 unsigned short optionalflags = 0,
310 const IndexList &inds = IndexList())
311 : opcode(opc), subclassoptionaldata(optionalflags), subclassdata(flags),
312 operands(ops), indices(inds) {}
313 uint8_t opcode;
314 uint8_t subclassoptionaldata;
315 uint16_t subclassdata;
316 std::vector<Constant*> operands;
317 IndexList indices;
318 bool operator==(const ExprMapKeyType& that) const {
319 return this->opcode == that.opcode &&
320 this->subclassdata == that.subclassdata &&
321 this->subclassoptionaldata == that.subclassoptionaldata &&
322 this->operands == that.operands &&
323 this->indices == that.indices;
325 bool operator<(const ExprMapKeyType & that) const {
326 if (this->opcode != that.opcode) return this->opcode < that.opcode;
327 if (this->operands != that.operands) return this->operands < that.operands;
328 if (this->subclassdata != that.subclassdata)
329 return this->subclassdata < that.subclassdata;
330 if (this->subclassoptionaldata != that.subclassoptionaldata)
331 return this->subclassoptionaldata < that.subclassoptionaldata;
332 if (this->indices != that.indices) return this->indices < that.indices;
333 return false;
336 bool operator!=(const ExprMapKeyType& that) const {
337 return !(*this == that);
341 struct InlineAsmKeyType {
342 InlineAsmKeyType(StringRef AsmString,
343 StringRef Constraints, bool hasSideEffects,
344 bool isAlignStack)
345 : asm_string(AsmString), constraints(Constraints),
346 has_side_effects(hasSideEffects), is_align_stack(isAlignStack) {}
347 std::string asm_string;
348 std::string constraints;
349 bool has_side_effects;
350 bool is_align_stack;
351 bool operator==(const InlineAsmKeyType& that) const {
352 return this->asm_string == that.asm_string &&
353 this->constraints == that.constraints &&
354 this->has_side_effects == that.has_side_effects &&
355 this->is_align_stack == that.is_align_stack;
357 bool operator<(const InlineAsmKeyType& that) const {
358 if (this->asm_string != that.asm_string)
359 return this->asm_string < that.asm_string;
360 if (this->constraints != that.constraints)
361 return this->constraints < that.constraints;
362 if (this->has_side_effects != that.has_side_effects)
363 return this->has_side_effects < that.has_side_effects;
364 if (this->is_align_stack != that.is_align_stack)
365 return this->is_align_stack < that.is_align_stack;
366 return false;
369 bool operator!=(const InlineAsmKeyType& that) const {
370 return !(*this == that);
374 // The number of operands for each ConstantCreator::create method is
375 // determined by the ConstantTraits template.
376 // ConstantCreator - A class that is used to create constants by
377 // ConstantUniqueMap*. This class should be partially specialized if there is
378 // something strange that needs to be done to interface to the ctor for the
379 // constant.
381 template<typename T, typename Alloc>
382 struct ConstantTraits< std::vector<T, Alloc> > {
383 static unsigned uses(const std::vector<T, Alloc>& v) {
384 return v.size();
388 template<>
389 struct ConstantTraits<Constant *> {
390 static unsigned uses(Constant * const & v) {
391 return 1;
395 template<class ConstantClass, class TypeClass, class ValType>
396 struct ConstantCreator {
397 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
398 return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V);
402 template<class ConstantClass>
403 struct ConstantKeyData {
404 typedef void ValType;
405 static ValType getValType(ConstantClass *C) {
406 llvm_unreachable("Unknown Constant type!");
410 template<>
411 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
412 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
413 unsigned short pred = 0) {
414 if (Instruction::isCast(V.opcode))
415 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
416 if ((V.opcode >= Instruction::BinaryOpsBegin &&
417 V.opcode < Instruction::BinaryOpsEnd))
418 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1],
419 V.subclassoptionaldata);
420 if (V.opcode == Instruction::Select)
421 return new SelectConstantExpr(V.operands[0], V.operands[1],
422 V.operands[2]);
423 if (V.opcode == Instruction::ExtractElement)
424 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
425 if (V.opcode == Instruction::InsertElement)
426 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
427 V.operands[2]);
428 if (V.opcode == Instruction::ShuffleVector)
429 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
430 V.operands[2]);
431 if (V.opcode == Instruction::InsertValue)
432 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
433 V.indices, Ty);
434 if (V.opcode == Instruction::ExtractValue)
435 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
436 if (V.opcode == Instruction::GetElementPtr) {
437 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
438 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty,
439 V.subclassoptionaldata);
442 // The compare instructions are weird. We have to encode the predicate
443 // value and it is combined with the instruction opcode by multiplying
444 // the opcode by one hundred. We must decode this to get the predicate.
445 if (V.opcode == Instruction::ICmp)
446 return new CompareConstantExpr(Ty, Instruction::ICmp, V.subclassdata,
447 V.operands[0], V.operands[1]);
448 if (V.opcode == Instruction::FCmp)
449 return new CompareConstantExpr(Ty, Instruction::FCmp, V.subclassdata,
450 V.operands[0], V.operands[1]);
451 llvm_unreachable("Invalid ConstantExpr!");
452 return 0;
456 template<>
457 struct ConstantKeyData<ConstantExpr> {
458 typedef ExprMapKeyType ValType;
459 static ValType getValType(ConstantExpr *CE) {
460 std::vector<Constant*> Operands;
461 Operands.reserve(CE->getNumOperands());
462 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
463 Operands.push_back(cast<Constant>(CE->getOperand(i)));
464 return ExprMapKeyType(CE->getOpcode(), Operands,
465 CE->isCompare() ? CE->getPredicate() : 0,
466 CE->getRawSubclassOptionalData(),
467 CE->hasIndices() ?
468 CE->getIndices() : SmallVector<unsigned, 4>());
472 // ConstantAggregateZero does not take extra "value" argument...
473 template<class ValType>
474 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
475 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
476 return new ConstantAggregateZero(Ty);
480 template<>
481 struct ConstantKeyData<ConstantVector> {
482 typedef std::vector<Constant*> ValType;
483 static ValType getValType(ConstantVector *CP) {
484 std::vector<Constant*> Elements;
485 Elements.reserve(CP->getNumOperands());
486 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
487 Elements.push_back(CP->getOperand(i));
488 return Elements;
492 template<>
493 struct ConstantKeyData<ConstantAggregateZero> {
494 typedef char ValType;
495 static ValType getValType(ConstantAggregateZero *C) {
496 return 0;
500 template<>
501 struct ConstantKeyData<ConstantArray> {
502 typedef std::vector<Constant*> ValType;
503 static ValType getValType(ConstantArray *CA) {
504 std::vector<Constant*> Elements;
505 Elements.reserve(CA->getNumOperands());
506 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
507 Elements.push_back(cast<Constant>(CA->getOperand(i)));
508 return Elements;
512 template<>
513 struct ConstantKeyData<ConstantStruct> {
514 typedef std::vector<Constant*> ValType;
515 static ValType getValType(ConstantStruct *CS) {
516 std::vector<Constant*> Elements;
517 Elements.reserve(CS->getNumOperands());
518 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
519 Elements.push_back(cast<Constant>(CS->getOperand(i)));
520 return Elements;
524 // ConstantPointerNull does not take extra "value" argument...
525 template<class ValType>
526 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
527 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
528 return new ConstantPointerNull(Ty);
532 template<>
533 struct ConstantKeyData<ConstantPointerNull> {
534 typedef char ValType;
535 static ValType getValType(ConstantPointerNull *C) {
536 return 0;
540 // UndefValue does not take extra "value" argument...
541 template<class ValType>
542 struct ConstantCreator<UndefValue, Type, ValType> {
543 static UndefValue *create(const Type *Ty, const ValType &V) {
544 return new UndefValue(Ty);
548 template<>
549 struct ConstantKeyData<UndefValue> {
550 typedef char ValType;
551 static ValType getValType(UndefValue *C) {
552 return 0;
556 template<>
557 struct ConstantCreator<InlineAsm, PointerType, InlineAsmKeyType> {
558 static InlineAsm *create(const PointerType *Ty, const InlineAsmKeyType &Key) {
559 return new InlineAsm(Ty, Key.asm_string, Key.constraints,
560 Key.has_side_effects, Key.is_align_stack);
564 template<>
565 struct ConstantKeyData<InlineAsm> {
566 typedef InlineAsmKeyType ValType;
567 static ValType getValType(InlineAsm *Asm) {
568 return InlineAsmKeyType(Asm->getAsmString(), Asm->getConstraintString(),
569 Asm->hasSideEffects(), Asm->isAlignStack());
573 template<class ValType, class TypeClass, class ConstantClass,
574 bool HasLargeKey = false /*true for arrays and structs*/ >
575 class ConstantUniqueMap : public AbstractTypeUser {
576 public:
577 typedef std::pair<const TypeClass*, ValType> MapKey;
578 typedef std::map<MapKey, ConstantClass *> MapTy;
579 typedef std::map<ConstantClass *, typename MapTy::iterator> InverseMapTy;
580 typedef std::map<const DerivedType*, typename MapTy::iterator>
581 AbstractTypeMapTy;
582 private:
583 /// Map - This is the main map from the element descriptor to the Constants.
584 /// This is the primary way we avoid creating two of the same shape
585 /// constant.
586 MapTy Map;
588 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
589 /// from the constants to their element in Map. This is important for
590 /// removal of constants from the array, which would otherwise have to scan
591 /// through the map with very large keys.
592 InverseMapTy InverseMap;
594 /// AbstractTypeMap - Map for abstract type constants.
596 AbstractTypeMapTy AbstractTypeMap;
598 public:
599 typename MapTy::iterator map_begin() { return Map.begin(); }
600 typename MapTy::iterator map_end() { return Map.end(); }
602 void freeConstants() {
603 for (typename MapTy::iterator I=Map.begin(), E=Map.end();
604 I != E; ++I) {
605 // Asserts that use_empty().
606 delete I->second;
610 /// InsertOrGetItem - Return an iterator for the specified element.
611 /// If the element exists in the map, the returned iterator points to the
612 /// entry and Exists=true. If not, the iterator points to the newly
613 /// inserted entry and returns Exists=false. Newly inserted entries have
614 /// I->second == 0, and should be filled in.
615 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, ConstantClass *>
616 &InsertVal,
617 bool &Exists) {
618 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
619 Exists = !IP.second;
620 return IP.first;
623 private:
624 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
625 if (HasLargeKey) {
626 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
627 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
628 IMI->second->second == CP &&
629 "InverseMap corrupt!");
630 return IMI->second;
633 typename MapTy::iterator I =
634 Map.find(MapKey(static_cast<const TypeClass*>(CP->getRawType()),
635 ConstantKeyData<ConstantClass>::getValType(CP)));
636 if (I == Map.end() || I->second != CP) {
637 // FIXME: This should not use a linear scan. If this gets to be a
638 // performance problem, someone should look at this.
639 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
640 /* empty */;
642 return I;
645 void AddAbstractTypeUser(const Type *Ty, typename MapTy::iterator I) {
646 // If the type of the constant is abstract, make sure that an entry
647 // exists for it in the AbstractTypeMap.
648 if (Ty->isAbstract()) {
649 const DerivedType *DTy = static_cast<const DerivedType *>(Ty);
650 typename AbstractTypeMapTy::iterator TI = AbstractTypeMap.find(DTy);
652 if (TI == AbstractTypeMap.end()) {
653 // Add ourselves to the ATU list of the type.
654 cast<DerivedType>(DTy)->addAbstractTypeUser(this);
656 AbstractTypeMap.insert(TI, std::make_pair(DTy, I));
661 ConstantClass* Create(const TypeClass *Ty, const ValType &V,
662 typename MapTy::iterator I) {
663 ConstantClass* Result =
664 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
666 assert(Result->getType() == Ty && "Type specified is not correct!");
667 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
669 if (HasLargeKey) // Remember the reverse mapping if needed.
670 InverseMap.insert(std::make_pair(Result, I));
672 AddAbstractTypeUser(Ty, I);
674 return Result;
676 public:
678 /// getOrCreate - Return the specified constant from the map, creating it if
679 /// necessary.
680 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
681 MapKey Lookup(Ty, V);
682 ConstantClass* Result = 0;
684 typename MapTy::iterator I = Map.find(Lookup);
685 // Is it in the map?
686 if (I != Map.end())
687 Result = I->second;
689 if (!Result) {
690 // If no preexisting value, create one now...
691 Result = Create(Ty, V, I);
694 return Result;
697 void UpdateAbstractTypeMap(const DerivedType *Ty,
698 typename MapTy::iterator I) {
699 assert(AbstractTypeMap.count(Ty) &&
700 "Abstract type not in AbstractTypeMap?");
701 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
702 if (ATMEntryIt == I) {
703 // Yes, we are removing the representative entry for this type.
704 // See if there are any other entries of the same type.
705 typename MapTy::iterator TmpIt = ATMEntryIt;
707 // First check the entry before this one...
708 if (TmpIt != Map.begin()) {
709 --TmpIt;
710 if (TmpIt->first.first != Ty) // Not the same type, move back...
711 ++TmpIt;
714 // If we didn't find the same type, try to move forward...
715 if (TmpIt == ATMEntryIt) {
716 ++TmpIt;
717 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
718 --TmpIt; // No entry afterwards with the same type
721 // If there is another entry in the map of the same abstract type,
722 // update the AbstractTypeMap entry now.
723 if (TmpIt != ATMEntryIt) {
724 ATMEntryIt = TmpIt;
725 } else {
726 // Otherwise, we are removing the last instance of this type
727 // from the table. Remove from the ATM, and from user list.
728 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
729 AbstractTypeMap.erase(Ty);
734 void remove(ConstantClass *CP) {
735 typename MapTy::iterator I = FindExistingElement(CP);
736 assert(I != Map.end() && "Constant not found in constant table!");
737 assert(I->second == CP && "Didn't find correct element?");
739 if (HasLargeKey) // Remember the reverse mapping if needed.
740 InverseMap.erase(CP);
742 // Now that we found the entry, make sure this isn't the entry that
743 // the AbstractTypeMap points to.
744 const TypeClass *Ty = I->first.first;
745 if (Ty->isAbstract())
746 UpdateAbstractTypeMap(static_cast<const DerivedType *>(Ty), I);
748 Map.erase(I);
751 /// MoveConstantToNewSlot - If we are about to change C to be the element
752 /// specified by I, update our internal data structures to reflect this
753 /// fact.
754 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
755 // First, remove the old location of the specified constant in the map.
756 typename MapTy::iterator OldI = FindExistingElement(C);
757 assert(OldI != Map.end() && "Constant not found in constant table!");
758 assert(OldI->second == C && "Didn't find correct element?");
760 // If this constant is the representative element for its abstract type,
761 // update the AbstractTypeMap so that the representative element is I.
763 // This must use getRawType() because if the type is under refinement, we
764 // will get the refineAbstractType callback below, and we don't want to
765 // kick union find in on the constant.
766 if (C->getRawType()->isAbstract()) {
767 typename AbstractTypeMapTy::iterator ATI =
768 AbstractTypeMap.find(cast<DerivedType>(C->getRawType()));
769 assert(ATI != AbstractTypeMap.end() &&
770 "Abstract type not in AbstractTypeMap?");
771 if (ATI->second == OldI)
772 ATI->second = I;
775 // Remove the old entry from the map.
776 Map.erase(OldI);
778 // Update the inverse map so that we know that this constant is now
779 // located at descriptor I.
780 if (HasLargeKey) {
781 assert(I->second == C && "Bad inversemap entry!");
782 InverseMap[C] = I;
786 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
787 typename AbstractTypeMapTy::iterator I = AbstractTypeMap.find(OldTy);
789 assert(I != AbstractTypeMap.end() &&
790 "Abstract type not in AbstractTypeMap?");
792 // Convert a constant at a time until the last one is gone. The last one
793 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
794 // eliminated eventually.
795 do {
796 ConstantClass *C = I->second->second;
797 MapKey Key(cast<TypeClass>(NewTy),
798 ConstantKeyData<ConstantClass>::getValType(C));
800 std::pair<typename MapTy::iterator, bool> IP =
801 Map.insert(std::make_pair(Key, C));
802 if (IP.second) {
803 // The map didn't previously have an appropriate constant in the
804 // new type.
806 // Remove the old entry.
807 typename MapTy::iterator OldI =
808 Map.find(MapKey(cast<TypeClass>(OldTy), IP.first->first.second));
809 assert(OldI != Map.end() && "Constant not in map!");
810 UpdateAbstractTypeMap(OldTy, OldI);
811 Map.erase(OldI);
813 // Set the constant's type. This is done in place!
814 setType(C, NewTy);
816 // Update the inverse map so that we know that this constant is now
817 // located at descriptor I.
818 if (HasLargeKey)
819 InverseMap[C] = IP.first;
821 AddAbstractTypeUser(NewTy, IP.first);
822 } else {
823 // The map already had an appropriate constant in the new type, so
824 // there's no longer a need for the old constant.
825 C->uncheckedReplaceAllUsesWith(IP.first->second);
826 C->destroyConstant(); // This constant is now dead, destroy it.
828 I = AbstractTypeMap.find(OldTy);
829 } while (I != AbstractTypeMap.end());
832 // If the type became concrete without being refined to any other existing
833 // type, we just remove ourselves from the ATU list.
834 void typeBecameConcrete(const DerivedType *AbsTy) {
835 AbsTy->removeAbstractTypeUser(this);
838 void dump() const {
839 DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n");
845 #endif