blob: 21f19b98b11191703f695a48b9189693017d4974 [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) {
316 return get(Instruction::Sub, getNullValue(C->getType()), C);
317}
318Constant *ConstantExpr::getNot(Constant *C) {
319 assert(isa<ConstantIntegral>(C) && "Cannot NOT a nonintegral type!");
320 return get(Instruction::Xor, C,
321 ConstantIntegral::getAllOnesValue(C->getType()));
322}
323Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
324 return get(Instruction::Add, C1, C2);
325}
326Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
327 return get(Instruction::Sub, C1, C2);
328}
329Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
330 return get(Instruction::Mul, C1, C2);
331}
332Constant *ConstantExpr::getDiv(Constant *C1, Constant *C2) {
333 return get(Instruction::Div, C1, C2);
334}
335Constant *ConstantExpr::getRem(Constant *C1, Constant *C2) {
336 return get(Instruction::Rem, C1, C2);
337}
338Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
339 return get(Instruction::And, C1, C2);
340}
341Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
342 return get(Instruction::Or, C1, C2);
343}
344Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
345 return get(Instruction::Xor, C1, C2);
346}
347Constant *ConstantExpr::getSetEQ(Constant *C1, Constant *C2) {
348 return get(Instruction::SetEQ, C1, C2);
349}
350Constant *ConstantExpr::getSetNE(Constant *C1, Constant *C2) {
351 return get(Instruction::SetNE, C1, C2);
352}
353Constant *ConstantExpr::getSetLT(Constant *C1, Constant *C2) {
354 return get(Instruction::SetLT, C1, C2);
355}
356Constant *ConstantExpr::getSetGT(Constant *C1, Constant *C2) {
357 return get(Instruction::SetGT, C1, C2);
358}
359Constant *ConstantExpr::getSetLE(Constant *C1, Constant *C2) {
360 return get(Instruction::SetLE, C1, C2);
361}
362Constant *ConstantExpr::getSetGE(Constant *C1, Constant *C2) {
363 return get(Instruction::SetGE, C1, C2);
364}
365Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
366 return get(Instruction::Shl, C1, C2);
367}
368Constant *ConstantExpr::getShr(Constant *C1, Constant *C2) {
369 return get(Instruction::Shr, C1, C2);
370}
371
372
Chris Lattner60e0dd72001-10-03 06:12:09 +0000373
Chris Lattner2f7c9632001-06-06 20:29:01 +0000374
375//===----------------------------------------------------------------------===//
Chris Lattnerd7a73302001-10-13 06:57:33 +0000376// classof implementations
377
Chris Lattnerb1585a92002-08-13 17:50:20 +0000378bool ConstantIntegral::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000379 return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
Chris Lattner41e99a02002-08-12 21:21:21 +0000380}
381
Chris Lattner3462ae32001-12-03 22:26:30 +0000382bool ConstantInt::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000383 return CPV->getType()->isInteger() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000384}
Chris Lattner3462ae32001-12-03 22:26:30 +0000385bool ConstantSInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000386 return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000387}
Chris Lattner3462ae32001-12-03 22:26:30 +0000388bool ConstantUInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000389 return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000390}
Chris Lattner3462ae32001-12-03 22:26:30 +0000391bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000392 const Type *Ty = CPV->getType();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000393 return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000394 !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000395}
Chris Lattner9fba3da2004-02-15 05:53:04 +0000396bool ConstantAggregateZero::classof(const Constant *CPV) {
397 return (isa<ArrayType>(CPV->getType()) || isa<StructType>(CPV->getType())) &&
398 CPV->isNullValue();
399}
Chris Lattner3462ae32001-12-03 22:26:30 +0000400bool ConstantArray::classof(const Constant *CPV) {
Chris Lattner9fba3da2004-02-15 05:53:04 +0000401 return isa<ArrayType>(CPV->getType()) && !CPV->isNullValue();
Chris Lattnerd7a73302001-10-13 06:57:33 +0000402}
Chris Lattner3462ae32001-12-03 22:26:30 +0000403bool ConstantStruct::classof(const Constant *CPV) {
Chris Lattner9fba3da2004-02-15 05:53:04 +0000404 return isa<StructType>(CPV->getType()) && !CPV->isNullValue();
Chris Lattnerd7a73302001-10-13 06:57:33 +0000405}
Chris Lattnere120a732003-11-17 19:47:21 +0000406
407bool ConstantPointerNull::classof(const Constant *CPV) {
408 return isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV) &&
409 CPV->getNumOperands() == 0;
410}
411
412bool ConstantPointerRef::classof(const Constant *CPV) {
413 return isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV) &&
414 CPV->getNumOperands() == 1;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000415}
416
417
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000418
Chris Lattner2f7c9632001-06-06 20:29:01 +0000419//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000420// isValueValidForType implementations
421
Chris Lattner3462ae32001-12-03 22:26:30 +0000422bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000423 switch (Ty->getPrimitiveID()) {
424 default:
425 return false; // These can't be represented as integers!!!
426
427 // Signed types...
428 case Type::SByteTyID:
429 return (Val <= INT8_MAX && Val >= INT8_MIN);
430 case Type::ShortTyID:
431 return (Val <= INT16_MAX && Val >= INT16_MIN);
432 case Type::IntTyID:
433 return (Val <= INT32_MAX && Val >= INT32_MIN);
434 case Type::LongTyID:
435 return true; // This is the largest type...
436 }
437 assert(0 && "WTF?");
438 return false;
439}
440
Chris Lattner3462ae32001-12-03 22:26:30 +0000441bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000442 switch (Ty->getPrimitiveID()) {
443 default:
444 return false; // These can't be represented as integers!!!
445
446 // Unsigned types...
447 case Type::UByteTyID:
448 return (Val <= UINT8_MAX);
449 case Type::UShortTyID:
450 return (Val <= UINT16_MAX);
451 case Type::UIntTyID:
452 return (Val <= UINT32_MAX);
453 case Type::ULongTyID:
454 return true; // This is the largest type...
455 }
456 assert(0 && "WTF?");
457 return false;
458}
459
Chris Lattner3462ae32001-12-03 22:26:30 +0000460bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000461 switch (Ty->getPrimitiveID()) {
462 default:
463 return false; // These can't be represented as floating point!
464
465 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000466 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000467 case Type::DoubleTyID:
468 return true; // This is the largest type...
469 }
470};
Chris Lattner9655e542001-07-20 19:16:02 +0000471
Chris Lattner49d855c2001-09-07 16:46:31 +0000472//===----------------------------------------------------------------------===//
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000473// replaceUsesOfWithOnConstant implementations
474
Chris Lattnerc27038d2003-08-29 05:36:46 +0000475void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
476 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000477 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
478
479 std::vector<Constant*> Values;
480 Values.reserve(getValues().size()); // Build replacement array...
481 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
482 Constant *Val = cast<Constant>(getValues()[i]);
483 if (Val == From) Val = cast<Constant>(To);
484 Values.push_back(Val);
485 }
486
Chris Lattnerc75bf522004-02-15 04:05:58 +0000487 Constant *Replacement = ConstantArray::get(getType(), Values);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000488 assert(Replacement != this && "I didn't contain From!");
489
490 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000491 if (DisableChecking)
492 uncheckedReplaceAllUsesWith(Replacement);
493 else
494 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000495
496 // Delete the old constant!
497 destroyConstant();
498}
499
Chris Lattnerc27038d2003-08-29 05:36:46 +0000500void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
501 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000502 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
503
504 std::vector<Constant*> Values;
505 Values.reserve(getValues().size());
506 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
507 Constant *Val = cast<Constant>(getValues()[i]);
508 if (Val == From) Val = cast<Constant>(To);
509 Values.push_back(Val);
510 }
511
Chris Lattner37a716f2004-02-15 04:07:32 +0000512 Constant *Replacement = ConstantStruct::get(getType(), Values);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000513 assert(Replacement != this && "I didn't contain From!");
514
515 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000516 if (DisableChecking)
517 uncheckedReplaceAllUsesWith(Replacement);
518 else
519 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000520
521 // Delete the old constant!
522 destroyConstant();
523}
524
Chris Lattnerc27038d2003-08-29 05:36:46 +0000525void ConstantPointerRef::replaceUsesOfWithOnConstant(Value *From, Value *To,
526 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000527 if (isa<GlobalValue>(To)) {
528 assert(From == getOperand(0) && "Doesn't contain from!");
529 ConstantPointerRef *Replacement =
530 ConstantPointerRef::get(cast<GlobalValue>(To));
531
532 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000533 if (DisableChecking)
534 uncheckedReplaceAllUsesWith(Replacement);
535 else
536 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000537
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000538 } else {
539 // Just replace ourselves with the To value specified.
Chris Lattnerc27038d2003-08-29 05:36:46 +0000540 if (DisableChecking)
541 uncheckedReplaceAllUsesWith(To);
542 else
543 replaceAllUsesWith(To);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000544 }
Chris Lattnerc27038d2003-08-29 05:36:46 +0000545
546 // Delete the old constant!
547 destroyConstant();
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000548}
549
Chris Lattnerc27038d2003-08-29 05:36:46 +0000550void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
551 bool DisableChecking) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000552 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
553 Constant *To = cast<Constant>(ToV);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000554
Chris Lattner46b3d302003-04-16 22:40:51 +0000555 Constant *Replacement = 0;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000556 if (getOpcode() == Instruction::GetElementPtr) {
557 std::vector<Constant*> Indices;
Chris Lattner55ed6562003-05-14 17:51:05 +0000558 Constant *Pointer = getOperand(0);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000559 Indices.reserve(getNumOperands()-1);
Chris Lattner55ed6562003-05-14 17:51:05 +0000560 if (Pointer == From) Pointer = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000561
562 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000563 Constant *Val = getOperand(i);
564 if (Val == From) Val = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000565 Indices.push_back(Val);
566 }
567 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
568 } else if (getOpcode() == Instruction::Cast) {
569 assert(getOperand(0) == From && "Cast only has one use!");
Chris Lattner55ed6562003-05-14 17:51:05 +0000570 Replacement = ConstantExpr::getCast(To, getType());
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000571 } else if (getNumOperands() == 2) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000572 Constant *C1 = getOperand(0);
573 Constant *C2 = getOperand(1);
574 if (C1 == From) C1 = To;
575 if (C2 == From) C2 = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000576 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
577 } else {
578 assert(0 && "Unknown ConstantExpr type!");
579 return;
580 }
581
582 assert(Replacement != this && "I didn't contain From!");
583
584 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000585 if (DisableChecking)
586 uncheckedReplaceAllUsesWith(Replacement);
587 else
588 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000589
590 // Delete the old constant!
591 destroyConstant();
592}
593
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000594//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000595// Factory Function Implementation
596
Chris Lattner98fa07b2003-05-23 20:03:32 +0000597// ConstantCreator - A class that is used to create constants by
598// ValueMap*. This class should be partially specialized if there is
599// something strange that needs to be done to interface to the ctor for the
600// constant.
601//
Chris Lattner189d19f2003-11-21 20:23:48 +0000602namespace llvm {
603 template<class ConstantClass, class TypeClass, class ValType>
604 struct ConstantCreator {
605 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
606 return new ConstantClass(Ty, V);
607 }
608 };
609
610 template<class ConstantClass, class TypeClass>
611 struct ConvertConstantType {
612 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
613 assert(0 && "This type cannot be converted!\n");
614 abort();
615 }
616 };
617}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000618
Chris Lattner98fa07b2003-05-23 20:03:32 +0000619namespace {
620 template<class ValType, class TypeClass, class ConstantClass>
Chris Lattnerb50d1352003-10-05 00:17:43 +0000621 class ValueMap : public AbstractTypeUser {
622 typedef std::pair<const TypeClass*, ValType> MapKey;
623 typedef std::map<MapKey, ConstantClass *> MapTy;
624 typedef typename MapTy::iterator MapIterator;
625 MapTy Map;
626
627 typedef std::map<const TypeClass*, MapIterator> AbstractTypeMapTy;
628 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000629 public:
630 // getOrCreate - Return the specified constant from the map, creating it if
631 // necessary.
632 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000633 MapKey Lookup(Ty, V);
634 MapIterator I = Map.lower_bound(Lookup);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000635 if (I != Map.end() && I->first == Lookup)
636 return I->second; // Is it in the map?
637
638 // If no preexisting value, create one now...
639 ConstantClass *Result =
640 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
641
Chris Lattnerb50d1352003-10-05 00:17:43 +0000642
643 /// FIXME: why does this assert fail when loading 176.gcc?
644 //assert(Result->getType() == Ty && "Type specified is not correct!");
645 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
646
647 // If the type of the constant is abstract, make sure that an entry exists
648 // for it in the AbstractTypeMap.
649 if (Ty->isAbstract()) {
650 typename AbstractTypeMapTy::iterator TI =
651 AbstractTypeMap.lower_bound(Ty);
652
653 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
654 // Add ourselves to the ATU list of the type.
655 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
656
657 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
658 }
659 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000660 return Result;
661 }
662
663 void remove(ConstantClass *CP) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000664 // FIXME: This should not use a linear scan. If this gets to be a
665 // performance problem, someone should look at this.
666 MapIterator I = Map.begin();
667 for (MapIterator E = Map.end(); I != E && I->second != CP; ++I)
668 /* empty */;
669
670 assert(I != Map.end() && "Constant not found in constant table!");
671
672 // Now that we found the entry, make sure this isn't the entry that
673 // the AbstractTypeMap points to.
674 const TypeClass *Ty = I->first.first;
675 if (Ty->isAbstract()) {
676 assert(AbstractTypeMap.count(Ty) &&
677 "Abstract type not in AbstractTypeMap?");
678 MapIterator &ATMEntryIt = AbstractTypeMap[Ty];
679 if (ATMEntryIt == I) {
680 // Yes, we are removing the representative entry for this type.
681 // See if there are any other entries of the same type.
682 MapIterator TmpIt = ATMEntryIt;
683
684 // First check the entry before this one...
685 if (TmpIt != Map.begin()) {
686 --TmpIt;
687 if (TmpIt->first.first != Ty) // Not the same type, move back...
688 ++TmpIt;
689 }
690
691 // If we didn't find the same type, try to move forward...
692 if (TmpIt == ATMEntryIt) {
693 ++TmpIt;
694 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
695 --TmpIt; // No entry afterwards with the same type
696 }
697
698 // If there is another entry in the map of the same abstract type,
699 // update the AbstractTypeMap entry now.
700 if (TmpIt != ATMEntryIt) {
701 ATMEntryIt = TmpIt;
702 } else {
703 // Otherwise, we are removing the last instance of this type
704 // from the table. Remove from the ATM, and from user list.
705 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
706 AbstractTypeMap.erase(Ty);
707 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000708 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000709 }
710
711 Map.erase(I);
712 }
713
714 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
715 typename AbstractTypeMapTy::iterator I =
716 AbstractTypeMap.find(cast<TypeClass>(OldTy));
717
718 assert(I != AbstractTypeMap.end() &&
719 "Abstract type not in AbstractTypeMap?");
720
721 // Convert a constant at a time until the last one is gone. The last one
722 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
723 // eliminated eventually.
724 do {
725 ConvertConstantType<ConstantClass,
726 TypeClass>::convert(I->second->second,
727 cast<TypeClass>(NewTy));
728
729 I = AbstractTypeMap.find(cast<TypeClass>(OldTy));
730 } while (I != AbstractTypeMap.end());
731 }
732
733 // If the type became concrete without being refined to any other existing
734 // type, we just remove ourselves from the ATU list.
735 void typeBecameConcrete(const DerivedType *AbsTy) {
736 AbsTy->removeAbstractTypeUser(this);
737 }
738
739 void dump() const {
740 std::cerr << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +0000741 }
742 };
743}
744
745
746
Chris Lattner3462ae32001-12-03 22:26:30 +0000747//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000748//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000749static ValueMap< int64_t, Type, ConstantSInt> SIntConstants;
750static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000751
Chris Lattner3462ae32001-12-03 22:26:30 +0000752ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000753 return SIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000754}
755
Chris Lattner3462ae32001-12-03 22:26:30 +0000756ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000757 return UIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000758}
759
Chris Lattner3462ae32001-12-03 22:26:30 +0000760ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000761 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000762 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
763 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000764}
765
Chris Lattner3462ae32001-12-03 22:26:30 +0000766//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000767//
Chris Lattnerac80ea42004-02-01 22:49:04 +0000768namespace llvm {
769 template<>
770 struct ConstantCreator<ConstantFP, Type, uint64_t> {
771 static ConstantFP *create(const Type *Ty, uint64_t V) {
772 assert(Ty == Type::DoubleTy);
773 union {
774 double F;
775 uint64_t I;
776 } T;
777 T.I = V;
778 return new ConstantFP(Ty, T.F);
779 }
780 };
781 template<>
782 struct ConstantCreator<ConstantFP, Type, uint32_t> {
783 static ConstantFP *create(const Type *Ty, uint32_t V) {
784 assert(Ty == Type::FloatTy);
785 union {
786 float F;
787 uint32_t I;
788 } T;
789 T.I = V;
790 return new ConstantFP(Ty, T.F);
791 }
792 };
793}
794
795static ValueMap<uint64_t, Type, ConstantFP> DoubleConstants;
796static ValueMap<uint32_t, Type, ConstantFP> FloatConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000797
Chris Lattner3462ae32001-12-03 22:26:30 +0000798ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner241ed4c2004-01-23 00:55:21 +0000799 if (Ty == Type::FloatTy) {
800 // Force the value through memory to normalize it.
Chris Lattnerac80ea42004-02-01 22:49:04 +0000801 union {
802 float F;
803 uint32_t I;
804 } T;
805 T.F = (float)V;
806 return FloatConstants.getOrCreate(Ty, T.I);
807 } else {
808 assert(Ty == Type::DoubleTy);
809 union {
810 double F;
811 uint64_t I;
812 } T;
813 T.F = V;
814 return DoubleConstants.getOrCreate(Ty, T.I);
Chris Lattner241ed4c2004-01-23 00:55:21 +0000815 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000816}
817
Chris Lattner9fba3da2004-02-15 05:53:04 +0000818//---- ConstantAggregateZero::get() implementation...
819//
820namespace llvm {
821 // ConstantAggregateZero does not take extra "value" argument...
822 template<class ValType>
823 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
824 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
825 return new ConstantAggregateZero(Ty);
826 }
827 };
828
829 template<>
830 struct ConvertConstantType<ConstantAggregateZero, Type> {
831 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
832 // Make everyone now use a constant of the new type...
833 Constant *New = ConstantAggregateZero::get(NewTy);
834 assert(New != OldC && "Didn't replace constant??");
835 OldC->uncheckedReplaceAllUsesWith(New);
836 OldC->destroyConstant(); // This constant is now dead, destroy it.
837 }
838 };
839}
840
841static ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
842
843Constant *ConstantAggregateZero::get(const Type *Ty) {
844 return AggZeroConstants.getOrCreate(Ty, 0);
845}
846
847// destroyConstant - Remove the constant from the constant table...
848//
849void ConstantAggregateZero::destroyConstant() {
850 AggZeroConstants.remove(this);
851 destroyConstantImpl();
852}
853
854void ConstantAggregateZero::replaceUsesOfWithOnConstant(Value *From, Value *To,
855 bool DisableChecking) {
856 assert(0 && "No uses!");
857 abort();
858}
859
860
861
Chris Lattner3462ae32001-12-03 22:26:30 +0000862//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000863//
Chris Lattner189d19f2003-11-21 20:23:48 +0000864namespace llvm {
865 template<>
866 struct ConvertConstantType<ConstantArray, ArrayType> {
867 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
868 // Make everyone now use a constant of the new type...
869 std::vector<Constant*> C;
870 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
871 C.push_back(cast<Constant>(OldC->getOperand(i)));
872 Constant *New = ConstantArray::get(NewTy, C);
873 assert(New != OldC && "Didn't replace constant??");
874 OldC->uncheckedReplaceAllUsesWith(New);
875 OldC->destroyConstant(); // This constant is now dead, destroy it.
876 }
877 };
878}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000879
Chris Lattner98fa07b2003-05-23 20:03:32 +0000880static ValueMap<std::vector<Constant*>, ArrayType,
881 ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000882
Chris Lattner015e8212004-02-15 04:14:47 +0000883Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +0000884 const std::vector<Constant*> &V) {
885 // If this is an all-zero array, return a ConstantAggregateZero object
886 if (!V.empty()) {
887 Constant *C = V[0];
888 if (!C->isNullValue())
889 return ArrayConstants.getOrCreate(Ty, V);
890 for (unsigned i = 1, e = V.size(); i != e; ++i)
891 if (V[i] != C)
892 return ArrayConstants.getOrCreate(Ty, V);
893 }
894 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000895}
896
Chris Lattner98fa07b2003-05-23 20:03:32 +0000897// destroyConstant - Remove the constant from the constant table...
898//
899void ConstantArray::destroyConstant() {
900 ArrayConstants.remove(this);
901 destroyConstantImpl();
902}
903
Chris Lattner3462ae32001-12-03 22:26:30 +0000904// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000905// contain the specified string. A null terminator is added to the specified
906// string so that it may be used in a natural way...
907//
Chris Lattner015e8212004-02-15 04:14:47 +0000908Constant *ConstantArray::get(const std::string &Str) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000909 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000910
911 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000912 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000913
914 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000915 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000916
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000917 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000918 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000919}
920
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000921/// isString - This method returns true if the array is an array of sbyte or
922/// ubyte, and if the elements of the array are all ConstantInt's.
923bool ConstantArray::isString() const {
924 // Check the element type for sbyte or ubyte...
Chris Lattnere8701f62004-01-14 17:51:53 +0000925 if (getType()->getElementType() != Type::UByteTy &&
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000926 getType()->getElementType() != Type::SByteTy)
927 return false;
928 // Check the elements to make sure they are all integers, not constant
929 // expressions.
930 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
931 if (!isa<ConstantInt>(getOperand(i)))
932 return false;
933 return true;
934}
935
Chris Lattner81fabb02002-08-26 17:53:56 +0000936// getAsString - If the sub-element type of this array is either sbyte or ubyte,
937// then this method converts the array to an std::string and returns it.
938// Otherwise, it asserts out.
939//
940std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000941 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +0000942 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +0000943 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
944 Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue();
Chris Lattner81fabb02002-08-26 17:53:56 +0000945 return Result;
946}
947
948
Chris Lattner3462ae32001-12-03 22:26:30 +0000949//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000950//
Chris Lattnerb50d1352003-10-05 00:17:43 +0000951
Chris Lattner189d19f2003-11-21 20:23:48 +0000952namespace llvm {
953 template<>
954 struct ConvertConstantType<ConstantStruct, StructType> {
955 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
956 // Make everyone now use a constant of the new type...
957 std::vector<Constant*> C;
958 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
959 C.push_back(cast<Constant>(OldC->getOperand(i)));
960 Constant *New = ConstantStruct::get(NewTy, C);
961 assert(New != OldC && "Didn't replace constant??");
962
963 OldC->uncheckedReplaceAllUsesWith(New);
964 OldC->destroyConstant(); // This constant is now dead, destroy it.
965 }
966 };
967}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000968
Chris Lattner98fa07b2003-05-23 20:03:32 +0000969static ValueMap<std::vector<Constant*>, StructType,
970 ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000971
Chris Lattner015e8212004-02-15 04:14:47 +0000972Constant *ConstantStruct::get(const StructType *Ty,
973 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +0000974 // Create a ConstantAggregateZero value if all elements are zeros...
975 for (unsigned i = 0, e = V.size(); i != e; ++i)
976 if (!V[i]->isNullValue())
977 return StructConstants.getOrCreate(Ty, V);
978
979 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000980}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000981
Chris Lattnerd7a73302001-10-13 06:57:33 +0000982// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000983//
Chris Lattner3462ae32001-12-03 22:26:30 +0000984void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000985 StructConstants.remove(this);
986 destroyConstantImpl();
987}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000988
Chris Lattner3462ae32001-12-03 22:26:30 +0000989//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000990//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000991
Chris Lattner189d19f2003-11-21 20:23:48 +0000992namespace llvm {
993 // ConstantPointerNull does not take extra "value" argument...
994 template<class ValType>
995 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
996 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
997 return new ConstantPointerNull(Ty);
998 }
999 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001000
Chris Lattner189d19f2003-11-21 20:23:48 +00001001 template<>
1002 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1003 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1004 // Make everyone now use a constant of the new type...
1005 Constant *New = ConstantPointerNull::get(NewTy);
1006 assert(New != OldC && "Didn't replace constant??");
1007 OldC->uncheckedReplaceAllUsesWith(New);
1008 OldC->destroyConstant(); // This constant is now dead, destroy it.
1009 }
1010 };
1011}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001012
Chris Lattner98fa07b2003-05-23 20:03:32 +00001013static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001014
Chris Lattner3462ae32001-12-03 22:26:30 +00001015ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner98fa07b2003-05-23 20:03:32 +00001016 return NullPtrConstants.getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001017}
1018
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001019// destroyConstant - Remove the constant from the constant table...
1020//
1021void ConstantPointerNull::destroyConstant() {
1022 NullPtrConstants.remove(this);
1023 destroyConstantImpl();
1024}
1025
1026
Chris Lattner3462ae32001-12-03 22:26:30 +00001027//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +00001028//
Chris Lattner3462ae32001-12-03 22:26:30 +00001029ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +00001030 assert(GV->getParent() && "Global Value must be attached to a module!");
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001031
Chris Lattnerd7a73302001-10-13 06:57:33 +00001032 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +00001033 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001034}
1035
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001036// destroyConstant - Remove the constant from the constant table...
1037//
1038void ConstantPointerRef::destroyConstant() {
1039 getValue()->getParent()->destroyConstantPointerRef(this);
1040 destroyConstantImpl();
1041}
1042
1043
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001044//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001045//
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001046typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Chris Lattner98fa07b2003-05-23 20:03:32 +00001047
Chris Lattner189d19f2003-11-21 20:23:48 +00001048namespace llvm {
1049 template<>
1050 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
1051 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
1052 if (V.first == Instruction::Cast)
1053 return new ConstantExpr(Instruction::Cast, V.second[0], Ty);
1054 if ((V.first >= Instruction::BinaryOpsBegin &&
1055 V.first < Instruction::BinaryOpsEnd) ||
1056 V.first == Instruction::Shl || V.first == Instruction::Shr)
1057 return new ConstantExpr(V.first, V.second[0], V.second[1]);
1058
1059 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
1060
1061 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
1062 return new ConstantExpr(V.second[0], IdxList, Ty);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001063 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001064 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001065
Chris Lattner189d19f2003-11-21 20:23:48 +00001066 template<>
1067 struct ConvertConstantType<ConstantExpr, Type> {
1068 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1069 Constant *New;
1070 switch (OldC->getOpcode()) {
1071 case Instruction::Cast:
1072 New = ConstantExpr::getCast(OldC->getOperand(0), NewTy);
1073 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001074 case Instruction::Select:
1075 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1076 OldC->getOperand(1),
1077 OldC->getOperand(2));
1078 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001079 case Instruction::Shl:
1080 case Instruction::Shr:
1081 New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
1082 OldC->getOperand(0), OldC->getOperand(1));
1083 break;
1084 default:
1085 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
1086 OldC->getOpcode() < Instruction::BinaryOpsEnd);
1087 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1088 OldC->getOperand(1));
1089 break;
1090 case Instruction::GetElementPtr:
1091 // Make everyone now use a constant of the new type...
1092 std::vector<Constant*> C;
1093 for (unsigned i = 1, e = OldC->getNumOperands(); i != e; ++i)
1094 C.push_back(cast<Constant>(OldC->getOperand(i)));
1095 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), C);
1096 break;
1097 }
1098
1099 assert(New != OldC && "Didn't replace constant??");
1100 OldC->uncheckedReplaceAllUsesWith(New);
1101 OldC->destroyConstant(); // This constant is now dead, destroy it.
1102 }
1103 };
1104} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001105
1106
Chris Lattner98fa07b2003-05-23 20:03:32 +00001107static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001108
Chris Lattner46b3d302003-04-16 22:40:51 +00001109Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001110 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1111
Chris Lattneracdbe712003-04-17 19:24:48 +00001112 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
1113 return FC; // Fold a few common cases...
1114
Vikram S. Adve4c485332002-07-15 18:19:33 +00001115 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001116 std::vector<Constant*> argVec(1, C);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001117 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
1118 return ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001119}
Chris Lattnerd7a73302001-10-13 06:57:33 +00001120
Chris Lattnerb50d1352003-10-05 00:17:43 +00001121Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
1122 Constant *C1, Constant *C2) {
Chris Lattner5645e8a2004-01-12 19:04:55 +00001123 if (Opcode == Instruction::Shl || Opcode == Instruction::Shr)
1124 return getShiftTy(ReqTy, Opcode, C1, C2);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001125 // Check the operands for consistency first
1126 assert((Opcode >= Instruction::BinaryOpsBegin &&
1127 Opcode < Instruction::BinaryOpsEnd) &&
1128 "Invalid opcode in binary constant expression");
1129 assert(C1->getType() == C2->getType() &&
1130 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001131
1132 if (ReqTy == C1->getType())
1133 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1134 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001135
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001136 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001137 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001138 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001139}
1140
Chris Lattner6e415c02004-03-12 05:54:04 +00001141Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
1142 Constant *V1, Constant *V2) {
1143 assert(C->getType() == Type::BoolTy && "Select condition must be bool!");
1144 assert(V1->getType() == V2->getType() && "Select value types must match!");
1145 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
1146
1147 if (ReqTy == V1->getType())
1148 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1149 return SC; // Fold common cases
1150
1151 std::vector<Constant*> argVec(3, C);
1152 argVec[1] = V1;
1153 argVec[2] = V2;
1154 ExprMapKeyType Key = std::make_pair(Instruction::Select, argVec);
1155 return ExprConstants.getOrCreate(ReqTy, Key);
1156}
1157
Chris Lattner9eb2b522004-01-12 19:12:58 +00001158/// getShiftTy - Return a shift left or shift right constant expr
Chris Lattnerb50d1352003-10-05 00:17:43 +00001159Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
1160 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001161 // Check the operands for consistency first
1162 assert((Opcode == Instruction::Shl ||
1163 Opcode == Instruction::Shr) &&
1164 "Invalid opcode in binary constant expression");
1165 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
1166 "Invalid operand types for Shift constant expr!");
1167
Chris Lattner0bba7712004-01-12 20:40:42 +00001168 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001169 return FC; // Fold a few common cases...
1170
1171 // Look up the constant in the table first to ensure uniqueness
1172 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001173 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001174 return ExprConstants.getOrCreate(ReqTy, Key);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001175}
1176
1177
Chris Lattnerb50d1352003-10-05 00:17:43 +00001178Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
1179 const std::vector<Constant*> &IdxList) {
Chris Lattner04b60fe2004-02-16 20:46:13 +00001180 assert(GetElementPtrInst::getIndexedType(C->getType(),
1181 std::vector<Value*>(IdxList.begin(), IdxList.end()), true) &&
1182 "GEP indices invalid!");
1183
Chris Lattneracdbe712003-04-17 19:24:48 +00001184 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
1185 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001186
Chris Lattnerb50d1352003-10-05 00:17:43 +00001187 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001188 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001189 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001190 std::vector<Constant*> argVec(1, C);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +00001191 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001192 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001193 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001194}
1195
Chris Lattnerb50d1352003-10-05 00:17:43 +00001196Constant *ConstantExpr::getGetElementPtr(Constant *C,
1197 const std::vector<Constant*> &IdxList){
1198 // Get the result type of the getelementptr!
1199 std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
1200
1201 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
1202 true);
1203 assert(Ty && "GEP indices invalid!");
1204 return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
1205}
1206
1207
Vikram S. Adve4c485332002-07-15 18:19:33 +00001208// destroyConstant - Remove the constant from the constant table...
1209//
1210void ConstantExpr::destroyConstant() {
1211 ExprConstants.remove(this);
1212 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001213}
1214
Chris Lattner3cd8c562002-07-30 18:54:25 +00001215const char *ConstantExpr::getOpcodeName() const {
1216 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001217}