1 //===-- Constants.cpp - Implement Constant nodes -----------------*- C++ -*--=//
3 // This file implements the Constant* classes...
5 //===----------------------------------------------------------------------===//
7 #include "llvm/Constants.h"
8 #include "llvm/DerivedTypes.h"
9 #include "llvm/iMemory.h"
10 #include "llvm/SymbolTable.h"
11 #include "llvm/Module.h"
12 #include "Support/StringExtras.h"
20 ConstantBool *ConstantBool::True = new ConstantBool(true);
21 ConstantBool *ConstantBool::False = new ConstantBool(false);
24 //===----------------------------------------------------------------------===//
26 //===----------------------------------------------------------------------===//
28 // Specialize setName to take care of symbol table majik
29 void Constant::setName(const std::string &Name, SymbolTable *ST) {
30 assert(ST && "Type::setName - Must provide symbol table argument!");
32 if (Name.size()) ST->insert(Name, this);
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 dependant 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 *CPV = cast<Constant>(V);
53 CPV->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->getPrimitiveID()) {
66 case Type::BoolTyID: return ConstantBool::get(false);
70 case Type::LongTyID: return ConstantSInt::get(Ty, 0);
73 case Type::UShortTyID:
75 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
78 case Type::DoubleTyID: return ConstantFP::get(Ty, 0);
80 case Type::PointerTyID:
81 return ConstantPointerNull::get(cast<PointerType>(Ty));
82 case Type::StructTyID: {
83 const StructType *ST = cast<StructType>(Ty);
85 const StructType::ElementTypes &ETs = ST->getElementTypes();
86 std::vector<Constant*> Elements;
87 Elements.resize(ETs.size());
88 for (unsigned i = 0, e = ETs.size(); i != e; ++i)
89 Elements[i] = Constant::getNullValue(ETs[i]);
90 return ConstantStruct::get(ST, Elements);
92 case Type::ArrayTyID: {
93 const ArrayType *AT = cast<ArrayType>(Ty);
94 Constant *El = Constant::getNullValue(AT->getElementType());
95 unsigned NumElements = AT->getNumElements();
96 return ConstantArray::get(AT, std::vector<Constant*>(NumElements, El));
99 // Function, Type, Label, or Opaque type?
100 assert(0 && "Cannot create a null constant of that type!");
105 // Static constructor to create the maximum constant of an integral type...
106 ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
107 switch (Ty->getPrimitiveID()) {
108 case Type::BoolTyID: return ConstantBool::True;
109 case Type::SByteTyID:
110 case Type::ShortTyID:
112 case Type::LongTyID: {
113 // Calculate 011111111111111...
114 unsigned TypeBits = Ty->getPrimitiveSize()*8;
115 int64_t Val = INT64_MAX; // All ones
116 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
117 return ConstantSInt::get(Ty, Val);
120 case Type::UByteTyID:
121 case Type::UShortTyID:
123 case Type::ULongTyID: return getAllOnesValue(Ty);
129 // Static constructor to create the minimum constant for an integral type...
130 ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
131 switch (Ty->getPrimitiveID()) {
132 case Type::BoolTyID: return ConstantBool::False;
133 case Type::SByteTyID:
134 case Type::ShortTyID:
136 case Type::LongTyID: {
137 // Calculate 1111111111000000000000
138 unsigned TypeBits = Ty->getPrimitiveSize()*8;
139 int64_t Val = -1; // All ones
140 Val <<= TypeBits-1; // Shift over to the right spot
141 return ConstantSInt::get(Ty, Val);
144 case Type::UByteTyID:
145 case Type::UShortTyID:
147 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
153 // Static constructor to create an integral constant with all bits set
154 ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
155 switch (Ty->getPrimitiveID()) {
156 case Type::BoolTyID: return ConstantBool::True;
157 case Type::SByteTyID:
158 case Type::ShortTyID:
160 case Type::LongTyID: return ConstantSInt::get(Ty, -1);
162 case Type::UByteTyID:
163 case Type::UShortTyID:
165 case Type::ULongTyID: {
166 // Calculate ~0 of the right type...
167 unsigned TypeBits = Ty->getPrimitiveSize()*8;
168 uint64_t Val = ~0ULL; // All ones
169 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
170 return ConstantUInt::get(Ty, Val);
176 bool ConstantUInt::isAllOnesValue() const {
177 unsigned TypeBits = getType()->getPrimitiveSize()*8;
178 uint64_t Val = ~0ULL; // All ones
179 Val >>= 64-TypeBits; // Shift out inappropriate bits
180 return getValue() == Val;
184 //===----------------------------------------------------------------------===//
185 // ConstantXXX Classes
186 //===----------------------------------------------------------------------===//
188 //===----------------------------------------------------------------------===//
189 // Normal Constructors
191 ConstantBool::ConstantBool(bool V) : ConstantIntegral(Type::BoolTy) {
195 ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : ConstantIntegral(Ty) {
199 ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
200 assert(Ty->isInteger() && Ty->isSigned() &&
201 "Illegal type for unsigned integer constant!");
202 assert(isValueValidForType(Ty, V) && "Value too large for type!");
205 ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
206 assert(Ty->isInteger() && Ty->isUnsigned() &&
207 "Illegal type for unsigned integer constant!");
208 assert(isValueValidForType(Ty, V) && "Value too large for type!");
211 ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
212 assert(isValueValidForType(Ty, V) && "Value too large for type!");
216 ConstantArray::ConstantArray(const ArrayType *T,
217 const std::vector<Constant*> &V) : Constant(T) {
218 Operands.reserve(V.size());
219 for (unsigned i = 0, e = V.size(); i != e; ++i) {
220 assert(V[i]->getType() == T->getElementType());
221 Operands.push_back(Use(V[i], this));
225 ConstantStruct::ConstantStruct(const StructType *T,
226 const std::vector<Constant*> &V) : Constant(T) {
227 const StructType::ElementTypes &ETypes = T->getElementTypes();
228 assert(V.size() == ETypes.size() &&
229 "Invalid initializer vector for constant structure");
230 Operands.reserve(V.size());
231 for (unsigned i = 0, e = V.size(); i != e; ++i) {
232 assert(V[i]->getType() == ETypes[i]);
233 Operands.push_back(Use(V[i], this));
237 ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
238 : ConstantPointer(GV->getType()) {
239 Operands.push_back(Use(GV, this));
242 ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
243 : Constant(Ty), iType(Opcode) {
244 Operands.push_back(Use(C, this));
247 ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
248 : Constant(C1->getType()), iType(Opcode) {
249 Operands.push_back(Use(C1, this));
250 Operands.push_back(Use(C2, this));
253 ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
255 : Constant(DestTy), iType(Instruction::GetElementPtr) {
256 Operands.reserve(1+IdxList.size());
257 Operands.push_back(Use(C, this));
258 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
259 Operands.push_back(Use(IdxList[i], this));
264 //===----------------------------------------------------------------------===//
265 // classof implementations
267 bool ConstantIntegral::classof(const Constant *CPV) {
268 return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
271 bool ConstantInt::classof(const Constant *CPV) {
272 return CPV->getType()->isInteger() && !isa<ConstantExpr>(CPV);
274 bool ConstantSInt::classof(const Constant *CPV) {
275 return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
277 bool ConstantUInt::classof(const Constant *CPV) {
278 return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV);
280 bool ConstantFP::classof(const Constant *CPV) {
281 const Type *Ty = CPV->getType();
282 return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
283 !isa<ConstantExpr>(CPV));
285 bool ConstantArray::classof(const Constant *CPV) {
286 return isa<ArrayType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
288 bool ConstantStruct::classof(const Constant *CPV) {
289 return isa<StructType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
291 bool ConstantPointer::classof(const Constant *CPV) {
292 return (isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV));
297 //===----------------------------------------------------------------------===//
298 // isValueValidForType implementations
300 bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
301 switch (Ty->getPrimitiveID()) {
303 return false; // These can't be represented as integers!!!
306 case Type::SByteTyID:
307 return (Val <= INT8_MAX && Val >= INT8_MIN);
308 case Type::ShortTyID:
309 return (Val <= INT16_MAX && Val >= INT16_MIN);
311 return (Val <= INT32_MAX && Val >= INT32_MIN);
313 return true; // This is the largest type...
319 bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
320 switch (Ty->getPrimitiveID()) {
322 return false; // These can't be represented as integers!!!
325 case Type::UByteTyID:
326 return (Val <= UINT8_MAX);
327 case Type::UShortTyID:
328 return (Val <= UINT16_MAX);
330 return (Val <= UINT32_MAX);
331 case Type::ULongTyID:
332 return true; // This is the largest type...
338 bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
339 switch (Ty->getPrimitiveID()) {
341 return false; // These can't be represented as floating point!
343 // TODO: Figure out how to test if a double can be cast to a float!
344 case Type::FloatTyID:
346 return (Val <= UINT8_MAX);
348 case Type::DoubleTyID:
349 return true; // This is the largest type...
353 //===----------------------------------------------------------------------===//
354 // replaceUsesOfWithOnConstant implementations
356 void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To) {
357 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
359 std::vector<Constant*> Values;
360 Values.reserve(getValues().size()); // Build replacement array...
361 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
362 Constant *Val = cast<Constant>(getValues()[i]);
363 if (Val == From) Val = cast<Constant>(To);
364 Values.push_back(Val);
367 ConstantArray *Replacement = ConstantArray::get(getType(), Values);
368 assert(Replacement != this && "I didn't contain From!");
370 // Everyone using this now uses the replacement...
371 replaceAllUsesWith(Replacement);
373 // Delete the old constant!
377 void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To) {
378 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
380 std::vector<Constant*> Values;
381 Values.reserve(getValues().size());
382 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
383 Constant *Val = cast<Constant>(getValues()[i]);
384 if (Val == From) Val = cast<Constant>(To);
385 Values.push_back(Val);
388 ConstantStruct *Replacement = ConstantStruct::get(getType(), Values);
389 assert(Replacement != this && "I didn't contain From!");
391 // Everyone using this now uses the replacement...
392 replaceAllUsesWith(Replacement);
394 // Delete the old constant!
398 void ConstantPointerRef::replaceUsesOfWithOnConstant(Value *From, Value *To) {
399 if (isa<GlobalValue>(To)) {
400 assert(From == getOperand(0) && "Doesn't contain from!");
401 ConstantPointerRef *Replacement =
402 ConstantPointerRef::get(cast<GlobalValue>(To));
404 // Everyone using this now uses the replacement...
405 replaceAllUsesWith(Replacement);
407 // Delete the old constant!
410 // Just replace ourselves with the To value specified.
411 replaceAllUsesWith(To);
413 // Delete the old constant!
418 void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *To) {
419 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
421 ConstantExpr *Replacement = 0;
422 if (getOpcode() == Instruction::GetElementPtr) {
423 std::vector<Constant*> Indices;
424 Constant *Pointer = cast<Constant>(getOperand(0));
425 Indices.reserve(getNumOperands()-1);
426 if (Pointer == From) Pointer = cast<Constant>(To);
428 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
429 Constant *Val = cast<Constant>(getOperand(i));
430 if (Val == From) Val = cast<Constant>(To);
431 Indices.push_back(Val);
433 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
434 } else if (getOpcode() == Instruction::Cast) {
435 assert(getOperand(0) == From && "Cast only has one use!");
436 Replacement = ConstantExpr::getCast(cast<Constant>(To), getType());
437 } else if (getNumOperands() == 2) {
438 Constant *C1 = cast<Constant>(getOperand(0));
439 Constant *C2 = cast<Constant>(getOperand(1));
440 if (C1 == From) C1 = cast<Constant>(To);
441 if (C2 == From) C2 = cast<Constant>(To);
442 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
444 assert(0 && "Unknown ConstantExpr type!");
448 assert(Replacement != this && "I didn't contain From!");
450 // Everyone using this now uses the replacement...
451 replaceAllUsesWith(Replacement);
453 // Delete the old constant!
459 //===----------------------------------------------------------------------===//
460 // Factory Function Implementation
462 template<class ValType, class ConstantClass>
464 typedef pair<const Type*, ValType> ConstHashKey;
465 map<ConstHashKey, ConstantClass *> Map;
467 inline ConstantClass *get(const Type *Ty, ValType V) {
468 typename map<ConstHashKey,ConstantClass *>::iterator I =
469 Map.find(ConstHashKey(Ty, V));
470 return (I != Map.end()) ? I->second : 0;
473 inline void add(const Type *Ty, ValType V, ConstantClass *CP) {
474 Map.insert(make_pair(ConstHashKey(Ty, V), CP));
477 inline void remove(ConstantClass *CP) {
478 for (typename map<ConstHashKey,ConstantClass *>::iterator I = Map.begin(),
479 E = Map.end(); I != E;++I)
480 if (I->second == CP) {
487 //---- ConstantUInt::get() and ConstantSInt::get() implementations...
489 static ValueMap<uint64_t, ConstantInt> IntConstants;
491 ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
492 ConstantSInt *Result = (ConstantSInt*)IntConstants.get(Ty, (uint64_t)V);
493 if (!Result) // If no preexisting value, create one now...
494 IntConstants.add(Ty, V, Result = new ConstantSInt(Ty, V));
498 ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
499 ConstantUInt *Result = (ConstantUInt*)IntConstants.get(Ty, V);
500 if (!Result) // If no preexisting value, create one now...
501 IntConstants.add(Ty, V, Result = new ConstantUInt(Ty, V));
505 ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
506 assert(V <= 127 && "Can only be used with very small positive constants!");
507 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
508 return ConstantUInt::get(Ty, V);
511 //---- ConstantFP::get() implementation...
513 static ValueMap<double, ConstantFP> FPConstants;
515 ConstantFP *ConstantFP::get(const Type *Ty, double V) {
516 ConstantFP *Result = FPConstants.get(Ty, V);
517 if (!Result) // If no preexisting value, create one now...
518 FPConstants.add(Ty, V, Result = new ConstantFP(Ty, V));
522 //---- ConstantArray::get() implementation...
524 static ValueMap<std::vector<Constant*>, ConstantArray> ArrayConstants;
526 ConstantArray *ConstantArray::get(const ArrayType *Ty,
527 const std::vector<Constant*> &V) {
528 ConstantArray *Result = ArrayConstants.get(Ty, V);
529 if (!Result) // If no preexisting value, create one now...
530 ArrayConstants.add(Ty, V, Result = new ConstantArray(Ty, V));
534 // ConstantArray::get(const string&) - Return an array that is initialized to
535 // contain the specified string. A null terminator is added to the specified
536 // string so that it may be used in a natural way...
538 ConstantArray *ConstantArray::get(const std::string &Str) {
539 std::vector<Constant*> ElementVals;
541 for (unsigned i = 0; i < Str.length(); ++i)
542 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
544 // Add a null terminator to the string...
545 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
547 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
548 return ConstantArray::get(ATy, ElementVals);
552 // destroyConstant - Remove the constant from the constant table...
554 void ConstantArray::destroyConstant() {
555 ArrayConstants.remove(this);
556 destroyConstantImpl();
559 // getAsString - If the sub-element type of this array is either sbyte or ubyte,
560 // then this method converts the array to an std::string and returns it.
561 // Otherwise, it asserts out.
563 std::string ConstantArray::getAsString() const {
565 if (getType()->getElementType() == Type::SByteTy)
566 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
567 Result += (char)cast<ConstantSInt>(getOperand(i))->getValue();
569 assert(getType()->getElementType() == Type::UByteTy && "Not a string!");
570 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
571 Result += (char)cast<ConstantUInt>(getOperand(i))->getValue();
577 //---- ConstantStruct::get() implementation...
579 static ValueMap<std::vector<Constant*>, ConstantStruct> StructConstants;
581 ConstantStruct *ConstantStruct::get(const StructType *Ty,
582 const std::vector<Constant*> &V) {
583 ConstantStruct *Result = StructConstants.get(Ty, V);
584 if (!Result) // If no preexisting value, create one now...
585 StructConstants.add(Ty, V, Result = new ConstantStruct(Ty, V));
589 // destroyConstant - Remove the constant from the constant table...
591 void ConstantStruct::destroyConstant() {
592 StructConstants.remove(this);
593 destroyConstantImpl();
597 //---- ConstantPointerNull::get() implementation...
599 static ValueMap<char, ConstantPointerNull> NullPtrConstants;
601 ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
602 ConstantPointerNull *Result = NullPtrConstants.get(Ty, 0);
603 if (!Result) // If no preexisting value, create one now...
604 NullPtrConstants.add(Ty, 0, Result = new ConstantPointerNull(Ty));
608 // destroyConstant - Remove the constant from the constant table...
610 void ConstantPointerNull::destroyConstant() {
611 NullPtrConstants.remove(this);
612 destroyConstantImpl();
616 //---- ConstantPointerRef::get() implementation...
618 ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
619 assert(GV->getParent() && "Global Value must be attached to a module!");
621 // The Module handles the pointer reference sharing...
622 return GV->getParent()->getConstantPointerRef(GV);
625 // destroyConstant - Remove the constant from the constant table...
627 void ConstantPointerRef::destroyConstant() {
628 getValue()->getParent()->destroyConstantPointerRef(this);
629 destroyConstantImpl();
633 //---- ConstantExpr::get() implementations...
635 typedef pair<unsigned, vector<Constant*> > ExprMapKeyType;
636 static ValueMap<const ExprMapKeyType, ConstantExpr> ExprConstants;
638 ConstantExpr *ConstantExpr::getCast(Constant *C, const Type *Ty) {
640 // Look up the constant in the table first to ensure uniqueness
641 vector<Constant*> argVec(1, C);
642 const ExprMapKeyType &Key = make_pair(Instruction::Cast, argVec);
643 ConstantExpr *Result = ExprConstants.get(Ty, Key);
644 if (Result) return Result;
646 // Its not in the table so create a new one and put it in the table.
647 Result = new ConstantExpr(Instruction::Cast, C, Ty);
648 ExprConstants.add(Ty, Key, Result);
652 ConstantExpr *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
653 // Look up the constant in the table first to ensure uniqueness
654 vector<Constant*> argVec(1, C1); argVec.push_back(C2);
655 const ExprMapKeyType &Key = make_pair(Opcode, argVec);
656 ConstantExpr *Result = ExprConstants.get(C1->getType(), Key);
657 if (Result) return Result;
659 // Its not in the table so create a new one and put it in the table.
660 // Check the operands for consistency first
661 assert((Opcode >= Instruction::BinaryOpsBegin &&
662 Opcode < Instruction::BinaryOpsEnd) &&
663 "Invalid opcode in binary constant expression");
665 assert(C1->getType() == C2->getType() &&
666 "Operand types in binary constant expression should match");
668 Result = new ConstantExpr(Opcode, C1, C2);
669 ExprConstants.add(C1->getType(), Key, Result);
673 ConstantExpr *ConstantExpr::getGetElementPtr(Constant *C,
674 const std::vector<Constant*> &IdxList) {
675 const Type *Ty = C->getType();
677 // Look up the constant in the table first to ensure uniqueness
678 vector<Constant*> argVec(1, C);
679 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
681 const ExprMapKeyType &Key = make_pair(Instruction::GetElementPtr, argVec);
682 ConstantExpr *Result = ExprConstants.get(Ty, Key);
683 if (Result) return Result;
685 // Its not in the table so create a new one and put it in the table.
686 // Check the operands for consistency first
688 assert(isa<PointerType>(Ty) &&
689 "Non-pointer type for constant GelElementPtr expression");
691 // Check that the indices list is valid...
692 std::vector<Value*> ValIdxList(IdxList.begin(), IdxList.end());
693 const Type *DestTy = GetElementPtrInst::getIndexedType(Ty, ValIdxList, true);
694 assert(DestTy && "Invalid index list for constant GelElementPtr expression");
696 Result = new ConstantExpr(C, IdxList, PointerType::get(DestTy));
697 ExprConstants.add(Ty, Key, Result);
701 // destroyConstant - Remove the constant from the constant table...
703 void ConstantExpr::destroyConstant() {
704 ExprConstants.remove(this);
705 destroyConstantImpl();
708 const char *ConstantExpr::getOpcodeName() const {
709 return Instruction::getOpcodeName(getOpcode());
712 unsigned Constant::mutateReferences(Value *OldV, Value *NewV) {
713 // Uses of constant pointer refs are global values, not constants!
714 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
715 GlobalValue *NewGV = cast<GlobalValue>(NewV);
716 GlobalValue *OldGV = CPR->getValue();
718 assert(OldGV == OldV && "Cannot mutate old value if I'm not using it!");
720 OldGV->getParent()->mutateConstantPointerRef(OldGV, NewGV);
724 Constant *NewC = cast<Constant>(NewV);
725 unsigned NumReplaced = 0;
726 for (unsigned i = 0, N = getNumOperands(); i != N; ++i)
727 if (Operands[i] == OldV) {