1 //===-- Constants.cpp - Implement Constant nodes --------------------------===//
3 // This file implements the Constant* classes...
5 //===----------------------------------------------------------------------===//
7 #include "llvm/Constants.h"
8 #include "llvm/ConstantHandling.h"
9 #include "llvm/DerivedTypes.h"
10 #include "llvm/iMemory.h"
11 #include "llvm/SymbolTable.h"
12 #include "llvm/Module.h"
13 #include "Support/StringExtras.h"
16 ConstantBool *ConstantBool::True = new ConstantBool(true);
17 ConstantBool *ConstantBool::False = new ConstantBool(false);
20 //===----------------------------------------------------------------------===//
22 //===----------------------------------------------------------------------===//
24 // Specialize setName to take care of symbol table majik
25 void Constant::setName(const std::string &Name, SymbolTable *ST) {
26 assert(ST && "Type::setName - Must provide symbol table argument!");
28 if (Name.size()) ST->insert(Name, this);
31 void Constant::destroyConstantImpl() {
32 // When a Constant is destroyed, there may be lingering
33 // references to the constant by other constants in the constant pool. These
34 // constants are implicitly dependant on the module that is being deleted,
35 // but they don't know that. Because we only find out when the CPV is
36 // deleted, we must now notify all of our users (that should only be
37 // Constants) that they are, in fact, invalid now and should be deleted.
39 while (!use_empty()) {
40 Value *V = use_back();
41 #ifndef NDEBUG // Only in -g mode...
42 if (!isa<Constant>(V))
43 std::cerr << "While deleting: " << *this
44 << "\n\nUse still stuck around after Def is destroyed: "
47 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
48 Constant *CPV = cast<Constant>(V);
49 CPV->destroyConstant();
51 // The constant should remove itself from our use list...
52 assert((use_empty() || use_back() != V) && "Constant not removed!");
55 // Value has no outstanding references it is safe to delete it now...
59 // Static constructor to create a '0' constant of arbitrary type...
60 Constant *Constant::getNullValue(const Type *Ty) {
61 switch (Ty->getPrimitiveID()) {
62 case Type::BoolTyID: return ConstantBool::get(false);
66 case Type::LongTyID: return ConstantSInt::get(Ty, 0);
69 case Type::UShortTyID:
71 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
74 case Type::DoubleTyID: return ConstantFP::get(Ty, 0);
76 case Type::PointerTyID:
77 return ConstantPointerNull::get(cast<PointerType>(Ty));
78 case Type::StructTyID: {
79 const StructType *ST = cast<StructType>(Ty);
81 const StructType::ElementTypes &ETs = ST->getElementTypes();
82 std::vector<Constant*> Elements;
83 Elements.resize(ETs.size());
84 for (unsigned i = 0, e = ETs.size(); i != e; ++i)
85 Elements[i] = Constant::getNullValue(ETs[i]);
86 return ConstantStruct::get(ST, Elements);
88 case Type::ArrayTyID: {
89 const ArrayType *AT = cast<ArrayType>(Ty);
90 Constant *El = Constant::getNullValue(AT->getElementType());
91 unsigned NumElements = AT->getNumElements();
92 return ConstantArray::get(AT, std::vector<Constant*>(NumElements, El));
95 // Function, Type, Label, or Opaque type?
96 assert(0 && "Cannot create a null constant of that type!");
101 // Static constructor to create the maximum constant of an integral type...
102 ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
103 switch (Ty->getPrimitiveID()) {
104 case Type::BoolTyID: return ConstantBool::True;
105 case Type::SByteTyID:
106 case Type::ShortTyID:
108 case Type::LongTyID: {
109 // Calculate 011111111111111...
110 unsigned TypeBits = Ty->getPrimitiveSize()*8;
111 int64_t Val = INT64_MAX; // All ones
112 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
113 return ConstantSInt::get(Ty, Val);
116 case Type::UByteTyID:
117 case Type::UShortTyID:
119 case Type::ULongTyID: return getAllOnesValue(Ty);
125 // Static constructor to create the minimum constant for an integral type...
126 ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
127 switch (Ty->getPrimitiveID()) {
128 case Type::BoolTyID: return ConstantBool::False;
129 case Type::SByteTyID:
130 case Type::ShortTyID:
132 case Type::LongTyID: {
133 // Calculate 1111111111000000000000
134 unsigned TypeBits = Ty->getPrimitiveSize()*8;
135 int64_t Val = -1; // All ones
136 Val <<= TypeBits-1; // Shift over to the right spot
137 return ConstantSInt::get(Ty, Val);
140 case Type::UByteTyID:
141 case Type::UShortTyID:
143 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
149 // Static constructor to create an integral constant with all bits set
150 ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
151 switch (Ty->getPrimitiveID()) {
152 case Type::BoolTyID: return ConstantBool::True;
153 case Type::SByteTyID:
154 case Type::ShortTyID:
156 case Type::LongTyID: return ConstantSInt::get(Ty, -1);
158 case Type::UByteTyID:
159 case Type::UShortTyID:
161 case Type::ULongTyID: {
162 // Calculate ~0 of the right type...
163 unsigned TypeBits = Ty->getPrimitiveSize()*8;
164 uint64_t Val = ~0ULL; // All ones
165 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
166 return ConstantUInt::get(Ty, Val);
172 bool ConstantUInt::isAllOnesValue() const {
173 unsigned TypeBits = getType()->getPrimitiveSize()*8;
174 uint64_t Val = ~0ULL; // All ones
175 Val >>= 64-TypeBits; // Shift out inappropriate bits
176 return getValue() == Val;
180 //===----------------------------------------------------------------------===//
181 // ConstantXXX Classes
182 //===----------------------------------------------------------------------===//
184 //===----------------------------------------------------------------------===//
185 // Normal Constructors
187 ConstantBool::ConstantBool(bool V) : ConstantIntegral(Type::BoolTy) {
191 ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : ConstantIntegral(Ty) {
195 ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
196 assert(Ty->isInteger() && Ty->isSigned() &&
197 "Illegal type for unsigned integer constant!");
198 assert(isValueValidForType(Ty, V) && "Value too large for type!");
201 ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
202 assert(Ty->isInteger() && Ty->isUnsigned() &&
203 "Illegal type for unsigned integer constant!");
204 assert(isValueValidForType(Ty, V) && "Value too large for type!");
207 ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
208 assert(isValueValidForType(Ty, V) && "Value too large for type!");
212 ConstantArray::ConstantArray(const ArrayType *T,
213 const std::vector<Constant*> &V) : Constant(T) {
214 Operands.reserve(V.size());
215 for (unsigned i = 0, e = V.size(); i != e; ++i) {
216 assert(V[i]->getType() == T->getElementType());
217 Operands.push_back(Use(V[i], this));
221 ConstantStruct::ConstantStruct(const StructType *T,
222 const std::vector<Constant*> &V) : Constant(T) {
223 const StructType::ElementTypes &ETypes = T->getElementTypes();
224 assert(V.size() == ETypes.size() &&
225 "Invalid initializer vector for constant structure");
226 Operands.reserve(V.size());
227 for (unsigned i = 0, e = V.size(); i != e; ++i) {
228 assert(V[i]->getType() == ETypes[i] &&
229 "Initializer for struct element doesn't match struct element type!");
230 Operands.push_back(Use(V[i], this));
234 ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
235 : ConstantPointer(GV->getType()) {
236 Operands.push_back(Use(GV, this));
239 ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
240 : Constant(Ty), iType(Opcode) {
241 Operands.push_back(Use(C, this));
244 ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
245 : Constant(C1->getType()), iType(Opcode) {
246 Operands.push_back(Use(C1, this));
247 Operands.push_back(Use(C2, this));
250 ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
252 : Constant(DestTy), iType(Instruction::GetElementPtr) {
253 Operands.reserve(1+IdxList.size());
254 Operands.push_back(Use(C, this));
255 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
256 Operands.push_back(Use(IdxList[i], this));
261 //===----------------------------------------------------------------------===//
262 // classof implementations
264 bool ConstantIntegral::classof(const Constant *CPV) {
265 return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
268 bool ConstantInt::classof(const Constant *CPV) {
269 return CPV->getType()->isInteger() && !isa<ConstantExpr>(CPV);
271 bool ConstantSInt::classof(const Constant *CPV) {
272 return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
274 bool ConstantUInt::classof(const Constant *CPV) {
275 return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV);
277 bool ConstantFP::classof(const Constant *CPV) {
278 const Type *Ty = CPV->getType();
279 return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
280 !isa<ConstantExpr>(CPV));
282 bool ConstantArray::classof(const Constant *CPV) {
283 return isa<ArrayType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
285 bool ConstantStruct::classof(const Constant *CPV) {
286 return isa<StructType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
288 bool ConstantPointer::classof(const Constant *CPV) {
289 return (isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV));
294 //===----------------------------------------------------------------------===//
295 // isValueValidForType implementations
297 bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
298 switch (Ty->getPrimitiveID()) {
300 return false; // These can't be represented as integers!!!
303 case Type::SByteTyID:
304 return (Val <= INT8_MAX && Val >= INT8_MIN);
305 case Type::ShortTyID:
306 return (Val <= INT16_MAX && Val >= INT16_MIN);
308 return (Val <= INT32_MAX && Val >= INT32_MIN);
310 return true; // This is the largest type...
316 bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
317 switch (Ty->getPrimitiveID()) {
319 return false; // These can't be represented as integers!!!
322 case Type::UByteTyID:
323 return (Val <= UINT8_MAX);
324 case Type::UShortTyID:
325 return (Val <= UINT16_MAX);
327 return (Val <= UINT32_MAX);
328 case Type::ULongTyID:
329 return true; // This is the largest type...
335 bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
336 switch (Ty->getPrimitiveID()) {
338 return false; // These can't be represented as floating point!
340 // TODO: Figure out how to test if a double can be cast to a float!
341 case Type::FloatTyID:
343 return (Val <= UINT8_MAX);
345 case Type::DoubleTyID:
346 return true; // This is the largest type...
350 //===----------------------------------------------------------------------===//
351 // replaceUsesOfWithOnConstant implementations
353 void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To) {
354 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
356 std::vector<Constant*> Values;
357 Values.reserve(getValues().size()); // Build replacement array...
358 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
359 Constant *Val = cast<Constant>(getValues()[i]);
360 if (Val == From) Val = cast<Constant>(To);
361 Values.push_back(Val);
364 ConstantArray *Replacement = ConstantArray::get(getType(), Values);
365 assert(Replacement != this && "I didn't contain From!");
367 // Everyone using this now uses the replacement...
368 replaceAllUsesWith(Replacement);
370 // Delete the old constant!
374 void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To) {
375 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
377 std::vector<Constant*> Values;
378 Values.reserve(getValues().size());
379 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
380 Constant *Val = cast<Constant>(getValues()[i]);
381 if (Val == From) Val = cast<Constant>(To);
382 Values.push_back(Val);
385 ConstantStruct *Replacement = ConstantStruct::get(getType(), Values);
386 assert(Replacement != this && "I didn't contain From!");
388 // Everyone using this now uses the replacement...
389 replaceAllUsesWith(Replacement);
391 // Delete the old constant!
395 void ConstantPointerRef::replaceUsesOfWithOnConstant(Value *From, Value *To) {
396 if (isa<GlobalValue>(To)) {
397 assert(From == getOperand(0) && "Doesn't contain from!");
398 ConstantPointerRef *Replacement =
399 ConstantPointerRef::get(cast<GlobalValue>(To));
401 // Everyone using this now uses the replacement...
402 replaceAllUsesWith(Replacement);
404 // Delete the old constant!
407 // Just replace ourselves with the To value specified.
408 replaceAllUsesWith(To);
410 // Delete the old constant!
415 void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV) {
416 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
417 Constant *To = cast<Constant>(ToV);
419 Constant *Replacement = 0;
420 if (getOpcode() == Instruction::GetElementPtr) {
421 std::vector<Constant*> Indices;
422 Constant *Pointer = getOperand(0);
423 Indices.reserve(getNumOperands()-1);
424 if (Pointer == From) Pointer = To;
426 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
427 Constant *Val = getOperand(i);
428 if (Val == From) Val = To;
429 Indices.push_back(Val);
431 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
432 } else if (getOpcode() == Instruction::Cast) {
433 assert(getOperand(0) == From && "Cast only has one use!");
434 Replacement = ConstantExpr::getCast(To, getType());
435 } else if (getNumOperands() == 2) {
436 Constant *C1 = getOperand(0);
437 Constant *C2 = getOperand(1);
438 if (C1 == From) C1 = To;
439 if (C2 == From) C2 = To;
440 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
442 assert(0 && "Unknown ConstantExpr type!");
446 assert(Replacement != this && "I didn't contain From!");
448 // Everyone using this now uses the replacement...
449 replaceAllUsesWith(Replacement);
451 // Delete the old constant!
455 //===----------------------------------------------------------------------===//
456 // Factory Function Implementation
458 // ConstantCreator - A class that is used to create constants by
459 // ValueMap*. This class should be partially specialized if there is
460 // something strange that needs to be done to interface to the ctor for the
463 template<class ConstantClass, class TypeClass, class ValType>
464 struct ConstantCreator {
465 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
466 return new ConstantClass(Ty, V);
471 template<class ValType, class TypeClass, class ConstantClass>
474 typedef std::pair<const TypeClass*, ValType> ConstHashKey;
475 std::map<ConstHashKey, ConstantClass *> Map;
477 // getOrCreate - Return the specified constant from the map, creating it if
479 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
480 ConstHashKey Lookup(Ty, V);
481 typename std::map<ConstHashKey,ConstantClass *>::iterator I =
482 Map.lower_bound(Lookup);
483 if (I != Map.end() && I->first == Lookup)
484 return I->second; // Is it in the map?
486 // If no preexisting value, create one now...
487 ConstantClass *Result =
488 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
490 Map.insert(I, std::make_pair(ConstHashKey(Ty, V), Result));
494 void remove(ConstantClass *CP) {
495 // FIXME: This could be sped up a LOT. If this gets to be a performance
496 // problem, someone should look at this.
497 for (typename std::map<ConstHashKey, ConstantClass*>::iterator
498 I = Map.begin(), E = Map.end(); I != E; ++I)
499 if (I->second == CP) {
503 assert(0 && "Constant not found in constant table!");
510 //---- ConstantUInt::get() and ConstantSInt::get() implementations...
512 static ValueMap< int64_t, Type, ConstantSInt> SIntConstants;
513 static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants;
515 ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
516 return SIntConstants.getOrCreate(Ty, V);
519 ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
520 return UIntConstants.getOrCreate(Ty, V);
523 ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
524 assert(V <= 127 && "Can only be used with very small positive constants!");
525 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
526 return ConstantUInt::get(Ty, V);
529 //---- ConstantFP::get() implementation...
531 static ValueMap<double, Type, ConstantFP> FPConstants;
533 ConstantFP *ConstantFP::get(const Type *Ty, double V) {
534 return FPConstants.getOrCreate(Ty, V);
537 //---- ConstantArray::get() implementation...
539 static ValueMap<std::vector<Constant*>, ArrayType,
540 ConstantArray> ArrayConstants;
542 ConstantArray *ConstantArray::get(const ArrayType *Ty,
543 const std::vector<Constant*> &V) {
544 return ArrayConstants.getOrCreate(Ty, V);
547 // destroyConstant - Remove the constant from the constant table...
549 void ConstantArray::destroyConstant() {
550 ArrayConstants.remove(this);
551 destroyConstantImpl();
554 /// refineAbstractType - If this callback is invoked, then this constant is of a
555 /// derived type, change all users to use a concrete constant of the new type.
557 void ConstantArray::refineAbstractType(const DerivedType *OldTy,
559 Value::refineAbstractType(OldTy, NewTy);
560 if (OldTy == NewTy) return;
562 // Make everyone now use a constant of the new type...
563 std::vector<Constant*> C;
564 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
565 C.push_back(cast<Constant>(getOperand(i)));
566 replaceAllUsesWith(ConstantArray::get(cast<ArrayType>(NewTy),
568 destroyConstant(); // This constant is now dead, destroy it.
572 // ConstantArray::get(const string&) - Return an array that is initialized to
573 // contain the specified string. A null terminator is added to the specified
574 // string so that it may be used in a natural way...
576 ConstantArray *ConstantArray::get(const std::string &Str) {
577 std::vector<Constant*> ElementVals;
579 for (unsigned i = 0; i < Str.length(); ++i)
580 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
582 // Add a null terminator to the string...
583 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
585 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
586 return ConstantArray::get(ATy, ElementVals);
589 // getAsString - If the sub-element type of this array is either sbyte or ubyte,
590 // then this method converts the array to an std::string and returns it.
591 // Otherwise, it asserts out.
593 std::string ConstantArray::getAsString() const {
595 if (getType()->getElementType() == Type::SByteTy)
596 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
597 Result += (char)cast<ConstantSInt>(getOperand(i))->getValue();
599 assert(getType()->getElementType() == Type::UByteTy && "Not a string!");
600 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
601 Result += (char)cast<ConstantUInt>(getOperand(i))->getValue();
607 //---- ConstantStruct::get() implementation...
609 static ValueMap<std::vector<Constant*>, StructType,
610 ConstantStruct> StructConstants;
612 ConstantStruct *ConstantStruct::get(const StructType *Ty,
613 const std::vector<Constant*> &V) {
614 return StructConstants.getOrCreate(Ty, V);
617 // destroyConstant - Remove the constant from the constant table...
619 void ConstantStruct::destroyConstant() {
620 StructConstants.remove(this);
621 destroyConstantImpl();
624 /// refineAbstractType - If this callback is invoked, then this constant is of a
625 /// derived type, change all users to use a concrete constant of the new type.
627 void ConstantStruct::refineAbstractType(const DerivedType *OldTy,
629 Value::refineAbstractType(OldTy, NewTy);
630 if (OldTy == NewTy) return;
632 // Make everyone now use a constant of the new type...
633 std::vector<Constant*> C;
634 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
635 C.push_back(cast<Constant>(getOperand(i)));
636 replaceAllUsesWith(ConstantStruct::get(cast<StructType>(NewTy),
638 destroyConstant(); // This constant is now dead, destroy it.
642 //---- ConstantPointerNull::get() implementation...
645 // ConstantPointerNull does not take extra "value" argument...
646 template<class ValType>
647 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
648 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
649 return new ConstantPointerNull(Ty);
653 static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
655 ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
656 return NullPtrConstants.getOrCreate(Ty, 0);
659 // destroyConstant - Remove the constant from the constant table...
661 void ConstantPointerNull::destroyConstant() {
662 NullPtrConstants.remove(this);
663 destroyConstantImpl();
666 /// refineAbstractType - If this callback is invoked, then this constant is of a
667 /// derived type, change all users to use a concrete constant of the new type.
669 void ConstantPointerNull::refineAbstractType(const DerivedType *OldTy,
671 Value::refineAbstractType(OldTy, NewTy);
672 if (OldTy == NewTy) return;
674 // Make everyone now use a constant of the new type...
675 replaceAllUsesWith(ConstantPointerNull::get(cast<PointerType>(NewTy)));
677 // This constant is now dead, destroy it.
683 //---- ConstantPointerRef::get() implementation...
685 ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
686 assert(GV->getParent() && "Global Value must be attached to a module!");
688 // The Module handles the pointer reference sharing...
689 return GV->getParent()->getConstantPointerRef(GV);
692 // destroyConstant - Remove the constant from the constant table...
694 void ConstantPointerRef::destroyConstant() {
695 getValue()->getParent()->destroyConstantPointerRef(this);
696 destroyConstantImpl();
700 //---- ConstantExpr::get() implementations...
702 typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
705 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
706 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
707 if (V.first == Instruction::Cast)
708 return new ConstantExpr(Instruction::Cast, V.second[0], Ty);
709 if ((V.first >= Instruction::BinaryOpsBegin &&
710 V.first < Instruction::BinaryOpsEnd) ||
711 V.first == Instruction::Shl || V.first == Instruction::Shr)
712 return new ConstantExpr(V.first, V.second[0], V.second[1]);
714 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
716 // Check that the indices list is valid...
717 std::vector<Value*> ValIdxList(V.second.begin()+1, V.second.end());
718 const Type *DestTy = GetElementPtrInst::getIndexedType(Ty, ValIdxList,
720 assert(DestTy && "Invalid index list for GetElementPtr expression");
722 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
723 return new ConstantExpr(V.second[0], IdxList, PointerType::get(DestTy));
727 static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
729 Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
730 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
731 return FC; // Fold a few common cases...
733 // Look up the constant in the table first to ensure uniqueness
734 std::vector<Constant*> argVec(1, C);
735 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
736 return ExprConstants.getOrCreate(Ty, Key);
739 Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
740 // Check the operands for consistency first
741 assert((Opcode >= Instruction::BinaryOpsBegin &&
742 Opcode < Instruction::BinaryOpsEnd) &&
743 "Invalid opcode in binary constant expression");
744 assert(C1->getType() == C2->getType() &&
745 "Operand types in binary constant expression should match");
747 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
748 return FC; // Fold a few common cases...
750 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
751 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
752 return ExprConstants.getOrCreate(C1->getType(), Key);
755 /// getShift - Return a shift left or shift right constant expr
756 Constant *ConstantExpr::getShift(unsigned Opcode, Constant *C1, Constant *C2) {
757 // Check the operands for consistency first
758 assert((Opcode == Instruction::Shl ||
759 Opcode == Instruction::Shr) &&
760 "Invalid opcode in binary constant expression");
761 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
762 "Invalid operand types for Shift constant expr!");
764 if (Constant *FC = ConstantFoldShiftInstruction(Opcode, C1, C2))
765 return FC; // Fold a few common cases...
767 // Look up the constant in the table first to ensure uniqueness
768 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
769 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
770 return ExprConstants.getOrCreate(C1->getType(), Key);
774 Constant *ConstantExpr::getGetElementPtr(Constant *C,
775 const std::vector<Constant*> &IdxList){
776 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
777 return FC; // Fold a few common cases...
778 const Type *Ty = C->getType();
779 assert(isa<PointerType>(Ty) &&
780 "Non-pointer type for constant GetElementPtr expression");
782 // Look up the constant in the table first to ensure uniqueness
783 std::vector<Constant*> argVec(1, C);
784 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
786 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,argVec);
787 return ExprConstants.getOrCreate(Ty, Key);
790 // destroyConstant - Remove the constant from the constant table...
792 void ConstantExpr::destroyConstant() {
793 ExprConstants.remove(this);
794 destroyConstantImpl();
797 /// refineAbstractType - If this callback is invoked, then this constant is of a
798 /// derived type, change all users to use a concrete constant of the new type.
800 void ConstantExpr::refineAbstractType(const DerivedType *OldTy,
802 Value::refineAbstractType(OldTy, NewTy);
803 if (OldTy == NewTy) return;
805 // FIXME: These need to use a lower-level implementation method, because the
806 // ::get methods intuit the type of the result based on the types of the
807 // operands. The operand types may not have had their types resolved yet.
809 if (getOpcode() == Instruction::Cast) {
810 replaceAllUsesWith(getCast(getOperand(0), NewTy));
811 } else if (getOpcode() >= Instruction::BinaryOpsBegin &&
812 getOpcode() < Instruction::BinaryOpsEnd) {
813 replaceAllUsesWith(get(getOpcode(), getOperand(0), getOperand(0)));
814 } else if (getOpcode() == Instruction::Shl || getOpcode() ==Instruction::Shr){
815 replaceAllUsesWith(getShift(getOpcode(), getOperand(0), getOperand(0)));
817 assert(getOpcode() == Instruction::GetElementPtr);
819 // Make everyone now use a constant of the new type...
820 std::vector<Constant*> C;
821 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
822 C.push_back(cast<Constant>(getOperand(i)));
823 replaceAllUsesWith(ConstantExpr::getGetElementPtr(getOperand(0),
826 destroyConstant(); // This constant is now dead, destroy it.
832 const char *ConstantExpr::getOpcodeName() const {
833 return Instruction::getOpcodeName(getOpcode());
836 unsigned Constant::mutateReferences(Value *OldV, Value *NewV) {
837 // Uses of constant pointer refs are global values, not constants!
838 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
839 GlobalValue *NewGV = cast<GlobalValue>(NewV);
840 GlobalValue *OldGV = CPR->getValue();
842 assert(OldGV == OldV && "Cannot mutate old value if I'm not using it!");
844 OldGV->getParent()->mutateConstantPointerRef(OldGV, NewGV);
847 Constant *NewC = cast<Constant>(NewV);
848 unsigned NumReplaced = 0;
849 for (unsigned i = 0, N = getNumOperands(); i != N; ++i)
850 if (Operands[i] == OldV) {