blob: f4ff31f24f21213076c1eab023d13dbd555ed7ef [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 Bocchino23004482006-01-10 19:05:34 +0000350/// ExtractElementConstantExpr - This class is private to Constants.cpp, and is used
351/// behind the scenes to implement extractelement constant exprs.
352class ExtractElementConstantExpr : public ConstantExpr {
353 Use Ops[2];
354public:
355 ExtractElementConstantExpr(Constant *C1, Constant *C2)
356 : ConstantExpr(cast<PackedType>(C1->getType())->getElementType(),
357 Instruction::ExtractElement, Ops, 2) {
358 Ops[0].init(C1, this);
359 Ops[1].init(C2, this);
360 }
361};
362
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000363/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
364/// used behind the scenes to implement getelementpr constant exprs.
365struct GetElementPtrConstantExpr : public ConstantExpr {
366 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
367 const Type *DestTy)
368 : ConstantExpr(DestTy, Instruction::GetElementPtr,
369 new Use[IdxList.size()+1], IdxList.size()+1) {
370 OperandList[0].init(C, this);
371 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
372 OperandList[i+1].init(IdxList[i], this);
373 }
374 ~GetElementPtrConstantExpr() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000375 delete [] OperandList;
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000376 }
377};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000378
Chris Lattner817175f2004-03-29 02:37:53 +0000379/// ConstantExpr::get* - Return some common constants without having to
380/// specify the full Instruction::OPCODE identifier.
381///
382Constant *ConstantExpr::getNeg(Constant *C) {
Chris Lattner3cdc27c2004-03-29 19:51:24 +0000383 if (!C->getType()->isFloatingPoint())
384 return get(Instruction::Sub, getNullValue(C->getType()), C);
385 else
386 return get(Instruction::Sub, ConstantFP::get(C->getType(), -0.0), C);
Chris Lattner817175f2004-03-29 02:37:53 +0000387}
388Constant *ConstantExpr::getNot(Constant *C) {
389 assert(isa<ConstantIntegral>(C) && "Cannot NOT a nonintegral type!");
390 return get(Instruction::Xor, C,
391 ConstantIntegral::getAllOnesValue(C->getType()));
392}
393Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
394 return get(Instruction::Add, C1, C2);
395}
396Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
397 return get(Instruction::Sub, C1, C2);
398}
399Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
400 return get(Instruction::Mul, C1, C2);
401}
402Constant *ConstantExpr::getDiv(Constant *C1, Constant *C2) {
403 return get(Instruction::Div, C1, C2);
404}
405Constant *ConstantExpr::getRem(Constant *C1, Constant *C2) {
406 return get(Instruction::Rem, C1, C2);
407}
408Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
409 return get(Instruction::And, C1, C2);
410}
411Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
412 return get(Instruction::Or, C1, C2);
413}
414Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
415 return get(Instruction::Xor, C1, C2);
416}
417Constant *ConstantExpr::getSetEQ(Constant *C1, Constant *C2) {
418 return get(Instruction::SetEQ, C1, C2);
419}
420Constant *ConstantExpr::getSetNE(Constant *C1, Constant *C2) {
421 return get(Instruction::SetNE, C1, C2);
422}
423Constant *ConstantExpr::getSetLT(Constant *C1, Constant *C2) {
424 return get(Instruction::SetLT, C1, C2);
425}
426Constant *ConstantExpr::getSetGT(Constant *C1, Constant *C2) {
427 return get(Instruction::SetGT, C1, C2);
428}
429Constant *ConstantExpr::getSetLE(Constant *C1, Constant *C2) {
430 return get(Instruction::SetLE, C1, C2);
431}
432Constant *ConstantExpr::getSetGE(Constant *C1, Constant *C2) {
433 return get(Instruction::SetGE, C1, C2);
434}
435Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
436 return get(Instruction::Shl, C1, C2);
437}
438Constant *ConstantExpr::getShr(Constant *C1, Constant *C2) {
439 return get(Instruction::Shr, C1, C2);
440}
441
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000442Constant *ConstantExpr::getUShr(Constant *C1, Constant *C2) {
443 if (C1->getType()->isUnsigned()) return getShr(C1, C2);
444 return getCast(getShr(getCast(C1,
445 C1->getType()->getUnsignedVersion()), C2), C1->getType());
446}
Chris Lattner817175f2004-03-29 02:37:53 +0000447
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000448Constant *ConstantExpr::getSShr(Constant *C1, Constant *C2) {
449 if (C1->getType()->isSigned()) return getShr(C1, C2);
450 return getCast(getShr(getCast(C1,
451 C1->getType()->getSignedVersion()), C2), C1->getType());
452}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000453
Chris Lattner2f7c9632001-06-06 20:29:01 +0000454
455//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000456// isValueValidForType implementations
457
Chris Lattner3462ae32001-12-03 22:26:30 +0000458bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000459 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000460 default:
461 return false; // These can't be represented as integers!!!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000462 // Signed types...
463 case Type::SByteTyID:
464 return (Val <= INT8_MAX && Val >= INT8_MIN);
465 case Type::ShortTyID:
466 return (Val <= INT16_MAX && Val >= INT16_MIN);
467 case Type::IntTyID:
Chris Lattner74248512004-06-08 23:21:39 +0000468 return (Val <= int(INT32_MAX) && Val >= int(INT32_MIN));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000469 case Type::LongTyID:
470 return true; // This is the largest type...
471 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000472}
473
Chris Lattner3462ae32001-12-03 22:26:30 +0000474bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_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!!!
478
479 // Unsigned types...
480 case Type::UByteTyID:
481 return (Val <= UINT8_MAX);
482 case Type::UShortTyID:
483 return (Val <= UINT16_MAX);
484 case Type::UIntTyID:
485 return (Val <= UINT32_MAX);
486 case Type::ULongTyID:
487 return true; // This is the largest type...
488 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000489}
490
Chris Lattner3462ae32001-12-03 22:26:30 +0000491bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000492 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000493 default:
494 return false; // These can't be represented as floating point!
495
Reid Spencerb95f8ab2004-12-07 07:38:08 +0000496 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000497 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000498 case Type::DoubleTyID:
499 return true; // This is the largest type...
500 }
501};
Chris Lattner9655e542001-07-20 19:16:02 +0000502
Chris Lattner49d855c2001-09-07 16:46:31 +0000503//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000504// Factory Function Implementation
505
Chris Lattner98fa07b2003-05-23 20:03:32 +0000506// ConstantCreator - A class that is used to create constants by
507// ValueMap*. This class should be partially specialized if there is
508// something strange that needs to be done to interface to the ctor for the
509// constant.
510//
Chris Lattner189d19f2003-11-21 20:23:48 +0000511namespace llvm {
512 template<class ConstantClass, class TypeClass, class ValType>
513 struct ConstantCreator {
514 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
515 return new ConstantClass(Ty, V);
516 }
517 };
Misha Brukmanb1c93172005-04-21 23:48:37 +0000518
Chris Lattner189d19f2003-11-21 20:23:48 +0000519 template<class ConstantClass, class TypeClass>
520 struct ConvertConstantType {
521 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
522 assert(0 && "This type cannot be converted!\n");
523 abort();
524 }
525 };
526}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000527
Chris Lattner98fa07b2003-05-23 20:03:32 +0000528namespace {
Chris Lattner935aa922005-10-04 17:48:46 +0000529 template<class ValType, class TypeClass, class ConstantClass,
530 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattnerb50d1352003-10-05 00:17:43 +0000531 class ValueMap : public AbstractTypeUser {
Chris Lattnerb64419a2005-10-03 22:51:37 +0000532 public:
Chris Lattnerb50d1352003-10-05 00:17:43 +0000533 typedef std::pair<const TypeClass*, ValType> MapKey;
534 typedef std::map<MapKey, ConstantClass *> MapTy;
535 typedef typename MapTy::iterator MapIterator;
Chris Lattnerb64419a2005-10-03 22:51:37 +0000536 private:
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000537 /// Map - This is the main map from the element descriptor to the Constants.
538 /// This is the primary way we avoid creating two of the same shape
539 /// constant.
Chris Lattnerb50d1352003-10-05 00:17:43 +0000540 MapTy Map;
Chris Lattner935aa922005-10-04 17:48:46 +0000541
542 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
543 /// from the constants to their element in Map. This is important for
544 /// removal of constants from the array, which would otherwise have to scan
545 /// through the map with very large keys.
546 std::map<ConstantClass*, MapIterator> InverseMap;
Chris Lattnerb50d1352003-10-05 00:17:43 +0000547
548 typedef std::map<const TypeClass*, MapIterator> AbstractTypeMapTy;
549 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner99a669b2004-11-19 16:39:44 +0000550
551 friend void Constant::clearAllValueMaps();
552 private:
553 void clear(std::vector<Constant *> &Constants) {
554 for(MapIterator I = Map.begin(); I != Map.end(); ++I)
555 Constants.push_back(I->second);
556 Map.clear();
557 AbstractTypeMap.clear();
Chris Lattner935aa922005-10-04 17:48:46 +0000558 InverseMap.clear();
Chris Lattner99a669b2004-11-19 16:39:44 +0000559 }
560
Chris Lattner98fa07b2003-05-23 20:03:32 +0000561 public:
Chris Lattnerb64419a2005-10-03 22:51:37 +0000562 MapIterator map_end() { return Map.end(); }
563
564 /// InsertOrGetItem - Return an iterator for the specified element.
565 /// If the element exists in the map, the returned iterator points to the
566 /// entry and Exists=true. If not, the iterator points to the newly
567 /// inserted entry and returns Exists=false. Newly inserted entries have
568 /// I->second == 0, and should be filled in.
569 MapIterator InsertOrGetItem(std::pair<MapKey, ConstantClass *> &InsertVal,
570 bool &Exists) {
571 std::pair<MapIterator, bool> IP = Map.insert(InsertVal);
572 Exists = !IP.second;
573 return IP.first;
574 }
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000575
Chris Lattner935aa922005-10-04 17:48:46 +0000576private:
577 MapIterator FindExistingElement(ConstantClass *CP) {
578 if (HasLargeKey) {
579 typename std::map<ConstantClass*, MapIterator>::iterator
580 IMI = InverseMap.find(CP);
581 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
582 IMI->second->second == CP &&
583 "InverseMap corrupt!");
584 return IMI->second;
585 }
586
587 MapIterator I =
588 Map.find(MapKey((TypeClass*)CP->getRawType(), getValType(CP)));
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000589 if (I == Map.end() || I->second != CP) {
590 // FIXME: This should not use a linear scan. If this gets to be a
591 // performance problem, someone should look at this.
592 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
593 /* empty */;
594 }
Chris Lattner935aa922005-10-04 17:48:46 +0000595 return I;
596 }
597public:
598
Chris Lattnerb64419a2005-10-03 22:51:37 +0000599 /// getOrCreate - Return the specified constant from the map, creating it if
600 /// necessary.
Chris Lattner98fa07b2003-05-23 20:03:32 +0000601 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000602 MapKey Lookup(Ty, V);
603 MapIterator I = Map.lower_bound(Lookup);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000604 if (I != Map.end() && I->first == Lookup)
605 return I->second; // Is it in the map?
606
607 // If no preexisting value, create one now...
608 ConstantClass *Result =
609 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
610
Chris Lattnerb50d1352003-10-05 00:17:43 +0000611 /// FIXME: why does this assert fail when loading 176.gcc?
612 //assert(Result->getType() == Ty && "Type specified is not correct!");
613 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
614
Chris Lattner935aa922005-10-04 17:48:46 +0000615 if (HasLargeKey) // Remember the reverse mapping if needed.
616 InverseMap.insert(std::make_pair(Result, I));
617
Chris Lattnerb50d1352003-10-05 00:17:43 +0000618 // If the type of the constant is abstract, make sure that an entry exists
619 // for it in the AbstractTypeMap.
620 if (Ty->isAbstract()) {
621 typename AbstractTypeMapTy::iterator TI =
622 AbstractTypeMap.lower_bound(Ty);
623
624 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
625 // Add ourselves to the ATU list of the type.
626 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
627
628 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
629 }
630 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000631 return Result;
632 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000633
Chris Lattner98fa07b2003-05-23 20:03:32 +0000634 void remove(ConstantClass *CP) {
Chris Lattner935aa922005-10-04 17:48:46 +0000635 MapIterator I = FindExistingElement(CP);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000636 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +0000637 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +0000638
Chris Lattner935aa922005-10-04 17:48:46 +0000639 if (HasLargeKey) // Remember the reverse mapping if needed.
640 InverseMap.erase(CP);
641
Chris Lattnerb50d1352003-10-05 00:17:43 +0000642 // Now that we found the entry, make sure this isn't the entry that
643 // the AbstractTypeMap points to.
644 const TypeClass *Ty = I->first.first;
645 if (Ty->isAbstract()) {
646 assert(AbstractTypeMap.count(Ty) &&
647 "Abstract type not in AbstractTypeMap?");
648 MapIterator &ATMEntryIt = AbstractTypeMap[Ty];
649 if (ATMEntryIt == I) {
650 // Yes, we are removing the representative entry for this type.
651 // See if there are any other entries of the same type.
652 MapIterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000653
Chris Lattnerb50d1352003-10-05 00:17:43 +0000654 // First check the entry before this one...
655 if (TmpIt != Map.begin()) {
656 --TmpIt;
657 if (TmpIt->first.first != Ty) // Not the same type, move back...
658 ++TmpIt;
659 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000660
Chris Lattnerb50d1352003-10-05 00:17:43 +0000661 // If we didn't find the same type, try to move forward...
662 if (TmpIt == ATMEntryIt) {
663 ++TmpIt;
664 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
665 --TmpIt; // No entry afterwards with the same type
666 }
667
668 // If there is another entry in the map of the same abstract type,
669 // update the AbstractTypeMap entry now.
670 if (TmpIt != ATMEntryIt) {
671 ATMEntryIt = TmpIt;
672 } else {
673 // Otherwise, we are removing the last instance of this type
674 // from the table. Remove from the ATM, and from user list.
675 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
676 AbstractTypeMap.erase(Ty);
677 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000678 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000679 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000680
Chris Lattnerb50d1352003-10-05 00:17:43 +0000681 Map.erase(I);
682 }
683
Chris Lattner3b793c62005-10-04 21:35:50 +0000684
685 /// MoveConstantToNewSlot - If we are about to change C to be the element
686 /// specified by I, update our internal data structures to reflect this
687 /// fact.
688 void MoveConstantToNewSlot(ConstantClass *C, MapIterator I) {
689 // First, remove the old location of the specified constant in the map.
690 MapIterator OldI = FindExistingElement(C);
691 assert(OldI != Map.end() && "Constant not found in constant table!");
692 assert(OldI->second == C && "Didn't find correct element?");
693
694 // If this constant is the representative element for its abstract type,
695 // update the AbstractTypeMap so that the representative element is I.
696 if (C->getType()->isAbstract()) {
697 typename AbstractTypeMapTy::iterator ATI =
698 AbstractTypeMap.find(C->getType());
699 assert(ATI != AbstractTypeMap.end() &&
700 "Abstract type not in AbstractTypeMap?");
701 if (ATI->second == OldI)
702 ATI->second = I;
703 }
704
705 // Remove the old entry from the map.
706 Map.erase(OldI);
707
708 // Update the inverse map so that we know that this constant is now
709 // located at descriptor I.
710 if (HasLargeKey) {
711 assert(I->second == C && "Bad inversemap entry!");
712 InverseMap[C] = I;
713 }
714 }
715
Chris Lattnerb50d1352003-10-05 00:17:43 +0000716 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000717 typename AbstractTypeMapTy::iterator I =
Chris Lattnerb50d1352003-10-05 00:17:43 +0000718 AbstractTypeMap.find(cast<TypeClass>(OldTy));
719
720 assert(I != AbstractTypeMap.end() &&
721 "Abstract type not in AbstractTypeMap?");
722
723 // Convert a constant at a time until the last one is gone. The last one
724 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
725 // eliminated eventually.
726 do {
727 ConvertConstantType<ConstantClass,
728 TypeClass>::convert(I->second->second,
729 cast<TypeClass>(NewTy));
730
731 I = AbstractTypeMap.find(cast<TypeClass>(OldTy));
732 } while (I != AbstractTypeMap.end());
733 }
734
735 // If the type became concrete without being refined to any other existing
736 // type, we just remove ourselves from the ATU list.
737 void typeBecameConcrete(const DerivedType *AbsTy) {
738 AbsTy->removeAbstractTypeUser(this);
739 }
740
741 void dump() const {
742 std::cerr << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +0000743 }
744 };
745}
746
Chris Lattner3462ae32001-12-03 22:26:30 +0000747//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000748//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000749static ValueMap< int64_t, Type, ConstantSInt> SIntConstants;
750static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000751
Chris Lattner3462ae32001-12-03 22:26:30 +0000752ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000753 return SIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000754}
755
Chris Lattner3462ae32001-12-03 22:26:30 +0000756ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000757 return UIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000758}
759
Chris Lattner3462ae32001-12-03 22:26:30 +0000760ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000761 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000762 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
763 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000764}
765
Chris Lattner3462ae32001-12-03 22:26:30 +0000766//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000767//
Chris Lattnerac80ea42004-02-01 22:49:04 +0000768namespace llvm {
769 template<>
770 struct ConstantCreator<ConstantFP, Type, uint64_t> {
771 static ConstantFP *create(const Type *Ty, uint64_t V) {
772 assert(Ty == Type::DoubleTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000773 return new ConstantFP(Ty, BitsToDouble(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000774 }
775 };
776 template<>
777 struct ConstantCreator<ConstantFP, Type, uint32_t> {
778 static ConstantFP *create(const Type *Ty, uint32_t V) {
779 assert(Ty == Type::FloatTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000780 return new ConstantFP(Ty, BitsToFloat(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000781 }
782 };
783}
784
785static ValueMap<uint64_t, Type, ConstantFP> DoubleConstants;
786static ValueMap<uint32_t, Type, ConstantFP> FloatConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000787
Jim Laskey8ad8f712005-08-17 20:06:22 +0000788bool ConstantFP::isNullValue() const {
789 return DoubleToBits(Val) == 0;
790}
791
792bool ConstantFP::isExactlyValue(double V) const {
793 return DoubleToBits(V) == DoubleToBits(Val);
794}
795
796
Chris Lattner3462ae32001-12-03 22:26:30 +0000797ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner241ed4c2004-01-23 00:55:21 +0000798 if (Ty == Type::FloatTy) {
799 // Force the value through memory to normalize it.
Jim Laskeyb74c6662005-08-17 19:34:49 +0000800 return FloatConstants.getOrCreate(Ty, FloatToBits(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000801 } else {
802 assert(Ty == Type::DoubleTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000803 return DoubleConstants.getOrCreate(Ty, DoubleToBits(V));
Chris Lattner241ed4c2004-01-23 00:55:21 +0000804 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000805}
806
Chris Lattner9fba3da2004-02-15 05:53:04 +0000807//---- ConstantAggregateZero::get() implementation...
808//
809namespace llvm {
810 // ConstantAggregateZero does not take extra "value" argument...
811 template<class ValType>
812 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
813 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
814 return new ConstantAggregateZero(Ty);
815 }
816 };
817
818 template<>
819 struct ConvertConstantType<ConstantAggregateZero, Type> {
820 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
821 // Make everyone now use a constant of the new type...
822 Constant *New = ConstantAggregateZero::get(NewTy);
823 assert(New != OldC && "Didn't replace constant??");
824 OldC->uncheckedReplaceAllUsesWith(New);
825 OldC->destroyConstant(); // This constant is now dead, destroy it.
826 }
827 };
828}
829
830static ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
831
Chris Lattner3e650af2004-08-04 04:48:01 +0000832static char getValType(ConstantAggregateZero *CPZ) { return 0; }
833
Chris Lattner9fba3da2004-02-15 05:53:04 +0000834Constant *ConstantAggregateZero::get(const Type *Ty) {
835 return AggZeroConstants.getOrCreate(Ty, 0);
836}
837
838// destroyConstant - Remove the constant from the constant table...
839//
840void ConstantAggregateZero::destroyConstant() {
841 AggZeroConstants.remove(this);
842 destroyConstantImpl();
843}
844
Chris Lattner3462ae32001-12-03 22:26:30 +0000845//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000846//
Chris Lattner189d19f2003-11-21 20:23:48 +0000847namespace llvm {
848 template<>
849 struct ConvertConstantType<ConstantArray, ArrayType> {
850 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
851 // Make everyone now use a constant of the new type...
852 std::vector<Constant*> C;
853 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
854 C.push_back(cast<Constant>(OldC->getOperand(i)));
855 Constant *New = ConstantArray::get(NewTy, C);
856 assert(New != OldC && "Didn't replace constant??");
857 OldC->uncheckedReplaceAllUsesWith(New);
858 OldC->destroyConstant(); // This constant is now dead, destroy it.
859 }
860 };
861}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000862
Chris Lattner3e650af2004-08-04 04:48:01 +0000863static std::vector<Constant*> getValType(ConstantArray *CA) {
864 std::vector<Constant*> Elements;
865 Elements.reserve(CA->getNumOperands());
866 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
867 Elements.push_back(cast<Constant>(CA->getOperand(i)));
868 return Elements;
869}
870
Chris Lattnerb64419a2005-10-03 22:51:37 +0000871typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattner935aa922005-10-04 17:48:46 +0000872 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattnerb64419a2005-10-03 22:51:37 +0000873static ArrayConstantsTy ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000874
Chris Lattner015e8212004-02-15 04:14:47 +0000875Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +0000876 const std::vector<Constant*> &V) {
877 // If this is an all-zero array, return a ConstantAggregateZero object
878 if (!V.empty()) {
879 Constant *C = V[0];
880 if (!C->isNullValue())
881 return ArrayConstants.getOrCreate(Ty, V);
882 for (unsigned i = 1, e = V.size(); i != e; ++i)
883 if (V[i] != C)
884 return ArrayConstants.getOrCreate(Ty, V);
885 }
886 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000887}
888
Chris Lattner98fa07b2003-05-23 20:03:32 +0000889// destroyConstant - Remove the constant from the constant table...
890//
891void ConstantArray::destroyConstant() {
892 ArrayConstants.remove(this);
893 destroyConstantImpl();
894}
895
Chris Lattner3462ae32001-12-03 22:26:30 +0000896// ConstantArray::get(const string&) - Return an array that is initialized to
Chris Lattner8f80fe02001-10-14 23:54:12 +0000897// contain the specified string. A null terminator is added to the specified
898// string so that it may be used in a natural way...
899//
Chris Lattner015e8212004-02-15 04:14:47 +0000900Constant *ConstantArray::get(const std::string &Str) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000901 std::vector<Constant*> ElementVals;
Chris Lattner8f80fe02001-10-14 23:54:12 +0000902
903 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000904 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000905
906 // Add a null terminator to the string...
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000907 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000908
Chris Lattnerbec9b0b2001-12-14 16:41:18 +0000909 ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
Chris Lattner3462ae32001-12-03 22:26:30 +0000910 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000911}
912
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000913/// isString - This method returns true if the array is an array of sbyte or
914/// ubyte, and if the elements of the array are all ConstantInt's.
915bool ConstantArray::isString() const {
916 // Check the element type for sbyte or ubyte...
Chris Lattnere8701f62004-01-14 17:51:53 +0000917 if (getType()->getElementType() != Type::UByteTy &&
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000918 getType()->getElementType() != Type::SByteTy)
919 return false;
920 // Check the elements to make sure they are all integers, not constant
921 // expressions.
922 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
923 if (!isa<ConstantInt>(getOperand(i)))
924 return false;
925 return true;
926}
927
Chris Lattner81fabb02002-08-26 17:53:56 +0000928// getAsString - If the sub-element type of this array is either sbyte or ubyte,
929// then this method converts the array to an std::string and returns it.
930// Otherwise, it asserts out.
931//
932std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000933 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +0000934 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +0000935 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
936 Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue();
Chris Lattner81fabb02002-08-26 17:53:56 +0000937 return Result;
938}
939
940
Chris Lattner3462ae32001-12-03 22:26:30 +0000941//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000942//
Chris Lattnerb50d1352003-10-05 00:17:43 +0000943
Chris Lattner189d19f2003-11-21 20:23:48 +0000944namespace llvm {
945 template<>
946 struct ConvertConstantType<ConstantStruct, StructType> {
947 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
948 // Make everyone now use a constant of the new type...
949 std::vector<Constant*> C;
950 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
951 C.push_back(cast<Constant>(OldC->getOperand(i)));
952 Constant *New = ConstantStruct::get(NewTy, C);
953 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000954
Chris Lattner189d19f2003-11-21 20:23:48 +0000955 OldC->uncheckedReplaceAllUsesWith(New);
956 OldC->destroyConstant(); // This constant is now dead, destroy it.
957 }
958 };
959}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000960
Chris Lattner8760ec72005-10-04 01:17:50 +0000961typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattner935aa922005-10-04 17:48:46 +0000962 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner8760ec72005-10-04 01:17:50 +0000963static StructConstantsTy StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000964
Chris Lattner3e650af2004-08-04 04:48:01 +0000965static std::vector<Constant*> getValType(ConstantStruct *CS) {
966 std::vector<Constant*> Elements;
967 Elements.reserve(CS->getNumOperands());
968 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
969 Elements.push_back(cast<Constant>(CS->getOperand(i)));
970 return Elements;
971}
972
Chris Lattner015e8212004-02-15 04:14:47 +0000973Constant *ConstantStruct::get(const StructType *Ty,
974 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +0000975 // Create a ConstantAggregateZero value if all elements are zeros...
976 for (unsigned i = 0, e = V.size(); i != e; ++i)
977 if (!V[i]->isNullValue())
978 return StructConstants.getOrCreate(Ty, V);
979
980 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000981}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000982
Chris Lattnerd6108ca2004-07-12 20:35:11 +0000983Constant *ConstantStruct::get(const std::vector<Constant*> &V) {
984 std::vector<const Type*> StructEls;
985 StructEls.reserve(V.size());
986 for (unsigned i = 0, e = V.size(); i != e; ++i)
987 StructEls.push_back(V[i]->getType());
988 return get(StructType::get(StructEls), V);
989}
990
Chris Lattnerd7a73302001-10-13 06:57:33 +0000991// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +0000992//
Chris Lattner3462ae32001-12-03 22:26:30 +0000993void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +0000994 StructConstants.remove(this);
995 destroyConstantImpl();
996}
Chris Lattner883ad0b2001-10-03 15:39:36 +0000997
Brian Gaeke02209042004-08-20 06:00:58 +0000998//---- ConstantPacked::get() implementation...
999//
1000namespace llvm {
1001 template<>
1002 struct ConvertConstantType<ConstantPacked, PackedType> {
1003 static void convert(ConstantPacked *OldC, const PackedType *NewTy) {
1004 // Make everyone now use a constant of the new type...
1005 std::vector<Constant*> C;
1006 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1007 C.push_back(cast<Constant>(OldC->getOperand(i)));
1008 Constant *New = ConstantPacked::get(NewTy, C);
1009 assert(New != OldC && "Didn't replace constant??");
1010 OldC->uncheckedReplaceAllUsesWith(New);
1011 OldC->destroyConstant(); // This constant is now dead, destroy it.
1012 }
1013 };
1014}
1015
1016static std::vector<Constant*> getValType(ConstantPacked *CP) {
1017 std::vector<Constant*> Elements;
1018 Elements.reserve(CP->getNumOperands());
1019 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1020 Elements.push_back(CP->getOperand(i));
1021 return Elements;
1022}
1023
1024static ValueMap<std::vector<Constant*>, PackedType,
1025 ConstantPacked> PackedConstants;
1026
1027Constant *ConstantPacked::get(const PackedType *Ty,
1028 const std::vector<Constant*> &V) {
1029 // If this is an all-zero packed, return a ConstantAggregateZero object
1030 if (!V.empty()) {
1031 Constant *C = V[0];
1032 if (!C->isNullValue())
1033 return PackedConstants.getOrCreate(Ty, V);
1034 for (unsigned i = 1, e = V.size(); i != e; ++i)
1035 if (V[i] != C)
1036 return PackedConstants.getOrCreate(Ty, V);
1037 }
1038 return ConstantAggregateZero::get(Ty);
1039}
1040
1041Constant *ConstantPacked::get(const std::vector<Constant*> &V) {
1042 assert(!V.empty() && "Cannot infer type if V is empty");
1043 return get(PackedType::get(V.front()->getType(),V.size()), V);
1044}
1045
1046// destroyConstant - Remove the constant from the constant table...
1047//
1048void ConstantPacked::destroyConstant() {
1049 PackedConstants.remove(this);
1050 destroyConstantImpl();
1051}
1052
Chris Lattner3462ae32001-12-03 22:26:30 +00001053//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001054//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001055
Chris Lattner189d19f2003-11-21 20:23:48 +00001056namespace llvm {
1057 // ConstantPointerNull does not take extra "value" argument...
1058 template<class ValType>
1059 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1060 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1061 return new ConstantPointerNull(Ty);
1062 }
1063 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001064
Chris Lattner189d19f2003-11-21 20:23:48 +00001065 template<>
1066 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1067 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1068 // Make everyone now use a constant of the new type...
1069 Constant *New = ConstantPointerNull::get(NewTy);
1070 assert(New != OldC && "Didn't replace constant??");
1071 OldC->uncheckedReplaceAllUsesWith(New);
1072 OldC->destroyConstant(); // This constant is now dead, destroy it.
1073 }
1074 };
1075}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001076
Chris Lattner98fa07b2003-05-23 20:03:32 +00001077static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001078
Chris Lattner3e650af2004-08-04 04:48:01 +00001079static char getValType(ConstantPointerNull *) {
1080 return 0;
1081}
1082
1083
Chris Lattner3462ae32001-12-03 22:26:30 +00001084ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner98fa07b2003-05-23 20:03:32 +00001085 return NullPtrConstants.getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001086}
1087
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001088// destroyConstant - Remove the constant from the constant table...
1089//
1090void ConstantPointerNull::destroyConstant() {
1091 NullPtrConstants.remove(this);
1092 destroyConstantImpl();
1093}
1094
1095
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001096//---- UndefValue::get() implementation...
1097//
1098
1099namespace llvm {
1100 // UndefValue does not take extra "value" argument...
1101 template<class ValType>
1102 struct ConstantCreator<UndefValue, Type, ValType> {
1103 static UndefValue *create(const Type *Ty, const ValType &V) {
1104 return new UndefValue(Ty);
1105 }
1106 };
1107
1108 template<>
1109 struct ConvertConstantType<UndefValue, Type> {
1110 static void convert(UndefValue *OldC, const Type *NewTy) {
1111 // Make everyone now use a constant of the new type.
1112 Constant *New = UndefValue::get(NewTy);
1113 assert(New != OldC && "Didn't replace constant??");
1114 OldC->uncheckedReplaceAllUsesWith(New);
1115 OldC->destroyConstant(); // This constant is now dead, destroy it.
1116 }
1117 };
1118}
1119
1120static ValueMap<char, Type, UndefValue> UndefValueConstants;
1121
1122static char getValType(UndefValue *) {
1123 return 0;
1124}
1125
1126
1127UndefValue *UndefValue::get(const Type *Ty) {
1128 return UndefValueConstants.getOrCreate(Ty, 0);
1129}
1130
1131// destroyConstant - Remove the constant from the constant table.
1132//
1133void UndefValue::destroyConstant() {
1134 UndefValueConstants.remove(this);
1135 destroyConstantImpl();
1136}
1137
1138
1139
1140
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001141//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001142//
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001143typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Chris Lattner98fa07b2003-05-23 20:03:32 +00001144
Chris Lattner189d19f2003-11-21 20:23:48 +00001145namespace llvm {
1146 template<>
1147 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
1148 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
1149 if (V.first == Instruction::Cast)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001150 return new UnaryConstantExpr(Instruction::Cast, V.second[0], Ty);
Chris Lattner189d19f2003-11-21 20:23:48 +00001151 if ((V.first >= Instruction::BinaryOpsBegin &&
1152 V.first < Instruction::BinaryOpsEnd) ||
1153 V.first == Instruction::Shl || V.first == Instruction::Shr)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001154 return new BinaryConstantExpr(V.first, V.second[0], V.second[1]);
Chris Lattnerb7897ce2004-03-30 22:51:03 +00001155 if (V.first == Instruction::Select)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001156 return new SelectConstantExpr(V.second[0], V.second[1], V.second[2]);
Robert Bocchino23004482006-01-10 19:05:34 +00001157 if (V.first == Instruction::ExtractElement)
1158 return new ExtractElementConstantExpr(V.second[0], V.second[1]);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001159
Chris Lattner189d19f2003-11-21 20:23:48 +00001160 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001161
Chris Lattner189d19f2003-11-21 20:23:48 +00001162 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001163 return new GetElementPtrConstantExpr(V.second[0], IdxList, Ty);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001164 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001165 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001166
Chris Lattner189d19f2003-11-21 20:23:48 +00001167 template<>
1168 struct ConvertConstantType<ConstantExpr, Type> {
1169 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1170 Constant *New;
1171 switch (OldC->getOpcode()) {
1172 case Instruction::Cast:
1173 New = ConstantExpr::getCast(OldC->getOperand(0), NewTy);
1174 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001175 case Instruction::Select:
1176 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1177 OldC->getOperand(1),
1178 OldC->getOperand(2));
1179 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001180 case Instruction::Shl:
1181 case Instruction::Shr:
1182 New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
1183 OldC->getOperand(0), OldC->getOperand(1));
1184 break;
1185 default:
1186 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
1187 OldC->getOpcode() < Instruction::BinaryOpsEnd);
1188 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1189 OldC->getOperand(1));
1190 break;
1191 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001192 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001193 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
1194 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), Idx);
Chris Lattner189d19f2003-11-21 20:23:48 +00001195 break;
1196 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001197
Chris Lattner189d19f2003-11-21 20:23:48 +00001198 assert(New != OldC && "Didn't replace constant??");
1199 OldC->uncheckedReplaceAllUsesWith(New);
1200 OldC->destroyConstant(); // This constant is now dead, destroy it.
1201 }
1202 };
1203} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001204
1205
Chris Lattner3e650af2004-08-04 04:48:01 +00001206static ExprMapKeyType getValType(ConstantExpr *CE) {
1207 std::vector<Constant*> Operands;
1208 Operands.reserve(CE->getNumOperands());
1209 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1210 Operands.push_back(cast<Constant>(CE->getOperand(i)));
1211 return ExprMapKeyType(CE->getOpcode(), Operands);
1212}
1213
Chris Lattner98fa07b2003-05-23 20:03:32 +00001214static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001215
Chris Lattner46b3d302003-04-16 22:40:51 +00001216Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001217 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1218
Chris Lattneracdbe712003-04-17 19:24:48 +00001219 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
1220 return FC; // Fold a few common cases...
1221
Vikram S. Adve4c485332002-07-15 18:19:33 +00001222 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001223 std::vector<Constant*> argVec(1, C);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001224 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
1225 return ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001226}
Chris Lattnerd7a73302001-10-13 06:57:33 +00001227
Chris Lattnerdd284742004-04-04 23:20:30 +00001228Constant *ConstantExpr::getSignExtend(Constant *C, const Type *Ty) {
Chris Lattner1ece6f82005-01-01 15:59:57 +00001229 assert(C->getType()->isIntegral() && Ty->isIntegral() &&
Chris Lattnerdd284742004-04-04 23:20:30 +00001230 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1231 "This is an illegal sign extension!");
Chris Lattner1ece6f82005-01-01 15:59:57 +00001232 if (C->getType() != Type::BoolTy) {
1233 C = ConstantExpr::getCast(C, C->getType()->getSignedVersion());
1234 return ConstantExpr::getCast(C, Ty);
1235 } else {
1236 if (C == ConstantBool::True)
1237 return ConstantIntegral::getAllOnesValue(Ty);
1238 else
1239 return ConstantIntegral::getNullValue(Ty);
1240 }
Chris Lattnerdd284742004-04-04 23:20:30 +00001241}
1242
1243Constant *ConstantExpr::getZeroExtend(Constant *C, const Type *Ty) {
Chris Lattner1ece6f82005-01-01 15:59:57 +00001244 assert(C->getType()->isIntegral() && Ty->isIntegral() &&
Chris Lattnerdd284742004-04-04 23:20:30 +00001245 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1246 "This is an illegal zero extension!");
Chris Lattner1ece6f82005-01-01 15:59:57 +00001247 if (C->getType() != Type::BoolTy)
1248 C = ConstantExpr::getCast(C, C->getType()->getUnsignedVersion());
Chris Lattnerdd284742004-04-04 23:20:30 +00001249 return ConstantExpr::getCast(C, Ty);
1250}
1251
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001252Constant *ConstantExpr::getSizeOf(const Type *Ty) {
Chris Lattneracc4e542004-12-13 19:48:51 +00001253 // sizeof is implemented as: (ulong) gep (Ty*)null, 1
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001254 return getCast(
Chris Lattneracc4e542004-12-13 19:48:51 +00001255 getGetElementPtr(getNullValue(PointerType::get(Ty)),
1256 std::vector<Constant*>(1, ConstantInt::get(Type::UIntTy, 1))),
1257 Type::ULongTy);
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001258}
1259
Alkis Evlogimenos9160d5f2005-03-19 11:40:31 +00001260Constant *ConstantExpr::getPtrPtrFromArrayPtr(Constant *C) {
1261 // pointer from array is implemented as: getelementptr arr ptr, 0, 0
1262 static std::vector<Constant*> Indices(2, ConstantUInt::get(Type::UIntTy, 0));
1263
1264 return ConstantExpr::getGetElementPtr(C, Indices);
1265}
1266
Chris Lattnerb50d1352003-10-05 00:17:43 +00001267Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
1268 Constant *C1, Constant *C2) {
Chris Lattner5645e8a2004-01-12 19:04:55 +00001269 if (Opcode == Instruction::Shl || Opcode == Instruction::Shr)
1270 return getShiftTy(ReqTy, Opcode, C1, C2);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001271 // Check the operands for consistency first
1272 assert((Opcode >= Instruction::BinaryOpsBegin &&
1273 Opcode < Instruction::BinaryOpsEnd) &&
1274 "Invalid opcode in binary constant expression");
1275 assert(C1->getType() == C2->getType() &&
1276 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001277
Chris Lattner29ca2c62004-08-04 18:50:09 +00001278 if (ReqTy == C1->getType() || (Instruction::isRelational(Opcode) &&
1279 ReqTy == Type::BoolTy))
Chris Lattnerb50d1352003-10-05 00:17:43 +00001280 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1281 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001282
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001283 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001284 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001285 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001286}
1287
Chris Lattner29ca2c62004-08-04 18:50:09 +00001288Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001289#ifndef NDEBUG
1290 switch (Opcode) {
1291 case Instruction::Add: case Instruction::Sub:
1292 case Instruction::Mul: case Instruction::Div:
1293 case Instruction::Rem:
1294 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattnerc421a262006-01-04 01:01:04 +00001295 assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint() ||
1296 isa<PackedType>(C1->getType())) &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001297 "Tried to create an arithmetic operation on a non-arithmetic type!");
1298 break;
1299 case Instruction::And:
1300 case Instruction::Or:
1301 case Instruction::Xor:
1302 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattnerc421a262006-01-04 01:01:04 +00001303 assert((C1->getType()->isIntegral() || isa<PackedType>(C1->getType())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001304 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001305 break;
1306 case Instruction::SetLT: case Instruction::SetGT: case Instruction::SetLE:
1307 case Instruction::SetGE: case Instruction::SetEQ: case Instruction::SetNE:
1308 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1309 break;
1310 case Instruction::Shl:
1311 case Instruction::Shr:
1312 assert(C2->getType() == Type::UByteTy && "Shift should be by ubyte!");
Chris Lattnerc421a262006-01-04 01:01:04 +00001313 assert((C1->getType()->isInteger() || isa<PackedType>(C1->getType())) &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001314 "Tried to create a shift operation on a non-integer type!");
1315 break;
1316 default:
1317 break;
1318 }
1319#endif
1320
Chris Lattner29ca2c62004-08-04 18:50:09 +00001321 if (Instruction::isRelational(Opcode))
1322 return getTy(Type::BoolTy, Opcode, C1, C2);
1323 else
1324 return getTy(C1->getType(), Opcode, C1, C2);
1325}
1326
Chris Lattner6e415c02004-03-12 05:54:04 +00001327Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
1328 Constant *V1, Constant *V2) {
1329 assert(C->getType() == Type::BoolTy && "Select condition must be bool!");
1330 assert(V1->getType() == V2->getType() && "Select value types must match!");
1331 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
1332
1333 if (ReqTy == V1->getType())
1334 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1335 return SC; // Fold common cases
1336
1337 std::vector<Constant*> argVec(3, C);
1338 argVec[1] = V1;
1339 argVec[2] = V2;
1340 ExprMapKeyType Key = std::make_pair(Instruction::Select, argVec);
1341 return ExprConstants.getOrCreate(ReqTy, Key);
1342}
1343
Chris Lattner9eb2b522004-01-12 19:12:58 +00001344/// getShiftTy - Return a shift left or shift right constant expr
Chris Lattnerb50d1352003-10-05 00:17:43 +00001345Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
1346 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001347 // Check the operands for consistency first
1348 assert((Opcode == Instruction::Shl ||
1349 Opcode == Instruction::Shr) &&
1350 "Invalid opcode in binary constant expression");
1351 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
1352 "Invalid operand types for Shift constant expr!");
1353
Chris Lattner0bba7712004-01-12 20:40:42 +00001354 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001355 return FC; // Fold a few common cases...
1356
1357 // Look up the constant in the table first to ensure uniqueness
1358 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001359 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001360 return ExprConstants.getOrCreate(ReqTy, Key);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001361}
1362
1363
Chris Lattnerb50d1352003-10-05 00:17:43 +00001364Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner13128ab2004-10-11 22:52:25 +00001365 const std::vector<Value*> &IdxList) {
1366 assert(GetElementPtrInst::getIndexedType(C->getType(), IdxList, true) &&
Chris Lattner04b60fe2004-02-16 20:46:13 +00001367 "GEP indices invalid!");
1368
Chris Lattneracdbe712003-04-17 19:24:48 +00001369 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
1370 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001371
Chris Lattnerb50d1352003-10-05 00:17:43 +00001372 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001373 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001374 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00001375 std::vector<Constant*> ArgVec;
1376 ArgVec.reserve(IdxList.size()+1);
1377 ArgVec.push_back(C);
1378 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
1379 ArgVec.push_back(cast<Constant>(IdxList[i]));
1380 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,ArgVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001381 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001382}
1383
Chris Lattnerb50d1352003-10-05 00:17:43 +00001384Constant *ConstantExpr::getGetElementPtr(Constant *C,
1385 const std::vector<Constant*> &IdxList){
1386 // Get the result type of the getelementptr!
1387 std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
1388
1389 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
1390 true);
1391 assert(Ty && "GEP indices invalid!");
Chris Lattner13128ab2004-10-11 22:52:25 +00001392 return getGetElementPtrTy(PointerType::get(Ty), C, VIdxList);
1393}
1394
1395Constant *ConstantExpr::getGetElementPtr(Constant *C,
1396 const std::vector<Value*> &IdxList) {
1397 // Get the result type of the getelementptr!
1398 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
1399 true);
1400 assert(Ty && "GEP indices invalid!");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001401 return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
1402}
1403
Robert Bocchino23004482006-01-10 19:05:34 +00001404Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
1405 Constant *Idx) {
Robert Bocchinode7f1c92006-01-10 20:03:46 +00001406 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
1407 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00001408 // Look up the constant in the table first to ensure uniqueness
1409 std::vector<Constant*> ArgVec(1, Val);
1410 ArgVec.push_back(Idx);
1411 const ExprMapKeyType &Key = std::make_pair(Instruction::ExtractElement,ArgVec);
1412 return ExprConstants.getOrCreate(ReqTy, Key);
1413}
1414
1415Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
1416 assert(isa<PackedType>(Val->getType()) &&
1417 "Tried to create extractelement operation on non-packed type!");
1418 assert(Idx->getType() == Type::UIntTy &&
1419 "Index must be uint type!");
1420 return getExtractElementTy(cast<PackedType>(Val->getType())->getElementType(),
1421 Val, Idx);
1422}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001423
Vikram S. Adve4c485332002-07-15 18:19:33 +00001424// destroyConstant - Remove the constant from the constant table...
1425//
1426void ConstantExpr::destroyConstant() {
1427 ExprConstants.remove(this);
1428 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001429}
1430
Chris Lattner3cd8c562002-07-30 18:54:25 +00001431const char *ConstantExpr::getOpcodeName() const {
1432 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001433}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00001434
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001435//===----------------------------------------------------------------------===//
1436// replaceUsesOfWithOnConstant implementations
1437
1438void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001439 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001440 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00001441 Constant *ToC = cast<Constant>(To);
Chris Lattnerdff59112005-10-04 18:47:09 +00001442
1443 unsigned OperandToUpdate = U-OperandList;
1444 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1445
Chris Lattnerb64419a2005-10-03 22:51:37 +00001446 std::pair<ArrayConstantsTy::MapKey, ConstantArray*> Lookup;
1447 Lookup.first.first = getType();
1448 Lookup.second = this;
Chris Lattnerdff59112005-10-04 18:47:09 +00001449
Chris Lattnerb64419a2005-10-03 22:51:37 +00001450 std::vector<Constant*> &Values = Lookup.first.second;
1451 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattnerdff59112005-10-04 18:47:09 +00001452
Chris Lattner8760ec72005-10-04 01:17:50 +00001453 // Fill values with the modified operands of the constant array. Also,
1454 // compute whether this turns into an all-zeros array.
Chris Lattnerdff59112005-10-04 18:47:09 +00001455 bool isAllZeros = false;
1456 if (!ToC->isNullValue()) {
1457 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1458 Values.push_back(cast<Constant>(O->get()));
1459 } else {
1460 isAllZeros = true;
1461 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1462 Constant *Val = cast<Constant>(O->get());
1463 Values.push_back(Val);
1464 if (isAllZeros) isAllZeros = Val->isNullValue();
1465 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001466 }
Chris Lattnerdff59112005-10-04 18:47:09 +00001467 Values[OperandToUpdate] = ToC;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001468
Chris Lattnerb64419a2005-10-03 22:51:37 +00001469 Constant *Replacement = 0;
1470 if (isAllZeros) {
1471 Replacement = ConstantAggregateZero::get(getType());
1472 } else {
1473 // Check to see if we have this array type already.
1474 bool Exists;
1475 ArrayConstantsTy::MapIterator I =
1476 ArrayConstants.InsertOrGetItem(Lookup, Exists);
1477
1478 if (Exists) {
1479 Replacement = I->second;
1480 } else {
1481 // Okay, the new shape doesn't exist in the system yet. Instead of
1482 // creating a new constant array, inserting it, replaceallusesof'ing the
1483 // old with the new, then deleting the old... just update the current one
1484 // in place!
Chris Lattner3b793c62005-10-04 21:35:50 +00001485 ArrayConstants.MoveConstantToNewSlot(this, I);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001486
Chris Lattnerdff59112005-10-04 18:47:09 +00001487 // Update to the new value.
1488 setOperand(OperandToUpdate, ToC);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001489 return;
1490 }
1491 }
1492
1493 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001494 assert(Replacement != this && "I didn't contain From!");
1495
Chris Lattner7a1450d2005-10-04 18:13:04 +00001496 // Everyone using this now uses the replacement.
1497 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001498
1499 // Delete the old constant!
1500 destroyConstant();
1501}
1502
1503void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001504 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001505 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00001506 Constant *ToC = cast<Constant>(To);
1507
Chris Lattnerdff59112005-10-04 18:47:09 +00001508 unsigned OperandToUpdate = U-OperandList;
1509 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1510
Chris Lattner8760ec72005-10-04 01:17:50 +00001511 std::pair<StructConstantsTy::MapKey, ConstantStruct*> Lookup;
1512 Lookup.first.first = getType();
1513 Lookup.second = this;
1514 std::vector<Constant*> &Values = Lookup.first.second;
1515 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001516
Chris Lattnerdff59112005-10-04 18:47:09 +00001517
Chris Lattner8760ec72005-10-04 01:17:50 +00001518 // Fill values with the modified operands of the constant struct. Also,
1519 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00001520 bool isAllZeros = false;
1521 if (!ToC->isNullValue()) {
1522 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1523 Values.push_back(cast<Constant>(O->get()));
1524 } else {
1525 isAllZeros = true;
1526 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1527 Constant *Val = cast<Constant>(O->get());
1528 Values.push_back(Val);
1529 if (isAllZeros) isAllZeros = Val->isNullValue();
1530 }
Chris Lattner8760ec72005-10-04 01:17:50 +00001531 }
Chris Lattnerdff59112005-10-04 18:47:09 +00001532 Values[OperandToUpdate] = ToC;
1533
Chris Lattner8760ec72005-10-04 01:17:50 +00001534 Constant *Replacement = 0;
1535 if (isAllZeros) {
1536 Replacement = ConstantAggregateZero::get(getType());
1537 } else {
1538 // Check to see if we have this array type already.
1539 bool Exists;
1540 StructConstantsTy::MapIterator I =
Chris Lattner3b793c62005-10-04 21:35:50 +00001541 StructConstants.InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00001542
1543 if (Exists) {
1544 Replacement = I->second;
1545 } else {
1546 // Okay, the new shape doesn't exist in the system yet. Instead of
1547 // creating a new constant struct, inserting it, replaceallusesof'ing the
1548 // old with the new, then deleting the old... just update the current one
1549 // in place!
Chris Lattner3b793c62005-10-04 21:35:50 +00001550 StructConstants.MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00001551
Chris Lattnerdff59112005-10-04 18:47:09 +00001552 // Update to the new value.
1553 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00001554 return;
1555 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001556 }
1557
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001558 assert(Replacement != this && "I didn't contain From!");
1559
Chris Lattner7a1450d2005-10-04 18:13:04 +00001560 // Everyone using this now uses the replacement.
1561 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001562
1563 // Delete the old constant!
1564 destroyConstant();
1565}
1566
1567void ConstantPacked::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001568 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001569 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
1570
1571 std::vector<Constant*> Values;
1572 Values.reserve(getNumOperands()); // Build replacement array...
1573 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1574 Constant *Val = getOperand(i);
1575 if (Val == From) Val = cast<Constant>(To);
1576 Values.push_back(Val);
1577 }
1578
1579 Constant *Replacement = ConstantPacked::get(getType(), Values);
1580 assert(Replacement != this && "I didn't contain From!");
1581
Chris Lattner7a1450d2005-10-04 18:13:04 +00001582 // Everyone using this now uses the replacement.
1583 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001584
1585 // Delete the old constant!
1586 destroyConstant();
1587}
1588
1589void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001590 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001591 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
1592 Constant *To = cast<Constant>(ToV);
1593
1594 Constant *Replacement = 0;
1595 if (getOpcode() == Instruction::GetElementPtr) {
1596 std::vector<Constant*> Indices;
1597 Constant *Pointer = getOperand(0);
1598 Indices.reserve(getNumOperands()-1);
1599 if (Pointer == From) Pointer = To;
1600
1601 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1602 Constant *Val = getOperand(i);
1603 if (Val == From) Val = To;
1604 Indices.push_back(Val);
1605 }
1606 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
1607 } else if (getOpcode() == Instruction::Cast) {
1608 assert(getOperand(0) == From && "Cast only has one use!");
1609 Replacement = ConstantExpr::getCast(To, getType());
1610 } else if (getOpcode() == Instruction::Select) {
1611 Constant *C1 = getOperand(0);
1612 Constant *C2 = getOperand(1);
1613 Constant *C3 = getOperand(2);
1614 if (C1 == From) C1 = To;
1615 if (C2 == From) C2 = To;
1616 if (C3 == From) C3 = To;
1617 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00001618 } else if (getOpcode() == Instruction::ExtractElement) {
1619 Constant *C1 = getOperand(0);
1620 Constant *C2 = getOperand(1);
1621 if (C1 == From) C1 = To;
1622 if (C2 == From) C2 = To;
1623 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001624 } else if (getNumOperands() == 2) {
1625 Constant *C1 = getOperand(0);
1626 Constant *C2 = getOperand(1);
1627 if (C1 == From) C1 = To;
1628 if (C2 == From) C2 = To;
1629 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
1630 } else {
1631 assert(0 && "Unknown ConstantExpr type!");
1632 return;
1633 }
1634
1635 assert(Replacement != this && "I didn't contain From!");
1636
Chris Lattner7a1450d2005-10-04 18:13:04 +00001637 // Everyone using this now uses the replacement.
1638 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001639
1640 // Delete the old constant!
1641 destroyConstant();
1642}
1643
1644
1645
Chris Lattner99a669b2004-11-19 16:39:44 +00001646/// clearAllValueMaps - This method frees all internal memory used by the
1647/// constant subsystem, which can be used in environments where this memory
1648/// is otherwise reported as a leak.
1649void Constant::clearAllValueMaps() {
1650 std::vector<Constant *> Constants;
1651
1652 DoubleConstants.clear(Constants);
1653 FloatConstants.clear(Constants);
1654 SIntConstants.clear(Constants);
1655 UIntConstants.clear(Constants);
1656 AggZeroConstants.clear(Constants);
1657 ArrayConstants.clear(Constants);
1658 StructConstants.clear(Constants);
1659 PackedConstants.clear(Constants);
1660 NullPtrConstants.clear(Constants);
1661 UndefValueConstants.clear(Constants);
1662 ExprConstants.clear(Constants);
1663
Misha Brukmanb1c93172005-04-21 23:48:37 +00001664 for (std::vector<Constant *>::iterator I = Constants.begin(),
Chris Lattner99a669b2004-11-19 16:39:44 +00001665 E = Constants.end(); I != E; ++I)
1666 (*I)->dropAllReferences();
1667 for (std::vector<Constant *>::iterator I = Constants.begin(),
1668 E = Constants.end(); I != E; ++I)
1669 (*I)->destroyConstantImpl();
1670 Constants.clear();
1671}