blob: 73490912a5f47dbd60b165123edca8a09a90d236 [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()) {
Reid Spencer542964f2007-01-11 18:21:29 +000095 case Type::Int1TyID: {
Reid Spencercddc9df2007-01-12 04:24:46 +000096 static Constant *NullBool = ConstantInt::get(Type::Int1Ty, false);
Chris Lattner3e88ef92003-10-03 19:34:51 +000097 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
Zhou Sheng75b871f2007-01-11 12:24:14 +0000138ConstantInt *ConstantInt::getAllOnesValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000139 switch (Ty->getTypeID()) {
Reid Spencer542964f2007-01-11 18:21:29 +0000140 case Type::Int1TyID: return ConstantInt::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 Lattnerecab54c2007-01-04 01:49:26 +0000149/// @returns the value for an packed integer constant of the given type that
150/// has all its bits set to true.
151/// @brief Get the all ones value
152ConstantPacked *ConstantPacked::getAllOnesValue(const PackedType *Ty) {
153 std::vector<Constant*> Elts;
154 Elts.resize(Ty->getNumElements(),
Zhou Sheng75b871f2007-01-11 12:24:14 +0000155 ConstantInt::getAllOnesValue(Ty->getElementType()));
Chris Lattnerecab54c2007-01-04 01:49:26 +0000156 assert(Elts[0] && "Not a packed integer type!");
157 return cast<ConstantPacked>(ConstantPacked::get(Elts));
158}
159
160
Chris Lattner2f7c9632001-06-06 20:29:01 +0000161//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +0000162// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +0000163//===----------------------------------------------------------------------===//
164
165//===----------------------------------------------------------------------===//
166// Normal Constructors
167
Zhou Sheng75b871f2007-01-11 12:24:14 +0000168ConstantInt::ConstantInt(bool V)
Reid Spencer542964f2007-01-11 18:21:29 +0000169 : Constant(Type::Int1Ty, ConstantIntVal, 0, 0), Val(uint64_t(V)) {
Chris Lattner265eb642004-06-21 12:12:12 +0000170}
171
Reid Spencere0fc4df2006-10-20 07:07:24 +0000172ConstantInt::ConstantInt(const Type *Ty, uint64_t V)
Reid Spencer542964f2007-01-11 18:21:29 +0000173 : Constant(Ty, ConstantIntVal, 0, 0), Val(Ty == Type::Int1Ty ? bool(V) : V) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000174}
175
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000176ConstantFP::ConstantFP(const Type *Ty, double V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000177 : Constant(Ty, ConstantFPVal, 0, 0) {
Chris Lattner9655e542001-07-20 19:16:02 +0000178 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000179 Val = V;
180}
181
Chris Lattner3462ae32001-12-03 22:26:30 +0000182ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000183 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000184 : Constant(T, ConstantArrayVal, new Use[V.size()], V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000185 assert(V.size() == T->getNumElements() &&
186 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000187 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000188 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
189 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000190 Constant *C = *I;
191 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000192 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000193 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000194 "Initializer for array element doesn't match array element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000195 OL->init(C, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000196 }
197}
198
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000199ConstantArray::~ConstantArray() {
200 delete [] OperandList;
201}
202
Chris Lattner3462ae32001-12-03 22:26:30 +0000203ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000204 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000205 : Constant(T, ConstantStructVal, new Use[V.size()], V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000206 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000207 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000208 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000209 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
210 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000211 Constant *C = *I;
212 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000213 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000214 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000215 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000216 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000217 "Initializer for struct element doesn't match struct element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000218 OL->init(C, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000219 }
220}
221
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000222ConstantStruct::~ConstantStruct() {
223 delete [] OperandList;
224}
225
226
Brian Gaeke02209042004-08-20 06:00:58 +0000227ConstantPacked::ConstantPacked(const PackedType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000228 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000229 : Constant(T, ConstantPackedVal, new Use[V.size()], V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000230 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000231 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
232 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000233 Constant *C = *I;
234 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000235 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000236 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000237 "Initializer for packed element doesn't match packed element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000238 OL->init(C, this);
Brian Gaeke02209042004-08-20 06:00:58 +0000239 }
240}
241
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000242ConstantPacked::~ConstantPacked() {
243 delete [] OperandList;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000244}
245
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000246// We declare several classes private to this file, so use an anonymous
247// namespace
248namespace {
249
250/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
251/// behind the scenes to implement unary constant exprs.
252class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
253 Use Op;
254public:
255 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
256 : ConstantExpr(Ty, Opcode, &Op, 1), Op(C, this) {}
257};
258
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000259/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
260/// behind the scenes to implement binary constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000261class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000262 Use Ops[2];
263public:
264 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Reid Spencer266e42b2006-12-23 06:05:41 +0000265 : ConstantExpr(C1->getType(), Opcode, Ops, 2) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000266 Ops[0].init(C1, this);
267 Ops[1].init(C2, this);
268 }
269};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000270
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000271/// SelectConstantExpr - This class is private to Constants.cpp, and is used
272/// behind the scenes to implement select constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000273class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000274 Use Ops[3];
275public:
276 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
277 : ConstantExpr(C2->getType(), Instruction::Select, Ops, 3) {
278 Ops[0].init(C1, this);
279 Ops[1].init(C2, this);
280 Ops[2].init(C3, this);
281 }
282};
283
Robert Bocchinoca27f032006-01-17 20:07:22 +0000284/// ExtractElementConstantExpr - This class is private to
285/// Constants.cpp, and is used behind the scenes to implement
286/// extractelement constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000287class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Robert Bocchino23004482006-01-10 19:05:34 +0000288 Use Ops[2];
289public:
290 ExtractElementConstantExpr(Constant *C1, Constant *C2)
291 : ConstantExpr(cast<PackedType>(C1->getType())->getElementType(),
292 Instruction::ExtractElement, Ops, 2) {
293 Ops[0].init(C1, this);
294 Ops[1].init(C2, this);
295 }
296};
297
Robert Bocchinoca27f032006-01-17 20:07:22 +0000298/// InsertElementConstantExpr - This class is private to
299/// Constants.cpp, and is used behind the scenes to implement
300/// insertelement constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000301class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Robert Bocchinoca27f032006-01-17 20:07:22 +0000302 Use Ops[3];
303public:
304 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
305 : ConstantExpr(C1->getType(), Instruction::InsertElement,
306 Ops, 3) {
307 Ops[0].init(C1, this);
308 Ops[1].init(C2, this);
309 Ops[2].init(C3, this);
310 }
311};
312
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000313/// ShuffleVectorConstantExpr - This class is private to
314/// Constants.cpp, and is used behind the scenes to implement
315/// shufflevector constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000316class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000317 Use Ops[3];
318public:
319 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
320 : ConstantExpr(C1->getType(), Instruction::ShuffleVector,
321 Ops, 3) {
322 Ops[0].init(C1, this);
323 Ops[1].init(C2, this);
324 Ops[2].init(C3, this);
325 }
326};
327
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000328/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
329/// used behind the scenes to implement getelementpr constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000330struct VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000331 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
332 const Type *DestTy)
333 : ConstantExpr(DestTy, Instruction::GetElementPtr,
334 new Use[IdxList.size()+1], IdxList.size()+1) {
335 OperandList[0].init(C, this);
336 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
337 OperandList[i+1].init(IdxList[i], this);
338 }
339 ~GetElementPtrConstantExpr() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000340 delete [] OperandList;
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000341 }
342};
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000343
344// CompareConstantExpr - This class is private to Constants.cpp, and is used
345// behind the scenes to implement ICmp and FCmp constant expressions. This is
346// needed in order to store the predicate value for these instructions.
347struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr {
348 unsigned short predicate;
349 Use Ops[2];
350 CompareConstantExpr(Instruction::OtherOps opc, unsigned short pred,
351 Constant* LHS, Constant* RHS)
Reid Spencer542964f2007-01-11 18:21:29 +0000352 : ConstantExpr(Type::Int1Ty, opc, Ops, 2), predicate(pred) {
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000353 OperandList[0].init(LHS, this);
354 OperandList[1].init(RHS, this);
355 }
356};
357
358} // end anonymous namespace
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000359
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000360
361// Utility function for determining if a ConstantExpr is a CastOp or not. This
362// can't be inline because we don't want to #include Instruction.h into
363// Constant.h
364bool ConstantExpr::isCast() const {
365 return Instruction::isCast(getOpcode());
366}
367
Reid Spenceree3c9912006-12-04 05:19:50 +0000368bool ConstantExpr::isCompare() const {
369 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
370}
371
Chris Lattner817175f2004-03-29 02:37:53 +0000372/// ConstantExpr::get* - Return some common constants without having to
373/// specify the full Instruction::OPCODE identifier.
374///
375Constant *ConstantExpr::getNeg(Constant *C) {
Chris Lattner3cdc27c2004-03-29 19:51:24 +0000376 if (!C->getType()->isFloatingPoint())
377 return get(Instruction::Sub, getNullValue(C->getType()), C);
378 else
379 return get(Instruction::Sub, ConstantFP::get(C->getType(), -0.0), C);
Chris Lattner817175f2004-03-29 02:37:53 +0000380}
381Constant *ConstantExpr::getNot(Constant *C) {
Zhou Sheng75b871f2007-01-11 12:24:14 +0000382 assert(isa<ConstantInt>(C) && "Cannot NOT a nonintegral type!");
Chris Lattner817175f2004-03-29 02:37:53 +0000383 return get(Instruction::Xor, C,
Zhou Sheng75b871f2007-01-11 12:24:14 +0000384 ConstantInt::getAllOnesValue(C->getType()));
Chris Lattner817175f2004-03-29 02:37:53 +0000385}
386Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
387 return get(Instruction::Add, C1, C2);
388}
389Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
390 return get(Instruction::Sub, C1, C2);
391}
392Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
393 return get(Instruction::Mul, C1, C2);
394}
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000395Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2) {
396 return get(Instruction::UDiv, C1, C2);
397}
398Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2) {
399 return get(Instruction::SDiv, C1, C2);
400}
401Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) {
402 return get(Instruction::FDiv, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000403}
Reid Spencer7eb55b32006-11-02 01:53:59 +0000404Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) {
405 return get(Instruction::URem, C1, C2);
406}
407Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) {
408 return get(Instruction::SRem, C1, C2);
409}
410Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) {
411 return get(Instruction::FRem, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000412}
413Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
414 return get(Instruction::And, C1, C2);
415}
416Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
417 return get(Instruction::Or, C1, C2);
418}
419Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
420 return get(Instruction::Xor, C1, C2);
421}
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000422unsigned ConstantExpr::getPredicate() const {
423 assert(getOpcode() == Instruction::FCmp || getOpcode() == Instruction::ICmp);
424 return dynamic_cast<const CompareConstantExpr*>(this)->predicate;
425}
Chris Lattner817175f2004-03-29 02:37:53 +0000426Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
427 return get(Instruction::Shl, C1, C2);
428}
Reid Spencerfdff9382006-11-08 06:47:33 +0000429Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2) {
430 return get(Instruction::LShr, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000431}
Reid Spencerfdff9382006-11-08 06:47:33 +0000432Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2) {
433 return get(Instruction::AShr, C1, C2);
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000434}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000435
Chris Lattner7c1018a2006-07-14 19:37:40 +0000436/// getWithOperandReplaced - Return a constant expression identical to this
437/// one, but with the specified operand set to the specified value.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000438Constant *
439ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +0000440 assert(OpNo < getNumOperands() && "Operand num is out of range!");
441 assert(Op->getType() == getOperand(OpNo)->getType() &&
442 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000443 if (getOperand(OpNo) == Op)
444 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000445
Chris Lattner227816342006-07-14 22:20:01 +0000446 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000447 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000448 case Instruction::Trunc:
449 case Instruction::ZExt:
450 case Instruction::SExt:
451 case Instruction::FPTrunc:
452 case Instruction::FPExt:
453 case Instruction::UIToFP:
454 case Instruction::SIToFP:
455 case Instruction::FPToUI:
456 case Instruction::FPToSI:
457 case Instruction::PtrToInt:
458 case Instruction::IntToPtr:
459 case Instruction::BitCast:
460 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000461 case Instruction::Select:
462 Op0 = (OpNo == 0) ? Op : getOperand(0);
463 Op1 = (OpNo == 1) ? Op : getOperand(1);
464 Op2 = (OpNo == 2) ? Op : getOperand(2);
465 return ConstantExpr::getSelect(Op0, Op1, Op2);
466 case Instruction::InsertElement:
467 Op0 = (OpNo == 0) ? Op : getOperand(0);
468 Op1 = (OpNo == 1) ? Op : getOperand(1);
469 Op2 = (OpNo == 2) ? Op : getOperand(2);
470 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
471 case Instruction::ExtractElement:
472 Op0 = (OpNo == 0) ? Op : getOperand(0);
473 Op1 = (OpNo == 1) ? Op : getOperand(1);
474 return ConstantExpr::getExtractElement(Op0, Op1);
475 case Instruction::ShuffleVector:
476 Op0 = (OpNo == 0) ? Op : getOperand(0);
477 Op1 = (OpNo == 1) ? Op : getOperand(1);
478 Op2 = (OpNo == 2) ? Op : getOperand(2);
479 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000480 case Instruction::GetElementPtr: {
481 std::vector<Constant*> Ops;
482 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
483 Ops.push_back(getOperand(i));
484 if (OpNo == 0)
485 return ConstantExpr::getGetElementPtr(Op, Ops);
486 Ops[OpNo-1] = Op;
487 return ConstantExpr::getGetElementPtr(getOperand(0), Ops);
488 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000489 default:
490 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000491 Op0 = (OpNo == 0) ? Op : getOperand(0);
492 Op1 = (OpNo == 1) ? Op : getOperand(1);
493 return ConstantExpr::get(getOpcode(), Op0, Op1);
494 }
495}
496
497/// getWithOperands - This returns the current constant expression with the
498/// operands replaced with the specified values. The specified operands must
499/// match count and type with the existing ones.
500Constant *ConstantExpr::
501getWithOperands(const std::vector<Constant*> &Ops) const {
502 assert(Ops.size() == getNumOperands() && "Operand count mismatch!");
503 bool AnyChange = false;
504 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
505 assert(Ops[i]->getType() == getOperand(i)->getType() &&
506 "Operand type mismatch!");
507 AnyChange |= Ops[i] != getOperand(i);
508 }
509 if (!AnyChange) // No operands changed, return self.
510 return const_cast<ConstantExpr*>(this);
511
512 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000513 case Instruction::Trunc:
514 case Instruction::ZExt:
515 case Instruction::SExt:
516 case Instruction::FPTrunc:
517 case Instruction::FPExt:
518 case Instruction::UIToFP:
519 case Instruction::SIToFP:
520 case Instruction::FPToUI:
521 case Instruction::FPToSI:
522 case Instruction::PtrToInt:
523 case Instruction::IntToPtr:
524 case Instruction::BitCast:
525 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattner227816342006-07-14 22:20:01 +0000526 case Instruction::Select:
527 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
528 case Instruction::InsertElement:
529 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
530 case Instruction::ExtractElement:
531 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
532 case Instruction::ShuffleVector:
533 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
534 case Instruction::GetElementPtr: {
535 std::vector<Constant*> ActualOps(Ops.begin()+1, Ops.end());
536 return ConstantExpr::getGetElementPtr(Ops[0], ActualOps);
537 }
Reid Spencer266e42b2006-12-23 06:05:41 +0000538 case Instruction::ICmp:
539 case Instruction::FCmp:
540 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattner227816342006-07-14 22:20:01 +0000541 default:
542 assert(getNumOperands() == 2 && "Must be binary operator?");
543 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000544 }
545}
546
Chris Lattner2f7c9632001-06-06 20:29:01 +0000547
548//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000549// isValueValidForType implementations
550
Reid Spencere7334722006-12-19 01:28:19 +0000551bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
552 switch (Ty->getTypeID()) {
553 default: return false; // These can't be represented as integers!
Reid Spencer542964f2007-01-11 18:21:29 +0000554 case Type::Int1TyID: return Val == 0 || Val == 1;
Reid Spencer8d9336d2006-12-31 05:26:44 +0000555 case Type::Int8TyID: return Val <= UINT8_MAX;
556 case Type::Int16TyID: return Val <= UINT16_MAX;
557 case Type::Int32TyID: return Val <= UINT32_MAX;
558 case Type::Int64TyID: return true; // always true, has to fit in largest type
Reid Spencere7334722006-12-19 01:28:19 +0000559 }
560}
561
Reid Spencere0fc4df2006-10-20 07:07:24 +0000562bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000563 switch (Ty->getTypeID()) {
Reid Spencere7334722006-12-19 01:28:19 +0000564 default: return false; // These can't be represented as integers!
Reid Spencer542964f2007-01-11 18:21:29 +0000565 case Type::Int1TyID: return (Val == 0 || Val == 1);
Reid Spencer8d9336d2006-12-31 05:26:44 +0000566 case Type::Int8TyID: return (Val >= INT8_MIN && Val <= INT8_MAX);
567 case Type::Int16TyID: return (Val >= INT16_MIN && Val <= UINT16_MAX);
568 case Type::Int32TyID: return (Val >= INT32_MIN && Val <= UINT32_MAX);
569 case Type::Int64TyID: return true; // always true, has to fit in largest type
Chris Lattner2f7c9632001-06-06 20:29:01 +0000570 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000571}
572
Chris Lattner3462ae32001-12-03 22:26:30 +0000573bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000574 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000575 default:
576 return false; // These can't be represented as floating point!
577
Reid Spencerb95f8ab2004-12-07 07:38:08 +0000578 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000579 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000580 case Type::DoubleTyID:
581 return true; // This is the largest type...
582 }
Chris Lattneraa2372562006-05-24 17:04:05 +0000583}
Chris Lattner9655e542001-07-20 19:16:02 +0000584
Chris Lattner49d855c2001-09-07 16:46:31 +0000585//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000586// Factory Function Implementation
587
Chris Lattner98fa07b2003-05-23 20:03:32 +0000588// ConstantCreator - A class that is used to create constants by
589// ValueMap*. This class should be partially specialized if there is
590// something strange that needs to be done to interface to the ctor for the
591// constant.
592//
Chris Lattner189d19f2003-11-21 20:23:48 +0000593namespace llvm {
594 template<class ConstantClass, class TypeClass, class ValType>
Chris Lattner02157b02006-06-28 21:38:54 +0000595 struct VISIBILITY_HIDDEN ConstantCreator {
Chris Lattner189d19f2003-11-21 20:23:48 +0000596 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
597 return new ConstantClass(Ty, V);
598 }
599 };
Misha Brukmanb1c93172005-04-21 23:48:37 +0000600
Chris Lattner189d19f2003-11-21 20:23:48 +0000601 template<class ConstantClass, class TypeClass>
Chris Lattner02157b02006-06-28 21:38:54 +0000602 struct VISIBILITY_HIDDEN ConvertConstantType {
Chris Lattner189d19f2003-11-21 20:23:48 +0000603 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
604 assert(0 && "This type cannot be converted!\n");
605 abort();
606 }
607 };
Chris Lattnerb50d1352003-10-05 00:17:43 +0000608
Chris Lattner935aa922005-10-04 17:48:46 +0000609 template<class ValType, class TypeClass, class ConstantClass,
610 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattner02157b02006-06-28 21:38:54 +0000611 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
Chris Lattnerb64419a2005-10-03 22:51:37 +0000612 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000613 typedef std::pair<const Type*, ValType> MapKey;
614 typedef std::map<MapKey, Constant *> MapTy;
615 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
616 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
Chris Lattnerb64419a2005-10-03 22:51:37 +0000617 private:
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000618 /// Map - This is the main map from the element descriptor to the Constants.
619 /// This is the primary way we avoid creating two of the same shape
620 /// constant.
Chris Lattnerb50d1352003-10-05 00:17:43 +0000621 MapTy Map;
Chris Lattner935aa922005-10-04 17:48:46 +0000622
623 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
624 /// from the constants to their element in Map. This is important for
625 /// removal of constants from the array, which would otherwise have to scan
626 /// through the map with very large keys.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000627 InverseMapTy InverseMap;
Chris Lattnerb50d1352003-10-05 00:17:43 +0000628
Jim Laskeyc03caef2006-07-17 17:38:29 +0000629 /// AbstractTypeMap - Map for abstract type constants.
630 ///
Chris Lattnerb50d1352003-10-05 00:17:43 +0000631 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner99a669b2004-11-19 16:39:44 +0000632
Chris Lattner99a669b2004-11-19 16:39:44 +0000633 private:
634 void clear(std::vector<Constant *> &Constants) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000635 for(typename MapTy::iterator I = Map.begin(); I != Map.end(); ++I)
Chris Lattner99a669b2004-11-19 16:39:44 +0000636 Constants.push_back(I->second);
637 Map.clear();
638 AbstractTypeMap.clear();
Chris Lattner935aa922005-10-04 17:48:46 +0000639 InverseMap.clear();
Chris Lattner99a669b2004-11-19 16:39:44 +0000640 }
641
Chris Lattner98fa07b2003-05-23 20:03:32 +0000642 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000643 typename MapTy::iterator map_end() { return Map.end(); }
Chris Lattnerb64419a2005-10-03 22:51:37 +0000644
645 /// InsertOrGetItem - Return an iterator for the specified element.
646 /// If the element exists in the map, the returned iterator points to the
647 /// entry and Exists=true. If not, the iterator points to the newly
648 /// inserted entry and returns Exists=false. Newly inserted entries have
649 /// I->second == 0, and should be filled in.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000650 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
651 &InsertVal,
Chris Lattnerb64419a2005-10-03 22:51:37 +0000652 bool &Exists) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000653 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
Chris Lattnerb64419a2005-10-03 22:51:37 +0000654 Exists = !IP.second;
655 return IP.first;
656 }
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000657
Chris Lattner935aa922005-10-04 17:48:46 +0000658private:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000659 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
Chris Lattner935aa922005-10-04 17:48:46 +0000660 if (HasLargeKey) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000661 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
Chris Lattner935aa922005-10-04 17:48:46 +0000662 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
663 IMI->second->second == CP &&
664 "InverseMap corrupt!");
665 return IMI->second;
666 }
667
Jim Laskeyc03caef2006-07-17 17:38:29 +0000668 typename MapTy::iterator I =
Chris Lattner935aa922005-10-04 17:48:46 +0000669 Map.find(MapKey((TypeClass*)CP->getRawType(), getValType(CP)));
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000670 if (I == Map.end() || I->second != CP) {
671 // FIXME: This should not use a linear scan. If this gets to be a
672 // performance problem, someone should look at this.
673 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
674 /* empty */;
675 }
Chris Lattner935aa922005-10-04 17:48:46 +0000676 return I;
677 }
678public:
679
Chris Lattnerb64419a2005-10-03 22:51:37 +0000680 /// getOrCreate - Return the specified constant from the map, creating it if
681 /// necessary.
Chris Lattner98fa07b2003-05-23 20:03:32 +0000682 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000683 MapKey Lookup(Ty, V);
Jim Laskeyc03caef2006-07-17 17:38:29 +0000684 typename MapTy::iterator I = Map.lower_bound(Lookup);
Reid Spencere0fc4df2006-10-20 07:07:24 +0000685 // Is it in the map?
Chris Lattner98fa07b2003-05-23 20:03:32 +0000686 if (I != Map.end() && I->first == Lookup)
Reid Spencere0fc4df2006-10-20 07:07:24 +0000687 return static_cast<ConstantClass *>(I->second);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000688
689 // If no preexisting value, create one now...
690 ConstantClass *Result =
691 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
692
Chris Lattnerb50d1352003-10-05 00:17:43 +0000693 /// FIXME: why does this assert fail when loading 176.gcc?
694 //assert(Result->getType() == Ty && "Type specified is not correct!");
695 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
696
Chris Lattner935aa922005-10-04 17:48:46 +0000697 if (HasLargeKey) // Remember the reverse mapping if needed.
698 InverseMap.insert(std::make_pair(Result, I));
699
Chris Lattnerb50d1352003-10-05 00:17:43 +0000700 // If the type of the constant is abstract, make sure that an entry exists
701 // for it in the AbstractTypeMap.
702 if (Ty->isAbstract()) {
703 typename AbstractTypeMapTy::iterator TI =
704 AbstractTypeMap.lower_bound(Ty);
705
706 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
707 // Add ourselves to the ATU list of the type.
708 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
709
710 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
711 }
712 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000713 return Result;
714 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000715
Chris Lattner98fa07b2003-05-23 20:03:32 +0000716 void remove(ConstantClass *CP) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000717 typename MapTy::iterator I = FindExistingElement(CP);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000718 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +0000719 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +0000720
Chris Lattner935aa922005-10-04 17:48:46 +0000721 if (HasLargeKey) // Remember the reverse mapping if needed.
722 InverseMap.erase(CP);
723
Chris Lattnerb50d1352003-10-05 00:17:43 +0000724 // Now that we found the entry, make sure this isn't the entry that
725 // the AbstractTypeMap points to.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000726 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000727 if (Ty->isAbstract()) {
728 assert(AbstractTypeMap.count(Ty) &&
729 "Abstract type not in AbstractTypeMap?");
Jim Laskeyc03caef2006-07-17 17:38:29 +0000730 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
Chris Lattnerb50d1352003-10-05 00:17:43 +0000731 if (ATMEntryIt == I) {
732 // Yes, we are removing the representative entry for this type.
733 // See if there are any other entries of the same type.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000734 typename MapTy::iterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000735
Chris Lattnerb50d1352003-10-05 00:17:43 +0000736 // First check the entry before this one...
737 if (TmpIt != Map.begin()) {
738 --TmpIt;
739 if (TmpIt->first.first != Ty) // Not the same type, move back...
740 ++TmpIt;
741 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000742
Chris Lattnerb50d1352003-10-05 00:17:43 +0000743 // If we didn't find the same type, try to move forward...
744 if (TmpIt == ATMEntryIt) {
745 ++TmpIt;
746 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
747 --TmpIt; // No entry afterwards with the same type
748 }
749
750 // If there is another entry in the map of the same abstract type,
751 // update the AbstractTypeMap entry now.
752 if (TmpIt != ATMEntryIt) {
753 ATMEntryIt = TmpIt;
754 } else {
755 // Otherwise, we are removing the last instance of this type
756 // from the table. Remove from the ATM, and from user list.
757 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
758 AbstractTypeMap.erase(Ty);
759 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000760 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000761 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000762
Chris Lattnerb50d1352003-10-05 00:17:43 +0000763 Map.erase(I);
764 }
765
Chris Lattner3b793c62005-10-04 21:35:50 +0000766
767 /// MoveConstantToNewSlot - If we are about to change C to be the element
768 /// specified by I, update our internal data structures to reflect this
769 /// fact.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000770 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
Chris Lattner3b793c62005-10-04 21:35:50 +0000771 // First, remove the old location of the specified constant in the map.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000772 typename MapTy::iterator OldI = FindExistingElement(C);
Chris Lattner3b793c62005-10-04 21:35:50 +0000773 assert(OldI != Map.end() && "Constant not found in constant table!");
774 assert(OldI->second == C && "Didn't find correct element?");
775
776 // If this constant is the representative element for its abstract type,
777 // update the AbstractTypeMap so that the representative element is I.
778 if (C->getType()->isAbstract()) {
779 typename AbstractTypeMapTy::iterator ATI =
780 AbstractTypeMap.find(C->getType());
781 assert(ATI != AbstractTypeMap.end() &&
782 "Abstract type not in AbstractTypeMap?");
783 if (ATI->second == OldI)
784 ATI->second = I;
785 }
786
787 // Remove the old entry from the map.
788 Map.erase(OldI);
789
790 // Update the inverse map so that we know that this constant is now
791 // located at descriptor I.
792 if (HasLargeKey) {
793 assert(I->second == C && "Bad inversemap entry!");
794 InverseMap[C] = I;
795 }
796 }
797
Chris Lattnerb50d1352003-10-05 00:17:43 +0000798 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000799 typename AbstractTypeMapTy::iterator I =
Jim Laskeyc03caef2006-07-17 17:38:29 +0000800 AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +0000801
802 assert(I != AbstractTypeMap.end() &&
803 "Abstract type not in AbstractTypeMap?");
804
805 // Convert a constant at a time until the last one is gone. The last one
806 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
807 // eliminated eventually.
808 do {
809 ConvertConstantType<ConstantClass,
Jim Laskeyc03caef2006-07-17 17:38:29 +0000810 TypeClass>::convert(
811 static_cast<ConstantClass *>(I->second->second),
Chris Lattnerb50d1352003-10-05 00:17:43 +0000812 cast<TypeClass>(NewTy));
813
Jim Laskeyc03caef2006-07-17 17:38:29 +0000814 I = AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +0000815 } while (I != AbstractTypeMap.end());
816 }
817
818 // If the type became concrete without being refined to any other existing
819 // type, we just remove ourselves from the ATU list.
820 void typeBecameConcrete(const DerivedType *AbsTy) {
821 AbsTy->removeAbstractTypeUser(this);
822 }
823
824 void dump() const {
Bill Wendling6a462f12006-11-17 08:03:48 +0000825 DOUT << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +0000826 }
827 };
828}
829
Chris Lattnera84df0a22006-09-28 23:36:21 +0000830
Reid Spencere0fc4df2006-10-20 07:07:24 +0000831//---- ConstantInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000832//
Reid Spencere0fc4df2006-10-20 07:07:24 +0000833static ManagedStatic<ValueMap<uint64_t, Type, ConstantInt> > IntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000834
Reid Spencere0fc4df2006-10-20 07:07:24 +0000835// Get a ConstantInt from an int64_t. Note here that we canoncialize the value
836// to a uint64_t value that has been zero extended down to the size of the
837// integer type of the ConstantInt. This allows the getZExtValue method to
838// just return the stored value while getSExtValue has to convert back to sign
839// extended. getZExtValue is more common in LLVM than getSExtValue().
840ConstantInt *ConstantInt::get(const Type *Ty, int64_t V) {
Reid Spencercddc9df2007-01-12 04:24:46 +0000841 if (Ty == Type::Int1Ty)
842 if (V & 1)
843 return getTrue();
844 else
845 return getFalse();
Chris Lattnerf16661c2006-12-01 19:20:02 +0000846 return IntConstants->getOrCreate(Ty, V & Ty->getIntegralTypeMask());
Chris Lattner49d855c2001-09-07 16:46:31 +0000847}
848
Chris Lattner3462ae32001-12-03 22:26:30 +0000849//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000850//
Chris Lattnerac80ea42004-02-01 22:49:04 +0000851namespace llvm {
852 template<>
853 struct ConstantCreator<ConstantFP, Type, uint64_t> {
854 static ConstantFP *create(const Type *Ty, uint64_t V) {
855 assert(Ty == Type::DoubleTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000856 return new ConstantFP(Ty, BitsToDouble(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000857 }
858 };
859 template<>
860 struct ConstantCreator<ConstantFP, Type, uint32_t> {
861 static ConstantFP *create(const Type *Ty, uint32_t V) {
862 assert(Ty == Type::FloatTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000863 return new ConstantFP(Ty, BitsToFloat(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000864 }
865 };
866}
867
Chris Lattner69edc982006-09-28 00:35:06 +0000868static ManagedStatic<ValueMap<uint64_t, Type, ConstantFP> > DoubleConstants;
869static ManagedStatic<ValueMap<uint32_t, Type, ConstantFP> > FloatConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000870
Jim Laskey8ad8f712005-08-17 20:06:22 +0000871bool ConstantFP::isNullValue() const {
872 return DoubleToBits(Val) == 0;
873}
874
875bool ConstantFP::isExactlyValue(double V) const {
876 return DoubleToBits(V) == DoubleToBits(Val);
877}
878
879
Chris Lattner3462ae32001-12-03 22:26:30 +0000880ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner241ed4c2004-01-23 00:55:21 +0000881 if (Ty == Type::FloatTy) {
882 // Force the value through memory to normalize it.
Chris Lattner69edc982006-09-28 00:35:06 +0000883 return FloatConstants->getOrCreate(Ty, FloatToBits(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000884 } else {
885 assert(Ty == Type::DoubleTy);
Chris Lattner69edc982006-09-28 00:35:06 +0000886 return DoubleConstants->getOrCreate(Ty, DoubleToBits(V));
Chris Lattner241ed4c2004-01-23 00:55:21 +0000887 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000888}
889
Chris Lattner9fba3da2004-02-15 05:53:04 +0000890//---- ConstantAggregateZero::get() implementation...
891//
892namespace llvm {
893 // ConstantAggregateZero does not take extra "value" argument...
894 template<class ValType>
895 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
896 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
897 return new ConstantAggregateZero(Ty);
898 }
899 };
900
901 template<>
902 struct ConvertConstantType<ConstantAggregateZero, Type> {
903 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
904 // Make everyone now use a constant of the new type...
905 Constant *New = ConstantAggregateZero::get(NewTy);
906 assert(New != OldC && "Didn't replace constant??");
907 OldC->uncheckedReplaceAllUsesWith(New);
908 OldC->destroyConstant(); // This constant is now dead, destroy it.
909 }
910 };
911}
912
Chris Lattner69edc982006-09-28 00:35:06 +0000913static ManagedStatic<ValueMap<char, Type,
914 ConstantAggregateZero> > AggZeroConstants;
Chris Lattner9fba3da2004-02-15 05:53:04 +0000915
Chris Lattner3e650af2004-08-04 04:48:01 +0000916static char getValType(ConstantAggregateZero *CPZ) { return 0; }
917
Chris Lattner9fba3da2004-02-15 05:53:04 +0000918Constant *ConstantAggregateZero::get(const Type *Ty) {
Chris Lattnerbfd0b6d2006-06-10 04:16:23 +0000919 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<PackedType>(Ty)) &&
920 "Cannot create an aggregate zero of non-aggregate type!");
Chris Lattner69edc982006-09-28 00:35:06 +0000921 return AggZeroConstants->getOrCreate(Ty, 0);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000922}
923
924// destroyConstant - Remove the constant from the constant table...
925//
926void ConstantAggregateZero::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +0000927 AggZeroConstants->remove(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000928 destroyConstantImpl();
929}
930
Chris Lattner3462ae32001-12-03 22:26:30 +0000931//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000932//
Chris Lattner189d19f2003-11-21 20:23:48 +0000933namespace llvm {
934 template<>
935 struct ConvertConstantType<ConstantArray, ArrayType> {
936 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
937 // Make everyone now use a constant of the new type...
938 std::vector<Constant*> C;
939 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
940 C.push_back(cast<Constant>(OldC->getOperand(i)));
941 Constant *New = ConstantArray::get(NewTy, C);
942 assert(New != OldC && "Didn't replace constant??");
943 OldC->uncheckedReplaceAllUsesWith(New);
944 OldC->destroyConstant(); // This constant is now dead, destroy it.
945 }
946 };
947}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000948
Chris Lattner3e650af2004-08-04 04:48:01 +0000949static std::vector<Constant*> getValType(ConstantArray *CA) {
950 std::vector<Constant*> Elements;
951 Elements.reserve(CA->getNumOperands());
952 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
953 Elements.push_back(cast<Constant>(CA->getOperand(i)));
954 return Elements;
955}
956
Chris Lattnerb64419a2005-10-03 22:51:37 +0000957typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattner935aa922005-10-04 17:48:46 +0000958 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +0000959static ManagedStatic<ArrayConstantsTy> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000960
Chris Lattner015e8212004-02-15 04:14:47 +0000961Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +0000962 const std::vector<Constant*> &V) {
963 // If this is an all-zero array, return a ConstantAggregateZero object
964 if (!V.empty()) {
965 Constant *C = V[0];
966 if (!C->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +0000967 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000968 for (unsigned i = 1, e = V.size(); i != e; ++i)
969 if (V[i] != C)
Chris Lattner69edc982006-09-28 00:35:06 +0000970 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000971 }
972 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000973}
974
Chris Lattner98fa07b2003-05-23 20:03:32 +0000975// destroyConstant - Remove the constant from the constant table...
976//
977void ConstantArray::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +0000978 ArrayConstants->remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000979 destroyConstantImpl();
980}
981
Reid Spencer6f614532006-05-30 08:23:18 +0000982/// ConstantArray::get(const string&) - Return an array that is initialized to
983/// contain the specified string. If length is zero then a null terminator is
984/// added to the specified string so that it may be used in a natural way.
985/// Otherwise, the length parameter specifies how much of the string to use
986/// and it won't be null terminated.
987///
Reid Spencer82ebaba2006-05-30 18:15:07 +0000988Constant *ConstantArray::get(const std::string &Str, bool AddNull) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000989 std::vector<Constant*> ElementVals;
Reid Spencer82ebaba2006-05-30 18:15:07 +0000990 for (unsigned i = 0; i < Str.length(); ++i)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000991 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000992
993 // Add a null terminator to the string...
Reid Spencer82ebaba2006-05-30 18:15:07 +0000994 if (AddNull) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000995 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, 0));
Reid Spencer6f614532006-05-30 08:23:18 +0000996 }
Chris Lattner8f80fe02001-10-14 23:54:12 +0000997
Reid Spencer8d9336d2006-12-31 05:26:44 +0000998 ArrayType *ATy = ArrayType::get(Type::Int8Ty, ElementVals.size());
Chris Lattner3462ae32001-12-03 22:26:30 +0000999 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +00001000}
1001
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001002/// isString - This method returns true if the array is an array of sbyte or
1003/// ubyte, and if the elements of the array are all ConstantInt's.
1004bool ConstantArray::isString() const {
1005 // Check the element type for sbyte or ubyte...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001006 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001007 return false;
1008 // Check the elements to make sure they are all integers, not constant
1009 // expressions.
1010 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1011 if (!isa<ConstantInt>(getOperand(i)))
1012 return false;
1013 return true;
1014}
1015
Evan Cheng3763c5b2006-10-26 19:15:05 +00001016/// isCString - This method returns true if the array is a string (see
1017/// isString) and it ends in a null byte \0 and does not contains any other
1018/// null bytes except its terminator.
1019bool ConstantArray::isCString() const {
Evan Chenge974da62006-10-26 21:48:03 +00001020 // Check the element type for sbyte or ubyte...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001021 if (getType()->getElementType() != Type::Int8Ty)
Evan Chenge974da62006-10-26 21:48:03 +00001022 return false;
1023 Constant *Zero = Constant::getNullValue(getOperand(0)->getType());
1024 // Last element must be a null.
1025 if (getOperand(getNumOperands()-1) != Zero)
1026 return false;
1027 // Other elements must be non-null integers.
1028 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1029 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +00001030 return false;
Evan Chenge974da62006-10-26 21:48:03 +00001031 if (getOperand(i) == Zero)
1032 return false;
1033 }
Evan Cheng3763c5b2006-10-26 19:15:05 +00001034 return true;
1035}
1036
1037
Chris Lattner81fabb02002-08-26 17:53:56 +00001038// getAsString - If the sub-element type of this array is either sbyte or ubyte,
1039// then this method converts the array to an std::string and returns it.
1040// Otherwise, it asserts out.
1041//
1042std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001043 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001044 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +00001045 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Reid Spencere0fc4df2006-10-20 07:07:24 +00001046 Result += (char)cast<ConstantInt>(getOperand(i))->getZExtValue();
Chris Lattner81fabb02002-08-26 17:53:56 +00001047 return Result;
1048}
1049
1050
Chris Lattner3462ae32001-12-03 22:26:30 +00001051//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001052//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001053
Chris Lattner189d19f2003-11-21 20:23:48 +00001054namespace llvm {
1055 template<>
1056 struct ConvertConstantType<ConstantStruct, StructType> {
1057 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1058 // Make everyone now use a constant of the new type...
1059 std::vector<Constant*> C;
1060 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1061 C.push_back(cast<Constant>(OldC->getOperand(i)));
1062 Constant *New = ConstantStruct::get(NewTy, C);
1063 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001064
Chris Lattner189d19f2003-11-21 20:23:48 +00001065 OldC->uncheckedReplaceAllUsesWith(New);
1066 OldC->destroyConstant(); // This constant is now dead, destroy it.
1067 }
1068 };
1069}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001070
Chris Lattner8760ec72005-10-04 01:17:50 +00001071typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattner935aa922005-10-04 17:48:46 +00001072 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001073static ManagedStatic<StructConstantsTy> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001074
Chris Lattner3e650af2004-08-04 04:48:01 +00001075static std::vector<Constant*> getValType(ConstantStruct *CS) {
1076 std::vector<Constant*> Elements;
1077 Elements.reserve(CS->getNumOperands());
1078 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1079 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1080 return Elements;
1081}
1082
Chris Lattner015e8212004-02-15 04:14:47 +00001083Constant *ConstantStruct::get(const StructType *Ty,
1084 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001085 // Create a ConstantAggregateZero value if all elements are zeros...
1086 for (unsigned i = 0, e = V.size(); i != e; ++i)
1087 if (!V[i]->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001088 return StructConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001089
1090 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001091}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001092
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001093Constant *ConstantStruct::get(const std::vector<Constant*> &V, bool packed) {
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001094 std::vector<const Type*> StructEls;
1095 StructEls.reserve(V.size());
1096 for (unsigned i = 0, e = V.size(); i != e; ++i)
1097 StructEls.push_back(V[i]->getType());
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001098 return get(StructType::get(StructEls, packed), V);
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001099}
1100
Chris Lattnerd7a73302001-10-13 06:57:33 +00001101// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001102//
Chris Lattner3462ae32001-12-03 22:26:30 +00001103void ConstantStruct::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001104 StructConstants->remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001105 destroyConstantImpl();
1106}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001107
Brian Gaeke02209042004-08-20 06:00:58 +00001108//---- ConstantPacked::get() implementation...
1109//
1110namespace llvm {
1111 template<>
1112 struct ConvertConstantType<ConstantPacked, PackedType> {
1113 static void convert(ConstantPacked *OldC, const PackedType *NewTy) {
1114 // Make everyone now use a constant of the new type...
1115 std::vector<Constant*> C;
1116 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1117 C.push_back(cast<Constant>(OldC->getOperand(i)));
1118 Constant *New = ConstantPacked::get(NewTy, C);
1119 assert(New != OldC && "Didn't replace constant??");
1120 OldC->uncheckedReplaceAllUsesWith(New);
1121 OldC->destroyConstant(); // This constant is now dead, destroy it.
1122 }
1123 };
1124}
1125
1126static std::vector<Constant*> getValType(ConstantPacked *CP) {
1127 std::vector<Constant*> Elements;
1128 Elements.reserve(CP->getNumOperands());
1129 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1130 Elements.push_back(CP->getOperand(i));
1131 return Elements;
1132}
1133
Chris Lattner69edc982006-09-28 00:35:06 +00001134static ManagedStatic<ValueMap<std::vector<Constant*>, PackedType,
1135 ConstantPacked> > PackedConstants;
Brian Gaeke02209042004-08-20 06:00:58 +00001136
1137Constant *ConstantPacked::get(const PackedType *Ty,
1138 const std::vector<Constant*> &V) {
1139 // If this is an all-zero packed, return a ConstantAggregateZero object
1140 if (!V.empty()) {
1141 Constant *C = V[0];
1142 if (!C->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001143 return PackedConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001144 for (unsigned i = 1, e = V.size(); i != e; ++i)
1145 if (V[i] != C)
Chris Lattner69edc982006-09-28 00:35:06 +00001146 return PackedConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001147 }
1148 return ConstantAggregateZero::get(Ty);
1149}
1150
1151Constant *ConstantPacked::get(const std::vector<Constant*> &V) {
1152 assert(!V.empty() && "Cannot infer type if V is empty");
1153 return get(PackedType::get(V.front()->getType(),V.size()), V);
1154}
1155
1156// destroyConstant - Remove the constant from the constant table...
1157//
1158void ConstantPacked::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001159 PackedConstants->remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001160 destroyConstantImpl();
1161}
1162
Chris Lattner3462ae32001-12-03 22:26:30 +00001163//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001164//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001165
Chris Lattner189d19f2003-11-21 20:23:48 +00001166namespace llvm {
1167 // ConstantPointerNull does not take extra "value" argument...
1168 template<class ValType>
1169 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1170 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1171 return new ConstantPointerNull(Ty);
1172 }
1173 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001174
Chris Lattner189d19f2003-11-21 20:23:48 +00001175 template<>
1176 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1177 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1178 // Make everyone now use a constant of the new type...
1179 Constant *New = ConstantPointerNull::get(NewTy);
1180 assert(New != OldC && "Didn't replace constant??");
1181 OldC->uncheckedReplaceAllUsesWith(New);
1182 OldC->destroyConstant(); // This constant is now dead, destroy it.
1183 }
1184 };
1185}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001186
Chris Lattner69edc982006-09-28 00:35:06 +00001187static ManagedStatic<ValueMap<char, PointerType,
1188 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001189
Chris Lattner3e650af2004-08-04 04:48:01 +00001190static char getValType(ConstantPointerNull *) {
1191 return 0;
1192}
1193
1194
Chris Lattner3462ae32001-12-03 22:26:30 +00001195ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001196 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001197}
1198
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001199// destroyConstant - Remove the constant from the constant table...
1200//
1201void ConstantPointerNull::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001202 NullPtrConstants->remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001203 destroyConstantImpl();
1204}
1205
1206
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001207//---- UndefValue::get() implementation...
1208//
1209
1210namespace llvm {
1211 // UndefValue does not take extra "value" argument...
1212 template<class ValType>
1213 struct ConstantCreator<UndefValue, Type, ValType> {
1214 static UndefValue *create(const Type *Ty, const ValType &V) {
1215 return new UndefValue(Ty);
1216 }
1217 };
1218
1219 template<>
1220 struct ConvertConstantType<UndefValue, Type> {
1221 static void convert(UndefValue *OldC, const Type *NewTy) {
1222 // Make everyone now use a constant of the new type.
1223 Constant *New = UndefValue::get(NewTy);
1224 assert(New != OldC && "Didn't replace constant??");
1225 OldC->uncheckedReplaceAllUsesWith(New);
1226 OldC->destroyConstant(); // This constant is now dead, destroy it.
1227 }
1228 };
1229}
1230
Chris Lattner69edc982006-09-28 00:35:06 +00001231static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001232
1233static char getValType(UndefValue *) {
1234 return 0;
1235}
1236
1237
1238UndefValue *UndefValue::get(const Type *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001239 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001240}
1241
1242// destroyConstant - Remove the constant from the constant table.
1243//
1244void UndefValue::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001245 UndefValueConstants->remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001246 destroyConstantImpl();
1247}
1248
1249
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001250//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001251//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001252
Reid Spenceree3c9912006-12-04 05:19:50 +00001253struct ExprMapKeyType {
1254 explicit ExprMapKeyType(unsigned opc, std::vector<Constant*> ops,
Reid Spencerdba6aa42006-12-04 18:38:05 +00001255 unsigned short pred = 0) : opcode(opc), predicate(pred), operands(ops) { }
1256 uint16_t opcode;
1257 uint16_t predicate;
Reid Spenceree3c9912006-12-04 05:19:50 +00001258 std::vector<Constant*> operands;
Reid Spenceree3c9912006-12-04 05:19:50 +00001259 bool operator==(const ExprMapKeyType& that) const {
1260 return this->opcode == that.opcode &&
1261 this->predicate == that.predicate &&
1262 this->operands == that.operands;
1263 }
1264 bool operator<(const ExprMapKeyType & that) const {
1265 return this->opcode < that.opcode ||
1266 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1267 (this->opcode == that.opcode && this->predicate == that.predicate &&
1268 this->operands < that.operands);
1269 }
1270
1271 bool operator!=(const ExprMapKeyType& that) const {
1272 return !(*this == that);
1273 }
1274};
Chris Lattner98fa07b2003-05-23 20:03:32 +00001275
Chris Lattner189d19f2003-11-21 20:23:48 +00001276namespace llvm {
1277 template<>
1278 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001279 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1280 unsigned short pred = 0) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001281 if (Instruction::isCast(V.opcode))
1282 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1283 if ((V.opcode >= Instruction::BinaryOpsBegin &&
1284 V.opcode < Instruction::BinaryOpsEnd) ||
1285 V.opcode == Instruction::Shl ||
1286 V.opcode == Instruction::LShr ||
1287 V.opcode == Instruction::AShr)
1288 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1289 if (V.opcode == Instruction::Select)
1290 return new SelectConstantExpr(V.operands[0], V.operands[1],
1291 V.operands[2]);
1292 if (V.opcode == Instruction::ExtractElement)
1293 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1294 if (V.opcode == Instruction::InsertElement)
1295 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1296 V.operands[2]);
1297 if (V.opcode == Instruction::ShuffleVector)
1298 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1299 V.operands[2]);
1300 if (V.opcode == Instruction::GetElementPtr) {
1301 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
1302 return new GetElementPtrConstantExpr(V.operands[0], IdxList, Ty);
1303 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001304
Reid Spenceree3c9912006-12-04 05:19:50 +00001305 // The compare instructions are weird. We have to encode the predicate
1306 // value and it is combined with the instruction opcode by multiplying
1307 // the opcode by one hundred. We must decode this to get the predicate.
1308 if (V.opcode == Instruction::ICmp)
1309 return new CompareConstantExpr(Instruction::ICmp, V.predicate,
1310 V.operands[0], V.operands[1]);
1311 if (V.opcode == Instruction::FCmp)
1312 return new CompareConstantExpr(Instruction::FCmp, V.predicate,
1313 V.operands[0], V.operands[1]);
1314 assert(0 && "Invalid ConstantExpr!");
Jeff Cohen9f469632006-12-15 21:47:01 +00001315 return 0;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001316 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001317 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001318
Chris Lattner189d19f2003-11-21 20:23:48 +00001319 template<>
1320 struct ConvertConstantType<ConstantExpr, Type> {
1321 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1322 Constant *New;
1323 switch (OldC->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001324 case Instruction::Trunc:
1325 case Instruction::ZExt:
1326 case Instruction::SExt:
1327 case Instruction::FPTrunc:
1328 case Instruction::FPExt:
1329 case Instruction::UIToFP:
1330 case Instruction::SIToFP:
1331 case Instruction::FPToUI:
1332 case Instruction::FPToSI:
1333 case Instruction::PtrToInt:
1334 case Instruction::IntToPtr:
1335 case Instruction::BitCast:
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001336 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
1337 NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001338 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001339 case Instruction::Select:
1340 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1341 OldC->getOperand(1),
1342 OldC->getOperand(2));
1343 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001344 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001345 case Instruction::LShr:
1346 case Instruction::AShr:
Chris Lattner189d19f2003-11-21 20:23:48 +00001347 New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
1348 OldC->getOperand(0), OldC->getOperand(1));
1349 break;
1350 default:
1351 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001352 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner189d19f2003-11-21 20:23:48 +00001353 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1354 OldC->getOperand(1));
1355 break;
1356 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001357 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001358 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
1359 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), Idx);
Chris Lattner189d19f2003-11-21 20:23:48 +00001360 break;
1361 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001362
Chris Lattner189d19f2003-11-21 20:23:48 +00001363 assert(New != OldC && "Didn't replace constant??");
1364 OldC->uncheckedReplaceAllUsesWith(New);
1365 OldC->destroyConstant(); // This constant is now dead, destroy it.
1366 }
1367 };
1368} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001369
1370
Chris Lattner3e650af2004-08-04 04:48:01 +00001371static ExprMapKeyType getValType(ConstantExpr *CE) {
1372 std::vector<Constant*> Operands;
1373 Operands.reserve(CE->getNumOperands());
1374 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1375 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spenceree3c9912006-12-04 05:19:50 +00001376 return ExprMapKeyType(CE->getOpcode(), Operands,
1377 CE->isCompare() ? CE->getPredicate() : 0);
Chris Lattner3e650af2004-08-04 04:48:01 +00001378}
1379
Chris Lattner69edc982006-09-28 00:35:06 +00001380static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1381 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001382
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001383/// This is a utility function to handle folding of casts and lookup of the
1384/// cast in the ExprConstants map. It is usedby the various get* methods below.
1385static inline Constant *getFoldedCast(
1386 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001387 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001388 // Fold a few common cases
1389 if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
1390 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001391
Vikram S. Adve4c485332002-07-15 18:19:33 +00001392 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001393 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00001394 ExprMapKeyType Key(opc, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001395 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001396}
Reid Spencerf37dc652006-12-05 19:14:13 +00001397
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001398Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
1399 Instruction::CastOps opc = Instruction::CastOps(oc);
1400 assert(Instruction::isCast(opc) && "opcode out of range");
1401 assert(C && Ty && "Null arguments to getCast");
1402 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1403
1404 switch (opc) {
1405 default:
1406 assert(0 && "Invalid cast opcode");
1407 break;
1408 case Instruction::Trunc: return getTrunc(C, Ty);
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001409 case Instruction::ZExt: return getZExt(C, Ty);
1410 case Instruction::SExt: return getSExt(C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001411 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1412 case Instruction::FPExt: return getFPExtend(C, Ty);
1413 case Instruction::UIToFP: return getUIToFP(C, Ty);
1414 case Instruction::SIToFP: return getSIToFP(C, Ty);
1415 case Instruction::FPToUI: return getFPToUI(C, Ty);
1416 case Instruction::FPToSI: return getFPToSI(C, Ty);
1417 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1418 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1419 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001420 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001421 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00001422}
1423
Reid Spencer5c140882006-12-04 20:17:56 +00001424Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
1425 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1426 return getCast(Instruction::BitCast, C, Ty);
1427 return getCast(Instruction::ZExt, C, Ty);
1428}
1429
1430Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
1431 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1432 return getCast(Instruction::BitCast, C, Ty);
1433 return getCast(Instruction::SExt, C, Ty);
1434}
1435
1436Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
1437 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1438 return getCast(Instruction::BitCast, C, Ty);
1439 return getCast(Instruction::Trunc, C, Ty);
1440}
1441
Reid Spencerbc245a02006-12-05 03:25:26 +00001442Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
1443 assert(isa<PointerType>(S->getType()) && "Invalid cast");
1444 assert((Ty->isIntegral() || Ty->getTypeID() == Type::PointerTyID) &&
1445 "Invalid cast");
1446
1447 if (Ty->isIntegral())
1448 return getCast(Instruction::PtrToInt, S, Ty);
1449 return getCast(Instruction::BitCast, S, Ty);
1450}
1451
Reid Spencer56521c42006-12-12 00:51:07 +00001452Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1453 bool isSigned) {
1454 assert(C->getType()->isIntegral() && Ty->isIntegral() && "Invalid cast");
1455 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1456 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1457 Instruction::CastOps opcode =
1458 (SrcBits == DstBits ? Instruction::BitCast :
1459 (SrcBits > DstBits ? Instruction::Trunc :
1460 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1461 return getCast(opcode, C, Ty);
1462}
1463
1464Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
1465 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1466 "Invalid cast");
1467 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1468 unsigned DstBits = Ty->getPrimitiveSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001469 if (SrcBits == DstBits)
1470 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001471 Instruction::CastOps opcode =
Reid Spencerca104e82006-12-12 05:38:50 +00001472 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001473 return getCast(opcode, C, Ty);
1474}
1475
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001476Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
1477 assert(C->getType()->isInteger() && "Trunc operand must be integer");
1478 assert(Ty->isIntegral() && "Trunc produces only integral");
1479 assert(C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&&
1480 "SrcTy must be larger than DestTy for Trunc!");
1481
1482 return getFoldedCast(Instruction::Trunc, C, Ty);
1483}
1484
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001485Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001486 assert(C->getType()->isIntegral() && "SEXt operand must be integral");
1487 assert(Ty->isInteger() && "SExt produces only integer");
1488 assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1489 "SrcTy must be smaller than DestTy for SExt!");
1490
1491 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerdd284742004-04-04 23:20:30 +00001492}
1493
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001494Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001495 assert(C->getType()->isIntegral() && "ZEXt operand must be integral");
1496 assert(Ty->isInteger() && "ZExt produces only integer");
1497 assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1498 "SrcTy must be smaller than DestTy for ZExt!");
1499
1500 return getFoldedCast(Instruction::ZExt, C, Ty);
1501}
1502
1503Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
1504 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1505 C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&&
1506 "This is an illegal floating point truncation!");
1507 return getFoldedCast(Instruction::FPTrunc, C, Ty);
1508}
1509
1510Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
1511 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1512 C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1513 "This is an illegal floating point extension!");
1514 return getFoldedCast(Instruction::FPExt, C, Ty);
1515}
1516
1517Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
1518 assert(C->getType()->isIntegral() && Ty->isFloatingPoint() &&
1519 "This is an illegal uint to floating point cast!");
1520 return getFoldedCast(Instruction::UIToFP, C, Ty);
1521}
1522
1523Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
1524 assert(C->getType()->isIntegral() && Ty->isFloatingPoint() &&
1525 "This is an illegal sint to floating point cast!");
1526 return getFoldedCast(Instruction::SIToFP, C, Ty);
1527}
1528
1529Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
1530 assert(C->getType()->isFloatingPoint() && Ty->isIntegral() &&
1531 "This is an illegal floating point to uint cast!");
1532 return getFoldedCast(Instruction::FPToUI, C, Ty);
1533}
1534
1535Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
1536 assert(C->getType()->isFloatingPoint() && Ty->isIntegral() &&
1537 "This is an illegal floating point to sint cast!");
1538 return getFoldedCast(Instruction::FPToSI, C, Ty);
1539}
1540
1541Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
1542 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
1543 assert(DstTy->isIntegral() && "PtrToInt destination must be integral");
1544 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
1545}
1546
1547Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
1548 assert(C->getType()->isIntegral() && "IntToPtr source must be integral");
1549 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
1550 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
1551}
1552
1553Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
1554 // BitCast implies a no-op cast of type only. No bits change. However, you
1555 // can't cast pointers to anything but pointers.
1556 const Type *SrcTy = C->getType();
1557 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer5c140882006-12-04 20:17:56 +00001558 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001559
1560 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
1561 // or nonptr->ptr). For all the other types, the cast is okay if source and
1562 // destination bit widths are identical.
1563 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1564 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Reid Spencer5c140882006-12-04 20:17:56 +00001565 assert(SrcBitSize == DstBitSize && "BitCast requies types of same width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001566 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerdd284742004-04-04 23:20:30 +00001567}
1568
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001569Constant *ConstantExpr::getSizeOf(const Type *Ty) {
Chris Lattneracc4e542004-12-13 19:48:51 +00001570 // sizeof is implemented as: (ulong) gep (Ty*)null, 1
Reid Spencerc4dacf22006-12-04 02:43:42 +00001571 return getCast(Instruction::PtrToInt, getGetElementPtr(getNullValue(
1572 PointerType::get(Ty)), std::vector<Constant*>(1,
Reid Spencer8d9336d2006-12-31 05:26:44 +00001573 ConstantInt::get(Type::Int32Ty, 1))), Type::Int64Ty);
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001574}
1575
Alkis Evlogimenos9160d5f2005-03-19 11:40:31 +00001576Constant *ConstantExpr::getPtrPtrFromArrayPtr(Constant *C) {
1577 // pointer from array is implemented as: getelementptr arr ptr, 0, 0
Reid Spencer8d9336d2006-12-31 05:26:44 +00001578 static std::vector<Constant*> Indices(2, ConstantInt::get(Type::Int32Ty, 0));
Alkis Evlogimenos9160d5f2005-03-19 11:40:31 +00001579
1580 return ConstantExpr::getGetElementPtr(C, Indices);
1581}
1582
Chris Lattnerb50d1352003-10-05 00:17:43 +00001583Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Reid Spencera009d0d2006-12-04 21:35:24 +00001584 Constant *C1, Constant *C2) {
Reid Spencerfdff9382006-11-08 06:47:33 +00001585 if (Opcode == Instruction::Shl || Opcode == Instruction::LShr ||
1586 Opcode == Instruction::AShr)
Chris Lattner5645e8a2004-01-12 19:04:55 +00001587 return getShiftTy(ReqTy, Opcode, C1, C2);
Reid Spenceree3c9912006-12-04 05:19:50 +00001588
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001589 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00001590 assert(Opcode >= Instruction::BinaryOpsBegin &&
1591 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001592 "Invalid opcode in binary constant expression");
1593 assert(C1->getType() == C2->getType() &&
1594 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001595
Reid Spencer542964f2007-01-11 18:21:29 +00001596 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Chris Lattnerb50d1352003-10-05 00:17:43 +00001597 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1598 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001599
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001600 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001601 ExprMapKeyType Key(Opcode, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001602 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001603}
1604
Reid Spencer266e42b2006-12-23 06:05:41 +00001605Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Reid Spencera009d0d2006-12-04 21:35:24 +00001606 Constant *C1, Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001607 switch (predicate) {
1608 default: assert(0 && "Invalid CmpInst predicate");
1609 case FCmpInst::FCMP_FALSE: case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_OGT:
1610 case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OLE:
1611 case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_ORD: case FCmpInst::FCMP_UNO:
1612 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UGT: case FCmpInst::FCMP_UGE:
1613 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_ULE: case FCmpInst::FCMP_UNE:
1614 case FCmpInst::FCMP_TRUE:
1615 return getFCmp(predicate, C1, C2);
1616 case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_UGT:
1617 case ICmpInst::ICMP_UGE: case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE:
1618 case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_SGE: case ICmpInst::ICMP_SLT:
1619 case ICmpInst::ICMP_SLE:
1620 return getICmp(predicate, C1, C2);
1621 }
Reid Spencera009d0d2006-12-04 21:35:24 +00001622}
1623
1624Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001625#ifndef NDEBUG
1626 switch (Opcode) {
Reid Spencer7eb55b32006-11-02 01:53:59 +00001627 case Instruction::Add:
1628 case Instruction::Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001629 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001630 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattnerc421a262006-01-04 01:01:04 +00001631 assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint() ||
1632 isa<PackedType>(C1->getType())) &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001633 "Tried to create an arithmetic operation on a non-arithmetic type!");
1634 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001635 case Instruction::UDiv:
1636 case Instruction::SDiv:
1637 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1638 assert((C1->getType()->isInteger() || (isa<PackedType>(C1->getType()) &&
1639 cast<PackedType>(C1->getType())->getElementType()->isInteger())) &&
1640 "Tried to create an arithmetic operation on a non-arithmetic type!");
1641 break;
1642 case Instruction::FDiv:
1643 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1644 assert((C1->getType()->isFloatingPoint() || (isa<PackedType>(C1->getType())
1645 && cast<PackedType>(C1->getType())->getElementType()->isFloatingPoint()))
1646 && "Tried to create an arithmetic operation on a non-arithmetic type!");
1647 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001648 case Instruction::URem:
1649 case Instruction::SRem:
1650 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1651 assert((C1->getType()->isInteger() || (isa<PackedType>(C1->getType()) &&
1652 cast<PackedType>(C1->getType())->getElementType()->isInteger())) &&
1653 "Tried to create an arithmetic operation on a non-arithmetic type!");
1654 break;
1655 case Instruction::FRem:
1656 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1657 assert((C1->getType()->isFloatingPoint() || (isa<PackedType>(C1->getType())
1658 && cast<PackedType>(C1->getType())->getElementType()->isFloatingPoint()))
1659 && "Tried to create an arithmetic operation on a non-arithmetic type!");
1660 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001661 case Instruction::And:
1662 case Instruction::Or:
1663 case Instruction::Xor:
1664 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattnerc421a262006-01-04 01:01:04 +00001665 assert((C1->getType()->isIntegral() || isa<PackedType>(C1->getType())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001666 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001667 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001668 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001669 case Instruction::LShr:
1670 case Instruction::AShr:
Reid Spencer8d9336d2006-12-31 05:26:44 +00001671 assert(C2->getType() == Type::Int8Ty && "Shift should be by ubyte!");
Reid Spencerfdff9382006-11-08 06:47:33 +00001672 assert(C1->getType()->isInteger() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001673 "Tried to create a shift operation on a non-integer type!");
1674 break;
1675 default:
1676 break;
1677 }
1678#endif
1679
Reid Spencera009d0d2006-12-04 21:35:24 +00001680 return getTy(C1->getType(), Opcode, C1, C2);
1681}
1682
Reid Spencer266e42b2006-12-23 06:05:41 +00001683Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00001684 Constant *C1, Constant *C2) {
1685 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001686 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00001687}
1688
Chris Lattner6e415c02004-03-12 05:54:04 +00001689Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
1690 Constant *V1, Constant *V2) {
Reid Spencer542964f2007-01-11 18:21:29 +00001691 assert(C->getType() == Type::Int1Ty && "Select condition must be bool!");
Chris Lattner6e415c02004-03-12 05:54:04 +00001692 assert(V1->getType() == V2->getType() && "Select value types must match!");
1693 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
1694
1695 if (ReqTy == V1->getType())
1696 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1697 return SC; // Fold common cases
1698
1699 std::vector<Constant*> argVec(3, C);
1700 argVec[1] = V1;
1701 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00001702 ExprMapKeyType Key(Instruction::Select, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001703 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00001704}
1705
Chris Lattner9eb2b522004-01-12 19:12:58 +00001706/// getShiftTy - Return a shift left or shift right constant expr
Chris Lattnerb50d1352003-10-05 00:17:43 +00001707Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
1708 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001709 // Check the operands for consistency first
Reid Spencerfdff9382006-11-08 06:47:33 +00001710 assert((Opcode == Instruction::Shl ||
1711 Opcode == Instruction::LShr ||
1712 Opcode == Instruction::AShr) &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001713 "Invalid opcode in binary constant expression");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001714 assert(C1->getType()->isIntegral() && C2->getType() == Type::Int8Ty &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001715 "Invalid operand types for Shift constant expr!");
1716
Chris Lattner0bba7712004-01-12 20:40:42 +00001717 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001718 return FC; // Fold a few common cases...
1719
1720 // Look up the constant in the table first to ensure uniqueness
1721 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spenceree3c9912006-12-04 05:19:50 +00001722 ExprMapKeyType Key(Opcode, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001723 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001724}
1725
Chris Lattnerb50d1352003-10-05 00:17:43 +00001726Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner13128ab2004-10-11 22:52:25 +00001727 const std::vector<Value*> &IdxList) {
1728 assert(GetElementPtrInst::getIndexedType(C->getType(), IdxList, true) &&
Chris Lattner04b60fe2004-02-16 20:46:13 +00001729 "GEP indices invalid!");
1730
Chris Lattneracdbe712003-04-17 19:24:48 +00001731 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
1732 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001733
Chris Lattnerb50d1352003-10-05 00:17:43 +00001734 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001735 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001736 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00001737 std::vector<Constant*> ArgVec;
1738 ArgVec.reserve(IdxList.size()+1);
1739 ArgVec.push_back(C);
1740 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
1741 ArgVec.push_back(cast<Constant>(IdxList[i]));
Reid Spenceree3c9912006-12-04 05:19:50 +00001742 const ExprMapKeyType Key(Instruction::GetElementPtr,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001743 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001744}
1745
Chris Lattnerb50d1352003-10-05 00:17:43 +00001746Constant *ConstantExpr::getGetElementPtr(Constant *C,
1747 const std::vector<Constant*> &IdxList){
1748 // Get the result type of the getelementptr!
1749 std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
1750
1751 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
1752 true);
1753 assert(Ty && "GEP indices invalid!");
Chris Lattner13128ab2004-10-11 22:52:25 +00001754 return getGetElementPtrTy(PointerType::get(Ty), C, VIdxList);
1755}
1756
1757Constant *ConstantExpr::getGetElementPtr(Constant *C,
1758 const std::vector<Value*> &IdxList) {
1759 // Get the result type of the getelementptr!
1760 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
1761 true);
1762 assert(Ty && "GEP indices invalid!");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001763 return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
1764}
1765
Reid Spenceree3c9912006-12-04 05:19:50 +00001766Constant *
1767ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1768 assert(LHS->getType() == RHS->getType());
1769 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
1770 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
1771
Reid Spencer266e42b2006-12-23 06:05:41 +00001772 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001773 return FC; // Fold a few common cases...
1774
1775 // Look up the constant in the table first to ensure uniqueness
1776 std::vector<Constant*> ArgVec;
1777 ArgVec.push_back(LHS);
1778 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001779 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001780 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Reid Spencer542964f2007-01-11 18:21:29 +00001781 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001782}
1783
1784Constant *
1785ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1786 assert(LHS->getType() == RHS->getType());
1787 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
1788
Reid Spencer266e42b2006-12-23 06:05:41 +00001789 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001790 return FC; // Fold a few common cases...
1791
1792 // Look up the constant in the table first to ensure uniqueness
1793 std::vector<Constant*> ArgVec;
1794 ArgVec.push_back(LHS);
1795 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001796 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001797 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Reid Spencer542964f2007-01-11 18:21:29 +00001798 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001799}
1800
Robert Bocchino23004482006-01-10 19:05:34 +00001801Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
1802 Constant *Idx) {
Robert Bocchinode7f1c92006-01-10 20:03:46 +00001803 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
1804 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00001805 // Look up the constant in the table first to ensure uniqueness
1806 std::vector<Constant*> ArgVec(1, Val);
1807 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001808 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001809 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00001810}
1811
1812Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
1813 assert(isa<PackedType>(Val->getType()) &&
1814 "Tried to create extractelement operation on non-packed type!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001815 assert(Idx->getType() == Type::Int32Ty &&
Robert Bocchinoca27f032006-01-17 20:07:22 +00001816 "Extractelement index must be uint type!");
Robert Bocchino23004482006-01-10 19:05:34 +00001817 return getExtractElementTy(cast<PackedType>(Val->getType())->getElementType(),
1818 Val, Idx);
1819}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001820
Robert Bocchinoca27f032006-01-17 20:07:22 +00001821Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
1822 Constant *Elt, Constant *Idx) {
1823 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
1824 return FC; // Fold a few common cases...
1825 // Look up the constant in the table first to ensure uniqueness
1826 std::vector<Constant*> ArgVec(1, Val);
1827 ArgVec.push_back(Elt);
1828 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001829 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001830 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001831}
1832
1833Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1834 Constant *Idx) {
1835 assert(isa<PackedType>(Val->getType()) &&
1836 "Tried to create insertelement operation on non-packed type!");
1837 assert(Elt->getType() == cast<PackedType>(Val->getType())->getElementType()
1838 && "Insertelement types must match!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001839 assert(Idx->getType() == Type::Int32Ty &&
Robert Bocchinoca27f032006-01-17 20:07:22 +00001840 "Insertelement index must be uint type!");
1841 return getInsertElementTy(cast<PackedType>(Val->getType())->getElementType(),
1842 Val, Elt, Idx);
1843}
1844
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001845Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
1846 Constant *V2, Constant *Mask) {
1847 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
1848 return FC; // Fold a few common cases...
1849 // Look up the constant in the table first to ensure uniqueness
1850 std::vector<Constant*> ArgVec(1, V1);
1851 ArgVec.push_back(V2);
1852 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00001853 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001854 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001855}
1856
1857Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
1858 Constant *Mask) {
1859 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
1860 "Invalid shuffle vector constant expr operands!");
1861 return getShuffleVectorTy(V1->getType(), V1, V2, Mask);
1862}
1863
Vikram S. Adve4c485332002-07-15 18:19:33 +00001864// destroyConstant - Remove the constant from the constant table...
1865//
1866void ConstantExpr::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001867 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001868 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001869}
1870
Chris Lattner3cd8c562002-07-30 18:54:25 +00001871const char *ConstantExpr::getOpcodeName() const {
1872 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001873}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00001874
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001875//===----------------------------------------------------------------------===//
1876// replaceUsesOfWithOnConstant implementations
1877
1878void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001879 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001880 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00001881 Constant *ToC = cast<Constant>(To);
Chris Lattnerdff59112005-10-04 18:47:09 +00001882
1883 unsigned OperandToUpdate = U-OperandList;
1884 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1885
Jim Laskeyc03caef2006-07-17 17:38:29 +00001886 std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnerb64419a2005-10-03 22:51:37 +00001887 Lookup.first.first = getType();
1888 Lookup.second = this;
Chris Lattnerdff59112005-10-04 18:47:09 +00001889
Chris Lattnerb64419a2005-10-03 22:51:37 +00001890 std::vector<Constant*> &Values = Lookup.first.second;
1891 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattnerdff59112005-10-04 18:47:09 +00001892
Chris Lattner8760ec72005-10-04 01:17:50 +00001893 // Fill values with the modified operands of the constant array. Also,
1894 // compute whether this turns into an all-zeros array.
Chris Lattnerdff59112005-10-04 18:47:09 +00001895 bool isAllZeros = false;
1896 if (!ToC->isNullValue()) {
1897 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1898 Values.push_back(cast<Constant>(O->get()));
1899 } else {
1900 isAllZeros = true;
1901 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1902 Constant *Val = cast<Constant>(O->get());
1903 Values.push_back(Val);
1904 if (isAllZeros) isAllZeros = Val->isNullValue();
1905 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001906 }
Chris Lattnerdff59112005-10-04 18:47:09 +00001907 Values[OperandToUpdate] = ToC;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001908
Chris Lattnerb64419a2005-10-03 22:51:37 +00001909 Constant *Replacement = 0;
1910 if (isAllZeros) {
1911 Replacement = ConstantAggregateZero::get(getType());
1912 } else {
1913 // Check to see if we have this array type already.
1914 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00001915 ArrayConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00001916 ArrayConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001917
1918 if (Exists) {
1919 Replacement = I->second;
1920 } else {
1921 // Okay, the new shape doesn't exist in the system yet. Instead of
1922 // creating a new constant array, inserting it, replaceallusesof'ing the
1923 // old with the new, then deleting the old... just update the current one
1924 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00001925 ArrayConstants->MoveConstantToNewSlot(this, I);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001926
Chris Lattnerdff59112005-10-04 18:47:09 +00001927 // Update to the new value.
1928 setOperand(OperandToUpdate, ToC);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001929 return;
1930 }
1931 }
1932
1933 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001934 assert(Replacement != this && "I didn't contain From!");
1935
Chris Lattner7a1450d2005-10-04 18:13:04 +00001936 // Everyone using this now uses the replacement.
1937 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001938
1939 // Delete the old constant!
1940 destroyConstant();
1941}
1942
1943void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001944 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001945 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00001946 Constant *ToC = cast<Constant>(To);
1947
Chris Lattnerdff59112005-10-04 18:47:09 +00001948 unsigned OperandToUpdate = U-OperandList;
1949 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1950
Jim Laskeyc03caef2006-07-17 17:38:29 +00001951 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattner8760ec72005-10-04 01:17:50 +00001952 Lookup.first.first = getType();
1953 Lookup.second = this;
1954 std::vector<Constant*> &Values = Lookup.first.second;
1955 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001956
Chris Lattnerdff59112005-10-04 18:47:09 +00001957
Chris Lattner8760ec72005-10-04 01:17:50 +00001958 // Fill values with the modified operands of the constant struct. Also,
1959 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00001960 bool isAllZeros = false;
1961 if (!ToC->isNullValue()) {
1962 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1963 Values.push_back(cast<Constant>(O->get()));
1964 } else {
1965 isAllZeros = true;
1966 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1967 Constant *Val = cast<Constant>(O->get());
1968 Values.push_back(Val);
1969 if (isAllZeros) isAllZeros = Val->isNullValue();
1970 }
Chris Lattner8760ec72005-10-04 01:17:50 +00001971 }
Chris Lattnerdff59112005-10-04 18:47:09 +00001972 Values[OperandToUpdate] = ToC;
1973
Chris Lattner8760ec72005-10-04 01:17:50 +00001974 Constant *Replacement = 0;
1975 if (isAllZeros) {
1976 Replacement = ConstantAggregateZero::get(getType());
1977 } else {
1978 // Check to see if we have this array type already.
1979 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00001980 StructConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00001981 StructConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00001982
1983 if (Exists) {
1984 Replacement = I->second;
1985 } else {
1986 // Okay, the new shape doesn't exist in the system yet. Instead of
1987 // creating a new constant struct, inserting it, replaceallusesof'ing the
1988 // old with the new, then deleting the old... just update the current one
1989 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00001990 StructConstants->MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00001991
Chris Lattnerdff59112005-10-04 18:47:09 +00001992 // Update to the new value.
1993 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00001994 return;
1995 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001996 }
1997
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001998 assert(Replacement != this && "I didn't contain From!");
1999
Chris Lattner7a1450d2005-10-04 18:13:04 +00002000 // Everyone using this now uses the replacement.
2001 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002002
2003 // Delete the old constant!
2004 destroyConstant();
2005}
2006
2007void ConstantPacked::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002008 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002009 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2010
2011 std::vector<Constant*> Values;
2012 Values.reserve(getNumOperands()); // Build replacement array...
2013 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2014 Constant *Val = getOperand(i);
2015 if (Val == From) Val = cast<Constant>(To);
2016 Values.push_back(Val);
2017 }
2018
2019 Constant *Replacement = ConstantPacked::get(getType(), Values);
2020 assert(Replacement != this && "I didn't contain From!");
2021
Chris Lattner7a1450d2005-10-04 18:13:04 +00002022 // Everyone using this now uses the replacement.
2023 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002024
2025 // Delete the old constant!
2026 destroyConstant();
2027}
2028
2029void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002030 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002031 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2032 Constant *To = cast<Constant>(ToV);
2033
2034 Constant *Replacement = 0;
2035 if (getOpcode() == Instruction::GetElementPtr) {
2036 std::vector<Constant*> Indices;
2037 Constant *Pointer = getOperand(0);
2038 Indices.reserve(getNumOperands()-1);
2039 if (Pointer == From) Pointer = To;
2040
2041 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2042 Constant *Val = getOperand(i);
2043 if (Val == From) Val = To;
2044 Indices.push_back(Val);
2045 }
2046 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002047 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002048 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002049 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002050 } else if (getOpcode() == Instruction::Select) {
2051 Constant *C1 = getOperand(0);
2052 Constant *C2 = getOperand(1);
2053 Constant *C3 = getOperand(2);
2054 if (C1 == From) C1 = To;
2055 if (C2 == From) C2 = To;
2056 if (C3 == From) C3 = To;
2057 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00002058 } else if (getOpcode() == Instruction::ExtractElement) {
2059 Constant *C1 = getOperand(0);
2060 Constant *C2 = getOperand(1);
2061 if (C1 == From) C1 = To;
2062 if (C2 == From) C2 = To;
2063 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00002064 } else if (getOpcode() == Instruction::InsertElement) {
2065 Constant *C1 = getOperand(0);
2066 Constant *C2 = getOperand(1);
2067 Constant *C3 = getOperand(1);
2068 if (C1 == From) C1 = To;
2069 if (C2 == From) C2 = To;
2070 if (C3 == From) C3 = To;
2071 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2072 } else if (getOpcode() == Instruction::ShuffleVector) {
2073 Constant *C1 = getOperand(0);
2074 Constant *C2 = getOperand(1);
2075 Constant *C3 = getOperand(2);
2076 if (C1 == From) C1 = To;
2077 if (C2 == From) C2 = To;
2078 if (C3 == From) C3 = To;
2079 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00002080 } else if (isCompare()) {
2081 Constant *C1 = getOperand(0);
2082 Constant *C2 = getOperand(1);
2083 if (C1 == From) C1 = To;
2084 if (C2 == From) C2 = To;
2085 if (getOpcode() == Instruction::ICmp)
2086 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
2087 else
2088 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002089 } else if (getNumOperands() == 2) {
2090 Constant *C1 = getOperand(0);
2091 Constant *C2 = getOperand(1);
2092 if (C1 == From) C1 = To;
2093 if (C2 == From) C2 = To;
2094 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2095 } else {
2096 assert(0 && "Unknown ConstantExpr type!");
2097 return;
2098 }
2099
2100 assert(Replacement != this && "I didn't contain From!");
2101
Chris Lattner7a1450d2005-10-04 18:13:04 +00002102 // Everyone using this now uses the replacement.
2103 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002104
2105 // Delete the old constant!
2106 destroyConstant();
2107}
2108
2109
Jim Laskey2698f0d2006-03-08 18:11:07 +00002110/// getStringValue - Turn an LLVM constant pointer that eventually points to a
2111/// global into a string value. Return an empty string if we can't do it.
Evan Cheng38280c02006-03-10 23:52:03 +00002112/// Parameter Chop determines if the result is chopped at the first null
2113/// terminator.
Jim Laskey2698f0d2006-03-08 18:11:07 +00002114///
Evan Cheng38280c02006-03-10 23:52:03 +00002115std::string Constant::getStringValue(bool Chop, unsigned Offset) {
Jim Laskey2698f0d2006-03-08 18:11:07 +00002116 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(this)) {
2117 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
2118 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
2119 if (Init->isString()) {
2120 std::string Result = Init->getAsString();
2121 if (Offset < Result.size()) {
2122 // If we are pointing INTO The string, erase the beginning...
2123 Result.erase(Result.begin(), Result.begin()+Offset);
2124
2125 // Take off the null terminator, and any string fragments after it.
Evan Cheng38280c02006-03-10 23:52:03 +00002126 if (Chop) {
2127 std::string::size_type NullPos = Result.find_first_of((char)0);
2128 if (NullPos != std::string::npos)
2129 Result.erase(Result.begin()+NullPos, Result.end());
2130 }
Jim Laskey2698f0d2006-03-08 18:11:07 +00002131 return Result;
2132 }
2133 }
2134 }
2135 } else if (Constant *C = dyn_cast<Constant>(this)) {
2136 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
Evan Cheng2c5e5302006-03-11 00:13:10 +00002137 return GV->getStringValue(Chop, Offset);
Jim Laskey2698f0d2006-03-08 18:11:07 +00002138 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
2139 if (CE->getOpcode() == Instruction::GetElementPtr) {
2140 // Turn a gep into the specified offset.
2141 if (CE->getNumOperands() == 3 &&
2142 cast<Constant>(CE->getOperand(1))->isNullValue() &&
2143 isa<ConstantInt>(CE->getOperand(2))) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00002144 Offset += cast<ConstantInt>(CE->getOperand(2))->getZExtValue();
Evan Cheng2c5e5302006-03-11 00:13:10 +00002145 return CE->getOperand(0)->getStringValue(Chop, Offset);
Jim Laskey2698f0d2006-03-08 18:11:07 +00002146 }
2147 }
2148 }
2149 }
2150 return "";
2151}