blob: 1a2b7748567104897df9a25e4ccb1a4881ee4162 [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 Lattner0d779712002-10-08 23:33:52 +0000251 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000252 assert((V[i]->getType() == T->getElementType() ||
253 (T->isAbstract() &&
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000254 V[i]->getType()->getTypeID()==T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000255 "Initializer for array element doesn't match array element type!");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000256 OL[i].init(V[i], this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000257 }
258}
259
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000260ConstantArray::~ConstantArray() {
261 delete [] OperandList;
262}
263
Chris Lattner3462ae32001-12-03 22:26:30 +0000264ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000265 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000266 : Constant(T, ConstantStructVal, new Use[V.size()], V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000267 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000268 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000269 Use *OL = OperandList;
Chris Lattner0d779712002-10-08 23:33:52 +0000270 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000271 assert((V[i]->getType() == T->getElementType(i) ||
272 ((T->getElementType(i)->isAbstract() ||
273 V[i]->getType()->isAbstract()) &&
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000274 T->getElementType(i)->getTypeID()==V[i]->getType()->getTypeID()))&&
Chris Lattner93c8f142003-06-02 17:42:47 +0000275 "Initializer for struct element doesn't match struct element type!");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000276 OL[i].init(V[i], this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000277 }
278}
279
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000280ConstantStruct::~ConstantStruct() {
281 delete [] OperandList;
282}
283
284
Brian Gaeke02209042004-08-20 06:00:58 +0000285ConstantPacked::ConstantPacked(const PackedType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000286 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000287 : Constant(T, ConstantPackedVal, new Use[V.size()], V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000288 Use *OL = OperandList;
Brian Gaeke02209042004-08-20 06:00:58 +0000289 for (unsigned i = 0, e = V.size(); i != e; ++i) {
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000290 assert((V[i]->getType() == T->getElementType() ||
291 (T->isAbstract() &&
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000292 V[i]->getType()->getTypeID()==T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000293 "Initializer for packed element doesn't match packed element type!");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000294 OL[i].init(V[i], this);
Brian Gaeke02209042004-08-20 06:00:58 +0000295 }
296}
297
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000298ConstantPacked::~ConstantPacked() {
299 delete [] OperandList;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000300}
301
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000302/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
303/// behind the scenes to implement unary constant exprs.
304class UnaryConstantExpr : public ConstantExpr {
305 Use Op;
306public:
307 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
308 : ConstantExpr(Ty, Opcode, &Op, 1), Op(C, this) {}
309};
Chris Lattner6e415c02004-03-12 05:54:04 +0000310
Chris Lattner22ced562003-06-22 20:48:30 +0000311static bool isSetCC(unsigned Opcode) {
312 return Opcode == Instruction::SetEQ || Opcode == Instruction::SetNE ||
313 Opcode == Instruction::SetLT || Opcode == Instruction::SetGT ||
314 Opcode == Instruction::SetLE || Opcode == Instruction::SetGE;
315}
316
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000317/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
318/// behind the scenes to implement binary constant exprs.
319class BinaryConstantExpr : public ConstantExpr {
320 Use Ops[2];
321public:
322 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
323 : ConstantExpr(isSetCC(Opcode) ? Type::BoolTy : C1->getType(),
324 Opcode, Ops, 2) {
325 Ops[0].init(C1, this);
326 Ops[1].init(C2, this);
327 }
328};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000329
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000330/// SelectConstantExpr - This class is private to Constants.cpp, and is used
331/// behind the scenes to implement select constant exprs.
332class SelectConstantExpr : public ConstantExpr {
333 Use Ops[3];
334public:
335 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
336 : ConstantExpr(C2->getType(), Instruction::Select, Ops, 3) {
337 Ops[0].init(C1, this);
338 Ops[1].init(C2, this);
339 Ops[2].init(C3, this);
340 }
341};
342
343/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
344/// used behind the scenes to implement getelementpr constant exprs.
345struct GetElementPtrConstantExpr : public ConstantExpr {
346 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
347 const Type *DestTy)
348 : ConstantExpr(DestTy, Instruction::GetElementPtr,
349 new Use[IdxList.size()+1], IdxList.size()+1) {
350 OperandList[0].init(C, this);
351 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
352 OperandList[i+1].init(IdxList[i], this);
353 }
354 ~GetElementPtrConstantExpr() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000355 delete [] OperandList;
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000356 }
357};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000358
Chris Lattner817175f2004-03-29 02:37:53 +0000359/// ConstantExpr::get* - Return some common constants without having to
360/// specify the full Instruction::OPCODE identifier.
361///
362Constant *ConstantExpr::getNeg(Constant *C) {
Chris Lattner3cdc27c2004-03-29 19:51:24 +0000363 if (!C->getType()->isFloatingPoint())
364 return get(Instruction::Sub, getNullValue(C->getType()), C);
365 else
366 return get(Instruction::Sub, ConstantFP::get(C->getType(), -0.0), C);
Chris Lattner817175f2004-03-29 02:37:53 +0000367}
368Constant *ConstantExpr::getNot(Constant *C) {
369 assert(isa<ConstantIntegral>(C) && "Cannot NOT a nonintegral type!");
370 return get(Instruction::Xor, C,
371 ConstantIntegral::getAllOnesValue(C->getType()));
372}
373Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
374 return get(Instruction::Add, C1, C2);
375}
376Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
377 return get(Instruction::Sub, C1, C2);
378}
379Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
380 return get(Instruction::Mul, C1, C2);
381}
382Constant *ConstantExpr::getDiv(Constant *C1, Constant *C2) {
383 return get(Instruction::Div, C1, C2);
384}
385Constant *ConstantExpr::getRem(Constant *C1, Constant *C2) {
386 return get(Instruction::Rem, C1, C2);
387}
388Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
389 return get(Instruction::And, C1, C2);
390}
391Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
392 return get(Instruction::Or, C1, C2);
393}
394Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
395 return get(Instruction::Xor, C1, C2);
396}
397Constant *ConstantExpr::getSetEQ(Constant *C1, Constant *C2) {
398 return get(Instruction::SetEQ, C1, C2);
399}
400Constant *ConstantExpr::getSetNE(Constant *C1, Constant *C2) {
401 return get(Instruction::SetNE, C1, C2);
402}
403Constant *ConstantExpr::getSetLT(Constant *C1, Constant *C2) {
404 return get(Instruction::SetLT, C1, C2);
405}
406Constant *ConstantExpr::getSetGT(Constant *C1, Constant *C2) {
407 return get(Instruction::SetGT, C1, C2);
408}
409Constant *ConstantExpr::getSetLE(Constant *C1, Constant *C2) {
410 return get(Instruction::SetLE, C1, C2);
411}
412Constant *ConstantExpr::getSetGE(Constant *C1, Constant *C2) {
413 return get(Instruction::SetGE, C1, C2);
414}
415Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
416 return get(Instruction::Shl, C1, C2);
417}
418Constant *ConstantExpr::getShr(Constant *C1, Constant *C2) {
419 return get(Instruction::Shr, C1, C2);
420}
421
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000422Constant *ConstantExpr::getUShr(Constant *C1, Constant *C2) {
423 if (C1->getType()->isUnsigned()) return getShr(C1, C2);
424 return getCast(getShr(getCast(C1,
425 C1->getType()->getUnsignedVersion()), C2), C1->getType());
426}
Chris Lattner817175f2004-03-29 02:37:53 +0000427
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000428Constant *ConstantExpr::getSShr(Constant *C1, Constant *C2) {
429 if (C1->getType()->isSigned()) return getShr(C1, C2);
430 return getCast(getShr(getCast(C1,
431 C1->getType()->getSignedVersion()), C2), C1->getType());
432}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000433
Chris Lattner2f7c9632001-06-06 20:29:01 +0000434
435//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000436// isValueValidForType implementations
437
Chris Lattner3462ae32001-12-03 22:26:30 +0000438bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000439 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000440 default:
441 return false; // These can't be represented as integers!!!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000442 // Signed types...
443 case Type::SByteTyID:
444 return (Val <= INT8_MAX && Val >= INT8_MIN);
445 case Type::ShortTyID:
446 return (Val <= INT16_MAX && Val >= INT16_MIN);
447 case Type::IntTyID:
Chris Lattner74248512004-06-08 23:21:39 +0000448 return (Val <= int(INT32_MAX) && Val >= int(INT32_MIN));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000449 case Type::LongTyID:
450 return true; // This is the largest type...
451 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000452}
453
Chris Lattner3462ae32001-12-03 22:26:30 +0000454bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000455 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000456 default:
457 return false; // These can't be represented as integers!!!
458
459 // Unsigned types...
460 case Type::UByteTyID:
461 return (Val <= UINT8_MAX);
462 case Type::UShortTyID:
463 return (Val <= UINT16_MAX);
464 case Type::UIntTyID:
465 return (Val <= UINT32_MAX);
466 case Type::ULongTyID:
467 return true; // This is the largest type...
468 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000469}
470
Chris Lattner3462ae32001-12-03 22:26:30 +0000471bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000472 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000473 default:
474 return false; // These can't be represented as floating point!
475
Reid Spencerb95f8ab2004-12-07 07:38:08 +0000476 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000477 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000478 case Type::DoubleTyID:
479 return true; // This is the largest type...
480 }
481};
Chris Lattner9655e542001-07-20 19:16:02 +0000482
Chris Lattner49d855c2001-09-07 16:46:31 +0000483//===----------------------------------------------------------------------===//
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000484// replaceUsesOfWithOnConstant implementations
485
Chris Lattnerc27038d2003-08-29 05:36:46 +0000486void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
487 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000488 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
489
490 std::vector<Constant*> Values;
Alkis Evlogimenosf0cc8142004-08-04 08:02:59 +0000491 Values.reserve(getNumOperands()); // Build replacement array...
492 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
493 Constant *Val = getOperand(i);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000494 if (Val == From) Val = cast<Constant>(To);
495 Values.push_back(Val);
496 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000497
Chris Lattnerc75bf522004-02-15 04:05:58 +0000498 Constant *Replacement = ConstantArray::get(getType(), Values);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000499 assert(Replacement != this && "I didn't contain From!");
500
501 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000502 if (DisableChecking)
503 uncheckedReplaceAllUsesWith(Replacement);
504 else
505 replaceAllUsesWith(Replacement);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000506
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000507 // Delete the old constant!
Misha Brukmanb1c93172005-04-21 23:48:37 +0000508 destroyConstant();
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000509}
510
Chris Lattnerc27038d2003-08-29 05:36:46 +0000511void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
512 bool DisableChecking) {
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000513 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
514
515 std::vector<Constant*> Values;
Alkis Evlogimenosf0cc8142004-08-04 08:02:59 +0000516 Values.reserve(getNumOperands()); // Build replacement array...
517 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
518 Constant *Val = getOperand(i);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000519 if (Val == From) Val = cast<Constant>(To);
520 Values.push_back(Val);
521 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000522
Chris Lattner37a716f2004-02-15 04:07:32 +0000523 Constant *Replacement = ConstantStruct::get(getType(), Values);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000524 assert(Replacement != this && "I didn't contain From!");
525
526 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000527 if (DisableChecking)
528 uncheckedReplaceAllUsesWith(Replacement);
529 else
530 replaceAllUsesWith(Replacement);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000531
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000532 // Delete the old constant!
533 destroyConstant();
534}
535
Brian Gaeke02209042004-08-20 06:00:58 +0000536void ConstantPacked::replaceUsesOfWithOnConstant(Value *From, Value *To,
537 bool DisableChecking) {
538 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
539
540 std::vector<Constant*> Values;
541 Values.reserve(getNumOperands()); // Build replacement array...
542 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
543 Constant *Val = getOperand(i);
544 if (Val == From) Val = cast<Constant>(To);
545 Values.push_back(Val);
546 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000547
Brian Gaeke02209042004-08-20 06:00:58 +0000548 Constant *Replacement = ConstantPacked::get(getType(), Values);
549 assert(Replacement != this && "I didn't contain From!");
550
551 // Everyone using this now uses the replacement...
552 if (DisableChecking)
553 uncheckedReplaceAllUsesWith(Replacement);
554 else
555 replaceAllUsesWith(Replacement);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000556
Brian Gaeke02209042004-08-20 06:00:58 +0000557 // Delete the old constant!
Misha Brukmanb1c93172005-04-21 23:48:37 +0000558 destroyConstant();
Brian Gaeke02209042004-08-20 06:00:58 +0000559}
560
Chris Lattnerc27038d2003-08-29 05:36:46 +0000561void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
562 bool DisableChecking) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000563 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
564 Constant *To = cast<Constant>(ToV);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000565
Chris Lattner46b3d302003-04-16 22:40:51 +0000566 Constant *Replacement = 0;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000567 if (getOpcode() == Instruction::GetElementPtr) {
568 std::vector<Constant*> Indices;
Chris Lattner55ed6562003-05-14 17:51:05 +0000569 Constant *Pointer = getOperand(0);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000570 Indices.reserve(getNumOperands()-1);
Chris Lattner55ed6562003-05-14 17:51:05 +0000571 if (Pointer == From) Pointer = To;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000572
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000573 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000574 Constant *Val = getOperand(i);
575 if (Val == From) Val = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000576 Indices.push_back(Val);
577 }
578 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
579 } else if (getOpcode() == Instruction::Cast) {
580 assert(getOperand(0) == From && "Cast only has one use!");
Chris Lattner55ed6562003-05-14 17:51:05 +0000581 Replacement = ConstantExpr::getCast(To, getType());
Chris Lattner467cb2b2004-03-31 02:56:11 +0000582 } else if (getOpcode() == Instruction::Select) {
583 Constant *C1 = getOperand(0);
584 Constant *C2 = getOperand(1);
585 Constant *C3 = getOperand(2);
586 if (C1 == From) C1 = To;
587 if (C2 == From) C2 = To;
588 if (C3 == From) C3 = To;
589 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000590 } else if (getNumOperands() == 2) {
Chris Lattner55ed6562003-05-14 17:51:05 +0000591 Constant *C1 = getOperand(0);
592 Constant *C2 = getOperand(1);
593 if (C1 == From) C1 = To;
594 if (C2 == From) C2 = To;
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000595 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
596 } else {
597 assert(0 && "Unknown ConstantExpr type!");
598 return;
599 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000600
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000601 assert(Replacement != this && "I didn't contain From!");
602
603 // Everyone using this now uses the replacement...
Chris Lattnerc27038d2003-08-29 05:36:46 +0000604 if (DisableChecking)
605 uncheckedReplaceAllUsesWith(Replacement);
606 else
607 replaceAllUsesWith(Replacement);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000608
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000609 // Delete the old constant!
610 destroyConstant();
611}
612
Chris Lattnerb1dd9bb2002-10-09 23:12:25 +0000613//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000614// Factory Function Implementation
615
Chris Lattner98fa07b2003-05-23 20:03:32 +0000616// ConstantCreator - A class that is used to create constants by
617// ValueMap*. This class should be partially specialized if there is
618// something strange that needs to be done to interface to the ctor for the
619// constant.
620//
Chris Lattner189d19f2003-11-21 20:23:48 +0000621namespace llvm {
622 template<class ConstantClass, class TypeClass, class ValType>
623 struct ConstantCreator {
624 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
625 return new ConstantClass(Ty, V);
626 }
627 };
Misha Brukmanb1c93172005-04-21 23:48:37 +0000628
Chris Lattner189d19f2003-11-21 20:23:48 +0000629 template<class ConstantClass, class TypeClass>
630 struct ConvertConstantType {
631 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
632 assert(0 && "This type cannot be converted!\n");
633 abort();
634 }
635 };
636}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000637
Chris Lattner98fa07b2003-05-23 20:03:32 +0000638namespace {
639 template<class ValType, class TypeClass, class ConstantClass>
Chris Lattnerb50d1352003-10-05 00:17:43 +0000640 class ValueMap : public AbstractTypeUser {
641 typedef std::pair<const TypeClass*, ValType> MapKey;
642 typedef std::map<MapKey, ConstantClass *> MapTy;
643 typedef typename MapTy::iterator MapIterator;
644 MapTy Map;
645
646 typedef std::map<const TypeClass*, MapIterator> AbstractTypeMapTy;
647 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner99a669b2004-11-19 16:39:44 +0000648
649 friend void Constant::clearAllValueMaps();
650 private:
651 void clear(std::vector<Constant *> &Constants) {
652 for(MapIterator I = Map.begin(); I != Map.end(); ++I)
653 Constants.push_back(I->second);
654 Map.clear();
655 AbstractTypeMap.clear();
656 }
657
Chris Lattner98fa07b2003-05-23 20:03:32 +0000658 public:
659 // getOrCreate - Return the specified constant from the map, creating it if
660 // necessary.
661 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000662 MapKey Lookup(Ty, V);
663 MapIterator I = Map.lower_bound(Lookup);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000664 if (I != Map.end() && I->first == Lookup)
665 return I->second; // Is it in the map?
666
667 // If no preexisting value, create one now...
668 ConstantClass *Result =
669 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
670
Chris Lattnerb50d1352003-10-05 00:17:43 +0000671
672 /// FIXME: why does this assert fail when loading 176.gcc?
673 //assert(Result->getType() == Ty && "Type specified is not correct!");
674 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
675
676 // If the type of the constant is abstract, make sure that an entry exists
677 // for it in the AbstractTypeMap.
678 if (Ty->isAbstract()) {
679 typename AbstractTypeMapTy::iterator TI =
680 AbstractTypeMap.lower_bound(Ty);
681
682 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
683 // Add ourselves to the ATU list of the type.
684 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
685
686 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
687 }
688 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000689 return Result;
690 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000691
Chris Lattner98fa07b2003-05-23 20:03:32 +0000692 void remove(ConstantClass *CP) {
Chris Lattner3e650af2004-08-04 04:48:01 +0000693 MapIterator I = Map.find(MapKey((TypeClass*)CP->getRawType(),
694 getValType(CP)));
Chris Lattner20a4dab2004-08-04 22:26:13 +0000695 if (I == Map.end() || I->second != CP) {
696 // FIXME: This should not use a linear scan. If this gets to be a
697 // performance problem, someone should look at this.
698 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
699 /* empty */;
700 }
701
Chris Lattnerb50d1352003-10-05 00:17:43 +0000702 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +0000703 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +0000704
705 // Now that we found the entry, make sure this isn't the entry that
706 // the AbstractTypeMap points to.
707 const TypeClass *Ty = I->first.first;
708 if (Ty->isAbstract()) {
709 assert(AbstractTypeMap.count(Ty) &&
710 "Abstract type not in AbstractTypeMap?");
711 MapIterator &ATMEntryIt = AbstractTypeMap[Ty];
712 if (ATMEntryIt == I) {
713 // Yes, we are removing the representative entry for this type.
714 // See if there are any other entries of the same type.
715 MapIterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000716
Chris Lattnerb50d1352003-10-05 00:17:43 +0000717 // First check the entry before this one...
718 if (TmpIt != Map.begin()) {
719 --TmpIt;
720 if (TmpIt->first.first != Ty) // Not the same type, move back...
721 ++TmpIt;
722 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000723
Chris Lattnerb50d1352003-10-05 00:17:43 +0000724 // If we didn't find the same type, try to move forward...
725 if (TmpIt == ATMEntryIt) {
726 ++TmpIt;
727 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
728 --TmpIt; // No entry afterwards with the same type
729 }
730
731 // If there is another entry in the map of the same abstract type,
732 // update the AbstractTypeMap entry now.
733 if (TmpIt != ATMEntryIt) {
734 ATMEntryIt = TmpIt;
735 } else {
736 // Otherwise, we are removing the last instance of this type
737 // from the table. Remove from the ATM, and from user list.
738 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
739 AbstractTypeMap.erase(Ty);
740 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000741 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000742 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000743
Chris Lattnerb50d1352003-10-05 00:17:43 +0000744 Map.erase(I);
745 }
746
747 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000748 typename AbstractTypeMapTy::iterator I =
Chris Lattnerb50d1352003-10-05 00:17:43 +0000749 AbstractTypeMap.find(cast<TypeClass>(OldTy));
750
751 assert(I != AbstractTypeMap.end() &&
752 "Abstract type not in AbstractTypeMap?");
753
754 // Convert a constant at a time until the last one is gone. The last one
755 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
756 // eliminated eventually.
757 do {
758 ConvertConstantType<ConstantClass,
759 TypeClass>::convert(I->second->second,
760 cast<TypeClass>(NewTy));
761
762 I = AbstractTypeMap.find(cast<TypeClass>(OldTy));
763 } while (I != AbstractTypeMap.end());
764 }
765
766 // If the type became concrete without being refined to any other existing
767 // type, we just remove ourselves from the ATU list.
768 void typeBecameConcrete(const DerivedType *AbsTy) {
769 AbsTy->removeAbstractTypeUser(this);
770 }
771
772 void dump() const {
773 std::cerr << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +0000774 }
775 };
776}
777
Chris Lattner3462ae32001-12-03 22:26:30 +0000778//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000779//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000780static ValueMap< int64_t, Type, ConstantSInt> SIntConstants;
781static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000782
Chris Lattner3462ae32001-12-03 22:26:30 +0000783ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000784 return SIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000785}
786
Chris Lattner3462ae32001-12-03 22:26:30 +0000787ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000788 return UIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000789}
790
Chris Lattner3462ae32001-12-03 22:26:30 +0000791ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000792 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000793 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
794 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000795}
796
Chris Lattner3462ae32001-12-03 22:26:30 +0000797//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000798//
Chris Lattnerac80ea42004-02-01 22:49:04 +0000799namespace llvm {
800 template<>
801 struct ConstantCreator<ConstantFP, Type, uint64_t> {
802 static ConstantFP *create(const Type *Ty, uint64_t V) {
803 assert(Ty == Type::DoubleTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000804 return new ConstantFP(Ty, BitsToDouble(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000805 }
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);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000811 return new ConstantFP(Ty, BitsToFloat(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000812 }
813 };
814}
815
816static ValueMap<uint64_t, Type, ConstantFP> DoubleConstants;
817static ValueMap<uint32_t, Type, ConstantFP> FloatConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000818
Jim Laskey8ad8f712005-08-17 20:06:22 +0000819bool ConstantFP::isNullValue() const {
820 return DoubleToBits(Val) == 0;
821}
822
823bool ConstantFP::isExactlyValue(double V) const {
824 return DoubleToBits(V) == DoubleToBits(Val);
825}
826
827
Chris Lattner3462ae32001-12-03 22:26:30 +0000828ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner241ed4c2004-01-23 00:55:21 +0000829 if (Ty == Type::FloatTy) {
830 // Force the value through memory to normalize it.
Jim Laskeyb74c6662005-08-17 19:34:49 +0000831 return FloatConstants.getOrCreate(Ty, FloatToBits(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000832 } else {
833 assert(Ty == Type::DoubleTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000834 return DoubleConstants.getOrCreate(Ty, DoubleToBits(V));
Chris Lattner241ed4c2004-01-23 00:55:21 +0000835 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000836}
837
Chris Lattner9fba3da2004-02-15 05:53:04 +0000838//---- ConstantAggregateZero::get() implementation...
839//
840namespace llvm {
841 // ConstantAggregateZero does not take extra "value" argument...
842 template<class ValType>
843 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
844 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
845 return new ConstantAggregateZero(Ty);
846 }
847 };
848
849 template<>
850 struct ConvertConstantType<ConstantAggregateZero, Type> {
851 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
852 // Make everyone now use a constant of the new type...
853 Constant *New = ConstantAggregateZero::get(NewTy);
854 assert(New != OldC && "Didn't replace constant??");
855 OldC->uncheckedReplaceAllUsesWith(New);
856 OldC->destroyConstant(); // This constant is now dead, destroy it.
857 }
858 };
859}
860
861static ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
862
Chris Lattner3e650af2004-08-04 04:48:01 +0000863static char getValType(ConstantAggregateZero *CPZ) { return 0; }
864
Chris Lattner9fba3da2004-02-15 05:53:04 +0000865Constant *ConstantAggregateZero::get(const Type *Ty) {
866 return AggZeroConstants.getOrCreate(Ty, 0);
867}
868
869// destroyConstant - Remove the constant from the constant table...
870//
871void ConstantAggregateZero::destroyConstant() {
872 AggZeroConstants.remove(this);
873 destroyConstantImpl();
874}
875
876void ConstantAggregateZero::replaceUsesOfWithOnConstant(Value *From, Value *To,
877 bool DisableChecking) {
878 assert(0 && "No uses!");
879 abort();
880}
881
882
883
Chris Lattner3462ae32001-12-03 22:26:30 +0000884//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000885//
Chris Lattner189d19f2003-11-21 20:23:48 +0000886namespace llvm {
887 template<>
888 struct ConvertConstantType<ConstantArray, ArrayType> {
889 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
890 // Make everyone now use a constant of the new type...
891 std::vector<Constant*> C;
892 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
893 C.push_back(cast<Constant>(OldC->getOperand(i)));
894 Constant *New = ConstantArray::get(NewTy, C);
895 assert(New != OldC && "Didn't replace constant??");
896 OldC->uncheckedReplaceAllUsesWith(New);
897 OldC->destroyConstant(); // This constant is now dead, destroy it.
898 }
899 };
900}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000901
Chris Lattner3e650af2004-08-04 04:48:01 +0000902static std::vector<Constant*> getValType(ConstantArray *CA) {
903 std::vector<Constant*> Elements;
904 Elements.reserve(CA->getNumOperands());
905 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
906 Elements.push_back(cast<Constant>(CA->getOperand(i)));
907 return Elements;
908}
909
Chris Lattner98fa07b2003-05-23 20:03:32 +0000910static ValueMap<std::vector<Constant*>, ArrayType,
911 ConstantArray> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000912
Chris Lattner015e8212004-02-15 04:14:47 +0000913Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +0000914 const std::vector<Constant*> &V) {
915 // If this is an all-zero array, return a ConstantAggregateZero object
916 if (!V.empty()) {
917 Constant *C = V[0];
918 if (!C->isNullValue())
919 return ArrayConstants.getOrCreate(Ty, V);
920 for (unsigned i = 1, e = V.size(); i != e; ++i)
921 if (V[i] != C)
922 return ArrayConstants.getOrCreate(Ty, V);
923 }
924 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000925}
926
Chris Lattner98fa07b2003-05-23 20:03:32 +0000927// destroyConstant - Remove the constant from the constant table...
928//
929void ConstantArray::destroyConstant() {
930 ArrayConstants.remove(this);
931 destroyConstantImpl();
932}
933
Chris Lattner3462ae32001-12-03 22:26:30 +0000934// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000935// contain the specified string. A null terminator is added to the specified
936// string so that it may be used in a natural way...
937//
Chris Lattner015e8212004-02-15 04:14:47 +0000938Constant *ConstantArray::get(const std::string &Str) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000939 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000940
941 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000942 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000943
944 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000945 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000946
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000947 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000948 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000949}
950
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000951/// isString - This method returns true if the array is an array of sbyte or
952/// ubyte, and if the elements of the array are all ConstantInt's.
953bool ConstantArray::isString() const {
954 // Check the element type for sbyte or ubyte...
Chris Lattnere8701f62004-01-14 17:51:53 +0000955 if (getType()->getElementType() != Type::UByteTy &&
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000956 getType()->getElementType() != Type::SByteTy)
957 return false;
958 // Check the elements to make sure they are all integers, not constant
959 // expressions.
960 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
961 if (!isa<ConstantInt>(getOperand(i)))
962 return false;
963 return true;
964}
965
Chris Lattner81fabb02002-08-26 17:53:56 +0000966// getAsString - If the sub-element type of this array is either sbyte or ubyte,
967// then this method converts the array to an std::string and returns it.
968// Otherwise, it asserts out.
969//
970std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000971 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +0000972 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +0000973 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
974 Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue();
Chris Lattner81fabb02002-08-26 17:53:56 +0000975 return Result;
976}
977
978
Chris Lattner3462ae32001-12-03 22:26:30 +0000979//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000980//
Chris Lattnerb50d1352003-10-05 00:17:43 +0000981
Chris Lattner189d19f2003-11-21 20:23:48 +0000982namespace llvm {
983 template<>
984 struct ConvertConstantType<ConstantStruct, StructType> {
985 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
986 // Make everyone now use a constant of the new type...
987 std::vector<Constant*> C;
988 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
989 C.push_back(cast<Constant>(OldC->getOperand(i)));
990 Constant *New = ConstantStruct::get(NewTy, C);
991 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000992
Chris Lattner189d19f2003-11-21 20:23:48 +0000993 OldC->uncheckedReplaceAllUsesWith(New);
994 OldC->destroyConstant(); // This constant is now dead, destroy it.
995 }
996 };
997}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000998
Misha Brukmanb1c93172005-04-21 23:48:37 +0000999static ValueMap<std::vector<Constant*>, StructType,
Chris Lattner98fa07b2003-05-23 20:03:32 +00001000 ConstantStruct> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001001
Chris Lattner3e650af2004-08-04 04:48:01 +00001002static std::vector<Constant*> getValType(ConstantStruct *CS) {
1003 std::vector<Constant*> Elements;
1004 Elements.reserve(CS->getNumOperands());
1005 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1006 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1007 return Elements;
1008}
1009
Chris Lattner015e8212004-02-15 04:14:47 +00001010Constant *ConstantStruct::get(const StructType *Ty,
1011 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001012 // Create a ConstantAggregateZero value if all elements are zeros...
1013 for (unsigned i = 0, e = V.size(); i != e; ++i)
1014 if (!V[i]->isNullValue())
1015 return StructConstants.getOrCreate(Ty, V);
1016
1017 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001018}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001019
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001020Constant *ConstantStruct::get(const std::vector<Constant*> &V) {
1021 std::vector<const Type*> StructEls;
1022 StructEls.reserve(V.size());
1023 for (unsigned i = 0, e = V.size(); i != e; ++i)
1024 StructEls.push_back(V[i]->getType());
1025 return get(StructType::get(StructEls), V);
1026}
1027
Chris Lattnerd7a73302001-10-13 06:57:33 +00001028// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001029//
Chris Lattner3462ae32001-12-03 22:26:30 +00001030void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +00001031 StructConstants.remove(this);
1032 destroyConstantImpl();
1033}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001034
Brian Gaeke02209042004-08-20 06:00:58 +00001035//---- ConstantPacked::get() implementation...
1036//
1037namespace llvm {
1038 template<>
1039 struct ConvertConstantType<ConstantPacked, PackedType> {
1040 static void convert(ConstantPacked *OldC, const PackedType *NewTy) {
1041 // Make everyone now use a constant of the new type...
1042 std::vector<Constant*> C;
1043 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1044 C.push_back(cast<Constant>(OldC->getOperand(i)));
1045 Constant *New = ConstantPacked::get(NewTy, C);
1046 assert(New != OldC && "Didn't replace constant??");
1047 OldC->uncheckedReplaceAllUsesWith(New);
1048 OldC->destroyConstant(); // This constant is now dead, destroy it.
1049 }
1050 };
1051}
1052
1053static std::vector<Constant*> getValType(ConstantPacked *CP) {
1054 std::vector<Constant*> Elements;
1055 Elements.reserve(CP->getNumOperands());
1056 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1057 Elements.push_back(CP->getOperand(i));
1058 return Elements;
1059}
1060
1061static ValueMap<std::vector<Constant*>, PackedType,
1062 ConstantPacked> PackedConstants;
1063
1064Constant *ConstantPacked::get(const PackedType *Ty,
1065 const std::vector<Constant*> &V) {
1066 // If this is an all-zero packed, return a ConstantAggregateZero object
1067 if (!V.empty()) {
1068 Constant *C = V[0];
1069 if (!C->isNullValue())
1070 return PackedConstants.getOrCreate(Ty, V);
1071 for (unsigned i = 1, e = V.size(); i != e; ++i)
1072 if (V[i] != C)
1073 return PackedConstants.getOrCreate(Ty, V);
1074 }
1075 return ConstantAggregateZero::get(Ty);
1076}
1077
1078Constant *ConstantPacked::get(const std::vector<Constant*> &V) {
1079 assert(!V.empty() && "Cannot infer type if V is empty");
1080 return get(PackedType::get(V.front()->getType(),V.size()), V);
1081}
1082
1083// destroyConstant - Remove the constant from the constant table...
1084//
1085void ConstantPacked::destroyConstant() {
1086 PackedConstants.remove(this);
1087 destroyConstantImpl();
1088}
1089
Chris Lattner3462ae32001-12-03 22:26:30 +00001090//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001091//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001092
Chris Lattner189d19f2003-11-21 20:23:48 +00001093namespace llvm {
1094 // ConstantPointerNull does not take extra "value" argument...
1095 template<class ValType>
1096 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1097 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1098 return new ConstantPointerNull(Ty);
1099 }
1100 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001101
Chris Lattner189d19f2003-11-21 20:23:48 +00001102 template<>
1103 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1104 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1105 // Make everyone now use a constant of the new type...
1106 Constant *New = ConstantPointerNull::get(NewTy);
1107 assert(New != OldC && "Didn't replace constant??");
1108 OldC->uncheckedReplaceAllUsesWith(New);
1109 OldC->destroyConstant(); // This constant is now dead, destroy it.
1110 }
1111 };
1112}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001113
Chris Lattner98fa07b2003-05-23 20:03:32 +00001114static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001115
Chris Lattner3e650af2004-08-04 04:48:01 +00001116static char getValType(ConstantPointerNull *) {
1117 return 0;
1118}
1119
1120
Chris Lattner3462ae32001-12-03 22:26:30 +00001121ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner98fa07b2003-05-23 20:03:32 +00001122 return NullPtrConstants.getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001123}
1124
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001125// destroyConstant - Remove the constant from the constant table...
1126//
1127void ConstantPointerNull::destroyConstant() {
1128 NullPtrConstants.remove(this);
1129 destroyConstantImpl();
1130}
1131
1132
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001133//---- UndefValue::get() implementation...
1134//
1135
1136namespace llvm {
1137 // UndefValue does not take extra "value" argument...
1138 template<class ValType>
1139 struct ConstantCreator<UndefValue, Type, ValType> {
1140 static UndefValue *create(const Type *Ty, const ValType &V) {
1141 return new UndefValue(Ty);
1142 }
1143 };
1144
1145 template<>
1146 struct ConvertConstantType<UndefValue, Type> {
1147 static void convert(UndefValue *OldC, const Type *NewTy) {
1148 // Make everyone now use a constant of the new type.
1149 Constant *New = UndefValue::get(NewTy);
1150 assert(New != OldC && "Didn't replace constant??");
1151 OldC->uncheckedReplaceAllUsesWith(New);
1152 OldC->destroyConstant(); // This constant is now dead, destroy it.
1153 }
1154 };
1155}
1156
1157static ValueMap<char, Type, UndefValue> UndefValueConstants;
1158
1159static char getValType(UndefValue *) {
1160 return 0;
1161}
1162
1163
1164UndefValue *UndefValue::get(const Type *Ty) {
1165 return UndefValueConstants.getOrCreate(Ty, 0);
1166}
1167
1168// destroyConstant - Remove the constant from the constant table.
1169//
1170void UndefValue::destroyConstant() {
1171 UndefValueConstants.remove(this);
1172 destroyConstantImpl();
1173}
1174
1175
1176
1177
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001178//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001179//
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001180typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Chris Lattner98fa07b2003-05-23 20:03:32 +00001181
Chris Lattner189d19f2003-11-21 20:23:48 +00001182namespace llvm {
1183 template<>
1184 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
1185 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
1186 if (V.first == Instruction::Cast)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001187 return new UnaryConstantExpr(Instruction::Cast, V.second[0], Ty);
Chris Lattner189d19f2003-11-21 20:23:48 +00001188 if ((V.first >= Instruction::BinaryOpsBegin &&
1189 V.first < Instruction::BinaryOpsEnd) ||
1190 V.first == Instruction::Shl || V.first == Instruction::Shr)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001191 return new BinaryConstantExpr(V.first, V.second[0], V.second[1]);
Chris Lattnerb7897ce2004-03-30 22:51:03 +00001192 if (V.first == Instruction::Select)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001193 return new SelectConstantExpr(V.second[0], V.second[1], V.second[2]);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001194
Chris Lattner189d19f2003-11-21 20:23:48 +00001195 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001196
Chris Lattner189d19f2003-11-21 20:23:48 +00001197 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001198 return new GetElementPtrConstantExpr(V.second[0], IdxList, Ty);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001199 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001200 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001201
Chris Lattner189d19f2003-11-21 20:23:48 +00001202 template<>
1203 struct ConvertConstantType<ConstantExpr, Type> {
1204 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1205 Constant *New;
1206 switch (OldC->getOpcode()) {
1207 case Instruction::Cast:
1208 New = ConstantExpr::getCast(OldC->getOperand(0), NewTy);
1209 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001210 case Instruction::Select:
1211 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1212 OldC->getOperand(1),
1213 OldC->getOperand(2));
1214 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001215 case Instruction::Shl:
1216 case Instruction::Shr:
1217 New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
1218 OldC->getOperand(0), OldC->getOperand(1));
1219 break;
1220 default:
1221 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
1222 OldC->getOpcode() < Instruction::BinaryOpsEnd);
1223 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1224 OldC->getOperand(1));
1225 break;
1226 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001227 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001228 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
1229 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), Idx);
Chris Lattner189d19f2003-11-21 20:23:48 +00001230 break;
1231 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001232
Chris Lattner189d19f2003-11-21 20:23:48 +00001233 assert(New != OldC && "Didn't replace constant??");
1234 OldC->uncheckedReplaceAllUsesWith(New);
1235 OldC->destroyConstant(); // This constant is now dead, destroy it.
1236 }
1237 };
1238} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001239
1240
Chris Lattner3e650af2004-08-04 04:48:01 +00001241static ExprMapKeyType getValType(ConstantExpr *CE) {
1242 std::vector<Constant*> Operands;
1243 Operands.reserve(CE->getNumOperands());
1244 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1245 Operands.push_back(cast<Constant>(CE->getOperand(i)));
1246 return ExprMapKeyType(CE->getOpcode(), Operands);
1247}
1248
Chris Lattner98fa07b2003-05-23 20:03:32 +00001249static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001250
Chris Lattner46b3d302003-04-16 22:40:51 +00001251Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001252 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1253
Chris Lattneracdbe712003-04-17 19:24:48 +00001254 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
1255 return FC; // Fold a few common cases...
1256
Vikram S. Adve4c485332002-07-15 18:19:33 +00001257 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001258 std::vector<Constant*> argVec(1, C);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001259 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
1260 return ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001261}
Chris Lattnerd7a73302001-10-13 06:57:33 +00001262
Chris Lattnerdd284742004-04-04 23:20:30 +00001263Constant *ConstantExpr::getSignExtend(Constant *C, const Type *Ty) {
Chris Lattner1ece6f82005-01-01 15:59:57 +00001264 assert(C->getType()->isIntegral() && Ty->isIntegral() &&
Chris Lattnerdd284742004-04-04 23:20:30 +00001265 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1266 "This is an illegal sign extension!");
Chris Lattner1ece6f82005-01-01 15:59:57 +00001267 if (C->getType() != Type::BoolTy) {
1268 C = ConstantExpr::getCast(C, C->getType()->getSignedVersion());
1269 return ConstantExpr::getCast(C, Ty);
1270 } else {
1271 if (C == ConstantBool::True)
1272 return ConstantIntegral::getAllOnesValue(Ty);
1273 else
1274 return ConstantIntegral::getNullValue(Ty);
1275 }
Chris Lattnerdd284742004-04-04 23:20:30 +00001276}
1277
1278Constant *ConstantExpr::getZeroExtend(Constant *C, const Type *Ty) {
Chris Lattner1ece6f82005-01-01 15:59:57 +00001279 assert(C->getType()->isIntegral() && Ty->isIntegral() &&
Chris Lattnerdd284742004-04-04 23:20:30 +00001280 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1281 "This is an illegal zero extension!");
Chris Lattner1ece6f82005-01-01 15:59:57 +00001282 if (C->getType() != Type::BoolTy)
1283 C = ConstantExpr::getCast(C, C->getType()->getUnsignedVersion());
Chris Lattnerdd284742004-04-04 23:20:30 +00001284 return ConstantExpr::getCast(C, Ty);
1285}
1286
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001287Constant *ConstantExpr::getSizeOf(const Type *Ty) {
Chris Lattneracc4e542004-12-13 19:48:51 +00001288 // sizeof is implemented as: (ulong) gep (Ty*)null, 1
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001289 return getCast(
Chris Lattneracc4e542004-12-13 19:48:51 +00001290 getGetElementPtr(getNullValue(PointerType::get(Ty)),
1291 std::vector<Constant*>(1, ConstantInt::get(Type::UIntTy, 1))),
1292 Type::ULongTy);
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001293}
1294
Alkis Evlogimenos9160d5f2005-03-19 11:40:31 +00001295Constant *ConstantExpr::getPtrPtrFromArrayPtr(Constant *C) {
1296 // pointer from array is implemented as: getelementptr arr ptr, 0, 0
1297 static std::vector<Constant*> Indices(2, ConstantUInt::get(Type::UIntTy, 0));
1298
1299 return ConstantExpr::getGetElementPtr(C, Indices);
1300}
1301
Chris Lattnerb50d1352003-10-05 00:17:43 +00001302Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
1303 Constant *C1, Constant *C2) {
Chris Lattner5645e8a2004-01-12 19:04:55 +00001304 if (Opcode == Instruction::Shl || Opcode == Instruction::Shr)
1305 return getShiftTy(ReqTy, Opcode, C1, C2);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001306 // Check the operands for consistency first
1307 assert((Opcode >= Instruction::BinaryOpsBegin &&
1308 Opcode < Instruction::BinaryOpsEnd) &&
1309 "Invalid opcode in binary constant expression");
1310 assert(C1->getType() == C2->getType() &&
1311 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001312
Chris Lattner29ca2c62004-08-04 18:50:09 +00001313 if (ReqTy == C1->getType() || (Instruction::isRelational(Opcode) &&
1314 ReqTy == Type::BoolTy))
Chris Lattnerb50d1352003-10-05 00:17:43 +00001315 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1316 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001317
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001318 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001319 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001320 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001321}
1322
Chris Lattner29ca2c62004-08-04 18:50:09 +00001323Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001324#ifndef NDEBUG
1325 switch (Opcode) {
1326 case Instruction::Add: case Instruction::Sub:
1327 case Instruction::Mul: case Instruction::Div:
1328 case Instruction::Rem:
1329 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001330 assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint()) &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001331 "Tried to create an arithmetic operation on a non-arithmetic type!");
1332 break;
1333 case Instruction::And:
1334 case Instruction::Or:
1335 case Instruction::Xor:
1336 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1337 assert(C1->getType()->isIntegral() &&
Misha Brukman3852f652005-01-27 06:46:38 +00001338 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001339 break;
1340 case Instruction::SetLT: case Instruction::SetGT: case Instruction::SetLE:
1341 case Instruction::SetGE: case Instruction::SetEQ: case Instruction::SetNE:
1342 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1343 break;
1344 case Instruction::Shl:
1345 case Instruction::Shr:
1346 assert(C2->getType() == Type::UByteTy && "Shift should be by ubyte!");
1347 assert(C1->getType()->isInteger() &&
1348 "Tried to create a shift operation on a non-integer type!");
1349 break;
1350 default:
1351 break;
1352 }
1353#endif
1354
Chris Lattner29ca2c62004-08-04 18:50:09 +00001355 if (Instruction::isRelational(Opcode))
1356 return getTy(Type::BoolTy, Opcode, C1, C2);
1357 else
1358 return getTy(C1->getType(), Opcode, C1, C2);
1359}
1360
Chris Lattner6e415c02004-03-12 05:54:04 +00001361Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
1362 Constant *V1, Constant *V2) {
1363 assert(C->getType() == Type::BoolTy && "Select condition must be bool!");
1364 assert(V1->getType() == V2->getType() && "Select value types must match!");
1365 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
1366
1367 if (ReqTy == V1->getType())
1368 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1369 return SC; // Fold common cases
1370
1371 std::vector<Constant*> argVec(3, C);
1372 argVec[1] = V1;
1373 argVec[2] = V2;
1374 ExprMapKeyType Key = std::make_pair(Instruction::Select, argVec);
1375 return ExprConstants.getOrCreate(ReqTy, Key);
1376}
1377
Chris Lattner9eb2b522004-01-12 19:12:58 +00001378/// getShiftTy - Return a shift left or shift right constant expr
Chris Lattnerb50d1352003-10-05 00:17:43 +00001379Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
1380 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001381 // Check the operands for consistency first
1382 assert((Opcode == Instruction::Shl ||
1383 Opcode == Instruction::Shr) &&
1384 "Invalid opcode in binary constant expression");
1385 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
1386 "Invalid operand types for Shift constant expr!");
1387
Chris Lattner0bba7712004-01-12 20:40:42 +00001388 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001389 return FC; // Fold a few common cases...
1390
1391 // Look up the constant in the table first to ensure uniqueness
1392 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001393 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001394 return ExprConstants.getOrCreate(ReqTy, Key);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001395}
1396
1397
Chris Lattnerb50d1352003-10-05 00:17:43 +00001398Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner13128ab2004-10-11 22:52:25 +00001399 const std::vector<Value*> &IdxList) {
1400 assert(GetElementPtrInst::getIndexedType(C->getType(), IdxList, true) &&
Chris Lattner04b60fe2004-02-16 20:46:13 +00001401 "GEP indices invalid!");
1402
Chris Lattneracdbe712003-04-17 19:24:48 +00001403 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
1404 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001405
Chris Lattnerb50d1352003-10-05 00:17:43 +00001406 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001407 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001408 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00001409 std::vector<Constant*> ArgVec;
1410 ArgVec.reserve(IdxList.size()+1);
1411 ArgVec.push_back(C);
1412 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
1413 ArgVec.push_back(cast<Constant>(IdxList[i]));
1414 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,ArgVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001415 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001416}
1417
Chris Lattnerb50d1352003-10-05 00:17:43 +00001418Constant *ConstantExpr::getGetElementPtr(Constant *C,
1419 const std::vector<Constant*> &IdxList){
1420 // Get the result type of the getelementptr!
1421 std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
1422
1423 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
1424 true);
1425 assert(Ty && "GEP indices invalid!");
Chris Lattner13128ab2004-10-11 22:52:25 +00001426 return getGetElementPtrTy(PointerType::get(Ty), C, VIdxList);
1427}
1428
1429Constant *ConstantExpr::getGetElementPtr(Constant *C,
1430 const std::vector<Value*> &IdxList) {
1431 // Get the result type of the getelementptr!
1432 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
1433 true);
1434 assert(Ty && "GEP indices invalid!");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001435 return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
1436}
1437
1438
Vikram S. Adve4c485332002-07-15 18:19:33 +00001439// destroyConstant - Remove the constant from the constant table...
1440//
1441void ConstantExpr::destroyConstant() {
1442 ExprConstants.remove(this);
1443 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001444}
1445
Chris Lattner3cd8c562002-07-30 18:54:25 +00001446const char *ConstantExpr::getOpcodeName() const {
1447 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001448}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00001449
Chris Lattner99a669b2004-11-19 16:39:44 +00001450/// clearAllValueMaps - This method frees all internal memory used by the
1451/// constant subsystem, which can be used in environments where this memory
1452/// is otherwise reported as a leak.
1453void Constant::clearAllValueMaps() {
1454 std::vector<Constant *> Constants;
1455
1456 DoubleConstants.clear(Constants);
1457 FloatConstants.clear(Constants);
1458 SIntConstants.clear(Constants);
1459 UIntConstants.clear(Constants);
1460 AggZeroConstants.clear(Constants);
1461 ArrayConstants.clear(Constants);
1462 StructConstants.clear(Constants);
1463 PackedConstants.clear(Constants);
1464 NullPtrConstants.clear(Constants);
1465 UndefValueConstants.clear(Constants);
1466 ExprConstants.clear(Constants);
1467
Misha Brukmanb1c93172005-04-21 23:48:37 +00001468 for (std::vector<Constant *>::iterator I = Constants.begin(),
Chris Lattner99a669b2004-11-19 16:39:44 +00001469 E = Constants.end(); I != E; ++I)
1470 (*I)->dropAllReferences();
1471 for (std::vector<Constant *>::iterator I = Constants.begin(),
1472 E = Constants.end(); I != E; ++I)
1473 (*I)->destroyConstantImpl();
1474 Constants.clear();
1475}