blob: 6728fc4080ff070400dbfc28682d6d38739645de [file] [log] [blame]
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001//===-- Constants.cpp - Implement Constant nodes --------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// 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.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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"
Reid Spencer7c16caa2004-09-01 22:55:40 +000021#include "llvm/ADT/StringExtras.h"
Jim Laskeyb74c6662005-08-17 19:34:49 +000022#include "llvm/Support/MathExtras.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000023#include <algorithm>
Reid Spencercf394bf2004-07-04 11:51:24 +000024#include <iostream>
Chris Lattner189d19f2003-11-21 20:23:48 +000025using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000026
Chris Lattner3462ae32001-12-03 22:26:30 +000027ConstantBool *ConstantBool::True = new ConstantBool(true);
28ConstantBool *ConstantBool::False = new ConstantBool(false);
Chris Lattner2f7c9632001-06-06 20:29:01 +000029
Chris Lattner9655e542001-07-20 19:16:02 +000030
Chris Lattner2f7c9632001-06-06 20:29:01 +000031//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000032// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000033//===----------------------------------------------------------------------===//
34
Chris Lattner3462ae32001-12-03 22:26:30 +000035void Constant::destroyConstantImpl() {
36 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000037 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000038 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +000039 // but they don't know that. Because we only find out when the CPV is
40 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000041 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000042 //
43 while (!use_empty()) {
44 Value *V = use_back();
45#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000046 if (!isa<Constant>(V))
47 std::cerr << "While deleting: " << *this
48 << "\n\nUse still stuck around after Def is destroyed: "
49 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000050#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000051 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000052 Constant *CV = cast<Constant>(V);
53 CV->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +000054
55 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000056 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000057 }
58
59 // Value has no outstanding references it is safe to delete it now...
60 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000061}
Chris Lattner2f7c9632001-06-06 20:29:01 +000062
Chris Lattnerb1585a92002-08-13 17:50:20 +000063// Static constructor to create a '0' constant of arbitrary type...
64Constant *Constant::getNullValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +000065 switch (Ty->getTypeID()) {
Chris Lattner3e88ef92003-10-03 19:34:51 +000066 case Type::BoolTyID: {
67 static Constant *NullBool = ConstantBool::get(false);
68 return NullBool;
69 }
70 case Type::SByteTyID: {
71 static Constant *NullSByte = ConstantSInt::get(Type::SByteTy, 0);
72 return NullSByte;
73 }
74 case Type::UByteTyID: {
75 static Constant *NullUByte = ConstantUInt::get(Type::UByteTy, 0);
76 return NullUByte;
77 }
78 case Type::ShortTyID: {
79 static Constant *NullShort = ConstantSInt::get(Type::ShortTy, 0);
80 return NullShort;
81 }
82 case Type::UShortTyID: {
83 static Constant *NullUShort = ConstantUInt::get(Type::UShortTy, 0);
84 return NullUShort;
85 }
86 case Type::IntTyID: {
87 static Constant *NullInt = ConstantSInt::get(Type::IntTy, 0);
88 return NullInt;
89 }
90 case Type::UIntTyID: {
91 static Constant *NullUInt = ConstantUInt::get(Type::UIntTy, 0);
92 return NullUInt;
93 }
94 case Type::LongTyID: {
95 static Constant *NullLong = ConstantSInt::get(Type::LongTy, 0);
96 return NullLong;
97 }
98 case Type::ULongTyID: {
99 static Constant *NullULong = ConstantUInt::get(Type::ULongTy, 0);
100 return NullULong;
101 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000102
Chris Lattner3e88ef92003-10-03 19:34:51 +0000103 case Type::FloatTyID: {
104 static Constant *NullFloat = ConstantFP::get(Type::FloatTy, 0);
105 return NullFloat;
106 }
107 case Type::DoubleTyID: {
108 static Constant *NullDouble = ConstantFP::get(Type::DoubleTy, 0);
109 return NullDouble;
110 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000111
Misha Brukmanb1c93172005-04-21 23:48:37 +0000112 case Type::PointerTyID:
Chris Lattnerb1585a92002-08-13 17:50:20 +0000113 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner3e88ef92003-10-03 19:34:51 +0000114
Chris Lattner9fba3da2004-02-15 05:53:04 +0000115 case Type::StructTyID:
116 case Type::ArrayTyID:
Brian Gaeke02209042004-08-20 06:00:58 +0000117 case Type::PackedTyID:
Chris Lattner9fba3da2004-02-15 05:53:04 +0000118 return ConstantAggregateZero::get(Ty);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000119 default:
Reid Spencercf394bf2004-07-04 11:51:24 +0000120 // Function, Label, or Opaque type?
121 assert(!"Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000122 return 0;
123 }
124}
125
126// Static constructor to create the maximum constant of an integral type...
127ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000128 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000129 case Type::BoolTyID: return ConstantBool::True;
130 case Type::SByteTyID:
131 case Type::ShortTyID:
132 case Type::IntTyID:
133 case Type::LongTyID: {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000134 // Calculate 011111111111111...
Chris Lattnerb1585a92002-08-13 17:50:20 +0000135 unsigned TypeBits = Ty->getPrimitiveSize()*8;
136 int64_t Val = INT64_MAX; // All ones
137 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
138 return ConstantSInt::get(Ty, Val);
139 }
140
141 case Type::UByteTyID:
142 case Type::UShortTyID:
143 case Type::UIntTyID:
144 case Type::ULongTyID: return getAllOnesValue(Ty);
145
Chris Lattner31408f72002-08-14 17:12:13 +0000146 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000147 }
148}
149
150// Static constructor to create the minimum constant for an integral type...
151ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000152 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000153 case Type::BoolTyID: return ConstantBool::False;
154 case Type::SByteTyID:
155 case Type::ShortTyID:
156 case Type::IntTyID:
157 case Type::LongTyID: {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000158 // Calculate 1111111111000000000000
Chris Lattnerb1585a92002-08-13 17:50:20 +0000159 unsigned TypeBits = Ty->getPrimitiveSize()*8;
160 int64_t Val = -1; // All ones
161 Val <<= TypeBits-1; // Shift over to the right spot
162 return ConstantSInt::get(Ty, Val);
163 }
164
165 case Type::UByteTyID:
166 case Type::UShortTyID:
167 case Type::UIntTyID:
168 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
169
Chris Lattner31408f72002-08-14 17:12:13 +0000170 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000171 }
172}
173
174// Static constructor to create an integral constant with all bits set
175ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000176 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000177 case Type::BoolTyID: return ConstantBool::True;
178 case Type::SByteTyID:
179 case Type::ShortTyID:
180 case Type::IntTyID:
181 case Type::LongTyID: return ConstantSInt::get(Ty, -1);
182
183 case Type::UByteTyID:
184 case Type::UShortTyID:
185 case Type::UIntTyID:
186 case Type::ULongTyID: {
187 // Calculate ~0 of the right type...
188 unsigned TypeBits = Ty->getPrimitiveSize()*8;
189 uint64_t Val = ~0ULL; // All ones
190 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
191 return ConstantUInt::get(Ty, Val);
192 }
Chris Lattner31408f72002-08-14 17:12:13 +0000193 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000194 }
195}
196
Chris Lattner83e5d392003-03-10 22:39:02 +0000197bool ConstantUInt::isAllOnesValue() const {
198 unsigned TypeBits = getType()->getPrimitiveSize()*8;
199 uint64_t Val = ~0ULL; // All ones
200 Val >>= 64-TypeBits; // Shift out inappropriate bits
201 return getValue() == Val;
202}
203
Chris Lattnerb1585a92002-08-13 17:50:20 +0000204
Chris Lattner2f7c9632001-06-06 20:29:01 +0000205//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +0000206// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +0000207//===----------------------------------------------------------------------===//
208
209//===----------------------------------------------------------------------===//
210// Normal Constructors
211
Chris Lattnere7e139e2005-09-27 06:09:08 +0000212ConstantIntegral::ConstantIntegral(const Type *Ty, ValueTy VT, uint64_t V)
213 : Constant(Ty, VT, 0, 0) {
Chris Lattner265eb642004-06-21 12:12:12 +0000214 Val.Unsigned = V;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000215}
Chris Lattner49d855c2001-09-07 16:46:31 +0000216
Chris Lattnere7e139e2005-09-27 06:09:08 +0000217ConstantBool::ConstantBool(bool V)
218 : ConstantIntegral(Type::BoolTy, ConstantBoolVal, V) {
Chris Lattner265eb642004-06-21 12:12:12 +0000219}
220
Chris Lattnere7e139e2005-09-27 06:09:08 +0000221ConstantInt::ConstantInt(const Type *Ty, ValueTy VT, uint64_t V)
222 : ConstantIntegral(Ty, VT, V) {
Chris Lattner7309d662001-07-21 19:16:08 +0000223}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000224
Chris Lattnere7e139e2005-09-27 06:09:08 +0000225ConstantSInt::ConstantSInt(const Type *Ty, int64_t V)
226 : ConstantInt(Ty, ConstantSIntVal, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000227 assert(Ty->isInteger() && Ty->isSigned() &&
Reid Spencerf064bb22005-03-09 15:19:41 +0000228 "Illegal type for signed integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000229 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000230}
231
Chris Lattnere7e139e2005-09-27 06:09:08 +0000232ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V)
233 : ConstantInt(Ty, ConstantUIntVal, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000234 assert(Ty->isInteger() && Ty->isUnsigned() &&
235 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000236 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000237}
238
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000239ConstantFP::ConstantFP(const Type *Ty, double V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000240 : Constant(Ty, ConstantFPVal, 0, 0) {
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 Lattnerd0df99c2005-01-29 00:34:39 +0000246 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000247 : Constant(T, ConstantArrayVal, new Use[V.size()], V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000248 assert(V.size() == T->getNumElements() &&
249 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000250 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000251 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
252 I != E; ++I, ++OL) {
253 Constant *E = *I;
254 assert((E->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000255 (T->isAbstract() &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000256 E->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000257 "Initializer for array element doesn't match array element type!");
Chris Lattner0144fad2005-10-03 21:56:24 +0000258 OL->init(E, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000259 }
260}
261
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000262ConstantArray::~ConstantArray() {
263 delete [] OperandList;
264}
265
Chris Lattner3462ae32001-12-03 22:26:30 +0000266ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000267 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000268 : Constant(T, ConstantStructVal, new Use[V.size()], V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000269 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000270 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000271 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000272 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
273 I != E; ++I, ++OL) {
274 Constant *E = *I;
275 assert((E->getType() == T->getElementType(I-V.begin()) ||
276 ((T->getElementType(I-V.begin())->isAbstract() ||
277 E->getType()->isAbstract()) &&
278 T->getElementType(I-V.begin())->getTypeID() ==
279 E->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000280 "Initializer for struct element doesn't match struct element type!");
Chris Lattner0144fad2005-10-03 21:56:24 +0000281 OL->init(E, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000282 }
283}
284
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000285ConstantStruct::~ConstantStruct() {
286 delete [] OperandList;
287}
288
289
Brian Gaeke02209042004-08-20 06:00:58 +0000290ConstantPacked::ConstantPacked(const PackedType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000291 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000292 : Constant(T, ConstantPackedVal, new Use[V.size()], V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000293 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000294 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
295 I != E; ++I, ++OL) {
296 Constant *E = *I;
297 assert((E->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000298 (T->isAbstract() &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000299 E->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000300 "Initializer for packed element doesn't match packed element type!");
Chris Lattner0144fad2005-10-03 21:56:24 +0000301 OL->init(E, this);
Brian Gaeke02209042004-08-20 06:00:58 +0000302 }
303}
304
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000305ConstantPacked::~ConstantPacked() {
306 delete [] OperandList;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000307}
308
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000309/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
310/// behind the scenes to implement unary constant exprs.
311class UnaryConstantExpr : public ConstantExpr {
312 Use Op;
313public:
314 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
315 : ConstantExpr(Ty, Opcode, &Op, 1), Op(C, this) {}
316};
Chris Lattner6e415c02004-03-12 05:54:04 +0000317
Chris Lattner22ced562003-06-22 20:48:30 +0000318static bool isSetCC(unsigned Opcode) {
319 return Opcode == Instruction::SetEQ || Opcode == Instruction::SetNE ||
320 Opcode == Instruction::SetLT || Opcode == Instruction::SetGT ||
321 Opcode == Instruction::SetLE || Opcode == Instruction::SetGE;
322}
323
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000324/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
325/// behind the scenes to implement binary constant exprs.
326class BinaryConstantExpr : public ConstantExpr {
327 Use Ops[2];
328public:
329 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
330 : ConstantExpr(isSetCC(Opcode) ? Type::BoolTy : C1->getType(),
331 Opcode, Ops, 2) {
332 Ops[0].init(C1, this);
333 Ops[1].init(C2, this);
334 }
335};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000336
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000337/// SelectConstantExpr - This class is private to Constants.cpp, and is used
338/// behind the scenes to implement select constant exprs.
339class SelectConstantExpr : public ConstantExpr {
340 Use Ops[3];
341public:
342 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
343 : ConstantExpr(C2->getType(), Instruction::Select, Ops, 3) {
344 Ops[0].init(C1, this);
345 Ops[1].init(C2, this);
346 Ops[2].init(C3, this);
347 }
348};
349
350/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
351/// used behind the scenes to implement getelementpr constant exprs.
352struct GetElementPtrConstantExpr : public ConstantExpr {
353 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
354 const Type *DestTy)
355 : ConstantExpr(DestTy, Instruction::GetElementPtr,
356 new Use[IdxList.size()+1], IdxList.size()+1) {
357 OperandList[0].init(C, this);
358 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
359 OperandList[i+1].init(IdxList[i], this);
360 }
361 ~GetElementPtrConstantExpr() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000362 delete [] OperandList;
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000363 }
364};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000365
Chris Lattner817175f2004-03-29 02:37:53 +0000366/// ConstantExpr::get* - Return some common constants without having to
367/// specify the full Instruction::OPCODE identifier.
368///
369Constant *ConstantExpr::getNeg(Constant *C) {
Chris Lattner3cdc27c2004-03-29 19:51:24 +0000370 if (!C->getType()->isFloatingPoint())
371 return get(Instruction::Sub, getNullValue(C->getType()), C);
372 else
373 return get(Instruction::Sub, ConstantFP::get(C->getType(), -0.0), C);
Chris Lattner817175f2004-03-29 02:37:53 +0000374}
375Constant *ConstantExpr::getNot(Constant *C) {
376 assert(isa<ConstantIntegral>(C) && "Cannot NOT a nonintegral type!");
377 return get(Instruction::Xor, C,
378 ConstantIntegral::getAllOnesValue(C->getType()));
379}
380Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
381 return get(Instruction::Add, C1, C2);
382}
383Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
384 return get(Instruction::Sub, C1, C2);
385}
386Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
387 return get(Instruction::Mul, C1, C2);
388}
389Constant *ConstantExpr::getDiv(Constant *C1, Constant *C2) {
390 return get(Instruction::Div, C1, C2);
391}
392Constant *ConstantExpr::getRem(Constant *C1, Constant *C2) {
393 return get(Instruction::Rem, C1, C2);
394}
395Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
396 return get(Instruction::And, C1, C2);
397}
398Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
399 return get(Instruction::Or, C1, C2);
400}
401Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
402 return get(Instruction::Xor, C1, C2);
403}
404Constant *ConstantExpr::getSetEQ(Constant *C1, Constant *C2) {
405 return get(Instruction::SetEQ, C1, C2);
406}
407Constant *ConstantExpr::getSetNE(Constant *C1, Constant *C2) {
408 return get(Instruction::SetNE, C1, C2);
409}
410Constant *ConstantExpr::getSetLT(Constant *C1, Constant *C2) {
411 return get(Instruction::SetLT, C1, C2);
412}
413Constant *ConstantExpr::getSetGT(Constant *C1, Constant *C2) {
414 return get(Instruction::SetGT, C1, C2);
415}
416Constant *ConstantExpr::getSetLE(Constant *C1, Constant *C2) {
417 return get(Instruction::SetLE, C1, C2);
418}
419Constant *ConstantExpr::getSetGE(Constant *C1, Constant *C2) {
420 return get(Instruction::SetGE, C1, C2);
421}
422Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
423 return get(Instruction::Shl, C1, C2);
424}
425Constant *ConstantExpr::getShr(Constant *C1, Constant *C2) {
426 return get(Instruction::Shr, C1, C2);
427}
428
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000429Constant *ConstantExpr::getUShr(Constant *C1, Constant *C2) {
430 if (C1->getType()->isUnsigned()) return getShr(C1, C2);
431 return getCast(getShr(getCast(C1,
432 C1->getType()->getUnsignedVersion()), C2), C1->getType());
433}
Chris Lattner817175f2004-03-29 02:37:53 +0000434
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000435Constant *ConstantExpr::getSShr(Constant *C1, Constant *C2) {
436 if (C1->getType()->isSigned()) return getShr(C1, C2);
437 return getCast(getShr(getCast(C1,
438 C1->getType()->getSignedVersion()), C2), C1->getType());
439}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000440
Chris Lattner2f7c9632001-06-06 20:29:01 +0000441
442//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000443// isValueValidForType implementations
444
Chris Lattner3462ae32001-12-03 22:26:30 +0000445bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000446 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000447 default:
448 return false; // These can't be represented as integers!!!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000449 // Signed types...
450 case Type::SByteTyID:
451 return (Val <= INT8_MAX && Val >= INT8_MIN);
452 case Type::ShortTyID:
453 return (Val <= INT16_MAX && Val >= INT16_MIN);
454 case Type::IntTyID:
Chris Lattner74248512004-06-08 23:21:39 +0000455 return (Val <= int(INT32_MAX) && Val >= int(INT32_MIN));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000456 case Type::LongTyID:
457 return true; // This is the largest type...
458 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000459}
460
Chris Lattner3462ae32001-12-03 22:26:30 +0000461bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000462 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000463 default:
464 return false; // These can't be represented as integers!!!
465
466 // Unsigned types...
467 case Type::UByteTyID:
468 return (Val <= UINT8_MAX);
469 case Type::UShortTyID:
470 return (Val <= UINT16_MAX);
471 case Type::UIntTyID:
472 return (Val <= UINT32_MAX);
473 case Type::ULongTyID:
474 return true; // This is the largest type...
475 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000476}
477
Chris Lattner3462ae32001-12-03 22:26:30 +0000478bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000479 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000480 default:
481 return false; // These can't be represented as floating point!
482
Reid Spencerb95f8ab2004-12-07 07:38:08 +0000483 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000484 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000485 case Type::DoubleTyID:
486 return true; // This is the largest type...
487 }
488};
Chris Lattner9655e542001-07-20 19:16:02 +0000489
Chris Lattner49d855c2001-09-07 16:46:31 +0000490//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000491// Factory Function Implementation
492
Chris Lattner98fa07b2003-05-23 20:03:32 +0000493// ConstantCreator - A class that is used to create constants by
494// ValueMap*. This class should be partially specialized if there is
495// something strange that needs to be done to interface to the ctor for the
496// constant.
497//
Chris Lattner189d19f2003-11-21 20:23:48 +0000498namespace llvm {
499 template<class ConstantClass, class TypeClass, class ValType>
500 struct ConstantCreator {
501 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
502 return new ConstantClass(Ty, V);
503 }
504 };
Misha Brukmanb1c93172005-04-21 23:48:37 +0000505
Chris Lattner189d19f2003-11-21 20:23:48 +0000506 template<class ConstantClass, class TypeClass>
507 struct ConvertConstantType {
508 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
509 assert(0 && "This type cannot be converted!\n");
510 abort();
511 }
512 };
513}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000514
Chris Lattner98fa07b2003-05-23 20:03:32 +0000515namespace {
516 template<class ValType, class TypeClass, class ConstantClass>
Chris Lattnerb50d1352003-10-05 00:17:43 +0000517 class ValueMap : public AbstractTypeUser {
Chris Lattnerb64419a2005-10-03 22:51:37 +0000518 public:
Chris Lattnerb50d1352003-10-05 00:17:43 +0000519 typedef std::pair<const TypeClass*, ValType> MapKey;
520 typedef std::map<MapKey, ConstantClass *> MapTy;
521 typedef typename MapTy::iterator MapIterator;
Chris Lattnerb64419a2005-10-03 22:51:37 +0000522 private:
Chris Lattnerb50d1352003-10-05 00:17:43 +0000523 MapTy Map;
524
525 typedef std::map<const TypeClass*, MapIterator> AbstractTypeMapTy;
526 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner99a669b2004-11-19 16:39:44 +0000527
528 friend void Constant::clearAllValueMaps();
529 private:
530 void clear(std::vector<Constant *> &Constants) {
531 for(MapIterator I = Map.begin(); I != Map.end(); ++I)
532 Constants.push_back(I->second);
533 Map.clear();
534 AbstractTypeMap.clear();
535 }
536
Chris Lattner98fa07b2003-05-23 20:03:32 +0000537 public:
Chris Lattnerb64419a2005-10-03 22:51:37 +0000538 MapIterator map_end() { return Map.end(); }
539
540 /// InsertOrGetItem - Return an iterator for the specified element.
541 /// If the element exists in the map, the returned iterator points to the
542 /// entry and Exists=true. If not, the iterator points to the newly
543 /// inserted entry and returns Exists=false. Newly inserted entries have
544 /// I->second == 0, and should be filled in.
545 MapIterator InsertOrGetItem(std::pair<MapKey, ConstantClass *> &InsertVal,
546 bool &Exists) {
547 std::pair<MapIterator, bool> IP = Map.insert(InsertVal);
548 Exists = !IP.second;
549 return IP.first;
550 }
551
552 /// getOrCreate - Return the specified constant from the map, creating it if
553 /// necessary.
Chris Lattner98fa07b2003-05-23 20:03:32 +0000554 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000555 MapKey Lookup(Ty, V);
556 MapIterator I = Map.lower_bound(Lookup);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000557 if (I != Map.end() && I->first == Lookup)
558 return I->second; // Is it in the map?
559
560 // If no preexisting value, create one now...
561 ConstantClass *Result =
562 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
563
Chris Lattnerb50d1352003-10-05 00:17:43 +0000564 /// FIXME: why does this assert fail when loading 176.gcc?
565 //assert(Result->getType() == Ty && "Type specified is not correct!");
566 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
567
568 // If the type of the constant is abstract, make sure that an entry exists
569 // for it in the AbstractTypeMap.
570 if (Ty->isAbstract()) {
571 typename AbstractTypeMapTy::iterator TI =
572 AbstractTypeMap.lower_bound(Ty);
573
574 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
575 // Add ourselves to the ATU list of the type.
576 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
577
578 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
579 }
580 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000581 return Result;
582 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000583
Chris Lattner98fa07b2003-05-23 20:03:32 +0000584 void remove(ConstantClass *CP) {
Chris Lattner3e650af2004-08-04 04:48:01 +0000585 MapIterator I = Map.find(MapKey((TypeClass*)CP->getRawType(),
586 getValType(CP)));
Chris Lattner20a4dab2004-08-04 22:26:13 +0000587 if (I == Map.end() || I->second != CP) {
588 // FIXME: This should not use a linear scan. If this gets to be a
589 // performance problem, someone should look at this.
590 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
591 /* empty */;
592 }
593
Chris Lattnerb50d1352003-10-05 00:17:43 +0000594 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +0000595 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +0000596
597 // Now that we found the entry, make sure this isn't the entry that
598 // the AbstractTypeMap points to.
599 const TypeClass *Ty = I->first.first;
600 if (Ty->isAbstract()) {
601 assert(AbstractTypeMap.count(Ty) &&
602 "Abstract type not in AbstractTypeMap?");
603 MapIterator &ATMEntryIt = AbstractTypeMap[Ty];
604 if (ATMEntryIt == I) {
605 // Yes, we are removing the representative entry for this type.
606 // See if there are any other entries of the same type.
607 MapIterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000608
Chris Lattnerb50d1352003-10-05 00:17:43 +0000609 // First check the entry before this one...
610 if (TmpIt != Map.begin()) {
611 --TmpIt;
612 if (TmpIt->first.first != Ty) // Not the same type, move back...
613 ++TmpIt;
614 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000615
Chris Lattnerb50d1352003-10-05 00:17:43 +0000616 // If we didn't find the same type, try to move forward...
617 if (TmpIt == ATMEntryIt) {
618 ++TmpIt;
619 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
620 --TmpIt; // No entry afterwards with the same type
621 }
622
623 // If there is another entry in the map of the same abstract type,
624 // update the AbstractTypeMap entry now.
625 if (TmpIt != ATMEntryIt) {
626 ATMEntryIt = TmpIt;
627 } else {
628 // Otherwise, we are removing the last instance of this type
629 // from the table. Remove from the ATM, and from user list.
630 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
631 AbstractTypeMap.erase(Ty);
632 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000633 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000634 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000635
Chris Lattnerb50d1352003-10-05 00:17:43 +0000636 Map.erase(I);
637 }
638
639 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000640 typename AbstractTypeMapTy::iterator I =
Chris Lattnerb50d1352003-10-05 00:17:43 +0000641 AbstractTypeMap.find(cast<TypeClass>(OldTy));
642
643 assert(I != AbstractTypeMap.end() &&
644 "Abstract type not in AbstractTypeMap?");
645
646 // Convert a constant at a time until the last one is gone. The last one
647 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
648 // eliminated eventually.
649 do {
650 ConvertConstantType<ConstantClass,
651 TypeClass>::convert(I->second->second,
652 cast<TypeClass>(NewTy));
653
654 I = AbstractTypeMap.find(cast<TypeClass>(OldTy));
655 } while (I != AbstractTypeMap.end());
656 }
657
658 // If the type became concrete without being refined to any other existing
659 // type, we just remove ourselves from the ATU list.
660 void typeBecameConcrete(const DerivedType *AbsTy) {
661 AbsTy->removeAbstractTypeUser(this);
662 }
663
664 void dump() const {
665 std::cerr << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +0000666 }
667 };
668}
669
Chris Lattner3462ae32001-12-03 22:26:30 +0000670//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000671//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000672static ValueMap< int64_t, Type, ConstantSInt> SIntConstants;
673static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000674
Chris Lattner3462ae32001-12-03 22:26:30 +0000675ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000676 return SIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000677}
678
Chris Lattner3462ae32001-12-03 22:26:30 +0000679ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000680 return UIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000681}
682
Chris Lattner3462ae32001-12-03 22:26:30 +0000683ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000684 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000685 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
686 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000687}
688
Chris Lattner3462ae32001-12-03 22:26:30 +0000689//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000690//
Chris Lattnerac80ea42004-02-01 22:49:04 +0000691namespace llvm {
692 template<>
693 struct ConstantCreator<ConstantFP, Type, uint64_t> {
694 static ConstantFP *create(const Type *Ty, uint64_t V) {
695 assert(Ty == Type::DoubleTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000696 return new ConstantFP(Ty, BitsToDouble(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000697 }
698 };
699 template<>
700 struct ConstantCreator<ConstantFP, Type, uint32_t> {
701 static ConstantFP *create(const Type *Ty, uint32_t V) {
702 assert(Ty == Type::FloatTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000703 return new ConstantFP(Ty, BitsToFloat(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000704 }
705 };
706}
707
708static ValueMap<uint64_t, Type, ConstantFP> DoubleConstants;
709static ValueMap<uint32_t, Type, ConstantFP> FloatConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000710
Jim Laskey8ad8f712005-08-17 20:06:22 +0000711bool ConstantFP::isNullValue() const {
712 return DoubleToBits(Val) == 0;
713}
714
715bool ConstantFP::isExactlyValue(double V) const {
716 return DoubleToBits(V) == DoubleToBits(Val);
717}
718
719
Chris Lattner3462ae32001-12-03 22:26:30 +0000720ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner241ed4c2004-01-23 00:55:21 +0000721 if (Ty == Type::FloatTy) {
722 // Force the value through memory to normalize it.
Jim Laskeyb74c6662005-08-17 19:34:49 +0000723 return FloatConstants.getOrCreate(Ty, FloatToBits(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000724 } else {
725 assert(Ty == Type::DoubleTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000726 return DoubleConstants.getOrCreate(Ty, DoubleToBits(V));
Chris Lattner241ed4c2004-01-23 00:55:21 +0000727 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000728}
729
Chris Lattner9fba3da2004-02-15 05:53:04 +0000730//---- ConstantAggregateZero::get() implementation...
731//
732namespace llvm {
733 // ConstantAggregateZero does not take extra "value" argument...
734 template<class ValType>
735 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
736 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
737 return new ConstantAggregateZero(Ty);
738 }
739 };
740
741 template<>
742 struct ConvertConstantType<ConstantAggregateZero, Type> {
743 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
744 // Make everyone now use a constant of the new type...
745 Constant *New = ConstantAggregateZero::get(NewTy);
746 assert(New != OldC && "Didn't replace constant??");
747 OldC->uncheckedReplaceAllUsesWith(New);
748 OldC->destroyConstant(); // This constant is now dead, destroy it.
749 }
750 };
751}
752
753static ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
754
Chris Lattner3e650af2004-08-04 04:48:01 +0000755static char getValType(ConstantAggregateZero *CPZ) { return 0; }
756
Chris Lattner9fba3da2004-02-15 05:53:04 +0000757Constant *ConstantAggregateZero::get(const Type *Ty) {
758 return AggZeroConstants.getOrCreate(Ty, 0);
759}
760
761// destroyConstant - Remove the constant from the constant table...
762//
763void ConstantAggregateZero::destroyConstant() {
764 AggZeroConstants.remove(this);
765 destroyConstantImpl();
766}
767
768void ConstantAggregateZero::replaceUsesOfWithOnConstant(Value *From, Value *To,
769 bool DisableChecking) {
770 assert(0 && "No uses!");
771 abort();
772}
773
774
775
Chris Lattner3462ae32001-12-03 22:26:30 +0000776//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000777//
Chris Lattner189d19f2003-11-21 20:23:48 +0000778namespace llvm {
779 template<>
780 struct ConvertConstantType<ConstantArray, ArrayType> {
781 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
782 // Make everyone now use a constant of the new type...
783 std::vector<Constant*> C;
784 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
785 C.push_back(cast<Constant>(OldC->getOperand(i)));
786 Constant *New = ConstantArray::get(NewTy, C);
787 assert(New != OldC && "Didn't replace constant??");
788 OldC->uncheckedReplaceAllUsesWith(New);
789 OldC->destroyConstant(); // This constant is now dead, destroy it.
790 }
791 };
792}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000793
Chris Lattner3e650af2004-08-04 04:48:01 +0000794static std::vector<Constant*> getValType(ConstantArray *CA) {
795 std::vector<Constant*> Elements;
796 Elements.reserve(CA->getNumOperands());
797 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
798 Elements.push_back(cast<Constant>(CA->getOperand(i)));
799 return Elements;
800}
801
Chris Lattnerb64419a2005-10-03 22:51:37 +0000802typedef ValueMap<std::vector<Constant*>, ArrayType,
803 ConstantArray> ArrayConstantsTy;
804static ArrayConstantsTy ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000805
Chris Lattner015e8212004-02-15 04:14:47 +0000806Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +0000807 const std::vector<Constant*> &V) {
808 // If this is an all-zero array, return a ConstantAggregateZero object
809 if (!V.empty()) {
810 Constant *C = V[0];
811 if (!C->isNullValue())
812 return ArrayConstants.getOrCreate(Ty, V);
813 for (unsigned i = 1, e = V.size(); i != e; ++i)
814 if (V[i] != C)
815 return ArrayConstants.getOrCreate(Ty, V);
816 }
817 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000818}
819
Chris Lattner98fa07b2003-05-23 20:03:32 +0000820// destroyConstant - Remove the constant from the constant table...
821//
822void ConstantArray::destroyConstant() {
823 ArrayConstants.remove(this);
824 destroyConstantImpl();
825}
826
Chris Lattner3462ae32001-12-03 22:26:30 +0000827// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000828// contain the specified string. A null terminator is added to the specified
829// string so that it may be used in a natural way...
830//
Chris Lattner015e8212004-02-15 04:14:47 +0000831Constant *ConstantArray::get(const std::string &Str) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000832 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000833
834 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000835 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000836
837 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000838 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000839
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000840 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000841 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000842}
843
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000844/// isString - This method returns true if the array is an array of sbyte or
845/// ubyte, and if the elements of the array are all ConstantInt's.
846bool ConstantArray::isString() const {
847 // Check the element type for sbyte or ubyte...
Chris Lattnere8701f62004-01-14 17:51:53 +0000848 if (getType()->getElementType() != Type::UByteTy &&
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000849 getType()->getElementType() != Type::SByteTy)
850 return false;
851 // Check the elements to make sure they are all integers, not constant
852 // expressions.
853 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
854 if (!isa<ConstantInt>(getOperand(i)))
855 return false;
856 return true;
857}
858
Chris Lattner81fabb02002-08-26 17:53:56 +0000859// getAsString - If the sub-element type of this array is either sbyte or ubyte,
860// then this method converts the array to an std::string and returns it.
861// Otherwise, it asserts out.
862//
863std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000864 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +0000865 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +0000866 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
867 Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue();
Chris Lattner81fabb02002-08-26 17:53:56 +0000868 return Result;
869}
870
871
Chris Lattner3462ae32001-12-03 22:26:30 +0000872//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000873//
Chris Lattnerb50d1352003-10-05 00:17:43 +0000874
Chris Lattner189d19f2003-11-21 20:23:48 +0000875namespace llvm {
876 template<>
877 struct ConvertConstantType<ConstantStruct, StructType> {
878 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
879 // Make everyone now use a constant of the new type...
880 std::vector<Constant*> C;
881 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
882 C.push_back(cast<Constant>(OldC->getOperand(i)));
883 Constant *New = ConstantStruct::get(NewTy, C);
884 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000885
Chris Lattner189d19f2003-11-21 20:23:48 +0000886 OldC->uncheckedReplaceAllUsesWith(New);
887 OldC->destroyConstant(); // This constant is now dead, destroy it.
888 }
889 };
890}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000891
Misha Brukmanb1c93172005-04-21 23:48:37 +0000892static ValueMap<std::vector<Constant*>, StructType,
Chris Lattner98fa07b2003-05-23 20:03:32 +0000893 ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000894
Chris Lattner3e650af2004-08-04 04:48:01 +0000895static std::vector<Constant*> getValType(ConstantStruct *CS) {
896 std::vector<Constant*> Elements;
897 Elements.reserve(CS->getNumOperands());
898 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
899 Elements.push_back(cast<Constant>(CS->getOperand(i)));
900 return Elements;
901}
902
Chris Lattner015e8212004-02-15 04:14:47 +0000903Constant *ConstantStruct::get(const StructType *Ty,
904 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +0000905 // Create a ConstantAggregateZero value if all elements are zeros...
906 for (unsigned i = 0, e = V.size(); i != e; ++i)
907 if (!V[i]->isNullValue())
908 return StructConstants.getOrCreate(Ty, V);
909
910 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000911}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000912
Chris Lattnerd6108ca2004-07-12 20:35:11 +0000913Constant *ConstantStruct::get(const std::vector<Constant*> &V) {
914 std::vector<const Type*> StructEls;
915 StructEls.reserve(V.size());
916 for (unsigned i = 0, e = V.size(); i != e; ++i)
917 StructEls.push_back(V[i]->getType());
918 return get(StructType::get(StructEls), V);
919}
920
Chris Lattnerd7a73302001-10-13 06:57:33 +0000921// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000922//
Chris Lattner3462ae32001-12-03 22:26:30 +0000923void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000924 StructConstants.remove(this);
925 destroyConstantImpl();
926}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000927
Brian Gaeke02209042004-08-20 06:00:58 +0000928//---- ConstantPacked::get() implementation...
929//
930namespace llvm {
931 template<>
932 struct ConvertConstantType<ConstantPacked, PackedType> {
933 static void convert(ConstantPacked *OldC, const PackedType *NewTy) {
934 // Make everyone now use a constant of the new type...
935 std::vector<Constant*> C;
936 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
937 C.push_back(cast<Constant>(OldC->getOperand(i)));
938 Constant *New = ConstantPacked::get(NewTy, C);
939 assert(New != OldC && "Didn't replace constant??");
940 OldC->uncheckedReplaceAllUsesWith(New);
941 OldC->destroyConstant(); // This constant is now dead, destroy it.
942 }
943 };
944}
945
946static std::vector<Constant*> getValType(ConstantPacked *CP) {
947 std::vector<Constant*> Elements;
948 Elements.reserve(CP->getNumOperands());
949 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
950 Elements.push_back(CP->getOperand(i));
951 return Elements;
952}
953
954static ValueMap<std::vector<Constant*>, PackedType,
955 ConstantPacked> PackedConstants;
956
957Constant *ConstantPacked::get(const PackedType *Ty,
958 const std::vector<Constant*> &V) {
959 // If this is an all-zero packed, return a ConstantAggregateZero object
960 if (!V.empty()) {
961 Constant *C = V[0];
962 if (!C->isNullValue())
963 return PackedConstants.getOrCreate(Ty, V);
964 for (unsigned i = 1, e = V.size(); i != e; ++i)
965 if (V[i] != C)
966 return PackedConstants.getOrCreate(Ty, V);
967 }
968 return ConstantAggregateZero::get(Ty);
969}
970
971Constant *ConstantPacked::get(const std::vector<Constant*> &V) {
972 assert(!V.empty() && "Cannot infer type if V is empty");
973 return get(PackedType::get(V.front()->getType(),V.size()), V);
974}
975
976// destroyConstant - Remove the constant from the constant table...
977//
978void ConstantPacked::destroyConstant() {
979 PackedConstants.remove(this);
980 destroyConstantImpl();
981}
982
Chris Lattner3462ae32001-12-03 22:26:30 +0000983//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +0000984//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000985
Chris Lattner189d19f2003-11-21 20:23:48 +0000986namespace llvm {
987 // ConstantPointerNull does not take extra "value" argument...
988 template<class ValType>
989 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
990 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
991 return new ConstantPointerNull(Ty);
992 }
993 };
Chris Lattner98fa07b2003-05-23 20:03:32 +0000994
Chris Lattner189d19f2003-11-21 20:23:48 +0000995 template<>
996 struct ConvertConstantType<ConstantPointerNull, PointerType> {
997 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
998 // Make everyone now use a constant of the new type...
999 Constant *New = ConstantPointerNull::get(NewTy);
1000 assert(New != OldC && "Didn't replace constant??");
1001 OldC->uncheckedReplaceAllUsesWith(New);
1002 OldC->destroyConstant(); // This constant is now dead, destroy it.
1003 }
1004 };
1005}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001006
Chris Lattner98fa07b2003-05-23 20:03:32 +00001007static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001008
Chris Lattner3e650af2004-08-04 04:48:01 +00001009static char getValType(ConstantPointerNull *) {
1010 return 0;
1011}
1012
1013
Chris Lattner3462ae32001-12-03 22:26:30 +00001014ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner98fa07b2003-05-23 20:03:32 +00001015 return NullPtrConstants.getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001016}
1017
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001018// destroyConstant - Remove the constant from the constant table...
1019//
1020void ConstantPointerNull::destroyConstant() {
1021 NullPtrConstants.remove(this);
1022 destroyConstantImpl();
1023}
1024
1025
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001026//---- UndefValue::get() implementation...
1027//
1028
1029namespace llvm {
1030 // UndefValue does not take extra "value" argument...
1031 template<class ValType>
1032 struct ConstantCreator<UndefValue, Type, ValType> {
1033 static UndefValue *create(const Type *Ty, const ValType &V) {
1034 return new UndefValue(Ty);
1035 }
1036 };
1037
1038 template<>
1039 struct ConvertConstantType<UndefValue, Type> {
1040 static void convert(UndefValue *OldC, const Type *NewTy) {
1041 // Make everyone now use a constant of the new type.
1042 Constant *New = UndefValue::get(NewTy);
1043 assert(New != OldC && "Didn't replace constant??");
1044 OldC->uncheckedReplaceAllUsesWith(New);
1045 OldC->destroyConstant(); // This constant is now dead, destroy it.
1046 }
1047 };
1048}
1049
1050static ValueMap<char, Type, UndefValue> UndefValueConstants;
1051
1052static char getValType(UndefValue *) {
1053 return 0;
1054}
1055
1056
1057UndefValue *UndefValue::get(const Type *Ty) {
1058 return UndefValueConstants.getOrCreate(Ty, 0);
1059}
1060
1061// destroyConstant - Remove the constant from the constant table.
1062//
1063void UndefValue::destroyConstant() {
1064 UndefValueConstants.remove(this);
1065 destroyConstantImpl();
1066}
1067
1068
1069
1070
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001071//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001072//
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001073typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Chris Lattner98fa07b2003-05-23 20:03:32 +00001074
Chris Lattner189d19f2003-11-21 20:23:48 +00001075namespace llvm {
1076 template<>
1077 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
1078 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
1079 if (V.first == Instruction::Cast)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001080 return new UnaryConstantExpr(Instruction::Cast, V.second[0], Ty);
Chris Lattner189d19f2003-11-21 20:23:48 +00001081 if ((V.first >= Instruction::BinaryOpsBegin &&
1082 V.first < Instruction::BinaryOpsEnd) ||
1083 V.first == Instruction::Shl || V.first == Instruction::Shr)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001084 return new BinaryConstantExpr(V.first, V.second[0], V.second[1]);
Chris Lattnerb7897ce2004-03-30 22:51:03 +00001085 if (V.first == Instruction::Select)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001086 return new SelectConstantExpr(V.second[0], V.second[1], V.second[2]);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001087
Chris Lattner189d19f2003-11-21 20:23:48 +00001088 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001089
Chris Lattner189d19f2003-11-21 20:23:48 +00001090 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001091 return new GetElementPtrConstantExpr(V.second[0], IdxList, Ty);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001092 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001093 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001094
Chris Lattner189d19f2003-11-21 20:23:48 +00001095 template<>
1096 struct ConvertConstantType<ConstantExpr, Type> {
1097 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1098 Constant *New;
1099 switch (OldC->getOpcode()) {
1100 case Instruction::Cast:
1101 New = ConstantExpr::getCast(OldC->getOperand(0), NewTy);
1102 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001103 case Instruction::Select:
1104 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1105 OldC->getOperand(1),
1106 OldC->getOperand(2));
1107 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001108 case Instruction::Shl:
1109 case Instruction::Shr:
1110 New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
1111 OldC->getOperand(0), OldC->getOperand(1));
1112 break;
1113 default:
1114 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
1115 OldC->getOpcode() < Instruction::BinaryOpsEnd);
1116 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1117 OldC->getOperand(1));
1118 break;
1119 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001120 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001121 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
1122 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), Idx);
Chris Lattner189d19f2003-11-21 20:23:48 +00001123 break;
1124 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001125
Chris Lattner189d19f2003-11-21 20:23:48 +00001126 assert(New != OldC && "Didn't replace constant??");
1127 OldC->uncheckedReplaceAllUsesWith(New);
1128 OldC->destroyConstant(); // This constant is now dead, destroy it.
1129 }
1130 };
1131} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001132
1133
Chris Lattner3e650af2004-08-04 04:48:01 +00001134static ExprMapKeyType getValType(ConstantExpr *CE) {
1135 std::vector<Constant*> Operands;
1136 Operands.reserve(CE->getNumOperands());
1137 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1138 Operands.push_back(cast<Constant>(CE->getOperand(i)));
1139 return ExprMapKeyType(CE->getOpcode(), Operands);
1140}
1141
Chris Lattner98fa07b2003-05-23 20:03:32 +00001142static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001143
Chris Lattner46b3d302003-04-16 22:40:51 +00001144Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001145 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1146
Chris Lattneracdbe712003-04-17 19:24:48 +00001147 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
1148 return FC; // Fold a few common cases...
1149
Vikram S. Adve4c485332002-07-15 18:19:33 +00001150 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001151 std::vector<Constant*> argVec(1, C);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001152 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
1153 return ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001154}
Chris Lattnerd7a73302001-10-13 06:57:33 +00001155
Chris Lattnerdd284742004-04-04 23:20:30 +00001156Constant *ConstantExpr::getSignExtend(Constant *C, const Type *Ty) {
Chris Lattner1ece6f82005-01-01 15:59:57 +00001157 assert(C->getType()->isIntegral() && Ty->isIntegral() &&
Chris Lattnerdd284742004-04-04 23:20:30 +00001158 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1159 "This is an illegal sign extension!");
Chris Lattner1ece6f82005-01-01 15:59:57 +00001160 if (C->getType() != Type::BoolTy) {
1161 C = ConstantExpr::getCast(C, C->getType()->getSignedVersion());
1162 return ConstantExpr::getCast(C, Ty);
1163 } else {
1164 if (C == ConstantBool::True)
1165 return ConstantIntegral::getAllOnesValue(Ty);
1166 else
1167 return ConstantIntegral::getNullValue(Ty);
1168 }
Chris Lattnerdd284742004-04-04 23:20:30 +00001169}
1170
1171Constant *ConstantExpr::getZeroExtend(Constant *C, const Type *Ty) {
Chris Lattner1ece6f82005-01-01 15:59:57 +00001172 assert(C->getType()->isIntegral() && Ty->isIntegral() &&
Chris Lattnerdd284742004-04-04 23:20:30 +00001173 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1174 "This is an illegal zero extension!");
Chris Lattner1ece6f82005-01-01 15:59:57 +00001175 if (C->getType() != Type::BoolTy)
1176 C = ConstantExpr::getCast(C, C->getType()->getUnsignedVersion());
Chris Lattnerdd284742004-04-04 23:20:30 +00001177 return ConstantExpr::getCast(C, Ty);
1178}
1179
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001180Constant *ConstantExpr::getSizeOf(const Type *Ty) {
Chris Lattneracc4e542004-12-13 19:48:51 +00001181 // sizeof is implemented as: (ulong) gep (Ty*)null, 1
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001182 return getCast(
Chris Lattneracc4e542004-12-13 19:48:51 +00001183 getGetElementPtr(getNullValue(PointerType::get(Ty)),
1184 std::vector<Constant*>(1, ConstantInt::get(Type::UIntTy, 1))),
1185 Type::ULongTy);
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001186}
1187
Alkis Evlogimenos9160d5f2005-03-19 11:40:31 +00001188Constant *ConstantExpr::getPtrPtrFromArrayPtr(Constant *C) {
1189 // pointer from array is implemented as: getelementptr arr ptr, 0, 0
1190 static std::vector<Constant*> Indices(2, ConstantUInt::get(Type::UIntTy, 0));
1191
1192 return ConstantExpr::getGetElementPtr(C, Indices);
1193}
1194
Chris Lattnerb50d1352003-10-05 00:17:43 +00001195Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
1196 Constant *C1, Constant *C2) {
Chris Lattner5645e8a2004-01-12 19:04:55 +00001197 if (Opcode == Instruction::Shl || Opcode == Instruction::Shr)
1198 return getShiftTy(ReqTy, Opcode, C1, C2);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001199 // Check the operands for consistency first
1200 assert((Opcode >= Instruction::BinaryOpsBegin &&
1201 Opcode < Instruction::BinaryOpsEnd) &&
1202 "Invalid opcode in binary constant expression");
1203 assert(C1->getType() == C2->getType() &&
1204 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001205
Chris Lattner29ca2c62004-08-04 18:50:09 +00001206 if (ReqTy == C1->getType() || (Instruction::isRelational(Opcode) &&
1207 ReqTy == Type::BoolTy))
Chris Lattnerb50d1352003-10-05 00:17:43 +00001208 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1209 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001210
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001211 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001212 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001213 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001214}
1215
Chris Lattner29ca2c62004-08-04 18:50:09 +00001216Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001217#ifndef NDEBUG
1218 switch (Opcode) {
1219 case Instruction::Add: case Instruction::Sub:
1220 case Instruction::Mul: case Instruction::Div:
1221 case Instruction::Rem:
1222 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001223 assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint()) &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001224 "Tried to create an arithmetic operation on a non-arithmetic type!");
1225 break;
1226 case Instruction::And:
1227 case Instruction::Or:
1228 case Instruction::Xor:
1229 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1230 assert(C1->getType()->isIntegral() &&
Misha Brukman3852f652005-01-27 06:46:38 +00001231 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001232 break;
1233 case Instruction::SetLT: case Instruction::SetGT: case Instruction::SetLE:
1234 case Instruction::SetGE: case Instruction::SetEQ: case Instruction::SetNE:
1235 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1236 break;
1237 case Instruction::Shl:
1238 case Instruction::Shr:
1239 assert(C2->getType() == Type::UByteTy && "Shift should be by ubyte!");
1240 assert(C1->getType()->isInteger() &&
1241 "Tried to create a shift operation on a non-integer type!");
1242 break;
1243 default:
1244 break;
1245 }
1246#endif
1247
Chris Lattner29ca2c62004-08-04 18:50:09 +00001248 if (Instruction::isRelational(Opcode))
1249 return getTy(Type::BoolTy, Opcode, C1, C2);
1250 else
1251 return getTy(C1->getType(), Opcode, C1, C2);
1252}
1253
Chris Lattner6e415c02004-03-12 05:54:04 +00001254Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
1255 Constant *V1, Constant *V2) {
1256 assert(C->getType() == Type::BoolTy && "Select condition must be bool!");
1257 assert(V1->getType() == V2->getType() && "Select value types must match!");
1258 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
1259
1260 if (ReqTy == V1->getType())
1261 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1262 return SC; // Fold common cases
1263
1264 std::vector<Constant*> argVec(3, C);
1265 argVec[1] = V1;
1266 argVec[2] = V2;
1267 ExprMapKeyType Key = std::make_pair(Instruction::Select, argVec);
1268 return ExprConstants.getOrCreate(ReqTy, Key);
1269}
1270
Chris Lattner9eb2b522004-01-12 19:12:58 +00001271/// getShiftTy - Return a shift left or shift right constant expr
Chris Lattnerb50d1352003-10-05 00:17:43 +00001272Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
1273 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001274 // Check the operands for consistency first
1275 assert((Opcode == Instruction::Shl ||
1276 Opcode == Instruction::Shr) &&
1277 "Invalid opcode in binary constant expression");
1278 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
1279 "Invalid operand types for Shift constant expr!");
1280
Chris Lattner0bba7712004-01-12 20:40:42 +00001281 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001282 return FC; // Fold a few common cases...
1283
1284 // Look up the constant in the table first to ensure uniqueness
1285 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001286 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001287 return ExprConstants.getOrCreate(ReqTy, Key);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001288}
1289
1290
Chris Lattnerb50d1352003-10-05 00:17:43 +00001291Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner13128ab2004-10-11 22:52:25 +00001292 const std::vector<Value*> &IdxList) {
1293 assert(GetElementPtrInst::getIndexedType(C->getType(), IdxList, true) &&
Chris Lattner04b60fe2004-02-16 20:46:13 +00001294 "GEP indices invalid!");
1295
Chris Lattneracdbe712003-04-17 19:24:48 +00001296 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
1297 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001298
Chris Lattnerb50d1352003-10-05 00:17:43 +00001299 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001300 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001301 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00001302 std::vector<Constant*> ArgVec;
1303 ArgVec.reserve(IdxList.size()+1);
1304 ArgVec.push_back(C);
1305 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
1306 ArgVec.push_back(cast<Constant>(IdxList[i]));
1307 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,ArgVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001308 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001309}
1310
Chris Lattnerb50d1352003-10-05 00:17:43 +00001311Constant *ConstantExpr::getGetElementPtr(Constant *C,
1312 const std::vector<Constant*> &IdxList){
1313 // Get the result type of the getelementptr!
1314 std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
1315
1316 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
1317 true);
1318 assert(Ty && "GEP indices invalid!");
Chris Lattner13128ab2004-10-11 22:52:25 +00001319 return getGetElementPtrTy(PointerType::get(Ty), C, VIdxList);
1320}
1321
1322Constant *ConstantExpr::getGetElementPtr(Constant *C,
1323 const std::vector<Value*> &IdxList) {
1324 // Get the result type of the getelementptr!
1325 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
1326 true);
1327 assert(Ty && "GEP indices invalid!");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001328 return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
1329}
1330
1331
Vikram S. Adve4c485332002-07-15 18:19:33 +00001332// destroyConstant - Remove the constant from the constant table...
1333//
1334void ConstantExpr::destroyConstant() {
1335 ExprConstants.remove(this);
1336 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001337}
1338
Chris Lattner3cd8c562002-07-30 18:54:25 +00001339const char *ConstantExpr::getOpcodeName() const {
1340 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001341}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00001342
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001343//===----------------------------------------------------------------------===//
1344// replaceUsesOfWithOnConstant implementations
1345
1346void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
1347 bool DisableChecking) {
1348 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
1349
Chris Lattnerb64419a2005-10-03 22:51:37 +00001350 std::pair<ArrayConstantsTy::MapKey, ConstantArray*> Lookup;
1351 Lookup.first.first = getType();
1352 Lookup.second = this;
1353 std::vector<Constant*> &Values = Lookup.first.second;
1354 Values.reserve(getNumOperands()); // Build replacement array.
1355
1356 bool isAllZeros = false; // Does this turn into an all-zeros array?
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001357 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1358 Constant *Val = getOperand(i);
1359 if (Val == From) Val = cast<Constant>(To);
1360 Values.push_back(Val);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001361 if (isAllZeros) isAllZeros = Val->isNullValue();
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001362 }
1363
Chris Lattnerb64419a2005-10-03 22:51:37 +00001364 Constant *Replacement = 0;
1365 if (isAllZeros) {
1366 Replacement = ConstantAggregateZero::get(getType());
1367 } else {
1368 // Check to see if we have this array type already.
1369 bool Exists;
1370 ArrayConstantsTy::MapIterator I =
1371 ArrayConstants.InsertOrGetItem(Lookup, Exists);
1372
1373 if (Exists) {
1374 Replacement = I->second;
1375 } else {
1376 // Okay, the new shape doesn't exist in the system yet. Instead of
1377 // creating a new constant array, inserting it, replaceallusesof'ing the
1378 // old with the new, then deleting the old... just update the current one
1379 // in place!
1380 if (I != ArrayConstants.map_end() && I->second == this)
1381 ++I; // Do not invalidate iterator!
1382 ArrayConstants.remove(this); // Remove old shape from the map.
1383
1384 // Update to the new values.
1385 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1386 if (getOperand(i) == From)
1387 setOperand(i, cast<Constant>(To));
1388 return;
1389 }
1390 }
1391
1392 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001393 assert(Replacement != this && "I didn't contain From!");
1394
1395 // Everyone using this now uses the replacement...
1396 if (DisableChecking)
1397 uncheckedReplaceAllUsesWith(Replacement);
1398 else
1399 replaceAllUsesWith(Replacement);
1400
1401 // Delete the old constant!
1402 destroyConstant();
1403}
1404
1405void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
1406 bool DisableChecking) {
1407 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
1408
1409 std::vector<Constant*> Values;
1410 Values.reserve(getNumOperands()); // Build replacement array...
1411 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1412 Constant *Val = getOperand(i);
1413 if (Val == From) Val = cast<Constant>(To);
1414 Values.push_back(Val);
1415 }
1416
1417 Constant *Replacement = ConstantStruct::get(getType(), Values);
1418 assert(Replacement != this && "I didn't contain From!");
1419
1420 // Everyone using this now uses the replacement...
1421 if (DisableChecking)
1422 uncheckedReplaceAllUsesWith(Replacement);
1423 else
1424 replaceAllUsesWith(Replacement);
1425
1426 // Delete the old constant!
1427 destroyConstant();
1428}
1429
1430void ConstantPacked::replaceUsesOfWithOnConstant(Value *From, Value *To,
1431 bool DisableChecking) {
1432 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
1433
1434 std::vector<Constant*> Values;
1435 Values.reserve(getNumOperands()); // Build replacement array...
1436 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1437 Constant *Val = getOperand(i);
1438 if (Val == From) Val = cast<Constant>(To);
1439 Values.push_back(Val);
1440 }
1441
1442 Constant *Replacement = ConstantPacked::get(getType(), Values);
1443 assert(Replacement != this && "I didn't contain From!");
1444
1445 // Everyone using this now uses the replacement...
1446 if (DisableChecking)
1447 uncheckedReplaceAllUsesWith(Replacement);
1448 else
1449 replaceAllUsesWith(Replacement);
1450
1451 // Delete the old constant!
1452 destroyConstant();
1453}
1454
1455void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
1456 bool DisableChecking) {
1457 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
1458 Constant *To = cast<Constant>(ToV);
1459
1460 Constant *Replacement = 0;
1461 if (getOpcode() == Instruction::GetElementPtr) {
1462 std::vector<Constant*> Indices;
1463 Constant *Pointer = getOperand(0);
1464 Indices.reserve(getNumOperands()-1);
1465 if (Pointer == From) Pointer = To;
1466
1467 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1468 Constant *Val = getOperand(i);
1469 if (Val == From) Val = To;
1470 Indices.push_back(Val);
1471 }
1472 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
1473 } else if (getOpcode() == Instruction::Cast) {
1474 assert(getOperand(0) == From && "Cast only has one use!");
1475 Replacement = ConstantExpr::getCast(To, getType());
1476 } else if (getOpcode() == Instruction::Select) {
1477 Constant *C1 = getOperand(0);
1478 Constant *C2 = getOperand(1);
1479 Constant *C3 = getOperand(2);
1480 if (C1 == From) C1 = To;
1481 if (C2 == From) C2 = To;
1482 if (C3 == From) C3 = To;
1483 Replacement = ConstantExpr::getSelect(C1, C2, C3);
1484 } else if (getNumOperands() == 2) {
1485 Constant *C1 = getOperand(0);
1486 Constant *C2 = getOperand(1);
1487 if (C1 == From) C1 = To;
1488 if (C2 == From) C2 = To;
1489 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
1490 } else {
1491 assert(0 && "Unknown ConstantExpr type!");
1492 return;
1493 }
1494
1495 assert(Replacement != this && "I didn't contain From!");
1496
1497 // Everyone using this now uses the replacement...
1498 if (DisableChecking)
1499 uncheckedReplaceAllUsesWith(Replacement);
1500 else
1501 replaceAllUsesWith(Replacement);
1502
1503 // Delete the old constant!
1504 destroyConstant();
1505}
1506
1507
1508
Chris Lattner99a669b2004-11-19 16:39:44 +00001509/// clearAllValueMaps - This method frees all internal memory used by the
1510/// constant subsystem, which can be used in environments where this memory
1511/// is otherwise reported as a leak.
1512void Constant::clearAllValueMaps() {
1513 std::vector<Constant *> Constants;
1514
1515 DoubleConstants.clear(Constants);
1516 FloatConstants.clear(Constants);
1517 SIntConstants.clear(Constants);
1518 UIntConstants.clear(Constants);
1519 AggZeroConstants.clear(Constants);
1520 ArrayConstants.clear(Constants);
1521 StructConstants.clear(Constants);
1522 PackedConstants.clear(Constants);
1523 NullPtrConstants.clear(Constants);
1524 UndefValueConstants.clear(Constants);
1525 ExprConstants.clear(Constants);
1526
Misha Brukmanb1c93172005-04-21 23:48:37 +00001527 for (std::vector<Constant *>::iterator I = Constants.begin(),
Chris Lattner99a669b2004-11-19 16:39:44 +00001528 E = Constants.end(); I != E; ++I)
1529 (*I)->dropAllReferences();
1530 for (std::vector<Constant *>::iterator I = Constants.begin(),
1531 E = Constants.end(); I != E; ++I)
1532 (*I)->destroyConstantImpl();
1533 Constants.clear();
1534}