blob: 9efc313be7684cf5c13f88ff0ba893167426c83c [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
Chris Lattner55ed6562003-05-14 17:51:05 +0000414void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV) {
415 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
416 Constant *To = cast<Constant>(ToV);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000417
Chris Lattner46b3d302003-04-16 22:40:51 +0000418 Constant *Replacement = 0;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000419 if (getOpcode() == Instruction::GetElementPtr) {
420 std::vector<Constant*> Indices;
Chris Lattner55ed6562003-05-14 17:51:05 +0000421 Constant *Pointer = getOperand(0);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000422 Indices.reserve(getNumOperands()-1);
Chris Lattner55ed6562003-05-14 17:51:05 +0000423 if (Pointer == From) Pointer = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000424
425 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000426 Constant *Val = getOperand(i);
427 if (Val == From) Val = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000428 Indices.push_back(Val);
429 }
430 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
431 } else if (getOpcode() == Instruction::Cast) {
432 assert(getOperand(0) == From && "Cast only has one use!");
Chris Lattner55ed6562003-05-14 17:51:05 +0000433 Replacement = ConstantExpr::getCast(To, getType());
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000434 } else if (getNumOperands() == 2) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000435 Constant *C1 = getOperand(0);
436 Constant *C2 = getOperand(1);
437 if (C1 == From) C1 = To;
438 if (C2 == From) C2 = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000439 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
440 } else {
441 assert(0 && "Unknown ConstantExpr type!");
442 return;
443 }
444
445 assert(Replacement != this && "I didn't contain From!");
446
447 // Everyone using this now uses the replacement...
448 replaceAllUsesWith(Replacement);
449
450 // Delete the old constant!
451 destroyConstant();
452}
453
454
455
456//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000457// Factory Function Implementation
458
Chris Lattner3462ae32001-12-03 22:26:30 +0000459template<class ValType, class ConstantClass>
Chris Lattner49d855c2001-09-07 16:46:31 +0000460struct ValueMap {
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000461 typedef std::pair<const Type*, ValType> ConstHashKey;
462 std::map<ConstHashKey, ConstantClass *> Map;
Chris Lattner49d855c2001-09-07 16:46:31 +0000463
Chris Lattner3462ae32001-12-03 22:26:30 +0000464 inline ConstantClass *get(const Type *Ty, ValType V) {
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000465 typename std::map<ConstHashKey,ConstantClass *>::iterator I =
Chris Lattner49d855c2001-09-07 16:46:31 +0000466 Map.find(ConstHashKey(Ty, V));
467 return (I != Map.end()) ? I->second : 0;
468 }
469
Chris Lattner3462ae32001-12-03 22:26:30 +0000470 inline void add(const Type *Ty, ValType V, ConstantClass *CP) {
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000471 Map.insert(std::make_pair(ConstHashKey(Ty, V), CP));
Chris Lattner49d855c2001-09-07 16:46:31 +0000472 }
Chris Lattnerd7a73302001-10-13 06:57:33 +0000473
Chris Lattner3462ae32001-12-03 22:26:30 +0000474 inline void remove(ConstantClass *CP) {
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000475 for (typename std::map<ConstHashKey, ConstantClass*>::iterator
476 I = Map.begin(), E = Map.end(); I != E; ++I)
Chris Lattnerd7a73302001-10-13 06:57:33 +0000477 if (I->second == CP) {
478 Map.erase(I);
479 return;
480 }
481 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000482};
483
Chris Lattner3462ae32001-12-03 22:26:30 +0000484//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000485//
Chris Lattner3462ae32001-12-03 22:26:30 +0000486static ValueMap<uint64_t, ConstantInt> IntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000487
Chris Lattner3462ae32001-12-03 22:26:30 +0000488ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
489 ConstantSInt *Result = (ConstantSInt*)IntConstants.get(Ty, (uint64_t)V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000490 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000491 IntConstants.add(Ty, V, Result = new ConstantSInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000492 return Result;
493}
494
Chris Lattner3462ae32001-12-03 22:26:30 +0000495ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
496 ConstantUInt *Result = (ConstantUInt*)IntConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000497 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000498 IntConstants.add(Ty, V, Result = new ConstantUInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000499 return Result;
500}
501
Chris Lattner3462ae32001-12-03 22:26:30 +0000502ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000503 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000504 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
505 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000506}
507
Chris Lattner3462ae32001-12-03 22:26:30 +0000508//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000509//
Chris Lattner3462ae32001-12-03 22:26:30 +0000510static ValueMap<double, ConstantFP> FPConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000511
Chris Lattner3462ae32001-12-03 22:26:30 +0000512ConstantFP *ConstantFP::get(const Type *Ty, double V) {
513 ConstantFP *Result = FPConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000514 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000515 FPConstants.add(Ty, V, Result = new ConstantFP(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000516 return Result;
517}
518
Chris Lattner3462ae32001-12-03 22:26:30 +0000519//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000520//
Chris Lattner7f74a562002-01-20 22:54:45 +0000521static ValueMap<std::vector<Constant*>, ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000522
Chris Lattner3462ae32001-12-03 22:26:30 +0000523ConstantArray *ConstantArray::get(const ArrayType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000524 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000525 ConstantArray *Result = ArrayConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000526 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000527 ArrayConstants.add(Ty, V, Result = new ConstantArray(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000528 return Result;
529}
530
Chris Lattner3462ae32001-12-03 22:26:30 +0000531// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000532// contain the specified string. A null terminator is added to the specified
533// string so that it may be used in a natural way...
534//
Chris Lattner7f74a562002-01-20 22:54:45 +0000535ConstantArray *ConstantArray::get(const std::string &Str) {
536 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000537
538 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000539 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000540
541 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000542 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000543
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000544 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000545 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000546}
547
548
Chris Lattnerd7a73302001-10-13 06:57:33 +0000549// destroyConstant - Remove the constant from the constant table...
550//
Chris Lattner3462ae32001-12-03 22:26:30 +0000551void ConstantArray::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000552 ArrayConstants.remove(this);
553 destroyConstantImpl();
554}
555
Chris Lattner81fabb02002-08-26 17:53:56 +0000556// getAsString - If the sub-element type of this array is either sbyte or ubyte,
557// then this method converts the array to an std::string and returns it.
558// Otherwise, it asserts out.
559//
560std::string ConstantArray::getAsString() const {
561 std::string Result;
562 if (getType()->getElementType() == Type::SByteTy)
563 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
564 Result += (char)cast<ConstantSInt>(getOperand(i))->getValue();
565 else {
566 assert(getType()->getElementType() == Type::UByteTy && "Not a string!");
567 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
568 Result += (char)cast<ConstantUInt>(getOperand(i))->getValue();
569 }
570 return Result;
571}
572
573
Chris Lattner3462ae32001-12-03 22:26:30 +0000574//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000575//
Chris Lattner7f74a562002-01-20 22:54:45 +0000576static ValueMap<std::vector<Constant*>, ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000577
Chris Lattner3462ae32001-12-03 22:26:30 +0000578ConstantStruct *ConstantStruct::get(const StructType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000579 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000580 ConstantStruct *Result = StructConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000581 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000582 StructConstants.add(Ty, V, Result = new ConstantStruct(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000583 return Result;
584}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000585
Chris Lattnerd7a73302001-10-13 06:57:33 +0000586// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000587//
Chris Lattner3462ae32001-12-03 22:26:30 +0000588void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000589 StructConstants.remove(this);
590 destroyConstantImpl();
591}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000592
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000593
Chris Lattner3462ae32001-12-03 22:26:30 +0000594//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000595//
Chris Lattner3462ae32001-12-03 22:26:30 +0000596static ValueMap<char, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000597
Chris Lattner3462ae32001-12-03 22:26:30 +0000598ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
599 ConstantPointerNull *Result = NullPtrConstants.get(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000600 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000601 NullPtrConstants.add(Ty, 0, Result = new ConstantPointerNull(Ty));
Chris Lattner883ad0b2001-10-03 15:39:36 +0000602 return Result;
603}
604
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000605// destroyConstant - Remove the constant from the constant table...
606//
607void ConstantPointerNull::destroyConstant() {
608 NullPtrConstants.remove(this);
609 destroyConstantImpl();
610}
611
612
Chris Lattner3462ae32001-12-03 22:26:30 +0000613//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +0000614//
Chris Lattner3462ae32001-12-03 22:26:30 +0000615ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000616 assert(GV->getParent() && "Global Value must be attached to a module!");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000617
Chris Lattnerd7a73302001-10-13 06:57:33 +0000618 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +0000619 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000620}
621
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000622// destroyConstant - Remove the constant from the constant table...
623//
624void ConstantPointerRef::destroyConstant() {
625 getValue()->getParent()->destroyConstantPointerRef(this);
626 destroyConstantImpl();
627}
628
629
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000630//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000631//
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000632typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000633static ValueMap<const ExprMapKeyType, ConstantExpr> ExprConstants;
634
Chris Lattner46b3d302003-04-16 22:40:51 +0000635Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattneracdbe712003-04-17 19:24:48 +0000636 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
637 return FC; // Fold a few common cases...
638
Vikram S. Adve4c485332002-07-15 18:19:33 +0000639 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000640 std::vector<Constant*> argVec(1, C);
641 const ExprMapKeyType &Key = std::make_pair(Instruction::Cast, argVec);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000642 ConstantExpr *Result = ExprConstants.get(Ty, Key);
643 if (Result) return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000644
645 // Its not in the table so create a new one and put it in the table.
Chris Lattner330b7ac2002-08-14 18:24:09 +0000646 Result = new ConstantExpr(Instruction::Cast, C, Ty);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000647 ExprConstants.add(Ty, Key, Result);
648 return Result;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000649}
Chris Lattnerd7a73302001-10-13 06:57:33 +0000650
Chris Lattner46b3d302003-04-16 22:40:51 +0000651Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattneracdbe712003-04-17 19:24:48 +0000652
653 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
654 return FC; // Fold a few common cases...
655
Vikram S. Adve4c485332002-07-15 18:19:33 +0000656 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000657 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
658 const ExprMapKeyType &Key = std::make_pair(Opcode, argVec);
Chris Lattner3cd8c562002-07-30 18:54:25 +0000659 ConstantExpr *Result = ExprConstants.get(C1->getType(), Key);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000660 if (Result) return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000661
662 // Its not in the table so create a new one and put it in the table.
663 // Check the operands for consistency first
Chris Lattner69ce8672002-10-13 19:39:16 +0000664 assert((Opcode >= Instruction::BinaryOpsBegin &&
665 Opcode < Instruction::BinaryOpsEnd) &&
Chris Lattner3cd8c562002-07-30 18:54:25 +0000666 "Invalid opcode in binary constant expression");
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000667
Chris Lattner3cd8c562002-07-30 18:54:25 +0000668 assert(C1->getType() == C2->getType() &&
669 "Operand types in binary constant expression should match");
Vikram S. Adve4c485332002-07-15 18:19:33 +0000670
Chris Lattner3cd8c562002-07-30 18:54:25 +0000671 Result = new ConstantExpr(Opcode, C1, C2);
672 ExprConstants.add(C1->getType(), Key, Result);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000673 return Result;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000674}
675
Chris Lattner46b3d302003-04-16 22:40:51 +0000676Constant *ConstantExpr::getGetElementPtr(Constant *C,
677 const std::vector<Constant*> &IdxList){
Chris Lattneracdbe712003-04-17 19:24:48 +0000678 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
679 return FC; // Fold a few common cases...
Chris Lattner3cd8c562002-07-30 18:54:25 +0000680 const Type *Ty = C->getType();
Vikram S. Adve4c485332002-07-15 18:19:33 +0000681
682 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000683 std::vector<Constant*> argVec(1, C);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000684 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
Vikram S. Adve4c485332002-07-15 18:19:33 +0000685
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000686 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,argVec);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000687 ConstantExpr *Result = ExprConstants.get(Ty, Key);
688 if (Result) return Result;
Chris Lattner3cd8c562002-07-30 18:54:25 +0000689
Vikram S. Adve4c485332002-07-15 18:19:33 +0000690 // Its not in the table so create a new one and put it in the table.
691 // Check the operands for consistency first
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000692 //
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000693 assert(isa<PointerType>(Ty) &&
694 "Non-pointer type for constant GelElementPtr expression");
695
Chris Lattner3cd8c562002-07-30 18:54:25 +0000696 // Check that the indices list is valid...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000697 std::vector<Value*> ValIdxList(IdxList.begin(), IdxList.end());
Chris Lattner3cd8c562002-07-30 18:54:25 +0000698 const Type *DestTy = GetElementPtrInst::getIndexedType(Ty, ValIdxList, true);
699 assert(DestTy && "Invalid index list for constant GelElementPtr expression");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000700
Chris Lattner3cd8c562002-07-30 18:54:25 +0000701 Result = new ConstantExpr(C, IdxList, PointerType::get(DestTy));
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000702 ExprConstants.add(Ty, Key, Result);
703 return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000704}
705
706// destroyConstant - Remove the constant from the constant table...
707//
708void ConstantExpr::destroyConstant() {
709 ExprConstants.remove(this);
710 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000711}
712
Chris Lattner3cd8c562002-07-30 18:54:25 +0000713const char *ConstantExpr::getOpcodeName() const {
714 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000715}
716
Chris Lattner163b8902002-10-14 03:30:23 +0000717unsigned Constant::mutateReferences(Value *OldV, Value *NewV) {
718 // Uses of constant pointer refs are global values, not constants!
719 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
720 GlobalValue *NewGV = cast<GlobalValue>(NewV);
721 GlobalValue *OldGV = CPR->getValue();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000722
Chris Lattner163b8902002-10-14 03:30:23 +0000723 assert(OldGV == OldV && "Cannot mutate old value if I'm not using it!");
Chris Lattner163b8902002-10-14 03:30:23 +0000724 Operands[0] = NewGV;
Chris Lattnercb4d26f2003-05-15 19:37:21 +0000725 OldGV->getParent()->mutateConstantPointerRef(OldGV, NewGV);
Chris Lattner163b8902002-10-14 03:30:23 +0000726 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}