blob: ff944877a78fdbe37688a957480ed7cabc77c071 [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"
Vikram S. Adve4e537b22002-07-14 23:13:17 +000018#include "llvm/iMemory.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)
272 : Constant(Ty), 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)
279 : Constant(V1->getType()), iType(Instruction::Select) {
280 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 Lattner22ced562003-06-22 20:48:30 +0000294 : Constant(isSetCC(Opcode) ? Type::BoolTy : C1->getType()), iType(Opcode) {
Chris Lattner6e415c02004-03-12 05:54:04 +0000295 Operands.reserve(2);
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000296 Operands.push_back(Use(C1, this));
297 Operands.push_back(Use(C2, this));
298}
299
Chris Lattner3cd8c562002-07-30 18:54:25 +0000300ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
301 const Type *DestTy)
302 : Constant(DestTy), iType(Instruction::GetElementPtr) {
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000303 Operands.reserve(1+IdxList.size());
304 Operands.push_back(Use(C, this));
305 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
306 Operands.push_back(Use(IdxList[i], this));
307}
308
Chris Lattner817175f2004-03-29 02:37:53 +0000309/// ConstantExpr::get* - Return some common constants without having to
310/// specify the full Instruction::OPCODE identifier.
311///
312Constant *ConstantExpr::getNeg(Constant *C) {
Chris Lattner3cdc27c2004-03-29 19:51:24 +0000313 if (!C->getType()->isFloatingPoint())
314 return get(Instruction::Sub, getNullValue(C->getType()), C);
315 else
316 return get(Instruction::Sub, ConstantFP::get(C->getType(), -0.0), C);
Chris Lattner817175f2004-03-29 02:37:53 +0000317}
318Constant *ConstantExpr::getNot(Constant *C) {
319 assert(isa<ConstantIntegral>(C) && "Cannot NOT a nonintegral type!");
320 return get(Instruction::Xor, C,
321 ConstantIntegral::getAllOnesValue(C->getType()));
322}
323Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
324 return get(Instruction::Add, C1, C2);
325}
326Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
327 return get(Instruction::Sub, C1, C2);
328}
329Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
330 return get(Instruction::Mul, C1, C2);
331}
332Constant *ConstantExpr::getDiv(Constant *C1, Constant *C2) {
333 return get(Instruction::Div, C1, C2);
334}
335Constant *ConstantExpr::getRem(Constant *C1, Constant *C2) {
336 return get(Instruction::Rem, C1, C2);
337}
338Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
339 return get(Instruction::And, C1, C2);
340}
341Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
342 return get(Instruction::Or, C1, C2);
343}
344Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
345 return get(Instruction::Xor, C1, C2);
346}
347Constant *ConstantExpr::getSetEQ(Constant *C1, Constant *C2) {
348 return get(Instruction::SetEQ, C1, C2);
349}
350Constant *ConstantExpr::getSetNE(Constant *C1, Constant *C2) {
351 return get(Instruction::SetNE, C1, C2);
352}
353Constant *ConstantExpr::getSetLT(Constant *C1, Constant *C2) {
354 return get(Instruction::SetLT, C1, C2);
355}
356Constant *ConstantExpr::getSetGT(Constant *C1, Constant *C2) {
357 return get(Instruction::SetGT, C1, C2);
358}
359Constant *ConstantExpr::getSetLE(Constant *C1, Constant *C2) {
360 return get(Instruction::SetLE, C1, C2);
361}
362Constant *ConstantExpr::getSetGE(Constant *C1, Constant *C2) {
363 return get(Instruction::SetGE, C1, C2);
364}
365Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
366 return get(Instruction::Shl, C1, C2);
367}
368Constant *ConstantExpr::getShr(Constant *C1, Constant *C2) {
369 return get(Instruction::Shr, C1, C2);
370}
371
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000372Constant *ConstantExpr::getUShr(Constant *C1, Constant *C2) {
373 if (C1->getType()->isUnsigned()) return getShr(C1, C2);
374 return getCast(getShr(getCast(C1,
375 C1->getType()->getUnsignedVersion()), C2), C1->getType());
376}
Chris Lattner817175f2004-03-29 02:37:53 +0000377
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000378Constant *ConstantExpr::getSShr(Constant *C1, Constant *C2) {
379 if (C1->getType()->isSigned()) return getShr(C1, C2);
380 return getCast(getShr(getCast(C1,
381 C1->getType()->getSignedVersion()), C2), C1->getType());
382}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000383
Chris Lattner2f7c9632001-06-06 20:29:01 +0000384
385//===----------------------------------------------------------------------===//
Chris Lattnerd7a73302001-10-13 06:57:33 +0000386// classof implementations
387
Chris Lattnerb1585a92002-08-13 17:50:20 +0000388bool ConstantIntegral::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000389 return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
Chris Lattner41e99a02002-08-12 21:21:21 +0000390}
391
Chris Lattner3462ae32001-12-03 22:26:30 +0000392bool ConstantInt::classof(const Constant *CPV) {
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000393 return CPV->getType()->isInteger() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000394}
Chris Lattner3462ae32001-12-03 22:26:30 +0000395bool ConstantSInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000396 return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000397}
Chris Lattner3462ae32001-12-03 22:26:30 +0000398bool ConstantUInt::classof(const Constant *CPV) {
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000399 return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV);
Chris Lattnerd7a73302001-10-13 06:57:33 +0000400}
Chris Lattner3462ae32001-12-03 22:26:30 +0000401bool ConstantFP::classof(const Constant *CPV) {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000402 const Type *Ty = CPV->getType();
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000403 return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
Chris Lattnerd9f4ac662002-07-18 00:14:50 +0000404 !isa<ConstantExpr>(CPV));
Chris Lattnerd7a73302001-10-13 06:57:33 +0000405}
Chris Lattner9fba3da2004-02-15 05:53:04 +0000406bool ConstantAggregateZero::classof(const Constant *CPV) {
407 return (isa<ArrayType>(CPV->getType()) || isa<StructType>(CPV->getType())) &&
408 CPV->isNullValue();
409}
Chris Lattner3462ae32001-12-03 22:26:30 +0000410bool ConstantArray::classof(const Constant *CPV) {
Chris Lattner9fba3da2004-02-15 05:53:04 +0000411 return isa<ArrayType>(CPV->getType()) && !CPV->isNullValue();
Chris Lattnerd7a73302001-10-13 06:57:33 +0000412}
Chris Lattner3462ae32001-12-03 22:26:30 +0000413bool ConstantStruct::classof(const Constant *CPV) {
Chris Lattner9fba3da2004-02-15 05:53:04 +0000414 return isa<StructType>(CPV->getType()) && !CPV->isNullValue();
Chris Lattnerd7a73302001-10-13 06:57:33 +0000415}
Chris Lattnere120a732003-11-17 19:47:21 +0000416
417bool ConstantPointerNull::classof(const Constant *CPV) {
Reid Spencer1ebe1ab2004-07-17 23:48:33 +0000418 return !isa<GlobalValue>(CPV) && isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV) &&
Chris Lattnere120a732003-11-17 19:47:21 +0000419 CPV->getNumOperands() == 0;
420}
421
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000422
Chris Lattner2f7c9632001-06-06 20:29:01 +0000423//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000424// isValueValidForType implementations
425
Chris Lattner3462ae32001-12-03 22:26:30 +0000426bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000427 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000428 default:
429 return false; // These can't be represented as integers!!!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000430 // Signed types...
431 case Type::SByteTyID:
432 return (Val <= INT8_MAX && Val >= INT8_MIN);
433 case Type::ShortTyID:
434 return (Val <= INT16_MAX && Val >= INT16_MIN);
435 case Type::IntTyID:
Chris Lattner74248512004-06-08 23:21:39 +0000436 return (Val <= int(INT32_MAX) && Val >= int(INT32_MIN));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000437 case Type::LongTyID:
438 return true; // This is the largest type...
439 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000440}
441
Chris Lattner3462ae32001-12-03 22:26:30 +0000442bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000443 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000444 default:
445 return false; // These can't be represented as integers!!!
446
447 // Unsigned types...
448 case Type::UByteTyID:
449 return (Val <= UINT8_MAX);
450 case Type::UShortTyID:
451 return (Val <= UINT16_MAX);
452 case Type::UIntTyID:
453 return (Val <= UINT32_MAX);
454 case Type::ULongTyID:
455 return true; // This is the largest type...
456 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000457}
458
Chris Lattner3462ae32001-12-03 22:26:30 +0000459bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000460 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000461 default:
462 return false; // These can't be represented as floating point!
463
464 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000465 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000466 case Type::DoubleTyID:
467 return true; // This is the largest type...
468 }
469};
Chris Lattner9655e542001-07-20 19:16:02 +0000470
Chris Lattner49d855c2001-09-07 16:46:31 +0000471//===----------------------------------------------------------------------===//
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000472// replaceUsesOfWithOnConstant implementations
473
Chris Lattnerc27038d2003-08-29 05:36:46 +0000474void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
475 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000476 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
477
478 std::vector<Constant*> Values;
479 Values.reserve(getValues().size()); // Build replacement array...
480 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
481 Constant *Val = cast<Constant>(getValues()[i]);
482 if (Val == From) Val = cast<Constant>(To);
483 Values.push_back(Val);
484 }
485
Chris Lattnerc75bf522004-02-15 04:05:58 +0000486 Constant *Replacement = ConstantArray::get(getType(), Values);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000487 assert(Replacement != this && "I didn't contain From!");
488
489 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000490 if (DisableChecking)
491 uncheckedReplaceAllUsesWith(Replacement);
492 else
493 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000494
495 // Delete the old constant!
496 destroyConstant();
497}
498
Chris Lattnerc27038d2003-08-29 05:36:46 +0000499void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
500 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000501 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
502
503 std::vector<Constant*> Values;
504 Values.reserve(getValues().size());
505 for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
506 Constant *Val = cast<Constant>(getValues()[i]);
507 if (Val == From) Val = cast<Constant>(To);
508 Values.push_back(Val);
509 }
510
Chris Lattner37a716f2004-02-15 04:07:32 +0000511 Constant *Replacement = ConstantStruct::get(getType(), Values);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000512 assert(Replacement != this && "I didn't contain From!");
513
514 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000515 if (DisableChecking)
516 uncheckedReplaceAllUsesWith(Replacement);
517 else
518 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000519
520 // Delete the old constant!
521 destroyConstant();
522}
523
Chris Lattnerc27038d2003-08-29 05:36:46 +0000524void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
525 bool DisableChecking) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000526 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
527 Constant *To = cast<Constant>(ToV);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000528
Chris Lattner46b3d302003-04-16 22:40:51 +0000529 Constant *Replacement = 0;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000530 if (getOpcode() == Instruction::GetElementPtr) {
531 std::vector<Constant*> Indices;
Chris Lattner55ed6562003-05-14 17:51:05 +0000532 Constant *Pointer = getOperand(0);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000533 Indices.reserve(getNumOperands()-1);
Chris Lattner55ed6562003-05-14 17:51:05 +0000534 if (Pointer == From) Pointer = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000535
536 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000537 Constant *Val = getOperand(i);
538 if (Val == From) Val = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000539 Indices.push_back(Val);
540 }
541 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
542 } else if (getOpcode() == Instruction::Cast) {
543 assert(getOperand(0) == From && "Cast only has one use!");
Chris Lattner55ed6562003-05-14 17:51:05 +0000544 Replacement = ConstantExpr::getCast(To, getType());
Chris Lattner467cb2b2004-03-31 02:56:11 +0000545 } else if (getOpcode() == Instruction::Select) {
546 Constant *C1 = getOperand(0);
547 Constant *C2 = getOperand(1);
548 Constant *C3 = getOperand(2);
549 if (C1 == From) C1 = To;
550 if (C2 == From) C2 = To;
551 if (C3 == From) C3 = To;
552 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000553 } else if (getNumOperands() == 2) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000554 Constant *C1 = getOperand(0);
555 Constant *C2 = getOperand(1);
556 if (C1 == From) C1 = To;
557 if (C2 == From) C2 = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000558 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
559 } else {
560 assert(0 && "Unknown ConstantExpr type!");
561 return;
562 }
563
564 assert(Replacement != this && "I didn't contain From!");
565
566 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000567 if (DisableChecking)
568 uncheckedReplaceAllUsesWith(Replacement);
569 else
570 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000571
572 // Delete the old constant!
573 destroyConstant();
574}
575
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000576//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000577// Factory Function Implementation
578
Chris Lattner98fa07b2003-05-23 20:03:32 +0000579// ConstantCreator - A class that is used to create constants by
580// ValueMap*. This class should be partially specialized if there is
581// something strange that needs to be done to interface to the ctor for the
582// constant.
583//
Chris Lattner189d19f2003-11-21 20:23:48 +0000584namespace llvm {
585 template<class ConstantClass, class TypeClass, class ValType>
586 struct ConstantCreator {
587 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
588 return new ConstantClass(Ty, V);
589 }
590 };
591
592 template<class ConstantClass, class TypeClass>
593 struct ConvertConstantType {
594 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
595 assert(0 && "This type cannot be converted!\n");
596 abort();
597 }
598 };
599}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000600
Chris Lattner98fa07b2003-05-23 20:03:32 +0000601namespace {
602 template<class ValType, class TypeClass, class ConstantClass>
Chris Lattnerb50d1352003-10-05 00:17:43 +0000603 class ValueMap : public AbstractTypeUser {
604 typedef std::pair<const TypeClass*, ValType> MapKey;
605 typedef std::map<MapKey, ConstantClass *> MapTy;
606 typedef typename MapTy::iterator MapIterator;
607 MapTy Map;
608
609 typedef std::map<const TypeClass*, MapIterator> AbstractTypeMapTy;
610 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner98fa07b2003-05-23 20:03:32 +0000611 public:
612 // getOrCreate - Return the specified constant from the map, creating it if
613 // necessary.
614 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000615 MapKey Lookup(Ty, V);
616 MapIterator I = Map.lower_bound(Lookup);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000617 if (I != Map.end() && I->first == Lookup)
618 return I->second; // Is it in the map?
619
620 // If no preexisting value, create one now...
621 ConstantClass *Result =
622 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
623
Chris Lattnerb50d1352003-10-05 00:17:43 +0000624
625 /// FIXME: why does this assert fail when loading 176.gcc?
626 //assert(Result->getType() == Ty && "Type specified is not correct!");
627 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
628
629 // If the type of the constant is abstract, make sure that an entry exists
630 // for it in the AbstractTypeMap.
631 if (Ty->isAbstract()) {
632 typename AbstractTypeMapTy::iterator TI =
633 AbstractTypeMap.lower_bound(Ty);
634
635 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
636 // Add ourselves to the ATU list of the type.
637 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
638
639 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
640 }
641 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000642 return Result;
643 }
644
645 void remove(ConstantClass *CP) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000646 // FIXME: This should not use a linear scan. If this gets to be a
647 // performance problem, someone should look at this.
648 MapIterator I = Map.begin();
649 for (MapIterator E = Map.end(); I != E && I->second != CP; ++I)
650 /* empty */;
651
652 assert(I != Map.end() && "Constant not found in constant table!");
653
654 // Now that we found the entry, make sure this isn't the entry that
655 // the AbstractTypeMap points to.
656 const TypeClass *Ty = I->first.first;
657 if (Ty->isAbstract()) {
658 assert(AbstractTypeMap.count(Ty) &&
659 "Abstract type not in AbstractTypeMap?");
660 MapIterator &ATMEntryIt = AbstractTypeMap[Ty];
661 if (ATMEntryIt == I) {
662 // Yes, we are removing the representative entry for this type.
663 // See if there are any other entries of the same type.
664 MapIterator TmpIt = ATMEntryIt;
665
666 // First check the entry before this one...
667 if (TmpIt != Map.begin()) {
668 --TmpIt;
669 if (TmpIt->first.first != Ty) // Not the same type, move back...
670 ++TmpIt;
671 }
672
673 // If we didn't find the same type, try to move forward...
674 if (TmpIt == ATMEntryIt) {
675 ++TmpIt;
676 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
677 --TmpIt; // No entry afterwards with the same type
678 }
679
680 // If there is another entry in the map of the same abstract type,
681 // update the AbstractTypeMap entry now.
682 if (TmpIt != ATMEntryIt) {
683 ATMEntryIt = TmpIt;
684 } else {
685 // Otherwise, we are removing the last instance of this type
686 // from the table. Remove from the ATM, and from user list.
687 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
688 AbstractTypeMap.erase(Ty);
689 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000690 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000691 }
692
693 Map.erase(I);
694 }
695
696 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
697 typename AbstractTypeMapTy::iterator I =
698 AbstractTypeMap.find(cast<TypeClass>(OldTy));
699
700 assert(I != AbstractTypeMap.end() &&
701 "Abstract type not in AbstractTypeMap?");
702
703 // Convert a constant at a time until the last one is gone. The last one
704 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
705 // eliminated eventually.
706 do {
707 ConvertConstantType<ConstantClass,
708 TypeClass>::convert(I->second->second,
709 cast<TypeClass>(NewTy));
710
711 I = AbstractTypeMap.find(cast<TypeClass>(OldTy));
712 } while (I != AbstractTypeMap.end());
713 }
714
715 // If the type became concrete without being refined to any other existing
716 // type, we just remove ourselves from the ATU list.
717 void typeBecameConcrete(const DerivedType *AbsTy) {
718 AbsTy->removeAbstractTypeUser(this);
719 }
720
721 void dump() const {
722 std::cerr << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +0000723 }
724 };
725}
726
727
728
Chris Lattner3462ae32001-12-03 22:26:30 +0000729//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000730//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000731static ValueMap< int64_t, Type, ConstantSInt> SIntConstants;
732static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000733
Chris Lattner3462ae32001-12-03 22:26:30 +0000734ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000735 return SIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000736}
737
Chris Lattner3462ae32001-12-03 22:26:30 +0000738ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000739 return UIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000740}
741
Chris Lattner3462ae32001-12-03 22:26:30 +0000742ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000743 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000744 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
745 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000746}
747
Chris Lattner3462ae32001-12-03 22:26:30 +0000748//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000749//
Chris Lattnerac80ea42004-02-01 22:49:04 +0000750namespace llvm {
751 template<>
752 struct ConstantCreator<ConstantFP, Type, uint64_t> {
753 static ConstantFP *create(const Type *Ty, uint64_t V) {
754 assert(Ty == Type::DoubleTy);
755 union {
756 double F;
757 uint64_t I;
758 } T;
759 T.I = V;
760 return new ConstantFP(Ty, T.F);
761 }
762 };
763 template<>
764 struct ConstantCreator<ConstantFP, Type, uint32_t> {
765 static ConstantFP *create(const Type *Ty, uint32_t V) {
766 assert(Ty == Type::FloatTy);
767 union {
768 float F;
769 uint32_t I;
770 } T;
771 T.I = V;
772 return new ConstantFP(Ty, T.F);
773 }
774 };
775}
776
777static ValueMap<uint64_t, Type, ConstantFP> DoubleConstants;
778static ValueMap<uint32_t, Type, ConstantFP> FloatConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000779
Chris Lattner3462ae32001-12-03 22:26:30 +0000780ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner241ed4c2004-01-23 00:55:21 +0000781 if (Ty == Type::FloatTy) {
782 // Force the value through memory to normalize it.
Chris Lattnerac80ea42004-02-01 22:49:04 +0000783 union {
784 float F;
785 uint32_t I;
786 } T;
787 T.F = (float)V;
788 return FloatConstants.getOrCreate(Ty, T.I);
789 } else {
790 assert(Ty == Type::DoubleTy);
791 union {
792 double F;
793 uint64_t I;
794 } T;
795 T.F = V;
796 return DoubleConstants.getOrCreate(Ty, T.I);
Chris Lattner241ed4c2004-01-23 00:55:21 +0000797 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000798}
799
Chris Lattner9fba3da2004-02-15 05:53:04 +0000800//---- ConstantAggregateZero::get() implementation...
801//
802namespace llvm {
803 // ConstantAggregateZero does not take extra "value" argument...
804 template<class ValType>
805 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
806 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
807 return new ConstantAggregateZero(Ty);
808 }
809 };
810
811 template<>
812 struct ConvertConstantType<ConstantAggregateZero, Type> {
813 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
814 // Make everyone now use a constant of the new type...
815 Constant *New = ConstantAggregateZero::get(NewTy);
816 assert(New != OldC && "Didn't replace constant??");
817 OldC->uncheckedReplaceAllUsesWith(New);
818 OldC->destroyConstant(); // This constant is now dead, destroy it.
819 }
820 };
821}
822
823static ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
824
825Constant *ConstantAggregateZero::get(const Type *Ty) {
826 return AggZeroConstants.getOrCreate(Ty, 0);
827}
828
829// destroyConstant - Remove the constant from the constant table...
830//
831void ConstantAggregateZero::destroyConstant() {
832 AggZeroConstants.remove(this);
833 destroyConstantImpl();
834}
835
836void ConstantAggregateZero::replaceUsesOfWithOnConstant(Value *From, Value *To,
837 bool DisableChecking) {
838 assert(0 && "No uses!");
839 abort();
840}
841
842
843
Chris Lattner3462ae32001-12-03 22:26:30 +0000844//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000845//
Chris Lattner189d19f2003-11-21 20:23:48 +0000846namespace llvm {
847 template<>
848 struct ConvertConstantType<ConstantArray, ArrayType> {
849 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
850 // Make everyone now use a constant of the new type...
851 std::vector<Constant*> C;
852 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
853 C.push_back(cast<Constant>(OldC->getOperand(i)));
854 Constant *New = ConstantArray::get(NewTy, C);
855 assert(New != OldC && "Didn't replace constant??");
856 OldC->uncheckedReplaceAllUsesWith(New);
857 OldC->destroyConstant(); // This constant is now dead, destroy it.
858 }
859 };
860}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000861
Chris Lattner98fa07b2003-05-23 20:03:32 +0000862static ValueMap<std::vector<Constant*>, ArrayType,
863 ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000864
Chris Lattner015e8212004-02-15 04:14:47 +0000865Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +0000866 const std::vector<Constant*> &V) {
867 // If this is an all-zero array, return a ConstantAggregateZero object
868 if (!V.empty()) {
869 Constant *C = V[0];
870 if (!C->isNullValue())
871 return ArrayConstants.getOrCreate(Ty, V);
872 for (unsigned i = 1, e = V.size(); i != e; ++i)
873 if (V[i] != C)
874 return ArrayConstants.getOrCreate(Ty, V);
875 }
876 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000877}
878
Chris Lattner98fa07b2003-05-23 20:03:32 +0000879// destroyConstant - Remove the constant from the constant table...
880//
881void ConstantArray::destroyConstant() {
882 ArrayConstants.remove(this);
883 destroyConstantImpl();
884}
885
Chris Lattner3462ae32001-12-03 22:26:30 +0000886// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000887// contain the specified string. A null terminator is added to the specified
888// string so that it may be used in a natural way...
889//
Chris Lattner015e8212004-02-15 04:14:47 +0000890Constant *ConstantArray::get(const std::string &Str) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000891 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000892
893 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000894 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000895
896 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000897 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000898
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000899 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000900 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000901}
902
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000903/// isString - This method returns true if the array is an array of sbyte or
904/// ubyte, and if the elements of the array are all ConstantInt's.
905bool ConstantArray::isString() const {
906 // Check the element type for sbyte or ubyte...
Chris Lattnere8701f62004-01-14 17:51:53 +0000907 if (getType()->getElementType() != Type::UByteTy &&
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000908 getType()->getElementType() != Type::SByteTy)
909 return false;
910 // Check the elements to make sure they are all integers, not constant
911 // expressions.
912 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
913 if (!isa<ConstantInt>(getOperand(i)))
914 return false;
915 return true;
916}
917
Chris Lattner81fabb02002-08-26 17:53:56 +0000918// getAsString - If the sub-element type of this array is either sbyte or ubyte,
919// then this method converts the array to an std::string and returns it.
920// Otherwise, it asserts out.
921//
922std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000923 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +0000924 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +0000925 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
926 Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue();
Chris Lattner81fabb02002-08-26 17:53:56 +0000927 return Result;
928}
929
930
Chris Lattner3462ae32001-12-03 22:26:30 +0000931//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000932//
Chris Lattnerb50d1352003-10-05 00:17:43 +0000933
Chris Lattner189d19f2003-11-21 20:23:48 +0000934namespace llvm {
935 template<>
936 struct ConvertConstantType<ConstantStruct, StructType> {
937 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
938 // Make everyone now use a constant of the new type...
939 std::vector<Constant*> C;
940 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
941 C.push_back(cast<Constant>(OldC->getOperand(i)));
942 Constant *New = ConstantStruct::get(NewTy, C);
943 assert(New != OldC && "Didn't replace constant??");
944
945 OldC->uncheckedReplaceAllUsesWith(New);
946 OldC->destroyConstant(); // This constant is now dead, destroy it.
947 }
948 };
949}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000950
Chris Lattner98fa07b2003-05-23 20:03:32 +0000951static ValueMap<std::vector<Constant*>, StructType,
952 ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000953
Chris Lattner015e8212004-02-15 04:14:47 +0000954Constant *ConstantStruct::get(const StructType *Ty,
955 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +0000956 // Create a ConstantAggregateZero value if all elements are zeros...
957 for (unsigned i = 0, e = V.size(); i != e; ++i)
958 if (!V[i]->isNullValue())
959 return StructConstants.getOrCreate(Ty, V);
960
961 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000962}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000963
Chris Lattnerd6108ca2004-07-12 20:35:11 +0000964Constant *ConstantStruct::get(const std::vector<Constant*> &V) {
965 std::vector<const Type*> StructEls;
966 StructEls.reserve(V.size());
967 for (unsigned i = 0, e = V.size(); i != e; ++i)
968 StructEls.push_back(V[i]->getType());
969 return get(StructType::get(StructEls), V);
970}
971
Chris Lattnerd7a73302001-10-13 06:57:33 +0000972// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000973//
Chris Lattner3462ae32001-12-03 22:26:30 +0000974void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000975 StructConstants.remove(this);
976 destroyConstantImpl();
977}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000978
Chris Lattner3462ae32001-12-03 22:26:30 +0000979//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000980//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000981
Chris Lattner189d19f2003-11-21 20:23:48 +0000982namespace llvm {
983 // ConstantPointerNull does not take extra "value" argument...
984 template<class ValType>
985 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
986 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
987 return new ConstantPointerNull(Ty);
988 }
989 };
Chris Lattner98fa07b2003-05-23 20:03:32 +0000990
Chris Lattner189d19f2003-11-21 20:23:48 +0000991 template<>
992 struct ConvertConstantType<ConstantPointerNull, PointerType> {
993 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
994 // Make everyone now use a constant of the new type...
995 Constant *New = ConstantPointerNull::get(NewTy);
996 assert(New != OldC && "Didn't replace constant??");
997 OldC->uncheckedReplaceAllUsesWith(New);
998 OldC->destroyConstant(); // This constant is now dead, destroy it.
999 }
1000 };
1001}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001002
Chris Lattner98fa07b2003-05-23 20:03:32 +00001003static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001004
Chris Lattner3462ae32001-12-03 22:26:30 +00001005ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner98fa07b2003-05-23 20:03:32 +00001006 return NullPtrConstants.getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001007}
1008
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001009// destroyConstant - Remove the constant from the constant table...
1010//
1011void ConstantPointerNull::destroyConstant() {
1012 NullPtrConstants.remove(this);
1013 destroyConstantImpl();
1014}
1015
1016
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001017//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001018//
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001019typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Chris Lattner98fa07b2003-05-23 20:03:32 +00001020
Chris Lattner189d19f2003-11-21 20:23:48 +00001021namespace llvm {
1022 template<>
1023 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
1024 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
1025 if (V.first == Instruction::Cast)
1026 return new ConstantExpr(Instruction::Cast, V.second[0], Ty);
1027 if ((V.first >= Instruction::BinaryOpsBegin &&
1028 V.first < Instruction::BinaryOpsEnd) ||
1029 V.first == Instruction::Shl || V.first == Instruction::Shr)
1030 return new ConstantExpr(V.first, V.second[0], V.second[1]);
Chris Lattnerb7897ce2004-03-30 22:51:03 +00001031 if (V.first == Instruction::Select)
1032 return new ConstantExpr(V.second[0], V.second[1], V.second[2]);
Chris Lattner189d19f2003-11-21 20:23:48 +00001033
1034 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
1035
1036 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
1037 return new ConstantExpr(V.second[0], IdxList, Ty);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001038 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001039 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001040
Chris Lattner189d19f2003-11-21 20:23:48 +00001041 template<>
1042 struct ConvertConstantType<ConstantExpr, Type> {
1043 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1044 Constant *New;
1045 switch (OldC->getOpcode()) {
1046 case Instruction::Cast:
1047 New = ConstantExpr::getCast(OldC->getOperand(0), NewTy);
1048 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001049 case Instruction::Select:
1050 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1051 OldC->getOperand(1),
1052 OldC->getOperand(2));
1053 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001054 case Instruction::Shl:
1055 case Instruction::Shr:
1056 New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
1057 OldC->getOperand(0), OldC->getOperand(1));
1058 break;
1059 default:
1060 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
1061 OldC->getOpcode() < Instruction::BinaryOpsEnd);
1062 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1063 OldC->getOperand(1));
1064 break;
1065 case Instruction::GetElementPtr:
1066 // Make everyone now use a constant of the new type...
1067 std::vector<Constant*> C;
1068 for (unsigned i = 1, e = OldC->getNumOperands(); i != e; ++i)
1069 C.push_back(cast<Constant>(OldC->getOperand(i)));
1070 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), C);
1071 break;
1072 }
1073
1074 assert(New != OldC && "Didn't replace constant??");
1075 OldC->uncheckedReplaceAllUsesWith(New);
1076 OldC->destroyConstant(); // This constant is now dead, destroy it.
1077 }
1078 };
1079} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001080
1081
Chris Lattner98fa07b2003-05-23 20:03:32 +00001082static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001083
Chris Lattner46b3d302003-04-16 22:40:51 +00001084Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001085 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1086
Chris Lattneracdbe712003-04-17 19:24:48 +00001087 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
1088 return FC; // Fold a few common cases...
1089
Vikram S. Adve4c485332002-07-15 18:19:33 +00001090 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001091 std::vector<Constant*> argVec(1, C);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001092 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
1093 return ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001094}
Chris Lattnerd7a73302001-10-13 06:57:33 +00001095
Chris Lattnerdd284742004-04-04 23:20:30 +00001096Constant *ConstantExpr::getSignExtend(Constant *C, const Type *Ty) {
1097 assert(C->getType()->isInteger() && Ty->isInteger() &&
1098 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1099 "This is an illegal sign extension!");
1100 C = ConstantExpr::getCast(C, C->getType()->getSignedVersion());
1101 return ConstantExpr::getCast(C, Ty);
1102}
1103
1104Constant *ConstantExpr::getZeroExtend(Constant *C, const Type *Ty) {
1105 assert(C->getType()->isInteger() && Ty->isInteger() &&
1106 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1107 "This is an illegal zero extension!");
1108 C = ConstantExpr::getCast(C, C->getType()->getUnsignedVersion());
1109 return ConstantExpr::getCast(C, Ty);
1110}
1111
Chris Lattnerb50d1352003-10-05 00:17:43 +00001112Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
1113 Constant *C1, Constant *C2) {
Chris Lattner5645e8a2004-01-12 19:04:55 +00001114 if (Opcode == Instruction::Shl || Opcode == Instruction::Shr)
1115 return getShiftTy(ReqTy, Opcode, C1, C2);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001116 // Check the operands for consistency first
1117 assert((Opcode >= Instruction::BinaryOpsBegin &&
1118 Opcode < Instruction::BinaryOpsEnd) &&
1119 "Invalid opcode in binary constant expression");
1120 assert(C1->getType() == C2->getType() &&
1121 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001122
1123 if (ReqTy == C1->getType())
1124 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1125 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001126
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001127 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001128 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001129 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001130}
1131
Chris Lattner6e415c02004-03-12 05:54:04 +00001132Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
1133 Constant *V1, Constant *V2) {
1134 assert(C->getType() == Type::BoolTy && "Select condition must be bool!");
1135 assert(V1->getType() == V2->getType() && "Select value types must match!");
1136 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
1137
1138 if (ReqTy == V1->getType())
1139 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1140 return SC; // Fold common cases
1141
1142 std::vector<Constant*> argVec(3, C);
1143 argVec[1] = V1;
1144 argVec[2] = V2;
1145 ExprMapKeyType Key = std::make_pair(Instruction::Select, argVec);
1146 return ExprConstants.getOrCreate(ReqTy, Key);
1147}
1148
Chris Lattner9eb2b522004-01-12 19:12:58 +00001149/// getShiftTy - Return a shift left or shift right constant expr
Chris Lattnerb50d1352003-10-05 00:17:43 +00001150Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
1151 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001152 // Check the operands for consistency first
1153 assert((Opcode == Instruction::Shl ||
1154 Opcode == Instruction::Shr) &&
1155 "Invalid opcode in binary constant expression");
1156 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
1157 "Invalid operand types for Shift constant expr!");
1158
Chris Lattner0bba7712004-01-12 20:40:42 +00001159 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001160 return FC; // Fold a few common cases...
1161
1162 // Look up the constant in the table first to ensure uniqueness
1163 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001164 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001165 return ExprConstants.getOrCreate(ReqTy, Key);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001166}
1167
1168
Chris Lattnerb50d1352003-10-05 00:17:43 +00001169Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
1170 const std::vector<Constant*> &IdxList) {
Chris Lattner04b60fe2004-02-16 20:46:13 +00001171 assert(GetElementPtrInst::getIndexedType(C->getType(),
1172 std::vector<Value*>(IdxList.begin(), IdxList.end()), true) &&
1173 "GEP indices invalid!");
1174
Chris Lattneracdbe712003-04-17 19:24:48 +00001175 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
1176 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001177
Chris Lattnerb50d1352003-10-05 00:17:43 +00001178 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001179 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001180 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001181 std::vector<Constant*> argVec(1, C);
Chris Lattnerd9f4ac662002-07-18 00:14:50 +00001182 argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001183 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001184 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001185}
1186
Chris Lattnerb50d1352003-10-05 00:17:43 +00001187Constant *ConstantExpr::getGetElementPtr(Constant *C,
1188 const std::vector<Constant*> &IdxList){
1189 // Get the result type of the getelementptr!
1190 std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
1191
1192 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
1193 true);
1194 assert(Ty && "GEP indices invalid!");
1195 return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
1196}
1197
1198
Vikram S. Adve4c485332002-07-15 18:19:33 +00001199// destroyConstant - Remove the constant from the constant table...
1200//
1201void ConstantExpr::destroyConstant() {
1202 ExprConstants.remove(this);
1203 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001204}
1205
Chris Lattner3cd8c562002-07-30 18:54:25 +00001206const char *ConstantExpr::getOpcodeName() const {
1207 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001208}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00001209