blob: 621e2ca94bff7c87adf329dc6e1d52f957dfa099 [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 Lattner3d27be12006-08-27 12:54:02 +000023#include "llvm/Support/Compiler.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000024#include <algorithm>
Reid Spencercf394bf2004-07-04 11:51:24 +000025#include <iostream>
Chris Lattner189d19f2003-11-21 20:23:48 +000026using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000027
Chris Lattner3462ae32001-12-03 22:26:30 +000028ConstantBool *ConstantBool::True = new ConstantBool(true);
29ConstantBool *ConstantBool::False = new ConstantBool(false);
Chris Lattner2f7c9632001-06-06 20:29:01 +000030
Chris Lattner9655e542001-07-20 19:16:02 +000031
Chris Lattner2f7c9632001-06-06 20:29:01 +000032//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000033// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000034//===----------------------------------------------------------------------===//
35
Chris Lattner3462ae32001-12-03 22:26:30 +000036void Constant::destroyConstantImpl() {
37 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000038 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000039 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +000040 // but they don't know that. Because we only find out when the CPV is
41 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000042 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000043 //
44 while (!use_empty()) {
45 Value *V = use_back();
46#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000047 if (!isa<Constant>(V))
48 std::cerr << "While deleting: " << *this
49 << "\n\nUse still stuck around after Def is destroyed: "
50 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000051#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000052 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000053 Constant *CV = cast<Constant>(V);
54 CV->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +000055
56 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000057 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000058 }
59
60 // Value has no outstanding references it is safe to delete it now...
61 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000062}
Chris Lattner2f7c9632001-06-06 20:29:01 +000063
Chris Lattnerb1585a92002-08-13 17:50:20 +000064// Static constructor to create a '0' constant of arbitrary type...
65Constant *Constant::getNullValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +000066 switch (Ty->getTypeID()) {
Chris Lattner3e88ef92003-10-03 19:34:51 +000067 case Type::BoolTyID: {
68 static Constant *NullBool = ConstantBool::get(false);
69 return NullBool;
70 }
71 case Type::SByteTyID: {
72 static Constant *NullSByte = ConstantSInt::get(Type::SByteTy, 0);
73 return NullSByte;
74 }
75 case Type::UByteTyID: {
76 static Constant *NullUByte = ConstantUInt::get(Type::UByteTy, 0);
77 return NullUByte;
78 }
79 case Type::ShortTyID: {
80 static Constant *NullShort = ConstantSInt::get(Type::ShortTy, 0);
81 return NullShort;
82 }
83 case Type::UShortTyID: {
84 static Constant *NullUShort = ConstantUInt::get(Type::UShortTy, 0);
85 return NullUShort;
86 }
87 case Type::IntTyID: {
88 static Constant *NullInt = ConstantSInt::get(Type::IntTy, 0);
89 return NullInt;
90 }
91 case Type::UIntTyID: {
92 static Constant *NullUInt = ConstantUInt::get(Type::UIntTy, 0);
93 return NullUInt;
94 }
95 case Type::LongTyID: {
96 static Constant *NullLong = ConstantSInt::get(Type::LongTy, 0);
97 return NullLong;
98 }
99 case Type::ULongTyID: {
100 static Constant *NullULong = ConstantUInt::get(Type::ULongTy, 0);
101 return NullULong;
102 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000103
Chris Lattner3e88ef92003-10-03 19:34:51 +0000104 case Type::FloatTyID: {
105 static Constant *NullFloat = ConstantFP::get(Type::FloatTy, 0);
106 return NullFloat;
107 }
108 case Type::DoubleTyID: {
109 static Constant *NullDouble = ConstantFP::get(Type::DoubleTy, 0);
110 return NullDouble;
111 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000112
Misha Brukmanb1c93172005-04-21 23:48:37 +0000113 case Type::PointerTyID:
Chris Lattnerb1585a92002-08-13 17:50:20 +0000114 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner3e88ef92003-10-03 19:34:51 +0000115
Chris Lattner9fba3da2004-02-15 05:53:04 +0000116 case Type::StructTyID:
117 case Type::ArrayTyID:
Brian Gaeke02209042004-08-20 06:00:58 +0000118 case Type::PackedTyID:
Chris Lattner9fba3da2004-02-15 05:53:04 +0000119 return ConstantAggregateZero::get(Ty);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000120 default:
Reid Spencercf394bf2004-07-04 11:51:24 +0000121 // Function, Label, or Opaque type?
122 assert(!"Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000123 return 0;
124 }
125}
126
127// Static constructor to create the maximum constant of an integral type...
128ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000129 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000130 case Type::BoolTyID: return ConstantBool::True;
131 case Type::SByteTyID:
132 case Type::ShortTyID:
133 case Type::IntTyID:
134 case Type::LongTyID: {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000135 // Calculate 011111111111111...
Chris Lattnerb1585a92002-08-13 17:50:20 +0000136 unsigned TypeBits = Ty->getPrimitiveSize()*8;
137 int64_t Val = INT64_MAX; // All ones
138 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
139 return ConstantSInt::get(Ty, Val);
140 }
141
142 case Type::UByteTyID:
143 case Type::UShortTyID:
144 case Type::UIntTyID:
145 case Type::ULongTyID: return getAllOnesValue(Ty);
146
Chris Lattner31408f72002-08-14 17:12:13 +0000147 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000148 }
149}
150
151// Static constructor to create the minimum constant for an integral type...
152ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000153 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000154 case Type::BoolTyID: return ConstantBool::False;
155 case Type::SByteTyID:
156 case Type::ShortTyID:
157 case Type::IntTyID:
158 case Type::LongTyID: {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000159 // Calculate 1111111111000000000000
Chris Lattnerb1585a92002-08-13 17:50:20 +0000160 unsigned TypeBits = Ty->getPrimitiveSize()*8;
161 int64_t Val = -1; // All ones
162 Val <<= TypeBits-1; // Shift over to the right spot
163 return ConstantSInt::get(Ty, Val);
164 }
165
166 case Type::UByteTyID:
167 case Type::UShortTyID:
168 case Type::UIntTyID:
169 case Type::ULongTyID: return ConstantUInt::get(Ty, 0);
170
Chris Lattner31408f72002-08-14 17:12:13 +0000171 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000172 }
173}
174
175// Static constructor to create an integral constant with all bits set
176ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000177 switch (Ty->getTypeID()) {
Chris Lattnerb1585a92002-08-13 17:50:20 +0000178 case Type::BoolTyID: return ConstantBool::True;
179 case Type::SByteTyID:
180 case Type::ShortTyID:
181 case Type::IntTyID:
182 case Type::LongTyID: return ConstantSInt::get(Ty, -1);
183
184 case Type::UByteTyID:
185 case Type::UShortTyID:
186 case Type::UIntTyID:
187 case Type::ULongTyID: {
188 // Calculate ~0 of the right type...
189 unsigned TypeBits = Ty->getPrimitiveSize()*8;
190 uint64_t Val = ~0ULL; // All ones
191 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
192 return ConstantUInt::get(Ty, Val);
193 }
Chris Lattner31408f72002-08-14 17:12:13 +0000194 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000195 }
196}
197
Chris Lattner83e5d392003-03-10 22:39:02 +0000198bool ConstantUInt::isAllOnesValue() const {
199 unsigned TypeBits = getType()->getPrimitiveSize()*8;
200 uint64_t Val = ~0ULL; // All ones
201 Val >>= 64-TypeBits; // Shift out inappropriate bits
202 return getValue() == Val;
203}
204
Chris Lattnerb1585a92002-08-13 17:50:20 +0000205
Chris Lattner2f7c9632001-06-06 20:29:01 +0000206//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +0000207// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +0000208//===----------------------------------------------------------------------===//
209
210//===----------------------------------------------------------------------===//
211// Normal Constructors
212
Chris Lattnere7e139e2005-09-27 06:09:08 +0000213ConstantIntegral::ConstantIntegral(const Type *Ty, ValueTy VT, uint64_t V)
214 : Constant(Ty, VT, 0, 0) {
Chris Lattner265eb642004-06-21 12:12:12 +0000215 Val.Unsigned = V;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000216}
Chris Lattner49d855c2001-09-07 16:46:31 +0000217
Chris Lattnere7e139e2005-09-27 06:09:08 +0000218ConstantBool::ConstantBool(bool V)
219 : ConstantIntegral(Type::BoolTy, ConstantBoolVal, V) {
Chris Lattner265eb642004-06-21 12:12:12 +0000220}
221
Chris Lattnere7e139e2005-09-27 06:09:08 +0000222ConstantInt::ConstantInt(const Type *Ty, ValueTy VT, uint64_t V)
223 : ConstantIntegral(Ty, VT, V) {
Chris Lattner7309d662001-07-21 19:16:08 +0000224}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000225
Chris Lattnere7e139e2005-09-27 06:09:08 +0000226ConstantSInt::ConstantSInt(const Type *Ty, int64_t V)
227 : ConstantInt(Ty, ConstantSIntVal, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000228 assert(Ty->isInteger() && Ty->isSigned() &&
Reid Spencerf064bb22005-03-09 15:19:41 +0000229 "Illegal type for signed integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000230 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000231}
232
Chris Lattnere7e139e2005-09-27 06:09:08 +0000233ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V)
234 : ConstantInt(Ty, ConstantUIntVal, V) {
Chris Lattner236c1292002-09-11 01:21:04 +0000235 assert(Ty->isInteger() && Ty->isUnsigned() &&
236 "Illegal type for unsigned integer constant!");
Chris Lattner9655e542001-07-20 19:16:02 +0000237 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000238}
239
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000240ConstantFP::ConstantFP(const Type *Ty, double V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000241 : Constant(Ty, ConstantFPVal, 0, 0) {
Chris Lattner9655e542001-07-20 19:16:02 +0000242 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000243 Val = V;
244}
245
Chris Lattner3462ae32001-12-03 22:26:30 +0000246ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000247 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000248 : Constant(T, ConstantArrayVal, new Use[V.size()], V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000249 assert(V.size() == T->getNumElements() &&
250 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000251 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000252 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
253 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000254 Constant *C = *I;
255 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000256 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000257 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000258 "Initializer for array element doesn't match array element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000259 OL->init(C, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000260 }
261}
262
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000263ConstantArray::~ConstantArray() {
264 delete [] OperandList;
265}
266
Chris Lattner3462ae32001-12-03 22:26:30 +0000267ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000268 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000269 : Constant(T, ConstantStructVal, new Use[V.size()], V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000270 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000271 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000272 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000273 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
274 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000275 Constant *C = *I;
276 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000277 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000278 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000279 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000280 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000281 "Initializer for struct element doesn't match struct element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000282 OL->init(C, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000283 }
284}
285
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000286ConstantStruct::~ConstantStruct() {
287 delete [] OperandList;
288}
289
290
Brian Gaeke02209042004-08-20 06:00:58 +0000291ConstantPacked::ConstantPacked(const PackedType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000292 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000293 : Constant(T, ConstantPackedVal, new Use[V.size()], V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000294 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000295 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
296 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000297 Constant *C = *I;
298 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000299 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000300 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000301 "Initializer for packed element doesn't match packed element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000302 OL->init(C, this);
Brian Gaeke02209042004-08-20 06:00:58 +0000303 }
304}
305
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000306ConstantPacked::~ConstantPacked() {
307 delete [] OperandList;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000308}
309
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000310/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
311/// behind the scenes to implement unary constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000312namespace {
313class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000314 Use Op;
315public:
316 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
317 : ConstantExpr(Ty, Opcode, &Op, 1), Op(C, this) {}
318};
Chris Lattner02157b02006-06-28 21:38:54 +0000319}
Chris Lattner6e415c02004-03-12 05:54:04 +0000320
Chris Lattner22ced562003-06-22 20:48:30 +0000321static bool isSetCC(unsigned Opcode) {
322 return Opcode == Instruction::SetEQ || Opcode == Instruction::SetNE ||
323 Opcode == Instruction::SetLT || Opcode == Instruction::SetGT ||
324 Opcode == Instruction::SetLE || Opcode == Instruction::SetGE;
325}
326
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000327/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
328/// behind the scenes to implement binary constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000329namespace {
330class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000331 Use Ops[2];
332public:
333 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
334 : ConstantExpr(isSetCC(Opcode) ? Type::BoolTy : C1->getType(),
335 Opcode, Ops, 2) {
336 Ops[0].init(C1, this);
337 Ops[1].init(C2, this);
338 }
339};
Chris Lattner02157b02006-06-28 21:38:54 +0000340}
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000341
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000342/// SelectConstantExpr - This class is private to Constants.cpp, and is used
343/// behind the scenes to implement select constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000344namespace {
345class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000346 Use Ops[3];
347public:
348 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
349 : ConstantExpr(C2->getType(), Instruction::Select, Ops, 3) {
350 Ops[0].init(C1, this);
351 Ops[1].init(C2, this);
352 Ops[2].init(C3, this);
353 }
354};
Chris Lattner02157b02006-06-28 21:38:54 +0000355}
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000356
Robert Bocchinoca27f032006-01-17 20:07:22 +0000357/// ExtractElementConstantExpr - This class is private to
358/// Constants.cpp, and is used behind the scenes to implement
359/// extractelement constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000360namespace {
361class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Robert Bocchino23004482006-01-10 19:05:34 +0000362 Use Ops[2];
363public:
364 ExtractElementConstantExpr(Constant *C1, Constant *C2)
365 : ConstantExpr(cast<PackedType>(C1->getType())->getElementType(),
366 Instruction::ExtractElement, Ops, 2) {
367 Ops[0].init(C1, this);
368 Ops[1].init(C2, this);
369 }
370};
Chris Lattner02157b02006-06-28 21:38:54 +0000371}
Robert Bocchino23004482006-01-10 19:05:34 +0000372
Robert Bocchinoca27f032006-01-17 20:07:22 +0000373/// InsertElementConstantExpr - This class is private to
374/// Constants.cpp, and is used behind the scenes to implement
375/// insertelement constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000376namespace {
377class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Robert Bocchinoca27f032006-01-17 20:07:22 +0000378 Use Ops[3];
379public:
380 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
381 : ConstantExpr(C1->getType(), Instruction::InsertElement,
382 Ops, 3) {
383 Ops[0].init(C1, this);
384 Ops[1].init(C2, this);
385 Ops[2].init(C3, this);
386 }
387};
Chris Lattner02157b02006-06-28 21:38:54 +0000388}
Robert Bocchinoca27f032006-01-17 20:07:22 +0000389
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000390/// ShuffleVectorConstantExpr - This class is private to
391/// Constants.cpp, and is used behind the scenes to implement
392/// shufflevector constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000393namespace {
394class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000395 Use Ops[3];
396public:
397 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
398 : ConstantExpr(C1->getType(), Instruction::ShuffleVector,
399 Ops, 3) {
400 Ops[0].init(C1, this);
401 Ops[1].init(C2, this);
402 Ops[2].init(C3, this);
403 }
404};
Chris Lattner02157b02006-06-28 21:38:54 +0000405}
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000406
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000407/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
408/// used behind the scenes to implement getelementpr constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000409namespace {
410struct VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000411 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
412 const Type *DestTy)
413 : ConstantExpr(DestTy, Instruction::GetElementPtr,
414 new Use[IdxList.size()+1], IdxList.size()+1) {
415 OperandList[0].init(C, this);
416 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
417 OperandList[i+1].init(IdxList[i], this);
418 }
419 ~GetElementPtrConstantExpr() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000420 delete [] OperandList;
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000421 }
422};
Chris Lattner02157b02006-06-28 21:38:54 +0000423}
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000424
Chris Lattner817175f2004-03-29 02:37:53 +0000425/// ConstantExpr::get* - Return some common constants without having to
426/// specify the full Instruction::OPCODE identifier.
427///
428Constant *ConstantExpr::getNeg(Constant *C) {
Chris Lattner3cdc27c2004-03-29 19:51:24 +0000429 if (!C->getType()->isFloatingPoint())
430 return get(Instruction::Sub, getNullValue(C->getType()), C);
431 else
432 return get(Instruction::Sub, ConstantFP::get(C->getType(), -0.0), C);
Chris Lattner817175f2004-03-29 02:37:53 +0000433}
434Constant *ConstantExpr::getNot(Constant *C) {
435 assert(isa<ConstantIntegral>(C) && "Cannot NOT a nonintegral type!");
436 return get(Instruction::Xor, C,
437 ConstantIntegral::getAllOnesValue(C->getType()));
438}
439Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
440 return get(Instruction::Add, C1, C2);
441}
442Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
443 return get(Instruction::Sub, C1, C2);
444}
445Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
446 return get(Instruction::Mul, C1, C2);
447}
448Constant *ConstantExpr::getDiv(Constant *C1, Constant *C2) {
449 return get(Instruction::Div, C1, C2);
450}
451Constant *ConstantExpr::getRem(Constant *C1, Constant *C2) {
452 return get(Instruction::Rem, C1, C2);
453}
454Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
455 return get(Instruction::And, C1, C2);
456}
457Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
458 return get(Instruction::Or, C1, C2);
459}
460Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
461 return get(Instruction::Xor, C1, C2);
462}
463Constant *ConstantExpr::getSetEQ(Constant *C1, Constant *C2) {
464 return get(Instruction::SetEQ, C1, C2);
465}
466Constant *ConstantExpr::getSetNE(Constant *C1, Constant *C2) {
467 return get(Instruction::SetNE, C1, C2);
468}
469Constant *ConstantExpr::getSetLT(Constant *C1, Constant *C2) {
470 return get(Instruction::SetLT, C1, C2);
471}
472Constant *ConstantExpr::getSetGT(Constant *C1, Constant *C2) {
473 return get(Instruction::SetGT, C1, C2);
474}
475Constant *ConstantExpr::getSetLE(Constant *C1, Constant *C2) {
476 return get(Instruction::SetLE, C1, C2);
477}
478Constant *ConstantExpr::getSetGE(Constant *C1, Constant *C2) {
479 return get(Instruction::SetGE, C1, C2);
480}
481Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
482 return get(Instruction::Shl, C1, C2);
483}
484Constant *ConstantExpr::getShr(Constant *C1, Constant *C2) {
485 return get(Instruction::Shr, C1, C2);
486}
487
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000488Constant *ConstantExpr::getUShr(Constant *C1, Constant *C2) {
489 if (C1->getType()->isUnsigned()) return getShr(C1, C2);
490 return getCast(getShr(getCast(C1,
491 C1->getType()->getUnsignedVersion()), C2), C1->getType());
492}
Chris Lattner817175f2004-03-29 02:37:53 +0000493
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000494Constant *ConstantExpr::getSShr(Constant *C1, Constant *C2) {
495 if (C1->getType()->isSigned()) return getShr(C1, C2);
496 return getCast(getShr(getCast(C1,
497 C1->getType()->getSignedVersion()), C2), C1->getType());
498}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000499
Chris Lattner7c1018a2006-07-14 19:37:40 +0000500/// getWithOperandReplaced - Return a constant expression identical to this
501/// one, but with the specified operand set to the specified value.
502Constant *ConstantExpr::getWithOperandReplaced(unsigned OpNo,
503 Constant *Op) const {
504 assert(OpNo < getNumOperands() && "Operand num is out of range!");
505 assert(Op->getType() == getOperand(OpNo)->getType() &&
506 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000507 if (getOperand(OpNo) == Op)
508 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000509
Chris Lattner227816342006-07-14 22:20:01 +0000510 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000511 switch (getOpcode()) {
512 case Instruction::Cast:
513 return ConstantExpr::getCast(Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000514 case Instruction::Select:
515 Op0 = (OpNo == 0) ? Op : getOperand(0);
516 Op1 = (OpNo == 1) ? Op : getOperand(1);
517 Op2 = (OpNo == 2) ? Op : getOperand(2);
518 return ConstantExpr::getSelect(Op0, Op1, Op2);
519 case Instruction::InsertElement:
520 Op0 = (OpNo == 0) ? Op : getOperand(0);
521 Op1 = (OpNo == 1) ? Op : getOperand(1);
522 Op2 = (OpNo == 2) ? Op : getOperand(2);
523 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
524 case Instruction::ExtractElement:
525 Op0 = (OpNo == 0) ? Op : getOperand(0);
526 Op1 = (OpNo == 1) ? Op : getOperand(1);
527 return ConstantExpr::getExtractElement(Op0, Op1);
528 case Instruction::ShuffleVector:
529 Op0 = (OpNo == 0) ? Op : getOperand(0);
530 Op1 = (OpNo == 1) ? Op : getOperand(1);
531 Op2 = (OpNo == 2) ? Op : getOperand(2);
532 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000533 case Instruction::GetElementPtr: {
534 std::vector<Constant*> Ops;
535 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
536 Ops.push_back(getOperand(i));
537 if (OpNo == 0)
538 return ConstantExpr::getGetElementPtr(Op, Ops);
539 Ops[OpNo-1] = Op;
540 return ConstantExpr::getGetElementPtr(getOperand(0), Ops);
541 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000542 default:
543 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000544 Op0 = (OpNo == 0) ? Op : getOperand(0);
545 Op1 = (OpNo == 1) ? Op : getOperand(1);
546 return ConstantExpr::get(getOpcode(), Op0, Op1);
547 }
548}
549
550/// getWithOperands - This returns the current constant expression with the
551/// operands replaced with the specified values. The specified operands must
552/// match count and type with the existing ones.
553Constant *ConstantExpr::
554getWithOperands(const std::vector<Constant*> &Ops) const {
555 assert(Ops.size() == getNumOperands() && "Operand count mismatch!");
556 bool AnyChange = false;
557 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
558 assert(Ops[i]->getType() == getOperand(i)->getType() &&
559 "Operand type mismatch!");
560 AnyChange |= Ops[i] != getOperand(i);
561 }
562 if (!AnyChange) // No operands changed, return self.
563 return const_cast<ConstantExpr*>(this);
564
565 switch (getOpcode()) {
566 case Instruction::Cast:
567 return ConstantExpr::getCast(Ops[0], getType());
568 case Instruction::Select:
569 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
570 case Instruction::InsertElement:
571 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
572 case Instruction::ExtractElement:
573 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
574 case Instruction::ShuffleVector:
575 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
576 case Instruction::GetElementPtr: {
577 std::vector<Constant*> ActualOps(Ops.begin()+1, Ops.end());
578 return ConstantExpr::getGetElementPtr(Ops[0], ActualOps);
579 }
580 default:
581 assert(getNumOperands() == 2 && "Must be binary operator?");
582 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000583 }
584}
585
Chris Lattner2f7c9632001-06-06 20:29:01 +0000586
587//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000588// isValueValidForType implementations
589
Chris Lattner3462ae32001-12-03 22:26:30 +0000590bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000591 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000592 default:
593 return false; // These can't be represented as integers!!!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000594 // Signed types...
595 case Type::SByteTyID:
596 return (Val <= INT8_MAX && Val >= INT8_MIN);
597 case Type::ShortTyID:
598 return (Val <= INT16_MAX && Val >= INT16_MIN);
599 case Type::IntTyID:
Chris Lattner74248512004-06-08 23:21:39 +0000600 return (Val <= int(INT32_MAX) && Val >= int(INT32_MIN));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000601 case Type::LongTyID:
602 return true; // This is the largest type...
603 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000604}
605
Chris Lattner3462ae32001-12-03 22:26:30 +0000606bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000607 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000608 default:
609 return false; // These can't be represented as integers!!!
610
611 // Unsigned types...
612 case Type::UByteTyID:
613 return (Val <= UINT8_MAX);
614 case Type::UShortTyID:
615 return (Val <= UINT16_MAX);
616 case Type::UIntTyID:
617 return (Val <= UINT32_MAX);
618 case Type::ULongTyID:
619 return true; // This is the largest type...
620 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000621}
622
Chris Lattner3462ae32001-12-03 22:26:30 +0000623bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000624 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000625 default:
626 return false; // These can't be represented as floating point!
627
Reid Spencerb95f8ab2004-12-07 07:38:08 +0000628 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000629 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000630 case Type::DoubleTyID:
631 return true; // This is the largest type...
632 }
Chris Lattneraa2372562006-05-24 17:04:05 +0000633}
Chris Lattner9655e542001-07-20 19:16:02 +0000634
Chris Lattner49d855c2001-09-07 16:46:31 +0000635//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000636// Factory Function Implementation
637
Chris Lattner98fa07b2003-05-23 20:03:32 +0000638// ConstantCreator - A class that is used to create constants by
639// ValueMap*. This class should be partially specialized if there is
640// something strange that needs to be done to interface to the ctor for the
641// constant.
642//
Chris Lattner189d19f2003-11-21 20:23:48 +0000643namespace llvm {
644 template<class ConstantClass, class TypeClass, class ValType>
Chris Lattner02157b02006-06-28 21:38:54 +0000645 struct VISIBILITY_HIDDEN ConstantCreator {
Chris Lattner189d19f2003-11-21 20:23:48 +0000646 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
647 return new ConstantClass(Ty, V);
648 }
649 };
Misha Brukmanb1c93172005-04-21 23:48:37 +0000650
Chris Lattner189d19f2003-11-21 20:23:48 +0000651 template<class ConstantClass, class TypeClass>
Chris Lattner02157b02006-06-28 21:38:54 +0000652 struct VISIBILITY_HIDDEN ConvertConstantType {
Chris Lattner189d19f2003-11-21 20:23:48 +0000653 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
654 assert(0 && "This type cannot be converted!\n");
655 abort();
656 }
657 };
Chris Lattnerb50d1352003-10-05 00:17:43 +0000658
Chris Lattner935aa922005-10-04 17:48:46 +0000659 template<class ValType, class TypeClass, class ConstantClass,
660 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattner02157b02006-06-28 21:38:54 +0000661 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
Chris Lattnerb64419a2005-10-03 22:51:37 +0000662 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000663 typedef std::pair<const Type*, ValType> MapKey;
664 typedef std::map<MapKey, Constant *> MapTy;
665 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
666 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
Chris Lattnerb64419a2005-10-03 22:51:37 +0000667 private:
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000668 /// Map - This is the main map from the element descriptor to the Constants.
669 /// This is the primary way we avoid creating two of the same shape
670 /// constant.
Chris Lattnerb50d1352003-10-05 00:17:43 +0000671 MapTy Map;
Chris Lattner935aa922005-10-04 17:48:46 +0000672
673 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
674 /// from the constants to their element in Map. This is important for
675 /// removal of constants from the array, which would otherwise have to scan
676 /// through the map with very large keys.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000677 InverseMapTy InverseMap;
Chris Lattnerb50d1352003-10-05 00:17:43 +0000678
Jim Laskeyc03caef2006-07-17 17:38:29 +0000679 /// AbstractTypeMap - Map for abstract type constants.
680 ///
Chris Lattnerb50d1352003-10-05 00:17:43 +0000681 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner99a669b2004-11-19 16:39:44 +0000682
683 friend void Constant::clearAllValueMaps();
684 private:
685 void clear(std::vector<Constant *> &Constants) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000686 for(typename MapTy::iterator I = Map.begin(); I != Map.end(); ++I)
Chris Lattner99a669b2004-11-19 16:39:44 +0000687 Constants.push_back(I->second);
688 Map.clear();
689 AbstractTypeMap.clear();
Chris Lattner935aa922005-10-04 17:48:46 +0000690 InverseMap.clear();
Chris Lattner99a669b2004-11-19 16:39:44 +0000691 }
692
Chris Lattner98fa07b2003-05-23 20:03:32 +0000693 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000694 typename MapTy::iterator map_end() { return Map.end(); }
Chris Lattnerb64419a2005-10-03 22:51:37 +0000695
696 /// InsertOrGetItem - Return an iterator for the specified element.
697 /// If the element exists in the map, the returned iterator points to the
698 /// entry and Exists=true. If not, the iterator points to the newly
699 /// inserted entry and returns Exists=false. Newly inserted entries have
700 /// I->second == 0, and should be filled in.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000701 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
702 &InsertVal,
Chris Lattnerb64419a2005-10-03 22:51:37 +0000703 bool &Exists) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000704 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
Chris Lattnerb64419a2005-10-03 22:51:37 +0000705 Exists = !IP.second;
706 return IP.first;
707 }
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000708
Chris Lattner935aa922005-10-04 17:48:46 +0000709private:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000710 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
Chris Lattner935aa922005-10-04 17:48:46 +0000711 if (HasLargeKey) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000712 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
Chris Lattner935aa922005-10-04 17:48:46 +0000713 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
714 IMI->second->second == CP &&
715 "InverseMap corrupt!");
716 return IMI->second;
717 }
718
Jim Laskeyc03caef2006-07-17 17:38:29 +0000719 typename MapTy::iterator I =
Chris Lattner935aa922005-10-04 17:48:46 +0000720 Map.find(MapKey((TypeClass*)CP->getRawType(), getValType(CP)));
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000721 if (I == Map.end() || I->second != CP) {
722 // FIXME: This should not use a linear scan. If this gets to be a
723 // performance problem, someone should look at this.
724 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
725 /* empty */;
726 }
Chris Lattner935aa922005-10-04 17:48:46 +0000727 return I;
728 }
729public:
730
Chris Lattnerb64419a2005-10-03 22:51:37 +0000731 /// getOrCreate - Return the specified constant from the map, creating it if
732 /// necessary.
Chris Lattner98fa07b2003-05-23 20:03:32 +0000733 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000734 MapKey Lookup(Ty, V);
Jim Laskeyc03caef2006-07-17 17:38:29 +0000735 typename MapTy::iterator I = Map.lower_bound(Lookup);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000736 if (I != Map.end() && I->first == Lookup)
Jim Laskeyc03caef2006-07-17 17:38:29 +0000737 return static_cast<ConstantClass *>(I->second); // Is it in the map?
Chris Lattner98fa07b2003-05-23 20:03:32 +0000738
739 // If no preexisting value, create one now...
740 ConstantClass *Result =
741 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
742
Chris Lattnerb50d1352003-10-05 00:17:43 +0000743 /// FIXME: why does this assert fail when loading 176.gcc?
744 //assert(Result->getType() == Ty && "Type specified is not correct!");
745 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
746
Chris Lattner935aa922005-10-04 17:48:46 +0000747 if (HasLargeKey) // Remember the reverse mapping if needed.
748 InverseMap.insert(std::make_pair(Result, I));
749
Chris Lattnerb50d1352003-10-05 00:17:43 +0000750 // If the type of the constant is abstract, make sure that an entry exists
751 // for it in the AbstractTypeMap.
752 if (Ty->isAbstract()) {
753 typename AbstractTypeMapTy::iterator TI =
754 AbstractTypeMap.lower_bound(Ty);
755
756 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
757 // Add ourselves to the ATU list of the type.
758 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
759
760 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
761 }
762 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000763 return Result;
764 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000765
Chris Lattner98fa07b2003-05-23 20:03:32 +0000766 void remove(ConstantClass *CP) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000767 typename MapTy::iterator I = FindExistingElement(CP);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000768 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +0000769 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +0000770
Chris Lattner935aa922005-10-04 17:48:46 +0000771 if (HasLargeKey) // Remember the reverse mapping if needed.
772 InverseMap.erase(CP);
773
Chris Lattnerb50d1352003-10-05 00:17:43 +0000774 // Now that we found the entry, make sure this isn't the entry that
775 // the AbstractTypeMap points to.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000776 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000777 if (Ty->isAbstract()) {
778 assert(AbstractTypeMap.count(Ty) &&
779 "Abstract type not in AbstractTypeMap?");
Jim Laskeyc03caef2006-07-17 17:38:29 +0000780 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
Chris Lattnerb50d1352003-10-05 00:17:43 +0000781 if (ATMEntryIt == I) {
782 // Yes, we are removing the representative entry for this type.
783 // See if there are any other entries of the same type.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000784 typename MapTy::iterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000785
Chris Lattnerb50d1352003-10-05 00:17:43 +0000786 // First check the entry before this one...
787 if (TmpIt != Map.begin()) {
788 --TmpIt;
789 if (TmpIt->first.first != Ty) // Not the same type, move back...
790 ++TmpIt;
791 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000792
Chris Lattnerb50d1352003-10-05 00:17:43 +0000793 // If we didn't find the same type, try to move forward...
794 if (TmpIt == ATMEntryIt) {
795 ++TmpIt;
796 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
797 --TmpIt; // No entry afterwards with the same type
798 }
799
800 // If there is another entry in the map of the same abstract type,
801 // update the AbstractTypeMap entry now.
802 if (TmpIt != ATMEntryIt) {
803 ATMEntryIt = TmpIt;
804 } else {
805 // Otherwise, we are removing the last instance of this type
806 // from the table. Remove from the ATM, and from user list.
807 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
808 AbstractTypeMap.erase(Ty);
809 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000810 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000811 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000812
Chris Lattnerb50d1352003-10-05 00:17:43 +0000813 Map.erase(I);
814 }
815
Chris Lattner3b793c62005-10-04 21:35:50 +0000816
817 /// MoveConstantToNewSlot - If we are about to change C to be the element
818 /// specified by I, update our internal data structures to reflect this
819 /// fact.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000820 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
Chris Lattner3b793c62005-10-04 21:35:50 +0000821 // First, remove the old location of the specified constant in the map.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000822 typename MapTy::iterator OldI = FindExistingElement(C);
Chris Lattner3b793c62005-10-04 21:35:50 +0000823 assert(OldI != Map.end() && "Constant not found in constant table!");
824 assert(OldI->second == C && "Didn't find correct element?");
825
826 // If this constant is the representative element for its abstract type,
827 // update the AbstractTypeMap so that the representative element is I.
828 if (C->getType()->isAbstract()) {
829 typename AbstractTypeMapTy::iterator ATI =
830 AbstractTypeMap.find(C->getType());
831 assert(ATI != AbstractTypeMap.end() &&
832 "Abstract type not in AbstractTypeMap?");
833 if (ATI->second == OldI)
834 ATI->second = I;
835 }
836
837 // Remove the old entry from the map.
838 Map.erase(OldI);
839
840 // Update the inverse map so that we know that this constant is now
841 // located at descriptor I.
842 if (HasLargeKey) {
843 assert(I->second == C && "Bad inversemap entry!");
844 InverseMap[C] = I;
845 }
846 }
847
Chris Lattnerb50d1352003-10-05 00:17:43 +0000848 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000849 typename AbstractTypeMapTy::iterator I =
Jim Laskeyc03caef2006-07-17 17:38:29 +0000850 AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +0000851
852 assert(I != AbstractTypeMap.end() &&
853 "Abstract type not in AbstractTypeMap?");
854
855 // Convert a constant at a time until the last one is gone. The last one
856 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
857 // eliminated eventually.
858 do {
859 ConvertConstantType<ConstantClass,
Jim Laskeyc03caef2006-07-17 17:38:29 +0000860 TypeClass>::convert(
861 static_cast<ConstantClass *>(I->second->second),
Chris Lattnerb50d1352003-10-05 00:17:43 +0000862 cast<TypeClass>(NewTy));
863
Jim Laskeyc03caef2006-07-17 17:38:29 +0000864 I = AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +0000865 } while (I != AbstractTypeMap.end());
866 }
867
868 // If the type became concrete without being refined to any other existing
869 // type, we just remove ourselves from the ATU list.
870 void typeBecameConcrete(const DerivedType *AbsTy) {
871 AbsTy->removeAbstractTypeUser(this);
872 }
873
874 void dump() const {
875 std::cerr << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +0000876 }
877 };
878}
879
Chris Lattner3462ae32001-12-03 22:26:30 +0000880//---- ConstantUInt::get() and ConstantSInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000881//
Chris Lattner98fa07b2003-05-23 20:03:32 +0000882static ValueMap< int64_t, Type, ConstantSInt> SIntConstants;
883static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000884
Chris Lattner3462ae32001-12-03 22:26:30 +0000885ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000886 return SIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000887}
888
Chris Lattner3462ae32001-12-03 22:26:30 +0000889ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
Chris Lattner98fa07b2003-05-23 20:03:32 +0000890 return UIntConstants.getOrCreate(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000891}
892
Chris Lattner3462ae32001-12-03 22:26:30 +0000893ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
Chris Lattner49d855c2001-09-07 16:46:31 +0000894 assert(V <= 127 && "Can only be used with very small positive constants!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000895 if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
896 return ConstantUInt::get(Ty, V);
Chris Lattner49d855c2001-09-07 16:46:31 +0000897}
898
Chris Lattner3462ae32001-12-03 22:26:30 +0000899//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000900//
Chris Lattnerac80ea42004-02-01 22:49:04 +0000901namespace llvm {
902 template<>
903 struct ConstantCreator<ConstantFP, Type, uint64_t> {
904 static ConstantFP *create(const Type *Ty, uint64_t V) {
905 assert(Ty == Type::DoubleTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000906 return new ConstantFP(Ty, BitsToDouble(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000907 }
908 };
909 template<>
910 struct ConstantCreator<ConstantFP, Type, uint32_t> {
911 static ConstantFP *create(const Type *Ty, uint32_t V) {
912 assert(Ty == Type::FloatTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000913 return new ConstantFP(Ty, BitsToFloat(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000914 }
915 };
916}
917
918static ValueMap<uint64_t, Type, ConstantFP> DoubleConstants;
919static ValueMap<uint32_t, Type, ConstantFP> FloatConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000920
Jim Laskey8ad8f712005-08-17 20:06:22 +0000921bool ConstantFP::isNullValue() const {
922 return DoubleToBits(Val) == 0;
923}
924
925bool ConstantFP::isExactlyValue(double V) const {
926 return DoubleToBits(V) == DoubleToBits(Val);
927}
928
929
Chris Lattner3462ae32001-12-03 22:26:30 +0000930ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner241ed4c2004-01-23 00:55:21 +0000931 if (Ty == Type::FloatTy) {
932 // Force the value through memory to normalize it.
Jim Laskeyb74c6662005-08-17 19:34:49 +0000933 return FloatConstants.getOrCreate(Ty, FloatToBits(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000934 } else {
935 assert(Ty == Type::DoubleTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000936 return DoubleConstants.getOrCreate(Ty, DoubleToBits(V));
Chris Lattner241ed4c2004-01-23 00:55:21 +0000937 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000938}
939
Chris Lattner9fba3da2004-02-15 05:53:04 +0000940//---- ConstantAggregateZero::get() implementation...
941//
942namespace llvm {
943 // ConstantAggregateZero does not take extra "value" argument...
944 template<class ValType>
945 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
946 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
947 return new ConstantAggregateZero(Ty);
948 }
949 };
950
951 template<>
952 struct ConvertConstantType<ConstantAggregateZero, Type> {
953 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
954 // Make everyone now use a constant of the new type...
955 Constant *New = ConstantAggregateZero::get(NewTy);
956 assert(New != OldC && "Didn't replace constant??");
957 OldC->uncheckedReplaceAllUsesWith(New);
958 OldC->destroyConstant(); // This constant is now dead, destroy it.
959 }
960 };
961}
962
963static ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
964
Chris Lattner3e650af2004-08-04 04:48:01 +0000965static char getValType(ConstantAggregateZero *CPZ) { return 0; }
966
Chris Lattner9fba3da2004-02-15 05:53:04 +0000967Constant *ConstantAggregateZero::get(const Type *Ty) {
Chris Lattnerbfd0b6d2006-06-10 04:16:23 +0000968 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<PackedType>(Ty)) &&
969 "Cannot create an aggregate zero of non-aggregate type!");
Chris Lattner9fba3da2004-02-15 05:53:04 +0000970 return AggZeroConstants.getOrCreate(Ty, 0);
971}
972
973// destroyConstant - Remove the constant from the constant table...
974//
975void ConstantAggregateZero::destroyConstant() {
976 AggZeroConstants.remove(this);
977 destroyConstantImpl();
978}
979
Chris Lattner3462ae32001-12-03 22:26:30 +0000980//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000981//
Chris Lattner189d19f2003-11-21 20:23:48 +0000982namespace llvm {
983 template<>
984 struct ConvertConstantType<ConstantArray, ArrayType> {
985 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
986 // Make everyone now use a constant of the new type...
987 std::vector<Constant*> C;
988 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
989 C.push_back(cast<Constant>(OldC->getOperand(i)));
990 Constant *New = ConstantArray::get(NewTy, C);
991 assert(New != OldC && "Didn't replace constant??");
992 OldC->uncheckedReplaceAllUsesWith(New);
993 OldC->destroyConstant(); // This constant is now dead, destroy it.
994 }
995 };
996}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000997
Chris Lattner3e650af2004-08-04 04:48:01 +0000998static std::vector<Constant*> getValType(ConstantArray *CA) {
999 std::vector<Constant*> Elements;
1000 Elements.reserve(CA->getNumOperands());
1001 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1002 Elements.push_back(cast<Constant>(CA->getOperand(i)));
1003 return Elements;
1004}
1005
Chris Lattnerb64419a2005-10-03 22:51:37 +00001006typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattner935aa922005-10-04 17:48:46 +00001007 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattnerb64419a2005-10-03 22:51:37 +00001008static ArrayConstantsTy ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001009
Chris Lattner015e8212004-02-15 04:14:47 +00001010Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +00001011 const std::vector<Constant*> &V) {
1012 // If this is an all-zero array, return a ConstantAggregateZero object
1013 if (!V.empty()) {
1014 Constant *C = V[0];
1015 if (!C->isNullValue())
1016 return ArrayConstants.getOrCreate(Ty, V);
1017 for (unsigned i = 1, e = V.size(); i != e; ++i)
1018 if (V[i] != C)
1019 return ArrayConstants.getOrCreate(Ty, V);
1020 }
1021 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001022}
1023
Chris Lattner98fa07b2003-05-23 20:03:32 +00001024// destroyConstant - Remove the constant from the constant table...
1025//
1026void ConstantArray::destroyConstant() {
1027 ArrayConstants.remove(this);
1028 destroyConstantImpl();
1029}
1030
Reid Spencer6f614532006-05-30 08:23:18 +00001031/// ConstantArray::get(const string&) - Return an array that is initialized to
1032/// contain the specified string. If length is zero then a null terminator is
1033/// added to the specified string so that it may be used in a natural way.
1034/// Otherwise, the length parameter specifies how much of the string to use
1035/// and it won't be null terminated.
1036///
Reid Spencer82ebaba2006-05-30 18:15:07 +00001037Constant *ConstantArray::get(const std::string &Str, bool AddNull) {
Chris Lattner7f74a562002-01-20 22:54:45 +00001038 std::vector<Constant*> ElementVals;
Reid Spencer82ebaba2006-05-30 18:15:07 +00001039 for (unsigned i = 0; i < Str.length(); ++i)
Chris Lattnerbec9b0b2001-12-14 16:41:18 +00001040 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +00001041
1042 // Add a null terminator to the string...
Reid Spencer82ebaba2006-05-30 18:15:07 +00001043 if (AddNull) {
Reid Spencer6f614532006-05-30 08:23:18 +00001044 ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
Reid Spencer6f614532006-05-30 08:23:18 +00001045 }
Chris Lattner8f80fe02001-10-14 23:54:12 +00001046
Reid Spencer82ebaba2006-05-30 18:15:07 +00001047 ArrayType *ATy = ArrayType::get(Type::SByteTy, ElementVals.size());
Chris Lattner3462ae32001-12-03 22:26:30 +00001048 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +00001049}
1050
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001051/// isString - This method returns true if the array is an array of sbyte or
1052/// ubyte, and if the elements of the array are all ConstantInt's.
1053bool ConstantArray::isString() const {
1054 // Check the element type for sbyte or ubyte...
Chris Lattnere8701f62004-01-14 17:51:53 +00001055 if (getType()->getElementType() != Type::UByteTy &&
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001056 getType()->getElementType() != Type::SByteTy)
1057 return false;
1058 // Check the elements to make sure they are all integers, not constant
1059 // expressions.
1060 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1061 if (!isa<ConstantInt>(getOperand(i)))
1062 return false;
1063 return true;
1064}
1065
Chris Lattner81fabb02002-08-26 17:53:56 +00001066// getAsString - If the sub-element type of this array is either sbyte or ubyte,
1067// then this method converts the array to an std::string and returns it.
1068// Otherwise, it asserts out.
1069//
1070std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001071 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001072 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +00001073 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1074 Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue();
Chris Lattner81fabb02002-08-26 17:53:56 +00001075 return Result;
1076}
1077
1078
Chris Lattner3462ae32001-12-03 22:26:30 +00001079//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001080//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001081
Chris Lattner189d19f2003-11-21 20:23:48 +00001082namespace llvm {
1083 template<>
1084 struct ConvertConstantType<ConstantStruct, StructType> {
1085 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1086 // Make everyone now use a constant of the new type...
1087 std::vector<Constant*> C;
1088 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1089 C.push_back(cast<Constant>(OldC->getOperand(i)));
1090 Constant *New = ConstantStruct::get(NewTy, C);
1091 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001092
Chris Lattner189d19f2003-11-21 20:23:48 +00001093 OldC->uncheckedReplaceAllUsesWith(New);
1094 OldC->destroyConstant(); // This constant is now dead, destroy it.
1095 }
1096 };
1097}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001098
Chris Lattner8760ec72005-10-04 01:17:50 +00001099typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattner935aa922005-10-04 17:48:46 +00001100 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner8760ec72005-10-04 01:17:50 +00001101static StructConstantsTy StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001102
Chris Lattner3e650af2004-08-04 04:48:01 +00001103static std::vector<Constant*> getValType(ConstantStruct *CS) {
1104 std::vector<Constant*> Elements;
1105 Elements.reserve(CS->getNumOperands());
1106 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1107 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1108 return Elements;
1109}
1110
Chris Lattner015e8212004-02-15 04:14:47 +00001111Constant *ConstantStruct::get(const StructType *Ty,
1112 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001113 // Create a ConstantAggregateZero value if all elements are zeros...
1114 for (unsigned i = 0, e = V.size(); i != e; ++i)
1115 if (!V[i]->isNullValue())
1116 return StructConstants.getOrCreate(Ty, V);
1117
1118 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001119}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001120
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001121Constant *ConstantStruct::get(const std::vector<Constant*> &V) {
1122 std::vector<const Type*> StructEls;
1123 StructEls.reserve(V.size());
1124 for (unsigned i = 0, e = V.size(); i != e; ++i)
1125 StructEls.push_back(V[i]->getType());
1126 return get(StructType::get(StructEls), V);
1127}
1128
Chris Lattnerd7a73302001-10-13 06:57:33 +00001129// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001130//
Chris Lattner3462ae32001-12-03 22:26:30 +00001131void ConstantStruct::destroyConstant() {
Chris Lattnerd7a73302001-10-13 06:57:33 +00001132 StructConstants.remove(this);
1133 destroyConstantImpl();
1134}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001135
Brian Gaeke02209042004-08-20 06:00:58 +00001136//---- ConstantPacked::get() implementation...
1137//
1138namespace llvm {
1139 template<>
1140 struct ConvertConstantType<ConstantPacked, PackedType> {
1141 static void convert(ConstantPacked *OldC, const PackedType *NewTy) {
1142 // Make everyone now use a constant of the new type...
1143 std::vector<Constant*> C;
1144 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1145 C.push_back(cast<Constant>(OldC->getOperand(i)));
1146 Constant *New = ConstantPacked::get(NewTy, C);
1147 assert(New != OldC && "Didn't replace constant??");
1148 OldC->uncheckedReplaceAllUsesWith(New);
1149 OldC->destroyConstant(); // This constant is now dead, destroy it.
1150 }
1151 };
1152}
1153
1154static std::vector<Constant*> getValType(ConstantPacked *CP) {
1155 std::vector<Constant*> Elements;
1156 Elements.reserve(CP->getNumOperands());
1157 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1158 Elements.push_back(CP->getOperand(i));
1159 return Elements;
1160}
1161
1162static ValueMap<std::vector<Constant*>, PackedType,
1163 ConstantPacked> PackedConstants;
1164
1165Constant *ConstantPacked::get(const PackedType *Ty,
1166 const std::vector<Constant*> &V) {
1167 // If this is an all-zero packed, return a ConstantAggregateZero object
1168 if (!V.empty()) {
1169 Constant *C = V[0];
1170 if (!C->isNullValue())
1171 return PackedConstants.getOrCreate(Ty, V);
1172 for (unsigned i = 1, e = V.size(); i != e; ++i)
1173 if (V[i] != C)
1174 return PackedConstants.getOrCreate(Ty, V);
1175 }
1176 return ConstantAggregateZero::get(Ty);
1177}
1178
1179Constant *ConstantPacked::get(const std::vector<Constant*> &V) {
1180 assert(!V.empty() && "Cannot infer type if V is empty");
1181 return get(PackedType::get(V.front()->getType(),V.size()), V);
1182}
1183
1184// destroyConstant - Remove the constant from the constant table...
1185//
1186void ConstantPacked::destroyConstant() {
1187 PackedConstants.remove(this);
1188 destroyConstantImpl();
1189}
1190
Chris Lattner3462ae32001-12-03 22:26:30 +00001191//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001192//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001193
Chris Lattner189d19f2003-11-21 20:23:48 +00001194namespace llvm {
1195 // ConstantPointerNull does not take extra "value" argument...
1196 template<class ValType>
1197 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1198 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1199 return new ConstantPointerNull(Ty);
1200 }
1201 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001202
Chris Lattner189d19f2003-11-21 20:23:48 +00001203 template<>
1204 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1205 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1206 // Make everyone now use a constant of the new type...
1207 Constant *New = ConstantPointerNull::get(NewTy);
1208 assert(New != OldC && "Didn't replace constant??");
1209 OldC->uncheckedReplaceAllUsesWith(New);
1210 OldC->destroyConstant(); // This constant is now dead, destroy it.
1211 }
1212 };
1213}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001214
Chris Lattner98fa07b2003-05-23 20:03:32 +00001215static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001216
Chris Lattner3e650af2004-08-04 04:48:01 +00001217static char getValType(ConstantPointerNull *) {
1218 return 0;
1219}
1220
1221
Chris Lattner3462ae32001-12-03 22:26:30 +00001222ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner98fa07b2003-05-23 20:03:32 +00001223 return NullPtrConstants.getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001224}
1225
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001226// destroyConstant - Remove the constant from the constant table...
1227//
1228void ConstantPointerNull::destroyConstant() {
1229 NullPtrConstants.remove(this);
1230 destroyConstantImpl();
1231}
1232
1233
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001234//---- UndefValue::get() implementation...
1235//
1236
1237namespace llvm {
1238 // UndefValue does not take extra "value" argument...
1239 template<class ValType>
1240 struct ConstantCreator<UndefValue, Type, ValType> {
1241 static UndefValue *create(const Type *Ty, const ValType &V) {
1242 return new UndefValue(Ty);
1243 }
1244 };
1245
1246 template<>
1247 struct ConvertConstantType<UndefValue, Type> {
1248 static void convert(UndefValue *OldC, const Type *NewTy) {
1249 // Make everyone now use a constant of the new type.
1250 Constant *New = UndefValue::get(NewTy);
1251 assert(New != OldC && "Didn't replace constant??");
1252 OldC->uncheckedReplaceAllUsesWith(New);
1253 OldC->destroyConstant(); // This constant is now dead, destroy it.
1254 }
1255 };
1256}
1257
1258static ValueMap<char, Type, UndefValue> UndefValueConstants;
1259
1260static char getValType(UndefValue *) {
1261 return 0;
1262}
1263
1264
1265UndefValue *UndefValue::get(const Type *Ty) {
1266 return UndefValueConstants.getOrCreate(Ty, 0);
1267}
1268
1269// destroyConstant - Remove the constant from the constant table.
1270//
1271void UndefValue::destroyConstant() {
1272 UndefValueConstants.remove(this);
1273 destroyConstantImpl();
1274}
1275
1276
1277
1278
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001279//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001280//
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001281typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Chris Lattner98fa07b2003-05-23 20:03:32 +00001282
Chris Lattner189d19f2003-11-21 20:23:48 +00001283namespace llvm {
1284 template<>
1285 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
1286 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
1287 if (V.first == Instruction::Cast)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001288 return new UnaryConstantExpr(Instruction::Cast, V.second[0], Ty);
Chris Lattner189d19f2003-11-21 20:23:48 +00001289 if ((V.first >= Instruction::BinaryOpsBegin &&
1290 V.first < Instruction::BinaryOpsEnd) ||
1291 V.first == Instruction::Shl || V.first == Instruction::Shr)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001292 return new BinaryConstantExpr(V.first, V.second[0], V.second[1]);
Chris Lattnerb7897ce2004-03-30 22:51:03 +00001293 if (V.first == Instruction::Select)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001294 return new SelectConstantExpr(V.second[0], V.second[1], V.second[2]);
Robert Bocchino23004482006-01-10 19:05:34 +00001295 if (V.first == Instruction::ExtractElement)
1296 return new ExtractElementConstantExpr(V.second[0], V.second[1]);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001297 if (V.first == Instruction::InsertElement)
1298 return new InsertElementConstantExpr(V.second[0], V.second[1],
1299 V.second[2]);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001300 if (V.first == Instruction::ShuffleVector)
1301 return new ShuffleVectorConstantExpr(V.second[0], V.second[1],
1302 V.second[2]);
1303
Chris Lattner189d19f2003-11-21 20:23:48 +00001304 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001305
Chris Lattner189d19f2003-11-21 20:23:48 +00001306 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001307 return new GetElementPtrConstantExpr(V.second[0], IdxList, Ty);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001308 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001309 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001310
Chris Lattner189d19f2003-11-21 20:23:48 +00001311 template<>
1312 struct ConvertConstantType<ConstantExpr, Type> {
1313 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1314 Constant *New;
1315 switch (OldC->getOpcode()) {
1316 case Instruction::Cast:
1317 New = ConstantExpr::getCast(OldC->getOperand(0), NewTy);
1318 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001319 case Instruction::Select:
1320 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1321 OldC->getOperand(1),
1322 OldC->getOperand(2));
1323 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001324 case Instruction::Shl:
1325 case Instruction::Shr:
1326 New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
1327 OldC->getOperand(0), OldC->getOperand(1));
1328 break;
1329 default:
1330 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
1331 OldC->getOpcode() < Instruction::BinaryOpsEnd);
1332 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1333 OldC->getOperand(1));
1334 break;
1335 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001336 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001337 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
1338 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), Idx);
Chris Lattner189d19f2003-11-21 20:23:48 +00001339 break;
1340 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001341
Chris Lattner189d19f2003-11-21 20:23:48 +00001342 assert(New != OldC && "Didn't replace constant??");
1343 OldC->uncheckedReplaceAllUsesWith(New);
1344 OldC->destroyConstant(); // This constant is now dead, destroy it.
1345 }
1346 };
1347} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001348
1349
Chris Lattner3e650af2004-08-04 04:48:01 +00001350static ExprMapKeyType getValType(ConstantExpr *CE) {
1351 std::vector<Constant*> Operands;
1352 Operands.reserve(CE->getNumOperands());
1353 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1354 Operands.push_back(cast<Constant>(CE->getOperand(i)));
1355 return ExprMapKeyType(CE->getOpcode(), Operands);
1356}
1357
Chris Lattner98fa07b2003-05-23 20:03:32 +00001358static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001359
Chris Lattner46b3d302003-04-16 22:40:51 +00001360Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001361 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1362
Chris Lattneracdbe712003-04-17 19:24:48 +00001363 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
1364 return FC; // Fold a few common cases...
1365
Vikram S. Adve4c485332002-07-15 18:19:33 +00001366 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001367 std::vector<Constant*> argVec(1, C);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001368 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
1369 return ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001370}
Chris Lattnerd7a73302001-10-13 06:57:33 +00001371
Chris Lattnerdd284742004-04-04 23:20:30 +00001372Constant *ConstantExpr::getSignExtend(Constant *C, const Type *Ty) {
Chris Lattner1ece6f82005-01-01 15:59:57 +00001373 assert(C->getType()->isIntegral() && Ty->isIntegral() &&
Chris Lattnerdd284742004-04-04 23:20:30 +00001374 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1375 "This is an illegal sign extension!");
Chris Lattner1ece6f82005-01-01 15:59:57 +00001376 if (C->getType() != Type::BoolTy) {
1377 C = ConstantExpr::getCast(C, C->getType()->getSignedVersion());
1378 return ConstantExpr::getCast(C, Ty);
1379 } else {
1380 if (C == ConstantBool::True)
1381 return ConstantIntegral::getAllOnesValue(Ty);
1382 else
1383 return ConstantIntegral::getNullValue(Ty);
1384 }
Chris Lattnerdd284742004-04-04 23:20:30 +00001385}
1386
1387Constant *ConstantExpr::getZeroExtend(Constant *C, const Type *Ty) {
Chris Lattner1ece6f82005-01-01 15:59:57 +00001388 assert(C->getType()->isIntegral() && Ty->isIntegral() &&
Chris Lattnerdd284742004-04-04 23:20:30 +00001389 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1390 "This is an illegal zero extension!");
Chris Lattner1ece6f82005-01-01 15:59:57 +00001391 if (C->getType() != Type::BoolTy)
1392 C = ConstantExpr::getCast(C, C->getType()->getUnsignedVersion());
Chris Lattnerdd284742004-04-04 23:20:30 +00001393 return ConstantExpr::getCast(C, Ty);
1394}
1395
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001396Constant *ConstantExpr::getSizeOf(const Type *Ty) {
Chris Lattneracc4e542004-12-13 19:48:51 +00001397 // sizeof is implemented as: (ulong) gep (Ty*)null, 1
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001398 return getCast(
Chris Lattneracc4e542004-12-13 19:48:51 +00001399 getGetElementPtr(getNullValue(PointerType::get(Ty)),
1400 std::vector<Constant*>(1, ConstantInt::get(Type::UIntTy, 1))),
1401 Type::ULongTy);
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001402}
1403
Alkis Evlogimenos9160d5f2005-03-19 11:40:31 +00001404Constant *ConstantExpr::getPtrPtrFromArrayPtr(Constant *C) {
1405 // pointer from array is implemented as: getelementptr arr ptr, 0, 0
1406 static std::vector<Constant*> Indices(2, ConstantUInt::get(Type::UIntTy, 0));
1407
1408 return ConstantExpr::getGetElementPtr(C, Indices);
1409}
1410
Chris Lattnerb50d1352003-10-05 00:17:43 +00001411Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
1412 Constant *C1, Constant *C2) {
Chris Lattner5645e8a2004-01-12 19:04:55 +00001413 if (Opcode == Instruction::Shl || Opcode == Instruction::Shr)
1414 return getShiftTy(ReqTy, Opcode, C1, C2);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001415 // Check the operands for consistency first
1416 assert((Opcode >= Instruction::BinaryOpsBegin &&
1417 Opcode < Instruction::BinaryOpsEnd) &&
1418 "Invalid opcode in binary constant expression");
1419 assert(C1->getType() == C2->getType() &&
1420 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001421
Chris Lattnere1496fb2006-09-17 19:14:47 +00001422 if (ReqTy == C1->getType() || (Instruction::isComparison(Opcode) &&
Chris Lattner29ca2c62004-08-04 18:50:09 +00001423 ReqTy == Type::BoolTy))
Chris Lattnerb50d1352003-10-05 00:17:43 +00001424 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1425 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001426
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001427 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001428 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001429 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001430}
1431
Chris Lattner29ca2c62004-08-04 18:50:09 +00001432Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001433#ifndef NDEBUG
1434 switch (Opcode) {
1435 case Instruction::Add: case Instruction::Sub:
1436 case Instruction::Mul: case Instruction::Div:
1437 case Instruction::Rem:
1438 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattnerc421a262006-01-04 01:01:04 +00001439 assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint() ||
1440 isa<PackedType>(C1->getType())) &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001441 "Tried to create an arithmetic operation on a non-arithmetic type!");
1442 break;
1443 case Instruction::And:
1444 case Instruction::Or:
1445 case Instruction::Xor:
1446 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattnerc421a262006-01-04 01:01:04 +00001447 assert((C1->getType()->isIntegral() || isa<PackedType>(C1->getType())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001448 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001449 break;
1450 case Instruction::SetLT: case Instruction::SetGT: case Instruction::SetLE:
1451 case Instruction::SetGE: case Instruction::SetEQ: case Instruction::SetNE:
1452 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1453 break;
1454 case Instruction::Shl:
1455 case Instruction::Shr:
1456 assert(C2->getType() == Type::UByteTy && "Shift should be by ubyte!");
Chris Lattnerc421a262006-01-04 01:01:04 +00001457 assert((C1->getType()->isInteger() || isa<PackedType>(C1->getType())) &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001458 "Tried to create a shift operation on a non-integer type!");
1459 break;
1460 default:
1461 break;
1462 }
1463#endif
1464
Chris Lattnere1496fb2006-09-17 19:14:47 +00001465 if (Instruction::isComparison(Opcode))
Chris Lattner29ca2c62004-08-04 18:50:09 +00001466 return getTy(Type::BoolTy, Opcode, C1, C2);
1467 else
1468 return getTy(C1->getType(), Opcode, C1, C2);
1469}
1470
Chris Lattner6e415c02004-03-12 05:54:04 +00001471Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
1472 Constant *V1, Constant *V2) {
1473 assert(C->getType() == Type::BoolTy && "Select condition must be bool!");
1474 assert(V1->getType() == V2->getType() && "Select value types must match!");
1475 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
1476
1477 if (ReqTy == V1->getType())
1478 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1479 return SC; // Fold common cases
1480
1481 std::vector<Constant*> argVec(3, C);
1482 argVec[1] = V1;
1483 argVec[2] = V2;
1484 ExprMapKeyType Key = std::make_pair(Instruction::Select, argVec);
1485 return ExprConstants.getOrCreate(ReqTy, Key);
1486}
1487
Chris Lattner9eb2b522004-01-12 19:12:58 +00001488/// getShiftTy - Return a shift left or shift right constant expr
Chris Lattnerb50d1352003-10-05 00:17:43 +00001489Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
1490 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001491 // Check the operands for consistency first
1492 assert((Opcode == Instruction::Shl ||
1493 Opcode == Instruction::Shr) &&
1494 "Invalid opcode in binary constant expression");
1495 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
1496 "Invalid operand types for Shift constant expr!");
1497
Chris Lattner0bba7712004-01-12 20:40:42 +00001498 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001499 return FC; // Fold a few common cases...
1500
1501 // Look up the constant in the table first to ensure uniqueness
1502 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001503 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001504 return ExprConstants.getOrCreate(ReqTy, Key);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001505}
1506
1507
Chris Lattnerb50d1352003-10-05 00:17:43 +00001508Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner13128ab2004-10-11 22:52:25 +00001509 const std::vector<Value*> &IdxList) {
1510 assert(GetElementPtrInst::getIndexedType(C->getType(), IdxList, true) &&
Chris Lattner04b60fe2004-02-16 20:46:13 +00001511 "GEP indices invalid!");
1512
Chris Lattneracdbe712003-04-17 19:24:48 +00001513 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
1514 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001515
Chris Lattnerb50d1352003-10-05 00:17:43 +00001516 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001517 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001518 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00001519 std::vector<Constant*> ArgVec;
1520 ArgVec.reserve(IdxList.size()+1);
1521 ArgVec.push_back(C);
1522 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
1523 ArgVec.push_back(cast<Constant>(IdxList[i]));
1524 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,ArgVec);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001525 return ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001526}
1527
Chris Lattnerb50d1352003-10-05 00:17:43 +00001528Constant *ConstantExpr::getGetElementPtr(Constant *C,
1529 const std::vector<Constant*> &IdxList){
1530 // Get the result type of the getelementptr!
1531 std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
1532
1533 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
1534 true);
1535 assert(Ty && "GEP indices invalid!");
Chris Lattner13128ab2004-10-11 22:52:25 +00001536 return getGetElementPtrTy(PointerType::get(Ty), C, VIdxList);
1537}
1538
1539Constant *ConstantExpr::getGetElementPtr(Constant *C,
1540 const std::vector<Value*> &IdxList) {
1541 // Get the result type of the getelementptr!
1542 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
1543 true);
1544 assert(Ty && "GEP indices invalid!");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001545 return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
1546}
1547
Robert Bocchino23004482006-01-10 19:05:34 +00001548Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
1549 Constant *Idx) {
Robert Bocchinode7f1c92006-01-10 20:03:46 +00001550 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
1551 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00001552 // Look up the constant in the table first to ensure uniqueness
1553 std::vector<Constant*> ArgVec(1, Val);
1554 ArgVec.push_back(Idx);
1555 const ExprMapKeyType &Key = std::make_pair(Instruction::ExtractElement,ArgVec);
1556 return ExprConstants.getOrCreate(ReqTy, Key);
1557}
1558
1559Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
1560 assert(isa<PackedType>(Val->getType()) &&
1561 "Tried to create extractelement operation on non-packed type!");
1562 assert(Idx->getType() == Type::UIntTy &&
Robert Bocchinoca27f032006-01-17 20:07:22 +00001563 "Extractelement index must be uint type!");
Robert Bocchino23004482006-01-10 19:05:34 +00001564 return getExtractElementTy(cast<PackedType>(Val->getType())->getElementType(),
1565 Val, Idx);
1566}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001567
Robert Bocchinoca27f032006-01-17 20:07:22 +00001568Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
1569 Constant *Elt, Constant *Idx) {
1570 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
1571 return FC; // Fold a few common cases...
1572 // Look up the constant in the table first to ensure uniqueness
1573 std::vector<Constant*> ArgVec(1, Val);
1574 ArgVec.push_back(Elt);
1575 ArgVec.push_back(Idx);
1576 const ExprMapKeyType &Key = std::make_pair(Instruction::InsertElement,ArgVec);
1577 return ExprConstants.getOrCreate(ReqTy, Key);
1578}
1579
1580Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1581 Constant *Idx) {
1582 assert(isa<PackedType>(Val->getType()) &&
1583 "Tried to create insertelement operation on non-packed type!");
1584 assert(Elt->getType() == cast<PackedType>(Val->getType())->getElementType()
1585 && "Insertelement types must match!");
1586 assert(Idx->getType() == Type::UIntTy &&
1587 "Insertelement index must be uint type!");
1588 return getInsertElementTy(cast<PackedType>(Val->getType())->getElementType(),
1589 Val, Elt, Idx);
1590}
1591
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001592Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
1593 Constant *V2, Constant *Mask) {
1594 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
1595 return FC; // Fold a few common cases...
1596 // Look up the constant in the table first to ensure uniqueness
1597 std::vector<Constant*> ArgVec(1, V1);
1598 ArgVec.push_back(V2);
1599 ArgVec.push_back(Mask);
1600 const ExprMapKeyType &Key = std::make_pair(Instruction::ShuffleVector,ArgVec);
1601 return ExprConstants.getOrCreate(ReqTy, Key);
1602}
1603
1604Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
1605 Constant *Mask) {
1606 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
1607 "Invalid shuffle vector constant expr operands!");
1608 return getShuffleVectorTy(V1->getType(), V1, V2, Mask);
1609}
1610
1611
Vikram S. Adve4c485332002-07-15 18:19:33 +00001612// destroyConstant - Remove the constant from the constant table...
1613//
1614void ConstantExpr::destroyConstant() {
1615 ExprConstants.remove(this);
1616 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001617}
1618
Chris Lattner3cd8c562002-07-30 18:54:25 +00001619const char *ConstantExpr::getOpcodeName() const {
1620 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001621}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00001622
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001623//===----------------------------------------------------------------------===//
1624// replaceUsesOfWithOnConstant implementations
1625
1626void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001627 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001628 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00001629 Constant *ToC = cast<Constant>(To);
Chris Lattnerdff59112005-10-04 18:47:09 +00001630
1631 unsigned OperandToUpdate = U-OperandList;
1632 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1633
Jim Laskeyc03caef2006-07-17 17:38:29 +00001634 std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnerb64419a2005-10-03 22:51:37 +00001635 Lookup.first.first = getType();
1636 Lookup.second = this;
Chris Lattnerdff59112005-10-04 18:47:09 +00001637
Chris Lattnerb64419a2005-10-03 22:51:37 +00001638 std::vector<Constant*> &Values = Lookup.first.second;
1639 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattnerdff59112005-10-04 18:47:09 +00001640
Chris Lattner8760ec72005-10-04 01:17:50 +00001641 // Fill values with the modified operands of the constant array. Also,
1642 // compute whether this turns into an all-zeros array.
Chris Lattnerdff59112005-10-04 18:47:09 +00001643 bool isAllZeros = false;
1644 if (!ToC->isNullValue()) {
1645 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1646 Values.push_back(cast<Constant>(O->get()));
1647 } else {
1648 isAllZeros = true;
1649 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1650 Constant *Val = cast<Constant>(O->get());
1651 Values.push_back(Val);
1652 if (isAllZeros) isAllZeros = Val->isNullValue();
1653 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001654 }
Chris Lattnerdff59112005-10-04 18:47:09 +00001655 Values[OperandToUpdate] = ToC;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001656
Chris Lattnerb64419a2005-10-03 22:51:37 +00001657 Constant *Replacement = 0;
1658 if (isAllZeros) {
1659 Replacement = ConstantAggregateZero::get(getType());
1660 } else {
1661 // Check to see if we have this array type already.
1662 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00001663 ArrayConstantsTy::MapTy::iterator I =
Chris Lattnerb64419a2005-10-03 22:51:37 +00001664 ArrayConstants.InsertOrGetItem(Lookup, Exists);
1665
1666 if (Exists) {
1667 Replacement = I->second;
1668 } else {
1669 // Okay, the new shape doesn't exist in the system yet. Instead of
1670 // creating a new constant array, inserting it, replaceallusesof'ing the
1671 // old with the new, then deleting the old... just update the current one
1672 // in place!
Chris Lattner3b793c62005-10-04 21:35:50 +00001673 ArrayConstants.MoveConstantToNewSlot(this, I);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001674
Chris Lattnerdff59112005-10-04 18:47:09 +00001675 // Update to the new value.
1676 setOperand(OperandToUpdate, ToC);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001677 return;
1678 }
1679 }
1680
1681 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001682 assert(Replacement != this && "I didn't contain From!");
1683
Chris Lattner7a1450d2005-10-04 18:13:04 +00001684 // Everyone using this now uses the replacement.
1685 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001686
1687 // Delete the old constant!
1688 destroyConstant();
1689}
1690
1691void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001692 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001693 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00001694 Constant *ToC = cast<Constant>(To);
1695
Chris Lattnerdff59112005-10-04 18:47:09 +00001696 unsigned OperandToUpdate = U-OperandList;
1697 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1698
Jim Laskeyc03caef2006-07-17 17:38:29 +00001699 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattner8760ec72005-10-04 01:17:50 +00001700 Lookup.first.first = getType();
1701 Lookup.second = this;
1702 std::vector<Constant*> &Values = Lookup.first.second;
1703 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001704
Chris Lattnerdff59112005-10-04 18:47:09 +00001705
Chris Lattner8760ec72005-10-04 01:17:50 +00001706 // Fill values with the modified operands of the constant struct. Also,
1707 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00001708 bool isAllZeros = false;
1709 if (!ToC->isNullValue()) {
1710 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1711 Values.push_back(cast<Constant>(O->get()));
1712 } else {
1713 isAllZeros = true;
1714 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1715 Constant *Val = cast<Constant>(O->get());
1716 Values.push_back(Val);
1717 if (isAllZeros) isAllZeros = Val->isNullValue();
1718 }
Chris Lattner8760ec72005-10-04 01:17:50 +00001719 }
Chris Lattnerdff59112005-10-04 18:47:09 +00001720 Values[OperandToUpdate] = ToC;
1721
Chris Lattner8760ec72005-10-04 01:17:50 +00001722 Constant *Replacement = 0;
1723 if (isAllZeros) {
1724 Replacement = ConstantAggregateZero::get(getType());
1725 } else {
1726 // Check to see if we have this array type already.
1727 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00001728 StructConstantsTy::MapTy::iterator I =
Chris Lattner3b793c62005-10-04 21:35:50 +00001729 StructConstants.InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00001730
1731 if (Exists) {
1732 Replacement = I->second;
1733 } else {
1734 // Okay, the new shape doesn't exist in the system yet. Instead of
1735 // creating a new constant struct, inserting it, replaceallusesof'ing the
1736 // old with the new, then deleting the old... just update the current one
1737 // in place!
Chris Lattner3b793c62005-10-04 21:35:50 +00001738 StructConstants.MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00001739
Chris Lattnerdff59112005-10-04 18:47:09 +00001740 // Update to the new value.
1741 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00001742 return;
1743 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001744 }
1745
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001746 assert(Replacement != this && "I didn't contain From!");
1747
Chris Lattner7a1450d2005-10-04 18:13:04 +00001748 // Everyone using this now uses the replacement.
1749 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001750
1751 // Delete the old constant!
1752 destroyConstant();
1753}
1754
1755void ConstantPacked::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001756 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001757 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
1758
1759 std::vector<Constant*> Values;
1760 Values.reserve(getNumOperands()); // Build replacement array...
1761 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1762 Constant *Val = getOperand(i);
1763 if (Val == From) Val = cast<Constant>(To);
1764 Values.push_back(Val);
1765 }
1766
1767 Constant *Replacement = ConstantPacked::get(getType(), Values);
1768 assert(Replacement != this && "I didn't contain From!");
1769
Chris Lattner7a1450d2005-10-04 18:13:04 +00001770 // Everyone using this now uses the replacement.
1771 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001772
1773 // Delete the old constant!
1774 destroyConstant();
1775}
1776
1777void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001778 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001779 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
1780 Constant *To = cast<Constant>(ToV);
1781
1782 Constant *Replacement = 0;
1783 if (getOpcode() == Instruction::GetElementPtr) {
1784 std::vector<Constant*> Indices;
1785 Constant *Pointer = getOperand(0);
1786 Indices.reserve(getNumOperands()-1);
1787 if (Pointer == From) Pointer = To;
1788
1789 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1790 Constant *Val = getOperand(i);
1791 if (Val == From) Val = To;
1792 Indices.push_back(Val);
1793 }
1794 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
1795 } else if (getOpcode() == Instruction::Cast) {
1796 assert(getOperand(0) == From && "Cast only has one use!");
1797 Replacement = ConstantExpr::getCast(To, getType());
1798 } else if (getOpcode() == Instruction::Select) {
1799 Constant *C1 = getOperand(0);
1800 Constant *C2 = getOperand(1);
1801 Constant *C3 = getOperand(2);
1802 if (C1 == From) C1 = To;
1803 if (C2 == From) C2 = To;
1804 if (C3 == From) C3 = To;
1805 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00001806 } else if (getOpcode() == Instruction::ExtractElement) {
1807 Constant *C1 = getOperand(0);
1808 Constant *C2 = getOperand(1);
1809 if (C1 == From) C1 = To;
1810 if (C2 == From) C2 = To;
1811 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00001812 } else if (getOpcode() == Instruction::InsertElement) {
1813 Constant *C1 = getOperand(0);
1814 Constant *C2 = getOperand(1);
1815 Constant *C3 = getOperand(1);
1816 if (C1 == From) C1 = To;
1817 if (C2 == From) C2 = To;
1818 if (C3 == From) C3 = To;
1819 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
1820 } else if (getOpcode() == Instruction::ShuffleVector) {
1821 Constant *C1 = getOperand(0);
1822 Constant *C2 = getOperand(1);
1823 Constant *C3 = getOperand(2);
1824 if (C1 == From) C1 = To;
1825 if (C2 == From) C2 = To;
1826 if (C3 == From) C3 = To;
1827 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001828 } else if (getNumOperands() == 2) {
1829 Constant *C1 = getOperand(0);
1830 Constant *C2 = getOperand(1);
1831 if (C1 == From) C1 = To;
1832 if (C2 == From) C2 = To;
1833 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
1834 } else {
1835 assert(0 && "Unknown ConstantExpr type!");
1836 return;
1837 }
1838
1839 assert(Replacement != this && "I didn't contain From!");
1840
Chris Lattner7a1450d2005-10-04 18:13:04 +00001841 // Everyone using this now uses the replacement.
1842 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001843
1844 // Delete the old constant!
1845 destroyConstant();
1846}
1847
1848
1849
Chris Lattner99a669b2004-11-19 16:39:44 +00001850/// clearAllValueMaps - This method frees all internal memory used by the
1851/// constant subsystem, which can be used in environments where this memory
1852/// is otherwise reported as a leak.
1853void Constant::clearAllValueMaps() {
1854 std::vector<Constant *> Constants;
1855
1856 DoubleConstants.clear(Constants);
1857 FloatConstants.clear(Constants);
1858 SIntConstants.clear(Constants);
1859 UIntConstants.clear(Constants);
1860 AggZeroConstants.clear(Constants);
1861 ArrayConstants.clear(Constants);
1862 StructConstants.clear(Constants);
1863 PackedConstants.clear(Constants);
1864 NullPtrConstants.clear(Constants);
1865 UndefValueConstants.clear(Constants);
1866 ExprConstants.clear(Constants);
1867
Misha Brukmanb1c93172005-04-21 23:48:37 +00001868 for (std::vector<Constant *>::iterator I = Constants.begin(),
Chris Lattner99a669b2004-11-19 16:39:44 +00001869 E = Constants.end(); I != E; ++I)
1870 (*I)->dropAllReferences();
1871 for (std::vector<Constant *>::iterator I = Constants.begin(),
1872 E = Constants.end(); I != E; ++I)
1873 (*I)->destroyConstantImpl();
1874 Constants.clear();
1875}
Jim Laskey2698f0d2006-03-08 18:11:07 +00001876
1877/// getStringValue - Turn an LLVM constant pointer that eventually points to a
1878/// global into a string value. Return an empty string if we can't do it.
Evan Cheng38280c02006-03-10 23:52:03 +00001879/// Parameter Chop determines if the result is chopped at the first null
1880/// terminator.
Jim Laskey2698f0d2006-03-08 18:11:07 +00001881///
Evan Cheng38280c02006-03-10 23:52:03 +00001882std::string Constant::getStringValue(bool Chop, unsigned Offset) {
Jim Laskey2698f0d2006-03-08 18:11:07 +00001883 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(this)) {
1884 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
1885 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
1886 if (Init->isString()) {
1887 std::string Result = Init->getAsString();
1888 if (Offset < Result.size()) {
1889 // If we are pointing INTO The string, erase the beginning...
1890 Result.erase(Result.begin(), Result.begin()+Offset);
1891
1892 // Take off the null terminator, and any string fragments after it.
Evan Cheng38280c02006-03-10 23:52:03 +00001893 if (Chop) {
1894 std::string::size_type NullPos = Result.find_first_of((char)0);
1895 if (NullPos != std::string::npos)
1896 Result.erase(Result.begin()+NullPos, Result.end());
1897 }
Jim Laskey2698f0d2006-03-08 18:11:07 +00001898 return Result;
1899 }
1900 }
1901 }
1902 } else if (Constant *C = dyn_cast<Constant>(this)) {
1903 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
Evan Cheng2c5e5302006-03-11 00:13:10 +00001904 return GV->getStringValue(Chop, Offset);
Jim Laskey2698f0d2006-03-08 18:11:07 +00001905 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
1906 if (CE->getOpcode() == Instruction::GetElementPtr) {
1907 // Turn a gep into the specified offset.
1908 if (CE->getNumOperands() == 3 &&
1909 cast<Constant>(CE->getOperand(1))->isNullValue() &&
1910 isa<ConstantInt>(CE->getOperand(2))) {
1911 Offset += cast<ConstantInt>(CE->getOperand(2))->getRawValue();
Evan Cheng2c5e5302006-03-11 00:13:10 +00001912 return CE->getOperand(0)->getStringValue(Chop, Offset);
Jim Laskey2698f0d2006-03-08 18:11:07 +00001913 }
1914 }
1915 }
1916 }
1917 return "";
1918}
1919