blob: 8fb665ac0e1a522abbc117bbb0dc665fb7377140 [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()) {
Chris Lattner3e88ef92003-10-03 19:34:51 +000062 case Type::BoolTyID: {
63 static Constant *NullBool = ConstantBool::get(false);
64 return NullBool;
65 }
66 case Type::SByteTyID: {
67 static Constant *NullSByte = ConstantSInt::get(Type::SByteTy, 0);
68 return NullSByte;
69 }
70 case Type::UByteTyID: {
71 static Constant *NullUByte = ConstantUInt::get(Type::UByteTy, 0);
72 return NullUByte;
73 }
74 case Type::ShortTyID: {
75 static Constant *NullShort = ConstantSInt::get(Type::ShortTy, 0);
76 return NullShort;
77 }
78 case Type::UShortTyID: {
79 static Constant *NullUShort = ConstantUInt::get(Type::UShortTy, 0);
80 return NullUShort;
81 }
82 case Type::IntTyID: {
83 static Constant *NullInt = ConstantSInt::get(Type::IntTy, 0);
84 return NullInt;
85 }
86 case Type::UIntTyID: {
87 static Constant *NullUInt = ConstantUInt::get(Type::UIntTy, 0);
88 return NullUInt;
89 }
90 case Type::LongTyID: {
91 static Constant *NullLong = ConstantSInt::get(Type::LongTy, 0);
92 return NullLong;
93 }
94 case Type::ULongTyID: {
95 static Constant *NullULong = ConstantUInt::get(Type::ULongTy, 0);
96 return NullULong;
97 }
Chris Lattnerb1585a92002-08-13 17:50:20 +000098
Chris Lattner3e88ef92003-10-03 19:34:51 +000099 case Type::FloatTyID: {
100 static Constant *NullFloat = ConstantFP::get(Type::FloatTy, 0);
101 return NullFloat;
102 }
103 case Type::DoubleTyID: {
104 static Constant *NullDouble = ConstantFP::get(Type::DoubleTy, 0);
105 return NullDouble;
106 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000107
108 case Type::PointerTyID:
109 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner3e88ef92003-10-03 19:34:51 +0000110
Chris Lattnerc33ae672003-03-06 21:02:18 +0000111 case Type::StructTyID: {
112 const StructType *ST = cast<StructType>(Ty);
Chris Lattnerc33ae672003-03-06 21:02:18 +0000113 const StructType::ElementTypes &ETs = ST->getElementTypes();
114 std::vector<Constant*> Elements;
115 Elements.resize(ETs.size());
116 for (unsigned i = 0, e = ETs.size(); i != e; ++i)
117 Elements[i] = Constant::getNullValue(ETs[i]);
118 return ConstantStruct::get(ST, Elements);
119 }
120 case Type::ArrayTyID: {
121 const ArrayType *AT = cast<ArrayType>(Ty);
122 Constant *El = Constant::getNullValue(AT->getElementType());
123 unsigned NumElements = AT->getNumElements();
124 return ConstantArray::get(AT, std::vector<Constant*>(NumElements, El));
125 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000126 default:
Chris Lattnerc33ae672003-03-06 21:02:18 +0000127 // Function, Type, Label, or Opaque type?
128 assert(0 && "Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000129 return 0;
130 }
131}
132
133// Static constructor to create the maximum constant of an integral type...
134ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
135 switch (Ty->getPrimitiveID()) {
136 case Type::BoolTyID: return ConstantBool::True;
137 case Type::SByteTyID:
138 case Type::ShortTyID:
139 case Type::IntTyID:
140 case Type::LongTyID: {
141 // Calculate 011111111111111...
142 unsigned TypeBits = Ty->getPrimitiveSize()*8;
143 int64_t Val = INT64_MAX; // All ones
144 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
145 return ConstantSInt::get(Ty, Val);
146 }
147
148 case Type::UByteTyID:
149 case Type::UShortTyID:
150 case Type::UIntTyID:
151 case Type::ULongTyID: return getAllOnesValue(Ty);
152
Chris Lattner31408f72002-08-14 17:12:13 +0000153 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000154 }
155}
156
157// Static constructor to create the minimum constant for an integral type...
158ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
159 switch (Ty->getPrimitiveID()) {
160 case Type::BoolTyID: return ConstantBool::False;
161 case Type::SByteTyID:
162 case Type::ShortTyID:
163 case Type::IntTyID:
164 case Type::LongTyID: {
165 // Calculate 1111111111000000000000
166 unsigned TypeBits = Ty->getPrimitiveSize()*8;
167 int64_t Val = -1; // All ones
168 Val <<= TypeBits-1; // Shift over to the right spot
169 return ConstantSInt::get(Ty, Val);
170 }
171
172 case Type::UByteTyID:
173 case Type::UShortTyID:
174 case Type::UIntTyID:
175 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
176
Chris Lattner31408f72002-08-14 17:12:13 +0000177 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000178 }
179}
180
181// Static constructor to create an integral constant with all bits set
182ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
183 switch (Ty->getPrimitiveID()) {
184 case Type::BoolTyID: return ConstantBool::True;
185 case Type::SByteTyID:
186 case Type::ShortTyID:
187 case Type::IntTyID:
188 case Type::LongTyID: return ConstantSInt::get(Ty, -1);
189
190 case Type::UByteTyID:
191 case Type::UShortTyID:
192 case Type::UIntTyID:
193 case Type::ULongTyID: {
194 // Calculate ~0 of the right type...
195 unsigned TypeBits = Ty->getPrimitiveSize()*8;
196 uint64_t Val = ~0ULL; // All ones
197 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
198 return ConstantUInt::get(Ty, Val);
199 }
Chris Lattner31408f72002-08-14 17:12:13 +0000200 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000201 }
202}
203
Chris Lattner83e5d392003-03-10 22:39:02 +0000204bool ConstantUInt::isAllOnesValue() const {
205 unsigned TypeBits = getType()->getPrimitiveSize()*8;
206 uint64_t Val = ~0ULL; // All ones
207 Val >>= 64-TypeBits; // Shift out inappropriate bits
208 return getValue() == Val;
209}
210
Chris Lattnerb1585a92002-08-13 17:50:20 +0000211
Chris Lattner2f7c9632001-06-06 20:29:01 +0000212//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +0000213// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +0000214//===----------------------------------------------------------------------===//
215
216//===----------------------------------------------------------------------===//
217// Normal Constructors
218
Chris Lattnerb1585a92002-08-13 17:50:20 +0000219ConstantBool::ConstantBool(bool V) : ConstantIntegral(Type::BoolTy) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000220 Val = V;
221}
Chris Lattner49d855c2001-09-07 16:46:31 +0000222
Chris Lattnerb1585a92002-08-13 17:50:20 +0000223ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : ConstantIntegral(Ty) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000224 Val.Unsigned = V;
Chris Lattner7309d662001-07-21 19:16:08 +0000225}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000226
Chris Lattner3462ae32001-12-03 22:26:30 +0000227ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000228 assert(Ty->isInteger() && Ty->isSigned() &&
229 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000230 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000231}
232
Chris Lattner3462ae32001-12-03 22:26:30 +0000233ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000234 assert(Ty->isInteger() && Ty->isUnsigned() &&
235 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000236 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000237}
238
Chris Lattner3462ae32001-12-03 22:26:30 +0000239ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
Chris Lattner9655e542001-07-20 19:16:02 +0000240 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000241 Val = V;
242}
243
Chris Lattner3462ae32001-12-03 22:26:30 +0000244ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000245 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner0d779712002-10-08 23:33:52 +0000246 Operands.reserve(V.size());
247 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattnerab69ecc2003-08-18 16:54:48 +0000248 assert(V[i]->getType() == T->getElementType() ||
249 (T->isAbstract() &&
250 V[i]->getType()->getPrimitiveID() ==
251 T->getElementType()->getPrimitiveID()));
Chris Lattnera073acb2001-07-07 08:36:50 +0000252 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000253 }
254}
255
Chris Lattner3462ae32001-12-03 22:26:30 +0000256ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000257 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000258 const StructType::ElementTypes &ETypes = T->getElementTypes();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000259 assert(V.size() == ETypes.size() &&
260 "Invalid initializer vector for constant structure");
Chris Lattner0d779712002-10-08 23:33:52 +0000261 Operands.reserve(V.size());
262 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000263 assert((V[i]->getType() == ETypes[i] ||
264 (ETypes[i]->isAbstract() &&
265 ETypes[i]->getPrimitiveID()==V[i]->getType()->getPrimitiveID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000266 "Initializer for struct element doesn't match struct element type!");
Chris Lattnera073acb2001-07-07 08:36:50 +0000267 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000268 }
269}
270
Chris Lattner3462ae32001-12-03 22:26:30 +0000271ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
272 : ConstantPointer(GV->getType()) {
Chris Lattner60e0dd72001-10-03 06:12:09 +0000273 Operands.push_back(Use(GV, this));
274}
275
Chris Lattner3cd8c562002-07-30 18:54:25 +0000276ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
277 : Constant(Ty), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000278 Operands.push_back(Use(C, this));
279}
280
Chris Lattner22ced562003-06-22 20:48:30 +0000281static bool isSetCC(unsigned Opcode) {
282 return Opcode == Instruction::SetEQ || Opcode == Instruction::SetNE ||
283 Opcode == Instruction::SetLT || Opcode == Instruction::SetGT ||
284 Opcode == Instruction::SetLE || Opcode == Instruction::SetGE;
285}
286
Chris Lattner3cd8c562002-07-30 18:54:25 +0000287ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Chris Lattner22ced562003-06-22 20:48:30 +0000288 : Constant(isSetCC(Opcode) ? Type::BoolTy : C1->getType()), iType(Opcode) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000289 Operands.push_back(Use(C1, this));
290 Operands.push_back(Use(C2, this));
291}
292
Chris Lattner3cd8c562002-07-30 18:54:25 +0000293ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
294 const Type *DestTy)
295 : Constant(DestTy), iType(Instruction::GetElementPtr) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000296 Operands.reserve(1+IdxList.size());
297 Operands.push_back(Use(C, this));
298 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
299 Operands.push_back(Use(IdxList[i], this));
300}
301
Chris Lattner60e0dd72001-10-03 06:12:09 +0000302
Chris Lattner2f7c9632001-06-06 20:29:01 +0000303
304//===----------------------------------------------------------------------===//
Chris Lattnerd7a73302001-10-13 06:57:33 +0000305// classof implementations
306
Chris Lattnerb1585a92002-08-13 17:50:20 +0000307bool ConstantIntegral::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000308 return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
Chris Lattner41e99a02002-08-12 21:21:21 +0000309}
310
Chris Lattner3462ae32001-12-03 22:26:30 +0000311bool ConstantInt::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000312 return CPV->getType()->isInteger() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000313}
Chris Lattner3462ae32001-12-03 22:26:30 +0000314bool ConstantSInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000315 return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000316}
Chris Lattner3462ae32001-12-03 22:26:30 +0000317bool ConstantUInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000318 return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000319}
Chris Lattner3462ae32001-12-03 22:26:30 +0000320bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000321 const Type *Ty = CPV->getType();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000322 return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000323 !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000324}
Chris Lattner3462ae32001-12-03 22:26:30 +0000325bool ConstantArray::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000326 return isa<ArrayType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000327}
Chris Lattner3462ae32001-12-03 22:26:30 +0000328bool ConstantStruct::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000329 return isa<StructType>(CPV->getType()) && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000330}
Chris Lattner3462ae32001-12-03 22:26:30 +0000331bool ConstantPointer::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000332 return (isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000333}
334
335
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000336
Chris Lattner2f7c9632001-06-06 20:29:01 +0000337//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000338// isValueValidForType implementations
339
Chris Lattner3462ae32001-12-03 22:26:30 +0000340bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000341 switch (Ty->getPrimitiveID()) {
342 default:
343 return false; // These can't be represented as integers!!!
344
345 // Signed types...
346 case Type::SByteTyID:
347 return (Val <= INT8_MAX && Val >= INT8_MIN);
348 case Type::ShortTyID:
349 return (Val <= INT16_MAX && Val >= INT16_MIN);
350 case Type::IntTyID:
351 return (Val <= INT32_MAX && Val >= INT32_MIN);
352 case Type::LongTyID:
353 return true; // This is the largest type...
354 }
355 assert(0 && "WTF?");
356 return false;
357}
358
Chris Lattner3462ae32001-12-03 22:26:30 +0000359bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000360 switch (Ty->getPrimitiveID()) {
361 default:
362 return false; // These can't be represented as integers!!!
363
364 // Unsigned types...
365 case Type::UByteTyID:
366 return (Val <= UINT8_MAX);
367 case Type::UShortTyID:
368 return (Val <= UINT16_MAX);
369 case Type::UIntTyID:
370 return (Val <= UINT32_MAX);
371 case Type::ULongTyID:
372 return true; // This is the largest type...
373 }
374 assert(0 && "WTF?");
375 return false;
376}
377
Chris Lattner3462ae32001-12-03 22:26:30 +0000378bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000379 switch (Ty->getPrimitiveID()) {
380 default:
381 return false; // These can't be represented as floating point!
382
383 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000384 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000385 case Type::DoubleTyID:
386 return true; // This is the largest type...
387 }
388};
Chris Lattner9655e542001-07-20 19:16:02 +0000389
Chris Lattner49d855c2001-09-07 16:46:31 +0000390//===----------------------------------------------------------------------===//
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000391// replaceUsesOfWithOnConstant implementations
392
Chris Lattnerc27038d2003-08-29 05:36:46 +0000393void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
394 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000395 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
396
397 std::vector<Constant*> Values;
398 Values.reserve(getValues().size()); // Build replacement array...
399 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
400 Constant *Val = cast<Constant>(getValues()[i]);
401 if (Val == From) Val = cast<Constant>(To);
402 Values.push_back(Val);
403 }
404
405 ConstantArray *Replacement = ConstantArray::get(getType(), Values);
406 assert(Replacement != this && "I didn't contain From!");
407
408 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000409 if (DisableChecking)
410 uncheckedReplaceAllUsesWith(Replacement);
411 else
412 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000413
414 // Delete the old constant!
415 destroyConstant();
416}
417
Chris Lattnerc27038d2003-08-29 05:36:46 +0000418void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
419 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000420 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
421
422 std::vector<Constant*> Values;
423 Values.reserve(getValues().size());
424 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
425 Constant *Val = cast<Constant>(getValues()[i]);
426 if (Val == From) Val = cast<Constant>(To);
427 Values.push_back(Val);
428 }
429
430 ConstantStruct *Replacement = ConstantStruct::get(getType(), Values);
431 assert(Replacement != this && "I didn't contain From!");
432
433 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000434 if (DisableChecking)
435 uncheckedReplaceAllUsesWith(Replacement);
436 else
437 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000438
439 // Delete the old constant!
440 destroyConstant();
441}
442
Chris Lattnerc27038d2003-08-29 05:36:46 +0000443void ConstantPointerRef::replaceUsesOfWithOnConstant(Value *From, Value *To,
444 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000445 if (isa<GlobalValue>(To)) {
446 assert(From == getOperand(0) && "Doesn't contain from!");
447 ConstantPointerRef *Replacement =
448 ConstantPointerRef::get(cast<GlobalValue>(To));
449
450 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000451 if (DisableChecking)
452 uncheckedReplaceAllUsesWith(Replacement);
453 else
454 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000455
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000456 } else {
457 // Just replace ourselves with the To value specified.
Chris Lattnerc27038d2003-08-29 05:36:46 +0000458 if (DisableChecking)
459 uncheckedReplaceAllUsesWith(To);
460 else
461 replaceAllUsesWith(To);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000462 }
Chris Lattnerc27038d2003-08-29 05:36:46 +0000463
464 // Delete the old constant!
465 destroyConstant();
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000466}
467
Chris Lattnerc27038d2003-08-29 05:36:46 +0000468void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
469 bool DisableChecking) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000470 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
471 Constant *To = cast<Constant>(ToV);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000472
Chris Lattner46b3d302003-04-16 22:40:51 +0000473 Constant *Replacement = 0;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000474 if (getOpcode() == Instruction::GetElementPtr) {
475 std::vector<Constant*> Indices;
Chris Lattner55ed6562003-05-14 17:51:05 +0000476 Constant *Pointer = getOperand(0);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000477 Indices.reserve(getNumOperands()-1);
Chris Lattner55ed6562003-05-14 17:51:05 +0000478 if (Pointer == From) Pointer = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000479
480 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000481 Constant *Val = getOperand(i);
482 if (Val == From) Val = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000483 Indices.push_back(Val);
484 }
485 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
486 } else if (getOpcode() == Instruction::Cast) {
487 assert(getOperand(0) == From && "Cast only has one use!");
Chris Lattner55ed6562003-05-14 17:51:05 +0000488 Replacement = ConstantExpr::getCast(To, getType());
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000489 } else if (getNumOperands() == 2) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000490 Constant *C1 = getOperand(0);
491 Constant *C2 = getOperand(1);
492 if (C1 == From) C1 = To;
493 if (C2 == From) C2 = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000494 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
495 } else {
496 assert(0 && "Unknown ConstantExpr type!");
497 return;
498 }
499
500 assert(Replacement != this && "I didn't contain From!");
501
502 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000503 if (DisableChecking)
504 uncheckedReplaceAllUsesWith(Replacement);
505 else
506 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000507
508 // Delete the old constant!
509 destroyConstant();
510}
511
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000512//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000513// Factory Function Implementation
514
Chris Lattner98fa07b2003-05-23 20:03:32 +0000515// ConstantCreator - A class that is used to create constants by
516// ValueMap*. This class should be partially specialized if there is
517// something strange that needs to be done to interface to the ctor for the
518// constant.
519//
520template<class ConstantClass, class TypeClass, class ValType>
521struct ConstantCreator {
522 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
523 return new ConstantClass(Ty, V);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000524 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000525};
526
Chris Lattner98fa07b2003-05-23 20:03:32 +0000527namespace {
528 template<class ValType, class TypeClass, class ConstantClass>
529 class ValueMap {
530 protected:
531 typedef std::pair<const TypeClass*, ValType> ConstHashKey;
532 std::map<ConstHashKey, ConstantClass *> Map;
533 public:
534 // getOrCreate - Return the specified constant from the map, creating it if
535 // necessary.
536 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
537 ConstHashKey Lookup(Ty, V);
538 typename std::map<ConstHashKey,ConstantClass *>::iterator I =
539 Map.lower_bound(Lookup);
540 if (I != Map.end() && I->first == Lookup)
541 return I->second; // Is it in the map?
542
543 // If no preexisting value, create one now...
544 ConstantClass *Result =
545 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
546
547 Map.insert(I, std::make_pair(ConstHashKey(Ty, V), Result));
548 return Result;
549 }
550
551 void remove(ConstantClass *CP) {
552 // FIXME: This could be sped up a LOT. If this gets to be a performance
553 // problem, someone should look at this.
554 for (typename std::map<ConstHashKey, ConstantClass*>::iterator
555 I = Map.begin(), E = Map.end(); I != E; ++I)
556 if (I->second == CP) {
557 Map.erase(I);
558 return;
559 }
560 assert(0 && "Constant not found in constant table!");
561 }
562 };
563}
564
565
566
Chris Lattner3462ae32001-12-03 22:26:30 +0000567//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000568//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000569static ValueMap< int64_t, Type, ConstantSInt> SIntConstants;
570static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000571
Chris Lattner3462ae32001-12-03 22:26:30 +0000572ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000573 return SIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000574}
575
Chris Lattner3462ae32001-12-03 22:26:30 +0000576ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000577 return UIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000578}
579
Chris Lattner3462ae32001-12-03 22:26:30 +0000580ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000581 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000582 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
583 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000584}
585
Chris Lattner3462ae32001-12-03 22:26:30 +0000586//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000587//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000588static ValueMap<double, Type, ConstantFP> FPConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000589
Chris Lattner3462ae32001-12-03 22:26:30 +0000590ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000591 return FPConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000592}
593
Chris Lattner3462ae32001-12-03 22:26:30 +0000594//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000595//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000596static ValueMap<std::vector<Constant*>, ArrayType,
597 ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000598
Chris Lattner3462ae32001-12-03 22:26:30 +0000599ConstantArray *ConstantArray::get(const ArrayType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000600 const std::vector<Constant*> &V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000601 return ArrayConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000602}
603
Chris Lattner98fa07b2003-05-23 20:03:32 +0000604// destroyConstant - Remove the constant from the constant table...
605//
606void ConstantArray::destroyConstant() {
607 ArrayConstants.remove(this);
608 destroyConstantImpl();
609}
610
Chris Lattner29dc65a2003-10-03 18:39:57 +0000611#if 0
Chris Lattner98fa07b2003-05-23 20:03:32 +0000612/// refineAbstractType - If this callback is invoked, then this constant is of a
613/// derived type, change all users to use a concrete constant of the new type.
614///
615void ConstantArray::refineAbstractType(const DerivedType *OldTy,
616 const Type *NewTy) {
Chris Lattner7fa67832003-06-02 17:25:46 +0000617 if (OldTy == NewTy) return;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000618
619 // Make everyone now use a constant of the new type...
620 std::vector<Constant*> C;
621 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
622 C.push_back(cast<Constant>(getOperand(i)));
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000623 Constant *New = ConstantArray::get(cast<ArrayType>(NewTy), C);
624 if (New != this) {
Chris Lattner9f158122003-08-29 05:09:37 +0000625 uncheckedReplaceAllUsesWith(New);
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000626 destroyConstant(); // This constant is now dead, destroy it.
627 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000628}
Chris Lattner29dc65a2003-10-03 18:39:57 +0000629#endif
Chris Lattner98fa07b2003-05-23 20:03:32 +0000630
Chris Lattner3462ae32001-12-03 22:26:30 +0000631// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000632// contain the specified string. A null terminator is added to the specified
633// string so that it may be used in a natural way...
634//
Chris Lattner7f74a562002-01-20 22:54:45 +0000635ConstantArray *ConstantArray::get(const std::string &Str) {
636 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000637
638 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000639 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000640
641 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000642 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000643
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000644 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000645 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000646}
647
Chris Lattner81fabb02002-08-26 17:53:56 +0000648// getAsString - If the sub-element type of this array is either sbyte or ubyte,
649// then this method converts the array to an std::string and returns it.
650// Otherwise, it asserts out.
651//
652std::string ConstantArray::getAsString() const {
Chris Lattner6077c312003-07-23 15:22:26 +0000653 assert((getType()->getElementType() == Type::UByteTy ||
654 getType()->getElementType() == Type::SByteTy) && "Not a string!");
655
Chris Lattner81fabb02002-08-26 17:53:56 +0000656 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +0000657 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
658 Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue();
Chris Lattner81fabb02002-08-26 17:53:56 +0000659 return Result;
660}
661
662
Chris Lattner3462ae32001-12-03 22:26:30 +0000663//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000664//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000665static ValueMap<std::vector<Constant*>, StructType,
666 ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000667
Chris Lattner3462ae32001-12-03 22:26:30 +0000668ConstantStruct *ConstantStruct::get(const StructType *Ty,
Chris Lattner7f74a562002-01-20 22:54:45 +0000669 const std::vector<Constant*> &V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000670 return StructConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000671}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000672
Chris Lattnerd7a73302001-10-13 06:57:33 +0000673// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000674//
Chris Lattner3462ae32001-12-03 22:26:30 +0000675void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000676 StructConstants.remove(this);
677 destroyConstantImpl();
678}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000679
Chris Lattner29dc65a2003-10-03 18:39:57 +0000680#if 0
Chris Lattner98fa07b2003-05-23 20:03:32 +0000681/// refineAbstractType - If this callback is invoked, then this constant is of a
682/// derived type, change all users to use a concrete constant of the new type.
683///
684void ConstantStruct::refineAbstractType(const DerivedType *OldTy,
685 const Type *NewTy) {
Chris Lattner7fa67832003-06-02 17:25:46 +0000686 if (OldTy == NewTy) return;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000687
688 // Make everyone now use a constant of the new type...
689 std::vector<Constant*> C;
690 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
691 C.push_back(cast<Constant>(getOperand(i)));
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000692 Constant *New = ConstantStruct::get(cast<StructType>(NewTy), C);
693 if (New != this) {
Chris Lattner9f158122003-08-29 05:09:37 +0000694 uncheckedReplaceAllUsesWith(New);
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000695 destroyConstant(); // This constant is now dead, destroy it.
696 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000697}
Chris Lattner29dc65a2003-10-03 18:39:57 +0000698#endif
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000699
Chris Lattner3462ae32001-12-03 22:26:30 +0000700//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000701//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000702
703// ConstantPointerNull does not take extra "value" argument...
704template<class ValType>
705struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
706 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
707 return new ConstantPointerNull(Ty);
708 }
709};
710
711static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000712
Chris Lattner3462ae32001-12-03 22:26:30 +0000713ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000714 return NullPtrConstants.getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000715}
716
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000717// destroyConstant - Remove the constant from the constant table...
718//
719void ConstantPointerNull::destroyConstant() {
720 NullPtrConstants.remove(this);
721 destroyConstantImpl();
722}
723
Chris Lattner29dc65a2003-10-03 18:39:57 +0000724#if 0
Chris Lattner98fa07b2003-05-23 20:03:32 +0000725/// refineAbstractType - If this callback is invoked, then this constant is of a
726/// derived type, change all users to use a concrete constant of the new type.
727///
728void ConstantPointerNull::refineAbstractType(const DerivedType *OldTy,
729 const Type *NewTy) {
Chris Lattner7fa67832003-06-02 17:25:46 +0000730 if (OldTy == NewTy) return;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000731
732 // Make everyone now use a constant of the new type...
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000733 Constant *New = ConstantPointerNull::get(cast<PointerType>(NewTy));
734 if (New != this) {
Chris Lattner9f158122003-08-29 05:09:37 +0000735 uncheckedReplaceAllUsesWith(New);
Chris Lattner20ec7bc2003-05-25 16:15:32 +0000736
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000737 // This constant is now dead, destroy it.
738 destroyConstant();
739 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000740}
Chris Lattner29dc65a2003-10-03 18:39:57 +0000741#endif
Chris Lattner98fa07b2003-05-23 20:03:32 +0000742
743
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000744
Chris Lattner3462ae32001-12-03 22:26:30 +0000745//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +0000746//
Chris Lattner3462ae32001-12-03 22:26:30 +0000747ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000748 assert(GV->getParent() && "Global Value must be attached to a module!");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000749
Chris Lattnerd7a73302001-10-13 06:57:33 +0000750 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +0000751 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000752}
753
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000754// destroyConstant - Remove the constant from the constant table...
755//
756void ConstantPointerRef::destroyConstant() {
757 getValue()->getParent()->destroyConstantPointerRef(this);
758 destroyConstantImpl();
759}
760
761
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000762//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000763//
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000764typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000765
766template<>
767struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
768 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
769 if (V.first == Instruction::Cast)
770 return new ConstantExpr(Instruction::Cast, V.second[0], Ty);
771 if ((V.first >= Instruction::BinaryOpsBegin &&
772 V.first < Instruction::BinaryOpsEnd) ||
773 V.first == Instruction::Shl || V.first == Instruction::Shr)
774 return new ConstantExpr(V.first, V.second[0], V.second[1]);
775
776 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
777
778 // Check that the indices list is valid...
779 std::vector<Value*> ValIdxList(V.second.begin()+1, V.second.end());
780 const Type *DestTy = GetElementPtrInst::getIndexedType(Ty, ValIdxList,
781 true);
782 assert(DestTy && "Invalid index list for GetElementPtr expression");
783
784 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
785 return new ConstantExpr(V.second[0], IdxList, PointerType::get(DestTy));
786 }
787};
788
789static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +0000790
Chris Lattner46b3d302003-04-16 22:40:51 +0000791Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattneracdbe712003-04-17 19:24:48 +0000792 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
793 return FC; // Fold a few common cases...
794
Vikram S. Adve4c485332002-07-15 18:19:33 +0000795 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000796 std::vector<Constant*> argVec(1, C);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000797 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
798 return ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000799}
Chris Lattnerd7a73302001-10-13 06:57:33 +0000800
Chris Lattner46b3d302003-04-16 22:40:51 +0000801Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +0000802 // Check the operands for consistency first
803 assert((Opcode >= Instruction::BinaryOpsBegin &&
804 Opcode < Instruction::BinaryOpsEnd) &&
805 "Invalid opcode in binary constant expression");
806 assert(C1->getType() == C2->getType() &&
807 "Operand types in binary constant expression should match");
Chris Lattneracdbe712003-04-17 19:24:48 +0000808
809 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
810 return FC; // Fold a few common cases...
811
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000812 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000813 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
814 return ExprConstants.getOrCreate(C1->getType(), Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000815}
816
Chris Lattner38a9bcd2003-05-21 17:49:25 +0000817/// getShift - Return a shift left or shift right constant expr
818Constant *ConstantExpr::getShift(unsigned Opcode, Constant *C1, Constant *C2) {
819 // Check the operands for consistency first
820 assert((Opcode == Instruction::Shl ||
821 Opcode == Instruction::Shr) &&
822 "Invalid opcode in binary constant expression");
823 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
824 "Invalid operand types for Shift constant expr!");
825
826 if (Constant *FC = ConstantFoldShiftInstruction(Opcode, C1, C2))
827 return FC; // Fold a few common cases...
828
829 // Look up the constant in the table first to ensure uniqueness
830 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000831 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
832 return ExprConstants.getOrCreate(C1->getType(), Key);
Chris Lattner38a9bcd2003-05-21 17:49:25 +0000833}
834
835
Chris Lattner46b3d302003-04-16 22:40:51 +0000836Constant *ConstantExpr::getGetElementPtr(Constant *C,
837 const std::vector<Constant*> &IdxList){
Chris Lattneracdbe712003-04-17 19:24:48 +0000838 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
839 return FC; // Fold a few common cases...
Chris Lattner3cd8c562002-07-30 18:54:25 +0000840 const Type *Ty = C->getType();
Chris Lattner98fa07b2003-05-23 20:03:32 +0000841 assert(isa<PointerType>(Ty) &&
842 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +0000843
844 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000845 std::vector<Constant*> argVec(1, C);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000846 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
Vikram S. Adve4c485332002-07-15 18:19:33 +0000847
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000848 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,argVec);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000849 return ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +0000850}
851
852// destroyConstant - Remove the constant from the constant table...
853//
854void ConstantExpr::destroyConstant() {
855 ExprConstants.remove(this);
856 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000857}
858
Chris Lattner29dc65a2003-10-03 18:39:57 +0000859#if 0
Chris Lattner98fa07b2003-05-23 20:03:32 +0000860/// refineAbstractType - If this callback is invoked, then this constant is of a
861/// derived type, change all users to use a concrete constant of the new type.
862///
863void ConstantExpr::refineAbstractType(const DerivedType *OldTy,
864 const Type *NewTy) {
Chris Lattner7fa67832003-06-02 17:25:46 +0000865 if (OldTy == NewTy) return;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000866
867 // FIXME: These need to use a lower-level implementation method, because the
868 // ::get methods intuit the type of the result based on the types of the
869 // operands. The operand types may not have had their types resolved yet.
870 //
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000871 Constant *New;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000872 if (getOpcode() == Instruction::Cast) {
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000873 New = getCast(getOperand(0), NewTy);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000874 } else if (getOpcode() >= Instruction::BinaryOpsBegin &&
875 getOpcode() < Instruction::BinaryOpsEnd) {
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000876 New = get(getOpcode(), getOperand(0), getOperand(0));
Chris Lattner98fa07b2003-05-23 20:03:32 +0000877 } else if (getOpcode() == Instruction::Shl || getOpcode() ==Instruction::Shr){
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000878 New = getShift(getOpcode(), getOperand(0), getOperand(0));
Chris Lattner98fa07b2003-05-23 20:03:32 +0000879 } else {
880 assert(getOpcode() == Instruction::GetElementPtr);
881
882 // Make everyone now use a constant of the new type...
883 std::vector<Constant*> C;
884 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
885 C.push_back(cast<Constant>(getOperand(i)));
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000886 New = ConstantExpr::getGetElementPtr(getOperand(0), C);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000887 }
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000888 if (New != this) {
Chris Lattner9f158122003-08-29 05:09:37 +0000889 uncheckedReplaceAllUsesWith(New);
Chris Lattnercc7d6ff2003-06-16 12:11:33 +0000890 destroyConstant(); // This constant is now dead, destroy it.
891 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000892}
Chris Lattner29dc65a2003-10-03 18:39:57 +0000893#endif
Chris Lattner98fa07b2003-05-23 20:03:32 +0000894
895
896
897
Chris Lattner3cd8c562002-07-30 18:54:25 +0000898const char *ConstantExpr::getOpcodeName() const {
899 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000900}
901
Chris Lattner163b8902002-10-14 03:30:23 +0000902unsigned Constant::mutateReferences(Value *OldV, Value *NewV) {
903 // Uses of constant pointer refs are global values, not constants!
904 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
905 GlobalValue *NewGV = cast<GlobalValue>(NewV);
906 GlobalValue *OldGV = CPR->getValue();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000907
Chris Lattner163b8902002-10-14 03:30:23 +0000908 assert(OldGV == OldV && "Cannot mutate old value if I'm not using it!");
Chris Lattner163b8902002-10-14 03:30:23 +0000909 Operands[0] = NewGV;
Chris Lattnercb4d26f2003-05-15 19:37:21 +0000910 OldGV->getParent()->mutateConstantPointerRef(OldGV, NewGV);
Chris Lattner163b8902002-10-14 03:30:23 +0000911 return 1;
912 } else {
913 Constant *NewC = cast<Constant>(NewV);
914 unsigned NumReplaced = 0;
915 for (unsigned i = 0, N = getNumOperands(); i != N; ++i)
916 if (Operands[i] == OldV) {
917 ++NumReplaced;
918 Operands[i] = NewC;
919 }
920 return NumReplaced;
921 }
Chris Lattner25033252001-10-03 19:28:15 +0000922}