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 Operands.push_back(Use(V[i], this));
233 ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
234 : ConstantPointer(GV->getType()) {
235 Operands.push_back(Use(GV, this));
238 ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
239 : Constant(Ty), iType(Opcode) {
240 Operands.push_back(Use(C, this));
243 ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
244 : Constant(C1->getType()), iType(Opcode) {
245 Operands.push_back(Use(C1, this));
246 Operands.push_back(Use(C2, this));
249 ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
251 : Constant(DestTy), iType(Instruction::GetElementPtr) {
252 Operands.reserve(1+IdxList.size());
253 Operands.push_back(Use(C, this));
254 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
255 Operands.push_back(Use(IdxList[i], this));
260 //===----------------------------------------------------------------------===//
261 // classof implementations
263 bool ConstantIntegral::classof(const Constant *CPV) {
264 return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
267 bool ConstantInt::classof(const Constant *CPV) {
268 return CPV->getType()->isInteger() && !isa<ConstantExpr>(CPV);
270 bool ConstantSInt::classof(const Constant *CPV) {
271 return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
273 bool ConstantUInt::classof(const Constant *CPV) {
274 return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV);
276 bool ConstantFP::classof(const Constant *CPV) {
277 const Type *Ty = CPV->getType();
278 return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
279 !isa<ConstantExpr>(CPV));
281 bool ConstantArray::classof(const Constant *CPV) {
282 return isa<ArrayType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
284 bool ConstantStruct::classof(const Constant *CPV) {
285 return isa<StructType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
287 bool ConstantPointer::classof(const Constant *CPV) {
288 return (isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV));
293 //===----------------------------------------------------------------------===//
294 // isValueValidForType implementations
296 bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
297 switch (Ty->getPrimitiveID()) {
299 return false; // These can't be represented as integers!!!
302 case Type::SByteTyID:
303 return (Val <= INT8_MAX && Val >= INT8_MIN);
304 case Type::ShortTyID:
305 return (Val <= INT16_MAX && Val >= INT16_MIN);
307 return (Val <= INT32_MAX && Val >= INT32_MIN);
309 return true; // This is the largest type...
315 bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
316 switch (Ty->getPrimitiveID()) {
318 return false; // These can't be represented as integers!!!
321 case Type::UByteTyID:
322 return (Val <= UINT8_MAX);
323 case Type::UShortTyID:
324 return (Val <= UINT16_MAX);
326 return (Val <= UINT32_MAX);
327 case Type::ULongTyID:
328 return true; // This is the largest type...
334 bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
335 switch (Ty->getPrimitiveID()) {
337 return false; // These can't be represented as floating point!
339 // TODO: Figure out how to test if a double can be cast to a float!
340 case Type::FloatTyID:
342 return (Val <= UINT8_MAX);
344 case Type::DoubleTyID:
345 return true; // This is the largest type...
349 //===----------------------------------------------------------------------===//
350 // replaceUsesOfWithOnConstant implementations
352 void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To) {
353 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
355 std::vector<Constant*> Values;
356 Values.reserve(getValues().size()); // Build replacement array...
357 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
358 Constant *Val = cast<Constant>(getValues()[i]);
359 if (Val == From) Val = cast<Constant>(To);
360 Values.push_back(Val);
363 ConstantArray *Replacement = ConstantArray::get(getType(), Values);
364 assert(Replacement != this && "I didn't contain From!");
366 // Everyone using this now uses the replacement...
367 replaceAllUsesWith(Replacement);
369 // Delete the old constant!
373 void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To) {
374 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
376 std::vector<Constant*> Values;
377 Values.reserve(getValues().size());
378 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
379 Constant *Val = cast<Constant>(getValues()[i]);
380 if (Val == From) Val = cast<Constant>(To);
381 Values.push_back(Val);
384 ConstantStruct *Replacement = ConstantStruct::get(getType(), Values);
385 assert(Replacement != this && "I didn't contain From!");
387 // Everyone using this now uses the replacement...
388 replaceAllUsesWith(Replacement);
390 // Delete the old constant!
394 void ConstantPointerRef::replaceUsesOfWithOnConstant(Value *From, Value *To) {
395 if (isa<GlobalValue>(To)) {
396 assert(From == getOperand(0) && "Doesn't contain from!");
397 ConstantPointerRef *Replacement =
398 ConstantPointerRef::get(cast<GlobalValue>(To));
400 // Everyone using this now uses the replacement...
401 replaceAllUsesWith(Replacement);
403 // Delete the old constant!
406 // Just replace ourselves with the To value specified.
407 replaceAllUsesWith(To);
409 // Delete the old constant!
414 void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV) {
415 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
416 Constant *To = cast<Constant>(ToV);
418 Constant *Replacement = 0;
419 if (getOpcode() == Instruction::GetElementPtr) {
420 std::vector<Constant*> Indices;
421 Constant *Pointer = getOperand(0);
422 Indices.reserve(getNumOperands()-1);
423 if (Pointer == From) Pointer = To;
425 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
426 Constant *Val = getOperand(i);
427 if (Val == From) Val = To;
428 Indices.push_back(Val);
430 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
431 } else if (getOpcode() == Instruction::Cast) {
432 assert(getOperand(0) == From && "Cast only has one use!");
433 Replacement = ConstantExpr::getCast(To, getType());
434 } else if (getNumOperands() == 2) {
435 Constant *C1 = getOperand(0);
436 Constant *C2 = getOperand(1);
437 if (C1 == From) C1 = To;
438 if (C2 == From) C2 = To;
439 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
441 assert(0 && "Unknown ConstantExpr type!");
445 assert(Replacement != this && "I didn't contain From!");
447 // Everyone using this now uses the replacement...
448 replaceAllUsesWith(Replacement);
450 // Delete the old constant!
456 //===----------------------------------------------------------------------===//
457 // Factory Function Implementation
459 template<class ValType, class ConstantClass>
461 typedef std::pair<const Type*, ValType> ConstHashKey;
462 std::map<ConstHashKey, ConstantClass *> Map;
464 inline ConstantClass *get(const Type *Ty, ValType V) {
465 typename std::map<ConstHashKey,ConstantClass *>::iterator I =
466 Map.find(ConstHashKey(Ty, V));
467 return (I != Map.end()) ? I->second : 0;
470 inline void add(const Type *Ty, ValType V, ConstantClass *CP) {
471 Map.insert(std::make_pair(ConstHashKey(Ty, V), CP));
474 inline void remove(ConstantClass *CP) {
475 for (typename std::map<ConstHashKey, ConstantClass*>::iterator
476 I = Map.begin(), E = Map.end(); I != E; ++I)
477 if (I->second == CP) {
484 //---- ConstantUInt::get() and ConstantSInt::get() implementations...
486 static ValueMap<uint64_t, ConstantInt> IntConstants;
488 ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
489 ConstantSInt *Result = (ConstantSInt*)IntConstants.get(Ty, (uint64_t)V);
490 if (!Result) // If no preexisting value, create one now...
491 IntConstants.add(Ty, V, Result = new ConstantSInt(Ty, V));
495 ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
496 ConstantUInt *Result = (ConstantUInt*)IntConstants.get(Ty, V);
497 if (!Result) // If no preexisting value, create one now...
498 IntConstants.add(Ty, V, Result = new ConstantUInt(Ty, V));
502 ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
503 assert(V <= 127 && "Can only be used with very small positive constants!");
504 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
505 return ConstantUInt::get(Ty, V);
508 //---- ConstantFP::get() implementation...
510 static ValueMap<double, ConstantFP> FPConstants;
512 ConstantFP *ConstantFP::get(const Type *Ty, double V) {
513 ConstantFP *Result = FPConstants.get(Ty, V);
514 if (!Result) // If no preexisting value, create one now...
515 FPConstants.add(Ty, V, Result = new ConstantFP(Ty, V));
519 //---- ConstantArray::get() implementation...
521 static ValueMap<std::vector<Constant*>, ConstantArray> ArrayConstants;
523 ConstantArray *ConstantArray::get(const ArrayType *Ty,
524 const std::vector<Constant*> &V) {
525 ConstantArray *Result = ArrayConstants.get(Ty, V);
526 if (!Result) // If no preexisting value, create one now...
527 ArrayConstants.add(Ty, V, Result = new ConstantArray(Ty, V));
531 // ConstantArray::get(const string&) - Return an array that is initialized to
532 // contain the specified string. A null terminator is added to the specified
533 // string so that it may be used in a natural way...
535 ConstantArray *ConstantArray::get(const std::string &Str) {
536 std::vector<Constant*> ElementVals;
538 for (unsigned i = 0; i < Str.length(); ++i)
539 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
541 // Add a null terminator to the string...
542 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
544 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
545 return ConstantArray::get(ATy, ElementVals);
549 // destroyConstant - Remove the constant from the constant table...
551 void ConstantArray::destroyConstant() {
552 ArrayConstants.remove(this);
553 destroyConstantImpl();
556 // getAsString - If the sub-element type of this array is either sbyte or ubyte,
557 // then this method converts the array to an std::string and returns it.
558 // Otherwise, it asserts out.
560 std::string ConstantArray::getAsString() const {
562 if (getType()->getElementType() == Type::SByteTy)
563 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
564 Result += (char)cast<ConstantSInt>(getOperand(i))->getValue();
566 assert(getType()->getElementType() == Type::UByteTy && "Not a string!");
567 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
568 Result += (char)cast<ConstantUInt>(getOperand(i))->getValue();
574 //---- ConstantStruct::get() implementation...
576 static ValueMap<std::vector<Constant*>, ConstantStruct> StructConstants;
578 ConstantStruct *ConstantStruct::get(const StructType *Ty,
579 const std::vector<Constant*> &V) {
580 ConstantStruct *Result = StructConstants.get(Ty, V);
581 if (!Result) // If no preexisting value, create one now...
582 StructConstants.add(Ty, V, Result = new ConstantStruct(Ty, V));
586 // destroyConstant - Remove the constant from the constant table...
588 void ConstantStruct::destroyConstant() {
589 StructConstants.remove(this);
590 destroyConstantImpl();
594 //---- ConstantPointerNull::get() implementation...
596 static ValueMap<char, ConstantPointerNull> NullPtrConstants;
598 ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
599 ConstantPointerNull *Result = NullPtrConstants.get(Ty, 0);
600 if (!Result) // If no preexisting value, create one now...
601 NullPtrConstants.add(Ty, 0, Result = new ConstantPointerNull(Ty));
605 // destroyConstant - Remove the constant from the constant table...
607 void ConstantPointerNull::destroyConstant() {
608 NullPtrConstants.remove(this);
609 destroyConstantImpl();
613 //---- ConstantPointerRef::get() implementation...
615 ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
616 assert(GV->getParent() && "Global Value must be attached to a module!");
618 // The Module handles the pointer reference sharing...
619 return GV->getParent()->getConstantPointerRef(GV);
622 // destroyConstant - Remove the constant from the constant table...
624 void ConstantPointerRef::destroyConstant() {
625 getValue()->getParent()->destroyConstantPointerRef(this);
626 destroyConstantImpl();
630 //---- ConstantExpr::get() implementations...
632 typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
633 static ValueMap<const ExprMapKeyType, ConstantExpr> ExprConstants;
635 Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
636 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
637 return FC; // Fold a few common cases...
639 // Look up the constant in the table first to ensure uniqueness
640 std::vector<Constant*> argVec(1, C);
641 const ExprMapKeyType &Key = std::make_pair(Instruction::Cast, argVec);
642 ConstantExpr *Result = ExprConstants.get(Ty, Key);
643 if (Result) return Result;
645 // Its not in the table so create a new one and put it in the table.
646 Result = new ConstantExpr(Instruction::Cast, C, Ty);
647 ExprConstants.add(Ty, Key, Result);
651 Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
652 // Check the operands for consistency first
653 assert((Opcode >= Instruction::BinaryOpsBegin &&
654 Opcode < Instruction::BinaryOpsEnd) &&
655 "Invalid opcode in binary constant expression");
656 assert(C1->getType() == C2->getType() &&
657 "Operand types in binary constant expression should match");
659 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
660 return FC; // Fold a few common cases...
662 // Look up the constant in the table first to ensure uniqueness
663 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
664 const ExprMapKeyType &Key = std::make_pair(Opcode, argVec);
665 ConstantExpr *Result = ExprConstants.get(C1->getType(), Key);
666 if (Result) return Result;
668 // It's not in the table so create a new one and put it in the table.
669 Result = new ConstantExpr(Opcode, C1, C2);
670 ExprConstants.add(C1->getType(), Key, Result);
674 /// getShift - Return a shift left or shift right constant expr
675 Constant *ConstantExpr::getShift(unsigned Opcode, Constant *C1, Constant *C2) {
676 // Check the operands for consistency first
677 assert((Opcode == Instruction::Shl ||
678 Opcode == Instruction::Shr) &&
679 "Invalid opcode in binary constant expression");
680 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
681 "Invalid operand types for Shift constant expr!");
683 if (Constant *FC = ConstantFoldShiftInstruction(Opcode, C1, C2))
684 return FC; // Fold a few common cases...
686 // Look up the constant in the table first to ensure uniqueness
687 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
688 const ExprMapKeyType &Key = std::make_pair(Opcode, argVec);
689 ConstantExpr *Result = ExprConstants.get(C1->getType(), Key);
690 if (Result) return Result;
692 // It's not in the table so create a new one and put it in the table.
693 Result = new ConstantExpr(Opcode, C1, C2);
694 ExprConstants.add(C1->getType(), Key, Result);
699 Constant *ConstantExpr::getGetElementPtr(Constant *C,
700 const std::vector<Constant*> &IdxList){
701 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
702 return FC; // Fold a few common cases...
703 const Type *Ty = C->getType();
705 // Look up the constant in the table first to ensure uniqueness
706 std::vector<Constant*> argVec(1, C);
707 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
709 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,argVec);
710 ConstantExpr *Result = ExprConstants.get(Ty, Key);
711 if (Result) return Result;
713 // Its not in the table so create a new one and put it in the table.
714 // Check the operands for consistency first
716 assert(isa<PointerType>(Ty) &&
717 "Non-pointer type for constant GelElementPtr expression");
719 // Check that the indices list is valid...
720 std::vector<Value*> ValIdxList(IdxList.begin(), IdxList.end());
721 const Type *DestTy = GetElementPtrInst::getIndexedType(Ty, ValIdxList, true);
722 assert(DestTy && "Invalid index list for constant GelElementPtr expression");
724 Result = new ConstantExpr(C, IdxList, PointerType::get(DestTy));
725 ExprConstants.add(Ty, Key, Result);
729 // destroyConstant - Remove the constant from the constant table...
731 void ConstantExpr::destroyConstant() {
732 ExprConstants.remove(this);
733 destroyConstantImpl();
736 const char *ConstantExpr::getOpcodeName() const {
737 return Instruction::getOpcodeName(getOpcode());
740 unsigned Constant::mutateReferences(Value *OldV, Value *NewV) {
741 // Uses of constant pointer refs are global values, not constants!
742 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
743 GlobalValue *NewGV = cast<GlobalValue>(NewV);
744 GlobalValue *OldGV = CPR->getValue();
746 assert(OldGV == OldV && "Cannot mutate old value if I'm not using it!");
748 OldGV->getParent()->mutateConstantPointerRef(OldGV, NewGV);
751 Constant *NewC = cast<Constant>(NewV);
752 unsigned NumReplaced = 0;
753 for (unsigned i = 0, N = getNumOperands(); i != N; ++i)
754 if (Operands[i] == OldV) {