blob: bc274f0e3242d24e142b3e14451204c515ee94b0 [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"
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000017#include "llvm/GlobalValue.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000018#include "llvm/Instructions.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000019#include "llvm/SymbolTable.h"
Chris Lattnerd7a73302001-10-13 06:57:33 +000020#include "llvm/Module.h"
Chris Lattner5de22042001-11-27 00:03:19 +000021#include "Support/StringExtras.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000022#include <algorithm>
Reid Spencercf394bf2004-07-04 11:51:24 +000023#include <iostream>
Chris Lattner189d19f2003-11-21 20:23:48 +000024using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000025
Chris Lattner3462ae32001-12-03 22:26:30 +000026ConstantBool *ConstantBool::True = new ConstantBool(true);
27ConstantBool *ConstantBool::False = new ConstantBool(false);
Chris Lattner2f7c9632001-06-06 20:29:01 +000028
Chris Lattner9655e542001-07-20 19:16:02 +000029
Chris Lattner2f7c9632001-06-06 20:29:01 +000030//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000031// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000032//===----------------------------------------------------------------------===//
33
34// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +000035void Constant::setName(const std::string &Name, SymbolTable *ST) {
Chris Lattner49d855c2001-09-07 16:46:31 +000036 assert(ST && "Type::setName - Must provide symbol table argument!");
37
38 if (Name.size()) ST->insert(Name, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000039}
40
Chris Lattner3462ae32001-12-03 22:26:30 +000041void Constant::destroyConstantImpl() {
42 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000043 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000044 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +000045 // but they don't know that. Because we only find out when the CPV is
46 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000047 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000048 //
49 while (!use_empty()) {
50 Value *V = use_back();
51#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000052 if (!isa<Constant>(V))
53 std::cerr << "While deleting: " << *this
54 << "\n\nUse still stuck around after Def is destroyed: "
55 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000056#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000057 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000058 Constant *CV = cast<Constant>(V);
59 CV->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +000060
61 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000062 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000063 }
64
65 // Value has no outstanding references it is safe to delete it now...
66 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000067}
Chris Lattner2f7c9632001-06-06 20:29:01 +000068
Chris Lattnerb1585a92002-08-13 17:50:20 +000069// Static constructor to create a '0' constant of arbitrary type...
70Constant *Constant::getNullValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +000071 switch (Ty->getTypeID()) {
Chris Lattner3e88ef92003-10-03 19:34:51 +000072 case Type::BoolTyID: {
73 static Constant *NullBool = ConstantBool::get(false);
74 return NullBool;
75 }
76 case Type::SByteTyID: {
77 static Constant *NullSByte = ConstantSInt::get(Type::SByteTy, 0);
78 return NullSByte;
79 }
80 case Type::UByteTyID: {
81 static Constant *NullUByte = ConstantUInt::get(Type::UByteTy, 0);
82 return NullUByte;
83 }
84 case Type::ShortTyID: {
85 static Constant *NullShort = ConstantSInt::get(Type::ShortTy, 0);
86 return NullShort;
87 }
88 case Type::UShortTyID: {
89 static Constant *NullUShort = ConstantUInt::get(Type::UShortTy, 0);
90 return NullUShort;
91 }
92 case Type::IntTyID: {
93 static Constant *NullInt = ConstantSInt::get(Type::IntTy, 0);
94 return NullInt;
95 }
96 case Type::UIntTyID: {
97 static Constant *NullUInt = ConstantUInt::get(Type::UIntTy, 0);
98 return NullUInt;
99 }
100 case Type::LongTyID: {
101 static Constant *NullLong = ConstantSInt::get(Type::LongTy, 0);
102 return NullLong;
103 }
104 case Type::ULongTyID: {
105 static Constant *NullULong = ConstantUInt::get(Type::ULongTy, 0);
106 return NullULong;
107 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000108
Chris Lattner3e88ef92003-10-03 19:34:51 +0000109 case Type::FloatTyID: {
110 static Constant *NullFloat = ConstantFP::get(Type::FloatTy, 0);
111 return NullFloat;
112 }
113 case Type::DoubleTyID: {
114 static Constant *NullDouble = ConstantFP::get(Type::DoubleTy, 0);
115 return NullDouble;
116 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000117
118 case Type::PointerTyID:
119 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner3e88ef92003-10-03 19:34:51 +0000120
Chris Lattner9fba3da2004-02-15 05:53:04 +0000121 case Type::StructTyID:
122 case Type::ArrayTyID:
123 return ConstantAggregateZero::get(Ty);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000124 default:
Reid Spencercf394bf2004-07-04 11:51:24 +0000125 // Function, Label, or Opaque type?
126 assert(!"Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000127 return 0;
128 }
129}
130
131// Static constructor to create the maximum constant of an integral type...
132ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000133 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000134 case Type::BoolTyID: return ConstantBool::True;
135 case Type::SByteTyID:
136 case Type::ShortTyID:
137 case Type::IntTyID:
138 case Type::LongTyID: {
139 // Calculate 011111111111111...
140 unsigned TypeBits = Ty->getPrimitiveSize()*8;
141 int64_t Val = INT64_MAX; // All ones
142 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
143 return ConstantSInt::get(Ty, Val);
144 }
145
146 case Type::UByteTyID:
147 case Type::UShortTyID:
148 case Type::UIntTyID:
149 case Type::ULongTyID: return getAllOnesValue(Ty);
150
Chris Lattner31408f72002-08-14 17:12:13 +0000151 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000152 }
153}
154
155// Static constructor to create the minimum constant for an integral type...
156ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000157 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000158 case Type::BoolTyID: return ConstantBool::False;
159 case Type::SByteTyID:
160 case Type::ShortTyID:
161 case Type::IntTyID:
162 case Type::LongTyID: {
163 // Calculate 1111111111000000000000
164 unsigned TypeBits = Ty->getPrimitiveSize()*8;
165 int64_t Val = -1; // All ones
166 Val <<= TypeBits-1; // Shift over to the right spot
167 return ConstantSInt::get(Ty, Val);
168 }
169
170 case Type::UByteTyID:
171 case Type::UShortTyID:
172 case Type::UIntTyID:
173 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
174
Chris Lattner31408f72002-08-14 17:12:13 +0000175 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000176 }
177}
178
179// Static constructor to create an integral constant with all bits set
180ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000181 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000182 case Type::BoolTyID: return ConstantBool::True;
183 case Type::SByteTyID:
184 case Type::ShortTyID:
185 case Type::IntTyID:
186 case Type::LongTyID: return ConstantSInt::get(Ty, -1);
187
188 case Type::UByteTyID:
189 case Type::UShortTyID:
190 case Type::UIntTyID:
191 case Type::ULongTyID: {
192 // Calculate ~0 of the right type...
193 unsigned TypeBits = Ty->getPrimitiveSize()*8;
194 uint64_t Val = ~0ULL; // All ones
195 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
196 return ConstantUInt::get(Ty, Val);
197 }
Chris Lattner31408f72002-08-14 17:12:13 +0000198 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000199 }
200}
201
Chris Lattner83e5d392003-03-10 22:39:02 +0000202bool ConstantUInt::isAllOnesValue() const {
203 unsigned TypeBits = getType()->getPrimitiveSize()*8;
204 uint64_t Val = ~0ULL; // All ones
205 Val >>= 64-TypeBits; // Shift out inappropriate bits
206 return getValue() == Val;
207}
208
Chris Lattnerb1585a92002-08-13 17:50:20 +0000209
Chris Lattner2f7c9632001-06-06 20:29:01 +0000210//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +0000211// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +0000212//===----------------------------------------------------------------------===//
213
214//===----------------------------------------------------------------------===//
215// Normal Constructors
216
Chris Lattner265eb642004-06-21 12:12:12 +0000217ConstantIntegral::ConstantIntegral(const Type *Ty, uint64_t V)
218 : Constant(Ty) {
219 Val.Unsigned = V;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000220}
Chris Lattner49d855c2001-09-07 16:46:31 +0000221
Chris Lattner265eb642004-06-21 12:12:12 +0000222ConstantBool::ConstantBool(bool V) : ConstantIntegral(Type::BoolTy, V) {
223}
224
225ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : ConstantIntegral(Ty, V) {
Chris Lattner7309d662001-07-21 19:16:08 +0000226}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000227
Chris Lattner3462ae32001-12-03 22:26:30 +0000228ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000229 assert(Ty->isInteger() && Ty->isSigned() &&
230 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000231 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000232}
233
Chris Lattner3462ae32001-12-03 22:26:30 +0000234ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000235 assert(Ty->isInteger() && Ty->isUnsigned() &&
236 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000237 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000238}
239
Chris Lattner3462ae32001-12-03 22:26:30 +0000240ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
Chris Lattner9655e542001-07-20 19:16:02 +0000241 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000242 Val = V;
243}
244
Chris Lattner3462ae32001-12-03 22:26:30 +0000245ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000246 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattner0d779712002-10-08 23:33:52 +0000247 Operands.reserve(V.size());
248 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattnerab69ecc2003-08-18 16:54:48 +0000249 assert(V[i]->getType() == T->getElementType() ||
250 (T->isAbstract() &&
Chris Lattner6b727592004-06-17 18:19:28 +0000251 V[i]->getType()->getTypeID() == T->getElementType()->getTypeID()));
Chris Lattnera073acb2001-07-07 08:36:50 +0000252 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000253 }
254}
255
Chris Lattner3462ae32001-12-03 22:26:30 +0000256ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattner7f74a562002-01-20 22:54:45 +0000257 const std::vector<Constant*> &V) : Constant(T) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000258 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000259 "Invalid initializer vector for constant structure");
Chris Lattner0d779712002-10-08 23:33:52 +0000260 Operands.reserve(V.size());
261 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000262 assert((V[i]->getType() == T->getElementType(i) ||
263 ((T->getElementType(i)->isAbstract() ||
264 V[i]->getType()->isAbstract()) &&
Chris Lattner6b727592004-06-17 18:19:28 +0000265 T->getElementType(i)->getTypeID() == V[i]->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000266 "Initializer for struct element doesn't match struct element type!");
Chris Lattnera073acb2001-07-07 08:36:50 +0000267 Operands.push_back(Use(V[i], this));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000268 }
269}
270
Chris Lattner3cd8c562002-07-30 18:54:25 +0000271ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
Chris Lattner5230e702004-07-19 00:59:10 +0000272 : Constant(Ty, ConstantExprVal), iType(Opcode) {
Chris Lattner6e415c02004-03-12 05:54:04 +0000273 Operands.reserve(1);
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000274 Operands.push_back(Use(C, this));
275}
276
Chris Lattner6e415c02004-03-12 05:54:04 +0000277// Select instruction creation ctor
278ConstantExpr::ConstantExpr(Constant *C, Constant *V1, Constant *V2)
Chris Lattner5230e702004-07-19 00:59:10 +0000279 : Constant(V1->getType(), ConstantExprVal), iType(Instruction::Select) {
Chris Lattner6e415c02004-03-12 05:54:04 +0000280 Operands.reserve(3);
281 Operands.push_back(Use(C, this));
282 Operands.push_back(Use(V1, this));
283 Operands.push_back(Use(V2, this));
284}
285
286
Chris Lattner22ced562003-06-22 20:48:30 +0000287static bool isSetCC(unsigned Opcode) {
288 return Opcode == Instruction::SetEQ || Opcode == Instruction::SetNE ||
289 Opcode == Instruction::SetLT || Opcode == Instruction::SetGT ||
290 Opcode == Instruction::SetLE || Opcode == Instruction::SetGE;
291}
292
Chris Lattner3cd8c562002-07-30 18:54:25 +0000293ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Chris Lattner5230e702004-07-19 00:59:10 +0000294 : Constant(isSetCC(Opcode) ? Type::BoolTy : C1->getType(), ConstantExprVal),
295 iType(Opcode) {
Chris Lattner6e415c02004-03-12 05:54:04 +0000296 Operands.reserve(2);
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000297 Operands.push_back(Use(C1, this));
298 Operands.push_back(Use(C2, this));
299}
300
Chris Lattner3cd8c562002-07-30 18:54:25 +0000301ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
302 const Type *DestTy)
Chris Lattner5230e702004-07-19 00:59:10 +0000303 : Constant(DestTy, ConstantExprVal), iType(Instruction::GetElementPtr) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000304 Operands.reserve(1+IdxList.size());
305 Operands.push_back(Use(C, this));
306 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
307 Operands.push_back(Use(IdxList[i], this));
308}
309
Chris Lattner817175f2004-03-29 02:37:53 +0000310/// ConstantExpr::get* - Return some common constants without having to
311/// specify the full Instruction::OPCODE identifier.
312///
313Constant *ConstantExpr::getNeg(Constant *C) {
Chris Lattner3cdc27c2004-03-29 19:51:24 +0000314 if (!C->getType()->isFloatingPoint())
315 return get(Instruction::Sub, getNullValue(C->getType()), C);
316 else
317 return get(Instruction::Sub, ConstantFP::get(C->getType(), -0.0), C);
Chris Lattner817175f2004-03-29 02:37:53 +0000318}
319Constant *ConstantExpr::getNot(Constant *C) {
320 assert(isa<ConstantIntegral>(C) && "Cannot NOT a nonintegral type!");
321 return get(Instruction::Xor, C,
322 ConstantIntegral::getAllOnesValue(C->getType()));
323}
324Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
325 return get(Instruction::Add, C1, C2);
326}
327Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
328 return get(Instruction::Sub, C1, C2);
329}
330Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
331 return get(Instruction::Mul, C1, C2);
332}
333Constant *ConstantExpr::getDiv(Constant *C1, Constant *C2) {
334 return get(Instruction::Div, C1, C2);
335}
336Constant *ConstantExpr::getRem(Constant *C1, Constant *C2) {
337 return get(Instruction::Rem, C1, C2);
338}
339Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
340 return get(Instruction::And, C1, C2);
341}
342Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
343 return get(Instruction::Or, C1, C2);
344}
345Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
346 return get(Instruction::Xor, C1, C2);
347}
348Constant *ConstantExpr::getSetEQ(Constant *C1, Constant *C2) {
349 return get(Instruction::SetEQ, C1, C2);
350}
351Constant *ConstantExpr::getSetNE(Constant *C1, Constant *C2) {
352 return get(Instruction::SetNE, C1, C2);
353}
354Constant *ConstantExpr::getSetLT(Constant *C1, Constant *C2) {
355 return get(Instruction::SetLT, C1, C2);
356}
357Constant *ConstantExpr::getSetGT(Constant *C1, Constant *C2) {
358 return get(Instruction::SetGT, C1, C2);
359}
360Constant *ConstantExpr::getSetLE(Constant *C1, Constant *C2) {
361 return get(Instruction::SetLE, C1, C2);
362}
363Constant *ConstantExpr::getSetGE(Constant *C1, Constant *C2) {
364 return get(Instruction::SetGE, C1, C2);
365}
366Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
367 return get(Instruction::Shl, C1, C2);
368}
369Constant *ConstantExpr::getShr(Constant *C1, Constant *C2) {
370 return get(Instruction::Shr, C1, C2);
371}
372
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000373Constant *ConstantExpr::getUShr(Constant *C1, Constant *C2) {
374 if (C1->getType()->isUnsigned()) return getShr(C1, C2);
375 return getCast(getShr(getCast(C1,
376 C1->getType()->getUnsignedVersion()), C2), C1->getType());
377}
Chris Lattner817175f2004-03-29 02:37:53 +0000378
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000379Constant *ConstantExpr::getSShr(Constant *C1, Constant *C2) {
380 if (C1->getType()->isSigned()) return getShr(C1, C2);
381 return getCast(getShr(getCast(C1,
382 C1->getType()->getSignedVersion()), C2), C1->getType());
383}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000384
Chris Lattner2f7c9632001-06-06 20:29:01 +0000385
386//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000387// isValueValidForType implementations
388
Chris Lattner3462ae32001-12-03 22:26:30 +0000389bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000390 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000391 default:
392 return false; // These can't be represented as integers!!!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000393 // Signed types...
394 case Type::SByteTyID:
395 return (Val <= INT8_MAX && Val >= INT8_MIN);
396 case Type::ShortTyID:
397 return (Val <= INT16_MAX && Val >= INT16_MIN);
398 case Type::IntTyID:
Chris Lattner74248512004-06-08 23:21:39 +0000399 return (Val <= int(INT32_MAX) && Val >= int(INT32_MIN));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000400 case Type::LongTyID:
401 return true; // This is the largest type...
402 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000403}
404
Chris Lattner3462ae32001-12-03 22:26:30 +0000405bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000406 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000407 default:
408 return false; // These can't be represented as integers!!!
409
410 // Unsigned types...
411 case Type::UByteTyID:
412 return (Val <= UINT8_MAX);
413 case Type::UShortTyID:
414 return (Val <= UINT16_MAX);
415 case Type::UIntTyID:
416 return (Val <= UINT32_MAX);
417 case Type::ULongTyID:
418 return true; // This is the largest type...
419 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000420}
421
Chris Lattner3462ae32001-12-03 22:26:30 +0000422bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000423 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000424 default:
425 return false; // These can't be represented as floating point!
426
427 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000428 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000429 case Type::DoubleTyID:
430 return true; // This is the largest type...
431 }
432};
Chris Lattner9655e542001-07-20 19:16:02 +0000433
Chris Lattner49d855c2001-09-07 16:46:31 +0000434//===----------------------------------------------------------------------===//
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000435// replaceUsesOfWithOnConstant implementations
436
Chris Lattnerc27038d2003-08-29 05:36:46 +0000437void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
438 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000439 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
440
441 std::vector<Constant*> Values;
Alkis Evlogimenosf0cc8142004-08-04 08:02:59 +0000442 Values.reserve(getNumOperands()); // Build replacement array...
443 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
444 Constant *Val = getOperand(i);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000445 if (Val == From) Val = cast<Constant>(To);
446 Values.push_back(Val);
447 }
448
Chris Lattnerc75bf522004-02-15 04:05:58 +0000449 Constant *Replacement = ConstantArray::get(getType(), Values);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000450 assert(Replacement != this && "I didn't contain From!");
451
452 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000453 if (DisableChecking)
454 uncheckedReplaceAllUsesWith(Replacement);
455 else
456 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000457
458 // Delete the old constant!
459 destroyConstant();
460}
461
Chris Lattnerc27038d2003-08-29 05:36:46 +0000462void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
463 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000464 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
465
466 std::vector<Constant*> Values;
Alkis Evlogimenosf0cc8142004-08-04 08:02:59 +0000467 Values.reserve(getNumOperands()); // Build replacement array...
468 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
469 Constant *Val = getOperand(i);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000470 if (Val == From) Val = cast<Constant>(To);
471 Values.push_back(Val);
472 }
473
Chris Lattner37a716f2004-02-15 04:07:32 +0000474 Constant *Replacement = ConstantStruct::get(getType(), Values);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000475 assert(Replacement != this && "I didn't contain From!");
476
477 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000478 if (DisableChecking)
479 uncheckedReplaceAllUsesWith(Replacement);
480 else
481 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000482
483 // Delete the old constant!
484 destroyConstant();
485}
486
Chris Lattnerc27038d2003-08-29 05:36:46 +0000487void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
488 bool DisableChecking) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000489 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
490 Constant *To = cast<Constant>(ToV);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000491
Chris Lattner46b3d302003-04-16 22:40:51 +0000492 Constant *Replacement = 0;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000493 if (getOpcode() == Instruction::GetElementPtr) {
494 std::vector<Constant*> Indices;
Chris Lattner55ed6562003-05-14 17:51:05 +0000495 Constant *Pointer = getOperand(0);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000496 Indices.reserve(getNumOperands()-1);
Chris Lattner55ed6562003-05-14 17:51:05 +0000497 if (Pointer == From) Pointer = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000498
499 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000500 Constant *Val = getOperand(i);
501 if (Val == From) Val = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000502 Indices.push_back(Val);
503 }
504 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
505 } else if (getOpcode() == Instruction::Cast) {
506 assert(getOperand(0) == From && "Cast only has one use!");
Chris Lattner55ed6562003-05-14 17:51:05 +0000507 Replacement = ConstantExpr::getCast(To, getType());
Chris Lattner467cb2b2004-03-31 02:56:11 +0000508 } else if (getOpcode() == Instruction::Select) {
509 Constant *C1 = getOperand(0);
510 Constant *C2 = getOperand(1);
511 Constant *C3 = getOperand(2);
512 if (C1 == From) C1 = To;
513 if (C2 == From) C2 = To;
514 if (C3 == From) C3 = To;
515 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000516 } else if (getNumOperands() == 2) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000517 Constant *C1 = getOperand(0);
518 Constant *C2 = getOperand(1);
519 if (C1 == From) C1 = To;
520 if (C2 == From) C2 = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000521 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
522 } else {
523 assert(0 && "Unknown ConstantExpr type!");
524 return;
525 }
526
527 assert(Replacement != this && "I didn't contain From!");
528
529 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000530 if (DisableChecking)
531 uncheckedReplaceAllUsesWith(Replacement);
532 else
533 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000534
535 // Delete the old constant!
536 destroyConstant();
537}
538
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000539//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000540// Factory Function Implementation
541
Chris Lattner98fa07b2003-05-23 20:03:32 +0000542// ConstantCreator - A class that is used to create constants by
543// ValueMap*. This class should be partially specialized if there is
544// something strange that needs to be done to interface to the ctor for the
545// constant.
546//
Chris Lattner189d19f2003-11-21 20:23:48 +0000547namespace llvm {
548 template<class ConstantClass, class TypeClass, class ValType>
549 struct ConstantCreator {
550 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
551 return new ConstantClass(Ty, V);
552 }
553 };
554
555 template<class ConstantClass, class TypeClass>
556 struct ConvertConstantType {
557 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
558 assert(0 && "This type cannot be converted!\n");
559 abort();
560 }
561 };
562}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000563
Chris Lattner98fa07b2003-05-23 20:03:32 +0000564namespace {
565 template<class ValType, class TypeClass, class ConstantClass>
Chris Lattnerb50d1352003-10-05 00:17:43 +0000566 class ValueMap : public AbstractTypeUser {
567 typedef std::pair<const TypeClass*, ValType> MapKey;
568 typedef std::map<MapKey, ConstantClass *> MapTy;
569 typedef typename MapTy::iterator MapIterator;
570 MapTy Map;
571
572 typedef std::map<const TypeClass*, MapIterator> AbstractTypeMapTy;
573 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000574 public:
575 // getOrCreate - Return the specified constant from the map, creating it if
576 // necessary.
577 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000578 MapKey Lookup(Ty, V);
579 MapIterator I = Map.lower_bound(Lookup);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000580 if (I != Map.end() && I->first == Lookup)
581 return I->second; // Is it in the map?
582
583 // If no preexisting value, create one now...
584 ConstantClass *Result =
585 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
586
Chris Lattnerb50d1352003-10-05 00:17:43 +0000587
588 /// FIXME: why does this assert fail when loading 176.gcc?
589 //assert(Result->getType() == Ty && "Type specified is not correct!");
590 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
591
592 // If the type of the constant is abstract, make sure that an entry exists
593 // for it in the AbstractTypeMap.
594 if (Ty->isAbstract()) {
595 typename AbstractTypeMapTy::iterator TI =
596 AbstractTypeMap.lower_bound(Ty);
597
598 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
599 // Add ourselves to the ATU list of the type.
600 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
601
602 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
603 }
604 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000605 return Result;
606 }
607
608 void remove(ConstantClass *CP) {
Chris Lattner3e650af2004-08-04 04:48:01 +0000609 MapIterator I = Map.find(MapKey((TypeClass*)CP->getRawType(),
610 getValType(CP)));
Chris Lattner20a4dab2004-08-04 22:26:13 +0000611 if (I == Map.end() || I->second != CP) {
612 // FIXME: This should not use a linear scan. If this gets to be a
613 // performance problem, someone should look at this.
614 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
615 /* empty */;
616 }
617
Chris Lattnerb50d1352003-10-05 00:17:43 +0000618 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +0000619 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +0000620
621 // Now that we found the entry, make sure this isn't the entry that
622 // the AbstractTypeMap points to.
623 const TypeClass *Ty = I->first.first;
624 if (Ty->isAbstract()) {
625 assert(AbstractTypeMap.count(Ty) &&
626 "Abstract type not in AbstractTypeMap?");
627 MapIterator &ATMEntryIt = AbstractTypeMap[Ty];
628 if (ATMEntryIt == I) {
629 // Yes, we are removing the representative entry for this type.
630 // See if there are any other entries of the same type.
631 MapIterator TmpIt = ATMEntryIt;
632
633 // First check the entry before this one...
634 if (TmpIt != Map.begin()) {
635 --TmpIt;
636 if (TmpIt->first.first != Ty) // Not the same type, move back...
637 ++TmpIt;
638 }
639
640 // If we didn't find the same type, try to move forward...
641 if (TmpIt == ATMEntryIt) {
642 ++TmpIt;
643 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
644 --TmpIt; // No entry afterwards with the same type
645 }
646
647 // If there is another entry in the map of the same abstract type,
648 // update the AbstractTypeMap entry now.
649 if (TmpIt != ATMEntryIt) {
650 ATMEntryIt = TmpIt;
651 } else {
652 // Otherwise, we are removing the last instance of this type
653 // from the table. Remove from the ATM, and from user list.
654 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
655 AbstractTypeMap.erase(Ty);
656 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000657 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000658 }
659
660 Map.erase(I);
661 }
662
663 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
664 typename AbstractTypeMapTy::iterator I =
665 AbstractTypeMap.find(cast<TypeClass>(OldTy));
666
667 assert(I != AbstractTypeMap.end() &&
668 "Abstract type not in AbstractTypeMap?");
669
670 // Convert a constant at a time until the last one is gone. The last one
671 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
672 // eliminated eventually.
673 do {
674 ConvertConstantType<ConstantClass,
675 TypeClass>::convert(I->second->second,
676 cast<TypeClass>(NewTy));
677
678 I = AbstractTypeMap.find(cast<TypeClass>(OldTy));
679 } while (I != AbstractTypeMap.end());
680 }
681
682 // If the type became concrete without being refined to any other existing
683 // type, we just remove ourselves from the ATU list.
684 void typeBecameConcrete(const DerivedType *AbsTy) {
685 AbsTy->removeAbstractTypeUser(this);
686 }
687
688 void dump() const {
689 std::cerr << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +0000690 }
691 };
692}
693
Chris Lattner3462ae32001-12-03 22:26:30 +0000694//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000695//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000696static ValueMap< int64_t, Type, ConstantSInt> SIntConstants;
697static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000698
Chris Lattner3462ae32001-12-03 22:26:30 +0000699ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000700 return SIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000701}
702
Chris Lattner3462ae32001-12-03 22:26:30 +0000703ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000704 return UIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000705}
706
Chris Lattner3462ae32001-12-03 22:26:30 +0000707ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000708 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000709 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
710 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000711}
712
Chris Lattner3462ae32001-12-03 22:26:30 +0000713//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000714//
Chris Lattnerac80ea42004-02-01 22:49:04 +0000715namespace llvm {
716 template<>
717 struct ConstantCreator<ConstantFP, Type, uint64_t> {
718 static ConstantFP *create(const Type *Ty, uint64_t V) {
719 assert(Ty == Type::DoubleTy);
720 union {
721 double F;
722 uint64_t I;
723 } T;
724 T.I = V;
725 return new ConstantFP(Ty, T.F);
726 }
727 };
728 template<>
729 struct ConstantCreator<ConstantFP, Type, uint32_t> {
730 static ConstantFP *create(const Type *Ty, uint32_t V) {
731 assert(Ty == Type::FloatTy);
732 union {
733 float F;
734 uint32_t I;
735 } T;
736 T.I = V;
737 return new ConstantFP(Ty, T.F);
738 }
739 };
740}
741
742static ValueMap<uint64_t, Type, ConstantFP> DoubleConstants;
743static ValueMap<uint32_t, Type, ConstantFP> FloatConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000744
Chris Lattner3462ae32001-12-03 22:26:30 +0000745ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner241ed4c2004-01-23 00:55:21 +0000746 if (Ty == Type::FloatTy) {
747 // Force the value through memory to normalize it.
Chris Lattnerac80ea42004-02-01 22:49:04 +0000748 union {
749 float F;
750 uint32_t I;
751 } T;
752 T.F = (float)V;
753 return FloatConstants.getOrCreate(Ty, T.I);
754 } else {
755 assert(Ty == Type::DoubleTy);
756 union {
757 double F;
758 uint64_t I;
759 } T;
760 T.F = V;
761 return DoubleConstants.getOrCreate(Ty, T.I);
Chris Lattner241ed4c2004-01-23 00:55:21 +0000762 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000763}
764
Chris Lattner9fba3da2004-02-15 05:53:04 +0000765//---- ConstantAggregateZero::get() implementation...
766//
767namespace llvm {
768 // ConstantAggregateZero does not take extra "value" argument...
769 template<class ValType>
770 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
771 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
772 return new ConstantAggregateZero(Ty);
773 }
774 };
775
776 template<>
777 struct ConvertConstantType<ConstantAggregateZero, Type> {
778 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
779 // Make everyone now use a constant of the new type...
780 Constant *New = ConstantAggregateZero::get(NewTy);
781 assert(New != OldC && "Didn't replace constant??");
782 OldC->uncheckedReplaceAllUsesWith(New);
783 OldC->destroyConstant(); // This constant is now dead, destroy it.
784 }
785 };
786}
787
788static ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
789
Chris Lattner3e650af2004-08-04 04:48:01 +0000790static char getValType(ConstantAggregateZero *CPZ) { return 0; }
791
Chris Lattner9fba3da2004-02-15 05:53:04 +0000792Constant *ConstantAggregateZero::get(const Type *Ty) {
793 return AggZeroConstants.getOrCreate(Ty, 0);
794}
795
796// destroyConstant - Remove the constant from the constant table...
797//
798void ConstantAggregateZero::destroyConstant() {
799 AggZeroConstants.remove(this);
800 destroyConstantImpl();
801}
802
803void ConstantAggregateZero::replaceUsesOfWithOnConstant(Value *From, Value *To,
804 bool DisableChecking) {
805 assert(0 && "No uses!");
806 abort();
807}
808
809
810
Chris Lattner3462ae32001-12-03 22:26:30 +0000811//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000812//
Chris Lattner189d19f2003-11-21 20:23:48 +0000813namespace llvm {
814 template<>
815 struct ConvertConstantType<ConstantArray, ArrayType> {
816 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
817 // Make everyone now use a constant of the new type...
818 std::vector<Constant*> C;
819 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
820 C.push_back(cast<Constant>(OldC->getOperand(i)));
821 Constant *New = ConstantArray::get(NewTy, C);
822 assert(New != OldC && "Didn't replace constant??");
823 OldC->uncheckedReplaceAllUsesWith(New);
824 OldC->destroyConstant(); // This constant is now dead, destroy it.
825 }
826 };
827}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000828
Chris Lattner3e650af2004-08-04 04:48:01 +0000829static std::vector<Constant*> getValType(ConstantArray *CA) {
830 std::vector<Constant*> Elements;
831 Elements.reserve(CA->getNumOperands());
832 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
833 Elements.push_back(cast<Constant>(CA->getOperand(i)));
834 return Elements;
835}
836
Chris Lattner98fa07b2003-05-23 20:03:32 +0000837static ValueMap<std::vector<Constant*>, ArrayType,
838 ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000839
Chris Lattner015e8212004-02-15 04:14:47 +0000840Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +0000841 const std::vector<Constant*> &V) {
842 // If this is an all-zero array, return a ConstantAggregateZero object
843 if (!V.empty()) {
844 Constant *C = V[0];
845 if (!C->isNullValue())
846 return ArrayConstants.getOrCreate(Ty, V);
847 for (unsigned i = 1, e = V.size(); i != e; ++i)
848 if (V[i] != C)
849 return ArrayConstants.getOrCreate(Ty, V);
850 }
851 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000852}
853
Chris Lattner98fa07b2003-05-23 20:03:32 +0000854// destroyConstant - Remove the constant from the constant table...
855//
856void ConstantArray::destroyConstant() {
857 ArrayConstants.remove(this);
858 destroyConstantImpl();
859}
860
Chris Lattner3462ae32001-12-03 22:26:30 +0000861// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000862// contain the specified string. A null terminator is added to the specified
863// string so that it may be used in a natural way...
864//
Chris Lattner015e8212004-02-15 04:14:47 +0000865Constant *ConstantArray::get(const std::string &Str) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000866 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000867
868 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000869 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000870
871 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000872 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000873
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000874 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000875 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000876}
877
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000878/// isString - This method returns true if the array is an array of sbyte or
879/// ubyte, and if the elements of the array are all ConstantInt's.
880bool ConstantArray::isString() const {
881 // Check the element type for sbyte or ubyte...
Chris Lattnere8701f62004-01-14 17:51:53 +0000882 if (getType()->getElementType() != Type::UByteTy &&
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000883 getType()->getElementType() != Type::SByteTy)
884 return false;
885 // Check the elements to make sure they are all integers, not constant
886 // expressions.
887 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
888 if (!isa<ConstantInt>(getOperand(i)))
889 return false;
890 return true;
891}
892
Chris Lattner81fabb02002-08-26 17:53:56 +0000893// getAsString - If the sub-element type of this array is either sbyte or ubyte,
894// then this method converts the array to an std::string and returns it.
895// Otherwise, it asserts out.
896//
897std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000898 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +0000899 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +0000900 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
901 Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue();
Chris Lattner81fabb02002-08-26 17:53:56 +0000902 return Result;
903}
904
905
Chris Lattner3462ae32001-12-03 22:26:30 +0000906//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000907//
Chris Lattnerb50d1352003-10-05 00:17:43 +0000908
Chris Lattner189d19f2003-11-21 20:23:48 +0000909namespace llvm {
910 template<>
911 struct ConvertConstantType<ConstantStruct, StructType> {
912 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
913 // Make everyone now use a constant of the new type...
914 std::vector<Constant*> C;
915 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
916 C.push_back(cast<Constant>(OldC->getOperand(i)));
917 Constant *New = ConstantStruct::get(NewTy, C);
918 assert(New != OldC && "Didn't replace constant??");
919
920 OldC->uncheckedReplaceAllUsesWith(New);
921 OldC->destroyConstant(); // This constant is now dead, destroy it.
922 }
923 };
924}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000925
Chris Lattner98fa07b2003-05-23 20:03:32 +0000926static ValueMap<std::vector<Constant*>, StructType,
927 ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000928
Chris Lattner3e650af2004-08-04 04:48:01 +0000929static std::vector<Constant*> getValType(ConstantStruct *CS) {
930 std::vector<Constant*> Elements;
931 Elements.reserve(CS->getNumOperands());
932 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
933 Elements.push_back(cast<Constant>(CS->getOperand(i)));
934 return Elements;
935}
936
Chris Lattner015e8212004-02-15 04:14:47 +0000937Constant *ConstantStruct::get(const StructType *Ty,
938 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +0000939 // Create a ConstantAggregateZero value if all elements are zeros...
940 for (unsigned i = 0, e = V.size(); i != e; ++i)
941 if (!V[i]->isNullValue())
942 return StructConstants.getOrCreate(Ty, V);
943
944 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000945}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000946
Chris Lattnerd6108ca2004-07-12 20:35:11 +0000947Constant *ConstantStruct::get(const std::vector<Constant*> &V) {
948 std::vector<const Type*> StructEls;
949 StructEls.reserve(V.size());
950 for (unsigned i = 0, e = V.size(); i != e; ++i)
951 StructEls.push_back(V[i]->getType());
952 return get(StructType::get(StructEls), V);
953}
954
Chris Lattnerd7a73302001-10-13 06:57:33 +0000955// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000956//
Chris Lattner3462ae32001-12-03 22:26:30 +0000957void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000958 StructConstants.remove(this);
959 destroyConstantImpl();
960}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000961
Chris Lattner3462ae32001-12-03 22:26:30 +0000962//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000963//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000964
Chris Lattner189d19f2003-11-21 20:23:48 +0000965namespace llvm {
966 // ConstantPointerNull does not take extra "value" argument...
967 template<class ValType>
968 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
969 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
970 return new ConstantPointerNull(Ty);
971 }
972 };
Chris Lattner98fa07b2003-05-23 20:03:32 +0000973
Chris Lattner189d19f2003-11-21 20:23:48 +0000974 template<>
975 struct ConvertConstantType<ConstantPointerNull, PointerType> {
976 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
977 // Make everyone now use a constant of the new type...
978 Constant *New = ConstantPointerNull::get(NewTy);
979 assert(New != OldC && "Didn't replace constant??");
980 OldC->uncheckedReplaceAllUsesWith(New);
981 OldC->destroyConstant(); // This constant is now dead, destroy it.
982 }
983 };
984}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000985
Chris Lattner98fa07b2003-05-23 20:03:32 +0000986static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +0000987
Chris Lattner3e650af2004-08-04 04:48:01 +0000988static char getValType(ConstantPointerNull *) {
989 return 0;
990}
991
992
Chris Lattner3462ae32001-12-03 22:26:30 +0000993ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000994 return NullPtrConstants.getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +0000995}
996
Chris Lattner0c6e0b92002-08-18 00:40:04 +0000997// destroyConstant - Remove the constant from the constant table...
998//
999void ConstantPointerNull::destroyConstant() {
1000 NullPtrConstants.remove(this);
1001 destroyConstantImpl();
1002}
1003
1004
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001005//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001006//
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001007typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Chris Lattner98fa07b2003-05-23 20:03:32 +00001008
Chris Lattner189d19f2003-11-21 20:23:48 +00001009namespace llvm {
1010 template<>
1011 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
1012 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
1013 if (V.first == Instruction::Cast)
1014 return new ConstantExpr(Instruction::Cast, V.second[0], Ty);
1015 if ((V.first >= Instruction::BinaryOpsBegin &&
1016 V.first < Instruction::BinaryOpsEnd) ||
1017 V.first == Instruction::Shl || V.first == Instruction::Shr)
1018 return new ConstantExpr(V.first, V.second[0], V.second[1]);
Chris Lattnerb7897ce2004-03-30 22:51:03 +00001019 if (V.first == Instruction::Select)
1020 return new ConstantExpr(V.second[0], V.second[1], V.second[2]);
Chris Lattner189d19f2003-11-21 20:23:48 +00001021
1022 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
1023
1024 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
1025 return new ConstantExpr(V.second[0], IdxList, Ty);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001026 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001027 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001028
Chris Lattner189d19f2003-11-21 20:23:48 +00001029 template<>
1030 struct ConvertConstantType<ConstantExpr, Type> {
1031 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1032 Constant *New;
1033 switch (OldC->getOpcode()) {
1034 case Instruction::Cast:
1035 New = ConstantExpr::getCast(OldC->getOperand(0), NewTy);
1036 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001037 case Instruction::Select:
1038 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1039 OldC->getOperand(1),
1040 OldC->getOperand(2));
1041 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001042 case Instruction::Shl:
1043 case Instruction::Shr:
1044 New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
1045 OldC->getOperand(0), OldC->getOperand(1));
1046 break;
1047 default:
1048 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
1049 OldC->getOpcode() < Instruction::BinaryOpsEnd);
1050 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1051 OldC->getOperand(1));
1052 break;
1053 case Instruction::GetElementPtr:
1054 // Make everyone now use a constant of the new type...
1055 std::vector<Constant*> C;
1056 for (unsigned i = 1, e = OldC->getNumOperands(); i != e; ++i)
1057 C.push_back(cast<Constant>(OldC->getOperand(i)));
1058 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), C);
1059 break;
1060 }
1061
1062 assert(New != OldC && "Didn't replace constant??");
1063 OldC->uncheckedReplaceAllUsesWith(New);
1064 OldC->destroyConstant(); // This constant is now dead, destroy it.
1065 }
1066 };
1067} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001068
1069
Chris Lattner3e650af2004-08-04 04:48:01 +00001070static ExprMapKeyType getValType(ConstantExpr *CE) {
1071 std::vector<Constant*> Operands;
1072 Operands.reserve(CE->getNumOperands());
1073 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1074 Operands.push_back(cast<Constant>(CE->getOperand(i)));
1075 return ExprMapKeyType(CE->getOpcode(), Operands);
1076}
1077
Chris Lattner98fa07b2003-05-23 20:03:32 +00001078static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001079
Chris Lattner46b3d302003-04-16 22:40:51 +00001080Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001081 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1082
Chris Lattneracdbe712003-04-17 19:24:48 +00001083 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
1084 return FC; // Fold a few common cases...
1085
Vikram S. Adve4c485332002-07-15 18:19:33 +00001086 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001087 std::vector<Constant*> argVec(1, C);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001088 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
1089 return ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001090}
Chris Lattnerd7a73302001-10-13 06:57:33 +00001091
Chris Lattnerdd284742004-04-04 23:20:30 +00001092Constant *ConstantExpr::getSignExtend(Constant *C, const Type *Ty) {
1093 assert(C->getType()->isInteger() && Ty->isInteger() &&
1094 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1095 "This is an illegal sign extension!");
1096 C = ConstantExpr::getCast(C, C->getType()->getSignedVersion());
1097 return ConstantExpr::getCast(C, Ty);
1098}
1099
1100Constant *ConstantExpr::getZeroExtend(Constant *C, const Type *Ty) {
1101 assert(C->getType()->isInteger() && Ty->isInteger() &&
1102 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1103 "This is an illegal zero extension!");
1104 C = ConstantExpr::getCast(C, C->getType()->getUnsignedVersion());
1105 return ConstantExpr::getCast(C, Ty);
1106}
1107
Chris Lattnerb50d1352003-10-05 00:17:43 +00001108Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
1109 Constant *C1, Constant *C2) {
Chris Lattner5645e8a2004-01-12 19:04:55 +00001110 if (Opcode == Instruction::Shl || Opcode == Instruction::Shr)
1111 return getShiftTy(ReqTy, Opcode, C1, C2);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001112 // Check the operands for consistency first
1113 assert((Opcode >= Instruction::BinaryOpsBegin &&
1114 Opcode < Instruction::BinaryOpsEnd) &&
1115 "Invalid opcode in binary constant expression");
1116 assert(C1->getType() == C2->getType() &&
1117 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001118
Chris Lattner29ca2c62004-08-04 18:50:09 +00001119 if (ReqTy == C1->getType() || (Instruction::isRelational(Opcode) &&
1120 ReqTy == Type::BoolTy))
Chris Lattnerb50d1352003-10-05 00:17:43 +00001121 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1122 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001123
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001124 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001125 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001126 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001127}
1128
Chris Lattner29ca2c62004-08-04 18:50:09 +00001129Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
1130 if (Instruction::isRelational(Opcode))
1131 return getTy(Type::BoolTy, Opcode, C1, C2);
1132 else
1133 return getTy(C1->getType(), Opcode, C1, C2);
1134}
1135
Chris Lattner6e415c02004-03-12 05:54:04 +00001136Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
1137 Constant *V1, Constant *V2) {
1138 assert(C->getType() == Type::BoolTy && "Select condition must be bool!");
1139 assert(V1->getType() == V2->getType() && "Select value types must match!");
1140 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
1141
1142 if (ReqTy == V1->getType())
1143 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1144 return SC; // Fold common cases
1145
1146 std::vector<Constant*> argVec(3, C);
1147 argVec[1] = V1;
1148 argVec[2] = V2;
1149 ExprMapKeyType Key = std::make_pair(Instruction::Select, argVec);
1150 return ExprConstants.getOrCreate(ReqTy, Key);
1151}
1152
Chris Lattner9eb2b522004-01-12 19:12:58 +00001153/// getShiftTy - Return a shift left or shift right constant expr
Chris Lattnerb50d1352003-10-05 00:17:43 +00001154Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
1155 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001156 // Check the operands for consistency first
1157 assert((Opcode == Instruction::Shl ||
1158 Opcode == Instruction::Shr) &&
1159 "Invalid opcode in binary constant expression");
1160 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
1161 "Invalid operand types for Shift constant expr!");
1162
Chris Lattner0bba7712004-01-12 20:40:42 +00001163 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001164 return FC; // Fold a few common cases...
1165
1166 // Look up the constant in the table first to ensure uniqueness
1167 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001168 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001169 return ExprConstants.getOrCreate(ReqTy, Key);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001170}
1171
1172
Chris Lattnerb50d1352003-10-05 00:17:43 +00001173Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
1174 const std::vector<Constant*> &IdxList) {
Chris Lattner04b60fe2004-02-16 20:46:13 +00001175 assert(GetElementPtrInst::getIndexedType(C->getType(),
1176 std::vector<Value*>(IdxList.begin(), IdxList.end()), true) &&
1177 "GEP indices invalid!");
1178
Chris Lattneracdbe712003-04-17 19:24:48 +00001179 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
1180 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001181
Chris Lattnerb50d1352003-10-05 00:17:43 +00001182 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001183 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001184 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001185 std::vector<Constant*> argVec(1, C);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +00001186 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001187 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001188 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001189}
1190
Chris Lattnerb50d1352003-10-05 00:17:43 +00001191Constant *ConstantExpr::getGetElementPtr(Constant *C,
1192 const std::vector<Constant*> &IdxList){
1193 // Get the result type of the getelementptr!
1194 std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
1195
1196 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
1197 true);
1198 assert(Ty && "GEP indices invalid!");
1199 return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
1200}
1201
1202
Vikram S. Adve4c485332002-07-15 18:19:33 +00001203// destroyConstant - Remove the constant from the constant table...
1204//
1205void ConstantExpr::destroyConstant() {
1206 ExprConstants.remove(this);
1207 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001208}
1209
Chris Lattner3cd8c562002-07-30 18:54:25 +00001210const char *ConstantExpr::getOpcodeName() const {
1211 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001212}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00001213