blob: c519da36a861ecaee3b26f4c775ebca72fc03c79 [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 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 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));
Chris Lattnerc33ae672003-03-06 21:02:18 +000083 case Type::StructTyID: {
84 const StructType *ST = cast<StructType>(Ty);
85
86 const StructType::ElementTypes &ETs = ST->getElementTypes();
87 std::vector<Constant*> Elements;
88 Elements.resize(ETs.size());
89 for (unsigned i = 0, e = ETs.size(); i != e; ++i)
90 Elements[i] = Constant::getNullValue(ETs[i]);
91 return ConstantStruct::get(ST, Elements);
92 }
93 case Type::ArrayTyID: {
94 const ArrayType *AT = cast<ArrayType>(Ty);
95 Constant *El = Constant::getNullValue(AT->getElementType());
96 unsigned NumElements = AT->getNumElements();
97 return ConstantArray::get(AT, std::vector<Constant*>(NumElements, El));
98 }
Chris Lattnerb1585a92002-08-13 17:50:20 +000099 default:
Chris Lattnerc33ae672003-03-06 21:02:18 +0000100 // Function, Type, Label, or Opaque type?
101 assert(0 && "Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000102 return 0;
103 }
104}
105
106// Static constructor to create the maximum constant of an integral type...
107ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
108 switch (Ty->getPrimitiveID()) {
109 case Type::BoolTyID: return ConstantBool::True;
110 case Type::SByteTyID:
111 case Type::ShortTyID:
112 case Type::IntTyID:
113 case Type::LongTyID: {
114 // Calculate 011111111111111...
115 unsigned TypeBits = Ty->getPrimitiveSize()*8;
116 int64_t Val = INT64_MAX; // All ones
117 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
118 return ConstantSInt::get(Ty, Val);
119 }
120
121 case Type::UByteTyID:
122 case Type::UShortTyID:
123 case Type::UIntTyID:
124 case Type::ULongTyID: return getAllOnesValue(Ty);
125
Chris Lattner31408f72002-08-14 17:12:13 +0000126 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000127 }
128}
129
130// Static constructor to create the minimum constant for an integral type...
131ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
132 switch (Ty->getPrimitiveID()) {
133 case Type::BoolTyID: return ConstantBool::False;
134 case Type::SByteTyID:
135 case Type::ShortTyID:
136 case Type::IntTyID:
137 case Type::LongTyID: {
138 // Calculate 1111111111000000000000
139 unsigned TypeBits = Ty->getPrimitiveSize()*8;
140 int64_t Val = -1; // All ones
141 Val <<= TypeBits-1; // Shift over to the right spot
142 return ConstantSInt::get(Ty, Val);
143 }
144
145 case Type::UByteTyID:
146 case Type::UShortTyID:
147 case Type::UIntTyID:
148 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
149
Chris Lattner31408f72002-08-14 17:12:13 +0000150 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000151 }
152}
153
154// Static constructor to create an integral constant with all bits set
155ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
156 switch (Ty->getPrimitiveID()) {
157 case Type::BoolTyID: return ConstantBool::True;
158 case Type::SByteTyID:
159 case Type::ShortTyID:
160 case Type::IntTyID:
161 case Type::LongTyID: return ConstantSInt::get(Ty, -1);
162
163 case Type::UByteTyID:
164 case Type::UShortTyID:
165 case Type::UIntTyID:
166 case Type::ULongTyID: {
167 // Calculate ~0 of the right type...
168 unsigned TypeBits = Ty->getPrimitiveSize()*8;
169 uint64_t Val = ~0ULL; // All ones
170 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
171 return ConstantUInt::get(Ty, Val);
172 }
Chris Lattner31408f72002-08-14 17:12:13 +0000173 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000174 }
175}
176
Chris Lattner83e5d392003-03-10 22:39:02 +0000177bool ConstantUInt::isAllOnesValue() const {
178 unsigned TypeBits = getType()->getPrimitiveSize()*8;
179 uint64_t Val = ~0ULL; // All ones
180 Val >>= 64-TypeBits; // Shift out inappropriate bits
181 return getValue() == Val;
182}
183
Chris Lattnerb1585a92002-08-13 17:50:20 +0000184
Chris Lattner2f7c9632001-06-06 20:29:01 +0000185//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +0000186// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +0000187//===----------------------------------------------------------------------===//
188
189//===----------------------------------------------------------------------===//
190// Normal Constructors
191
Chris Lattnerb1585a92002-08-13 17:50:20 +0000192ConstantBool::ConstantBool(bool V) : ConstantIntegral(Type::BoolTy) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000193 Val = V;
194}
Chris Lattner49d855c2001-09-07 16:46:31 +0000195
Chris Lattnerb1585a92002-08-13 17:50:20 +0000196ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : ConstantIntegral(Ty) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000197 Val.Unsigned = V;
Chris Lattner7309d662001-07-21 19:16:08 +0000198}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000199
Chris Lattner3462ae32001-12-03 22:26:30 +0000200ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000201 assert(Ty->isInteger() && Ty->isSigned() &&
202 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000203 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000204}
205
Chris Lattner3462ae32001-12-03 22:26:30 +0000206ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000207 assert(Ty->isInteger() && Ty->isUnsigned() &&
208 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000209 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000210}
211
Chris Lattner3462ae32001-12-03 22:26:30 +0000212ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
Chris Lattner9655e542001-07-20 19:16:02 +0000213 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000214 Val = V;
215}
216
Chris Lattner3462ae32001-12-03 22:26:30 +0000217ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000218 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner0d779712002-10-08 23:33:52 +0000219 Operands.reserve(V.size());
220 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000221 assert(V[i]->getType() == T->getElementType());
Chris Lattnera073acb2001-07-07 08:36:50 +0000222 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000223 }
224}
225
Chris Lattner3462ae32001-12-03 22:26:30 +0000226ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000227 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000228 const StructType::ElementTypes &ETypes = T->getElementTypes();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000229 assert(V.size() == ETypes.size() &&
230 "Invalid initializer vector for constant structure");
Chris Lattner0d779712002-10-08 23:33:52 +0000231 Operands.reserve(V.size());
232 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000233 assert(V[i]->getType() == ETypes[i]);
Chris Lattnera073acb2001-07-07 08:36:50 +0000234 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000235 }
236}
237
Chris Lattner3462ae32001-12-03 22:26:30 +0000238ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
239 : ConstantPointer(GV->getType()) {
Chris Lattner60e0dd72001-10-03 06:12:09 +0000240 Operands.push_back(Use(GV, this));
241}
242
Chris Lattner3cd8c562002-07-30 18:54:25 +0000243ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
244 : Constant(Ty), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000245 Operands.push_back(Use(C, this));
246}
247
Chris Lattner3cd8c562002-07-30 18:54:25 +0000248ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
249 : Constant(C1->getType()), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000250 Operands.push_back(Use(C1, this));
251 Operands.push_back(Use(C2, this));
252}
253
Chris Lattner3cd8c562002-07-30 18:54:25 +0000254ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
255 const Type *DestTy)
256 : Constant(DestTy), iType(Instruction::GetElementPtr) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000257 Operands.reserve(1+IdxList.size());
258 Operands.push_back(Use(C, this));
259 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
260 Operands.push_back(Use(IdxList[i], this));
261}
262
Chris Lattner60e0dd72001-10-03 06:12:09 +0000263
Chris Lattner2f7c9632001-06-06 20:29:01 +0000264
265//===----------------------------------------------------------------------===//
Chris Lattnerd7a73302001-10-13 06:57:33 +0000266// classof implementations
267
Chris Lattnerb1585a92002-08-13 17:50:20 +0000268bool ConstantIntegral::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000269 return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
Chris Lattner41e99a02002-08-12 21:21:21 +0000270}
271
Chris Lattner3462ae32001-12-03 22:26:30 +0000272bool ConstantInt::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000273 return CPV->getType()->isInteger() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000274}
Chris Lattner3462ae32001-12-03 22:26:30 +0000275bool ConstantSInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000276 return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000277}
Chris Lattner3462ae32001-12-03 22:26:30 +0000278bool ConstantUInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000279 return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000280}
Chris Lattner3462ae32001-12-03 22:26:30 +0000281bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000282 const Type *Ty = CPV->getType();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000283 return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000284 !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000285}
Chris Lattner3462ae32001-12-03 22:26:30 +0000286bool ConstantArray::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000287 return isa<ArrayType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000288}
Chris Lattner3462ae32001-12-03 22:26:30 +0000289bool ConstantStruct::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000290 return isa<StructType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000291}
Chris Lattner3462ae32001-12-03 22:26:30 +0000292bool ConstantPointer::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000293 return (isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000294}
295
296
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000297
Chris Lattner2f7c9632001-06-06 20:29:01 +0000298//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000299// isValueValidForType implementations
300
Chris Lattner3462ae32001-12-03 22:26:30 +0000301bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000302 switch (Ty->getPrimitiveID()) {
303 default:
304 return false; // These can't be represented as integers!!!
305
306 // Signed types...
307 case Type::SByteTyID:
308 return (Val <= INT8_MAX && Val >= INT8_MIN);
309 case Type::ShortTyID:
310 return (Val <= INT16_MAX && Val >= INT16_MIN);
311 case Type::IntTyID:
312 return (Val <= INT32_MAX && Val >= INT32_MIN);
313 case Type::LongTyID:
314 return true; // This is the largest type...
315 }
316 assert(0 && "WTF?");
317 return false;
318}
319
Chris Lattner3462ae32001-12-03 22:26:30 +0000320bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000321 switch (Ty->getPrimitiveID()) {
322 default:
323 return false; // These can't be represented as integers!!!
324
325 // Unsigned types...
326 case Type::UByteTyID:
327 return (Val <= UINT8_MAX);
328 case Type::UShortTyID:
329 return (Val <= UINT16_MAX);
330 case Type::UIntTyID:
331 return (Val <= UINT32_MAX);
332 case Type::ULongTyID:
333 return true; // This is the largest type...
334 }
335 assert(0 && "WTF?");
336 return false;
337}
338
Chris Lattner3462ae32001-12-03 22:26:30 +0000339bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000340 switch (Ty->getPrimitiveID()) {
341 default:
342 return false; // These can't be represented as floating point!
343
344 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000345 case Type::FloatTyID:
Chris Lattnerd06dd692001-07-15 00:18:39 +0000346 /*
Chris Lattner2f7c9632001-06-06 20:29:01 +0000347 return (Val <= UINT8_MAX);
348 */
349 case Type::DoubleTyID:
350 return true; // This is the largest type...
351 }
352};
Chris Lattner9655e542001-07-20 19:16:02 +0000353
Chris Lattner49d855c2001-09-07 16:46:31 +0000354//===----------------------------------------------------------------------===//
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000355// replaceUsesOfWithOnConstant implementations
356
357void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To) {
358 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
359
360 std::vector<Constant*> Values;
361 Values.reserve(getValues().size()); // Build replacement array...
362 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
363 Constant *Val = cast<Constant>(getValues()[i]);
364 if (Val == From) Val = cast<Constant>(To);
365 Values.push_back(Val);
366 }
367
368 ConstantArray *Replacement = ConstantArray::get(getType(), Values);
369 assert(Replacement != this && "I didn't contain From!");
370
371 // Everyone using this now uses the replacement...
372 replaceAllUsesWith(Replacement);
373
374 // Delete the old constant!
375 destroyConstant();
376}
377
378void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To) {
379 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
380
381 std::vector<Constant*> Values;
382 Values.reserve(getValues().size());
383 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
384 Constant *Val = cast<Constant>(getValues()[i]);
385 if (Val == From) Val = cast<Constant>(To);
386 Values.push_back(Val);
387 }
388
389 ConstantStruct *Replacement = ConstantStruct::get(getType(), Values);
390 assert(Replacement != this && "I didn't contain From!");
391
392 // Everyone using this now uses the replacement...
393 replaceAllUsesWith(Replacement);
394
395 // Delete the old constant!
396 destroyConstant();
397}
398
399void ConstantPointerRef::replaceUsesOfWithOnConstant(Value *From, Value *To) {
400 if (isa<GlobalValue>(To)) {
401 assert(From == getOperand(0) && "Doesn't contain from!");
402 ConstantPointerRef *Replacement =
403 ConstantPointerRef::get(cast<GlobalValue>(To));
404
405 // Everyone using this now uses the replacement...
406 replaceAllUsesWith(Replacement);
407
408 // Delete the old constant!
409 destroyConstant();
410 } else {
411 // Just replace ourselves with the To value specified.
412 replaceAllUsesWith(To);
413
414 // Delete the old constant!
415 destroyConstant();
416 }
417}
418
419void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *To) {
420 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
421
Chris Lattner46b3d302003-04-16 22:40:51 +0000422 Constant *Replacement = 0;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000423 if (getOpcode() == Instruction::GetElementPtr) {
424 std::vector<Constant*> Indices;
425 Constant *Pointer = cast<Constant>(getOperand(0));
426 Indices.reserve(getNumOperands()-1);
427 if (Pointer == From) Pointer = cast<Constant>(To);
428
429 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
430 Constant *Val = cast<Constant>(getOperand(i));
431 if (Val == From) Val = cast<Constant>(To);
432 Indices.push_back(Val);
433 }
434 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
435 } else if (getOpcode() == Instruction::Cast) {
436 assert(getOperand(0) == From && "Cast only has one use!");
437 Replacement = ConstantExpr::getCast(cast<Constant>(To), getType());
438 } else if (getNumOperands() == 2) {
439 Constant *C1 = cast<Constant>(getOperand(0));
440 Constant *C2 = cast<Constant>(getOperand(1));
441 if (C1 == From) C1 = cast<Constant>(To);
442 if (C2 == From) C2 = cast<Constant>(To);
443 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
444 } else {
445 assert(0 && "Unknown ConstantExpr type!");
446 return;
447 }
448
449 assert(Replacement != this && "I didn't contain From!");
450
451 // Everyone using this now uses the replacement...
452 replaceAllUsesWith(Replacement);
453
454 // Delete the old constant!
455 destroyConstant();
456}
457
458
459
460//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000461// Factory Function Implementation
462
Chris Lattner3462ae32001-12-03 22:26:30 +0000463template<class ValType, class ConstantClass>
Chris Lattner49d855c2001-09-07 16:46:31 +0000464struct ValueMap {
465 typedef pair<const Type*, ValType> ConstHashKey;
Chris Lattner3462ae32001-12-03 22:26:30 +0000466 map<ConstHashKey, ConstantClass *> Map;
Chris Lattner49d855c2001-09-07 16:46:31 +0000467
Chris Lattner3462ae32001-12-03 22:26:30 +0000468 inline ConstantClass *get(const Type *Ty, ValType V) {
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000469 typename map<ConstHashKey,ConstantClass *>::iterator I =
Chris Lattner49d855c2001-09-07 16:46:31 +0000470 Map.find(ConstHashKey(Ty, V));
471 return (I != Map.end()) ? I->second : 0;
472 }
473
Chris Lattner3462ae32001-12-03 22:26:30 +0000474 inline void add(const Type *Ty, ValType V, ConstantClass *CP) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000475 Map.insert(make_pair(ConstHashKey(Ty, V), CP));
476 }
Chris Lattnerd7a73302001-10-13 06:57:33 +0000477
Chris Lattner3462ae32001-12-03 22:26:30 +0000478 inline void remove(ConstantClass *CP) {
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000479 for (typename map<ConstHashKey,ConstantClass *>::iterator I = Map.begin(),
Chris Lattnerd7a73302001-10-13 06:57:33 +0000480 E = Map.end(); I != E;++I)
481 if (I->second == CP) {
482 Map.erase(I);
483 return;
484 }
485 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000486};
487
Chris Lattner3462ae32001-12-03 22:26:30 +0000488//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000489//
Chris Lattner3462ae32001-12-03 22:26:30 +0000490static ValueMap<uint64_t, ConstantInt> IntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000491
Chris Lattner3462ae32001-12-03 22:26:30 +0000492ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
493 ConstantSInt *Result = (ConstantSInt*)IntConstants.get(Ty, (uint64_t)V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000494 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000495 IntConstants.add(Ty, V, Result = new ConstantSInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000496 return Result;
497}
498
Chris Lattner3462ae32001-12-03 22:26:30 +0000499ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
500 ConstantUInt *Result = (ConstantUInt*)IntConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000501 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000502 IntConstants.add(Ty, V, Result = new ConstantUInt(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000503 return Result;
504}
505
Chris Lattner3462ae32001-12-03 22:26:30 +0000506ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000507 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000508 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
509 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000510}
511
Chris Lattner3462ae32001-12-03 22:26:30 +0000512//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000513//
Chris Lattner3462ae32001-12-03 22:26:30 +0000514static ValueMap<double, ConstantFP> FPConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000515
Chris Lattner3462ae32001-12-03 22:26:30 +0000516ConstantFP *ConstantFP::get(const Type *Ty, double V) {
517 ConstantFP *Result = FPConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000518 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000519 FPConstants.add(Ty, V, Result = new ConstantFP(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000520 return Result;
521}
522
Chris Lattner3462ae32001-12-03 22:26:30 +0000523//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000524//
Chris Lattner7f74a562002-01-20 22:54:45 +0000525static ValueMap<std::vector<Constant*>, ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000526
Chris Lattner3462ae32001-12-03 22:26:30 +0000527ConstantArray *ConstantArray::get(const ArrayType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000528 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000529 ConstantArray *Result = ArrayConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000530 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000531 ArrayConstants.add(Ty, V, Result = new ConstantArray(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000532 return Result;
533}
534
Chris Lattner3462ae32001-12-03 22:26:30 +0000535// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000536// contain the specified string. A null terminator is added to the specified
537// string so that it may be used in a natural way...
538//
Chris Lattner7f74a562002-01-20 22:54:45 +0000539ConstantArray *ConstantArray::get(const std::string &Str) {
540 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000541
542 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000543 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000544
545 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000546 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000547
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000548 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000549 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000550}
551
552
Chris Lattnerd7a73302001-10-13 06:57:33 +0000553// destroyConstant - Remove the constant from the constant table...
554//
Chris Lattner3462ae32001-12-03 22:26:30 +0000555void ConstantArray::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000556 ArrayConstants.remove(this);
557 destroyConstantImpl();
558}
559
Chris Lattner81fabb02002-08-26 17:53:56 +0000560// getAsString - If the sub-element type of this array is either sbyte or ubyte,
561// then this method converts the array to an std::string and returns it.
562// Otherwise, it asserts out.
563//
564std::string ConstantArray::getAsString() const {
565 std::string Result;
566 if (getType()->getElementType() == Type::SByteTy)
567 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
568 Result += (char)cast<ConstantSInt>(getOperand(i))->getValue();
569 else {
570 assert(getType()->getElementType() == Type::UByteTy && "Not a string!");
571 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
572 Result += (char)cast<ConstantUInt>(getOperand(i))->getValue();
573 }
574 return Result;
575}
576
577
Chris Lattner3462ae32001-12-03 22:26:30 +0000578//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000579//
Chris Lattner7f74a562002-01-20 22:54:45 +0000580static ValueMap<std::vector<Constant*>, ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000581
Chris Lattner3462ae32001-12-03 22:26:30 +0000582ConstantStruct *ConstantStruct::get(const StructType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000583 const std::vector<Constant*> &V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000584 ConstantStruct *Result = StructConstants.get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000585 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000586 StructConstants.add(Ty, V, Result = new ConstantStruct(Ty, V));
Chris Lattner49d855c2001-09-07 16:46:31 +0000587 return Result;
588}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000589
Chris Lattnerd7a73302001-10-13 06:57:33 +0000590// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000591//
Chris Lattner3462ae32001-12-03 22:26:30 +0000592void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000593 StructConstants.remove(this);
594 destroyConstantImpl();
595}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000596
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000597
Chris Lattner3462ae32001-12-03 22:26:30 +0000598//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000599//
Chris Lattner3462ae32001-12-03 22:26:30 +0000600static ValueMap<char, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000601
Chris Lattner3462ae32001-12-03 22:26:30 +0000602ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
603 ConstantPointerNull *Result = NullPtrConstants.get(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000604 if (!Result) // If no preexisting value, create one now...
Chris Lattner3462ae32001-12-03 22:26:30 +0000605 NullPtrConstants.add(Ty, 0, Result = new ConstantPointerNull(Ty));
Chris Lattner883ad0b2001-10-03 15:39:36 +0000606 return Result;
607}
608
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000609// destroyConstant - Remove the constant from the constant table...
610//
611void ConstantPointerNull::destroyConstant() {
612 NullPtrConstants.remove(this);
613 destroyConstantImpl();
614}
615
616
Chris Lattner3462ae32001-12-03 22:26:30 +0000617//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +0000618//
Chris Lattner3462ae32001-12-03 22:26:30 +0000619ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000620 assert(GV->getParent() && "Global Value must be attached to a module!");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000621
Chris Lattnerd7a73302001-10-13 06:57:33 +0000622 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +0000623 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000624}
625
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000626// destroyConstant - Remove the constant from the constant table...
627//
628void ConstantPointerRef::destroyConstant() {
629 getValue()->getParent()->destroyConstantPointerRef(this);
630 destroyConstantImpl();
631}
632
633
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000634//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000635//
Vikram S. Adve4c485332002-07-15 18:19:33 +0000636typedef pair<unsigned, vector<Constant*> > ExprMapKeyType;
637static ValueMap<const ExprMapKeyType, ConstantExpr> ExprConstants;
638
Chris Lattner46b3d302003-04-16 22:40:51 +0000639Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattneracdbe712003-04-17 19:24:48 +0000640 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
641 return FC; // Fold a few common cases...
642
Vikram S. Adve4c485332002-07-15 18:19:33 +0000643 // Look up the constant in the table first to ensure uniqueness
644 vector<Constant*> argVec(1, C);
Chris Lattner330b7ac2002-08-14 18:24:09 +0000645 const ExprMapKeyType &Key = make_pair(Instruction::Cast, argVec);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000646 ConstantExpr *Result = ExprConstants.get(Ty, Key);
647 if (Result) return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000648
649 // Its not in the table so create a new one and put it in the table.
Chris Lattner330b7ac2002-08-14 18:24:09 +0000650 Result = new ConstantExpr(Instruction::Cast, C, Ty);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000651 ExprConstants.add(Ty, Key, Result);
652 return Result;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000653}
Chris Lattnerd7a73302001-10-13 06:57:33 +0000654
Chris Lattner46b3d302003-04-16 22:40:51 +0000655Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattneracdbe712003-04-17 19:24:48 +0000656
657 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
658 return FC; // Fold a few common cases...
659
Vikram S. Adve4c485332002-07-15 18:19:33 +0000660 // Look up the constant in the table first to ensure uniqueness
661 vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000662 const ExprMapKeyType &Key = make_pair(Opcode, argVec);
Chris Lattner3cd8c562002-07-30 18:54:25 +0000663 ConstantExpr *Result = ExprConstants.get(C1->getType(), Key);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000664 if (Result) return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000665
666 // Its not in the table so create a new one and put it in the table.
667 // Check the operands for consistency first
Chris Lattner69ce8672002-10-13 19:39:16 +0000668 assert((Opcode >= Instruction::BinaryOpsBegin &&
669 Opcode < Instruction::BinaryOpsEnd) &&
Chris Lattner3cd8c562002-07-30 18:54:25 +0000670 "Invalid opcode in binary constant expression");
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000671
Chris Lattner3cd8c562002-07-30 18:54:25 +0000672 assert(C1->getType() == C2->getType() &&
673 "Operand types in binary constant expression should match");
Vikram S. Adve4c485332002-07-15 18:19:33 +0000674
Chris Lattner3cd8c562002-07-30 18:54:25 +0000675 Result = new ConstantExpr(Opcode, C1, C2);
676 ExprConstants.add(C1->getType(), Key, Result);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000677 return Result;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000678}
679
Chris Lattner46b3d302003-04-16 22:40:51 +0000680Constant *ConstantExpr::getGetElementPtr(Constant *C,
681 const std::vector<Constant*> &IdxList){
Chris Lattneracdbe712003-04-17 19:24:48 +0000682 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
683 return FC; // Fold a few common cases...
Chris Lattner3cd8c562002-07-30 18:54:25 +0000684 const Type *Ty = C->getType();
Vikram S. Adve4c485332002-07-15 18:19:33 +0000685
686 // Look up the constant in the table first to ensure uniqueness
687 vector<Constant*> argVec(1, C);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000688 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
Vikram S. Adve4c485332002-07-15 18:19:33 +0000689
Chris Lattner3cd8c562002-07-30 18:54:25 +0000690 const ExprMapKeyType &Key = make_pair(Instruction::GetElementPtr, argVec);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000691 ConstantExpr *Result = ExprConstants.get(Ty, Key);
692 if (Result) return Result;
Chris Lattner3cd8c562002-07-30 18:54:25 +0000693
Vikram S. Adve4c485332002-07-15 18:19:33 +0000694 // Its not in the table so create a new one and put it in the table.
695 // Check the operands for consistency first
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000696 //
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000697 assert(isa<PointerType>(Ty) &&
698 "Non-pointer type for constant GelElementPtr expression");
699
Chris Lattner3cd8c562002-07-30 18:54:25 +0000700 // Check that the indices list is valid...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000701 std::vector<Value*> ValIdxList(IdxList.begin(), IdxList.end());
Chris Lattner3cd8c562002-07-30 18:54:25 +0000702 const Type *DestTy = GetElementPtrInst::getIndexedType(Ty, ValIdxList, true);
703 assert(DestTy && "Invalid index list for constant GelElementPtr expression");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000704
Chris Lattner3cd8c562002-07-30 18:54:25 +0000705 Result = new ConstantExpr(C, IdxList, PointerType::get(DestTy));
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000706 ExprConstants.add(Ty, Key, Result);
707 return Result;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000708}
709
710// destroyConstant - Remove the constant from the constant table...
711//
712void ConstantExpr::destroyConstant() {
713 ExprConstants.remove(this);
714 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000715}
716
Chris Lattner3cd8c562002-07-30 18:54:25 +0000717const char *ConstantExpr::getOpcodeName() const {
718 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000719}
720
Chris Lattner163b8902002-10-14 03:30:23 +0000721unsigned Constant::mutateReferences(Value *OldV, Value *NewV) {
722 // Uses of constant pointer refs are global values, not constants!
723 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
724 GlobalValue *NewGV = cast<GlobalValue>(NewV);
725 GlobalValue *OldGV = CPR->getValue();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000726
Chris Lattner163b8902002-10-14 03:30:23 +0000727 assert(OldGV == OldV && "Cannot mutate old value if I'm not using it!");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000728
Chris Lattner163b8902002-10-14 03:30:23 +0000729 OldGV->getParent()->mutateConstantPointerRef(OldGV, NewGV);
730 Operands[0] = NewGV;
731 return 1;
732 } else {
733 Constant *NewC = cast<Constant>(NewV);
734 unsigned NumReplaced = 0;
735 for (unsigned i = 0, N = getNumOperands(); i != N; ++i)
736 if (Operands[i] == OldV) {
737 ++NumReplaced;
738 Operands[i] = NewC;
739 }
740 return NumReplaced;
741 }
Chris Lattner25033252001-10-03 19:28:15 +0000742}