blob: 71754fdac4b93428ee875bcd06a507e18f75c062 [file] [log] [blame]
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001//===-- Constants.cpp - Implement Constant nodes --------------------------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner3462ae32001-12-03 22:26:30 +000010// This file implements the Constant* classes...
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerca142372002-04-28 19:55:58 +000014#include "llvm/Constants.h"
Chris Lattner5a945e32004-01-12 21:13:12 +000015#include "ConstantFolding.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000016#include "llvm/DerivedTypes.h"
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000017#include "llvm/GlobalValue.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000018#include "llvm/Instructions.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000019#include "llvm/SymbolTable.h"
Chris Lattnerd7a73302001-10-13 06:57:33 +000020#include "llvm/Module.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000021#include "llvm/ADT/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
Chris Lattner3462ae32001-12-03 22:26:30 +000034void Constant::destroyConstantImpl() {
35 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000036 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000037 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +000038 // but they don't know that. Because we only find out when the CPV is
39 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000040 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000041 //
42 while (!use_empty()) {
43 Value *V = use_back();
44#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000045 if (!isa<Constant>(V))
46 std::cerr << "While deleting: " << *this
47 << "\n\nUse still stuck around after Def is destroyed: "
48 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000049#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000050 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000051 Constant *CV = cast<Constant>(V);
52 CV->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +000053
54 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000055 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000056 }
57
58 // Value has no outstanding references it is safe to delete it now...
59 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000060}
Chris Lattner2f7c9632001-06-06 20:29:01 +000061
Chris Lattnerb1585a92002-08-13 17:50:20 +000062// Static constructor to create a '0' constant of arbitrary type...
63Constant *Constant::getNullValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +000064 switch (Ty->getTypeID()) {
Chris Lattner3e88ef92003-10-03 19:34:51 +000065 case Type::BoolTyID: {
66 static Constant *NullBool = ConstantBool::get(false);
67 return NullBool;
68 }
69 case Type::SByteTyID: {
70 static Constant *NullSByte = ConstantSInt::get(Type::SByteTy, 0);
71 return NullSByte;
72 }
73 case Type::UByteTyID: {
74 static Constant *NullUByte = ConstantUInt::get(Type::UByteTy, 0);
75 return NullUByte;
76 }
77 case Type::ShortTyID: {
78 static Constant *NullShort = ConstantSInt::get(Type::ShortTy, 0);
79 return NullShort;
80 }
81 case Type::UShortTyID: {
82 static Constant *NullUShort = ConstantUInt::get(Type::UShortTy, 0);
83 return NullUShort;
84 }
85 case Type::IntTyID: {
86 static Constant *NullInt = ConstantSInt::get(Type::IntTy, 0);
87 return NullInt;
88 }
89 case Type::UIntTyID: {
90 static Constant *NullUInt = ConstantUInt::get(Type::UIntTy, 0);
91 return NullUInt;
92 }
93 case Type::LongTyID: {
94 static Constant *NullLong = ConstantSInt::get(Type::LongTy, 0);
95 return NullLong;
96 }
97 case Type::ULongTyID: {
98 static Constant *NullULong = ConstantUInt::get(Type::ULongTy, 0);
99 return NullULong;
100 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000101
Chris Lattner3e88ef92003-10-03 19:34:51 +0000102 case Type::FloatTyID: {
103 static Constant *NullFloat = ConstantFP::get(Type::FloatTy, 0);
104 return NullFloat;
105 }
106 case Type::DoubleTyID: {
107 static Constant *NullDouble = ConstantFP::get(Type::DoubleTy, 0);
108 return NullDouble;
109 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000110
111 case Type::PointerTyID:
112 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner3e88ef92003-10-03 19:34:51 +0000113
Chris Lattner9fba3da2004-02-15 05:53:04 +0000114 case Type::StructTyID:
115 case Type::ArrayTyID:
Brian Gaeke02209042004-08-20 06:00:58 +0000116 case Type::PackedTyID:
Chris Lattner9fba3da2004-02-15 05:53:04 +0000117 return ConstantAggregateZero::get(Ty);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000118 default:
Reid Spencercf394bf2004-07-04 11:51:24 +0000119 // Function, Label, or Opaque type?
120 assert(!"Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000121 return 0;
122 }
123}
124
125// Static constructor to create the maximum constant of an integral type...
126ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000127 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000128 case Type::BoolTyID: return ConstantBool::True;
129 case Type::SByteTyID:
130 case Type::ShortTyID:
131 case Type::IntTyID:
132 case Type::LongTyID: {
133 // Calculate 011111111111111...
134 unsigned TypeBits = Ty->getPrimitiveSize()*8;
135 int64_t Val = INT64_MAX; // All ones
136 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
137 return ConstantSInt::get(Ty, Val);
138 }
139
140 case Type::UByteTyID:
141 case Type::UShortTyID:
142 case Type::UIntTyID:
143 case Type::ULongTyID: return getAllOnesValue(Ty);
144
Chris Lattner31408f72002-08-14 17:12:13 +0000145 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000146 }
147}
148
149// Static constructor to create the minimum constant for an integral type...
150ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000151 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000152 case Type::BoolTyID: return ConstantBool::False;
153 case Type::SByteTyID:
154 case Type::ShortTyID:
155 case Type::IntTyID:
156 case Type::LongTyID: {
157 // Calculate 1111111111000000000000
158 unsigned TypeBits = Ty->getPrimitiveSize()*8;
159 int64_t Val = -1; // All ones
160 Val <<= TypeBits-1; // Shift over to the right spot
161 return ConstantSInt::get(Ty, Val);
162 }
163
164 case Type::UByteTyID:
165 case Type::UShortTyID:
166 case Type::UIntTyID:
167 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
168
Chris Lattner31408f72002-08-14 17:12:13 +0000169 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000170 }
171}
172
173// Static constructor to create an integral constant with all bits set
174ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000175 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000176 case Type::BoolTyID: return ConstantBool::True;
177 case Type::SByteTyID:
178 case Type::ShortTyID:
179 case Type::IntTyID:
180 case Type::LongTyID: return ConstantSInt::get(Ty, -1);
181
182 case Type::UByteTyID:
183 case Type::UShortTyID:
184 case Type::UIntTyID:
185 case Type::ULongTyID: {
186 // Calculate ~0 of the right type...
187 unsigned TypeBits = Ty->getPrimitiveSize()*8;
188 uint64_t Val = ~0ULL; // All ones
189 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
190 return ConstantUInt::get(Ty, Val);
191 }
Chris Lattner31408f72002-08-14 17:12:13 +0000192 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000193 }
194}
195
Chris Lattner83e5d392003-03-10 22:39:02 +0000196bool ConstantUInt::isAllOnesValue() const {
197 unsigned TypeBits = getType()->getPrimitiveSize()*8;
198 uint64_t Val = ~0ULL; // All ones
199 Val >>= 64-TypeBits; // Shift out inappropriate bits
200 return getValue() == Val;
201}
202
Chris Lattnerb1585a92002-08-13 17:50:20 +0000203
Chris Lattner2f7c9632001-06-06 20:29:01 +0000204//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +0000205// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +0000206//===----------------------------------------------------------------------===//
207
208//===----------------------------------------------------------------------===//
209// Normal Constructors
210
Chris Lattner265eb642004-06-21 12:12:12 +0000211ConstantIntegral::ConstantIntegral(const Type *Ty, uint64_t V)
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000212 : Constant(Ty, SimpleConstantVal, 0, 0) {
Chris Lattner265eb642004-06-21 12:12:12 +0000213 Val.Unsigned = V;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000214}
Chris Lattner49d855c2001-09-07 16:46:31 +0000215
Chris Lattner265eb642004-06-21 12:12:12 +0000216ConstantBool::ConstantBool(bool V) : ConstantIntegral(Type::BoolTy, V) {
217}
218
219ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : ConstantIntegral(Ty, V) {
Chris Lattner7309d662001-07-21 19:16:08 +0000220}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000221
Chris Lattner3462ae32001-12-03 22:26:30 +0000222ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000223 assert(Ty->isInteger() && Ty->isSigned() &&
Reid Spencerf064bb22005-03-09 15:19:41 +0000224 "Illegal type for signed integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000225 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000226}
227
Chris Lattner3462ae32001-12-03 22:26:30 +0000228ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000229 assert(Ty->isInteger() && Ty->isUnsigned() &&
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 Lattnerd0df99c2005-01-29 00:34:39 +0000234ConstantFP::ConstantFP(const Type *Ty, double V)
235 : Constant(Ty, SimpleConstantVal, 0, 0) {
Chris Lattner9655e542001-07-20 19:16:02 +0000236 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000237 Val = V;
238}
239
Chris Lattner3462ae32001-12-03 22:26:30 +0000240ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000241 const std::vector<Constant*> &V)
242 : Constant(T, SimpleConstantVal, new Use[V.size()], V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000243 assert(V.size() == T->getNumElements() &&
244 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000245 Use *OL = OperandList;
Chris Lattner0d779712002-10-08 23:33:52 +0000246 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000247 assert((V[i]->getType() == T->getElementType() ||
248 (T->isAbstract() &&
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000249 V[i]->getType()->getTypeID()==T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000250 "Initializer for array element doesn't match array element type!");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000251 OL[i].init(V[i], this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000252 }
253}
254
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000255ConstantArray::~ConstantArray() {
256 delete [] OperandList;
257}
258
Chris Lattner3462ae32001-12-03 22:26:30 +0000259ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000260 const std::vector<Constant*> &V)
261 : Constant(T, SimpleConstantVal, new Use[V.size()], V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000262 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000263 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000264 Use *OL = OperandList;
Chris Lattner0d779712002-10-08 23:33:52 +0000265 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000266 assert((V[i]->getType() == T->getElementType(i) ||
267 ((T->getElementType(i)->isAbstract() ||
268 V[i]->getType()->isAbstract()) &&
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000269 T->getElementType(i)->getTypeID()==V[i]->getType()->getTypeID()))&&
Chris Lattner93c8f142003-06-02 17:42:47 +0000270 "Initializer for struct element doesn't match struct element type!");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000271 OL[i].init(V[i], this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000272 }
273}
274
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000275ConstantStruct::~ConstantStruct() {
276 delete [] OperandList;
277}
278
279
Brian Gaeke02209042004-08-20 06:00:58 +0000280ConstantPacked::ConstantPacked(const PackedType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000281 const std::vector<Constant*> &V)
282 : Constant(T, SimpleConstantVal, new Use[V.size()], V.size()) {
283 Use *OL = OperandList;
Brian Gaeke02209042004-08-20 06:00:58 +0000284 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000285 assert((V[i]->getType() == T->getElementType() ||
286 (T->isAbstract() &&
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000287 V[i]->getType()->getTypeID()==T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000288 "Initializer for packed element doesn't match packed element type!");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000289 OL[i].init(V[i], this);
Brian Gaeke02209042004-08-20 06:00:58 +0000290 }
291}
292
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000293ConstantPacked::~ConstantPacked() {
294 delete [] OperandList;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000295}
296
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000297/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
298/// behind the scenes to implement unary constant exprs.
299class UnaryConstantExpr : public ConstantExpr {
300 Use Op;
301public:
302 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
303 : ConstantExpr(Ty, Opcode, &Op, 1), Op(C, this) {}
304};
Chris Lattner6e415c02004-03-12 05:54:04 +0000305
Chris Lattner22ced562003-06-22 20:48:30 +0000306static bool isSetCC(unsigned Opcode) {
307 return Opcode == Instruction::SetEQ || Opcode == Instruction::SetNE ||
308 Opcode == Instruction::SetLT || Opcode == Instruction::SetGT ||
309 Opcode == Instruction::SetLE || Opcode == Instruction::SetGE;
310}
311
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000312/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
313/// behind the scenes to implement binary constant exprs.
314class BinaryConstantExpr : public ConstantExpr {
315 Use Ops[2];
316public:
317 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
318 : ConstantExpr(isSetCC(Opcode) ? Type::BoolTy : C1->getType(),
319 Opcode, Ops, 2) {
320 Ops[0].init(C1, this);
321 Ops[1].init(C2, this);
322 }
323};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000324
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000325/// SelectConstantExpr - This class is private to Constants.cpp, and is used
326/// behind the scenes to implement select constant exprs.
327class SelectConstantExpr : public ConstantExpr {
328 Use Ops[3];
329public:
330 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
331 : ConstantExpr(C2->getType(), Instruction::Select, Ops, 3) {
332 Ops[0].init(C1, this);
333 Ops[1].init(C2, this);
334 Ops[2].init(C3, this);
335 }
336};
337
338/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
339/// used behind the scenes to implement getelementpr constant exprs.
340struct GetElementPtrConstantExpr : public ConstantExpr {
341 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
342 const Type *DestTy)
343 : ConstantExpr(DestTy, Instruction::GetElementPtr,
344 new Use[IdxList.size()+1], IdxList.size()+1) {
345 OperandList[0].init(C, this);
346 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
347 OperandList[i+1].init(IdxList[i], this);
348 }
349 ~GetElementPtrConstantExpr() {
350 delete [] OperandList;
351 }
352};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000353
Chris Lattner817175f2004-03-29 02:37:53 +0000354/// ConstantExpr::get* - Return some common constants without having to
355/// specify the full Instruction::OPCODE identifier.
356///
357Constant *ConstantExpr::getNeg(Constant *C) {
Chris Lattner3cdc27c2004-03-29 19:51:24 +0000358 if (!C->getType()->isFloatingPoint())
359 return get(Instruction::Sub, getNullValue(C->getType()), C);
360 else
361 return get(Instruction::Sub, ConstantFP::get(C->getType(), -0.0), C);
Chris Lattner817175f2004-03-29 02:37:53 +0000362}
363Constant *ConstantExpr::getNot(Constant *C) {
364 assert(isa<ConstantIntegral>(C) && "Cannot NOT a nonintegral type!");
365 return get(Instruction::Xor, C,
366 ConstantIntegral::getAllOnesValue(C->getType()));
367}
368Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
369 return get(Instruction::Add, C1, C2);
370}
371Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
372 return get(Instruction::Sub, C1, C2);
373}
374Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
375 return get(Instruction::Mul, C1, C2);
376}
377Constant *ConstantExpr::getDiv(Constant *C1, Constant *C2) {
378 return get(Instruction::Div, C1, C2);
379}
380Constant *ConstantExpr::getRem(Constant *C1, Constant *C2) {
381 return get(Instruction::Rem, C1, C2);
382}
383Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
384 return get(Instruction::And, C1, C2);
385}
386Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
387 return get(Instruction::Or, C1, C2);
388}
389Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
390 return get(Instruction::Xor, C1, C2);
391}
392Constant *ConstantExpr::getSetEQ(Constant *C1, Constant *C2) {
393 return get(Instruction::SetEQ, C1, C2);
394}
395Constant *ConstantExpr::getSetNE(Constant *C1, Constant *C2) {
396 return get(Instruction::SetNE, C1, C2);
397}
398Constant *ConstantExpr::getSetLT(Constant *C1, Constant *C2) {
399 return get(Instruction::SetLT, C1, C2);
400}
401Constant *ConstantExpr::getSetGT(Constant *C1, Constant *C2) {
402 return get(Instruction::SetGT, C1, C2);
403}
404Constant *ConstantExpr::getSetLE(Constant *C1, Constant *C2) {
405 return get(Instruction::SetLE, C1, C2);
406}
407Constant *ConstantExpr::getSetGE(Constant *C1, Constant *C2) {
408 return get(Instruction::SetGE, C1, C2);
409}
410Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
411 return get(Instruction::Shl, C1, C2);
412}
413Constant *ConstantExpr::getShr(Constant *C1, Constant *C2) {
414 return get(Instruction::Shr, C1, C2);
415}
416
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000417Constant *ConstantExpr::getUShr(Constant *C1, Constant *C2) {
418 if (C1->getType()->isUnsigned()) return getShr(C1, C2);
419 return getCast(getShr(getCast(C1,
420 C1->getType()->getUnsignedVersion()), C2), C1->getType());
421}
Chris Lattner817175f2004-03-29 02:37:53 +0000422
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000423Constant *ConstantExpr::getSShr(Constant *C1, Constant *C2) {
424 if (C1->getType()->isSigned()) return getShr(C1, C2);
425 return getCast(getShr(getCast(C1,
426 C1->getType()->getSignedVersion()), C2), C1->getType());
427}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000428
Chris Lattner2f7c9632001-06-06 20:29:01 +0000429
430//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000431// isValueValidForType implementations
432
Chris Lattner3462ae32001-12-03 22:26:30 +0000433bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000434 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000435 default:
436 return false; // These can't be represented as integers!!!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000437 // Signed types...
438 case Type::SByteTyID:
439 return (Val <= INT8_MAX && Val >= INT8_MIN);
440 case Type::ShortTyID:
441 return (Val <= INT16_MAX && Val >= INT16_MIN);
442 case Type::IntTyID:
Chris Lattner74248512004-06-08 23:21:39 +0000443 return (Val <= int(INT32_MAX) && Val >= int(INT32_MIN));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000444 case Type::LongTyID:
445 return true; // This is the largest type...
446 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000447}
448
Chris Lattner3462ae32001-12-03 22:26:30 +0000449bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000450 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000451 default:
452 return false; // These can't be represented as integers!!!
453
454 // Unsigned types...
455 case Type::UByteTyID:
456 return (Val <= UINT8_MAX);
457 case Type::UShortTyID:
458 return (Val <= UINT16_MAX);
459 case Type::UIntTyID:
460 return (Val <= UINT32_MAX);
461 case Type::ULongTyID:
462 return true; // This is the largest type...
463 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000464}
465
Chris Lattner3462ae32001-12-03 22:26:30 +0000466bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000467 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000468 default:
469 return false; // These can't be represented as floating point!
470
Reid Spencerb95f8ab2004-12-07 07:38:08 +0000471 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000472 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000473 case Type::DoubleTyID:
474 return true; // This is the largest type...
475 }
476};
Chris Lattner9655e542001-07-20 19:16:02 +0000477
Chris Lattner49d855c2001-09-07 16:46:31 +0000478//===----------------------------------------------------------------------===//
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000479// replaceUsesOfWithOnConstant implementations
480
Chris Lattnerc27038d2003-08-29 05:36:46 +0000481void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
482 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000483 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
484
485 std::vector<Constant*> Values;
Alkis Evlogimenosf0cc8142004-08-04 08:02:59 +0000486 Values.reserve(getNumOperands()); // Build replacement array...
487 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
488 Constant *Val = getOperand(i);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000489 if (Val == From) Val = cast<Constant>(To);
490 Values.push_back(Val);
491 }
492
Chris Lattnerc75bf522004-02-15 04:05:58 +0000493 Constant *Replacement = ConstantArray::get(getType(), Values);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000494 assert(Replacement != this && "I didn't contain From!");
495
496 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000497 if (DisableChecking)
498 uncheckedReplaceAllUsesWith(Replacement);
499 else
500 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000501
502 // Delete the old constant!
503 destroyConstant();
504}
505
Chris Lattnerc27038d2003-08-29 05:36:46 +0000506void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
507 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000508 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
509
510 std::vector<Constant*> Values;
Alkis Evlogimenosf0cc8142004-08-04 08:02:59 +0000511 Values.reserve(getNumOperands()); // Build replacement array...
512 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
513 Constant *Val = getOperand(i);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000514 if (Val == From) Val = cast<Constant>(To);
515 Values.push_back(Val);
516 }
517
Chris Lattner37a716f2004-02-15 04:07:32 +0000518 Constant *Replacement = ConstantStruct::get(getType(), Values);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000519 assert(Replacement != this && "I didn't contain From!");
520
521 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000522 if (DisableChecking)
523 uncheckedReplaceAllUsesWith(Replacement);
524 else
525 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000526
527 // Delete the old constant!
528 destroyConstant();
529}
530
Brian Gaeke02209042004-08-20 06:00:58 +0000531void ConstantPacked::replaceUsesOfWithOnConstant(Value *From, Value *To,
532 bool DisableChecking) {
533 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
534
535 std::vector<Constant*> Values;
536 Values.reserve(getNumOperands()); // Build replacement array...
537 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
538 Constant *Val = getOperand(i);
539 if (Val == From) Val = cast<Constant>(To);
540 Values.push_back(Val);
541 }
542
543 Constant *Replacement = ConstantPacked::get(getType(), Values);
544 assert(Replacement != this && "I didn't contain From!");
545
546 // Everyone using this now uses the replacement...
547 if (DisableChecking)
548 uncheckedReplaceAllUsesWith(Replacement);
549 else
550 replaceAllUsesWith(Replacement);
551
552 // Delete the old constant!
553 destroyConstant();
554}
555
Chris Lattnerc27038d2003-08-29 05:36:46 +0000556void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
557 bool DisableChecking) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000558 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
559 Constant *To = cast<Constant>(ToV);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000560
Chris Lattner46b3d302003-04-16 22:40:51 +0000561 Constant *Replacement = 0;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000562 if (getOpcode() == Instruction::GetElementPtr) {
563 std::vector<Constant*> Indices;
Chris Lattner55ed6562003-05-14 17:51:05 +0000564 Constant *Pointer = getOperand(0);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000565 Indices.reserve(getNumOperands()-1);
Chris Lattner55ed6562003-05-14 17:51:05 +0000566 if (Pointer == From) Pointer = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000567
568 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000569 Constant *Val = getOperand(i);
570 if (Val == From) Val = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000571 Indices.push_back(Val);
572 }
573 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
574 } else if (getOpcode() == Instruction::Cast) {
575 assert(getOperand(0) == From && "Cast only has one use!");
Chris Lattner55ed6562003-05-14 17:51:05 +0000576 Replacement = ConstantExpr::getCast(To, getType());
Chris Lattner467cb2b2004-03-31 02:56:11 +0000577 } else if (getOpcode() == Instruction::Select) {
578 Constant *C1 = getOperand(0);
579 Constant *C2 = getOperand(1);
580 Constant *C3 = getOperand(2);
581 if (C1 == From) C1 = To;
582 if (C2 == From) C2 = To;
583 if (C3 == From) C3 = To;
584 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000585 } else if (getNumOperands() == 2) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000586 Constant *C1 = getOperand(0);
587 Constant *C2 = getOperand(1);
588 if (C1 == From) C1 = To;
589 if (C2 == From) C2 = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000590 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
591 } else {
592 assert(0 && "Unknown ConstantExpr type!");
593 return;
594 }
595
596 assert(Replacement != this && "I didn't contain From!");
597
598 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000599 if (DisableChecking)
600 uncheckedReplaceAllUsesWith(Replacement);
601 else
602 replaceAllUsesWith(Replacement);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000603
604 // Delete the old constant!
605 destroyConstant();
606}
607
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000608//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000609// Factory Function Implementation
610
Chris Lattner98fa07b2003-05-23 20:03:32 +0000611// ConstantCreator - A class that is used to create constants by
612// ValueMap*. This class should be partially specialized if there is
613// something strange that needs to be done to interface to the ctor for the
614// constant.
615//
Chris Lattner189d19f2003-11-21 20:23:48 +0000616namespace llvm {
617 template<class ConstantClass, class TypeClass, class ValType>
618 struct ConstantCreator {
619 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
620 return new ConstantClass(Ty, V);
621 }
622 };
623
624 template<class ConstantClass, class TypeClass>
625 struct ConvertConstantType {
626 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
627 assert(0 && "This type cannot be converted!\n");
628 abort();
629 }
630 };
631}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000632
Chris Lattner98fa07b2003-05-23 20:03:32 +0000633namespace {
634 template<class ValType, class TypeClass, class ConstantClass>
Chris Lattnerb50d1352003-10-05 00:17:43 +0000635 class ValueMap : public AbstractTypeUser {
636 typedef std::pair<const TypeClass*, ValType> MapKey;
637 typedef std::map<MapKey, ConstantClass *> MapTy;
638 typedef typename MapTy::iterator MapIterator;
639 MapTy Map;
640
641 typedef std::map<const TypeClass*, MapIterator> AbstractTypeMapTy;
642 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner99a669b2004-11-19 16:39:44 +0000643
644 friend void Constant::clearAllValueMaps();
645 private:
646 void clear(std::vector<Constant *> &Constants) {
647 for(MapIterator I = Map.begin(); I != Map.end(); ++I)
648 Constants.push_back(I->second);
649 Map.clear();
650 AbstractTypeMap.clear();
651 }
652
Chris Lattner98fa07b2003-05-23 20:03:32 +0000653 public:
654 // getOrCreate - Return the specified constant from the map, creating it if
655 // necessary.
656 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000657 MapKey Lookup(Ty, V);
658 MapIterator I = Map.lower_bound(Lookup);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000659 if (I != Map.end() && I->first == Lookup)
660 return I->second; // Is it in the map?
661
662 // If no preexisting value, create one now...
663 ConstantClass *Result =
664 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
665
Chris Lattnerb50d1352003-10-05 00:17:43 +0000666
667 /// FIXME: why does this assert fail when loading 176.gcc?
668 //assert(Result->getType() == Ty && "Type specified is not correct!");
669 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
670
671 // If the type of the constant is abstract, make sure that an entry exists
672 // for it in the AbstractTypeMap.
673 if (Ty->isAbstract()) {
674 typename AbstractTypeMapTy::iterator TI =
675 AbstractTypeMap.lower_bound(Ty);
676
677 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
678 // Add ourselves to the ATU list of the type.
679 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
680
681 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
682 }
683 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000684 return Result;
685 }
686
687 void remove(ConstantClass *CP) {
Chris Lattner3e650af2004-08-04 04:48:01 +0000688 MapIterator I = Map.find(MapKey((TypeClass*)CP->getRawType(),
689 getValType(CP)));
Chris Lattner20a4dab2004-08-04 22:26:13 +0000690 if (I == Map.end() || I->second != CP) {
691 // FIXME: This should not use a linear scan. If this gets to be a
692 // performance problem, someone should look at this.
693 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
694 /* empty */;
695 }
696
Chris Lattnerb50d1352003-10-05 00:17:43 +0000697 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +0000698 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +0000699
700 // Now that we found the entry, make sure this isn't the entry that
701 // the AbstractTypeMap points to.
702 const TypeClass *Ty = I->first.first;
703 if (Ty->isAbstract()) {
704 assert(AbstractTypeMap.count(Ty) &&
705 "Abstract type not in AbstractTypeMap?");
706 MapIterator &ATMEntryIt = AbstractTypeMap[Ty];
707 if (ATMEntryIt == I) {
708 // Yes, we are removing the representative entry for this type.
709 // See if there are any other entries of the same type.
710 MapIterator TmpIt = ATMEntryIt;
711
712 // First check the entry before this one...
713 if (TmpIt != Map.begin()) {
714 --TmpIt;
715 if (TmpIt->first.first != Ty) // Not the same type, move back...
716 ++TmpIt;
717 }
718
719 // If we didn't find the same type, try to move forward...
720 if (TmpIt == ATMEntryIt) {
721 ++TmpIt;
722 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
723 --TmpIt; // No entry afterwards with the same type
724 }
725
726 // If there is another entry in the map of the same abstract type,
727 // update the AbstractTypeMap entry now.
728 if (TmpIt != ATMEntryIt) {
729 ATMEntryIt = TmpIt;
730 } else {
731 // Otherwise, we are removing the last instance of this type
732 // from the table. Remove from the ATM, and from user list.
733 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
734 AbstractTypeMap.erase(Ty);
735 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000736 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000737 }
738
739 Map.erase(I);
740 }
741
742 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
743 typename AbstractTypeMapTy::iterator I =
744 AbstractTypeMap.find(cast<TypeClass>(OldTy));
745
746 assert(I != AbstractTypeMap.end() &&
747 "Abstract type not in AbstractTypeMap?");
748
749 // Convert a constant at a time until the last one is gone. The last one
750 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
751 // eliminated eventually.
752 do {
753 ConvertConstantType<ConstantClass,
754 TypeClass>::convert(I->second->second,
755 cast<TypeClass>(NewTy));
756
757 I = AbstractTypeMap.find(cast<TypeClass>(OldTy));
758 } while (I != AbstractTypeMap.end());
759 }
760
761 // If the type became concrete without being refined to any other existing
762 // type, we just remove ourselves from the ATU list.
763 void typeBecameConcrete(const DerivedType *AbsTy) {
764 AbsTy->removeAbstractTypeUser(this);
765 }
766
767 void dump() const {
768 std::cerr << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +0000769 }
770 };
771}
772
Chris Lattner3462ae32001-12-03 22:26:30 +0000773//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000774//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000775static ValueMap< int64_t, Type, ConstantSInt> SIntConstants;
776static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000777
Chris Lattner3462ae32001-12-03 22:26:30 +0000778ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000779 return SIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000780}
781
Chris Lattner3462ae32001-12-03 22:26:30 +0000782ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000783 return UIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000784}
785
Chris Lattner3462ae32001-12-03 22:26:30 +0000786ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000787 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000788 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
789 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000790}
791
Chris Lattner3462ae32001-12-03 22:26:30 +0000792//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000793//
Chris Lattnerac80ea42004-02-01 22:49:04 +0000794namespace llvm {
795 template<>
796 struct ConstantCreator<ConstantFP, Type, uint64_t> {
797 static ConstantFP *create(const Type *Ty, uint64_t V) {
798 assert(Ty == Type::DoubleTy);
799 union {
800 double F;
801 uint64_t I;
802 } T;
803 T.I = V;
804 return new ConstantFP(Ty, T.F);
805 }
806 };
807 template<>
808 struct ConstantCreator<ConstantFP, Type, uint32_t> {
809 static ConstantFP *create(const Type *Ty, uint32_t V) {
810 assert(Ty == Type::FloatTy);
811 union {
812 float F;
813 uint32_t I;
814 } T;
815 T.I = V;
816 return new ConstantFP(Ty, T.F);
817 }
818 };
819}
820
821static ValueMap<uint64_t, Type, ConstantFP> DoubleConstants;
822static ValueMap<uint32_t, Type, ConstantFP> FloatConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000823
Chris Lattner3462ae32001-12-03 22:26:30 +0000824ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner241ed4c2004-01-23 00:55:21 +0000825 if (Ty == Type::FloatTy) {
826 // Force the value through memory to normalize it.
Chris Lattnerac80ea42004-02-01 22:49:04 +0000827 union {
828 float F;
829 uint32_t I;
830 } T;
831 T.F = (float)V;
832 return FloatConstants.getOrCreate(Ty, T.I);
833 } else {
834 assert(Ty == Type::DoubleTy);
835 union {
836 double F;
837 uint64_t I;
838 } T;
839 T.F = V;
840 return DoubleConstants.getOrCreate(Ty, T.I);
Chris Lattner241ed4c2004-01-23 00:55:21 +0000841 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000842}
843
Chris Lattner9fba3da2004-02-15 05:53:04 +0000844//---- ConstantAggregateZero::get() implementation...
845//
846namespace llvm {
847 // ConstantAggregateZero does not take extra "value" argument...
848 template<class ValType>
849 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
850 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
851 return new ConstantAggregateZero(Ty);
852 }
853 };
854
855 template<>
856 struct ConvertConstantType<ConstantAggregateZero, Type> {
857 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
858 // Make everyone now use a constant of the new type...
859 Constant *New = ConstantAggregateZero::get(NewTy);
860 assert(New != OldC && "Didn't replace constant??");
861 OldC->uncheckedReplaceAllUsesWith(New);
862 OldC->destroyConstant(); // This constant is now dead, destroy it.
863 }
864 };
865}
866
867static ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
868
Chris Lattner3e650af2004-08-04 04:48:01 +0000869static char getValType(ConstantAggregateZero *CPZ) { return 0; }
870
Chris Lattner9fba3da2004-02-15 05:53:04 +0000871Constant *ConstantAggregateZero::get(const Type *Ty) {
872 return AggZeroConstants.getOrCreate(Ty, 0);
873}
874
875// destroyConstant - Remove the constant from the constant table...
876//
877void ConstantAggregateZero::destroyConstant() {
878 AggZeroConstants.remove(this);
879 destroyConstantImpl();
880}
881
882void ConstantAggregateZero::replaceUsesOfWithOnConstant(Value *From, Value *To,
883 bool DisableChecking) {
884 assert(0 && "No uses!");
885 abort();
886}
887
888
889
Chris Lattner3462ae32001-12-03 22:26:30 +0000890//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000891//
Chris Lattner189d19f2003-11-21 20:23:48 +0000892namespace llvm {
893 template<>
894 struct ConvertConstantType<ConstantArray, ArrayType> {
895 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
896 // Make everyone now use a constant of the new type...
897 std::vector<Constant*> C;
898 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
899 C.push_back(cast<Constant>(OldC->getOperand(i)));
900 Constant *New = ConstantArray::get(NewTy, C);
901 assert(New != OldC && "Didn't replace constant??");
902 OldC->uncheckedReplaceAllUsesWith(New);
903 OldC->destroyConstant(); // This constant is now dead, destroy it.
904 }
905 };
906}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000907
Chris Lattner3e650af2004-08-04 04:48:01 +0000908static std::vector<Constant*> getValType(ConstantArray *CA) {
909 std::vector<Constant*> Elements;
910 Elements.reserve(CA->getNumOperands());
911 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
912 Elements.push_back(cast<Constant>(CA->getOperand(i)));
913 return Elements;
914}
915
Chris Lattner98fa07b2003-05-23 20:03:32 +0000916static ValueMap<std::vector<Constant*>, ArrayType,
917 ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000918
Chris Lattner015e8212004-02-15 04:14:47 +0000919Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +0000920 const std::vector<Constant*> &V) {
921 // If this is an all-zero array, return a ConstantAggregateZero object
922 if (!V.empty()) {
923 Constant *C = V[0];
924 if (!C->isNullValue())
925 return ArrayConstants.getOrCreate(Ty, V);
926 for (unsigned i = 1, e = V.size(); i != e; ++i)
927 if (V[i] != C)
928 return ArrayConstants.getOrCreate(Ty, V);
929 }
930 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000931}
932
Chris Lattner98fa07b2003-05-23 20:03:32 +0000933// destroyConstant - Remove the constant from the constant table...
934//
935void ConstantArray::destroyConstant() {
936 ArrayConstants.remove(this);
937 destroyConstantImpl();
938}
939
Chris Lattner3462ae32001-12-03 22:26:30 +0000940// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000941// contain the specified string. A null terminator is added to the specified
942// string so that it may be used in a natural way...
943//
Chris Lattner015e8212004-02-15 04:14:47 +0000944Constant *ConstantArray::get(const std::string &Str) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000945 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000946
947 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000948 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000949
950 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000951 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000952
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000953 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000954 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000955}
956
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000957/// isString - This method returns true if the array is an array of sbyte or
958/// ubyte, and if the elements of the array are all ConstantInt's.
959bool ConstantArray::isString() const {
960 // Check the element type for sbyte or ubyte...
Chris Lattnere8701f62004-01-14 17:51:53 +0000961 if (getType()->getElementType() != Type::UByteTy &&
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000962 getType()->getElementType() != Type::SByteTy)
963 return false;
964 // Check the elements to make sure they are all integers, not constant
965 // expressions.
966 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
967 if (!isa<ConstantInt>(getOperand(i)))
968 return false;
969 return true;
970}
971
Chris Lattner81fabb02002-08-26 17:53:56 +0000972// getAsString - If the sub-element type of this array is either sbyte or ubyte,
973// then this method converts the array to an std::string and returns it.
974// Otherwise, it asserts out.
975//
976std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000977 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +0000978 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +0000979 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
980 Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue();
Chris Lattner81fabb02002-08-26 17:53:56 +0000981 return Result;
982}
983
984
Chris Lattner3462ae32001-12-03 22:26:30 +0000985//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000986//
Chris Lattnerb50d1352003-10-05 00:17:43 +0000987
Chris Lattner189d19f2003-11-21 20:23:48 +0000988namespace llvm {
989 template<>
990 struct ConvertConstantType<ConstantStruct, StructType> {
991 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
992 // Make everyone now use a constant of the new type...
993 std::vector<Constant*> C;
994 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
995 C.push_back(cast<Constant>(OldC->getOperand(i)));
996 Constant *New = ConstantStruct::get(NewTy, C);
997 assert(New != OldC && "Didn't replace constant??");
998
999 OldC->uncheckedReplaceAllUsesWith(New);
1000 OldC->destroyConstant(); // This constant is now dead, destroy it.
1001 }
1002 };
1003}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001004
Chris Lattner98fa07b2003-05-23 20:03:32 +00001005static ValueMap<std::vector<Constant*>, StructType,
1006 ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001007
Chris Lattner3e650af2004-08-04 04:48:01 +00001008static std::vector<Constant*> getValType(ConstantStruct *CS) {
1009 std::vector<Constant*> Elements;
1010 Elements.reserve(CS->getNumOperands());
1011 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1012 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1013 return Elements;
1014}
1015
Chris Lattner015e8212004-02-15 04:14:47 +00001016Constant *ConstantStruct::get(const StructType *Ty,
1017 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001018 // Create a ConstantAggregateZero value if all elements are zeros...
1019 for (unsigned i = 0, e = V.size(); i != e; ++i)
1020 if (!V[i]->isNullValue())
1021 return StructConstants.getOrCreate(Ty, V);
1022
1023 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001024}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001025
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001026Constant *ConstantStruct::get(const std::vector<Constant*> &V) {
1027 std::vector<const Type*> StructEls;
1028 StructEls.reserve(V.size());
1029 for (unsigned i = 0, e = V.size(); i != e; ++i)
1030 StructEls.push_back(V[i]->getType());
1031 return get(StructType::get(StructEls), V);
1032}
1033
Chris Lattnerd7a73302001-10-13 06:57:33 +00001034// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001035//
Chris Lattner3462ae32001-12-03 22:26:30 +00001036void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +00001037 StructConstants.remove(this);
1038 destroyConstantImpl();
1039}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001040
Brian Gaeke02209042004-08-20 06:00:58 +00001041//---- ConstantPacked::get() implementation...
1042//
1043namespace llvm {
1044 template<>
1045 struct ConvertConstantType<ConstantPacked, PackedType> {
1046 static void convert(ConstantPacked *OldC, const PackedType *NewTy) {
1047 // Make everyone now use a constant of the new type...
1048 std::vector<Constant*> C;
1049 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1050 C.push_back(cast<Constant>(OldC->getOperand(i)));
1051 Constant *New = ConstantPacked::get(NewTy, C);
1052 assert(New != OldC && "Didn't replace constant??");
1053 OldC->uncheckedReplaceAllUsesWith(New);
1054 OldC->destroyConstant(); // This constant is now dead, destroy it.
1055 }
1056 };
1057}
1058
1059static std::vector<Constant*> getValType(ConstantPacked *CP) {
1060 std::vector<Constant*> Elements;
1061 Elements.reserve(CP->getNumOperands());
1062 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1063 Elements.push_back(CP->getOperand(i));
1064 return Elements;
1065}
1066
1067static ValueMap<std::vector<Constant*>, PackedType,
1068 ConstantPacked> PackedConstants;
1069
1070Constant *ConstantPacked::get(const PackedType *Ty,
1071 const std::vector<Constant*> &V) {
1072 // If this is an all-zero packed, return a ConstantAggregateZero object
1073 if (!V.empty()) {
1074 Constant *C = V[0];
1075 if (!C->isNullValue())
1076 return PackedConstants.getOrCreate(Ty, V);
1077 for (unsigned i = 1, e = V.size(); i != e; ++i)
1078 if (V[i] != C)
1079 return PackedConstants.getOrCreate(Ty, V);
1080 }
1081 return ConstantAggregateZero::get(Ty);
1082}
1083
1084Constant *ConstantPacked::get(const std::vector<Constant*> &V) {
1085 assert(!V.empty() && "Cannot infer type if V is empty");
1086 return get(PackedType::get(V.front()->getType(),V.size()), V);
1087}
1088
1089// destroyConstant - Remove the constant from the constant table...
1090//
1091void ConstantPacked::destroyConstant() {
1092 PackedConstants.remove(this);
1093 destroyConstantImpl();
1094}
1095
Chris Lattner3462ae32001-12-03 22:26:30 +00001096//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001097//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001098
Chris Lattner189d19f2003-11-21 20:23:48 +00001099namespace llvm {
1100 // ConstantPointerNull does not take extra "value" argument...
1101 template<class ValType>
1102 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1103 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1104 return new ConstantPointerNull(Ty);
1105 }
1106 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001107
Chris Lattner189d19f2003-11-21 20:23:48 +00001108 template<>
1109 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1110 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1111 // Make everyone now use a constant of the new type...
1112 Constant *New = ConstantPointerNull::get(NewTy);
1113 assert(New != OldC && "Didn't replace constant??");
1114 OldC->uncheckedReplaceAllUsesWith(New);
1115 OldC->destroyConstant(); // This constant is now dead, destroy it.
1116 }
1117 };
1118}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001119
Chris Lattner98fa07b2003-05-23 20:03:32 +00001120static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001121
Chris Lattner3e650af2004-08-04 04:48:01 +00001122static char getValType(ConstantPointerNull *) {
1123 return 0;
1124}
1125
1126
Chris Lattner3462ae32001-12-03 22:26:30 +00001127ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner98fa07b2003-05-23 20:03:32 +00001128 return NullPtrConstants.getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001129}
1130
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001131// destroyConstant - Remove the constant from the constant table...
1132//
1133void ConstantPointerNull::destroyConstant() {
1134 NullPtrConstants.remove(this);
1135 destroyConstantImpl();
1136}
1137
1138
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001139//---- UndefValue::get() implementation...
1140//
1141
1142namespace llvm {
1143 // UndefValue does not take extra "value" argument...
1144 template<class ValType>
1145 struct ConstantCreator<UndefValue, Type, ValType> {
1146 static UndefValue *create(const Type *Ty, const ValType &V) {
1147 return new UndefValue(Ty);
1148 }
1149 };
1150
1151 template<>
1152 struct ConvertConstantType<UndefValue, Type> {
1153 static void convert(UndefValue *OldC, const Type *NewTy) {
1154 // Make everyone now use a constant of the new type.
1155 Constant *New = UndefValue::get(NewTy);
1156 assert(New != OldC && "Didn't replace constant??");
1157 OldC->uncheckedReplaceAllUsesWith(New);
1158 OldC->destroyConstant(); // This constant is now dead, destroy it.
1159 }
1160 };
1161}
1162
1163static ValueMap<char, Type, UndefValue> UndefValueConstants;
1164
1165static char getValType(UndefValue *) {
1166 return 0;
1167}
1168
1169
1170UndefValue *UndefValue::get(const Type *Ty) {
1171 return UndefValueConstants.getOrCreate(Ty, 0);
1172}
1173
1174// destroyConstant - Remove the constant from the constant table.
1175//
1176void UndefValue::destroyConstant() {
1177 UndefValueConstants.remove(this);
1178 destroyConstantImpl();
1179}
1180
1181
1182
1183
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001184//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001185//
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001186typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Chris Lattner98fa07b2003-05-23 20:03:32 +00001187
Chris Lattner189d19f2003-11-21 20:23:48 +00001188namespace llvm {
1189 template<>
1190 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
1191 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
1192 if (V.first == Instruction::Cast)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001193 return new UnaryConstantExpr(Instruction::Cast, V.second[0], Ty);
Chris Lattner189d19f2003-11-21 20:23:48 +00001194 if ((V.first >= Instruction::BinaryOpsBegin &&
1195 V.first < Instruction::BinaryOpsEnd) ||
1196 V.first == Instruction::Shl || V.first == Instruction::Shr)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001197 return new BinaryConstantExpr(V.first, V.second[0], V.second[1]);
Chris Lattnerb7897ce2004-03-30 22:51:03 +00001198 if (V.first == Instruction::Select)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001199 return new SelectConstantExpr(V.second[0], V.second[1], V.second[2]);
Chris Lattner189d19f2003-11-21 20:23:48 +00001200
1201 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
1202
1203 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001204 return new GetElementPtrConstantExpr(V.second[0], IdxList, Ty);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001205 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001206 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001207
Chris Lattner189d19f2003-11-21 20:23:48 +00001208 template<>
1209 struct ConvertConstantType<ConstantExpr, Type> {
1210 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1211 Constant *New;
1212 switch (OldC->getOpcode()) {
1213 case Instruction::Cast:
1214 New = ConstantExpr::getCast(OldC->getOperand(0), NewTy);
1215 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001216 case Instruction::Select:
1217 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1218 OldC->getOperand(1),
1219 OldC->getOperand(2));
1220 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001221 case Instruction::Shl:
1222 case Instruction::Shr:
1223 New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
1224 OldC->getOperand(0), OldC->getOperand(1));
1225 break;
1226 default:
1227 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
1228 OldC->getOpcode() < Instruction::BinaryOpsEnd);
1229 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1230 OldC->getOperand(1));
1231 break;
1232 case Instruction::GetElementPtr:
1233 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001234 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
1235 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), Idx);
Chris Lattner189d19f2003-11-21 20:23:48 +00001236 break;
1237 }
1238
1239 assert(New != OldC && "Didn't replace constant??");
1240 OldC->uncheckedReplaceAllUsesWith(New);
1241 OldC->destroyConstant(); // This constant is now dead, destroy it.
1242 }
1243 };
1244} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001245
1246
Chris Lattner3e650af2004-08-04 04:48:01 +00001247static ExprMapKeyType getValType(ConstantExpr *CE) {
1248 std::vector<Constant*> Operands;
1249 Operands.reserve(CE->getNumOperands());
1250 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1251 Operands.push_back(cast<Constant>(CE->getOperand(i)));
1252 return ExprMapKeyType(CE->getOpcode(), Operands);
1253}
1254
Chris Lattner98fa07b2003-05-23 20:03:32 +00001255static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001256
Chris Lattner46b3d302003-04-16 22:40:51 +00001257Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001258 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1259
Chris Lattneracdbe712003-04-17 19:24:48 +00001260 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
1261 return FC; // Fold a few common cases...
1262
Vikram S. Adve4c485332002-07-15 18:19:33 +00001263 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001264 std::vector<Constant*> argVec(1, C);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001265 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
1266 return ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001267}
Chris Lattnerd7a73302001-10-13 06:57:33 +00001268
Chris Lattnerdd284742004-04-04 23:20:30 +00001269Constant *ConstantExpr::getSignExtend(Constant *C, const Type *Ty) {
Chris Lattner1ece6f82005-01-01 15:59:57 +00001270 assert(C->getType()->isIntegral() && Ty->isIntegral() &&
Chris Lattnerdd284742004-04-04 23:20:30 +00001271 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1272 "This is an illegal sign extension!");
Chris Lattner1ece6f82005-01-01 15:59:57 +00001273 if (C->getType() != Type::BoolTy) {
1274 C = ConstantExpr::getCast(C, C->getType()->getSignedVersion());
1275 return ConstantExpr::getCast(C, Ty);
1276 } else {
1277 if (C == ConstantBool::True)
1278 return ConstantIntegral::getAllOnesValue(Ty);
1279 else
1280 return ConstantIntegral::getNullValue(Ty);
1281 }
Chris Lattnerdd284742004-04-04 23:20:30 +00001282}
1283
1284Constant *ConstantExpr::getZeroExtend(Constant *C, const Type *Ty) {
Chris Lattner1ece6f82005-01-01 15:59:57 +00001285 assert(C->getType()->isIntegral() && Ty->isIntegral() &&
Chris Lattnerdd284742004-04-04 23:20:30 +00001286 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1287 "This is an illegal zero extension!");
Chris Lattner1ece6f82005-01-01 15:59:57 +00001288 if (C->getType() != Type::BoolTy)
1289 C = ConstantExpr::getCast(C, C->getType()->getUnsignedVersion());
Chris Lattnerdd284742004-04-04 23:20:30 +00001290 return ConstantExpr::getCast(C, Ty);
1291}
1292
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001293Constant *ConstantExpr::getSizeOf(const Type *Ty) {
Chris Lattneracc4e542004-12-13 19:48:51 +00001294 // sizeof is implemented as: (ulong) gep (Ty*)null, 1
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001295 return getCast(
Chris Lattneracc4e542004-12-13 19:48:51 +00001296 getGetElementPtr(getNullValue(PointerType::get(Ty)),
1297 std::vector<Constant*>(1, ConstantInt::get(Type::UIntTy, 1))),
1298 Type::ULongTy);
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001299}
1300
Chris Lattnerb50d1352003-10-05 00:17:43 +00001301Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
1302 Constant *C1, Constant *C2) {
Chris Lattner5645e8a2004-01-12 19:04:55 +00001303 if (Opcode == Instruction::Shl || Opcode == Instruction::Shr)
1304 return getShiftTy(ReqTy, Opcode, C1, C2);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001305 // Check the operands for consistency first
1306 assert((Opcode >= Instruction::BinaryOpsBegin &&
1307 Opcode < Instruction::BinaryOpsEnd) &&
1308 "Invalid opcode in binary constant expression");
1309 assert(C1->getType() == C2->getType() &&
1310 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001311
Chris Lattner29ca2c62004-08-04 18:50:09 +00001312 if (ReqTy == C1->getType() || (Instruction::isRelational(Opcode) &&
1313 ReqTy == Type::BoolTy))
Chris Lattnerb50d1352003-10-05 00:17:43 +00001314 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1315 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001316
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001317 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001318 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001319 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001320}
1321
Chris Lattner29ca2c62004-08-04 18:50:09 +00001322Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001323#ifndef NDEBUG
1324 switch (Opcode) {
1325 case Instruction::Add: case Instruction::Sub:
1326 case Instruction::Mul: case Instruction::Div:
1327 case Instruction::Rem:
1328 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1329 assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint()) &&
1330 "Tried to create an arithmetic operation on a non-arithmetic type!");
1331 break;
1332 case Instruction::And:
1333 case Instruction::Or:
1334 case Instruction::Xor:
1335 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1336 assert(C1->getType()->isIntegral() &&
Misha Brukman3852f652005-01-27 06:46:38 +00001337 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001338 break;
1339 case Instruction::SetLT: case Instruction::SetGT: case Instruction::SetLE:
1340 case Instruction::SetGE: case Instruction::SetEQ: case Instruction::SetNE:
1341 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1342 break;
1343 case Instruction::Shl:
1344 case Instruction::Shr:
1345 assert(C2->getType() == Type::UByteTy && "Shift should be by ubyte!");
1346 assert(C1->getType()->isInteger() &&
1347 "Tried to create a shift operation on a non-integer type!");
1348 break;
1349 default:
1350 break;
1351 }
1352#endif
1353
Chris Lattner29ca2c62004-08-04 18:50:09 +00001354 if (Instruction::isRelational(Opcode))
1355 return getTy(Type::BoolTy, Opcode, C1, C2);
1356 else
1357 return getTy(C1->getType(), Opcode, C1, C2);
1358}
1359
Chris Lattner6e415c02004-03-12 05:54:04 +00001360Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
1361 Constant *V1, Constant *V2) {
1362 assert(C->getType() == Type::BoolTy && "Select condition must be bool!");
1363 assert(V1->getType() == V2->getType() && "Select value types must match!");
1364 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
1365
1366 if (ReqTy == V1->getType())
1367 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1368 return SC; // Fold common cases
1369
1370 std::vector<Constant*> argVec(3, C);
1371 argVec[1] = V1;
1372 argVec[2] = V2;
1373 ExprMapKeyType Key = std::make_pair(Instruction::Select, argVec);
1374 return ExprConstants.getOrCreate(ReqTy, Key);
1375}
1376
Chris Lattner9eb2b522004-01-12 19:12:58 +00001377/// getShiftTy - Return a shift left or shift right constant expr
Chris Lattnerb50d1352003-10-05 00:17:43 +00001378Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
1379 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001380 // Check the operands for consistency first
1381 assert((Opcode == Instruction::Shl ||
1382 Opcode == Instruction::Shr) &&
1383 "Invalid opcode in binary constant expression");
1384 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
1385 "Invalid operand types for Shift constant expr!");
1386
Chris Lattner0bba7712004-01-12 20:40:42 +00001387 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001388 return FC; // Fold a few common cases...
1389
1390 // Look up the constant in the table first to ensure uniqueness
1391 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001392 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001393 return ExprConstants.getOrCreate(ReqTy, Key);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001394}
1395
1396
Chris Lattnerb50d1352003-10-05 00:17:43 +00001397Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner13128ab2004-10-11 22:52:25 +00001398 const std::vector<Value*> &IdxList) {
1399 assert(GetElementPtrInst::getIndexedType(C->getType(), IdxList, true) &&
Chris Lattner04b60fe2004-02-16 20:46:13 +00001400 "GEP indices invalid!");
1401
Chris Lattneracdbe712003-04-17 19:24:48 +00001402 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
1403 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001404
Chris Lattnerb50d1352003-10-05 00:17:43 +00001405 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001406 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001407 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00001408 std::vector<Constant*> ArgVec;
1409 ArgVec.reserve(IdxList.size()+1);
1410 ArgVec.push_back(C);
1411 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
1412 ArgVec.push_back(cast<Constant>(IdxList[i]));
1413 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,ArgVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001414 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001415}
1416
Chris Lattnerb50d1352003-10-05 00:17:43 +00001417Constant *ConstantExpr::getGetElementPtr(Constant *C,
1418 const std::vector<Constant*> &IdxList){
1419 // Get the result type of the getelementptr!
1420 std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
1421
1422 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
1423 true);
1424 assert(Ty && "GEP indices invalid!");
Chris Lattner13128ab2004-10-11 22:52:25 +00001425 return getGetElementPtrTy(PointerType::get(Ty), C, VIdxList);
1426}
1427
1428Constant *ConstantExpr::getGetElementPtr(Constant *C,
1429 const std::vector<Value*> &IdxList) {
1430 // Get the result type of the getelementptr!
1431 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
1432 true);
1433 assert(Ty && "GEP indices invalid!");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001434 return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
1435}
1436
1437
Vikram S. Adve4c485332002-07-15 18:19:33 +00001438// destroyConstant - Remove the constant from the constant table...
1439//
1440void ConstantExpr::destroyConstant() {
1441 ExprConstants.remove(this);
1442 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001443}
1444
Chris Lattner3cd8c562002-07-30 18:54:25 +00001445const char *ConstantExpr::getOpcodeName() const {
1446 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001447}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00001448
Chris Lattner99a669b2004-11-19 16:39:44 +00001449/// clearAllValueMaps - This method frees all internal memory used by the
1450/// constant subsystem, which can be used in environments where this memory
1451/// is otherwise reported as a leak.
1452void Constant::clearAllValueMaps() {
1453 std::vector<Constant *> Constants;
1454
1455 DoubleConstants.clear(Constants);
1456 FloatConstants.clear(Constants);
1457 SIntConstants.clear(Constants);
1458 UIntConstants.clear(Constants);
1459 AggZeroConstants.clear(Constants);
1460 ArrayConstants.clear(Constants);
1461 StructConstants.clear(Constants);
1462 PackedConstants.clear(Constants);
1463 NullPtrConstants.clear(Constants);
1464 UndefValueConstants.clear(Constants);
1465 ExprConstants.clear(Constants);
1466
1467 for (std::vector<Constant *>::iterator I = Constants.begin(),
1468 E = Constants.end(); I != E; ++I)
1469 (*I)->dropAllReferences();
1470 for (std::vector<Constant *>::iterator I = Constants.begin(),
1471 E = Constants.end(); I != E; ++I)
1472 (*I)->destroyConstantImpl();
1473 Constants.clear();
1474}