1 //===-- Constants.cpp - Implement Constant nodes -----------------*- C++ -*--=//
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"
21 ConstantBool *ConstantBool::True = new ConstantBool(true);
22 ConstantBool *ConstantBool::False = new ConstantBool(false);
25 //===----------------------------------------------------------------------===//
27 //===----------------------------------------------------------------------===//
29 // Specialize setName to take care of symbol table majik
30 void Constant::setName(const std::string &Name, SymbolTable *ST) {
31 assert(ST && "Type::setName - Must provide symbol table argument!");
33 if (Name.size()) ST->insert(Name, this);
36 void Constant::destroyConstantImpl() {
37 // When a Constant is destroyed, there may be lingering
38 // references to the constant by other constants in the constant pool. These
39 // constants are implicitly dependant on the module that is being deleted,
40 // but they don't know that. Because we only find out when the CPV is
41 // deleted, we must now notify all of our users (that should only be
42 // Constants) that they are, in fact, invalid now and should be deleted.
44 while (!use_empty()) {
45 Value *V = use_back();
46 #ifndef NDEBUG // Only in -g mode...
47 if (!isa<Constant>(V))
48 std::cerr << "While deleting: " << *this
49 << "\n\nUse still stuck around after Def is destroyed: "
52 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
53 Constant *CPV = cast<Constant>(V);
54 CPV->destroyConstant();
56 // The constant should remove itself from our use list...
57 assert((use_empty() || use_back() != V) && "Constant not removed!");
60 // Value has no outstanding references it is safe to delete it now...
64 // Static constructor to create a '0' constant of arbitrary type...
65 Constant *Constant::getNullValue(const Type *Ty) {
66 switch (Ty->getPrimitiveID()) {
67 case Type::BoolTyID: return ConstantBool::get(false);
71 case Type::LongTyID: return ConstantSInt::get(Ty, 0);
74 case Type::UShortTyID:
76 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
79 case Type::DoubleTyID: return ConstantFP::get(Ty, 0);
81 case Type::PointerTyID:
82 return ConstantPointerNull::get(cast<PointerType>(Ty));
83 case Type::StructTyID: {
84 const StructType *ST = cast<StructType>(Ty);
86 const StructType::ElementTypes &ETs = ST->getElementTypes();
87 std::vector<Constant*> Elements;
88 Elements.resize(ETs.size());
89 for (unsigned i = 0, e = ETs.size(); i != e; ++i)
90 Elements[i] = Constant::getNullValue(ETs[i]);
91 return ConstantStruct::get(ST, Elements);
93 case Type::ArrayTyID: {
94 const ArrayType *AT = cast<ArrayType>(Ty);
95 Constant *El = Constant::getNullValue(AT->getElementType());
96 unsigned NumElements = AT->getNumElements();
97 return ConstantArray::get(AT, std::vector<Constant*>(NumElements, El));
100 // Function, Type, Label, or Opaque type?
101 assert(0 && "Cannot create a null constant of that type!");
106 // Static constructor to create the maximum constant of an integral type...
107 ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
108 switch (Ty->getPrimitiveID()) {
109 case Type::BoolTyID: return ConstantBool::True;
110 case Type::SByteTyID:
111 case Type::ShortTyID:
113 case Type::LongTyID: {
114 // Calculate 011111111111111...
115 unsigned TypeBits = Ty->getPrimitiveSize()*8;
116 int64_t Val = INT64_MAX; // All ones
117 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
118 return ConstantSInt::get(Ty, Val);
121 case Type::UByteTyID:
122 case Type::UShortTyID:
124 case Type::ULongTyID: return getAllOnesValue(Ty);
130 // Static constructor to create the minimum constant for an integral type...
131 ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
132 switch (Ty->getPrimitiveID()) {
133 case Type::BoolTyID: return ConstantBool::False;
134 case Type::SByteTyID:
135 case Type::ShortTyID:
137 case Type::LongTyID: {
138 // Calculate 1111111111000000000000
139 unsigned TypeBits = Ty->getPrimitiveSize()*8;
140 int64_t Val = -1; // All ones
141 Val <<= TypeBits-1; // Shift over to the right spot
142 return ConstantSInt::get(Ty, Val);
145 case Type::UByteTyID:
146 case Type::UShortTyID:
148 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
154 // Static constructor to create an integral constant with all bits set
155 ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
156 switch (Ty->getPrimitiveID()) {
157 case Type::BoolTyID: return ConstantBool::True;
158 case Type::SByteTyID:
159 case Type::ShortTyID:
161 case Type::LongTyID: return ConstantSInt::get(Ty, -1);
163 case Type::UByteTyID:
164 case Type::UShortTyID:
166 case Type::ULongTyID: {
167 // Calculate ~0 of the right type...
168 unsigned TypeBits = Ty->getPrimitiveSize()*8;
169 uint64_t Val = ~0ULL; // All ones
170 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
171 return ConstantUInt::get(Ty, Val);
177 bool ConstantUInt::isAllOnesValue() const {
178 unsigned TypeBits = getType()->getPrimitiveSize()*8;
179 uint64_t Val = ~0ULL; // All ones
180 Val >>= 64-TypeBits; // Shift out inappropriate bits
181 return getValue() == Val;
185 //===----------------------------------------------------------------------===//
186 // ConstantXXX Classes
187 //===----------------------------------------------------------------------===//
189 //===----------------------------------------------------------------------===//
190 // Normal Constructors
192 ConstantBool::ConstantBool(bool V) : ConstantIntegral(Type::BoolTy) {
196 ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : ConstantIntegral(Ty) {
200 ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
201 assert(Ty->isInteger() && Ty->isSigned() &&
202 "Illegal type for unsigned integer constant!");
203 assert(isValueValidForType(Ty, V) && "Value too large for type!");
206 ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
207 assert(Ty->isInteger() && Ty->isUnsigned() &&
208 "Illegal type for unsigned integer constant!");
209 assert(isValueValidForType(Ty, V) && "Value too large for type!");
212 ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
213 assert(isValueValidForType(Ty, V) && "Value too large for type!");
217 ConstantArray::ConstantArray(const ArrayType *T,
218 const std::vector<Constant*> &V) : Constant(T) {
219 Operands.reserve(V.size());
220 for (unsigned i = 0, e = V.size(); i != e; ++i) {
221 assert(V[i]->getType() == T->getElementType());
222 Operands.push_back(Use(V[i], this));
226 ConstantStruct::ConstantStruct(const StructType *T,
227 const std::vector<Constant*> &V) : Constant(T) {
228 const StructType::ElementTypes &ETypes = T->getElementTypes();
229 assert(V.size() == ETypes.size() &&
230 "Invalid initializer vector for constant structure");
231 Operands.reserve(V.size());
232 for (unsigned i = 0, e = V.size(); i != e; ++i) {
233 assert(V[i]->getType() == ETypes[i]);
234 Operands.push_back(Use(V[i], this));
238 ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
239 : ConstantPointer(GV->getType()) {
240 Operands.push_back(Use(GV, this));
243 ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
244 : Constant(Ty), iType(Opcode) {
245 Operands.push_back(Use(C, this));
248 ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
249 : Constant(C1->getType()), iType(Opcode) {
250 Operands.push_back(Use(C1, this));
251 Operands.push_back(Use(C2, this));
254 ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
256 : Constant(DestTy), iType(Instruction::GetElementPtr) {
257 Operands.reserve(1+IdxList.size());
258 Operands.push_back(Use(C, this));
259 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
260 Operands.push_back(Use(IdxList[i], this));
265 //===----------------------------------------------------------------------===//
266 // classof implementations
268 bool ConstantIntegral::classof(const Constant *CPV) {
269 return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
272 bool ConstantInt::classof(const Constant *CPV) {
273 return CPV->getType()->isInteger() && !isa<ConstantExpr>(CPV);
275 bool ConstantSInt::classof(const Constant *CPV) {
276 return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
278 bool ConstantUInt::classof(const Constant *CPV) {
279 return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV);
281 bool ConstantFP::classof(const Constant *CPV) {
282 const Type *Ty = CPV->getType();
283 return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
284 !isa<ConstantExpr>(CPV));
286 bool ConstantArray::classof(const Constant *CPV) {
287 return isa<ArrayType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
289 bool ConstantStruct::classof(const Constant *CPV) {
290 return isa<StructType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
292 bool ConstantPointer::classof(const Constant *CPV) {
293 return (isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV));
298 //===----------------------------------------------------------------------===//
299 // isValueValidForType implementations
301 bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
302 switch (Ty->getPrimitiveID()) {
304 return false; // These can't be represented as integers!!!
307 case Type::SByteTyID:
308 return (Val <= INT8_MAX && Val >= INT8_MIN);
309 case Type::ShortTyID:
310 return (Val <= INT16_MAX && Val >= INT16_MIN);
312 return (Val <= INT32_MAX && Val >= INT32_MIN);
314 return true; // This is the largest type...
320 bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
321 switch (Ty->getPrimitiveID()) {
323 return false; // These can't be represented as integers!!!
326 case Type::UByteTyID:
327 return (Val <= UINT8_MAX);
328 case Type::UShortTyID:
329 return (Val <= UINT16_MAX);
331 return (Val <= UINT32_MAX);
332 case Type::ULongTyID:
333 return true; // This is the largest type...
339 bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
340 switch (Ty->getPrimitiveID()) {
342 return false; // These can't be represented as floating point!
344 // TODO: Figure out how to test if a double can be cast to a float!
345 case Type::FloatTyID:
347 return (Val <= UINT8_MAX);
349 case Type::DoubleTyID:
350 return true; // This is the largest type...
354 //===----------------------------------------------------------------------===//
355 // replaceUsesOfWithOnConstant implementations
357 void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To) {
358 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
360 std::vector<Constant*> Values;
361 Values.reserve(getValues().size()); // Build replacement array...
362 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
363 Constant *Val = cast<Constant>(getValues()[i]);
364 if (Val == From) Val = cast<Constant>(To);
365 Values.push_back(Val);
368 ConstantArray *Replacement = ConstantArray::get(getType(), Values);
369 assert(Replacement != this && "I didn't contain From!");
371 // Everyone using this now uses the replacement...
372 replaceAllUsesWith(Replacement);
374 // Delete the old constant!
378 void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To) {
379 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
381 std::vector<Constant*> Values;
382 Values.reserve(getValues().size());
383 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
384 Constant *Val = cast<Constant>(getValues()[i]);
385 if (Val == From) Val = cast<Constant>(To);
386 Values.push_back(Val);
389 ConstantStruct *Replacement = ConstantStruct::get(getType(), Values);
390 assert(Replacement != this && "I didn't contain From!");
392 // Everyone using this now uses the replacement...
393 replaceAllUsesWith(Replacement);
395 // Delete the old constant!
399 void ConstantPointerRef::replaceUsesOfWithOnConstant(Value *From, Value *To) {
400 if (isa<GlobalValue>(To)) {
401 assert(From == getOperand(0) && "Doesn't contain from!");
402 ConstantPointerRef *Replacement =
403 ConstantPointerRef::get(cast<GlobalValue>(To));
405 // Everyone using this now uses the replacement...
406 replaceAllUsesWith(Replacement);
408 // Delete the old constant!
411 // Just replace ourselves with the To value specified.
412 replaceAllUsesWith(To);
414 // Delete the old constant!
419 void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *To) {
420 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
422 Constant *Replacement = 0;
423 if (getOpcode() == Instruction::GetElementPtr) {
424 std::vector<Constant*> Indices;
425 Constant *Pointer = cast<Constant>(getOperand(0));
426 Indices.reserve(getNumOperands()-1);
427 if (Pointer == From) Pointer = cast<Constant>(To);
429 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
430 Constant *Val = cast<Constant>(getOperand(i));
431 if (Val == From) Val = cast<Constant>(To);
432 Indices.push_back(Val);
434 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
435 } else if (getOpcode() == Instruction::Cast) {
436 assert(getOperand(0) == From && "Cast only has one use!");
437 Replacement = ConstantExpr::getCast(cast<Constant>(To), getType());
438 } else if (getNumOperands() == 2) {
439 Constant *C1 = cast<Constant>(getOperand(0));
440 Constant *C2 = cast<Constant>(getOperand(1));
441 if (C1 == From) C1 = cast<Constant>(To);
442 if (C2 == From) C2 = cast<Constant>(To);
443 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
445 assert(0 && "Unknown ConstantExpr type!");
449 assert(Replacement != this && "I didn't contain From!");
451 // Everyone using this now uses the replacement...
452 replaceAllUsesWith(Replacement);
454 // Delete the old constant!
460 //===----------------------------------------------------------------------===//
461 // Factory Function Implementation
463 template<class ValType, class ConstantClass>
465 typedef pair<const Type*, ValType> ConstHashKey;
466 map<ConstHashKey, ConstantClass *> Map;
468 inline ConstantClass *get(const Type *Ty, ValType V) {
469 typename map<ConstHashKey,ConstantClass *>::iterator I =
470 Map.find(ConstHashKey(Ty, V));
471 return (I != Map.end()) ? I->second : 0;
474 inline void add(const Type *Ty, ValType V, ConstantClass *CP) {
475 Map.insert(make_pair(ConstHashKey(Ty, V), CP));
478 inline void remove(ConstantClass *CP) {
479 for (typename map<ConstHashKey,ConstantClass *>::iterator I = Map.begin(),
480 E = Map.end(); I != E;++I)
481 if (I->second == CP) {
488 //---- ConstantUInt::get() and ConstantSInt::get() implementations...
490 static ValueMap<uint64_t, ConstantInt> IntConstants;
492 ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
493 ConstantSInt *Result = (ConstantSInt*)IntConstants.get(Ty, (uint64_t)V);
494 if (!Result) // If no preexisting value, create one now...
495 IntConstants.add(Ty, V, Result = new ConstantSInt(Ty, V));
499 ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
500 ConstantUInt *Result = (ConstantUInt*)IntConstants.get(Ty, V);
501 if (!Result) // If no preexisting value, create one now...
502 IntConstants.add(Ty, V, Result = new ConstantUInt(Ty, V));
506 ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
507 assert(V <= 127 && "Can only be used with very small positive constants!");
508 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
509 return ConstantUInt::get(Ty, V);
512 //---- ConstantFP::get() implementation...
514 static ValueMap<double, ConstantFP> FPConstants;
516 ConstantFP *ConstantFP::get(const Type *Ty, double V) {
517 ConstantFP *Result = FPConstants.get(Ty, V);
518 if (!Result) // If no preexisting value, create one now...
519 FPConstants.add(Ty, V, Result = new ConstantFP(Ty, V));
523 //---- ConstantArray::get() implementation...
525 static ValueMap<std::vector<Constant*>, ConstantArray> ArrayConstants;
527 ConstantArray *ConstantArray::get(const ArrayType *Ty,
528 const std::vector<Constant*> &V) {
529 ConstantArray *Result = ArrayConstants.get(Ty, V);
530 if (!Result) // If no preexisting value, create one now...
531 ArrayConstants.add(Ty, V, Result = new ConstantArray(Ty, V));
535 // ConstantArray::get(const string&) - Return an array that is initialized to
536 // contain the specified string. A null terminator is added to the specified
537 // string so that it may be used in a natural way...
539 ConstantArray *ConstantArray::get(const std::string &Str) {
540 std::vector<Constant*> ElementVals;
542 for (unsigned i = 0; i < Str.length(); ++i)
543 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
545 // Add a null terminator to the string...
546 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
548 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
549 return ConstantArray::get(ATy, ElementVals);
553 // destroyConstant - Remove the constant from the constant table...
555 void ConstantArray::destroyConstant() {
556 ArrayConstants.remove(this);
557 destroyConstantImpl();
560 // getAsString - If the sub-element type of this array is either sbyte or ubyte,
561 // then this method converts the array to an std::string and returns it.
562 // Otherwise, it asserts out.
564 std::string ConstantArray::getAsString() const {
566 if (getType()->getElementType() == Type::SByteTy)
567 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
568 Result += (char)cast<ConstantSInt>(getOperand(i))->getValue();
570 assert(getType()->getElementType() == Type::UByteTy && "Not a string!");
571 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
572 Result += (char)cast<ConstantUInt>(getOperand(i))->getValue();
578 //---- ConstantStruct::get() implementation...
580 static ValueMap<std::vector<Constant*>, ConstantStruct> StructConstants;
582 ConstantStruct *ConstantStruct::get(const StructType *Ty,
583 const std::vector<Constant*> &V) {
584 ConstantStruct *Result = StructConstants.get(Ty, V);
585 if (!Result) // If no preexisting value, create one now...
586 StructConstants.add(Ty, V, Result = new ConstantStruct(Ty, V));
590 // destroyConstant - Remove the constant from the constant table...
592 void ConstantStruct::destroyConstant() {
593 StructConstants.remove(this);
594 destroyConstantImpl();
598 //---- ConstantPointerNull::get() implementation...
600 static ValueMap<char, ConstantPointerNull> NullPtrConstants;
602 ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
603 ConstantPointerNull *Result = NullPtrConstants.get(Ty, 0);
604 if (!Result) // If no preexisting value, create one now...
605 NullPtrConstants.add(Ty, 0, Result = new ConstantPointerNull(Ty));
609 // destroyConstant - Remove the constant from the constant table...
611 void ConstantPointerNull::destroyConstant() {
612 NullPtrConstants.remove(this);
613 destroyConstantImpl();
617 //---- ConstantPointerRef::get() implementation...
619 ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
620 assert(GV->getParent() && "Global Value must be attached to a module!");
622 // The Module handles the pointer reference sharing...
623 return GV->getParent()->getConstantPointerRef(GV);
626 // destroyConstant - Remove the constant from the constant table...
628 void ConstantPointerRef::destroyConstant() {
629 getValue()->getParent()->destroyConstantPointerRef(this);
630 destroyConstantImpl();
634 //---- ConstantExpr::get() implementations...
636 typedef pair<unsigned, vector<Constant*> > ExprMapKeyType;
637 static ValueMap<const ExprMapKeyType, ConstantExpr> ExprConstants;
639 Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
640 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
641 return FC; // Fold a few common cases...
643 // Look up the constant in the table first to ensure uniqueness
644 vector<Constant*> argVec(1, C);
645 const ExprMapKeyType &Key = make_pair(Instruction::Cast, argVec);
646 ConstantExpr *Result = ExprConstants.get(Ty, Key);
647 if (Result) return Result;
649 // Its not in the table so create a new one and put it in the table.
650 Result = new ConstantExpr(Instruction::Cast, C, Ty);
651 ExprConstants.add(Ty, Key, Result);
655 Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
657 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
658 return FC; // Fold a few common cases...
660 // Look up the constant in the table first to ensure uniqueness
661 vector<Constant*> argVec(1, C1); argVec.push_back(C2);
662 const ExprMapKeyType &Key = make_pair(Opcode, argVec);
663 ConstantExpr *Result = ExprConstants.get(C1->getType(), Key);
664 if (Result) return Result;
666 // Its not in the table so create a new one and put it in the table.
667 // Check the operands for consistency first
668 assert((Opcode >= Instruction::BinaryOpsBegin &&
669 Opcode < Instruction::BinaryOpsEnd) &&
670 "Invalid opcode in binary constant expression");
672 assert(C1->getType() == C2->getType() &&
673 "Operand types in binary constant expression should match");
675 Result = new ConstantExpr(Opcode, C1, C2);
676 ExprConstants.add(C1->getType(), Key, Result);
680 Constant *ConstantExpr::getGetElementPtr(Constant *C,
681 const std::vector<Constant*> &IdxList){
682 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
683 return FC; // Fold a few common cases...
684 const Type *Ty = C->getType();
686 // Look up the constant in the table first to ensure uniqueness
687 vector<Constant*> argVec(1, C);
688 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
690 const ExprMapKeyType &Key = make_pair(Instruction::GetElementPtr, argVec);
691 ConstantExpr *Result = ExprConstants.get(Ty, Key);
692 if (Result) return Result;
694 // Its not in the table so create a new one and put it in the table.
695 // Check the operands for consistency first
697 assert(isa<PointerType>(Ty) &&
698 "Non-pointer type for constant GelElementPtr expression");
700 // Check that the indices list is valid...
701 std::vector<Value*> ValIdxList(IdxList.begin(), IdxList.end());
702 const Type *DestTy = GetElementPtrInst::getIndexedType(Ty, ValIdxList, true);
703 assert(DestTy && "Invalid index list for constant GelElementPtr expression");
705 Result = new ConstantExpr(C, IdxList, PointerType::get(DestTy));
706 ExprConstants.add(Ty, Key, Result);
710 // destroyConstant - Remove the constant from the constant table...
712 void ConstantExpr::destroyConstant() {
713 ExprConstants.remove(this);
714 destroyConstantImpl();
717 const char *ConstantExpr::getOpcodeName() const {
718 return Instruction::getOpcodeName(getOpcode());
721 unsigned Constant::mutateReferences(Value *OldV, Value *NewV) {
722 // Uses of constant pointer refs are global values, not constants!
723 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
724 GlobalValue *NewGV = cast<GlobalValue>(NewV);
725 GlobalValue *OldGV = CPR->getValue();
727 assert(OldGV == OldV && "Cannot mutate old value if I'm not using it!");
729 OldGV->getParent()->mutateConstantPointerRef(OldGV, NewGV);
733 Constant *NewC = cast<Constant>(NewV);
734 unsigned NumReplaced = 0;
735 for (unsigned i = 0, N = getNumOperands(); i != N; ++i)
736 if (Operands[i] == OldV) {