blob: 06dcbb38a7bf7f84a68d92f4222ee53eb804a8bb [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) {
932 unsigned Size = Ty->getPrimitiveSizeInBits();
933 uint64_t ZeroExtendedCanonicalization = V & (~uint64_t(0UL) >> (64-Size));
934 return IntConstants->getOrCreate(Ty, ZeroExtendedCanonicalization );
Chris Lattner531daef2001-09-07 16:46:31 +0000935}
936
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000937//---- ConstantFP::get() implementation...
Chris Lattner531daef2001-09-07 16:46:31 +0000938//
Chris Lattnercfdf2412004-02-01 22:49:04 +0000939namespace llvm {
940 template<>
941 struct ConstantCreator<ConstantFP, Type, uint64_t> {
942 static ConstantFP *create(const Type *Ty, uint64_t V) {
943 assert(Ty == Type::DoubleTy);
Jim Laskeycb6682f2005-08-17 19:34:49 +0000944 return new ConstantFP(Ty, BitsToDouble(V));
Chris Lattnercfdf2412004-02-01 22:49:04 +0000945 }
946 };
947 template<>
948 struct ConstantCreator<ConstantFP, Type, uint32_t> {
949 static ConstantFP *create(const Type *Ty, uint32_t V) {
950 assert(Ty == Type::FloatTy);
Jim Laskeycb6682f2005-08-17 19:34:49 +0000951 return new ConstantFP(Ty, BitsToFloat(V));
Chris Lattnercfdf2412004-02-01 22:49:04 +0000952 }
953 };
954}
955
Chris Lattner8a94bf12006-09-28 00:35:06 +0000956static ManagedStatic<ValueMap<uint64_t, Type, ConstantFP> > DoubleConstants;
957static ManagedStatic<ValueMap<uint32_t, Type, ConstantFP> > FloatConstants;
Chris Lattner531daef2001-09-07 16:46:31 +0000958
Jim Laskey3a1eff72005-08-17 20:06:22 +0000959bool ConstantFP::isNullValue() const {
960 return DoubleToBits(Val) == 0;
961}
962
963bool ConstantFP::isExactlyValue(double V) const {
964 return DoubleToBits(V) == DoubleToBits(Val);
965}
966
967
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000968ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner90fb19e2004-01-23 00:55:21 +0000969 if (Ty == Type::FloatTy) {
970 // Force the value through memory to normalize it.
Chris Lattner8a94bf12006-09-28 00:35:06 +0000971 return FloatConstants->getOrCreate(Ty, FloatToBits(V));
Chris Lattnercfdf2412004-02-01 22:49:04 +0000972 } else {
973 assert(Ty == Type::DoubleTy);
Chris Lattner8a94bf12006-09-28 00:35:06 +0000974 return DoubleConstants->getOrCreate(Ty, DoubleToBits(V));
Chris Lattner90fb19e2004-01-23 00:55:21 +0000975 }
Chris Lattner531daef2001-09-07 16:46:31 +0000976}
977
Chris Lattner40bbeb52004-02-15 05:53:04 +0000978//---- ConstantAggregateZero::get() implementation...
979//
980namespace llvm {
981 // ConstantAggregateZero does not take extra "value" argument...
982 template<class ValType>
983 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
984 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
985 return new ConstantAggregateZero(Ty);
986 }
987 };
988
989 template<>
990 struct ConvertConstantType<ConstantAggregateZero, Type> {
991 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
992 // Make everyone now use a constant of the new type...
993 Constant *New = ConstantAggregateZero::get(NewTy);
994 assert(New != OldC && "Didn't replace constant??");
995 OldC->uncheckedReplaceAllUsesWith(New);
996 OldC->destroyConstant(); // This constant is now dead, destroy it.
997 }
998 };
999}
1000
Chris Lattner8a94bf12006-09-28 00:35:06 +00001001static ManagedStatic<ValueMap<char, Type,
1002 ConstantAggregateZero> > AggZeroConstants;
Chris Lattner40bbeb52004-02-15 05:53:04 +00001003
Chris Lattner6823c9f2004-08-04 04:48:01 +00001004static char getValType(ConstantAggregateZero *CPZ) { return 0; }
1005
Chris Lattner40bbeb52004-02-15 05:53:04 +00001006Constant *ConstantAggregateZero::get(const Type *Ty) {
Chris Lattner31d1a2c2006-06-10 04:16:23 +00001007 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<PackedType>(Ty)) &&
1008 "Cannot create an aggregate zero of non-aggregate type!");
Chris Lattner8a94bf12006-09-28 00:35:06 +00001009 return AggZeroConstants->getOrCreate(Ty, 0);
Chris Lattner40bbeb52004-02-15 05:53:04 +00001010}
1011
1012// destroyConstant - Remove the constant from the constant table...
1013//
1014void ConstantAggregateZero::destroyConstant() {
Chris Lattner8a94bf12006-09-28 00:35:06 +00001015 AggZeroConstants->remove(this);
Chris Lattner40bbeb52004-02-15 05:53:04 +00001016 destroyConstantImpl();
1017}
1018
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001019//---- ConstantArray::get() implementation...
Chris Lattner531daef2001-09-07 16:46:31 +00001020//
Chris Lattner31f84992003-11-21 20:23:48 +00001021namespace llvm {
1022 template<>
1023 struct ConvertConstantType<ConstantArray, ArrayType> {
1024 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
1025 // Make everyone now use a constant of the new type...
1026 std::vector<Constant*> C;
1027 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1028 C.push_back(cast<Constant>(OldC->getOperand(i)));
1029 Constant *New = ConstantArray::get(NewTy, C);
1030 assert(New != OldC && "Didn't replace constant??");
1031 OldC->uncheckedReplaceAllUsesWith(New);
1032 OldC->destroyConstant(); // This constant is now dead, destroy it.
1033 }
1034 };
1035}
Chris Lattnered468e372003-10-05 00:17:43 +00001036
Chris Lattner6823c9f2004-08-04 04:48:01 +00001037static std::vector<Constant*> getValType(ConstantArray *CA) {
1038 std::vector<Constant*> Elements;
1039 Elements.reserve(CA->getNumOperands());
1040 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1041 Elements.push_back(cast<Constant>(CA->getOperand(i)));
1042 return Elements;
1043}
1044
Chris Lattnercea141f2005-10-03 22:51:37 +00001045typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattnera55b30a2005-10-04 17:48:46 +00001046 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattner8a94bf12006-09-28 00:35:06 +00001047static ManagedStatic<ArrayConstantsTy> ArrayConstants;
Chris Lattner531daef2001-09-07 16:46:31 +00001048
Chris Lattnerca705fa2004-02-15 04:14:47 +00001049Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner40bbeb52004-02-15 05:53:04 +00001050 const std::vector<Constant*> &V) {
1051 // If this is an all-zero array, return a ConstantAggregateZero object
1052 if (!V.empty()) {
1053 Constant *C = V[0];
1054 if (!C->isNullValue())
Chris Lattner8a94bf12006-09-28 00:35:06 +00001055 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner40bbeb52004-02-15 05:53:04 +00001056 for (unsigned i = 1, e = V.size(); i != e; ++i)
1057 if (V[i] != C)
Chris Lattner8a94bf12006-09-28 00:35:06 +00001058 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner40bbeb52004-02-15 05:53:04 +00001059 }
1060 return ConstantAggregateZero::get(Ty);
Chris Lattner531daef2001-09-07 16:46:31 +00001061}
1062
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001063// destroyConstant - Remove the constant from the constant table...
1064//
1065void ConstantArray::destroyConstant() {
Chris Lattner8a94bf12006-09-28 00:35:06 +00001066 ArrayConstants->remove(this);
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001067 destroyConstantImpl();
1068}
1069
Reid Spencer89494772006-05-30 08:23:18 +00001070/// ConstantArray::get(const string&) - Return an array that is initialized to
1071/// contain the specified string. If length is zero then a null terminator is
1072/// added to the specified string so that it may be used in a natural way.
1073/// Otherwise, the length parameter specifies how much of the string to use
1074/// and it won't be null terminated.
1075///
Reid Spencer461bed22006-05-30 18:15:07 +00001076Constant *ConstantArray::get(const std::string &Str, bool AddNull) {
Chris Lattner697954c2002-01-20 22:54:45 +00001077 std::vector<Constant*> ElementVals;
Reid Spencer461bed22006-05-30 18:15:07 +00001078 for (unsigned i = 0; i < Str.length(); ++i)
Reid Spencerb83eb642006-10-20 07:07:24 +00001079 ElementVals.push_back(ConstantInt::get(Type::SByteTy, Str[i]));
Chris Lattnerc5bdb242001-10-14 23:54:12 +00001080
1081 // Add a null terminator to the string...
Reid Spencer461bed22006-05-30 18:15:07 +00001082 if (AddNull) {
Reid Spencerb83eb642006-10-20 07:07:24 +00001083 ElementVals.push_back(ConstantInt::get(Type::SByteTy, 0));
Reid Spencer89494772006-05-30 08:23:18 +00001084 }
Chris Lattnerc5bdb242001-10-14 23:54:12 +00001085
Reid Spencer461bed22006-05-30 18:15:07 +00001086 ArrayType *ATy = ArrayType::get(Type::SByteTy, ElementVals.size());
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001087 return ConstantArray::get(ATy, ElementVals);
Vikram S. Advedb2da492001-10-14 23:17:20 +00001088}
1089
Chris Lattner13cfdea2004-01-14 17:06:38 +00001090/// isString - This method returns true if the array is an array of sbyte or
1091/// ubyte, and if the elements of the array are all ConstantInt's.
1092bool ConstantArray::isString() const {
1093 // Check the element type for sbyte or ubyte...
Chris Lattner7aa162b2004-01-14 17:51:53 +00001094 if (getType()->getElementType() != Type::UByteTy &&
Chris Lattner13cfdea2004-01-14 17:06:38 +00001095 getType()->getElementType() != Type::SByteTy)
1096 return false;
1097 // Check the elements to make sure they are all integers, not constant
1098 // expressions.
1099 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1100 if (!isa<ConstantInt>(getOperand(i)))
1101 return false;
1102 return true;
1103}
1104
Evan Cheng22c70302006-10-26 19:15:05 +00001105/// isCString - This method returns true if the array is a string (see
1106/// isString) and it ends in a null byte \0 and does not contains any other
1107/// null bytes except its terminator.
1108bool ConstantArray::isCString() const {
Evan Chengabf63452006-10-26 21:48:03 +00001109 // Check the element type for sbyte or ubyte...
1110 if (getType()->getElementType() != Type::UByteTy &&
1111 getType()->getElementType() != Type::SByteTy)
1112 return false;
1113 Constant *Zero = Constant::getNullValue(getOperand(0)->getType());
1114 // Last element must be a null.
1115 if (getOperand(getNumOperands()-1) != Zero)
1116 return false;
1117 // Other elements must be non-null integers.
1118 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1119 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng22c70302006-10-26 19:15:05 +00001120 return false;
Evan Chengabf63452006-10-26 21:48:03 +00001121 if (getOperand(i) == Zero)
1122 return false;
1123 }
Evan Cheng22c70302006-10-26 19:15:05 +00001124 return true;
1125}
1126
1127
Chris Lattner93aeea32002-08-26 17:53:56 +00001128// getAsString - If the sub-element type of this array is either sbyte or ubyte,
1129// then this method converts the array to an std::string and returns it.
1130// Otherwise, it asserts out.
1131//
1132std::string ConstantArray::getAsString() const {
Chris Lattner13cfdea2004-01-14 17:06:38 +00001133 assert(isString() && "Not a string!");
Chris Lattner93aeea32002-08-26 17:53:56 +00001134 std::string Result;
Chris Lattnerc07736a2003-07-23 15:22:26 +00001135 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Reid Spencerb83eb642006-10-20 07:07:24 +00001136 Result += (char)cast<ConstantInt>(getOperand(i))->getZExtValue();
Chris Lattner93aeea32002-08-26 17:53:56 +00001137 return Result;
1138}
1139
1140
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001141//---- ConstantStruct::get() implementation...
Chris Lattner531daef2001-09-07 16:46:31 +00001142//
Chris Lattnered468e372003-10-05 00:17:43 +00001143
Chris Lattner31f84992003-11-21 20:23:48 +00001144namespace llvm {
1145 template<>
1146 struct ConvertConstantType<ConstantStruct, StructType> {
1147 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1148 // Make everyone now use a constant of the new type...
1149 std::vector<Constant*> C;
1150 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1151 C.push_back(cast<Constant>(OldC->getOperand(i)));
1152 Constant *New = ConstantStruct::get(NewTy, C);
1153 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanfd939082005-04-21 23:48:37 +00001154
Chris Lattner31f84992003-11-21 20:23:48 +00001155 OldC->uncheckedReplaceAllUsesWith(New);
1156 OldC->destroyConstant(); // This constant is now dead, destroy it.
1157 }
1158 };
1159}
Chris Lattnered468e372003-10-05 00:17:43 +00001160
Chris Lattnerc182a882005-10-04 01:17:50 +00001161typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattnera55b30a2005-10-04 17:48:46 +00001162 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner8a94bf12006-09-28 00:35:06 +00001163static ManagedStatic<StructConstantsTy> StructConstants;
Chris Lattner531daef2001-09-07 16:46:31 +00001164
Chris Lattner6823c9f2004-08-04 04:48:01 +00001165static std::vector<Constant*> getValType(ConstantStruct *CS) {
1166 std::vector<Constant*> Elements;
1167 Elements.reserve(CS->getNumOperands());
1168 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1169 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1170 return Elements;
1171}
1172
Chris Lattnerca705fa2004-02-15 04:14:47 +00001173Constant *ConstantStruct::get(const StructType *Ty,
1174 const std::vector<Constant*> &V) {
Chris Lattner40bbeb52004-02-15 05:53:04 +00001175 // Create a ConstantAggregateZero value if all elements are zeros...
1176 for (unsigned i = 0, e = V.size(); i != e; ++i)
1177 if (!V[i]->isNullValue())
Chris Lattner8a94bf12006-09-28 00:35:06 +00001178 return StructConstants->getOrCreate(Ty, V);
Chris Lattner40bbeb52004-02-15 05:53:04 +00001179
1180 return ConstantAggregateZero::get(Ty);
Chris Lattner531daef2001-09-07 16:46:31 +00001181}
Chris Lattner6a57baa2001-10-03 15:39:36 +00001182
Chris Lattnerb370c7a2004-07-12 20:35:11 +00001183Constant *ConstantStruct::get(const std::vector<Constant*> &V) {
1184 std::vector<const Type*> StructEls;
1185 StructEls.reserve(V.size());
1186 for (unsigned i = 0, e = V.size(); i != e; ++i)
1187 StructEls.push_back(V[i]->getType());
1188 return get(StructType::get(StructEls), V);
1189}
1190
Chris Lattnerf5ec48d2001-10-13 06:57:33 +00001191// destroyConstant - Remove the constant from the constant table...
Chris Lattner6a57baa2001-10-03 15:39:36 +00001192//
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001193void ConstantStruct::destroyConstant() {
Chris Lattner8a94bf12006-09-28 00:35:06 +00001194 StructConstants->remove(this);
Chris Lattnerf5ec48d2001-10-13 06:57:33 +00001195 destroyConstantImpl();
1196}
Chris Lattner6a57baa2001-10-03 15:39:36 +00001197
Brian Gaeke715c90b2004-08-20 06:00:58 +00001198//---- ConstantPacked::get() implementation...
1199//
1200namespace llvm {
1201 template<>
1202 struct ConvertConstantType<ConstantPacked, PackedType> {
1203 static void convert(ConstantPacked *OldC, const PackedType *NewTy) {
1204 // Make everyone now use a constant of the new type...
1205 std::vector<Constant*> C;
1206 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1207 C.push_back(cast<Constant>(OldC->getOperand(i)));
1208 Constant *New = ConstantPacked::get(NewTy, C);
1209 assert(New != OldC && "Didn't replace constant??");
1210 OldC->uncheckedReplaceAllUsesWith(New);
1211 OldC->destroyConstant(); // This constant is now dead, destroy it.
1212 }
1213 };
1214}
1215
1216static std::vector<Constant*> getValType(ConstantPacked *CP) {
1217 std::vector<Constant*> Elements;
1218 Elements.reserve(CP->getNumOperands());
1219 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1220 Elements.push_back(CP->getOperand(i));
1221 return Elements;
1222}
1223
Chris Lattner8a94bf12006-09-28 00:35:06 +00001224static ManagedStatic<ValueMap<std::vector<Constant*>, PackedType,
1225 ConstantPacked> > PackedConstants;
Brian Gaeke715c90b2004-08-20 06:00:58 +00001226
1227Constant *ConstantPacked::get(const PackedType *Ty,
1228 const std::vector<Constant*> &V) {
1229 // If this is an all-zero packed, return a ConstantAggregateZero object
1230 if (!V.empty()) {
1231 Constant *C = V[0];
1232 if (!C->isNullValue())
Chris Lattner8a94bf12006-09-28 00:35:06 +00001233 return PackedConstants->getOrCreate(Ty, V);
Brian Gaeke715c90b2004-08-20 06:00:58 +00001234 for (unsigned i = 1, e = V.size(); i != e; ++i)
1235 if (V[i] != C)
Chris Lattner8a94bf12006-09-28 00:35:06 +00001236 return PackedConstants->getOrCreate(Ty, V);
Brian Gaeke715c90b2004-08-20 06:00:58 +00001237 }
1238 return ConstantAggregateZero::get(Ty);
1239}
1240
1241Constant *ConstantPacked::get(const std::vector<Constant*> &V) {
1242 assert(!V.empty() && "Cannot infer type if V is empty");
1243 return get(PackedType::get(V.front()->getType(),V.size()), V);
1244}
1245
1246// destroyConstant - Remove the constant from the constant table...
1247//
1248void ConstantPacked::destroyConstant() {
Chris Lattner8a94bf12006-09-28 00:35:06 +00001249 PackedConstants->remove(this);
Brian Gaeke715c90b2004-08-20 06:00:58 +00001250 destroyConstantImpl();
1251}
1252
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001253//---- ConstantPointerNull::get() implementation...
Chris Lattnerf5ec48d2001-10-13 06:57:33 +00001254//
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001255
Chris Lattner31f84992003-11-21 20:23:48 +00001256namespace llvm {
1257 // ConstantPointerNull does not take extra "value" argument...
1258 template<class ValType>
1259 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1260 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1261 return new ConstantPointerNull(Ty);
1262 }
1263 };
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001264
Chris Lattner31f84992003-11-21 20:23:48 +00001265 template<>
1266 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1267 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1268 // Make everyone now use a constant of the new type...
1269 Constant *New = ConstantPointerNull::get(NewTy);
1270 assert(New != OldC && "Didn't replace constant??");
1271 OldC->uncheckedReplaceAllUsesWith(New);
1272 OldC->destroyConstant(); // This constant is now dead, destroy it.
1273 }
1274 };
1275}
Chris Lattnered468e372003-10-05 00:17:43 +00001276
Chris Lattner8a94bf12006-09-28 00:35:06 +00001277static ManagedStatic<ValueMap<char, PointerType,
1278 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerf5ec48d2001-10-13 06:57:33 +00001279
Chris Lattner6823c9f2004-08-04 04:48:01 +00001280static char getValType(ConstantPointerNull *) {
1281 return 0;
1282}
1283
1284
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001285ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner8a94bf12006-09-28 00:35:06 +00001286 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner6a57baa2001-10-03 15:39:36 +00001287}
1288
Chris Lattner41661fd2002-08-18 00:40:04 +00001289// destroyConstant - Remove the constant from the constant table...
1290//
1291void ConstantPointerNull::destroyConstant() {
Chris Lattner8a94bf12006-09-28 00:35:06 +00001292 NullPtrConstants->remove(this);
Chris Lattner41661fd2002-08-18 00:40:04 +00001293 destroyConstantImpl();
1294}
1295
1296
Chris Lattnerb9f18592004-10-16 18:07:16 +00001297//---- UndefValue::get() implementation...
1298//
1299
1300namespace llvm {
1301 // UndefValue does not take extra "value" argument...
1302 template<class ValType>
1303 struct ConstantCreator<UndefValue, Type, ValType> {
1304 static UndefValue *create(const Type *Ty, const ValType &V) {
1305 return new UndefValue(Ty);
1306 }
1307 };
1308
1309 template<>
1310 struct ConvertConstantType<UndefValue, Type> {
1311 static void convert(UndefValue *OldC, const Type *NewTy) {
1312 // Make everyone now use a constant of the new type.
1313 Constant *New = UndefValue::get(NewTy);
1314 assert(New != OldC && "Didn't replace constant??");
1315 OldC->uncheckedReplaceAllUsesWith(New);
1316 OldC->destroyConstant(); // This constant is now dead, destroy it.
1317 }
1318 };
1319}
1320
Chris Lattner8a94bf12006-09-28 00:35:06 +00001321static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerb9f18592004-10-16 18:07:16 +00001322
1323static char getValType(UndefValue *) {
1324 return 0;
1325}
1326
1327
1328UndefValue *UndefValue::get(const Type *Ty) {
Chris Lattner8a94bf12006-09-28 00:35:06 +00001329 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerb9f18592004-10-16 18:07:16 +00001330}
1331
1332// destroyConstant - Remove the constant from the constant table.
1333//
1334void UndefValue::destroyConstant() {
Chris Lattner8a94bf12006-09-28 00:35:06 +00001335 UndefValueConstants->remove(this);
Chris Lattnerb9f18592004-10-16 18:07:16 +00001336 destroyConstantImpl();
1337}
1338
1339
1340
1341
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001342//---- ConstantExpr::get() implementations...
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001343//
Chris Lattner9bc02a42003-05-13 21:37:02 +00001344typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001345
Chris Lattner31f84992003-11-21 20:23:48 +00001346namespace llvm {
1347 template<>
1348 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
1349 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
Reid Spencer3da59db2006-11-27 01:05:10 +00001350 if (Instruction::isCast(V.first))
1351 return new UnaryConstantExpr(V.first, V.second[0], Ty);
Chris Lattner31f84992003-11-21 20:23:48 +00001352 if ((V.first >= Instruction::BinaryOpsBegin &&
1353 V.first < Instruction::BinaryOpsEnd) ||
Reid Spencer3822ff52006-11-08 06:47:33 +00001354 V.first == Instruction::Shl ||
1355 V.first == Instruction::LShr ||
1356 V.first == Instruction::AShr)
Chris Lattnere4671472005-01-29 00:34:39 +00001357 return new BinaryConstantExpr(V.first, V.second[0], V.second[1]);
Chris Lattner92e84222004-03-30 22:51:03 +00001358 if (V.first == Instruction::Select)
Chris Lattnere4671472005-01-29 00:34:39 +00001359 return new SelectConstantExpr(V.second[0], V.second[1], V.second[2]);
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001360 if (V.first == Instruction::ExtractElement)
1361 return new ExtractElementConstantExpr(V.second[0], V.second[1]);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001362 if (V.first == Instruction::InsertElement)
1363 return new InsertElementConstantExpr(V.second[0], V.second[1],
1364 V.second[2]);
Chris Lattner00f10232006-04-08 01:18:18 +00001365 if (V.first == Instruction::ShuffleVector)
1366 return new ShuffleVectorConstantExpr(V.second[0], V.second[1],
1367 V.second[2]);
1368
Chris Lattner31f84992003-11-21 20:23:48 +00001369 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
Misha Brukmanfd939082005-04-21 23:48:37 +00001370
Chris Lattner31f84992003-11-21 20:23:48 +00001371 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
Chris Lattnere4671472005-01-29 00:34:39 +00001372 return new GetElementPtrConstantExpr(V.second[0], IdxList, Ty);
Chris Lattnered468e372003-10-05 00:17:43 +00001373 }
Chris Lattner31f84992003-11-21 20:23:48 +00001374 };
Chris Lattnered468e372003-10-05 00:17:43 +00001375
Chris Lattner31f84992003-11-21 20:23:48 +00001376 template<>
1377 struct ConvertConstantType<ConstantExpr, Type> {
1378 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1379 Constant *New;
1380 switch (OldC->getOpcode()) {
Reid Spencer3da59db2006-11-27 01:05:10 +00001381 case Instruction::Trunc:
1382 case Instruction::ZExt:
1383 case Instruction::SExt:
1384 case Instruction::FPTrunc:
1385 case Instruction::FPExt:
1386 case Instruction::UIToFP:
1387 case Instruction::SIToFP:
1388 case Instruction::FPToUI:
1389 case Instruction::FPToSI:
1390 case Instruction::PtrToInt:
1391 case Instruction::IntToPtr:
1392 case Instruction::BitCast:
1393 New = ConstantExpr::getCast(
1394 OldC->getOpcode(), OldC->getOperand(0), NewTy);
Chris Lattner31f84992003-11-21 20:23:48 +00001395 break;
Chris Lattner08a45cc2004-03-12 05:54:04 +00001396 case Instruction::Select:
1397 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1398 OldC->getOperand(1),
1399 OldC->getOperand(2));
1400 break;
Chris Lattner31f84992003-11-21 20:23:48 +00001401 case Instruction::Shl:
Reid Spencer3822ff52006-11-08 06:47:33 +00001402 case Instruction::LShr:
1403 case Instruction::AShr:
Chris Lattner31f84992003-11-21 20:23:48 +00001404 New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
1405 OldC->getOperand(0), OldC->getOperand(1));
1406 break;
1407 default:
1408 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer0a783f72006-11-02 01:53:59 +00001409 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner31f84992003-11-21 20:23:48 +00001410 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1411 OldC->getOperand(1));
1412 break;
1413 case Instruction::GetElementPtr:
Misha Brukmanfd939082005-04-21 23:48:37 +00001414 // Make everyone now use a constant of the new type...
Chris Lattner7fa6e662004-10-11 22:52:25 +00001415 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
1416 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), Idx);
Chris Lattner31f84992003-11-21 20:23:48 +00001417 break;
1418 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001419
Chris Lattner31f84992003-11-21 20:23:48 +00001420 assert(New != OldC && "Didn't replace constant??");
1421 OldC->uncheckedReplaceAllUsesWith(New);
1422 OldC->destroyConstant(); // This constant is now dead, destroy it.
1423 }
1424 };
1425} // end namespace llvm
Chris Lattnered468e372003-10-05 00:17:43 +00001426
1427
Chris Lattner6823c9f2004-08-04 04:48:01 +00001428static ExprMapKeyType getValType(ConstantExpr *CE) {
1429 std::vector<Constant*> Operands;
1430 Operands.reserve(CE->getNumOperands());
1431 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1432 Operands.push_back(cast<Constant>(CE->getOperand(i)));
1433 return ExprMapKeyType(CE->getOpcode(), Operands);
1434}
1435
Chris Lattner8a94bf12006-09-28 00:35:06 +00001436static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1437 ConstantExpr> > ExprConstants;
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001438
Reid Spencer3da59db2006-11-27 01:05:10 +00001439/// This is a utility function to handle folding of casts and lookup of the
1440/// cast in the ExprConstants map. It is usedby the various get* methods below.
1441static inline Constant *getFoldedCast(
1442 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner9eacf8a2003-10-07 22:19:19 +00001443 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer3da59db2006-11-27 01:05:10 +00001444 // Fold a few common cases
1445 if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
1446 return FC;
Chris Lattnerd628f6a2003-04-17 19:24:48 +00001447
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001448 // Look up the constant in the table first to ensure uniqueness
Chris Lattner9bc02a42003-05-13 21:37:02 +00001449 std::vector<Constant*> argVec(1, C);
Reid Spencer3da59db2006-11-27 01:05:10 +00001450 ExprMapKeyType Key = std::make_pair(opc, argVec);
Chris Lattner8a94bf12006-09-28 00:35:06 +00001451 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001452}
Chris Lattnerf5ec48d2001-10-13 06:57:33 +00001453
Reid Spencer3da59db2006-11-27 01:05:10 +00001454Constant *ConstantExpr::getCast( Constant *C, const Type *Ty ) {
1455 // Note: we can't inline this because it requires the Instructions.h header
1456 return getCast(CastInst::getCastOpcode(C, Ty), C, Ty);
1457}
1458
1459Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
1460 Instruction::CastOps opc = Instruction::CastOps(oc);
1461 assert(Instruction::isCast(opc) && "opcode out of range");
1462 assert(C && Ty && "Null arguments to getCast");
1463 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1464
1465 switch (opc) {
1466 default:
1467 assert(0 && "Invalid cast opcode");
1468 break;
1469 case Instruction::Trunc: return getTrunc(C, Ty);
1470 case Instruction::ZExt: return getZeroExtend(C, Ty);
1471 case Instruction::SExt: return getSignExtend(C, Ty);
1472 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1473 case Instruction::FPExt: return getFPExtend(C, Ty);
1474 case Instruction::UIToFP: return getUIToFP(C, Ty);
1475 case Instruction::SIToFP: return getSIToFP(C, Ty);
1476 case Instruction::FPToUI: return getFPToUI(C, Ty);
1477 case Instruction::FPToSI: return getFPToSI(C, Ty);
1478 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1479 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1480 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattnerf5ac6c22005-01-01 15:59:57 +00001481 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001482 return 0;
1483}
1484
1485Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
1486 assert(C->getType()->isInteger() && "Trunc operand must be integer");
1487 assert(Ty->isIntegral() && "Trunc produces only integral");
1488 assert(C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&&
1489 "SrcTy must be larger than DestTy for Trunc!");
1490
1491 return getFoldedCast(Instruction::Trunc, C, Ty);
1492}
1493
1494Constant *ConstantExpr::getSignExtend(Constant *C, const Type *Ty) {
1495 assert(C->getType()->isIntegral() && "SEXt operand must be integral");
1496 assert(Ty->isInteger() && "SExt produces only integer");
1497 assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1498 "SrcTy must be smaller than DestTy for SExt!");
1499
1500 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerd144f422004-04-04 23:20:30 +00001501}
1502
1503Constant *ConstantExpr::getZeroExtend(Constant *C, const Type *Ty) {
Reid Spencer3da59db2006-11-27 01:05:10 +00001504 assert(C->getType()->isIntegral() && "ZEXt operand must be integral");
1505 assert(Ty->isInteger() && "ZExt produces only integer");
1506 assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1507 "SrcTy must be smaller than DestTy for ZExt!");
1508
1509 return getFoldedCast(Instruction::ZExt, C, Ty);
1510}
1511
1512Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
1513 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1514 C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&&
1515 "This is an illegal floating point truncation!");
1516 return getFoldedCast(Instruction::FPTrunc, C, Ty);
1517}
1518
1519Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
1520 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1521 C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1522 "This is an illegal floating point extension!");
1523 return getFoldedCast(Instruction::FPExt, C, Ty);
1524}
1525
1526Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
1527 assert(C->getType()->isIntegral() && Ty->isFloatingPoint() &&
1528 "This is an illegal uint to floating point cast!");
1529 return getFoldedCast(Instruction::UIToFP, C, Ty);
1530}
1531
1532Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
1533 assert(C->getType()->isIntegral() && Ty->isFloatingPoint() &&
1534 "This is an illegal sint to floating point cast!");
1535 return getFoldedCast(Instruction::SIToFP, C, Ty);
1536}
1537
1538Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
1539 assert(C->getType()->isFloatingPoint() && Ty->isIntegral() &&
1540 "This is an illegal floating point to uint cast!");
1541 return getFoldedCast(Instruction::FPToUI, C, Ty);
1542}
1543
1544Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
1545 assert(C->getType()->isFloatingPoint() && Ty->isIntegral() &&
1546 "This is an illegal floating point to sint cast!");
1547 return getFoldedCast(Instruction::FPToSI, C, Ty);
1548}
1549
1550Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
1551 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
1552 assert(DstTy->isIntegral() && "PtrToInt destination must be integral");
1553 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
1554}
1555
1556Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
1557 assert(C->getType()->isIntegral() && "IntToPtr source must be integral");
1558 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
1559 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
1560}
1561
1562Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
1563 // BitCast implies a no-op cast of type only. No bits change. However, you
1564 // can't cast pointers to anything but pointers.
1565 const Type *SrcTy = C->getType();
1566 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
1567 "Bitcast cannot cast pointer to non-pointer and vice versa");
1568
1569 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
1570 // or nonptr->ptr). For all the other types, the cast is okay if source and
1571 // destination bit widths are identical.
1572 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1573 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
1574 assert(SrcBitSize == DstBitSize && "Bitcast requies types of same width");
1575 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerd144f422004-04-04 23:20:30 +00001576}
1577
Alkis Evlogimenos60ab1402004-10-24 01:41:10 +00001578Constant *ConstantExpr::getSizeOf(const Type *Ty) {
Chris Lattnerfcdd82e2004-12-13 19:48:51 +00001579 // sizeof is implemented as: (ulong) gep (Ty*)null, 1
Alkis Evlogimenos60ab1402004-10-24 01:41:10 +00001580 return getCast(
Chris Lattnerfcdd82e2004-12-13 19:48:51 +00001581 getGetElementPtr(getNullValue(PointerType::get(Ty)),
1582 std::vector<Constant*>(1, ConstantInt::get(Type::UIntTy, 1))),
1583 Type::ULongTy);
Alkis Evlogimenos60ab1402004-10-24 01:41:10 +00001584}
1585
Alkis Evlogimenos1cecd3a2005-03-19 11:40:31 +00001586Constant *ConstantExpr::getPtrPtrFromArrayPtr(Constant *C) {
1587 // pointer from array is implemented as: getelementptr arr ptr, 0, 0
Reid Spencerb83eb642006-10-20 07:07:24 +00001588 static std::vector<Constant*> Indices(2, ConstantInt::get(Type::UIntTy, 0));
Alkis Evlogimenos1cecd3a2005-03-19 11:40:31 +00001589
1590 return ConstantExpr::getGetElementPtr(C, Indices);
1591}
1592
Chris Lattnered468e372003-10-05 00:17:43 +00001593Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
1594 Constant *C1, Constant *C2) {
Reid Spencer3822ff52006-11-08 06:47:33 +00001595 if (Opcode == Instruction::Shl || Opcode == Instruction::LShr ||
1596 Opcode == Instruction::AShr)
Chris Lattner3edd3972004-01-12 19:04:55 +00001597 return getShiftTy(ReqTy, Opcode, C1, C2);
Chris Lattnerf31f5832003-05-21 17:49:25 +00001598 // Check the operands for consistency first
Reid Spencer0a783f72006-11-02 01:53:59 +00001599 assert(Opcode >= Instruction::BinaryOpsBegin &&
1600 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattnerf31f5832003-05-21 17:49:25 +00001601 "Invalid opcode in binary constant expression");
1602 assert(C1->getType() == C2->getType() &&
1603 "Operand types in binary constant expression should match");
Chris Lattnered468e372003-10-05 00:17:43 +00001604
Chris Lattnera5b07402006-09-17 19:14:47 +00001605 if (ReqTy == C1->getType() || (Instruction::isComparison(Opcode) &&
Chris Lattnerc3d12f02004-08-04 18:50:09 +00001606 ReqTy == Type::BoolTy))
Chris Lattnered468e372003-10-05 00:17:43 +00001607 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1608 return FC; // Fold a few common cases...
Chris Lattnerd628f6a2003-04-17 19:24:48 +00001609
Chris Lattner9bc02a42003-05-13 21:37:02 +00001610 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001611 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattner8a94bf12006-09-28 00:35:06 +00001612 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001613}
1614
Chris Lattnerc3d12f02004-08-04 18:50:09 +00001615Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattner91b362b2004-08-17 17:28:46 +00001616#ifndef NDEBUG
1617 switch (Opcode) {
Reid Spencer0a783f72006-11-02 01:53:59 +00001618 case Instruction::Add:
1619 case Instruction::Sub:
Reid Spencer1628cec2006-10-26 06:15:43 +00001620 case Instruction::Mul:
Chris Lattner91b362b2004-08-17 17:28:46 +00001621 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattner20542952006-01-04 01:01:04 +00001622 assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint() ||
1623 isa<PackedType>(C1->getType())) &&
Chris Lattner91b362b2004-08-17 17:28:46 +00001624 "Tried to create an arithmetic operation on a non-arithmetic type!");
1625 break;
Reid Spencer1628cec2006-10-26 06:15:43 +00001626 case Instruction::UDiv:
1627 case Instruction::SDiv:
1628 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1629 assert((C1->getType()->isInteger() || (isa<PackedType>(C1->getType()) &&
1630 cast<PackedType>(C1->getType())->getElementType()->isInteger())) &&
1631 "Tried to create an arithmetic operation on a non-arithmetic type!");
1632 break;
1633 case Instruction::FDiv:
1634 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1635 assert((C1->getType()->isFloatingPoint() || (isa<PackedType>(C1->getType())
1636 && cast<PackedType>(C1->getType())->getElementType()->isFloatingPoint()))
1637 && "Tried to create an arithmetic operation on a non-arithmetic type!");
1638 break;
Reid Spencer0a783f72006-11-02 01:53:59 +00001639 case Instruction::URem:
1640 case Instruction::SRem:
1641 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1642 assert((C1->getType()->isInteger() || (isa<PackedType>(C1->getType()) &&
1643 cast<PackedType>(C1->getType())->getElementType()->isInteger())) &&
1644 "Tried to create an arithmetic operation on a non-arithmetic type!");
1645 break;
1646 case Instruction::FRem:
1647 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1648 assert((C1->getType()->isFloatingPoint() || (isa<PackedType>(C1->getType())
1649 && cast<PackedType>(C1->getType())->getElementType()->isFloatingPoint()))
1650 && "Tried to create an arithmetic operation on a non-arithmetic type!");
1651 break;
Chris Lattner91b362b2004-08-17 17:28:46 +00001652 case Instruction::And:
1653 case Instruction::Or:
1654 case Instruction::Xor:
1655 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattner20542952006-01-04 01:01:04 +00001656 assert((C1->getType()->isIntegral() || isa<PackedType>(C1->getType())) &&
Misha Brukman1bae2912005-01-27 06:46:38 +00001657 "Tried to create a logical operation on a non-integral type!");
Chris Lattner91b362b2004-08-17 17:28:46 +00001658 break;
1659 case Instruction::SetLT: case Instruction::SetGT: case Instruction::SetLE:
1660 case Instruction::SetGE: case Instruction::SetEQ: case Instruction::SetNE:
1661 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1662 break;
1663 case Instruction::Shl:
Reid Spencer3822ff52006-11-08 06:47:33 +00001664 case Instruction::LShr:
1665 case Instruction::AShr:
Chris Lattner91b362b2004-08-17 17:28:46 +00001666 assert(C2->getType() == Type::UByteTy && "Shift should be by ubyte!");
Reid Spencer3822ff52006-11-08 06:47:33 +00001667 assert(C1->getType()->isInteger() &&
Chris Lattner91b362b2004-08-17 17:28:46 +00001668 "Tried to create a shift operation on a non-integer type!");
1669 break;
1670 default:
1671 break;
1672 }
1673#endif
1674
Chris Lattnera5b07402006-09-17 19:14:47 +00001675 if (Instruction::isComparison(Opcode))
Chris Lattnerc3d12f02004-08-04 18:50:09 +00001676 return getTy(Type::BoolTy, Opcode, C1, C2);
1677 else
1678 return getTy(C1->getType(), Opcode, C1, C2);
1679}
1680
Chris Lattner08a45cc2004-03-12 05:54:04 +00001681Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
1682 Constant *V1, Constant *V2) {
1683 assert(C->getType() == Type::BoolTy && "Select condition must be bool!");
1684 assert(V1->getType() == V2->getType() && "Select value types must match!");
1685 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
1686
1687 if (ReqTy == V1->getType())
1688 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1689 return SC; // Fold common cases
1690
1691 std::vector<Constant*> argVec(3, C);
1692 argVec[1] = V1;
1693 argVec[2] = V2;
1694 ExprMapKeyType Key = std::make_pair(Instruction::Select, argVec);
Chris Lattner8a94bf12006-09-28 00:35:06 +00001695 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner08a45cc2004-03-12 05:54:04 +00001696}
1697
Chris Lattner69b9e892004-01-12 19:12:58 +00001698/// getShiftTy - Return a shift left or shift right constant expr
Chris Lattnered468e372003-10-05 00:17:43 +00001699Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
1700 Constant *C1, Constant *C2) {
Chris Lattnerf31f5832003-05-21 17:49:25 +00001701 // Check the operands for consistency first
Reid Spencer3822ff52006-11-08 06:47:33 +00001702 assert((Opcode == Instruction::Shl ||
1703 Opcode == Instruction::LShr ||
1704 Opcode == Instruction::AShr) &&
Chris Lattnerf31f5832003-05-21 17:49:25 +00001705 "Invalid opcode in binary constant expression");
1706 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
1707 "Invalid operand types for Shift constant expr!");
1708
Chris Lattnerd4403b42004-01-12 20:40:42 +00001709 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattnerf31f5832003-05-21 17:49:25 +00001710 return FC; // Fold a few common cases...
1711
1712 // Look up the constant in the table first to ensure uniqueness
1713 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001714 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattner8a94bf12006-09-28 00:35:06 +00001715 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerf31f5832003-05-21 17:49:25 +00001716}
1717
1718
Chris Lattnered468e372003-10-05 00:17:43 +00001719Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner7fa6e662004-10-11 22:52:25 +00001720 const std::vector<Value*> &IdxList) {
1721 assert(GetElementPtrInst::getIndexedType(C->getType(), IdxList, true) &&
Chris Lattner2e9bb1a2004-02-16 20:46:13 +00001722 "GEP indices invalid!");
1723
Chris Lattnerd628f6a2003-04-17 19:24:48 +00001724 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
1725 return FC; // Fold a few common cases...
Chris Lattner2e9bb1a2004-02-16 20:46:13 +00001726
Chris Lattnered468e372003-10-05 00:17:43 +00001727 assert(isa<PointerType>(C->getType()) &&
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001728 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001729 // Look up the constant in the table first to ensure uniqueness
Chris Lattner7fa6e662004-10-11 22:52:25 +00001730 std::vector<Constant*> ArgVec;
1731 ArgVec.reserve(IdxList.size()+1);
1732 ArgVec.push_back(C);
1733 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
1734 ArgVec.push_back(cast<Constant>(IdxList[i]));
1735 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,ArgVec);
Chris Lattner8a94bf12006-09-28 00:35:06 +00001736 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001737}
1738
Chris Lattnered468e372003-10-05 00:17:43 +00001739Constant *ConstantExpr::getGetElementPtr(Constant *C,
1740 const std::vector<Constant*> &IdxList){
1741 // Get the result type of the getelementptr!
1742 std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
1743
1744 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
1745 true);
1746 assert(Ty && "GEP indices invalid!");
Chris Lattner7fa6e662004-10-11 22:52:25 +00001747 return getGetElementPtrTy(PointerType::get(Ty), C, VIdxList);
1748}
1749
1750Constant *ConstantExpr::getGetElementPtr(Constant *C,
1751 const std::vector<Value*> &IdxList) {
1752 // Get the result type of the getelementptr!
1753 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
1754 true);
1755 assert(Ty && "GEP indices invalid!");
Chris Lattnered468e372003-10-05 00:17:43 +00001756 return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
1757}
1758
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001759Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
1760 Constant *Idx) {
Robert Bocchinobb90a7a2006-01-10 20:03:46 +00001761 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
1762 return FC; // Fold a few common cases...
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001763 // Look up the constant in the table first to ensure uniqueness
1764 std::vector<Constant*> ArgVec(1, Val);
1765 ArgVec.push_back(Idx);
1766 const ExprMapKeyType &Key = std::make_pair(Instruction::ExtractElement,ArgVec);
Chris Lattner8a94bf12006-09-28 00:35:06 +00001767 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001768}
1769
1770Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
1771 assert(isa<PackedType>(Val->getType()) &&
1772 "Tried to create extractelement operation on non-packed type!");
1773 assert(Idx->getType() == Type::UIntTy &&
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001774 "Extractelement index must be uint type!");
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001775 return getExtractElementTy(cast<PackedType>(Val->getType())->getElementType(),
1776 Val, Idx);
1777}
Chris Lattnered468e372003-10-05 00:17:43 +00001778
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001779Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
1780 Constant *Elt, Constant *Idx) {
1781 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
1782 return FC; // Fold a few common cases...
1783 // Look up the constant in the table first to ensure uniqueness
1784 std::vector<Constant*> ArgVec(1, Val);
1785 ArgVec.push_back(Elt);
1786 ArgVec.push_back(Idx);
1787 const ExprMapKeyType &Key = std::make_pair(Instruction::InsertElement,ArgVec);
Chris Lattner8a94bf12006-09-28 00:35:06 +00001788 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001789}
1790
1791Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1792 Constant *Idx) {
1793 assert(isa<PackedType>(Val->getType()) &&
1794 "Tried to create insertelement operation on non-packed type!");
1795 assert(Elt->getType() == cast<PackedType>(Val->getType())->getElementType()
1796 && "Insertelement types must match!");
1797 assert(Idx->getType() == Type::UIntTy &&
1798 "Insertelement index must be uint type!");
1799 return getInsertElementTy(cast<PackedType>(Val->getType())->getElementType(),
1800 Val, Elt, Idx);
1801}
1802
Chris Lattner00f10232006-04-08 01:18:18 +00001803Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
1804 Constant *V2, Constant *Mask) {
1805 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
1806 return FC; // Fold a few common cases...
1807 // Look up the constant in the table first to ensure uniqueness
1808 std::vector<Constant*> ArgVec(1, V1);
1809 ArgVec.push_back(V2);
1810 ArgVec.push_back(Mask);
1811 const ExprMapKeyType &Key = std::make_pair(Instruction::ShuffleVector,ArgVec);
Chris Lattner8a94bf12006-09-28 00:35:06 +00001812 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner00f10232006-04-08 01:18:18 +00001813}
1814
1815Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
1816 Constant *Mask) {
1817 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
1818 "Invalid shuffle vector constant expr operands!");
1819 return getShuffleVectorTy(V1->getType(), V1, V2, Mask);
1820}
1821
1822
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001823// destroyConstant - Remove the constant from the constant table...
1824//
1825void ConstantExpr::destroyConstant() {
Chris Lattner8a94bf12006-09-28 00:35:06 +00001826 ExprConstants->remove(this);
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001827 destroyConstantImpl();
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001828}
1829
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001830const char *ConstantExpr::getOpcodeName() const {
1831 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001832}
Reid Spencer1c9c8e62004-07-17 23:48:33 +00001833
Chris Lattner5cbade92005-10-03 21:58:36 +00001834//===----------------------------------------------------------------------===//
1835// replaceUsesOfWithOnConstant implementations
1836
1837void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00001838 Use *U) {
Chris Lattner5cbade92005-10-03 21:58:36 +00001839 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattnerc182a882005-10-04 01:17:50 +00001840 Constant *ToC = cast<Constant>(To);
Chris Lattner23ec01f2005-10-04 18:47:09 +00001841
1842 unsigned OperandToUpdate = U-OperandList;
1843 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1844
Jim Laskeyede5aa42006-07-17 17:38:29 +00001845 std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnercea141f2005-10-03 22:51:37 +00001846 Lookup.first.first = getType();
1847 Lookup.second = this;
Chris Lattner23ec01f2005-10-04 18:47:09 +00001848
Chris Lattnercea141f2005-10-03 22:51:37 +00001849 std::vector<Constant*> &Values = Lookup.first.second;
1850 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattner23ec01f2005-10-04 18:47:09 +00001851
Chris Lattnerc182a882005-10-04 01:17:50 +00001852 // Fill values with the modified operands of the constant array. Also,
1853 // compute whether this turns into an all-zeros array.
Chris Lattner23ec01f2005-10-04 18:47:09 +00001854 bool isAllZeros = false;
1855 if (!ToC->isNullValue()) {
1856 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1857 Values.push_back(cast<Constant>(O->get()));
1858 } else {
1859 isAllZeros = true;
1860 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1861 Constant *Val = cast<Constant>(O->get());
1862 Values.push_back(Val);
1863 if (isAllZeros) isAllZeros = Val->isNullValue();
1864 }
Chris Lattner5cbade92005-10-03 21:58:36 +00001865 }
Chris Lattner23ec01f2005-10-04 18:47:09 +00001866 Values[OperandToUpdate] = ToC;
Chris Lattner5cbade92005-10-03 21:58:36 +00001867
Chris Lattnercea141f2005-10-03 22:51:37 +00001868 Constant *Replacement = 0;
1869 if (isAllZeros) {
1870 Replacement = ConstantAggregateZero::get(getType());
1871 } else {
1872 // Check to see if we have this array type already.
1873 bool Exists;
Jim Laskeyede5aa42006-07-17 17:38:29 +00001874 ArrayConstantsTy::MapTy::iterator I =
Chris Lattner8a94bf12006-09-28 00:35:06 +00001875 ArrayConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattnercea141f2005-10-03 22:51:37 +00001876
1877 if (Exists) {
1878 Replacement = I->second;
1879 } else {
1880 // Okay, the new shape doesn't exist in the system yet. Instead of
1881 // creating a new constant array, inserting it, replaceallusesof'ing the
1882 // old with the new, then deleting the old... just update the current one
1883 // in place!
Chris Lattner8a94bf12006-09-28 00:35:06 +00001884 ArrayConstants->MoveConstantToNewSlot(this, I);
Chris Lattnercea141f2005-10-03 22:51:37 +00001885
Chris Lattner23ec01f2005-10-04 18:47:09 +00001886 // Update to the new value.
1887 setOperand(OperandToUpdate, ToC);
Chris Lattnercea141f2005-10-03 22:51:37 +00001888 return;
1889 }
1890 }
1891
1892 // Otherwise, I do need to replace this with an existing value.
Chris Lattner5cbade92005-10-03 21:58:36 +00001893 assert(Replacement != this && "I didn't contain From!");
1894
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00001895 // Everyone using this now uses the replacement.
1896 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattner5cbade92005-10-03 21:58:36 +00001897
1898 // Delete the old constant!
1899 destroyConstant();
1900}
1901
1902void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00001903 Use *U) {
Chris Lattner5cbade92005-10-03 21:58:36 +00001904 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattnerc182a882005-10-04 01:17:50 +00001905 Constant *ToC = cast<Constant>(To);
1906
Chris Lattner23ec01f2005-10-04 18:47:09 +00001907 unsigned OperandToUpdate = U-OperandList;
1908 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1909
Jim Laskeyede5aa42006-07-17 17:38:29 +00001910 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnerc182a882005-10-04 01:17:50 +00001911 Lookup.first.first = getType();
1912 Lookup.second = this;
1913 std::vector<Constant*> &Values = Lookup.first.second;
1914 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattner5cbade92005-10-03 21:58:36 +00001915
Chris Lattner23ec01f2005-10-04 18:47:09 +00001916
Chris Lattnerc182a882005-10-04 01:17:50 +00001917 // Fill values with the modified operands of the constant struct. Also,
1918 // compute whether this turns into an all-zeros struct.
Chris Lattner23ec01f2005-10-04 18:47:09 +00001919 bool isAllZeros = false;
1920 if (!ToC->isNullValue()) {
1921 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1922 Values.push_back(cast<Constant>(O->get()));
1923 } else {
1924 isAllZeros = true;
1925 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1926 Constant *Val = cast<Constant>(O->get());
1927 Values.push_back(Val);
1928 if (isAllZeros) isAllZeros = Val->isNullValue();
1929 }
Chris Lattnerc182a882005-10-04 01:17:50 +00001930 }
Chris Lattner23ec01f2005-10-04 18:47:09 +00001931 Values[OperandToUpdate] = ToC;
1932
Chris Lattnerc182a882005-10-04 01:17:50 +00001933 Constant *Replacement = 0;
1934 if (isAllZeros) {
1935 Replacement = ConstantAggregateZero::get(getType());
1936 } else {
1937 // Check to see if we have this array type already.
1938 bool Exists;
Jim Laskeyede5aa42006-07-17 17:38:29 +00001939 StructConstantsTy::MapTy::iterator I =
Chris Lattner8a94bf12006-09-28 00:35:06 +00001940 StructConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattnerc182a882005-10-04 01:17:50 +00001941
1942 if (Exists) {
1943 Replacement = I->second;
1944 } else {
1945 // Okay, the new shape doesn't exist in the system yet. Instead of
1946 // creating a new constant struct, inserting it, replaceallusesof'ing the
1947 // old with the new, then deleting the old... just update the current one
1948 // in place!
Chris Lattner8a94bf12006-09-28 00:35:06 +00001949 StructConstants->MoveConstantToNewSlot(this, I);
Chris Lattnerc182a882005-10-04 01:17:50 +00001950
Chris Lattner23ec01f2005-10-04 18:47:09 +00001951 // Update to the new value.
1952 setOperand(OperandToUpdate, ToC);
Chris Lattnerc182a882005-10-04 01:17:50 +00001953 return;
1954 }
Chris Lattner5cbade92005-10-03 21:58:36 +00001955 }
1956
Chris Lattner5cbade92005-10-03 21:58:36 +00001957 assert(Replacement != this && "I didn't contain From!");
1958
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00001959 // Everyone using this now uses the replacement.
1960 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattner5cbade92005-10-03 21:58:36 +00001961
1962 // Delete the old constant!
1963 destroyConstant();
1964}
1965
1966void ConstantPacked::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00001967 Use *U) {
Chris Lattner5cbade92005-10-03 21:58:36 +00001968 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
1969
1970 std::vector<Constant*> Values;
1971 Values.reserve(getNumOperands()); // Build replacement array...
1972 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1973 Constant *Val = getOperand(i);
1974 if (Val == From) Val = cast<Constant>(To);
1975 Values.push_back(Val);
1976 }
1977
1978 Constant *Replacement = ConstantPacked::get(getType(), Values);
1979 assert(Replacement != this && "I didn't contain From!");
1980
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00001981 // Everyone using this now uses the replacement.
1982 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattner5cbade92005-10-03 21:58:36 +00001983
1984 // Delete the old constant!
1985 destroyConstant();
1986}
1987
1988void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00001989 Use *U) {
Chris Lattner5cbade92005-10-03 21:58:36 +00001990 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
1991 Constant *To = cast<Constant>(ToV);
1992
1993 Constant *Replacement = 0;
1994 if (getOpcode() == Instruction::GetElementPtr) {
1995 std::vector<Constant*> Indices;
1996 Constant *Pointer = getOperand(0);
1997 Indices.reserve(getNumOperands()-1);
1998 if (Pointer == From) Pointer = To;
1999
2000 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2001 Constant *Val = getOperand(i);
2002 if (Val == From) Val = To;
2003 Indices.push_back(Val);
2004 }
2005 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
Reid Spencer3da59db2006-11-27 01:05:10 +00002006 } else if (isCast()) {
Chris Lattner5cbade92005-10-03 21:58:36 +00002007 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer3da59db2006-11-27 01:05:10 +00002008 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattner5cbade92005-10-03 21:58:36 +00002009 } else if (getOpcode() == Instruction::Select) {
2010 Constant *C1 = getOperand(0);
2011 Constant *C2 = getOperand(1);
2012 Constant *C3 = getOperand(2);
2013 if (C1 == From) C1 = To;
2014 if (C2 == From) C2 = To;
2015 if (C3 == From) C3 = To;
2016 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00002017 } else if (getOpcode() == Instruction::ExtractElement) {
2018 Constant *C1 = getOperand(0);
2019 Constant *C2 = getOperand(1);
2020 if (C1 == From) C1 = To;
2021 if (C2 == From) C2 = To;
2022 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattner42b55802006-04-08 05:09:48 +00002023 } else if (getOpcode() == Instruction::InsertElement) {
2024 Constant *C1 = getOperand(0);
2025 Constant *C2 = getOperand(1);
2026 Constant *C3 = getOperand(1);
2027 if (C1 == From) C1 = To;
2028 if (C2 == From) C2 = To;
2029 if (C3 == From) C3 = To;
2030 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2031 } else if (getOpcode() == Instruction::ShuffleVector) {
2032 Constant *C1 = getOperand(0);
2033 Constant *C2 = getOperand(1);
2034 Constant *C3 = getOperand(2);
2035 if (C1 == From) C1 = To;
2036 if (C2 == From) C2 = To;
2037 if (C3 == From) C3 = To;
2038 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Chris Lattner5cbade92005-10-03 21:58:36 +00002039 } else if (getNumOperands() == 2) {
2040 Constant *C1 = getOperand(0);
2041 Constant *C2 = getOperand(1);
2042 if (C1 == From) C1 = To;
2043 if (C2 == From) C2 = To;
2044 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2045 } else {
2046 assert(0 && "Unknown ConstantExpr type!");
2047 return;
2048 }
2049
2050 assert(Replacement != this && "I didn't contain From!");
2051
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002052 // Everyone using this now uses the replacement.
2053 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattner5cbade92005-10-03 21:58:36 +00002054
2055 // Delete the old constant!
2056 destroyConstant();
2057}
2058
2059
Jim Laskey21b6c9d2006-03-08 18:11:07 +00002060/// getStringValue - Turn an LLVM constant pointer that eventually points to a
2061/// global into a string value. Return an empty string if we can't do it.
Evan Cheng09371032006-03-10 23:52:03 +00002062/// Parameter Chop determines if the result is chopped at the first null
2063/// terminator.
Jim Laskey21b6c9d2006-03-08 18:11:07 +00002064///
Evan Cheng09371032006-03-10 23:52:03 +00002065std::string Constant::getStringValue(bool Chop, unsigned Offset) {
Jim Laskey21b6c9d2006-03-08 18:11:07 +00002066 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(this)) {
2067 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
2068 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
2069 if (Init->isString()) {
2070 std::string Result = Init->getAsString();
2071 if (Offset < Result.size()) {
2072 // If we are pointing INTO The string, erase the beginning...
2073 Result.erase(Result.begin(), Result.begin()+Offset);
2074
2075 // Take off the null terminator, and any string fragments after it.
Evan Cheng09371032006-03-10 23:52:03 +00002076 if (Chop) {
2077 std::string::size_type NullPos = Result.find_first_of((char)0);
2078 if (NullPos != std::string::npos)
2079 Result.erase(Result.begin()+NullPos, Result.end());
2080 }
Jim Laskey21b6c9d2006-03-08 18:11:07 +00002081 return Result;
2082 }
2083 }
2084 }
2085 } else if (Constant *C = dyn_cast<Constant>(this)) {
2086 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
Evan Cheng2352ce92006-03-11 00:13:10 +00002087 return GV->getStringValue(Chop, Offset);
Jim Laskey21b6c9d2006-03-08 18:11:07 +00002088 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
2089 if (CE->getOpcode() == Instruction::GetElementPtr) {
2090 // Turn a gep into the specified offset.
2091 if (CE->getNumOperands() == 3 &&
2092 cast<Constant>(CE->getOperand(1))->isNullValue() &&
2093 isa<ConstantInt>(CE->getOperand(2))) {
Reid Spencerb83eb642006-10-20 07:07:24 +00002094 Offset += cast<ConstantInt>(CE->getOperand(2))->getZExtValue();
Evan Cheng2352ce92006-03-11 00:13:10 +00002095 return CE->getOperand(0)->getStringValue(Chop, Offset);
Jim Laskey21b6c9d2006-03-08 18:11:07 +00002096 }
2097 }
2098 }
2099 }
2100 return "";
2101}
2102