blob: 76904ad3f97bacc1c20b5881c4678c12bcd8b811 [file] [log] [blame]
Chris Lattner9bc02a42003-05-13 21:37:02 +00001//===-- Constants.cpp - Implement Constant nodes --------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00002//
Chris Lattnere9bb2df2001-12-03 22:26:30 +00003// This file implements the Constant* classes...
Chris Lattner00950542001-06-06 20:29:01 +00004//
5//===----------------------------------------------------------------------===//
6
Chris Lattner31bcdb82002-04-28 19:55:58 +00007#include "llvm/Constants.h"
Chris Lattnerd628f6a2003-04-17 19:24:48 +00008#include "llvm/ConstantHandling.h"
Chris Lattner00950542001-06-06 20:29:01 +00009#include "llvm/DerivedTypes.h"
Vikram S. Adve345e0cf2002-07-14 23:13:17 +000010#include "llvm/iMemory.h"
Chris Lattner00950542001-06-06 20:29:01 +000011#include "llvm/SymbolTable.h"
Chris Lattnerf5ec48d2001-10-13 06:57:33 +000012#include "llvm/Module.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000013#include "Support/StringExtras.h"
Chris Lattner00950542001-06-06 20:29:01 +000014#include <algorithm>
Chris Lattner00950542001-06-06 20:29:01 +000015
Chris Lattnere9bb2df2001-12-03 22:26:30 +000016ConstantBool *ConstantBool::True = new ConstantBool(true);
17ConstantBool *ConstantBool::False = new ConstantBool(false);
Chris Lattner00950542001-06-06 20:29:01 +000018
Chris Lattner37bf6302001-07-20 19:16:02 +000019
Chris Lattner00950542001-06-06 20:29:01 +000020//===----------------------------------------------------------------------===//
Chris Lattnere9bb2df2001-12-03 22:26:30 +000021// Constant Class
Chris Lattner00950542001-06-06 20:29:01 +000022//===----------------------------------------------------------------------===//
23
24// Specialize setName to take care of symbol table majik
Chris Lattner697954c2002-01-20 22:54:45 +000025void Constant::setName(const std::string &Name, SymbolTable *ST) {
Chris Lattner531daef2001-09-07 16:46:31 +000026 assert(ST && "Type::setName - Must provide symbol table argument!");
27
28 if (Name.size()) ST->insert(Name, this);
Chris Lattner00950542001-06-06 20:29:01 +000029}
30
Chris Lattnere9bb2df2001-12-03 22:26:30 +000031void Constant::destroyConstantImpl() {
32 // When a Constant is destroyed, there may be lingering
Chris Lattnerf5ec48d2001-10-13 06:57:33 +000033 // 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
Chris Lattnere9bb2df2001-12-03 22:26:30 +000037 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerf5ec48d2001-10-13 06:57:33 +000038 //
39 while (!use_empty()) {
40 Value *V = use_back();
41#ifndef NDEBUG // Only in -g mode...
Chris Lattner6183b922002-07-18 00:14:50 +000042 if (!isa<Constant>(V))
43 std::cerr << "While deleting: " << *this
44 << "\n\nUse still stuck around after Def is destroyed: "
45 << *V << "\n\n";
Chris Lattnerf5ec48d2001-10-13 06:57:33 +000046#endif
Vikram S. Adve345e0cf2002-07-14 23:13:17 +000047 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Chris Lattnere9bb2df2001-12-03 22:26:30 +000048 Constant *CPV = cast<Constant>(V);
Chris Lattnerf5ec48d2001-10-13 06:57:33 +000049 CPV->destroyConstant();
50
51 // The constant should remove itself from our use list...
Vikram S. Adve345e0cf2002-07-14 23:13:17 +000052 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerf5ec48d2001-10-13 06:57:33 +000053 }
54
55 // Value has no outstanding references it is safe to delete it now...
56 delete this;
Chris Lattner1d87bcf2001-10-01 20:11:19 +000057}
Chris Lattner00950542001-06-06 20:29:01 +000058
Chris Lattner9fb96412002-08-13 17:50:20 +000059// Static constructor to create a '0' constant of arbitrary type...
60Constant *Constant::getNullValue(const Type *Ty) {
61 switch (Ty->getPrimitiveID()) {
62 case Type::BoolTyID: return ConstantBool::get(false);
63 case Type::SByteTyID:
64 case Type::ShortTyID:
65 case Type::IntTyID:
66 case Type::LongTyID: return ConstantSInt::get(Ty, 0);
67
68 case Type::UByteTyID:
69 case Type::UShortTyID:
70 case Type::UIntTyID:
71 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
72
73 case Type::FloatTyID:
74 case Type::DoubleTyID: return ConstantFP::get(Ty, 0);
75
76 case Type::PointerTyID:
77 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner27accf72003-03-06 21:02:18 +000078 case Type::StructTyID: {
79 const StructType *ST = cast<StructType>(Ty);
80
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);
87 }
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));
93 }
Chris Lattner9fb96412002-08-13 17:50:20 +000094 default:
Chris Lattner27accf72003-03-06 21:02:18 +000095 // Function, Type, Label, or Opaque type?
96 assert(0 && "Cannot create a null constant of that type!");
Chris Lattner9fb96412002-08-13 17:50:20 +000097 return 0;
98 }
99}
100
101// Static constructor to create the maximum constant of an integral type...
102ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
103 switch (Ty->getPrimitiveID()) {
104 case Type::BoolTyID: return ConstantBool::True;
105 case Type::SByteTyID:
106 case Type::ShortTyID:
107 case Type::IntTyID:
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);
114 }
115
116 case Type::UByteTyID:
117 case Type::UShortTyID:
118 case Type::UIntTyID:
119 case Type::ULongTyID: return getAllOnesValue(Ty);
120
Chris Lattner227b86c2002-08-14 17:12:13 +0000121 default: return 0;
Chris Lattner9fb96412002-08-13 17:50:20 +0000122 }
123}
124
125// Static constructor to create the minimum constant for an integral type...
126ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
127 switch (Ty->getPrimitiveID()) {
128 case Type::BoolTyID: return ConstantBool::False;
129 case Type::SByteTyID:
130 case Type::ShortTyID:
131 case Type::IntTyID:
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);
138 }
139
140 case Type::UByteTyID:
141 case Type::UShortTyID:
142 case Type::UIntTyID:
143 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
144
Chris Lattner227b86c2002-08-14 17:12:13 +0000145 default: return 0;
Chris Lattner9fb96412002-08-13 17:50:20 +0000146 }
147}
148
149// Static constructor to create an integral constant with all bits set
150ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
151 switch (Ty->getPrimitiveID()) {
152 case Type::BoolTyID: return ConstantBool::True;
153 case Type::SByteTyID:
154 case Type::ShortTyID:
155 case Type::IntTyID:
156 case Type::LongTyID: return ConstantSInt::get(Ty, -1);
157
158 case Type::UByteTyID:
159 case Type::UShortTyID:
160 case Type::UIntTyID:
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);
167 }
Chris Lattner227b86c2002-08-14 17:12:13 +0000168 default: return 0;
Chris Lattner9fb96412002-08-13 17:50:20 +0000169 }
170}
171
Chris Lattner16803122003-03-10 22:39:02 +0000172bool 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;
177}
178
Chris Lattner9fb96412002-08-13 17:50:20 +0000179
Chris Lattner00950542001-06-06 20:29:01 +0000180//===----------------------------------------------------------------------===//
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000181// ConstantXXX Classes
Chris Lattner00950542001-06-06 20:29:01 +0000182//===----------------------------------------------------------------------===//
183
184//===----------------------------------------------------------------------===//
185// Normal Constructors
186
Chris Lattner9fb96412002-08-13 17:50:20 +0000187ConstantBool::ConstantBool(bool V) : ConstantIntegral(Type::BoolTy) {
Chris Lattner00950542001-06-06 20:29:01 +0000188 Val = V;
189}
Chris Lattner531daef2001-09-07 16:46:31 +0000190
Chris Lattner9fb96412002-08-13 17:50:20 +0000191ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : ConstantIntegral(Ty) {
Chris Lattner531daef2001-09-07 16:46:31 +0000192 Val.Unsigned = V;
Chris Lattnerd7fa0fd2001-07-21 19:16:08 +0000193}
Chris Lattner00950542001-06-06 20:29:01 +0000194
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000195ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
Chris Lattner562219d2002-09-11 01:21:04 +0000196 assert(Ty->isInteger() && Ty->isSigned() &&
197 "Illegal type for unsigned integer constant!");
Chris Lattner37bf6302001-07-20 19:16:02 +0000198 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner00950542001-06-06 20:29:01 +0000199}
200
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000201ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
Chris Lattner562219d2002-09-11 01:21:04 +0000202 assert(Ty->isInteger() && Ty->isUnsigned() &&
203 "Illegal type for unsigned integer constant!");
Chris Lattner37bf6302001-07-20 19:16:02 +0000204 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner00950542001-06-06 20:29:01 +0000205}
206
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000207ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
Chris Lattner37bf6302001-07-20 19:16:02 +0000208 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner00950542001-06-06 20:29:01 +0000209 Val = V;
210}
211
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000212ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattner697954c2002-01-20 22:54:45 +0000213 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattnerd43c7d22002-10-08 23:33:52 +0000214 Operands.reserve(V.size());
215 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattner00950542001-06-06 20:29:01 +0000216 assert(V[i]->getType() == T->getElementType());
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000217 Operands.push_back(Use(V[i], this));
Chris Lattner00950542001-06-06 20:29:01 +0000218 }
219}
220
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000221ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattner697954c2002-01-20 22:54:45 +0000222 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner00950542001-06-06 20:29:01 +0000223 const StructType::ElementTypes &ETypes = T->getElementTypes();
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000224 assert(V.size() == ETypes.size() &&
225 "Invalid initializer vector for constant structure");
Chris Lattnerd43c7d22002-10-08 23:33:52 +0000226 Operands.reserve(V.size());
227 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattner00950542001-06-06 20:29:01 +0000228 assert(V[i]->getType() == ETypes[i]);
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000229 Operands.push_back(Use(V[i], this));
Chris Lattner00950542001-06-06 20:29:01 +0000230 }
231}
232
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000233ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
234 : ConstantPointer(GV->getType()) {
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000235 Operands.push_back(Use(GV, this));
236}
237
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000238ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
239 : Constant(Ty), iType(Opcode) {
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000240 Operands.push_back(Use(C, this));
241}
242
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000243ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
244 : Constant(C1->getType()), iType(Opcode) {
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000245 Operands.push_back(Use(C1, this));
246 Operands.push_back(Use(C2, this));
247}
248
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000249ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
250 const Type *DestTy)
251 : Constant(DestTy), iType(Instruction::GetElementPtr) {
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000252 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));
256}
257
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000258
Chris Lattner00950542001-06-06 20:29:01 +0000259
260//===----------------------------------------------------------------------===//
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000261// classof implementations
262
Chris Lattner9fb96412002-08-13 17:50:20 +0000263bool ConstantIntegral::classof(const Constant *CPV) {
Chris Lattner0c4e8862002-09-03 01:08:28 +0000264 return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
Chris Lattner994b9f32002-08-12 21:21:21 +0000265}
266
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000267bool ConstantInt::classof(const Constant *CPV) {
Chris Lattner0c4e8862002-09-03 01:08:28 +0000268 return CPV->getType()->isInteger() && !isa<ConstantExpr>(CPV);
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000269}
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000270bool ConstantSInt::classof(const Constant *CPV) {
Chris Lattner6183b922002-07-18 00:14:50 +0000271 return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000272}
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000273bool ConstantUInt::classof(const Constant *CPV) {
Chris Lattner6183b922002-07-18 00:14:50 +0000274 return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000275}
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000276bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000277 const Type *Ty = CPV->getType();
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000278 return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
Chris Lattner6183b922002-07-18 00:14:50 +0000279 !isa<ConstantExpr>(CPV));
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000280}
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000281bool ConstantArray::classof(const Constant *CPV) {
Chris Lattner6183b922002-07-18 00:14:50 +0000282 return isa<ArrayType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000283}
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000284bool ConstantStruct::classof(const Constant *CPV) {
Chris Lattner6183b922002-07-18 00:14:50 +0000285 return isa<StructType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000286}
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000287bool ConstantPointer::classof(const Constant *CPV) {
Chris Lattner6183b922002-07-18 00:14:50 +0000288 return (isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV));
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000289}
290
291
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000292
Chris Lattner00950542001-06-06 20:29:01 +0000293//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +0000294// isValueValidForType implementations
295
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000296bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner00950542001-06-06 20:29:01 +0000297 switch (Ty->getPrimitiveID()) {
298 default:
299 return false; // These can't be represented as integers!!!
300
301 // Signed types...
302 case Type::SByteTyID:
303 return (Val <= INT8_MAX && Val >= INT8_MIN);
304 case Type::ShortTyID:
305 return (Val <= INT16_MAX && Val >= INT16_MIN);
306 case Type::IntTyID:
307 return (Val <= INT32_MAX && Val >= INT32_MIN);
308 case Type::LongTyID:
309 return true; // This is the largest type...
310 }
311 assert(0 && "WTF?");
312 return false;
313}
314
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000315bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner00950542001-06-06 20:29:01 +0000316 switch (Ty->getPrimitiveID()) {
317 default:
318 return false; // These can't be represented as integers!!!
319
320 // Unsigned types...
321 case Type::UByteTyID:
322 return (Val <= UINT8_MAX);
323 case Type::UShortTyID:
324 return (Val <= UINT16_MAX);
325 case Type::UIntTyID:
326 return (Val <= UINT32_MAX);
327 case Type::ULongTyID:
328 return true; // This is the largest type...
329 }
330 assert(0 && "WTF?");
331 return false;
332}
333
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000334bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner00950542001-06-06 20:29:01 +0000335 switch (Ty->getPrimitiveID()) {
336 default:
337 return false; // These can't be represented as floating point!
338
339 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner00950542001-06-06 20:29:01 +0000340 case Type::FloatTyID:
Chris Lattnera59c2662001-07-15 00:18:39 +0000341 /*
Chris Lattner00950542001-06-06 20:29:01 +0000342 return (Val <= UINT8_MAX);
343 */
344 case Type::DoubleTyID:
345 return true; // This is the largest type...
346 }
347};
Chris Lattner37bf6302001-07-20 19:16:02 +0000348
Chris Lattner531daef2001-09-07 16:46:31 +0000349//===----------------------------------------------------------------------===//
Chris Lattnerc251f9e2002-10-09 23:12:25 +0000350// replaceUsesOfWithOnConstant implementations
351
352void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To) {
353 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
354
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);
361 }
362
363 ConstantArray *Replacement = ConstantArray::get(getType(), Values);
364 assert(Replacement != this && "I didn't contain From!");
365
366 // Everyone using this now uses the replacement...
367 replaceAllUsesWith(Replacement);
368
369 // Delete the old constant!
370 destroyConstant();
371}
372
373void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To) {
374 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
375
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);
382 }
383
384 ConstantStruct *Replacement = ConstantStruct::get(getType(), Values);
385 assert(Replacement != this && "I didn't contain From!");
386
387 // Everyone using this now uses the replacement...
388 replaceAllUsesWith(Replacement);
389
390 // Delete the old constant!
391 destroyConstant();
392}
393
394void 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));
399
400 // Everyone using this now uses the replacement...
401 replaceAllUsesWith(Replacement);
402
403 // Delete the old constant!
404 destroyConstant();
405 } else {
406 // Just replace ourselves with the To value specified.
407 replaceAllUsesWith(To);
408
409 // Delete the old constant!
410 destroyConstant();
411 }
412}
413
Chris Lattner3b9922f2003-05-14 17:51:05 +0000414void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV) {
415 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
416 Constant *To = cast<Constant>(ToV);
Chris Lattnerc251f9e2002-10-09 23:12:25 +0000417
Chris Lattnerfb242b62003-04-16 22:40:51 +0000418 Constant *Replacement = 0;
Chris Lattnerc251f9e2002-10-09 23:12:25 +0000419 if (getOpcode() == Instruction::GetElementPtr) {
420 std::vector<Constant*> Indices;
Chris Lattner3b9922f2003-05-14 17:51:05 +0000421 Constant *Pointer = getOperand(0);
Chris Lattnerc251f9e2002-10-09 23:12:25 +0000422 Indices.reserve(getNumOperands()-1);
Chris Lattner3b9922f2003-05-14 17:51:05 +0000423 if (Pointer == From) Pointer = To;
Chris Lattnerc251f9e2002-10-09 23:12:25 +0000424
425 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Chris Lattner3b9922f2003-05-14 17:51:05 +0000426 Constant *Val = getOperand(i);
427 if (Val == From) Val = To;
Chris Lattnerc251f9e2002-10-09 23:12:25 +0000428 Indices.push_back(Val);
429 }
430 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
431 } else if (getOpcode() == Instruction::Cast) {
432 assert(getOperand(0) == From && "Cast only has one use!");
Chris Lattner3b9922f2003-05-14 17:51:05 +0000433 Replacement = ConstantExpr::getCast(To, getType());
Chris Lattnerc251f9e2002-10-09 23:12:25 +0000434 } else if (getNumOperands() == 2) {
Chris Lattner3b9922f2003-05-14 17:51:05 +0000435 Constant *C1 = getOperand(0);
436 Constant *C2 = getOperand(1);
437 if (C1 == From) C1 = To;
438 if (C2 == From) C2 = To;
Chris Lattnerc251f9e2002-10-09 23:12:25 +0000439 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
440 } else {
441 assert(0 && "Unknown ConstantExpr type!");
442 return;
443 }
444
445 assert(Replacement != this && "I didn't contain From!");
446
447 // Everyone using this now uses the replacement...
448 replaceAllUsesWith(Replacement);
449
450 // Delete the old constant!
451 destroyConstant();
452}
453
Chris Lattnerc251f9e2002-10-09 23:12:25 +0000454//===----------------------------------------------------------------------===//
Chris Lattner531daef2001-09-07 16:46:31 +0000455// Factory Function Implementation
456
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000457// ConstantCreator - A class that is used to create constants by
458// ValueMap*. This class should be partially specialized if there is
459// something strange that needs to be done to interface to the ctor for the
460// constant.
461//
462template<class ConstantClass, class TypeClass, class ValType>
463struct ConstantCreator {
464 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
465 return new ConstantClass(Ty, V);
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000466 }
Chris Lattner531daef2001-09-07 16:46:31 +0000467};
468
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000469namespace {
470 template<class ValType, class TypeClass, class ConstantClass>
471 class ValueMap {
472 protected:
473 typedef std::pair<const TypeClass*, ValType> ConstHashKey;
474 std::map<ConstHashKey, ConstantClass *> Map;
475 public:
476 // getOrCreate - Return the specified constant from the map, creating it if
477 // necessary.
478 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
479 ConstHashKey Lookup(Ty, V);
480 typename std::map<ConstHashKey,ConstantClass *>::iterator I =
481 Map.lower_bound(Lookup);
482 if (I != Map.end() && I->first == Lookup)
483 return I->second; // Is it in the map?
484
485 // If no preexisting value, create one now...
486 ConstantClass *Result =
487 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
488
489 Map.insert(I, std::make_pair(ConstHashKey(Ty, V), Result));
490 return Result;
491 }
492
493 void remove(ConstantClass *CP) {
494 // FIXME: This could be sped up a LOT. If this gets to be a performance
495 // problem, someone should look at this.
496 for (typename std::map<ConstHashKey, ConstantClass*>::iterator
497 I = Map.begin(), E = Map.end(); I != E; ++I)
498 if (I->second == CP) {
499 Map.erase(I);
500 return;
501 }
502 assert(0 && "Constant not found in constant table!");
503 }
504 };
505}
506
507
508
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000509//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner531daef2001-09-07 16:46:31 +0000510//
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000511static ValueMap< int64_t, Type, ConstantSInt> SIntConstants;
512static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants;
Chris Lattner531daef2001-09-07 16:46:31 +0000513
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000514ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000515 return SIntConstants.getOrCreate(Ty, V);
Chris Lattner531daef2001-09-07 16:46:31 +0000516}
517
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000518ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000519 return UIntConstants.getOrCreate(Ty, V);
Chris Lattner531daef2001-09-07 16:46:31 +0000520}
521
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000522ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner531daef2001-09-07 16:46:31 +0000523 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000524 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
525 return ConstantUInt::get(Ty, V);
Chris Lattner531daef2001-09-07 16:46:31 +0000526}
527
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000528//---- ConstantFP::get() implementation...
Chris Lattner531daef2001-09-07 16:46:31 +0000529//
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000530static ValueMap<double, Type, ConstantFP> FPConstants;
Chris Lattner531daef2001-09-07 16:46:31 +0000531
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000532ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000533 return FPConstants.getOrCreate(Ty, V);
Chris Lattner531daef2001-09-07 16:46:31 +0000534}
535
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000536//---- ConstantArray::get() implementation...
Chris Lattner531daef2001-09-07 16:46:31 +0000537//
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000538static ValueMap<std::vector<Constant*>, ArrayType,
539 ConstantArray> ArrayConstants;
Chris Lattner531daef2001-09-07 16:46:31 +0000540
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000541ConstantArray *ConstantArray::get(const ArrayType *Ty,
Chris Lattner697954c2002-01-20 22:54:45 +0000542 const std::vector<Constant*> &V) {
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000543 return ArrayConstants.getOrCreate(Ty, V);
Chris Lattner531daef2001-09-07 16:46:31 +0000544}
545
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000546// destroyConstant - Remove the constant from the constant table...
547//
548void ConstantArray::destroyConstant() {
549 ArrayConstants.remove(this);
550 destroyConstantImpl();
551}
552
553/// refineAbstractType - If this callback is invoked, then this constant is of a
554/// derived type, change all users to use a concrete constant of the new type.
555///
556void ConstantArray::refineAbstractType(const DerivedType *OldTy,
557 const Type *NewTy) {
558 Value::refineAbstractType(OldTy, NewTy);
559
560 // Make everyone now use a constant of the new type...
561 std::vector<Constant*> C;
562 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
563 C.push_back(cast<Constant>(getOperand(i)));
564 replaceAllUsesWith(ConstantArray::get(cast<ArrayType>(NewTy),
565 C));
566 destroyConstant(); // This constant is now dead, destroy it.
567}
568
569
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000570// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattnerc5bdb242001-10-14 23:54:12 +0000571// contain the specified string. A null terminator is added to the specified
572// string so that it may be used in a natural way...
573//
Chris Lattner697954c2002-01-20 22:54:45 +0000574ConstantArray *ConstantArray::get(const std::string &Str) {
575 std::vector<Constant*> ElementVals;
Chris Lattnerc5bdb242001-10-14 23:54:12 +0000576
577 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnera2644f62001-12-14 16:41:18 +0000578 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattnerc5bdb242001-10-14 23:54:12 +0000579
580 // Add a null terminator to the string...
Chris Lattnera2644f62001-12-14 16:41:18 +0000581 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattnerc5bdb242001-10-14 23:54:12 +0000582
Chris Lattnera2644f62001-12-14 16:41:18 +0000583 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000584 return ConstantArray::get(ATy, ElementVals);
Vikram S. Advedb2da492001-10-14 23:17:20 +0000585}
586
Chris Lattner93aeea32002-08-26 17:53:56 +0000587// getAsString - If the sub-element type of this array is either sbyte or ubyte,
588// then this method converts the array to an std::string and returns it.
589// Otherwise, it asserts out.
590//
591std::string ConstantArray::getAsString() const {
592 std::string Result;
593 if (getType()->getElementType() == Type::SByteTy)
594 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
595 Result += (char)cast<ConstantSInt>(getOperand(i))->getValue();
596 else {
597 assert(getType()->getElementType() == Type::UByteTy && "Not a string!");
598 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
599 Result += (char)cast<ConstantUInt>(getOperand(i))->getValue();
600 }
601 return Result;
602}
603
604
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000605//---- ConstantStruct::get() implementation...
Chris Lattner531daef2001-09-07 16:46:31 +0000606//
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000607static ValueMap<std::vector<Constant*>, StructType,
608 ConstantStruct> StructConstants;
Chris Lattner531daef2001-09-07 16:46:31 +0000609
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000610ConstantStruct *ConstantStruct::get(const StructType *Ty,
Chris Lattner697954c2002-01-20 22:54:45 +0000611 const std::vector<Constant*> &V) {
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000612 return StructConstants.getOrCreate(Ty, V);
Chris Lattner531daef2001-09-07 16:46:31 +0000613}
Chris Lattner6a57baa2001-10-03 15:39:36 +0000614
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000615// destroyConstant - Remove the constant from the constant table...
Chris Lattner6a57baa2001-10-03 15:39:36 +0000616//
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000617void ConstantStruct::destroyConstant() {
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000618 StructConstants.remove(this);
619 destroyConstantImpl();
620}
Chris Lattner6a57baa2001-10-03 15:39:36 +0000621
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000622/// refineAbstractType - If this callback is invoked, then this constant is of a
623/// derived type, change all users to use a concrete constant of the new type.
624///
625void ConstantStruct::refineAbstractType(const DerivedType *OldTy,
626 const Type *NewTy) {
627 Value::refineAbstractType(OldTy, NewTy);
628
629 // Make everyone now use a constant of the new type...
630 std::vector<Constant*> C;
631 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
632 C.push_back(cast<Constant>(getOperand(i)));
633 replaceAllUsesWith(ConstantStruct::get(cast<StructType>(NewTy),
634 C));
635 destroyConstant(); // This constant is now dead, destroy it.
636}
637
Chris Lattner41661fd2002-08-18 00:40:04 +0000638
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000639//---- ConstantPointerNull::get() implementation...
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000640//
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000641
642// ConstantPointerNull does not take extra "value" argument...
643template<class ValType>
644struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
645 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
646 return new ConstantPointerNull(Ty);
647 }
648};
649
650static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000651
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000652ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000653 return NullPtrConstants.getOrCreate(Ty, 0);
Chris Lattner6a57baa2001-10-03 15:39:36 +0000654}
655
Chris Lattner41661fd2002-08-18 00:40:04 +0000656// destroyConstant - Remove the constant from the constant table...
657//
658void ConstantPointerNull::destroyConstant() {
659 NullPtrConstants.remove(this);
660 destroyConstantImpl();
661}
662
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000663/// refineAbstractType - If this callback is invoked, then this constant is of a
664/// derived type, change all users to use a concrete constant of the new type.
665///
666void ConstantPointerNull::refineAbstractType(const DerivedType *OldTy,
667 const Type *NewTy) {
668 Value::refineAbstractType(OldTy, NewTy);
669
670 // Make everyone now use a constant of the new type...
671 replaceAllUsesWith(ConstantPointerNull::get(cast<PointerType>(NewTy)));
672
673 // This constant is now dead, destroy it.
674 destroyConstant();
675}
676
677
Chris Lattner41661fd2002-08-18 00:40:04 +0000678
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000679//---- ConstantPointerRef::get() implementation...
Chris Lattner43873702001-10-03 19:28:15 +0000680//
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000681ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000682 assert(GV->getParent() && "Global Value must be attached to a module!");
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000683
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000684 // The Module handles the pointer reference sharing...
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000685 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000686}
687
Chris Lattner41661fd2002-08-18 00:40:04 +0000688// destroyConstant - Remove the constant from the constant table...
689//
690void ConstantPointerRef::destroyConstant() {
691 getValue()->getParent()->destroyConstantPointerRef(this);
692 destroyConstantImpl();
693}
694
695
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000696//---- ConstantExpr::get() implementations...
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000697//
Chris Lattner9bc02a42003-05-13 21:37:02 +0000698typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000699
700template<>
701struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
702 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
703 if (V.first == Instruction::Cast)
704 return new ConstantExpr(Instruction::Cast, V.second[0], Ty);
705 if ((V.first >= Instruction::BinaryOpsBegin &&
706 V.first < Instruction::BinaryOpsEnd) ||
707 V.first == Instruction::Shl || V.first == Instruction::Shr)
708 return new ConstantExpr(V.first, V.second[0], V.second[1]);
709
710 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
711
712 // Check that the indices list is valid...
713 std::vector<Value*> ValIdxList(V.second.begin()+1, V.second.end());
714 const Type *DestTy = GetElementPtrInst::getIndexedType(Ty, ValIdxList,
715 true);
716 assert(DestTy && "Invalid index list for GetElementPtr expression");
717
718 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
719 return new ConstantExpr(V.second[0], IdxList, PointerType::get(DestTy));
720 }
721};
722
723static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
Vikram S. Adved0b1bb02002-07-15 18:19:33 +0000724
Chris Lattnerfb242b62003-04-16 22:40:51 +0000725Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattnerd628f6a2003-04-17 19:24:48 +0000726 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
727 return FC; // Fold a few common cases...
728
Vikram S. Adved0b1bb02002-07-15 18:19:33 +0000729 // Look up the constant in the table first to ensure uniqueness
Chris Lattner9bc02a42003-05-13 21:37:02 +0000730 std::vector<Constant*> argVec(1, C);
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000731 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
732 return ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000733}
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000734
Chris Lattnerfb242b62003-04-16 22:40:51 +0000735Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattnerf31f5832003-05-21 17:49:25 +0000736 // Check the operands for consistency first
737 assert((Opcode >= Instruction::BinaryOpsBegin &&
738 Opcode < Instruction::BinaryOpsEnd) &&
739 "Invalid opcode in binary constant expression");
740 assert(C1->getType() == C2->getType() &&
741 "Operand types in binary constant expression should match");
Chris Lattnerd628f6a2003-04-17 19:24:48 +0000742
743 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
744 return FC; // Fold a few common cases...
745
Chris Lattner9bc02a42003-05-13 21:37:02 +0000746 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000747 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
748 return ExprConstants.getOrCreate(C1->getType(), Key);
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000749}
750
Chris Lattnerf31f5832003-05-21 17:49:25 +0000751/// getShift - Return a shift left or shift right constant expr
752Constant *ConstantExpr::getShift(unsigned Opcode, Constant *C1, Constant *C2) {
753 // Check the operands for consistency first
754 assert((Opcode == Instruction::Shl ||
755 Opcode == Instruction::Shr) &&
756 "Invalid opcode in binary constant expression");
757 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
758 "Invalid operand types for Shift constant expr!");
759
760 if (Constant *FC = ConstantFoldShiftInstruction(Opcode, C1, C2))
761 return FC; // Fold a few common cases...
762
763 // Look up the constant in the table first to ensure uniqueness
764 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000765 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
766 return ExprConstants.getOrCreate(C1->getType(), Key);
Chris Lattnerf31f5832003-05-21 17:49:25 +0000767}
768
769
Chris Lattnerfb242b62003-04-16 22:40:51 +0000770Constant *ConstantExpr::getGetElementPtr(Constant *C,
771 const std::vector<Constant*> &IdxList){
Chris Lattnerd628f6a2003-04-17 19:24:48 +0000772 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
773 return FC; // Fold a few common cases...
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000774 const Type *Ty = C->getType();
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000775 assert(isa<PointerType>(Ty) &&
776 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adved0b1bb02002-07-15 18:19:33 +0000777
778 // Look up the constant in the table first to ensure uniqueness
Chris Lattner9bc02a42003-05-13 21:37:02 +0000779 std::vector<Constant*> argVec(1, C);
Chris Lattner6183b922002-07-18 00:14:50 +0000780 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
Vikram S. Adved0b1bb02002-07-15 18:19:33 +0000781
Chris Lattner9bc02a42003-05-13 21:37:02 +0000782 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,argVec);
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000783 return ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adved0b1bb02002-07-15 18:19:33 +0000784}
785
786// destroyConstant - Remove the constant from the constant table...
787//
788void ConstantExpr::destroyConstant() {
789 ExprConstants.remove(this);
790 destroyConstantImpl();
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000791}
792
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000793/// refineAbstractType - If this callback is invoked, then this constant is of a
794/// derived type, change all users to use a concrete constant of the new type.
795///
796void ConstantExpr::refineAbstractType(const DerivedType *OldTy,
797 const Type *NewTy) {
798 Value::refineAbstractType(OldTy, NewTy);
799
800 // FIXME: These need to use a lower-level implementation method, because the
801 // ::get methods intuit the type of the result based on the types of the
802 // operands. The operand types may not have had their types resolved yet.
803 //
804 if (getOpcode() == Instruction::Cast) {
805 replaceAllUsesWith(getCast(getOperand(0), NewTy));
806 } else if (getOpcode() >= Instruction::BinaryOpsBegin &&
807 getOpcode() < Instruction::BinaryOpsEnd) {
808 replaceAllUsesWith(get(getOpcode(), getOperand(0), getOperand(0)));
809 } else if (getOpcode() == Instruction::Shl || getOpcode() ==Instruction::Shr){
810 replaceAllUsesWith(getShift(getOpcode(), getOperand(0), getOperand(0)));
811 } else {
812 assert(getOpcode() == Instruction::GetElementPtr);
813
814 // Make everyone now use a constant of the new type...
815 std::vector<Constant*> C;
816 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
817 C.push_back(cast<Constant>(getOperand(i)));
818 replaceAllUsesWith(ConstantExpr::getGetElementPtr(getOperand(0),
819 C));
820 }
821 destroyConstant(); // This constant is now dead, destroy it.
822}
823
824
825
826
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000827const char *ConstantExpr::getOpcodeName() const {
828 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000829}
830
Chris Lattner34048e22002-10-14 03:30:23 +0000831unsigned Constant::mutateReferences(Value *OldV, Value *NewV) {
832 // Uses of constant pointer refs are global values, not constants!
833 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
834 GlobalValue *NewGV = cast<GlobalValue>(NewV);
835 GlobalValue *OldGV = CPR->getValue();
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000836
Chris Lattner34048e22002-10-14 03:30:23 +0000837 assert(OldGV == OldV && "Cannot mutate old value if I'm not using it!");
Chris Lattner34048e22002-10-14 03:30:23 +0000838 Operands[0] = NewGV;
Chris Lattner608f4b02003-05-15 19:37:21 +0000839 OldGV->getParent()->mutateConstantPointerRef(OldGV, NewGV);
Chris Lattner34048e22002-10-14 03:30:23 +0000840 return 1;
841 } else {
842 Constant *NewC = cast<Constant>(NewV);
843 unsigned NumReplaced = 0;
844 for (unsigned i = 0, N = getNumOperands(); i != N; ++i)
845 if (Operands[i] == OldV) {
846 ++NumReplaced;
847 Operands[i] = NewC;
848 }
849 return NumReplaced;
850 }
Chris Lattner43873702001-10-03 19:28:15 +0000851}