blob: eb93da3c1d0a9dbc009a77da20119f410eb11a01 [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 Lattnerab69ecc2003-08-18 16:54:48 +0000216 assert(V[i]->getType() == T->getElementType() ||
217 (T->isAbstract() &&
218 V[i]->getType()->getPrimitiveID() ==
219 T->getElementType()->getPrimitiveID()));
Chris Lattnera073acb2001-07-07 08:36:50 +0000220 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000221 }
222}
223
Chris Lattner3462ae32001-12-03 22:26:30 +0000224ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000225 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000226 const StructType::ElementTypes &ETypes = T->getElementTypes();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000227 assert(V.size() == ETypes.size() &&
228 "Invalid initializer vector for constant structure");
Chris Lattner0d779712002-10-08 23:33:52 +0000229 Operands.reserve(V.size());
230 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000231 assert((V[i]->getType() == ETypes[i] ||
232 (ETypes[i]->isAbstract() &&
233 ETypes[i]->getPrimitiveID()==V[i]->getType()->getPrimitiveID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000234 "Initializer for struct element doesn't match struct element type!");
Chris Lattnera073acb2001-07-07 08:36:50 +0000235 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000236 }
237}
238
Chris Lattner3462ae32001-12-03 22:26:30 +0000239ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
240 : ConstantPointer(GV->getType()) {
Chris Lattner60e0dd72001-10-03 06:12:09 +0000241 Operands.push_back(Use(GV, this));
242}
243
Chris Lattner3cd8c562002-07-30 18:54:25 +0000244ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
245 : Constant(Ty), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000246 Operands.push_back(Use(C, this));
247}
248
Chris Lattner22ced562003-06-22 20:48:30 +0000249static bool isSetCC(unsigned Opcode) {
250 return Opcode == Instruction::SetEQ || Opcode == Instruction::SetNE ||
251 Opcode == Instruction::SetLT || Opcode == Instruction::SetGT ||
252 Opcode == Instruction::SetLE || Opcode == Instruction::SetGE;
253}
254
Chris Lattner3cd8c562002-07-30 18:54:25 +0000255ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Chris Lattner22ced562003-06-22 20:48:30 +0000256 : Constant(isSetCC(Opcode) ? Type::BoolTy : C1->getType()), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000257 Operands.push_back(Use(C1, this));
258 Operands.push_back(Use(C2, this));
259}
260
Chris Lattner3cd8c562002-07-30 18:54:25 +0000261ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
262 const Type *DestTy)
263 : Constant(DestTy), iType(Instruction::GetElementPtr) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000264 Operands.reserve(1+IdxList.size());
265 Operands.push_back(Use(C, this));
266 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
267 Operands.push_back(Use(IdxList[i], this));
268}
269
Chris Lattner60e0dd72001-10-03 06:12:09 +0000270
Chris Lattner2f7c9632001-06-06 20:29:01 +0000271
272//===----------------------------------------------------------------------===//
Chris Lattnerd7a73302001-10-13 06:57:33 +0000273// classof implementations
274
Chris Lattnerb1585a92002-08-13 17:50:20 +0000275bool ConstantIntegral::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000276 return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
Chris Lattner41e99a02002-08-12 21:21:21 +0000277}
278
Chris Lattner3462ae32001-12-03 22:26:30 +0000279bool ConstantInt::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000280 return CPV->getType()->isInteger() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000281}
Chris Lattner3462ae32001-12-03 22:26:30 +0000282bool ConstantSInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000283 return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000284}
Chris Lattner3462ae32001-12-03 22:26:30 +0000285bool ConstantUInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000286 return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000287}
Chris Lattner3462ae32001-12-03 22:26:30 +0000288bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000289 const Type *Ty = CPV->getType();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000290 return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000291 !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000292}
Chris Lattner3462ae32001-12-03 22:26:30 +0000293bool ConstantArray::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000294 return isa<ArrayType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000295}
Chris Lattner3462ae32001-12-03 22:26:30 +0000296bool ConstantStruct::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000297 return isa<StructType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000298}
Chris Lattner3462ae32001-12-03 22:26:30 +0000299bool ConstantPointer::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000300 return (isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000301}
302
303
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000304
Chris Lattner2f7c9632001-06-06 20:29:01 +0000305//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000306// isValueValidForType implementations
307
Chris Lattner3462ae32001-12-03 22:26:30 +0000308bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000309 switch (Ty->getPrimitiveID()) {
310 default:
311 return false; // These can't be represented as integers!!!
312
313 // Signed types...
314 case Type::SByteTyID:
315 return (Val <= INT8_MAX && Val >= INT8_MIN);
316 case Type::ShortTyID:
317 return (Val <= INT16_MAX && Val >= INT16_MIN);
318 case Type::IntTyID:
319 return (Val <= INT32_MAX && Val >= INT32_MIN);
320 case Type::LongTyID:
321 return true; // This is the largest type...
322 }
323 assert(0 && "WTF?");
324 return false;
325}
326
Chris Lattner3462ae32001-12-03 22:26:30 +0000327bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000328 switch (Ty->getPrimitiveID()) {
329 default:
330 return false; // These can't be represented as integers!!!
331
332 // Unsigned types...
333 case Type::UByteTyID:
334 return (Val <= UINT8_MAX);
335 case Type::UShortTyID:
336 return (Val <= UINT16_MAX);
337 case Type::UIntTyID:
338 return (Val <= UINT32_MAX);
339 case Type::ULongTyID:
340 return true; // This is the largest type...
341 }
342 assert(0 && "WTF?");
343 return false;
344}
345
Chris Lattner3462ae32001-12-03 22:26:30 +0000346bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000347 switch (Ty->getPrimitiveID()) {
348 default:
349 return false; // These can't be represented as floating point!
350
351 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000352 case Type::FloatTyID:
Chris Lattnerd06dd692001-07-15 00:18:39 +0000353 /*
Chris Lattner2f7c9632001-06-06 20:29:01 +0000354 return (Val <= UINT8_MAX);
355 */
356 case Type::DoubleTyID:
357 return true; // This is the largest type...
358 }
359};
Chris Lattner9655e542001-07-20 19:16:02 +0000360
Chris Lattner49d855c2001-09-07 16:46:31 +0000361//===----------------------------------------------------------------------===//
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000362// replaceUsesOfWithOnConstant implementations
363
364void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To) {
365 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
366
367 std::vector<Constant*> Values;
368 Values.reserve(getValues().size()); // Build replacement array...
369 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
370 Constant *Val = cast<Constant>(getValues()[i]);
371 if (Val == From) Val = cast<Constant>(To);
372 Values.push_back(Val);
373 }
374
375 ConstantArray *Replacement = ConstantArray::get(getType(), Values);
376 assert(Replacement != this && "I didn't contain From!");
377
378 // Everyone using this now uses the replacement...
379 replaceAllUsesWith(Replacement);
380
381 // Delete the old constant!
382 destroyConstant();
383}
384
385void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To) {
386 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
387
388 std::vector<Constant*> Values;
389 Values.reserve(getValues().size());
390 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
391 Constant *Val = cast<Constant>(getValues()[i]);
392 if (Val == From) Val = cast<Constant>(To);
393 Values.push_back(Val);
394 }
395
396 ConstantStruct *Replacement = ConstantStruct::get(getType(), Values);
397 assert(Replacement != this && "I didn't contain From!");
398
399 // Everyone using this now uses the replacement...
400 replaceAllUsesWith(Replacement);
401
402 // Delete the old constant!
403 destroyConstant();
404}
405
406void ConstantPointerRef::replaceUsesOfWithOnConstant(Value *From, Value *To) {
407 if (isa<GlobalValue>(To)) {
408 assert(From == getOperand(0) && "Doesn't contain from!");
409 ConstantPointerRef *Replacement =
410 ConstantPointerRef::get(cast<GlobalValue>(To));
411
412 // Everyone using this now uses the replacement...
413 replaceAllUsesWith(Replacement);
414
415 // Delete the old constant!
416 destroyConstant();
417 } else {
418 // Just replace ourselves with the To value specified.
419 replaceAllUsesWith(To);
420
421 // Delete the old constant!
422 destroyConstant();
423 }
424}
425
Chris Lattner55ed6562003-05-14 17:51:05 +0000426void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV) {
427 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
428 Constant *To = cast<Constant>(ToV);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000429
Chris Lattner46b3d302003-04-16 22:40:51 +0000430 Constant *Replacement = 0;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000431 if (getOpcode() == Instruction::GetElementPtr) {
432 std::vector<Constant*> Indices;
Chris Lattner55ed6562003-05-14 17:51:05 +0000433 Constant *Pointer = getOperand(0);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000434 Indices.reserve(getNumOperands()-1);
Chris Lattner55ed6562003-05-14 17:51:05 +0000435 if (Pointer == From) Pointer = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000436
437 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000438 Constant *Val = getOperand(i);
439 if (Val == From) Val = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000440 Indices.push_back(Val);
441 }
442 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
443 } else if (getOpcode() == Instruction::Cast) {
444 assert(getOperand(0) == From && "Cast only has one use!");
Chris Lattner55ed6562003-05-14 17:51:05 +0000445 Replacement = ConstantExpr::getCast(To, getType());
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000446 } else if (getNumOperands() == 2) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000447 Constant *C1 = getOperand(0);
448 Constant *C2 = getOperand(1);
449 if (C1 == From) C1 = To;
450 if (C2 == From) C2 = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000451 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
452 } else {
453 assert(0 && "Unknown ConstantExpr type!");
454 return;
455 }
456
457 assert(Replacement != this && "I didn't contain From!");
458
459 // Everyone using this now uses the replacement...
460 replaceAllUsesWith(Replacement);
461
462 // Delete the old constant!
463 destroyConstant();
464}
465
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000466//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000467// Factory Function Implementation
468
Chris Lattner3a5c2822003-07-30 19:04:37 +0000469// ReplaceUsesOfWith - This is exactly the same as Value::replaceAllUsesWith,
470// except that it doesn't have all of the asserts. The asserts fail because we
471// are half-way done resolving types, which causes some types to exist as two
472// different Type*'s at the same time. This is a sledgehammer to work around
473// this problem.
474//
475static void ReplaceUsesOfWith(Value *Old, Value *New) {
476 while (!Old->use_empty()) {
477 User *Use = Old->use_back();
478 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
479 // constant!
480 if (Constant *C = dyn_cast<Constant>(Use)) {
481 C->replaceUsesOfWithOnConstant(Old, New);
482 } else {
483 Use->replaceUsesOfWith(Old, New);
484 }
485 }
486}
487
488
Chris Lattner98fa07b2003-05-23 20:03:32 +0000489// ConstantCreator - A class that is used to create constants by
490// ValueMap*. This class should be partially specialized if there is
491// something strange that needs to be done to interface to the ctor for the
492// constant.
493//
494template<class ConstantClass, class TypeClass, class ValType>
495struct ConstantCreator {
496 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
497 return new ConstantClass(Ty, V);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000498 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000499};
500
Chris Lattner98fa07b2003-05-23 20:03:32 +0000501namespace {
502 template<class ValType, class TypeClass, class ConstantClass>
503 class ValueMap {
504 protected:
505 typedef std::pair<const TypeClass*, ValType> ConstHashKey;
506 std::map<ConstHashKey, ConstantClass *> Map;
507 public:
508 // getOrCreate - Return the specified constant from the map, creating it if
509 // necessary.
510 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
511 ConstHashKey Lookup(Ty, V);
512 typename std::map<ConstHashKey,ConstantClass *>::iterator I =
513 Map.lower_bound(Lookup);
514 if (I != Map.end() && I->first == Lookup)
515 return I->second; // Is it in the map?
516
517 // If no preexisting value, create one now...
518 ConstantClass *Result =
519 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
520
521 Map.insert(I, std::make_pair(ConstHashKey(Ty, V), Result));
522 return Result;
523 }
524
525 void remove(ConstantClass *CP) {
526 // FIXME: This could be sped up a LOT. If this gets to be a performance
527 // problem, someone should look at this.
528 for (typename std::map<ConstHashKey, ConstantClass*>::iterator
529 I = Map.begin(), E = Map.end(); I != E; ++I)
530 if (I->second == CP) {
531 Map.erase(I);
532 return;
533 }
534 assert(0 && "Constant not found in constant table!");
535 }
536 };
537}
538
539
540
Chris Lattner3462ae32001-12-03 22:26:30 +0000541//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000542//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000543static ValueMap< int64_t, Type, ConstantSInt> SIntConstants;
544static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000545
Chris Lattner3462ae32001-12-03 22:26:30 +0000546ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000547 return SIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000548}
549
Chris Lattner3462ae32001-12-03 22:26:30 +0000550ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000551 return UIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000552}
553
Chris Lattner3462ae32001-12-03 22:26:30 +0000554ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000555 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000556 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
557 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000558}
559
Chris Lattner3462ae32001-12-03 22:26:30 +0000560//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000561//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000562static ValueMap<double, Type, ConstantFP> FPConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000563
Chris Lattner3462ae32001-12-03 22:26:30 +0000564ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000565 return FPConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000566}
567
Chris Lattner3462ae32001-12-03 22:26:30 +0000568//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000569//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000570static ValueMap<std::vector<Constant*>, ArrayType,
571 ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000572
Chris Lattner3462ae32001-12-03 22:26:30 +0000573ConstantArray *ConstantArray::get(const ArrayType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000574 const std::vector<Constant*> &V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000575 return ArrayConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000576}
577
Chris Lattner98fa07b2003-05-23 20:03:32 +0000578// destroyConstant - Remove the constant from the constant table...
579//
580void ConstantArray::destroyConstant() {
581 ArrayConstants.remove(this);
582 destroyConstantImpl();
583}
584
585/// refineAbstractType - If this callback is invoked, then this constant is of a
586/// derived type, change all users to use a concrete constant of the new type.
587///
588void ConstantArray::refineAbstractType(const DerivedType *OldTy,
589 const Type *NewTy) {
590 Value::refineAbstractType(OldTy, NewTy);
Chris Lattner7fa67832003-06-02 17:25:46 +0000591 if (OldTy == NewTy) return;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000592
593 // Make everyone now use a constant of the new type...
594 std::vector<Constant*> C;
595 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
596 C.push_back(cast<Constant>(getOperand(i)));
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000597 Constant *New = ConstantArray::get(cast<ArrayType>(NewTy), C);
598 if (New != this) {
Chris Lattner3a5c2822003-07-30 19:04:37 +0000599 ReplaceUsesOfWith(this, New);
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000600 destroyConstant(); // This constant is now dead, destroy it.
601 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000602}
603
604
Chris Lattner3462ae32001-12-03 22:26:30 +0000605// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000606// contain the specified string. A null terminator is added to the specified
607// string so that it may be used in a natural way...
608//
Chris Lattner7f74a562002-01-20 22:54:45 +0000609ConstantArray *ConstantArray::get(const std::string &Str) {
610 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000611
612 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000613 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000614
615 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000616 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000617
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000618 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000619 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000620}
621
Chris Lattner81fabb02002-08-26 17:53:56 +0000622// getAsString - If the sub-element type of this array is either sbyte or ubyte,
623// then this method converts the array to an std::string and returns it.
624// Otherwise, it asserts out.
625//
626std::string ConstantArray::getAsString() const {
Chris Lattner6077c312003-07-23 15:22:26 +0000627 assert((getType()->getElementType() == Type::UByteTy ||
628 getType()->getElementType() == Type::SByteTy) && "Not a string!");
629
Chris Lattner81fabb02002-08-26 17:53:56 +0000630 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +0000631 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
632 Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue();
Chris Lattner81fabb02002-08-26 17:53:56 +0000633 return Result;
634}
635
636
Chris Lattner3462ae32001-12-03 22:26:30 +0000637//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000638//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000639static ValueMap<std::vector<Constant*>, StructType,
640 ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000641
Chris Lattner3462ae32001-12-03 22:26:30 +0000642ConstantStruct *ConstantStruct::get(const StructType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000643 const std::vector<Constant*> &V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000644 return StructConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000645}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000646
Chris Lattnerd7a73302001-10-13 06:57:33 +0000647// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000648//
Chris Lattner3462ae32001-12-03 22:26:30 +0000649void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000650 StructConstants.remove(this);
651 destroyConstantImpl();
652}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000653
Chris Lattner98fa07b2003-05-23 20:03:32 +0000654/// refineAbstractType - If this callback is invoked, then this constant is of a
655/// derived type, change all users to use a concrete constant of the new type.
656///
657void ConstantStruct::refineAbstractType(const DerivedType *OldTy,
658 const Type *NewTy) {
659 Value::refineAbstractType(OldTy, NewTy);
Chris Lattner7fa67832003-06-02 17:25:46 +0000660 if (OldTy == NewTy) return;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000661
662 // Make everyone now use a constant of the new type...
663 std::vector<Constant*> C;
664 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
665 C.push_back(cast<Constant>(getOperand(i)));
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000666 Constant *New = ConstantStruct::get(cast<StructType>(NewTy), C);
667 if (New != this) {
Chris Lattner3a5c2822003-07-30 19:04:37 +0000668 ReplaceUsesOfWith(this, New);
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000669 destroyConstant(); // This constant is now dead, destroy it.
670 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000671}
672
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000673
Chris Lattner3462ae32001-12-03 22:26:30 +0000674//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000675//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000676
677// ConstantPointerNull does not take extra "value" argument...
678template<class ValType>
679struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
680 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
681 return new ConstantPointerNull(Ty);
682 }
683};
684
685static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000686
Chris Lattner3462ae32001-12-03 22:26:30 +0000687ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000688 return NullPtrConstants.getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000689}
690
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000691// destroyConstant - Remove the constant from the constant table...
692//
693void ConstantPointerNull::destroyConstant() {
694 NullPtrConstants.remove(this);
695 destroyConstantImpl();
696}
697
Chris Lattner98fa07b2003-05-23 20:03:32 +0000698/// refineAbstractType - If this callback is invoked, then this constant is of a
699/// derived type, change all users to use a concrete constant of the new type.
700///
701void ConstantPointerNull::refineAbstractType(const DerivedType *OldTy,
702 const Type *NewTy) {
703 Value::refineAbstractType(OldTy, NewTy);
Chris Lattner7fa67832003-06-02 17:25:46 +0000704 if (OldTy == NewTy) return;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000705
706 // Make everyone now use a constant of the new type...
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000707 Constant *New = ConstantPointerNull::get(cast<PointerType>(NewTy));
708 if (New != this) {
Chris Lattner3a5c2822003-07-30 19:04:37 +0000709 ReplaceUsesOfWith(this, New);
Chris Lattner20ec7bc2003-05-25 16:15:32 +0000710
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000711 // This constant is now dead, destroy it.
712 destroyConstant();
713 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000714}
715
716
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000717
Chris Lattner3462ae32001-12-03 22:26:30 +0000718//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +0000719//
Chris Lattner3462ae32001-12-03 22:26:30 +0000720ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000721 assert(GV->getParent() && "Global Value must be attached to a module!");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000722
Chris Lattnerd7a73302001-10-13 06:57:33 +0000723 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +0000724 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000725}
726
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000727// destroyConstant - Remove the constant from the constant table...
728//
729void ConstantPointerRef::destroyConstant() {
730 getValue()->getParent()->destroyConstantPointerRef(this);
731 destroyConstantImpl();
732}
733
734
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000735//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000736//
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000737typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000738
739template<>
740struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
741 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
742 if (V.first == Instruction::Cast)
743 return new ConstantExpr(Instruction::Cast, V.second[0], Ty);
744 if ((V.first >= Instruction::BinaryOpsBegin &&
745 V.first < Instruction::BinaryOpsEnd) ||
746 V.first == Instruction::Shl || V.first == Instruction::Shr)
747 return new ConstantExpr(V.first, V.second[0], V.second[1]);
748
749 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
750
751 // Check that the indices list is valid...
752 std::vector<Value*> ValIdxList(V.second.begin()+1, V.second.end());
753 const Type *DestTy = GetElementPtrInst::getIndexedType(Ty, ValIdxList,
754 true);
755 assert(DestTy && "Invalid index list for GetElementPtr expression");
756
757 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
758 return new ConstantExpr(V.second[0], IdxList, PointerType::get(DestTy));
759 }
760};
761
762static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000763
Chris Lattner46b3d302003-04-16 22:40:51 +0000764Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattneracdbe712003-04-17 19:24:48 +0000765 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
766 return FC; // Fold a few common cases...
767
Vikram S. Adve4c485332002-07-15 18:19:33 +0000768 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000769 std::vector<Constant*> argVec(1, C);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000770 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
771 return ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000772}
Chris Lattnerd7a73302001-10-13 06:57:33 +0000773
Chris Lattner46b3d302003-04-16 22:40:51 +0000774Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +0000775 // Check the operands for consistency first
776 assert((Opcode >= Instruction::BinaryOpsBegin &&
777 Opcode < Instruction::BinaryOpsEnd) &&
778 "Invalid opcode in binary constant expression");
779 assert(C1->getType() == C2->getType() &&
780 "Operand types in binary constant expression should match");
Chris Lattneracdbe712003-04-17 19:24:48 +0000781
782 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
783 return FC; // Fold a few common cases...
784
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000785 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000786 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
787 return ExprConstants.getOrCreate(C1->getType(), Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000788}
789
Chris Lattner38a9bcd2003-05-21 17:49:25 +0000790/// getShift - Return a shift left or shift right constant expr
791Constant *ConstantExpr::getShift(unsigned Opcode, Constant *C1, Constant *C2) {
792 // Check the operands for consistency first
793 assert((Opcode == Instruction::Shl ||
794 Opcode == Instruction::Shr) &&
795 "Invalid opcode in binary constant expression");
796 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
797 "Invalid operand types for Shift constant expr!");
798
799 if (Constant *FC = ConstantFoldShiftInstruction(Opcode, C1, C2))
800 return FC; // Fold a few common cases...
801
802 // Look up the constant in the table first to ensure uniqueness
803 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000804 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
805 return ExprConstants.getOrCreate(C1->getType(), Key);
Chris Lattner38a9bcd2003-05-21 17:49:25 +0000806}
807
808
Chris Lattner46b3d302003-04-16 22:40:51 +0000809Constant *ConstantExpr::getGetElementPtr(Constant *C,
810 const std::vector<Constant*> &IdxList){
Chris Lattneracdbe712003-04-17 19:24:48 +0000811 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
812 return FC; // Fold a few common cases...
Chris Lattner3cd8c562002-07-30 18:54:25 +0000813 const Type *Ty = C->getType();
Chris Lattner98fa07b2003-05-23 20:03:32 +0000814 assert(isa<PointerType>(Ty) &&
815 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +0000816
817 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000818 std::vector<Constant*> argVec(1, C);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000819 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
Vikram S. Adve4c485332002-07-15 18:19:33 +0000820
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000821 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,argVec);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000822 return ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +0000823}
824
825// destroyConstant - Remove the constant from the constant table...
826//
827void ConstantExpr::destroyConstant() {
828 ExprConstants.remove(this);
829 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000830}
831
Chris Lattner98fa07b2003-05-23 20:03:32 +0000832/// refineAbstractType - If this callback is invoked, then this constant is of a
833/// derived type, change all users to use a concrete constant of the new type.
834///
835void ConstantExpr::refineAbstractType(const DerivedType *OldTy,
836 const Type *NewTy) {
837 Value::refineAbstractType(OldTy, NewTy);
Chris Lattner7fa67832003-06-02 17:25:46 +0000838 if (OldTy == NewTy) return;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000839
840 // FIXME: These need to use a lower-level implementation method, because the
841 // ::get methods intuit the type of the result based on the types of the
842 // operands. The operand types may not have had their types resolved yet.
843 //
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000844 Constant *New;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000845 if (getOpcode() == Instruction::Cast) {
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000846 New = getCast(getOperand(0), NewTy);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000847 } else if (getOpcode() >= Instruction::BinaryOpsBegin &&
848 getOpcode() < Instruction::BinaryOpsEnd) {
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000849 New = get(getOpcode(), getOperand(0), getOperand(0));
Chris Lattner98fa07b2003-05-23 20:03:32 +0000850 } else if (getOpcode() == Instruction::Shl || getOpcode() ==Instruction::Shr){
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000851 New = getShift(getOpcode(), getOperand(0), getOperand(0));
Chris Lattner98fa07b2003-05-23 20:03:32 +0000852 } else {
853 assert(getOpcode() == Instruction::GetElementPtr);
854
855 // Make everyone now use a constant of the new type...
856 std::vector<Constant*> C;
857 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
858 C.push_back(cast<Constant>(getOperand(i)));
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000859 New = ConstantExpr::getGetElementPtr(getOperand(0), C);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000860 }
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000861 if (New != this) {
Chris Lattner3a5c2822003-07-30 19:04:37 +0000862 ReplaceUsesOfWith(this, New);
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000863 destroyConstant(); // This constant is now dead, destroy it.
864 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000865}
866
867
868
869
Chris Lattner3cd8c562002-07-30 18:54:25 +0000870const char *ConstantExpr::getOpcodeName() const {
871 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000872}
873
Chris Lattner163b8902002-10-14 03:30:23 +0000874unsigned Constant::mutateReferences(Value *OldV, Value *NewV) {
875 // Uses of constant pointer refs are global values, not constants!
876 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
877 GlobalValue *NewGV = cast<GlobalValue>(NewV);
878 GlobalValue *OldGV = CPR->getValue();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000879
Chris Lattner163b8902002-10-14 03:30:23 +0000880 assert(OldGV == OldV && "Cannot mutate old value if I'm not using it!");
Chris Lattner163b8902002-10-14 03:30:23 +0000881 Operands[0] = NewGV;
Chris Lattnercb4d26f2003-05-15 19:37:21 +0000882 OldGV->getParent()->mutateConstantPointerRef(OldGV, NewGV);
Chris Lattner163b8902002-10-14 03:30:23 +0000883 return 1;
884 } else {
885 Constant *NewC = cast<Constant>(NewV);
886 unsigned NumReplaced = 0;
887 for (unsigned i = 0, N = getNumOperands(); i != N; ++i)
888 if (Operands[i] == OldV) {
889 ++NumReplaced;
890 Operands[i] = NewC;
891 }
892 return NumReplaced;
893 }
Chris Lattner25033252001-10-03 19:28:15 +0000894}