blob: a393be98ae3899434f3f245bf92aa13da27e999b [file] [log] [blame]
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001//===-- Constants.cpp - Implement Constant nodes --------------------------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner3462ae32001-12-03 22:26:30 +000010// This file implements the Constant* classes...
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerca142372002-04-28 19:55:58 +000014#include "llvm/Constants.h"
Chris Lattner5a945e32004-01-12 21:13:12 +000015#include "ConstantFolding.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000016#include "llvm/DerivedTypes.h"
Vikram S. Adve4e537b22002-07-14 23:13:17 +000017#include "llvm/iMemory.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000018#include "llvm/SymbolTable.h"
Chris Lattnerd7a73302001-10-13 06:57:33 +000019#include "llvm/Module.h"
Chris Lattner5de22042001-11-27 00:03:19 +000020#include "Support/StringExtras.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000021#include <algorithm>
Chris Lattner189d19f2003-11-21 20:23:48 +000022using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000023
Chris Lattner3462ae32001-12-03 22:26:30 +000024ConstantBool *ConstantBool::True = new ConstantBool(true);
25ConstantBool *ConstantBool::False = new ConstantBool(false);
Chris Lattner2f7c9632001-06-06 20:29:01 +000026
Chris Lattner9655e542001-07-20 19:16:02 +000027
Chris Lattner2f7c9632001-06-06 20:29:01 +000028//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000029// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000030//===----------------------------------------------------------------------===//
31
32// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +000033void Constant::setName(const std::string &Name, SymbolTable *ST) {
Chris Lattner49d855c2001-09-07 16:46:31 +000034 assert(ST && "Type::setName - Must provide symbol table argument!");
35
36 if (Name.size()) ST->insert(Name, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000037}
38
Chris Lattner3462ae32001-12-03 22:26:30 +000039void Constant::destroyConstantImpl() {
40 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000041 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000042 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +000043 // but they don't know that. Because we only find out when the CPV is
44 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000045 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000046 //
47 while (!use_empty()) {
48 Value *V = use_back();
49#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000050 if (!isa<Constant>(V))
51 std::cerr << "While deleting: " << *this
52 << "\n\nUse still stuck around after Def is destroyed: "
53 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000054#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000055 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Chris Lattner3462ae32001-12-03 22:26:30 +000056 Constant *CPV = cast<Constant>(V);
Chris Lattnerd7a73302001-10-13 06:57:33 +000057 CPV->destroyConstant();
58
59 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000060 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000061 }
62
63 // Value has no outstanding references it is safe to delete it now...
64 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000065}
Chris Lattner2f7c9632001-06-06 20:29:01 +000066
Chris Lattnerb1585a92002-08-13 17:50:20 +000067// Static constructor to create a '0' constant of arbitrary type...
68Constant *Constant::getNullValue(const Type *Ty) {
69 switch (Ty->getPrimitiveID()) {
Chris Lattner3e88ef92003-10-03 19:34:51 +000070 case Type::BoolTyID: {
71 static Constant *NullBool = ConstantBool::get(false);
72 return NullBool;
73 }
74 case Type::SByteTyID: {
75 static Constant *NullSByte = ConstantSInt::get(Type::SByteTy, 0);
76 return NullSByte;
77 }
78 case Type::UByteTyID: {
79 static Constant *NullUByte = ConstantUInt::get(Type::UByteTy, 0);
80 return NullUByte;
81 }
82 case Type::ShortTyID: {
83 static Constant *NullShort = ConstantSInt::get(Type::ShortTy, 0);
84 return NullShort;
85 }
86 case Type::UShortTyID: {
87 static Constant *NullUShort = ConstantUInt::get(Type::UShortTy, 0);
88 return NullUShort;
89 }
90 case Type::IntTyID: {
91 static Constant *NullInt = ConstantSInt::get(Type::IntTy, 0);
92 return NullInt;
93 }
94 case Type::UIntTyID: {
95 static Constant *NullUInt = ConstantUInt::get(Type::UIntTy, 0);
96 return NullUInt;
97 }
98 case Type::LongTyID: {
99 static Constant *NullLong = ConstantSInt::get(Type::LongTy, 0);
100 return NullLong;
101 }
102 case Type::ULongTyID: {
103 static Constant *NullULong = ConstantUInt::get(Type::ULongTy, 0);
104 return NullULong;
105 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000106
Chris Lattner3e88ef92003-10-03 19:34:51 +0000107 case Type::FloatTyID: {
108 static Constant *NullFloat = ConstantFP::get(Type::FloatTy, 0);
109 return NullFloat;
110 }
111 case Type::DoubleTyID: {
112 static Constant *NullDouble = ConstantFP::get(Type::DoubleTy, 0);
113 return NullDouble;
114 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000115
116 case Type::PointerTyID:
117 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner3e88ef92003-10-03 19:34:51 +0000118
Chris Lattner9fba3da2004-02-15 05:53:04 +0000119 case Type::StructTyID:
120 case Type::ArrayTyID:
121 return ConstantAggregateZero::get(Ty);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000122 default:
Chris Lattnerc33ae672003-03-06 21:02:18 +0000123 // Function, Type, Label, or Opaque type?
124 assert(0 && "Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000125 return 0;
126 }
127}
128
129// Static constructor to create the maximum constant of an integral type...
130ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
131 switch (Ty->getPrimitiveID()) {
132 case Type::BoolTyID: return ConstantBool::True;
133 case Type::SByteTyID:
134 case Type::ShortTyID:
135 case Type::IntTyID:
136 case Type::LongTyID: {
137 // Calculate 011111111111111...
138 unsigned TypeBits = Ty->getPrimitiveSize()*8;
139 int64_t Val = INT64_MAX; // All ones
140 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
141 return ConstantSInt::get(Ty, Val);
142 }
143
144 case Type::UByteTyID:
145 case Type::UShortTyID:
146 case Type::UIntTyID:
147 case Type::ULongTyID: return getAllOnesValue(Ty);
148
Chris Lattner31408f72002-08-14 17:12:13 +0000149 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000150 }
151}
152
153// Static constructor to create the minimum constant for an integral type...
154ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
155 switch (Ty->getPrimitiveID()) {
156 case Type::BoolTyID: return ConstantBool::False;
157 case Type::SByteTyID:
158 case Type::ShortTyID:
159 case Type::IntTyID:
160 case Type::LongTyID: {
161 // Calculate 1111111111000000000000
162 unsigned TypeBits = Ty->getPrimitiveSize()*8;
163 int64_t Val = -1; // All ones
164 Val <<= TypeBits-1; // Shift over to the right spot
165 return ConstantSInt::get(Ty, Val);
166 }
167
168 case Type::UByteTyID:
169 case Type::UShortTyID:
170 case Type::UIntTyID:
171 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
172
Chris Lattner31408f72002-08-14 17:12:13 +0000173 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000174 }
175}
176
177// Static constructor to create an integral constant with all bits set
178ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
179 switch (Ty->getPrimitiveID()) {
180 case Type::BoolTyID: return ConstantBool::True;
181 case Type::SByteTyID:
182 case Type::ShortTyID:
183 case Type::IntTyID:
184 case Type::LongTyID: return ConstantSInt::get(Ty, -1);
185
186 case Type::UByteTyID:
187 case Type::UShortTyID:
188 case Type::UIntTyID:
189 case Type::ULongTyID: {
190 // Calculate ~0 of the right type...
191 unsigned TypeBits = Ty->getPrimitiveSize()*8;
192 uint64_t Val = ~0ULL; // All ones
193 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
194 return ConstantUInt::get(Ty, Val);
195 }
Chris Lattner31408f72002-08-14 17:12:13 +0000196 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000197 }
198}
199
Chris Lattner83e5d392003-03-10 22:39:02 +0000200bool ConstantUInt::isAllOnesValue() const {
201 unsigned TypeBits = getType()->getPrimitiveSize()*8;
202 uint64_t Val = ~0ULL; // All ones
203 Val >>= 64-TypeBits; // Shift out inappropriate bits
204 return getValue() == Val;
205}
206
Chris Lattnerb1585a92002-08-13 17:50:20 +0000207
Chris Lattner2f7c9632001-06-06 20:29:01 +0000208//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +0000209// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +0000210//===----------------------------------------------------------------------===//
211
212//===----------------------------------------------------------------------===//
213// Normal Constructors
214
Chris Lattnerb1585a92002-08-13 17:50:20 +0000215ConstantBool::ConstantBool(bool V) : ConstantIntegral(Type::BoolTy) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000216 Val = V;
217}
Chris Lattner49d855c2001-09-07 16:46:31 +0000218
Chris Lattnerb1585a92002-08-13 17:50:20 +0000219ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : ConstantIntegral(Ty) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000220 Val.Unsigned = V;
Chris Lattner7309d662001-07-21 19:16:08 +0000221}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000222
Chris Lattner3462ae32001-12-03 22:26:30 +0000223ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000224 assert(Ty->isInteger() && Ty->isSigned() &&
225 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000226 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000227}
228
Chris Lattner3462ae32001-12-03 22:26:30 +0000229ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000230 assert(Ty->isInteger() && Ty->isUnsigned() &&
231 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000232 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000233}
234
Chris Lattner3462ae32001-12-03 22:26:30 +0000235ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
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 Val = V;
238}
239
Chris Lattner3462ae32001-12-03 22:26:30 +0000240ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000241 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner0d779712002-10-08 23:33:52 +0000242 Operands.reserve(V.size());
243 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattnerab69ecc2003-08-18 16:54:48 +0000244 assert(V[i]->getType() == T->getElementType() ||
245 (T->isAbstract() &&
246 V[i]->getType()->getPrimitiveID() ==
247 T->getElementType()->getPrimitiveID()));
Chris Lattnera073acb2001-07-07 08:36:50 +0000248 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000249 }
250}
251
Chris Lattner3462ae32001-12-03 22:26:30 +0000252ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000253 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000254 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000255 "Invalid initializer vector for constant structure");
Chris Lattner0d779712002-10-08 23:33:52 +0000256 Operands.reserve(V.size());
257 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000258 assert((V[i]->getType() == T->getElementType(i) ||
259 ((T->getElementType(i)->isAbstract() ||
260 V[i]->getType()->isAbstract()) &&
261 T->getElementType(i)->getPrimitiveID() ==
262 V[i]->getType()->getPrimitiveID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000263 "Initializer for struct element doesn't match struct element type!");
Chris Lattnera073acb2001-07-07 08:36:50 +0000264 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000265 }
266}
267
Chris Lattner3462ae32001-12-03 22:26:30 +0000268ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
Chris Lattnere120a732003-11-17 19:47:21 +0000269 : Constant(GV->getType()) {
Chris Lattner6e415c02004-03-12 05:54:04 +0000270 Operands.reserve(1);
Chris Lattner60e0dd72001-10-03 06:12:09 +0000271 Operands.push_back(Use(GV, this));
272}
273
Chris Lattner3cd8c562002-07-30 18:54:25 +0000274ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
275 : Constant(Ty), iType(Opcode) {
Chris Lattner6e415c02004-03-12 05:54:04 +0000276 Operands.reserve(1);
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000277 Operands.push_back(Use(C, this));
278}
279
Chris Lattner6e415c02004-03-12 05:54:04 +0000280// Select instruction creation ctor
281ConstantExpr::ConstantExpr(Constant *C, Constant *V1, Constant *V2)
282 : Constant(V1->getType()), iType(Instruction::Select) {
283 Operands.reserve(3);
284 Operands.push_back(Use(C, this));
285 Operands.push_back(Use(V1, this));
286 Operands.push_back(Use(V2, this));
287}
288
289
Chris Lattner22ced562003-06-22 20:48:30 +0000290static bool isSetCC(unsigned Opcode) {
291 return Opcode == Instruction::SetEQ || Opcode == Instruction::SetNE ||
292 Opcode == Instruction::SetLT || Opcode == Instruction::SetGT ||
293 Opcode == Instruction::SetLE || Opcode == Instruction::SetGE;
294}
295
Chris Lattner3cd8c562002-07-30 18:54:25 +0000296ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Chris Lattner22ced562003-06-22 20:48:30 +0000297 : Constant(isSetCC(Opcode) ? Type::BoolTy : C1->getType()), iType(Opcode) {
Chris Lattner6e415c02004-03-12 05:54:04 +0000298 Operands.reserve(2);
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000299 Operands.push_back(Use(C1, this));
300 Operands.push_back(Use(C2, this));
301}
302
Chris Lattner3cd8c562002-07-30 18:54:25 +0000303ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
304 const Type *DestTy)
305 : Constant(DestTy), iType(Instruction::GetElementPtr) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000306 Operands.reserve(1+IdxList.size());
307 Operands.push_back(Use(C, this));
308 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
309 Operands.push_back(Use(IdxList[i], this));
310}
311
Chris Lattner817175f2004-03-29 02:37:53 +0000312/// ConstantExpr::get* - Return some common constants without having to
313/// specify the full Instruction::OPCODE identifier.
314///
315Constant *ConstantExpr::getNeg(Constant *C) {
Chris Lattner3cdc27c2004-03-29 19:51:24 +0000316 if (!C->getType()->isFloatingPoint())
317 return get(Instruction::Sub, getNullValue(C->getType()), C);
318 else
319 return get(Instruction::Sub, ConstantFP::get(C->getType(), -0.0), C);
Chris Lattner817175f2004-03-29 02:37:53 +0000320}
321Constant *ConstantExpr::getNot(Constant *C) {
322 assert(isa<ConstantIntegral>(C) && "Cannot NOT a nonintegral type!");
323 return get(Instruction::Xor, C,
324 ConstantIntegral::getAllOnesValue(C->getType()));
325}
326Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
327 return get(Instruction::Add, C1, C2);
328}
329Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
330 return get(Instruction::Sub, C1, C2);
331}
332Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
333 return get(Instruction::Mul, C1, C2);
334}
335Constant *ConstantExpr::getDiv(Constant *C1, Constant *C2) {
336 return get(Instruction::Div, C1, C2);
337}
338Constant *ConstantExpr::getRem(Constant *C1, Constant *C2) {
339 return get(Instruction::Rem, C1, C2);
340}
341Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
342 return get(Instruction::And, C1, C2);
343}
344Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
345 return get(Instruction::Or, C1, C2);
346}
347Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
348 return get(Instruction::Xor, C1, C2);
349}
350Constant *ConstantExpr::getSetEQ(Constant *C1, Constant *C2) {
351 return get(Instruction::SetEQ, C1, C2);
352}
353Constant *ConstantExpr::getSetNE(Constant *C1, Constant *C2) {
354 return get(Instruction::SetNE, C1, C2);
355}
356Constant *ConstantExpr::getSetLT(Constant *C1, Constant *C2) {
357 return get(Instruction::SetLT, C1, C2);
358}
359Constant *ConstantExpr::getSetGT(Constant *C1, Constant *C2) {
360 return get(Instruction::SetGT, C1, C2);
361}
362Constant *ConstantExpr::getSetLE(Constant *C1, Constant *C2) {
363 return get(Instruction::SetLE, C1, C2);
364}
365Constant *ConstantExpr::getSetGE(Constant *C1, Constant *C2) {
366 return get(Instruction::SetGE, C1, C2);
367}
368Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
369 return get(Instruction::Shl, C1, C2);
370}
371Constant *ConstantExpr::getShr(Constant *C1, Constant *C2) {
372 return get(Instruction::Shr, C1, C2);
373}
374
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000375Constant *ConstantExpr::getUShr(Constant *C1, Constant *C2) {
376 if (C1->getType()->isUnsigned()) return getShr(C1, C2);
377 return getCast(getShr(getCast(C1,
378 C1->getType()->getUnsignedVersion()), C2), C1->getType());
379}
Chris Lattner817175f2004-03-29 02:37:53 +0000380
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000381Constant *ConstantExpr::getSShr(Constant *C1, Constant *C2) {
382 if (C1->getType()->isSigned()) return getShr(C1, C2);
383 return getCast(getShr(getCast(C1,
384 C1->getType()->getSignedVersion()), C2), C1->getType());
385}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000386
Chris Lattner2f7c9632001-06-06 20:29:01 +0000387
388//===----------------------------------------------------------------------===//
Chris Lattnerd7a73302001-10-13 06:57:33 +0000389// classof implementations
390
Chris Lattnerb1585a92002-08-13 17:50:20 +0000391bool ConstantIntegral::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000392 return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
Chris Lattner41e99a02002-08-12 21:21:21 +0000393}
394
Chris Lattner3462ae32001-12-03 22:26:30 +0000395bool ConstantInt::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000396 return CPV->getType()->isInteger() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000397}
Chris Lattner3462ae32001-12-03 22:26:30 +0000398bool ConstantSInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000399 return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000400}
Chris Lattner3462ae32001-12-03 22:26:30 +0000401bool ConstantUInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000402 return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000403}
Chris Lattner3462ae32001-12-03 22:26:30 +0000404bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000405 const Type *Ty = CPV->getType();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000406 return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000407 !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000408}
Chris Lattner9fba3da2004-02-15 05:53:04 +0000409bool ConstantAggregateZero::classof(const Constant *CPV) {
410 return (isa<ArrayType>(CPV->getType()) || isa<StructType>(CPV->getType())) &&
411 CPV->isNullValue();
412}
Chris Lattner3462ae32001-12-03 22:26:30 +0000413bool ConstantArray::classof(const Constant *CPV) {
Chris Lattner9fba3da2004-02-15 05:53:04 +0000414 return isa<ArrayType>(CPV->getType()) && !CPV->isNullValue();
Chris Lattnerd7a73302001-10-13 06:57:33 +0000415}
Chris Lattner3462ae32001-12-03 22:26:30 +0000416bool ConstantStruct::classof(const Constant *CPV) {
Chris Lattner9fba3da2004-02-15 05:53:04 +0000417 return isa<StructType>(CPV->getType()) && !CPV->isNullValue();
Chris Lattnerd7a73302001-10-13 06:57:33 +0000418}
Chris Lattnere120a732003-11-17 19:47:21 +0000419
420bool ConstantPointerNull::classof(const Constant *CPV) {
421 return isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV) &&
422 CPV->getNumOperands() == 0;
423}
424
425bool ConstantPointerRef::classof(const Constant *CPV) {
426 return isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV) &&
427 CPV->getNumOperands() == 1;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000428}
429
430
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000431
Chris Lattner2f7c9632001-06-06 20:29:01 +0000432//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000433// isValueValidForType implementations
434
Chris Lattner3462ae32001-12-03 22:26:30 +0000435bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000436 switch (Ty->getPrimitiveID()) {
437 default:
438 return false; // These can't be represented as integers!!!
439
440 // Signed types...
441 case Type::SByteTyID:
442 return (Val <= INT8_MAX && Val >= INT8_MIN);
443 case Type::ShortTyID:
444 return (Val <= INT16_MAX && Val >= INT16_MIN);
445 case Type::IntTyID:
446 return (Val <= INT32_MAX && Val >= INT32_MIN);
447 case Type::LongTyID:
448 return true; // This is the largest type...
449 }
450 assert(0 && "WTF?");
451 return false;
452}
453
Chris Lattner3462ae32001-12-03 22:26:30 +0000454bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000455 switch (Ty->getPrimitiveID()) {
456 default:
457 return false; // These can't be represented as integers!!!
458
459 // Unsigned types...
460 case Type::UByteTyID:
461 return (Val <= UINT8_MAX);
462 case Type::UShortTyID:
463 return (Val <= UINT16_MAX);
464 case Type::UIntTyID:
465 return (Val <= UINT32_MAX);
466 case Type::ULongTyID:
467 return true; // This is the largest type...
468 }
469 assert(0 && "WTF?");
470 return false;
471}
472
Chris Lattner3462ae32001-12-03 22:26:30 +0000473bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000474 switch (Ty->getPrimitiveID()) {
475 default:
476 return false; // These can't be represented as floating point!
477
478 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000479 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000480 case Type::DoubleTyID:
481 return true; // This is the largest type...
482 }
483};
Chris Lattner9655e542001-07-20 19:16:02 +0000484
Chris Lattner49d855c2001-09-07 16:46:31 +0000485//===----------------------------------------------------------------------===//
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000486// replaceUsesOfWithOnConstant implementations
487
Chris Lattnerc27038d2003-08-29 05:36:46 +0000488void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
489 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000490 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
491
492 std::vector<Constant*> Values;
493 Values.reserve(getValues().size()); // Build replacement array...
494 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
495 Constant *Val = cast<Constant>(getValues()[i]);
496 if (Val == From) Val = cast<Constant>(To);
497 Values.push_back(Val);
498 }
499
Chris Lattnerc75bf522004-02-15 04:05:58 +0000500 Constant *Replacement = ConstantArray::get(getType(), Values);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000501 assert(Replacement != this && "I didn't contain From!");
502
503 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000504 if (DisableChecking)
505 uncheckedReplaceAllUsesWith(Replacement);
506 else
507 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000508
509 // Delete the old constant!
510 destroyConstant();
511}
512
Chris Lattnerc27038d2003-08-29 05:36:46 +0000513void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
514 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000515 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
516
517 std::vector<Constant*> Values;
518 Values.reserve(getValues().size());
519 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
520 Constant *Val = cast<Constant>(getValues()[i]);
521 if (Val == From) Val = cast<Constant>(To);
522 Values.push_back(Val);
523 }
524
Chris Lattner37a716f2004-02-15 04:07:32 +0000525 Constant *Replacement = ConstantStruct::get(getType(), Values);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000526 assert(Replacement != this && "I didn't contain From!");
527
528 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000529 if (DisableChecking)
530 uncheckedReplaceAllUsesWith(Replacement);
531 else
532 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000533
534 // Delete the old constant!
535 destroyConstant();
536}
537
Chris Lattnerc27038d2003-08-29 05:36:46 +0000538void ConstantPointerRef::replaceUsesOfWithOnConstant(Value *From, Value *To,
539 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000540 if (isa<GlobalValue>(To)) {
541 assert(From == getOperand(0) && "Doesn't contain from!");
542 ConstantPointerRef *Replacement =
543 ConstantPointerRef::get(cast<GlobalValue>(To));
544
545 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000546 if (DisableChecking)
547 uncheckedReplaceAllUsesWith(Replacement);
548 else
549 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000550
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000551 } else {
552 // Just replace ourselves with the To value specified.
Chris Lattnerc27038d2003-08-29 05:36:46 +0000553 if (DisableChecking)
554 uncheckedReplaceAllUsesWith(To);
555 else
556 replaceAllUsesWith(To);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000557 }
Chris Lattnerc27038d2003-08-29 05:36:46 +0000558
559 // Delete the old constant!
560 destroyConstant();
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000561}
562
Chris Lattnerc27038d2003-08-29 05:36:46 +0000563void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
564 bool DisableChecking) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000565 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
566 Constant *To = cast<Constant>(ToV);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000567
Chris Lattner46b3d302003-04-16 22:40:51 +0000568 Constant *Replacement = 0;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000569 if (getOpcode() == Instruction::GetElementPtr) {
570 std::vector<Constant*> Indices;
Chris Lattner55ed6562003-05-14 17:51:05 +0000571 Constant *Pointer = getOperand(0);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000572 Indices.reserve(getNumOperands()-1);
Chris Lattner55ed6562003-05-14 17:51:05 +0000573 if (Pointer == From) Pointer = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000574
575 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000576 Constant *Val = getOperand(i);
577 if (Val == From) Val = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000578 Indices.push_back(Val);
579 }
580 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
581 } else if (getOpcode() == Instruction::Cast) {
582 assert(getOperand(0) == From && "Cast only has one use!");
Chris Lattner55ed6562003-05-14 17:51:05 +0000583 Replacement = ConstantExpr::getCast(To, getType());
Chris Lattner467cb2b2004-03-31 02:56:11 +0000584 } else if (getOpcode() == Instruction::Select) {
585 Constant *C1 = getOperand(0);
586 Constant *C2 = getOperand(1);
587 Constant *C3 = getOperand(2);
588 if (C1 == From) C1 = To;
589 if (C2 == From) C2 = To;
590 if (C3 == From) C3 = To;
591 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000592 } else if (getNumOperands() == 2) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000593 Constant *C1 = getOperand(0);
594 Constant *C2 = getOperand(1);
595 if (C1 == From) C1 = To;
596 if (C2 == From) C2 = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000597 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
598 } else {
599 assert(0 && "Unknown ConstantExpr type!");
600 return;
601 }
602
603 assert(Replacement != this && "I didn't contain From!");
604
605 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000606 if (DisableChecking)
607 uncheckedReplaceAllUsesWith(Replacement);
608 else
609 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000610
611 // Delete the old constant!
612 destroyConstant();
613}
614
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000615//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000616// Factory Function Implementation
617
Chris Lattner98fa07b2003-05-23 20:03:32 +0000618// ConstantCreator - A class that is used to create constants by
619// ValueMap*. This class should be partially specialized if there is
620// something strange that needs to be done to interface to the ctor for the
621// constant.
622//
Chris Lattner189d19f2003-11-21 20:23:48 +0000623namespace llvm {
624 template<class ConstantClass, class TypeClass, class ValType>
625 struct ConstantCreator {
626 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
627 return new ConstantClass(Ty, V);
628 }
629 };
630
631 template<class ConstantClass, class TypeClass>
632 struct ConvertConstantType {
633 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
634 assert(0 && "This type cannot be converted!\n");
635 abort();
636 }
637 };
638}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000639
Chris Lattner98fa07b2003-05-23 20:03:32 +0000640namespace {
641 template<class ValType, class TypeClass, class ConstantClass>
Chris Lattnerb50d1352003-10-05 00:17:43 +0000642 class ValueMap : public AbstractTypeUser {
643 typedef std::pair<const TypeClass*, ValType> MapKey;
644 typedef std::map<MapKey, ConstantClass *> MapTy;
645 typedef typename MapTy::iterator MapIterator;
646 MapTy Map;
647
648 typedef std::map<const TypeClass*, MapIterator> AbstractTypeMapTy;
649 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000650 public:
651 // getOrCreate - Return the specified constant from the map, creating it if
652 // necessary.
653 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000654 MapKey Lookup(Ty, V);
655 MapIterator I = Map.lower_bound(Lookup);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000656 if (I != Map.end() && I->first == Lookup)
657 return I->second; // Is it in the map?
658
659 // If no preexisting value, create one now...
660 ConstantClass *Result =
661 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
662
Chris Lattnerb50d1352003-10-05 00:17:43 +0000663
664 /// FIXME: why does this assert fail when loading 176.gcc?
665 //assert(Result->getType() == Ty && "Type specified is not correct!");
666 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
667
668 // If the type of the constant is abstract, make sure that an entry exists
669 // for it in the AbstractTypeMap.
670 if (Ty->isAbstract()) {
671 typename AbstractTypeMapTy::iterator TI =
672 AbstractTypeMap.lower_bound(Ty);
673
674 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
675 // Add ourselves to the ATU list of the type.
676 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
677
678 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
679 }
680 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000681 return Result;
682 }
683
684 void remove(ConstantClass *CP) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000685 // FIXME: This should not use a linear scan. If this gets to be a
686 // performance problem, someone should look at this.
687 MapIterator I = Map.begin();
688 for (MapIterator E = Map.end(); I != E && I->second != CP; ++I)
689 /* empty */;
690
691 assert(I != Map.end() && "Constant not found in constant table!");
692
693 // Now that we found the entry, make sure this isn't the entry that
694 // the AbstractTypeMap points to.
695 const TypeClass *Ty = I->first.first;
696 if (Ty->isAbstract()) {
697 assert(AbstractTypeMap.count(Ty) &&
698 "Abstract type not in AbstractTypeMap?");
699 MapIterator &ATMEntryIt = AbstractTypeMap[Ty];
700 if (ATMEntryIt == I) {
701 // Yes, we are removing the representative entry for this type.
702 // See if there are any other entries of the same type.
703 MapIterator TmpIt = ATMEntryIt;
704
705 // First check the entry before this one...
706 if (TmpIt != Map.begin()) {
707 --TmpIt;
708 if (TmpIt->first.first != Ty) // Not the same type, move back...
709 ++TmpIt;
710 }
711
712 // If we didn't find the same type, try to move forward...
713 if (TmpIt == ATMEntryIt) {
714 ++TmpIt;
715 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
716 --TmpIt; // No entry afterwards with the same type
717 }
718
719 // If there is another entry in the map of the same abstract type,
720 // update the AbstractTypeMap entry now.
721 if (TmpIt != ATMEntryIt) {
722 ATMEntryIt = TmpIt;
723 } else {
724 // Otherwise, we are removing the last instance of this type
725 // from the table. Remove from the ATM, and from user list.
726 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
727 AbstractTypeMap.erase(Ty);
728 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000729 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000730 }
731
732 Map.erase(I);
733 }
734
735 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
736 typename AbstractTypeMapTy::iterator I =
737 AbstractTypeMap.find(cast<TypeClass>(OldTy));
738
739 assert(I != AbstractTypeMap.end() &&
740 "Abstract type not in AbstractTypeMap?");
741
742 // Convert a constant at a time until the last one is gone. The last one
743 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
744 // eliminated eventually.
745 do {
746 ConvertConstantType<ConstantClass,
747 TypeClass>::convert(I->second->second,
748 cast<TypeClass>(NewTy));
749
750 I = AbstractTypeMap.find(cast<TypeClass>(OldTy));
751 } while (I != AbstractTypeMap.end());
752 }
753
754 // If the type became concrete without being refined to any other existing
755 // type, we just remove ourselves from the ATU list.
756 void typeBecameConcrete(const DerivedType *AbsTy) {
757 AbsTy->removeAbstractTypeUser(this);
758 }
759
760 void dump() const {
761 std::cerr << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +0000762 }
763 };
764}
765
766
767
Chris Lattner3462ae32001-12-03 22:26:30 +0000768//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000769//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000770static ValueMap< int64_t, Type, ConstantSInt> SIntConstants;
771static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000772
Chris Lattner3462ae32001-12-03 22:26:30 +0000773ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000774 return SIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000775}
776
Chris Lattner3462ae32001-12-03 22:26:30 +0000777ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000778 return UIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000779}
780
Chris Lattner3462ae32001-12-03 22:26:30 +0000781ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000782 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000783 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
784 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000785}
786
Chris Lattner3462ae32001-12-03 22:26:30 +0000787//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000788//
Chris Lattnerac80ea42004-02-01 22:49:04 +0000789namespace llvm {
790 template<>
791 struct ConstantCreator<ConstantFP, Type, uint64_t> {
792 static ConstantFP *create(const Type *Ty, uint64_t V) {
793 assert(Ty == Type::DoubleTy);
794 union {
795 double F;
796 uint64_t I;
797 } T;
798 T.I = V;
799 return new ConstantFP(Ty, T.F);
800 }
801 };
802 template<>
803 struct ConstantCreator<ConstantFP, Type, uint32_t> {
804 static ConstantFP *create(const Type *Ty, uint32_t V) {
805 assert(Ty == Type::FloatTy);
806 union {
807 float F;
808 uint32_t I;
809 } T;
810 T.I = V;
811 return new ConstantFP(Ty, T.F);
812 }
813 };
814}
815
816static ValueMap<uint64_t, Type, ConstantFP> DoubleConstants;
817static ValueMap<uint32_t, Type, ConstantFP> FloatConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000818
Chris Lattner3462ae32001-12-03 22:26:30 +0000819ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner241ed4c2004-01-23 00:55:21 +0000820 if (Ty == Type::FloatTy) {
821 // Force the value through memory to normalize it.
Chris Lattnerac80ea42004-02-01 22:49:04 +0000822 union {
823 float F;
824 uint32_t I;
825 } T;
826 T.F = (float)V;
827 return FloatConstants.getOrCreate(Ty, T.I);
828 } else {
829 assert(Ty == Type::DoubleTy);
830 union {
831 double F;
832 uint64_t I;
833 } T;
834 T.F = V;
835 return DoubleConstants.getOrCreate(Ty, T.I);
Chris Lattner241ed4c2004-01-23 00:55:21 +0000836 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000837}
838
Chris Lattner9fba3da2004-02-15 05:53:04 +0000839//---- ConstantAggregateZero::get() implementation...
840//
841namespace llvm {
842 // ConstantAggregateZero does not take extra "value" argument...
843 template<class ValType>
844 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
845 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
846 return new ConstantAggregateZero(Ty);
847 }
848 };
849
850 template<>
851 struct ConvertConstantType<ConstantAggregateZero, Type> {
852 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
853 // Make everyone now use a constant of the new type...
854 Constant *New = ConstantAggregateZero::get(NewTy);
855 assert(New != OldC && "Didn't replace constant??");
856 OldC->uncheckedReplaceAllUsesWith(New);
857 OldC->destroyConstant(); // This constant is now dead, destroy it.
858 }
859 };
860}
861
862static ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
863
864Constant *ConstantAggregateZero::get(const Type *Ty) {
865 return AggZeroConstants.getOrCreate(Ty, 0);
866}
867
868// destroyConstant - Remove the constant from the constant table...
869//
870void ConstantAggregateZero::destroyConstant() {
871 AggZeroConstants.remove(this);
872 destroyConstantImpl();
873}
874
875void ConstantAggregateZero::replaceUsesOfWithOnConstant(Value *From, Value *To,
876 bool DisableChecking) {
877 assert(0 && "No uses!");
878 abort();
879}
880
881
882
Chris Lattner3462ae32001-12-03 22:26:30 +0000883//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000884//
Chris Lattner189d19f2003-11-21 20:23:48 +0000885namespace llvm {
886 template<>
887 struct ConvertConstantType<ConstantArray, ArrayType> {
888 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
889 // Make everyone now use a constant of the new type...
890 std::vector<Constant*> C;
891 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
892 C.push_back(cast<Constant>(OldC->getOperand(i)));
893 Constant *New = ConstantArray::get(NewTy, C);
894 assert(New != OldC && "Didn't replace constant??");
895 OldC->uncheckedReplaceAllUsesWith(New);
896 OldC->destroyConstant(); // This constant is now dead, destroy it.
897 }
898 };
899}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000900
Chris Lattner98fa07b2003-05-23 20:03:32 +0000901static ValueMap<std::vector<Constant*>, ArrayType,
902 ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000903
Chris Lattner015e8212004-02-15 04:14:47 +0000904Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +0000905 const std::vector<Constant*> &V) {
906 // If this is an all-zero array, return a ConstantAggregateZero object
907 if (!V.empty()) {
908 Constant *C = V[0];
909 if (!C->isNullValue())
910 return ArrayConstants.getOrCreate(Ty, V);
911 for (unsigned i = 1, e = V.size(); i != e; ++i)
912 if (V[i] != C)
913 return ArrayConstants.getOrCreate(Ty, V);
914 }
915 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000916}
917
Chris Lattner98fa07b2003-05-23 20:03:32 +0000918// destroyConstant - Remove the constant from the constant table...
919//
920void ConstantArray::destroyConstant() {
921 ArrayConstants.remove(this);
922 destroyConstantImpl();
923}
924
Chris Lattner3462ae32001-12-03 22:26:30 +0000925// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000926// contain the specified string. A null terminator is added to the specified
927// string so that it may be used in a natural way...
928//
Chris Lattner015e8212004-02-15 04:14:47 +0000929Constant *ConstantArray::get(const std::string &Str) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000930 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000931
932 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000933 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000934
935 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000936 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000937
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000938 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000939 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000940}
941
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000942/// isString - This method returns true if the array is an array of sbyte or
943/// ubyte, and if the elements of the array are all ConstantInt's.
944bool ConstantArray::isString() const {
945 // Check the element type for sbyte or ubyte...
Chris Lattnere8701f62004-01-14 17:51:53 +0000946 if (getType()->getElementType() != Type::UByteTy &&
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000947 getType()->getElementType() != Type::SByteTy)
948 return false;
949 // Check the elements to make sure they are all integers, not constant
950 // expressions.
951 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
952 if (!isa<ConstantInt>(getOperand(i)))
953 return false;
954 return true;
955}
956
Chris Lattner81fabb02002-08-26 17:53:56 +0000957// getAsString - If the sub-element type of this array is either sbyte or ubyte,
958// then this method converts the array to an std::string and returns it.
959// Otherwise, it asserts out.
960//
961std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000962 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +0000963 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +0000964 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
965 Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue();
Chris Lattner81fabb02002-08-26 17:53:56 +0000966 return Result;
967}
968
969
Chris Lattner3462ae32001-12-03 22:26:30 +0000970//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000971//
Chris Lattnerb50d1352003-10-05 00:17:43 +0000972
Chris Lattner189d19f2003-11-21 20:23:48 +0000973namespace llvm {
974 template<>
975 struct ConvertConstantType<ConstantStruct, StructType> {
976 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
977 // Make everyone now use a constant of the new type...
978 std::vector<Constant*> C;
979 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
980 C.push_back(cast<Constant>(OldC->getOperand(i)));
981 Constant *New = ConstantStruct::get(NewTy, C);
982 assert(New != OldC && "Didn't replace constant??");
983
984 OldC->uncheckedReplaceAllUsesWith(New);
985 OldC->destroyConstant(); // This constant is now dead, destroy it.
986 }
987 };
988}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000989
Chris Lattner98fa07b2003-05-23 20:03:32 +0000990static ValueMap<std::vector<Constant*>, StructType,
991 ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000992
Chris Lattner015e8212004-02-15 04:14:47 +0000993Constant *ConstantStruct::get(const StructType *Ty,
994 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +0000995 // Create a ConstantAggregateZero value if all elements are zeros...
996 for (unsigned i = 0, e = V.size(); i != e; ++i)
997 if (!V[i]->isNullValue())
998 return StructConstants.getOrCreate(Ty, V);
999
1000 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001001}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001002
Chris Lattnerd7a73302001-10-13 06:57:33 +00001003// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001004//
Chris Lattner3462ae32001-12-03 22:26:30 +00001005void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +00001006 StructConstants.remove(this);
1007 destroyConstantImpl();
1008}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001009
Chris Lattner3462ae32001-12-03 22:26:30 +00001010//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001011//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001012
Chris Lattner189d19f2003-11-21 20:23:48 +00001013namespace llvm {
1014 // ConstantPointerNull does not take extra "value" argument...
1015 template<class ValType>
1016 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1017 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1018 return new ConstantPointerNull(Ty);
1019 }
1020 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001021
Chris Lattner189d19f2003-11-21 20:23:48 +00001022 template<>
1023 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1024 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1025 // Make everyone now use a constant of the new type...
1026 Constant *New = ConstantPointerNull::get(NewTy);
1027 assert(New != OldC && "Didn't replace constant??");
1028 OldC->uncheckedReplaceAllUsesWith(New);
1029 OldC->destroyConstant(); // This constant is now dead, destroy it.
1030 }
1031 };
1032}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001033
Chris Lattner98fa07b2003-05-23 20:03:32 +00001034static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001035
Chris Lattner3462ae32001-12-03 22:26:30 +00001036ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner98fa07b2003-05-23 20:03:32 +00001037 return NullPtrConstants.getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001038}
1039
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001040// destroyConstant - Remove the constant from the constant table...
1041//
1042void ConstantPointerNull::destroyConstant() {
1043 NullPtrConstants.remove(this);
1044 destroyConstantImpl();
1045}
1046
1047
Chris Lattner3462ae32001-12-03 22:26:30 +00001048//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +00001049//
Chris Lattner3462ae32001-12-03 22:26:30 +00001050ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +00001051 assert(GV->getParent() && "Global Value must be attached to a module!");
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001052
Chris Lattnerd7a73302001-10-13 06:57:33 +00001053 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +00001054 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001055}
1056
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001057// destroyConstant - Remove the constant from the constant table...
1058//
1059void ConstantPointerRef::destroyConstant() {
1060 getValue()->getParent()->destroyConstantPointerRef(this);
1061 destroyConstantImpl();
1062}
1063
1064
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001065//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001066//
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001067typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Chris Lattner98fa07b2003-05-23 20:03:32 +00001068
Chris Lattner189d19f2003-11-21 20:23:48 +00001069namespace llvm {
1070 template<>
1071 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
1072 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
1073 if (V.first == Instruction::Cast)
1074 return new ConstantExpr(Instruction::Cast, V.second[0], Ty);
1075 if ((V.first >= Instruction::BinaryOpsBegin &&
1076 V.first < Instruction::BinaryOpsEnd) ||
1077 V.first == Instruction::Shl || V.first == Instruction::Shr)
1078 return new ConstantExpr(V.first, V.second[0], V.second[1]);
Chris Lattnerb7897ce2004-03-30 22:51:03 +00001079 if (V.first == Instruction::Select)
1080 return new ConstantExpr(V.second[0], V.second[1], V.second[2]);
Chris Lattner189d19f2003-11-21 20:23:48 +00001081
1082 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
1083
1084 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
1085 return new ConstantExpr(V.second[0], IdxList, Ty);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001086 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001087 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001088
Chris Lattner189d19f2003-11-21 20:23:48 +00001089 template<>
1090 struct ConvertConstantType<ConstantExpr, Type> {
1091 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1092 Constant *New;
1093 switch (OldC->getOpcode()) {
1094 case Instruction::Cast:
1095 New = ConstantExpr::getCast(OldC->getOperand(0), NewTy);
1096 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001097 case Instruction::Select:
1098 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1099 OldC->getOperand(1),
1100 OldC->getOperand(2));
1101 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001102 case Instruction::Shl:
1103 case Instruction::Shr:
1104 New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
1105 OldC->getOperand(0), OldC->getOperand(1));
1106 break;
1107 default:
1108 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
1109 OldC->getOpcode() < Instruction::BinaryOpsEnd);
1110 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1111 OldC->getOperand(1));
1112 break;
1113 case Instruction::GetElementPtr:
1114 // Make everyone now use a constant of the new type...
1115 std::vector<Constant*> C;
1116 for (unsigned i = 1, e = OldC->getNumOperands(); i != e; ++i)
1117 C.push_back(cast<Constant>(OldC->getOperand(i)));
1118 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), C);
1119 break;
1120 }
1121
1122 assert(New != OldC && "Didn't replace constant??");
1123 OldC->uncheckedReplaceAllUsesWith(New);
1124 OldC->destroyConstant(); // This constant is now dead, destroy it.
1125 }
1126 };
1127} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001128
1129
Chris Lattner98fa07b2003-05-23 20:03:32 +00001130static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001131
Chris Lattner46b3d302003-04-16 22:40:51 +00001132Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001133 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1134
Chris Lattneracdbe712003-04-17 19:24:48 +00001135 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
1136 return FC; // Fold a few common cases...
1137
Vikram S. Adve4c485332002-07-15 18:19:33 +00001138 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001139 std::vector<Constant*> argVec(1, C);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001140 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
1141 return ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001142}
Chris Lattnerd7a73302001-10-13 06:57:33 +00001143
Chris Lattnerdd284742004-04-04 23:20:30 +00001144Constant *ConstantExpr::getSignExtend(Constant *C, const Type *Ty) {
1145 assert(C->getType()->isInteger() && Ty->isInteger() &&
1146 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1147 "This is an illegal sign extension!");
1148 C = ConstantExpr::getCast(C, C->getType()->getSignedVersion());
1149 return ConstantExpr::getCast(C, Ty);
1150}
1151
1152Constant *ConstantExpr::getZeroExtend(Constant *C, const Type *Ty) {
1153 assert(C->getType()->isInteger() && Ty->isInteger() &&
1154 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1155 "This is an illegal zero extension!");
1156 C = ConstantExpr::getCast(C, C->getType()->getUnsignedVersion());
1157 return ConstantExpr::getCast(C, Ty);
1158}
1159
Chris Lattnerb50d1352003-10-05 00:17:43 +00001160Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
1161 Constant *C1, Constant *C2) {
Chris Lattner5645e8a2004-01-12 19:04:55 +00001162 if (Opcode == Instruction::Shl || Opcode == Instruction::Shr)
1163 return getShiftTy(ReqTy, Opcode, C1, C2);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001164 // Check the operands for consistency first
1165 assert((Opcode >= Instruction::BinaryOpsBegin &&
1166 Opcode < Instruction::BinaryOpsEnd) &&
1167 "Invalid opcode in binary constant expression");
1168 assert(C1->getType() == C2->getType() &&
1169 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001170
1171 if (ReqTy == C1->getType())
1172 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1173 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001174
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001175 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001176 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001177 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001178}
1179
Chris Lattner6e415c02004-03-12 05:54:04 +00001180Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
1181 Constant *V1, Constant *V2) {
1182 assert(C->getType() == Type::BoolTy && "Select condition must be bool!");
1183 assert(V1->getType() == V2->getType() && "Select value types must match!");
1184 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
1185
1186 if (ReqTy == V1->getType())
1187 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1188 return SC; // Fold common cases
1189
1190 std::vector<Constant*> argVec(3, C);
1191 argVec[1] = V1;
1192 argVec[2] = V2;
1193 ExprMapKeyType Key = std::make_pair(Instruction::Select, argVec);
1194 return ExprConstants.getOrCreate(ReqTy, Key);
1195}
1196
Chris Lattner9eb2b522004-01-12 19:12:58 +00001197/// getShiftTy - Return a shift left or shift right constant expr
Chris Lattnerb50d1352003-10-05 00:17:43 +00001198Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
1199 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001200 // Check the operands for consistency first
1201 assert((Opcode == Instruction::Shl ||
1202 Opcode == Instruction::Shr) &&
1203 "Invalid opcode in binary constant expression");
1204 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
1205 "Invalid operand types for Shift constant expr!");
1206
Chris Lattner0bba7712004-01-12 20:40:42 +00001207 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001208 return FC; // Fold a few common cases...
1209
1210 // Look up the constant in the table first to ensure uniqueness
1211 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001212 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001213 return ExprConstants.getOrCreate(ReqTy, Key);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001214}
1215
1216
Chris Lattnerb50d1352003-10-05 00:17:43 +00001217Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
1218 const std::vector<Constant*> &IdxList) {
Chris Lattner04b60fe2004-02-16 20:46:13 +00001219 assert(GetElementPtrInst::getIndexedType(C->getType(),
1220 std::vector<Value*>(IdxList.begin(), IdxList.end()), true) &&
1221 "GEP indices invalid!");
1222
Chris Lattneracdbe712003-04-17 19:24:48 +00001223 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
1224 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001225
Chris Lattnerb50d1352003-10-05 00:17:43 +00001226 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001227 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001228 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001229 std::vector<Constant*> argVec(1, C);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +00001230 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001231 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001232 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001233}
1234
Chris Lattnerb50d1352003-10-05 00:17:43 +00001235Constant *ConstantExpr::getGetElementPtr(Constant *C,
1236 const std::vector<Constant*> &IdxList){
1237 // Get the result type of the getelementptr!
1238 std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
1239
1240 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
1241 true);
1242 assert(Ty && "GEP indices invalid!");
1243 return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
1244}
1245
1246
Vikram S. Adve4c485332002-07-15 18:19:33 +00001247// destroyConstant - Remove the constant from the constant table...
1248//
1249void ConstantExpr::destroyConstant() {
1250 ExprConstants.remove(this);
1251 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001252}
1253
Chris Lattner3cd8c562002-07-30 18:54:25 +00001254const char *ConstantExpr::getOpcodeName() const {
1255 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001256}