blob: c8a5f356bc032d91cf36a09461feb2bd4fe37061 [file] [log] [blame]
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001//===-- Constants.cpp - Implement Constant nodes --------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner3462ae32001-12-03 22:26:30 +000010// This file implements the Constant* classes...
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerca142372002-04-28 19:55:58 +000014#include "llvm/Constants.h"
Chris Lattner5a945e32004-01-12 21:13:12 +000015#include "ConstantFolding.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000016#include "llvm/DerivedTypes.h"
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000017#include "llvm/GlobalValue.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000018#include "llvm/Instructions.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000019#include "llvm/SymbolTable.h"
Chris Lattnerd7a73302001-10-13 06:57:33 +000020#include "llvm/Module.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000021#include "llvm/ADT/StringExtras.h"
Jim Laskeyb74c6662005-08-17 19:34:49 +000022#include "llvm/Support/MathExtras.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000023#include <algorithm>
Reid Spencercf394bf2004-07-04 11:51:24 +000024#include <iostream>
Chris Lattner189d19f2003-11-21 20:23:48 +000025using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000026
Chris Lattner3462ae32001-12-03 22:26:30 +000027ConstantBool *ConstantBool::True = new ConstantBool(true);
28ConstantBool *ConstantBool::False = new ConstantBool(false);
Chris Lattner2f7c9632001-06-06 20:29:01 +000029
Chris Lattner9655e542001-07-20 19:16:02 +000030
Chris Lattner2f7c9632001-06-06 20:29:01 +000031//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000032// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000033//===----------------------------------------------------------------------===//
34
Chris Lattner3462ae32001-12-03 22:26:30 +000035void Constant::destroyConstantImpl() {
36 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000037 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000038 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +000039 // but they don't know that. Because we only find out when the CPV is
40 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000041 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000042 //
43 while (!use_empty()) {
44 Value *V = use_back();
45#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000046 if (!isa<Constant>(V))
47 std::cerr << "While deleting: " << *this
48 << "\n\nUse still stuck around after Def is destroyed: "
49 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000050#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000051 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000052 Constant *CV = cast<Constant>(V);
53 CV->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +000054
55 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000056 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000057 }
58
59 // Value has no outstanding references it is safe to delete it now...
60 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000061}
Chris Lattner2f7c9632001-06-06 20:29:01 +000062
Chris Lattnerb1585a92002-08-13 17:50:20 +000063// Static constructor to create a '0' constant of arbitrary type...
64Constant *Constant::getNullValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +000065 switch (Ty->getTypeID()) {
Chris Lattner3e88ef92003-10-03 19:34:51 +000066 case Type::BoolTyID: {
67 static Constant *NullBool = ConstantBool::get(false);
68 return NullBool;
69 }
70 case Type::SByteTyID: {
71 static Constant *NullSByte = ConstantSInt::get(Type::SByteTy, 0);
72 return NullSByte;
73 }
74 case Type::UByteTyID: {
75 static Constant *NullUByte = ConstantUInt::get(Type::UByteTy, 0);
76 return NullUByte;
77 }
78 case Type::ShortTyID: {
79 static Constant *NullShort = ConstantSInt::get(Type::ShortTy, 0);
80 return NullShort;
81 }
82 case Type::UShortTyID: {
83 static Constant *NullUShort = ConstantUInt::get(Type::UShortTy, 0);
84 return NullUShort;
85 }
86 case Type::IntTyID: {
87 static Constant *NullInt = ConstantSInt::get(Type::IntTy, 0);
88 return NullInt;
89 }
90 case Type::UIntTyID: {
91 static Constant *NullUInt = ConstantUInt::get(Type::UIntTy, 0);
92 return NullUInt;
93 }
94 case Type::LongTyID: {
95 static Constant *NullLong = ConstantSInt::get(Type::LongTy, 0);
96 return NullLong;
97 }
98 case Type::ULongTyID: {
99 static Constant *NullULong = ConstantUInt::get(Type::ULongTy, 0);
100 return NullULong;
101 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000102
Chris Lattner3e88ef92003-10-03 19:34:51 +0000103 case Type::FloatTyID: {
104 static Constant *NullFloat = ConstantFP::get(Type::FloatTy, 0);
105 return NullFloat;
106 }
107 case Type::DoubleTyID: {
108 static Constant *NullDouble = ConstantFP::get(Type::DoubleTy, 0);
109 return NullDouble;
110 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000111
Misha Brukmanb1c93172005-04-21 23:48:37 +0000112 case Type::PointerTyID:
Chris Lattnerb1585a92002-08-13 17:50:20 +0000113 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner3e88ef92003-10-03 19:34:51 +0000114
Chris Lattner9fba3da2004-02-15 05:53:04 +0000115 case Type::StructTyID:
116 case Type::ArrayTyID:
Brian Gaeke02209042004-08-20 06:00:58 +0000117 case Type::PackedTyID:
Chris Lattner9fba3da2004-02-15 05:53:04 +0000118 return ConstantAggregateZero::get(Ty);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000119 default:
Reid Spencercf394bf2004-07-04 11:51:24 +0000120 // Function, Label, or Opaque type?
121 assert(!"Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000122 return 0;
123 }
124}
125
126// Static constructor to create the maximum constant of an integral type...
127ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000128 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000129 case Type::BoolTyID: return ConstantBool::True;
130 case Type::SByteTyID:
131 case Type::ShortTyID:
132 case Type::IntTyID:
133 case Type::LongTyID: {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000134 // Calculate 011111111111111...
Chris Lattnerb1585a92002-08-13 17:50:20 +0000135 unsigned TypeBits = Ty->getPrimitiveSize()*8;
136 int64_t Val = INT64_MAX; // All ones
137 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
138 return ConstantSInt::get(Ty, Val);
139 }
140
141 case Type::UByteTyID:
142 case Type::UShortTyID:
143 case Type::UIntTyID:
144 case Type::ULongTyID: return getAllOnesValue(Ty);
145
Chris Lattner31408f72002-08-14 17:12:13 +0000146 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000147 }
148}
149
150// Static constructor to create the minimum constant for an integral type...
151ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000152 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000153 case Type::BoolTyID: return ConstantBool::False;
154 case Type::SByteTyID:
155 case Type::ShortTyID:
156 case Type::IntTyID:
157 case Type::LongTyID: {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000158 // Calculate 1111111111000000000000
Chris Lattnerb1585a92002-08-13 17:50:20 +0000159 unsigned TypeBits = Ty->getPrimitiveSize()*8;
160 int64_t Val = -1; // All ones
161 Val <<= TypeBits-1; // Shift over to the right spot
162 return ConstantSInt::get(Ty, Val);
163 }
164
165 case Type::UByteTyID:
166 case Type::UShortTyID:
167 case Type::UIntTyID:
168 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
169
Chris Lattner31408f72002-08-14 17:12:13 +0000170 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000171 }
172}
173
174// Static constructor to create an integral constant with all bits set
175ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000176 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000177 case Type::BoolTyID: return ConstantBool::True;
178 case Type::SByteTyID:
179 case Type::ShortTyID:
180 case Type::IntTyID:
181 case Type::LongTyID: return ConstantSInt::get(Ty, -1);
182
183 case Type::UByteTyID:
184 case Type::UShortTyID:
185 case Type::UIntTyID:
186 case Type::ULongTyID: {
187 // Calculate ~0 of the right type...
188 unsigned TypeBits = Ty->getPrimitiveSize()*8;
189 uint64_t Val = ~0ULL; // All ones
190 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
191 return ConstantUInt::get(Ty, Val);
192 }
Chris Lattner31408f72002-08-14 17:12:13 +0000193 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000194 }
195}
196
Chris Lattner83e5d392003-03-10 22:39:02 +0000197bool ConstantUInt::isAllOnesValue() const {
198 unsigned TypeBits = getType()->getPrimitiveSize()*8;
199 uint64_t Val = ~0ULL; // All ones
200 Val >>= 64-TypeBits; // Shift out inappropriate bits
201 return getValue() == Val;
202}
203
Chris Lattnerb1585a92002-08-13 17:50:20 +0000204
Chris Lattner2f7c9632001-06-06 20:29:01 +0000205//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +0000206// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +0000207//===----------------------------------------------------------------------===//
208
209//===----------------------------------------------------------------------===//
210// Normal Constructors
211
Chris Lattnere7e139e2005-09-27 06:09:08 +0000212ConstantIntegral::ConstantIntegral(const Type *Ty, ValueTy VT, uint64_t V)
213 : Constant(Ty, VT, 0, 0) {
Chris Lattner265eb642004-06-21 12:12:12 +0000214 Val.Unsigned = V;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000215}
Chris Lattner49d855c2001-09-07 16:46:31 +0000216
Chris Lattnere7e139e2005-09-27 06:09:08 +0000217ConstantBool::ConstantBool(bool V)
218 : ConstantIntegral(Type::BoolTy, ConstantBoolVal, V) {
Chris Lattner265eb642004-06-21 12:12:12 +0000219}
220
Chris Lattnere7e139e2005-09-27 06:09:08 +0000221ConstantInt::ConstantInt(const Type *Ty, ValueTy VT, uint64_t V)
222 : ConstantIntegral(Ty, VT, V) {
Chris Lattner7309d662001-07-21 19:16:08 +0000223}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000224
Chris Lattnere7e139e2005-09-27 06:09:08 +0000225ConstantSInt::ConstantSInt(const Type *Ty, int64_t V)
226 : ConstantInt(Ty, ConstantSIntVal, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000227 assert(Ty->isInteger() && Ty->isSigned() &&
Reid Spencerf064bb22005-03-09 15:19:41 +0000228 "Illegal type for signed integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000229 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000230}
231
Chris Lattnere7e139e2005-09-27 06:09:08 +0000232ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V)
233 : ConstantInt(Ty, ConstantUIntVal, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000234 assert(Ty->isInteger() && Ty->isUnsigned() &&
235 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000236 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000237}
238
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000239ConstantFP::ConstantFP(const Type *Ty, double V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000240 : Constant(Ty, ConstantFPVal, 0, 0) {
Chris Lattner9655e542001-07-20 19:16:02 +0000241 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000242 Val = V;
243}
244
Chris Lattner3462ae32001-12-03 22:26:30 +0000245ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000246 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000247 : Constant(T, ConstantArrayVal, new Use[V.size()], V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000248 assert(V.size() == T->getNumElements() &&
249 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000250 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000251 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
252 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000253 Constant *C = *I;
254 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000255 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000256 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000257 "Initializer for array element doesn't match array element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000258 OL->init(C, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000259 }
260}
261
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000262ConstantArray::~ConstantArray() {
263 delete [] OperandList;
264}
265
Chris Lattner3462ae32001-12-03 22:26:30 +0000266ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000267 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000268 : Constant(T, ConstantStructVal, new Use[V.size()], V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000269 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000270 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000271 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000272 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
273 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000274 Constant *C = *I;
275 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000276 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000277 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000278 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000279 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000280 "Initializer for struct element doesn't match struct element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000281 OL->init(C, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000282 }
283}
284
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000285ConstantStruct::~ConstantStruct() {
286 delete [] OperandList;
287}
288
289
Brian Gaeke02209042004-08-20 06:00:58 +0000290ConstantPacked::ConstantPacked(const PackedType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000291 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000292 : Constant(T, ConstantPackedVal, new Use[V.size()], V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000293 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000294 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
295 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000296 Constant *C = *I;
297 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000298 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000299 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000300 "Initializer for packed element doesn't match packed element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000301 OL->init(C, this);
Brian Gaeke02209042004-08-20 06:00:58 +0000302 }
303}
304
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000305ConstantPacked::~ConstantPacked() {
306 delete [] OperandList;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000307}
308
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000309/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
310/// behind the scenes to implement unary constant exprs.
311class UnaryConstantExpr : public ConstantExpr {
312 Use Op;
313public:
314 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
315 : ConstantExpr(Ty, Opcode, &Op, 1), Op(C, this) {}
316};
Chris Lattner6e415c02004-03-12 05:54:04 +0000317
Chris Lattner22ced562003-06-22 20:48:30 +0000318static bool isSetCC(unsigned Opcode) {
319 return Opcode == Instruction::SetEQ || Opcode == Instruction::SetNE ||
320 Opcode == Instruction::SetLT || Opcode == Instruction::SetGT ||
321 Opcode == Instruction::SetLE || Opcode == Instruction::SetGE;
322}
323
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000324/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
325/// behind the scenes to implement binary constant exprs.
326class BinaryConstantExpr : public ConstantExpr {
327 Use Ops[2];
328public:
329 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
330 : ConstantExpr(isSetCC(Opcode) ? Type::BoolTy : C1->getType(),
331 Opcode, Ops, 2) {
332 Ops[0].init(C1, this);
333 Ops[1].init(C2, this);
334 }
335};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000336
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000337/// SelectConstantExpr - This class is private to Constants.cpp, and is used
338/// behind the scenes to implement select constant exprs.
339class SelectConstantExpr : public ConstantExpr {
340 Use Ops[3];
341public:
342 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
343 : ConstantExpr(C2->getType(), Instruction::Select, Ops, 3) {
344 Ops[0].init(C1, this);
345 Ops[1].init(C2, this);
346 Ops[2].init(C3, this);
347 }
348};
349
Robert Bocchinoca27f032006-01-17 20:07:22 +0000350/// ExtractElementConstantExpr - This class is private to
351/// Constants.cpp, and is used behind the scenes to implement
352/// extractelement constant exprs.
Robert Bocchino23004482006-01-10 19:05:34 +0000353class ExtractElementConstantExpr : public ConstantExpr {
354 Use Ops[2];
355public:
356 ExtractElementConstantExpr(Constant *C1, Constant *C2)
357 : ConstantExpr(cast<PackedType>(C1->getType())->getElementType(),
358 Instruction::ExtractElement, Ops, 2) {
359 Ops[0].init(C1, this);
360 Ops[1].init(C2, this);
361 }
362};
363
Robert Bocchinoca27f032006-01-17 20:07:22 +0000364/// InsertElementConstantExpr - This class is private to
365/// Constants.cpp, and is used behind the scenes to implement
366/// insertelement constant exprs.
367class InsertElementConstantExpr : public ConstantExpr {
368 Use Ops[3];
369public:
370 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
371 : ConstantExpr(C1->getType(), Instruction::InsertElement,
372 Ops, 3) {
373 Ops[0].init(C1, this);
374 Ops[1].init(C2, this);
375 Ops[2].init(C3, this);
376 }
377};
378
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000379/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
380/// used behind the scenes to implement getelementpr constant exprs.
381struct GetElementPtrConstantExpr : public ConstantExpr {
382 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
383 const Type *DestTy)
384 : ConstantExpr(DestTy, Instruction::GetElementPtr,
385 new Use[IdxList.size()+1], IdxList.size()+1) {
386 OperandList[0].init(C, this);
387 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
388 OperandList[i+1].init(IdxList[i], this);
389 }
390 ~GetElementPtrConstantExpr() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000391 delete [] OperandList;
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000392 }
393};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000394
Chris Lattner817175f2004-03-29 02:37:53 +0000395/// ConstantExpr::get* - Return some common constants without having to
396/// specify the full Instruction::OPCODE identifier.
397///
398Constant *ConstantExpr::getNeg(Constant *C) {
Chris Lattner3cdc27c2004-03-29 19:51:24 +0000399 if (!C->getType()->isFloatingPoint())
400 return get(Instruction::Sub, getNullValue(C->getType()), C);
401 else
402 return get(Instruction::Sub, ConstantFP::get(C->getType(), -0.0), C);
Chris Lattner817175f2004-03-29 02:37:53 +0000403}
404Constant *ConstantExpr::getNot(Constant *C) {
405 assert(isa<ConstantIntegral>(C) && "Cannot NOT a nonintegral type!");
406 return get(Instruction::Xor, C,
407 ConstantIntegral::getAllOnesValue(C->getType()));
408}
409Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
410 return get(Instruction::Add, C1, C2);
411}
412Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
413 return get(Instruction::Sub, C1, C2);
414}
415Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
416 return get(Instruction::Mul, C1, C2);
417}
418Constant *ConstantExpr::getDiv(Constant *C1, Constant *C2) {
419 return get(Instruction::Div, C1, C2);
420}
421Constant *ConstantExpr::getRem(Constant *C1, Constant *C2) {
422 return get(Instruction::Rem, C1, C2);
423}
424Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
425 return get(Instruction::And, C1, C2);
426}
427Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
428 return get(Instruction::Or, C1, C2);
429}
430Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
431 return get(Instruction::Xor, C1, C2);
432}
433Constant *ConstantExpr::getSetEQ(Constant *C1, Constant *C2) {
434 return get(Instruction::SetEQ, C1, C2);
435}
436Constant *ConstantExpr::getSetNE(Constant *C1, Constant *C2) {
437 return get(Instruction::SetNE, C1, C2);
438}
439Constant *ConstantExpr::getSetLT(Constant *C1, Constant *C2) {
440 return get(Instruction::SetLT, C1, C2);
441}
442Constant *ConstantExpr::getSetGT(Constant *C1, Constant *C2) {
443 return get(Instruction::SetGT, C1, C2);
444}
445Constant *ConstantExpr::getSetLE(Constant *C1, Constant *C2) {
446 return get(Instruction::SetLE, C1, C2);
447}
448Constant *ConstantExpr::getSetGE(Constant *C1, Constant *C2) {
449 return get(Instruction::SetGE, C1, C2);
450}
451Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
452 return get(Instruction::Shl, C1, C2);
453}
454Constant *ConstantExpr::getShr(Constant *C1, Constant *C2) {
455 return get(Instruction::Shr, C1, C2);
456}
457
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000458Constant *ConstantExpr::getUShr(Constant *C1, Constant *C2) {
459 if (C1->getType()->isUnsigned()) return getShr(C1, C2);
460 return getCast(getShr(getCast(C1,
461 C1->getType()->getUnsignedVersion()), C2), C1->getType());
462}
Chris Lattner817175f2004-03-29 02:37:53 +0000463
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000464Constant *ConstantExpr::getSShr(Constant *C1, Constant *C2) {
465 if (C1->getType()->isSigned()) return getShr(C1, C2);
466 return getCast(getShr(getCast(C1,
467 C1->getType()->getSignedVersion()), C2), C1->getType());
468}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000469
Chris Lattner2f7c9632001-06-06 20:29:01 +0000470
471//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000472// isValueValidForType implementations
473
Chris Lattner3462ae32001-12-03 22:26:30 +0000474bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000475 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000476 default:
477 return false; // These can't be represented as integers!!!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000478 // Signed types...
479 case Type::SByteTyID:
480 return (Val <= INT8_MAX && Val >= INT8_MIN);
481 case Type::ShortTyID:
482 return (Val <= INT16_MAX && Val >= INT16_MIN);
483 case Type::IntTyID:
Chris Lattner74248512004-06-08 23:21:39 +0000484 return (Val <= int(INT32_MAX) && Val >= int(INT32_MIN));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000485 case Type::LongTyID:
486 return true; // This is the largest type...
487 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000488}
489
Chris Lattner3462ae32001-12-03 22:26:30 +0000490bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000491 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000492 default:
493 return false; // These can't be represented as integers!!!
494
495 // Unsigned types...
496 case Type::UByteTyID:
497 return (Val <= UINT8_MAX);
498 case Type::UShortTyID:
499 return (Val <= UINT16_MAX);
500 case Type::UIntTyID:
501 return (Val <= UINT32_MAX);
502 case Type::ULongTyID:
503 return true; // This is the largest type...
504 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000505}
506
Chris Lattner3462ae32001-12-03 22:26:30 +0000507bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000508 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000509 default:
510 return false; // These can't be represented as floating point!
511
Reid Spencerb95f8ab2004-12-07 07:38:08 +0000512 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000513 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000514 case Type::DoubleTyID:
515 return true; // This is the largest type...
516 }
517};
Chris Lattner9655e542001-07-20 19:16:02 +0000518
Chris Lattner49d855c2001-09-07 16:46:31 +0000519//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000520// Factory Function Implementation
521
Chris Lattner98fa07b2003-05-23 20:03:32 +0000522// ConstantCreator - A class that is used to create constants by
523// ValueMap*. This class should be partially specialized if there is
524// something strange that needs to be done to interface to the ctor for the
525// constant.
526//
Chris Lattner189d19f2003-11-21 20:23:48 +0000527namespace llvm {
528 template<class ConstantClass, class TypeClass, class ValType>
529 struct ConstantCreator {
530 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
531 return new ConstantClass(Ty, V);
532 }
533 };
Misha Brukmanb1c93172005-04-21 23:48:37 +0000534
Chris Lattner189d19f2003-11-21 20:23:48 +0000535 template<class ConstantClass, class TypeClass>
536 struct ConvertConstantType {
537 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
538 assert(0 && "This type cannot be converted!\n");
539 abort();
540 }
541 };
542}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000543
Chris Lattner98fa07b2003-05-23 20:03:32 +0000544namespace {
Chris Lattner935aa922005-10-04 17:48:46 +0000545 template<class ValType, class TypeClass, class ConstantClass,
546 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattnerb50d1352003-10-05 00:17:43 +0000547 class ValueMap : public AbstractTypeUser {
Chris Lattnerb64419a2005-10-03 22:51:37 +0000548 public:
Chris Lattnerb50d1352003-10-05 00:17:43 +0000549 typedef std::pair<const TypeClass*, ValType> MapKey;
550 typedef std::map<MapKey, ConstantClass *> MapTy;
551 typedef typename MapTy::iterator MapIterator;
Chris Lattnerb64419a2005-10-03 22:51:37 +0000552 private:
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000553 /// Map - This is the main map from the element descriptor to the Constants.
554 /// This is the primary way we avoid creating two of the same shape
555 /// constant.
Chris Lattnerb50d1352003-10-05 00:17:43 +0000556 MapTy Map;
Chris Lattner935aa922005-10-04 17:48:46 +0000557
558 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
559 /// from the constants to their element in Map. This is important for
560 /// removal of constants from the array, which would otherwise have to scan
561 /// through the map with very large keys.
562 std::map<ConstantClass*, MapIterator> InverseMap;
Chris Lattnerb50d1352003-10-05 00:17:43 +0000563
564 typedef std::map<const TypeClass*, MapIterator> AbstractTypeMapTy;
565 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner99a669b2004-11-19 16:39:44 +0000566
567 friend void Constant::clearAllValueMaps();
568 private:
569 void clear(std::vector<Constant *> &Constants) {
570 for(MapIterator I = Map.begin(); I != Map.end(); ++I)
571 Constants.push_back(I->second);
572 Map.clear();
573 AbstractTypeMap.clear();
Chris Lattner935aa922005-10-04 17:48:46 +0000574 InverseMap.clear();
Chris Lattner99a669b2004-11-19 16:39:44 +0000575 }
576
Chris Lattner98fa07b2003-05-23 20:03:32 +0000577 public:
Chris Lattnerb64419a2005-10-03 22:51:37 +0000578 MapIterator map_end() { return Map.end(); }
579
580 /// InsertOrGetItem - Return an iterator for the specified element.
581 /// If the element exists in the map, the returned iterator points to the
582 /// entry and Exists=true. If not, the iterator points to the newly
583 /// inserted entry and returns Exists=false. Newly inserted entries have
584 /// I->second == 0, and should be filled in.
585 MapIterator InsertOrGetItem(std::pair<MapKey, ConstantClass *> &InsertVal,
586 bool &Exists) {
587 std::pair<MapIterator, bool> IP = Map.insert(InsertVal);
588 Exists = !IP.second;
589 return IP.first;
590 }
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000591
Chris Lattner935aa922005-10-04 17:48:46 +0000592private:
593 MapIterator FindExistingElement(ConstantClass *CP) {
594 if (HasLargeKey) {
595 typename std::map<ConstantClass*, MapIterator>::iterator
596 IMI = InverseMap.find(CP);
597 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
598 IMI->second->second == CP &&
599 "InverseMap corrupt!");
600 return IMI->second;
601 }
602
603 MapIterator I =
604 Map.find(MapKey((TypeClass*)CP->getRawType(), getValType(CP)));
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000605 if (I == Map.end() || I->second != CP) {
606 // FIXME: This should not use a linear scan. If this gets to be a
607 // performance problem, someone should look at this.
608 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
609 /* empty */;
610 }
Chris Lattner935aa922005-10-04 17:48:46 +0000611 return I;
612 }
613public:
614
Chris Lattnerb64419a2005-10-03 22:51:37 +0000615 /// getOrCreate - Return the specified constant from the map, creating it if
616 /// necessary.
Chris Lattner98fa07b2003-05-23 20:03:32 +0000617 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000618 MapKey Lookup(Ty, V);
619 MapIterator I = Map.lower_bound(Lookup);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000620 if (I != Map.end() && I->first == Lookup)
621 return I->second; // Is it in the map?
622
623 // If no preexisting value, create one now...
624 ConstantClass *Result =
625 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
626
Chris Lattnerb50d1352003-10-05 00:17:43 +0000627 /// FIXME: why does this assert fail when loading 176.gcc?
628 //assert(Result->getType() == Ty && "Type specified is not correct!");
629 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
630
Chris Lattner935aa922005-10-04 17:48:46 +0000631 if (HasLargeKey) // Remember the reverse mapping if needed.
632 InverseMap.insert(std::make_pair(Result, I));
633
Chris Lattnerb50d1352003-10-05 00:17:43 +0000634 // If the type of the constant is abstract, make sure that an entry exists
635 // for it in the AbstractTypeMap.
636 if (Ty->isAbstract()) {
637 typename AbstractTypeMapTy::iterator TI =
638 AbstractTypeMap.lower_bound(Ty);
639
640 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
641 // Add ourselves to the ATU list of the type.
642 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
643
644 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
645 }
646 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000647 return Result;
648 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000649
Chris Lattner98fa07b2003-05-23 20:03:32 +0000650 void remove(ConstantClass *CP) {
Chris Lattner935aa922005-10-04 17:48:46 +0000651 MapIterator I = FindExistingElement(CP);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000652 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +0000653 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +0000654
Chris Lattner935aa922005-10-04 17:48:46 +0000655 if (HasLargeKey) // Remember the reverse mapping if needed.
656 InverseMap.erase(CP);
657
Chris Lattnerb50d1352003-10-05 00:17:43 +0000658 // Now that we found the entry, make sure this isn't the entry that
659 // the AbstractTypeMap points to.
660 const TypeClass *Ty = I->first.first;
661 if (Ty->isAbstract()) {
662 assert(AbstractTypeMap.count(Ty) &&
663 "Abstract type not in AbstractTypeMap?");
664 MapIterator &ATMEntryIt = AbstractTypeMap[Ty];
665 if (ATMEntryIt == I) {
666 // Yes, we are removing the representative entry for this type.
667 // See if there are any other entries of the same type.
668 MapIterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000669
Chris Lattnerb50d1352003-10-05 00:17:43 +0000670 // First check the entry before this one...
671 if (TmpIt != Map.begin()) {
672 --TmpIt;
673 if (TmpIt->first.first != Ty) // Not the same type, move back...
674 ++TmpIt;
675 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000676
Chris Lattnerb50d1352003-10-05 00:17:43 +0000677 // If we didn't find the same type, try to move forward...
678 if (TmpIt == ATMEntryIt) {
679 ++TmpIt;
680 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
681 --TmpIt; // No entry afterwards with the same type
682 }
683
684 // If there is another entry in the map of the same abstract type,
685 // update the AbstractTypeMap entry now.
686 if (TmpIt != ATMEntryIt) {
687 ATMEntryIt = TmpIt;
688 } else {
689 // Otherwise, we are removing the last instance of this type
690 // from the table. Remove from the ATM, and from user list.
691 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
692 AbstractTypeMap.erase(Ty);
693 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000694 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000695 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000696
Chris Lattnerb50d1352003-10-05 00:17:43 +0000697 Map.erase(I);
698 }
699
Chris Lattner3b793c62005-10-04 21:35:50 +0000700
701 /// MoveConstantToNewSlot - If we are about to change C to be the element
702 /// specified by I, update our internal data structures to reflect this
703 /// fact.
704 void MoveConstantToNewSlot(ConstantClass *C, MapIterator I) {
705 // First, remove the old location of the specified constant in the map.
706 MapIterator OldI = FindExistingElement(C);
707 assert(OldI != Map.end() && "Constant not found in constant table!");
708 assert(OldI->second == C && "Didn't find correct element?");
709
710 // If this constant is the representative element for its abstract type,
711 // update the AbstractTypeMap so that the representative element is I.
712 if (C->getType()->isAbstract()) {
713 typename AbstractTypeMapTy::iterator ATI =
714 AbstractTypeMap.find(C->getType());
715 assert(ATI != AbstractTypeMap.end() &&
716 "Abstract type not in AbstractTypeMap?");
717 if (ATI->second == OldI)
718 ATI->second = I;
719 }
720
721 // Remove the old entry from the map.
722 Map.erase(OldI);
723
724 // Update the inverse map so that we know that this constant is now
725 // located at descriptor I.
726 if (HasLargeKey) {
727 assert(I->second == C && "Bad inversemap entry!");
728 InverseMap[C] = I;
729 }
730 }
731
Chris Lattnerb50d1352003-10-05 00:17:43 +0000732 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000733 typename AbstractTypeMapTy::iterator I =
Chris Lattnerb50d1352003-10-05 00:17:43 +0000734 AbstractTypeMap.find(cast<TypeClass>(OldTy));
735
736 assert(I != AbstractTypeMap.end() &&
737 "Abstract type not in AbstractTypeMap?");
738
739 // Convert a constant at a time until the last one is gone. The last one
740 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
741 // eliminated eventually.
742 do {
743 ConvertConstantType<ConstantClass,
744 TypeClass>::convert(I->second->second,
745 cast<TypeClass>(NewTy));
746
747 I = AbstractTypeMap.find(cast<TypeClass>(OldTy));
748 } while (I != AbstractTypeMap.end());
749 }
750
751 // If the type became concrete without being refined to any other existing
752 // type, we just remove ourselves from the ATU list.
753 void typeBecameConcrete(const DerivedType *AbsTy) {
754 AbsTy->removeAbstractTypeUser(this);
755 }
756
757 void dump() const {
758 std::cerr << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +0000759 }
760 };
761}
762
Chris Lattner3462ae32001-12-03 22:26:30 +0000763//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000764//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000765static ValueMap< int64_t, Type, ConstantSInt> SIntConstants;
766static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000767
Chris Lattner3462ae32001-12-03 22:26:30 +0000768ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000769 return SIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000770}
771
Chris Lattner3462ae32001-12-03 22:26:30 +0000772ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000773 return UIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000774}
775
Chris Lattner3462ae32001-12-03 22:26:30 +0000776ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000777 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000778 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
779 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000780}
781
Chris Lattner3462ae32001-12-03 22:26:30 +0000782//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000783//
Chris Lattnerac80ea42004-02-01 22:49:04 +0000784namespace llvm {
785 template<>
786 struct ConstantCreator<ConstantFP, Type, uint64_t> {
787 static ConstantFP *create(const Type *Ty, uint64_t V) {
788 assert(Ty == Type::DoubleTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000789 return new ConstantFP(Ty, BitsToDouble(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000790 }
791 };
792 template<>
793 struct ConstantCreator<ConstantFP, Type, uint32_t> {
794 static ConstantFP *create(const Type *Ty, uint32_t V) {
795 assert(Ty == Type::FloatTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000796 return new ConstantFP(Ty, BitsToFloat(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000797 }
798 };
799}
800
801static ValueMap<uint64_t, Type, ConstantFP> DoubleConstants;
802static ValueMap<uint32_t, Type, ConstantFP> FloatConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000803
Jim Laskey8ad8f712005-08-17 20:06:22 +0000804bool ConstantFP::isNullValue() const {
805 return DoubleToBits(Val) == 0;
806}
807
808bool ConstantFP::isExactlyValue(double V) const {
809 return DoubleToBits(V) == DoubleToBits(Val);
810}
811
812
Chris Lattner3462ae32001-12-03 22:26:30 +0000813ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner241ed4c2004-01-23 00:55:21 +0000814 if (Ty == Type::FloatTy) {
815 // Force the value through memory to normalize it.
Jim Laskeyb74c6662005-08-17 19:34:49 +0000816 return FloatConstants.getOrCreate(Ty, FloatToBits(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000817 } else {
818 assert(Ty == Type::DoubleTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000819 return DoubleConstants.getOrCreate(Ty, DoubleToBits(V));
Chris Lattner241ed4c2004-01-23 00:55:21 +0000820 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000821}
822
Chris Lattner9fba3da2004-02-15 05:53:04 +0000823//---- ConstantAggregateZero::get() implementation...
824//
825namespace llvm {
826 // ConstantAggregateZero does not take extra "value" argument...
827 template<class ValType>
828 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
829 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
830 return new ConstantAggregateZero(Ty);
831 }
832 };
833
834 template<>
835 struct ConvertConstantType<ConstantAggregateZero, Type> {
836 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
837 // Make everyone now use a constant of the new type...
838 Constant *New = ConstantAggregateZero::get(NewTy);
839 assert(New != OldC && "Didn't replace constant??");
840 OldC->uncheckedReplaceAllUsesWith(New);
841 OldC->destroyConstant(); // This constant is now dead, destroy it.
842 }
843 };
844}
845
846static ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
847
Chris Lattner3e650af2004-08-04 04:48:01 +0000848static char getValType(ConstantAggregateZero *CPZ) { return 0; }
849
Chris Lattner9fba3da2004-02-15 05:53:04 +0000850Constant *ConstantAggregateZero::get(const Type *Ty) {
851 return AggZeroConstants.getOrCreate(Ty, 0);
852}
853
854// destroyConstant - Remove the constant from the constant table...
855//
856void ConstantAggregateZero::destroyConstant() {
857 AggZeroConstants.remove(this);
858 destroyConstantImpl();
859}
860
Chris Lattner3462ae32001-12-03 22:26:30 +0000861//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000862//
Chris Lattner189d19f2003-11-21 20:23:48 +0000863namespace llvm {
864 template<>
865 struct ConvertConstantType<ConstantArray, ArrayType> {
866 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
867 // Make everyone now use a constant of the new type...
868 std::vector<Constant*> C;
869 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
870 C.push_back(cast<Constant>(OldC->getOperand(i)));
871 Constant *New = ConstantArray::get(NewTy, C);
872 assert(New != OldC && "Didn't replace constant??");
873 OldC->uncheckedReplaceAllUsesWith(New);
874 OldC->destroyConstant(); // This constant is now dead, destroy it.
875 }
876 };
877}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000878
Chris Lattner3e650af2004-08-04 04:48:01 +0000879static std::vector<Constant*> getValType(ConstantArray *CA) {
880 std::vector<Constant*> Elements;
881 Elements.reserve(CA->getNumOperands());
882 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
883 Elements.push_back(cast<Constant>(CA->getOperand(i)));
884 return Elements;
885}
886
Chris Lattnerb64419a2005-10-03 22:51:37 +0000887typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattner935aa922005-10-04 17:48:46 +0000888 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattnerb64419a2005-10-03 22:51:37 +0000889static ArrayConstantsTy ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000890
Chris Lattner015e8212004-02-15 04:14:47 +0000891Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +0000892 const std::vector<Constant*> &V) {
893 // If this is an all-zero array, return a ConstantAggregateZero object
894 if (!V.empty()) {
895 Constant *C = V[0];
896 if (!C->isNullValue())
897 return ArrayConstants.getOrCreate(Ty, V);
898 for (unsigned i = 1, e = V.size(); i != e; ++i)
899 if (V[i] != C)
900 return ArrayConstants.getOrCreate(Ty, V);
901 }
902 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000903}
904
Chris Lattner98fa07b2003-05-23 20:03:32 +0000905// destroyConstant - Remove the constant from the constant table...
906//
907void ConstantArray::destroyConstant() {
908 ArrayConstants.remove(this);
909 destroyConstantImpl();
910}
911
Chris Lattner3462ae32001-12-03 22:26:30 +0000912// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000913// contain the specified string. A null terminator is added to the specified
914// string so that it may be used in a natural way...
915//
Chris Lattner015e8212004-02-15 04:14:47 +0000916Constant *ConstantArray::get(const std::string &Str) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000917 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000918
919 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000920 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000921
922 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000923 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000924
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000925 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000926 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000927}
928
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000929/// isString - This method returns true if the array is an array of sbyte or
930/// ubyte, and if the elements of the array are all ConstantInt's.
931bool ConstantArray::isString() const {
932 // Check the element type for sbyte or ubyte...
Chris Lattnere8701f62004-01-14 17:51:53 +0000933 if (getType()->getElementType() != Type::UByteTy &&
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000934 getType()->getElementType() != Type::SByteTy)
935 return false;
936 // Check the elements to make sure they are all integers, not constant
937 // expressions.
938 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
939 if (!isa<ConstantInt>(getOperand(i)))
940 return false;
941 return true;
942}
943
Chris Lattner81fabb02002-08-26 17:53:56 +0000944// getAsString - If the sub-element type of this array is either sbyte or ubyte,
945// then this method converts the array to an std::string and returns it.
946// Otherwise, it asserts out.
947//
948std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000949 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +0000950 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +0000951 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
952 Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue();
Chris Lattner81fabb02002-08-26 17:53:56 +0000953 return Result;
954}
955
956
Chris Lattner3462ae32001-12-03 22:26:30 +0000957//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000958//
Chris Lattnerb50d1352003-10-05 00:17:43 +0000959
Chris Lattner189d19f2003-11-21 20:23:48 +0000960namespace llvm {
961 template<>
962 struct ConvertConstantType<ConstantStruct, StructType> {
963 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
964 // Make everyone now use a constant of the new type...
965 std::vector<Constant*> C;
966 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
967 C.push_back(cast<Constant>(OldC->getOperand(i)));
968 Constant *New = ConstantStruct::get(NewTy, C);
969 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000970
Chris Lattner189d19f2003-11-21 20:23:48 +0000971 OldC->uncheckedReplaceAllUsesWith(New);
972 OldC->destroyConstant(); // This constant is now dead, destroy it.
973 }
974 };
975}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000976
Chris Lattner8760ec72005-10-04 01:17:50 +0000977typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattner935aa922005-10-04 17:48:46 +0000978 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner8760ec72005-10-04 01:17:50 +0000979static StructConstantsTy StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000980
Chris Lattner3e650af2004-08-04 04:48:01 +0000981static std::vector<Constant*> getValType(ConstantStruct *CS) {
982 std::vector<Constant*> Elements;
983 Elements.reserve(CS->getNumOperands());
984 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
985 Elements.push_back(cast<Constant>(CS->getOperand(i)));
986 return Elements;
987}
988
Chris Lattner015e8212004-02-15 04:14:47 +0000989Constant *ConstantStruct::get(const StructType *Ty,
990 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +0000991 // Create a ConstantAggregateZero value if all elements are zeros...
992 for (unsigned i = 0, e = V.size(); i != e; ++i)
993 if (!V[i]->isNullValue())
994 return StructConstants.getOrCreate(Ty, V);
995
996 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000997}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000998
Chris Lattnerd6108ca2004-07-12 20:35:11 +0000999Constant *ConstantStruct::get(const std::vector<Constant*> &V) {
1000 std::vector<const Type*> StructEls;
1001 StructEls.reserve(V.size());
1002 for (unsigned i = 0, e = V.size(); i != e; ++i)
1003 StructEls.push_back(V[i]->getType());
1004 return get(StructType::get(StructEls), V);
1005}
1006
Chris Lattnerd7a73302001-10-13 06:57:33 +00001007// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001008//
Chris Lattner3462ae32001-12-03 22:26:30 +00001009void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +00001010 StructConstants.remove(this);
1011 destroyConstantImpl();
1012}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001013
Brian Gaeke02209042004-08-20 06:00:58 +00001014//---- ConstantPacked::get() implementation...
1015//
1016namespace llvm {
1017 template<>
1018 struct ConvertConstantType<ConstantPacked, PackedType> {
1019 static void convert(ConstantPacked *OldC, const PackedType *NewTy) {
1020 // Make everyone now use a constant of the new type...
1021 std::vector<Constant*> C;
1022 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1023 C.push_back(cast<Constant>(OldC->getOperand(i)));
1024 Constant *New = ConstantPacked::get(NewTy, C);
1025 assert(New != OldC && "Didn't replace constant??");
1026 OldC->uncheckedReplaceAllUsesWith(New);
1027 OldC->destroyConstant(); // This constant is now dead, destroy it.
1028 }
1029 };
1030}
1031
1032static std::vector<Constant*> getValType(ConstantPacked *CP) {
1033 std::vector<Constant*> Elements;
1034 Elements.reserve(CP->getNumOperands());
1035 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1036 Elements.push_back(CP->getOperand(i));
1037 return Elements;
1038}
1039
1040static ValueMap<std::vector<Constant*>, PackedType,
1041 ConstantPacked> PackedConstants;
1042
1043Constant *ConstantPacked::get(const PackedType *Ty,
1044 const std::vector<Constant*> &V) {
1045 // If this is an all-zero packed, return a ConstantAggregateZero object
1046 if (!V.empty()) {
1047 Constant *C = V[0];
1048 if (!C->isNullValue())
1049 return PackedConstants.getOrCreate(Ty, V);
1050 for (unsigned i = 1, e = V.size(); i != e; ++i)
1051 if (V[i] != C)
1052 return PackedConstants.getOrCreate(Ty, V);
1053 }
1054 return ConstantAggregateZero::get(Ty);
1055}
1056
1057Constant *ConstantPacked::get(const std::vector<Constant*> &V) {
1058 assert(!V.empty() && "Cannot infer type if V is empty");
1059 return get(PackedType::get(V.front()->getType(),V.size()), V);
1060}
1061
1062// destroyConstant - Remove the constant from the constant table...
1063//
1064void ConstantPacked::destroyConstant() {
1065 PackedConstants.remove(this);
1066 destroyConstantImpl();
1067}
1068
Chris Lattner3462ae32001-12-03 22:26:30 +00001069//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001070//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001071
Chris Lattner189d19f2003-11-21 20:23:48 +00001072namespace llvm {
1073 // ConstantPointerNull does not take extra "value" argument...
1074 template<class ValType>
1075 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1076 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1077 return new ConstantPointerNull(Ty);
1078 }
1079 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001080
Chris Lattner189d19f2003-11-21 20:23:48 +00001081 template<>
1082 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1083 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1084 // Make everyone now use a constant of the new type...
1085 Constant *New = ConstantPointerNull::get(NewTy);
1086 assert(New != OldC && "Didn't replace constant??");
1087 OldC->uncheckedReplaceAllUsesWith(New);
1088 OldC->destroyConstant(); // This constant is now dead, destroy it.
1089 }
1090 };
1091}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001092
Chris Lattner98fa07b2003-05-23 20:03:32 +00001093static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001094
Chris Lattner3e650af2004-08-04 04:48:01 +00001095static char getValType(ConstantPointerNull *) {
1096 return 0;
1097}
1098
1099
Chris Lattner3462ae32001-12-03 22:26:30 +00001100ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner98fa07b2003-05-23 20:03:32 +00001101 return NullPtrConstants.getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001102}
1103
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001104// destroyConstant - Remove the constant from the constant table...
1105//
1106void ConstantPointerNull::destroyConstant() {
1107 NullPtrConstants.remove(this);
1108 destroyConstantImpl();
1109}
1110
1111
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001112//---- UndefValue::get() implementation...
1113//
1114
1115namespace llvm {
1116 // UndefValue does not take extra "value" argument...
1117 template<class ValType>
1118 struct ConstantCreator<UndefValue, Type, ValType> {
1119 static UndefValue *create(const Type *Ty, const ValType &V) {
1120 return new UndefValue(Ty);
1121 }
1122 };
1123
1124 template<>
1125 struct ConvertConstantType<UndefValue, Type> {
1126 static void convert(UndefValue *OldC, const Type *NewTy) {
1127 // Make everyone now use a constant of the new type.
1128 Constant *New = UndefValue::get(NewTy);
1129 assert(New != OldC && "Didn't replace constant??");
1130 OldC->uncheckedReplaceAllUsesWith(New);
1131 OldC->destroyConstant(); // This constant is now dead, destroy it.
1132 }
1133 };
1134}
1135
1136static ValueMap<char, Type, UndefValue> UndefValueConstants;
1137
1138static char getValType(UndefValue *) {
1139 return 0;
1140}
1141
1142
1143UndefValue *UndefValue::get(const Type *Ty) {
1144 return UndefValueConstants.getOrCreate(Ty, 0);
1145}
1146
1147// destroyConstant - Remove the constant from the constant table.
1148//
1149void UndefValue::destroyConstant() {
1150 UndefValueConstants.remove(this);
1151 destroyConstantImpl();
1152}
1153
1154
1155
1156
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001157//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001158//
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001159typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Chris Lattner98fa07b2003-05-23 20:03:32 +00001160
Chris Lattner189d19f2003-11-21 20:23:48 +00001161namespace llvm {
1162 template<>
1163 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
1164 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
1165 if (V.first == Instruction::Cast)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001166 return new UnaryConstantExpr(Instruction::Cast, V.second[0], Ty);
Chris Lattner189d19f2003-11-21 20:23:48 +00001167 if ((V.first >= Instruction::BinaryOpsBegin &&
1168 V.first < Instruction::BinaryOpsEnd) ||
1169 V.first == Instruction::Shl || V.first == Instruction::Shr)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001170 return new BinaryConstantExpr(V.first, V.second[0], V.second[1]);
Chris Lattnerb7897ce2004-03-30 22:51:03 +00001171 if (V.first == Instruction::Select)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001172 return new SelectConstantExpr(V.second[0], V.second[1], V.second[2]);
Robert Bocchino23004482006-01-10 19:05:34 +00001173 if (V.first == Instruction::ExtractElement)
1174 return new ExtractElementConstantExpr(V.second[0], V.second[1]);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001175 if (V.first == Instruction::InsertElement)
1176 return new InsertElementConstantExpr(V.second[0], V.second[1],
1177 V.second[2]);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001178
Chris Lattner189d19f2003-11-21 20:23:48 +00001179 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001180
Chris Lattner189d19f2003-11-21 20:23:48 +00001181 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001182 return new GetElementPtrConstantExpr(V.second[0], IdxList, Ty);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001183 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001184 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001185
Chris Lattner189d19f2003-11-21 20:23:48 +00001186 template<>
1187 struct ConvertConstantType<ConstantExpr, Type> {
1188 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1189 Constant *New;
1190 switch (OldC->getOpcode()) {
1191 case Instruction::Cast:
1192 New = ConstantExpr::getCast(OldC->getOperand(0), NewTy);
1193 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001194 case Instruction::Select:
1195 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1196 OldC->getOperand(1),
1197 OldC->getOperand(2));
1198 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001199 case Instruction::Shl:
1200 case Instruction::Shr:
1201 New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
1202 OldC->getOperand(0), OldC->getOperand(1));
1203 break;
1204 default:
1205 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
1206 OldC->getOpcode() < Instruction::BinaryOpsEnd);
1207 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1208 OldC->getOperand(1));
1209 break;
1210 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001211 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001212 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
1213 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), Idx);
Chris Lattner189d19f2003-11-21 20:23:48 +00001214 break;
1215 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001216
Chris Lattner189d19f2003-11-21 20:23:48 +00001217 assert(New != OldC && "Didn't replace constant??");
1218 OldC->uncheckedReplaceAllUsesWith(New);
1219 OldC->destroyConstant(); // This constant is now dead, destroy it.
1220 }
1221 };
1222} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001223
1224
Chris Lattner3e650af2004-08-04 04:48:01 +00001225static ExprMapKeyType getValType(ConstantExpr *CE) {
1226 std::vector<Constant*> Operands;
1227 Operands.reserve(CE->getNumOperands());
1228 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1229 Operands.push_back(cast<Constant>(CE->getOperand(i)));
1230 return ExprMapKeyType(CE->getOpcode(), Operands);
1231}
1232
Chris Lattner98fa07b2003-05-23 20:03:32 +00001233static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001234
Chris Lattner46b3d302003-04-16 22:40:51 +00001235Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001236 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1237
Chris Lattneracdbe712003-04-17 19:24:48 +00001238 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
1239 return FC; // Fold a few common cases...
1240
Vikram S. Adve4c485332002-07-15 18:19:33 +00001241 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001242 std::vector<Constant*> argVec(1, C);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001243 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
1244 return ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001245}
Chris Lattnerd7a73302001-10-13 06:57:33 +00001246
Chris Lattnerdd284742004-04-04 23:20:30 +00001247Constant *ConstantExpr::getSignExtend(Constant *C, const Type *Ty) {
Chris Lattner1ece6f82005-01-01 15:59:57 +00001248 assert(C->getType()->isIntegral() && Ty->isIntegral() &&
Chris Lattnerdd284742004-04-04 23:20:30 +00001249 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1250 "This is an illegal sign extension!");
Chris Lattner1ece6f82005-01-01 15:59:57 +00001251 if (C->getType() != Type::BoolTy) {
1252 C = ConstantExpr::getCast(C, C->getType()->getSignedVersion());
1253 return ConstantExpr::getCast(C, Ty);
1254 } else {
1255 if (C == ConstantBool::True)
1256 return ConstantIntegral::getAllOnesValue(Ty);
1257 else
1258 return ConstantIntegral::getNullValue(Ty);
1259 }
Chris Lattnerdd284742004-04-04 23:20:30 +00001260}
1261
1262Constant *ConstantExpr::getZeroExtend(Constant *C, const Type *Ty) {
Chris Lattner1ece6f82005-01-01 15:59:57 +00001263 assert(C->getType()->isIntegral() && Ty->isIntegral() &&
Chris Lattnerdd284742004-04-04 23:20:30 +00001264 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1265 "This is an illegal zero extension!");
Chris Lattner1ece6f82005-01-01 15:59:57 +00001266 if (C->getType() != Type::BoolTy)
1267 C = ConstantExpr::getCast(C, C->getType()->getUnsignedVersion());
Chris Lattnerdd284742004-04-04 23:20:30 +00001268 return ConstantExpr::getCast(C, Ty);
1269}
1270
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001271Constant *ConstantExpr::getSizeOf(const Type *Ty) {
Chris Lattneracc4e542004-12-13 19:48:51 +00001272 // sizeof is implemented as: (ulong) gep (Ty*)null, 1
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001273 return getCast(
Chris Lattneracc4e542004-12-13 19:48:51 +00001274 getGetElementPtr(getNullValue(PointerType::get(Ty)),
1275 std::vector<Constant*>(1, ConstantInt::get(Type::UIntTy, 1))),
1276 Type::ULongTy);
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001277}
1278
Alkis Evlogimenos9160d5f2005-03-19 11:40:31 +00001279Constant *ConstantExpr::getPtrPtrFromArrayPtr(Constant *C) {
1280 // pointer from array is implemented as: getelementptr arr ptr, 0, 0
1281 static std::vector<Constant*> Indices(2, ConstantUInt::get(Type::UIntTy, 0));
1282
1283 return ConstantExpr::getGetElementPtr(C, Indices);
1284}
1285
Chris Lattnerb50d1352003-10-05 00:17:43 +00001286Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
1287 Constant *C1, Constant *C2) {
Chris Lattner5645e8a2004-01-12 19:04:55 +00001288 if (Opcode == Instruction::Shl || Opcode == Instruction::Shr)
1289 return getShiftTy(ReqTy, Opcode, C1, C2);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001290 // Check the operands for consistency first
1291 assert((Opcode >= Instruction::BinaryOpsBegin &&
1292 Opcode < Instruction::BinaryOpsEnd) &&
1293 "Invalid opcode in binary constant expression");
1294 assert(C1->getType() == C2->getType() &&
1295 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001296
Chris Lattner29ca2c62004-08-04 18:50:09 +00001297 if (ReqTy == C1->getType() || (Instruction::isRelational(Opcode) &&
1298 ReqTy == Type::BoolTy))
Chris Lattnerb50d1352003-10-05 00:17:43 +00001299 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1300 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001301
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001302 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001303 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001304 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001305}
1306
Chris Lattner29ca2c62004-08-04 18:50:09 +00001307Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001308#ifndef NDEBUG
1309 switch (Opcode) {
1310 case Instruction::Add: case Instruction::Sub:
1311 case Instruction::Mul: case Instruction::Div:
1312 case Instruction::Rem:
1313 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattnerc421a262006-01-04 01:01:04 +00001314 assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint() ||
1315 isa<PackedType>(C1->getType())) &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001316 "Tried to create an arithmetic operation on a non-arithmetic type!");
1317 break;
1318 case Instruction::And:
1319 case Instruction::Or:
1320 case Instruction::Xor:
1321 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattnerc421a262006-01-04 01:01:04 +00001322 assert((C1->getType()->isIntegral() || isa<PackedType>(C1->getType())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001323 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001324 break;
1325 case Instruction::SetLT: case Instruction::SetGT: case Instruction::SetLE:
1326 case Instruction::SetGE: case Instruction::SetEQ: case Instruction::SetNE:
1327 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1328 break;
1329 case Instruction::Shl:
1330 case Instruction::Shr:
1331 assert(C2->getType() == Type::UByteTy && "Shift should be by ubyte!");
Chris Lattnerc421a262006-01-04 01:01:04 +00001332 assert((C1->getType()->isInteger() || isa<PackedType>(C1->getType())) &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001333 "Tried to create a shift operation on a non-integer type!");
1334 break;
1335 default:
1336 break;
1337 }
1338#endif
1339
Chris Lattner29ca2c62004-08-04 18:50:09 +00001340 if (Instruction::isRelational(Opcode))
1341 return getTy(Type::BoolTy, Opcode, C1, C2);
1342 else
1343 return getTy(C1->getType(), Opcode, C1, C2);
1344}
1345
Chris Lattner6e415c02004-03-12 05:54:04 +00001346Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
1347 Constant *V1, Constant *V2) {
1348 assert(C->getType() == Type::BoolTy && "Select condition must be bool!");
1349 assert(V1->getType() == V2->getType() && "Select value types must match!");
1350 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
1351
1352 if (ReqTy == V1->getType())
1353 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1354 return SC; // Fold common cases
1355
1356 std::vector<Constant*> argVec(3, C);
1357 argVec[1] = V1;
1358 argVec[2] = V2;
1359 ExprMapKeyType Key = std::make_pair(Instruction::Select, argVec);
1360 return ExprConstants.getOrCreate(ReqTy, Key);
1361}
1362
Chris Lattner9eb2b522004-01-12 19:12:58 +00001363/// getShiftTy - Return a shift left or shift right constant expr
Chris Lattnerb50d1352003-10-05 00:17:43 +00001364Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
1365 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001366 // Check the operands for consistency first
1367 assert((Opcode == Instruction::Shl ||
1368 Opcode == Instruction::Shr) &&
1369 "Invalid opcode in binary constant expression");
1370 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
1371 "Invalid operand types for Shift constant expr!");
1372
Chris Lattner0bba7712004-01-12 20:40:42 +00001373 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001374 return FC; // Fold a few common cases...
1375
1376 // Look up the constant in the table first to ensure uniqueness
1377 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001378 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001379 return ExprConstants.getOrCreate(ReqTy, Key);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001380}
1381
1382
Chris Lattnerb50d1352003-10-05 00:17:43 +00001383Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner13128ab2004-10-11 22:52:25 +00001384 const std::vector<Value*> &IdxList) {
1385 assert(GetElementPtrInst::getIndexedType(C->getType(), IdxList, true) &&
Chris Lattner04b60fe2004-02-16 20:46:13 +00001386 "GEP indices invalid!");
1387
Chris Lattneracdbe712003-04-17 19:24:48 +00001388 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
1389 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001390
Chris Lattnerb50d1352003-10-05 00:17:43 +00001391 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001392 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001393 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00001394 std::vector<Constant*> ArgVec;
1395 ArgVec.reserve(IdxList.size()+1);
1396 ArgVec.push_back(C);
1397 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
1398 ArgVec.push_back(cast<Constant>(IdxList[i]));
1399 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,ArgVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001400 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001401}
1402
Chris Lattnerb50d1352003-10-05 00:17:43 +00001403Constant *ConstantExpr::getGetElementPtr(Constant *C,
1404 const std::vector<Constant*> &IdxList){
1405 // Get the result type of the getelementptr!
1406 std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
1407
1408 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
1409 true);
1410 assert(Ty && "GEP indices invalid!");
Chris Lattner13128ab2004-10-11 22:52:25 +00001411 return getGetElementPtrTy(PointerType::get(Ty), C, VIdxList);
1412}
1413
1414Constant *ConstantExpr::getGetElementPtr(Constant *C,
1415 const std::vector<Value*> &IdxList) {
1416 // Get the result type of the getelementptr!
1417 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
1418 true);
1419 assert(Ty && "GEP indices invalid!");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001420 return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
1421}
1422
Robert Bocchino23004482006-01-10 19:05:34 +00001423Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
1424 Constant *Idx) {
Robert Bocchinode7f1c92006-01-10 20:03:46 +00001425 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
1426 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00001427 // Look up the constant in the table first to ensure uniqueness
1428 std::vector<Constant*> ArgVec(1, Val);
1429 ArgVec.push_back(Idx);
1430 const ExprMapKeyType &Key = std::make_pair(Instruction::ExtractElement,ArgVec);
1431 return ExprConstants.getOrCreate(ReqTy, Key);
1432}
1433
1434Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
1435 assert(isa<PackedType>(Val->getType()) &&
1436 "Tried to create extractelement operation on non-packed type!");
1437 assert(Idx->getType() == Type::UIntTy &&
Robert Bocchinoca27f032006-01-17 20:07:22 +00001438 "Extractelement index must be uint type!");
Robert Bocchino23004482006-01-10 19:05:34 +00001439 return getExtractElementTy(cast<PackedType>(Val->getType())->getElementType(),
1440 Val, Idx);
1441}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001442
Robert Bocchinoca27f032006-01-17 20:07:22 +00001443Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
1444 Constant *Elt, Constant *Idx) {
1445 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
1446 return FC; // Fold a few common cases...
1447 // Look up the constant in the table first to ensure uniqueness
1448 std::vector<Constant*> ArgVec(1, Val);
1449 ArgVec.push_back(Elt);
1450 ArgVec.push_back(Idx);
1451 const ExprMapKeyType &Key = std::make_pair(Instruction::InsertElement,ArgVec);
1452 return ExprConstants.getOrCreate(ReqTy, Key);
1453}
1454
1455Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1456 Constant *Idx) {
1457 assert(isa<PackedType>(Val->getType()) &&
1458 "Tried to create insertelement operation on non-packed type!");
1459 assert(Elt->getType() == cast<PackedType>(Val->getType())->getElementType()
1460 && "Insertelement types must match!");
1461 assert(Idx->getType() == Type::UIntTy &&
1462 "Insertelement index must be uint type!");
1463 return getInsertElementTy(cast<PackedType>(Val->getType())->getElementType(),
1464 Val, Elt, Idx);
1465}
1466
Vikram S. Adve4c485332002-07-15 18:19:33 +00001467// destroyConstant - Remove the constant from the constant table...
1468//
1469void ConstantExpr::destroyConstant() {
1470 ExprConstants.remove(this);
1471 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001472}
1473
Chris Lattner3cd8c562002-07-30 18:54:25 +00001474const char *ConstantExpr::getOpcodeName() const {
1475 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001476}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00001477
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001478//===----------------------------------------------------------------------===//
1479// replaceUsesOfWithOnConstant implementations
1480
1481void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001482 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001483 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00001484 Constant *ToC = cast<Constant>(To);
Chris Lattnerdff59112005-10-04 18:47:09 +00001485
1486 unsigned OperandToUpdate = U-OperandList;
1487 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1488
Chris Lattnerb64419a2005-10-03 22:51:37 +00001489 std::pair<ArrayConstantsTy::MapKey, ConstantArray*> Lookup;
1490 Lookup.first.first = getType();
1491 Lookup.second = this;
Chris Lattnerdff59112005-10-04 18:47:09 +00001492
Chris Lattnerb64419a2005-10-03 22:51:37 +00001493 std::vector<Constant*> &Values = Lookup.first.second;
1494 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattnerdff59112005-10-04 18:47:09 +00001495
Chris Lattner8760ec72005-10-04 01:17:50 +00001496 // Fill values with the modified operands of the constant array. Also,
1497 // compute whether this turns into an all-zeros array.
Chris Lattnerdff59112005-10-04 18:47:09 +00001498 bool isAllZeros = false;
1499 if (!ToC->isNullValue()) {
1500 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1501 Values.push_back(cast<Constant>(O->get()));
1502 } else {
1503 isAllZeros = true;
1504 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1505 Constant *Val = cast<Constant>(O->get());
1506 Values.push_back(Val);
1507 if (isAllZeros) isAllZeros = Val->isNullValue();
1508 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001509 }
Chris Lattnerdff59112005-10-04 18:47:09 +00001510 Values[OperandToUpdate] = ToC;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001511
Chris Lattnerb64419a2005-10-03 22:51:37 +00001512 Constant *Replacement = 0;
1513 if (isAllZeros) {
1514 Replacement = ConstantAggregateZero::get(getType());
1515 } else {
1516 // Check to see if we have this array type already.
1517 bool Exists;
1518 ArrayConstantsTy::MapIterator I =
1519 ArrayConstants.InsertOrGetItem(Lookup, Exists);
1520
1521 if (Exists) {
1522 Replacement = I->second;
1523 } else {
1524 // Okay, the new shape doesn't exist in the system yet. Instead of
1525 // creating a new constant array, inserting it, replaceallusesof'ing the
1526 // old with the new, then deleting the old... just update the current one
1527 // in place!
Chris Lattner3b793c62005-10-04 21:35:50 +00001528 ArrayConstants.MoveConstantToNewSlot(this, I);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001529
Chris Lattnerdff59112005-10-04 18:47:09 +00001530 // Update to the new value.
1531 setOperand(OperandToUpdate, ToC);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001532 return;
1533 }
1534 }
1535
1536 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001537 assert(Replacement != this && "I didn't contain From!");
1538
Chris Lattner7a1450d2005-10-04 18:13:04 +00001539 // Everyone using this now uses the replacement.
1540 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001541
1542 // Delete the old constant!
1543 destroyConstant();
1544}
1545
1546void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001547 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001548 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00001549 Constant *ToC = cast<Constant>(To);
1550
Chris Lattnerdff59112005-10-04 18:47:09 +00001551 unsigned OperandToUpdate = U-OperandList;
1552 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1553
Chris Lattner8760ec72005-10-04 01:17:50 +00001554 std::pair<StructConstantsTy::MapKey, ConstantStruct*> Lookup;
1555 Lookup.first.first = getType();
1556 Lookup.second = this;
1557 std::vector<Constant*> &Values = Lookup.first.second;
1558 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001559
Chris Lattnerdff59112005-10-04 18:47:09 +00001560
Chris Lattner8760ec72005-10-04 01:17:50 +00001561 // Fill values with the modified operands of the constant struct. Also,
1562 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00001563 bool isAllZeros = false;
1564 if (!ToC->isNullValue()) {
1565 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1566 Values.push_back(cast<Constant>(O->get()));
1567 } else {
1568 isAllZeros = true;
1569 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1570 Constant *Val = cast<Constant>(O->get());
1571 Values.push_back(Val);
1572 if (isAllZeros) isAllZeros = Val->isNullValue();
1573 }
Chris Lattner8760ec72005-10-04 01:17:50 +00001574 }
Chris Lattnerdff59112005-10-04 18:47:09 +00001575 Values[OperandToUpdate] = ToC;
1576
Chris Lattner8760ec72005-10-04 01:17:50 +00001577 Constant *Replacement = 0;
1578 if (isAllZeros) {
1579 Replacement = ConstantAggregateZero::get(getType());
1580 } else {
1581 // Check to see if we have this array type already.
1582 bool Exists;
1583 StructConstantsTy::MapIterator I =
Chris Lattner3b793c62005-10-04 21:35:50 +00001584 StructConstants.InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00001585
1586 if (Exists) {
1587 Replacement = I->second;
1588 } else {
1589 // Okay, the new shape doesn't exist in the system yet. Instead of
1590 // creating a new constant struct, inserting it, replaceallusesof'ing the
1591 // old with the new, then deleting the old... just update the current one
1592 // in place!
Chris Lattner3b793c62005-10-04 21:35:50 +00001593 StructConstants.MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00001594
Chris Lattnerdff59112005-10-04 18:47:09 +00001595 // Update to the new value.
1596 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00001597 return;
1598 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001599 }
1600
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001601 assert(Replacement != this && "I didn't contain From!");
1602
Chris Lattner7a1450d2005-10-04 18:13:04 +00001603 // Everyone using this now uses the replacement.
1604 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001605
1606 // Delete the old constant!
1607 destroyConstant();
1608}
1609
1610void ConstantPacked::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001611 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001612 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
1613
1614 std::vector<Constant*> Values;
1615 Values.reserve(getNumOperands()); // Build replacement array...
1616 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1617 Constant *Val = getOperand(i);
1618 if (Val == From) Val = cast<Constant>(To);
1619 Values.push_back(Val);
1620 }
1621
1622 Constant *Replacement = ConstantPacked::get(getType(), Values);
1623 assert(Replacement != this && "I didn't contain From!");
1624
Chris Lattner7a1450d2005-10-04 18:13:04 +00001625 // Everyone using this now uses the replacement.
1626 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001627
1628 // Delete the old constant!
1629 destroyConstant();
1630}
1631
1632void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001633 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001634 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
1635 Constant *To = cast<Constant>(ToV);
1636
1637 Constant *Replacement = 0;
1638 if (getOpcode() == Instruction::GetElementPtr) {
1639 std::vector<Constant*> Indices;
1640 Constant *Pointer = getOperand(0);
1641 Indices.reserve(getNumOperands()-1);
1642 if (Pointer == From) Pointer = To;
1643
1644 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1645 Constant *Val = getOperand(i);
1646 if (Val == From) Val = To;
1647 Indices.push_back(Val);
1648 }
1649 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
1650 } else if (getOpcode() == Instruction::Cast) {
1651 assert(getOperand(0) == From && "Cast only has one use!");
1652 Replacement = ConstantExpr::getCast(To, getType());
1653 } else if (getOpcode() == Instruction::Select) {
1654 Constant *C1 = getOperand(0);
1655 Constant *C2 = getOperand(1);
1656 Constant *C3 = getOperand(2);
1657 if (C1 == From) C1 = To;
1658 if (C2 == From) C2 = To;
1659 if (C3 == From) C3 = To;
1660 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00001661 } else if (getOpcode() == Instruction::ExtractElement) {
1662 Constant *C1 = getOperand(0);
1663 Constant *C2 = getOperand(1);
1664 if (C1 == From) C1 = To;
1665 if (C2 == From) C2 = To;
1666 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001667 } else if (getNumOperands() == 2) {
1668 Constant *C1 = getOperand(0);
1669 Constant *C2 = getOperand(1);
1670 if (C1 == From) C1 = To;
1671 if (C2 == From) C2 = To;
1672 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
1673 } else {
1674 assert(0 && "Unknown ConstantExpr type!");
1675 return;
1676 }
1677
1678 assert(Replacement != this && "I didn't contain From!");
1679
Chris Lattner7a1450d2005-10-04 18:13:04 +00001680 // Everyone using this now uses the replacement.
1681 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001682
1683 // Delete the old constant!
1684 destroyConstant();
1685}
1686
1687
1688
Chris Lattner99a669b2004-11-19 16:39:44 +00001689/// clearAllValueMaps - This method frees all internal memory used by the
1690/// constant subsystem, which can be used in environments where this memory
1691/// is otherwise reported as a leak.
1692void Constant::clearAllValueMaps() {
1693 std::vector<Constant *> Constants;
1694
1695 DoubleConstants.clear(Constants);
1696 FloatConstants.clear(Constants);
1697 SIntConstants.clear(Constants);
1698 UIntConstants.clear(Constants);
1699 AggZeroConstants.clear(Constants);
1700 ArrayConstants.clear(Constants);
1701 StructConstants.clear(Constants);
1702 PackedConstants.clear(Constants);
1703 NullPtrConstants.clear(Constants);
1704 UndefValueConstants.clear(Constants);
1705 ExprConstants.clear(Constants);
1706
Misha Brukmanb1c93172005-04-21 23:48:37 +00001707 for (std::vector<Constant *>::iterator I = Constants.begin(),
Chris Lattner99a669b2004-11-19 16:39:44 +00001708 E = Constants.end(); I != E; ++I)
1709 (*I)->dropAllReferences();
1710 for (std::vector<Constant *>::iterator I = Constants.begin(),
1711 E = Constants.end(); I != E; ++I)
1712 (*I)->destroyConstantImpl();
1713 Constants.clear();
1714}