blob: fdde9d9e23473213c989587bf1b58a31d25a9a15 [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 Lattner60e0dd72001-10-03 06:12:09 +0000312
Chris Lattner2f7c9632001-06-06 20:29:01 +0000313
314//===----------------------------------------------------------------------===//
Chris Lattnerd7a73302001-10-13 06:57:33 +0000315// classof implementations
316
Chris Lattnerb1585a92002-08-13 17:50:20 +0000317bool ConstantIntegral::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000318 return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
Chris Lattner41e99a02002-08-12 21:21:21 +0000319}
320
Chris Lattner3462ae32001-12-03 22:26:30 +0000321bool ConstantInt::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000322 return CPV->getType()->isInteger() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000323}
Chris Lattner3462ae32001-12-03 22:26:30 +0000324bool ConstantSInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000325 return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000326}
Chris Lattner3462ae32001-12-03 22:26:30 +0000327bool ConstantUInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000328 return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000329}
Chris Lattner3462ae32001-12-03 22:26:30 +0000330bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000331 const Type *Ty = CPV->getType();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000332 return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000333 !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000334}
Chris Lattner9fba3da2004-02-15 05:53:04 +0000335bool ConstantAggregateZero::classof(const Constant *CPV) {
336 return (isa<ArrayType>(CPV->getType()) || isa<StructType>(CPV->getType())) &&
337 CPV->isNullValue();
338}
Chris Lattner3462ae32001-12-03 22:26:30 +0000339bool ConstantArray::classof(const Constant *CPV) {
Chris Lattner9fba3da2004-02-15 05:53:04 +0000340 return isa<ArrayType>(CPV->getType()) && !CPV->isNullValue();
Chris Lattnerd7a73302001-10-13 06:57:33 +0000341}
Chris Lattner3462ae32001-12-03 22:26:30 +0000342bool ConstantStruct::classof(const Constant *CPV) {
Chris Lattner9fba3da2004-02-15 05:53:04 +0000343 return isa<StructType>(CPV->getType()) && !CPV->isNullValue();
Chris Lattnerd7a73302001-10-13 06:57:33 +0000344}
Chris Lattnere120a732003-11-17 19:47:21 +0000345
346bool ConstantPointerNull::classof(const Constant *CPV) {
347 return isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV) &&
348 CPV->getNumOperands() == 0;
349}
350
351bool ConstantPointerRef::classof(const Constant *CPV) {
352 return isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV) &&
353 CPV->getNumOperands() == 1;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000354}
355
356
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000357
Chris Lattner2f7c9632001-06-06 20:29:01 +0000358//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000359// isValueValidForType implementations
360
Chris Lattner3462ae32001-12-03 22:26:30 +0000361bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000362 switch (Ty->getPrimitiveID()) {
363 default:
364 return false; // These can't be represented as integers!!!
365
366 // Signed types...
367 case Type::SByteTyID:
368 return (Val <= INT8_MAX && Val >= INT8_MIN);
369 case Type::ShortTyID:
370 return (Val <= INT16_MAX && Val >= INT16_MIN);
371 case Type::IntTyID:
372 return (Val <= INT32_MAX && Val >= INT32_MIN);
373 case Type::LongTyID:
374 return true; // This is the largest type...
375 }
376 assert(0 && "WTF?");
377 return false;
378}
379
Chris Lattner3462ae32001-12-03 22:26:30 +0000380bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000381 switch (Ty->getPrimitiveID()) {
382 default:
383 return false; // These can't be represented as integers!!!
384
385 // Unsigned types...
386 case Type::UByteTyID:
387 return (Val <= UINT8_MAX);
388 case Type::UShortTyID:
389 return (Val <= UINT16_MAX);
390 case Type::UIntTyID:
391 return (Val <= UINT32_MAX);
392 case Type::ULongTyID:
393 return true; // This is the largest type...
394 }
395 assert(0 && "WTF?");
396 return false;
397}
398
Chris Lattner3462ae32001-12-03 22:26:30 +0000399bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000400 switch (Ty->getPrimitiveID()) {
401 default:
402 return false; // These can't be represented as floating point!
403
404 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000405 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000406 case Type::DoubleTyID:
407 return true; // This is the largest type...
408 }
409};
Chris Lattner9655e542001-07-20 19:16:02 +0000410
Chris Lattner49d855c2001-09-07 16:46:31 +0000411//===----------------------------------------------------------------------===//
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000412// replaceUsesOfWithOnConstant implementations
413
Chris Lattnerc27038d2003-08-29 05:36:46 +0000414void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
415 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000416 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
417
418 std::vector<Constant*> Values;
419 Values.reserve(getValues().size()); // Build replacement array...
420 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
421 Constant *Val = cast<Constant>(getValues()[i]);
422 if (Val == From) Val = cast<Constant>(To);
423 Values.push_back(Val);
424 }
425
Chris Lattnerc75bf522004-02-15 04:05:58 +0000426 Constant *Replacement = ConstantArray::get(getType(), Values);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000427 assert(Replacement != this && "I didn't contain From!");
428
429 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000430 if (DisableChecking)
431 uncheckedReplaceAllUsesWith(Replacement);
432 else
433 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000434
435 // Delete the old constant!
436 destroyConstant();
437}
438
Chris Lattnerc27038d2003-08-29 05:36:46 +0000439void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
440 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000441 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
442
443 std::vector<Constant*> Values;
444 Values.reserve(getValues().size());
445 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
446 Constant *Val = cast<Constant>(getValues()[i]);
447 if (Val == From) Val = cast<Constant>(To);
448 Values.push_back(Val);
449 }
450
Chris Lattner37a716f2004-02-15 04:07:32 +0000451 Constant *Replacement = ConstantStruct::get(getType(), Values);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000452 assert(Replacement != this && "I didn't contain From!");
453
454 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000455 if (DisableChecking)
456 uncheckedReplaceAllUsesWith(Replacement);
457 else
458 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000459
460 // Delete the old constant!
461 destroyConstant();
462}
463
Chris Lattnerc27038d2003-08-29 05:36:46 +0000464void ConstantPointerRef::replaceUsesOfWithOnConstant(Value *From, Value *To,
465 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000466 if (isa<GlobalValue>(To)) {
467 assert(From == getOperand(0) && "Doesn't contain from!");
468 ConstantPointerRef *Replacement =
469 ConstantPointerRef::get(cast<GlobalValue>(To));
470
471 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000472 if (DisableChecking)
473 uncheckedReplaceAllUsesWith(Replacement);
474 else
475 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000476
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000477 } else {
478 // Just replace ourselves with the To value specified.
Chris Lattnerc27038d2003-08-29 05:36:46 +0000479 if (DisableChecking)
480 uncheckedReplaceAllUsesWith(To);
481 else
482 replaceAllUsesWith(To);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000483 }
Chris Lattnerc27038d2003-08-29 05:36:46 +0000484
485 // Delete the old constant!
486 destroyConstant();
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000487}
488
Chris Lattnerc27038d2003-08-29 05:36:46 +0000489void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
490 bool DisableChecking) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000491 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
492 Constant *To = cast<Constant>(ToV);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000493
Chris Lattner46b3d302003-04-16 22:40:51 +0000494 Constant *Replacement = 0;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000495 if (getOpcode() == Instruction::GetElementPtr) {
496 std::vector<Constant*> Indices;
Chris Lattner55ed6562003-05-14 17:51:05 +0000497 Constant *Pointer = getOperand(0);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000498 Indices.reserve(getNumOperands()-1);
Chris Lattner55ed6562003-05-14 17:51:05 +0000499 if (Pointer == From) Pointer = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000500
501 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000502 Constant *Val = getOperand(i);
503 if (Val == From) Val = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000504 Indices.push_back(Val);
505 }
506 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
507 } else if (getOpcode() == Instruction::Cast) {
508 assert(getOperand(0) == From && "Cast only has one use!");
Chris Lattner55ed6562003-05-14 17:51:05 +0000509 Replacement = ConstantExpr::getCast(To, getType());
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000510 } else if (getNumOperands() == 2) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000511 Constant *C1 = getOperand(0);
512 Constant *C2 = getOperand(1);
513 if (C1 == From) C1 = To;
514 if (C2 == From) C2 = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000515 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
516 } else {
517 assert(0 && "Unknown ConstantExpr type!");
518 return;
519 }
520
521 assert(Replacement != this && "I didn't contain From!");
522
523 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000524 if (DisableChecking)
525 uncheckedReplaceAllUsesWith(Replacement);
526 else
527 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000528
529 // Delete the old constant!
530 destroyConstant();
531}
532
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000533//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000534// Factory Function Implementation
535
Chris Lattner98fa07b2003-05-23 20:03:32 +0000536// ConstantCreator - A class that is used to create constants by
537// ValueMap*. This class should be partially specialized if there is
538// something strange that needs to be done to interface to the ctor for the
539// constant.
540//
Chris Lattner189d19f2003-11-21 20:23:48 +0000541namespace llvm {
542 template<class ConstantClass, class TypeClass, class ValType>
543 struct ConstantCreator {
544 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
545 return new ConstantClass(Ty, V);
546 }
547 };
548
549 template<class ConstantClass, class TypeClass>
550 struct ConvertConstantType {
551 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
552 assert(0 && "This type cannot be converted!\n");
553 abort();
554 }
555 };
556}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000557
Chris Lattner98fa07b2003-05-23 20:03:32 +0000558namespace {
559 template<class ValType, class TypeClass, class ConstantClass>
Chris Lattnerb50d1352003-10-05 00:17:43 +0000560 class ValueMap : public AbstractTypeUser {
561 typedef std::pair<const TypeClass*, ValType> MapKey;
562 typedef std::map<MapKey, ConstantClass *> MapTy;
563 typedef typename MapTy::iterator MapIterator;
564 MapTy Map;
565
566 typedef std::map<const TypeClass*, MapIterator> AbstractTypeMapTy;
567 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000568 public:
569 // getOrCreate - Return the specified constant from the map, creating it if
570 // necessary.
571 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000572 MapKey Lookup(Ty, V);
573 MapIterator I = Map.lower_bound(Lookup);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000574 if (I != Map.end() && I->first == Lookup)
575 return I->second; // Is it in the map?
576
577 // If no preexisting value, create one now...
578 ConstantClass *Result =
579 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
580
Chris Lattnerb50d1352003-10-05 00:17:43 +0000581
582 /// FIXME: why does this assert fail when loading 176.gcc?
583 //assert(Result->getType() == Ty && "Type specified is not correct!");
584 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
585
586 // If the type of the constant is abstract, make sure that an entry exists
587 // for it in the AbstractTypeMap.
588 if (Ty->isAbstract()) {
589 typename AbstractTypeMapTy::iterator TI =
590 AbstractTypeMap.lower_bound(Ty);
591
592 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
593 // Add ourselves to the ATU list of the type.
594 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
595
596 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
597 }
598 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000599 return Result;
600 }
601
602 void remove(ConstantClass *CP) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000603 // FIXME: This should not use a linear scan. If this gets to be a
604 // performance problem, someone should look at this.
605 MapIterator I = Map.begin();
606 for (MapIterator E = Map.end(); I != E && I->second != CP; ++I)
607 /* empty */;
608
609 assert(I != Map.end() && "Constant not found in constant table!");
610
611 // Now that we found the entry, make sure this isn't the entry that
612 // the AbstractTypeMap points to.
613 const TypeClass *Ty = I->first.first;
614 if (Ty->isAbstract()) {
615 assert(AbstractTypeMap.count(Ty) &&
616 "Abstract type not in AbstractTypeMap?");
617 MapIterator &ATMEntryIt = AbstractTypeMap[Ty];
618 if (ATMEntryIt == I) {
619 // Yes, we are removing the representative entry for this type.
620 // See if there are any other entries of the same type.
621 MapIterator TmpIt = ATMEntryIt;
622
623 // First check the entry before this one...
624 if (TmpIt != Map.begin()) {
625 --TmpIt;
626 if (TmpIt->first.first != Ty) // Not the same type, move back...
627 ++TmpIt;
628 }
629
630 // If we didn't find the same type, try to move forward...
631 if (TmpIt == ATMEntryIt) {
632 ++TmpIt;
633 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
634 --TmpIt; // No entry afterwards with the same type
635 }
636
637 // If there is another entry in the map of the same abstract type,
638 // update the AbstractTypeMap entry now.
639 if (TmpIt != ATMEntryIt) {
640 ATMEntryIt = TmpIt;
641 } else {
642 // Otherwise, we are removing the last instance of this type
643 // from the table. Remove from the ATM, and from user list.
644 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
645 AbstractTypeMap.erase(Ty);
646 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000647 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000648 }
649
650 Map.erase(I);
651 }
652
653 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
654 typename AbstractTypeMapTy::iterator I =
655 AbstractTypeMap.find(cast<TypeClass>(OldTy));
656
657 assert(I != AbstractTypeMap.end() &&
658 "Abstract type not in AbstractTypeMap?");
659
660 // Convert a constant at a time until the last one is gone. The last one
661 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
662 // eliminated eventually.
663 do {
664 ConvertConstantType<ConstantClass,
665 TypeClass>::convert(I->second->second,
666 cast<TypeClass>(NewTy));
667
668 I = AbstractTypeMap.find(cast<TypeClass>(OldTy));
669 } while (I != AbstractTypeMap.end());
670 }
671
672 // If the type became concrete without being refined to any other existing
673 // type, we just remove ourselves from the ATU list.
674 void typeBecameConcrete(const DerivedType *AbsTy) {
675 AbsTy->removeAbstractTypeUser(this);
676 }
677
678 void dump() const {
679 std::cerr << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +0000680 }
681 };
682}
683
684
685
Chris Lattner3462ae32001-12-03 22:26:30 +0000686//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000687//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000688static ValueMap< int64_t, Type, ConstantSInt> SIntConstants;
689static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000690
Chris Lattner3462ae32001-12-03 22:26:30 +0000691ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000692 return SIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000693}
694
Chris Lattner3462ae32001-12-03 22:26:30 +0000695ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000696 return UIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000697}
698
Chris Lattner3462ae32001-12-03 22:26:30 +0000699ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000700 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000701 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
702 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000703}
704
Chris Lattner3462ae32001-12-03 22:26:30 +0000705//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000706//
Chris Lattnerac80ea42004-02-01 22:49:04 +0000707namespace llvm {
708 template<>
709 struct ConstantCreator<ConstantFP, Type, uint64_t> {
710 static ConstantFP *create(const Type *Ty, uint64_t V) {
711 assert(Ty == Type::DoubleTy);
712 union {
713 double F;
714 uint64_t I;
715 } T;
716 T.I = V;
717 return new ConstantFP(Ty, T.F);
718 }
719 };
720 template<>
721 struct ConstantCreator<ConstantFP, Type, uint32_t> {
722 static ConstantFP *create(const Type *Ty, uint32_t V) {
723 assert(Ty == Type::FloatTy);
724 union {
725 float F;
726 uint32_t I;
727 } T;
728 T.I = V;
729 return new ConstantFP(Ty, T.F);
730 }
731 };
732}
733
734static ValueMap<uint64_t, Type, ConstantFP> DoubleConstants;
735static ValueMap<uint32_t, Type, ConstantFP> FloatConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000736
Chris Lattner3462ae32001-12-03 22:26:30 +0000737ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner241ed4c2004-01-23 00:55:21 +0000738 if (Ty == Type::FloatTy) {
739 // Force the value through memory to normalize it.
Chris Lattnerac80ea42004-02-01 22:49:04 +0000740 union {
741 float F;
742 uint32_t I;
743 } T;
744 T.F = (float)V;
745 return FloatConstants.getOrCreate(Ty, T.I);
746 } else {
747 assert(Ty == Type::DoubleTy);
748 union {
749 double F;
750 uint64_t I;
751 } T;
752 T.F = V;
753 return DoubleConstants.getOrCreate(Ty, T.I);
Chris Lattner241ed4c2004-01-23 00:55:21 +0000754 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000755}
756
Chris Lattner9fba3da2004-02-15 05:53:04 +0000757//---- ConstantAggregateZero::get() implementation...
758//
759namespace llvm {
760 // ConstantAggregateZero does not take extra "value" argument...
761 template<class ValType>
762 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
763 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
764 return new ConstantAggregateZero(Ty);
765 }
766 };
767
768 template<>
769 struct ConvertConstantType<ConstantAggregateZero, Type> {
770 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
771 // Make everyone now use a constant of the new type...
772 Constant *New = ConstantAggregateZero::get(NewTy);
773 assert(New != OldC && "Didn't replace constant??");
774 OldC->uncheckedReplaceAllUsesWith(New);
775 OldC->destroyConstant(); // This constant is now dead, destroy it.
776 }
777 };
778}
779
780static ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
781
782Constant *ConstantAggregateZero::get(const Type *Ty) {
783 return AggZeroConstants.getOrCreate(Ty, 0);
784}
785
786// destroyConstant - Remove the constant from the constant table...
787//
788void ConstantAggregateZero::destroyConstant() {
789 AggZeroConstants.remove(this);
790 destroyConstantImpl();
791}
792
793void ConstantAggregateZero::replaceUsesOfWithOnConstant(Value *From, Value *To,
794 bool DisableChecking) {
795 assert(0 && "No uses!");
796 abort();
797}
798
799
800
Chris Lattner3462ae32001-12-03 22:26:30 +0000801//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000802//
Chris Lattner189d19f2003-11-21 20:23:48 +0000803namespace llvm {
804 template<>
805 struct ConvertConstantType<ConstantArray, ArrayType> {
806 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
807 // Make everyone now use a constant of the new type...
808 std::vector<Constant*> C;
809 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
810 C.push_back(cast<Constant>(OldC->getOperand(i)));
811 Constant *New = ConstantArray::get(NewTy, C);
812 assert(New != OldC && "Didn't replace constant??");
813 OldC->uncheckedReplaceAllUsesWith(New);
814 OldC->destroyConstant(); // This constant is now dead, destroy it.
815 }
816 };
817}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000818
Chris Lattner98fa07b2003-05-23 20:03:32 +0000819static ValueMap<std::vector<Constant*>, ArrayType,
820 ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000821
Chris Lattner015e8212004-02-15 04:14:47 +0000822Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +0000823 const std::vector<Constant*> &V) {
824 // If this is an all-zero array, return a ConstantAggregateZero object
825 if (!V.empty()) {
826 Constant *C = V[0];
827 if (!C->isNullValue())
828 return ArrayConstants.getOrCreate(Ty, V);
829 for (unsigned i = 1, e = V.size(); i != e; ++i)
830 if (V[i] != C)
831 return ArrayConstants.getOrCreate(Ty, V);
832 }
833 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000834}
835
Chris Lattner98fa07b2003-05-23 20:03:32 +0000836// destroyConstant - Remove the constant from the constant table...
837//
838void ConstantArray::destroyConstant() {
839 ArrayConstants.remove(this);
840 destroyConstantImpl();
841}
842
Chris Lattner3462ae32001-12-03 22:26:30 +0000843// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000844// contain the specified string. A null terminator is added to the specified
845// string so that it may be used in a natural way...
846//
Chris Lattner015e8212004-02-15 04:14:47 +0000847Constant *ConstantArray::get(const std::string &Str) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000848 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000849
850 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000851 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000852
853 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000854 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000855
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000856 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000857 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000858}
859
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000860/// isString - This method returns true if the array is an array of sbyte or
861/// ubyte, and if the elements of the array are all ConstantInt's.
862bool ConstantArray::isString() const {
863 // Check the element type for sbyte or ubyte...
Chris Lattnere8701f62004-01-14 17:51:53 +0000864 if (getType()->getElementType() != Type::UByteTy &&
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000865 getType()->getElementType() != Type::SByteTy)
866 return false;
867 // Check the elements to make sure they are all integers, not constant
868 // expressions.
869 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
870 if (!isa<ConstantInt>(getOperand(i)))
871 return false;
872 return true;
873}
874
Chris Lattner81fabb02002-08-26 17:53:56 +0000875// getAsString - If the sub-element type of this array is either sbyte or ubyte,
876// then this method converts the array to an std::string and returns it.
877// Otherwise, it asserts out.
878//
879std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000880 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +0000881 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +0000882 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
883 Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue();
Chris Lattner81fabb02002-08-26 17:53:56 +0000884 return Result;
885}
886
887
Chris Lattner3462ae32001-12-03 22:26:30 +0000888//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000889//
Chris Lattnerb50d1352003-10-05 00:17:43 +0000890
Chris Lattner189d19f2003-11-21 20:23:48 +0000891namespace llvm {
892 template<>
893 struct ConvertConstantType<ConstantStruct, StructType> {
894 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
895 // Make everyone now use a constant of the new type...
896 std::vector<Constant*> C;
897 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
898 C.push_back(cast<Constant>(OldC->getOperand(i)));
899 Constant *New = ConstantStruct::get(NewTy, C);
900 assert(New != OldC && "Didn't replace constant??");
901
902 OldC->uncheckedReplaceAllUsesWith(New);
903 OldC->destroyConstant(); // This constant is now dead, destroy it.
904 }
905 };
906}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000907
Chris Lattner98fa07b2003-05-23 20:03:32 +0000908static ValueMap<std::vector<Constant*>, StructType,
909 ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000910
Chris Lattner015e8212004-02-15 04:14:47 +0000911Constant *ConstantStruct::get(const StructType *Ty,
912 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +0000913 // Create a ConstantAggregateZero value if all elements are zeros...
914 for (unsigned i = 0, e = V.size(); i != e; ++i)
915 if (!V[i]->isNullValue())
916 return StructConstants.getOrCreate(Ty, V);
917
918 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000919}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000920
Chris Lattnerd7a73302001-10-13 06:57:33 +0000921// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000922//
Chris Lattner3462ae32001-12-03 22:26:30 +0000923void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000924 StructConstants.remove(this);
925 destroyConstantImpl();
926}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000927
Chris Lattner3462ae32001-12-03 22:26:30 +0000928//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000929//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000930
Chris Lattner189d19f2003-11-21 20:23:48 +0000931namespace llvm {
932 // ConstantPointerNull does not take extra "value" argument...
933 template<class ValType>
934 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
935 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
936 return new ConstantPointerNull(Ty);
937 }
938 };
Chris Lattner98fa07b2003-05-23 20:03:32 +0000939
Chris Lattner189d19f2003-11-21 20:23:48 +0000940 template<>
941 struct ConvertConstantType<ConstantPointerNull, PointerType> {
942 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
943 // Make everyone now use a constant of the new type...
944 Constant *New = ConstantPointerNull::get(NewTy);
945 assert(New != OldC && "Didn't replace constant??");
946 OldC->uncheckedReplaceAllUsesWith(New);
947 OldC->destroyConstant(); // This constant is now dead, destroy it.
948 }
949 };
950}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000951
Chris Lattner98fa07b2003-05-23 20:03:32 +0000952static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000953
Chris Lattner3462ae32001-12-03 22:26:30 +0000954ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000955 return NullPtrConstants.getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000956}
957
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000958// destroyConstant - Remove the constant from the constant table...
959//
960void ConstantPointerNull::destroyConstant() {
961 NullPtrConstants.remove(this);
962 destroyConstantImpl();
963}
964
965
Chris Lattner3462ae32001-12-03 22:26:30 +0000966//---- ConstantPointerRef::get() implementation...
Chris Lattner25033252001-10-03 19:28:15 +0000967//
Chris Lattner3462ae32001-12-03 22:26:30 +0000968ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000969 assert(GV->getParent() && "Global Value must be attached to a module!");
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000970
Chris Lattnerd7a73302001-10-13 06:57:33 +0000971 // The Module handles the pointer reference sharing...
Chris Lattner3462ae32001-12-03 22:26:30 +0000972 return GV->getParent()->getConstantPointerRef(GV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000973}
974
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000975// destroyConstant - Remove the constant from the constant table...
976//
977void ConstantPointerRef::destroyConstant() {
978 getValue()->getParent()->destroyConstantPointerRef(this);
979 destroyConstantImpl();
980}
981
982
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000983//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000984//
Chris Lattner2b383d2e2003-05-13 21:37:02 +0000985typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000986
Chris Lattner189d19f2003-11-21 20:23:48 +0000987namespace llvm {
988 template<>
989 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
990 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
991 if (V.first == Instruction::Cast)
992 return new ConstantExpr(Instruction::Cast, V.second[0], Ty);
993 if ((V.first >= Instruction::BinaryOpsBegin &&
994 V.first < Instruction::BinaryOpsEnd) ||
995 V.first == Instruction::Shl || V.first == Instruction::Shr)
996 return new ConstantExpr(V.first, V.second[0], V.second[1]);
997
998 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
999
1000 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
1001 return new ConstantExpr(V.second[0], IdxList, Ty);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001002 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001003 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001004
Chris Lattner189d19f2003-11-21 20:23:48 +00001005 template<>
1006 struct ConvertConstantType<ConstantExpr, Type> {
1007 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1008 Constant *New;
1009 switch (OldC->getOpcode()) {
1010 case Instruction::Cast:
1011 New = ConstantExpr::getCast(OldC->getOperand(0), NewTy);
1012 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001013 case Instruction::Select:
1014 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1015 OldC->getOperand(1),
1016 OldC->getOperand(2));
1017 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001018 case Instruction::Shl:
1019 case Instruction::Shr:
1020 New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
1021 OldC->getOperand(0), OldC->getOperand(1));
1022 break;
1023 default:
1024 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
1025 OldC->getOpcode() < Instruction::BinaryOpsEnd);
1026 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1027 OldC->getOperand(1));
1028 break;
1029 case Instruction::GetElementPtr:
1030 // Make everyone now use a constant of the new type...
1031 std::vector<Constant*> C;
1032 for (unsigned i = 1, e = OldC->getNumOperands(); i != e; ++i)
1033 C.push_back(cast<Constant>(OldC->getOperand(i)));
1034 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), C);
1035 break;
1036 }
1037
1038 assert(New != OldC && "Didn't replace constant??");
1039 OldC->uncheckedReplaceAllUsesWith(New);
1040 OldC->destroyConstant(); // This constant is now dead, destroy it.
1041 }
1042 };
1043} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001044
1045
Chris Lattner98fa07b2003-05-23 20:03:32 +00001046static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001047
Chris Lattner46b3d302003-04-16 22:40:51 +00001048Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001049 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1050
Chris Lattneracdbe712003-04-17 19:24:48 +00001051 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
1052 return FC; // Fold a few common cases...
1053
Vikram S. Adve4c485332002-07-15 18:19:33 +00001054 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001055 std::vector<Constant*> argVec(1, C);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001056 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
1057 return ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001058}
Chris Lattnerd7a73302001-10-13 06:57:33 +00001059
Chris Lattnerb50d1352003-10-05 00:17:43 +00001060Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
1061 Constant *C1, Constant *C2) {
Chris Lattner5645e8a2004-01-12 19:04:55 +00001062 if (Opcode == Instruction::Shl || Opcode == Instruction::Shr)
1063 return getShiftTy(ReqTy, Opcode, C1, C2);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001064 // Check the operands for consistency first
1065 assert((Opcode >= Instruction::BinaryOpsBegin &&
1066 Opcode < Instruction::BinaryOpsEnd) &&
1067 "Invalid opcode in binary constant expression");
1068 assert(C1->getType() == C2->getType() &&
1069 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001070
1071 if (ReqTy == C1->getType())
1072 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1073 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001074
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001075 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001076 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001077 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001078}
1079
Chris Lattner6e415c02004-03-12 05:54:04 +00001080Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
1081 Constant *V1, Constant *V2) {
1082 assert(C->getType() == Type::BoolTy && "Select condition must be bool!");
1083 assert(V1->getType() == V2->getType() && "Select value types must match!");
1084 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
1085
1086 if (ReqTy == V1->getType())
1087 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1088 return SC; // Fold common cases
1089
1090 std::vector<Constant*> argVec(3, C);
1091 argVec[1] = V1;
1092 argVec[2] = V2;
1093 ExprMapKeyType Key = std::make_pair(Instruction::Select, argVec);
1094 return ExprConstants.getOrCreate(ReqTy, Key);
1095}
1096
Chris Lattner9eb2b522004-01-12 19:12:58 +00001097/// getShiftTy - Return a shift left or shift right constant expr
Chris Lattnerb50d1352003-10-05 00:17:43 +00001098Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
1099 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001100 // Check the operands for consistency first
1101 assert((Opcode == Instruction::Shl ||
1102 Opcode == Instruction::Shr) &&
1103 "Invalid opcode in binary constant expression");
1104 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
1105 "Invalid operand types for Shift constant expr!");
1106
Chris Lattner0bba7712004-01-12 20:40:42 +00001107 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001108 return FC; // Fold a few common cases...
1109
1110 // Look up the constant in the table first to ensure uniqueness
1111 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001112 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001113 return ExprConstants.getOrCreate(ReqTy, Key);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001114}
1115
1116
Chris Lattnerb50d1352003-10-05 00:17:43 +00001117Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
1118 const std::vector<Constant*> &IdxList) {
Chris Lattner04b60fe2004-02-16 20:46:13 +00001119 assert(GetElementPtrInst::getIndexedType(C->getType(),
1120 std::vector<Value*>(IdxList.begin(), IdxList.end()), true) &&
1121 "GEP indices invalid!");
1122
Chris Lattneracdbe712003-04-17 19:24:48 +00001123 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
1124 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001125
Chris Lattnerb50d1352003-10-05 00:17:43 +00001126 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001127 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001128 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001129 std::vector<Constant*> argVec(1, C);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +00001130 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001131 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001132 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001133}
1134
Chris Lattnerb50d1352003-10-05 00:17:43 +00001135Constant *ConstantExpr::getGetElementPtr(Constant *C,
1136 const std::vector<Constant*> &IdxList){
1137 // Get the result type of the getelementptr!
1138 std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
1139
1140 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
1141 true);
1142 assert(Ty && "GEP indices invalid!");
1143 return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
1144}
1145
1146
Vikram S. Adve4c485332002-07-15 18:19:33 +00001147// destroyConstant - Remove the constant from the constant table...
1148//
1149void ConstantExpr::destroyConstant() {
1150 ExprConstants.remove(this);
1151 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001152}
1153
Chris Lattner3cd8c562002-07-30 18:54:25 +00001154const char *ConstantExpr::getOpcodeName() const {
1155 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001156}