blob: c36fea94869f38f46c40b9494a67aa979c5c9ebc [file] [log] [blame]
Chris Lattner9bc02a42003-05-13 21:37:02 +00001//===-- Constants.cpp - Implement Constant nodes --------------------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-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 Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
Chris Lattnere9bb2df2001-12-03 22:26:30 +000010// This file implements the Constant* classes...
Chris Lattner00950542001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner31bcdb82002-04-28 19:55:58 +000014#include "llvm/Constants.h"
Chris Lattnercbfd4062004-01-12 21:13:12 +000015#include "ConstantFolding.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include "llvm/DerivedTypes.h"
Reid Spencer1c9c8e62004-07-17 23:48:33 +000017#include "llvm/GlobalValue.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000018#include "llvm/Instructions.h"
Chris Lattner00950542001-06-06 20:29:01 +000019#include "llvm/SymbolTable.h"
Chris Lattnerf5ec48d2001-10-13 06:57:33 +000020#include "llvm/Module.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/ADT/StringExtras.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000022#include "llvm/Support/Compiler.h"
Bill Wendling2e3def12006-11-17 08:03:48 +000023#include "llvm/Support/Debug.h"
Chris Lattner8a94bf12006-09-28 00:35:06 +000024#include "llvm/Support/ManagedStatic.h"
Bill Wendling2e3def12006-11-17 08:03:48 +000025#include "llvm/Support/MathExtras.h"
Chris Lattner00950542001-06-06 20:29:01 +000026#include <algorithm>
Chris Lattner31f84992003-11-21 20:23:48 +000027using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000028
Chris Lattner00950542001-06-06 20:29:01 +000029//===----------------------------------------------------------------------===//
Chris Lattnere9bb2df2001-12-03 22:26:30 +000030// Constant Class
Chris Lattner00950542001-06-06 20:29:01 +000031//===----------------------------------------------------------------------===//
32
Chris Lattnere9bb2df2001-12-03 22:26:30 +000033void Constant::destroyConstantImpl() {
34 // When a Constant is destroyed, there may be lingering
Chris Lattnerf5ec48d2001-10-13 06:57:33 +000035 // references to the constant by other constants in the constant pool. These
Misha Brukmanef6a6a62003-08-21 22:14:26 +000036 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerf5ec48d2001-10-13 06:57:33 +000037 // but they don't know that. Because we only find out when the CPV is
38 // deleted, we must now notify all of our users (that should only be
Chris Lattnere9bb2df2001-12-03 22:26:30 +000039 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerf5ec48d2001-10-13 06:57:33 +000040 //
41 while (!use_empty()) {
42 Value *V = use_back();
43#ifndef NDEBUG // Only in -g mode...
Chris Lattner6183b922002-07-18 00:14:50 +000044 if (!isa<Constant>(V))
Bill Wendling2e3def12006-11-17 08:03:48 +000045 DOUT << "While deleting: " << *this
46 << "\n\nUse still stuck around after Def is destroyed: "
47 << *V << "\n\n";
Chris Lattnerf5ec48d2001-10-13 06:57:33 +000048#endif
Vikram S. Adve345e0cf2002-07-14 23:13:17 +000049 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1c9c8e62004-07-17 23:48:33 +000050 Constant *CV = cast<Constant>(V);
51 CV->destroyConstant();
Chris Lattnerf5ec48d2001-10-13 06:57:33 +000052
53 // The constant should remove itself from our use list...
Vikram S. Adve345e0cf2002-07-14 23:13:17 +000054 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerf5ec48d2001-10-13 06:57:33 +000055 }
56
57 // Value has no outstanding references it is safe to delete it now...
58 delete this;
Chris Lattner1d87bcf2001-10-01 20:11:19 +000059}
Chris Lattner00950542001-06-06 20:29:01 +000060
Chris Lattner35b89fa2006-10-20 00:27:06 +000061/// canTrap - Return true if evaluation of this constant could trap. This is
62/// true for things like constant expressions that could divide by zero.
63bool Constant::canTrap() const {
64 assert(getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
65 // The only thing that could possibly trap are constant exprs.
66 const ConstantExpr *CE = dyn_cast<ConstantExpr>(this);
67 if (!CE) return false;
68
69 // ConstantExpr traps if any operands can trap.
70 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
71 if (getOperand(i)->canTrap())
72 return true;
73
74 // Otherwise, only specific operations can trap.
75 switch (CE->getOpcode()) {
76 default:
77 return false;
Reid Spencer1628cec2006-10-26 06:15:43 +000078 case Instruction::UDiv:
79 case Instruction::SDiv:
80 case Instruction::FDiv:
Reid Spencer0a783f72006-11-02 01:53:59 +000081 case Instruction::URem:
82 case Instruction::SRem:
83 case Instruction::FRem:
Chris Lattner35b89fa2006-10-20 00:27:06 +000084 // Div and rem can trap if the RHS is not known to be non-zero.
85 if (!isa<ConstantInt>(getOperand(1)) || getOperand(1)->isNullValue())
86 return true;
87 return false;
88 }
89}
90
91
Chris Lattner9fb96412002-08-13 17:50:20 +000092// Static constructor to create a '0' constant of arbitrary type...
93Constant *Constant::getNullValue(const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000094 switch (Ty->getTypeID()) {
Chris Lattner70b06fc2003-10-03 19:34:51 +000095 case Type::BoolTyID: {
96 static Constant *NullBool = ConstantBool::get(false);
97 return NullBool;
98 }
99 case Type::SByteTyID: {
Reid Spencerb83eb642006-10-20 07:07:24 +0000100 static Constant *NullSByte = ConstantInt::get(Type::SByteTy, 0);
Chris Lattner70b06fc2003-10-03 19:34:51 +0000101 return NullSByte;
102 }
103 case Type::UByteTyID: {
Reid Spencerb83eb642006-10-20 07:07:24 +0000104 static Constant *NullUByte = ConstantInt::get(Type::UByteTy, 0);
Chris Lattner70b06fc2003-10-03 19:34:51 +0000105 return NullUByte;
106 }
107 case Type::ShortTyID: {
Reid Spencerb83eb642006-10-20 07:07:24 +0000108 static Constant *NullShort = ConstantInt::get(Type::ShortTy, 0);
Chris Lattner70b06fc2003-10-03 19:34:51 +0000109 return NullShort;
110 }
111 case Type::UShortTyID: {
Reid Spencerb83eb642006-10-20 07:07:24 +0000112 static Constant *NullUShort = ConstantInt::get(Type::UShortTy, 0);
Chris Lattner70b06fc2003-10-03 19:34:51 +0000113 return NullUShort;
114 }
115 case Type::IntTyID: {
Reid Spencerb83eb642006-10-20 07:07:24 +0000116 static Constant *NullInt = ConstantInt::get(Type::IntTy, 0);
Chris Lattner70b06fc2003-10-03 19:34:51 +0000117 return NullInt;
118 }
119 case Type::UIntTyID: {
Reid Spencerb83eb642006-10-20 07:07:24 +0000120 static Constant *NullUInt = ConstantInt::get(Type::UIntTy, 0);
Chris Lattner70b06fc2003-10-03 19:34:51 +0000121 return NullUInt;
122 }
123 case Type::LongTyID: {
Reid Spencerb83eb642006-10-20 07:07:24 +0000124 static Constant *NullLong = ConstantInt::get(Type::LongTy, 0);
Chris Lattner70b06fc2003-10-03 19:34:51 +0000125 return NullLong;
126 }
127 case Type::ULongTyID: {
Reid Spencerb83eb642006-10-20 07:07:24 +0000128 static Constant *NullULong = ConstantInt::get(Type::ULongTy, 0);
Chris Lattner70b06fc2003-10-03 19:34:51 +0000129 return NullULong;
130 }
Chris Lattner9fb96412002-08-13 17:50:20 +0000131
Chris Lattner70b06fc2003-10-03 19:34:51 +0000132 case Type::FloatTyID: {
133 static Constant *NullFloat = ConstantFP::get(Type::FloatTy, 0);
134 return NullFloat;
135 }
136 case Type::DoubleTyID: {
137 static Constant *NullDouble = ConstantFP::get(Type::DoubleTy, 0);
138 return NullDouble;
139 }
Chris Lattner9fb96412002-08-13 17:50:20 +0000140
Misha Brukmanfd939082005-04-21 23:48:37 +0000141 case Type::PointerTyID:
Chris Lattner9fb96412002-08-13 17:50:20 +0000142 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner70b06fc2003-10-03 19:34:51 +0000143
Chris Lattner40bbeb52004-02-15 05:53:04 +0000144 case Type::StructTyID:
145 case Type::ArrayTyID:
Brian Gaeke715c90b2004-08-20 06:00:58 +0000146 case Type::PackedTyID:
Chris Lattner40bbeb52004-02-15 05:53:04 +0000147 return ConstantAggregateZero::get(Ty);
Chris Lattner9fb96412002-08-13 17:50:20 +0000148 default:
Reid Spencer57f6efc2004-07-04 11:51:24 +0000149 // Function, Label, or Opaque type?
150 assert(!"Cannot create a null constant of that type!");
Chris Lattner9fb96412002-08-13 17:50:20 +0000151 return 0;
152 }
153}
154
155// Static constructor to create the maximum constant of an integral type...
156ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000157 switch (Ty->getTypeID()) {
Chris Lattner003cbf32006-09-28 23:36:21 +0000158 case Type::BoolTyID: return ConstantBool::getTrue();
Chris Lattner9fb96412002-08-13 17:50:20 +0000159 case Type::SByteTyID:
160 case Type::ShortTyID:
161 case Type::IntTyID:
162 case Type::LongTyID: {
Misha Brukmanfd939082005-04-21 23:48:37 +0000163 // Calculate 011111111111111...
Chris Lattner9fb96412002-08-13 17:50:20 +0000164 unsigned TypeBits = Ty->getPrimitiveSize()*8;
165 int64_t Val = INT64_MAX; // All ones
166 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
Reid Spencerb83eb642006-10-20 07:07:24 +0000167 return ConstantInt::get(Ty, Val);
Chris Lattner9fb96412002-08-13 17:50:20 +0000168 }
169
170 case Type::UByteTyID:
171 case Type::UShortTyID:
172 case Type::UIntTyID:
173 case Type::ULongTyID: return getAllOnesValue(Ty);
174
Chris Lattner227b86c2002-08-14 17:12:13 +0000175 default: return 0;
Chris Lattner9fb96412002-08-13 17:50:20 +0000176 }
177}
178
179// Static constructor to create the minimum constant for an integral type...
180ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000181 switch (Ty->getTypeID()) {
Chris Lattner003cbf32006-09-28 23:36:21 +0000182 case Type::BoolTyID: return ConstantBool::getFalse();
Chris Lattner9fb96412002-08-13 17:50:20 +0000183 case Type::SByteTyID:
184 case Type::ShortTyID:
185 case Type::IntTyID:
186 case Type::LongTyID: {
Misha Brukmanfd939082005-04-21 23:48:37 +0000187 // Calculate 1111111111000000000000
Chris Lattner9fb96412002-08-13 17:50:20 +0000188 unsigned TypeBits = Ty->getPrimitiveSize()*8;
189 int64_t Val = -1; // All ones
190 Val <<= TypeBits-1; // Shift over to the right spot
Reid Spencerb83eb642006-10-20 07:07:24 +0000191 return ConstantInt::get(Ty, Val);
Chris Lattner9fb96412002-08-13 17:50:20 +0000192 }
193
194 case Type::UByteTyID:
195 case Type::UShortTyID:
196 case Type::UIntTyID:
Reid Spencerb83eb642006-10-20 07:07:24 +0000197 case Type::ULongTyID: return ConstantInt::get(Ty, 0);
Chris Lattner9fb96412002-08-13 17:50:20 +0000198
Chris Lattner227b86c2002-08-14 17:12:13 +0000199 default: return 0;
Chris Lattner9fb96412002-08-13 17:50:20 +0000200 }
201}
202
203// Static constructor to create an integral constant with all bits set
204ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000205 switch (Ty->getTypeID()) {
Chris Lattner003cbf32006-09-28 23:36:21 +0000206 case Type::BoolTyID: return ConstantBool::getTrue();
Chris Lattner9fb96412002-08-13 17:50:20 +0000207 case Type::SByteTyID:
208 case Type::ShortTyID:
209 case Type::IntTyID:
Reid Spencerb83eb642006-10-20 07:07:24 +0000210 case Type::LongTyID: return ConstantInt::get(Ty, -1);
Chris Lattner9fb96412002-08-13 17:50:20 +0000211
212 case Type::UByteTyID:
213 case Type::UShortTyID:
214 case Type::UIntTyID:
215 case Type::ULongTyID: {
216 // Calculate ~0 of the right type...
217 unsigned TypeBits = Ty->getPrimitiveSize()*8;
218 uint64_t Val = ~0ULL; // All ones
219 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
Reid Spencerb83eb642006-10-20 07:07:24 +0000220 return ConstantInt::get(Ty, Val);
Chris Lattner9fb96412002-08-13 17:50:20 +0000221 }
Chris Lattner227b86c2002-08-14 17:12:13 +0000222 default: return 0;
Chris Lattner9fb96412002-08-13 17:50:20 +0000223 }
224}
225
Chris Lattner00950542001-06-06 20:29:01 +0000226//===----------------------------------------------------------------------===//
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000227// ConstantXXX Classes
Chris Lattner00950542001-06-06 20:29:01 +0000228//===----------------------------------------------------------------------===//
229
230//===----------------------------------------------------------------------===//
231// Normal Constructors
232
Chris Lattnerdf0ef1d2005-09-27 06:09:08 +0000233ConstantIntegral::ConstantIntegral(const Type *Ty, ValueTy VT, uint64_t V)
Reid Spencerb83eb642006-10-20 07:07:24 +0000234 : Constant(Ty, VT, 0, 0), Val(V) {
Chris Lattner00950542001-06-06 20:29:01 +0000235}
Chris Lattner531daef2001-09-07 16:46:31 +0000236
Chris Lattnerdf0ef1d2005-09-27 06:09:08 +0000237ConstantBool::ConstantBool(bool V)
Reid Spencerb83eb642006-10-20 07:07:24 +0000238 : ConstantIntegral(Type::BoolTy, ConstantBoolVal, uint64_t(V)) {
Chris Lattnerc2dfb8b2004-06-21 12:12:12 +0000239}
240
Reid Spencerb83eb642006-10-20 07:07:24 +0000241ConstantInt::ConstantInt(const Type *Ty, uint64_t V)
242 : ConstantIntegral(Ty, ConstantIntVal, V) {
Chris Lattner00950542001-06-06 20:29:01 +0000243}
244
Chris Lattnere4671472005-01-29 00:34:39 +0000245ConstantFP::ConstantFP(const Type *Ty, double V)
Chris Lattnerdf0ef1d2005-09-27 06:09:08 +0000246 : Constant(Ty, ConstantFPVal, 0, 0) {
Chris Lattner37bf6302001-07-20 19:16:02 +0000247 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner00950542001-06-06 20:29:01 +0000248 Val = V;
249}
250
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000251ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnere4671472005-01-29 00:34:39 +0000252 const std::vector<Constant*> &V)
Chris Lattnerdf0ef1d2005-09-27 06:09:08 +0000253 : Constant(T, ConstantArrayVal, new Use[V.size()], V.size()) {
Alkis Evlogimenose0de1d62004-09-15 02:32:15 +0000254 assert(V.size() == T->getNumElements() &&
255 "Invalid initializer vector for constant array");
Chris Lattnere4671472005-01-29 00:34:39 +0000256 Use *OL = OperandList;
Chris Lattnerdfdd6c52005-10-03 21:56:24 +0000257 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
258 I != E; ++I, ++OL) {
Chris Lattner71abaab2005-10-07 05:23:36 +0000259 Constant *C = *I;
260 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscad90ad2004-09-10 04:16:59 +0000261 (T->isAbstract() &&
Chris Lattner71abaab2005-10-07 05:23:36 +0000262 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscad90ad2004-09-10 04:16:59 +0000263 "Initializer for array element doesn't match array element type!");
Chris Lattner71abaab2005-10-07 05:23:36 +0000264 OL->init(C, this);
Chris Lattner00950542001-06-06 20:29:01 +0000265 }
266}
267
Chris Lattnere4671472005-01-29 00:34:39 +0000268ConstantArray::~ConstantArray() {
269 delete [] OperandList;
270}
271
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000272ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnere4671472005-01-29 00:34:39 +0000273 const std::vector<Constant*> &V)
Chris Lattnerdf0ef1d2005-09-27 06:09:08 +0000274 : Constant(T, ConstantStructVal, new Use[V.size()], V.size()) {
Chris Lattnerd21cd802004-02-09 04:37:31 +0000275 assert(V.size() == T->getNumElements() &&
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000276 "Invalid initializer vector for constant structure");
Chris Lattnere4671472005-01-29 00:34:39 +0000277 Use *OL = OperandList;
Chris Lattnerdfdd6c52005-10-03 21:56:24 +0000278 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
279 I != E; ++I, ++OL) {
Chris Lattner71abaab2005-10-07 05:23:36 +0000280 Constant *C = *I;
281 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattnerdfdd6c52005-10-03 21:56:24 +0000282 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner71abaab2005-10-07 05:23:36 +0000283 C->getType()->isAbstract()) &&
Chris Lattnerdfdd6c52005-10-03 21:56:24 +0000284 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner71abaab2005-10-07 05:23:36 +0000285 C->getType()->getTypeID())) &&
Chris Lattnerb8438892003-06-02 17:42:47 +0000286 "Initializer for struct element doesn't match struct element type!");
Chris Lattner71abaab2005-10-07 05:23:36 +0000287 OL->init(C, this);
Chris Lattner00950542001-06-06 20:29:01 +0000288 }
289}
290
Chris Lattnere4671472005-01-29 00:34:39 +0000291ConstantStruct::~ConstantStruct() {
292 delete [] OperandList;
293}
294
295
Brian Gaeke715c90b2004-08-20 06:00:58 +0000296ConstantPacked::ConstantPacked(const PackedType *T,
Chris Lattnere4671472005-01-29 00:34:39 +0000297 const std::vector<Constant*> &V)
Chris Lattnerdf0ef1d2005-09-27 06:09:08 +0000298 : Constant(T, ConstantPackedVal, new Use[V.size()], V.size()) {
Chris Lattnere4671472005-01-29 00:34:39 +0000299 Use *OL = OperandList;
Chris Lattnerdfdd6c52005-10-03 21:56:24 +0000300 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
301 I != E; ++I, ++OL) {
Chris Lattner71abaab2005-10-07 05:23:36 +0000302 Constant *C = *I;
303 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscad90ad2004-09-10 04:16:59 +0000304 (T->isAbstract() &&
Chris Lattner71abaab2005-10-07 05:23:36 +0000305 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscad90ad2004-09-10 04:16:59 +0000306 "Initializer for packed element doesn't match packed element type!");
Chris Lattner71abaab2005-10-07 05:23:36 +0000307 OL->init(C, this);
Brian Gaeke715c90b2004-08-20 06:00:58 +0000308 }
309}
310
Chris Lattnere4671472005-01-29 00:34:39 +0000311ConstantPacked::~ConstantPacked() {
312 delete [] OperandList;
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000313}
314
Chris Lattnere4671472005-01-29 00:34:39 +0000315/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
316/// behind the scenes to implement unary constant exprs.
Chris Lattnerf190d382006-06-28 21:38:54 +0000317namespace {
318class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
Chris Lattnere4671472005-01-29 00:34:39 +0000319 Use Op;
320public:
321 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
322 : ConstantExpr(Ty, Opcode, &Op, 1), Op(C, this) {}
323};
Chris Lattnerf190d382006-06-28 21:38:54 +0000324}
Chris Lattner08a45cc2004-03-12 05:54:04 +0000325
Chris Lattner037d2582003-06-22 20:48:30 +0000326static bool isSetCC(unsigned Opcode) {
327 return Opcode == Instruction::SetEQ || Opcode == Instruction::SetNE ||
328 Opcode == Instruction::SetLT || Opcode == Instruction::SetGT ||
329 Opcode == Instruction::SetLE || Opcode == Instruction::SetGE;
330}
331
Chris Lattnere4671472005-01-29 00:34:39 +0000332/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
333/// behind the scenes to implement binary constant exprs.
Chris Lattnerf190d382006-06-28 21:38:54 +0000334namespace {
335class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Chris Lattnere4671472005-01-29 00:34:39 +0000336 Use Ops[2];
337public:
338 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
339 : ConstantExpr(isSetCC(Opcode) ? Type::BoolTy : C1->getType(),
340 Opcode, Ops, 2) {
341 Ops[0].init(C1, this);
342 Ops[1].init(C2, this);
343 }
344};
Chris Lattnerf190d382006-06-28 21:38:54 +0000345}
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000346
Chris Lattnere4671472005-01-29 00:34:39 +0000347/// SelectConstantExpr - This class is private to Constants.cpp, and is used
348/// behind the scenes to implement select constant exprs.
Chris Lattnerf190d382006-06-28 21:38:54 +0000349namespace {
350class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Chris Lattnere4671472005-01-29 00:34:39 +0000351 Use Ops[3];
352public:
353 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
354 : ConstantExpr(C2->getType(), Instruction::Select, Ops, 3) {
355 Ops[0].init(C1, this);
356 Ops[1].init(C2, this);
357 Ops[2].init(C3, this);
358 }
359};
Chris Lattnerf190d382006-06-28 21:38:54 +0000360}
Chris Lattnere4671472005-01-29 00:34:39 +0000361
Robert Bocchinoc152f9c2006-01-17 20:07:22 +0000362/// ExtractElementConstantExpr - This class is private to
363/// Constants.cpp, and is used behind the scenes to implement
364/// extractelement constant exprs.
Chris Lattnerf190d382006-06-28 21:38:54 +0000365namespace {
366class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Robert Bocchinob52ee7f2006-01-10 19:05:34 +0000367 Use Ops[2];
368public:
369 ExtractElementConstantExpr(Constant *C1, Constant *C2)
370 : ConstantExpr(cast<PackedType>(C1->getType())->getElementType(),
371 Instruction::ExtractElement, Ops, 2) {
372 Ops[0].init(C1, this);
373 Ops[1].init(C2, this);
374 }
375};
Chris Lattnerf190d382006-06-28 21:38:54 +0000376}
Robert Bocchinob52ee7f2006-01-10 19:05:34 +0000377
Robert Bocchinoc152f9c2006-01-17 20:07:22 +0000378/// InsertElementConstantExpr - This class is private to
379/// Constants.cpp, and is used behind the scenes to implement
380/// insertelement constant exprs.
Chris Lattnerf190d382006-06-28 21:38:54 +0000381namespace {
382class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Robert Bocchinoc152f9c2006-01-17 20:07:22 +0000383 Use Ops[3];
384public:
385 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
386 : ConstantExpr(C1->getType(), Instruction::InsertElement,
387 Ops, 3) {
388 Ops[0].init(C1, this);
389 Ops[1].init(C2, this);
390 Ops[2].init(C3, this);
391 }
392};
Chris Lattnerf190d382006-06-28 21:38:54 +0000393}
Robert Bocchinoc152f9c2006-01-17 20:07:22 +0000394
Chris Lattner00f10232006-04-08 01:18:18 +0000395/// ShuffleVectorConstantExpr - This class is private to
396/// Constants.cpp, and is used behind the scenes to implement
397/// shufflevector constant exprs.
Chris Lattnerf190d382006-06-28 21:38:54 +0000398namespace {
399class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Chris Lattner00f10232006-04-08 01:18:18 +0000400 Use Ops[3];
401public:
402 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
403 : ConstantExpr(C1->getType(), Instruction::ShuffleVector,
404 Ops, 3) {
405 Ops[0].init(C1, this);
406 Ops[1].init(C2, this);
407 Ops[2].init(C3, this);
408 }
409};
Chris Lattnerf190d382006-06-28 21:38:54 +0000410}
Chris Lattner00f10232006-04-08 01:18:18 +0000411
Chris Lattnere4671472005-01-29 00:34:39 +0000412/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
413/// used behind the scenes to implement getelementpr constant exprs.
Chris Lattnerf190d382006-06-28 21:38:54 +0000414namespace {
415struct VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Chris Lattnere4671472005-01-29 00:34:39 +0000416 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
417 const Type *DestTy)
418 : ConstantExpr(DestTy, Instruction::GetElementPtr,
419 new Use[IdxList.size()+1], IdxList.size()+1) {
420 OperandList[0].init(C, this);
421 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
422 OperandList[i+1].init(IdxList[i], this);
423 }
424 ~GetElementPtrConstantExpr() {
Misha Brukmanfd939082005-04-21 23:48:37 +0000425 delete [] OperandList;
Chris Lattnere4671472005-01-29 00:34:39 +0000426 }
427};
Chris Lattnerf190d382006-06-28 21:38:54 +0000428}
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000429
Reid Spencer3da59db2006-11-27 01:05:10 +0000430
431// Utility function for determining if a ConstantExpr is a CastOp or not. This
432// can't be inline because we don't want to #include Instruction.h into
433// Constant.h
434bool ConstantExpr::isCast() const {
435 return Instruction::isCast(getOpcode());
436}
437
Chris Lattner4dcb5402004-03-29 02:37:53 +0000438/// ConstantExpr::get* - Return some common constants without having to
439/// specify the full Instruction::OPCODE identifier.
440///
441Constant *ConstantExpr::getNeg(Constant *C) {
Chris Lattnerd25e3ec2004-03-29 19:51:24 +0000442 if (!C->getType()->isFloatingPoint())
443 return get(Instruction::Sub, getNullValue(C->getType()), C);
444 else
445 return get(Instruction::Sub, ConstantFP::get(C->getType(), -0.0), C);
Chris Lattner4dcb5402004-03-29 02:37:53 +0000446}
447Constant *ConstantExpr::getNot(Constant *C) {
448 assert(isa<ConstantIntegral>(C) && "Cannot NOT a nonintegral type!");
449 return get(Instruction::Xor, C,
450 ConstantIntegral::getAllOnesValue(C->getType()));
451}
452Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
453 return get(Instruction::Add, C1, C2);
454}
455Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
456 return get(Instruction::Sub, C1, C2);
457}
458Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
459 return get(Instruction::Mul, C1, C2);
460}
Reid Spencer1628cec2006-10-26 06:15:43 +0000461Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2) {
462 return get(Instruction::UDiv, C1, C2);
463}
464Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2) {
465 return get(Instruction::SDiv, C1, C2);
466}
467Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) {
468 return get(Instruction::FDiv, C1, C2);
Chris Lattner4dcb5402004-03-29 02:37:53 +0000469}
Reid Spencer0a783f72006-11-02 01:53:59 +0000470Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) {
471 return get(Instruction::URem, C1, C2);
472}
473Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) {
474 return get(Instruction::SRem, C1, C2);
475}
476Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) {
477 return get(Instruction::FRem, C1, C2);
Chris Lattner4dcb5402004-03-29 02:37:53 +0000478}
479Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
480 return get(Instruction::And, C1, C2);
481}
482Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
483 return get(Instruction::Or, C1, C2);
484}
485Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
486 return get(Instruction::Xor, C1, C2);
487}
488Constant *ConstantExpr::getSetEQ(Constant *C1, Constant *C2) {
489 return get(Instruction::SetEQ, C1, C2);
490}
491Constant *ConstantExpr::getSetNE(Constant *C1, Constant *C2) {
492 return get(Instruction::SetNE, C1, C2);
493}
494Constant *ConstantExpr::getSetLT(Constant *C1, Constant *C2) {
495 return get(Instruction::SetLT, C1, C2);
496}
497Constant *ConstantExpr::getSetGT(Constant *C1, Constant *C2) {
498 return get(Instruction::SetGT, C1, C2);
499}
500Constant *ConstantExpr::getSetLE(Constant *C1, Constant *C2) {
501 return get(Instruction::SetLE, C1, C2);
502}
503Constant *ConstantExpr::getSetGE(Constant *C1, Constant *C2) {
504 return get(Instruction::SetGE, C1, C2);
505}
506Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
507 return get(Instruction::Shl, C1, C2);
508}
Reid Spencer3822ff52006-11-08 06:47:33 +0000509Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2) {
510 return get(Instruction::LShr, C1, C2);
Chris Lattner4dcb5402004-03-29 02:37:53 +0000511}
Reid Spencer3822ff52006-11-08 06:47:33 +0000512Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2) {
513 return get(Instruction::AShr, C1, C2);
Chris Lattnerc9710252004-05-25 05:32:43 +0000514}
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000515
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000516/// getWithOperandReplaced - Return a constant expression identical to this
517/// one, but with the specified operand set to the specified value.
Reid Spencer3da59db2006-11-27 01:05:10 +0000518Constant *
519ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000520 assert(OpNo < getNumOperands() && "Operand num is out of range!");
521 assert(Op->getType() == getOperand(OpNo)->getType() &&
522 "Replacing operand with value of different type!");
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000523 if (getOperand(OpNo) == Op)
524 return const_cast<ConstantExpr*>(this);
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000525
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000526 Constant *Op0, *Op1, *Op2;
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000527 switch (getOpcode()) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000528 case Instruction::Trunc:
529 case Instruction::ZExt:
530 case Instruction::SExt:
531 case Instruction::FPTrunc:
532 case Instruction::FPExt:
533 case Instruction::UIToFP:
534 case Instruction::SIToFP:
535 case Instruction::FPToUI:
536 case Instruction::FPToSI:
537 case Instruction::PtrToInt:
538 case Instruction::IntToPtr:
539 case Instruction::BitCast:
540 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000541 case Instruction::Select:
542 Op0 = (OpNo == 0) ? Op : getOperand(0);
543 Op1 = (OpNo == 1) ? Op : getOperand(1);
544 Op2 = (OpNo == 2) ? Op : getOperand(2);
545 return ConstantExpr::getSelect(Op0, Op1, Op2);
546 case Instruction::InsertElement:
547 Op0 = (OpNo == 0) ? Op : getOperand(0);
548 Op1 = (OpNo == 1) ? Op : getOperand(1);
549 Op2 = (OpNo == 2) ? Op : getOperand(2);
550 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
551 case Instruction::ExtractElement:
552 Op0 = (OpNo == 0) ? Op : getOperand(0);
553 Op1 = (OpNo == 1) ? Op : getOperand(1);
554 return ConstantExpr::getExtractElement(Op0, Op1);
555 case Instruction::ShuffleVector:
556 Op0 = (OpNo == 0) ? Op : getOperand(0);
557 Op1 = (OpNo == 1) ? Op : getOperand(1);
558 Op2 = (OpNo == 2) ? Op : getOperand(2);
559 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000560 case Instruction::GetElementPtr: {
561 std::vector<Constant*> Ops;
562 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
563 Ops.push_back(getOperand(i));
564 if (OpNo == 0)
565 return ConstantExpr::getGetElementPtr(Op, Ops);
566 Ops[OpNo-1] = Op;
567 return ConstantExpr::getGetElementPtr(getOperand(0), Ops);
568 }
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000569 default:
570 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000571 Op0 = (OpNo == 0) ? Op : getOperand(0);
572 Op1 = (OpNo == 1) ? Op : getOperand(1);
573 return ConstantExpr::get(getOpcode(), Op0, Op1);
574 }
575}
576
577/// getWithOperands - This returns the current constant expression with the
578/// operands replaced with the specified values. The specified operands must
579/// match count and type with the existing ones.
580Constant *ConstantExpr::
581getWithOperands(const std::vector<Constant*> &Ops) const {
582 assert(Ops.size() == getNumOperands() && "Operand count mismatch!");
583 bool AnyChange = false;
584 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
585 assert(Ops[i]->getType() == getOperand(i)->getType() &&
586 "Operand type mismatch!");
587 AnyChange |= Ops[i] != getOperand(i);
588 }
589 if (!AnyChange) // No operands changed, return self.
590 return const_cast<ConstantExpr*>(this);
591
592 switch (getOpcode()) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000593 case Instruction::Trunc:
594 case Instruction::ZExt:
595 case Instruction::SExt:
596 case Instruction::FPTrunc:
597 case Instruction::FPExt:
598 case Instruction::UIToFP:
599 case Instruction::SIToFP:
600 case Instruction::FPToUI:
601 case Instruction::FPToSI:
602 case Instruction::PtrToInt:
603 case Instruction::IntToPtr:
604 case Instruction::BitCast:
605 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000606 case Instruction::Select:
607 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
608 case Instruction::InsertElement:
609 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
610 case Instruction::ExtractElement:
611 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
612 case Instruction::ShuffleVector:
613 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
614 case Instruction::GetElementPtr: {
615 std::vector<Constant*> ActualOps(Ops.begin()+1, Ops.end());
616 return ConstantExpr::getGetElementPtr(Ops[0], ActualOps);
617 }
618 default:
619 assert(getNumOperands() == 2 && "Must be binary operator?");
620 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000621 }
622}
623
Chris Lattner00950542001-06-06 20:29:01 +0000624
625//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +0000626// isValueValidForType implementations
627
Reid Spencerb83eb642006-10-20 07:07:24 +0000628bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000629 switch (Ty->getTypeID()) {
Chris Lattner00950542001-06-06 20:29:01 +0000630 default:
631 return false; // These can't be represented as integers!!!
Chris Lattner00950542001-06-06 20:29:01 +0000632 // Signed types...
633 case Type::SByteTyID:
634 return (Val <= INT8_MAX && Val >= INT8_MIN);
Reid Spencerb83eb642006-10-20 07:07:24 +0000635 case Type::UByteTyID:
636 return (Val >= 0) && (Val <= UINT8_MAX);
Chris Lattner00950542001-06-06 20:29:01 +0000637 case Type::ShortTyID:
638 return (Val <= INT16_MAX && Val >= INT16_MIN);
Reid Spencerb83eb642006-10-20 07:07:24 +0000639 case Type::UShortTyID:
640 return (Val >= 0) && (Val <= UINT16_MAX);
Chris Lattner00950542001-06-06 20:29:01 +0000641 case Type::IntTyID:
Chris Lattner2a502012004-06-08 23:21:39 +0000642 return (Val <= int(INT32_MAX) && Val >= int(INT32_MIN));
Chris Lattner00950542001-06-06 20:29:01 +0000643 case Type::UIntTyID:
Reid Spencerb83eb642006-10-20 07:07:24 +0000644 return (Val >= 0) && (Val <= UINT32_MAX);
645 case Type::LongTyID:
Chris Lattner00950542001-06-06 20:29:01 +0000646 case Type::ULongTyID:
Reid Spencerb83eb642006-10-20 07:07:24 +0000647 return true; // always true, has to fit in largest type
Chris Lattner00950542001-06-06 20:29:01 +0000648 }
Chris Lattner00950542001-06-06 20:29:01 +0000649}
650
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000651bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000652 switch (Ty->getTypeID()) {
Chris Lattner00950542001-06-06 20:29:01 +0000653 default:
654 return false; // These can't be represented as floating point!
655
Reid Spencer574cdb32004-12-07 07:38:08 +0000656 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner00950542001-06-06 20:29:01 +0000657 case Type::FloatTyID:
Chris Lattner00950542001-06-06 20:29:01 +0000658 case Type::DoubleTyID:
659 return true; // This is the largest type...
660 }
Chris Lattnerd74ea2b2006-05-24 17:04:05 +0000661}
Chris Lattner37bf6302001-07-20 19:16:02 +0000662
Chris Lattner531daef2001-09-07 16:46:31 +0000663//===----------------------------------------------------------------------===//
Chris Lattner531daef2001-09-07 16:46:31 +0000664// Factory Function Implementation
665
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000666// ConstantCreator - A class that is used to create constants by
667// ValueMap*. This class should be partially specialized if there is
668// something strange that needs to be done to interface to the ctor for the
669// constant.
670//
Chris Lattner31f84992003-11-21 20:23:48 +0000671namespace llvm {
672 template<class ConstantClass, class TypeClass, class ValType>
Chris Lattnerf190d382006-06-28 21:38:54 +0000673 struct VISIBILITY_HIDDEN ConstantCreator {
Chris Lattner31f84992003-11-21 20:23:48 +0000674 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
675 return new ConstantClass(Ty, V);
676 }
677 };
Misha Brukmanfd939082005-04-21 23:48:37 +0000678
Chris Lattner31f84992003-11-21 20:23:48 +0000679 template<class ConstantClass, class TypeClass>
Chris Lattnerf190d382006-06-28 21:38:54 +0000680 struct VISIBILITY_HIDDEN ConvertConstantType {
Chris Lattner31f84992003-11-21 20:23:48 +0000681 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
682 assert(0 && "This type cannot be converted!\n");
683 abort();
684 }
685 };
Chris Lattnered468e372003-10-05 00:17:43 +0000686
Chris Lattnera55b30a2005-10-04 17:48:46 +0000687 template<class ValType, class TypeClass, class ConstantClass,
688 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattnerf190d382006-06-28 21:38:54 +0000689 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
Chris Lattnercea141f2005-10-03 22:51:37 +0000690 public:
Jim Laskeyede5aa42006-07-17 17:38:29 +0000691 typedef std::pair<const Type*, ValType> MapKey;
692 typedef std::map<MapKey, Constant *> MapTy;
693 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
694 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
Chris Lattnercea141f2005-10-03 22:51:37 +0000695 private:
Chris Lattnerd7a3fc62005-10-04 16:52:46 +0000696 /// Map - This is the main map from the element descriptor to the Constants.
697 /// This is the primary way we avoid creating two of the same shape
698 /// constant.
Chris Lattnered468e372003-10-05 00:17:43 +0000699 MapTy Map;
Chris Lattnera55b30a2005-10-04 17:48:46 +0000700
701 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
702 /// from the constants to their element in Map. This is important for
703 /// removal of constants from the array, which would otherwise have to scan
704 /// through the map with very large keys.
Jim Laskeyede5aa42006-07-17 17:38:29 +0000705 InverseMapTy InverseMap;
Chris Lattnered468e372003-10-05 00:17:43 +0000706
Jim Laskeyede5aa42006-07-17 17:38:29 +0000707 /// AbstractTypeMap - Map for abstract type constants.
708 ///
Chris Lattnered468e372003-10-05 00:17:43 +0000709 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner8a7ad2d2004-11-19 16:39:44 +0000710
Chris Lattner8a7ad2d2004-11-19 16:39:44 +0000711 private:
712 void clear(std::vector<Constant *> &Constants) {
Jim Laskeyede5aa42006-07-17 17:38:29 +0000713 for(typename MapTy::iterator I = Map.begin(); I != Map.end(); ++I)
Chris Lattner8a7ad2d2004-11-19 16:39:44 +0000714 Constants.push_back(I->second);
715 Map.clear();
716 AbstractTypeMap.clear();
Chris Lattnera55b30a2005-10-04 17:48:46 +0000717 InverseMap.clear();
Chris Lattner8a7ad2d2004-11-19 16:39:44 +0000718 }
719
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000720 public:
Jim Laskeyede5aa42006-07-17 17:38:29 +0000721 typename MapTy::iterator map_end() { return Map.end(); }
Chris Lattnercea141f2005-10-03 22:51:37 +0000722
723 /// InsertOrGetItem - Return an iterator for the specified element.
724 /// If the element exists in the map, the returned iterator points to the
725 /// entry and Exists=true. If not, the iterator points to the newly
726 /// inserted entry and returns Exists=false. Newly inserted entries have
727 /// I->second == 0, and should be filled in.
Jim Laskeyede5aa42006-07-17 17:38:29 +0000728 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
729 &InsertVal,
Chris Lattnercea141f2005-10-03 22:51:37 +0000730 bool &Exists) {
Jim Laskeyede5aa42006-07-17 17:38:29 +0000731 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
Chris Lattnercea141f2005-10-03 22:51:37 +0000732 Exists = !IP.second;
733 return IP.first;
734 }
Chris Lattnerd7a3fc62005-10-04 16:52:46 +0000735
Chris Lattnera55b30a2005-10-04 17:48:46 +0000736private:
Jim Laskeyede5aa42006-07-17 17:38:29 +0000737 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
Chris Lattnera55b30a2005-10-04 17:48:46 +0000738 if (HasLargeKey) {
Jim Laskeyede5aa42006-07-17 17:38:29 +0000739 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
Chris Lattnera55b30a2005-10-04 17:48:46 +0000740 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
741 IMI->second->second == CP &&
742 "InverseMap corrupt!");
743 return IMI->second;
744 }
745
Jim Laskeyede5aa42006-07-17 17:38:29 +0000746 typename MapTy::iterator I =
Chris Lattnera55b30a2005-10-04 17:48:46 +0000747 Map.find(MapKey((TypeClass*)CP->getRawType(), getValType(CP)));
Chris Lattnerd7a3fc62005-10-04 16:52:46 +0000748 if (I == Map.end() || I->second != CP) {
749 // FIXME: This should not use a linear scan. If this gets to be a
750 // performance problem, someone should look at this.
751 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
752 /* empty */;
753 }
Chris Lattnera55b30a2005-10-04 17:48:46 +0000754 return I;
755 }
756public:
757
Chris Lattnercea141f2005-10-03 22:51:37 +0000758 /// getOrCreate - Return the specified constant from the map, creating it if
759 /// necessary.
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000760 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnered468e372003-10-05 00:17:43 +0000761 MapKey Lookup(Ty, V);
Jim Laskeyede5aa42006-07-17 17:38:29 +0000762 typename MapTy::iterator I = Map.lower_bound(Lookup);
Reid Spencerb83eb642006-10-20 07:07:24 +0000763 // Is it in the map?
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000764 if (I != Map.end() && I->first == Lookup)
Reid Spencerb83eb642006-10-20 07:07:24 +0000765 return static_cast<ConstantClass *>(I->second);
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000766
767 // If no preexisting value, create one now...
768 ConstantClass *Result =
769 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
770
Chris Lattnered468e372003-10-05 00:17:43 +0000771 /// FIXME: why does this assert fail when loading 176.gcc?
772 //assert(Result->getType() == Ty && "Type specified is not correct!");
773 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
774
Chris Lattnera55b30a2005-10-04 17:48:46 +0000775 if (HasLargeKey) // Remember the reverse mapping if needed.
776 InverseMap.insert(std::make_pair(Result, I));
777
Chris Lattnered468e372003-10-05 00:17:43 +0000778 // If the type of the constant is abstract, make sure that an entry exists
779 // for it in the AbstractTypeMap.
780 if (Ty->isAbstract()) {
781 typename AbstractTypeMapTy::iterator TI =
782 AbstractTypeMap.lower_bound(Ty);
783
784 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
785 // Add ourselves to the ATU list of the type.
786 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
787
788 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
789 }
790 }
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000791 return Result;
792 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000793
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000794 void remove(ConstantClass *CP) {
Jim Laskeyede5aa42006-07-17 17:38:29 +0000795 typename MapTy::iterator I = FindExistingElement(CP);
Chris Lattnered468e372003-10-05 00:17:43 +0000796 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner6823c9f2004-08-04 04:48:01 +0000797 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnered468e372003-10-05 00:17:43 +0000798
Chris Lattnera55b30a2005-10-04 17:48:46 +0000799 if (HasLargeKey) // Remember the reverse mapping if needed.
800 InverseMap.erase(CP);
801
Chris Lattnered468e372003-10-05 00:17:43 +0000802 // Now that we found the entry, make sure this isn't the entry that
803 // the AbstractTypeMap points to.
Jim Laskeyede5aa42006-07-17 17:38:29 +0000804 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
Chris Lattnered468e372003-10-05 00:17:43 +0000805 if (Ty->isAbstract()) {
806 assert(AbstractTypeMap.count(Ty) &&
807 "Abstract type not in AbstractTypeMap?");
Jim Laskeyede5aa42006-07-17 17:38:29 +0000808 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
Chris Lattnered468e372003-10-05 00:17:43 +0000809 if (ATMEntryIt == I) {
810 // Yes, we are removing the representative entry for this type.
811 // See if there are any other entries of the same type.
Jim Laskeyede5aa42006-07-17 17:38:29 +0000812 typename MapTy::iterator TmpIt = ATMEntryIt;
Misha Brukmanfd939082005-04-21 23:48:37 +0000813
Chris Lattnered468e372003-10-05 00:17:43 +0000814 // First check the entry before this one...
815 if (TmpIt != Map.begin()) {
816 --TmpIt;
817 if (TmpIt->first.first != Ty) // Not the same type, move back...
818 ++TmpIt;
819 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000820
Chris Lattnered468e372003-10-05 00:17:43 +0000821 // If we didn't find the same type, try to move forward...
822 if (TmpIt == ATMEntryIt) {
823 ++TmpIt;
824 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
825 --TmpIt; // No entry afterwards with the same type
826 }
827
828 // If there is another entry in the map of the same abstract type,
829 // update the AbstractTypeMap entry now.
830 if (TmpIt != ATMEntryIt) {
831 ATMEntryIt = TmpIt;
832 } else {
833 // Otherwise, we are removing the last instance of this type
834 // from the table. Remove from the ATM, and from user list.
835 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
836 AbstractTypeMap.erase(Ty);
837 }
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000838 }
Chris Lattnered468e372003-10-05 00:17:43 +0000839 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000840
Chris Lattnered468e372003-10-05 00:17:43 +0000841 Map.erase(I);
842 }
843
Chris Lattnera1e3f542005-10-04 21:35:50 +0000844
845 /// MoveConstantToNewSlot - If we are about to change C to be the element
846 /// specified by I, update our internal data structures to reflect this
847 /// fact.
Jim Laskeyede5aa42006-07-17 17:38:29 +0000848 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
Chris Lattnera1e3f542005-10-04 21:35:50 +0000849 // First, remove the old location of the specified constant in the map.
Jim Laskeyede5aa42006-07-17 17:38:29 +0000850 typename MapTy::iterator OldI = FindExistingElement(C);
Chris Lattnera1e3f542005-10-04 21:35:50 +0000851 assert(OldI != Map.end() && "Constant not found in constant table!");
852 assert(OldI->second == C && "Didn't find correct element?");
853
854 // If this constant is the representative element for its abstract type,
855 // update the AbstractTypeMap so that the representative element is I.
856 if (C->getType()->isAbstract()) {
857 typename AbstractTypeMapTy::iterator ATI =
858 AbstractTypeMap.find(C->getType());
859 assert(ATI != AbstractTypeMap.end() &&
860 "Abstract type not in AbstractTypeMap?");
861 if (ATI->second == OldI)
862 ATI->second = I;
863 }
864
865 // Remove the old entry from the map.
866 Map.erase(OldI);
867
868 // Update the inverse map so that we know that this constant is now
869 // located at descriptor I.
870 if (HasLargeKey) {
871 assert(I->second == C && "Bad inversemap entry!");
872 InverseMap[C] = I;
873 }
874 }
875
Chris Lattnered468e372003-10-05 00:17:43 +0000876 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Misha Brukmanfd939082005-04-21 23:48:37 +0000877 typename AbstractTypeMapTy::iterator I =
Jim Laskeyede5aa42006-07-17 17:38:29 +0000878 AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnered468e372003-10-05 00:17:43 +0000879
880 assert(I != AbstractTypeMap.end() &&
881 "Abstract type not in AbstractTypeMap?");
882
883 // Convert a constant at a time until the last one is gone. The last one
884 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
885 // eliminated eventually.
886 do {
887 ConvertConstantType<ConstantClass,
Jim Laskeyede5aa42006-07-17 17:38:29 +0000888 TypeClass>::convert(
889 static_cast<ConstantClass *>(I->second->second),
Chris Lattnered468e372003-10-05 00:17:43 +0000890 cast<TypeClass>(NewTy));
891
Jim Laskeyede5aa42006-07-17 17:38:29 +0000892 I = AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnered468e372003-10-05 00:17:43 +0000893 } while (I != AbstractTypeMap.end());
894 }
895
896 // If the type became concrete without being refined to any other existing
897 // type, we just remove ourselves from the ATU list.
898 void typeBecameConcrete(const DerivedType *AbsTy) {
899 AbsTy->removeAbstractTypeUser(this);
900 }
901
902 void dump() const {
Bill Wendling2e3def12006-11-17 08:03:48 +0000903 DOUT << "Constant.cpp: ValueMap\n";
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000904 }
905 };
906}
907
Chris Lattner003cbf32006-09-28 23:36:21 +0000908
909//---- ConstantBool::get*() implementation.
910
911ConstantBool *ConstantBool::getTrue() {
912 static ConstantBool *T = 0;
913 if (T) return T;
914 return T = new ConstantBool(true);
915}
916ConstantBool *ConstantBool::getFalse() {
917 static ConstantBool *F = 0;
918 if (F) return F;
919 return F = new ConstantBool(false);
920}
921
Reid Spencerb83eb642006-10-20 07:07:24 +0000922//---- ConstantInt::get() implementations...
Chris Lattner531daef2001-09-07 16:46:31 +0000923//
Reid Spencerb83eb642006-10-20 07:07:24 +0000924static ManagedStatic<ValueMap<uint64_t, Type, ConstantInt> > IntConstants;
Chris Lattner531daef2001-09-07 16:46:31 +0000925
Reid Spencerb83eb642006-10-20 07:07:24 +0000926// Get a ConstantInt from an int64_t. Note here that we canoncialize the value
927// to a uint64_t value that has been zero extended down to the size of the
928// integer type of the ConstantInt. This allows the getZExtValue method to
929// just return the stored value while getSExtValue has to convert back to sign
930// extended. getZExtValue is more common in LLVM than getSExtValue().
931ConstantInt *ConstantInt::get(const Type *Ty, int64_t V) {
Chris Lattner68329b22006-12-01 19:20:02 +0000932 return IntConstants->getOrCreate(Ty, V & Ty->getIntegralTypeMask());
933}
934
935ConstantIntegral *ConstantIntegral::get(const Type *Ty, int64_t V) {
936 if (Ty == Type::BoolTy) return ConstantBool::get(V&1);
937 return IntConstants->getOrCreate(Ty, V & Ty->getIntegralTypeMask());
Chris Lattner531daef2001-09-07 16:46:31 +0000938}
939
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000940//---- ConstantFP::get() implementation...
Chris Lattner531daef2001-09-07 16:46:31 +0000941//
Chris Lattnercfdf2412004-02-01 22:49:04 +0000942namespace llvm {
943 template<>
944 struct ConstantCreator<ConstantFP, Type, uint64_t> {
945 static ConstantFP *create(const Type *Ty, uint64_t V) {
946 assert(Ty == Type::DoubleTy);
Jim Laskeycb6682f2005-08-17 19:34:49 +0000947 return new ConstantFP(Ty, BitsToDouble(V));
Chris Lattnercfdf2412004-02-01 22:49:04 +0000948 }
949 };
950 template<>
951 struct ConstantCreator<ConstantFP, Type, uint32_t> {
952 static ConstantFP *create(const Type *Ty, uint32_t V) {
953 assert(Ty == Type::FloatTy);
Jim Laskeycb6682f2005-08-17 19:34:49 +0000954 return new ConstantFP(Ty, BitsToFloat(V));
Chris Lattnercfdf2412004-02-01 22:49:04 +0000955 }
956 };
957}
958
Chris Lattner8a94bf12006-09-28 00:35:06 +0000959static ManagedStatic<ValueMap<uint64_t, Type, ConstantFP> > DoubleConstants;
960static ManagedStatic<ValueMap<uint32_t, Type, ConstantFP> > FloatConstants;
Chris Lattner531daef2001-09-07 16:46:31 +0000961
Jim Laskey3a1eff72005-08-17 20:06:22 +0000962bool ConstantFP::isNullValue() const {
963 return DoubleToBits(Val) == 0;
964}
965
966bool ConstantFP::isExactlyValue(double V) const {
967 return DoubleToBits(V) == DoubleToBits(Val);
968}
969
970
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000971ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner90fb19e2004-01-23 00:55:21 +0000972 if (Ty == Type::FloatTy) {
973 // Force the value through memory to normalize it.
Chris Lattner8a94bf12006-09-28 00:35:06 +0000974 return FloatConstants->getOrCreate(Ty, FloatToBits(V));
Chris Lattnercfdf2412004-02-01 22:49:04 +0000975 } else {
976 assert(Ty == Type::DoubleTy);
Chris Lattner8a94bf12006-09-28 00:35:06 +0000977 return DoubleConstants->getOrCreate(Ty, DoubleToBits(V));
Chris Lattner90fb19e2004-01-23 00:55:21 +0000978 }
Chris Lattner531daef2001-09-07 16:46:31 +0000979}
980
Chris Lattner40bbeb52004-02-15 05:53:04 +0000981//---- ConstantAggregateZero::get() implementation...
982//
983namespace llvm {
984 // ConstantAggregateZero does not take extra "value" argument...
985 template<class ValType>
986 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
987 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
988 return new ConstantAggregateZero(Ty);
989 }
990 };
991
992 template<>
993 struct ConvertConstantType<ConstantAggregateZero, Type> {
994 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
995 // Make everyone now use a constant of the new type...
996 Constant *New = ConstantAggregateZero::get(NewTy);
997 assert(New != OldC && "Didn't replace constant??");
998 OldC->uncheckedReplaceAllUsesWith(New);
999 OldC->destroyConstant(); // This constant is now dead, destroy it.
1000 }
1001 };
1002}
1003
Chris Lattner8a94bf12006-09-28 00:35:06 +00001004static ManagedStatic<ValueMap<char, Type,
1005 ConstantAggregateZero> > AggZeroConstants;
Chris Lattner40bbeb52004-02-15 05:53:04 +00001006
Chris Lattner6823c9f2004-08-04 04:48:01 +00001007static char getValType(ConstantAggregateZero *CPZ) { return 0; }
1008
Chris Lattner40bbeb52004-02-15 05:53:04 +00001009Constant *ConstantAggregateZero::get(const Type *Ty) {
Chris Lattner31d1a2c2006-06-10 04:16:23 +00001010 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<PackedType>(Ty)) &&
1011 "Cannot create an aggregate zero of non-aggregate type!");
Chris Lattner8a94bf12006-09-28 00:35:06 +00001012 return AggZeroConstants->getOrCreate(Ty, 0);
Chris Lattner40bbeb52004-02-15 05:53:04 +00001013}
1014
1015// destroyConstant - Remove the constant from the constant table...
1016//
1017void ConstantAggregateZero::destroyConstant() {
Chris Lattner8a94bf12006-09-28 00:35:06 +00001018 AggZeroConstants->remove(this);
Chris Lattner40bbeb52004-02-15 05:53:04 +00001019 destroyConstantImpl();
1020}
1021
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001022//---- ConstantArray::get() implementation...
Chris Lattner531daef2001-09-07 16:46:31 +00001023//
Chris Lattner31f84992003-11-21 20:23:48 +00001024namespace llvm {
1025 template<>
1026 struct ConvertConstantType<ConstantArray, ArrayType> {
1027 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
1028 // Make everyone now use a constant of the new type...
1029 std::vector<Constant*> C;
1030 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1031 C.push_back(cast<Constant>(OldC->getOperand(i)));
1032 Constant *New = ConstantArray::get(NewTy, C);
1033 assert(New != OldC && "Didn't replace constant??");
1034 OldC->uncheckedReplaceAllUsesWith(New);
1035 OldC->destroyConstant(); // This constant is now dead, destroy it.
1036 }
1037 };
1038}
Chris Lattnered468e372003-10-05 00:17:43 +00001039
Chris Lattner6823c9f2004-08-04 04:48:01 +00001040static std::vector<Constant*> getValType(ConstantArray *CA) {
1041 std::vector<Constant*> Elements;
1042 Elements.reserve(CA->getNumOperands());
1043 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1044 Elements.push_back(cast<Constant>(CA->getOperand(i)));
1045 return Elements;
1046}
1047
Chris Lattnercea141f2005-10-03 22:51:37 +00001048typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattnera55b30a2005-10-04 17:48:46 +00001049 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattner8a94bf12006-09-28 00:35:06 +00001050static ManagedStatic<ArrayConstantsTy> ArrayConstants;
Chris Lattner531daef2001-09-07 16:46:31 +00001051
Chris Lattnerca705fa2004-02-15 04:14:47 +00001052Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner40bbeb52004-02-15 05:53:04 +00001053 const std::vector<Constant*> &V) {
1054 // If this is an all-zero array, return a ConstantAggregateZero object
1055 if (!V.empty()) {
1056 Constant *C = V[0];
1057 if (!C->isNullValue())
Chris Lattner8a94bf12006-09-28 00:35:06 +00001058 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner40bbeb52004-02-15 05:53:04 +00001059 for (unsigned i = 1, e = V.size(); i != e; ++i)
1060 if (V[i] != C)
Chris Lattner8a94bf12006-09-28 00:35:06 +00001061 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner40bbeb52004-02-15 05:53:04 +00001062 }
1063 return ConstantAggregateZero::get(Ty);
Chris Lattner531daef2001-09-07 16:46:31 +00001064}
1065
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001066// destroyConstant - Remove the constant from the constant table...
1067//
1068void ConstantArray::destroyConstant() {
Chris Lattner8a94bf12006-09-28 00:35:06 +00001069 ArrayConstants->remove(this);
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001070 destroyConstantImpl();
1071}
1072
Reid Spencer89494772006-05-30 08:23:18 +00001073/// ConstantArray::get(const string&) - Return an array that is initialized to
1074/// contain the specified string. If length is zero then a null terminator is
1075/// added to the specified string so that it may be used in a natural way.
1076/// Otherwise, the length parameter specifies how much of the string to use
1077/// and it won't be null terminated.
1078///
Reid Spencer461bed22006-05-30 18:15:07 +00001079Constant *ConstantArray::get(const std::string &Str, bool AddNull) {
Chris Lattner697954c2002-01-20 22:54:45 +00001080 std::vector<Constant*> ElementVals;
Reid Spencer461bed22006-05-30 18:15:07 +00001081 for (unsigned i = 0; i < Str.length(); ++i)
Reid Spencerb83eb642006-10-20 07:07:24 +00001082 ElementVals.push_back(ConstantInt::get(Type::SByteTy, Str[i]));
Chris Lattnerc5bdb242001-10-14 23:54:12 +00001083
1084 // Add a null terminator to the string...
Reid Spencer461bed22006-05-30 18:15:07 +00001085 if (AddNull) {
Reid Spencerb83eb642006-10-20 07:07:24 +00001086 ElementVals.push_back(ConstantInt::get(Type::SByteTy, 0));
Reid Spencer89494772006-05-30 08:23:18 +00001087 }
Chris Lattnerc5bdb242001-10-14 23:54:12 +00001088
Reid Spencer461bed22006-05-30 18:15:07 +00001089 ArrayType *ATy = ArrayType::get(Type::SByteTy, ElementVals.size());
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001090 return ConstantArray::get(ATy, ElementVals);
Vikram S. Advedb2da492001-10-14 23:17:20 +00001091}
1092
Chris Lattner13cfdea2004-01-14 17:06:38 +00001093/// isString - This method returns true if the array is an array of sbyte or
1094/// ubyte, and if the elements of the array are all ConstantInt's.
1095bool ConstantArray::isString() const {
1096 // Check the element type for sbyte or ubyte...
Chris Lattner7aa162b2004-01-14 17:51:53 +00001097 if (getType()->getElementType() != Type::UByteTy &&
Chris Lattner13cfdea2004-01-14 17:06:38 +00001098 getType()->getElementType() != Type::SByteTy)
1099 return false;
1100 // Check the elements to make sure they are all integers, not constant
1101 // expressions.
1102 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1103 if (!isa<ConstantInt>(getOperand(i)))
1104 return false;
1105 return true;
1106}
1107
Evan Cheng22c70302006-10-26 19:15:05 +00001108/// isCString - This method returns true if the array is a string (see
1109/// isString) and it ends in a null byte \0 and does not contains any other
1110/// null bytes except its terminator.
1111bool ConstantArray::isCString() const {
Evan Chengabf63452006-10-26 21:48:03 +00001112 // Check the element type for sbyte or ubyte...
1113 if (getType()->getElementType() != Type::UByteTy &&
1114 getType()->getElementType() != Type::SByteTy)
1115 return false;
1116 Constant *Zero = Constant::getNullValue(getOperand(0)->getType());
1117 // Last element must be a null.
1118 if (getOperand(getNumOperands()-1) != Zero)
1119 return false;
1120 // Other elements must be non-null integers.
1121 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1122 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng22c70302006-10-26 19:15:05 +00001123 return false;
Evan Chengabf63452006-10-26 21:48:03 +00001124 if (getOperand(i) == Zero)
1125 return false;
1126 }
Evan Cheng22c70302006-10-26 19:15:05 +00001127 return true;
1128}
1129
1130
Chris Lattner93aeea32002-08-26 17:53:56 +00001131// getAsString - If the sub-element type of this array is either sbyte or ubyte,
1132// then this method converts the array to an std::string and returns it.
1133// Otherwise, it asserts out.
1134//
1135std::string ConstantArray::getAsString() const {
Chris Lattner13cfdea2004-01-14 17:06:38 +00001136 assert(isString() && "Not a string!");
Chris Lattner93aeea32002-08-26 17:53:56 +00001137 std::string Result;
Chris Lattnerc07736a2003-07-23 15:22:26 +00001138 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Reid Spencerb83eb642006-10-20 07:07:24 +00001139 Result += (char)cast<ConstantInt>(getOperand(i))->getZExtValue();
Chris Lattner93aeea32002-08-26 17:53:56 +00001140 return Result;
1141}
1142
1143
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001144//---- ConstantStruct::get() implementation...
Chris Lattner531daef2001-09-07 16:46:31 +00001145//
Chris Lattnered468e372003-10-05 00:17:43 +00001146
Chris Lattner31f84992003-11-21 20:23:48 +00001147namespace llvm {
1148 template<>
1149 struct ConvertConstantType<ConstantStruct, StructType> {
1150 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1151 // Make everyone now use a constant of the new type...
1152 std::vector<Constant*> C;
1153 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1154 C.push_back(cast<Constant>(OldC->getOperand(i)));
1155 Constant *New = ConstantStruct::get(NewTy, C);
1156 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanfd939082005-04-21 23:48:37 +00001157
Chris Lattner31f84992003-11-21 20:23:48 +00001158 OldC->uncheckedReplaceAllUsesWith(New);
1159 OldC->destroyConstant(); // This constant is now dead, destroy it.
1160 }
1161 };
1162}
Chris Lattnered468e372003-10-05 00:17:43 +00001163
Chris Lattnerc182a882005-10-04 01:17:50 +00001164typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattnera55b30a2005-10-04 17:48:46 +00001165 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner8a94bf12006-09-28 00:35:06 +00001166static ManagedStatic<StructConstantsTy> StructConstants;
Chris Lattner531daef2001-09-07 16:46:31 +00001167
Chris Lattner6823c9f2004-08-04 04:48:01 +00001168static std::vector<Constant*> getValType(ConstantStruct *CS) {
1169 std::vector<Constant*> Elements;
1170 Elements.reserve(CS->getNumOperands());
1171 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1172 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1173 return Elements;
1174}
1175
Chris Lattnerca705fa2004-02-15 04:14:47 +00001176Constant *ConstantStruct::get(const StructType *Ty,
1177 const std::vector<Constant*> &V) {
Chris Lattner40bbeb52004-02-15 05:53:04 +00001178 // Create a ConstantAggregateZero value if all elements are zeros...
1179 for (unsigned i = 0, e = V.size(); i != e; ++i)
1180 if (!V[i]->isNullValue())
Chris Lattner8a94bf12006-09-28 00:35:06 +00001181 return StructConstants->getOrCreate(Ty, V);
Chris Lattner40bbeb52004-02-15 05:53:04 +00001182
1183 return ConstantAggregateZero::get(Ty);
Chris Lattner531daef2001-09-07 16:46:31 +00001184}
Chris Lattner6a57baa2001-10-03 15:39:36 +00001185
Chris Lattnerb370c7a2004-07-12 20:35:11 +00001186Constant *ConstantStruct::get(const std::vector<Constant*> &V) {
1187 std::vector<const Type*> StructEls;
1188 StructEls.reserve(V.size());
1189 for (unsigned i = 0, e = V.size(); i != e; ++i)
1190 StructEls.push_back(V[i]->getType());
1191 return get(StructType::get(StructEls), V);
1192}
1193
Chris Lattnerf5ec48d2001-10-13 06:57:33 +00001194// destroyConstant - Remove the constant from the constant table...
Chris Lattner6a57baa2001-10-03 15:39:36 +00001195//
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001196void ConstantStruct::destroyConstant() {
Chris Lattner8a94bf12006-09-28 00:35:06 +00001197 StructConstants->remove(this);
Chris Lattnerf5ec48d2001-10-13 06:57:33 +00001198 destroyConstantImpl();
1199}
Chris Lattner6a57baa2001-10-03 15:39:36 +00001200
Brian Gaeke715c90b2004-08-20 06:00:58 +00001201//---- ConstantPacked::get() implementation...
1202//
1203namespace llvm {
1204 template<>
1205 struct ConvertConstantType<ConstantPacked, PackedType> {
1206 static void convert(ConstantPacked *OldC, const PackedType *NewTy) {
1207 // Make everyone now use a constant of the new type...
1208 std::vector<Constant*> C;
1209 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1210 C.push_back(cast<Constant>(OldC->getOperand(i)));
1211 Constant *New = ConstantPacked::get(NewTy, C);
1212 assert(New != OldC && "Didn't replace constant??");
1213 OldC->uncheckedReplaceAllUsesWith(New);
1214 OldC->destroyConstant(); // This constant is now dead, destroy it.
1215 }
1216 };
1217}
1218
1219static std::vector<Constant*> getValType(ConstantPacked *CP) {
1220 std::vector<Constant*> Elements;
1221 Elements.reserve(CP->getNumOperands());
1222 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1223 Elements.push_back(CP->getOperand(i));
1224 return Elements;
1225}
1226
Chris Lattner8a94bf12006-09-28 00:35:06 +00001227static ManagedStatic<ValueMap<std::vector<Constant*>, PackedType,
1228 ConstantPacked> > PackedConstants;
Brian Gaeke715c90b2004-08-20 06:00:58 +00001229
1230Constant *ConstantPacked::get(const PackedType *Ty,
1231 const std::vector<Constant*> &V) {
1232 // If this is an all-zero packed, return a ConstantAggregateZero object
1233 if (!V.empty()) {
1234 Constant *C = V[0];
1235 if (!C->isNullValue())
Chris Lattner8a94bf12006-09-28 00:35:06 +00001236 return PackedConstants->getOrCreate(Ty, V);
Brian Gaeke715c90b2004-08-20 06:00:58 +00001237 for (unsigned i = 1, e = V.size(); i != e; ++i)
1238 if (V[i] != C)
Chris Lattner8a94bf12006-09-28 00:35:06 +00001239 return PackedConstants->getOrCreate(Ty, V);
Brian Gaeke715c90b2004-08-20 06:00:58 +00001240 }
1241 return ConstantAggregateZero::get(Ty);
1242}
1243
1244Constant *ConstantPacked::get(const std::vector<Constant*> &V) {
1245 assert(!V.empty() && "Cannot infer type if V is empty");
1246 return get(PackedType::get(V.front()->getType(),V.size()), V);
1247}
1248
1249// destroyConstant - Remove the constant from the constant table...
1250//
1251void ConstantPacked::destroyConstant() {
Chris Lattner8a94bf12006-09-28 00:35:06 +00001252 PackedConstants->remove(this);
Brian Gaeke715c90b2004-08-20 06:00:58 +00001253 destroyConstantImpl();
1254}
1255
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001256//---- ConstantPointerNull::get() implementation...
Chris Lattnerf5ec48d2001-10-13 06:57:33 +00001257//
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001258
Chris Lattner31f84992003-11-21 20:23:48 +00001259namespace llvm {
1260 // ConstantPointerNull does not take extra "value" argument...
1261 template<class ValType>
1262 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1263 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1264 return new ConstantPointerNull(Ty);
1265 }
1266 };
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001267
Chris Lattner31f84992003-11-21 20:23:48 +00001268 template<>
1269 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1270 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1271 // Make everyone now use a constant of the new type...
1272 Constant *New = ConstantPointerNull::get(NewTy);
1273 assert(New != OldC && "Didn't replace constant??");
1274 OldC->uncheckedReplaceAllUsesWith(New);
1275 OldC->destroyConstant(); // This constant is now dead, destroy it.
1276 }
1277 };
1278}
Chris Lattnered468e372003-10-05 00:17:43 +00001279
Chris Lattner8a94bf12006-09-28 00:35:06 +00001280static ManagedStatic<ValueMap<char, PointerType,
1281 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerf5ec48d2001-10-13 06:57:33 +00001282
Chris Lattner6823c9f2004-08-04 04:48:01 +00001283static char getValType(ConstantPointerNull *) {
1284 return 0;
1285}
1286
1287
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001288ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner8a94bf12006-09-28 00:35:06 +00001289 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner6a57baa2001-10-03 15:39:36 +00001290}
1291
Chris Lattner41661fd2002-08-18 00:40:04 +00001292// destroyConstant - Remove the constant from the constant table...
1293//
1294void ConstantPointerNull::destroyConstant() {
Chris Lattner8a94bf12006-09-28 00:35:06 +00001295 NullPtrConstants->remove(this);
Chris Lattner41661fd2002-08-18 00:40:04 +00001296 destroyConstantImpl();
1297}
1298
1299
Chris Lattnerb9f18592004-10-16 18:07:16 +00001300//---- UndefValue::get() implementation...
1301//
1302
1303namespace llvm {
1304 // UndefValue does not take extra "value" argument...
1305 template<class ValType>
1306 struct ConstantCreator<UndefValue, Type, ValType> {
1307 static UndefValue *create(const Type *Ty, const ValType &V) {
1308 return new UndefValue(Ty);
1309 }
1310 };
1311
1312 template<>
1313 struct ConvertConstantType<UndefValue, Type> {
1314 static void convert(UndefValue *OldC, const Type *NewTy) {
1315 // Make everyone now use a constant of the new type.
1316 Constant *New = UndefValue::get(NewTy);
1317 assert(New != OldC && "Didn't replace constant??");
1318 OldC->uncheckedReplaceAllUsesWith(New);
1319 OldC->destroyConstant(); // This constant is now dead, destroy it.
1320 }
1321 };
1322}
1323
Chris Lattner8a94bf12006-09-28 00:35:06 +00001324static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerb9f18592004-10-16 18:07:16 +00001325
1326static char getValType(UndefValue *) {
1327 return 0;
1328}
1329
1330
1331UndefValue *UndefValue::get(const Type *Ty) {
Chris Lattner8a94bf12006-09-28 00:35:06 +00001332 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerb9f18592004-10-16 18:07:16 +00001333}
1334
1335// destroyConstant - Remove the constant from the constant table.
1336//
1337void UndefValue::destroyConstant() {
Chris Lattner8a94bf12006-09-28 00:35:06 +00001338 UndefValueConstants->remove(this);
Chris Lattnerb9f18592004-10-16 18:07:16 +00001339 destroyConstantImpl();
1340}
1341
1342
1343
1344
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001345//---- ConstantExpr::get() implementations...
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001346//
Chris Lattner9bc02a42003-05-13 21:37:02 +00001347typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001348
Chris Lattner31f84992003-11-21 20:23:48 +00001349namespace llvm {
1350 template<>
1351 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
1352 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
Reid Spencer3da59db2006-11-27 01:05:10 +00001353 if (Instruction::isCast(V.first))
1354 return new UnaryConstantExpr(V.first, V.second[0], Ty);
Chris Lattner31f84992003-11-21 20:23:48 +00001355 if ((V.first >= Instruction::BinaryOpsBegin &&
1356 V.first < Instruction::BinaryOpsEnd) ||
Reid Spencer3822ff52006-11-08 06:47:33 +00001357 V.first == Instruction::Shl ||
1358 V.first == Instruction::LShr ||
1359 V.first == Instruction::AShr)
Chris Lattnere4671472005-01-29 00:34:39 +00001360 return new BinaryConstantExpr(V.first, V.second[0], V.second[1]);
Chris Lattner92e84222004-03-30 22:51:03 +00001361 if (V.first == Instruction::Select)
Chris Lattnere4671472005-01-29 00:34:39 +00001362 return new SelectConstantExpr(V.second[0], V.second[1], V.second[2]);
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001363 if (V.first == Instruction::ExtractElement)
1364 return new ExtractElementConstantExpr(V.second[0], V.second[1]);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001365 if (V.first == Instruction::InsertElement)
1366 return new InsertElementConstantExpr(V.second[0], V.second[1],
1367 V.second[2]);
Chris Lattner00f10232006-04-08 01:18:18 +00001368 if (V.first == Instruction::ShuffleVector)
1369 return new ShuffleVectorConstantExpr(V.second[0], V.second[1],
1370 V.second[2]);
1371
Chris Lattner31f84992003-11-21 20:23:48 +00001372 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
Misha Brukmanfd939082005-04-21 23:48:37 +00001373
Chris Lattner31f84992003-11-21 20:23:48 +00001374 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
Chris Lattnere4671472005-01-29 00:34:39 +00001375 return new GetElementPtrConstantExpr(V.second[0], IdxList, Ty);
Chris Lattnered468e372003-10-05 00:17:43 +00001376 }
Chris Lattner31f84992003-11-21 20:23:48 +00001377 };
Chris Lattnered468e372003-10-05 00:17:43 +00001378
Chris Lattner31f84992003-11-21 20:23:48 +00001379 template<>
1380 struct ConvertConstantType<ConstantExpr, Type> {
1381 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1382 Constant *New;
1383 switch (OldC->getOpcode()) {
Reid Spencer3da59db2006-11-27 01:05:10 +00001384 case Instruction::Trunc:
1385 case Instruction::ZExt:
1386 case Instruction::SExt:
1387 case Instruction::FPTrunc:
1388 case Instruction::FPExt:
1389 case Instruction::UIToFP:
1390 case Instruction::SIToFP:
1391 case Instruction::FPToUI:
1392 case Instruction::FPToSI:
1393 case Instruction::PtrToInt:
1394 case Instruction::IntToPtr:
1395 case Instruction::BitCast:
1396 New = ConstantExpr::getCast(
1397 OldC->getOpcode(), OldC->getOperand(0), NewTy);
Chris Lattner31f84992003-11-21 20:23:48 +00001398 break;
Chris Lattner08a45cc2004-03-12 05:54:04 +00001399 case Instruction::Select:
1400 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1401 OldC->getOperand(1),
1402 OldC->getOperand(2));
1403 break;
Chris Lattner31f84992003-11-21 20:23:48 +00001404 case Instruction::Shl:
Reid Spencer3822ff52006-11-08 06:47:33 +00001405 case Instruction::LShr:
1406 case Instruction::AShr:
Chris Lattner31f84992003-11-21 20:23:48 +00001407 New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
1408 OldC->getOperand(0), OldC->getOperand(1));
1409 break;
1410 default:
1411 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer0a783f72006-11-02 01:53:59 +00001412 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner31f84992003-11-21 20:23:48 +00001413 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1414 OldC->getOperand(1));
1415 break;
1416 case Instruction::GetElementPtr:
Misha Brukmanfd939082005-04-21 23:48:37 +00001417 // Make everyone now use a constant of the new type...
Chris Lattner7fa6e662004-10-11 22:52:25 +00001418 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
1419 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), Idx);
Chris Lattner31f84992003-11-21 20:23:48 +00001420 break;
1421 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001422
Chris Lattner31f84992003-11-21 20:23:48 +00001423 assert(New != OldC && "Didn't replace constant??");
1424 OldC->uncheckedReplaceAllUsesWith(New);
1425 OldC->destroyConstant(); // This constant is now dead, destroy it.
1426 }
1427 };
1428} // end namespace llvm
Chris Lattnered468e372003-10-05 00:17:43 +00001429
1430
Chris Lattner6823c9f2004-08-04 04:48:01 +00001431static ExprMapKeyType getValType(ConstantExpr *CE) {
1432 std::vector<Constant*> Operands;
1433 Operands.reserve(CE->getNumOperands());
1434 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1435 Operands.push_back(cast<Constant>(CE->getOperand(i)));
1436 return ExprMapKeyType(CE->getOpcode(), Operands);
1437}
1438
Chris Lattner8a94bf12006-09-28 00:35:06 +00001439static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1440 ConstantExpr> > ExprConstants;
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001441
Reid Spencer3da59db2006-11-27 01:05:10 +00001442/// This is a utility function to handle folding of casts and lookup of the
1443/// cast in the ExprConstants map. It is usedby the various get* methods below.
1444static inline Constant *getFoldedCast(
1445 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner9eacf8a2003-10-07 22:19:19 +00001446 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer3da59db2006-11-27 01:05:10 +00001447 // Fold a few common cases
1448 if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
1449 return FC;
Chris Lattnerd628f6a2003-04-17 19:24:48 +00001450
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001451 // Look up the constant in the table first to ensure uniqueness
Chris Lattner9bc02a42003-05-13 21:37:02 +00001452 std::vector<Constant*> argVec(1, C);
Reid Spencer3da59db2006-11-27 01:05:10 +00001453 ExprMapKeyType Key = std::make_pair(opc, argVec);
Chris Lattner8a94bf12006-09-28 00:35:06 +00001454 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001455}
Chris Lattnerf5ec48d2001-10-13 06:57:33 +00001456
Reid Spencer3da59db2006-11-27 01:05:10 +00001457Constant *ConstantExpr::getCast( Constant *C, const Type *Ty ) {
1458 // Note: we can't inline this because it requires the Instructions.h header
1459 return getCast(CastInst::getCastOpcode(C, Ty), C, Ty);
1460}
1461
1462Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
1463 Instruction::CastOps opc = Instruction::CastOps(oc);
1464 assert(Instruction::isCast(opc) && "opcode out of range");
1465 assert(C && Ty && "Null arguments to getCast");
1466 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1467
1468 switch (opc) {
1469 default:
1470 assert(0 && "Invalid cast opcode");
1471 break;
1472 case Instruction::Trunc: return getTrunc(C, Ty);
1473 case Instruction::ZExt: return getZeroExtend(C, Ty);
1474 case Instruction::SExt: return getSignExtend(C, Ty);
1475 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1476 case Instruction::FPExt: return getFPExtend(C, Ty);
1477 case Instruction::UIToFP: return getUIToFP(C, Ty);
1478 case Instruction::SIToFP: return getSIToFP(C, Ty);
1479 case Instruction::FPToUI: return getFPToUI(C, Ty);
1480 case Instruction::FPToSI: return getFPToSI(C, Ty);
1481 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1482 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1483 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattnerf5ac6c22005-01-01 15:59:57 +00001484 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001485 return 0;
1486}
1487
1488Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
1489 assert(C->getType()->isInteger() && "Trunc operand must be integer");
1490 assert(Ty->isIntegral() && "Trunc produces only integral");
1491 assert(C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&&
1492 "SrcTy must be larger than DestTy for Trunc!");
1493
1494 return getFoldedCast(Instruction::Trunc, C, Ty);
1495}
1496
1497Constant *ConstantExpr::getSignExtend(Constant *C, const Type *Ty) {
1498 assert(C->getType()->isIntegral() && "SEXt operand must be integral");
1499 assert(Ty->isInteger() && "SExt produces only integer");
1500 assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1501 "SrcTy must be smaller than DestTy for SExt!");
1502
1503 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerd144f422004-04-04 23:20:30 +00001504}
1505
1506Constant *ConstantExpr::getZeroExtend(Constant *C, const Type *Ty) {
Reid Spencer3da59db2006-11-27 01:05:10 +00001507 assert(C->getType()->isIntegral() && "ZEXt operand must be integral");
1508 assert(Ty->isInteger() && "ZExt produces only integer");
1509 assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1510 "SrcTy must be smaller than DestTy for ZExt!");
1511
1512 return getFoldedCast(Instruction::ZExt, C, Ty);
1513}
1514
1515Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
1516 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1517 C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&&
1518 "This is an illegal floating point truncation!");
1519 return getFoldedCast(Instruction::FPTrunc, C, Ty);
1520}
1521
1522Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
1523 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1524 C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1525 "This is an illegal floating point extension!");
1526 return getFoldedCast(Instruction::FPExt, C, Ty);
1527}
1528
1529Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
1530 assert(C->getType()->isIntegral() && Ty->isFloatingPoint() &&
1531 "This is an illegal uint to floating point cast!");
1532 return getFoldedCast(Instruction::UIToFP, C, Ty);
1533}
1534
1535Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
1536 assert(C->getType()->isIntegral() && Ty->isFloatingPoint() &&
1537 "This is an illegal sint to floating point cast!");
1538 return getFoldedCast(Instruction::SIToFP, C, Ty);
1539}
1540
1541Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
1542 assert(C->getType()->isFloatingPoint() && Ty->isIntegral() &&
1543 "This is an illegal floating point to uint cast!");
1544 return getFoldedCast(Instruction::FPToUI, C, Ty);
1545}
1546
1547Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
1548 assert(C->getType()->isFloatingPoint() && Ty->isIntegral() &&
1549 "This is an illegal floating point to sint cast!");
1550 return getFoldedCast(Instruction::FPToSI, C, Ty);
1551}
1552
1553Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
1554 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
1555 assert(DstTy->isIntegral() && "PtrToInt destination must be integral");
1556 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
1557}
1558
1559Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
1560 assert(C->getType()->isIntegral() && "IntToPtr source must be integral");
1561 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
1562 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
1563}
1564
1565Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
1566 // BitCast implies a no-op cast of type only. No bits change. However, you
1567 // can't cast pointers to anything but pointers.
1568 const Type *SrcTy = C->getType();
1569 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
1570 "Bitcast cannot cast pointer to non-pointer and vice versa");
1571
1572 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
1573 // or nonptr->ptr). For all the other types, the cast is okay if source and
1574 // destination bit widths are identical.
1575 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1576 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
1577 assert(SrcBitSize == DstBitSize && "Bitcast requies types of same width");
1578 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerd144f422004-04-04 23:20:30 +00001579}
1580
Alkis Evlogimenos60ab1402004-10-24 01:41:10 +00001581Constant *ConstantExpr::getSizeOf(const Type *Ty) {
Chris Lattnerfcdd82e2004-12-13 19:48:51 +00001582 // sizeof is implemented as: (ulong) gep (Ty*)null, 1
Alkis Evlogimenos60ab1402004-10-24 01:41:10 +00001583 return getCast(
Chris Lattnerfcdd82e2004-12-13 19:48:51 +00001584 getGetElementPtr(getNullValue(PointerType::get(Ty)),
1585 std::vector<Constant*>(1, ConstantInt::get(Type::UIntTy, 1))),
1586 Type::ULongTy);
Alkis Evlogimenos60ab1402004-10-24 01:41:10 +00001587}
1588
Alkis Evlogimenos1cecd3a2005-03-19 11:40:31 +00001589Constant *ConstantExpr::getPtrPtrFromArrayPtr(Constant *C) {
1590 // pointer from array is implemented as: getelementptr arr ptr, 0, 0
Reid Spencerb83eb642006-10-20 07:07:24 +00001591 static std::vector<Constant*> Indices(2, ConstantInt::get(Type::UIntTy, 0));
Alkis Evlogimenos1cecd3a2005-03-19 11:40:31 +00001592
1593 return ConstantExpr::getGetElementPtr(C, Indices);
1594}
1595
Chris Lattnered468e372003-10-05 00:17:43 +00001596Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
1597 Constant *C1, Constant *C2) {
Reid Spencer3822ff52006-11-08 06:47:33 +00001598 if (Opcode == Instruction::Shl || Opcode == Instruction::LShr ||
1599 Opcode == Instruction::AShr)
Chris Lattner3edd3972004-01-12 19:04:55 +00001600 return getShiftTy(ReqTy, Opcode, C1, C2);
Chris Lattnerf31f5832003-05-21 17:49:25 +00001601 // Check the operands for consistency first
Reid Spencer0a783f72006-11-02 01:53:59 +00001602 assert(Opcode >= Instruction::BinaryOpsBegin &&
1603 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattnerf31f5832003-05-21 17:49:25 +00001604 "Invalid opcode in binary constant expression");
1605 assert(C1->getType() == C2->getType() &&
1606 "Operand types in binary constant expression should match");
Chris Lattnered468e372003-10-05 00:17:43 +00001607
Chris Lattnera5b07402006-09-17 19:14:47 +00001608 if (ReqTy == C1->getType() || (Instruction::isComparison(Opcode) &&
Chris Lattnerc3d12f02004-08-04 18:50:09 +00001609 ReqTy == Type::BoolTy))
Chris Lattnered468e372003-10-05 00:17:43 +00001610 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1611 return FC; // Fold a few common cases...
Chris Lattnerd628f6a2003-04-17 19:24:48 +00001612
Chris Lattner9bc02a42003-05-13 21:37:02 +00001613 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001614 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattner8a94bf12006-09-28 00:35:06 +00001615 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001616}
1617
Chris Lattnerc3d12f02004-08-04 18:50:09 +00001618Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattner91b362b2004-08-17 17:28:46 +00001619#ifndef NDEBUG
1620 switch (Opcode) {
Reid Spencer0a783f72006-11-02 01:53:59 +00001621 case Instruction::Add:
1622 case Instruction::Sub:
Reid Spencer1628cec2006-10-26 06:15:43 +00001623 case Instruction::Mul:
Chris Lattner91b362b2004-08-17 17:28:46 +00001624 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattner20542952006-01-04 01:01:04 +00001625 assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint() ||
1626 isa<PackedType>(C1->getType())) &&
Chris Lattner91b362b2004-08-17 17:28:46 +00001627 "Tried to create an arithmetic operation on a non-arithmetic type!");
1628 break;
Reid Spencer1628cec2006-10-26 06:15:43 +00001629 case Instruction::UDiv:
1630 case Instruction::SDiv:
1631 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1632 assert((C1->getType()->isInteger() || (isa<PackedType>(C1->getType()) &&
1633 cast<PackedType>(C1->getType())->getElementType()->isInteger())) &&
1634 "Tried to create an arithmetic operation on a non-arithmetic type!");
1635 break;
1636 case Instruction::FDiv:
1637 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1638 assert((C1->getType()->isFloatingPoint() || (isa<PackedType>(C1->getType())
1639 && cast<PackedType>(C1->getType())->getElementType()->isFloatingPoint()))
1640 && "Tried to create an arithmetic operation on a non-arithmetic type!");
1641 break;
Reid Spencer0a783f72006-11-02 01:53:59 +00001642 case Instruction::URem:
1643 case Instruction::SRem:
1644 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1645 assert((C1->getType()->isInteger() || (isa<PackedType>(C1->getType()) &&
1646 cast<PackedType>(C1->getType())->getElementType()->isInteger())) &&
1647 "Tried to create an arithmetic operation on a non-arithmetic type!");
1648 break;
1649 case Instruction::FRem:
1650 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1651 assert((C1->getType()->isFloatingPoint() || (isa<PackedType>(C1->getType())
1652 && cast<PackedType>(C1->getType())->getElementType()->isFloatingPoint()))
1653 && "Tried to create an arithmetic operation on a non-arithmetic type!");
1654 break;
Chris Lattner91b362b2004-08-17 17:28:46 +00001655 case Instruction::And:
1656 case Instruction::Or:
1657 case Instruction::Xor:
1658 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattner20542952006-01-04 01:01:04 +00001659 assert((C1->getType()->isIntegral() || isa<PackedType>(C1->getType())) &&
Misha Brukman1bae2912005-01-27 06:46:38 +00001660 "Tried to create a logical operation on a non-integral type!");
Chris Lattner91b362b2004-08-17 17:28:46 +00001661 break;
1662 case Instruction::SetLT: case Instruction::SetGT: case Instruction::SetLE:
1663 case Instruction::SetGE: case Instruction::SetEQ: case Instruction::SetNE:
1664 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1665 break;
1666 case Instruction::Shl:
Reid Spencer3822ff52006-11-08 06:47:33 +00001667 case Instruction::LShr:
1668 case Instruction::AShr:
Chris Lattner91b362b2004-08-17 17:28:46 +00001669 assert(C2->getType() == Type::UByteTy && "Shift should be by ubyte!");
Reid Spencer3822ff52006-11-08 06:47:33 +00001670 assert(C1->getType()->isInteger() &&
Chris Lattner91b362b2004-08-17 17:28:46 +00001671 "Tried to create a shift operation on a non-integer type!");
1672 break;
1673 default:
1674 break;
1675 }
1676#endif
1677
Chris Lattnera5b07402006-09-17 19:14:47 +00001678 if (Instruction::isComparison(Opcode))
Chris Lattnerc3d12f02004-08-04 18:50:09 +00001679 return getTy(Type::BoolTy, Opcode, C1, C2);
1680 else
1681 return getTy(C1->getType(), Opcode, C1, C2);
1682}
1683
Chris Lattner08a45cc2004-03-12 05:54:04 +00001684Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
1685 Constant *V1, Constant *V2) {
1686 assert(C->getType() == Type::BoolTy && "Select condition must be bool!");
1687 assert(V1->getType() == V2->getType() && "Select value types must match!");
1688 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
1689
1690 if (ReqTy == V1->getType())
1691 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1692 return SC; // Fold common cases
1693
1694 std::vector<Constant*> argVec(3, C);
1695 argVec[1] = V1;
1696 argVec[2] = V2;
1697 ExprMapKeyType Key = std::make_pair(Instruction::Select, argVec);
Chris Lattner8a94bf12006-09-28 00:35:06 +00001698 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner08a45cc2004-03-12 05:54:04 +00001699}
1700
Chris Lattner69b9e892004-01-12 19:12:58 +00001701/// getShiftTy - Return a shift left or shift right constant expr
Chris Lattnered468e372003-10-05 00:17:43 +00001702Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
1703 Constant *C1, Constant *C2) {
Chris Lattnerf31f5832003-05-21 17:49:25 +00001704 // Check the operands for consistency first
Reid Spencer3822ff52006-11-08 06:47:33 +00001705 assert((Opcode == Instruction::Shl ||
1706 Opcode == Instruction::LShr ||
1707 Opcode == Instruction::AShr) &&
Chris Lattnerf31f5832003-05-21 17:49:25 +00001708 "Invalid opcode in binary constant expression");
1709 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
1710 "Invalid operand types for Shift constant expr!");
1711
Chris Lattnerd4403b42004-01-12 20:40:42 +00001712 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattnerf31f5832003-05-21 17:49:25 +00001713 return FC; // Fold a few common cases...
1714
1715 // Look up the constant in the table first to ensure uniqueness
1716 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001717 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattner8a94bf12006-09-28 00:35:06 +00001718 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerf31f5832003-05-21 17:49:25 +00001719}
1720
1721
Chris Lattnered468e372003-10-05 00:17:43 +00001722Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner7fa6e662004-10-11 22:52:25 +00001723 const std::vector<Value*> &IdxList) {
1724 assert(GetElementPtrInst::getIndexedType(C->getType(), IdxList, true) &&
Chris Lattner2e9bb1a2004-02-16 20:46:13 +00001725 "GEP indices invalid!");
1726
Chris Lattnerd628f6a2003-04-17 19:24:48 +00001727 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
1728 return FC; // Fold a few common cases...
Chris Lattner2e9bb1a2004-02-16 20:46:13 +00001729
Chris Lattnered468e372003-10-05 00:17:43 +00001730 assert(isa<PointerType>(C->getType()) &&
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001731 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001732 // Look up the constant in the table first to ensure uniqueness
Chris Lattner7fa6e662004-10-11 22:52:25 +00001733 std::vector<Constant*> ArgVec;
1734 ArgVec.reserve(IdxList.size()+1);
1735 ArgVec.push_back(C);
1736 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
1737 ArgVec.push_back(cast<Constant>(IdxList[i]));
1738 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,ArgVec);
Chris Lattner8a94bf12006-09-28 00:35:06 +00001739 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001740}
1741
Chris Lattnered468e372003-10-05 00:17:43 +00001742Constant *ConstantExpr::getGetElementPtr(Constant *C,
1743 const std::vector<Constant*> &IdxList){
1744 // Get the result type of the getelementptr!
1745 std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
1746
1747 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
1748 true);
1749 assert(Ty && "GEP indices invalid!");
Chris Lattner7fa6e662004-10-11 22:52:25 +00001750 return getGetElementPtrTy(PointerType::get(Ty), C, VIdxList);
1751}
1752
1753Constant *ConstantExpr::getGetElementPtr(Constant *C,
1754 const std::vector<Value*> &IdxList) {
1755 // Get the result type of the getelementptr!
1756 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
1757 true);
1758 assert(Ty && "GEP indices invalid!");
Chris Lattnered468e372003-10-05 00:17:43 +00001759 return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
1760}
1761
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001762Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
1763 Constant *Idx) {
Robert Bocchinobb90a7a2006-01-10 20:03:46 +00001764 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
1765 return FC; // Fold a few common cases...
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001766 // Look up the constant in the table first to ensure uniqueness
1767 std::vector<Constant*> ArgVec(1, Val);
1768 ArgVec.push_back(Idx);
1769 const ExprMapKeyType &Key = std::make_pair(Instruction::ExtractElement,ArgVec);
Chris Lattner8a94bf12006-09-28 00:35:06 +00001770 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001771}
1772
1773Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
1774 assert(isa<PackedType>(Val->getType()) &&
1775 "Tried to create extractelement operation on non-packed type!");
1776 assert(Idx->getType() == Type::UIntTy &&
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001777 "Extractelement index must be uint type!");
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001778 return getExtractElementTy(cast<PackedType>(Val->getType())->getElementType(),
1779 Val, Idx);
1780}
Chris Lattnered468e372003-10-05 00:17:43 +00001781
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001782Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
1783 Constant *Elt, Constant *Idx) {
1784 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
1785 return FC; // Fold a few common cases...
1786 // Look up the constant in the table first to ensure uniqueness
1787 std::vector<Constant*> ArgVec(1, Val);
1788 ArgVec.push_back(Elt);
1789 ArgVec.push_back(Idx);
1790 const ExprMapKeyType &Key = std::make_pair(Instruction::InsertElement,ArgVec);
Chris Lattner8a94bf12006-09-28 00:35:06 +00001791 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001792}
1793
1794Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1795 Constant *Idx) {
1796 assert(isa<PackedType>(Val->getType()) &&
1797 "Tried to create insertelement operation on non-packed type!");
1798 assert(Elt->getType() == cast<PackedType>(Val->getType())->getElementType()
1799 && "Insertelement types must match!");
1800 assert(Idx->getType() == Type::UIntTy &&
1801 "Insertelement index must be uint type!");
1802 return getInsertElementTy(cast<PackedType>(Val->getType())->getElementType(),
1803 Val, Elt, Idx);
1804}
1805
Chris Lattner00f10232006-04-08 01:18:18 +00001806Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
1807 Constant *V2, Constant *Mask) {
1808 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
1809 return FC; // Fold a few common cases...
1810 // Look up the constant in the table first to ensure uniqueness
1811 std::vector<Constant*> ArgVec(1, V1);
1812 ArgVec.push_back(V2);
1813 ArgVec.push_back(Mask);
1814 const ExprMapKeyType &Key = std::make_pair(Instruction::ShuffleVector,ArgVec);
Chris Lattner8a94bf12006-09-28 00:35:06 +00001815 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner00f10232006-04-08 01:18:18 +00001816}
1817
1818Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
1819 Constant *Mask) {
1820 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
1821 "Invalid shuffle vector constant expr operands!");
1822 return getShuffleVectorTy(V1->getType(), V1, V2, Mask);
1823}
1824
1825
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001826// destroyConstant - Remove the constant from the constant table...
1827//
1828void ConstantExpr::destroyConstant() {
Chris Lattner8a94bf12006-09-28 00:35:06 +00001829 ExprConstants->remove(this);
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001830 destroyConstantImpl();
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001831}
1832
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001833const char *ConstantExpr::getOpcodeName() const {
1834 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001835}
Reid Spencer1c9c8e62004-07-17 23:48:33 +00001836
Chris Lattner5cbade92005-10-03 21:58:36 +00001837//===----------------------------------------------------------------------===//
1838// replaceUsesOfWithOnConstant implementations
1839
1840void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00001841 Use *U) {
Chris Lattner5cbade92005-10-03 21:58:36 +00001842 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattnerc182a882005-10-04 01:17:50 +00001843 Constant *ToC = cast<Constant>(To);
Chris Lattner23ec01f2005-10-04 18:47:09 +00001844
1845 unsigned OperandToUpdate = U-OperandList;
1846 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1847
Jim Laskeyede5aa42006-07-17 17:38:29 +00001848 std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnercea141f2005-10-03 22:51:37 +00001849 Lookup.first.first = getType();
1850 Lookup.second = this;
Chris Lattner23ec01f2005-10-04 18:47:09 +00001851
Chris Lattnercea141f2005-10-03 22:51:37 +00001852 std::vector<Constant*> &Values = Lookup.first.second;
1853 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattner23ec01f2005-10-04 18:47:09 +00001854
Chris Lattnerc182a882005-10-04 01:17:50 +00001855 // Fill values with the modified operands of the constant array. Also,
1856 // compute whether this turns into an all-zeros array.
Chris Lattner23ec01f2005-10-04 18:47:09 +00001857 bool isAllZeros = false;
1858 if (!ToC->isNullValue()) {
1859 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1860 Values.push_back(cast<Constant>(O->get()));
1861 } else {
1862 isAllZeros = true;
1863 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1864 Constant *Val = cast<Constant>(O->get());
1865 Values.push_back(Val);
1866 if (isAllZeros) isAllZeros = Val->isNullValue();
1867 }
Chris Lattner5cbade92005-10-03 21:58:36 +00001868 }
Chris Lattner23ec01f2005-10-04 18:47:09 +00001869 Values[OperandToUpdate] = ToC;
Chris Lattner5cbade92005-10-03 21:58:36 +00001870
Chris Lattnercea141f2005-10-03 22:51:37 +00001871 Constant *Replacement = 0;
1872 if (isAllZeros) {
1873 Replacement = ConstantAggregateZero::get(getType());
1874 } else {
1875 // Check to see if we have this array type already.
1876 bool Exists;
Jim Laskeyede5aa42006-07-17 17:38:29 +00001877 ArrayConstantsTy::MapTy::iterator I =
Chris Lattner8a94bf12006-09-28 00:35:06 +00001878 ArrayConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattnercea141f2005-10-03 22:51:37 +00001879
1880 if (Exists) {
1881 Replacement = I->second;
1882 } else {
1883 // Okay, the new shape doesn't exist in the system yet. Instead of
1884 // creating a new constant array, inserting it, replaceallusesof'ing the
1885 // old with the new, then deleting the old... just update the current one
1886 // in place!
Chris Lattner8a94bf12006-09-28 00:35:06 +00001887 ArrayConstants->MoveConstantToNewSlot(this, I);
Chris Lattnercea141f2005-10-03 22:51:37 +00001888
Chris Lattner23ec01f2005-10-04 18:47:09 +00001889 // Update to the new value.
1890 setOperand(OperandToUpdate, ToC);
Chris Lattnercea141f2005-10-03 22:51:37 +00001891 return;
1892 }
1893 }
1894
1895 // Otherwise, I do need to replace this with an existing value.
Chris Lattner5cbade92005-10-03 21:58:36 +00001896 assert(Replacement != this && "I didn't contain From!");
1897
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00001898 // Everyone using this now uses the replacement.
1899 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattner5cbade92005-10-03 21:58:36 +00001900
1901 // Delete the old constant!
1902 destroyConstant();
1903}
1904
1905void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00001906 Use *U) {
Chris Lattner5cbade92005-10-03 21:58:36 +00001907 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattnerc182a882005-10-04 01:17:50 +00001908 Constant *ToC = cast<Constant>(To);
1909
Chris Lattner23ec01f2005-10-04 18:47:09 +00001910 unsigned OperandToUpdate = U-OperandList;
1911 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1912
Jim Laskeyede5aa42006-07-17 17:38:29 +00001913 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnerc182a882005-10-04 01:17:50 +00001914 Lookup.first.first = getType();
1915 Lookup.second = this;
1916 std::vector<Constant*> &Values = Lookup.first.second;
1917 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattner5cbade92005-10-03 21:58:36 +00001918
Chris Lattner23ec01f2005-10-04 18:47:09 +00001919
Chris Lattnerc182a882005-10-04 01:17:50 +00001920 // Fill values with the modified operands of the constant struct. Also,
1921 // compute whether this turns into an all-zeros struct.
Chris Lattner23ec01f2005-10-04 18:47:09 +00001922 bool isAllZeros = false;
1923 if (!ToC->isNullValue()) {
1924 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1925 Values.push_back(cast<Constant>(O->get()));
1926 } else {
1927 isAllZeros = true;
1928 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1929 Constant *Val = cast<Constant>(O->get());
1930 Values.push_back(Val);
1931 if (isAllZeros) isAllZeros = Val->isNullValue();
1932 }
Chris Lattnerc182a882005-10-04 01:17:50 +00001933 }
Chris Lattner23ec01f2005-10-04 18:47:09 +00001934 Values[OperandToUpdate] = ToC;
1935
Chris Lattnerc182a882005-10-04 01:17:50 +00001936 Constant *Replacement = 0;
1937 if (isAllZeros) {
1938 Replacement = ConstantAggregateZero::get(getType());
1939 } else {
1940 // Check to see if we have this array type already.
1941 bool Exists;
Jim Laskeyede5aa42006-07-17 17:38:29 +00001942 StructConstantsTy::MapTy::iterator I =
Chris Lattner8a94bf12006-09-28 00:35:06 +00001943 StructConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattnerc182a882005-10-04 01:17:50 +00001944
1945 if (Exists) {
1946 Replacement = I->second;
1947 } else {
1948 // Okay, the new shape doesn't exist in the system yet. Instead of
1949 // creating a new constant struct, inserting it, replaceallusesof'ing the
1950 // old with the new, then deleting the old... just update the current one
1951 // in place!
Chris Lattner8a94bf12006-09-28 00:35:06 +00001952 StructConstants->MoveConstantToNewSlot(this, I);
Chris Lattnerc182a882005-10-04 01:17:50 +00001953
Chris Lattner23ec01f2005-10-04 18:47:09 +00001954 // Update to the new value.
1955 setOperand(OperandToUpdate, ToC);
Chris Lattnerc182a882005-10-04 01:17:50 +00001956 return;
1957 }
Chris Lattner5cbade92005-10-03 21:58:36 +00001958 }
1959
Chris Lattner5cbade92005-10-03 21:58:36 +00001960 assert(Replacement != this && "I didn't contain From!");
1961
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00001962 // Everyone using this now uses the replacement.
1963 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattner5cbade92005-10-03 21:58:36 +00001964
1965 // Delete the old constant!
1966 destroyConstant();
1967}
1968
1969void ConstantPacked::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00001970 Use *U) {
Chris Lattner5cbade92005-10-03 21:58:36 +00001971 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
1972
1973 std::vector<Constant*> Values;
1974 Values.reserve(getNumOperands()); // Build replacement array...
1975 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1976 Constant *Val = getOperand(i);
1977 if (Val == From) Val = cast<Constant>(To);
1978 Values.push_back(Val);
1979 }
1980
1981 Constant *Replacement = ConstantPacked::get(getType(), Values);
1982 assert(Replacement != this && "I didn't contain From!");
1983
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00001984 // Everyone using this now uses the replacement.
1985 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattner5cbade92005-10-03 21:58:36 +00001986
1987 // Delete the old constant!
1988 destroyConstant();
1989}
1990
1991void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00001992 Use *U) {
Chris Lattner5cbade92005-10-03 21:58:36 +00001993 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
1994 Constant *To = cast<Constant>(ToV);
1995
1996 Constant *Replacement = 0;
1997 if (getOpcode() == Instruction::GetElementPtr) {
1998 std::vector<Constant*> Indices;
1999 Constant *Pointer = getOperand(0);
2000 Indices.reserve(getNumOperands()-1);
2001 if (Pointer == From) Pointer = To;
2002
2003 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2004 Constant *Val = getOperand(i);
2005 if (Val == From) Val = To;
2006 Indices.push_back(Val);
2007 }
2008 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
Reid Spencer3da59db2006-11-27 01:05:10 +00002009 } else if (isCast()) {
Chris Lattner5cbade92005-10-03 21:58:36 +00002010 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer3da59db2006-11-27 01:05:10 +00002011 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattner5cbade92005-10-03 21:58:36 +00002012 } else if (getOpcode() == Instruction::Select) {
2013 Constant *C1 = getOperand(0);
2014 Constant *C2 = getOperand(1);
2015 Constant *C3 = getOperand(2);
2016 if (C1 == From) C1 = To;
2017 if (C2 == From) C2 = To;
2018 if (C3 == From) C3 = To;
2019 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00002020 } else if (getOpcode() == Instruction::ExtractElement) {
2021 Constant *C1 = getOperand(0);
2022 Constant *C2 = getOperand(1);
2023 if (C1 == From) C1 = To;
2024 if (C2 == From) C2 = To;
2025 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattner42b55802006-04-08 05:09:48 +00002026 } else if (getOpcode() == Instruction::InsertElement) {
2027 Constant *C1 = getOperand(0);
2028 Constant *C2 = getOperand(1);
2029 Constant *C3 = getOperand(1);
2030 if (C1 == From) C1 = To;
2031 if (C2 == From) C2 = To;
2032 if (C3 == From) C3 = To;
2033 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2034 } else if (getOpcode() == Instruction::ShuffleVector) {
2035 Constant *C1 = getOperand(0);
2036 Constant *C2 = getOperand(1);
2037 Constant *C3 = getOperand(2);
2038 if (C1 == From) C1 = To;
2039 if (C2 == From) C2 = To;
2040 if (C3 == From) C3 = To;
2041 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Chris Lattner5cbade92005-10-03 21:58:36 +00002042 } else if (getNumOperands() == 2) {
2043 Constant *C1 = getOperand(0);
2044 Constant *C2 = getOperand(1);
2045 if (C1 == From) C1 = To;
2046 if (C2 == From) C2 = To;
2047 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2048 } else {
2049 assert(0 && "Unknown ConstantExpr type!");
2050 return;
2051 }
2052
2053 assert(Replacement != this && "I didn't contain From!");
2054
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002055 // Everyone using this now uses the replacement.
2056 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattner5cbade92005-10-03 21:58:36 +00002057
2058 // Delete the old constant!
2059 destroyConstant();
2060}
2061
2062
Jim Laskey21b6c9d2006-03-08 18:11:07 +00002063/// getStringValue - Turn an LLVM constant pointer that eventually points to a
2064/// global into a string value. Return an empty string if we can't do it.
Evan Cheng09371032006-03-10 23:52:03 +00002065/// Parameter Chop determines if the result is chopped at the first null
2066/// terminator.
Jim Laskey21b6c9d2006-03-08 18:11:07 +00002067///
Evan Cheng09371032006-03-10 23:52:03 +00002068std::string Constant::getStringValue(bool Chop, unsigned Offset) {
Jim Laskey21b6c9d2006-03-08 18:11:07 +00002069 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(this)) {
2070 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
2071 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
2072 if (Init->isString()) {
2073 std::string Result = Init->getAsString();
2074 if (Offset < Result.size()) {
2075 // If we are pointing INTO The string, erase the beginning...
2076 Result.erase(Result.begin(), Result.begin()+Offset);
2077
2078 // Take off the null terminator, and any string fragments after it.
Evan Cheng09371032006-03-10 23:52:03 +00002079 if (Chop) {
2080 std::string::size_type NullPos = Result.find_first_of((char)0);
2081 if (NullPos != std::string::npos)
2082 Result.erase(Result.begin()+NullPos, Result.end());
2083 }
Jim Laskey21b6c9d2006-03-08 18:11:07 +00002084 return Result;
2085 }
2086 }
2087 }
2088 } else if (Constant *C = dyn_cast<Constant>(this)) {
2089 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
Evan Cheng2352ce92006-03-11 00:13:10 +00002090 return GV->getStringValue(Chop, Offset);
Jim Laskey21b6c9d2006-03-08 18:11:07 +00002091 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
2092 if (CE->getOpcode() == Instruction::GetElementPtr) {
2093 // Turn a gep into the specified offset.
2094 if (CE->getNumOperands() == 3 &&
2095 cast<Constant>(CE->getOperand(1))->isNullValue() &&
2096 isa<ConstantInt>(CE->getOperand(2))) {
Reid Spencerb83eb642006-10-20 07:07:24 +00002097 Offset += cast<ConstantInt>(CE->getOperand(2))->getZExtValue();
Evan Cheng2352ce92006-03-11 00:13:10 +00002098 return CE->getOperand(0)->getStringValue(Chop, Offset);
Jim Laskey21b6c9d2006-03-08 18:11:07 +00002099 }
2100 }
2101 }
2102 }
2103 return "";
2104}
2105