blob: 8a9bfc787be67ef8fb90000df7ad55557ea261f7 [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 Lattner02157b02006-06-28 21:38:54 +000023#include "llvm/Support/Visibility.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000024#include <algorithm>
Reid Spencercf394bf2004-07-04 11:51:24 +000025#include <iostream>
Chris Lattner189d19f2003-11-21 20:23:48 +000026using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000027
Chris Lattner3462ae32001-12-03 22:26:30 +000028ConstantBool *ConstantBool::True = new ConstantBool(true);
29ConstantBool *ConstantBool::False = new ConstantBool(false);
Chris Lattner2f7c9632001-06-06 20:29:01 +000030
Chris Lattner9655e542001-07-20 19:16:02 +000031
Chris Lattner2f7c9632001-06-06 20:29:01 +000032//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000033// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000034//===----------------------------------------------------------------------===//
35
Chris Lattner3462ae32001-12-03 22:26:30 +000036void Constant::destroyConstantImpl() {
37 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000038 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000039 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +000040 // but they don't know that. Because we only find out when the CPV is
41 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000042 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000043 //
44 while (!use_empty()) {
45 Value *V = use_back();
46#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000047 if (!isa<Constant>(V))
48 std::cerr << "While deleting: " << *this
49 << "\n\nUse still stuck around after Def is destroyed: "
50 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000051#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000052 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000053 Constant *CV = cast<Constant>(V);
54 CV->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +000055
56 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000057 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000058 }
59
60 // Value has no outstanding references it is safe to delete it now...
61 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000062}
Chris Lattner2f7c9632001-06-06 20:29:01 +000063
Chris Lattnerb1585a92002-08-13 17:50:20 +000064// Static constructor to create a '0' constant of arbitrary type...
65Constant *Constant::getNullValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +000066 switch (Ty->getTypeID()) {
Chris Lattner3e88ef92003-10-03 19:34:51 +000067 case Type::BoolTyID: {
68 static Constant *NullBool = ConstantBool::get(false);
69 return NullBool;
70 }
71 case Type::SByteTyID: {
72 static Constant *NullSByte = ConstantSInt::get(Type::SByteTy, 0);
73 return NullSByte;
74 }
75 case Type::UByteTyID: {
76 static Constant *NullUByte = ConstantUInt::get(Type::UByteTy, 0);
77 return NullUByte;
78 }
79 case Type::ShortTyID: {
80 static Constant *NullShort = ConstantSInt::get(Type::ShortTy, 0);
81 return NullShort;
82 }
83 case Type::UShortTyID: {
84 static Constant *NullUShort = ConstantUInt::get(Type::UShortTy, 0);
85 return NullUShort;
86 }
87 case Type::IntTyID: {
88 static Constant *NullInt = ConstantSInt::get(Type::IntTy, 0);
89 return NullInt;
90 }
91 case Type::UIntTyID: {
92 static Constant *NullUInt = ConstantUInt::get(Type::UIntTy, 0);
93 return NullUInt;
94 }
95 case Type::LongTyID: {
96 static Constant *NullLong = ConstantSInt::get(Type::LongTy, 0);
97 return NullLong;
98 }
99 case Type::ULongTyID: {
100 static Constant *NullULong = ConstantUInt::get(Type::ULongTy, 0);
101 return NullULong;
102 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000103
Chris Lattner3e88ef92003-10-03 19:34:51 +0000104 case Type::FloatTyID: {
105 static Constant *NullFloat = ConstantFP::get(Type::FloatTy, 0);
106 return NullFloat;
107 }
108 case Type::DoubleTyID: {
109 static Constant *NullDouble = ConstantFP::get(Type::DoubleTy, 0);
110 return NullDouble;
111 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000112
Misha Brukmanb1c93172005-04-21 23:48:37 +0000113 case Type::PointerTyID:
Chris Lattnerb1585a92002-08-13 17:50:20 +0000114 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner3e88ef92003-10-03 19:34:51 +0000115
Chris Lattner9fba3da2004-02-15 05:53:04 +0000116 case Type::StructTyID:
117 case Type::ArrayTyID:
Brian Gaeke02209042004-08-20 06:00:58 +0000118 case Type::PackedTyID:
Chris Lattner9fba3da2004-02-15 05:53:04 +0000119 return ConstantAggregateZero::get(Ty);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000120 default:
Reid Spencercf394bf2004-07-04 11:51:24 +0000121 // Function, Label, or Opaque type?
122 assert(!"Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000123 return 0;
124 }
125}
126
127// Static constructor to create the maximum constant of an integral type...
128ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000129 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000130 case Type::BoolTyID: return ConstantBool::True;
131 case Type::SByteTyID:
132 case Type::ShortTyID:
133 case Type::IntTyID:
134 case Type::LongTyID: {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000135 // Calculate 011111111111111...
Chris Lattnerb1585a92002-08-13 17:50:20 +0000136 unsigned TypeBits = Ty->getPrimitiveSize()*8;
137 int64_t Val = INT64_MAX; // All ones
138 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
139 return ConstantSInt::get(Ty, Val);
140 }
141
142 case Type::UByteTyID:
143 case Type::UShortTyID:
144 case Type::UIntTyID:
145 case Type::ULongTyID: return getAllOnesValue(Ty);
146
Chris Lattner31408f72002-08-14 17:12:13 +0000147 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000148 }
149}
150
151// Static constructor to create the minimum constant for an integral type...
152ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000153 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000154 case Type::BoolTyID: return ConstantBool::False;
155 case Type::SByteTyID:
156 case Type::ShortTyID:
157 case Type::IntTyID:
158 case Type::LongTyID: {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000159 // Calculate 1111111111000000000000
Chris Lattnerb1585a92002-08-13 17:50:20 +0000160 unsigned TypeBits = Ty->getPrimitiveSize()*8;
161 int64_t Val = -1; // All ones
162 Val <<= TypeBits-1; // Shift over to the right spot
163 return ConstantSInt::get(Ty, Val);
164 }
165
166 case Type::UByteTyID:
167 case Type::UShortTyID:
168 case Type::UIntTyID:
169 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
170
Chris Lattner31408f72002-08-14 17:12:13 +0000171 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000172 }
173}
174
175// Static constructor to create an integral constant with all bits set
176ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000177 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000178 case Type::BoolTyID: return ConstantBool::True;
179 case Type::SByteTyID:
180 case Type::ShortTyID:
181 case Type::IntTyID:
182 case Type::LongTyID: return ConstantSInt::get(Ty, -1);
183
184 case Type::UByteTyID:
185 case Type::UShortTyID:
186 case Type::UIntTyID:
187 case Type::ULongTyID: {
188 // Calculate ~0 of the right type...
189 unsigned TypeBits = Ty->getPrimitiveSize()*8;
190 uint64_t Val = ~0ULL; // All ones
191 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
192 return ConstantUInt::get(Ty, Val);
193 }
Chris Lattner31408f72002-08-14 17:12:13 +0000194 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000195 }
196}
197
Chris Lattner83e5d392003-03-10 22:39:02 +0000198bool ConstantUInt::isAllOnesValue() const {
199 unsigned TypeBits = getType()->getPrimitiveSize()*8;
200 uint64_t Val = ~0ULL; // All ones
201 Val >>= 64-TypeBits; // Shift out inappropriate bits
202 return getValue() == Val;
203}
204
Chris Lattnerb1585a92002-08-13 17:50:20 +0000205
Chris Lattner2f7c9632001-06-06 20:29:01 +0000206//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +0000207// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +0000208//===----------------------------------------------------------------------===//
209
210//===----------------------------------------------------------------------===//
211// Normal Constructors
212
Chris Lattnere7e139e2005-09-27 06:09:08 +0000213ConstantIntegral::ConstantIntegral(const Type *Ty, ValueTy VT, uint64_t V)
214 : Constant(Ty, VT, 0, 0) {
Chris Lattner265eb642004-06-21 12:12:12 +0000215 Val.Unsigned = V;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000216}
Chris Lattner49d855c2001-09-07 16:46:31 +0000217
Chris Lattnere7e139e2005-09-27 06:09:08 +0000218ConstantBool::ConstantBool(bool V)
219 : ConstantIntegral(Type::BoolTy, ConstantBoolVal, V) {
Chris Lattner265eb642004-06-21 12:12:12 +0000220}
221
Chris Lattnere7e139e2005-09-27 06:09:08 +0000222ConstantInt::ConstantInt(const Type *Ty, ValueTy VT, uint64_t V)
223 : ConstantIntegral(Ty, VT, V) {
Chris Lattner7309d662001-07-21 19:16:08 +0000224}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000225
Chris Lattnere7e139e2005-09-27 06:09:08 +0000226ConstantSInt::ConstantSInt(const Type *Ty, int64_t V)
227 : ConstantInt(Ty, ConstantSIntVal, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000228 assert(Ty->isInteger() && Ty->isSigned() &&
Reid Spencerf064bb22005-03-09 15:19:41 +0000229 "Illegal type for signed integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000230 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000231}
232
Chris Lattnere7e139e2005-09-27 06:09:08 +0000233ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V)
234 : ConstantInt(Ty, ConstantUIntVal, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000235 assert(Ty->isInteger() && Ty->isUnsigned() &&
236 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000237 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000238}
239
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000240ConstantFP::ConstantFP(const Type *Ty, double V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000241 : Constant(Ty, ConstantFPVal, 0, 0) {
Chris Lattner9655e542001-07-20 19:16:02 +0000242 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000243 Val = V;
244}
245
Chris Lattner3462ae32001-12-03 22:26:30 +0000246ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000247 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000248 : Constant(T, ConstantArrayVal, new Use[V.size()], V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000249 assert(V.size() == T->getNumElements() &&
250 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000251 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000252 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
253 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000254 Constant *C = *I;
255 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000256 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000257 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000258 "Initializer for array element doesn't match array element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000259 OL->init(C, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000260 }
261}
262
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000263ConstantArray::~ConstantArray() {
264 delete [] OperandList;
265}
266
Chris Lattner3462ae32001-12-03 22:26:30 +0000267ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000268 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000269 : Constant(T, ConstantStructVal, new Use[V.size()], V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000270 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000271 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000272 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000273 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
274 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000275 Constant *C = *I;
276 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000277 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000278 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000279 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000280 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000281 "Initializer for struct element doesn't match struct element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000282 OL->init(C, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000283 }
284}
285
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000286ConstantStruct::~ConstantStruct() {
287 delete [] OperandList;
288}
289
290
Brian Gaeke02209042004-08-20 06:00:58 +0000291ConstantPacked::ConstantPacked(const PackedType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000292 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000293 : Constant(T, ConstantPackedVal, new Use[V.size()], V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000294 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000295 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
296 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000297 Constant *C = *I;
298 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000299 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000300 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000301 "Initializer for packed element doesn't match packed element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000302 OL->init(C, this);
Brian Gaeke02209042004-08-20 06:00:58 +0000303 }
304}
305
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000306ConstantPacked::~ConstantPacked() {
307 delete [] OperandList;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000308}
309
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000310/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
311/// behind the scenes to implement unary constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000312namespace {
313class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000314 Use Op;
315public:
316 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
317 : ConstantExpr(Ty, Opcode, &Op, 1), Op(C, this) {}
318};
Chris Lattner02157b02006-06-28 21:38:54 +0000319}
Chris Lattner6e415c02004-03-12 05:54:04 +0000320
Chris Lattner22ced562003-06-22 20:48:30 +0000321static bool isSetCC(unsigned Opcode) {
322 return Opcode == Instruction::SetEQ || Opcode == Instruction::SetNE ||
323 Opcode == Instruction::SetLT || Opcode == Instruction::SetGT ||
324 Opcode == Instruction::SetLE || Opcode == Instruction::SetGE;
325}
326
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000327/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
328/// behind the scenes to implement binary constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000329namespace {
330class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000331 Use Ops[2];
332public:
333 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
334 : ConstantExpr(isSetCC(Opcode) ? Type::BoolTy : C1->getType(),
335 Opcode, Ops, 2) {
336 Ops[0].init(C1, this);
337 Ops[1].init(C2, this);
338 }
339};
Chris Lattner02157b02006-06-28 21:38:54 +0000340}
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000341
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000342/// SelectConstantExpr - This class is private to Constants.cpp, and is used
343/// behind the scenes to implement select constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000344namespace {
345class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000346 Use Ops[3];
347public:
348 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
349 : ConstantExpr(C2->getType(), Instruction::Select, Ops, 3) {
350 Ops[0].init(C1, this);
351 Ops[1].init(C2, this);
352 Ops[2].init(C3, this);
353 }
354};
Chris Lattner02157b02006-06-28 21:38:54 +0000355}
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000356
Robert Bocchinoca27f032006-01-17 20:07:22 +0000357/// ExtractElementConstantExpr - This class is private to
358/// Constants.cpp, and is used behind the scenes to implement
359/// extractelement constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000360namespace {
361class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Robert Bocchino23004482006-01-10 19:05:34 +0000362 Use Ops[2];
363public:
364 ExtractElementConstantExpr(Constant *C1, Constant *C2)
365 : ConstantExpr(cast<PackedType>(C1->getType())->getElementType(),
366 Instruction::ExtractElement, Ops, 2) {
367 Ops[0].init(C1, this);
368 Ops[1].init(C2, this);
369 }
370};
Chris Lattner02157b02006-06-28 21:38:54 +0000371}
Robert Bocchino23004482006-01-10 19:05:34 +0000372
Robert Bocchinoca27f032006-01-17 20:07:22 +0000373/// InsertElementConstantExpr - This class is private to
374/// Constants.cpp, and is used behind the scenes to implement
375/// insertelement constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000376namespace {
377class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Robert Bocchinoca27f032006-01-17 20:07:22 +0000378 Use Ops[3];
379public:
380 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
381 : ConstantExpr(C1->getType(), Instruction::InsertElement,
382 Ops, 3) {
383 Ops[0].init(C1, this);
384 Ops[1].init(C2, this);
385 Ops[2].init(C3, this);
386 }
387};
Chris Lattner02157b02006-06-28 21:38:54 +0000388}
Robert Bocchinoca27f032006-01-17 20:07:22 +0000389
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000390/// ShuffleVectorConstantExpr - This class is private to
391/// Constants.cpp, and is used behind the scenes to implement
392/// shufflevector constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000393namespace {
394class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000395 Use Ops[3];
396public:
397 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
398 : ConstantExpr(C1->getType(), Instruction::ShuffleVector,
399 Ops, 3) {
400 Ops[0].init(C1, this);
401 Ops[1].init(C2, this);
402 Ops[2].init(C3, this);
403 }
404};
Chris Lattner02157b02006-06-28 21:38:54 +0000405}
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000406
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000407/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
408/// used behind the scenes to implement getelementpr constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000409namespace {
410struct VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000411 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
412 const Type *DestTy)
413 : ConstantExpr(DestTy, Instruction::GetElementPtr,
414 new Use[IdxList.size()+1], IdxList.size()+1) {
415 OperandList[0].init(C, this);
416 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
417 OperandList[i+1].init(IdxList[i], this);
418 }
419 ~GetElementPtrConstantExpr() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000420 delete [] OperandList;
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000421 }
422};
Chris Lattner02157b02006-06-28 21:38:54 +0000423}
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000424
Chris Lattner817175f2004-03-29 02:37:53 +0000425/// ConstantExpr::get* - Return some common constants without having to
426/// specify the full Instruction::OPCODE identifier.
427///
428Constant *ConstantExpr::getNeg(Constant *C) {
Chris Lattner3cdc27c2004-03-29 19:51:24 +0000429 if (!C->getType()->isFloatingPoint())
430 return get(Instruction::Sub, getNullValue(C->getType()), C);
431 else
432 return get(Instruction::Sub, ConstantFP::get(C->getType(), -0.0), C);
Chris Lattner817175f2004-03-29 02:37:53 +0000433}
434Constant *ConstantExpr::getNot(Constant *C) {
435 assert(isa<ConstantIntegral>(C) && "Cannot NOT a nonintegral type!");
436 return get(Instruction::Xor, C,
437 ConstantIntegral::getAllOnesValue(C->getType()));
438}
439Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
440 return get(Instruction::Add, C1, C2);
441}
442Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
443 return get(Instruction::Sub, C1, C2);
444}
445Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
446 return get(Instruction::Mul, C1, C2);
447}
448Constant *ConstantExpr::getDiv(Constant *C1, Constant *C2) {
449 return get(Instruction::Div, C1, C2);
450}
451Constant *ConstantExpr::getRem(Constant *C1, Constant *C2) {
452 return get(Instruction::Rem, C1, C2);
453}
454Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
455 return get(Instruction::And, C1, C2);
456}
457Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
458 return get(Instruction::Or, C1, C2);
459}
460Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
461 return get(Instruction::Xor, C1, C2);
462}
463Constant *ConstantExpr::getSetEQ(Constant *C1, Constant *C2) {
464 return get(Instruction::SetEQ, C1, C2);
465}
466Constant *ConstantExpr::getSetNE(Constant *C1, Constant *C2) {
467 return get(Instruction::SetNE, C1, C2);
468}
469Constant *ConstantExpr::getSetLT(Constant *C1, Constant *C2) {
470 return get(Instruction::SetLT, C1, C2);
471}
472Constant *ConstantExpr::getSetGT(Constant *C1, Constant *C2) {
473 return get(Instruction::SetGT, C1, C2);
474}
475Constant *ConstantExpr::getSetLE(Constant *C1, Constant *C2) {
476 return get(Instruction::SetLE, C1, C2);
477}
478Constant *ConstantExpr::getSetGE(Constant *C1, Constant *C2) {
479 return get(Instruction::SetGE, C1, C2);
480}
481Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
482 return get(Instruction::Shl, C1, C2);
483}
484Constant *ConstantExpr::getShr(Constant *C1, Constant *C2) {
485 return get(Instruction::Shr, C1, C2);
486}
487
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000488Constant *ConstantExpr::getUShr(Constant *C1, Constant *C2) {
489 if (C1->getType()->isUnsigned()) return getShr(C1, C2);
490 return getCast(getShr(getCast(C1,
491 C1->getType()->getUnsignedVersion()), C2), C1->getType());
492}
Chris Lattner817175f2004-03-29 02:37:53 +0000493
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000494Constant *ConstantExpr::getSShr(Constant *C1, Constant *C2) {
495 if (C1->getType()->isSigned()) return getShr(C1, C2);
496 return getCast(getShr(getCast(C1,
497 C1->getType()->getSignedVersion()), C2), C1->getType());
498}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000499
Chris Lattner7c1018a2006-07-14 19:37:40 +0000500/// getWithOperandReplaced - Return a constant expression identical to this
501/// one, but with the specified operand set to the specified value.
502Constant *ConstantExpr::getWithOperandReplaced(unsigned OpNo,
503 Constant *Op) const {
504 assert(OpNo < getNumOperands() && "Operand num is out of range!");
505 assert(Op->getType() == getOperand(OpNo)->getType() &&
506 "Replacing operand with value of different type!");
507 if (getOperand(OpNo) == Op) return const_cast<ConstantExpr*>(this);
508
509 switch (getOpcode()) {
510 case Instruction::Cast:
511 return ConstantExpr::getCast(Op, getType());
512 case Instruction::GetElementPtr: {
513 std::vector<Constant*> Ops;
514 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
515 Ops.push_back(getOperand(i));
516 if (OpNo == 0)
517 return ConstantExpr::getGetElementPtr(Op, Ops);
518 Ops[OpNo-1] = Op;
519 return ConstantExpr::getGetElementPtr(getOperand(0), Ops);
520 }
521 case Instruction::Select:
522 if (OpNo == 0)
523 return ConstantExpr::getSelect(Op, getOperand(1), getOperand(2));
524 if (OpNo == 1)
525 return ConstantExpr::getSelect(getOperand(0), Op, getOperand(2));
526 return ConstantExpr::getSelect(getOperand(0), getOperand(1), Op);
527 case Instruction::InsertElement:
528 if (OpNo == 0)
529 return ConstantExpr::getInsertElement(Op, getOperand(1), getOperand(2));
530 if (OpNo == 1)
531 return ConstantExpr::getInsertElement(getOperand(0), Op, getOperand(2));
532 return ConstantExpr::getInsertElement(getOperand(0), getOperand(1), Op);
533 case Instruction::ExtractElement:
534 if (OpNo == 0)
535 return ConstantExpr::getExtractElement(Op, getOperand(1));
536 return ConstantExpr::getExtractElement(getOperand(0), Op);
537 case Instruction::ShuffleVector:
538 if (OpNo == 0)
539 return ConstantExpr::getShuffleVector(Op, getOperand(1), getOperand(2));
540 if (OpNo == 1)
541 return ConstantExpr::getShuffleVector(getOperand(0), Op, getOperand(2));
542 return ConstantExpr::getShuffleVector(getOperand(0), getOperand(1), Op);
543 default:
544 assert(getNumOperands() == 2 && "Must be binary operator?");
545 if (OpNo == 0)
546 return ConstantExpr::get(getOpcode(), Op, getOperand(1));
547 return ConstantExpr::get(getOpcode(), getOperand(0), Op);
548 }
549}
550
Chris Lattner2f7c9632001-06-06 20:29:01 +0000551
552//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000553// isValueValidForType implementations
554
Chris Lattner3462ae32001-12-03 22:26:30 +0000555bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000556 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000557 default:
558 return false; // These can't be represented as integers!!!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000559 // Signed types...
560 case Type::SByteTyID:
561 return (Val <= INT8_MAX && Val >= INT8_MIN);
562 case Type::ShortTyID:
563 return (Val <= INT16_MAX && Val >= INT16_MIN);
564 case Type::IntTyID:
Chris Lattner74248512004-06-08 23:21:39 +0000565 return (Val <= int(INT32_MAX) && Val >= int(INT32_MIN));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000566 case Type::LongTyID:
567 return true; // This is the largest type...
568 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000569}
570
Chris Lattner3462ae32001-12-03 22:26:30 +0000571bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000572 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000573 default:
574 return false; // These can't be represented as integers!!!
575
576 // Unsigned types...
577 case Type::UByteTyID:
578 return (Val <= UINT8_MAX);
579 case Type::UShortTyID:
580 return (Val <= UINT16_MAX);
581 case Type::UIntTyID:
582 return (Val <= UINT32_MAX);
583 case Type::ULongTyID:
584 return true; // This is the largest type...
585 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000586}
587
Chris Lattner3462ae32001-12-03 22:26:30 +0000588bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000589 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000590 default:
591 return false; // These can't be represented as floating point!
592
Reid Spencerb95f8ab2004-12-07 07:38:08 +0000593 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000594 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000595 case Type::DoubleTyID:
596 return true; // This is the largest type...
597 }
Chris Lattneraa2372562006-05-24 17:04:05 +0000598}
Chris Lattner9655e542001-07-20 19:16:02 +0000599
Chris Lattner49d855c2001-09-07 16:46:31 +0000600//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000601// Factory Function Implementation
602
Chris Lattner98fa07b2003-05-23 20:03:32 +0000603// ConstantCreator - A class that is used to create constants by
604// ValueMap*. This class should be partially specialized if there is
605// something strange that needs to be done to interface to the ctor for the
606// constant.
607//
Chris Lattner189d19f2003-11-21 20:23:48 +0000608namespace llvm {
609 template<class ConstantClass, class TypeClass, class ValType>
Chris Lattner02157b02006-06-28 21:38:54 +0000610 struct VISIBILITY_HIDDEN ConstantCreator {
Chris Lattner189d19f2003-11-21 20:23:48 +0000611 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
612 return new ConstantClass(Ty, V);
613 }
614 };
Misha Brukmanb1c93172005-04-21 23:48:37 +0000615
Chris Lattner189d19f2003-11-21 20:23:48 +0000616 template<class ConstantClass, class TypeClass>
Chris Lattner02157b02006-06-28 21:38:54 +0000617 struct VISIBILITY_HIDDEN ConvertConstantType {
Chris Lattner189d19f2003-11-21 20:23:48 +0000618 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
619 assert(0 && "This type cannot be converted!\n");
620 abort();
621 }
622 };
623}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000624
Chris Lattner98fa07b2003-05-23 20:03:32 +0000625namespace {
Chris Lattner935aa922005-10-04 17:48:46 +0000626 template<class ValType, class TypeClass, class ConstantClass,
627 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattner02157b02006-06-28 21:38:54 +0000628 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
Chris Lattnerb64419a2005-10-03 22:51:37 +0000629 public:
Chris Lattnerb50d1352003-10-05 00:17:43 +0000630 typedef std::pair<const TypeClass*, ValType> MapKey;
631 typedef std::map<MapKey, ConstantClass *> MapTy;
632 typedef typename MapTy::iterator MapIterator;
Chris Lattnerb64419a2005-10-03 22:51:37 +0000633 private:
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000634 /// Map - This is the main map from the element descriptor to the Constants.
635 /// This is the primary way we avoid creating two of the same shape
636 /// constant.
Chris Lattnerb50d1352003-10-05 00:17:43 +0000637 MapTy Map;
Chris Lattner935aa922005-10-04 17:48:46 +0000638
639 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
640 /// from the constants to their element in Map. This is important for
641 /// removal of constants from the array, which would otherwise have to scan
642 /// through the map with very large keys.
643 std::map<ConstantClass*, MapIterator> InverseMap;
Chris Lattnerb50d1352003-10-05 00:17:43 +0000644
645 typedef std::map<const TypeClass*, MapIterator> AbstractTypeMapTy;
646 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner99a669b2004-11-19 16:39:44 +0000647
648 friend void Constant::clearAllValueMaps();
649 private:
650 void clear(std::vector<Constant *> &Constants) {
651 for(MapIterator I = Map.begin(); I != Map.end(); ++I)
652 Constants.push_back(I->second);
653 Map.clear();
654 AbstractTypeMap.clear();
Chris Lattner935aa922005-10-04 17:48:46 +0000655 InverseMap.clear();
Chris Lattner99a669b2004-11-19 16:39:44 +0000656 }
657
Chris Lattner98fa07b2003-05-23 20:03:32 +0000658 public:
Chris Lattnerb64419a2005-10-03 22:51:37 +0000659 MapIterator map_end() { return Map.end(); }
660
661 /// InsertOrGetItem - Return an iterator for the specified element.
662 /// If the element exists in the map, the returned iterator points to the
663 /// entry and Exists=true. If not, the iterator points to the newly
664 /// inserted entry and returns Exists=false. Newly inserted entries have
665 /// I->second == 0, and should be filled in.
666 MapIterator InsertOrGetItem(std::pair<MapKey, ConstantClass *> &InsertVal,
667 bool &Exists) {
668 std::pair<MapIterator, bool> IP = Map.insert(InsertVal);
669 Exists = !IP.second;
670 return IP.first;
671 }
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000672
Chris Lattner935aa922005-10-04 17:48:46 +0000673private:
674 MapIterator FindExistingElement(ConstantClass *CP) {
675 if (HasLargeKey) {
676 typename std::map<ConstantClass*, MapIterator>::iterator
677 IMI = InverseMap.find(CP);
678 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
679 IMI->second->second == CP &&
680 "InverseMap corrupt!");
681 return IMI->second;
682 }
683
684 MapIterator I =
685 Map.find(MapKey((TypeClass*)CP->getRawType(), getValType(CP)));
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000686 if (I == Map.end() || I->second != CP) {
687 // FIXME: This should not use a linear scan. If this gets to be a
688 // performance problem, someone should look at this.
689 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
690 /* empty */;
691 }
Chris Lattner935aa922005-10-04 17:48:46 +0000692 return I;
693 }
694public:
695
Chris Lattnerb64419a2005-10-03 22:51:37 +0000696 /// getOrCreate - Return the specified constant from the map, creating it if
697 /// necessary.
Chris Lattner98fa07b2003-05-23 20:03:32 +0000698 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000699 MapKey Lookup(Ty, V);
700 MapIterator I = Map.lower_bound(Lookup);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000701 if (I != Map.end() && I->first == Lookup)
702 return I->second; // Is it in the map?
703
704 // If no preexisting value, create one now...
705 ConstantClass *Result =
706 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
707
Chris Lattnerb50d1352003-10-05 00:17:43 +0000708 /// FIXME: why does this assert fail when loading 176.gcc?
709 //assert(Result->getType() == Ty && "Type specified is not correct!");
710 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
711
Chris Lattner935aa922005-10-04 17:48:46 +0000712 if (HasLargeKey) // Remember the reverse mapping if needed.
713 InverseMap.insert(std::make_pair(Result, I));
714
Chris Lattnerb50d1352003-10-05 00:17:43 +0000715 // If the type of the constant is abstract, make sure that an entry exists
716 // for it in the AbstractTypeMap.
717 if (Ty->isAbstract()) {
718 typename AbstractTypeMapTy::iterator TI =
719 AbstractTypeMap.lower_bound(Ty);
720
721 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
722 // Add ourselves to the ATU list of the type.
723 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
724
725 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
726 }
727 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000728 return Result;
729 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000730
Chris Lattner98fa07b2003-05-23 20:03:32 +0000731 void remove(ConstantClass *CP) {
Chris Lattner935aa922005-10-04 17:48:46 +0000732 MapIterator I = FindExistingElement(CP);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000733 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +0000734 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +0000735
Chris Lattner935aa922005-10-04 17:48:46 +0000736 if (HasLargeKey) // Remember the reverse mapping if needed.
737 InverseMap.erase(CP);
738
Chris Lattnerb50d1352003-10-05 00:17:43 +0000739 // Now that we found the entry, make sure this isn't the entry that
740 // the AbstractTypeMap points to.
741 const TypeClass *Ty = I->first.first;
742 if (Ty->isAbstract()) {
743 assert(AbstractTypeMap.count(Ty) &&
744 "Abstract type not in AbstractTypeMap?");
745 MapIterator &ATMEntryIt = AbstractTypeMap[Ty];
746 if (ATMEntryIt == I) {
747 // Yes, we are removing the representative entry for this type.
748 // See if there are any other entries of the same type.
749 MapIterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000750
Chris Lattnerb50d1352003-10-05 00:17:43 +0000751 // First check the entry before this one...
752 if (TmpIt != Map.begin()) {
753 --TmpIt;
754 if (TmpIt->first.first != Ty) // Not the same type, move back...
755 ++TmpIt;
756 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000757
Chris Lattnerb50d1352003-10-05 00:17:43 +0000758 // If we didn't find the same type, try to move forward...
759 if (TmpIt == ATMEntryIt) {
760 ++TmpIt;
761 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
762 --TmpIt; // No entry afterwards with the same type
763 }
764
765 // If there is another entry in the map of the same abstract type,
766 // update the AbstractTypeMap entry now.
767 if (TmpIt != ATMEntryIt) {
768 ATMEntryIt = TmpIt;
769 } else {
770 // Otherwise, we are removing the last instance of this type
771 // from the table. Remove from the ATM, and from user list.
772 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
773 AbstractTypeMap.erase(Ty);
774 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000775 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000776 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000777
Chris Lattnerb50d1352003-10-05 00:17:43 +0000778 Map.erase(I);
779 }
780
Chris Lattner3b793c62005-10-04 21:35:50 +0000781
782 /// MoveConstantToNewSlot - If we are about to change C to be the element
783 /// specified by I, update our internal data structures to reflect this
784 /// fact.
785 void MoveConstantToNewSlot(ConstantClass *C, MapIterator I) {
786 // First, remove the old location of the specified constant in the map.
787 MapIterator OldI = FindExistingElement(C);
788 assert(OldI != Map.end() && "Constant not found in constant table!");
789 assert(OldI->second == C && "Didn't find correct element?");
790
791 // If this constant is the representative element for its abstract type,
792 // update the AbstractTypeMap so that the representative element is I.
793 if (C->getType()->isAbstract()) {
794 typename AbstractTypeMapTy::iterator ATI =
795 AbstractTypeMap.find(C->getType());
796 assert(ATI != AbstractTypeMap.end() &&
797 "Abstract type not in AbstractTypeMap?");
798 if (ATI->second == OldI)
799 ATI->second = I;
800 }
801
802 // Remove the old entry from the map.
803 Map.erase(OldI);
804
805 // Update the inverse map so that we know that this constant is now
806 // located at descriptor I.
807 if (HasLargeKey) {
808 assert(I->second == C && "Bad inversemap entry!");
809 InverseMap[C] = I;
810 }
811 }
812
Chris Lattnerb50d1352003-10-05 00:17:43 +0000813 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000814 typename AbstractTypeMapTy::iterator I =
Chris Lattnerb50d1352003-10-05 00:17:43 +0000815 AbstractTypeMap.find(cast<TypeClass>(OldTy));
816
817 assert(I != AbstractTypeMap.end() &&
818 "Abstract type not in AbstractTypeMap?");
819
820 // Convert a constant at a time until the last one is gone. The last one
821 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
822 // eliminated eventually.
823 do {
824 ConvertConstantType<ConstantClass,
825 TypeClass>::convert(I->second->second,
826 cast<TypeClass>(NewTy));
827
828 I = AbstractTypeMap.find(cast<TypeClass>(OldTy));
829 } while (I != AbstractTypeMap.end());
830 }
831
832 // If the type became concrete without being refined to any other existing
833 // type, we just remove ourselves from the ATU list.
834 void typeBecameConcrete(const DerivedType *AbsTy) {
835 AbsTy->removeAbstractTypeUser(this);
836 }
837
838 void dump() const {
839 std::cerr << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +0000840 }
841 };
842}
843
Chris Lattner3462ae32001-12-03 22:26:30 +0000844//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000845//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000846static ValueMap< int64_t, Type, ConstantSInt> SIntConstants;
847static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000848
Chris Lattner3462ae32001-12-03 22:26:30 +0000849ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000850 return SIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000851}
852
Chris Lattner3462ae32001-12-03 22:26:30 +0000853ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000854 return UIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000855}
856
Chris Lattner3462ae32001-12-03 22:26:30 +0000857ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000858 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000859 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
860 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000861}
862
Chris Lattner3462ae32001-12-03 22:26:30 +0000863//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000864//
Chris Lattnerac80ea42004-02-01 22:49:04 +0000865namespace llvm {
866 template<>
867 struct ConstantCreator<ConstantFP, Type, uint64_t> {
868 static ConstantFP *create(const Type *Ty, uint64_t V) {
869 assert(Ty == Type::DoubleTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000870 return new ConstantFP(Ty, BitsToDouble(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000871 }
872 };
873 template<>
874 struct ConstantCreator<ConstantFP, Type, uint32_t> {
875 static ConstantFP *create(const Type *Ty, uint32_t V) {
876 assert(Ty == Type::FloatTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000877 return new ConstantFP(Ty, BitsToFloat(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000878 }
879 };
880}
881
882static ValueMap<uint64_t, Type, ConstantFP> DoubleConstants;
883static ValueMap<uint32_t, Type, ConstantFP> FloatConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000884
Jim Laskey8ad8f712005-08-17 20:06:22 +0000885bool ConstantFP::isNullValue() const {
886 return DoubleToBits(Val) == 0;
887}
888
889bool ConstantFP::isExactlyValue(double V) const {
890 return DoubleToBits(V) == DoubleToBits(Val);
891}
892
893
Chris Lattner3462ae32001-12-03 22:26:30 +0000894ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner241ed4c2004-01-23 00:55:21 +0000895 if (Ty == Type::FloatTy) {
896 // Force the value through memory to normalize it.
Jim Laskeyb74c6662005-08-17 19:34:49 +0000897 return FloatConstants.getOrCreate(Ty, FloatToBits(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000898 } else {
899 assert(Ty == Type::DoubleTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000900 return DoubleConstants.getOrCreate(Ty, DoubleToBits(V));
Chris Lattner241ed4c2004-01-23 00:55:21 +0000901 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000902}
903
Chris Lattner9fba3da2004-02-15 05:53:04 +0000904//---- ConstantAggregateZero::get() implementation...
905//
906namespace llvm {
907 // ConstantAggregateZero does not take extra "value" argument...
908 template<class ValType>
909 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
910 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
911 return new ConstantAggregateZero(Ty);
912 }
913 };
914
915 template<>
916 struct ConvertConstantType<ConstantAggregateZero, Type> {
917 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
918 // Make everyone now use a constant of the new type...
919 Constant *New = ConstantAggregateZero::get(NewTy);
920 assert(New != OldC && "Didn't replace constant??");
921 OldC->uncheckedReplaceAllUsesWith(New);
922 OldC->destroyConstant(); // This constant is now dead, destroy it.
923 }
924 };
925}
926
927static ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
928
Chris Lattner3e650af2004-08-04 04:48:01 +0000929static char getValType(ConstantAggregateZero *CPZ) { return 0; }
930
Chris Lattner9fba3da2004-02-15 05:53:04 +0000931Constant *ConstantAggregateZero::get(const Type *Ty) {
Chris Lattnerbfd0b6d2006-06-10 04:16:23 +0000932 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<PackedType>(Ty)) &&
933 "Cannot create an aggregate zero of non-aggregate type!");
Chris Lattner9fba3da2004-02-15 05:53:04 +0000934 return AggZeroConstants.getOrCreate(Ty, 0);
935}
936
937// destroyConstant - Remove the constant from the constant table...
938//
939void ConstantAggregateZero::destroyConstant() {
940 AggZeroConstants.remove(this);
941 destroyConstantImpl();
942}
943
Chris Lattner3462ae32001-12-03 22:26:30 +0000944//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000945//
Chris Lattner189d19f2003-11-21 20:23:48 +0000946namespace llvm {
947 template<>
948 struct ConvertConstantType<ConstantArray, ArrayType> {
949 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
950 // Make everyone now use a constant of the new type...
951 std::vector<Constant*> C;
952 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
953 C.push_back(cast<Constant>(OldC->getOperand(i)));
954 Constant *New = ConstantArray::get(NewTy, C);
955 assert(New != OldC && "Didn't replace constant??");
956 OldC->uncheckedReplaceAllUsesWith(New);
957 OldC->destroyConstant(); // This constant is now dead, destroy it.
958 }
959 };
960}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000961
Chris Lattner3e650af2004-08-04 04:48:01 +0000962static std::vector<Constant*> getValType(ConstantArray *CA) {
963 std::vector<Constant*> Elements;
964 Elements.reserve(CA->getNumOperands());
965 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
966 Elements.push_back(cast<Constant>(CA->getOperand(i)));
967 return Elements;
968}
969
Chris Lattnerb64419a2005-10-03 22:51:37 +0000970typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattner935aa922005-10-04 17:48:46 +0000971 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattnerb64419a2005-10-03 22:51:37 +0000972static ArrayConstantsTy ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000973
Chris Lattner015e8212004-02-15 04:14:47 +0000974Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +0000975 const std::vector<Constant*> &V) {
976 // If this is an all-zero array, return a ConstantAggregateZero object
977 if (!V.empty()) {
978 Constant *C = V[0];
979 if (!C->isNullValue())
980 return ArrayConstants.getOrCreate(Ty, V);
981 for (unsigned i = 1, e = V.size(); i != e; ++i)
982 if (V[i] != C)
983 return ArrayConstants.getOrCreate(Ty, V);
984 }
985 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000986}
987
Chris Lattner98fa07b2003-05-23 20:03:32 +0000988// destroyConstant - Remove the constant from the constant table...
989//
990void ConstantArray::destroyConstant() {
991 ArrayConstants.remove(this);
992 destroyConstantImpl();
993}
994
Reid Spencer6f614532006-05-30 08:23:18 +0000995/// ConstantArray::get(const string&) - Return an array that is initialized to
996/// contain the specified string. If length is zero then a null terminator is
997/// added to the specified string so that it may be used in a natural way.
998/// Otherwise, the length parameter specifies how much of the string to use
999/// and it won't be null terminated.
1000///
Reid Spencer82ebaba2006-05-30 18:15:07 +00001001Constant *ConstantArray::get(const std::string &Str, bool AddNull) {
Chris Lattner7f74a562002-01-20 22:54:45 +00001002 std::vector<Constant*> ElementVals;
Reid Spencer82ebaba2006-05-30 18:15:07 +00001003 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +00001004 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +00001005
1006 // Add a null terminator to the string...
Reid Spencer82ebaba2006-05-30 18:15:07 +00001007 if (AddNull) {
Reid Spencer6f614532006-05-30 08:23:18 +00001008 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Reid Spencer6f614532006-05-30 08:23:18 +00001009 }
Chris Lattner8f80fe02001-10-14 23:54:12 +00001010
Reid Spencer82ebaba2006-05-30 18:15:07 +00001011 ArrayType *ATy = ArrayType::get(Type::SByteTy, ElementVals.size());
Chris Lattner3462ae32001-12-03 22:26:30 +00001012 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +00001013}
1014
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001015/// isString - This method returns true if the array is an array of sbyte or
1016/// ubyte, and if the elements of the array are all ConstantInt's.
1017bool ConstantArray::isString() const {
1018 // Check the element type for sbyte or ubyte...
Chris Lattnere8701f62004-01-14 17:51:53 +00001019 if (getType()->getElementType() != Type::UByteTy &&
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001020 getType()->getElementType() != Type::SByteTy)
1021 return false;
1022 // Check the elements to make sure they are all integers, not constant
1023 // expressions.
1024 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1025 if (!isa<ConstantInt>(getOperand(i)))
1026 return false;
1027 return true;
1028}
1029
Chris Lattner81fabb02002-08-26 17:53:56 +00001030// getAsString - If the sub-element type of this array is either sbyte or ubyte,
1031// then this method converts the array to an std::string and returns it.
1032// Otherwise, it asserts out.
1033//
1034std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001035 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001036 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +00001037 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1038 Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue();
Chris Lattner81fabb02002-08-26 17:53:56 +00001039 return Result;
1040}
1041
1042
Chris Lattner3462ae32001-12-03 22:26:30 +00001043//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001044//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001045
Chris Lattner189d19f2003-11-21 20:23:48 +00001046namespace llvm {
1047 template<>
1048 struct ConvertConstantType<ConstantStruct, StructType> {
1049 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1050 // Make everyone now use a constant of the new type...
1051 std::vector<Constant*> C;
1052 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1053 C.push_back(cast<Constant>(OldC->getOperand(i)));
1054 Constant *New = ConstantStruct::get(NewTy, C);
1055 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001056
Chris Lattner189d19f2003-11-21 20:23:48 +00001057 OldC->uncheckedReplaceAllUsesWith(New);
1058 OldC->destroyConstant(); // This constant is now dead, destroy it.
1059 }
1060 };
1061}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001062
Chris Lattner8760ec72005-10-04 01:17:50 +00001063typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattner935aa922005-10-04 17:48:46 +00001064 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner8760ec72005-10-04 01:17:50 +00001065static StructConstantsTy StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001066
Chris Lattner3e650af2004-08-04 04:48:01 +00001067static std::vector<Constant*> getValType(ConstantStruct *CS) {
1068 std::vector<Constant*> Elements;
1069 Elements.reserve(CS->getNumOperands());
1070 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1071 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1072 return Elements;
1073}
1074
Chris Lattner015e8212004-02-15 04:14:47 +00001075Constant *ConstantStruct::get(const StructType *Ty,
1076 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001077 // Create a ConstantAggregateZero value if all elements are zeros...
1078 for (unsigned i = 0, e = V.size(); i != e; ++i)
1079 if (!V[i]->isNullValue())
1080 return StructConstants.getOrCreate(Ty, V);
1081
1082 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001083}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001084
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001085Constant *ConstantStruct::get(const std::vector<Constant*> &V) {
1086 std::vector<const Type*> StructEls;
1087 StructEls.reserve(V.size());
1088 for (unsigned i = 0, e = V.size(); i != e; ++i)
1089 StructEls.push_back(V[i]->getType());
1090 return get(StructType::get(StructEls), V);
1091}
1092
Chris Lattnerd7a73302001-10-13 06:57:33 +00001093// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001094//
Chris Lattner3462ae32001-12-03 22:26:30 +00001095void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +00001096 StructConstants.remove(this);
1097 destroyConstantImpl();
1098}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001099
Brian Gaeke02209042004-08-20 06:00:58 +00001100//---- ConstantPacked::get() implementation...
1101//
1102namespace llvm {
1103 template<>
1104 struct ConvertConstantType<ConstantPacked, PackedType> {
1105 static void convert(ConstantPacked *OldC, const PackedType *NewTy) {
1106 // Make everyone now use a constant of the new type...
1107 std::vector<Constant*> C;
1108 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1109 C.push_back(cast<Constant>(OldC->getOperand(i)));
1110 Constant *New = ConstantPacked::get(NewTy, C);
1111 assert(New != OldC && "Didn't replace constant??");
1112 OldC->uncheckedReplaceAllUsesWith(New);
1113 OldC->destroyConstant(); // This constant is now dead, destroy it.
1114 }
1115 };
1116}
1117
1118static std::vector<Constant*> getValType(ConstantPacked *CP) {
1119 std::vector<Constant*> Elements;
1120 Elements.reserve(CP->getNumOperands());
1121 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1122 Elements.push_back(CP->getOperand(i));
1123 return Elements;
1124}
1125
1126static ValueMap<std::vector<Constant*>, PackedType,
1127 ConstantPacked> PackedConstants;
1128
1129Constant *ConstantPacked::get(const PackedType *Ty,
1130 const std::vector<Constant*> &V) {
1131 // If this is an all-zero packed, return a ConstantAggregateZero object
1132 if (!V.empty()) {
1133 Constant *C = V[0];
1134 if (!C->isNullValue())
1135 return PackedConstants.getOrCreate(Ty, V);
1136 for (unsigned i = 1, e = V.size(); i != e; ++i)
1137 if (V[i] != C)
1138 return PackedConstants.getOrCreate(Ty, V);
1139 }
1140 return ConstantAggregateZero::get(Ty);
1141}
1142
1143Constant *ConstantPacked::get(const std::vector<Constant*> &V) {
1144 assert(!V.empty() && "Cannot infer type if V is empty");
1145 return get(PackedType::get(V.front()->getType(),V.size()), V);
1146}
1147
1148// destroyConstant - Remove the constant from the constant table...
1149//
1150void ConstantPacked::destroyConstant() {
1151 PackedConstants.remove(this);
1152 destroyConstantImpl();
1153}
1154
Chris Lattner3462ae32001-12-03 22:26:30 +00001155//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001156//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001157
Chris Lattner189d19f2003-11-21 20:23:48 +00001158namespace llvm {
1159 // ConstantPointerNull does not take extra "value" argument...
1160 template<class ValType>
1161 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1162 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1163 return new ConstantPointerNull(Ty);
1164 }
1165 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001166
Chris Lattner189d19f2003-11-21 20:23:48 +00001167 template<>
1168 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1169 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1170 // Make everyone now use a constant of the new type...
1171 Constant *New = ConstantPointerNull::get(NewTy);
1172 assert(New != OldC && "Didn't replace constant??");
1173 OldC->uncheckedReplaceAllUsesWith(New);
1174 OldC->destroyConstant(); // This constant is now dead, destroy it.
1175 }
1176 };
1177}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001178
Chris Lattner98fa07b2003-05-23 20:03:32 +00001179static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001180
Chris Lattner3e650af2004-08-04 04:48:01 +00001181static char getValType(ConstantPointerNull *) {
1182 return 0;
1183}
1184
1185
Chris Lattner3462ae32001-12-03 22:26:30 +00001186ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner98fa07b2003-05-23 20:03:32 +00001187 return NullPtrConstants.getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001188}
1189
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001190// destroyConstant - Remove the constant from the constant table...
1191//
1192void ConstantPointerNull::destroyConstant() {
1193 NullPtrConstants.remove(this);
1194 destroyConstantImpl();
1195}
1196
1197
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001198//---- UndefValue::get() implementation...
1199//
1200
1201namespace llvm {
1202 // UndefValue does not take extra "value" argument...
1203 template<class ValType>
1204 struct ConstantCreator<UndefValue, Type, ValType> {
1205 static UndefValue *create(const Type *Ty, const ValType &V) {
1206 return new UndefValue(Ty);
1207 }
1208 };
1209
1210 template<>
1211 struct ConvertConstantType<UndefValue, Type> {
1212 static void convert(UndefValue *OldC, const Type *NewTy) {
1213 // Make everyone now use a constant of the new type.
1214 Constant *New = UndefValue::get(NewTy);
1215 assert(New != OldC && "Didn't replace constant??");
1216 OldC->uncheckedReplaceAllUsesWith(New);
1217 OldC->destroyConstant(); // This constant is now dead, destroy it.
1218 }
1219 };
1220}
1221
1222static ValueMap<char, Type, UndefValue> UndefValueConstants;
1223
1224static char getValType(UndefValue *) {
1225 return 0;
1226}
1227
1228
1229UndefValue *UndefValue::get(const Type *Ty) {
1230 return UndefValueConstants.getOrCreate(Ty, 0);
1231}
1232
1233// destroyConstant - Remove the constant from the constant table.
1234//
1235void UndefValue::destroyConstant() {
1236 UndefValueConstants.remove(this);
1237 destroyConstantImpl();
1238}
1239
1240
1241
1242
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001243//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001244//
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001245typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Chris Lattner98fa07b2003-05-23 20:03:32 +00001246
Chris Lattner189d19f2003-11-21 20:23:48 +00001247namespace llvm {
1248 template<>
1249 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
1250 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
1251 if (V.first == Instruction::Cast)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001252 return new UnaryConstantExpr(Instruction::Cast, V.second[0], Ty);
Chris Lattner189d19f2003-11-21 20:23:48 +00001253 if ((V.first >= Instruction::BinaryOpsBegin &&
1254 V.first < Instruction::BinaryOpsEnd) ||
1255 V.first == Instruction::Shl || V.first == Instruction::Shr)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001256 return new BinaryConstantExpr(V.first, V.second[0], V.second[1]);
Chris Lattnerb7897ce2004-03-30 22:51:03 +00001257 if (V.first == Instruction::Select)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001258 return new SelectConstantExpr(V.second[0], V.second[1], V.second[2]);
Robert Bocchino23004482006-01-10 19:05:34 +00001259 if (V.first == Instruction::ExtractElement)
1260 return new ExtractElementConstantExpr(V.second[0], V.second[1]);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001261 if (V.first == Instruction::InsertElement)
1262 return new InsertElementConstantExpr(V.second[0], V.second[1],
1263 V.second[2]);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001264 if (V.first == Instruction::ShuffleVector)
1265 return new ShuffleVectorConstantExpr(V.second[0], V.second[1],
1266 V.second[2]);
1267
Chris Lattner189d19f2003-11-21 20:23:48 +00001268 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001269
Chris Lattner189d19f2003-11-21 20:23:48 +00001270 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001271 return new GetElementPtrConstantExpr(V.second[0], IdxList, Ty);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001272 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001273 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001274
Chris Lattner189d19f2003-11-21 20:23:48 +00001275 template<>
1276 struct ConvertConstantType<ConstantExpr, Type> {
1277 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1278 Constant *New;
1279 switch (OldC->getOpcode()) {
1280 case Instruction::Cast:
1281 New = ConstantExpr::getCast(OldC->getOperand(0), NewTy);
1282 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001283 case Instruction::Select:
1284 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1285 OldC->getOperand(1),
1286 OldC->getOperand(2));
1287 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001288 case Instruction::Shl:
1289 case Instruction::Shr:
1290 New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
1291 OldC->getOperand(0), OldC->getOperand(1));
1292 break;
1293 default:
1294 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
1295 OldC->getOpcode() < Instruction::BinaryOpsEnd);
1296 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1297 OldC->getOperand(1));
1298 break;
1299 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001300 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001301 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
1302 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), Idx);
Chris Lattner189d19f2003-11-21 20:23:48 +00001303 break;
1304 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001305
Chris Lattner189d19f2003-11-21 20:23:48 +00001306 assert(New != OldC && "Didn't replace constant??");
1307 OldC->uncheckedReplaceAllUsesWith(New);
1308 OldC->destroyConstant(); // This constant is now dead, destroy it.
1309 }
1310 };
1311} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001312
1313
Chris Lattner3e650af2004-08-04 04:48:01 +00001314static ExprMapKeyType getValType(ConstantExpr *CE) {
1315 std::vector<Constant*> Operands;
1316 Operands.reserve(CE->getNumOperands());
1317 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1318 Operands.push_back(cast<Constant>(CE->getOperand(i)));
1319 return ExprMapKeyType(CE->getOpcode(), Operands);
1320}
1321
Chris Lattner98fa07b2003-05-23 20:03:32 +00001322static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001323
Chris Lattner46b3d302003-04-16 22:40:51 +00001324Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001325 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1326
Chris Lattneracdbe712003-04-17 19:24:48 +00001327 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
1328 return FC; // Fold a few common cases...
1329
Vikram S. Adve4c485332002-07-15 18:19:33 +00001330 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001331 std::vector<Constant*> argVec(1, C);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001332 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
1333 return ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001334}
Chris Lattnerd7a73302001-10-13 06:57:33 +00001335
Chris Lattnerdd284742004-04-04 23:20:30 +00001336Constant *ConstantExpr::getSignExtend(Constant *C, const Type *Ty) {
Chris Lattner1ece6f82005-01-01 15:59:57 +00001337 assert(C->getType()->isIntegral() && Ty->isIntegral() &&
Chris Lattnerdd284742004-04-04 23:20:30 +00001338 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1339 "This is an illegal sign extension!");
Chris Lattner1ece6f82005-01-01 15:59:57 +00001340 if (C->getType() != Type::BoolTy) {
1341 C = ConstantExpr::getCast(C, C->getType()->getSignedVersion());
1342 return ConstantExpr::getCast(C, Ty);
1343 } else {
1344 if (C == ConstantBool::True)
1345 return ConstantIntegral::getAllOnesValue(Ty);
1346 else
1347 return ConstantIntegral::getNullValue(Ty);
1348 }
Chris Lattnerdd284742004-04-04 23:20:30 +00001349}
1350
1351Constant *ConstantExpr::getZeroExtend(Constant *C, const Type *Ty) {
Chris Lattner1ece6f82005-01-01 15:59:57 +00001352 assert(C->getType()->isIntegral() && Ty->isIntegral() &&
Chris Lattnerdd284742004-04-04 23:20:30 +00001353 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1354 "This is an illegal zero extension!");
Chris Lattner1ece6f82005-01-01 15:59:57 +00001355 if (C->getType() != Type::BoolTy)
1356 C = ConstantExpr::getCast(C, C->getType()->getUnsignedVersion());
Chris Lattnerdd284742004-04-04 23:20:30 +00001357 return ConstantExpr::getCast(C, Ty);
1358}
1359
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001360Constant *ConstantExpr::getSizeOf(const Type *Ty) {
Chris Lattneracc4e542004-12-13 19:48:51 +00001361 // sizeof is implemented as: (ulong) gep (Ty*)null, 1
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001362 return getCast(
Chris Lattneracc4e542004-12-13 19:48:51 +00001363 getGetElementPtr(getNullValue(PointerType::get(Ty)),
1364 std::vector<Constant*>(1, ConstantInt::get(Type::UIntTy, 1))),
1365 Type::ULongTy);
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001366}
1367
Alkis Evlogimenos9160d5f2005-03-19 11:40:31 +00001368Constant *ConstantExpr::getPtrPtrFromArrayPtr(Constant *C) {
1369 // pointer from array is implemented as: getelementptr arr ptr, 0, 0
1370 static std::vector<Constant*> Indices(2, ConstantUInt::get(Type::UIntTy, 0));
1371
1372 return ConstantExpr::getGetElementPtr(C, Indices);
1373}
1374
Chris Lattnerb50d1352003-10-05 00:17:43 +00001375Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
1376 Constant *C1, Constant *C2) {
Chris Lattner5645e8a2004-01-12 19:04:55 +00001377 if (Opcode == Instruction::Shl || Opcode == Instruction::Shr)
1378 return getShiftTy(ReqTy, Opcode, C1, C2);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001379 // Check the operands for consistency first
1380 assert((Opcode >= Instruction::BinaryOpsBegin &&
1381 Opcode < Instruction::BinaryOpsEnd) &&
1382 "Invalid opcode in binary constant expression");
1383 assert(C1->getType() == C2->getType() &&
1384 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001385
Chris Lattner29ca2c62004-08-04 18:50:09 +00001386 if (ReqTy == C1->getType() || (Instruction::isRelational(Opcode) &&
1387 ReqTy == Type::BoolTy))
Chris Lattnerb50d1352003-10-05 00:17:43 +00001388 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1389 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001390
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001391 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);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001394}
1395
Chris Lattner29ca2c62004-08-04 18:50:09 +00001396Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001397#ifndef NDEBUG
1398 switch (Opcode) {
1399 case Instruction::Add: case Instruction::Sub:
1400 case Instruction::Mul: case Instruction::Div:
1401 case Instruction::Rem:
1402 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattnerc421a262006-01-04 01:01:04 +00001403 assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint() ||
1404 isa<PackedType>(C1->getType())) &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001405 "Tried to create an arithmetic operation on a non-arithmetic type!");
1406 break;
1407 case Instruction::And:
1408 case Instruction::Or:
1409 case Instruction::Xor:
1410 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattnerc421a262006-01-04 01:01:04 +00001411 assert((C1->getType()->isIntegral() || isa<PackedType>(C1->getType())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001412 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001413 break;
1414 case Instruction::SetLT: case Instruction::SetGT: case Instruction::SetLE:
1415 case Instruction::SetGE: case Instruction::SetEQ: case Instruction::SetNE:
1416 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1417 break;
1418 case Instruction::Shl:
1419 case Instruction::Shr:
1420 assert(C2->getType() == Type::UByteTy && "Shift should be by ubyte!");
Chris Lattnerc421a262006-01-04 01:01:04 +00001421 assert((C1->getType()->isInteger() || isa<PackedType>(C1->getType())) &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001422 "Tried to create a shift operation on a non-integer type!");
1423 break;
1424 default:
1425 break;
1426 }
1427#endif
1428
Chris Lattner29ca2c62004-08-04 18:50:09 +00001429 if (Instruction::isRelational(Opcode))
1430 return getTy(Type::BoolTy, Opcode, C1, C2);
1431 else
1432 return getTy(C1->getType(), Opcode, C1, C2);
1433}
1434
Chris Lattner6e415c02004-03-12 05:54:04 +00001435Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
1436 Constant *V1, Constant *V2) {
1437 assert(C->getType() == Type::BoolTy && "Select condition must be bool!");
1438 assert(V1->getType() == V2->getType() && "Select value types must match!");
1439 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
1440
1441 if (ReqTy == V1->getType())
1442 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1443 return SC; // Fold common cases
1444
1445 std::vector<Constant*> argVec(3, C);
1446 argVec[1] = V1;
1447 argVec[2] = V2;
1448 ExprMapKeyType Key = std::make_pair(Instruction::Select, argVec);
1449 return ExprConstants.getOrCreate(ReqTy, Key);
1450}
1451
Chris Lattner9eb2b522004-01-12 19:12:58 +00001452/// getShiftTy - Return a shift left or shift right constant expr
Chris Lattnerb50d1352003-10-05 00:17:43 +00001453Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
1454 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001455 // Check the operands for consistency first
1456 assert((Opcode == Instruction::Shl ||
1457 Opcode == Instruction::Shr) &&
1458 "Invalid opcode in binary constant expression");
1459 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
1460 "Invalid operand types for Shift constant expr!");
1461
Chris Lattner0bba7712004-01-12 20:40:42 +00001462 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001463 return FC; // Fold a few common cases...
1464
1465 // Look up the constant in the table first to ensure uniqueness
1466 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001467 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001468 return ExprConstants.getOrCreate(ReqTy, Key);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001469}
1470
1471
Chris Lattnerb50d1352003-10-05 00:17:43 +00001472Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner13128ab2004-10-11 22:52:25 +00001473 const std::vector<Value*> &IdxList) {
1474 assert(GetElementPtrInst::getIndexedType(C->getType(), IdxList, true) &&
Chris Lattner04b60fe2004-02-16 20:46:13 +00001475 "GEP indices invalid!");
1476
Chris Lattneracdbe712003-04-17 19:24:48 +00001477 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
1478 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001479
Chris Lattnerb50d1352003-10-05 00:17:43 +00001480 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001481 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001482 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00001483 std::vector<Constant*> ArgVec;
1484 ArgVec.reserve(IdxList.size()+1);
1485 ArgVec.push_back(C);
1486 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
1487 ArgVec.push_back(cast<Constant>(IdxList[i]));
1488 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,ArgVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001489 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001490}
1491
Chris Lattnerb50d1352003-10-05 00:17:43 +00001492Constant *ConstantExpr::getGetElementPtr(Constant *C,
1493 const std::vector<Constant*> &IdxList){
1494 // Get the result type of the getelementptr!
1495 std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
1496
1497 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
1498 true);
1499 assert(Ty && "GEP indices invalid!");
Chris Lattner13128ab2004-10-11 22:52:25 +00001500 return getGetElementPtrTy(PointerType::get(Ty), C, VIdxList);
1501}
1502
1503Constant *ConstantExpr::getGetElementPtr(Constant *C,
1504 const std::vector<Value*> &IdxList) {
1505 // Get the result type of the getelementptr!
1506 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
1507 true);
1508 assert(Ty && "GEP indices invalid!");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001509 return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
1510}
1511
Robert Bocchino23004482006-01-10 19:05:34 +00001512Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
1513 Constant *Idx) {
Robert Bocchinode7f1c92006-01-10 20:03:46 +00001514 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
1515 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00001516 // Look up the constant in the table first to ensure uniqueness
1517 std::vector<Constant*> ArgVec(1, Val);
1518 ArgVec.push_back(Idx);
1519 const ExprMapKeyType &Key = std::make_pair(Instruction::ExtractElement,ArgVec);
1520 return ExprConstants.getOrCreate(ReqTy, Key);
1521}
1522
1523Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
1524 assert(isa<PackedType>(Val->getType()) &&
1525 "Tried to create extractelement operation on non-packed type!");
1526 assert(Idx->getType() == Type::UIntTy &&
Robert Bocchinoca27f032006-01-17 20:07:22 +00001527 "Extractelement index must be uint type!");
Robert Bocchino23004482006-01-10 19:05:34 +00001528 return getExtractElementTy(cast<PackedType>(Val->getType())->getElementType(),
1529 Val, Idx);
1530}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001531
Robert Bocchinoca27f032006-01-17 20:07:22 +00001532Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
1533 Constant *Elt, Constant *Idx) {
1534 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
1535 return FC; // Fold a few common cases...
1536 // Look up the constant in the table first to ensure uniqueness
1537 std::vector<Constant*> ArgVec(1, Val);
1538 ArgVec.push_back(Elt);
1539 ArgVec.push_back(Idx);
1540 const ExprMapKeyType &Key = std::make_pair(Instruction::InsertElement,ArgVec);
1541 return ExprConstants.getOrCreate(ReqTy, Key);
1542}
1543
1544Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1545 Constant *Idx) {
1546 assert(isa<PackedType>(Val->getType()) &&
1547 "Tried to create insertelement operation on non-packed type!");
1548 assert(Elt->getType() == cast<PackedType>(Val->getType())->getElementType()
1549 && "Insertelement types must match!");
1550 assert(Idx->getType() == Type::UIntTy &&
1551 "Insertelement index must be uint type!");
1552 return getInsertElementTy(cast<PackedType>(Val->getType())->getElementType(),
1553 Val, Elt, Idx);
1554}
1555
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001556Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
1557 Constant *V2, Constant *Mask) {
1558 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
1559 return FC; // Fold a few common cases...
1560 // Look up the constant in the table first to ensure uniqueness
1561 std::vector<Constant*> ArgVec(1, V1);
1562 ArgVec.push_back(V2);
1563 ArgVec.push_back(Mask);
1564 const ExprMapKeyType &Key = std::make_pair(Instruction::ShuffleVector,ArgVec);
1565 return ExprConstants.getOrCreate(ReqTy, Key);
1566}
1567
1568Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
1569 Constant *Mask) {
1570 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
1571 "Invalid shuffle vector constant expr operands!");
1572 return getShuffleVectorTy(V1->getType(), V1, V2, Mask);
1573}
1574
1575
Vikram S. Adve4c485332002-07-15 18:19:33 +00001576// destroyConstant - Remove the constant from the constant table...
1577//
1578void ConstantExpr::destroyConstant() {
1579 ExprConstants.remove(this);
1580 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001581}
1582
Chris Lattner3cd8c562002-07-30 18:54:25 +00001583const char *ConstantExpr::getOpcodeName() const {
1584 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001585}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00001586
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001587//===----------------------------------------------------------------------===//
1588// replaceUsesOfWithOnConstant implementations
1589
1590void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001591 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001592 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00001593 Constant *ToC = cast<Constant>(To);
Chris Lattnerdff59112005-10-04 18:47:09 +00001594
1595 unsigned OperandToUpdate = U-OperandList;
1596 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1597
Chris Lattnerb64419a2005-10-03 22:51:37 +00001598 std::pair<ArrayConstantsTy::MapKey, ConstantArray*> Lookup;
1599 Lookup.first.first = getType();
1600 Lookup.second = this;
Chris Lattnerdff59112005-10-04 18:47:09 +00001601
Chris Lattnerb64419a2005-10-03 22:51:37 +00001602 std::vector<Constant*> &Values = Lookup.first.second;
1603 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattnerdff59112005-10-04 18:47:09 +00001604
Chris Lattner8760ec72005-10-04 01:17:50 +00001605 // Fill values with the modified operands of the constant array. Also,
1606 // compute whether this turns into an all-zeros array.
Chris Lattnerdff59112005-10-04 18:47:09 +00001607 bool isAllZeros = false;
1608 if (!ToC->isNullValue()) {
1609 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1610 Values.push_back(cast<Constant>(O->get()));
1611 } else {
1612 isAllZeros = true;
1613 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1614 Constant *Val = cast<Constant>(O->get());
1615 Values.push_back(Val);
1616 if (isAllZeros) isAllZeros = Val->isNullValue();
1617 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001618 }
Chris Lattnerdff59112005-10-04 18:47:09 +00001619 Values[OperandToUpdate] = ToC;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001620
Chris Lattnerb64419a2005-10-03 22:51:37 +00001621 Constant *Replacement = 0;
1622 if (isAllZeros) {
1623 Replacement = ConstantAggregateZero::get(getType());
1624 } else {
1625 // Check to see if we have this array type already.
1626 bool Exists;
1627 ArrayConstantsTy::MapIterator I =
1628 ArrayConstants.InsertOrGetItem(Lookup, Exists);
1629
1630 if (Exists) {
1631 Replacement = I->second;
1632 } else {
1633 // Okay, the new shape doesn't exist in the system yet. Instead of
1634 // creating a new constant array, inserting it, replaceallusesof'ing the
1635 // old with the new, then deleting the old... just update the current one
1636 // in place!
Chris Lattner3b793c62005-10-04 21:35:50 +00001637 ArrayConstants.MoveConstantToNewSlot(this, I);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001638
Chris Lattnerdff59112005-10-04 18:47:09 +00001639 // Update to the new value.
1640 setOperand(OperandToUpdate, ToC);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001641 return;
1642 }
1643 }
1644
1645 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001646 assert(Replacement != this && "I didn't contain From!");
1647
Chris Lattner7a1450d2005-10-04 18:13:04 +00001648 // Everyone using this now uses the replacement.
1649 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001650
1651 // Delete the old constant!
1652 destroyConstant();
1653}
1654
1655void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001656 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001657 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00001658 Constant *ToC = cast<Constant>(To);
1659
Chris Lattnerdff59112005-10-04 18:47:09 +00001660 unsigned OperandToUpdate = U-OperandList;
1661 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1662
Chris Lattner8760ec72005-10-04 01:17:50 +00001663 std::pair<StructConstantsTy::MapKey, ConstantStruct*> Lookup;
1664 Lookup.first.first = getType();
1665 Lookup.second = this;
1666 std::vector<Constant*> &Values = Lookup.first.second;
1667 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001668
Chris Lattnerdff59112005-10-04 18:47:09 +00001669
Chris Lattner8760ec72005-10-04 01:17:50 +00001670 // Fill values with the modified operands of the constant struct. Also,
1671 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00001672 bool isAllZeros = false;
1673 if (!ToC->isNullValue()) {
1674 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1675 Values.push_back(cast<Constant>(O->get()));
1676 } else {
1677 isAllZeros = true;
1678 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1679 Constant *Val = cast<Constant>(O->get());
1680 Values.push_back(Val);
1681 if (isAllZeros) isAllZeros = Val->isNullValue();
1682 }
Chris Lattner8760ec72005-10-04 01:17:50 +00001683 }
Chris Lattnerdff59112005-10-04 18:47:09 +00001684 Values[OperandToUpdate] = ToC;
1685
Chris Lattner8760ec72005-10-04 01:17:50 +00001686 Constant *Replacement = 0;
1687 if (isAllZeros) {
1688 Replacement = ConstantAggregateZero::get(getType());
1689 } else {
1690 // Check to see if we have this array type already.
1691 bool Exists;
1692 StructConstantsTy::MapIterator I =
Chris Lattner3b793c62005-10-04 21:35:50 +00001693 StructConstants.InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00001694
1695 if (Exists) {
1696 Replacement = I->second;
1697 } else {
1698 // Okay, the new shape doesn't exist in the system yet. Instead of
1699 // creating a new constant struct, inserting it, replaceallusesof'ing the
1700 // old with the new, then deleting the old... just update the current one
1701 // in place!
Chris Lattner3b793c62005-10-04 21:35:50 +00001702 StructConstants.MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00001703
Chris Lattnerdff59112005-10-04 18:47:09 +00001704 // Update to the new value.
1705 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00001706 return;
1707 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001708 }
1709
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001710 assert(Replacement != this && "I didn't contain From!");
1711
Chris Lattner7a1450d2005-10-04 18:13:04 +00001712 // Everyone using this now uses the replacement.
1713 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001714
1715 // Delete the old constant!
1716 destroyConstant();
1717}
1718
1719void ConstantPacked::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001720 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001721 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
1722
1723 std::vector<Constant*> Values;
1724 Values.reserve(getNumOperands()); // Build replacement array...
1725 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1726 Constant *Val = getOperand(i);
1727 if (Val == From) Val = cast<Constant>(To);
1728 Values.push_back(Val);
1729 }
1730
1731 Constant *Replacement = ConstantPacked::get(getType(), Values);
1732 assert(Replacement != this && "I didn't contain From!");
1733
Chris Lattner7a1450d2005-10-04 18:13:04 +00001734 // Everyone using this now uses the replacement.
1735 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001736
1737 // Delete the old constant!
1738 destroyConstant();
1739}
1740
1741void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001742 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001743 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
1744 Constant *To = cast<Constant>(ToV);
1745
1746 Constant *Replacement = 0;
1747 if (getOpcode() == Instruction::GetElementPtr) {
1748 std::vector<Constant*> Indices;
1749 Constant *Pointer = getOperand(0);
1750 Indices.reserve(getNumOperands()-1);
1751 if (Pointer == From) Pointer = To;
1752
1753 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1754 Constant *Val = getOperand(i);
1755 if (Val == From) Val = To;
1756 Indices.push_back(Val);
1757 }
1758 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
1759 } else if (getOpcode() == Instruction::Cast) {
1760 assert(getOperand(0) == From && "Cast only has one use!");
1761 Replacement = ConstantExpr::getCast(To, getType());
1762 } else if (getOpcode() == Instruction::Select) {
1763 Constant *C1 = getOperand(0);
1764 Constant *C2 = getOperand(1);
1765 Constant *C3 = getOperand(2);
1766 if (C1 == From) C1 = To;
1767 if (C2 == From) C2 = To;
1768 if (C3 == From) C3 = To;
1769 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00001770 } else if (getOpcode() == Instruction::ExtractElement) {
1771 Constant *C1 = getOperand(0);
1772 Constant *C2 = getOperand(1);
1773 if (C1 == From) C1 = To;
1774 if (C2 == From) C2 = To;
1775 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00001776 } else if (getOpcode() == Instruction::InsertElement) {
1777 Constant *C1 = getOperand(0);
1778 Constant *C2 = getOperand(1);
1779 Constant *C3 = getOperand(1);
1780 if (C1 == From) C1 = To;
1781 if (C2 == From) C2 = To;
1782 if (C3 == From) C3 = To;
1783 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
1784 } else if (getOpcode() == Instruction::ShuffleVector) {
1785 Constant *C1 = getOperand(0);
1786 Constant *C2 = getOperand(1);
1787 Constant *C3 = getOperand(2);
1788 if (C1 == From) C1 = To;
1789 if (C2 == From) C2 = To;
1790 if (C3 == From) C3 = To;
1791 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001792 } else if (getNumOperands() == 2) {
1793 Constant *C1 = getOperand(0);
1794 Constant *C2 = getOperand(1);
1795 if (C1 == From) C1 = To;
1796 if (C2 == From) C2 = To;
1797 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
1798 } else {
1799 assert(0 && "Unknown ConstantExpr type!");
1800 return;
1801 }
1802
1803 assert(Replacement != this && "I didn't contain From!");
1804
Chris Lattner7a1450d2005-10-04 18:13:04 +00001805 // Everyone using this now uses the replacement.
1806 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001807
1808 // Delete the old constant!
1809 destroyConstant();
1810}
1811
1812
1813
Chris Lattner99a669b2004-11-19 16:39:44 +00001814/// clearAllValueMaps - This method frees all internal memory used by the
1815/// constant subsystem, which can be used in environments where this memory
1816/// is otherwise reported as a leak.
1817void Constant::clearAllValueMaps() {
1818 std::vector<Constant *> Constants;
1819
1820 DoubleConstants.clear(Constants);
1821 FloatConstants.clear(Constants);
1822 SIntConstants.clear(Constants);
1823 UIntConstants.clear(Constants);
1824 AggZeroConstants.clear(Constants);
1825 ArrayConstants.clear(Constants);
1826 StructConstants.clear(Constants);
1827 PackedConstants.clear(Constants);
1828 NullPtrConstants.clear(Constants);
1829 UndefValueConstants.clear(Constants);
1830 ExprConstants.clear(Constants);
1831
Misha Brukmanb1c93172005-04-21 23:48:37 +00001832 for (std::vector<Constant *>::iterator I = Constants.begin(),
Chris Lattner99a669b2004-11-19 16:39:44 +00001833 E = Constants.end(); I != E; ++I)
1834 (*I)->dropAllReferences();
1835 for (std::vector<Constant *>::iterator I = Constants.begin(),
1836 E = Constants.end(); I != E; ++I)
1837 (*I)->destroyConstantImpl();
1838 Constants.clear();
1839}
Jim Laskey2698f0d2006-03-08 18:11:07 +00001840
1841/// getStringValue - Turn an LLVM constant pointer that eventually points to a
1842/// global into a string value. Return an empty string if we can't do it.
Evan Cheng38280c02006-03-10 23:52:03 +00001843/// Parameter Chop determines if the result is chopped at the first null
1844/// terminator.
Jim Laskey2698f0d2006-03-08 18:11:07 +00001845///
Evan Cheng38280c02006-03-10 23:52:03 +00001846std::string Constant::getStringValue(bool Chop, unsigned Offset) {
Jim Laskey2698f0d2006-03-08 18:11:07 +00001847 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(this)) {
1848 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
1849 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
1850 if (Init->isString()) {
1851 std::string Result = Init->getAsString();
1852 if (Offset < Result.size()) {
1853 // If we are pointing INTO The string, erase the beginning...
1854 Result.erase(Result.begin(), Result.begin()+Offset);
1855
1856 // Take off the null terminator, and any string fragments after it.
Evan Cheng38280c02006-03-10 23:52:03 +00001857 if (Chop) {
1858 std::string::size_type NullPos = Result.find_first_of((char)0);
1859 if (NullPos != std::string::npos)
1860 Result.erase(Result.begin()+NullPos, Result.end());
1861 }
Jim Laskey2698f0d2006-03-08 18:11:07 +00001862 return Result;
1863 }
1864 }
1865 }
1866 } else if (Constant *C = dyn_cast<Constant>(this)) {
1867 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
Evan Cheng2c5e5302006-03-11 00:13:10 +00001868 return GV->getStringValue(Chop, Offset);
Jim Laskey2698f0d2006-03-08 18:11:07 +00001869 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
1870 if (CE->getOpcode() == Instruction::GetElementPtr) {
1871 // Turn a gep into the specified offset.
1872 if (CE->getNumOperands() == 3 &&
1873 cast<Constant>(CE->getOperand(1))->isNullValue() &&
1874 isa<ConstantInt>(CE->getOperand(2))) {
1875 Offset += cast<ConstantInt>(CE->getOperand(2))->getRawValue();
Evan Cheng2c5e5302006-03-11 00:13:10 +00001876 return CE->getOperand(0)->getStringValue(Chop, Offset);
Jim Laskey2698f0d2006-03-08 18:11:07 +00001877 }
1878 }
1879 }
1880 }
1881 return "";
1882}
1883