blob: 84f2566e46868da2caf1aba07208c41ee0a42653 [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
Chris Lattner83e5d392003-03-10 22:39:02 +0000176bool ConstantUInt::isAllOnesValue() const {
177 unsigned TypeBits = getType()->getPrimitiveSize()*8;
178 uint64_t Val = ~0ULL; // All ones
179 Val >>= 64-TypeBits; // Shift out inappropriate bits
180 return getValue() == Val;
181}
182
Chris Lattnerb1585a92002-08-13 17:50:20 +0000183
Chris Lattner2f7c9632001-06-06 20:29:01 +0000184//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +0000185// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +0000186//===----------------------------------------------------------------------===//
187
188//===----------------------------------------------------------------------===//
189// Normal Constructors
190
Chris Lattnerb1585a92002-08-13 17:50:20 +0000191ConstantBool::ConstantBool(bool V) : ConstantIntegral(Type::BoolTy) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000192 Val = V;
193}
Chris Lattner49d855c2001-09-07 16:46:31 +0000194
Chris Lattnerb1585a92002-08-13 17:50:20 +0000195ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : ConstantIntegral(Ty) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000196 Val.Unsigned = V;
Chris Lattner7309d662001-07-21 19:16:08 +0000197}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000198
Chris Lattner3462ae32001-12-03 22:26:30 +0000199ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000200 assert(Ty->isInteger() && Ty->isSigned() &&
201 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000202 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000203}
204
Chris Lattner3462ae32001-12-03 22:26:30 +0000205ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000206 assert(Ty->isInteger() && Ty->isUnsigned() &&
207 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000208 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000209}
210
Chris Lattner3462ae32001-12-03 22:26:30 +0000211ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
Chris Lattner9655e542001-07-20 19:16:02 +0000212 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000213 Val = V;
214}
215
Chris Lattner3462ae32001-12-03 22:26:30 +0000216ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000217 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner0d779712002-10-08 23:33:52 +0000218 Operands.reserve(V.size());
219 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000220 assert(V[i]->getType() == T->getElementType());
Chris Lattnera073acb2001-07-07 08:36:50 +0000221 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000222 }
223}
224
Chris Lattner3462ae32001-12-03 22:26:30 +0000225ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000226 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000227 const StructType::ElementTypes &ETypes = T->getElementTypes();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000228 assert(V.size() == ETypes.size() &&
229 "Invalid initializer vector for constant structure");
Chris Lattner0d779712002-10-08 23:33:52 +0000230 Operands.reserve(V.size());
231 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000232 assert(V[i]->getType() == ETypes[i]);
Chris Lattnera073acb2001-07-07 08:36:50 +0000233 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000234 }
235}
236
Chris Lattner3462ae32001-12-03 22:26:30 +0000237ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
238 : ConstantPointer(GV->getType()) {
Chris Lattner60e0dd72001-10-03 06:12:09 +0000239 Operands.push_back(Use(GV, this));
240}
241
Chris Lattner3cd8c562002-07-30 18:54:25 +0000242ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
243 : Constant(Ty), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000244 Operands.push_back(Use(C, this));
245}
246
Chris Lattner3cd8c562002-07-30 18:54:25 +0000247ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
248 : Constant(C1->getType()), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000249 Operands.push_back(Use(C1, this));
250 Operands.push_back(Use(C2, this));
251}
252
Chris Lattner3cd8c562002-07-30 18:54:25 +0000253ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
254 const Type *DestTy)
255 : Constant(DestTy), iType(Instruction::GetElementPtr) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000256 Operands.reserve(1+IdxList.size());
257 Operands.push_back(Use(C, this));
258 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
259 Operands.push_back(Use(IdxList[i], this));
260}
261
Chris Lattner60e0dd72001-10-03 06:12:09 +0000262
Chris Lattner2f7c9632001-06-06 20:29:01 +0000263
264//===----------------------------------------------------------------------===//
Chris Lattnerd7a73302001-10-13 06:57:33 +0000265// classof implementations
266
Chris Lattnerb1585a92002-08-13 17:50:20 +0000267bool ConstantIntegral::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000268 return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
Chris Lattner41e99a02002-08-12 21:21:21 +0000269}
270
Chris Lattner3462ae32001-12-03 22:26:30 +0000271bool ConstantInt::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000272 return CPV->getType()->isInteger() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000273}
Chris Lattner3462ae32001-12-03 22:26:30 +0000274bool ConstantSInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000275 return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000276}
Chris Lattner3462ae32001-12-03 22:26:30 +0000277bool ConstantUInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000278 return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000279}
Chris Lattner3462ae32001-12-03 22:26:30 +0000280bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000281 const Type *Ty = CPV->getType();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000282 return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000283 !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000284}
Chris Lattner3462ae32001-12-03 22:26:30 +0000285bool ConstantArray::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000286 return isa<ArrayType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000287}
Chris Lattner3462ae32001-12-03 22:26:30 +0000288bool ConstantStruct::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000289 return isa<StructType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000290}
Chris Lattner3462ae32001-12-03 22:26:30 +0000291bool ConstantPointer::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000292 return (isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000293}
294
295
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000296
Chris Lattner2f7c9632001-06-06 20:29:01 +0000297//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000298// isValueValidForType implementations
299
Chris Lattner3462ae32001-12-03 22:26:30 +0000300bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000301 switch (Ty->getPrimitiveID()) {
302 default:
303 return false; // These can't be represented as integers!!!
304
305 // Signed types...
306 case Type::SByteTyID:
307 return (Val <= INT8_MAX && Val >= INT8_MIN);
308 case Type::ShortTyID:
309 return (Val <= INT16_MAX && Val >= INT16_MIN);
310 case Type::IntTyID:
311 return (Val <= INT32_MAX && Val >= INT32_MIN);
312 case Type::LongTyID:
313 return true; // This is the largest type...
314 }
315 assert(0 && "WTF?");
316 return false;
317}
318
Chris Lattner3462ae32001-12-03 22:26:30 +0000319bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000320 switch (Ty->getPrimitiveID()) {
321 default:
322 return false; // These can't be represented as integers!!!
323
324 // Unsigned types...
325 case Type::UByteTyID:
326 return (Val <= UINT8_MAX);
327 case Type::UShortTyID:
328 return (Val <= UINT16_MAX);
329 case Type::UIntTyID:
330 return (Val <= UINT32_MAX);
331 case Type::ULongTyID:
332 return true; // This is the largest type...
333 }
334 assert(0 && "WTF?");
335 return false;
336}
337
Chris Lattner3462ae32001-12-03 22:26:30 +0000338bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000339 switch (Ty->getPrimitiveID()) {
340 default:
341 return false; // These can't be represented as floating point!
342
343 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000344 case Type::FloatTyID:
Chris Lattnerd06dd692001-07-15 00:18:39 +0000345 /*
Chris Lattner2f7c9632001-06-06 20:29:01 +0000346 return (Val <= UINT8_MAX);
347 */
348 case Type::DoubleTyID:
349 return true; // This is the largest type...
350 }
351};
Chris Lattner9655e542001-07-20 19:16:02 +0000352
Chris Lattner49d855c2001-09-07 16:46:31 +0000353//===----------------------------------------------------------------------===//
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000354// replaceUsesOfWithOnConstant implementations
355
356void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To) {
357 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
358
359 std::vector<Constant*> Values;
360 Values.reserve(getValues().size()); // Build replacement array...
361 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
362 Constant *Val = cast<Constant>(getValues()[i]);
363 if (Val == From) Val = cast<Constant>(To);
364 Values.push_back(Val);
365 }
366
367 ConstantArray *Replacement = ConstantArray::get(getType(), Values);
368 assert(Replacement != this && "I didn't contain From!");
369
370 // Everyone using this now uses the replacement...
371 replaceAllUsesWith(Replacement);
372
373 // Delete the old constant!
374 destroyConstant();
375}
376
377void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To) {
378 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
379
380 std::vector<Constant*> Values;
381 Values.reserve(getValues().size());
382 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
383 Constant *Val = cast<Constant>(getValues()[i]);
384 if (Val == From) Val = cast<Constant>(To);
385 Values.push_back(Val);
386 }
387
388 ConstantStruct *Replacement = ConstantStruct::get(getType(), Values);
389 assert(Replacement != this && "I didn't contain From!");
390
391 // Everyone using this now uses the replacement...
392 replaceAllUsesWith(Replacement);
393
394 // Delete the old constant!
395 destroyConstant();
396}
397
398void ConstantPointerRef::replaceUsesOfWithOnConstant(Value *From, Value *To) {
399 if (isa<GlobalValue>(To)) {
400 assert(From == getOperand(0) && "Doesn't contain from!");
401 ConstantPointerRef *Replacement =
402 ConstantPointerRef::get(cast<GlobalValue>(To));
403
404 // Everyone using this now uses the replacement...
405 replaceAllUsesWith(Replacement);
406
407 // Delete the old constant!
408 destroyConstant();
409 } else {
410 // Just replace ourselves with the To value specified.
411 replaceAllUsesWith(To);
412
413 // Delete the old constant!
414 destroyConstant();
415 }
416}
417
418void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *To) {
419 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
420
421 ConstantExpr *Replacement = 0;
422 if (getOpcode() == Instruction::GetElementPtr) {
423 std::vector<Constant*> Indices;
424 Constant *Pointer = cast<Constant>(getOperand(0));
425 Indices.reserve(getNumOperands()-1);
426 if (Pointer == From) Pointer = cast<Constant>(To);
427
428 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
429 Constant *Val = cast<Constant>(getOperand(i));
430 if (Val == From) Val = cast<Constant>(To);
431 Indices.push_back(Val);
432 }
433 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
434 } else if (getOpcode() == Instruction::Cast) {
435 assert(getOperand(0) == From && "Cast only has one use!");
436 Replacement = ConstantExpr::getCast(cast<Constant>(To), getType());
437 } else if (getNumOperands() == 2) {
438 Constant *C1 = cast<Constant>(getOperand(0));
439 Constant *C2 = cast<Constant>(getOperand(1));
440 if (C1 == From) C1 = cast<Constant>(To);
441 if (C2 == From) C2 = cast<Constant>(To);
442 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
443 } else {
444 assert(0 && "Unknown ConstantExpr type!");
445 return;
446 }
447
448 assert(Replacement != this && "I didn't contain From!");
449
450 // Everyone using this now uses the replacement...
451 replaceAllUsesWith(Replacement);
452
453 // Delete the old constant!
454 destroyConstant();
455}
456
457
458
459//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000460// Factory Function Implementation
461
Chris Lattner3462ae32001-12-03 22:26:30 +0000462template<class ValType, class ConstantClass>
Chris Lattner49d855c2001-09-07 16:46:31 +0000463struct ValueMap {
464 typedef pair<const Type*, ValType> ConstHashKey;
Chris Lattner3462ae32001-12-03 22:26:30 +0000465 map<ConstHashKey, ConstantClass *> Map;
Chris Lattner49d855c2001-09-07 16:46:31 +0000466
Chris Lattner3462ae32001-12-03 22:26:30 +0000467 inline ConstantClass *get(const Type *Ty, ValType V) {
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000468 typename map<ConstHashKey,ConstantClass *>::iterator I =
Chris Lattner49d855c2001-09-07 16:46:31 +0000469 Map.find(ConstHashKey(Ty, V));
470 return (I != Map.end()) ? I->second : 0;
471 }
472
Chris Lattner3462ae32001-12-03 22:26:30 +0000473 inline void add(const Type *Ty, ValType V, ConstantClass *CP) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000474 Map.insert(make_pair(ConstHashKey(Ty, V), CP));
475 }
Chris Lattnerd7a73302001-10-13 06:57:33 +0000476
Chris Lattner3462ae32001-12-03 22:26:30 +0000477 inline void remove(ConstantClass *CP) {
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000478 for (typename map<ConstHashKey,ConstantClass *>::iterator I = Map.begin(),
Chris Lattnerd7a73302001-10-13 06:57:33 +0000479 E = Map.end(); I != E;++I)
480 if (I->second == CP) {
481 Map.erase(I);
482 return;
483 }
484 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000485};
486
Chris Lattner3462ae32001-12-03 22:26:30 +0000487//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000488//
Chris Lattner3462ae32001-12-03 22:26:30 +0000489static ValueMap<uint64_t, ConstantInt> IntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000490
Chris Lattner3462ae32001-12-03 22:26:30 +0000491ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
492 ConstantSInt *Result = (ConstantSInt*)IntConstants.get(Ty, (uint64_t)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 ConstantSInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000495 return Result;
496}
497
Chris Lattner3462ae32001-12-03 22:26:30 +0000498ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
499 ConstantUInt *Result = (ConstantUInt*)IntConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000500 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000501 IntConstants.add(Ty, V, Result = new ConstantUInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000502 return Result;
503}
504
Chris Lattner3462ae32001-12-03 22:26:30 +0000505ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000506 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000507 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
508 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000509}
510
Chris Lattner3462ae32001-12-03 22:26:30 +0000511//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000512//
Chris Lattner3462ae32001-12-03 22:26:30 +0000513static ValueMap<double, ConstantFP> FPConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000514
Chris Lattner3462ae32001-12-03 22:26:30 +0000515ConstantFP *ConstantFP::get(const Type *Ty, double V) {
516 ConstantFP *Result = FPConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000517 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000518 FPConstants.add(Ty, V, Result = new ConstantFP(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000519 return Result;
520}
521
Chris Lattner3462ae32001-12-03 22:26:30 +0000522//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000523//
Chris Lattner7f74a562002-01-20 22:54:45 +0000524static ValueMap<std::vector<Constant*>, ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000525
Chris Lattner3462ae32001-12-03 22:26:30 +0000526ConstantArray *ConstantArray::get(const ArrayType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000527 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000528 ConstantArray *Result = ArrayConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000529 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000530 ArrayConstants.add(Ty, V, Result = new ConstantArray(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000531 return Result;
532}
533
Chris Lattner3462ae32001-12-03 22:26:30 +0000534// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000535// contain the specified string. A null terminator is added to the specified
536// string so that it may be used in a natural way...
537//
Chris Lattner7f74a562002-01-20 22:54:45 +0000538ConstantArray *ConstantArray::get(const std::string &Str) {
539 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000540
541 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000542 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000543
544 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000545 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000546
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000547 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000548 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000549}
550
551
Chris Lattnerd7a73302001-10-13 06:57:33 +0000552// destroyConstant - Remove the constant from the constant table...
553//
Chris Lattner3462ae32001-12-03 22:26:30 +0000554void ConstantArray::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000555 ArrayConstants.remove(this);
556 destroyConstantImpl();
557}
558
Chris Lattner81fabb02002-08-26 17:53:56 +0000559// getAsString - If the sub-element type of this array is either sbyte or ubyte,
560// then this method converts the array to an std::string and returns it.
561// Otherwise, it asserts out.
562//
563std::string ConstantArray::getAsString() const {
564 std::string Result;
565 if (getType()->getElementType() == Type::SByteTy)
566 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
567 Result += (char)cast<ConstantSInt>(getOperand(i))->getValue();
568 else {
569 assert(getType()->getElementType() == Type::UByteTy && "Not a string!");
570 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
571 Result += (char)cast<ConstantUInt>(getOperand(i))->getValue();
572 }
573 return Result;
574}
575
576
Chris Lattner3462ae32001-12-03 22:26:30 +0000577//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000578//
Chris Lattner7f74a562002-01-20 22:54:45 +0000579static ValueMap<std::vector<Constant*>, ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000580
Chris Lattner3462ae32001-12-03 22:26:30 +0000581ConstantStruct *ConstantStruct::get(const StructType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000582 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000583 ConstantStruct *Result = StructConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000584 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000585 StructConstants.add(Ty, V, Result = new ConstantStruct(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000586 return Result;
587}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000588
Chris Lattnerd7a73302001-10-13 06:57:33 +0000589// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000590//
Chris Lattner3462ae32001-12-03 22:26:30 +0000591void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000592 StructConstants.remove(this);
593 destroyConstantImpl();
594}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000595
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000596
Chris Lattner3462ae32001-12-03 22:26:30 +0000597//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000598//
Chris Lattner3462ae32001-12-03 22:26:30 +0000599static ValueMap<char, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000600
Chris Lattner3462ae32001-12-03 22:26:30 +0000601ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
602 ConstantPointerNull *Result = NullPtrConstants.get(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000603 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000604 NullPtrConstants.add(Ty, 0, Result = new ConstantPointerNull(Ty));
Chris Lattner883ad0b2001-10-03 15:39:36 +0000605 return Result;
606}
607
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000608// destroyConstant - Remove the constant from the constant table...
609//
610void ConstantPointerNull::destroyConstant() {
611 NullPtrConstants.remove(this);
612 destroyConstantImpl();
613}
614
615
Chris Lattner3462ae32001-12-03 22:26:30 +0000616//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +0000617//
Chris Lattner3462ae32001-12-03 22:26:30 +0000618ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000619 assert(GV->getParent() && "Global Value must be attached to a module!");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000620
Chris Lattnerd7a73302001-10-13 06:57:33 +0000621 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +0000622 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000623}
624
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000625// destroyConstant - Remove the constant from the constant table...
626//
627void ConstantPointerRef::destroyConstant() {
628 getValue()->getParent()->destroyConstantPointerRef(this);
629 destroyConstantImpl();
630}
631
632
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000633//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000634//
Vikram S. Adve4c485332002-07-15 18:19:33 +0000635typedef pair<unsigned, vector<Constant*> > ExprMapKeyType;
636static ValueMap<const ExprMapKeyType, ConstantExpr> ExprConstants;
637
Chris Lattner330b7ac2002-08-14 18:24:09 +0000638ConstantExpr *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Vikram S. Adve4c485332002-07-15 18:19:33 +0000639
640 // Look up the constant in the table first to ensure uniqueness
641 vector<Constant*> argVec(1, C);
Chris Lattner330b7ac2002-08-14 18:24:09 +0000642 const ExprMapKeyType &Key = make_pair(Instruction::Cast, argVec);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000643 ConstantExpr *Result = ExprConstants.get(Ty, Key);
644 if (Result) return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000645
646 // Its not in the table so create a new one and put it in the table.
Chris Lattner330b7ac2002-08-14 18:24:09 +0000647 Result = new ConstantExpr(Instruction::Cast, C, Ty);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000648 ExprConstants.add(Ty, Key, Result);
649 return Result;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000650}
Chris Lattnerd7a73302001-10-13 06:57:33 +0000651
Chris Lattner3cd8c562002-07-30 18:54:25 +0000652ConstantExpr *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Vikram S. Adve4c485332002-07-15 18:19:33 +0000653 // Look up the constant in the table first to ensure uniqueness
654 vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000655 const ExprMapKeyType &Key = make_pair(Opcode, argVec);
Chris Lattner3cd8c562002-07-30 18:54:25 +0000656 ConstantExpr *Result = ExprConstants.get(C1->getType(), Key);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000657 if (Result) return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000658
659 // Its not in the table so create a new one and put it in the table.
660 // Check the operands for consistency first
Chris Lattner69ce8672002-10-13 19:39:16 +0000661 assert((Opcode >= Instruction::BinaryOpsBegin &&
662 Opcode < Instruction::BinaryOpsEnd) &&
Chris Lattner3cd8c562002-07-30 18:54:25 +0000663 "Invalid opcode in binary constant expression");
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000664
Chris Lattner3cd8c562002-07-30 18:54:25 +0000665 assert(C1->getType() == C2->getType() &&
666 "Operand types in binary constant expression should match");
Vikram S. Adve4c485332002-07-15 18:19:33 +0000667
Chris Lattner3cd8c562002-07-30 18:54:25 +0000668 Result = new ConstantExpr(Opcode, C1, C2);
669 ExprConstants.add(C1->getType(), Key, Result);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000670 return Result;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000671}
672
Chris Lattner3cd8c562002-07-30 18:54:25 +0000673ConstantExpr *ConstantExpr::getGetElementPtr(Constant *C,
674 const std::vector<Constant*> &IdxList) {
675 const Type *Ty = C->getType();
Vikram S. Adve4c485332002-07-15 18:19:33 +0000676
677 // Look up the constant in the table first to ensure uniqueness
678 vector<Constant*> argVec(1, C);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000679 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
Vikram S. Adve4c485332002-07-15 18:19:33 +0000680
Chris Lattner3cd8c562002-07-30 18:54:25 +0000681 const ExprMapKeyType &Key = make_pair(Instruction::GetElementPtr, argVec);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000682 ConstantExpr *Result = ExprConstants.get(Ty, Key);
683 if (Result) return Result;
Chris Lattner3cd8c562002-07-30 18:54:25 +0000684
Vikram S. Adve4c485332002-07-15 18:19:33 +0000685 // Its not in the table so create a new one and put it in the table.
686 // Check the operands for consistency first
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000687 //
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000688 assert(isa<PointerType>(Ty) &&
689 "Non-pointer type for constant GelElementPtr expression");
690
Chris Lattner3cd8c562002-07-30 18:54:25 +0000691 // Check that the indices list is valid...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000692 std::vector<Value*> ValIdxList(IdxList.begin(), IdxList.end());
Chris Lattner3cd8c562002-07-30 18:54:25 +0000693 const Type *DestTy = GetElementPtrInst::getIndexedType(Ty, ValIdxList, true);
694 assert(DestTy && "Invalid index list for constant GelElementPtr expression");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000695
Chris Lattner3cd8c562002-07-30 18:54:25 +0000696 Result = new ConstantExpr(C, IdxList, PointerType::get(DestTy));
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000697 ExprConstants.add(Ty, Key, Result);
698 return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000699}
700
701// destroyConstant - Remove the constant from the constant table...
702//
703void ConstantExpr::destroyConstant() {
704 ExprConstants.remove(this);
705 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000706}
707
Chris Lattner3cd8c562002-07-30 18:54:25 +0000708const char *ConstantExpr::getOpcodeName() const {
709 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000710}
711
Chris Lattner163b8902002-10-14 03:30:23 +0000712unsigned Constant::mutateReferences(Value *OldV, Value *NewV) {
713 // Uses of constant pointer refs are global values, not constants!
714 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
715 GlobalValue *NewGV = cast<GlobalValue>(NewV);
716 GlobalValue *OldGV = CPR->getValue();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000717
Chris Lattner163b8902002-10-14 03:30:23 +0000718 assert(OldGV == OldV && "Cannot mutate old value if I'm not using it!");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000719
Chris Lattner163b8902002-10-14 03:30:23 +0000720 OldGV->getParent()->mutateConstantPointerRef(OldGV, NewGV);
721 Operands[0] = NewGV;
722 return 1;
723 } else {
724 Constant *NewC = cast<Constant>(NewV);
725 unsigned NumReplaced = 0;
726 for (unsigned i = 0, N = getNumOperands(); i != N; ++i)
727 if (Operands[i] == OldV) {
728 ++NumReplaced;
729 Operands[i] = NewC;
730 }
731 return NumReplaced;
732 }
Chris Lattner25033252001-10-03 19:28:15 +0000733}