1 //===-- Constants.cpp - Implement Constant nodes --------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Constant* classes...
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Constants.h"
15 #include "ConstantFolding.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/GlobalValue.h"
18 #include "llvm/Instructions.h"
19 #include "llvm/SymbolTable.h"
20 #include "llvm/Module.h"
21 #include "llvm/ADT/StringExtras.h"
22 #include "llvm/Support/MathExtras.h"
27 ConstantBool *ConstantBool::True = new ConstantBool(true);
28 ConstantBool *ConstantBool::False = new ConstantBool(false);
31 //===----------------------------------------------------------------------===//
33 //===----------------------------------------------------------------------===//
35 void Constant::destroyConstantImpl() {
36 // When a Constant is destroyed, there may be lingering
37 // references to the constant by other constants in the constant pool. These
38 // constants are implicitly dependent on the module that is being deleted,
39 // but they don't know that. Because we only find out when the CPV is
40 // deleted, we must now notify all of our users (that should only be
41 // Constants) that they are, in fact, invalid now and should be deleted.
43 while (!use_empty()) {
44 Value *V = use_back();
45 #ifndef NDEBUG // Only in -g mode...
46 if (!isa<Constant>(V))
47 std::cerr << "While deleting: " << *this
48 << "\n\nUse still stuck around after Def is destroyed: "
51 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
52 Constant *CV = cast<Constant>(V);
53 CV->destroyConstant();
55 // The constant should remove itself from our use list...
56 assert((use_empty() || use_back() != V) && "Constant not removed!");
59 // Value has no outstanding references it is safe to delete it now...
63 // Static constructor to create a '0' constant of arbitrary type...
64 Constant *Constant::getNullValue(const Type *Ty) {
65 switch (Ty->getTypeID()) {
66 case Type::BoolTyID: {
67 static Constant *NullBool = ConstantBool::get(false);
70 case Type::SByteTyID: {
71 static Constant *NullSByte = ConstantSInt::get(Type::SByteTy, 0);
74 case Type::UByteTyID: {
75 static Constant *NullUByte = ConstantUInt::get(Type::UByteTy, 0);
78 case Type::ShortTyID: {
79 static Constant *NullShort = ConstantSInt::get(Type::ShortTy, 0);
82 case Type::UShortTyID: {
83 static Constant *NullUShort = ConstantUInt::get(Type::UShortTy, 0);
87 static Constant *NullInt = ConstantSInt::get(Type::IntTy, 0);
90 case Type::UIntTyID: {
91 static Constant *NullUInt = ConstantUInt::get(Type::UIntTy, 0);
94 case Type::LongTyID: {
95 static Constant *NullLong = ConstantSInt::get(Type::LongTy, 0);
98 case Type::ULongTyID: {
99 static Constant *NullULong = ConstantUInt::get(Type::ULongTy, 0);
103 case Type::FloatTyID: {
104 static Constant *NullFloat = ConstantFP::get(Type::FloatTy, 0);
107 case Type::DoubleTyID: {
108 static Constant *NullDouble = ConstantFP::get(Type::DoubleTy, 0);
112 case Type::PointerTyID:
113 return ConstantPointerNull::get(cast<PointerType>(Ty));
115 case Type::StructTyID:
116 case Type::ArrayTyID:
117 case Type::PackedTyID:
118 return ConstantAggregateZero::get(Ty);
120 // Function, Label, or Opaque type?
121 assert(!"Cannot create a null constant of that type!");
126 // Static constructor to create the maximum constant of an integral type...
127 ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
128 switch (Ty->getTypeID()) {
129 case Type::BoolTyID: return ConstantBool::True;
130 case Type::SByteTyID:
131 case Type::ShortTyID:
133 case Type::LongTyID: {
134 // Calculate 011111111111111...
135 unsigned TypeBits = Ty->getPrimitiveSize()*8;
136 int64_t Val = INT64_MAX; // All ones
137 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
138 return ConstantSInt::get(Ty, Val);
141 case Type::UByteTyID:
142 case Type::UShortTyID:
144 case Type::ULongTyID: return getAllOnesValue(Ty);
150 // Static constructor to create the minimum constant for an integral type...
151 ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
152 switch (Ty->getTypeID()) {
153 case Type::BoolTyID: return ConstantBool::False;
154 case Type::SByteTyID:
155 case Type::ShortTyID:
157 case Type::LongTyID: {
158 // Calculate 1111111111000000000000
159 unsigned TypeBits = Ty->getPrimitiveSize()*8;
160 int64_t Val = -1; // All ones
161 Val <<= TypeBits-1; // Shift over to the right spot
162 return ConstantSInt::get(Ty, Val);
165 case Type::UByteTyID:
166 case Type::UShortTyID:
168 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
174 // Static constructor to create an integral constant with all bits set
175 ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
176 switch (Ty->getTypeID()) {
177 case Type::BoolTyID: return ConstantBool::True;
178 case Type::SByteTyID:
179 case Type::ShortTyID:
181 case Type::LongTyID: return ConstantSInt::get(Ty, -1);
183 case Type::UByteTyID:
184 case Type::UShortTyID:
186 case Type::ULongTyID: {
187 // Calculate ~0 of the right type...
188 unsigned TypeBits = Ty->getPrimitiveSize()*8;
189 uint64_t Val = ~0ULL; // All ones
190 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
191 return ConstantUInt::get(Ty, Val);
197 bool ConstantUInt::isAllOnesValue() const {
198 unsigned TypeBits = getType()->getPrimitiveSize()*8;
199 uint64_t Val = ~0ULL; // All ones
200 Val >>= 64-TypeBits; // Shift out inappropriate bits
201 return getValue() == Val;
205 //===----------------------------------------------------------------------===//
206 // ConstantXXX Classes
207 //===----------------------------------------------------------------------===//
209 //===----------------------------------------------------------------------===//
210 // Normal Constructors
212 ConstantIntegral::ConstantIntegral(const Type *Ty, uint64_t V)
213 : Constant(Ty, SimpleConstantVal, 0, 0) {
217 ConstantBool::ConstantBool(bool V) : ConstantIntegral(Type::BoolTy, V) {
220 ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : ConstantIntegral(Ty, V) {
223 ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
224 assert(Ty->isInteger() && Ty->isSigned() &&
225 "Illegal type for signed integer constant!");
226 assert(isValueValidForType(Ty, V) && "Value too large for type!");
229 ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
230 assert(Ty->isInteger() && Ty->isUnsigned() &&
231 "Illegal type for unsigned integer constant!");
232 assert(isValueValidForType(Ty, V) && "Value too large for type!");
235 ConstantFP::ConstantFP(const Type *Ty, double V)
236 : Constant(Ty, SimpleConstantVal, 0, 0) {
237 assert(isValueValidForType(Ty, V) && "Value too large for type!");
241 ConstantArray::ConstantArray(const ArrayType *T,
242 const std::vector<Constant*> &V)
243 : Constant(T, SimpleConstantVal, new Use[V.size()], V.size()) {
244 assert(V.size() == T->getNumElements() &&
245 "Invalid initializer vector for constant array");
246 Use *OL = OperandList;
247 for (unsigned i = 0, e = V.size(); i != e; ++i) {
248 assert((V[i]->getType() == T->getElementType() ||
250 V[i]->getType()->getTypeID()==T->getElementType()->getTypeID())) &&
251 "Initializer for array element doesn't match array element type!");
252 OL[i].init(V[i], this);
256 ConstantArray::~ConstantArray() {
257 delete [] OperandList;
260 ConstantStruct::ConstantStruct(const StructType *T,
261 const std::vector<Constant*> &V)
262 : Constant(T, SimpleConstantVal, new Use[V.size()], V.size()) {
263 assert(V.size() == T->getNumElements() &&
264 "Invalid initializer vector for constant structure");
265 Use *OL = OperandList;
266 for (unsigned i = 0, e = V.size(); i != e; ++i) {
267 assert((V[i]->getType() == T->getElementType(i) ||
268 ((T->getElementType(i)->isAbstract() ||
269 V[i]->getType()->isAbstract()) &&
270 T->getElementType(i)->getTypeID()==V[i]->getType()->getTypeID()))&&
271 "Initializer for struct element doesn't match struct element type!");
272 OL[i].init(V[i], this);
276 ConstantStruct::~ConstantStruct() {
277 delete [] OperandList;
281 ConstantPacked::ConstantPacked(const PackedType *T,
282 const std::vector<Constant*> &V)
283 : Constant(T, SimpleConstantVal, new Use[V.size()], V.size()) {
284 Use *OL = OperandList;
285 for (unsigned i = 0, e = V.size(); i != e; ++i) {
286 assert((V[i]->getType() == T->getElementType() ||
288 V[i]->getType()->getTypeID()==T->getElementType()->getTypeID())) &&
289 "Initializer for packed element doesn't match packed element type!");
290 OL[i].init(V[i], this);
294 ConstantPacked::~ConstantPacked() {
295 delete [] OperandList;
298 /// UnaryConstantExpr - This class is private to Constants.cpp, and is used
299 /// behind the scenes to implement unary constant exprs.
300 class UnaryConstantExpr : public ConstantExpr {
303 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
304 : ConstantExpr(Ty, Opcode, &Op, 1), Op(C, this) {}
307 static bool isSetCC(unsigned Opcode) {
308 return Opcode == Instruction::SetEQ || Opcode == Instruction::SetNE ||
309 Opcode == Instruction::SetLT || Opcode == Instruction::SetGT ||
310 Opcode == Instruction::SetLE || Opcode == Instruction::SetGE;
313 /// BinaryConstantExpr - This class is private to Constants.cpp, and is used
314 /// behind the scenes to implement binary constant exprs.
315 class BinaryConstantExpr : public ConstantExpr {
318 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
319 : ConstantExpr(isSetCC(Opcode) ? Type::BoolTy : C1->getType(),
321 Ops[0].init(C1, this);
322 Ops[1].init(C2, this);
326 /// SelectConstantExpr - This class is private to Constants.cpp, and is used
327 /// behind the scenes to implement select constant exprs.
328 class SelectConstantExpr : public ConstantExpr {
331 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
332 : ConstantExpr(C2->getType(), Instruction::Select, Ops, 3) {
333 Ops[0].init(C1, this);
334 Ops[1].init(C2, this);
335 Ops[2].init(C3, this);
339 /// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
340 /// used behind the scenes to implement getelementpr constant exprs.
341 struct GetElementPtrConstantExpr : public ConstantExpr {
342 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
344 : ConstantExpr(DestTy, Instruction::GetElementPtr,
345 new Use[IdxList.size()+1], IdxList.size()+1) {
346 OperandList[0].init(C, this);
347 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
348 OperandList[i+1].init(IdxList[i], this);
350 ~GetElementPtrConstantExpr() {
351 delete [] OperandList;
355 /// ConstantExpr::get* - Return some common constants without having to
356 /// specify the full Instruction::OPCODE identifier.
358 Constant *ConstantExpr::getNeg(Constant *C) {
359 if (!C->getType()->isFloatingPoint())
360 return get(Instruction::Sub, getNullValue(C->getType()), C);
362 return get(Instruction::Sub, ConstantFP::get(C->getType(), -0.0), C);
364 Constant *ConstantExpr::getNot(Constant *C) {
365 assert(isa<ConstantIntegral>(C) && "Cannot NOT a nonintegral type!");
366 return get(Instruction::Xor, C,
367 ConstantIntegral::getAllOnesValue(C->getType()));
369 Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
370 return get(Instruction::Add, C1, C2);
372 Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
373 return get(Instruction::Sub, C1, C2);
375 Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
376 return get(Instruction::Mul, C1, C2);
378 Constant *ConstantExpr::getDiv(Constant *C1, Constant *C2) {
379 return get(Instruction::Div, C1, C2);
381 Constant *ConstantExpr::getRem(Constant *C1, Constant *C2) {
382 return get(Instruction::Rem, C1, C2);
384 Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
385 return get(Instruction::And, C1, C2);
387 Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
388 return get(Instruction::Or, C1, C2);
390 Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
391 return get(Instruction::Xor, C1, C2);
393 Constant *ConstantExpr::getSetEQ(Constant *C1, Constant *C2) {
394 return get(Instruction::SetEQ, C1, C2);
396 Constant *ConstantExpr::getSetNE(Constant *C1, Constant *C2) {
397 return get(Instruction::SetNE, C1, C2);
399 Constant *ConstantExpr::getSetLT(Constant *C1, Constant *C2) {
400 return get(Instruction::SetLT, C1, C2);
402 Constant *ConstantExpr::getSetGT(Constant *C1, Constant *C2) {
403 return get(Instruction::SetGT, C1, C2);
405 Constant *ConstantExpr::getSetLE(Constant *C1, Constant *C2) {
406 return get(Instruction::SetLE, C1, C2);
408 Constant *ConstantExpr::getSetGE(Constant *C1, Constant *C2) {
409 return get(Instruction::SetGE, C1, C2);
411 Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
412 return get(Instruction::Shl, C1, C2);
414 Constant *ConstantExpr::getShr(Constant *C1, Constant *C2) {
415 return get(Instruction::Shr, C1, C2);
418 Constant *ConstantExpr::getUShr(Constant *C1, Constant *C2) {
419 if (C1->getType()->isUnsigned()) return getShr(C1, C2);
420 return getCast(getShr(getCast(C1,
421 C1->getType()->getUnsignedVersion()), C2), C1->getType());
424 Constant *ConstantExpr::getSShr(Constant *C1, Constant *C2) {
425 if (C1->getType()->isSigned()) return getShr(C1, C2);
426 return getCast(getShr(getCast(C1,
427 C1->getType()->getSignedVersion()), C2), C1->getType());
431 //===----------------------------------------------------------------------===//
432 // isValueValidForType implementations
434 bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
435 switch (Ty->getTypeID()) {
437 return false; // These can't be represented as integers!!!
439 case Type::SByteTyID:
440 return (Val <= INT8_MAX && Val >= INT8_MIN);
441 case Type::ShortTyID:
442 return (Val <= INT16_MAX && Val >= INT16_MIN);
444 return (Val <= int(INT32_MAX) && Val >= int(INT32_MIN));
446 return true; // This is the largest type...
450 bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
451 switch (Ty->getTypeID()) {
453 return false; // These can't be represented as integers!!!
456 case Type::UByteTyID:
457 return (Val <= UINT8_MAX);
458 case Type::UShortTyID:
459 return (Val <= UINT16_MAX);
461 return (Val <= UINT32_MAX);
462 case Type::ULongTyID:
463 return true; // This is the largest type...
467 bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
468 switch (Ty->getTypeID()) {
470 return false; // These can't be represented as floating point!
472 // TODO: Figure out how to test if a double can be cast to a float!
473 case Type::FloatTyID:
474 case Type::DoubleTyID:
475 return true; // This is the largest type...
479 //===----------------------------------------------------------------------===//
480 // replaceUsesOfWithOnConstant implementations
482 void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
483 bool DisableChecking) {
484 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
486 std::vector<Constant*> Values;
487 Values.reserve(getNumOperands()); // Build replacement array...
488 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
489 Constant *Val = getOperand(i);
490 if (Val == From) Val = cast<Constant>(To);
491 Values.push_back(Val);
494 Constant *Replacement = ConstantArray::get(getType(), Values);
495 assert(Replacement != this && "I didn't contain From!");
497 // Everyone using this now uses the replacement...
499 uncheckedReplaceAllUsesWith(Replacement);
501 replaceAllUsesWith(Replacement);
503 // Delete the old constant!
507 void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
508 bool DisableChecking) {
509 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
511 std::vector<Constant*> Values;
512 Values.reserve(getNumOperands()); // Build replacement array...
513 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
514 Constant *Val = getOperand(i);
515 if (Val == From) Val = cast<Constant>(To);
516 Values.push_back(Val);
519 Constant *Replacement = ConstantStruct::get(getType(), Values);
520 assert(Replacement != this && "I didn't contain From!");
522 // Everyone using this now uses the replacement...
524 uncheckedReplaceAllUsesWith(Replacement);
526 replaceAllUsesWith(Replacement);
528 // Delete the old constant!
532 void ConstantPacked::replaceUsesOfWithOnConstant(Value *From, Value *To,
533 bool DisableChecking) {
534 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
536 std::vector<Constant*> Values;
537 Values.reserve(getNumOperands()); // Build replacement array...
538 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
539 Constant *Val = getOperand(i);
540 if (Val == From) Val = cast<Constant>(To);
541 Values.push_back(Val);
544 Constant *Replacement = ConstantPacked::get(getType(), Values);
545 assert(Replacement != this && "I didn't contain From!");
547 // Everyone using this now uses the replacement...
549 uncheckedReplaceAllUsesWith(Replacement);
551 replaceAllUsesWith(Replacement);
553 // Delete the old constant!
557 void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
558 bool DisableChecking) {
559 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
560 Constant *To = cast<Constant>(ToV);
562 Constant *Replacement = 0;
563 if (getOpcode() == Instruction::GetElementPtr) {
564 std::vector<Constant*> Indices;
565 Constant *Pointer = getOperand(0);
566 Indices.reserve(getNumOperands()-1);
567 if (Pointer == From) Pointer = To;
569 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
570 Constant *Val = getOperand(i);
571 if (Val == From) Val = To;
572 Indices.push_back(Val);
574 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
575 } else if (getOpcode() == Instruction::Cast) {
576 assert(getOperand(0) == From && "Cast only has one use!");
577 Replacement = ConstantExpr::getCast(To, getType());
578 } else if (getOpcode() == Instruction::Select) {
579 Constant *C1 = getOperand(0);
580 Constant *C2 = getOperand(1);
581 Constant *C3 = getOperand(2);
582 if (C1 == From) C1 = To;
583 if (C2 == From) C2 = To;
584 if (C3 == From) C3 = To;
585 Replacement = ConstantExpr::getSelect(C1, C2, C3);
586 } else if (getNumOperands() == 2) {
587 Constant *C1 = getOperand(0);
588 Constant *C2 = getOperand(1);
589 if (C1 == From) C1 = To;
590 if (C2 == From) C2 = To;
591 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
593 assert(0 && "Unknown ConstantExpr type!");
597 assert(Replacement != this && "I didn't contain From!");
599 // Everyone using this now uses the replacement...
601 uncheckedReplaceAllUsesWith(Replacement);
603 replaceAllUsesWith(Replacement);
605 // Delete the old constant!
609 //===----------------------------------------------------------------------===//
610 // Factory Function Implementation
612 // ConstantCreator - A class that is used to create constants by
613 // ValueMap*. This class should be partially specialized if there is
614 // something strange that needs to be done to interface to the ctor for the
618 template<class ConstantClass, class TypeClass, class ValType>
619 struct ConstantCreator {
620 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
621 return new ConstantClass(Ty, V);
625 template<class ConstantClass, class TypeClass>
626 struct ConvertConstantType {
627 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
628 assert(0 && "This type cannot be converted!\n");
635 template<class ValType, class TypeClass, class ConstantClass>
636 class ValueMap : public AbstractTypeUser {
637 typedef std::pair<const TypeClass*, ValType> MapKey;
638 typedef std::map<MapKey, ConstantClass *> MapTy;
639 typedef typename MapTy::iterator MapIterator;
642 typedef std::map<const TypeClass*, MapIterator> AbstractTypeMapTy;
643 AbstractTypeMapTy AbstractTypeMap;
645 friend void Constant::clearAllValueMaps();
647 void clear(std::vector<Constant *> &Constants) {
648 for(MapIterator I = Map.begin(); I != Map.end(); ++I)
649 Constants.push_back(I->second);
651 AbstractTypeMap.clear();
655 // getOrCreate - Return the specified constant from the map, creating it if
657 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
658 MapKey Lookup(Ty, V);
659 MapIterator I = Map.lower_bound(Lookup);
660 if (I != Map.end() && I->first == Lookup)
661 return I->second; // Is it in the map?
663 // If no preexisting value, create one now...
664 ConstantClass *Result =
665 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
668 /// FIXME: why does this assert fail when loading 176.gcc?
669 //assert(Result->getType() == Ty && "Type specified is not correct!");
670 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
672 // If the type of the constant is abstract, make sure that an entry exists
673 // for it in the AbstractTypeMap.
674 if (Ty->isAbstract()) {
675 typename AbstractTypeMapTy::iterator TI =
676 AbstractTypeMap.lower_bound(Ty);
678 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
679 // Add ourselves to the ATU list of the type.
680 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
682 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
688 void remove(ConstantClass *CP) {
689 MapIterator I = Map.find(MapKey((TypeClass*)CP->getRawType(),
691 if (I == Map.end() || I->second != CP) {
692 // FIXME: This should not use a linear scan. If this gets to be a
693 // performance problem, someone should look at this.
694 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
698 assert(I != Map.end() && "Constant not found in constant table!");
699 assert(I->second == CP && "Didn't find correct element?");
701 // Now that we found the entry, make sure this isn't the entry that
702 // the AbstractTypeMap points to.
703 const TypeClass *Ty = I->first.first;
704 if (Ty->isAbstract()) {
705 assert(AbstractTypeMap.count(Ty) &&
706 "Abstract type not in AbstractTypeMap?");
707 MapIterator &ATMEntryIt = AbstractTypeMap[Ty];
708 if (ATMEntryIt == I) {
709 // Yes, we are removing the representative entry for this type.
710 // See if there are any other entries of the same type.
711 MapIterator TmpIt = ATMEntryIt;
713 // First check the entry before this one...
714 if (TmpIt != Map.begin()) {
716 if (TmpIt->first.first != Ty) // Not the same type, move back...
720 // If we didn't find the same type, try to move forward...
721 if (TmpIt == ATMEntryIt) {
723 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
724 --TmpIt; // No entry afterwards with the same type
727 // If there is another entry in the map of the same abstract type,
728 // update the AbstractTypeMap entry now.
729 if (TmpIt != ATMEntryIt) {
732 // Otherwise, we are removing the last instance of this type
733 // from the table. Remove from the ATM, and from user list.
734 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
735 AbstractTypeMap.erase(Ty);
743 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
744 typename AbstractTypeMapTy::iterator I =
745 AbstractTypeMap.find(cast<TypeClass>(OldTy));
747 assert(I != AbstractTypeMap.end() &&
748 "Abstract type not in AbstractTypeMap?");
750 // Convert a constant at a time until the last one is gone. The last one
751 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
752 // eliminated eventually.
754 ConvertConstantType<ConstantClass,
755 TypeClass>::convert(I->second->second,
756 cast<TypeClass>(NewTy));
758 I = AbstractTypeMap.find(cast<TypeClass>(OldTy));
759 } while (I != AbstractTypeMap.end());
762 // If the type became concrete without being refined to any other existing
763 // type, we just remove ourselves from the ATU list.
764 void typeBecameConcrete(const DerivedType *AbsTy) {
765 AbsTy->removeAbstractTypeUser(this);
769 std::cerr << "Constant.cpp: ValueMap\n";
774 //---- ConstantUInt::get() and ConstantSInt::get() implementations...
776 static ValueMap< int64_t, Type, ConstantSInt> SIntConstants;
777 static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants;
779 ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
780 return SIntConstants.getOrCreate(Ty, V);
783 ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
784 return UIntConstants.getOrCreate(Ty, V);
787 ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
788 assert(V <= 127 && "Can only be used with very small positive constants!");
789 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
790 return ConstantUInt::get(Ty, V);
793 //---- ConstantFP::get() implementation...
797 struct ConstantCreator<ConstantFP, Type, uint64_t> {
798 static ConstantFP *create(const Type *Ty, uint64_t V) {
799 assert(Ty == Type::DoubleTy);
800 return new ConstantFP(Ty, BitsToDouble(V));
804 struct ConstantCreator<ConstantFP, Type, uint32_t> {
805 static ConstantFP *create(const Type *Ty, uint32_t V) {
806 assert(Ty == Type::FloatTy);
807 return new ConstantFP(Ty, BitsToFloat(V));
812 static ValueMap<uint64_t, Type, ConstantFP> DoubleConstants;
813 static ValueMap<uint32_t, Type, ConstantFP> FloatConstants;
815 bool ConstantFP::isNullValue() const {
816 return DoubleToBits(Val) == 0;
819 bool ConstantFP::isExactlyValue(double V) const {
820 return DoubleToBits(V) == DoubleToBits(Val);
824 ConstantFP *ConstantFP::get(const Type *Ty, double V) {
825 if (Ty == Type::FloatTy) {
826 // Force the value through memory to normalize it.
827 return FloatConstants.getOrCreate(Ty, FloatToBits(V));
829 assert(Ty == Type::DoubleTy);
830 return DoubleConstants.getOrCreate(Ty, DoubleToBits(V));
834 //---- ConstantAggregateZero::get() implementation...
837 // ConstantAggregateZero does not take extra "value" argument...
838 template<class ValType>
839 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
840 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
841 return new ConstantAggregateZero(Ty);
846 struct ConvertConstantType<ConstantAggregateZero, Type> {
847 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
848 // Make everyone now use a constant of the new type...
849 Constant *New = ConstantAggregateZero::get(NewTy);
850 assert(New != OldC && "Didn't replace constant??");
851 OldC->uncheckedReplaceAllUsesWith(New);
852 OldC->destroyConstant(); // This constant is now dead, destroy it.
857 static ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
859 static char getValType(ConstantAggregateZero *CPZ) { return 0; }
861 Constant *ConstantAggregateZero::get(const Type *Ty) {
862 return AggZeroConstants.getOrCreate(Ty, 0);
865 // destroyConstant - Remove the constant from the constant table...
867 void ConstantAggregateZero::destroyConstant() {
868 AggZeroConstants.remove(this);
869 destroyConstantImpl();
872 void ConstantAggregateZero::replaceUsesOfWithOnConstant(Value *From, Value *To,
873 bool DisableChecking) {
874 assert(0 && "No uses!");
880 //---- ConstantArray::get() implementation...
884 struct ConvertConstantType<ConstantArray, ArrayType> {
885 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
886 // Make everyone now use a constant of the new type...
887 std::vector<Constant*> C;
888 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
889 C.push_back(cast<Constant>(OldC->getOperand(i)));
890 Constant *New = ConstantArray::get(NewTy, C);
891 assert(New != OldC && "Didn't replace constant??");
892 OldC->uncheckedReplaceAllUsesWith(New);
893 OldC->destroyConstant(); // This constant is now dead, destroy it.
898 static std::vector<Constant*> getValType(ConstantArray *CA) {
899 std::vector<Constant*> Elements;
900 Elements.reserve(CA->getNumOperands());
901 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
902 Elements.push_back(cast<Constant>(CA->getOperand(i)));
906 static ValueMap<std::vector<Constant*>, ArrayType,
907 ConstantArray> ArrayConstants;
909 Constant *ConstantArray::get(const ArrayType *Ty,
910 const std::vector<Constant*> &V) {
911 // If this is an all-zero array, return a ConstantAggregateZero object
914 if (!C->isNullValue())
915 return ArrayConstants.getOrCreate(Ty, V);
916 for (unsigned i = 1, e = V.size(); i != e; ++i)
918 return ArrayConstants.getOrCreate(Ty, V);
920 return ConstantAggregateZero::get(Ty);
923 // destroyConstant - Remove the constant from the constant table...
925 void ConstantArray::destroyConstant() {
926 ArrayConstants.remove(this);
927 destroyConstantImpl();
930 // ConstantArray::get(const string&) - Return an array that is initialized to
931 // contain the specified string. A null terminator is added to the specified
932 // string so that it may be used in a natural way...
934 Constant *ConstantArray::get(const std::string &Str) {
935 std::vector<Constant*> ElementVals;
937 for (unsigned i = 0; i < Str.length(); ++i)
938 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
940 // Add a null terminator to the string...
941 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
943 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
944 return ConstantArray::get(ATy, ElementVals);
947 /// isString - This method returns true if the array is an array of sbyte or
948 /// ubyte, and if the elements of the array are all ConstantInt's.
949 bool ConstantArray::isString() const {
950 // Check the element type for sbyte or ubyte...
951 if (getType()->getElementType() != Type::UByteTy &&
952 getType()->getElementType() != Type::SByteTy)
954 // Check the elements to make sure they are all integers, not constant
956 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
957 if (!isa<ConstantInt>(getOperand(i)))
962 // getAsString - If the sub-element type of this array is either sbyte or ubyte,
963 // then this method converts the array to an std::string and returns it.
964 // Otherwise, it asserts out.
966 std::string ConstantArray::getAsString() const {
967 assert(isString() && "Not a string!");
969 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
970 Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue();
975 //---- ConstantStruct::get() implementation...
980 struct ConvertConstantType<ConstantStruct, StructType> {
981 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
982 // Make everyone now use a constant of the new type...
983 std::vector<Constant*> C;
984 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
985 C.push_back(cast<Constant>(OldC->getOperand(i)));
986 Constant *New = ConstantStruct::get(NewTy, C);
987 assert(New != OldC && "Didn't replace constant??");
989 OldC->uncheckedReplaceAllUsesWith(New);
990 OldC->destroyConstant(); // This constant is now dead, destroy it.
995 static ValueMap<std::vector<Constant*>, StructType,
996 ConstantStruct> StructConstants;
998 static std::vector<Constant*> getValType(ConstantStruct *CS) {
999 std::vector<Constant*> Elements;
1000 Elements.reserve(CS->getNumOperands());
1001 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1002 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1006 Constant *ConstantStruct::get(const StructType *Ty,
1007 const std::vector<Constant*> &V) {
1008 // Create a ConstantAggregateZero value if all elements are zeros...
1009 for (unsigned i = 0, e = V.size(); i != e; ++i)
1010 if (!V[i]->isNullValue())
1011 return StructConstants.getOrCreate(Ty, V);
1013 return ConstantAggregateZero::get(Ty);
1016 Constant *ConstantStruct::get(const std::vector<Constant*> &V) {
1017 std::vector<const Type*> StructEls;
1018 StructEls.reserve(V.size());
1019 for (unsigned i = 0, e = V.size(); i != e; ++i)
1020 StructEls.push_back(V[i]->getType());
1021 return get(StructType::get(StructEls), V);
1024 // destroyConstant - Remove the constant from the constant table...
1026 void ConstantStruct::destroyConstant() {
1027 StructConstants.remove(this);
1028 destroyConstantImpl();
1031 //---- ConstantPacked::get() implementation...
1035 struct ConvertConstantType<ConstantPacked, PackedType> {
1036 static void convert(ConstantPacked *OldC, const PackedType *NewTy) {
1037 // Make everyone now use a constant of the new type...
1038 std::vector<Constant*> C;
1039 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1040 C.push_back(cast<Constant>(OldC->getOperand(i)));
1041 Constant *New = ConstantPacked::get(NewTy, C);
1042 assert(New != OldC && "Didn't replace constant??");
1043 OldC->uncheckedReplaceAllUsesWith(New);
1044 OldC->destroyConstant(); // This constant is now dead, destroy it.
1049 static std::vector<Constant*> getValType(ConstantPacked *CP) {
1050 std::vector<Constant*> Elements;
1051 Elements.reserve(CP->getNumOperands());
1052 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1053 Elements.push_back(CP->getOperand(i));
1057 static ValueMap<std::vector<Constant*>, PackedType,
1058 ConstantPacked> PackedConstants;
1060 Constant *ConstantPacked::get(const PackedType *Ty,
1061 const std::vector<Constant*> &V) {
1062 // If this is an all-zero packed, return a ConstantAggregateZero object
1065 if (!C->isNullValue())
1066 return PackedConstants.getOrCreate(Ty, V);
1067 for (unsigned i = 1, e = V.size(); i != e; ++i)
1069 return PackedConstants.getOrCreate(Ty, V);
1071 return ConstantAggregateZero::get(Ty);
1074 Constant *ConstantPacked::get(const std::vector<Constant*> &V) {
1075 assert(!V.empty() && "Cannot infer type if V is empty");
1076 return get(PackedType::get(V.front()->getType(),V.size()), V);
1079 // destroyConstant - Remove the constant from the constant table...
1081 void ConstantPacked::destroyConstant() {
1082 PackedConstants.remove(this);
1083 destroyConstantImpl();
1086 //---- ConstantPointerNull::get() implementation...
1090 // ConstantPointerNull does not take extra "value" argument...
1091 template<class ValType>
1092 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1093 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1094 return new ConstantPointerNull(Ty);
1099 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1100 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1101 // Make everyone now use a constant of the new type...
1102 Constant *New = ConstantPointerNull::get(NewTy);
1103 assert(New != OldC && "Didn't replace constant??");
1104 OldC->uncheckedReplaceAllUsesWith(New);
1105 OldC->destroyConstant(); // This constant is now dead, destroy it.
1110 static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
1112 static char getValType(ConstantPointerNull *) {
1117 ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
1118 return NullPtrConstants.getOrCreate(Ty, 0);
1121 // destroyConstant - Remove the constant from the constant table...
1123 void ConstantPointerNull::destroyConstant() {
1124 NullPtrConstants.remove(this);
1125 destroyConstantImpl();
1129 //---- UndefValue::get() implementation...
1133 // UndefValue does not take extra "value" argument...
1134 template<class ValType>
1135 struct ConstantCreator<UndefValue, Type, ValType> {
1136 static UndefValue *create(const Type *Ty, const ValType &V) {
1137 return new UndefValue(Ty);
1142 struct ConvertConstantType<UndefValue, Type> {
1143 static void convert(UndefValue *OldC, const Type *NewTy) {
1144 // Make everyone now use a constant of the new type.
1145 Constant *New = UndefValue::get(NewTy);
1146 assert(New != OldC && "Didn't replace constant??");
1147 OldC->uncheckedReplaceAllUsesWith(New);
1148 OldC->destroyConstant(); // This constant is now dead, destroy it.
1153 static ValueMap<char, Type, UndefValue> UndefValueConstants;
1155 static char getValType(UndefValue *) {
1160 UndefValue *UndefValue::get(const Type *Ty) {
1161 return UndefValueConstants.getOrCreate(Ty, 0);
1164 // destroyConstant - Remove the constant from the constant table.
1166 void UndefValue::destroyConstant() {
1167 UndefValueConstants.remove(this);
1168 destroyConstantImpl();
1174 //---- ConstantExpr::get() implementations...
1176 typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
1180 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
1181 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
1182 if (V.first == Instruction::Cast)
1183 return new UnaryConstantExpr(Instruction::Cast, V.second[0], Ty);
1184 if ((V.first >= Instruction::BinaryOpsBegin &&
1185 V.first < Instruction::BinaryOpsEnd) ||
1186 V.first == Instruction::Shl || V.first == Instruction::Shr)
1187 return new BinaryConstantExpr(V.first, V.second[0], V.second[1]);
1188 if (V.first == Instruction::Select)
1189 return new SelectConstantExpr(V.second[0], V.second[1], V.second[2]);
1191 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
1193 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
1194 return new GetElementPtrConstantExpr(V.second[0], IdxList, Ty);
1199 struct ConvertConstantType<ConstantExpr, Type> {
1200 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1202 switch (OldC->getOpcode()) {
1203 case Instruction::Cast:
1204 New = ConstantExpr::getCast(OldC->getOperand(0), NewTy);
1206 case Instruction::Select:
1207 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1208 OldC->getOperand(1),
1209 OldC->getOperand(2));
1211 case Instruction::Shl:
1212 case Instruction::Shr:
1213 New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
1214 OldC->getOperand(0), OldC->getOperand(1));
1217 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
1218 OldC->getOpcode() < Instruction::BinaryOpsEnd);
1219 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1220 OldC->getOperand(1));
1222 case Instruction::GetElementPtr:
1223 // Make everyone now use a constant of the new type...
1224 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
1225 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), Idx);
1229 assert(New != OldC && "Didn't replace constant??");
1230 OldC->uncheckedReplaceAllUsesWith(New);
1231 OldC->destroyConstant(); // This constant is now dead, destroy it.
1234 } // end namespace llvm
1237 static ExprMapKeyType getValType(ConstantExpr *CE) {
1238 std::vector<Constant*> Operands;
1239 Operands.reserve(CE->getNumOperands());
1240 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1241 Operands.push_back(cast<Constant>(CE->getOperand(i)));
1242 return ExprMapKeyType(CE->getOpcode(), Operands);
1245 static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
1247 Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
1248 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1250 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
1251 return FC; // Fold a few common cases...
1253 // Look up the constant in the table first to ensure uniqueness
1254 std::vector<Constant*> argVec(1, C);
1255 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
1256 return ExprConstants.getOrCreate(Ty, Key);
1259 Constant *ConstantExpr::getSignExtend(Constant *C, const Type *Ty) {
1260 assert(C->getType()->isIntegral() && Ty->isIntegral() &&
1261 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1262 "This is an illegal sign extension!");
1263 if (C->getType() != Type::BoolTy) {
1264 C = ConstantExpr::getCast(C, C->getType()->getSignedVersion());
1265 return ConstantExpr::getCast(C, Ty);
1267 if (C == ConstantBool::True)
1268 return ConstantIntegral::getAllOnesValue(Ty);
1270 return ConstantIntegral::getNullValue(Ty);
1274 Constant *ConstantExpr::getZeroExtend(Constant *C, const Type *Ty) {
1275 assert(C->getType()->isIntegral() && Ty->isIntegral() &&
1276 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1277 "This is an illegal zero extension!");
1278 if (C->getType() != Type::BoolTy)
1279 C = ConstantExpr::getCast(C, C->getType()->getUnsignedVersion());
1280 return ConstantExpr::getCast(C, Ty);
1283 Constant *ConstantExpr::getSizeOf(const Type *Ty) {
1284 // sizeof is implemented as: (ulong) gep (Ty*)null, 1
1286 getGetElementPtr(getNullValue(PointerType::get(Ty)),
1287 std::vector<Constant*>(1, ConstantInt::get(Type::UIntTy, 1))),
1291 Constant *ConstantExpr::getPtrPtrFromArrayPtr(Constant *C) {
1292 // pointer from array is implemented as: getelementptr arr ptr, 0, 0
1293 static std::vector<Constant*> Indices(2, ConstantUInt::get(Type::UIntTy, 0));
1295 return ConstantExpr::getGetElementPtr(C, Indices);
1298 Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
1299 Constant *C1, Constant *C2) {
1300 if (Opcode == Instruction::Shl || Opcode == Instruction::Shr)
1301 return getShiftTy(ReqTy, Opcode, C1, C2);
1302 // Check the operands for consistency first
1303 assert((Opcode >= Instruction::BinaryOpsBegin &&
1304 Opcode < Instruction::BinaryOpsEnd) &&
1305 "Invalid opcode in binary constant expression");
1306 assert(C1->getType() == C2->getType() &&
1307 "Operand types in binary constant expression should match");
1309 if (ReqTy == C1->getType() || (Instruction::isRelational(Opcode) &&
1310 ReqTy == Type::BoolTy))
1311 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1312 return FC; // Fold a few common cases...
1314 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
1315 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
1316 return ExprConstants.getOrCreate(ReqTy, Key);
1319 Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
1322 case Instruction::Add: case Instruction::Sub:
1323 case Instruction::Mul: case Instruction::Div:
1324 case Instruction::Rem:
1325 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1326 assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint()) &&
1327 "Tried to create an arithmetic operation on a non-arithmetic type!");
1329 case Instruction::And:
1330 case Instruction::Or:
1331 case Instruction::Xor:
1332 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1333 assert(C1->getType()->isIntegral() &&
1334 "Tried to create a logical operation on a non-integral type!");
1336 case Instruction::SetLT: case Instruction::SetGT: case Instruction::SetLE:
1337 case Instruction::SetGE: case Instruction::SetEQ: case Instruction::SetNE:
1338 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1340 case Instruction::Shl:
1341 case Instruction::Shr:
1342 assert(C2->getType() == Type::UByteTy && "Shift should be by ubyte!");
1343 assert(C1->getType()->isInteger() &&
1344 "Tried to create a shift operation on a non-integer type!");
1351 if (Instruction::isRelational(Opcode))
1352 return getTy(Type::BoolTy, Opcode, C1, C2);
1354 return getTy(C1->getType(), Opcode, C1, C2);
1357 Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
1358 Constant *V1, Constant *V2) {
1359 assert(C->getType() == Type::BoolTy && "Select condition must be bool!");
1360 assert(V1->getType() == V2->getType() && "Select value types must match!");
1361 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
1363 if (ReqTy == V1->getType())
1364 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1365 return SC; // Fold common cases
1367 std::vector<Constant*> argVec(3, C);
1370 ExprMapKeyType Key = std::make_pair(Instruction::Select, argVec);
1371 return ExprConstants.getOrCreate(ReqTy, Key);
1374 /// getShiftTy - Return a shift left or shift right constant expr
1375 Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
1376 Constant *C1, Constant *C2) {
1377 // Check the operands for consistency first
1378 assert((Opcode == Instruction::Shl ||
1379 Opcode == Instruction::Shr) &&
1380 "Invalid opcode in binary constant expression");
1381 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
1382 "Invalid operand types for Shift constant expr!");
1384 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1385 return FC; // Fold a few common cases...
1387 // Look up the constant in the table first to ensure uniqueness
1388 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
1389 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
1390 return ExprConstants.getOrCreate(ReqTy, Key);
1394 Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
1395 const std::vector<Value*> &IdxList) {
1396 assert(GetElementPtrInst::getIndexedType(C->getType(), IdxList, true) &&
1397 "GEP indices invalid!");
1399 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
1400 return FC; // Fold a few common cases...
1402 assert(isa<PointerType>(C->getType()) &&
1403 "Non-pointer type for constant GetElementPtr expression");
1404 // Look up the constant in the table first to ensure uniqueness
1405 std::vector<Constant*> ArgVec;
1406 ArgVec.reserve(IdxList.size()+1);
1407 ArgVec.push_back(C);
1408 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
1409 ArgVec.push_back(cast<Constant>(IdxList[i]));
1410 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,ArgVec);
1411 return ExprConstants.getOrCreate(ReqTy, Key);
1414 Constant *ConstantExpr::getGetElementPtr(Constant *C,
1415 const std::vector<Constant*> &IdxList){
1416 // Get the result type of the getelementptr!
1417 std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
1419 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
1421 assert(Ty && "GEP indices invalid!");
1422 return getGetElementPtrTy(PointerType::get(Ty), C, VIdxList);
1425 Constant *ConstantExpr::getGetElementPtr(Constant *C,
1426 const std::vector<Value*> &IdxList) {
1427 // Get the result type of the getelementptr!
1428 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
1430 assert(Ty && "GEP indices invalid!");
1431 return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
1435 // destroyConstant - Remove the constant from the constant table...
1437 void ConstantExpr::destroyConstant() {
1438 ExprConstants.remove(this);
1439 destroyConstantImpl();
1442 const char *ConstantExpr::getOpcodeName() const {
1443 return Instruction::getOpcodeName(getOpcode());
1446 /// clearAllValueMaps - This method frees all internal memory used by the
1447 /// constant subsystem, which can be used in environments where this memory
1448 /// is otherwise reported as a leak.
1449 void Constant::clearAllValueMaps() {
1450 std::vector<Constant *> Constants;
1452 DoubleConstants.clear(Constants);
1453 FloatConstants.clear(Constants);
1454 SIntConstants.clear(Constants);
1455 UIntConstants.clear(Constants);
1456 AggZeroConstants.clear(Constants);
1457 ArrayConstants.clear(Constants);
1458 StructConstants.clear(Constants);
1459 PackedConstants.clear(Constants);
1460 NullPtrConstants.clear(Constants);
1461 UndefValueConstants.clear(Constants);
1462 ExprConstants.clear(Constants);
1464 for (std::vector<Constant *>::iterator I = Constants.begin(),
1465 E = Constants.end(); I != E; ++I)
1466 (*I)->dropAllReferences();
1467 for (std::vector<Constant *>::iterator I = Constants.begin(),
1468 E = Constants.end(); I != E; ++I)
1469 (*I)->destroyConstantImpl();