blob: 8559883088788345c736d5f2a586e9d93ea1c132 [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 Spencer7a9c62b2007-01-12 07:05:14 +000095 case Type::IntegerTyID: {
96 const IntegerType *ITy = dyn_cast<IntegerType>(Ty);
97 switch (ITy->getBitWidth()) {
98 case 1: {
99 static Constant *NullBool = ConstantInt::get(Ty, false);
100 return NullBool;
101 }
102 case 8: {
103 static Constant *NullInt8 = ConstantInt::get(Ty, 0);
104 return NullInt8;
105 }
106 case 16: {
107 static Constant *NullInt16 = ConstantInt::get(Ty, 0);
108 return NullInt16;
109 }
110 case 32: {
111 static Constant *NullInt32 = ConstantInt::get(Ty, 0);
112 return NullInt32;
113 }
114 case 64: {
115 static Constant *NullInt64 = ConstantInt::get(Ty, 0);
116 return NullInt64;
117 }
118 default:
119 return ConstantInt::get(Ty, 0);
120 }
Chris Lattner3e88ef92003-10-03 19:34:51 +0000121 }
Chris Lattner3e88ef92003-10-03 19:34:51 +0000122 case Type::FloatTyID: {
123 static Constant *NullFloat = ConstantFP::get(Type::FloatTy, 0);
124 return NullFloat;
125 }
126 case Type::DoubleTyID: {
127 static Constant *NullDouble = ConstantFP::get(Type::DoubleTy, 0);
128 return NullDouble;
129 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000130 case Type::PointerTyID:
Chris Lattnerb1585a92002-08-13 17:50:20 +0000131 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner9fba3da2004-02-15 05:53:04 +0000132 case Type::StructTyID:
133 case Type::ArrayTyID:
Brian Gaeke02209042004-08-20 06:00:58 +0000134 case Type::PackedTyID:
Chris Lattner9fba3da2004-02-15 05:53:04 +0000135 return ConstantAggregateZero::get(Ty);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000136 default:
Reid Spencercf394bf2004-07-04 11:51:24 +0000137 // Function, Label, or Opaque type?
138 assert(!"Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000139 return 0;
140 }
141}
142
Chris Lattnerb1585a92002-08-13 17:50:20 +0000143
144// Static constructor to create an integral constant with all bits set
Zhou Sheng75b871f2007-01-11 12:24:14 +0000145ConstantInt *ConstantInt::getAllOnesValue(const Type *Ty) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000146 if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty))
147 if (ITy->getBitWidth() == 1)
148 return ConstantInt::getTrue();
149 else
150 return ConstantInt::get(Ty, int64_t(-1));
151 return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000152}
153
Chris Lattnerecab54c2007-01-04 01:49:26 +0000154/// @returns the value for an packed integer constant of the given type that
155/// has all its bits set to true.
156/// @brief Get the all ones value
157ConstantPacked *ConstantPacked::getAllOnesValue(const PackedType *Ty) {
158 std::vector<Constant*> Elts;
159 Elts.resize(Ty->getNumElements(),
Zhou Sheng75b871f2007-01-11 12:24:14 +0000160 ConstantInt::getAllOnesValue(Ty->getElementType()));
Chris Lattnerecab54c2007-01-04 01:49:26 +0000161 assert(Elts[0] && "Not a packed integer type!");
162 return cast<ConstantPacked>(ConstantPacked::get(Elts));
163}
164
165
Chris Lattner2f7c9632001-06-06 20:29:01 +0000166//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +0000167// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +0000168//===----------------------------------------------------------------------===//
169
170//===----------------------------------------------------------------------===//
171// Normal Constructors
172
Zhou Sheng75b871f2007-01-11 12:24:14 +0000173ConstantInt::ConstantInt(bool V)
Reid Spencer542964f2007-01-11 18:21:29 +0000174 : Constant(Type::Int1Ty, ConstantIntVal, 0, 0), Val(uint64_t(V)) {
Chris Lattner265eb642004-06-21 12:12:12 +0000175}
176
Reid Spencere0fc4df2006-10-20 07:07:24 +0000177ConstantInt::ConstantInt(const Type *Ty, uint64_t V)
Reid Spencer542964f2007-01-11 18:21:29 +0000178 : Constant(Ty, ConstantIntVal, 0, 0), Val(Ty == Type::Int1Ty ? bool(V) : V) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000179}
180
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000181ConstantFP::ConstantFP(const Type *Ty, double V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000182 : Constant(Ty, ConstantFPVal, 0, 0) {
Chris Lattner9655e542001-07-20 19:16:02 +0000183 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000184 Val = V;
185}
186
Chris Lattner3462ae32001-12-03 22:26:30 +0000187ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000188 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000189 : Constant(T, ConstantArrayVal, new Use[V.size()], V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000190 assert(V.size() == T->getNumElements() &&
191 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000192 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000193 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
194 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000195 Constant *C = *I;
196 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000197 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000198 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000199 "Initializer for array element doesn't match array element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000200 OL->init(C, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000201 }
202}
203
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000204ConstantArray::~ConstantArray() {
205 delete [] OperandList;
206}
207
Chris Lattner3462ae32001-12-03 22:26:30 +0000208ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000209 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000210 : Constant(T, ConstantStructVal, new Use[V.size()], V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000211 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000212 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000213 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000214 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
215 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000216 Constant *C = *I;
217 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000218 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000219 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000220 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000221 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000222 "Initializer for struct element doesn't match struct element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000223 OL->init(C, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000224 }
225}
226
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000227ConstantStruct::~ConstantStruct() {
228 delete [] OperandList;
229}
230
231
Brian Gaeke02209042004-08-20 06:00:58 +0000232ConstantPacked::ConstantPacked(const PackedType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000233 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000234 : Constant(T, ConstantPackedVal, new Use[V.size()], V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000235 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000236 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
237 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000238 Constant *C = *I;
239 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000240 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000241 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000242 "Initializer for packed element doesn't match packed element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000243 OL->init(C, this);
Brian Gaeke02209042004-08-20 06:00:58 +0000244 }
245}
246
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000247ConstantPacked::~ConstantPacked() {
248 delete [] OperandList;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000249}
250
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000251// We declare several classes private to this file, so use an anonymous
252// namespace
253namespace {
254
255/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
256/// behind the scenes to implement unary constant exprs.
257class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
258 Use Op;
259public:
260 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
261 : ConstantExpr(Ty, Opcode, &Op, 1), Op(C, this) {}
262};
263
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000264/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
265/// behind the scenes to implement binary constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000266class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000267 Use Ops[2];
268public:
269 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Reid Spencer266e42b2006-12-23 06:05:41 +0000270 : ConstantExpr(C1->getType(), Opcode, Ops, 2) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000271 Ops[0].init(C1, this);
272 Ops[1].init(C2, this);
273 }
274};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000275
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000276/// SelectConstantExpr - This class is private to Constants.cpp, and is used
277/// behind the scenes to implement select constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000278class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000279 Use Ops[3];
280public:
281 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
282 : ConstantExpr(C2->getType(), Instruction::Select, Ops, 3) {
283 Ops[0].init(C1, this);
284 Ops[1].init(C2, this);
285 Ops[2].init(C3, this);
286 }
287};
288
Robert Bocchinoca27f032006-01-17 20:07:22 +0000289/// ExtractElementConstantExpr - This class is private to
290/// Constants.cpp, and is used behind the scenes to implement
291/// extractelement constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000292class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Robert Bocchino23004482006-01-10 19:05:34 +0000293 Use Ops[2];
294public:
295 ExtractElementConstantExpr(Constant *C1, Constant *C2)
296 : ConstantExpr(cast<PackedType>(C1->getType())->getElementType(),
297 Instruction::ExtractElement, Ops, 2) {
298 Ops[0].init(C1, this);
299 Ops[1].init(C2, this);
300 }
301};
302
Robert Bocchinoca27f032006-01-17 20:07:22 +0000303/// InsertElementConstantExpr - This class is private to
304/// Constants.cpp, and is used behind the scenes to implement
305/// insertelement constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000306class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Robert Bocchinoca27f032006-01-17 20:07:22 +0000307 Use Ops[3];
308public:
309 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
310 : ConstantExpr(C1->getType(), Instruction::InsertElement,
311 Ops, 3) {
312 Ops[0].init(C1, this);
313 Ops[1].init(C2, this);
314 Ops[2].init(C3, this);
315 }
316};
317
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000318/// ShuffleVectorConstantExpr - This class is private to
319/// Constants.cpp, and is used behind the scenes to implement
320/// shufflevector constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000321class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000322 Use Ops[3];
323public:
324 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
325 : ConstantExpr(C1->getType(), Instruction::ShuffleVector,
326 Ops, 3) {
327 Ops[0].init(C1, this);
328 Ops[1].init(C2, this);
329 Ops[2].init(C3, this);
330 }
331};
332
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000333/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
334/// used behind the scenes to implement getelementpr constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000335struct VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000336 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
337 const Type *DestTy)
338 : ConstantExpr(DestTy, Instruction::GetElementPtr,
339 new Use[IdxList.size()+1], IdxList.size()+1) {
340 OperandList[0].init(C, this);
341 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
342 OperandList[i+1].init(IdxList[i], this);
343 }
344 ~GetElementPtrConstantExpr() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000345 delete [] OperandList;
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000346 }
347};
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000348
349// CompareConstantExpr - This class is private to Constants.cpp, and is used
350// behind the scenes to implement ICmp and FCmp constant expressions. This is
351// needed in order to store the predicate value for these instructions.
352struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr {
353 unsigned short predicate;
354 Use Ops[2];
355 CompareConstantExpr(Instruction::OtherOps opc, unsigned short pred,
356 Constant* LHS, Constant* RHS)
Reid Spencer542964f2007-01-11 18:21:29 +0000357 : ConstantExpr(Type::Int1Ty, opc, Ops, 2), predicate(pred) {
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000358 OperandList[0].init(LHS, this);
359 OperandList[1].init(RHS, this);
360 }
361};
362
363} // end anonymous namespace
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000364
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000365
366// Utility function for determining if a ConstantExpr is a CastOp or not. This
367// can't be inline because we don't want to #include Instruction.h into
368// Constant.h
369bool ConstantExpr::isCast() const {
370 return Instruction::isCast(getOpcode());
371}
372
Reid Spenceree3c9912006-12-04 05:19:50 +0000373bool ConstantExpr::isCompare() const {
374 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
375}
376
Chris Lattner817175f2004-03-29 02:37:53 +0000377/// ConstantExpr::get* - Return some common constants without having to
378/// specify the full Instruction::OPCODE identifier.
379///
380Constant *ConstantExpr::getNeg(Constant *C) {
Chris Lattner3cdc27c2004-03-29 19:51:24 +0000381 if (!C->getType()->isFloatingPoint())
382 return get(Instruction::Sub, getNullValue(C->getType()), C);
383 else
384 return get(Instruction::Sub, ConstantFP::get(C->getType(), -0.0), C);
Chris Lattner817175f2004-03-29 02:37:53 +0000385}
386Constant *ConstantExpr::getNot(Constant *C) {
Zhou Sheng75b871f2007-01-11 12:24:14 +0000387 assert(isa<ConstantInt>(C) && "Cannot NOT a nonintegral type!");
Chris Lattner817175f2004-03-29 02:37:53 +0000388 return get(Instruction::Xor, C,
Zhou Sheng75b871f2007-01-11 12:24:14 +0000389 ConstantInt::getAllOnesValue(C->getType()));
Chris Lattner817175f2004-03-29 02:37:53 +0000390}
391Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
392 return get(Instruction::Add, C1, C2);
393}
394Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
395 return get(Instruction::Sub, C1, C2);
396}
397Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
398 return get(Instruction::Mul, C1, C2);
399}
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000400Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2) {
401 return get(Instruction::UDiv, C1, C2);
402}
403Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2) {
404 return get(Instruction::SDiv, C1, C2);
405}
406Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) {
407 return get(Instruction::FDiv, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000408}
Reid Spencer7eb55b32006-11-02 01:53:59 +0000409Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) {
410 return get(Instruction::URem, C1, C2);
411}
412Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) {
413 return get(Instruction::SRem, C1, C2);
414}
415Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) {
416 return get(Instruction::FRem, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000417}
418Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
419 return get(Instruction::And, C1, C2);
420}
421Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
422 return get(Instruction::Or, C1, C2);
423}
424Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
425 return get(Instruction::Xor, C1, C2);
426}
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000427unsigned ConstantExpr::getPredicate() const {
428 assert(getOpcode() == Instruction::FCmp || getOpcode() == Instruction::ICmp);
429 return dynamic_cast<const CompareConstantExpr*>(this)->predicate;
430}
Chris Lattner817175f2004-03-29 02:37:53 +0000431Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
432 return get(Instruction::Shl, C1, C2);
433}
Reid Spencerfdff9382006-11-08 06:47:33 +0000434Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2) {
435 return get(Instruction::LShr, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000436}
Reid Spencerfdff9382006-11-08 06:47:33 +0000437Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2) {
438 return get(Instruction::AShr, C1, C2);
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000439}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000440
Chris Lattner7c1018a2006-07-14 19:37:40 +0000441/// getWithOperandReplaced - Return a constant expression identical to this
442/// one, but with the specified operand set to the specified value.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000443Constant *
444ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +0000445 assert(OpNo < getNumOperands() && "Operand num is out of range!");
446 assert(Op->getType() == getOperand(OpNo)->getType() &&
447 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000448 if (getOperand(OpNo) == Op)
449 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000450
Chris Lattner227816342006-07-14 22:20:01 +0000451 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000452 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000453 case Instruction::Trunc:
454 case Instruction::ZExt:
455 case Instruction::SExt:
456 case Instruction::FPTrunc:
457 case Instruction::FPExt:
458 case Instruction::UIToFP:
459 case Instruction::SIToFP:
460 case Instruction::FPToUI:
461 case Instruction::FPToSI:
462 case Instruction::PtrToInt:
463 case Instruction::IntToPtr:
464 case Instruction::BitCast:
465 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000466 case Instruction::Select:
467 Op0 = (OpNo == 0) ? Op : getOperand(0);
468 Op1 = (OpNo == 1) ? Op : getOperand(1);
469 Op2 = (OpNo == 2) ? Op : getOperand(2);
470 return ConstantExpr::getSelect(Op0, Op1, Op2);
471 case Instruction::InsertElement:
472 Op0 = (OpNo == 0) ? Op : getOperand(0);
473 Op1 = (OpNo == 1) ? Op : getOperand(1);
474 Op2 = (OpNo == 2) ? Op : getOperand(2);
475 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
476 case Instruction::ExtractElement:
477 Op0 = (OpNo == 0) ? Op : getOperand(0);
478 Op1 = (OpNo == 1) ? Op : getOperand(1);
479 return ConstantExpr::getExtractElement(Op0, Op1);
480 case Instruction::ShuffleVector:
481 Op0 = (OpNo == 0) ? Op : getOperand(0);
482 Op1 = (OpNo == 1) ? Op : getOperand(1);
483 Op2 = (OpNo == 2) ? Op : getOperand(2);
484 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000485 case Instruction::GetElementPtr: {
486 std::vector<Constant*> Ops;
487 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
488 Ops.push_back(getOperand(i));
489 if (OpNo == 0)
490 return ConstantExpr::getGetElementPtr(Op, Ops);
491 Ops[OpNo-1] = Op;
492 return ConstantExpr::getGetElementPtr(getOperand(0), Ops);
493 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000494 default:
495 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000496 Op0 = (OpNo == 0) ? Op : getOperand(0);
497 Op1 = (OpNo == 1) ? Op : getOperand(1);
498 return ConstantExpr::get(getOpcode(), Op0, Op1);
499 }
500}
501
502/// getWithOperands - This returns the current constant expression with the
503/// operands replaced with the specified values. The specified operands must
504/// match count and type with the existing ones.
505Constant *ConstantExpr::
506getWithOperands(const std::vector<Constant*> &Ops) const {
507 assert(Ops.size() == getNumOperands() && "Operand count mismatch!");
508 bool AnyChange = false;
509 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
510 assert(Ops[i]->getType() == getOperand(i)->getType() &&
511 "Operand type mismatch!");
512 AnyChange |= Ops[i] != getOperand(i);
513 }
514 if (!AnyChange) // No operands changed, return self.
515 return const_cast<ConstantExpr*>(this);
516
517 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000518 case Instruction::Trunc:
519 case Instruction::ZExt:
520 case Instruction::SExt:
521 case Instruction::FPTrunc:
522 case Instruction::FPExt:
523 case Instruction::UIToFP:
524 case Instruction::SIToFP:
525 case Instruction::FPToUI:
526 case Instruction::FPToSI:
527 case Instruction::PtrToInt:
528 case Instruction::IntToPtr:
529 case Instruction::BitCast:
530 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattner227816342006-07-14 22:20:01 +0000531 case Instruction::Select:
532 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
533 case Instruction::InsertElement:
534 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
535 case Instruction::ExtractElement:
536 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
537 case Instruction::ShuffleVector:
538 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
539 case Instruction::GetElementPtr: {
540 std::vector<Constant*> ActualOps(Ops.begin()+1, Ops.end());
541 return ConstantExpr::getGetElementPtr(Ops[0], ActualOps);
542 }
Reid Spencer266e42b2006-12-23 06:05:41 +0000543 case Instruction::ICmp:
544 case Instruction::FCmp:
545 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattner227816342006-07-14 22:20:01 +0000546 default:
547 assert(getNumOperands() == 2 && "Must be binary operator?");
548 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000549 }
550}
551
Chris Lattner2f7c9632001-06-06 20:29:01 +0000552
553//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000554// isValueValidForType implementations
555
Reid Spencere7334722006-12-19 01:28:19 +0000556bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000557 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
558 assert(NumBits <= 64 && "Not implemented: integers > 64-bits");
559 if (Ty == Type::Int1Ty)
560 return Val == 0 || Val == 1;
561 if (NumBits == 64)
562 return true; // always true, has to fit in largest type
563 uint64_t Max = (1ll << NumBits) - 1;
564 return Val <= Max;
Reid Spencere7334722006-12-19 01:28:19 +0000565}
566
Reid Spencere0fc4df2006-10-20 07:07:24 +0000567bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000568 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
569 assert(NumBits <= 64 && "Not implemented: integers > 64-bits");
570 if (Ty == Type::Int1Ty)
571 return Val == 0 || Val == 1;
572 if (NumBits == 64)
573 return true; // always true, has to fit in largest type
574 int64_t Min = -(1ll << (NumBits-1));
575 int64_t Max = (1ll << (NumBits-1)) - 1;
576 return (Val >= Min && Val <= Max);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000577}
578
Chris Lattner3462ae32001-12-03 22:26:30 +0000579bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000580 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000581 default:
582 return false; // These can't be represented as floating point!
583
Reid Spencerb95f8ab2004-12-07 07:38:08 +0000584 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000585 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000586 case Type::DoubleTyID:
587 return true; // This is the largest type...
588 }
Chris Lattneraa2372562006-05-24 17:04:05 +0000589}
Chris Lattner9655e542001-07-20 19:16:02 +0000590
Chris Lattner49d855c2001-09-07 16:46:31 +0000591//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000592// Factory Function Implementation
593
Chris Lattner98fa07b2003-05-23 20:03:32 +0000594// ConstantCreator - A class that is used to create constants by
595// ValueMap*. This class should be partially specialized if there is
596// something strange that needs to be done to interface to the ctor for the
597// constant.
598//
Chris Lattner189d19f2003-11-21 20:23:48 +0000599namespace llvm {
600 template<class ConstantClass, class TypeClass, class ValType>
Chris Lattner02157b02006-06-28 21:38:54 +0000601 struct VISIBILITY_HIDDEN ConstantCreator {
Chris Lattner189d19f2003-11-21 20:23:48 +0000602 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
603 return new ConstantClass(Ty, V);
604 }
605 };
Misha Brukmanb1c93172005-04-21 23:48:37 +0000606
Chris Lattner189d19f2003-11-21 20:23:48 +0000607 template<class ConstantClass, class TypeClass>
Chris Lattner02157b02006-06-28 21:38:54 +0000608 struct VISIBILITY_HIDDEN ConvertConstantType {
Chris Lattner189d19f2003-11-21 20:23:48 +0000609 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
610 assert(0 && "This type cannot be converted!\n");
611 abort();
612 }
613 };
Chris Lattnerb50d1352003-10-05 00:17:43 +0000614
Chris Lattner935aa922005-10-04 17:48:46 +0000615 template<class ValType, class TypeClass, class ConstantClass,
616 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattner02157b02006-06-28 21:38:54 +0000617 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
Chris Lattnerb64419a2005-10-03 22:51:37 +0000618 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000619 typedef std::pair<const Type*, ValType> MapKey;
620 typedef std::map<MapKey, Constant *> MapTy;
621 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
622 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
Chris Lattnerb64419a2005-10-03 22:51:37 +0000623 private:
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000624 /// Map - This is the main map from the element descriptor to the Constants.
625 /// This is the primary way we avoid creating two of the same shape
626 /// constant.
Chris Lattnerb50d1352003-10-05 00:17:43 +0000627 MapTy Map;
Chris Lattner935aa922005-10-04 17:48:46 +0000628
629 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
630 /// from the constants to their element in Map. This is important for
631 /// removal of constants from the array, which would otherwise have to scan
632 /// through the map with very large keys.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000633 InverseMapTy InverseMap;
Chris Lattnerb50d1352003-10-05 00:17:43 +0000634
Jim Laskeyc03caef2006-07-17 17:38:29 +0000635 /// AbstractTypeMap - Map for abstract type constants.
636 ///
Chris Lattnerb50d1352003-10-05 00:17:43 +0000637 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner99a669b2004-11-19 16:39:44 +0000638
Chris Lattner99a669b2004-11-19 16:39:44 +0000639 private:
640 void clear(std::vector<Constant *> &Constants) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000641 for(typename MapTy::iterator I = Map.begin(); I != Map.end(); ++I)
Chris Lattner99a669b2004-11-19 16:39:44 +0000642 Constants.push_back(I->second);
643 Map.clear();
644 AbstractTypeMap.clear();
Chris Lattner935aa922005-10-04 17:48:46 +0000645 InverseMap.clear();
Chris Lattner99a669b2004-11-19 16:39:44 +0000646 }
647
Chris Lattner98fa07b2003-05-23 20:03:32 +0000648 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000649 typename MapTy::iterator map_end() { return Map.end(); }
Chris Lattnerb64419a2005-10-03 22:51:37 +0000650
651 /// InsertOrGetItem - Return an iterator for the specified element.
652 /// If the element exists in the map, the returned iterator points to the
653 /// entry and Exists=true. If not, the iterator points to the newly
654 /// inserted entry and returns Exists=false. Newly inserted entries have
655 /// I->second == 0, and should be filled in.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000656 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
657 &InsertVal,
Chris Lattnerb64419a2005-10-03 22:51:37 +0000658 bool &Exists) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000659 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
Chris Lattnerb64419a2005-10-03 22:51:37 +0000660 Exists = !IP.second;
661 return IP.first;
662 }
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000663
Chris Lattner935aa922005-10-04 17:48:46 +0000664private:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000665 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
Chris Lattner935aa922005-10-04 17:48:46 +0000666 if (HasLargeKey) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000667 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
Chris Lattner935aa922005-10-04 17:48:46 +0000668 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
669 IMI->second->second == CP &&
670 "InverseMap corrupt!");
671 return IMI->second;
672 }
673
Jim Laskeyc03caef2006-07-17 17:38:29 +0000674 typename MapTy::iterator I =
Chris Lattner935aa922005-10-04 17:48:46 +0000675 Map.find(MapKey((TypeClass*)CP->getRawType(), getValType(CP)));
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000676 if (I == Map.end() || I->second != CP) {
677 // FIXME: This should not use a linear scan. If this gets to be a
678 // performance problem, someone should look at this.
679 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
680 /* empty */;
681 }
Chris Lattner935aa922005-10-04 17:48:46 +0000682 return I;
683 }
684public:
685
Chris Lattnerb64419a2005-10-03 22:51:37 +0000686 /// getOrCreate - Return the specified constant from the map, creating it if
687 /// necessary.
Chris Lattner98fa07b2003-05-23 20:03:32 +0000688 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000689 MapKey Lookup(Ty, V);
Jim Laskeyc03caef2006-07-17 17:38:29 +0000690 typename MapTy::iterator I = Map.lower_bound(Lookup);
Reid Spencere0fc4df2006-10-20 07:07:24 +0000691 // Is it in the map?
Chris Lattner98fa07b2003-05-23 20:03:32 +0000692 if (I != Map.end() && I->first == Lookup)
Reid Spencere0fc4df2006-10-20 07:07:24 +0000693 return static_cast<ConstantClass *>(I->second);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000694
695 // If no preexisting value, create one now...
696 ConstantClass *Result =
697 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
698
Chris Lattnerb50d1352003-10-05 00:17:43 +0000699 /// FIXME: why does this assert fail when loading 176.gcc?
700 //assert(Result->getType() == Ty && "Type specified is not correct!");
701 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
702
Chris Lattner935aa922005-10-04 17:48:46 +0000703 if (HasLargeKey) // Remember the reverse mapping if needed.
704 InverseMap.insert(std::make_pair(Result, I));
705
Chris Lattnerb50d1352003-10-05 00:17:43 +0000706 // If the type of the constant is abstract, make sure that an entry exists
707 // for it in the AbstractTypeMap.
708 if (Ty->isAbstract()) {
709 typename AbstractTypeMapTy::iterator TI =
710 AbstractTypeMap.lower_bound(Ty);
711
712 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
713 // Add ourselves to the ATU list of the type.
714 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
715
716 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
717 }
718 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000719 return Result;
720 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000721
Chris Lattner98fa07b2003-05-23 20:03:32 +0000722 void remove(ConstantClass *CP) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000723 typename MapTy::iterator I = FindExistingElement(CP);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000724 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +0000725 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +0000726
Chris Lattner935aa922005-10-04 17:48:46 +0000727 if (HasLargeKey) // Remember the reverse mapping if needed.
728 InverseMap.erase(CP);
729
Chris Lattnerb50d1352003-10-05 00:17:43 +0000730 // Now that we found the entry, make sure this isn't the entry that
731 // the AbstractTypeMap points to.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000732 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000733 if (Ty->isAbstract()) {
734 assert(AbstractTypeMap.count(Ty) &&
735 "Abstract type not in AbstractTypeMap?");
Jim Laskeyc03caef2006-07-17 17:38:29 +0000736 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
Chris Lattnerb50d1352003-10-05 00:17:43 +0000737 if (ATMEntryIt == I) {
738 // Yes, we are removing the representative entry for this type.
739 // See if there are any other entries of the same type.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000740 typename MapTy::iterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000741
Chris Lattnerb50d1352003-10-05 00:17:43 +0000742 // First check the entry before this one...
743 if (TmpIt != Map.begin()) {
744 --TmpIt;
745 if (TmpIt->first.first != Ty) // Not the same type, move back...
746 ++TmpIt;
747 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000748
Chris Lattnerb50d1352003-10-05 00:17:43 +0000749 // If we didn't find the same type, try to move forward...
750 if (TmpIt == ATMEntryIt) {
751 ++TmpIt;
752 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
753 --TmpIt; // No entry afterwards with the same type
754 }
755
756 // If there is another entry in the map of the same abstract type,
757 // update the AbstractTypeMap entry now.
758 if (TmpIt != ATMEntryIt) {
759 ATMEntryIt = TmpIt;
760 } else {
761 // Otherwise, we are removing the last instance of this type
762 // from the table. Remove from the ATM, and from user list.
763 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
764 AbstractTypeMap.erase(Ty);
765 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000766 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000767 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000768
Chris Lattnerb50d1352003-10-05 00:17:43 +0000769 Map.erase(I);
770 }
771
Chris Lattner3b793c62005-10-04 21:35:50 +0000772
773 /// MoveConstantToNewSlot - If we are about to change C to be the element
774 /// specified by I, update our internal data structures to reflect this
775 /// fact.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000776 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
Chris Lattner3b793c62005-10-04 21:35:50 +0000777 // First, remove the old location of the specified constant in the map.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000778 typename MapTy::iterator OldI = FindExistingElement(C);
Chris Lattner3b793c62005-10-04 21:35:50 +0000779 assert(OldI != Map.end() && "Constant not found in constant table!");
780 assert(OldI->second == C && "Didn't find correct element?");
781
782 // If this constant is the representative element for its abstract type,
783 // update the AbstractTypeMap so that the representative element is I.
784 if (C->getType()->isAbstract()) {
785 typename AbstractTypeMapTy::iterator ATI =
786 AbstractTypeMap.find(C->getType());
787 assert(ATI != AbstractTypeMap.end() &&
788 "Abstract type not in AbstractTypeMap?");
789 if (ATI->second == OldI)
790 ATI->second = I;
791 }
792
793 // Remove the old entry from the map.
794 Map.erase(OldI);
795
796 // Update the inverse map so that we know that this constant is now
797 // located at descriptor I.
798 if (HasLargeKey) {
799 assert(I->second == C && "Bad inversemap entry!");
800 InverseMap[C] = I;
801 }
802 }
803
Chris Lattnerb50d1352003-10-05 00:17:43 +0000804 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000805 typename AbstractTypeMapTy::iterator I =
Jim Laskeyc03caef2006-07-17 17:38:29 +0000806 AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +0000807
808 assert(I != AbstractTypeMap.end() &&
809 "Abstract type not in AbstractTypeMap?");
810
811 // Convert a constant at a time until the last one is gone. The last one
812 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
813 // eliminated eventually.
814 do {
815 ConvertConstantType<ConstantClass,
Jim Laskeyc03caef2006-07-17 17:38:29 +0000816 TypeClass>::convert(
817 static_cast<ConstantClass *>(I->second->second),
Chris Lattnerb50d1352003-10-05 00:17:43 +0000818 cast<TypeClass>(NewTy));
819
Jim Laskeyc03caef2006-07-17 17:38:29 +0000820 I = AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +0000821 } while (I != AbstractTypeMap.end());
822 }
823
824 // If the type became concrete without being refined to any other existing
825 // type, we just remove ourselves from the ATU list.
826 void typeBecameConcrete(const DerivedType *AbsTy) {
827 AbsTy->removeAbstractTypeUser(this);
828 }
829
830 void dump() const {
Bill Wendling6a462f12006-11-17 08:03:48 +0000831 DOUT << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +0000832 }
833 };
834}
835
Chris Lattnera84df0a22006-09-28 23:36:21 +0000836
Reid Spencere0fc4df2006-10-20 07:07:24 +0000837//---- ConstantInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000838//
Reid Spencere0fc4df2006-10-20 07:07:24 +0000839static ManagedStatic<ValueMap<uint64_t, Type, ConstantInt> > IntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000840
Reid Spencere0fc4df2006-10-20 07:07:24 +0000841// Get a ConstantInt from an int64_t. Note here that we canoncialize the value
842// to a uint64_t value that has been zero extended down to the size of the
843// integer type of the ConstantInt. This allows the getZExtValue method to
844// just return the stored value while getSExtValue has to convert back to sign
845// extended. getZExtValue is more common in LLVM than getSExtValue().
846ConstantInt *ConstantInt::get(const Type *Ty, int64_t V) {
Reid Spencercddc9df2007-01-12 04:24:46 +0000847 if (Ty == Type::Int1Ty)
848 if (V & 1)
849 return getTrue();
850 else
851 return getFalse();
Chris Lattner03c49532007-01-15 02:27:26 +0000852 return IntConstants->getOrCreate(Ty, V & Ty->getIntegerTypeMask());
Chris Lattner49d855c2001-09-07 16:46:31 +0000853}
854
Chris Lattner3462ae32001-12-03 22:26:30 +0000855//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000856//
Chris Lattnerac80ea42004-02-01 22:49:04 +0000857namespace llvm {
858 template<>
859 struct ConstantCreator<ConstantFP, Type, uint64_t> {
860 static ConstantFP *create(const Type *Ty, uint64_t V) {
861 assert(Ty == Type::DoubleTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000862 return new ConstantFP(Ty, BitsToDouble(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000863 }
864 };
865 template<>
866 struct ConstantCreator<ConstantFP, Type, uint32_t> {
867 static ConstantFP *create(const Type *Ty, uint32_t V) {
868 assert(Ty == Type::FloatTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000869 return new ConstantFP(Ty, BitsToFloat(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000870 }
871 };
872}
873
Chris Lattner69edc982006-09-28 00:35:06 +0000874static ManagedStatic<ValueMap<uint64_t, Type, ConstantFP> > DoubleConstants;
875static ManagedStatic<ValueMap<uint32_t, Type, ConstantFP> > FloatConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000876
Jim Laskey8ad8f712005-08-17 20:06:22 +0000877bool ConstantFP::isNullValue() const {
878 return DoubleToBits(Val) == 0;
879}
880
881bool ConstantFP::isExactlyValue(double V) const {
882 return DoubleToBits(V) == DoubleToBits(Val);
883}
884
885
Chris Lattner3462ae32001-12-03 22:26:30 +0000886ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner241ed4c2004-01-23 00:55:21 +0000887 if (Ty == Type::FloatTy) {
888 // Force the value through memory to normalize it.
Chris Lattner69edc982006-09-28 00:35:06 +0000889 return FloatConstants->getOrCreate(Ty, FloatToBits(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000890 } else {
891 assert(Ty == Type::DoubleTy);
Chris Lattner69edc982006-09-28 00:35:06 +0000892 return DoubleConstants->getOrCreate(Ty, DoubleToBits(V));
Chris Lattner241ed4c2004-01-23 00:55:21 +0000893 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000894}
895
Chris Lattner9fba3da2004-02-15 05:53:04 +0000896//---- ConstantAggregateZero::get() implementation...
897//
898namespace llvm {
899 // ConstantAggregateZero does not take extra "value" argument...
900 template<class ValType>
901 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
902 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
903 return new ConstantAggregateZero(Ty);
904 }
905 };
906
907 template<>
908 struct ConvertConstantType<ConstantAggregateZero, Type> {
909 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
910 // Make everyone now use a constant of the new type...
911 Constant *New = ConstantAggregateZero::get(NewTy);
912 assert(New != OldC && "Didn't replace constant??");
913 OldC->uncheckedReplaceAllUsesWith(New);
914 OldC->destroyConstant(); // This constant is now dead, destroy it.
915 }
916 };
917}
918
Chris Lattner69edc982006-09-28 00:35:06 +0000919static ManagedStatic<ValueMap<char, Type,
920 ConstantAggregateZero> > AggZeroConstants;
Chris Lattner9fba3da2004-02-15 05:53:04 +0000921
Chris Lattner3e650af2004-08-04 04:48:01 +0000922static char getValType(ConstantAggregateZero *CPZ) { return 0; }
923
Chris Lattner9fba3da2004-02-15 05:53:04 +0000924Constant *ConstantAggregateZero::get(const Type *Ty) {
Chris Lattnerbfd0b6d2006-06-10 04:16:23 +0000925 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<PackedType>(Ty)) &&
926 "Cannot create an aggregate zero of non-aggregate type!");
Chris Lattner69edc982006-09-28 00:35:06 +0000927 return AggZeroConstants->getOrCreate(Ty, 0);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000928}
929
930// destroyConstant - Remove the constant from the constant table...
931//
932void ConstantAggregateZero::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +0000933 AggZeroConstants->remove(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000934 destroyConstantImpl();
935}
936
Chris Lattner3462ae32001-12-03 22:26:30 +0000937//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000938//
Chris Lattner189d19f2003-11-21 20:23:48 +0000939namespace llvm {
940 template<>
941 struct ConvertConstantType<ConstantArray, ArrayType> {
942 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
943 // Make everyone now use a constant of the new type...
944 std::vector<Constant*> C;
945 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
946 C.push_back(cast<Constant>(OldC->getOperand(i)));
947 Constant *New = ConstantArray::get(NewTy, C);
948 assert(New != OldC && "Didn't replace constant??");
949 OldC->uncheckedReplaceAllUsesWith(New);
950 OldC->destroyConstant(); // This constant is now dead, destroy it.
951 }
952 };
953}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000954
Chris Lattner3e650af2004-08-04 04:48:01 +0000955static std::vector<Constant*> getValType(ConstantArray *CA) {
956 std::vector<Constant*> Elements;
957 Elements.reserve(CA->getNumOperands());
958 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
959 Elements.push_back(cast<Constant>(CA->getOperand(i)));
960 return Elements;
961}
962
Chris Lattnerb64419a2005-10-03 22:51:37 +0000963typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattner935aa922005-10-04 17:48:46 +0000964 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +0000965static ManagedStatic<ArrayConstantsTy> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000966
Chris Lattner015e8212004-02-15 04:14:47 +0000967Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +0000968 const std::vector<Constant*> &V) {
969 // If this is an all-zero array, return a ConstantAggregateZero object
970 if (!V.empty()) {
971 Constant *C = V[0];
972 if (!C->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +0000973 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000974 for (unsigned i = 1, e = V.size(); i != e; ++i)
975 if (V[i] != C)
Chris Lattner69edc982006-09-28 00:35:06 +0000976 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000977 }
978 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000979}
980
Chris Lattner98fa07b2003-05-23 20:03:32 +0000981// destroyConstant - Remove the constant from the constant table...
982//
983void ConstantArray::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +0000984 ArrayConstants->remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000985 destroyConstantImpl();
986}
987
Reid Spencer6f614532006-05-30 08:23:18 +0000988/// ConstantArray::get(const string&) - Return an array that is initialized to
989/// contain the specified string. If length is zero then a null terminator is
990/// added to the specified string so that it may be used in a natural way.
991/// Otherwise, the length parameter specifies how much of the string to use
992/// and it won't be null terminated.
993///
Reid Spencer82ebaba2006-05-30 18:15:07 +0000994Constant *ConstantArray::get(const std::string &Str, bool AddNull) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000995 std::vector<Constant*> ElementVals;
Reid Spencer82ebaba2006-05-30 18:15:07 +0000996 for (unsigned i = 0; i < Str.length(); ++i)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000997 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000998
999 // Add a null terminator to the string...
Reid Spencer82ebaba2006-05-30 18:15:07 +00001000 if (AddNull) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001001 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, 0));
Reid Spencer6f614532006-05-30 08:23:18 +00001002 }
Chris Lattner8f80fe02001-10-14 23:54:12 +00001003
Reid Spencer8d9336d2006-12-31 05:26:44 +00001004 ArrayType *ATy = ArrayType::get(Type::Int8Ty, ElementVals.size());
Chris Lattner3462ae32001-12-03 22:26:30 +00001005 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +00001006}
1007
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001008/// isString - This method returns true if the array is an array of sbyte or
1009/// ubyte, and if the elements of the array are all ConstantInt's.
1010bool ConstantArray::isString() const {
1011 // Check the element type for sbyte or ubyte...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001012 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001013 return false;
1014 // Check the elements to make sure they are all integers, not constant
1015 // expressions.
1016 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1017 if (!isa<ConstantInt>(getOperand(i)))
1018 return false;
1019 return true;
1020}
1021
Evan Cheng3763c5b2006-10-26 19:15:05 +00001022/// isCString - This method returns true if the array is a string (see
1023/// isString) and it ends in a null byte \0 and does not contains any other
1024/// null bytes except its terminator.
1025bool ConstantArray::isCString() const {
Evan Chenge974da62006-10-26 21:48:03 +00001026 // Check the element type for sbyte or ubyte...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001027 if (getType()->getElementType() != Type::Int8Ty)
Evan Chenge974da62006-10-26 21:48:03 +00001028 return false;
1029 Constant *Zero = Constant::getNullValue(getOperand(0)->getType());
1030 // Last element must be a null.
1031 if (getOperand(getNumOperands()-1) != Zero)
1032 return false;
1033 // Other elements must be non-null integers.
1034 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1035 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +00001036 return false;
Evan Chenge974da62006-10-26 21:48:03 +00001037 if (getOperand(i) == Zero)
1038 return false;
1039 }
Evan Cheng3763c5b2006-10-26 19:15:05 +00001040 return true;
1041}
1042
1043
Chris Lattner81fabb02002-08-26 17:53:56 +00001044// getAsString - If the sub-element type of this array is either sbyte or ubyte,
1045// then this method converts the array to an std::string and returns it.
1046// Otherwise, it asserts out.
1047//
1048std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001049 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001050 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +00001051 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Reid Spencere0fc4df2006-10-20 07:07:24 +00001052 Result += (char)cast<ConstantInt>(getOperand(i))->getZExtValue();
Chris Lattner81fabb02002-08-26 17:53:56 +00001053 return Result;
1054}
1055
1056
Chris Lattner3462ae32001-12-03 22:26:30 +00001057//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001058//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001059
Chris Lattner189d19f2003-11-21 20:23:48 +00001060namespace llvm {
1061 template<>
1062 struct ConvertConstantType<ConstantStruct, StructType> {
1063 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1064 // Make everyone now use a constant of the new type...
1065 std::vector<Constant*> C;
1066 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1067 C.push_back(cast<Constant>(OldC->getOperand(i)));
1068 Constant *New = ConstantStruct::get(NewTy, C);
1069 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001070
Chris Lattner189d19f2003-11-21 20:23:48 +00001071 OldC->uncheckedReplaceAllUsesWith(New);
1072 OldC->destroyConstant(); // This constant is now dead, destroy it.
1073 }
1074 };
1075}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001076
Chris Lattner8760ec72005-10-04 01:17:50 +00001077typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattner935aa922005-10-04 17:48:46 +00001078 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001079static ManagedStatic<StructConstantsTy> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001080
Chris Lattner3e650af2004-08-04 04:48:01 +00001081static std::vector<Constant*> getValType(ConstantStruct *CS) {
1082 std::vector<Constant*> Elements;
1083 Elements.reserve(CS->getNumOperands());
1084 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1085 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1086 return Elements;
1087}
1088
Chris Lattner015e8212004-02-15 04:14:47 +00001089Constant *ConstantStruct::get(const StructType *Ty,
1090 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001091 // Create a ConstantAggregateZero value if all elements are zeros...
1092 for (unsigned i = 0, e = V.size(); i != e; ++i)
1093 if (!V[i]->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001094 return StructConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001095
1096 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001097}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001098
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001099Constant *ConstantStruct::get(const std::vector<Constant*> &V, bool packed) {
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001100 std::vector<const Type*> StructEls;
1101 StructEls.reserve(V.size());
1102 for (unsigned i = 0, e = V.size(); i != e; ++i)
1103 StructEls.push_back(V[i]->getType());
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001104 return get(StructType::get(StructEls, packed), V);
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001105}
1106
Chris Lattnerd7a73302001-10-13 06:57:33 +00001107// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001108//
Chris Lattner3462ae32001-12-03 22:26:30 +00001109void ConstantStruct::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001110 StructConstants->remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001111 destroyConstantImpl();
1112}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001113
Brian Gaeke02209042004-08-20 06:00:58 +00001114//---- ConstantPacked::get() implementation...
1115//
1116namespace llvm {
1117 template<>
1118 struct ConvertConstantType<ConstantPacked, PackedType> {
1119 static void convert(ConstantPacked *OldC, const PackedType *NewTy) {
1120 // Make everyone now use a constant of the new type...
1121 std::vector<Constant*> C;
1122 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1123 C.push_back(cast<Constant>(OldC->getOperand(i)));
1124 Constant *New = ConstantPacked::get(NewTy, C);
1125 assert(New != OldC && "Didn't replace constant??");
1126 OldC->uncheckedReplaceAllUsesWith(New);
1127 OldC->destroyConstant(); // This constant is now dead, destroy it.
1128 }
1129 };
1130}
1131
1132static std::vector<Constant*> getValType(ConstantPacked *CP) {
1133 std::vector<Constant*> Elements;
1134 Elements.reserve(CP->getNumOperands());
1135 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1136 Elements.push_back(CP->getOperand(i));
1137 return Elements;
1138}
1139
Chris Lattner69edc982006-09-28 00:35:06 +00001140static ManagedStatic<ValueMap<std::vector<Constant*>, PackedType,
1141 ConstantPacked> > PackedConstants;
Brian Gaeke02209042004-08-20 06:00:58 +00001142
1143Constant *ConstantPacked::get(const PackedType *Ty,
1144 const std::vector<Constant*> &V) {
1145 // If this is an all-zero packed, return a ConstantAggregateZero object
1146 if (!V.empty()) {
1147 Constant *C = V[0];
1148 if (!C->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001149 return PackedConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001150 for (unsigned i = 1, e = V.size(); i != e; ++i)
1151 if (V[i] != C)
Chris Lattner69edc982006-09-28 00:35:06 +00001152 return PackedConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001153 }
1154 return ConstantAggregateZero::get(Ty);
1155}
1156
1157Constant *ConstantPacked::get(const std::vector<Constant*> &V) {
1158 assert(!V.empty() && "Cannot infer type if V is empty");
1159 return get(PackedType::get(V.front()->getType(),V.size()), V);
1160}
1161
1162// destroyConstant - Remove the constant from the constant table...
1163//
1164void ConstantPacked::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001165 PackedConstants->remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001166 destroyConstantImpl();
1167}
1168
Jim Laskeyf0478822007-01-12 22:39:14 +00001169/// This function will return true iff every element in this packed constant
1170/// is set to all ones.
1171/// @returns true iff this constant's emements are all set to all ones.
1172/// @brief Determine if the value is all ones.
1173bool ConstantPacked::isAllOnesValue() const {
1174 // Check out first element.
1175 const Constant *Elt = getOperand(0);
1176 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1177 if (!CI || !CI->isAllOnesValue()) return false;
1178 // Then make sure all remaining elements point to the same value.
1179 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1180 if (getOperand(I) != Elt) return false;
1181 }
1182 return true;
1183}
1184
Chris Lattner3462ae32001-12-03 22:26:30 +00001185//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001186//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001187
Chris Lattner189d19f2003-11-21 20:23:48 +00001188namespace llvm {
1189 // ConstantPointerNull does not take extra "value" argument...
1190 template<class ValType>
1191 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1192 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1193 return new ConstantPointerNull(Ty);
1194 }
1195 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001196
Chris Lattner189d19f2003-11-21 20:23:48 +00001197 template<>
1198 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1199 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1200 // Make everyone now use a constant of the new type...
1201 Constant *New = ConstantPointerNull::get(NewTy);
1202 assert(New != OldC && "Didn't replace constant??");
1203 OldC->uncheckedReplaceAllUsesWith(New);
1204 OldC->destroyConstant(); // This constant is now dead, destroy it.
1205 }
1206 };
1207}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001208
Chris Lattner69edc982006-09-28 00:35:06 +00001209static ManagedStatic<ValueMap<char, PointerType,
1210 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001211
Chris Lattner3e650af2004-08-04 04:48:01 +00001212static char getValType(ConstantPointerNull *) {
1213 return 0;
1214}
1215
1216
Chris Lattner3462ae32001-12-03 22:26:30 +00001217ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001218 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001219}
1220
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001221// destroyConstant - Remove the constant from the constant table...
1222//
1223void ConstantPointerNull::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001224 NullPtrConstants->remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001225 destroyConstantImpl();
1226}
1227
1228
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001229//---- UndefValue::get() implementation...
1230//
1231
1232namespace llvm {
1233 // UndefValue does not take extra "value" argument...
1234 template<class ValType>
1235 struct ConstantCreator<UndefValue, Type, ValType> {
1236 static UndefValue *create(const Type *Ty, const ValType &V) {
1237 return new UndefValue(Ty);
1238 }
1239 };
1240
1241 template<>
1242 struct ConvertConstantType<UndefValue, Type> {
1243 static void convert(UndefValue *OldC, const Type *NewTy) {
1244 // Make everyone now use a constant of the new type.
1245 Constant *New = UndefValue::get(NewTy);
1246 assert(New != OldC && "Didn't replace constant??");
1247 OldC->uncheckedReplaceAllUsesWith(New);
1248 OldC->destroyConstant(); // This constant is now dead, destroy it.
1249 }
1250 };
1251}
1252
Chris Lattner69edc982006-09-28 00:35:06 +00001253static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001254
1255static char getValType(UndefValue *) {
1256 return 0;
1257}
1258
1259
1260UndefValue *UndefValue::get(const Type *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001261 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001262}
1263
1264// destroyConstant - Remove the constant from the constant table.
1265//
1266void UndefValue::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001267 UndefValueConstants->remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001268 destroyConstantImpl();
1269}
1270
1271
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001272//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001273//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001274
Reid Spenceree3c9912006-12-04 05:19:50 +00001275struct ExprMapKeyType {
1276 explicit ExprMapKeyType(unsigned opc, std::vector<Constant*> ops,
Reid Spencerdba6aa42006-12-04 18:38:05 +00001277 unsigned short pred = 0) : opcode(opc), predicate(pred), operands(ops) { }
1278 uint16_t opcode;
1279 uint16_t predicate;
Reid Spenceree3c9912006-12-04 05:19:50 +00001280 std::vector<Constant*> operands;
Reid Spenceree3c9912006-12-04 05:19:50 +00001281 bool operator==(const ExprMapKeyType& that) const {
1282 return this->opcode == that.opcode &&
1283 this->predicate == that.predicate &&
1284 this->operands == that.operands;
1285 }
1286 bool operator<(const ExprMapKeyType & that) const {
1287 return this->opcode < that.opcode ||
1288 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1289 (this->opcode == that.opcode && this->predicate == that.predicate &&
1290 this->operands < that.operands);
1291 }
1292
1293 bool operator!=(const ExprMapKeyType& that) const {
1294 return !(*this == that);
1295 }
1296};
Chris Lattner98fa07b2003-05-23 20:03:32 +00001297
Chris Lattner189d19f2003-11-21 20:23:48 +00001298namespace llvm {
1299 template<>
1300 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001301 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1302 unsigned short pred = 0) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001303 if (Instruction::isCast(V.opcode))
1304 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1305 if ((V.opcode >= Instruction::BinaryOpsBegin &&
1306 V.opcode < Instruction::BinaryOpsEnd) ||
1307 V.opcode == Instruction::Shl ||
1308 V.opcode == Instruction::LShr ||
1309 V.opcode == Instruction::AShr)
1310 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1311 if (V.opcode == Instruction::Select)
1312 return new SelectConstantExpr(V.operands[0], V.operands[1],
1313 V.operands[2]);
1314 if (V.opcode == Instruction::ExtractElement)
1315 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1316 if (V.opcode == Instruction::InsertElement)
1317 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1318 V.operands[2]);
1319 if (V.opcode == Instruction::ShuffleVector)
1320 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1321 V.operands[2]);
1322 if (V.opcode == Instruction::GetElementPtr) {
1323 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
1324 return new GetElementPtrConstantExpr(V.operands[0], IdxList, Ty);
1325 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001326
Reid Spenceree3c9912006-12-04 05:19:50 +00001327 // The compare instructions are weird. We have to encode the predicate
1328 // value and it is combined with the instruction opcode by multiplying
1329 // the opcode by one hundred. We must decode this to get the predicate.
1330 if (V.opcode == Instruction::ICmp)
1331 return new CompareConstantExpr(Instruction::ICmp, V.predicate,
1332 V.operands[0], V.operands[1]);
1333 if (V.opcode == Instruction::FCmp)
1334 return new CompareConstantExpr(Instruction::FCmp, V.predicate,
1335 V.operands[0], V.operands[1]);
1336 assert(0 && "Invalid ConstantExpr!");
Jeff Cohen9f469632006-12-15 21:47:01 +00001337 return 0;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001338 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001339 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001340
Chris Lattner189d19f2003-11-21 20:23:48 +00001341 template<>
1342 struct ConvertConstantType<ConstantExpr, Type> {
1343 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1344 Constant *New;
1345 switch (OldC->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001346 case Instruction::Trunc:
1347 case Instruction::ZExt:
1348 case Instruction::SExt:
1349 case Instruction::FPTrunc:
1350 case Instruction::FPExt:
1351 case Instruction::UIToFP:
1352 case Instruction::SIToFP:
1353 case Instruction::FPToUI:
1354 case Instruction::FPToSI:
1355 case Instruction::PtrToInt:
1356 case Instruction::IntToPtr:
1357 case Instruction::BitCast:
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001358 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
1359 NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001360 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001361 case Instruction::Select:
1362 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1363 OldC->getOperand(1),
1364 OldC->getOperand(2));
1365 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001366 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001367 case Instruction::LShr:
1368 case Instruction::AShr:
Chris Lattner189d19f2003-11-21 20:23:48 +00001369 New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
1370 OldC->getOperand(0), OldC->getOperand(1));
1371 break;
1372 default:
1373 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001374 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner189d19f2003-11-21 20:23:48 +00001375 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1376 OldC->getOperand(1));
1377 break;
1378 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001379 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001380 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
1381 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), Idx);
Chris Lattner189d19f2003-11-21 20:23:48 +00001382 break;
1383 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001384
Chris Lattner189d19f2003-11-21 20:23:48 +00001385 assert(New != OldC && "Didn't replace constant??");
1386 OldC->uncheckedReplaceAllUsesWith(New);
1387 OldC->destroyConstant(); // This constant is now dead, destroy it.
1388 }
1389 };
1390} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001391
1392
Chris Lattner3e650af2004-08-04 04:48:01 +00001393static ExprMapKeyType getValType(ConstantExpr *CE) {
1394 std::vector<Constant*> Operands;
1395 Operands.reserve(CE->getNumOperands());
1396 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1397 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spenceree3c9912006-12-04 05:19:50 +00001398 return ExprMapKeyType(CE->getOpcode(), Operands,
1399 CE->isCompare() ? CE->getPredicate() : 0);
Chris Lattner3e650af2004-08-04 04:48:01 +00001400}
1401
Chris Lattner69edc982006-09-28 00:35:06 +00001402static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1403 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001404
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001405/// This is a utility function to handle folding of casts and lookup of the
1406/// cast in the ExprConstants map. It is usedby the various get* methods below.
1407static inline Constant *getFoldedCast(
1408 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001409 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001410 // Fold a few common cases
1411 if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
1412 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001413
Vikram S. Adve4c485332002-07-15 18:19:33 +00001414 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001415 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00001416 ExprMapKeyType Key(opc, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001417 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001418}
Reid Spencerf37dc652006-12-05 19:14:13 +00001419
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001420Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
1421 Instruction::CastOps opc = Instruction::CastOps(oc);
1422 assert(Instruction::isCast(opc) && "opcode out of range");
1423 assert(C && Ty && "Null arguments to getCast");
1424 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1425
1426 switch (opc) {
1427 default:
1428 assert(0 && "Invalid cast opcode");
1429 break;
1430 case Instruction::Trunc: return getTrunc(C, Ty);
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001431 case Instruction::ZExt: return getZExt(C, Ty);
1432 case Instruction::SExt: return getSExt(C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001433 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1434 case Instruction::FPExt: return getFPExtend(C, Ty);
1435 case Instruction::UIToFP: return getUIToFP(C, Ty);
1436 case Instruction::SIToFP: return getSIToFP(C, Ty);
1437 case Instruction::FPToUI: return getFPToUI(C, Ty);
1438 case Instruction::FPToSI: return getFPToSI(C, Ty);
1439 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1440 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1441 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001442 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001443 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00001444}
1445
Reid Spencer5c140882006-12-04 20:17:56 +00001446Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
1447 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1448 return getCast(Instruction::BitCast, C, Ty);
1449 return getCast(Instruction::ZExt, C, Ty);
1450}
1451
1452Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
1453 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1454 return getCast(Instruction::BitCast, C, Ty);
1455 return getCast(Instruction::SExt, C, Ty);
1456}
1457
1458Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
1459 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1460 return getCast(Instruction::BitCast, C, Ty);
1461 return getCast(Instruction::Trunc, C, Ty);
1462}
1463
Reid Spencerbc245a02006-12-05 03:25:26 +00001464Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
1465 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001466 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001467
Chris Lattner03c49532007-01-15 02:27:26 +00001468 if (Ty->isInteger())
Reid Spencerbc245a02006-12-05 03:25:26 +00001469 return getCast(Instruction::PtrToInt, S, Ty);
1470 return getCast(Instruction::BitCast, S, Ty);
1471}
1472
Reid Spencer56521c42006-12-12 00:51:07 +00001473Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1474 bool isSigned) {
Chris Lattner03c49532007-01-15 02:27:26 +00001475 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer56521c42006-12-12 00:51:07 +00001476 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1477 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1478 Instruction::CastOps opcode =
1479 (SrcBits == DstBits ? Instruction::BitCast :
1480 (SrcBits > DstBits ? Instruction::Trunc :
1481 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1482 return getCast(opcode, C, Ty);
1483}
1484
1485Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
1486 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1487 "Invalid cast");
1488 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1489 unsigned DstBits = Ty->getPrimitiveSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001490 if (SrcBits == DstBits)
1491 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001492 Instruction::CastOps opcode =
Reid Spencerca104e82006-12-12 05:38:50 +00001493 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001494 return getCast(opcode, C, Ty);
1495}
1496
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001497Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001498 assert(C->getType()->isInteger() && "Trunc operand must be integer");
1499 assert(Ty->isInteger() && "Trunc produces only integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001500 assert(C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&&
1501 "SrcTy must be larger than DestTy for Trunc!");
1502
1503 return getFoldedCast(Instruction::Trunc, C, Ty);
1504}
1505
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001506Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001507 assert(C->getType()->isInteger() && "SEXt operand must be integral");
1508 assert(Ty->isInteger() && "SExt produces only integer");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001509 assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1510 "SrcTy must be smaller than DestTy for SExt!");
1511
1512 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerdd284742004-04-04 23:20:30 +00001513}
1514
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001515Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001516 assert(C->getType()->isInteger() && "ZEXt operand must be integral");
1517 assert(Ty->isInteger() && "ZExt produces only integer");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001518 assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1519 "SrcTy must be smaller than DestTy for ZExt!");
1520
1521 return getFoldedCast(Instruction::ZExt, C, Ty);
1522}
1523
1524Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
1525 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1526 C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&&
1527 "This is an illegal floating point truncation!");
1528 return getFoldedCast(Instruction::FPTrunc, C, Ty);
1529}
1530
1531Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
1532 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1533 C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1534 "This is an illegal floating point extension!");
1535 return getFoldedCast(Instruction::FPExt, C, Ty);
1536}
1537
1538Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001539 assert(C->getType()->isInteger() && Ty->isFloatingPoint() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001540 "This is an illegal uint to floating point cast!");
1541 return getFoldedCast(Instruction::UIToFP, C, Ty);
1542}
1543
1544Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001545 assert(C->getType()->isInteger() && Ty->isFloatingPoint() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001546 "This is an illegal sint to floating point cast!");
1547 return getFoldedCast(Instruction::SIToFP, C, Ty);
1548}
1549
1550Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001551 assert(C->getType()->isFloatingPoint() && Ty->isInteger() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001552 "This is an illegal floating point to uint cast!");
1553 return getFoldedCast(Instruction::FPToUI, C, Ty);
1554}
1555
1556Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001557 assert(C->getType()->isFloatingPoint() && Ty->isInteger() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001558 "This is an illegal floating point to sint cast!");
1559 return getFoldedCast(Instruction::FPToSI, C, Ty);
1560}
1561
1562Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
1563 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner03c49532007-01-15 02:27:26 +00001564 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001565 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
1566}
1567
1568Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner03c49532007-01-15 02:27:26 +00001569 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001570 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
1571 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
1572}
1573
1574Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
1575 // BitCast implies a no-op cast of type only. No bits change. However, you
1576 // can't cast pointers to anything but pointers.
1577 const Type *SrcTy = C->getType();
1578 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer5c140882006-12-04 20:17:56 +00001579 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001580
1581 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
1582 // or nonptr->ptr). For all the other types, the cast is okay if source and
1583 // destination bit widths are identical.
1584 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1585 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Reid Spencer5c140882006-12-04 20:17:56 +00001586 assert(SrcBitSize == DstBitSize && "BitCast requies types of same width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001587 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerdd284742004-04-04 23:20:30 +00001588}
1589
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001590Constant *ConstantExpr::getSizeOf(const Type *Ty) {
Chris Lattneracc4e542004-12-13 19:48:51 +00001591 // sizeof is implemented as: (ulong) gep (Ty*)null, 1
Reid Spencerc4dacf22006-12-04 02:43:42 +00001592 return getCast(Instruction::PtrToInt, getGetElementPtr(getNullValue(
1593 PointerType::get(Ty)), std::vector<Constant*>(1,
Reid Spencer8d9336d2006-12-31 05:26:44 +00001594 ConstantInt::get(Type::Int32Ty, 1))), Type::Int64Ty);
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001595}
1596
Alkis Evlogimenos9160d5f2005-03-19 11:40:31 +00001597Constant *ConstantExpr::getPtrPtrFromArrayPtr(Constant *C) {
1598 // pointer from array is implemented as: getelementptr arr ptr, 0, 0
Reid Spencer8d9336d2006-12-31 05:26:44 +00001599 static std::vector<Constant*> Indices(2, ConstantInt::get(Type::Int32Ty, 0));
Alkis Evlogimenos9160d5f2005-03-19 11:40:31 +00001600
1601 return ConstantExpr::getGetElementPtr(C, Indices);
1602}
1603
Chris Lattnerb50d1352003-10-05 00:17:43 +00001604Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Reid Spencera009d0d2006-12-04 21:35:24 +00001605 Constant *C1, Constant *C2) {
Reid Spencerfdff9382006-11-08 06:47:33 +00001606 if (Opcode == Instruction::Shl || Opcode == Instruction::LShr ||
1607 Opcode == Instruction::AShr)
Chris Lattner5645e8a2004-01-12 19:04:55 +00001608 return getShiftTy(ReqTy, Opcode, C1, C2);
Reid Spenceree3c9912006-12-04 05:19:50 +00001609
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001610 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00001611 assert(Opcode >= Instruction::BinaryOpsBegin &&
1612 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001613 "Invalid opcode in binary constant expression");
1614 assert(C1->getType() == C2->getType() &&
1615 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001616
Reid Spencer542964f2007-01-11 18:21:29 +00001617 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Chris Lattnerb50d1352003-10-05 00:17:43 +00001618 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1619 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001620
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001621 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001622 ExprMapKeyType Key(Opcode, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001623 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001624}
1625
Reid Spencer266e42b2006-12-23 06:05:41 +00001626Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Reid Spencera009d0d2006-12-04 21:35:24 +00001627 Constant *C1, Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001628 switch (predicate) {
1629 default: assert(0 && "Invalid CmpInst predicate");
1630 case FCmpInst::FCMP_FALSE: case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_OGT:
1631 case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OLE:
1632 case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_ORD: case FCmpInst::FCMP_UNO:
1633 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UGT: case FCmpInst::FCMP_UGE:
1634 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_ULE: case FCmpInst::FCMP_UNE:
1635 case FCmpInst::FCMP_TRUE:
1636 return getFCmp(predicate, C1, C2);
1637 case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_UGT:
1638 case ICmpInst::ICMP_UGE: case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE:
1639 case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_SGE: case ICmpInst::ICMP_SLT:
1640 case ICmpInst::ICMP_SLE:
1641 return getICmp(predicate, C1, C2);
1642 }
Reid Spencera009d0d2006-12-04 21:35:24 +00001643}
1644
1645Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001646#ifndef NDEBUG
1647 switch (Opcode) {
Reid Spencer7eb55b32006-11-02 01:53:59 +00001648 case Instruction::Add:
1649 case Instruction::Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001650 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001651 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattner03c49532007-01-15 02:27:26 +00001652 assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint() ||
Chris Lattnerc421a262006-01-04 01:01:04 +00001653 isa<PackedType>(C1->getType())) &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001654 "Tried to create an arithmetic operation on a non-arithmetic type!");
1655 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001656 case Instruction::UDiv:
1657 case Instruction::SDiv:
1658 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattner03c49532007-01-15 02:27:26 +00001659 assert((C1->getType()->isInteger() || (isa<PackedType>(C1->getType()) &&
1660 cast<PackedType>(C1->getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001661 "Tried to create an arithmetic operation on a non-arithmetic type!");
1662 break;
1663 case Instruction::FDiv:
1664 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1665 assert((C1->getType()->isFloatingPoint() || (isa<PackedType>(C1->getType())
1666 && cast<PackedType>(C1->getType())->getElementType()->isFloatingPoint()))
1667 && "Tried to create an arithmetic operation on a non-arithmetic type!");
1668 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001669 case Instruction::URem:
1670 case Instruction::SRem:
1671 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattner03c49532007-01-15 02:27:26 +00001672 assert((C1->getType()->isInteger() || (isa<PackedType>(C1->getType()) &&
1673 cast<PackedType>(C1->getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001674 "Tried to create an arithmetic operation on a non-arithmetic type!");
1675 break;
1676 case Instruction::FRem:
1677 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1678 assert((C1->getType()->isFloatingPoint() || (isa<PackedType>(C1->getType())
1679 && cast<PackedType>(C1->getType())->getElementType()->isFloatingPoint()))
1680 && "Tried to create an arithmetic operation on a non-arithmetic type!");
1681 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001682 case Instruction::And:
1683 case Instruction::Or:
1684 case Instruction::Xor:
1685 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattner03c49532007-01-15 02:27:26 +00001686 assert((C1->getType()->isInteger() || isa<PackedType>(C1->getType())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001687 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001688 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001689 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001690 case Instruction::LShr:
1691 case Instruction::AShr:
Reid Spencer8d9336d2006-12-31 05:26:44 +00001692 assert(C2->getType() == Type::Int8Ty && "Shift should be by ubyte!");
Chris Lattner03c49532007-01-15 02:27:26 +00001693 assert(C1->getType()->isInteger() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001694 "Tried to create a shift operation on a non-integer type!");
1695 break;
1696 default:
1697 break;
1698 }
1699#endif
1700
Reid Spencera009d0d2006-12-04 21:35:24 +00001701 return getTy(C1->getType(), Opcode, C1, C2);
1702}
1703
Reid Spencer266e42b2006-12-23 06:05:41 +00001704Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00001705 Constant *C1, Constant *C2) {
1706 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001707 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00001708}
1709
Chris Lattner6e415c02004-03-12 05:54:04 +00001710Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
1711 Constant *V1, Constant *V2) {
Reid Spencer542964f2007-01-11 18:21:29 +00001712 assert(C->getType() == Type::Int1Ty && "Select condition must be bool!");
Chris Lattner6e415c02004-03-12 05:54:04 +00001713 assert(V1->getType() == V2->getType() && "Select value types must match!");
1714 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
1715
1716 if (ReqTy == V1->getType())
1717 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1718 return SC; // Fold common cases
1719
1720 std::vector<Constant*> argVec(3, C);
1721 argVec[1] = V1;
1722 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00001723 ExprMapKeyType Key(Instruction::Select, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001724 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00001725}
1726
Chris Lattner9eb2b522004-01-12 19:12:58 +00001727/// getShiftTy - Return a shift left or shift right constant expr
Chris Lattnerb50d1352003-10-05 00:17:43 +00001728Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
1729 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001730 // Check the operands for consistency first
Reid Spencerfdff9382006-11-08 06:47:33 +00001731 assert((Opcode == Instruction::Shl ||
1732 Opcode == Instruction::LShr ||
1733 Opcode == Instruction::AShr) &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001734 "Invalid opcode in binary constant expression");
Chris Lattner03c49532007-01-15 02:27:26 +00001735 assert(C1->getType()->isInteger() && C2->getType() == Type::Int8Ty &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001736 "Invalid operand types for Shift constant expr!");
1737
Chris Lattner0bba7712004-01-12 20:40:42 +00001738 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001739 return FC; // Fold a few common cases...
1740
1741 // Look up the constant in the table first to ensure uniqueness
1742 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spenceree3c9912006-12-04 05:19:50 +00001743 ExprMapKeyType Key(Opcode, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001744 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001745}
1746
Chris Lattnerb50d1352003-10-05 00:17:43 +00001747Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner13128ab2004-10-11 22:52:25 +00001748 const std::vector<Value*> &IdxList) {
1749 assert(GetElementPtrInst::getIndexedType(C->getType(), IdxList, true) &&
Chris Lattner04b60fe2004-02-16 20:46:13 +00001750 "GEP indices invalid!");
1751
Chris Lattneracdbe712003-04-17 19:24:48 +00001752 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
1753 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001754
Chris Lattnerb50d1352003-10-05 00:17:43 +00001755 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001756 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001757 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00001758 std::vector<Constant*> ArgVec;
1759 ArgVec.reserve(IdxList.size()+1);
1760 ArgVec.push_back(C);
1761 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
1762 ArgVec.push_back(cast<Constant>(IdxList[i]));
Reid Spenceree3c9912006-12-04 05:19:50 +00001763 const ExprMapKeyType Key(Instruction::GetElementPtr,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001764 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001765}
1766
Chris Lattnerb50d1352003-10-05 00:17:43 +00001767Constant *ConstantExpr::getGetElementPtr(Constant *C,
1768 const std::vector<Constant*> &IdxList){
1769 // Get the result type of the getelementptr!
1770 std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
1771
1772 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
1773 true);
1774 assert(Ty && "GEP indices invalid!");
Chris Lattner13128ab2004-10-11 22:52:25 +00001775 return getGetElementPtrTy(PointerType::get(Ty), C, VIdxList);
1776}
1777
1778Constant *ConstantExpr::getGetElementPtr(Constant *C,
1779 const std::vector<Value*> &IdxList) {
1780 // Get the result type of the getelementptr!
1781 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
1782 true);
1783 assert(Ty && "GEP indices invalid!");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001784 return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
1785}
1786
Reid Spenceree3c9912006-12-04 05:19:50 +00001787Constant *
1788ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1789 assert(LHS->getType() == RHS->getType());
1790 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
1791 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
1792
Reid Spencer266e42b2006-12-23 06:05:41 +00001793 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001794 return FC; // Fold a few common cases...
1795
1796 // Look up the constant in the table first to ensure uniqueness
1797 std::vector<Constant*> ArgVec;
1798 ArgVec.push_back(LHS);
1799 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001800 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001801 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Reid Spencer542964f2007-01-11 18:21:29 +00001802 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001803}
1804
1805Constant *
1806ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1807 assert(LHS->getType() == RHS->getType());
1808 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
1809
Reid Spencer266e42b2006-12-23 06:05:41 +00001810 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001811 return FC; // Fold a few common cases...
1812
1813 // Look up the constant in the table first to ensure uniqueness
1814 std::vector<Constant*> ArgVec;
1815 ArgVec.push_back(LHS);
1816 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001817 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001818 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Reid Spencer542964f2007-01-11 18:21:29 +00001819 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001820}
1821
Robert Bocchino23004482006-01-10 19:05:34 +00001822Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
1823 Constant *Idx) {
Robert Bocchinode7f1c92006-01-10 20:03:46 +00001824 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
1825 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00001826 // Look up the constant in the table first to ensure uniqueness
1827 std::vector<Constant*> ArgVec(1, Val);
1828 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001829 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001830 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00001831}
1832
1833Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
1834 assert(isa<PackedType>(Val->getType()) &&
1835 "Tried to create extractelement operation on non-packed type!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001836 assert(Idx->getType() == Type::Int32Ty &&
Robert Bocchinoca27f032006-01-17 20:07:22 +00001837 "Extractelement index must be uint type!");
Robert Bocchino23004482006-01-10 19:05:34 +00001838 return getExtractElementTy(cast<PackedType>(Val->getType())->getElementType(),
1839 Val, Idx);
1840}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001841
Robert Bocchinoca27f032006-01-17 20:07:22 +00001842Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
1843 Constant *Elt, Constant *Idx) {
1844 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
1845 return FC; // Fold a few common cases...
1846 // Look up the constant in the table first to ensure uniqueness
1847 std::vector<Constant*> ArgVec(1, Val);
1848 ArgVec.push_back(Elt);
1849 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001850 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001851 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001852}
1853
1854Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1855 Constant *Idx) {
1856 assert(isa<PackedType>(Val->getType()) &&
1857 "Tried to create insertelement operation on non-packed type!");
1858 assert(Elt->getType() == cast<PackedType>(Val->getType())->getElementType()
1859 && "Insertelement types must match!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001860 assert(Idx->getType() == Type::Int32Ty &&
Robert Bocchinoca27f032006-01-17 20:07:22 +00001861 "Insertelement index must be uint type!");
1862 return getInsertElementTy(cast<PackedType>(Val->getType())->getElementType(),
1863 Val, Elt, Idx);
1864}
1865
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001866Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
1867 Constant *V2, Constant *Mask) {
1868 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
1869 return FC; // Fold a few common cases...
1870 // Look up the constant in the table first to ensure uniqueness
1871 std::vector<Constant*> ArgVec(1, V1);
1872 ArgVec.push_back(V2);
1873 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00001874 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001875 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001876}
1877
1878Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
1879 Constant *Mask) {
1880 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
1881 "Invalid shuffle vector constant expr operands!");
1882 return getShuffleVectorTy(V1->getType(), V1, V2, Mask);
1883}
1884
Vikram S. Adve4c485332002-07-15 18:19:33 +00001885// destroyConstant - Remove the constant from the constant table...
1886//
1887void ConstantExpr::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001888 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001889 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001890}
1891
Chris Lattner3cd8c562002-07-30 18:54:25 +00001892const char *ConstantExpr::getOpcodeName() const {
1893 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001894}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00001895
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001896//===----------------------------------------------------------------------===//
1897// replaceUsesOfWithOnConstant implementations
1898
1899void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001900 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001901 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00001902 Constant *ToC = cast<Constant>(To);
Chris Lattnerdff59112005-10-04 18:47:09 +00001903
1904 unsigned OperandToUpdate = U-OperandList;
1905 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1906
Jim Laskeyc03caef2006-07-17 17:38:29 +00001907 std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnerb64419a2005-10-03 22:51:37 +00001908 Lookup.first.first = getType();
1909 Lookup.second = this;
Chris Lattnerdff59112005-10-04 18:47:09 +00001910
Chris Lattnerb64419a2005-10-03 22:51:37 +00001911 std::vector<Constant*> &Values = Lookup.first.second;
1912 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattnerdff59112005-10-04 18:47:09 +00001913
Chris Lattner8760ec72005-10-04 01:17:50 +00001914 // Fill values with the modified operands of the constant array. Also,
1915 // compute whether this turns into an all-zeros array.
Chris Lattnerdff59112005-10-04 18:47:09 +00001916 bool isAllZeros = false;
1917 if (!ToC->isNullValue()) {
1918 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1919 Values.push_back(cast<Constant>(O->get()));
1920 } else {
1921 isAllZeros = true;
1922 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1923 Constant *Val = cast<Constant>(O->get());
1924 Values.push_back(Val);
1925 if (isAllZeros) isAllZeros = Val->isNullValue();
1926 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001927 }
Chris Lattnerdff59112005-10-04 18:47:09 +00001928 Values[OperandToUpdate] = ToC;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001929
Chris Lattnerb64419a2005-10-03 22:51:37 +00001930 Constant *Replacement = 0;
1931 if (isAllZeros) {
1932 Replacement = ConstantAggregateZero::get(getType());
1933 } else {
1934 // Check to see if we have this array type already.
1935 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00001936 ArrayConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00001937 ArrayConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001938
1939 if (Exists) {
1940 Replacement = I->second;
1941 } else {
1942 // Okay, the new shape doesn't exist in the system yet. Instead of
1943 // creating a new constant array, inserting it, replaceallusesof'ing the
1944 // old with the new, then deleting the old... just update the current one
1945 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00001946 ArrayConstants->MoveConstantToNewSlot(this, I);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001947
Chris Lattnerdff59112005-10-04 18:47:09 +00001948 // Update to the new value.
1949 setOperand(OperandToUpdate, ToC);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001950 return;
1951 }
1952 }
1953
1954 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001955 assert(Replacement != this && "I didn't contain From!");
1956
Chris Lattner7a1450d2005-10-04 18:13:04 +00001957 // Everyone using this now uses the replacement.
1958 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001959
1960 // Delete the old constant!
1961 destroyConstant();
1962}
1963
1964void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001965 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001966 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00001967 Constant *ToC = cast<Constant>(To);
1968
Chris Lattnerdff59112005-10-04 18:47:09 +00001969 unsigned OperandToUpdate = U-OperandList;
1970 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1971
Jim Laskeyc03caef2006-07-17 17:38:29 +00001972 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattner8760ec72005-10-04 01:17:50 +00001973 Lookup.first.first = getType();
1974 Lookup.second = this;
1975 std::vector<Constant*> &Values = Lookup.first.second;
1976 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001977
Chris Lattnerdff59112005-10-04 18:47:09 +00001978
Chris Lattner8760ec72005-10-04 01:17:50 +00001979 // Fill values with the modified operands of the constant struct. Also,
1980 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00001981 bool isAllZeros = false;
1982 if (!ToC->isNullValue()) {
1983 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1984 Values.push_back(cast<Constant>(O->get()));
1985 } else {
1986 isAllZeros = true;
1987 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1988 Constant *Val = cast<Constant>(O->get());
1989 Values.push_back(Val);
1990 if (isAllZeros) isAllZeros = Val->isNullValue();
1991 }
Chris Lattner8760ec72005-10-04 01:17:50 +00001992 }
Chris Lattnerdff59112005-10-04 18:47:09 +00001993 Values[OperandToUpdate] = ToC;
1994
Chris Lattner8760ec72005-10-04 01:17:50 +00001995 Constant *Replacement = 0;
1996 if (isAllZeros) {
1997 Replacement = ConstantAggregateZero::get(getType());
1998 } else {
1999 // Check to see if we have this array type already.
2000 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002001 StructConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002002 StructConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00002003
2004 if (Exists) {
2005 Replacement = I->second;
2006 } else {
2007 // Okay, the new shape doesn't exist in the system yet. Instead of
2008 // creating a new constant struct, inserting it, replaceallusesof'ing the
2009 // old with the new, then deleting the old... just update the current one
2010 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002011 StructConstants->MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00002012
Chris Lattnerdff59112005-10-04 18:47:09 +00002013 // Update to the new value.
2014 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00002015 return;
2016 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002017 }
2018
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002019 assert(Replacement != this && "I didn't contain From!");
2020
Chris Lattner7a1450d2005-10-04 18:13:04 +00002021 // Everyone using this now uses the replacement.
2022 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002023
2024 // Delete the old constant!
2025 destroyConstant();
2026}
2027
2028void ConstantPacked::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002029 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002030 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2031
2032 std::vector<Constant*> Values;
2033 Values.reserve(getNumOperands()); // Build replacement array...
2034 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2035 Constant *Val = getOperand(i);
2036 if (Val == From) Val = cast<Constant>(To);
2037 Values.push_back(Val);
2038 }
2039
2040 Constant *Replacement = ConstantPacked::get(getType(), Values);
2041 assert(Replacement != this && "I didn't contain From!");
2042
Chris Lattner7a1450d2005-10-04 18:13:04 +00002043 // Everyone using this now uses the replacement.
2044 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002045
2046 // Delete the old constant!
2047 destroyConstant();
2048}
2049
2050void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002051 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002052 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2053 Constant *To = cast<Constant>(ToV);
2054
2055 Constant *Replacement = 0;
2056 if (getOpcode() == Instruction::GetElementPtr) {
2057 std::vector<Constant*> Indices;
2058 Constant *Pointer = getOperand(0);
2059 Indices.reserve(getNumOperands()-1);
2060 if (Pointer == From) Pointer = To;
2061
2062 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2063 Constant *Val = getOperand(i);
2064 if (Val == From) Val = To;
2065 Indices.push_back(Val);
2066 }
2067 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002068 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002069 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002070 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002071 } else if (getOpcode() == Instruction::Select) {
2072 Constant *C1 = getOperand(0);
2073 Constant *C2 = getOperand(1);
2074 Constant *C3 = getOperand(2);
2075 if (C1 == From) C1 = To;
2076 if (C2 == From) C2 = To;
2077 if (C3 == From) C3 = To;
2078 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00002079 } else if (getOpcode() == Instruction::ExtractElement) {
2080 Constant *C1 = getOperand(0);
2081 Constant *C2 = getOperand(1);
2082 if (C1 == From) C1 = To;
2083 if (C2 == From) C2 = To;
2084 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00002085 } else if (getOpcode() == Instruction::InsertElement) {
2086 Constant *C1 = getOperand(0);
2087 Constant *C2 = getOperand(1);
2088 Constant *C3 = getOperand(1);
2089 if (C1 == From) C1 = To;
2090 if (C2 == From) C2 = To;
2091 if (C3 == From) C3 = To;
2092 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2093 } else if (getOpcode() == Instruction::ShuffleVector) {
2094 Constant *C1 = getOperand(0);
2095 Constant *C2 = getOperand(1);
2096 Constant *C3 = getOperand(2);
2097 if (C1 == From) C1 = To;
2098 if (C2 == From) C2 = To;
2099 if (C3 == From) C3 = To;
2100 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00002101 } else if (isCompare()) {
2102 Constant *C1 = getOperand(0);
2103 Constant *C2 = getOperand(1);
2104 if (C1 == From) C1 = To;
2105 if (C2 == From) C2 = To;
2106 if (getOpcode() == Instruction::ICmp)
2107 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
2108 else
2109 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002110 } else if (getNumOperands() == 2) {
2111 Constant *C1 = getOperand(0);
2112 Constant *C2 = getOperand(1);
2113 if (C1 == From) C1 = To;
2114 if (C2 == From) C2 = To;
2115 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2116 } else {
2117 assert(0 && "Unknown ConstantExpr type!");
2118 return;
2119 }
2120
2121 assert(Replacement != this && "I didn't contain From!");
2122
Chris Lattner7a1450d2005-10-04 18:13:04 +00002123 // Everyone using this now uses the replacement.
2124 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002125
2126 // Delete the old constant!
2127 destroyConstant();
2128}
2129
2130
Jim Laskey2698f0d2006-03-08 18:11:07 +00002131/// getStringValue - Turn an LLVM constant pointer that eventually points to a
2132/// global into a string value. Return an empty string if we can't do it.
Evan Cheng38280c02006-03-10 23:52:03 +00002133/// Parameter Chop determines if the result is chopped at the first null
2134/// terminator.
Jim Laskey2698f0d2006-03-08 18:11:07 +00002135///
Evan Cheng38280c02006-03-10 23:52:03 +00002136std::string Constant::getStringValue(bool Chop, unsigned Offset) {
Jim Laskey2698f0d2006-03-08 18:11:07 +00002137 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(this)) {
2138 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
2139 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
2140 if (Init->isString()) {
2141 std::string Result = Init->getAsString();
2142 if (Offset < Result.size()) {
2143 // If we are pointing INTO The string, erase the beginning...
2144 Result.erase(Result.begin(), Result.begin()+Offset);
2145
2146 // Take off the null terminator, and any string fragments after it.
Evan Cheng38280c02006-03-10 23:52:03 +00002147 if (Chop) {
2148 std::string::size_type NullPos = Result.find_first_of((char)0);
2149 if (NullPos != std::string::npos)
2150 Result.erase(Result.begin()+NullPos, Result.end());
2151 }
Jim Laskey2698f0d2006-03-08 18:11:07 +00002152 return Result;
2153 }
2154 }
2155 }
2156 } else if (Constant *C = dyn_cast<Constant>(this)) {
2157 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
Evan Cheng2c5e5302006-03-11 00:13:10 +00002158 return GV->getStringValue(Chop, Offset);
Jim Laskey2698f0d2006-03-08 18:11:07 +00002159 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
2160 if (CE->getOpcode() == Instruction::GetElementPtr) {
2161 // Turn a gep into the specified offset.
2162 if (CE->getNumOperands() == 3 &&
2163 cast<Constant>(CE->getOperand(1))->isNullValue() &&
2164 isa<ConstantInt>(CE->getOperand(2))) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00002165 Offset += cast<ConstantInt>(CE->getOperand(2))->getZExtValue();
Evan Cheng2c5e5302006-03-11 00:13:10 +00002166 return CE->getOperand(0)->getStringValue(Chop, Offset);
Jim Laskey2698f0d2006-03-08 18:11:07 +00002167 }
2168 }
2169 }
2170 }
2171 return "";
2172}