blob: c252247dd0b70fc042d4575cbc3e8dcf249d34f0 [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
Misha Brukmanbe372b92003-08-21 22:14:26 +000034 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +000035 // 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 Lattner2f7c9632001-06-06 20:29:01 +0000353 case Type::DoubleTyID:
354 return true; // This is the largest type...
355 }
356};
Chris Lattner9655e542001-07-20 19:16:02 +0000357
Chris Lattner49d855c2001-09-07 16:46:31 +0000358//===----------------------------------------------------------------------===//
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000359// replaceUsesOfWithOnConstant implementations
360
Chris Lattnerc27038d2003-08-29 05:36:46 +0000361void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
362 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000363 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
364
365 std::vector<Constant*> Values;
366 Values.reserve(getValues().size()); // Build replacement array...
367 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
368 Constant *Val = cast<Constant>(getValues()[i]);
369 if (Val == From) Val = cast<Constant>(To);
370 Values.push_back(Val);
371 }
372
373 ConstantArray *Replacement = ConstantArray::get(getType(), Values);
374 assert(Replacement != this && "I didn't contain From!");
375
376 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000377 if (DisableChecking)
378 uncheckedReplaceAllUsesWith(Replacement);
379 else
380 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000381
382 // Delete the old constant!
383 destroyConstant();
384}
385
Chris Lattnerc27038d2003-08-29 05:36:46 +0000386void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
387 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000388 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
389
390 std::vector<Constant*> Values;
391 Values.reserve(getValues().size());
392 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
393 Constant *Val = cast<Constant>(getValues()[i]);
394 if (Val == From) Val = cast<Constant>(To);
395 Values.push_back(Val);
396 }
397
398 ConstantStruct *Replacement = ConstantStruct::get(getType(), Values);
399 assert(Replacement != this && "I didn't contain From!");
400
401 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000402 if (DisableChecking)
403 uncheckedReplaceAllUsesWith(Replacement);
404 else
405 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000406
407 // Delete the old constant!
408 destroyConstant();
409}
410
Chris Lattnerc27038d2003-08-29 05:36:46 +0000411void ConstantPointerRef::replaceUsesOfWithOnConstant(Value *From, Value *To,
412 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000413 if (isa<GlobalValue>(To)) {
414 assert(From == getOperand(0) && "Doesn't contain from!");
415 ConstantPointerRef *Replacement =
416 ConstantPointerRef::get(cast<GlobalValue>(To));
417
418 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000419 if (DisableChecking)
420 uncheckedReplaceAllUsesWith(Replacement);
421 else
422 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000423
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000424 } else {
425 // Just replace ourselves with the To value specified.
Chris Lattnerc27038d2003-08-29 05:36:46 +0000426 if (DisableChecking)
427 uncheckedReplaceAllUsesWith(To);
428 else
429 replaceAllUsesWith(To);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000430 }
Chris Lattnerc27038d2003-08-29 05:36:46 +0000431
432 // Delete the old constant!
433 destroyConstant();
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000434}
435
Chris Lattnerc27038d2003-08-29 05:36:46 +0000436void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
437 bool DisableChecking) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000438 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
439 Constant *To = cast<Constant>(ToV);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000440
Chris Lattner46b3d302003-04-16 22:40:51 +0000441 Constant *Replacement = 0;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000442 if (getOpcode() == Instruction::GetElementPtr) {
443 std::vector<Constant*> Indices;
Chris Lattner55ed6562003-05-14 17:51:05 +0000444 Constant *Pointer = getOperand(0);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000445 Indices.reserve(getNumOperands()-1);
Chris Lattner55ed6562003-05-14 17:51:05 +0000446 if (Pointer == From) Pointer = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000447
448 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000449 Constant *Val = getOperand(i);
450 if (Val == From) Val = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000451 Indices.push_back(Val);
452 }
453 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
454 } else if (getOpcode() == Instruction::Cast) {
455 assert(getOperand(0) == From && "Cast only has one use!");
Chris Lattner55ed6562003-05-14 17:51:05 +0000456 Replacement = ConstantExpr::getCast(To, getType());
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000457 } else if (getNumOperands() == 2) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000458 Constant *C1 = getOperand(0);
459 Constant *C2 = getOperand(1);
460 if (C1 == From) C1 = To;
461 if (C2 == From) C2 = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000462 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
463 } else {
464 assert(0 && "Unknown ConstantExpr type!");
465 return;
466 }
467
468 assert(Replacement != this && "I didn't contain From!");
469
470 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000471 if (DisableChecking)
472 uncheckedReplaceAllUsesWith(Replacement);
473 else
474 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000475
476 // Delete the old constant!
477 destroyConstant();
478}
479
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000480//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000481// Factory Function Implementation
482
Chris Lattner98fa07b2003-05-23 20:03:32 +0000483// ConstantCreator - A class that is used to create constants by
484// ValueMap*. This class should be partially specialized if there is
485// something strange that needs to be done to interface to the ctor for the
486// constant.
487//
488template<class ConstantClass, class TypeClass, class ValType>
489struct ConstantCreator {
490 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
491 return new ConstantClass(Ty, V);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000492 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000493};
494
Chris Lattner98fa07b2003-05-23 20:03:32 +0000495namespace {
496 template<class ValType, class TypeClass, class ConstantClass>
497 class ValueMap {
498 protected:
499 typedef std::pair<const TypeClass*, ValType> ConstHashKey;
500 std::map<ConstHashKey, ConstantClass *> Map;
501 public:
502 // getOrCreate - Return the specified constant from the map, creating it if
503 // necessary.
504 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
505 ConstHashKey Lookup(Ty, V);
506 typename std::map<ConstHashKey,ConstantClass *>::iterator I =
507 Map.lower_bound(Lookup);
508 if (I != Map.end() && I->first == Lookup)
509 return I->second; // Is it in the map?
510
511 // If no preexisting value, create one now...
512 ConstantClass *Result =
513 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
514
515 Map.insert(I, std::make_pair(ConstHashKey(Ty, V), Result));
516 return Result;
517 }
518
519 void remove(ConstantClass *CP) {
520 // FIXME: This could be sped up a LOT. If this gets to be a performance
521 // problem, someone should look at this.
522 for (typename std::map<ConstHashKey, ConstantClass*>::iterator
523 I = Map.begin(), E = Map.end(); I != E; ++I)
524 if (I->second == CP) {
525 Map.erase(I);
526 return;
527 }
528 assert(0 && "Constant not found in constant table!");
529 }
530 };
531}
532
533
534
Chris Lattner3462ae32001-12-03 22:26:30 +0000535//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000536//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000537static ValueMap< int64_t, Type, ConstantSInt> SIntConstants;
538static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000539
Chris Lattner3462ae32001-12-03 22:26:30 +0000540ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000541 return SIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000542}
543
Chris Lattner3462ae32001-12-03 22:26:30 +0000544ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000545 return UIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000546}
547
Chris Lattner3462ae32001-12-03 22:26:30 +0000548ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000549 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000550 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
551 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000552}
553
Chris Lattner3462ae32001-12-03 22:26:30 +0000554//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000555//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000556static ValueMap<double, Type, ConstantFP> FPConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000557
Chris Lattner3462ae32001-12-03 22:26:30 +0000558ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000559 return FPConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000560}
561
Chris Lattner3462ae32001-12-03 22:26:30 +0000562//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000563//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000564static ValueMap<std::vector<Constant*>, ArrayType,
565 ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000566
Chris Lattner3462ae32001-12-03 22:26:30 +0000567ConstantArray *ConstantArray::get(const ArrayType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000568 const std::vector<Constant*> &V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000569 return ArrayConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000570}
571
Chris Lattner98fa07b2003-05-23 20:03:32 +0000572// destroyConstant - Remove the constant from the constant table...
573//
574void ConstantArray::destroyConstant() {
575 ArrayConstants.remove(this);
576 destroyConstantImpl();
577}
578
Chris Lattner29dc65a2003-10-03 18:39:57 +0000579#if 0
Chris Lattner98fa07b2003-05-23 20:03:32 +0000580/// refineAbstractType - If this callback is invoked, then this constant is of a
581/// derived type, change all users to use a concrete constant of the new type.
582///
583void ConstantArray::refineAbstractType(const DerivedType *OldTy,
584 const Type *NewTy) {
Chris Lattner7fa67832003-06-02 17:25:46 +0000585 if (OldTy == NewTy) return;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000586
587 // Make everyone now use a constant of the new type...
588 std::vector<Constant*> C;
589 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
590 C.push_back(cast<Constant>(getOperand(i)));
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000591 Constant *New = ConstantArray::get(cast<ArrayType>(NewTy), C);
592 if (New != this) {
Chris Lattner9f158122003-08-29 05:09:37 +0000593 uncheckedReplaceAllUsesWith(New);
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000594 destroyConstant(); // This constant is now dead, destroy it.
595 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000596}
Chris Lattner29dc65a2003-10-03 18:39:57 +0000597#endif
Chris Lattner98fa07b2003-05-23 20:03:32 +0000598
Chris Lattner3462ae32001-12-03 22:26:30 +0000599// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000600// contain the specified string. A null terminator is added to the specified
601// string so that it may be used in a natural way...
602//
Chris Lattner7f74a562002-01-20 22:54:45 +0000603ConstantArray *ConstantArray::get(const std::string &Str) {
604 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000605
606 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000607 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000608
609 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000610 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000611
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000612 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000613 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000614}
615
Chris Lattner81fabb02002-08-26 17:53:56 +0000616// getAsString - If the sub-element type of this array is either sbyte or ubyte,
617// then this method converts the array to an std::string and returns it.
618// Otherwise, it asserts out.
619//
620std::string ConstantArray::getAsString() const {
Chris Lattner6077c312003-07-23 15:22:26 +0000621 assert((getType()->getElementType() == Type::UByteTy ||
622 getType()->getElementType() == Type::SByteTy) && "Not a string!");
623
Chris Lattner81fabb02002-08-26 17:53:56 +0000624 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +0000625 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
626 Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue();
Chris Lattner81fabb02002-08-26 17:53:56 +0000627 return Result;
628}
629
630
Chris Lattner3462ae32001-12-03 22:26:30 +0000631//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000632//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000633static ValueMap<std::vector<Constant*>, StructType,
634 ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000635
Chris Lattner3462ae32001-12-03 22:26:30 +0000636ConstantStruct *ConstantStruct::get(const StructType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000637 const std::vector<Constant*> &V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000638 return StructConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000639}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000640
Chris Lattnerd7a73302001-10-13 06:57:33 +0000641// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000642//
Chris Lattner3462ae32001-12-03 22:26:30 +0000643void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000644 StructConstants.remove(this);
645 destroyConstantImpl();
646}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000647
Chris Lattner29dc65a2003-10-03 18:39:57 +0000648#if 0
Chris Lattner98fa07b2003-05-23 20:03:32 +0000649/// refineAbstractType - If this callback is invoked, then this constant is of a
650/// derived type, change all users to use a concrete constant of the new type.
651///
652void ConstantStruct::refineAbstractType(const DerivedType *OldTy,
653 const Type *NewTy) {
Chris Lattner7fa67832003-06-02 17:25:46 +0000654 if (OldTy == NewTy) return;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000655
656 // Make everyone now use a constant of the new type...
657 std::vector<Constant*> C;
658 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
659 C.push_back(cast<Constant>(getOperand(i)));
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000660 Constant *New = ConstantStruct::get(cast<StructType>(NewTy), C);
661 if (New != this) {
Chris Lattner9f158122003-08-29 05:09:37 +0000662 uncheckedReplaceAllUsesWith(New);
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000663 destroyConstant(); // This constant is now dead, destroy it.
664 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000665}
Chris Lattner29dc65a2003-10-03 18:39:57 +0000666#endif
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000667
Chris Lattner3462ae32001-12-03 22:26:30 +0000668//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000669//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000670
671// ConstantPointerNull does not take extra "value" argument...
672template<class ValType>
673struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
674 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
675 return new ConstantPointerNull(Ty);
676 }
677};
678
679static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000680
Chris Lattner3462ae32001-12-03 22:26:30 +0000681ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000682 return NullPtrConstants.getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000683}
684
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000685// destroyConstant - Remove the constant from the constant table...
686//
687void ConstantPointerNull::destroyConstant() {
688 NullPtrConstants.remove(this);
689 destroyConstantImpl();
690}
691
Chris Lattner29dc65a2003-10-03 18:39:57 +0000692#if 0
Chris Lattner98fa07b2003-05-23 20:03:32 +0000693/// refineAbstractType - If this callback is invoked, then this constant is of a
694/// derived type, change all users to use a concrete constant of the new type.
695///
696void ConstantPointerNull::refineAbstractType(const DerivedType *OldTy,
697 const Type *NewTy) {
Chris Lattner7fa67832003-06-02 17:25:46 +0000698 if (OldTy == NewTy) return;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000699
700 // Make everyone now use a constant of the new type...
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000701 Constant *New = ConstantPointerNull::get(cast<PointerType>(NewTy));
702 if (New != this) {
Chris Lattner9f158122003-08-29 05:09:37 +0000703 uncheckedReplaceAllUsesWith(New);
Chris Lattner20ec7bc2003-05-25 16:15:32 +0000704
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000705 // This constant is now dead, destroy it.
706 destroyConstant();
707 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000708}
Chris Lattner29dc65a2003-10-03 18:39:57 +0000709#endif
Chris Lattner98fa07b2003-05-23 20:03:32 +0000710
711
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000712
Chris Lattner3462ae32001-12-03 22:26:30 +0000713//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +0000714//
Chris Lattner3462ae32001-12-03 22:26:30 +0000715ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000716 assert(GV->getParent() && "Global Value must be attached to a module!");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000717
Chris Lattnerd7a73302001-10-13 06:57:33 +0000718 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +0000719 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000720}
721
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000722// destroyConstant - Remove the constant from the constant table...
723//
724void ConstantPointerRef::destroyConstant() {
725 getValue()->getParent()->destroyConstantPointerRef(this);
726 destroyConstantImpl();
727}
728
729
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000730//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000731//
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000732typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000733
734template<>
735struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
736 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
737 if (V.first == Instruction::Cast)
738 return new ConstantExpr(Instruction::Cast, V.second[0], Ty);
739 if ((V.first >= Instruction::BinaryOpsBegin &&
740 V.first < Instruction::BinaryOpsEnd) ||
741 V.first == Instruction::Shl || V.first == Instruction::Shr)
742 return new ConstantExpr(V.first, V.second[0], V.second[1]);
743
744 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
745
746 // Check that the indices list is valid...
747 std::vector<Value*> ValIdxList(V.second.begin()+1, V.second.end());
748 const Type *DestTy = GetElementPtrInst::getIndexedType(Ty, ValIdxList,
749 true);
750 assert(DestTy && "Invalid index list for GetElementPtr expression");
751
752 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
753 return new ConstantExpr(V.second[0], IdxList, PointerType::get(DestTy));
754 }
755};
756
757static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000758
Chris Lattner46b3d302003-04-16 22:40:51 +0000759Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattneracdbe712003-04-17 19:24:48 +0000760 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
761 return FC; // Fold a few common cases...
762
Vikram S. Adve4c485332002-07-15 18:19:33 +0000763 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000764 std::vector<Constant*> argVec(1, C);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000765 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
766 return ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000767}
Chris Lattnerd7a73302001-10-13 06:57:33 +0000768
Chris Lattner46b3d302003-04-16 22:40:51 +0000769Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +0000770 // Check the operands for consistency first
771 assert((Opcode >= Instruction::BinaryOpsBegin &&
772 Opcode < Instruction::BinaryOpsEnd) &&
773 "Invalid opcode in binary constant expression");
774 assert(C1->getType() == C2->getType() &&
775 "Operand types in binary constant expression should match");
Chris Lattneracdbe712003-04-17 19:24:48 +0000776
777 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
778 return FC; // Fold a few common cases...
779
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000780 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000781 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
782 return ExprConstants.getOrCreate(C1->getType(), Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000783}
784
Chris Lattner38a9bcd2003-05-21 17:49:25 +0000785/// getShift - Return a shift left or shift right constant expr
786Constant *ConstantExpr::getShift(unsigned Opcode, Constant *C1, Constant *C2) {
787 // Check the operands for consistency first
788 assert((Opcode == Instruction::Shl ||
789 Opcode == Instruction::Shr) &&
790 "Invalid opcode in binary constant expression");
791 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
792 "Invalid operand types for Shift constant expr!");
793
794 if (Constant *FC = ConstantFoldShiftInstruction(Opcode, C1, C2))
795 return FC; // Fold a few common cases...
796
797 // Look up the constant in the table first to ensure uniqueness
798 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000799 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
800 return ExprConstants.getOrCreate(C1->getType(), Key);
Chris Lattner38a9bcd2003-05-21 17:49:25 +0000801}
802
803
Chris Lattner46b3d302003-04-16 22:40:51 +0000804Constant *ConstantExpr::getGetElementPtr(Constant *C,
805 const std::vector<Constant*> &IdxList){
Chris Lattneracdbe712003-04-17 19:24:48 +0000806 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
807 return FC; // Fold a few common cases...
Chris Lattner3cd8c562002-07-30 18:54:25 +0000808 const Type *Ty = C->getType();
Chris Lattner98fa07b2003-05-23 20:03:32 +0000809 assert(isa<PointerType>(Ty) &&
810 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +0000811
812 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000813 std::vector<Constant*> argVec(1, C);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000814 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
Vikram S. Adve4c485332002-07-15 18:19:33 +0000815
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000816 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,argVec);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000817 return ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +0000818}
819
820// destroyConstant - Remove the constant from the constant table...
821//
822void ConstantExpr::destroyConstant() {
823 ExprConstants.remove(this);
824 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000825}
826
Chris Lattner29dc65a2003-10-03 18:39:57 +0000827#if 0
Chris Lattner98fa07b2003-05-23 20:03:32 +0000828/// refineAbstractType - If this callback is invoked, then this constant is of a
829/// derived type, change all users to use a concrete constant of the new type.
830///
831void ConstantExpr::refineAbstractType(const DerivedType *OldTy,
832 const Type *NewTy) {
Chris Lattner7fa67832003-06-02 17:25:46 +0000833 if (OldTy == NewTy) return;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000834
835 // FIXME: These need to use a lower-level implementation method, because the
836 // ::get methods intuit the type of the result based on the types of the
837 // operands. The operand types may not have had their types resolved yet.
838 //
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000839 Constant *New;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000840 if (getOpcode() == Instruction::Cast) {
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000841 New = getCast(getOperand(0), NewTy);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000842 } else if (getOpcode() >= Instruction::BinaryOpsBegin &&
843 getOpcode() < Instruction::BinaryOpsEnd) {
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000844 New = get(getOpcode(), getOperand(0), getOperand(0));
Chris Lattner98fa07b2003-05-23 20:03:32 +0000845 } else if (getOpcode() == Instruction::Shl || getOpcode() ==Instruction::Shr){
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000846 New = getShift(getOpcode(), getOperand(0), getOperand(0));
Chris Lattner98fa07b2003-05-23 20:03:32 +0000847 } else {
848 assert(getOpcode() == Instruction::GetElementPtr);
849
850 // Make everyone now use a constant of the new type...
851 std::vector<Constant*> C;
852 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
853 C.push_back(cast<Constant>(getOperand(i)));
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000854 New = ConstantExpr::getGetElementPtr(getOperand(0), C);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000855 }
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000856 if (New != this) {
Chris Lattner9f158122003-08-29 05:09:37 +0000857 uncheckedReplaceAllUsesWith(New);
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000858 destroyConstant(); // This constant is now dead, destroy it.
859 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000860}
Chris Lattner29dc65a2003-10-03 18:39:57 +0000861#endif
Chris Lattner98fa07b2003-05-23 20:03:32 +0000862
863
864
865
Chris Lattner3cd8c562002-07-30 18:54:25 +0000866const char *ConstantExpr::getOpcodeName() const {
867 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000868}
869
Chris Lattner163b8902002-10-14 03:30:23 +0000870unsigned Constant::mutateReferences(Value *OldV, Value *NewV) {
871 // Uses of constant pointer refs are global values, not constants!
872 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
873 GlobalValue *NewGV = cast<GlobalValue>(NewV);
874 GlobalValue *OldGV = CPR->getValue();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000875
Chris Lattner163b8902002-10-14 03:30:23 +0000876 assert(OldGV == OldV && "Cannot mutate old value if I'm not using it!");
Chris Lattner163b8902002-10-14 03:30:23 +0000877 Operands[0] = NewGV;
Chris Lattnercb4d26f2003-05-15 19:37:21 +0000878 OldGV->getParent()->mutateConstantPointerRef(OldGV, NewGV);
Chris Lattner163b8902002-10-14 03:30:23 +0000879 return 1;
880 } else {
881 Constant *NewC = cast<Constant>(NewV);
882 unsigned NumReplaced = 0;
883 for (unsigned i = 0, N = getNumOperands(); i != N; ++i)
884 if (Operands[i] == OldV) {
885 ++NumReplaced;
886 Operands[i] = NewC;
887 }
888 return NumReplaced;
889 }
Chris Lattner25033252001-10-03 19:28:15 +0000890}