blob: 6a91757641a7c42a2e227c6e00bc7479a3e4d1ed [file] [log] [blame]
Chris Lattnerca142372002-04-28 19:55:58 +00001//===-- Constants.cpp - Implement Constant nodes -----------------*- C++ -*--=//
Chris Lattner2f7c9632001-06-06 20:29:01 +00002//
Chris Lattner3462ae32001-12-03 22:26:30 +00003// This file implements the Constant* classes...
Chris Lattner2f7c9632001-06-06 20:29:01 +00004//
5//===----------------------------------------------------------------------===//
6
Chris Lattnerca142372002-04-28 19:55:58 +00007#include "llvm/Constants.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +00008#include "llvm/DerivedTypes.h"
Vikram S. Adve4e537b22002-07-14 23:13:17 +00009#include "llvm/iMemory.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000010#include "llvm/SymbolTable.h"
Chris Lattnerd7a73302001-10-13 06:57:33 +000011#include "llvm/Module.h"
Chris Lattner5de22042001-11-27 00:03:19 +000012#include "Support/StringExtras.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000013#include <algorithm>
Chris Lattner2f7c9632001-06-06 20:29:01 +000014
Chris Lattner7f74a562002-01-20 22:54:45 +000015using std::map;
16using std::pair;
17using std::make_pair;
Anand Shukla991873f2002-07-16 00:02:17 +000018using std::vector;
Chris Lattner7f74a562002-01-20 22:54:45 +000019
Chris Lattner3462ae32001-12-03 22:26:30 +000020ConstantBool *ConstantBool::True = new ConstantBool(true);
21ConstantBool *ConstantBool::False = new ConstantBool(false);
Chris Lattner2f7c9632001-06-06 20:29:01 +000022
Chris Lattner9655e542001-07-20 19:16:02 +000023
Chris Lattner2f7c9632001-06-06 20:29:01 +000024//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000025// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000026//===----------------------------------------------------------------------===//
27
28// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +000029void Constant::setName(const std::string &Name, SymbolTable *ST) {
Chris Lattner49d855c2001-09-07 16:46:31 +000030 assert(ST && "Type::setName - Must provide symbol table argument!");
31
32 if (Name.size()) ST->insert(Name, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000033}
34
Chris Lattner3462ae32001-12-03 22:26:30 +000035void Constant::destroyConstantImpl() {
36 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000037 // 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
Chris Lattner3462ae32001-12-03 22:26:30 +000041 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000042 //
43 while (!use_empty()) {
44 Value *V = use_back();
45#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000046 if (!isa<Constant>(V))
47 std::cerr << "While deleting: " << *this
48 << "\n\nUse still stuck around after Def is destroyed: "
49 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000050#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000051 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Chris Lattner3462ae32001-12-03 22:26:30 +000052 Constant *CPV = cast<Constant>(V);
Chris Lattnerd7a73302001-10-13 06:57:33 +000053 CPV->destroyConstant();
54
55 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000056 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000057 }
58
59 // Value has no outstanding references it is safe to delete it now...
60 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000061}
Chris Lattner2f7c9632001-06-06 20:29:01 +000062
Chris Lattnerb1585a92002-08-13 17:50:20 +000063// Static constructor to create a '0' constant of arbitrary type...
64Constant *Constant::getNullValue(const Type *Ty) {
65 switch (Ty->getPrimitiveID()) {
66 case Type::BoolTyID: return ConstantBool::get(false);
67 case Type::SByteTyID:
68 case Type::ShortTyID:
69 case Type::IntTyID:
70 case Type::LongTyID: return ConstantSInt::get(Ty, 0);
71
72 case Type::UByteTyID:
73 case Type::UShortTyID:
74 case Type::UIntTyID:
75 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
76
77 case Type::FloatTyID:
78 case Type::DoubleTyID: return ConstantFP::get(Ty, 0);
79
80 case Type::PointerTyID:
81 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattnerc33ae672003-03-06 21:02:18 +000082 case Type::StructTyID: {
83 const StructType *ST = cast<StructType>(Ty);
84
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);
91 }
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));
97 }
Chris Lattnerb1585a92002-08-13 17:50:20 +000098 default:
Chris Lattnerc33ae672003-03-06 21:02:18 +000099 // Function, Type, Label, or Opaque type?
100 assert(0 && "Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000101 return 0;
102 }
103}
104
105// Static constructor to create the maximum constant of an integral type...
106ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
107 switch (Ty->getPrimitiveID()) {
108 case Type::BoolTyID: return ConstantBool::True;
109 case Type::SByteTyID:
110 case Type::ShortTyID:
111 case Type::IntTyID:
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);
118 }
119
120 case Type::UByteTyID:
121 case Type::UShortTyID:
122 case Type::UIntTyID:
123 case Type::ULongTyID: return getAllOnesValue(Ty);
124
Chris Lattner31408f72002-08-14 17:12:13 +0000125 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000126 }
127}
128
129// Static constructor to create the minimum constant for an integral type...
130ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
131 switch (Ty->getPrimitiveID()) {
132 case Type::BoolTyID: return ConstantBool::False;
133 case Type::SByteTyID:
134 case Type::ShortTyID:
135 case Type::IntTyID:
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);
142 }
143
144 case Type::UByteTyID:
145 case Type::UShortTyID:
146 case Type::UIntTyID:
147 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
148
Chris Lattner31408f72002-08-14 17:12:13 +0000149 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000150 }
151}
152
153// Static constructor to create an integral constant with all bits set
154ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
155 switch (Ty->getPrimitiveID()) {
156 case Type::BoolTyID: return ConstantBool::True;
157 case Type::SByteTyID:
158 case Type::ShortTyID:
159 case Type::IntTyID:
160 case Type::LongTyID: return ConstantSInt::get(Ty, -1);
161
162 case Type::UByteTyID:
163 case Type::UShortTyID:
164 case Type::UIntTyID:
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);
171 }
Chris Lattner31408f72002-08-14 17:12:13 +0000172 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000173 }
174}
175
176
Chris Lattner2f7c9632001-06-06 20:29:01 +0000177//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +0000178// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +0000179//===----------------------------------------------------------------------===//
180
181//===----------------------------------------------------------------------===//
182// Normal Constructors
183
Chris Lattnerb1585a92002-08-13 17:50:20 +0000184ConstantBool::ConstantBool(bool V) : ConstantIntegral(Type::BoolTy) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000185 Val = V;
186}
Chris Lattner49d855c2001-09-07 16:46:31 +0000187
Chris Lattnerb1585a92002-08-13 17:50:20 +0000188ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : ConstantIntegral(Ty) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000189 Val.Unsigned = V;
Chris Lattner7309d662001-07-21 19:16:08 +0000190}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000191
Chris Lattner3462ae32001-12-03 22:26:30 +0000192ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000193 assert(Ty->isInteger() && Ty->isSigned() &&
194 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000195 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000196}
197
Chris Lattner3462ae32001-12-03 22:26:30 +0000198ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000199 assert(Ty->isInteger() && Ty->isUnsigned() &&
200 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000201 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000202}
203
Chris Lattner3462ae32001-12-03 22:26:30 +0000204ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
Chris Lattner9655e542001-07-20 19:16:02 +0000205 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000206 Val = V;
207}
208
Chris Lattner3462ae32001-12-03 22:26:30 +0000209ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000210 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner0d779712002-10-08 23:33:52 +0000211 Operands.reserve(V.size());
212 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000213 assert(V[i]->getType() == T->getElementType());
Chris Lattnera073acb2001-07-07 08:36:50 +0000214 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000215 }
216}
217
Chris Lattner3462ae32001-12-03 22:26:30 +0000218ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000219 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000220 const StructType::ElementTypes &ETypes = T->getElementTypes();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000221 assert(V.size() == ETypes.size() &&
222 "Invalid initializer vector for constant structure");
Chris Lattner0d779712002-10-08 23:33:52 +0000223 Operands.reserve(V.size());
224 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000225 assert(V[i]->getType() == ETypes[i]);
Chris Lattnera073acb2001-07-07 08:36:50 +0000226 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000227 }
228}
229
Chris Lattner3462ae32001-12-03 22:26:30 +0000230ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
231 : ConstantPointer(GV->getType()) {
Chris Lattner60e0dd72001-10-03 06:12:09 +0000232 Operands.push_back(Use(GV, this));
233}
234
Chris Lattner3cd8c562002-07-30 18:54:25 +0000235ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
236 : Constant(Ty), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000237 Operands.push_back(Use(C, this));
238}
239
Chris Lattner3cd8c562002-07-30 18:54:25 +0000240ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
241 : Constant(C1->getType()), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000242 Operands.push_back(Use(C1, this));
243 Operands.push_back(Use(C2, this));
244}
245
Chris Lattner3cd8c562002-07-30 18:54:25 +0000246ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
247 const Type *DestTy)
248 : Constant(DestTy), iType(Instruction::GetElementPtr) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000249 Operands.reserve(1+IdxList.size());
250 Operands.push_back(Use(C, this));
251 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
252 Operands.push_back(Use(IdxList[i], this));
253}
254
Chris Lattner60e0dd72001-10-03 06:12:09 +0000255
Chris Lattner2f7c9632001-06-06 20:29:01 +0000256
257//===----------------------------------------------------------------------===//
Chris Lattnerd7a73302001-10-13 06:57:33 +0000258// classof implementations
259
Chris Lattnerb1585a92002-08-13 17:50:20 +0000260bool ConstantIntegral::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000261 return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
Chris Lattner41e99a02002-08-12 21:21:21 +0000262}
263
Chris Lattner3462ae32001-12-03 22:26:30 +0000264bool ConstantInt::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000265 return CPV->getType()->isInteger() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000266}
Chris Lattner3462ae32001-12-03 22:26:30 +0000267bool ConstantSInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000268 return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000269}
Chris Lattner3462ae32001-12-03 22:26:30 +0000270bool ConstantUInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000271 return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000272}
Chris Lattner3462ae32001-12-03 22:26:30 +0000273bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000274 const Type *Ty = CPV->getType();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000275 return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000276 !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000277}
Chris Lattner3462ae32001-12-03 22:26:30 +0000278bool ConstantArray::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000279 return isa<ArrayType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000280}
Chris Lattner3462ae32001-12-03 22:26:30 +0000281bool ConstantStruct::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000282 return isa<StructType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000283}
Chris Lattner3462ae32001-12-03 22:26:30 +0000284bool ConstantPointer::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000285 return (isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000286}
287
288
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000289
Chris Lattner2f7c9632001-06-06 20:29:01 +0000290//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000291// isValueValidForType implementations
292
Chris Lattner3462ae32001-12-03 22:26:30 +0000293bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000294 switch (Ty->getPrimitiveID()) {
295 default:
296 return false; // These can't be represented as integers!!!
297
298 // Signed types...
299 case Type::SByteTyID:
300 return (Val <= INT8_MAX && Val >= INT8_MIN);
301 case Type::ShortTyID:
302 return (Val <= INT16_MAX && Val >= INT16_MIN);
303 case Type::IntTyID:
304 return (Val <= INT32_MAX && Val >= INT32_MIN);
305 case Type::LongTyID:
306 return true; // This is the largest type...
307 }
308 assert(0 && "WTF?");
309 return false;
310}
311
Chris Lattner3462ae32001-12-03 22:26:30 +0000312bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000313 switch (Ty->getPrimitiveID()) {
314 default:
315 return false; // These can't be represented as integers!!!
316
317 // Unsigned types...
318 case Type::UByteTyID:
319 return (Val <= UINT8_MAX);
320 case Type::UShortTyID:
321 return (Val <= UINT16_MAX);
322 case Type::UIntTyID:
323 return (Val <= UINT32_MAX);
324 case Type::ULongTyID:
325 return true; // This is the largest type...
326 }
327 assert(0 && "WTF?");
328 return false;
329}
330
Chris Lattner3462ae32001-12-03 22:26:30 +0000331bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000332 switch (Ty->getPrimitiveID()) {
333 default:
334 return false; // These can't be represented as floating point!
335
336 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000337 case Type::FloatTyID:
Chris Lattnerd06dd692001-07-15 00:18:39 +0000338 /*
Chris Lattner2f7c9632001-06-06 20:29:01 +0000339 return (Val <= UINT8_MAX);
340 */
341 case Type::DoubleTyID:
342 return true; // This is the largest type...
343 }
344};
Chris Lattner9655e542001-07-20 19:16:02 +0000345
Chris Lattner49d855c2001-09-07 16:46:31 +0000346//===----------------------------------------------------------------------===//
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000347// replaceUsesOfWithOnConstant implementations
348
349void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To) {
350 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
351
352 std::vector<Constant*> Values;
353 Values.reserve(getValues().size()); // Build replacement array...
354 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
355 Constant *Val = cast<Constant>(getValues()[i]);
356 if (Val == From) Val = cast<Constant>(To);
357 Values.push_back(Val);
358 }
359
360 ConstantArray *Replacement = ConstantArray::get(getType(), Values);
361 assert(Replacement != this && "I didn't contain From!");
362
363 // Everyone using this now uses the replacement...
364 replaceAllUsesWith(Replacement);
365
366 // Delete the old constant!
367 destroyConstant();
368}
369
370void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To) {
371 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
372
373 std::vector<Constant*> Values;
374 Values.reserve(getValues().size());
375 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
376 Constant *Val = cast<Constant>(getValues()[i]);
377 if (Val == From) Val = cast<Constant>(To);
378 Values.push_back(Val);
379 }
380
381 ConstantStruct *Replacement = ConstantStruct::get(getType(), Values);
382 assert(Replacement != this && "I didn't contain From!");
383
384 // Everyone using this now uses the replacement...
385 replaceAllUsesWith(Replacement);
386
387 // Delete the old constant!
388 destroyConstant();
389}
390
391void ConstantPointerRef::replaceUsesOfWithOnConstant(Value *From, Value *To) {
392 if (isa<GlobalValue>(To)) {
393 assert(From == getOperand(0) && "Doesn't contain from!");
394 ConstantPointerRef *Replacement =
395 ConstantPointerRef::get(cast<GlobalValue>(To));
396
397 // Everyone using this now uses the replacement...
398 replaceAllUsesWith(Replacement);
399
400 // Delete the old constant!
401 destroyConstant();
402 } else {
403 // Just replace ourselves with the To value specified.
404 replaceAllUsesWith(To);
405
406 // Delete the old constant!
407 destroyConstant();
408 }
409}
410
411void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *To) {
412 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
413
414 ConstantExpr *Replacement = 0;
415 if (getOpcode() == Instruction::GetElementPtr) {
416 std::vector<Constant*> Indices;
417 Constant *Pointer = cast<Constant>(getOperand(0));
418 Indices.reserve(getNumOperands()-1);
419 if (Pointer == From) Pointer = cast<Constant>(To);
420
421 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
422 Constant *Val = cast<Constant>(getOperand(i));
423 if (Val == From) Val = cast<Constant>(To);
424 Indices.push_back(Val);
425 }
426 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
427 } else if (getOpcode() == Instruction::Cast) {
428 assert(getOperand(0) == From && "Cast only has one use!");
429 Replacement = ConstantExpr::getCast(cast<Constant>(To), getType());
430 } else if (getNumOperands() == 2) {
431 Constant *C1 = cast<Constant>(getOperand(0));
432 Constant *C2 = cast<Constant>(getOperand(1));
433 if (C1 == From) C1 = cast<Constant>(To);
434 if (C2 == From) C2 = cast<Constant>(To);
435 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
436 } else {
437 assert(0 && "Unknown ConstantExpr type!");
438 return;
439 }
440
441 assert(Replacement != this && "I didn't contain From!");
442
443 // Everyone using this now uses the replacement...
444 replaceAllUsesWith(Replacement);
445
446 // Delete the old constant!
447 destroyConstant();
448}
449
450
451
452//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000453// Factory Function Implementation
454
Chris Lattner3462ae32001-12-03 22:26:30 +0000455template<class ValType, class ConstantClass>
Chris Lattner49d855c2001-09-07 16:46:31 +0000456struct ValueMap {
457 typedef pair<const Type*, ValType> ConstHashKey;
Chris Lattner3462ae32001-12-03 22:26:30 +0000458 map<ConstHashKey, ConstantClass *> Map;
Chris Lattner49d855c2001-09-07 16:46:31 +0000459
Chris Lattner3462ae32001-12-03 22:26:30 +0000460 inline ConstantClass *get(const Type *Ty, ValType V) {
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000461 typename map<ConstHashKey,ConstantClass *>::iterator I =
Chris Lattner49d855c2001-09-07 16:46:31 +0000462 Map.find(ConstHashKey(Ty, V));
463 return (I != Map.end()) ? I->second : 0;
464 }
465
Chris Lattner3462ae32001-12-03 22:26:30 +0000466 inline void add(const Type *Ty, ValType V, ConstantClass *CP) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000467 Map.insert(make_pair(ConstHashKey(Ty, V), CP));
468 }
Chris Lattnerd7a73302001-10-13 06:57:33 +0000469
Chris Lattner3462ae32001-12-03 22:26:30 +0000470 inline void remove(ConstantClass *CP) {
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000471 for (typename map<ConstHashKey,ConstantClass *>::iterator I = Map.begin(),
Chris Lattnerd7a73302001-10-13 06:57:33 +0000472 E = Map.end(); I != E;++I)
473 if (I->second == CP) {
474 Map.erase(I);
475 return;
476 }
477 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000478};
479
Chris Lattner3462ae32001-12-03 22:26:30 +0000480//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000481//
Chris Lattner3462ae32001-12-03 22:26:30 +0000482static ValueMap<uint64_t, ConstantInt> IntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000483
Chris Lattner3462ae32001-12-03 22:26:30 +0000484ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
485 ConstantSInt *Result = (ConstantSInt*)IntConstants.get(Ty, (uint64_t)V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000486 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000487 IntConstants.add(Ty, V, Result = new ConstantSInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000488 return Result;
489}
490
Chris Lattner3462ae32001-12-03 22:26:30 +0000491ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
492 ConstantUInt *Result = (ConstantUInt*)IntConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000493 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000494 IntConstants.add(Ty, V, Result = new ConstantUInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000495 return Result;
496}
497
Chris Lattner3462ae32001-12-03 22:26:30 +0000498ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000499 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000500 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
501 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000502}
503
Chris Lattner3462ae32001-12-03 22:26:30 +0000504//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000505//
Chris Lattner3462ae32001-12-03 22:26:30 +0000506static ValueMap<double, ConstantFP> FPConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000507
Chris Lattner3462ae32001-12-03 22:26:30 +0000508ConstantFP *ConstantFP::get(const Type *Ty, double V) {
509 ConstantFP *Result = FPConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000510 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000511 FPConstants.add(Ty, V, Result = new ConstantFP(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000512 return Result;
513}
514
Chris Lattner3462ae32001-12-03 22:26:30 +0000515//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000516//
Chris Lattner7f74a562002-01-20 22:54:45 +0000517static ValueMap<std::vector<Constant*>, ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000518
Chris Lattner3462ae32001-12-03 22:26:30 +0000519ConstantArray *ConstantArray::get(const ArrayType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000520 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000521 ConstantArray *Result = ArrayConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000522 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000523 ArrayConstants.add(Ty, V, Result = new ConstantArray(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000524 return Result;
525}
526
Chris Lattner3462ae32001-12-03 22:26:30 +0000527// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000528// contain the specified string. A null terminator is added to the specified
529// string so that it may be used in a natural way...
530//
Chris Lattner7f74a562002-01-20 22:54:45 +0000531ConstantArray *ConstantArray::get(const std::string &Str) {
532 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000533
534 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000535 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000536
537 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000538 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000539
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000540 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000541 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000542}
543
544
Chris Lattnerd7a73302001-10-13 06:57:33 +0000545// destroyConstant - Remove the constant from the constant table...
546//
Chris Lattner3462ae32001-12-03 22:26:30 +0000547void ConstantArray::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000548 ArrayConstants.remove(this);
549 destroyConstantImpl();
550}
551
Chris Lattner81fabb02002-08-26 17:53:56 +0000552// getAsString - If the sub-element type of this array is either sbyte or ubyte,
553// then this method converts the array to an std::string and returns it.
554// Otherwise, it asserts out.
555//
556std::string ConstantArray::getAsString() const {
557 std::string Result;
558 if (getType()->getElementType() == Type::SByteTy)
559 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
560 Result += (char)cast<ConstantSInt>(getOperand(i))->getValue();
561 else {
562 assert(getType()->getElementType() == Type::UByteTy && "Not a string!");
563 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
564 Result += (char)cast<ConstantUInt>(getOperand(i))->getValue();
565 }
566 return Result;
567}
568
569
Chris Lattner3462ae32001-12-03 22:26:30 +0000570//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000571//
Chris Lattner7f74a562002-01-20 22:54:45 +0000572static ValueMap<std::vector<Constant*>, ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000573
Chris Lattner3462ae32001-12-03 22:26:30 +0000574ConstantStruct *ConstantStruct::get(const StructType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000575 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000576 ConstantStruct *Result = StructConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000577 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000578 StructConstants.add(Ty, V, Result = new ConstantStruct(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000579 return Result;
580}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000581
Chris Lattnerd7a73302001-10-13 06:57:33 +0000582// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000583//
Chris Lattner3462ae32001-12-03 22:26:30 +0000584void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000585 StructConstants.remove(this);
586 destroyConstantImpl();
587}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000588
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000589
Chris Lattner3462ae32001-12-03 22:26:30 +0000590//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000591//
Chris Lattner3462ae32001-12-03 22:26:30 +0000592static ValueMap<char, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000593
Chris Lattner3462ae32001-12-03 22:26:30 +0000594ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
595 ConstantPointerNull *Result = NullPtrConstants.get(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000596 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000597 NullPtrConstants.add(Ty, 0, Result = new ConstantPointerNull(Ty));
Chris Lattner883ad0b2001-10-03 15:39:36 +0000598 return Result;
599}
600
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000601// destroyConstant - Remove the constant from the constant table...
602//
603void ConstantPointerNull::destroyConstant() {
604 NullPtrConstants.remove(this);
605 destroyConstantImpl();
606}
607
608
Chris Lattner3462ae32001-12-03 22:26:30 +0000609//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +0000610//
Chris Lattner3462ae32001-12-03 22:26:30 +0000611ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000612 assert(GV->getParent() && "Global Value must be attached to a module!");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000613
Chris Lattnerd7a73302001-10-13 06:57:33 +0000614 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +0000615 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000616}
617
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000618// destroyConstant - Remove the constant from the constant table...
619//
620void ConstantPointerRef::destroyConstant() {
621 getValue()->getParent()->destroyConstantPointerRef(this);
622 destroyConstantImpl();
623}
624
625
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000626//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000627//
Vikram S. Adve4c485332002-07-15 18:19:33 +0000628typedef pair<unsigned, vector<Constant*> > ExprMapKeyType;
629static ValueMap<const ExprMapKeyType, ConstantExpr> ExprConstants;
630
Chris Lattner330b7ac2002-08-14 18:24:09 +0000631ConstantExpr *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Vikram S. Adve4c485332002-07-15 18:19:33 +0000632
633 // Look up the constant in the table first to ensure uniqueness
634 vector<Constant*> argVec(1, C);
Chris Lattner330b7ac2002-08-14 18:24:09 +0000635 const ExprMapKeyType &Key = make_pair(Instruction::Cast, argVec);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000636 ConstantExpr *Result = ExprConstants.get(Ty, Key);
637 if (Result) return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000638
639 // Its not in the table so create a new one and put it in the table.
Chris Lattner330b7ac2002-08-14 18:24:09 +0000640 Result = new ConstantExpr(Instruction::Cast, C, Ty);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000641 ExprConstants.add(Ty, Key, Result);
642 return Result;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000643}
Chris Lattnerd7a73302001-10-13 06:57:33 +0000644
Chris Lattner3cd8c562002-07-30 18:54:25 +0000645ConstantExpr *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Vikram S. Adve4c485332002-07-15 18:19:33 +0000646 // Look up the constant in the table first to ensure uniqueness
647 vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000648 const ExprMapKeyType &Key = make_pair(Opcode, argVec);
Chris Lattner3cd8c562002-07-30 18:54:25 +0000649 ConstantExpr *Result = ExprConstants.get(C1->getType(), Key);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000650 if (Result) return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000651
652 // Its not in the table so create a new one and put it in the table.
653 // Check the operands for consistency first
Chris Lattner69ce8672002-10-13 19:39:16 +0000654 assert((Opcode >= Instruction::BinaryOpsBegin &&
655 Opcode < Instruction::BinaryOpsEnd) &&
Chris Lattner3cd8c562002-07-30 18:54:25 +0000656 "Invalid opcode in binary constant expression");
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000657
Chris Lattner3cd8c562002-07-30 18:54:25 +0000658 assert(C1->getType() == C2->getType() &&
659 "Operand types in binary constant expression should match");
Vikram S. Adve4c485332002-07-15 18:19:33 +0000660
Chris Lattner3cd8c562002-07-30 18:54:25 +0000661 Result = new ConstantExpr(Opcode, C1, C2);
662 ExprConstants.add(C1->getType(), Key, Result);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000663 return Result;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000664}
665
Chris Lattner3cd8c562002-07-30 18:54:25 +0000666ConstantExpr *ConstantExpr::getGetElementPtr(Constant *C,
667 const std::vector<Constant*> &IdxList) {
668 const Type *Ty = C->getType();
Vikram S. Adve4c485332002-07-15 18:19:33 +0000669
670 // Look up the constant in the table first to ensure uniqueness
671 vector<Constant*> argVec(1, C);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000672 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
Vikram S. Adve4c485332002-07-15 18:19:33 +0000673
Chris Lattner3cd8c562002-07-30 18:54:25 +0000674 const ExprMapKeyType &Key = make_pair(Instruction::GetElementPtr, argVec);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000675 ConstantExpr *Result = ExprConstants.get(Ty, Key);
676 if (Result) return Result;
Chris Lattner3cd8c562002-07-30 18:54:25 +0000677
Vikram S. Adve4c485332002-07-15 18:19:33 +0000678 // Its not in the table so create a new one and put it in the table.
679 // Check the operands for consistency first
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000680 //
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000681 assert(isa<PointerType>(Ty) &&
682 "Non-pointer type for constant GelElementPtr expression");
683
Chris Lattner3cd8c562002-07-30 18:54:25 +0000684 // Check that the indices list is valid...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000685 std::vector<Value*> ValIdxList(IdxList.begin(), IdxList.end());
Chris Lattner3cd8c562002-07-30 18:54:25 +0000686 const Type *DestTy = GetElementPtrInst::getIndexedType(Ty, ValIdxList, true);
687 assert(DestTy && "Invalid index list for constant GelElementPtr expression");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000688
Chris Lattner3cd8c562002-07-30 18:54:25 +0000689 Result = new ConstantExpr(C, IdxList, PointerType::get(DestTy));
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000690 ExprConstants.add(Ty, Key, Result);
691 return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000692}
693
694// destroyConstant - Remove the constant from the constant table...
695//
696void ConstantExpr::destroyConstant() {
697 ExprConstants.remove(this);
698 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000699}
700
Chris Lattner3cd8c562002-07-30 18:54:25 +0000701const char *ConstantExpr::getOpcodeName() const {
702 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000703}
704
Chris Lattner163b8902002-10-14 03:30:23 +0000705unsigned Constant::mutateReferences(Value *OldV, Value *NewV) {
706 // Uses of constant pointer refs are global values, not constants!
707 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
708 GlobalValue *NewGV = cast<GlobalValue>(NewV);
709 GlobalValue *OldGV = CPR->getValue();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000710
Chris Lattner163b8902002-10-14 03:30:23 +0000711 assert(OldGV == OldV && "Cannot mutate old value if I'm not using it!");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000712
Chris Lattner163b8902002-10-14 03:30:23 +0000713 OldGV->getParent()->mutateConstantPointerRef(OldGV, NewGV);
714 Operands[0] = NewGV;
715 return 1;
716 } else {
717 Constant *NewC = cast<Constant>(NewV);
718 unsigned NumReplaced = 0;
719 for (unsigned i = 0, N = getNumOperands(); i != N; ++i)
720 if (Operands[i] == OldV) {
721 ++NumReplaced;
722 Operands[i] = NewC;
723 }
724 return NumReplaced;
725 }
Chris Lattner25033252001-10-03 19:28:15 +0000726}