blob: 499452c4251551cfb2f8062320ada03187f0c439 [file] [log] [blame]
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001//===-- Constants.cpp - Implement Constant nodes --------------------------===//
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 Lattneracdbe712003-04-17 19:24:48 +00008#include "llvm/ConstantHandling.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +00009#include "llvm/DerivedTypes.h"
Vikram S. Adve4e537b22002-07-14 23:13:17 +000010#include "llvm/iMemory.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000011#include "llvm/SymbolTable.h"
Chris Lattnerd7a73302001-10-13 06:57:33 +000012#include "llvm/Module.h"
Chris Lattner5de22042001-11-27 00:03:19 +000013#include "Support/StringExtras.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000014#include <algorithm>
Chris Lattner2f7c9632001-06-06 20:29:01 +000015
Chris Lattner3462ae32001-12-03 22:26:30 +000016ConstantBool *ConstantBool::True = new ConstantBool(true);
17ConstantBool *ConstantBool::False = new ConstantBool(false);
Chris Lattner2f7c9632001-06-06 20:29:01 +000018
Chris Lattner9655e542001-07-20 19:16:02 +000019
Chris Lattner2f7c9632001-06-06 20:29:01 +000020//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000021// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000022//===----------------------------------------------------------------------===//
23
24// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +000025void Constant::setName(const std::string &Name, SymbolTable *ST) {
Chris Lattner49d855c2001-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 Lattner2f7c9632001-06-06 20:29:01 +000029}
30
Chris Lattner3462ae32001-12-03 22:26:30 +000031void Constant::destroyConstantImpl() {
32 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-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 Lattner3462ae32001-12-03 22:26:30 +000037 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000038 //
39 while (!use_empty()) {
40 Value *V = use_back();
41#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-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 Lattnerd7a73302001-10-13 06:57:33 +000046#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000047 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Chris Lattner3462ae32001-12-03 22:26:30 +000048 Constant *CPV = cast<Constant>(V);
Chris Lattnerd7a73302001-10-13 06:57:33 +000049 CPV->destroyConstant();
50
51 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000052 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000053 }
54
55 // Value has no outstanding references it is safe to delete it now...
56 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000057}
Chris Lattner2f7c9632001-06-06 20:29:01 +000058
Chris Lattnerb1585a92002-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 Lattnerc33ae672003-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 Lattnerb1585a92002-08-13 17:50:20 +000094 default:
Chris Lattnerc33ae672003-03-06 21:02:18 +000095 // Function, Type, Label, or Opaque type?
96 assert(0 && "Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-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 Lattner31408f72002-08-14 17:12:13 +0000121 default: return 0;
Chris Lattnerb1585a92002-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 Lattner31408f72002-08-14 17:12:13 +0000145 default: return 0;
Chris Lattnerb1585a92002-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 Lattner31408f72002-08-14 17:12:13 +0000168 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000169 }
170}
171
Chris Lattner83e5d392003-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 Lattnerb1585a92002-08-13 17:50:20 +0000179
Chris Lattner2f7c9632001-06-06 20:29:01 +0000180//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +0000181// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +0000182//===----------------------------------------------------------------------===//
183
184//===----------------------------------------------------------------------===//
185// Normal Constructors
186
Chris Lattnerb1585a92002-08-13 17:50:20 +0000187ConstantBool::ConstantBool(bool V) : ConstantIntegral(Type::BoolTy) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000188 Val = V;
189}
Chris Lattner49d855c2001-09-07 16:46:31 +0000190
Chris Lattnerb1585a92002-08-13 17:50:20 +0000191ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : ConstantIntegral(Ty) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000192 Val.Unsigned = V;
Chris Lattner7309d662001-07-21 19:16:08 +0000193}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000194
Chris Lattner3462ae32001-12-03 22:26:30 +0000195ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000196 assert(Ty->isInteger() && Ty->isSigned() &&
197 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000198 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000199}
200
Chris Lattner3462ae32001-12-03 22:26:30 +0000201ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000202 assert(Ty->isInteger() && Ty->isUnsigned() &&
203 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000204 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000205}
206
Chris Lattner3462ae32001-12-03 22:26:30 +0000207ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
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 Val = V;
210}
211
Chris Lattner3462ae32001-12-03 22:26:30 +0000212ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000213 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner0d779712002-10-08 23:33:52 +0000214 Operands.reserve(V.size());
215 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000216 assert(V[i]->getType() == T->getElementType());
Chris Lattnera073acb2001-07-07 08:36:50 +0000217 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000218 }
219}
220
Chris Lattner3462ae32001-12-03 22:26:30 +0000221ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000222 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000223 const StructType::ElementTypes &ETypes = T->getElementTypes();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000224 assert(V.size() == ETypes.size() &&
225 "Invalid initializer vector for constant structure");
Chris Lattner0d779712002-10-08 23:33:52 +0000226 Operands.reserve(V.size());
227 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000228 assert(V[i]->getType() == ETypes[i]);
Chris Lattnera073acb2001-07-07 08:36:50 +0000229 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000230 }
231}
232
Chris Lattner3462ae32001-12-03 22:26:30 +0000233ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
234 : ConstantPointer(GV->getType()) {
Chris Lattner60e0dd72001-10-03 06:12:09 +0000235 Operands.push_back(Use(GV, this));
236}
237
Chris Lattner3cd8c562002-07-30 18:54:25 +0000238ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
239 : Constant(Ty), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000240 Operands.push_back(Use(C, this));
241}
242
Chris Lattner3cd8c562002-07-30 18:54:25 +0000243ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
244 : Constant(C1->getType()), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000245 Operands.push_back(Use(C1, this));
246 Operands.push_back(Use(C2, this));
247}
248
Chris Lattner3cd8c562002-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. Adve4e537b22002-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 Lattner60e0dd72001-10-03 06:12:09 +0000258
Chris Lattner2f7c9632001-06-06 20:29:01 +0000259
260//===----------------------------------------------------------------------===//
Chris Lattnerd7a73302001-10-13 06:57:33 +0000261// classof implementations
262
Chris Lattnerb1585a92002-08-13 17:50:20 +0000263bool ConstantIntegral::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000264 return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
Chris Lattner41e99a02002-08-12 21:21:21 +0000265}
266
Chris Lattner3462ae32001-12-03 22:26:30 +0000267bool ConstantInt::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000268 return CPV->getType()->isInteger() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000269}
Chris Lattner3462ae32001-12-03 22:26:30 +0000270bool ConstantSInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000271 return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000272}
Chris Lattner3462ae32001-12-03 22:26:30 +0000273bool ConstantUInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000274 return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000275}
Chris Lattner3462ae32001-12-03 22:26:30 +0000276bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000277 const Type *Ty = CPV->getType();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000278 return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000279 !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000280}
Chris Lattner3462ae32001-12-03 22:26:30 +0000281bool ConstantArray::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000282 return isa<ArrayType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000283}
Chris Lattner3462ae32001-12-03 22:26:30 +0000284bool ConstantStruct::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000285 return isa<StructType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000286}
Chris Lattner3462ae32001-12-03 22:26:30 +0000287bool ConstantPointer::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000288 return (isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000289}
290
291
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000292
Chris Lattner2f7c9632001-06-06 20:29:01 +0000293//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000294// isValueValidForType implementations
295
Chris Lattner3462ae32001-12-03 22:26:30 +0000296bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner2f7c9632001-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 Lattner3462ae32001-12-03 22:26:30 +0000315bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner2f7c9632001-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 Lattner3462ae32001-12-03 22:26:30 +0000334bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner2f7c9632001-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 Lattner2f7c9632001-06-06 20:29:01 +0000340 case Type::FloatTyID:
Chris Lattnerd06dd692001-07-15 00:18:39 +0000341 /*
Chris Lattner2f7c9632001-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 Lattner9655e542001-07-20 19:16:02 +0000348
Chris Lattner49d855c2001-09-07 16:46:31 +0000349//===----------------------------------------------------------------------===//
Chris Lattnerb1dd9bb2002-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
414void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *To) {
415 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
416
Chris Lattner46b3d302003-04-16 22:40:51 +0000417 Constant *Replacement = 0;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000418 if (getOpcode() == Instruction::GetElementPtr) {
419 std::vector<Constant*> Indices;
420 Constant *Pointer = cast<Constant>(getOperand(0));
421 Indices.reserve(getNumOperands()-1);
422 if (Pointer == From) Pointer = cast<Constant>(To);
423
424 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
425 Constant *Val = cast<Constant>(getOperand(i));
426 if (Val == From) Val = cast<Constant>(To);
427 Indices.push_back(Val);
428 }
429 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
430 } else if (getOpcode() == Instruction::Cast) {
431 assert(getOperand(0) == From && "Cast only has one use!");
432 Replacement = ConstantExpr::getCast(cast<Constant>(To), getType());
433 } else if (getNumOperands() == 2) {
434 Constant *C1 = cast<Constant>(getOperand(0));
435 Constant *C2 = cast<Constant>(getOperand(1));
436 if (C1 == From) C1 = cast<Constant>(To);
437 if (C2 == From) C2 = cast<Constant>(To);
438 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
439 } else {
440 assert(0 && "Unknown ConstantExpr type!");
441 return;
442 }
443
444 assert(Replacement != this && "I didn't contain From!");
445
446 // Everyone using this now uses the replacement...
447 replaceAllUsesWith(Replacement);
448
449 // Delete the old constant!
450 destroyConstant();
451}
452
453
454
455//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000456// Factory Function Implementation
457
Chris Lattner3462ae32001-12-03 22:26:30 +0000458template<class ValType, class ConstantClass>
Chris Lattner49d855c2001-09-07 16:46:31 +0000459struct ValueMap {
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000460 typedef std::pair<const Type*, ValType> ConstHashKey;
461 std::map<ConstHashKey, ConstantClass *> Map;
Chris Lattner49d855c2001-09-07 16:46:31 +0000462
Chris Lattner3462ae32001-12-03 22:26:30 +0000463 inline ConstantClass *get(const Type *Ty, ValType V) {
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000464 typename std::map<ConstHashKey,ConstantClass *>::iterator I =
Chris Lattner49d855c2001-09-07 16:46:31 +0000465 Map.find(ConstHashKey(Ty, V));
466 return (I != Map.end()) ? I->second : 0;
467 }
468
Chris Lattner3462ae32001-12-03 22:26:30 +0000469 inline void add(const Type *Ty, ValType V, ConstantClass *CP) {
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000470 Map.insert(std::make_pair(ConstHashKey(Ty, V), CP));
Chris Lattner49d855c2001-09-07 16:46:31 +0000471 }
Chris Lattnerd7a73302001-10-13 06:57:33 +0000472
Chris Lattner3462ae32001-12-03 22:26:30 +0000473 inline void remove(ConstantClass *CP) {
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000474 for (typename std::map<ConstHashKey, ConstantClass*>::iterator
475 I = Map.begin(), E = Map.end(); I != E; ++I)
Chris Lattnerd7a73302001-10-13 06:57:33 +0000476 if (I->second == CP) {
477 Map.erase(I);
478 return;
479 }
480 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000481};
482
Chris Lattner3462ae32001-12-03 22:26:30 +0000483//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000484//
Chris Lattner3462ae32001-12-03 22:26:30 +0000485static ValueMap<uint64_t, ConstantInt> IntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000486
Chris Lattner3462ae32001-12-03 22:26:30 +0000487ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
488 ConstantSInt *Result = (ConstantSInt*)IntConstants.get(Ty, (uint64_t)V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000489 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000490 IntConstants.add(Ty, V, Result = new ConstantSInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000491 return Result;
492}
493
Chris Lattner3462ae32001-12-03 22:26:30 +0000494ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
495 ConstantUInt *Result = (ConstantUInt*)IntConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000496 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000497 IntConstants.add(Ty, V, Result = new ConstantUInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000498 return Result;
499}
500
Chris Lattner3462ae32001-12-03 22:26:30 +0000501ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000502 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000503 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
504 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000505}
506
Chris Lattner3462ae32001-12-03 22:26:30 +0000507//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000508//
Chris Lattner3462ae32001-12-03 22:26:30 +0000509static ValueMap<double, ConstantFP> FPConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000510
Chris Lattner3462ae32001-12-03 22:26:30 +0000511ConstantFP *ConstantFP::get(const Type *Ty, double V) {
512 ConstantFP *Result = FPConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000513 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000514 FPConstants.add(Ty, V, Result = new ConstantFP(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000515 return Result;
516}
517
Chris Lattner3462ae32001-12-03 22:26:30 +0000518//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000519//
Chris Lattner7f74a562002-01-20 22:54:45 +0000520static ValueMap<std::vector<Constant*>, ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000521
Chris Lattner3462ae32001-12-03 22:26:30 +0000522ConstantArray *ConstantArray::get(const ArrayType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000523 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000524 ConstantArray *Result = ArrayConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000525 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000526 ArrayConstants.add(Ty, V, Result = new ConstantArray(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000527 return Result;
528}
529
Chris Lattner3462ae32001-12-03 22:26:30 +0000530// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000531// contain the specified string. A null terminator is added to the specified
532// string so that it may be used in a natural way...
533//
Chris Lattner7f74a562002-01-20 22:54:45 +0000534ConstantArray *ConstantArray::get(const std::string &Str) {
535 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000536
537 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000538 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000539
540 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000541 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000542
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000543 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000544 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000545}
546
547
Chris Lattnerd7a73302001-10-13 06:57:33 +0000548// destroyConstant - Remove the constant from the constant table...
549//
Chris Lattner3462ae32001-12-03 22:26:30 +0000550void ConstantArray::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000551 ArrayConstants.remove(this);
552 destroyConstantImpl();
553}
554
Chris Lattner81fabb02002-08-26 17:53:56 +0000555// getAsString - If the sub-element type of this array is either sbyte or ubyte,
556// then this method converts the array to an std::string and returns it.
557// Otherwise, it asserts out.
558//
559std::string ConstantArray::getAsString() const {
560 std::string Result;
561 if (getType()->getElementType() == Type::SByteTy)
562 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
563 Result += (char)cast<ConstantSInt>(getOperand(i))->getValue();
564 else {
565 assert(getType()->getElementType() == Type::UByteTy && "Not a string!");
566 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
567 Result += (char)cast<ConstantUInt>(getOperand(i))->getValue();
568 }
569 return Result;
570}
571
572
Chris Lattner3462ae32001-12-03 22:26:30 +0000573//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000574//
Chris Lattner7f74a562002-01-20 22:54:45 +0000575static ValueMap<std::vector<Constant*>, ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000576
Chris Lattner3462ae32001-12-03 22:26:30 +0000577ConstantStruct *ConstantStruct::get(const StructType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000578 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000579 ConstantStruct *Result = StructConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000580 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000581 StructConstants.add(Ty, V, Result = new ConstantStruct(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000582 return Result;
583}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000584
Chris Lattnerd7a73302001-10-13 06:57:33 +0000585// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000586//
Chris Lattner3462ae32001-12-03 22:26:30 +0000587void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000588 StructConstants.remove(this);
589 destroyConstantImpl();
590}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000591
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000592
Chris Lattner3462ae32001-12-03 22:26:30 +0000593//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000594//
Chris Lattner3462ae32001-12-03 22:26:30 +0000595static ValueMap<char, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000596
Chris Lattner3462ae32001-12-03 22:26:30 +0000597ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
598 ConstantPointerNull *Result = NullPtrConstants.get(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000599 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000600 NullPtrConstants.add(Ty, 0, Result = new ConstantPointerNull(Ty));
Chris Lattner883ad0b2001-10-03 15:39:36 +0000601 return Result;
602}
603
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000604// destroyConstant - Remove the constant from the constant table...
605//
606void ConstantPointerNull::destroyConstant() {
607 NullPtrConstants.remove(this);
608 destroyConstantImpl();
609}
610
611
Chris Lattner3462ae32001-12-03 22:26:30 +0000612//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +0000613//
Chris Lattner3462ae32001-12-03 22:26:30 +0000614ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000615 assert(GV->getParent() && "Global Value must be attached to a module!");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000616
Chris Lattnerd7a73302001-10-13 06:57:33 +0000617 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +0000618 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000619}
620
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000621// destroyConstant - Remove the constant from the constant table...
622//
623void ConstantPointerRef::destroyConstant() {
624 getValue()->getParent()->destroyConstantPointerRef(this);
625 destroyConstantImpl();
626}
627
628
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000629//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000630//
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000631typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000632static ValueMap<const ExprMapKeyType, ConstantExpr> ExprConstants;
633
Chris Lattner46b3d302003-04-16 22:40:51 +0000634Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattneracdbe712003-04-17 19:24:48 +0000635 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
636 return FC; // Fold a few common cases...
637
Vikram S. Adve4c485332002-07-15 18:19:33 +0000638 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000639 std::vector<Constant*> argVec(1, C);
640 const ExprMapKeyType &Key = std::make_pair(Instruction::Cast, argVec);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000641 ConstantExpr *Result = ExprConstants.get(Ty, Key);
642 if (Result) return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000643
644 // Its not in the table so create a new one and put it in the table.
Chris Lattner330b7ac2002-08-14 18:24:09 +0000645 Result = new ConstantExpr(Instruction::Cast, C, Ty);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000646 ExprConstants.add(Ty, Key, Result);
647 return Result;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000648}
Chris Lattnerd7a73302001-10-13 06:57:33 +0000649
Chris Lattner46b3d302003-04-16 22:40:51 +0000650Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattneracdbe712003-04-17 19:24:48 +0000651
652 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
653 return FC; // Fold a few common cases...
654
Vikram S. Adve4c485332002-07-15 18:19:33 +0000655 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000656 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
657 const ExprMapKeyType &Key = std::make_pair(Opcode, argVec);
Chris Lattner3cd8c562002-07-30 18:54:25 +0000658 ConstantExpr *Result = ExprConstants.get(C1->getType(), Key);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000659 if (Result) return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000660
661 // Its not in the table so create a new one and put it in the table.
662 // Check the operands for consistency first
Chris Lattner69ce8672002-10-13 19:39:16 +0000663 assert((Opcode >= Instruction::BinaryOpsBegin &&
664 Opcode < Instruction::BinaryOpsEnd) &&
Chris Lattner3cd8c562002-07-30 18:54:25 +0000665 "Invalid opcode in binary constant expression");
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000666
Chris Lattner3cd8c562002-07-30 18:54:25 +0000667 assert(C1->getType() == C2->getType() &&
668 "Operand types in binary constant expression should match");
Vikram S. Adve4c485332002-07-15 18:19:33 +0000669
Chris Lattner3cd8c562002-07-30 18:54:25 +0000670 Result = new ConstantExpr(Opcode, C1, C2);
671 ExprConstants.add(C1->getType(), Key, Result);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000672 return Result;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000673}
674
Chris Lattner46b3d302003-04-16 22:40:51 +0000675Constant *ConstantExpr::getGetElementPtr(Constant *C,
676 const std::vector<Constant*> &IdxList){
Chris Lattneracdbe712003-04-17 19:24:48 +0000677 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
678 return FC; // Fold a few common cases...
Chris Lattner3cd8c562002-07-30 18:54:25 +0000679 const Type *Ty = C->getType();
Vikram S. Adve4c485332002-07-15 18:19:33 +0000680
681 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000682 std::vector<Constant*> argVec(1, C);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000683 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
Vikram S. Adve4c485332002-07-15 18:19:33 +0000684
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000685 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,argVec);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000686 ConstantExpr *Result = ExprConstants.get(Ty, Key);
687 if (Result) return Result;
Chris Lattner3cd8c562002-07-30 18:54:25 +0000688
Vikram S. Adve4c485332002-07-15 18:19:33 +0000689 // Its not in the table so create a new one and put it in the table.
690 // Check the operands for consistency first
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000691 //
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000692 assert(isa<PointerType>(Ty) &&
693 "Non-pointer type for constant GelElementPtr expression");
694
Chris Lattner3cd8c562002-07-30 18:54:25 +0000695 // Check that the indices list is valid...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000696 std::vector<Value*> ValIdxList(IdxList.begin(), IdxList.end());
Chris Lattner3cd8c562002-07-30 18:54:25 +0000697 const Type *DestTy = GetElementPtrInst::getIndexedType(Ty, ValIdxList, true);
698 assert(DestTy && "Invalid index list for constant GelElementPtr expression");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000699
Chris Lattner3cd8c562002-07-30 18:54:25 +0000700 Result = new ConstantExpr(C, IdxList, PointerType::get(DestTy));
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000701 ExprConstants.add(Ty, Key, Result);
702 return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000703}
704
705// destroyConstant - Remove the constant from the constant table...
706//
707void ConstantExpr::destroyConstant() {
708 ExprConstants.remove(this);
709 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000710}
711
Chris Lattner3cd8c562002-07-30 18:54:25 +0000712const char *ConstantExpr::getOpcodeName() const {
713 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000714}
715
Chris Lattner163b8902002-10-14 03:30:23 +0000716unsigned Constant::mutateReferences(Value *OldV, Value *NewV) {
717 // Uses of constant pointer refs are global values, not constants!
718 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
719 GlobalValue *NewGV = cast<GlobalValue>(NewV);
720 GlobalValue *OldGV = CPR->getValue();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000721
Chris Lattner163b8902002-10-14 03:30:23 +0000722 assert(OldGV == OldV && "Cannot mutate old value if I'm not using it!");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000723
Chris Lattner163b8902002-10-14 03:30:23 +0000724 OldGV->getParent()->mutateConstantPointerRef(OldGV, NewGV);
725 Operands[0] = NewGV;
726 return 1;
727 } else {
728 Constant *NewC = cast<Constant>(NewV);
729 unsigned NumReplaced = 0;
730 for (unsigned i = 0, N = getNumOperands(); i != N; ++i)
731 if (Operands[i] == OldV) {
732 ++NumReplaced;
733 Operands[i] = NewC;
734 }
735 return NumReplaced;
736 }
Chris Lattner25033252001-10-03 19:28:15 +0000737}