blob: 56219e27567a591527e7979946337dfb8392b50c [file] [log] [blame]
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001//===-- Constants.cpp - Implement Constant nodes --------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner3462ae32001-12-03 22:26:30 +000010// This file implements the Constant* classes...
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerca142372002-04-28 19:55:58 +000014#include "llvm/Constants.h"
Chris Lattner5a945e32004-01-12 21:13:12 +000015#include "ConstantFolding.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000016#include "llvm/DerivedTypes.h"
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000017#include "llvm/GlobalValue.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000018#include "llvm/Instructions.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000019#include "llvm/SymbolTable.h"
Chris Lattnerd7a73302001-10-13 06:57:33 +000020#include "llvm/Module.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000021#include "llvm/ADT/StringExtras.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000022#include "llvm/Support/Compiler.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000023#include "llvm/Support/Debug.h"
Chris Lattner69edc982006-09-28 00:35:06 +000024#include "llvm/Support/ManagedStatic.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000025#include "llvm/Support/MathExtras.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000026#include <algorithm>
Chris Lattner189d19f2003-11-21 20:23:48 +000027using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000028
Chris Lattner2f7c9632001-06-06 20:29:01 +000029//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000030// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000031//===----------------------------------------------------------------------===//
32
Chris Lattner3462ae32001-12-03 22:26:30 +000033void Constant::destroyConstantImpl() {
34 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000035 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000036 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-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 Lattner3462ae32001-12-03 22:26:30 +000039 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000040 //
41 while (!use_empty()) {
42 Value *V = use_back();
43#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000044 if (!isa<Constant>(V))
Bill Wendling6a462f12006-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 Lattnerd7a73302001-10-13 06:57:33 +000048#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000049 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000050 Constant *CV = cast<Constant>(V);
51 CV->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +000052
53 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000054 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000055 }
56
57 // Value has no outstanding references it is safe to delete it now...
58 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000059}
Chris Lattner2f7c9632001-06-06 20:29:01 +000060
Chris Lattner23dd1f62006-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 Spencer7e80b0b2006-10-26 06:15:43 +000078 case Instruction::UDiv:
79 case Instruction::SDiv:
80 case Instruction::FDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +000081 case Instruction::URem:
82 case Instruction::SRem:
83 case Instruction::FRem:
Chris Lattner23dd1f62006-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 Lattnerb1585a92002-08-13 17:50:20 +000092// Static constructor to create a '0' constant of arbitrary type...
93Constant *Constant::getNullValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +000094 switch (Ty->getTypeID()) {
Chris Lattner3e88ef92003-10-03 19:34:51 +000095 case Type::BoolTyID: {
96 static Constant *NullBool = ConstantBool::get(false);
97 return NullBool;
98 }
Reid Spencer8d9336d2006-12-31 05:26:44 +000099 case Type::Int8TyID: {
100 static Constant *NullInt8 = ConstantInt::get(Type::Int8Ty, 0);
101 return NullInt8;
Chris Lattner3e88ef92003-10-03 19:34:51 +0000102 }
Reid Spencer8d9336d2006-12-31 05:26:44 +0000103 case Type::Int16TyID: {
104 static Constant *NullInt16 = ConstantInt::get(Type::Int16Ty, 0);
105 return NullInt16;
Chris Lattner3e88ef92003-10-03 19:34:51 +0000106 }
Reid Spencer8d9336d2006-12-31 05:26:44 +0000107 case Type::Int32TyID: {
108 static Constant *NullInt32 = ConstantInt::get(Type::Int32Ty, 0);
109 return NullInt32;
Chris Lattner3e88ef92003-10-03 19:34:51 +0000110 }
Reid Spencer8d9336d2006-12-31 05:26:44 +0000111 case Type::Int64TyID: {
112 static Constant *NullInt64 = ConstantInt::get(Type::Int64Ty, 0);
113 return NullInt64;
Chris Lattner3e88ef92003-10-03 19:34:51 +0000114 }
Chris Lattner3e88ef92003-10-03 19:34:51 +0000115 case Type::FloatTyID: {
116 static Constant *NullFloat = ConstantFP::get(Type::FloatTy, 0);
117 return NullFloat;
118 }
119 case Type::DoubleTyID: {
120 static Constant *NullDouble = ConstantFP::get(Type::DoubleTy, 0);
121 return NullDouble;
122 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000123 case Type::PointerTyID:
Chris Lattnerb1585a92002-08-13 17:50:20 +0000124 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner9fba3da2004-02-15 05:53:04 +0000125 case Type::StructTyID:
126 case Type::ArrayTyID:
Brian Gaeke02209042004-08-20 06:00:58 +0000127 case Type::PackedTyID:
Chris Lattner9fba3da2004-02-15 05:53:04 +0000128 return ConstantAggregateZero::get(Ty);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000129 default:
Reid Spencercf394bf2004-07-04 11:51:24 +0000130 // Function, Label, or Opaque type?
131 assert(!"Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000132 return 0;
133 }
134}
135
Chris Lattnerb1585a92002-08-13 17:50:20 +0000136
137// Static constructor to create an integral constant with all bits set
138ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000139 switch (Ty->getTypeID()) {
Chris Lattnera84df0a22006-09-28 23:36:21 +0000140 case Type::BoolTyID: return ConstantBool::getTrue();
Reid Spencer8d9336d2006-12-31 05:26:44 +0000141 case Type::Int8TyID:
142 case Type::Int16TyID:
143 case Type::Int32TyID:
144 case Type::Int64TyID: return ConstantInt::get(Ty, int64_t(-1));
Chris Lattner31408f72002-08-14 17:12:13 +0000145 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000146 }
147}
148
Chris Lattner2f7c9632001-06-06 20:29:01 +0000149//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +0000150// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +0000151//===----------------------------------------------------------------------===//
152
153//===----------------------------------------------------------------------===//
154// Normal Constructors
155
Chris Lattnere7e139e2005-09-27 06:09:08 +0000156ConstantIntegral::ConstantIntegral(const Type *Ty, ValueTy VT, uint64_t V)
Reid Spencere0fc4df2006-10-20 07:07:24 +0000157 : Constant(Ty, VT, 0, 0), Val(V) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000158}
Chris Lattner49d855c2001-09-07 16:46:31 +0000159
Chris Lattnere7e139e2005-09-27 06:09:08 +0000160ConstantBool::ConstantBool(bool V)
Reid Spencere0fc4df2006-10-20 07:07:24 +0000161 : ConstantIntegral(Type::BoolTy, ConstantBoolVal, uint64_t(V)) {
Chris Lattner265eb642004-06-21 12:12:12 +0000162}
163
Reid Spencere0fc4df2006-10-20 07:07:24 +0000164ConstantInt::ConstantInt(const Type *Ty, uint64_t V)
165 : ConstantIntegral(Ty, ConstantIntVal, V) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000166}
167
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000168ConstantFP::ConstantFP(const Type *Ty, double V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000169 : Constant(Ty, ConstantFPVal, 0, 0) {
Chris Lattner9655e542001-07-20 19:16:02 +0000170 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000171 Val = V;
172}
173
Chris Lattner3462ae32001-12-03 22:26:30 +0000174ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000175 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000176 : Constant(T, ConstantArrayVal, new Use[V.size()], V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000177 assert(V.size() == T->getNumElements() &&
178 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000179 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000180 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
181 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000182 Constant *C = *I;
183 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000184 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000185 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000186 "Initializer for array element doesn't match array element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000187 OL->init(C, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000188 }
189}
190
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000191ConstantArray::~ConstantArray() {
192 delete [] OperandList;
193}
194
Chris Lattner3462ae32001-12-03 22:26:30 +0000195ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000196 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000197 : Constant(T, ConstantStructVal, new Use[V.size()], V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000198 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000199 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000200 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000201 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
202 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000203 Constant *C = *I;
204 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000205 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000206 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000207 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000208 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000209 "Initializer for struct element doesn't match struct element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000210 OL->init(C, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000211 }
212}
213
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000214ConstantStruct::~ConstantStruct() {
215 delete [] OperandList;
216}
217
218
Brian Gaeke02209042004-08-20 06:00:58 +0000219ConstantPacked::ConstantPacked(const PackedType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000220 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000221 : Constant(T, ConstantPackedVal, new Use[V.size()], V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000222 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000223 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
224 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000225 Constant *C = *I;
226 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000227 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000228 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000229 "Initializer for packed element doesn't match packed element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000230 OL->init(C, this);
Brian Gaeke02209042004-08-20 06:00:58 +0000231 }
232}
233
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000234ConstantPacked::~ConstantPacked() {
235 delete [] OperandList;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000236}
237
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000238// We declare several classes private to this file, so use an anonymous
239// namespace
240namespace {
241
242/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
243/// behind the scenes to implement unary constant exprs.
244class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
245 Use Op;
246public:
247 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
248 : ConstantExpr(Ty, Opcode, &Op, 1), Op(C, this) {}
249};
250
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000251/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
252/// behind the scenes to implement binary constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000253class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000254 Use Ops[2];
255public:
256 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Reid Spencer266e42b2006-12-23 06:05:41 +0000257 : ConstantExpr(C1->getType(), Opcode, Ops, 2) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000258 Ops[0].init(C1, this);
259 Ops[1].init(C2, this);
260 }
261};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000262
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000263/// SelectConstantExpr - This class is private to Constants.cpp, and is used
264/// behind the scenes to implement select constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000265class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000266 Use Ops[3];
267public:
268 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
269 : ConstantExpr(C2->getType(), Instruction::Select, Ops, 3) {
270 Ops[0].init(C1, this);
271 Ops[1].init(C2, this);
272 Ops[2].init(C3, this);
273 }
274};
275
Robert Bocchinoca27f032006-01-17 20:07:22 +0000276/// ExtractElementConstantExpr - This class is private to
277/// Constants.cpp, and is used behind the scenes to implement
278/// extractelement constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000279class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Robert Bocchino23004482006-01-10 19:05:34 +0000280 Use Ops[2];
281public:
282 ExtractElementConstantExpr(Constant *C1, Constant *C2)
283 : ConstantExpr(cast<PackedType>(C1->getType())->getElementType(),
284 Instruction::ExtractElement, Ops, 2) {
285 Ops[0].init(C1, this);
286 Ops[1].init(C2, this);
287 }
288};
289
Robert Bocchinoca27f032006-01-17 20:07:22 +0000290/// InsertElementConstantExpr - This class is private to
291/// Constants.cpp, and is used behind the scenes to implement
292/// insertelement constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000293class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Robert Bocchinoca27f032006-01-17 20:07:22 +0000294 Use Ops[3];
295public:
296 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
297 : ConstantExpr(C1->getType(), Instruction::InsertElement,
298 Ops, 3) {
299 Ops[0].init(C1, this);
300 Ops[1].init(C2, this);
301 Ops[2].init(C3, this);
302 }
303};
304
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000305/// ShuffleVectorConstantExpr - This class is private to
306/// Constants.cpp, and is used behind the scenes to implement
307/// shufflevector constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000308class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000309 Use Ops[3];
310public:
311 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
312 : ConstantExpr(C1->getType(), Instruction::ShuffleVector,
313 Ops, 3) {
314 Ops[0].init(C1, this);
315 Ops[1].init(C2, this);
316 Ops[2].init(C3, this);
317 }
318};
319
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000320/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
321/// used behind the scenes to implement getelementpr constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000322struct VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000323 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
324 const Type *DestTy)
325 : ConstantExpr(DestTy, Instruction::GetElementPtr,
326 new Use[IdxList.size()+1], IdxList.size()+1) {
327 OperandList[0].init(C, this);
328 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
329 OperandList[i+1].init(IdxList[i], this);
330 }
331 ~GetElementPtrConstantExpr() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000332 delete [] OperandList;
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000333 }
334};
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000335
336// CompareConstantExpr - This class is private to Constants.cpp, and is used
337// behind the scenes to implement ICmp and FCmp constant expressions. This is
338// needed in order to store the predicate value for these instructions.
339struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr {
340 unsigned short predicate;
341 Use Ops[2];
342 CompareConstantExpr(Instruction::OtherOps opc, unsigned short pred,
343 Constant* LHS, Constant* RHS)
Reid Spencerfcb0dd32006-12-07 04:18:31 +0000344 : ConstantExpr(Type::BoolTy, opc, Ops, 2), predicate(pred) {
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000345 OperandList[0].init(LHS, this);
346 OperandList[1].init(RHS, this);
347 }
348};
349
350} // end anonymous namespace
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000351
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000352
353// Utility function for determining if a ConstantExpr is a CastOp or not. This
354// can't be inline because we don't want to #include Instruction.h into
355// Constant.h
356bool ConstantExpr::isCast() const {
357 return Instruction::isCast(getOpcode());
358}
359
Reid Spenceree3c9912006-12-04 05:19:50 +0000360bool ConstantExpr::isCompare() const {
361 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
362}
363
Chris Lattner817175f2004-03-29 02:37:53 +0000364/// ConstantExpr::get* - Return some common constants without having to
365/// specify the full Instruction::OPCODE identifier.
366///
367Constant *ConstantExpr::getNeg(Constant *C) {
Chris Lattner3cdc27c2004-03-29 19:51:24 +0000368 if (!C->getType()->isFloatingPoint())
369 return get(Instruction::Sub, getNullValue(C->getType()), C);
370 else
371 return get(Instruction::Sub, ConstantFP::get(C->getType(), -0.0), C);
Chris Lattner817175f2004-03-29 02:37:53 +0000372}
373Constant *ConstantExpr::getNot(Constant *C) {
374 assert(isa<ConstantIntegral>(C) && "Cannot NOT a nonintegral type!");
375 return get(Instruction::Xor, C,
376 ConstantIntegral::getAllOnesValue(C->getType()));
377}
378Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
379 return get(Instruction::Add, C1, C2);
380}
381Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
382 return get(Instruction::Sub, C1, C2);
383}
384Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
385 return get(Instruction::Mul, C1, C2);
386}
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000387Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2) {
388 return get(Instruction::UDiv, C1, C2);
389}
390Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2) {
391 return get(Instruction::SDiv, C1, C2);
392}
393Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) {
394 return get(Instruction::FDiv, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000395}
Reid Spencer7eb55b32006-11-02 01:53:59 +0000396Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) {
397 return get(Instruction::URem, C1, C2);
398}
399Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) {
400 return get(Instruction::SRem, C1, C2);
401}
402Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) {
403 return get(Instruction::FRem, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000404}
405Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
406 return get(Instruction::And, C1, C2);
407}
408Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
409 return get(Instruction::Or, C1, C2);
410}
411Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
412 return get(Instruction::Xor, C1, C2);
413}
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000414unsigned ConstantExpr::getPredicate() const {
415 assert(getOpcode() == Instruction::FCmp || getOpcode() == Instruction::ICmp);
416 return dynamic_cast<const CompareConstantExpr*>(this)->predicate;
417}
Chris Lattner817175f2004-03-29 02:37:53 +0000418Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
419 return get(Instruction::Shl, C1, C2);
420}
Reid Spencerfdff9382006-11-08 06:47:33 +0000421Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2) {
422 return get(Instruction::LShr, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000423}
Reid Spencerfdff9382006-11-08 06:47:33 +0000424Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2) {
425 return get(Instruction::AShr, C1, C2);
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000426}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000427
Chris Lattner7c1018a2006-07-14 19:37:40 +0000428/// getWithOperandReplaced - Return a constant expression identical to this
429/// one, but with the specified operand set to the specified value.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000430Constant *
431ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +0000432 assert(OpNo < getNumOperands() && "Operand num is out of range!");
433 assert(Op->getType() == getOperand(OpNo)->getType() &&
434 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000435 if (getOperand(OpNo) == Op)
436 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000437
Chris Lattner227816342006-07-14 22:20:01 +0000438 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000439 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000440 case Instruction::Trunc:
441 case Instruction::ZExt:
442 case Instruction::SExt:
443 case Instruction::FPTrunc:
444 case Instruction::FPExt:
445 case Instruction::UIToFP:
446 case Instruction::SIToFP:
447 case Instruction::FPToUI:
448 case Instruction::FPToSI:
449 case Instruction::PtrToInt:
450 case Instruction::IntToPtr:
451 case Instruction::BitCast:
452 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000453 case Instruction::Select:
454 Op0 = (OpNo == 0) ? Op : getOperand(0);
455 Op1 = (OpNo == 1) ? Op : getOperand(1);
456 Op2 = (OpNo == 2) ? Op : getOperand(2);
457 return ConstantExpr::getSelect(Op0, Op1, Op2);
458 case Instruction::InsertElement:
459 Op0 = (OpNo == 0) ? Op : getOperand(0);
460 Op1 = (OpNo == 1) ? Op : getOperand(1);
461 Op2 = (OpNo == 2) ? Op : getOperand(2);
462 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
463 case Instruction::ExtractElement:
464 Op0 = (OpNo == 0) ? Op : getOperand(0);
465 Op1 = (OpNo == 1) ? Op : getOperand(1);
466 return ConstantExpr::getExtractElement(Op0, Op1);
467 case Instruction::ShuffleVector:
468 Op0 = (OpNo == 0) ? Op : getOperand(0);
469 Op1 = (OpNo == 1) ? Op : getOperand(1);
470 Op2 = (OpNo == 2) ? Op : getOperand(2);
471 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000472 case Instruction::GetElementPtr: {
473 std::vector<Constant*> Ops;
474 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
475 Ops.push_back(getOperand(i));
476 if (OpNo == 0)
477 return ConstantExpr::getGetElementPtr(Op, Ops);
478 Ops[OpNo-1] = Op;
479 return ConstantExpr::getGetElementPtr(getOperand(0), Ops);
480 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000481 default:
482 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000483 Op0 = (OpNo == 0) ? Op : getOperand(0);
484 Op1 = (OpNo == 1) ? Op : getOperand(1);
485 return ConstantExpr::get(getOpcode(), Op0, Op1);
486 }
487}
488
489/// getWithOperands - This returns the current constant expression with the
490/// operands replaced with the specified values. The specified operands must
491/// match count and type with the existing ones.
492Constant *ConstantExpr::
493getWithOperands(const std::vector<Constant*> &Ops) const {
494 assert(Ops.size() == getNumOperands() && "Operand count mismatch!");
495 bool AnyChange = false;
496 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
497 assert(Ops[i]->getType() == getOperand(i)->getType() &&
498 "Operand type mismatch!");
499 AnyChange |= Ops[i] != getOperand(i);
500 }
501 if (!AnyChange) // No operands changed, return self.
502 return const_cast<ConstantExpr*>(this);
503
504 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000505 case Instruction::Trunc:
506 case Instruction::ZExt:
507 case Instruction::SExt:
508 case Instruction::FPTrunc:
509 case Instruction::FPExt:
510 case Instruction::UIToFP:
511 case Instruction::SIToFP:
512 case Instruction::FPToUI:
513 case Instruction::FPToSI:
514 case Instruction::PtrToInt:
515 case Instruction::IntToPtr:
516 case Instruction::BitCast:
517 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattner227816342006-07-14 22:20:01 +0000518 case Instruction::Select:
519 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
520 case Instruction::InsertElement:
521 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
522 case Instruction::ExtractElement:
523 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
524 case Instruction::ShuffleVector:
525 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
526 case Instruction::GetElementPtr: {
527 std::vector<Constant*> ActualOps(Ops.begin()+1, Ops.end());
528 return ConstantExpr::getGetElementPtr(Ops[0], ActualOps);
529 }
Reid Spencer266e42b2006-12-23 06:05:41 +0000530 case Instruction::ICmp:
531 case Instruction::FCmp:
532 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattner227816342006-07-14 22:20:01 +0000533 default:
534 assert(getNumOperands() == 2 && "Must be binary operator?");
535 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000536 }
537}
538
Chris Lattner2f7c9632001-06-06 20:29:01 +0000539
540//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000541// isValueValidForType implementations
542
Reid Spencere7334722006-12-19 01:28:19 +0000543bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
544 switch (Ty->getTypeID()) {
545 default: return false; // These can't be represented as integers!
Reid Spencer8d9336d2006-12-31 05:26:44 +0000546 case Type::Int8TyID: return Val <= UINT8_MAX;
547 case Type::Int16TyID: return Val <= UINT16_MAX;
548 case Type::Int32TyID: return Val <= UINT32_MAX;
549 case Type::Int64TyID: return true; // always true, has to fit in largest type
Reid Spencere7334722006-12-19 01:28:19 +0000550 }
551}
552
Reid Spencere0fc4df2006-10-20 07:07:24 +0000553bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000554 switch (Ty->getTypeID()) {
Reid Spencere7334722006-12-19 01:28:19 +0000555 default: return false; // These can't be represented as integers!
Reid Spencer8d9336d2006-12-31 05:26:44 +0000556 case Type::Int8TyID: return (Val >= INT8_MIN && Val <= INT8_MAX);
557 case Type::Int16TyID: return (Val >= INT16_MIN && Val <= UINT16_MAX);
558 case Type::Int32TyID: return (Val >= INT32_MIN && Val <= UINT32_MAX);
559 case Type::Int64TyID: return true; // always true, has to fit in largest type
Chris Lattner2f7c9632001-06-06 20:29:01 +0000560 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000561}
562
Chris Lattner3462ae32001-12-03 22:26:30 +0000563bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000564 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000565 default:
566 return false; // These can't be represented as floating point!
567
Reid Spencerb95f8ab2004-12-07 07:38:08 +0000568 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000569 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000570 case Type::DoubleTyID:
571 return true; // This is the largest type...
572 }
Chris Lattneraa2372562006-05-24 17:04:05 +0000573}
Chris Lattner9655e542001-07-20 19:16:02 +0000574
Chris Lattner49d855c2001-09-07 16:46:31 +0000575//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000576// Factory Function Implementation
577
Chris Lattner98fa07b2003-05-23 20:03:32 +0000578// ConstantCreator - A class that is used to create constants by
579// ValueMap*. This class should be partially specialized if there is
580// something strange that needs to be done to interface to the ctor for the
581// constant.
582//
Chris Lattner189d19f2003-11-21 20:23:48 +0000583namespace llvm {
584 template<class ConstantClass, class TypeClass, class ValType>
Chris Lattner02157b02006-06-28 21:38:54 +0000585 struct VISIBILITY_HIDDEN ConstantCreator {
Chris Lattner189d19f2003-11-21 20:23:48 +0000586 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
587 return new ConstantClass(Ty, V);
588 }
589 };
Misha Brukmanb1c93172005-04-21 23:48:37 +0000590
Chris Lattner189d19f2003-11-21 20:23:48 +0000591 template<class ConstantClass, class TypeClass>
Chris Lattner02157b02006-06-28 21:38:54 +0000592 struct VISIBILITY_HIDDEN ConvertConstantType {
Chris Lattner189d19f2003-11-21 20:23:48 +0000593 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
594 assert(0 && "This type cannot be converted!\n");
595 abort();
596 }
597 };
Chris Lattnerb50d1352003-10-05 00:17:43 +0000598
Chris Lattner935aa922005-10-04 17:48:46 +0000599 template<class ValType, class TypeClass, class ConstantClass,
600 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattner02157b02006-06-28 21:38:54 +0000601 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
Chris Lattnerb64419a2005-10-03 22:51:37 +0000602 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000603 typedef std::pair<const Type*, ValType> MapKey;
604 typedef std::map<MapKey, Constant *> MapTy;
605 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
606 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
Chris Lattnerb64419a2005-10-03 22:51:37 +0000607 private:
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000608 /// Map - This is the main map from the element descriptor to the Constants.
609 /// This is the primary way we avoid creating two of the same shape
610 /// constant.
Chris Lattnerb50d1352003-10-05 00:17:43 +0000611 MapTy Map;
Chris Lattner935aa922005-10-04 17:48:46 +0000612
613 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
614 /// from the constants to their element in Map. This is important for
615 /// removal of constants from the array, which would otherwise have to scan
616 /// through the map with very large keys.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000617 InverseMapTy InverseMap;
Chris Lattnerb50d1352003-10-05 00:17:43 +0000618
Jim Laskeyc03caef2006-07-17 17:38:29 +0000619 /// AbstractTypeMap - Map for abstract type constants.
620 ///
Chris Lattnerb50d1352003-10-05 00:17:43 +0000621 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner99a669b2004-11-19 16:39:44 +0000622
Chris Lattner99a669b2004-11-19 16:39:44 +0000623 private:
624 void clear(std::vector<Constant *> &Constants) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000625 for(typename MapTy::iterator I = Map.begin(); I != Map.end(); ++I)
Chris Lattner99a669b2004-11-19 16:39:44 +0000626 Constants.push_back(I->second);
627 Map.clear();
628 AbstractTypeMap.clear();
Chris Lattner935aa922005-10-04 17:48:46 +0000629 InverseMap.clear();
Chris Lattner99a669b2004-11-19 16:39:44 +0000630 }
631
Chris Lattner98fa07b2003-05-23 20:03:32 +0000632 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000633 typename MapTy::iterator map_end() { return Map.end(); }
Chris Lattnerb64419a2005-10-03 22:51:37 +0000634
635 /// InsertOrGetItem - Return an iterator for the specified element.
636 /// If the element exists in the map, the returned iterator points to the
637 /// entry and Exists=true. If not, the iterator points to the newly
638 /// inserted entry and returns Exists=false. Newly inserted entries have
639 /// I->second == 0, and should be filled in.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000640 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
641 &InsertVal,
Chris Lattnerb64419a2005-10-03 22:51:37 +0000642 bool &Exists) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000643 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
Chris Lattnerb64419a2005-10-03 22:51:37 +0000644 Exists = !IP.second;
645 return IP.first;
646 }
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000647
Chris Lattner935aa922005-10-04 17:48:46 +0000648private:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000649 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
Chris Lattner935aa922005-10-04 17:48:46 +0000650 if (HasLargeKey) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000651 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
Chris Lattner935aa922005-10-04 17:48:46 +0000652 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
653 IMI->second->second == CP &&
654 "InverseMap corrupt!");
655 return IMI->second;
656 }
657
Jim Laskeyc03caef2006-07-17 17:38:29 +0000658 typename MapTy::iterator I =
Chris Lattner935aa922005-10-04 17:48:46 +0000659 Map.find(MapKey((TypeClass*)CP->getRawType(), getValType(CP)));
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000660 if (I == Map.end() || I->second != CP) {
661 // FIXME: This should not use a linear scan. If this gets to be a
662 // performance problem, someone should look at this.
663 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
664 /* empty */;
665 }
Chris Lattner935aa922005-10-04 17:48:46 +0000666 return I;
667 }
668public:
669
Chris Lattnerb64419a2005-10-03 22:51:37 +0000670 /// getOrCreate - Return the specified constant from the map, creating it if
671 /// necessary.
Chris Lattner98fa07b2003-05-23 20:03:32 +0000672 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000673 MapKey Lookup(Ty, V);
Jim Laskeyc03caef2006-07-17 17:38:29 +0000674 typename MapTy::iterator I = Map.lower_bound(Lookup);
Reid Spencere0fc4df2006-10-20 07:07:24 +0000675 // Is it in the map?
Chris Lattner98fa07b2003-05-23 20:03:32 +0000676 if (I != Map.end() && I->first == Lookup)
Reid Spencere0fc4df2006-10-20 07:07:24 +0000677 return static_cast<ConstantClass *>(I->second);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000678
679 // If no preexisting value, create one now...
680 ConstantClass *Result =
681 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
682
Chris Lattnerb50d1352003-10-05 00:17:43 +0000683 /// FIXME: why does this assert fail when loading 176.gcc?
684 //assert(Result->getType() == Ty && "Type specified is not correct!");
685 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
686
Chris Lattner935aa922005-10-04 17:48:46 +0000687 if (HasLargeKey) // Remember the reverse mapping if needed.
688 InverseMap.insert(std::make_pair(Result, I));
689
Chris Lattnerb50d1352003-10-05 00:17:43 +0000690 // If the type of the constant is abstract, make sure that an entry exists
691 // for it in the AbstractTypeMap.
692 if (Ty->isAbstract()) {
693 typename AbstractTypeMapTy::iterator TI =
694 AbstractTypeMap.lower_bound(Ty);
695
696 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
697 // Add ourselves to the ATU list of the type.
698 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
699
700 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
701 }
702 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000703 return Result;
704 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000705
Chris Lattner98fa07b2003-05-23 20:03:32 +0000706 void remove(ConstantClass *CP) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000707 typename MapTy::iterator I = FindExistingElement(CP);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000708 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +0000709 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +0000710
Chris Lattner935aa922005-10-04 17:48:46 +0000711 if (HasLargeKey) // Remember the reverse mapping if needed.
712 InverseMap.erase(CP);
713
Chris Lattnerb50d1352003-10-05 00:17:43 +0000714 // Now that we found the entry, make sure this isn't the entry that
715 // the AbstractTypeMap points to.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000716 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000717 if (Ty->isAbstract()) {
718 assert(AbstractTypeMap.count(Ty) &&
719 "Abstract type not in AbstractTypeMap?");
Jim Laskeyc03caef2006-07-17 17:38:29 +0000720 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
Chris Lattnerb50d1352003-10-05 00:17:43 +0000721 if (ATMEntryIt == I) {
722 // Yes, we are removing the representative entry for this type.
723 // See if there are any other entries of the same type.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000724 typename MapTy::iterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000725
Chris Lattnerb50d1352003-10-05 00:17:43 +0000726 // First check the entry before this one...
727 if (TmpIt != Map.begin()) {
728 --TmpIt;
729 if (TmpIt->first.first != Ty) // Not the same type, move back...
730 ++TmpIt;
731 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000732
Chris Lattnerb50d1352003-10-05 00:17:43 +0000733 // If we didn't find the same type, try to move forward...
734 if (TmpIt == ATMEntryIt) {
735 ++TmpIt;
736 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
737 --TmpIt; // No entry afterwards with the same type
738 }
739
740 // If there is another entry in the map of the same abstract type,
741 // update the AbstractTypeMap entry now.
742 if (TmpIt != ATMEntryIt) {
743 ATMEntryIt = TmpIt;
744 } else {
745 // Otherwise, we are removing the last instance of this type
746 // from the table. Remove from the ATM, and from user list.
747 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
748 AbstractTypeMap.erase(Ty);
749 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000750 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000751 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000752
Chris Lattnerb50d1352003-10-05 00:17:43 +0000753 Map.erase(I);
754 }
755
Chris Lattner3b793c62005-10-04 21:35:50 +0000756
757 /// MoveConstantToNewSlot - If we are about to change C to be the element
758 /// specified by I, update our internal data structures to reflect this
759 /// fact.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000760 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
Chris Lattner3b793c62005-10-04 21:35:50 +0000761 // First, remove the old location of the specified constant in the map.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000762 typename MapTy::iterator OldI = FindExistingElement(C);
Chris Lattner3b793c62005-10-04 21:35:50 +0000763 assert(OldI != Map.end() && "Constant not found in constant table!");
764 assert(OldI->second == C && "Didn't find correct element?");
765
766 // If this constant is the representative element for its abstract type,
767 // update the AbstractTypeMap so that the representative element is I.
768 if (C->getType()->isAbstract()) {
769 typename AbstractTypeMapTy::iterator ATI =
770 AbstractTypeMap.find(C->getType());
771 assert(ATI != AbstractTypeMap.end() &&
772 "Abstract type not in AbstractTypeMap?");
773 if (ATI->second == OldI)
774 ATI->second = I;
775 }
776
777 // Remove the old entry from the map.
778 Map.erase(OldI);
779
780 // Update the inverse map so that we know that this constant is now
781 // located at descriptor I.
782 if (HasLargeKey) {
783 assert(I->second == C && "Bad inversemap entry!");
784 InverseMap[C] = I;
785 }
786 }
787
Chris Lattnerb50d1352003-10-05 00:17:43 +0000788 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000789 typename AbstractTypeMapTy::iterator I =
Jim Laskeyc03caef2006-07-17 17:38:29 +0000790 AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +0000791
792 assert(I != AbstractTypeMap.end() &&
793 "Abstract type not in AbstractTypeMap?");
794
795 // Convert a constant at a time until the last one is gone. The last one
796 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
797 // eliminated eventually.
798 do {
799 ConvertConstantType<ConstantClass,
Jim Laskeyc03caef2006-07-17 17:38:29 +0000800 TypeClass>::convert(
801 static_cast<ConstantClass *>(I->second->second),
Chris Lattnerb50d1352003-10-05 00:17:43 +0000802 cast<TypeClass>(NewTy));
803
Jim Laskeyc03caef2006-07-17 17:38:29 +0000804 I = AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +0000805 } while (I != AbstractTypeMap.end());
806 }
807
808 // If the type became concrete without being refined to any other existing
809 // type, we just remove ourselves from the ATU list.
810 void typeBecameConcrete(const DerivedType *AbsTy) {
811 AbsTy->removeAbstractTypeUser(this);
812 }
813
814 void dump() const {
Bill Wendling6a462f12006-11-17 08:03:48 +0000815 DOUT << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +0000816 }
817 };
818}
819
Chris Lattnera84df0a22006-09-28 23:36:21 +0000820
821//---- ConstantBool::get*() implementation.
822
823ConstantBool *ConstantBool::getTrue() {
824 static ConstantBool *T = 0;
825 if (T) return T;
826 return T = new ConstantBool(true);
827}
828ConstantBool *ConstantBool::getFalse() {
829 static ConstantBool *F = 0;
830 if (F) return F;
831 return F = new ConstantBool(false);
832}
833
Reid Spencere0fc4df2006-10-20 07:07:24 +0000834//---- ConstantInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000835//
Reid Spencere0fc4df2006-10-20 07:07:24 +0000836static ManagedStatic<ValueMap<uint64_t, Type, ConstantInt> > IntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000837
Reid Spencere0fc4df2006-10-20 07:07:24 +0000838// Get a ConstantInt from an int64_t. Note here that we canoncialize the value
839// to a uint64_t value that has been zero extended down to the size of the
840// integer type of the ConstantInt. This allows the getZExtValue method to
841// just return the stored value while getSExtValue has to convert back to sign
842// extended. getZExtValue is more common in LLVM than getSExtValue().
843ConstantInt *ConstantInt::get(const Type *Ty, int64_t V) {
Chris Lattnerf16661c2006-12-01 19:20:02 +0000844 return IntConstants->getOrCreate(Ty, V & Ty->getIntegralTypeMask());
845}
846
847ConstantIntegral *ConstantIntegral::get(const Type *Ty, int64_t V) {
848 if (Ty == Type::BoolTy) return ConstantBool::get(V&1);
849 return IntConstants->getOrCreate(Ty, V & Ty->getIntegralTypeMask());
Chris Lattner49d855c2001-09-07 16:46:31 +0000850}
851
Chris Lattner3462ae32001-12-03 22:26:30 +0000852//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000853//
Chris Lattnerac80ea42004-02-01 22:49:04 +0000854namespace llvm {
855 template<>
856 struct ConstantCreator<ConstantFP, Type, uint64_t> {
857 static ConstantFP *create(const Type *Ty, uint64_t V) {
858 assert(Ty == Type::DoubleTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000859 return new ConstantFP(Ty, BitsToDouble(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000860 }
861 };
862 template<>
863 struct ConstantCreator<ConstantFP, Type, uint32_t> {
864 static ConstantFP *create(const Type *Ty, uint32_t V) {
865 assert(Ty == Type::FloatTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000866 return new ConstantFP(Ty, BitsToFloat(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000867 }
868 };
869}
870
Chris Lattner69edc982006-09-28 00:35:06 +0000871static ManagedStatic<ValueMap<uint64_t, Type, ConstantFP> > DoubleConstants;
872static ManagedStatic<ValueMap<uint32_t, Type, ConstantFP> > FloatConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000873
Jim Laskey8ad8f712005-08-17 20:06:22 +0000874bool ConstantFP::isNullValue() const {
875 return DoubleToBits(Val) == 0;
876}
877
878bool ConstantFP::isExactlyValue(double V) const {
879 return DoubleToBits(V) == DoubleToBits(Val);
880}
881
882
Chris Lattner3462ae32001-12-03 22:26:30 +0000883ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner241ed4c2004-01-23 00:55:21 +0000884 if (Ty == Type::FloatTy) {
885 // Force the value through memory to normalize it.
Chris Lattner69edc982006-09-28 00:35:06 +0000886 return FloatConstants->getOrCreate(Ty, FloatToBits(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000887 } else {
888 assert(Ty == Type::DoubleTy);
Chris Lattner69edc982006-09-28 00:35:06 +0000889 return DoubleConstants->getOrCreate(Ty, DoubleToBits(V));
Chris Lattner241ed4c2004-01-23 00:55:21 +0000890 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000891}
892
Chris Lattner9fba3da2004-02-15 05:53:04 +0000893//---- ConstantAggregateZero::get() implementation...
894//
895namespace llvm {
896 // ConstantAggregateZero does not take extra "value" argument...
897 template<class ValType>
898 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
899 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
900 return new ConstantAggregateZero(Ty);
901 }
902 };
903
904 template<>
905 struct ConvertConstantType<ConstantAggregateZero, Type> {
906 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
907 // Make everyone now use a constant of the new type...
908 Constant *New = ConstantAggregateZero::get(NewTy);
909 assert(New != OldC && "Didn't replace constant??");
910 OldC->uncheckedReplaceAllUsesWith(New);
911 OldC->destroyConstant(); // This constant is now dead, destroy it.
912 }
913 };
914}
915
Chris Lattner69edc982006-09-28 00:35:06 +0000916static ManagedStatic<ValueMap<char, Type,
917 ConstantAggregateZero> > AggZeroConstants;
Chris Lattner9fba3da2004-02-15 05:53:04 +0000918
Chris Lattner3e650af2004-08-04 04:48:01 +0000919static char getValType(ConstantAggregateZero *CPZ) { return 0; }
920
Chris Lattner9fba3da2004-02-15 05:53:04 +0000921Constant *ConstantAggregateZero::get(const Type *Ty) {
Chris Lattnerbfd0b6d2006-06-10 04:16:23 +0000922 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<PackedType>(Ty)) &&
923 "Cannot create an aggregate zero of non-aggregate type!");
Chris Lattner69edc982006-09-28 00:35:06 +0000924 return AggZeroConstants->getOrCreate(Ty, 0);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000925}
926
927// destroyConstant - Remove the constant from the constant table...
928//
929void ConstantAggregateZero::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +0000930 AggZeroConstants->remove(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000931 destroyConstantImpl();
932}
933
Chris Lattner3462ae32001-12-03 22:26:30 +0000934//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000935//
Chris Lattner189d19f2003-11-21 20:23:48 +0000936namespace llvm {
937 template<>
938 struct ConvertConstantType<ConstantArray, ArrayType> {
939 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
940 // Make everyone now use a constant of the new type...
941 std::vector<Constant*> C;
942 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
943 C.push_back(cast<Constant>(OldC->getOperand(i)));
944 Constant *New = ConstantArray::get(NewTy, C);
945 assert(New != OldC && "Didn't replace constant??");
946 OldC->uncheckedReplaceAllUsesWith(New);
947 OldC->destroyConstant(); // This constant is now dead, destroy it.
948 }
949 };
950}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000951
Chris Lattner3e650af2004-08-04 04:48:01 +0000952static std::vector<Constant*> getValType(ConstantArray *CA) {
953 std::vector<Constant*> Elements;
954 Elements.reserve(CA->getNumOperands());
955 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
956 Elements.push_back(cast<Constant>(CA->getOperand(i)));
957 return Elements;
958}
959
Chris Lattnerb64419a2005-10-03 22:51:37 +0000960typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattner935aa922005-10-04 17:48:46 +0000961 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +0000962static ManagedStatic<ArrayConstantsTy> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000963
Chris Lattner015e8212004-02-15 04:14:47 +0000964Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +0000965 const std::vector<Constant*> &V) {
966 // If this is an all-zero array, return a ConstantAggregateZero object
967 if (!V.empty()) {
968 Constant *C = V[0];
969 if (!C->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +0000970 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000971 for (unsigned i = 1, e = V.size(); i != e; ++i)
972 if (V[i] != C)
Chris Lattner69edc982006-09-28 00:35:06 +0000973 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000974 }
975 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000976}
977
Chris Lattner98fa07b2003-05-23 20:03:32 +0000978// destroyConstant - Remove the constant from the constant table...
979//
980void ConstantArray::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +0000981 ArrayConstants->remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000982 destroyConstantImpl();
983}
984
Reid Spencer6f614532006-05-30 08:23:18 +0000985/// ConstantArray::get(const string&) - Return an array that is initialized to
986/// contain the specified string. If length is zero then a null terminator is
987/// added to the specified string so that it may be used in a natural way.
988/// Otherwise, the length parameter specifies how much of the string to use
989/// and it won't be null terminated.
990///
Reid Spencer82ebaba2006-05-30 18:15:07 +0000991Constant *ConstantArray::get(const std::string &Str, bool AddNull) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000992 std::vector<Constant*> ElementVals;
Reid Spencer82ebaba2006-05-30 18:15:07 +0000993 for (unsigned i = 0; i < Str.length(); ++i)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000994 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000995
996 // Add a null terminator to the string...
Reid Spencer82ebaba2006-05-30 18:15:07 +0000997 if (AddNull) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000998 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, 0));
Reid Spencer6f614532006-05-30 08:23:18 +0000999 }
Chris Lattner8f80fe02001-10-14 23:54:12 +00001000
Reid Spencer8d9336d2006-12-31 05:26:44 +00001001 ArrayType *ATy = ArrayType::get(Type::Int8Ty, ElementVals.size());
Chris Lattner3462ae32001-12-03 22:26:30 +00001002 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +00001003}
1004
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001005/// isString - This method returns true if the array is an array of sbyte or
1006/// ubyte, and if the elements of the array are all ConstantInt's.
1007bool ConstantArray::isString() const {
1008 // Check the element type for sbyte or ubyte...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001009 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001010 return false;
1011 // Check the elements to make sure they are all integers, not constant
1012 // expressions.
1013 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1014 if (!isa<ConstantInt>(getOperand(i)))
1015 return false;
1016 return true;
1017}
1018
Evan Cheng3763c5b2006-10-26 19:15:05 +00001019/// isCString - This method returns true if the array is a string (see
1020/// isString) and it ends in a null byte \0 and does not contains any other
1021/// null bytes except its terminator.
1022bool ConstantArray::isCString() const {
Evan Chenge974da62006-10-26 21:48:03 +00001023 // Check the element type for sbyte or ubyte...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001024 if (getType()->getElementType() != Type::Int8Ty)
Evan Chenge974da62006-10-26 21:48:03 +00001025 return false;
1026 Constant *Zero = Constant::getNullValue(getOperand(0)->getType());
1027 // Last element must be a null.
1028 if (getOperand(getNumOperands()-1) != Zero)
1029 return false;
1030 // Other elements must be non-null integers.
1031 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1032 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +00001033 return false;
Evan Chenge974da62006-10-26 21:48:03 +00001034 if (getOperand(i) == Zero)
1035 return false;
1036 }
Evan Cheng3763c5b2006-10-26 19:15:05 +00001037 return true;
1038}
1039
1040
Chris Lattner81fabb02002-08-26 17:53:56 +00001041// getAsString - If the sub-element type of this array is either sbyte or ubyte,
1042// then this method converts the array to an std::string and returns it.
1043// Otherwise, it asserts out.
1044//
1045std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001046 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001047 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +00001048 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Reid Spencere0fc4df2006-10-20 07:07:24 +00001049 Result += (char)cast<ConstantInt>(getOperand(i))->getZExtValue();
Chris Lattner81fabb02002-08-26 17:53:56 +00001050 return Result;
1051}
1052
1053
Chris Lattner3462ae32001-12-03 22:26:30 +00001054//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001055//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001056
Chris Lattner189d19f2003-11-21 20:23:48 +00001057namespace llvm {
1058 template<>
1059 struct ConvertConstantType<ConstantStruct, StructType> {
1060 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1061 // Make everyone now use a constant of the new type...
1062 std::vector<Constant*> C;
1063 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1064 C.push_back(cast<Constant>(OldC->getOperand(i)));
1065 Constant *New = ConstantStruct::get(NewTy, C);
1066 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001067
Chris Lattner189d19f2003-11-21 20:23:48 +00001068 OldC->uncheckedReplaceAllUsesWith(New);
1069 OldC->destroyConstant(); // This constant is now dead, destroy it.
1070 }
1071 };
1072}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001073
Chris Lattner8760ec72005-10-04 01:17:50 +00001074typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattner935aa922005-10-04 17:48:46 +00001075 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001076static ManagedStatic<StructConstantsTy> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001077
Chris Lattner3e650af2004-08-04 04:48:01 +00001078static std::vector<Constant*> getValType(ConstantStruct *CS) {
1079 std::vector<Constant*> Elements;
1080 Elements.reserve(CS->getNumOperands());
1081 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1082 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1083 return Elements;
1084}
1085
Chris Lattner015e8212004-02-15 04:14:47 +00001086Constant *ConstantStruct::get(const StructType *Ty,
1087 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001088 // Create a ConstantAggregateZero value if all elements are zeros...
1089 for (unsigned i = 0, e = V.size(); i != e; ++i)
1090 if (!V[i]->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001091 return StructConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001092
1093 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001094}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001095
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001096Constant *ConstantStruct::get(const std::vector<Constant*> &V, bool packed) {
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001097 std::vector<const Type*> StructEls;
1098 StructEls.reserve(V.size());
1099 for (unsigned i = 0, e = V.size(); i != e; ++i)
1100 StructEls.push_back(V[i]->getType());
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001101 return get(StructType::get(StructEls, packed), V);
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001102}
1103
Chris Lattnerd7a73302001-10-13 06:57:33 +00001104// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001105//
Chris Lattner3462ae32001-12-03 22:26:30 +00001106void ConstantStruct::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001107 StructConstants->remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001108 destroyConstantImpl();
1109}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001110
Brian Gaeke02209042004-08-20 06:00:58 +00001111//---- ConstantPacked::get() implementation...
1112//
1113namespace llvm {
1114 template<>
1115 struct ConvertConstantType<ConstantPacked, PackedType> {
1116 static void convert(ConstantPacked *OldC, const PackedType *NewTy) {
1117 // Make everyone now use a constant of the new type...
1118 std::vector<Constant*> C;
1119 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1120 C.push_back(cast<Constant>(OldC->getOperand(i)));
1121 Constant *New = ConstantPacked::get(NewTy, C);
1122 assert(New != OldC && "Didn't replace constant??");
1123 OldC->uncheckedReplaceAllUsesWith(New);
1124 OldC->destroyConstant(); // This constant is now dead, destroy it.
1125 }
1126 };
1127}
1128
1129static std::vector<Constant*> getValType(ConstantPacked *CP) {
1130 std::vector<Constant*> Elements;
1131 Elements.reserve(CP->getNumOperands());
1132 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1133 Elements.push_back(CP->getOperand(i));
1134 return Elements;
1135}
1136
Chris Lattner69edc982006-09-28 00:35:06 +00001137static ManagedStatic<ValueMap<std::vector<Constant*>, PackedType,
1138 ConstantPacked> > PackedConstants;
Brian Gaeke02209042004-08-20 06:00:58 +00001139
1140Constant *ConstantPacked::get(const PackedType *Ty,
1141 const std::vector<Constant*> &V) {
1142 // If this is an all-zero packed, return a ConstantAggregateZero object
1143 if (!V.empty()) {
1144 Constant *C = V[0];
1145 if (!C->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001146 return PackedConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001147 for (unsigned i = 1, e = V.size(); i != e; ++i)
1148 if (V[i] != C)
Chris Lattner69edc982006-09-28 00:35:06 +00001149 return PackedConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001150 }
1151 return ConstantAggregateZero::get(Ty);
1152}
1153
1154Constant *ConstantPacked::get(const std::vector<Constant*> &V) {
1155 assert(!V.empty() && "Cannot infer type if V is empty");
1156 return get(PackedType::get(V.front()->getType(),V.size()), V);
1157}
1158
1159// destroyConstant - Remove the constant from the constant table...
1160//
1161void ConstantPacked::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001162 PackedConstants->remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001163 destroyConstantImpl();
1164}
1165
Chris Lattner3462ae32001-12-03 22:26:30 +00001166//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001167//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001168
Chris Lattner189d19f2003-11-21 20:23:48 +00001169namespace llvm {
1170 // ConstantPointerNull does not take extra "value" argument...
1171 template<class ValType>
1172 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1173 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1174 return new ConstantPointerNull(Ty);
1175 }
1176 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001177
Chris Lattner189d19f2003-11-21 20:23:48 +00001178 template<>
1179 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1180 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1181 // Make everyone now use a constant of the new type...
1182 Constant *New = ConstantPointerNull::get(NewTy);
1183 assert(New != OldC && "Didn't replace constant??");
1184 OldC->uncheckedReplaceAllUsesWith(New);
1185 OldC->destroyConstant(); // This constant is now dead, destroy it.
1186 }
1187 };
1188}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001189
Chris Lattner69edc982006-09-28 00:35:06 +00001190static ManagedStatic<ValueMap<char, PointerType,
1191 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001192
Chris Lattner3e650af2004-08-04 04:48:01 +00001193static char getValType(ConstantPointerNull *) {
1194 return 0;
1195}
1196
1197
Chris Lattner3462ae32001-12-03 22:26:30 +00001198ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001199 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001200}
1201
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001202// destroyConstant - Remove the constant from the constant table...
1203//
1204void ConstantPointerNull::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001205 NullPtrConstants->remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001206 destroyConstantImpl();
1207}
1208
1209
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001210//---- UndefValue::get() implementation...
1211//
1212
1213namespace llvm {
1214 // UndefValue does not take extra "value" argument...
1215 template<class ValType>
1216 struct ConstantCreator<UndefValue, Type, ValType> {
1217 static UndefValue *create(const Type *Ty, const ValType &V) {
1218 return new UndefValue(Ty);
1219 }
1220 };
1221
1222 template<>
1223 struct ConvertConstantType<UndefValue, Type> {
1224 static void convert(UndefValue *OldC, const Type *NewTy) {
1225 // Make everyone now use a constant of the new type.
1226 Constant *New = UndefValue::get(NewTy);
1227 assert(New != OldC && "Didn't replace constant??");
1228 OldC->uncheckedReplaceAllUsesWith(New);
1229 OldC->destroyConstant(); // This constant is now dead, destroy it.
1230 }
1231 };
1232}
1233
Chris Lattner69edc982006-09-28 00:35:06 +00001234static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001235
1236static char getValType(UndefValue *) {
1237 return 0;
1238}
1239
1240
1241UndefValue *UndefValue::get(const Type *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001242 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001243}
1244
1245// destroyConstant - Remove the constant from the constant table.
1246//
1247void UndefValue::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001248 UndefValueConstants->remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001249 destroyConstantImpl();
1250}
1251
1252
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001253//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001254//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001255
Reid Spenceree3c9912006-12-04 05:19:50 +00001256struct ExprMapKeyType {
1257 explicit ExprMapKeyType(unsigned opc, std::vector<Constant*> ops,
Reid Spencerdba6aa42006-12-04 18:38:05 +00001258 unsigned short pred = 0) : opcode(opc), predicate(pred), operands(ops) { }
1259 uint16_t opcode;
1260 uint16_t predicate;
Reid Spenceree3c9912006-12-04 05:19:50 +00001261 std::vector<Constant*> operands;
Reid Spenceree3c9912006-12-04 05:19:50 +00001262 bool operator==(const ExprMapKeyType& that) const {
1263 return this->opcode == that.opcode &&
1264 this->predicate == that.predicate &&
1265 this->operands == that.operands;
1266 }
1267 bool operator<(const ExprMapKeyType & that) const {
1268 return this->opcode < that.opcode ||
1269 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1270 (this->opcode == that.opcode && this->predicate == that.predicate &&
1271 this->operands < that.operands);
1272 }
1273
1274 bool operator!=(const ExprMapKeyType& that) const {
1275 return !(*this == that);
1276 }
1277};
Chris Lattner98fa07b2003-05-23 20:03:32 +00001278
Chris Lattner189d19f2003-11-21 20:23:48 +00001279namespace llvm {
1280 template<>
1281 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001282 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1283 unsigned short pred = 0) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001284 if (Instruction::isCast(V.opcode))
1285 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1286 if ((V.opcode >= Instruction::BinaryOpsBegin &&
1287 V.opcode < Instruction::BinaryOpsEnd) ||
1288 V.opcode == Instruction::Shl ||
1289 V.opcode == Instruction::LShr ||
1290 V.opcode == Instruction::AShr)
1291 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1292 if (V.opcode == Instruction::Select)
1293 return new SelectConstantExpr(V.operands[0], V.operands[1],
1294 V.operands[2]);
1295 if (V.opcode == Instruction::ExtractElement)
1296 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1297 if (V.opcode == Instruction::InsertElement)
1298 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1299 V.operands[2]);
1300 if (V.opcode == Instruction::ShuffleVector)
1301 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1302 V.operands[2]);
1303 if (V.opcode == Instruction::GetElementPtr) {
1304 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
1305 return new GetElementPtrConstantExpr(V.operands[0], IdxList, Ty);
1306 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001307
Reid Spenceree3c9912006-12-04 05:19:50 +00001308 // The compare instructions are weird. We have to encode the predicate
1309 // value and it is combined with the instruction opcode by multiplying
1310 // the opcode by one hundred. We must decode this to get the predicate.
1311 if (V.opcode == Instruction::ICmp)
1312 return new CompareConstantExpr(Instruction::ICmp, V.predicate,
1313 V.operands[0], V.operands[1]);
1314 if (V.opcode == Instruction::FCmp)
1315 return new CompareConstantExpr(Instruction::FCmp, V.predicate,
1316 V.operands[0], V.operands[1]);
1317 assert(0 && "Invalid ConstantExpr!");
Jeff Cohen9f469632006-12-15 21:47:01 +00001318 return 0;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001319 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001320 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001321
Chris Lattner189d19f2003-11-21 20:23:48 +00001322 template<>
1323 struct ConvertConstantType<ConstantExpr, Type> {
1324 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1325 Constant *New;
1326 switch (OldC->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001327 case Instruction::Trunc:
1328 case Instruction::ZExt:
1329 case Instruction::SExt:
1330 case Instruction::FPTrunc:
1331 case Instruction::FPExt:
1332 case Instruction::UIToFP:
1333 case Instruction::SIToFP:
1334 case Instruction::FPToUI:
1335 case Instruction::FPToSI:
1336 case Instruction::PtrToInt:
1337 case Instruction::IntToPtr:
1338 case Instruction::BitCast:
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001339 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
1340 NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001341 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001342 case Instruction::Select:
1343 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1344 OldC->getOperand(1),
1345 OldC->getOperand(2));
1346 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001347 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001348 case Instruction::LShr:
1349 case Instruction::AShr:
Chris Lattner189d19f2003-11-21 20:23:48 +00001350 New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
1351 OldC->getOperand(0), OldC->getOperand(1));
1352 break;
1353 default:
1354 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001355 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner189d19f2003-11-21 20:23:48 +00001356 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1357 OldC->getOperand(1));
1358 break;
1359 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001360 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001361 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
1362 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), Idx);
Chris Lattner189d19f2003-11-21 20:23:48 +00001363 break;
1364 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001365
Chris Lattner189d19f2003-11-21 20:23:48 +00001366 assert(New != OldC && "Didn't replace constant??");
1367 OldC->uncheckedReplaceAllUsesWith(New);
1368 OldC->destroyConstant(); // This constant is now dead, destroy it.
1369 }
1370 };
1371} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001372
1373
Chris Lattner3e650af2004-08-04 04:48:01 +00001374static ExprMapKeyType getValType(ConstantExpr *CE) {
1375 std::vector<Constant*> Operands;
1376 Operands.reserve(CE->getNumOperands());
1377 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1378 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spenceree3c9912006-12-04 05:19:50 +00001379 return ExprMapKeyType(CE->getOpcode(), Operands,
1380 CE->isCompare() ? CE->getPredicate() : 0);
Chris Lattner3e650af2004-08-04 04:48:01 +00001381}
1382
Chris Lattner69edc982006-09-28 00:35:06 +00001383static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1384 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001385
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001386/// This is a utility function to handle folding of casts and lookup of the
1387/// cast in the ExprConstants map. It is usedby the various get* methods below.
1388static inline Constant *getFoldedCast(
1389 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001390 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001391 // Fold a few common cases
1392 if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
1393 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001394
Vikram S. Adve4c485332002-07-15 18:19:33 +00001395 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001396 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00001397 ExprMapKeyType Key(opc, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001398 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001399}
Reid Spencerf37dc652006-12-05 19:14:13 +00001400
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001401Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
1402 Instruction::CastOps opc = Instruction::CastOps(oc);
1403 assert(Instruction::isCast(opc) && "opcode out of range");
1404 assert(C && Ty && "Null arguments to getCast");
1405 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1406
1407 switch (opc) {
1408 default:
1409 assert(0 && "Invalid cast opcode");
1410 break;
1411 case Instruction::Trunc: return getTrunc(C, Ty);
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001412 case Instruction::ZExt: return getZExt(C, Ty);
1413 case Instruction::SExt: return getSExt(C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001414 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1415 case Instruction::FPExt: return getFPExtend(C, Ty);
1416 case Instruction::UIToFP: return getUIToFP(C, Ty);
1417 case Instruction::SIToFP: return getSIToFP(C, Ty);
1418 case Instruction::FPToUI: return getFPToUI(C, Ty);
1419 case Instruction::FPToSI: return getFPToSI(C, Ty);
1420 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1421 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1422 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001423 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001424 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00001425}
1426
Reid Spencer5c140882006-12-04 20:17:56 +00001427Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
1428 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1429 return getCast(Instruction::BitCast, C, Ty);
1430 return getCast(Instruction::ZExt, C, Ty);
1431}
1432
1433Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
1434 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1435 return getCast(Instruction::BitCast, C, Ty);
1436 return getCast(Instruction::SExt, C, Ty);
1437}
1438
1439Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
1440 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1441 return getCast(Instruction::BitCast, C, Ty);
1442 return getCast(Instruction::Trunc, C, Ty);
1443}
1444
Reid Spencerbc245a02006-12-05 03:25:26 +00001445Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
1446 assert(isa<PointerType>(S->getType()) && "Invalid cast");
1447 assert((Ty->isIntegral() || Ty->getTypeID() == Type::PointerTyID) &&
1448 "Invalid cast");
1449
1450 if (Ty->isIntegral())
1451 return getCast(Instruction::PtrToInt, S, Ty);
1452 return getCast(Instruction::BitCast, S, Ty);
1453}
1454
Reid Spencer56521c42006-12-12 00:51:07 +00001455Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1456 bool isSigned) {
1457 assert(C->getType()->isIntegral() && Ty->isIntegral() && "Invalid cast");
1458 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1459 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1460 Instruction::CastOps opcode =
1461 (SrcBits == DstBits ? Instruction::BitCast :
1462 (SrcBits > DstBits ? Instruction::Trunc :
1463 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1464 return getCast(opcode, C, Ty);
1465}
1466
1467Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
1468 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1469 "Invalid cast");
1470 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1471 unsigned DstBits = Ty->getPrimitiveSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001472 if (SrcBits == DstBits)
1473 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001474 Instruction::CastOps opcode =
Reid Spencerca104e82006-12-12 05:38:50 +00001475 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001476 return getCast(opcode, C, Ty);
1477}
1478
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001479Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
1480 assert(C->getType()->isInteger() && "Trunc operand must be integer");
1481 assert(Ty->isIntegral() && "Trunc produces only integral");
1482 assert(C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&&
1483 "SrcTy must be larger than DestTy for Trunc!");
1484
1485 return getFoldedCast(Instruction::Trunc, C, Ty);
1486}
1487
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001488Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001489 assert(C->getType()->isIntegral() && "SEXt operand must be integral");
1490 assert(Ty->isInteger() && "SExt produces only integer");
1491 assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1492 "SrcTy must be smaller than DestTy for SExt!");
1493
1494 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerdd284742004-04-04 23:20:30 +00001495}
1496
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001497Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001498 assert(C->getType()->isIntegral() && "ZEXt operand must be integral");
1499 assert(Ty->isInteger() && "ZExt produces only integer");
1500 assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1501 "SrcTy must be smaller than DestTy for ZExt!");
1502
1503 return getFoldedCast(Instruction::ZExt, C, Ty);
1504}
1505
1506Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
1507 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1508 C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&&
1509 "This is an illegal floating point truncation!");
1510 return getFoldedCast(Instruction::FPTrunc, C, Ty);
1511}
1512
1513Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
1514 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1515 C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1516 "This is an illegal floating point extension!");
1517 return getFoldedCast(Instruction::FPExt, C, Ty);
1518}
1519
1520Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
1521 assert(C->getType()->isIntegral() && Ty->isFloatingPoint() &&
1522 "This is an illegal uint to floating point cast!");
1523 return getFoldedCast(Instruction::UIToFP, C, Ty);
1524}
1525
1526Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
1527 assert(C->getType()->isIntegral() && Ty->isFloatingPoint() &&
1528 "This is an illegal sint to floating point cast!");
1529 return getFoldedCast(Instruction::SIToFP, C, Ty);
1530}
1531
1532Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
1533 assert(C->getType()->isFloatingPoint() && Ty->isIntegral() &&
1534 "This is an illegal floating point to uint cast!");
1535 return getFoldedCast(Instruction::FPToUI, C, Ty);
1536}
1537
1538Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
1539 assert(C->getType()->isFloatingPoint() && Ty->isIntegral() &&
1540 "This is an illegal floating point to sint cast!");
1541 return getFoldedCast(Instruction::FPToSI, C, Ty);
1542}
1543
1544Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
1545 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
1546 assert(DstTy->isIntegral() && "PtrToInt destination must be integral");
1547 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
1548}
1549
1550Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
1551 assert(C->getType()->isIntegral() && "IntToPtr source must be integral");
1552 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
1553 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
1554}
1555
1556Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
1557 // BitCast implies a no-op cast of type only. No bits change. However, you
1558 // can't cast pointers to anything but pointers.
1559 const Type *SrcTy = C->getType();
1560 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer5c140882006-12-04 20:17:56 +00001561 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001562
1563 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
1564 // or nonptr->ptr). For all the other types, the cast is okay if source and
1565 // destination bit widths are identical.
1566 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1567 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Reid Spencer5c140882006-12-04 20:17:56 +00001568 assert(SrcBitSize == DstBitSize && "BitCast requies types of same width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001569 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerdd284742004-04-04 23:20:30 +00001570}
1571
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001572Constant *ConstantExpr::getSizeOf(const Type *Ty) {
Chris Lattneracc4e542004-12-13 19:48:51 +00001573 // sizeof is implemented as: (ulong) gep (Ty*)null, 1
Reid Spencerc4dacf22006-12-04 02:43:42 +00001574 return getCast(Instruction::PtrToInt, getGetElementPtr(getNullValue(
1575 PointerType::get(Ty)), std::vector<Constant*>(1,
Reid Spencer8d9336d2006-12-31 05:26:44 +00001576 ConstantInt::get(Type::Int32Ty, 1))), Type::Int64Ty);
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001577}
1578
Alkis Evlogimenos9160d5f2005-03-19 11:40:31 +00001579Constant *ConstantExpr::getPtrPtrFromArrayPtr(Constant *C) {
1580 // pointer from array is implemented as: getelementptr arr ptr, 0, 0
Reid Spencer8d9336d2006-12-31 05:26:44 +00001581 static std::vector<Constant*> Indices(2, ConstantInt::get(Type::Int32Ty, 0));
Alkis Evlogimenos9160d5f2005-03-19 11:40:31 +00001582
1583 return ConstantExpr::getGetElementPtr(C, Indices);
1584}
1585
Chris Lattnerb50d1352003-10-05 00:17:43 +00001586Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Reid Spencera009d0d2006-12-04 21:35:24 +00001587 Constant *C1, Constant *C2) {
Reid Spencerfdff9382006-11-08 06:47:33 +00001588 if (Opcode == Instruction::Shl || Opcode == Instruction::LShr ||
1589 Opcode == Instruction::AShr)
Chris Lattner5645e8a2004-01-12 19:04:55 +00001590 return getShiftTy(ReqTy, Opcode, C1, C2);
Reid Spenceree3c9912006-12-04 05:19:50 +00001591
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001592 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00001593 assert(Opcode >= Instruction::BinaryOpsBegin &&
1594 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001595 "Invalid opcode in binary constant expression");
1596 assert(C1->getType() == C2->getType() &&
1597 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001598
Reid Spencer266e42b2006-12-23 06:05:41 +00001599 if (ReqTy == C1->getType() || ReqTy == Type::BoolTy)
Chris Lattnerb50d1352003-10-05 00:17:43 +00001600 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1601 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001602
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001603 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001604 ExprMapKeyType Key(Opcode, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001605 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001606}
1607
Reid Spencer266e42b2006-12-23 06:05:41 +00001608Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Reid Spencera009d0d2006-12-04 21:35:24 +00001609 Constant *C1, Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001610 switch (predicate) {
1611 default: assert(0 && "Invalid CmpInst predicate");
1612 case FCmpInst::FCMP_FALSE: case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_OGT:
1613 case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OLE:
1614 case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_ORD: case FCmpInst::FCMP_UNO:
1615 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UGT: case FCmpInst::FCMP_UGE:
1616 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_ULE: case FCmpInst::FCMP_UNE:
1617 case FCmpInst::FCMP_TRUE:
1618 return getFCmp(predicate, C1, C2);
1619 case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_UGT:
1620 case ICmpInst::ICMP_UGE: case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE:
1621 case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_SGE: case ICmpInst::ICMP_SLT:
1622 case ICmpInst::ICMP_SLE:
1623 return getICmp(predicate, C1, C2);
1624 }
Reid Spencera009d0d2006-12-04 21:35:24 +00001625}
1626
1627Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001628#ifndef NDEBUG
1629 switch (Opcode) {
Reid Spencer7eb55b32006-11-02 01:53:59 +00001630 case Instruction::Add:
1631 case Instruction::Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001632 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001633 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattnerc421a262006-01-04 01:01:04 +00001634 assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint() ||
1635 isa<PackedType>(C1->getType())) &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001636 "Tried to create an arithmetic operation on a non-arithmetic type!");
1637 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001638 case Instruction::UDiv:
1639 case Instruction::SDiv:
1640 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1641 assert((C1->getType()->isInteger() || (isa<PackedType>(C1->getType()) &&
1642 cast<PackedType>(C1->getType())->getElementType()->isInteger())) &&
1643 "Tried to create an arithmetic operation on a non-arithmetic type!");
1644 break;
1645 case Instruction::FDiv:
1646 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1647 assert((C1->getType()->isFloatingPoint() || (isa<PackedType>(C1->getType())
1648 && cast<PackedType>(C1->getType())->getElementType()->isFloatingPoint()))
1649 && "Tried to create an arithmetic operation on a non-arithmetic type!");
1650 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001651 case Instruction::URem:
1652 case Instruction::SRem:
1653 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1654 assert((C1->getType()->isInteger() || (isa<PackedType>(C1->getType()) &&
1655 cast<PackedType>(C1->getType())->getElementType()->isInteger())) &&
1656 "Tried to create an arithmetic operation on a non-arithmetic type!");
1657 break;
1658 case Instruction::FRem:
1659 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1660 assert((C1->getType()->isFloatingPoint() || (isa<PackedType>(C1->getType())
1661 && cast<PackedType>(C1->getType())->getElementType()->isFloatingPoint()))
1662 && "Tried to create an arithmetic operation on a non-arithmetic type!");
1663 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001664 case Instruction::And:
1665 case Instruction::Or:
1666 case Instruction::Xor:
1667 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattnerc421a262006-01-04 01:01:04 +00001668 assert((C1->getType()->isIntegral() || isa<PackedType>(C1->getType())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001669 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001670 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001671 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001672 case Instruction::LShr:
1673 case Instruction::AShr:
Reid Spencer8d9336d2006-12-31 05:26:44 +00001674 assert(C2->getType() == Type::Int8Ty && "Shift should be by ubyte!");
Reid Spencerfdff9382006-11-08 06:47:33 +00001675 assert(C1->getType()->isInteger() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001676 "Tried to create a shift operation on a non-integer type!");
1677 break;
1678 default:
1679 break;
1680 }
1681#endif
1682
Reid Spencera009d0d2006-12-04 21:35:24 +00001683 return getTy(C1->getType(), Opcode, C1, C2);
1684}
1685
Reid Spencer266e42b2006-12-23 06:05:41 +00001686Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00001687 Constant *C1, Constant *C2) {
1688 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001689 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00001690}
1691
Chris Lattner6e415c02004-03-12 05:54:04 +00001692Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
1693 Constant *V1, Constant *V2) {
1694 assert(C->getType() == Type::BoolTy && "Select condition must be bool!");
1695 assert(V1->getType() == V2->getType() && "Select value types must match!");
1696 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
1697
1698 if (ReqTy == V1->getType())
1699 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1700 return SC; // Fold common cases
1701
1702 std::vector<Constant*> argVec(3, C);
1703 argVec[1] = V1;
1704 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00001705 ExprMapKeyType Key(Instruction::Select, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001706 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00001707}
1708
Chris Lattner9eb2b522004-01-12 19:12:58 +00001709/// getShiftTy - Return a shift left or shift right constant expr
Chris Lattnerb50d1352003-10-05 00:17:43 +00001710Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
1711 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001712 // Check the operands for consistency first
Reid Spencerfdff9382006-11-08 06:47:33 +00001713 assert((Opcode == Instruction::Shl ||
1714 Opcode == Instruction::LShr ||
1715 Opcode == Instruction::AShr) &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001716 "Invalid opcode in binary constant expression");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001717 assert(C1->getType()->isIntegral() && C2->getType() == Type::Int8Ty &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001718 "Invalid operand types for Shift constant expr!");
1719
Chris Lattner0bba7712004-01-12 20:40:42 +00001720 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001721 return FC; // Fold a few common cases...
1722
1723 // Look up the constant in the table first to ensure uniqueness
1724 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spenceree3c9912006-12-04 05:19:50 +00001725 ExprMapKeyType Key(Opcode, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001726 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001727}
1728
Chris Lattnerb50d1352003-10-05 00:17:43 +00001729Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner13128ab2004-10-11 22:52:25 +00001730 const std::vector<Value*> &IdxList) {
1731 assert(GetElementPtrInst::getIndexedType(C->getType(), IdxList, true) &&
Chris Lattner04b60fe2004-02-16 20:46:13 +00001732 "GEP indices invalid!");
1733
Chris Lattneracdbe712003-04-17 19:24:48 +00001734 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
1735 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001736
Chris Lattnerb50d1352003-10-05 00:17:43 +00001737 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001738 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001739 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00001740 std::vector<Constant*> ArgVec;
1741 ArgVec.reserve(IdxList.size()+1);
1742 ArgVec.push_back(C);
1743 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
1744 ArgVec.push_back(cast<Constant>(IdxList[i]));
Reid Spenceree3c9912006-12-04 05:19:50 +00001745 const ExprMapKeyType Key(Instruction::GetElementPtr,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001746 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001747}
1748
Chris Lattnerb50d1352003-10-05 00:17:43 +00001749Constant *ConstantExpr::getGetElementPtr(Constant *C,
1750 const std::vector<Constant*> &IdxList){
1751 // Get the result type of the getelementptr!
1752 std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
1753
1754 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
1755 true);
1756 assert(Ty && "GEP indices invalid!");
Chris Lattner13128ab2004-10-11 22:52:25 +00001757 return getGetElementPtrTy(PointerType::get(Ty), C, VIdxList);
1758}
1759
1760Constant *ConstantExpr::getGetElementPtr(Constant *C,
1761 const std::vector<Value*> &IdxList) {
1762 // Get the result type of the getelementptr!
1763 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
1764 true);
1765 assert(Ty && "GEP indices invalid!");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001766 return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
1767}
1768
Reid Spenceree3c9912006-12-04 05:19:50 +00001769Constant *
1770ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1771 assert(LHS->getType() == RHS->getType());
1772 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
1773 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
1774
Reid Spencer266e42b2006-12-23 06:05:41 +00001775 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001776 return FC; // Fold a few common cases...
1777
1778 // Look up the constant in the table first to ensure uniqueness
1779 std::vector<Constant*> ArgVec;
1780 ArgVec.push_back(LHS);
1781 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001782 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001783 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
1784 return ExprConstants->getOrCreate(Type::BoolTy, Key);
1785}
1786
1787Constant *
1788ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1789 assert(LHS->getType() == RHS->getType());
1790 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
1791
Reid Spencer266e42b2006-12-23 06:05:41 +00001792 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001793 return FC; // Fold a few common cases...
1794
1795 // Look up the constant in the table first to ensure uniqueness
1796 std::vector<Constant*> ArgVec;
1797 ArgVec.push_back(LHS);
1798 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001799 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001800 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
1801 return ExprConstants->getOrCreate(Type::BoolTy, Key);
1802}
1803
Robert Bocchino23004482006-01-10 19:05:34 +00001804Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
1805 Constant *Idx) {
Robert Bocchinode7f1c92006-01-10 20:03:46 +00001806 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
1807 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00001808 // Look up the constant in the table first to ensure uniqueness
1809 std::vector<Constant*> ArgVec(1, Val);
1810 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001811 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001812 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00001813}
1814
1815Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
1816 assert(isa<PackedType>(Val->getType()) &&
1817 "Tried to create extractelement operation on non-packed type!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001818 assert(Idx->getType() == Type::Int32Ty &&
Robert Bocchinoca27f032006-01-17 20:07:22 +00001819 "Extractelement index must be uint type!");
Robert Bocchino23004482006-01-10 19:05:34 +00001820 return getExtractElementTy(cast<PackedType>(Val->getType())->getElementType(),
1821 Val, Idx);
1822}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001823
Robert Bocchinoca27f032006-01-17 20:07:22 +00001824Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
1825 Constant *Elt, Constant *Idx) {
1826 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
1827 return FC; // Fold a few common cases...
1828 // Look up the constant in the table first to ensure uniqueness
1829 std::vector<Constant*> ArgVec(1, Val);
1830 ArgVec.push_back(Elt);
1831 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001832 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001833 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001834}
1835
1836Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1837 Constant *Idx) {
1838 assert(isa<PackedType>(Val->getType()) &&
1839 "Tried to create insertelement operation on non-packed type!");
1840 assert(Elt->getType() == cast<PackedType>(Val->getType())->getElementType()
1841 && "Insertelement types must match!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001842 assert(Idx->getType() == Type::Int32Ty &&
Robert Bocchinoca27f032006-01-17 20:07:22 +00001843 "Insertelement index must be uint type!");
1844 return getInsertElementTy(cast<PackedType>(Val->getType())->getElementType(),
1845 Val, Elt, Idx);
1846}
1847
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001848Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
1849 Constant *V2, Constant *Mask) {
1850 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
1851 return FC; // Fold a few common cases...
1852 // Look up the constant in the table first to ensure uniqueness
1853 std::vector<Constant*> ArgVec(1, V1);
1854 ArgVec.push_back(V2);
1855 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00001856 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001857 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001858}
1859
1860Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
1861 Constant *Mask) {
1862 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
1863 "Invalid shuffle vector constant expr operands!");
1864 return getShuffleVectorTy(V1->getType(), V1, V2, Mask);
1865}
1866
Vikram S. Adve4c485332002-07-15 18:19:33 +00001867// destroyConstant - Remove the constant from the constant table...
1868//
1869void ConstantExpr::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001870 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001871 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001872}
1873
Chris Lattner3cd8c562002-07-30 18:54:25 +00001874const char *ConstantExpr::getOpcodeName() const {
1875 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001876}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00001877
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001878//===----------------------------------------------------------------------===//
1879// replaceUsesOfWithOnConstant implementations
1880
1881void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001882 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001883 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00001884 Constant *ToC = cast<Constant>(To);
Chris Lattnerdff59112005-10-04 18:47:09 +00001885
1886 unsigned OperandToUpdate = U-OperandList;
1887 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1888
Jim Laskeyc03caef2006-07-17 17:38:29 +00001889 std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnerb64419a2005-10-03 22:51:37 +00001890 Lookup.first.first = getType();
1891 Lookup.second = this;
Chris Lattnerdff59112005-10-04 18:47:09 +00001892
Chris Lattnerb64419a2005-10-03 22:51:37 +00001893 std::vector<Constant*> &Values = Lookup.first.second;
1894 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattnerdff59112005-10-04 18:47:09 +00001895
Chris Lattner8760ec72005-10-04 01:17:50 +00001896 // Fill values with the modified operands of the constant array. Also,
1897 // compute whether this turns into an all-zeros array.
Chris Lattnerdff59112005-10-04 18:47:09 +00001898 bool isAllZeros = false;
1899 if (!ToC->isNullValue()) {
1900 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1901 Values.push_back(cast<Constant>(O->get()));
1902 } else {
1903 isAllZeros = true;
1904 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1905 Constant *Val = cast<Constant>(O->get());
1906 Values.push_back(Val);
1907 if (isAllZeros) isAllZeros = Val->isNullValue();
1908 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001909 }
Chris Lattnerdff59112005-10-04 18:47:09 +00001910 Values[OperandToUpdate] = ToC;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001911
Chris Lattnerb64419a2005-10-03 22:51:37 +00001912 Constant *Replacement = 0;
1913 if (isAllZeros) {
1914 Replacement = ConstantAggregateZero::get(getType());
1915 } else {
1916 // Check to see if we have this array type already.
1917 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00001918 ArrayConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00001919 ArrayConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001920
1921 if (Exists) {
1922 Replacement = I->second;
1923 } else {
1924 // Okay, the new shape doesn't exist in the system yet. Instead of
1925 // creating a new constant array, inserting it, replaceallusesof'ing the
1926 // old with the new, then deleting the old... just update the current one
1927 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00001928 ArrayConstants->MoveConstantToNewSlot(this, I);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001929
Chris Lattnerdff59112005-10-04 18:47:09 +00001930 // Update to the new value.
1931 setOperand(OperandToUpdate, ToC);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001932 return;
1933 }
1934 }
1935
1936 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001937 assert(Replacement != this && "I didn't contain From!");
1938
Chris Lattner7a1450d2005-10-04 18:13:04 +00001939 // Everyone using this now uses the replacement.
1940 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001941
1942 // Delete the old constant!
1943 destroyConstant();
1944}
1945
1946void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001947 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001948 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00001949 Constant *ToC = cast<Constant>(To);
1950
Chris Lattnerdff59112005-10-04 18:47:09 +00001951 unsigned OperandToUpdate = U-OperandList;
1952 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1953
Jim Laskeyc03caef2006-07-17 17:38:29 +00001954 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattner8760ec72005-10-04 01:17:50 +00001955 Lookup.first.first = getType();
1956 Lookup.second = this;
1957 std::vector<Constant*> &Values = Lookup.first.second;
1958 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001959
Chris Lattnerdff59112005-10-04 18:47:09 +00001960
Chris Lattner8760ec72005-10-04 01:17:50 +00001961 // Fill values with the modified operands of the constant struct. Also,
1962 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00001963 bool isAllZeros = false;
1964 if (!ToC->isNullValue()) {
1965 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1966 Values.push_back(cast<Constant>(O->get()));
1967 } else {
1968 isAllZeros = true;
1969 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1970 Constant *Val = cast<Constant>(O->get());
1971 Values.push_back(Val);
1972 if (isAllZeros) isAllZeros = Val->isNullValue();
1973 }
Chris Lattner8760ec72005-10-04 01:17:50 +00001974 }
Chris Lattnerdff59112005-10-04 18:47:09 +00001975 Values[OperandToUpdate] = ToC;
1976
Chris Lattner8760ec72005-10-04 01:17:50 +00001977 Constant *Replacement = 0;
1978 if (isAllZeros) {
1979 Replacement = ConstantAggregateZero::get(getType());
1980 } else {
1981 // Check to see if we have this array type already.
1982 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00001983 StructConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00001984 StructConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00001985
1986 if (Exists) {
1987 Replacement = I->second;
1988 } else {
1989 // Okay, the new shape doesn't exist in the system yet. Instead of
1990 // creating a new constant struct, inserting it, replaceallusesof'ing the
1991 // old with the new, then deleting the old... just update the current one
1992 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00001993 StructConstants->MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00001994
Chris Lattnerdff59112005-10-04 18:47:09 +00001995 // Update to the new value.
1996 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00001997 return;
1998 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001999 }
2000
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002001 assert(Replacement != this && "I didn't contain From!");
2002
Chris Lattner7a1450d2005-10-04 18:13:04 +00002003 // Everyone using this now uses the replacement.
2004 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002005
2006 // Delete the old constant!
2007 destroyConstant();
2008}
2009
2010void ConstantPacked::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002011 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002012 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2013
2014 std::vector<Constant*> Values;
2015 Values.reserve(getNumOperands()); // Build replacement array...
2016 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2017 Constant *Val = getOperand(i);
2018 if (Val == From) Val = cast<Constant>(To);
2019 Values.push_back(Val);
2020 }
2021
2022 Constant *Replacement = ConstantPacked::get(getType(), Values);
2023 assert(Replacement != this && "I didn't contain From!");
2024
Chris Lattner7a1450d2005-10-04 18:13:04 +00002025 // Everyone using this now uses the replacement.
2026 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002027
2028 // Delete the old constant!
2029 destroyConstant();
2030}
2031
2032void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002033 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002034 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2035 Constant *To = cast<Constant>(ToV);
2036
2037 Constant *Replacement = 0;
2038 if (getOpcode() == Instruction::GetElementPtr) {
2039 std::vector<Constant*> Indices;
2040 Constant *Pointer = getOperand(0);
2041 Indices.reserve(getNumOperands()-1);
2042 if (Pointer == From) Pointer = To;
2043
2044 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2045 Constant *Val = getOperand(i);
2046 if (Val == From) Val = To;
2047 Indices.push_back(Val);
2048 }
2049 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002050 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002051 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002052 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002053 } else if (getOpcode() == Instruction::Select) {
2054 Constant *C1 = getOperand(0);
2055 Constant *C2 = getOperand(1);
2056 Constant *C3 = getOperand(2);
2057 if (C1 == From) C1 = To;
2058 if (C2 == From) C2 = To;
2059 if (C3 == From) C3 = To;
2060 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00002061 } else if (getOpcode() == Instruction::ExtractElement) {
2062 Constant *C1 = getOperand(0);
2063 Constant *C2 = getOperand(1);
2064 if (C1 == From) C1 = To;
2065 if (C2 == From) C2 = To;
2066 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00002067 } else if (getOpcode() == Instruction::InsertElement) {
2068 Constant *C1 = getOperand(0);
2069 Constant *C2 = getOperand(1);
2070 Constant *C3 = getOperand(1);
2071 if (C1 == From) C1 = To;
2072 if (C2 == From) C2 = To;
2073 if (C3 == From) C3 = To;
2074 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2075 } else if (getOpcode() == Instruction::ShuffleVector) {
2076 Constant *C1 = getOperand(0);
2077 Constant *C2 = getOperand(1);
2078 Constant *C3 = getOperand(2);
2079 if (C1 == From) C1 = To;
2080 if (C2 == From) C2 = To;
2081 if (C3 == From) C3 = To;
2082 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00002083 } else if (isCompare()) {
2084 Constant *C1 = getOperand(0);
2085 Constant *C2 = getOperand(1);
2086 if (C1 == From) C1 = To;
2087 if (C2 == From) C2 = To;
2088 if (getOpcode() == Instruction::ICmp)
2089 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
2090 else
2091 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002092 } else if (getNumOperands() == 2) {
2093 Constant *C1 = getOperand(0);
2094 Constant *C2 = getOperand(1);
2095 if (C1 == From) C1 = To;
2096 if (C2 == From) C2 = To;
2097 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2098 } else {
2099 assert(0 && "Unknown ConstantExpr type!");
2100 return;
2101 }
2102
2103 assert(Replacement != this && "I didn't contain From!");
2104
Chris Lattner7a1450d2005-10-04 18:13:04 +00002105 // Everyone using this now uses the replacement.
2106 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002107
2108 // Delete the old constant!
2109 destroyConstant();
2110}
2111
2112
Jim Laskey2698f0d2006-03-08 18:11:07 +00002113/// getStringValue - Turn an LLVM constant pointer that eventually points to a
2114/// global into a string value. Return an empty string if we can't do it.
Evan Cheng38280c02006-03-10 23:52:03 +00002115/// Parameter Chop determines if the result is chopped at the first null
2116/// terminator.
Jim Laskey2698f0d2006-03-08 18:11:07 +00002117///
Evan Cheng38280c02006-03-10 23:52:03 +00002118std::string Constant::getStringValue(bool Chop, unsigned Offset) {
Jim Laskey2698f0d2006-03-08 18:11:07 +00002119 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(this)) {
2120 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
2121 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
2122 if (Init->isString()) {
2123 std::string Result = Init->getAsString();
2124 if (Offset < Result.size()) {
2125 // If we are pointing INTO The string, erase the beginning...
2126 Result.erase(Result.begin(), Result.begin()+Offset);
2127
2128 // Take off the null terminator, and any string fragments after it.
Evan Cheng38280c02006-03-10 23:52:03 +00002129 if (Chop) {
2130 std::string::size_type NullPos = Result.find_first_of((char)0);
2131 if (NullPos != std::string::npos)
2132 Result.erase(Result.begin()+NullPos, Result.end());
2133 }
Jim Laskey2698f0d2006-03-08 18:11:07 +00002134 return Result;
2135 }
2136 }
2137 }
2138 } else if (Constant *C = dyn_cast<Constant>(this)) {
2139 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
Evan Cheng2c5e5302006-03-11 00:13:10 +00002140 return GV->getStringValue(Chop, Offset);
Jim Laskey2698f0d2006-03-08 18:11:07 +00002141 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
2142 if (CE->getOpcode() == Instruction::GetElementPtr) {
2143 // Turn a gep into the specified offset.
2144 if (CE->getNumOperands() == 3 &&
2145 cast<Constant>(CE->getOperand(1))->isNullValue() &&
2146 isa<ConstantInt>(CE->getOperand(2))) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00002147 Offset += cast<ConstantInt>(CE->getOperand(2))->getZExtValue();
Evan Cheng2c5e5302006-03-11 00:13:10 +00002148 return CE->getOperand(0)->getStringValue(Chop, Offset);
Jim Laskey2698f0d2006-03-08 18:11:07 +00002149 }
2150 }
2151 }
2152 }
2153 return "";
2154}