blob: 3887e33274a693d72cae36299a8f2ac9e491822c [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 Lattner6915f8f2002-04-07 22:49:37 +000012#include "llvm/SlotCalculator.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 Lattner7f74a562002-01-20 22:54:45 +000016using std::map;
17using std::pair;
18using std::make_pair;
Anand Shukla991873f2002-07-16 00:02:17 +000019using std::vector;
Chris Lattner7f74a562002-01-20 22:54:45 +000020
Chris Lattner3462ae32001-12-03 22:26:30 +000021ConstantBool *ConstantBool::True = new ConstantBool(true);
22ConstantBool *ConstantBool::False = new ConstantBool(false);
Chris Lattner2f7c9632001-06-06 20:29:01 +000023
Chris Lattner9655e542001-07-20 19:16:02 +000024
Chris Lattner2f7c9632001-06-06 20:29:01 +000025//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000026// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000027//===----------------------------------------------------------------------===//
28
29// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +000030void Constant::setName(const std::string &Name, SymbolTable *ST) {
Chris Lattner49d855c2001-09-07 16:46:31 +000031 assert(ST && "Type::setName - Must provide symbol table argument!");
32
33 if (Name.size()) ST->insert(Name, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000034}
35
Chris Lattner3462ae32001-12-03 22:26:30 +000036void Constant::destroyConstantImpl() {
37 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000038 // references to the constant by other constants in the constant pool. These
39 // constants are implicitly dependant on the module that is being deleted,
40 // but they don't know that. Because we only find out when the CPV is
41 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000042 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000043 //
44 while (!use_empty()) {
45 Value *V = use_back();
46#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000047 if (!isa<Constant>(V))
48 std::cerr << "While deleting: " << *this
49 << "\n\nUse still stuck around after Def is destroyed: "
50 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000051#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000052 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Chris Lattner3462ae32001-12-03 22:26:30 +000053 Constant *CPV = cast<Constant>(V);
Chris Lattnerd7a73302001-10-13 06:57:33 +000054 CPV->destroyConstant();
55
56 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000057 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000058 }
59
60 // Value has no outstanding references it is safe to delete it now...
61 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000062}
Chris Lattner2f7c9632001-06-06 20:29:01 +000063
Chris Lattnerb1585a92002-08-13 17:50:20 +000064// Static constructor to create a '0' constant of arbitrary type...
65Constant *Constant::getNullValue(const Type *Ty) {
66 switch (Ty->getPrimitiveID()) {
67 case Type::BoolTyID: return ConstantBool::get(false);
68 case Type::SByteTyID:
69 case Type::ShortTyID:
70 case Type::IntTyID:
71 case Type::LongTyID: return ConstantSInt::get(Ty, 0);
72
73 case Type::UByteTyID:
74 case Type::UShortTyID:
75 case Type::UIntTyID:
76 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
77
78 case Type::FloatTyID:
79 case Type::DoubleTyID: return ConstantFP::get(Ty, 0);
80
81 case Type::PointerTyID:
82 return ConstantPointerNull::get(cast<PointerType>(Ty));
83 default:
84 return 0;
85 }
86}
87
88// Static constructor to create the maximum constant of an integral type...
89ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
90 switch (Ty->getPrimitiveID()) {
91 case Type::BoolTyID: return ConstantBool::True;
92 case Type::SByteTyID:
93 case Type::ShortTyID:
94 case Type::IntTyID:
95 case Type::LongTyID: {
96 // Calculate 011111111111111...
97 unsigned TypeBits = Ty->getPrimitiveSize()*8;
98 int64_t Val = INT64_MAX; // All ones
99 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
100 return ConstantSInt::get(Ty, Val);
101 }
102
103 case Type::UByteTyID:
104 case Type::UShortTyID:
105 case Type::UIntTyID:
106 case Type::ULongTyID: return getAllOnesValue(Ty);
107
Chris Lattner31408f72002-08-14 17:12:13 +0000108 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000109 }
110}
111
112// Static constructor to create the minimum constant for an integral type...
113ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
114 switch (Ty->getPrimitiveID()) {
115 case Type::BoolTyID: return ConstantBool::False;
116 case Type::SByteTyID:
117 case Type::ShortTyID:
118 case Type::IntTyID:
119 case Type::LongTyID: {
120 // Calculate 1111111111000000000000
121 unsigned TypeBits = Ty->getPrimitiveSize()*8;
122 int64_t Val = -1; // All ones
123 Val <<= TypeBits-1; // Shift over to the right spot
124 return ConstantSInt::get(Ty, Val);
125 }
126
127 case Type::UByteTyID:
128 case Type::UShortTyID:
129 case Type::UIntTyID:
130 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
131
Chris Lattner31408f72002-08-14 17:12:13 +0000132 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000133 }
134}
135
136// Static constructor to create an integral constant with all bits set
137ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
138 switch (Ty->getPrimitiveID()) {
139 case Type::BoolTyID: return ConstantBool::True;
140 case Type::SByteTyID:
141 case Type::ShortTyID:
142 case Type::IntTyID:
143 case Type::LongTyID: return ConstantSInt::get(Ty, -1);
144
145 case Type::UByteTyID:
146 case Type::UShortTyID:
147 case Type::UIntTyID:
148 case Type::ULongTyID: {
149 // Calculate ~0 of the right type...
150 unsigned TypeBits = Ty->getPrimitiveSize()*8;
151 uint64_t Val = ~0ULL; // All ones
152 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
153 return ConstantUInt::get(Ty, Val);
154 }
Chris Lattner31408f72002-08-14 17:12:13 +0000155 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000156 }
157}
158
159
Chris Lattner2f7c9632001-06-06 20:29:01 +0000160//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +0000161// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +0000162//===----------------------------------------------------------------------===//
163
164//===----------------------------------------------------------------------===//
165// Normal Constructors
166
Chris Lattnerb1585a92002-08-13 17:50:20 +0000167ConstantBool::ConstantBool(bool V) : ConstantIntegral(Type::BoolTy) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000168 Val = V;
169}
Chris Lattner49d855c2001-09-07 16:46:31 +0000170
Chris Lattnerb1585a92002-08-13 17:50:20 +0000171ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : ConstantIntegral(Ty) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000172 Val.Unsigned = V;
Chris Lattner7309d662001-07-21 19:16:08 +0000173}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000174
Chris Lattner3462ae32001-12-03 22:26:30 +0000175ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000176 assert(Ty->isInteger() && Ty->isSigned() &&
177 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000178 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000179}
180
Chris Lattner3462ae32001-12-03 22:26:30 +0000181ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000182 assert(Ty->isInteger() && Ty->isUnsigned() &&
183 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000184 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000185}
186
Chris Lattner3462ae32001-12-03 22:26:30 +0000187ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
Chris Lattner9655e542001-07-20 19:16:02 +0000188 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000189 Val = V;
190}
191
Chris Lattner3462ae32001-12-03 22:26:30 +0000192ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000193 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner0d779712002-10-08 23:33:52 +0000194 Operands.reserve(V.size());
195 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000196 assert(V[i]->getType() == T->getElementType());
Chris Lattnera073acb2001-07-07 08:36:50 +0000197 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000198 }
199}
200
Chris Lattner3462ae32001-12-03 22:26:30 +0000201ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000202 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000203 const StructType::ElementTypes &ETypes = T->getElementTypes();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000204 assert(V.size() == ETypes.size() &&
205 "Invalid initializer vector for constant structure");
Chris Lattner0d779712002-10-08 23:33:52 +0000206 Operands.reserve(V.size());
207 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000208 assert(V[i]->getType() == ETypes[i]);
Chris Lattnera073acb2001-07-07 08:36:50 +0000209 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000210 }
211}
212
Chris Lattner3462ae32001-12-03 22:26:30 +0000213ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
214 : ConstantPointer(GV->getType()) {
Chris Lattner60e0dd72001-10-03 06:12:09 +0000215 Operands.push_back(Use(GV, this));
216}
217
Chris Lattner3cd8c562002-07-30 18:54:25 +0000218ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
219 : Constant(Ty), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000220 Operands.push_back(Use(C, this));
221}
222
Chris Lattner3cd8c562002-07-30 18:54:25 +0000223ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
224 : Constant(C1->getType()), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000225 Operands.push_back(Use(C1, this));
226 Operands.push_back(Use(C2, this));
227}
228
Chris Lattner3cd8c562002-07-30 18:54:25 +0000229ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
230 const Type *DestTy)
231 : Constant(DestTy), iType(Instruction::GetElementPtr) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000232 Operands.reserve(1+IdxList.size());
233 Operands.push_back(Use(C, this));
234 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
235 Operands.push_back(Use(IdxList[i], this));
236}
237
Chris Lattner60e0dd72001-10-03 06:12:09 +0000238
Chris Lattner2f7c9632001-06-06 20:29:01 +0000239
240//===----------------------------------------------------------------------===//
Chris Lattnerd7a73302001-10-13 06:57:33 +0000241// classof implementations
242
Chris Lattnerb1585a92002-08-13 17:50:20 +0000243bool ConstantIntegral::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000244 return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
Chris Lattner41e99a02002-08-12 21:21:21 +0000245}
246
Chris Lattner3462ae32001-12-03 22:26:30 +0000247bool ConstantInt::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000248 return CPV->getType()->isInteger() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000249}
Chris Lattner3462ae32001-12-03 22:26:30 +0000250bool ConstantSInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000251 return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000252}
Chris Lattner3462ae32001-12-03 22:26:30 +0000253bool ConstantUInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000254 return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000255}
Chris Lattner3462ae32001-12-03 22:26:30 +0000256bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000257 const Type *Ty = CPV->getType();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000258 return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000259 !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000260}
Chris Lattner3462ae32001-12-03 22:26:30 +0000261bool ConstantArray::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000262 return isa<ArrayType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000263}
Chris Lattner3462ae32001-12-03 22:26:30 +0000264bool ConstantStruct::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000265 return isa<StructType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000266}
Chris Lattner3462ae32001-12-03 22:26:30 +0000267bool ConstantPointer::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000268 return (isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000269}
270
271
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000272
Chris Lattner2f7c9632001-06-06 20:29:01 +0000273//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000274// isValueValidForType implementations
275
Chris Lattner3462ae32001-12-03 22:26:30 +0000276bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000277 switch (Ty->getPrimitiveID()) {
278 default:
279 return false; // These can't be represented as integers!!!
280
281 // Signed types...
282 case Type::SByteTyID:
283 return (Val <= INT8_MAX && Val >= INT8_MIN);
284 case Type::ShortTyID:
285 return (Val <= INT16_MAX && Val >= INT16_MIN);
286 case Type::IntTyID:
287 return (Val <= INT32_MAX && Val >= INT32_MIN);
288 case Type::LongTyID:
289 return true; // This is the largest type...
290 }
291 assert(0 && "WTF?");
292 return false;
293}
294
Chris Lattner3462ae32001-12-03 22:26:30 +0000295bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000296 switch (Ty->getPrimitiveID()) {
297 default:
298 return false; // These can't be represented as integers!!!
299
300 // Unsigned types...
301 case Type::UByteTyID:
302 return (Val <= UINT8_MAX);
303 case Type::UShortTyID:
304 return (Val <= UINT16_MAX);
305 case Type::UIntTyID:
306 return (Val <= UINT32_MAX);
307 case Type::ULongTyID:
308 return true; // This is the largest type...
309 }
310 assert(0 && "WTF?");
311 return false;
312}
313
Chris Lattner3462ae32001-12-03 22:26:30 +0000314bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000315 switch (Ty->getPrimitiveID()) {
316 default:
317 return false; // These can't be represented as floating point!
318
319 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000320 case Type::FloatTyID:
Chris Lattnerd06dd692001-07-15 00:18:39 +0000321 /*
Chris Lattner2f7c9632001-06-06 20:29:01 +0000322 return (Val <= UINT8_MAX);
323 */
324 case Type::DoubleTyID:
325 return true; // This is the largest type...
326 }
327};
Chris Lattner9655e542001-07-20 19:16:02 +0000328
Chris Lattner49d855c2001-09-07 16:46:31 +0000329//===----------------------------------------------------------------------===//
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000330// replaceUsesOfWithOnConstant implementations
331
332void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To) {
333 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
334
335 std::vector<Constant*> Values;
336 Values.reserve(getValues().size()); // Build replacement array...
337 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
338 Constant *Val = cast<Constant>(getValues()[i]);
339 if (Val == From) Val = cast<Constant>(To);
340 Values.push_back(Val);
341 }
342
343 ConstantArray *Replacement = ConstantArray::get(getType(), Values);
344 assert(Replacement != this && "I didn't contain From!");
345
346 // Everyone using this now uses the replacement...
347 replaceAllUsesWith(Replacement);
348
349 // Delete the old constant!
350 destroyConstant();
351}
352
353void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To) {
354 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
355
356 std::vector<Constant*> Values;
357 Values.reserve(getValues().size());
358 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
359 Constant *Val = cast<Constant>(getValues()[i]);
360 if (Val == From) Val = cast<Constant>(To);
361 Values.push_back(Val);
362 }
363
364 ConstantStruct *Replacement = ConstantStruct::get(getType(), Values);
365 assert(Replacement != this && "I didn't contain From!");
366
367 // Everyone using this now uses the replacement...
368 replaceAllUsesWith(Replacement);
369
370 // Delete the old constant!
371 destroyConstant();
372}
373
374void ConstantPointerRef::replaceUsesOfWithOnConstant(Value *From, Value *To) {
375 if (isa<GlobalValue>(To)) {
376 assert(From == getOperand(0) && "Doesn't contain from!");
377 ConstantPointerRef *Replacement =
378 ConstantPointerRef::get(cast<GlobalValue>(To));
379
380 // Everyone using this now uses the replacement...
381 replaceAllUsesWith(Replacement);
382
383 // Delete the old constant!
384 destroyConstant();
385 } else {
386 // Just replace ourselves with the To value specified.
387 replaceAllUsesWith(To);
388
389 // Delete the old constant!
390 destroyConstant();
391 }
392}
393
394void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *To) {
395 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
396
397 ConstantExpr *Replacement = 0;
398 if (getOpcode() == Instruction::GetElementPtr) {
399 std::vector<Constant*> Indices;
400 Constant *Pointer = cast<Constant>(getOperand(0));
401 Indices.reserve(getNumOperands()-1);
402 if (Pointer == From) Pointer = cast<Constant>(To);
403
404 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
405 Constant *Val = cast<Constant>(getOperand(i));
406 if (Val == From) Val = cast<Constant>(To);
407 Indices.push_back(Val);
408 }
409 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
410 } else if (getOpcode() == Instruction::Cast) {
411 assert(getOperand(0) == From && "Cast only has one use!");
412 Replacement = ConstantExpr::getCast(cast<Constant>(To), getType());
413 } else if (getNumOperands() == 2) {
414 Constant *C1 = cast<Constant>(getOperand(0));
415 Constant *C2 = cast<Constant>(getOperand(1));
416 if (C1 == From) C1 = cast<Constant>(To);
417 if (C2 == From) C2 = cast<Constant>(To);
418 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
419 } else {
420 assert(0 && "Unknown ConstantExpr type!");
421 return;
422 }
423
424 assert(Replacement != this && "I didn't contain From!");
425
426 // Everyone using this now uses the replacement...
427 replaceAllUsesWith(Replacement);
428
429 // Delete the old constant!
430 destroyConstant();
431}
432
433
434
435//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000436// Factory Function Implementation
437
Chris Lattner3462ae32001-12-03 22:26:30 +0000438template<class ValType, class ConstantClass>
Chris Lattner49d855c2001-09-07 16:46:31 +0000439struct ValueMap {
440 typedef pair<const Type*, ValType> ConstHashKey;
Chris Lattner3462ae32001-12-03 22:26:30 +0000441 map<ConstHashKey, ConstantClass *> Map;
Chris Lattner49d855c2001-09-07 16:46:31 +0000442
Chris Lattner3462ae32001-12-03 22:26:30 +0000443 inline ConstantClass *get(const Type *Ty, ValType V) {
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000444 typename map<ConstHashKey,ConstantClass *>::iterator I =
Chris Lattner49d855c2001-09-07 16:46:31 +0000445 Map.find(ConstHashKey(Ty, V));
446 return (I != Map.end()) ? I->second : 0;
447 }
448
Chris Lattner3462ae32001-12-03 22:26:30 +0000449 inline void add(const Type *Ty, ValType V, ConstantClass *CP) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000450 Map.insert(make_pair(ConstHashKey(Ty, V), CP));
451 }
Chris Lattnerd7a73302001-10-13 06:57:33 +0000452
Chris Lattner3462ae32001-12-03 22:26:30 +0000453 inline void remove(ConstantClass *CP) {
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000454 for (typename map<ConstHashKey,ConstantClass *>::iterator I = Map.begin(),
Chris Lattnerd7a73302001-10-13 06:57:33 +0000455 E = Map.end(); I != E;++I)
456 if (I->second == CP) {
457 Map.erase(I);
458 return;
459 }
460 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000461};
462
Chris Lattner3462ae32001-12-03 22:26:30 +0000463//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000464//
Chris Lattner3462ae32001-12-03 22:26:30 +0000465static ValueMap<uint64_t, ConstantInt> IntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000466
Chris Lattner3462ae32001-12-03 22:26:30 +0000467ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
468 ConstantSInt *Result = (ConstantSInt*)IntConstants.get(Ty, (uint64_t)V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000469 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000470 IntConstants.add(Ty, V, Result = new ConstantSInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000471 return Result;
472}
473
Chris Lattner3462ae32001-12-03 22:26:30 +0000474ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
475 ConstantUInt *Result = (ConstantUInt*)IntConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000476 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000477 IntConstants.add(Ty, V, Result = new ConstantUInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000478 return Result;
479}
480
Chris Lattner3462ae32001-12-03 22:26:30 +0000481ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000482 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000483 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
484 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000485}
486
Chris Lattner3462ae32001-12-03 22:26:30 +0000487//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000488//
Chris Lattner3462ae32001-12-03 22:26:30 +0000489static ValueMap<double, ConstantFP> FPConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000490
Chris Lattner3462ae32001-12-03 22:26:30 +0000491ConstantFP *ConstantFP::get(const Type *Ty, double V) {
492 ConstantFP *Result = FPConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000493 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000494 FPConstants.add(Ty, V, Result = new ConstantFP(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000495 return Result;
496}
497
Chris Lattner3462ae32001-12-03 22:26:30 +0000498//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000499//
Chris Lattner7f74a562002-01-20 22:54:45 +0000500static ValueMap<std::vector<Constant*>, ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000501
Chris Lattner3462ae32001-12-03 22:26:30 +0000502ConstantArray *ConstantArray::get(const ArrayType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000503 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000504 ConstantArray *Result = ArrayConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000505 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000506 ArrayConstants.add(Ty, V, Result = new ConstantArray(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000507 return Result;
508}
509
Chris Lattner3462ae32001-12-03 22:26:30 +0000510// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000511// contain the specified string. A null terminator is added to the specified
512// string so that it may be used in a natural way...
513//
Chris Lattner7f74a562002-01-20 22:54:45 +0000514ConstantArray *ConstantArray::get(const std::string &Str) {
515 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000516
517 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000518 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000519
520 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000521 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000522
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000523 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000524 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000525}
526
527
Chris Lattnerd7a73302001-10-13 06:57:33 +0000528// destroyConstant - Remove the constant from the constant table...
529//
Chris Lattner3462ae32001-12-03 22:26:30 +0000530void ConstantArray::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000531 ArrayConstants.remove(this);
532 destroyConstantImpl();
533}
534
Chris Lattner81fabb02002-08-26 17:53:56 +0000535// getAsString - If the sub-element type of this array is either sbyte or ubyte,
536// then this method converts the array to an std::string and returns it.
537// Otherwise, it asserts out.
538//
539std::string ConstantArray::getAsString() const {
540 std::string Result;
541 if (getType()->getElementType() == Type::SByteTy)
542 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
543 Result += (char)cast<ConstantSInt>(getOperand(i))->getValue();
544 else {
545 assert(getType()->getElementType() == Type::UByteTy && "Not a string!");
546 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
547 Result += (char)cast<ConstantUInt>(getOperand(i))->getValue();
548 }
549 return Result;
550}
551
552
Chris Lattner3462ae32001-12-03 22:26:30 +0000553//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000554//
Chris Lattner7f74a562002-01-20 22:54:45 +0000555static ValueMap<std::vector<Constant*>, ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000556
Chris Lattner3462ae32001-12-03 22:26:30 +0000557ConstantStruct *ConstantStruct::get(const StructType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000558 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000559 ConstantStruct *Result = StructConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000560 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000561 StructConstants.add(Ty, V, Result = new ConstantStruct(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000562 return Result;
563}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000564
Chris Lattnerd7a73302001-10-13 06:57:33 +0000565// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000566//
Chris Lattner3462ae32001-12-03 22:26:30 +0000567void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000568 StructConstants.remove(this);
569 destroyConstantImpl();
570}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000571
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000572
Chris Lattner3462ae32001-12-03 22:26:30 +0000573//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000574//
Chris Lattner3462ae32001-12-03 22:26:30 +0000575static ValueMap<char, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000576
Chris Lattner3462ae32001-12-03 22:26:30 +0000577ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
578 ConstantPointerNull *Result = NullPtrConstants.get(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000579 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000580 NullPtrConstants.add(Ty, 0, Result = new ConstantPointerNull(Ty));
Chris Lattner883ad0b2001-10-03 15:39:36 +0000581 return Result;
582}
583
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000584// destroyConstant - Remove the constant from the constant table...
585//
586void ConstantPointerNull::destroyConstant() {
587 NullPtrConstants.remove(this);
588 destroyConstantImpl();
589}
590
591
Chris Lattner3462ae32001-12-03 22:26:30 +0000592//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +0000593//
Chris Lattner3462ae32001-12-03 22:26:30 +0000594ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000595 assert(GV->getParent() && "Global Value must be attached to a module!");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000596
Chris Lattnerd7a73302001-10-13 06:57:33 +0000597 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +0000598 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000599}
600
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000601// destroyConstant - Remove the constant from the constant table...
602//
603void ConstantPointerRef::destroyConstant() {
604 getValue()->getParent()->destroyConstantPointerRef(this);
605 destroyConstantImpl();
606}
607
608
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000609//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000610//
Vikram S. Adve4c485332002-07-15 18:19:33 +0000611typedef pair<unsigned, vector<Constant*> > ExprMapKeyType;
612static ValueMap<const ExprMapKeyType, ConstantExpr> ExprConstants;
613
Chris Lattner330b7ac2002-08-14 18:24:09 +0000614ConstantExpr *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Vikram S. Adve4c485332002-07-15 18:19:33 +0000615
616 // Look up the constant in the table first to ensure uniqueness
617 vector<Constant*> argVec(1, C);
Chris Lattner330b7ac2002-08-14 18:24:09 +0000618 const ExprMapKeyType &Key = make_pair(Instruction::Cast, argVec);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000619 ConstantExpr *Result = ExprConstants.get(Ty, Key);
620 if (Result) return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000621
622 // Its not in the table so create a new one and put it in the table.
Chris Lattner330b7ac2002-08-14 18:24:09 +0000623 Result = new ConstantExpr(Instruction::Cast, C, Ty);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000624 ExprConstants.add(Ty, Key, Result);
625 return Result;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000626}
Chris Lattnerd7a73302001-10-13 06:57:33 +0000627
Chris Lattner3cd8c562002-07-30 18:54:25 +0000628ConstantExpr *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Vikram S. Adve4c485332002-07-15 18:19:33 +0000629 // Look up the constant in the table first to ensure uniqueness
630 vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000631 const ExprMapKeyType &Key = make_pair(Opcode, argVec);
Chris Lattner3cd8c562002-07-30 18:54:25 +0000632 ConstantExpr *Result = ExprConstants.get(C1->getType(), Key);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000633 if (Result) return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000634
635 // Its not in the table so create a new one and put it in the table.
636 // Check the operands for consistency first
Chris Lattner69ce8672002-10-13 19:39:16 +0000637 assert((Opcode >= Instruction::BinaryOpsBegin &&
638 Opcode < Instruction::BinaryOpsEnd) &&
Chris Lattner3cd8c562002-07-30 18:54:25 +0000639 "Invalid opcode in binary constant expression");
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000640
Chris Lattner3cd8c562002-07-30 18:54:25 +0000641 assert(C1->getType() == C2->getType() &&
642 "Operand types in binary constant expression should match");
Vikram S. Adve4c485332002-07-15 18:19:33 +0000643
Chris Lattner3cd8c562002-07-30 18:54:25 +0000644 Result = new ConstantExpr(Opcode, C1, C2);
645 ExprConstants.add(C1->getType(), Key, Result);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000646 return Result;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000647}
648
Chris Lattner3cd8c562002-07-30 18:54:25 +0000649ConstantExpr *ConstantExpr::getGetElementPtr(Constant *C,
650 const std::vector<Constant*> &IdxList) {
651 const Type *Ty = C->getType();
Vikram S. Adve4c485332002-07-15 18:19:33 +0000652
653 // Look up the constant in the table first to ensure uniqueness
654 vector<Constant*> argVec(1, C);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000655 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
Vikram S. Adve4c485332002-07-15 18:19:33 +0000656
Chris Lattner3cd8c562002-07-30 18:54:25 +0000657 const ExprMapKeyType &Key = make_pair(Instruction::GetElementPtr, argVec);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000658 ConstantExpr *Result = ExprConstants.get(Ty, Key);
659 if (Result) return Result;
Chris Lattner3cd8c562002-07-30 18:54:25 +0000660
Vikram S. Adve4c485332002-07-15 18:19:33 +0000661 // Its not in the table so create a new one and put it in the table.
662 // Check the operands for consistency first
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000663 //
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000664 assert(isa<PointerType>(Ty) &&
665 "Non-pointer type for constant GelElementPtr expression");
666
Chris Lattner3cd8c562002-07-30 18:54:25 +0000667 // Check that the indices list is valid...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000668 std::vector<Value*> ValIdxList(IdxList.begin(), IdxList.end());
Chris Lattner3cd8c562002-07-30 18:54:25 +0000669 const Type *DestTy = GetElementPtrInst::getIndexedType(Ty, ValIdxList, true);
670 assert(DestTy && "Invalid index list for constant GelElementPtr expression");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000671
Chris Lattner3cd8c562002-07-30 18:54:25 +0000672 Result = new ConstantExpr(C, IdxList, PointerType::get(DestTy));
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000673 ExprConstants.add(Ty, Key, Result);
674 return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000675}
676
677// destroyConstant - Remove the constant from the constant table...
678//
679void ConstantExpr::destroyConstant() {
680 ExprConstants.remove(this);
681 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000682}
683
Chris Lattner3cd8c562002-07-30 18:54:25 +0000684const char *ConstantExpr::getOpcodeName() const {
685 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000686}
687
Chris Lattner163b8902002-10-14 03:30:23 +0000688unsigned Constant::mutateReferences(Value *OldV, Value *NewV) {
689 // Uses of constant pointer refs are global values, not constants!
690 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
691 GlobalValue *NewGV = cast<GlobalValue>(NewV);
692 GlobalValue *OldGV = CPR->getValue();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000693
Chris Lattner163b8902002-10-14 03:30:23 +0000694 assert(OldGV == OldV && "Cannot mutate old value if I'm not using it!");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000695
Chris Lattner163b8902002-10-14 03:30:23 +0000696 OldGV->getParent()->mutateConstantPointerRef(OldGV, NewGV);
697 Operands[0] = NewGV;
698 return 1;
699 } else {
700 Constant *NewC = cast<Constant>(NewV);
701 unsigned NumReplaced = 0;
702 for (unsigned i = 0, N = getNumOperands(); i != N; ++i)
703 if (Operands[i] == OldV) {
704 ++NumReplaced;
705 Operands[i] = NewC;
706 }
707 return NumReplaced;
708 }
Chris Lattner25033252001-10-03 19:28:15 +0000709}