blob: 7bbbdb32023493b018e593113246ad4bfedf6baf [file] [log] [blame]
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001//===-- Constants.cpp - Implement Constant nodes --------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00002//
Chris Lattner3462ae32001-12-03 22:26:30 +00003// This file implements the Constant* classes...
Chris Lattner2f7c9632001-06-06 20:29:01 +00004//
5//===----------------------------------------------------------------------===//
6
Chris Lattnerca142372002-04-28 19:55:58 +00007#include "llvm/Constants.h"
Chris Lattneracdbe712003-04-17 19:24:48 +00008#include "llvm/ConstantHandling.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +00009#include "llvm/DerivedTypes.h"
Vikram S. Adve4e537b22002-07-14 23:13:17 +000010#include "llvm/iMemory.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000011#include "llvm/SymbolTable.h"
Chris Lattnerd7a73302001-10-13 06:57:33 +000012#include "llvm/Module.h"
Chris Lattner5de22042001-11-27 00:03:19 +000013#include "Support/StringExtras.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000014#include <algorithm>
Chris Lattner2f7c9632001-06-06 20:29:01 +000015
Chris Lattner3462ae32001-12-03 22:26:30 +000016ConstantBool *ConstantBool::True = new ConstantBool(true);
17ConstantBool *ConstantBool::False = new ConstantBool(false);
Chris Lattner2f7c9632001-06-06 20:29:01 +000018
Chris Lattner9655e542001-07-20 19:16:02 +000019
Chris Lattner2f7c9632001-06-06 20:29:01 +000020//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000021// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000022//===----------------------------------------------------------------------===//
23
24// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +000025void Constant::setName(const std::string &Name, SymbolTable *ST) {
Chris Lattner49d855c2001-09-07 16:46:31 +000026 assert(ST && "Type::setName - Must provide symbol table argument!");
27
28 if (Name.size()) ST->insert(Name, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000029}
30
Chris Lattner3462ae32001-12-03 22:26:30 +000031void Constant::destroyConstantImpl() {
32 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000033 // references to the constant by other constants in the constant pool. These
34 // constants are implicitly dependant on the module that is being deleted,
35 // but they don't know that. Because we only find out when the CPV is
36 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000037 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000038 //
39 while (!use_empty()) {
40 Value *V = use_back();
41#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000042 if (!isa<Constant>(V))
43 std::cerr << "While deleting: " << *this
44 << "\n\nUse still stuck around after Def is destroyed: "
45 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000046#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000047 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Chris Lattner3462ae32001-12-03 22:26:30 +000048 Constant *CPV = cast<Constant>(V);
Chris Lattnerd7a73302001-10-13 06:57:33 +000049 CPV->destroyConstant();
50
51 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000052 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000053 }
54
55 // Value has no outstanding references it is safe to delete it now...
56 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000057}
Chris Lattner2f7c9632001-06-06 20:29:01 +000058
Chris Lattnerb1585a92002-08-13 17:50:20 +000059// Static constructor to create a '0' constant of arbitrary type...
60Constant *Constant::getNullValue(const Type *Ty) {
61 switch (Ty->getPrimitiveID()) {
62 case Type::BoolTyID: return ConstantBool::get(false);
63 case Type::SByteTyID:
64 case Type::ShortTyID:
65 case Type::IntTyID:
66 case Type::LongTyID: return ConstantSInt::get(Ty, 0);
67
68 case Type::UByteTyID:
69 case Type::UShortTyID:
70 case Type::UIntTyID:
71 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
72
73 case Type::FloatTyID:
74 case Type::DoubleTyID: return ConstantFP::get(Ty, 0);
75
76 case Type::PointerTyID:
77 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattnerc33ae672003-03-06 21:02:18 +000078 case Type::StructTyID: {
79 const StructType *ST = cast<StructType>(Ty);
80
81 const StructType::ElementTypes &ETs = ST->getElementTypes();
82 std::vector<Constant*> Elements;
83 Elements.resize(ETs.size());
84 for (unsigned i = 0, e = ETs.size(); i != e; ++i)
85 Elements[i] = Constant::getNullValue(ETs[i]);
86 return ConstantStruct::get(ST, Elements);
87 }
88 case Type::ArrayTyID: {
89 const ArrayType *AT = cast<ArrayType>(Ty);
90 Constant *El = Constant::getNullValue(AT->getElementType());
91 unsigned NumElements = AT->getNumElements();
92 return ConstantArray::get(AT, std::vector<Constant*>(NumElements, El));
93 }
Chris Lattnerb1585a92002-08-13 17:50:20 +000094 default:
Chris Lattnerc33ae672003-03-06 21:02:18 +000095 // Function, Type, Label, or Opaque type?
96 assert(0 && "Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +000097 return 0;
98 }
99}
100
101// Static constructor to create the maximum constant of an integral type...
102ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
103 switch (Ty->getPrimitiveID()) {
104 case Type::BoolTyID: return ConstantBool::True;
105 case Type::SByteTyID:
106 case Type::ShortTyID:
107 case Type::IntTyID:
108 case Type::LongTyID: {
109 // Calculate 011111111111111...
110 unsigned TypeBits = Ty->getPrimitiveSize()*8;
111 int64_t Val = INT64_MAX; // All ones
112 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
113 return ConstantSInt::get(Ty, Val);
114 }
115
116 case Type::UByteTyID:
117 case Type::UShortTyID:
118 case Type::UIntTyID:
119 case Type::ULongTyID: return getAllOnesValue(Ty);
120
Chris Lattner31408f72002-08-14 17:12:13 +0000121 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000122 }
123}
124
125// Static constructor to create the minimum constant for an integral type...
126ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
127 switch (Ty->getPrimitiveID()) {
128 case Type::BoolTyID: return ConstantBool::False;
129 case Type::SByteTyID:
130 case Type::ShortTyID:
131 case Type::IntTyID:
132 case Type::LongTyID: {
133 // Calculate 1111111111000000000000
134 unsigned TypeBits = Ty->getPrimitiveSize()*8;
135 int64_t Val = -1; // All ones
136 Val <<= TypeBits-1; // Shift over to the right spot
137 return ConstantSInt::get(Ty, Val);
138 }
139
140 case Type::UByteTyID:
141 case Type::UShortTyID:
142 case Type::UIntTyID:
143 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
144
Chris Lattner31408f72002-08-14 17:12:13 +0000145 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000146 }
147}
148
149// Static constructor to create an integral constant with all bits set
150ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
151 switch (Ty->getPrimitiveID()) {
152 case Type::BoolTyID: return ConstantBool::True;
153 case Type::SByteTyID:
154 case Type::ShortTyID:
155 case Type::IntTyID:
156 case Type::LongTyID: return ConstantSInt::get(Ty, -1);
157
158 case Type::UByteTyID:
159 case Type::UShortTyID:
160 case Type::UIntTyID:
161 case Type::ULongTyID: {
162 // Calculate ~0 of the right type...
163 unsigned TypeBits = Ty->getPrimitiveSize()*8;
164 uint64_t Val = ~0ULL; // All ones
165 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
166 return ConstantUInt::get(Ty, Val);
167 }
Chris Lattner31408f72002-08-14 17:12:13 +0000168 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000169 }
170}
171
Chris Lattner83e5d392003-03-10 22:39:02 +0000172bool ConstantUInt::isAllOnesValue() const {
173 unsigned TypeBits = getType()->getPrimitiveSize()*8;
174 uint64_t Val = ~0ULL; // All ones
175 Val >>= 64-TypeBits; // Shift out inappropriate bits
176 return getValue() == Val;
177}
178
Chris Lattnerb1585a92002-08-13 17:50:20 +0000179
Chris Lattner2f7c9632001-06-06 20:29:01 +0000180//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +0000181// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +0000182//===----------------------------------------------------------------------===//
183
184//===----------------------------------------------------------------------===//
185// Normal Constructors
186
Chris Lattnerb1585a92002-08-13 17:50:20 +0000187ConstantBool::ConstantBool(bool V) : ConstantIntegral(Type::BoolTy) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000188 Val = V;
189}
Chris Lattner49d855c2001-09-07 16:46:31 +0000190
Chris Lattnerb1585a92002-08-13 17:50:20 +0000191ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : ConstantIntegral(Ty) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000192 Val.Unsigned = V;
Chris Lattner7309d662001-07-21 19:16:08 +0000193}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000194
Chris Lattner3462ae32001-12-03 22:26:30 +0000195ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000196 assert(Ty->isInteger() && Ty->isSigned() &&
197 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000198 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000199}
200
Chris Lattner3462ae32001-12-03 22:26:30 +0000201ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000202 assert(Ty->isInteger() && Ty->isUnsigned() &&
203 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000204 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000205}
206
Chris Lattner3462ae32001-12-03 22:26:30 +0000207ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
Chris Lattner9655e542001-07-20 19:16:02 +0000208 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000209 Val = V;
210}
211
Chris Lattner3462ae32001-12-03 22:26:30 +0000212ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000213 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner0d779712002-10-08 23:33:52 +0000214 Operands.reserve(V.size());
215 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000216 assert(V[i]->getType() == T->getElementType());
Chris Lattnera073acb2001-07-07 08:36:50 +0000217 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000218 }
219}
220
Chris Lattner3462ae32001-12-03 22:26:30 +0000221ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000222 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000223 const StructType::ElementTypes &ETypes = T->getElementTypes();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000224 assert(V.size() == ETypes.size() &&
225 "Invalid initializer vector for constant structure");
Chris Lattner0d779712002-10-08 23:33:52 +0000226 Operands.reserve(V.size());
227 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000228 assert((V[i]->getType() == ETypes[i] ||
229 (ETypes[i]->isAbstract() &&
230 ETypes[i]->getPrimitiveID()==V[i]->getType()->getPrimitiveID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000231 "Initializer for struct element doesn't match struct element type!");
Chris Lattnera073acb2001-07-07 08:36:50 +0000232 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000233 }
234}
235
Chris Lattner3462ae32001-12-03 22:26:30 +0000236ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
237 : ConstantPointer(GV->getType()) {
Chris Lattner60e0dd72001-10-03 06:12:09 +0000238 Operands.push_back(Use(GV, this));
239}
240
Chris Lattner3cd8c562002-07-30 18:54:25 +0000241ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
242 : Constant(Ty), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000243 Operands.push_back(Use(C, this));
244}
245
Chris Lattner22ced562003-06-22 20:48:30 +0000246static bool isSetCC(unsigned Opcode) {
247 return Opcode == Instruction::SetEQ || Opcode == Instruction::SetNE ||
248 Opcode == Instruction::SetLT || Opcode == Instruction::SetGT ||
249 Opcode == Instruction::SetLE || Opcode == Instruction::SetGE;
250}
251
Chris Lattner3cd8c562002-07-30 18:54:25 +0000252ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Chris Lattner22ced562003-06-22 20:48:30 +0000253 : Constant(isSetCC(Opcode) ? Type::BoolTy : C1->getType()), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000254 Operands.push_back(Use(C1, this));
255 Operands.push_back(Use(C2, this));
256}
257
Chris Lattner3cd8c562002-07-30 18:54:25 +0000258ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
259 const Type *DestTy)
260 : Constant(DestTy), iType(Instruction::GetElementPtr) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000261 Operands.reserve(1+IdxList.size());
262 Operands.push_back(Use(C, this));
263 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
264 Operands.push_back(Use(IdxList[i], this));
265}
266
Chris Lattner60e0dd72001-10-03 06:12:09 +0000267
Chris Lattner2f7c9632001-06-06 20:29:01 +0000268
269//===----------------------------------------------------------------------===//
Chris Lattnerd7a73302001-10-13 06:57:33 +0000270// classof implementations
271
Chris Lattnerb1585a92002-08-13 17:50:20 +0000272bool ConstantIntegral::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000273 return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
Chris Lattner41e99a02002-08-12 21:21:21 +0000274}
275
Chris Lattner3462ae32001-12-03 22:26:30 +0000276bool ConstantInt::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000277 return CPV->getType()->isInteger() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000278}
Chris Lattner3462ae32001-12-03 22:26:30 +0000279bool ConstantSInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000280 return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000281}
Chris Lattner3462ae32001-12-03 22:26:30 +0000282bool ConstantUInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000283 return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000284}
Chris Lattner3462ae32001-12-03 22:26:30 +0000285bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000286 const Type *Ty = CPV->getType();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000287 return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000288 !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000289}
Chris Lattner3462ae32001-12-03 22:26:30 +0000290bool ConstantArray::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000291 return isa<ArrayType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000292}
Chris Lattner3462ae32001-12-03 22:26:30 +0000293bool ConstantStruct::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000294 return isa<StructType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000295}
Chris Lattner3462ae32001-12-03 22:26:30 +0000296bool ConstantPointer::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000297 return (isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000298}
299
300
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000301
Chris Lattner2f7c9632001-06-06 20:29:01 +0000302//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000303// isValueValidForType implementations
304
Chris Lattner3462ae32001-12-03 22:26:30 +0000305bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000306 switch (Ty->getPrimitiveID()) {
307 default:
308 return false; // These can't be represented as integers!!!
309
310 // Signed types...
311 case Type::SByteTyID:
312 return (Val <= INT8_MAX && Val >= INT8_MIN);
313 case Type::ShortTyID:
314 return (Val <= INT16_MAX && Val >= INT16_MIN);
315 case Type::IntTyID:
316 return (Val <= INT32_MAX && Val >= INT32_MIN);
317 case Type::LongTyID:
318 return true; // This is the largest type...
319 }
320 assert(0 && "WTF?");
321 return false;
322}
323
Chris Lattner3462ae32001-12-03 22:26:30 +0000324bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000325 switch (Ty->getPrimitiveID()) {
326 default:
327 return false; // These can't be represented as integers!!!
328
329 // Unsigned types...
330 case Type::UByteTyID:
331 return (Val <= UINT8_MAX);
332 case Type::UShortTyID:
333 return (Val <= UINT16_MAX);
334 case Type::UIntTyID:
335 return (Val <= UINT32_MAX);
336 case Type::ULongTyID:
337 return true; // This is the largest type...
338 }
339 assert(0 && "WTF?");
340 return false;
341}
342
Chris Lattner3462ae32001-12-03 22:26:30 +0000343bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000344 switch (Ty->getPrimitiveID()) {
345 default:
346 return false; // These can't be represented as floating point!
347
348 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000349 case Type::FloatTyID:
Chris Lattnerd06dd692001-07-15 00:18:39 +0000350 /*
Chris Lattner2f7c9632001-06-06 20:29:01 +0000351 return (Val <= UINT8_MAX);
352 */
353 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
361void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To) {
362 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
363
364 std::vector<Constant*> Values;
365 Values.reserve(getValues().size()); // Build replacement array...
366 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
367 Constant *Val = cast<Constant>(getValues()[i]);
368 if (Val == From) Val = cast<Constant>(To);
369 Values.push_back(Val);
370 }
371
372 ConstantArray *Replacement = ConstantArray::get(getType(), Values);
373 assert(Replacement != this && "I didn't contain From!");
374
375 // Everyone using this now uses the replacement...
376 replaceAllUsesWith(Replacement);
377
378 // Delete the old constant!
379 destroyConstant();
380}
381
382void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To) {
383 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
384
385 std::vector<Constant*> Values;
386 Values.reserve(getValues().size());
387 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
388 Constant *Val = cast<Constant>(getValues()[i]);
389 if (Val == From) Val = cast<Constant>(To);
390 Values.push_back(Val);
391 }
392
393 ConstantStruct *Replacement = ConstantStruct::get(getType(), Values);
394 assert(Replacement != this && "I didn't contain From!");
395
396 // Everyone using this now uses the replacement...
397 replaceAllUsesWith(Replacement);
398
399 // Delete the old constant!
400 destroyConstant();
401}
402
403void ConstantPointerRef::replaceUsesOfWithOnConstant(Value *From, Value *To) {
404 if (isa<GlobalValue>(To)) {
405 assert(From == getOperand(0) && "Doesn't contain from!");
406 ConstantPointerRef *Replacement =
407 ConstantPointerRef::get(cast<GlobalValue>(To));
408
409 // Everyone using this now uses the replacement...
410 replaceAllUsesWith(Replacement);
411
412 // Delete the old constant!
413 destroyConstant();
414 } else {
415 // Just replace ourselves with the To value specified.
416 replaceAllUsesWith(To);
417
418 // Delete the old constant!
419 destroyConstant();
420 }
421}
422
Chris Lattner55ed6562003-05-14 17:51:05 +0000423void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV) {
424 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
425 Constant *To = cast<Constant>(ToV);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000426
Chris Lattner46b3d302003-04-16 22:40:51 +0000427 Constant *Replacement = 0;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000428 if (getOpcode() == Instruction::GetElementPtr) {
429 std::vector<Constant*> Indices;
Chris Lattner55ed6562003-05-14 17:51:05 +0000430 Constant *Pointer = getOperand(0);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000431 Indices.reserve(getNumOperands()-1);
Chris Lattner55ed6562003-05-14 17:51:05 +0000432 if (Pointer == From) Pointer = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000433
434 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000435 Constant *Val = getOperand(i);
436 if (Val == From) Val = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000437 Indices.push_back(Val);
438 }
439 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
440 } else if (getOpcode() == Instruction::Cast) {
441 assert(getOperand(0) == From && "Cast only has one use!");
Chris Lattner55ed6562003-05-14 17:51:05 +0000442 Replacement = ConstantExpr::getCast(To, getType());
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000443 } else if (getNumOperands() == 2) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000444 Constant *C1 = getOperand(0);
445 Constant *C2 = getOperand(1);
446 if (C1 == From) C1 = To;
447 if (C2 == From) C2 = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000448 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
449 } else {
450 assert(0 && "Unknown ConstantExpr type!");
451 return;
452 }
453
454 assert(Replacement != this && "I didn't contain From!");
455
456 // Everyone using this now uses the replacement...
457 replaceAllUsesWith(Replacement);
458
459 // Delete the old constant!
460 destroyConstant();
461}
462
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000463//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000464// Factory Function Implementation
465
Chris Lattner98fa07b2003-05-23 20:03:32 +0000466// ConstantCreator - A class that is used to create constants by
467// ValueMap*. This class should be partially specialized if there is
468// something strange that needs to be done to interface to the ctor for the
469// constant.
470//
471template<class ConstantClass, class TypeClass, class ValType>
472struct ConstantCreator {
473 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
474 return new ConstantClass(Ty, V);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000475 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000476};
477
Chris Lattner98fa07b2003-05-23 20:03:32 +0000478namespace {
479 template<class ValType, class TypeClass, class ConstantClass>
480 class ValueMap {
481 protected:
482 typedef std::pair<const TypeClass*, ValType> ConstHashKey;
483 std::map<ConstHashKey, ConstantClass *> Map;
484 public:
485 // getOrCreate - Return the specified constant from the map, creating it if
486 // necessary.
487 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
488 ConstHashKey Lookup(Ty, V);
489 typename std::map<ConstHashKey,ConstantClass *>::iterator I =
490 Map.lower_bound(Lookup);
491 if (I != Map.end() && I->first == Lookup)
492 return I->second; // Is it in the map?
493
494 // If no preexisting value, create one now...
495 ConstantClass *Result =
496 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
497
498 Map.insert(I, std::make_pair(ConstHashKey(Ty, V), Result));
499 return Result;
500 }
501
502 void remove(ConstantClass *CP) {
503 // FIXME: This could be sped up a LOT. If this gets to be a performance
504 // problem, someone should look at this.
505 for (typename std::map<ConstHashKey, ConstantClass*>::iterator
506 I = Map.begin(), E = Map.end(); I != E; ++I)
507 if (I->second == CP) {
508 Map.erase(I);
509 return;
510 }
511 assert(0 && "Constant not found in constant table!");
512 }
513 };
514}
515
516
517
Chris Lattner3462ae32001-12-03 22:26:30 +0000518//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000519//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000520static ValueMap< int64_t, Type, ConstantSInt> SIntConstants;
521static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000522
Chris Lattner3462ae32001-12-03 22:26:30 +0000523ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000524 return SIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000525}
526
Chris Lattner3462ae32001-12-03 22:26:30 +0000527ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000528 return UIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000529}
530
Chris Lattner3462ae32001-12-03 22:26:30 +0000531ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000532 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000533 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
534 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000535}
536
Chris Lattner3462ae32001-12-03 22:26:30 +0000537//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000538//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000539static ValueMap<double, Type, ConstantFP> FPConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000540
Chris Lattner3462ae32001-12-03 22:26:30 +0000541ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000542 return FPConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000543}
544
Chris Lattner3462ae32001-12-03 22:26:30 +0000545//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000546//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000547static ValueMap<std::vector<Constant*>, ArrayType,
548 ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000549
Chris Lattner3462ae32001-12-03 22:26:30 +0000550ConstantArray *ConstantArray::get(const ArrayType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000551 const std::vector<Constant*> &V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000552 return ArrayConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000553}
554
Chris Lattner98fa07b2003-05-23 20:03:32 +0000555// destroyConstant - Remove the constant from the constant table...
556//
557void ConstantArray::destroyConstant() {
558 ArrayConstants.remove(this);
559 destroyConstantImpl();
560}
561
562/// refineAbstractType - If this callback is invoked, then this constant is of a
563/// derived type, change all users to use a concrete constant of the new type.
564///
565void ConstantArray::refineAbstractType(const DerivedType *OldTy,
566 const Type *NewTy) {
567 Value::refineAbstractType(OldTy, NewTy);
Chris Lattner7fa67832003-06-02 17:25:46 +0000568 if (OldTy == NewTy) return;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000569
570 // Make everyone now use a constant of the new type...
571 std::vector<Constant*> C;
572 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
573 C.push_back(cast<Constant>(getOperand(i)));
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000574 Constant *New = ConstantArray::get(cast<ArrayType>(NewTy), C);
575 if (New != this) {
576 replaceAllUsesWith(New);
577 destroyConstant(); // This constant is now dead, destroy it.
578 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000579}
580
581
Chris Lattner3462ae32001-12-03 22:26:30 +0000582// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000583// contain the specified string. A null terminator is added to the specified
584// string so that it may be used in a natural way...
585//
Chris Lattner7f74a562002-01-20 22:54:45 +0000586ConstantArray *ConstantArray::get(const std::string &Str) {
587 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000588
589 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000590 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000591
592 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000593 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000594
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000595 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000596 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000597}
598
Chris Lattner81fabb02002-08-26 17:53:56 +0000599// getAsString - If the sub-element type of this array is either sbyte or ubyte,
600// then this method converts the array to an std::string and returns it.
601// Otherwise, it asserts out.
602//
603std::string ConstantArray::getAsString() const {
Chris Lattner6077c312003-07-23 15:22:26 +0000604 assert((getType()->getElementType() == Type::UByteTy ||
605 getType()->getElementType() == Type::SByteTy) && "Not a string!");
606
Chris Lattner81fabb02002-08-26 17:53:56 +0000607 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +0000608 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
609 Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue();
Chris Lattner81fabb02002-08-26 17:53:56 +0000610 return Result;
611}
612
613
Chris Lattner3462ae32001-12-03 22:26:30 +0000614//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000615//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000616static ValueMap<std::vector<Constant*>, StructType,
617 ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000618
Chris Lattner3462ae32001-12-03 22:26:30 +0000619ConstantStruct *ConstantStruct::get(const StructType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000620 const std::vector<Constant*> &V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000621 return StructConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000622}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000623
Chris Lattnerd7a73302001-10-13 06:57:33 +0000624// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000625//
Chris Lattner3462ae32001-12-03 22:26:30 +0000626void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000627 StructConstants.remove(this);
628 destroyConstantImpl();
629}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000630
Chris Lattner98fa07b2003-05-23 20:03:32 +0000631/// refineAbstractType - If this callback is invoked, then this constant is of a
632/// derived type, change all users to use a concrete constant of the new type.
633///
634void ConstantStruct::refineAbstractType(const DerivedType *OldTy,
635 const Type *NewTy) {
636 Value::refineAbstractType(OldTy, NewTy);
Chris Lattner7fa67832003-06-02 17:25:46 +0000637 if (OldTy == NewTy) return;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000638
639 // Make everyone now use a constant of the new type...
640 std::vector<Constant*> C;
641 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
642 C.push_back(cast<Constant>(getOperand(i)));
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000643 Constant *New = ConstantStruct::get(cast<StructType>(NewTy), C);
644 if (New != this) {
645 replaceAllUsesWith(New);
646 destroyConstant(); // This constant is now dead, destroy it.
647 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000648}
649
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000650
Chris Lattner3462ae32001-12-03 22:26:30 +0000651//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000652//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000653
654// ConstantPointerNull does not take extra "value" argument...
655template<class ValType>
656struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
657 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
658 return new ConstantPointerNull(Ty);
659 }
660};
661
662static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000663
Chris Lattner3462ae32001-12-03 22:26:30 +0000664ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000665 return NullPtrConstants.getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000666}
667
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000668// destroyConstant - Remove the constant from the constant table...
669//
670void ConstantPointerNull::destroyConstant() {
671 NullPtrConstants.remove(this);
672 destroyConstantImpl();
673}
674
Chris Lattner98fa07b2003-05-23 20:03:32 +0000675/// refineAbstractType - If this callback is invoked, then this constant is of a
676/// derived type, change all users to use a concrete constant of the new type.
677///
678void ConstantPointerNull::refineAbstractType(const DerivedType *OldTy,
679 const Type *NewTy) {
680 Value::refineAbstractType(OldTy, NewTy);
Chris Lattner7fa67832003-06-02 17:25:46 +0000681 if (OldTy == NewTy) return;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000682
683 // Make everyone now use a constant of the new type...
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000684 Constant *New = ConstantPointerNull::get(cast<PointerType>(NewTy));
685 if (New != this) {
686 replaceAllUsesWith(New);
Chris Lattner20ec7bc2003-05-25 16:15:32 +0000687
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000688 // This constant is now dead, destroy it.
689 destroyConstant();
690 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000691}
692
693
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000694
Chris Lattner3462ae32001-12-03 22:26:30 +0000695//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +0000696//
Chris Lattner3462ae32001-12-03 22:26:30 +0000697ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000698 assert(GV->getParent() && "Global Value must be attached to a module!");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000699
Chris Lattnerd7a73302001-10-13 06:57:33 +0000700 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +0000701 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000702}
703
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000704// destroyConstant - Remove the constant from the constant table...
705//
706void ConstantPointerRef::destroyConstant() {
707 getValue()->getParent()->destroyConstantPointerRef(this);
708 destroyConstantImpl();
709}
710
711
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000712//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000713//
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000714typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000715
716template<>
717struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
718 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
719 if (V.first == Instruction::Cast)
720 return new ConstantExpr(Instruction::Cast, V.second[0], Ty);
721 if ((V.first >= Instruction::BinaryOpsBegin &&
722 V.first < Instruction::BinaryOpsEnd) ||
723 V.first == Instruction::Shl || V.first == Instruction::Shr)
724 return new ConstantExpr(V.first, V.second[0], V.second[1]);
725
726 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
727
728 // Check that the indices list is valid...
729 std::vector<Value*> ValIdxList(V.second.begin()+1, V.second.end());
730 const Type *DestTy = GetElementPtrInst::getIndexedType(Ty, ValIdxList,
731 true);
732 assert(DestTy && "Invalid index list for GetElementPtr expression");
733
734 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
735 return new ConstantExpr(V.second[0], IdxList, PointerType::get(DestTy));
736 }
737};
738
739static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000740
Chris Lattner46b3d302003-04-16 22:40:51 +0000741Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattneracdbe712003-04-17 19:24:48 +0000742 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
743 return FC; // Fold a few common cases...
744
Vikram S. Adve4c485332002-07-15 18:19:33 +0000745 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000746 std::vector<Constant*> argVec(1, C);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000747 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
748 return ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000749}
Chris Lattnerd7a73302001-10-13 06:57:33 +0000750
Chris Lattner46b3d302003-04-16 22:40:51 +0000751Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +0000752 // Check the operands for consistency first
753 assert((Opcode >= Instruction::BinaryOpsBegin &&
754 Opcode < Instruction::BinaryOpsEnd) &&
755 "Invalid opcode in binary constant expression");
756 assert(C1->getType() == C2->getType() &&
757 "Operand types in binary constant expression should match");
Chris Lattneracdbe712003-04-17 19:24:48 +0000758
759 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
760 return FC; // Fold a few common cases...
761
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000762 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000763 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
764 return ExprConstants.getOrCreate(C1->getType(), Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000765}
766
Chris Lattner38a9bcd2003-05-21 17:49:25 +0000767/// getShift - Return a shift left or shift right constant expr
768Constant *ConstantExpr::getShift(unsigned Opcode, Constant *C1, Constant *C2) {
769 // Check the operands for consistency first
770 assert((Opcode == Instruction::Shl ||
771 Opcode == Instruction::Shr) &&
772 "Invalid opcode in binary constant expression");
773 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
774 "Invalid operand types for Shift constant expr!");
775
776 if (Constant *FC = ConstantFoldShiftInstruction(Opcode, C1, C2))
777 return FC; // Fold a few common cases...
778
779 // Look up the constant in the table first to ensure uniqueness
780 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);
Chris Lattner38a9bcd2003-05-21 17:49:25 +0000783}
784
785
Chris Lattner46b3d302003-04-16 22:40:51 +0000786Constant *ConstantExpr::getGetElementPtr(Constant *C,
787 const std::vector<Constant*> &IdxList){
Chris Lattneracdbe712003-04-17 19:24:48 +0000788 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
789 return FC; // Fold a few common cases...
Chris Lattner3cd8c562002-07-30 18:54:25 +0000790 const Type *Ty = C->getType();
Chris Lattner98fa07b2003-05-23 20:03:32 +0000791 assert(isa<PointerType>(Ty) &&
792 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +0000793
794 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000795 std::vector<Constant*> argVec(1, C);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000796 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
Vikram S. Adve4c485332002-07-15 18:19:33 +0000797
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000798 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,argVec);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000799 return ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +0000800}
801
802// destroyConstant - Remove the constant from the constant table...
803//
804void ConstantExpr::destroyConstant() {
805 ExprConstants.remove(this);
806 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000807}
808
Chris Lattner98fa07b2003-05-23 20:03:32 +0000809/// refineAbstractType - If this callback is invoked, then this constant is of a
810/// derived type, change all users to use a concrete constant of the new type.
811///
812void ConstantExpr::refineAbstractType(const DerivedType *OldTy,
813 const Type *NewTy) {
814 Value::refineAbstractType(OldTy, NewTy);
Chris Lattner7fa67832003-06-02 17:25:46 +0000815 if (OldTy == NewTy) return;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000816
817 // FIXME: These need to use a lower-level implementation method, because the
818 // ::get methods intuit the type of the result based on the types of the
819 // operands. The operand types may not have had their types resolved yet.
820 //
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000821 Constant *New;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000822 if (getOpcode() == Instruction::Cast) {
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000823 New = getCast(getOperand(0), NewTy);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000824 } else if (getOpcode() >= Instruction::BinaryOpsBegin &&
825 getOpcode() < Instruction::BinaryOpsEnd) {
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000826 New = get(getOpcode(), getOperand(0), getOperand(0));
Chris Lattner98fa07b2003-05-23 20:03:32 +0000827 } else if (getOpcode() == Instruction::Shl || getOpcode() ==Instruction::Shr){
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000828 New = getShift(getOpcode(), getOperand(0), getOperand(0));
Chris Lattner98fa07b2003-05-23 20:03:32 +0000829 } else {
830 assert(getOpcode() == Instruction::GetElementPtr);
831
832 // Make everyone now use a constant of the new type...
833 std::vector<Constant*> C;
834 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
835 C.push_back(cast<Constant>(getOperand(i)));
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000836 New = ConstantExpr::getGetElementPtr(getOperand(0), C);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000837 }
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000838 if (New != this) {
839 replaceAllUsesWith(New);
840 destroyConstant(); // This constant is now dead, destroy it.
841 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000842}
843
844
845
846
Chris Lattner3cd8c562002-07-30 18:54:25 +0000847const char *ConstantExpr::getOpcodeName() const {
848 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000849}
850
Chris Lattner163b8902002-10-14 03:30:23 +0000851unsigned Constant::mutateReferences(Value *OldV, Value *NewV) {
852 // Uses of constant pointer refs are global values, not constants!
853 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
854 GlobalValue *NewGV = cast<GlobalValue>(NewV);
855 GlobalValue *OldGV = CPR->getValue();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000856
Chris Lattner163b8902002-10-14 03:30:23 +0000857 assert(OldGV == OldV && "Cannot mutate old value if I'm not using it!");
Chris Lattner163b8902002-10-14 03:30:23 +0000858 Operands[0] = NewGV;
Chris Lattnercb4d26f2003-05-15 19:37:21 +0000859 OldGV->getParent()->mutateConstantPointerRef(OldGV, NewGV);
Chris Lattner163b8902002-10-14 03:30:23 +0000860 return 1;
861 } else {
862 Constant *NewC = cast<Constant>(NewV);
863 unsigned NumReplaced = 0;
864 for (unsigned i = 0, N = getNumOperands(); i != N; ++i)
865 if (Operands[i] == OldV) {
866 ++NumReplaced;
867 Operands[i] = NewC;
868 }
869 return NumReplaced;
870 }
Chris Lattner25033252001-10-03 19:28:15 +0000871}