blob: ec02d30bd289b69b6571375ae01c4990ddbb7c15 [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 Lattnerd7a73302001-10-13 06:57:33 +000019#include "llvm/Module.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000020#include "llvm/ADT/StringExtras.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000021#include "llvm/Support/Compiler.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000022#include "llvm/Support/Debug.h"
Chris Lattner69edc982006-09-28 00:35:06 +000023#include "llvm/Support/ManagedStatic.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000024#include "llvm/Support/MathExtras.h"
Chris Lattnerb5d70302007-02-19 20:01:23 +000025#include "llvm/ADT/SmallVector.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000026#include <algorithm>
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000027#include <map>
Chris Lattner189d19f2003-11-21 20:23:48 +000028using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000029
Chris Lattner2f7c9632001-06-06 20:29:01 +000030//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000031// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000032//===----------------------------------------------------------------------===//
33
Chris Lattner3462ae32001-12-03 22:26:30 +000034void Constant::destroyConstantImpl() {
35 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000036 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000037 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +000038 // but they don't know that. Because we only find out when the CPV is
39 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000040 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000041 //
42 while (!use_empty()) {
43 Value *V = use_back();
44#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000045 if (!isa<Constant>(V))
Bill Wendling6a462f12006-11-17 08:03:48 +000046 DOUT << "While deleting: " << *this
47 << "\n\nUse still stuck around after Def is destroyed: "
48 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000049#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000050 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000051 Constant *CV = cast<Constant>(V);
52 CV->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +000053
54 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000055 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000056 }
57
58 // Value has no outstanding references it is safe to delete it now...
59 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000060}
Chris Lattner2f7c9632001-06-06 20:29:01 +000061
Chris Lattner23dd1f62006-10-20 00:27:06 +000062/// canTrap - Return true if evaluation of this constant could trap. This is
63/// true for things like constant expressions that could divide by zero.
64bool Constant::canTrap() const {
65 assert(getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
66 // The only thing that could possibly trap are constant exprs.
67 const ConstantExpr *CE = dyn_cast<ConstantExpr>(this);
68 if (!CE) return false;
69
70 // ConstantExpr traps if any operands can trap.
71 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
72 if (getOperand(i)->canTrap())
73 return true;
74
75 // Otherwise, only specific operations can trap.
76 switch (CE->getOpcode()) {
77 default:
78 return false;
Reid Spencer7e80b0b2006-10-26 06:15:43 +000079 case Instruction::UDiv:
80 case Instruction::SDiv:
81 case Instruction::FDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +000082 case Instruction::URem:
83 case Instruction::SRem:
84 case Instruction::FRem:
Chris Lattner23dd1f62006-10-20 00:27:06 +000085 // Div and rem can trap if the RHS is not known to be non-zero.
86 if (!isa<ConstantInt>(getOperand(1)) || getOperand(1)->isNullValue())
87 return true;
88 return false;
89 }
90}
91
Chris Lattnerb1585a92002-08-13 17:50:20 +000092// Static constructor to create a '0' constant of arbitrary type...
93Constant *Constant::getNullValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +000094 switch (Ty->getTypeID()) {
Chris Lattnerdbcb0d32007-02-20 05:46:39 +000095 case Type::IntegerTyID:
96 return ConstantInt::get(Ty, 0);
97 case Type::FloatTyID:
98 case Type::DoubleTyID:
99 return ConstantFP::get(Ty, 0.0);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000100 case Type::PointerTyID:
Chris Lattnerb1585a92002-08-13 17:50:20 +0000101 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner9fba3da2004-02-15 05:53:04 +0000102 case Type::StructTyID:
103 case Type::ArrayTyID:
Reid Spencerd84d35b2007-02-15 02:26:10 +0000104 case Type::VectorTyID:
Chris Lattner9fba3da2004-02-15 05:53:04 +0000105 return ConstantAggregateZero::get(Ty);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000106 default:
Reid Spencercf394bf2004-07-04 11:51:24 +0000107 // Function, Label, or Opaque type?
108 assert(!"Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000109 return 0;
110 }
111}
112
Chris Lattnerb1585a92002-08-13 17:50:20 +0000113
114// Static constructor to create an integral constant with all bits set
Zhou Sheng75b871f2007-01-11 12:24:14 +0000115ConstantInt *ConstantInt::getAllOnesValue(const Type *Ty) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000116 if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty))
117 if (ITy->getBitWidth() == 1)
118 return ConstantInt::getTrue();
119 else
120 return ConstantInt::get(Ty, int64_t(-1));
121 return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000122}
123
Chris Lattnerecab54c2007-01-04 01:49:26 +0000124/// @returns the value for an packed integer constant of the given type that
125/// has all its bits set to true.
126/// @brief Get the all ones value
Reid Spencerd84d35b2007-02-15 02:26:10 +0000127ConstantVector *ConstantVector::getAllOnesValue(const VectorType *Ty) {
Chris Lattnerecab54c2007-01-04 01:49:26 +0000128 std::vector<Constant*> Elts;
129 Elts.resize(Ty->getNumElements(),
Zhou Sheng75b871f2007-01-11 12:24:14 +0000130 ConstantInt::getAllOnesValue(Ty->getElementType()));
Chris Lattnerecab54c2007-01-04 01:49:26 +0000131 assert(Elts[0] && "Not a packed integer type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +0000132 return cast<ConstantVector>(ConstantVector::get(Elts));
Chris Lattnerecab54c2007-01-04 01:49:26 +0000133}
134
135
Chris Lattner2f7c9632001-06-06 20:29:01 +0000136//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +0000137// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +0000138//===----------------------------------------------------------------------===//
139
140//===----------------------------------------------------------------------===//
141// Normal Constructors
142
Chris Lattner5db2f472007-02-20 05:55:46 +0000143ConstantInt::ConstantInt(const IntegerType *Ty, uint64_t V)
144 : Constant(Ty, ConstantIntVal, 0, 0), Val(V) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000145}
146
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000147ConstantFP::ConstantFP(const Type *Ty, double V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000148 : Constant(Ty, ConstantFPVal, 0, 0) {
Chris Lattner9655e542001-07-20 19:16:02 +0000149 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000150 Val = V;
151}
152
Chris Lattner3462ae32001-12-03 22:26:30 +0000153ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000154 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000155 : Constant(T, ConstantArrayVal, new Use[V.size()], V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000156 assert(V.size() == T->getNumElements() &&
157 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000158 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000159 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
160 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000161 Constant *C = *I;
162 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000163 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000164 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000165 "Initializer for array element doesn't match array element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000166 OL->init(C, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000167 }
168}
169
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000170ConstantArray::~ConstantArray() {
171 delete [] OperandList;
172}
173
Chris Lattner3462ae32001-12-03 22:26:30 +0000174ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000175 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000176 : Constant(T, ConstantStructVal, new Use[V.size()], V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000177 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000178 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000179 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000180 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
181 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000182 Constant *C = *I;
183 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000184 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000185 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000186 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000187 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000188 "Initializer for struct element doesn't match struct element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000189 OL->init(C, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000190 }
191}
192
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000193ConstantStruct::~ConstantStruct() {
194 delete [] OperandList;
195}
196
197
Reid Spencerd84d35b2007-02-15 02:26:10 +0000198ConstantVector::ConstantVector(const VectorType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000199 const std::vector<Constant*> &V)
Reid Spencerd84d35b2007-02-15 02:26:10 +0000200 : Constant(T, ConstantVectorVal, new Use[V.size()], V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000201 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000202 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
203 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000204 Constant *C = *I;
205 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000206 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000207 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000208 "Initializer for packed element doesn't match packed element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000209 OL->init(C, this);
Brian Gaeke02209042004-08-20 06:00:58 +0000210 }
211}
212
Reid Spencerd84d35b2007-02-15 02:26:10 +0000213ConstantVector::~ConstantVector() {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000214 delete [] OperandList;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000215}
216
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000217// We declare several classes private to this file, so use an anonymous
218// namespace
219namespace {
220
221/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
222/// behind the scenes to implement unary constant exprs.
223class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
224 Use Op;
225public:
226 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
227 : ConstantExpr(Ty, Opcode, &Op, 1), Op(C, this) {}
228};
229
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000230/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
231/// behind the scenes to implement binary constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000232class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000233 Use Ops[2];
234public:
235 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Reid Spencer266e42b2006-12-23 06:05:41 +0000236 : ConstantExpr(C1->getType(), Opcode, Ops, 2) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000237 Ops[0].init(C1, this);
238 Ops[1].init(C2, this);
239 }
240};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000241
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000242/// SelectConstantExpr - This class is private to Constants.cpp, and is used
243/// behind the scenes to implement select constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000244class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000245 Use Ops[3];
246public:
247 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
248 : ConstantExpr(C2->getType(), Instruction::Select, Ops, 3) {
249 Ops[0].init(C1, this);
250 Ops[1].init(C2, this);
251 Ops[2].init(C3, this);
252 }
253};
254
Robert Bocchinoca27f032006-01-17 20:07:22 +0000255/// ExtractElementConstantExpr - This class is private to
256/// Constants.cpp, and is used behind the scenes to implement
257/// extractelement constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000258class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Robert Bocchino23004482006-01-10 19:05:34 +0000259 Use Ops[2];
260public:
261 ExtractElementConstantExpr(Constant *C1, Constant *C2)
Reid Spencerd84d35b2007-02-15 02:26:10 +0000262 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +0000263 Instruction::ExtractElement, Ops, 2) {
264 Ops[0].init(C1, this);
265 Ops[1].init(C2, this);
266 }
267};
268
Robert Bocchinoca27f032006-01-17 20:07:22 +0000269/// InsertElementConstantExpr - This class is private to
270/// Constants.cpp, and is used behind the scenes to implement
271/// insertelement constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000272class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Robert Bocchinoca27f032006-01-17 20:07:22 +0000273 Use Ops[3];
274public:
275 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
276 : ConstantExpr(C1->getType(), Instruction::InsertElement,
277 Ops, 3) {
278 Ops[0].init(C1, this);
279 Ops[1].init(C2, this);
280 Ops[2].init(C3, this);
281 }
282};
283
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000284/// ShuffleVectorConstantExpr - This class is private to
285/// Constants.cpp, and is used behind the scenes to implement
286/// shufflevector constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000287class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000288 Use Ops[3];
289public:
290 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
291 : ConstantExpr(C1->getType(), Instruction::ShuffleVector,
292 Ops, 3) {
293 Ops[0].init(C1, this);
294 Ops[1].init(C2, this);
295 Ops[2].init(C3, this);
296 }
297};
298
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000299/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
300/// used behind the scenes to implement getelementpr constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000301struct VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000302 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
303 const Type *DestTy)
304 : ConstantExpr(DestTy, Instruction::GetElementPtr,
305 new Use[IdxList.size()+1], IdxList.size()+1) {
306 OperandList[0].init(C, this);
307 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
308 OperandList[i+1].init(IdxList[i], this);
309 }
310 ~GetElementPtrConstantExpr() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000311 delete [] OperandList;
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000312 }
313};
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000314
315// CompareConstantExpr - This class is private to Constants.cpp, and is used
316// behind the scenes to implement ICmp and FCmp constant expressions. This is
317// needed in order to store the predicate value for these instructions.
318struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr {
319 unsigned short predicate;
320 Use Ops[2];
321 CompareConstantExpr(Instruction::OtherOps opc, unsigned short pred,
322 Constant* LHS, Constant* RHS)
Reid Spencer542964f2007-01-11 18:21:29 +0000323 : ConstantExpr(Type::Int1Ty, opc, Ops, 2), predicate(pred) {
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000324 OperandList[0].init(LHS, this);
325 OperandList[1].init(RHS, this);
326 }
327};
328
329} // end anonymous namespace
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000330
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000331
332// Utility function for determining if a ConstantExpr is a CastOp or not. This
333// can't be inline because we don't want to #include Instruction.h into
334// Constant.h
335bool ConstantExpr::isCast() const {
336 return Instruction::isCast(getOpcode());
337}
338
Reid Spenceree3c9912006-12-04 05:19:50 +0000339bool ConstantExpr::isCompare() const {
340 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
341}
342
Chris Lattner817175f2004-03-29 02:37:53 +0000343/// ConstantExpr::get* - Return some common constants without having to
344/// specify the full Instruction::OPCODE identifier.
345///
346Constant *ConstantExpr::getNeg(Constant *C) {
Reid Spencer2eadb532007-01-21 00:29:26 +0000347 return get(Instruction::Sub,
348 ConstantExpr::getZeroValueForNegationExpr(C->getType()),
349 C);
Chris Lattner817175f2004-03-29 02:37:53 +0000350}
351Constant *ConstantExpr::getNot(Constant *C) {
Zhou Sheng75b871f2007-01-11 12:24:14 +0000352 assert(isa<ConstantInt>(C) && "Cannot NOT a nonintegral type!");
Chris Lattner817175f2004-03-29 02:37:53 +0000353 return get(Instruction::Xor, C,
Zhou Sheng75b871f2007-01-11 12:24:14 +0000354 ConstantInt::getAllOnesValue(C->getType()));
Chris Lattner817175f2004-03-29 02:37:53 +0000355}
356Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
357 return get(Instruction::Add, C1, C2);
358}
359Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
360 return get(Instruction::Sub, C1, C2);
361}
362Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
363 return get(Instruction::Mul, C1, C2);
364}
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000365Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2) {
366 return get(Instruction::UDiv, C1, C2);
367}
368Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2) {
369 return get(Instruction::SDiv, C1, C2);
370}
371Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) {
372 return get(Instruction::FDiv, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000373}
Reid Spencer7eb55b32006-11-02 01:53:59 +0000374Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) {
375 return get(Instruction::URem, C1, C2);
376}
377Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) {
378 return get(Instruction::SRem, C1, C2);
379}
380Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) {
381 return get(Instruction::FRem, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000382}
383Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
384 return get(Instruction::And, C1, C2);
385}
386Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
387 return get(Instruction::Or, C1, C2);
388}
389Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
390 return get(Instruction::Xor, C1, C2);
391}
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000392unsigned ConstantExpr::getPredicate() const {
393 assert(getOpcode() == Instruction::FCmp || getOpcode() == Instruction::ICmp);
394 return dynamic_cast<const CompareConstantExpr*>(this)->predicate;
395}
Chris Lattner817175f2004-03-29 02:37:53 +0000396Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
397 return get(Instruction::Shl, C1, C2);
398}
Reid Spencerfdff9382006-11-08 06:47:33 +0000399Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2) {
400 return get(Instruction::LShr, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000401}
Reid Spencerfdff9382006-11-08 06:47:33 +0000402Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2) {
403 return get(Instruction::AShr, C1, C2);
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000404}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000405
Chris Lattner7c1018a2006-07-14 19:37:40 +0000406/// getWithOperandReplaced - Return a constant expression identical to this
407/// one, but with the specified operand set to the specified value.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000408Constant *
409ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +0000410 assert(OpNo < getNumOperands() && "Operand num is out of range!");
411 assert(Op->getType() == getOperand(OpNo)->getType() &&
412 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000413 if (getOperand(OpNo) == Op)
414 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000415
Chris Lattner227816342006-07-14 22:20:01 +0000416 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000417 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000418 case Instruction::Trunc:
419 case Instruction::ZExt:
420 case Instruction::SExt:
421 case Instruction::FPTrunc:
422 case Instruction::FPExt:
423 case Instruction::UIToFP:
424 case Instruction::SIToFP:
425 case Instruction::FPToUI:
426 case Instruction::FPToSI:
427 case Instruction::PtrToInt:
428 case Instruction::IntToPtr:
429 case Instruction::BitCast:
430 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000431 case Instruction::Select:
432 Op0 = (OpNo == 0) ? Op : getOperand(0);
433 Op1 = (OpNo == 1) ? Op : getOperand(1);
434 Op2 = (OpNo == 2) ? Op : getOperand(2);
435 return ConstantExpr::getSelect(Op0, Op1, Op2);
436 case Instruction::InsertElement:
437 Op0 = (OpNo == 0) ? Op : getOperand(0);
438 Op1 = (OpNo == 1) ? Op : getOperand(1);
439 Op2 = (OpNo == 2) ? Op : getOperand(2);
440 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
441 case Instruction::ExtractElement:
442 Op0 = (OpNo == 0) ? Op : getOperand(0);
443 Op1 = (OpNo == 1) ? Op : getOperand(1);
444 return ConstantExpr::getExtractElement(Op0, Op1);
445 case Instruction::ShuffleVector:
446 Op0 = (OpNo == 0) ? Op : getOperand(0);
447 Op1 = (OpNo == 1) ? Op : getOperand(1);
448 Op2 = (OpNo == 2) ? Op : getOperand(2);
449 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000450 case Instruction::GetElementPtr: {
Chris Lattnerb5d70302007-02-19 20:01:23 +0000451 SmallVector<Constant*, 8> Ops;
452 Ops.resize(getNumOperands());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000453 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Chris Lattnerb5d70302007-02-19 20:01:23 +0000454 Ops[i] = getOperand(i);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000455 if (OpNo == 0)
Chris Lattnerb5d70302007-02-19 20:01:23 +0000456 return ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000457 Ops[OpNo-1] = Op;
Chris Lattnerb5d70302007-02-19 20:01:23 +0000458 return ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000459 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000460 default:
461 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000462 Op0 = (OpNo == 0) ? Op : getOperand(0);
463 Op1 = (OpNo == 1) ? Op : getOperand(1);
464 return ConstantExpr::get(getOpcode(), Op0, Op1);
465 }
466}
467
468/// getWithOperands - This returns the current constant expression with the
469/// operands replaced with the specified values. The specified operands must
470/// match count and type with the existing ones.
471Constant *ConstantExpr::
472getWithOperands(const std::vector<Constant*> &Ops) const {
473 assert(Ops.size() == getNumOperands() && "Operand count mismatch!");
474 bool AnyChange = false;
475 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
476 assert(Ops[i]->getType() == getOperand(i)->getType() &&
477 "Operand type mismatch!");
478 AnyChange |= Ops[i] != getOperand(i);
479 }
480 if (!AnyChange) // No operands changed, return self.
481 return const_cast<ConstantExpr*>(this);
482
483 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000484 case Instruction::Trunc:
485 case Instruction::ZExt:
486 case Instruction::SExt:
487 case Instruction::FPTrunc:
488 case Instruction::FPExt:
489 case Instruction::UIToFP:
490 case Instruction::SIToFP:
491 case Instruction::FPToUI:
492 case Instruction::FPToSI:
493 case Instruction::PtrToInt:
494 case Instruction::IntToPtr:
495 case Instruction::BitCast:
496 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattner227816342006-07-14 22:20:01 +0000497 case Instruction::Select:
498 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
499 case Instruction::InsertElement:
500 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
501 case Instruction::ExtractElement:
502 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
503 case Instruction::ShuffleVector:
504 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Chris Lattnerb5d70302007-02-19 20:01:23 +0000505 case Instruction::GetElementPtr:
506 return ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], Ops.size()-1);
Reid Spencer266e42b2006-12-23 06:05:41 +0000507 case Instruction::ICmp:
508 case Instruction::FCmp:
509 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattner227816342006-07-14 22:20:01 +0000510 default:
511 assert(getNumOperands() == 2 && "Must be binary operator?");
512 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000513 }
514}
515
Chris Lattner2f7c9632001-06-06 20:29:01 +0000516
517//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000518// isValueValidForType implementations
519
Reid Spencere7334722006-12-19 01:28:19 +0000520bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000521 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000522 if (Ty == Type::Int1Ty)
523 return Val == 0 || Val == 1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000524 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000525 return true; // always true, has to fit in largest type
526 uint64_t Max = (1ll << NumBits) - 1;
527 return Val <= Max;
Reid Spencere7334722006-12-19 01:28:19 +0000528}
529
Reid Spencere0fc4df2006-10-20 07:07:24 +0000530bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000531 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000532 if (Ty == Type::Int1Ty)
Reid Spencera94d3942007-01-19 21:13:56 +0000533 return Val == 0 || Val == 1 || Val == -1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000534 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000535 return true; // always true, has to fit in largest type
536 int64_t Min = -(1ll << (NumBits-1));
537 int64_t Max = (1ll << (NumBits-1)) - 1;
538 return (Val >= Min && Val <= Max);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000539}
540
Chris Lattner3462ae32001-12-03 22:26:30 +0000541bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000542 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000543 default:
544 return false; // These can't be represented as floating point!
545
Reid Spencerb95f8ab2004-12-07 07:38:08 +0000546 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000547 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000548 case Type::DoubleTyID:
549 return true; // This is the largest type...
550 }
Chris Lattneraa2372562006-05-24 17:04:05 +0000551}
Chris Lattner9655e542001-07-20 19:16:02 +0000552
Chris Lattner49d855c2001-09-07 16:46:31 +0000553//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000554// Factory Function Implementation
555
Chris Lattner98fa07b2003-05-23 20:03:32 +0000556// ConstantCreator - A class that is used to create constants by
557// ValueMap*. This class should be partially specialized if there is
558// something strange that needs to be done to interface to the ctor for the
559// constant.
560//
Chris Lattner189d19f2003-11-21 20:23:48 +0000561namespace llvm {
562 template<class ConstantClass, class TypeClass, class ValType>
Chris Lattner02157b02006-06-28 21:38:54 +0000563 struct VISIBILITY_HIDDEN ConstantCreator {
Chris Lattner189d19f2003-11-21 20:23:48 +0000564 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
565 return new ConstantClass(Ty, V);
566 }
567 };
Misha Brukmanb1c93172005-04-21 23:48:37 +0000568
Chris Lattner189d19f2003-11-21 20:23:48 +0000569 template<class ConstantClass, class TypeClass>
Chris Lattner02157b02006-06-28 21:38:54 +0000570 struct VISIBILITY_HIDDEN ConvertConstantType {
Chris Lattner189d19f2003-11-21 20:23:48 +0000571 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
572 assert(0 && "This type cannot be converted!\n");
573 abort();
574 }
575 };
Chris Lattnerb50d1352003-10-05 00:17:43 +0000576
Chris Lattner935aa922005-10-04 17:48:46 +0000577 template<class ValType, class TypeClass, class ConstantClass,
578 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattner02157b02006-06-28 21:38:54 +0000579 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
Chris Lattnerb64419a2005-10-03 22:51:37 +0000580 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000581 typedef std::pair<const Type*, ValType> MapKey;
582 typedef std::map<MapKey, Constant *> MapTy;
583 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
584 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
Chris Lattnerb64419a2005-10-03 22:51:37 +0000585 private:
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000586 /// Map - This is the main map from the element descriptor to the Constants.
587 /// This is the primary way we avoid creating two of the same shape
588 /// constant.
Chris Lattnerb50d1352003-10-05 00:17:43 +0000589 MapTy Map;
Chris Lattner935aa922005-10-04 17:48:46 +0000590
591 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
592 /// from the constants to their element in Map. This is important for
593 /// removal of constants from the array, which would otherwise have to scan
594 /// through the map with very large keys.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000595 InverseMapTy InverseMap;
Chris Lattnerb50d1352003-10-05 00:17:43 +0000596
Jim Laskeyc03caef2006-07-17 17:38:29 +0000597 /// AbstractTypeMap - Map for abstract type constants.
598 ///
Chris Lattnerb50d1352003-10-05 00:17:43 +0000599 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner99a669b2004-11-19 16:39:44 +0000600
Chris Lattner99a669b2004-11-19 16:39:44 +0000601 private:
602 void clear(std::vector<Constant *> &Constants) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000603 for(typename MapTy::iterator I = Map.begin(); I != Map.end(); ++I)
Chris Lattner99a669b2004-11-19 16:39:44 +0000604 Constants.push_back(I->second);
605 Map.clear();
606 AbstractTypeMap.clear();
Chris Lattner935aa922005-10-04 17:48:46 +0000607 InverseMap.clear();
Chris Lattner99a669b2004-11-19 16:39:44 +0000608 }
609
Chris Lattner98fa07b2003-05-23 20:03:32 +0000610 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000611 typename MapTy::iterator map_end() { return Map.end(); }
Chris Lattnerb64419a2005-10-03 22:51:37 +0000612
613 /// InsertOrGetItem - Return an iterator for the specified element.
614 /// If the element exists in the map, the returned iterator points to the
615 /// entry and Exists=true. If not, the iterator points to the newly
616 /// inserted entry and returns Exists=false. Newly inserted entries have
617 /// I->second == 0, and should be filled in.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000618 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
619 &InsertVal,
Chris Lattnerb64419a2005-10-03 22:51:37 +0000620 bool &Exists) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000621 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
Chris Lattnerb64419a2005-10-03 22:51:37 +0000622 Exists = !IP.second;
623 return IP.first;
624 }
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000625
Chris Lattner935aa922005-10-04 17:48:46 +0000626private:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000627 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
Chris Lattner935aa922005-10-04 17:48:46 +0000628 if (HasLargeKey) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000629 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
Chris Lattner935aa922005-10-04 17:48:46 +0000630 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
631 IMI->second->second == CP &&
632 "InverseMap corrupt!");
633 return IMI->second;
634 }
635
Jim Laskeyc03caef2006-07-17 17:38:29 +0000636 typename MapTy::iterator I =
Chris Lattner935aa922005-10-04 17:48:46 +0000637 Map.find(MapKey((TypeClass*)CP->getRawType(), getValType(CP)));
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000638 if (I == Map.end() || I->second != CP) {
639 // FIXME: This should not use a linear scan. If this gets to be a
640 // performance problem, someone should look at this.
641 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
642 /* empty */;
643 }
Chris Lattner935aa922005-10-04 17:48:46 +0000644 return I;
645 }
646public:
647
Chris Lattnerb64419a2005-10-03 22:51:37 +0000648 /// getOrCreate - Return the specified constant from the map, creating it if
649 /// necessary.
Chris Lattner98fa07b2003-05-23 20:03:32 +0000650 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000651 MapKey Lookup(Ty, V);
Jim Laskeyc03caef2006-07-17 17:38:29 +0000652 typename MapTy::iterator I = Map.lower_bound(Lookup);
Reid Spencere0fc4df2006-10-20 07:07:24 +0000653 // Is it in the map?
Chris Lattner98fa07b2003-05-23 20:03:32 +0000654 if (I != Map.end() && I->first == Lookup)
Reid Spencere0fc4df2006-10-20 07:07:24 +0000655 return static_cast<ConstantClass *>(I->second);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000656
657 // If no preexisting value, create one now...
658 ConstantClass *Result =
659 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
660
Chris Lattnerb50d1352003-10-05 00:17:43 +0000661 /// FIXME: why does this assert fail when loading 176.gcc?
662 //assert(Result->getType() == Ty && "Type specified is not correct!");
663 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
664
Chris Lattner935aa922005-10-04 17:48:46 +0000665 if (HasLargeKey) // Remember the reverse mapping if needed.
666 InverseMap.insert(std::make_pair(Result, I));
667
Chris Lattnerb50d1352003-10-05 00:17:43 +0000668 // If the type of the constant is abstract, make sure that an entry exists
669 // for it in the AbstractTypeMap.
670 if (Ty->isAbstract()) {
671 typename AbstractTypeMapTy::iterator TI =
672 AbstractTypeMap.lower_bound(Ty);
673
674 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
675 // Add ourselves to the ATU list of the type.
676 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
677
678 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
679 }
680 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000681 return Result;
682 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000683
Chris Lattner98fa07b2003-05-23 20:03:32 +0000684 void remove(ConstantClass *CP) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000685 typename MapTy::iterator I = FindExistingElement(CP);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000686 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +0000687 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +0000688
Chris Lattner935aa922005-10-04 17:48:46 +0000689 if (HasLargeKey) // Remember the reverse mapping if needed.
690 InverseMap.erase(CP);
691
Chris Lattnerb50d1352003-10-05 00:17:43 +0000692 // Now that we found the entry, make sure this isn't the entry that
693 // the AbstractTypeMap points to.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000694 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000695 if (Ty->isAbstract()) {
696 assert(AbstractTypeMap.count(Ty) &&
697 "Abstract type not in AbstractTypeMap?");
Jim Laskeyc03caef2006-07-17 17:38:29 +0000698 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
Chris Lattnerb50d1352003-10-05 00:17:43 +0000699 if (ATMEntryIt == I) {
700 // Yes, we are removing the representative entry for this type.
701 // See if there are any other entries of the same type.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000702 typename MapTy::iterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000703
Chris Lattnerb50d1352003-10-05 00:17:43 +0000704 // First check the entry before this one...
705 if (TmpIt != Map.begin()) {
706 --TmpIt;
707 if (TmpIt->first.first != Ty) // Not the same type, move back...
708 ++TmpIt;
709 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000710
Chris Lattnerb50d1352003-10-05 00:17:43 +0000711 // If we didn't find the same type, try to move forward...
712 if (TmpIt == ATMEntryIt) {
713 ++TmpIt;
714 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
715 --TmpIt; // No entry afterwards with the same type
716 }
717
718 // If there is another entry in the map of the same abstract type,
719 // update the AbstractTypeMap entry now.
720 if (TmpIt != ATMEntryIt) {
721 ATMEntryIt = TmpIt;
722 } else {
723 // Otherwise, we are removing the last instance of this type
724 // from the table. Remove from the ATM, and from user list.
725 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
726 AbstractTypeMap.erase(Ty);
727 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000728 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000729 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000730
Chris Lattnerb50d1352003-10-05 00:17:43 +0000731 Map.erase(I);
732 }
733
Chris Lattner3b793c62005-10-04 21:35:50 +0000734
735 /// MoveConstantToNewSlot - If we are about to change C to be the element
736 /// specified by I, update our internal data structures to reflect this
737 /// fact.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000738 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
Chris Lattner3b793c62005-10-04 21:35:50 +0000739 // First, remove the old location of the specified constant in the map.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000740 typename MapTy::iterator OldI = FindExistingElement(C);
Chris Lattner3b793c62005-10-04 21:35:50 +0000741 assert(OldI != Map.end() && "Constant not found in constant table!");
742 assert(OldI->second == C && "Didn't find correct element?");
743
744 // If this constant is the representative element for its abstract type,
745 // update the AbstractTypeMap so that the representative element is I.
746 if (C->getType()->isAbstract()) {
747 typename AbstractTypeMapTy::iterator ATI =
748 AbstractTypeMap.find(C->getType());
749 assert(ATI != AbstractTypeMap.end() &&
750 "Abstract type not in AbstractTypeMap?");
751 if (ATI->second == OldI)
752 ATI->second = I;
753 }
754
755 // Remove the old entry from the map.
756 Map.erase(OldI);
757
758 // Update the inverse map so that we know that this constant is now
759 // located at descriptor I.
760 if (HasLargeKey) {
761 assert(I->second == C && "Bad inversemap entry!");
762 InverseMap[C] = I;
763 }
764 }
765
Chris Lattnerb50d1352003-10-05 00:17:43 +0000766 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000767 typename AbstractTypeMapTy::iterator I =
Jim Laskeyc03caef2006-07-17 17:38:29 +0000768 AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +0000769
770 assert(I != AbstractTypeMap.end() &&
771 "Abstract type not in AbstractTypeMap?");
772
773 // Convert a constant at a time until the last one is gone. The last one
774 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
775 // eliminated eventually.
776 do {
777 ConvertConstantType<ConstantClass,
Jim Laskeyc03caef2006-07-17 17:38:29 +0000778 TypeClass>::convert(
779 static_cast<ConstantClass *>(I->second->second),
Chris Lattnerb50d1352003-10-05 00:17:43 +0000780 cast<TypeClass>(NewTy));
781
Jim Laskeyc03caef2006-07-17 17:38:29 +0000782 I = AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +0000783 } while (I != AbstractTypeMap.end());
784 }
785
786 // If the type became concrete without being refined to any other existing
787 // type, we just remove ourselves from the ATU list.
788 void typeBecameConcrete(const DerivedType *AbsTy) {
789 AbsTy->removeAbstractTypeUser(this);
790 }
791
792 void dump() const {
Bill Wendling6a462f12006-11-17 08:03:48 +0000793 DOUT << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +0000794 }
795 };
796}
797
Chris Lattnera84df0a22006-09-28 23:36:21 +0000798
Reid Spencere0fc4df2006-10-20 07:07:24 +0000799//---- ConstantInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000800//
Chris Lattner5db2f472007-02-20 05:55:46 +0000801static ManagedStatic<ValueMap<uint64_t, IntegerType, ConstantInt> >IntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000802
Chris Lattner28173502007-02-20 06:11:36 +0000803
Reid Spencere0fc4df2006-10-20 07:07:24 +0000804// Get a ConstantInt from an int64_t. Note here that we canoncialize the value
805// to a uint64_t value that has been zero extended down to the size of the
806// integer type of the ConstantInt. This allows the getZExtValue method to
807// just return the stored value while getSExtValue has to convert back to sign
808// extended. getZExtValue is more common in LLVM than getSExtValue().
809ConstantInt *ConstantInt::get(const Type *Ty, int64_t V) {
Chris Lattner5db2f472007-02-20 05:55:46 +0000810 const IntegerType *ITy = cast<IntegerType>(Ty);
Chris Lattner5db2f472007-02-20 05:55:46 +0000811 return IntConstants->getOrCreate(ITy, V & ITy->getBitMask());
Chris Lattner49d855c2001-09-07 16:46:31 +0000812}
813
Chris Lattner28173502007-02-20 06:11:36 +0000814ConstantInt *ConstantInt::TheTrueVal = 0;
815ConstantInt *ConstantInt::TheFalseVal = 0;
816
817void CleanupTrueFalse(void *) {
818 ConstantInt::ResetTrueFalse();
819}
820
821static ManagedCleanup<CleanupTrueFalse> TrueFalseCleanup;
822
823ConstantInt *ConstantInt::CreateTrueFalseVals(bool WhichOne) {
824 assert(TheTrueVal == 0 && TheFalseVal == 0);
825 TheTrueVal = get(Type::Int1Ty, 1);
826 TheFalseVal = get(Type::Int1Ty, 0);
827
828 // Ensure that llvm_shutdown nulls out TheTrueVal/TheFalseVal.
829 TrueFalseCleanup.Register();
830
831 return WhichOne ? TheTrueVal : TheFalseVal;
832}
833
834
835
836
Chris Lattner3462ae32001-12-03 22:26:30 +0000837//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000838//
Chris Lattnerac80ea42004-02-01 22:49:04 +0000839namespace llvm {
840 template<>
841 struct ConstantCreator<ConstantFP, Type, uint64_t> {
842 static ConstantFP *create(const Type *Ty, uint64_t V) {
843 assert(Ty == Type::DoubleTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000844 return new ConstantFP(Ty, BitsToDouble(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000845 }
846 };
847 template<>
848 struct ConstantCreator<ConstantFP, Type, uint32_t> {
849 static ConstantFP *create(const Type *Ty, uint32_t V) {
850 assert(Ty == Type::FloatTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000851 return new ConstantFP(Ty, BitsToFloat(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000852 }
853 };
854}
855
Chris Lattner69edc982006-09-28 00:35:06 +0000856static ManagedStatic<ValueMap<uint64_t, Type, ConstantFP> > DoubleConstants;
857static ManagedStatic<ValueMap<uint32_t, Type, ConstantFP> > FloatConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000858
Jim Laskey8ad8f712005-08-17 20:06:22 +0000859bool ConstantFP::isNullValue() const {
860 return DoubleToBits(Val) == 0;
861}
862
863bool ConstantFP::isExactlyValue(double V) const {
864 return DoubleToBits(V) == DoubleToBits(Val);
865}
866
867
Chris Lattner3462ae32001-12-03 22:26:30 +0000868ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner241ed4c2004-01-23 00:55:21 +0000869 if (Ty == Type::FloatTy) {
870 // Force the value through memory to normalize it.
Chris Lattner69edc982006-09-28 00:35:06 +0000871 return FloatConstants->getOrCreate(Ty, FloatToBits(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000872 } else {
873 assert(Ty == Type::DoubleTy);
Chris Lattner69edc982006-09-28 00:35:06 +0000874 return DoubleConstants->getOrCreate(Ty, DoubleToBits(V));
Chris Lattner241ed4c2004-01-23 00:55:21 +0000875 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000876}
877
Chris Lattner9fba3da2004-02-15 05:53:04 +0000878//---- ConstantAggregateZero::get() implementation...
879//
880namespace llvm {
881 // ConstantAggregateZero does not take extra "value" argument...
882 template<class ValType>
883 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
884 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
885 return new ConstantAggregateZero(Ty);
886 }
887 };
888
889 template<>
890 struct ConvertConstantType<ConstantAggregateZero, Type> {
891 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
892 // Make everyone now use a constant of the new type...
893 Constant *New = ConstantAggregateZero::get(NewTy);
894 assert(New != OldC && "Didn't replace constant??");
895 OldC->uncheckedReplaceAllUsesWith(New);
896 OldC->destroyConstant(); // This constant is now dead, destroy it.
897 }
898 };
899}
900
Chris Lattner69edc982006-09-28 00:35:06 +0000901static ManagedStatic<ValueMap<char, Type,
902 ConstantAggregateZero> > AggZeroConstants;
Chris Lattner9fba3da2004-02-15 05:53:04 +0000903
Chris Lattner3e650af2004-08-04 04:48:01 +0000904static char getValType(ConstantAggregateZero *CPZ) { return 0; }
905
Chris Lattner9fba3da2004-02-15 05:53:04 +0000906Constant *ConstantAggregateZero::get(const Type *Ty) {
Reid Spencerd84d35b2007-02-15 02:26:10 +0000907 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) &&
Chris Lattnerbfd0b6d2006-06-10 04:16:23 +0000908 "Cannot create an aggregate zero of non-aggregate type!");
Chris Lattner69edc982006-09-28 00:35:06 +0000909 return AggZeroConstants->getOrCreate(Ty, 0);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000910}
911
912// destroyConstant - Remove the constant from the constant table...
913//
914void ConstantAggregateZero::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +0000915 AggZeroConstants->remove(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000916 destroyConstantImpl();
917}
918
Chris Lattner3462ae32001-12-03 22:26:30 +0000919//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000920//
Chris Lattner189d19f2003-11-21 20:23:48 +0000921namespace llvm {
922 template<>
923 struct ConvertConstantType<ConstantArray, ArrayType> {
924 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
925 // Make everyone now use a constant of the new type...
926 std::vector<Constant*> C;
927 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
928 C.push_back(cast<Constant>(OldC->getOperand(i)));
929 Constant *New = ConstantArray::get(NewTy, C);
930 assert(New != OldC && "Didn't replace constant??");
931 OldC->uncheckedReplaceAllUsesWith(New);
932 OldC->destroyConstant(); // This constant is now dead, destroy it.
933 }
934 };
935}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000936
Chris Lattner3e650af2004-08-04 04:48:01 +0000937static std::vector<Constant*> getValType(ConstantArray *CA) {
938 std::vector<Constant*> Elements;
939 Elements.reserve(CA->getNumOperands());
940 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
941 Elements.push_back(cast<Constant>(CA->getOperand(i)));
942 return Elements;
943}
944
Chris Lattnerb64419a2005-10-03 22:51:37 +0000945typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattner935aa922005-10-04 17:48:46 +0000946 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +0000947static ManagedStatic<ArrayConstantsTy> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000948
Chris Lattner015e8212004-02-15 04:14:47 +0000949Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +0000950 const std::vector<Constant*> &V) {
951 // If this is an all-zero array, return a ConstantAggregateZero object
952 if (!V.empty()) {
953 Constant *C = V[0];
954 if (!C->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +0000955 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000956 for (unsigned i = 1, e = V.size(); i != e; ++i)
957 if (V[i] != C)
Chris Lattner69edc982006-09-28 00:35:06 +0000958 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000959 }
960 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000961}
962
Chris Lattner98fa07b2003-05-23 20:03:32 +0000963// destroyConstant - Remove the constant from the constant table...
964//
965void ConstantArray::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +0000966 ArrayConstants->remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000967 destroyConstantImpl();
968}
969
Reid Spencer6f614532006-05-30 08:23:18 +0000970/// ConstantArray::get(const string&) - Return an array that is initialized to
971/// contain the specified string. If length is zero then a null terminator is
972/// added to the specified string so that it may be used in a natural way.
973/// Otherwise, the length parameter specifies how much of the string to use
974/// and it won't be null terminated.
975///
Reid Spencer82ebaba2006-05-30 18:15:07 +0000976Constant *ConstantArray::get(const std::string &Str, bool AddNull) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000977 std::vector<Constant*> ElementVals;
Reid Spencer82ebaba2006-05-30 18:15:07 +0000978 for (unsigned i = 0; i < Str.length(); ++i)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000979 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000980
981 // Add a null terminator to the string...
Reid Spencer82ebaba2006-05-30 18:15:07 +0000982 if (AddNull) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000983 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, 0));
Reid Spencer6f614532006-05-30 08:23:18 +0000984 }
Chris Lattner8f80fe02001-10-14 23:54:12 +0000985
Reid Spencer8d9336d2006-12-31 05:26:44 +0000986 ArrayType *ATy = ArrayType::get(Type::Int8Ty, ElementVals.size());
Chris Lattner3462ae32001-12-03 22:26:30 +0000987 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000988}
989
Reid Spencer2546b762007-01-26 07:37:34 +0000990/// isString - This method returns true if the array is an array of i8, and
991/// if the elements of the array are all ConstantInt's.
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000992bool ConstantArray::isString() const {
Reid Spencer2546b762007-01-26 07:37:34 +0000993 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +0000994 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000995 return false;
996 // Check the elements to make sure they are all integers, not constant
997 // expressions.
998 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
999 if (!isa<ConstantInt>(getOperand(i)))
1000 return false;
1001 return true;
1002}
1003
Evan Cheng3763c5b2006-10-26 19:15:05 +00001004/// isCString - This method returns true if the array is a string (see
1005/// isString) and it ends in a null byte \0 and does not contains any other
1006/// null bytes except its terminator.
1007bool ConstantArray::isCString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001008 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001009 if (getType()->getElementType() != Type::Int8Ty)
Evan Chenge974da62006-10-26 21:48:03 +00001010 return false;
1011 Constant *Zero = Constant::getNullValue(getOperand(0)->getType());
1012 // Last element must be a null.
1013 if (getOperand(getNumOperands()-1) != Zero)
1014 return false;
1015 // Other elements must be non-null integers.
1016 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1017 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +00001018 return false;
Evan Chenge974da62006-10-26 21:48:03 +00001019 if (getOperand(i) == Zero)
1020 return false;
1021 }
Evan Cheng3763c5b2006-10-26 19:15:05 +00001022 return true;
1023}
1024
1025
Reid Spencer2546b762007-01-26 07:37:34 +00001026// getAsString - If the sub-element type of this array is i8
Chris Lattner81fabb02002-08-26 17:53:56 +00001027// then this method converts the array to an std::string and returns it.
1028// Otherwise, it asserts out.
1029//
1030std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001031 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001032 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +00001033 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Reid Spencere0fc4df2006-10-20 07:07:24 +00001034 Result += (char)cast<ConstantInt>(getOperand(i))->getZExtValue();
Chris Lattner81fabb02002-08-26 17:53:56 +00001035 return Result;
1036}
1037
1038
Chris Lattner3462ae32001-12-03 22:26:30 +00001039//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001040//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001041
Chris Lattner189d19f2003-11-21 20:23:48 +00001042namespace llvm {
1043 template<>
1044 struct ConvertConstantType<ConstantStruct, StructType> {
1045 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1046 // Make everyone now use a constant of the new type...
1047 std::vector<Constant*> C;
1048 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1049 C.push_back(cast<Constant>(OldC->getOperand(i)));
1050 Constant *New = ConstantStruct::get(NewTy, C);
1051 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001052
Chris Lattner189d19f2003-11-21 20:23:48 +00001053 OldC->uncheckedReplaceAllUsesWith(New);
1054 OldC->destroyConstant(); // This constant is now dead, destroy it.
1055 }
1056 };
1057}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001058
Chris Lattner8760ec72005-10-04 01:17:50 +00001059typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattner935aa922005-10-04 17:48:46 +00001060 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001061static ManagedStatic<StructConstantsTy> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001062
Chris Lattner3e650af2004-08-04 04:48:01 +00001063static std::vector<Constant*> getValType(ConstantStruct *CS) {
1064 std::vector<Constant*> Elements;
1065 Elements.reserve(CS->getNumOperands());
1066 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1067 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1068 return Elements;
1069}
1070
Chris Lattner015e8212004-02-15 04:14:47 +00001071Constant *ConstantStruct::get(const StructType *Ty,
1072 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001073 // Create a ConstantAggregateZero value if all elements are zeros...
1074 for (unsigned i = 0, e = V.size(); i != e; ++i)
1075 if (!V[i]->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001076 return StructConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001077
1078 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001079}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001080
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001081Constant *ConstantStruct::get(const std::vector<Constant*> &V, bool packed) {
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001082 std::vector<const Type*> StructEls;
1083 StructEls.reserve(V.size());
1084 for (unsigned i = 0, e = V.size(); i != e; ++i)
1085 StructEls.push_back(V[i]->getType());
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001086 return get(StructType::get(StructEls, packed), V);
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001087}
1088
Chris Lattnerd7a73302001-10-13 06:57:33 +00001089// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001090//
Chris Lattner3462ae32001-12-03 22:26:30 +00001091void ConstantStruct::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001092 StructConstants->remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001093 destroyConstantImpl();
1094}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001095
Reid Spencerd84d35b2007-02-15 02:26:10 +00001096//---- ConstantVector::get() implementation...
Brian Gaeke02209042004-08-20 06:00:58 +00001097//
1098namespace llvm {
1099 template<>
Reid Spencerd84d35b2007-02-15 02:26:10 +00001100 struct ConvertConstantType<ConstantVector, VectorType> {
1101 static void convert(ConstantVector *OldC, const VectorType *NewTy) {
Brian Gaeke02209042004-08-20 06:00:58 +00001102 // Make everyone now use a constant of the new type...
1103 std::vector<Constant*> C;
1104 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1105 C.push_back(cast<Constant>(OldC->getOperand(i)));
Reid Spencerd84d35b2007-02-15 02:26:10 +00001106 Constant *New = ConstantVector::get(NewTy, C);
Brian Gaeke02209042004-08-20 06:00:58 +00001107 assert(New != OldC && "Didn't replace constant??");
1108 OldC->uncheckedReplaceAllUsesWith(New);
1109 OldC->destroyConstant(); // This constant is now dead, destroy it.
1110 }
1111 };
1112}
1113
Reid Spencerd84d35b2007-02-15 02:26:10 +00001114static std::vector<Constant*> getValType(ConstantVector *CP) {
Brian Gaeke02209042004-08-20 06:00:58 +00001115 std::vector<Constant*> Elements;
1116 Elements.reserve(CP->getNumOperands());
1117 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1118 Elements.push_back(CP->getOperand(i));
1119 return Elements;
1120}
1121
Reid Spencerd84d35b2007-02-15 02:26:10 +00001122static ManagedStatic<ValueMap<std::vector<Constant*>, VectorType,
Reid Spencer09575ba2007-02-15 03:39:18 +00001123 ConstantVector> > VectorConstants;
Brian Gaeke02209042004-08-20 06:00:58 +00001124
Reid Spencerd84d35b2007-02-15 02:26:10 +00001125Constant *ConstantVector::get(const VectorType *Ty,
Brian Gaeke02209042004-08-20 06:00:58 +00001126 const std::vector<Constant*> &V) {
1127 // If this is an all-zero packed, return a ConstantAggregateZero object
1128 if (!V.empty()) {
1129 Constant *C = V[0];
1130 if (!C->isNullValue())
Reid Spencer09575ba2007-02-15 03:39:18 +00001131 return VectorConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001132 for (unsigned i = 1, e = V.size(); i != e; ++i)
1133 if (V[i] != C)
Reid Spencer09575ba2007-02-15 03:39:18 +00001134 return VectorConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001135 }
1136 return ConstantAggregateZero::get(Ty);
1137}
1138
Reid Spencerd84d35b2007-02-15 02:26:10 +00001139Constant *ConstantVector::get(const std::vector<Constant*> &V) {
Brian Gaeke02209042004-08-20 06:00:58 +00001140 assert(!V.empty() && "Cannot infer type if V is empty");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001141 return get(VectorType::get(V.front()->getType(),V.size()), V);
Brian Gaeke02209042004-08-20 06:00:58 +00001142}
1143
1144// destroyConstant - Remove the constant from the constant table...
1145//
Reid Spencerd84d35b2007-02-15 02:26:10 +00001146void ConstantVector::destroyConstant() {
Reid Spencer09575ba2007-02-15 03:39:18 +00001147 VectorConstants->remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001148 destroyConstantImpl();
1149}
1150
Jim Laskeyf0478822007-01-12 22:39:14 +00001151/// This function will return true iff every element in this packed constant
1152/// is set to all ones.
1153/// @returns true iff this constant's emements are all set to all ones.
1154/// @brief Determine if the value is all ones.
Reid Spencerd84d35b2007-02-15 02:26:10 +00001155bool ConstantVector::isAllOnesValue() const {
Jim Laskeyf0478822007-01-12 22:39:14 +00001156 // Check out first element.
1157 const Constant *Elt = getOperand(0);
1158 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1159 if (!CI || !CI->isAllOnesValue()) return false;
1160 // Then make sure all remaining elements point to the same value.
1161 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1162 if (getOperand(I) != Elt) return false;
1163 }
1164 return true;
1165}
1166
Chris Lattner3462ae32001-12-03 22:26:30 +00001167//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001168//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001169
Chris Lattner189d19f2003-11-21 20:23:48 +00001170namespace llvm {
1171 // ConstantPointerNull does not take extra "value" argument...
1172 template<class ValType>
1173 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1174 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1175 return new ConstantPointerNull(Ty);
1176 }
1177 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001178
Chris Lattner189d19f2003-11-21 20:23:48 +00001179 template<>
1180 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1181 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1182 // Make everyone now use a constant of the new type...
1183 Constant *New = ConstantPointerNull::get(NewTy);
1184 assert(New != OldC && "Didn't replace constant??");
1185 OldC->uncheckedReplaceAllUsesWith(New);
1186 OldC->destroyConstant(); // This constant is now dead, destroy it.
1187 }
1188 };
1189}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001190
Chris Lattner69edc982006-09-28 00:35:06 +00001191static ManagedStatic<ValueMap<char, PointerType,
1192 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001193
Chris Lattner3e650af2004-08-04 04:48:01 +00001194static char getValType(ConstantPointerNull *) {
1195 return 0;
1196}
1197
1198
Chris Lattner3462ae32001-12-03 22:26:30 +00001199ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001200 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001201}
1202
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001203// destroyConstant - Remove the constant from the constant table...
1204//
1205void ConstantPointerNull::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001206 NullPtrConstants->remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001207 destroyConstantImpl();
1208}
1209
1210
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001211//---- UndefValue::get() implementation...
1212//
1213
1214namespace llvm {
1215 // UndefValue does not take extra "value" argument...
1216 template<class ValType>
1217 struct ConstantCreator<UndefValue, Type, ValType> {
1218 static UndefValue *create(const Type *Ty, const ValType &V) {
1219 return new UndefValue(Ty);
1220 }
1221 };
1222
1223 template<>
1224 struct ConvertConstantType<UndefValue, Type> {
1225 static void convert(UndefValue *OldC, const Type *NewTy) {
1226 // Make everyone now use a constant of the new type.
1227 Constant *New = UndefValue::get(NewTy);
1228 assert(New != OldC && "Didn't replace constant??");
1229 OldC->uncheckedReplaceAllUsesWith(New);
1230 OldC->destroyConstant(); // This constant is now dead, destroy it.
1231 }
1232 };
1233}
1234
Chris Lattner69edc982006-09-28 00:35:06 +00001235static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001236
1237static char getValType(UndefValue *) {
1238 return 0;
1239}
1240
1241
1242UndefValue *UndefValue::get(const Type *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001243 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001244}
1245
1246// destroyConstant - Remove the constant from the constant table.
1247//
1248void UndefValue::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001249 UndefValueConstants->remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001250 destroyConstantImpl();
1251}
1252
1253
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001254//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001255//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001256
Reid Spenceree3c9912006-12-04 05:19:50 +00001257struct ExprMapKeyType {
1258 explicit ExprMapKeyType(unsigned opc, std::vector<Constant*> ops,
Reid Spencerdba6aa42006-12-04 18:38:05 +00001259 unsigned short pred = 0) : opcode(opc), predicate(pred), operands(ops) { }
1260 uint16_t opcode;
1261 uint16_t predicate;
Reid Spenceree3c9912006-12-04 05:19:50 +00001262 std::vector<Constant*> operands;
Reid Spenceree3c9912006-12-04 05:19:50 +00001263 bool operator==(const ExprMapKeyType& that) const {
1264 return this->opcode == that.opcode &&
1265 this->predicate == that.predicate &&
1266 this->operands == that.operands;
1267 }
1268 bool operator<(const ExprMapKeyType & that) const {
1269 return this->opcode < that.opcode ||
1270 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1271 (this->opcode == that.opcode && this->predicate == that.predicate &&
1272 this->operands < that.operands);
1273 }
1274
1275 bool operator!=(const ExprMapKeyType& that) const {
1276 return !(*this == that);
1277 }
1278};
Chris Lattner98fa07b2003-05-23 20:03:32 +00001279
Chris Lattner189d19f2003-11-21 20:23:48 +00001280namespace llvm {
1281 template<>
1282 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001283 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1284 unsigned short pred = 0) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001285 if (Instruction::isCast(V.opcode))
1286 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1287 if ((V.opcode >= Instruction::BinaryOpsBegin &&
Reid Spencer2341c222007-02-02 02:16:23 +00001288 V.opcode < Instruction::BinaryOpsEnd))
Reid Spenceree3c9912006-12-04 05:19:50 +00001289 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1290 if (V.opcode == Instruction::Select)
1291 return new SelectConstantExpr(V.operands[0], V.operands[1],
1292 V.operands[2]);
1293 if (V.opcode == Instruction::ExtractElement)
1294 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1295 if (V.opcode == Instruction::InsertElement)
1296 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1297 V.operands[2]);
1298 if (V.opcode == Instruction::ShuffleVector)
1299 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1300 V.operands[2]);
1301 if (V.opcode == Instruction::GetElementPtr) {
1302 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
1303 return new GetElementPtrConstantExpr(V.operands[0], IdxList, Ty);
1304 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001305
Reid Spenceree3c9912006-12-04 05:19:50 +00001306 // The compare instructions are weird. We have to encode the predicate
1307 // value and it is combined with the instruction opcode by multiplying
1308 // the opcode by one hundred. We must decode this to get the predicate.
1309 if (V.opcode == Instruction::ICmp)
1310 return new CompareConstantExpr(Instruction::ICmp, V.predicate,
1311 V.operands[0], V.operands[1]);
1312 if (V.opcode == Instruction::FCmp)
1313 return new CompareConstantExpr(Instruction::FCmp, V.predicate,
1314 V.operands[0], V.operands[1]);
1315 assert(0 && "Invalid ConstantExpr!");
Jeff Cohen9f469632006-12-15 21:47:01 +00001316 return 0;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001317 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001318 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001319
Chris Lattner189d19f2003-11-21 20:23:48 +00001320 template<>
1321 struct ConvertConstantType<ConstantExpr, Type> {
1322 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1323 Constant *New;
1324 switch (OldC->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001325 case Instruction::Trunc:
1326 case Instruction::ZExt:
1327 case Instruction::SExt:
1328 case Instruction::FPTrunc:
1329 case Instruction::FPExt:
1330 case Instruction::UIToFP:
1331 case Instruction::SIToFP:
1332 case Instruction::FPToUI:
1333 case Instruction::FPToSI:
1334 case Instruction::PtrToInt:
1335 case Instruction::IntToPtr:
1336 case Instruction::BitCast:
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001337 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
1338 NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001339 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001340 case Instruction::Select:
1341 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1342 OldC->getOperand(1),
1343 OldC->getOperand(2));
1344 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001345 default:
1346 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001347 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner189d19f2003-11-21 20:23:48 +00001348 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1349 OldC->getOperand(1));
1350 break;
1351 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001352 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001353 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
Chris Lattner302116a2007-01-31 04:40:28 +00001354 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
1355 &Idx[0], Idx.size());
Chris Lattner189d19f2003-11-21 20:23:48 +00001356 break;
1357 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001358
Chris Lattner189d19f2003-11-21 20:23:48 +00001359 assert(New != OldC && "Didn't replace constant??");
1360 OldC->uncheckedReplaceAllUsesWith(New);
1361 OldC->destroyConstant(); // This constant is now dead, destroy it.
1362 }
1363 };
1364} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001365
1366
Chris Lattner3e650af2004-08-04 04:48:01 +00001367static ExprMapKeyType getValType(ConstantExpr *CE) {
1368 std::vector<Constant*> Operands;
1369 Operands.reserve(CE->getNumOperands());
1370 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1371 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spenceree3c9912006-12-04 05:19:50 +00001372 return ExprMapKeyType(CE->getOpcode(), Operands,
1373 CE->isCompare() ? CE->getPredicate() : 0);
Chris Lattner3e650af2004-08-04 04:48:01 +00001374}
1375
Chris Lattner69edc982006-09-28 00:35:06 +00001376static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1377 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001378
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001379/// This is a utility function to handle folding of casts and lookup of the
1380/// cast in the ExprConstants map. It is usedby the various get* methods below.
1381static inline Constant *getFoldedCast(
1382 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001383 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001384 // Fold a few common cases
1385 if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
1386 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001387
Vikram S. Adve4c485332002-07-15 18:19:33 +00001388 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001389 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00001390 ExprMapKeyType Key(opc, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001391 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001392}
Reid Spencerf37dc652006-12-05 19:14:13 +00001393
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001394Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
1395 Instruction::CastOps opc = Instruction::CastOps(oc);
1396 assert(Instruction::isCast(opc) && "opcode out of range");
1397 assert(C && Ty && "Null arguments to getCast");
1398 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1399
1400 switch (opc) {
1401 default:
1402 assert(0 && "Invalid cast opcode");
1403 break;
1404 case Instruction::Trunc: return getTrunc(C, Ty);
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001405 case Instruction::ZExt: return getZExt(C, Ty);
1406 case Instruction::SExt: return getSExt(C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001407 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1408 case Instruction::FPExt: return getFPExtend(C, Ty);
1409 case Instruction::UIToFP: return getUIToFP(C, Ty);
1410 case Instruction::SIToFP: return getSIToFP(C, Ty);
1411 case Instruction::FPToUI: return getFPToUI(C, Ty);
1412 case Instruction::FPToSI: return getFPToSI(C, Ty);
1413 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1414 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1415 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001416 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001417 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00001418}
1419
Reid Spencer5c140882006-12-04 20:17:56 +00001420Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
1421 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1422 return getCast(Instruction::BitCast, C, Ty);
1423 return getCast(Instruction::ZExt, C, Ty);
1424}
1425
1426Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
1427 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1428 return getCast(Instruction::BitCast, C, Ty);
1429 return getCast(Instruction::SExt, C, Ty);
1430}
1431
1432Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
1433 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1434 return getCast(Instruction::BitCast, C, Ty);
1435 return getCast(Instruction::Trunc, C, Ty);
1436}
1437
Reid Spencerbc245a02006-12-05 03:25:26 +00001438Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
1439 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001440 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001441
Chris Lattner03c49532007-01-15 02:27:26 +00001442 if (Ty->isInteger())
Reid Spencerbc245a02006-12-05 03:25:26 +00001443 return getCast(Instruction::PtrToInt, S, Ty);
1444 return getCast(Instruction::BitCast, S, Ty);
1445}
1446
Reid Spencer56521c42006-12-12 00:51:07 +00001447Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1448 bool isSigned) {
Chris Lattner03c49532007-01-15 02:27:26 +00001449 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer56521c42006-12-12 00:51:07 +00001450 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1451 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1452 Instruction::CastOps opcode =
1453 (SrcBits == DstBits ? Instruction::BitCast :
1454 (SrcBits > DstBits ? Instruction::Trunc :
1455 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1456 return getCast(opcode, C, Ty);
1457}
1458
1459Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
1460 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1461 "Invalid cast");
1462 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1463 unsigned DstBits = Ty->getPrimitiveSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001464 if (SrcBits == DstBits)
1465 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001466 Instruction::CastOps opcode =
Reid Spencerca104e82006-12-12 05:38:50 +00001467 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001468 return getCast(opcode, C, Ty);
1469}
1470
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001471Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001472 assert(C->getType()->isInteger() && "Trunc operand must be integer");
1473 assert(Ty->isInteger() && "Trunc produces only integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001474 assert(C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&&
1475 "SrcTy must be larger than DestTy for Trunc!");
1476
1477 return getFoldedCast(Instruction::Trunc, C, Ty);
1478}
1479
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001480Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001481 assert(C->getType()->isInteger() && "SEXt operand must be integral");
1482 assert(Ty->isInteger() && "SExt produces only integer");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001483 assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1484 "SrcTy must be smaller than DestTy for SExt!");
1485
1486 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerdd284742004-04-04 23:20:30 +00001487}
1488
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001489Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001490 assert(C->getType()->isInteger() && "ZEXt operand must be integral");
1491 assert(Ty->isInteger() && "ZExt produces only integer");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001492 assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1493 "SrcTy must be smaller than DestTy for ZExt!");
1494
1495 return getFoldedCast(Instruction::ZExt, C, Ty);
1496}
1497
1498Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
1499 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1500 C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&&
1501 "This is an illegal floating point truncation!");
1502 return getFoldedCast(Instruction::FPTrunc, C, Ty);
1503}
1504
1505Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
1506 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1507 C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1508 "This is an illegal floating point extension!");
1509 return getFoldedCast(Instruction::FPExt, C, Ty);
1510}
1511
1512Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001513 assert(C->getType()->isInteger() && Ty->isFloatingPoint() &&
Reid Spencer2546b762007-01-26 07:37:34 +00001514 "This is an illegal i32 to floating point cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001515 return getFoldedCast(Instruction::UIToFP, C, Ty);
1516}
1517
1518Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001519 assert(C->getType()->isInteger() && Ty->isFloatingPoint() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001520 "This is an illegal sint to floating point cast!");
1521 return getFoldedCast(Instruction::SIToFP, C, Ty);
1522}
1523
1524Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001525 assert(C->getType()->isFloatingPoint() && Ty->isInteger() &&
Reid Spencer2546b762007-01-26 07:37:34 +00001526 "This is an illegal floating point to i32 cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001527 return getFoldedCast(Instruction::FPToUI, C, Ty);
1528}
1529
1530Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001531 assert(C->getType()->isFloatingPoint() && Ty->isInteger() &&
Reid Spencer2546b762007-01-26 07:37:34 +00001532 "This is an illegal floating point to i32 cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001533 return getFoldedCast(Instruction::FPToSI, C, Ty);
1534}
1535
1536Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
1537 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner03c49532007-01-15 02:27:26 +00001538 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001539 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
1540}
1541
1542Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner03c49532007-01-15 02:27:26 +00001543 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001544 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
1545 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
1546}
1547
1548Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
1549 // BitCast implies a no-op cast of type only. No bits change. However, you
1550 // can't cast pointers to anything but pointers.
1551 const Type *SrcTy = C->getType();
1552 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer5c140882006-12-04 20:17:56 +00001553 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001554
1555 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
1556 // or nonptr->ptr). For all the other types, the cast is okay if source and
1557 // destination bit widths are identical.
1558 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1559 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Reid Spencer5c140882006-12-04 20:17:56 +00001560 assert(SrcBitSize == DstBitSize && "BitCast requies types of same width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001561 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerdd284742004-04-04 23:20:30 +00001562}
1563
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001564Constant *ConstantExpr::getSizeOf(const Type *Ty) {
Chris Lattneracc4e542004-12-13 19:48:51 +00001565 // sizeof is implemented as: (ulong) gep (Ty*)null, 1
Chris Lattnerb5d70302007-02-19 20:01:23 +00001566 Constant *GEPIdx = ConstantInt::get(Type::Int32Ty, 1);
1567 Constant *GEP =
1568 getGetElementPtr(getNullValue(PointerType::get(Ty)), &GEPIdx, 1);
1569 return getCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
Alkis Evlogimenos9160d5f2005-03-19 11:40:31 +00001570}
1571
Chris Lattnerb50d1352003-10-05 00:17:43 +00001572Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Reid Spencera009d0d2006-12-04 21:35:24 +00001573 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001574 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00001575 assert(Opcode >= Instruction::BinaryOpsBegin &&
1576 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001577 "Invalid opcode in binary constant expression");
1578 assert(C1->getType() == C2->getType() &&
1579 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001580
Reid Spencer542964f2007-01-11 18:21:29 +00001581 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Chris Lattnerb50d1352003-10-05 00:17:43 +00001582 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1583 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001584
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001585 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001586 ExprMapKeyType Key(Opcode, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001587 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001588}
1589
Reid Spencer266e42b2006-12-23 06:05:41 +00001590Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Reid Spencera009d0d2006-12-04 21:35:24 +00001591 Constant *C1, Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001592 switch (predicate) {
1593 default: assert(0 && "Invalid CmpInst predicate");
1594 case FCmpInst::FCMP_FALSE: case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_OGT:
1595 case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OLE:
1596 case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_ORD: case FCmpInst::FCMP_UNO:
1597 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UGT: case FCmpInst::FCMP_UGE:
1598 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_ULE: case FCmpInst::FCMP_UNE:
1599 case FCmpInst::FCMP_TRUE:
1600 return getFCmp(predicate, C1, C2);
1601 case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_UGT:
1602 case ICmpInst::ICMP_UGE: case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE:
1603 case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_SGE: case ICmpInst::ICMP_SLT:
1604 case ICmpInst::ICMP_SLE:
1605 return getICmp(predicate, C1, C2);
1606 }
Reid Spencera009d0d2006-12-04 21:35:24 +00001607}
1608
1609Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001610#ifndef NDEBUG
1611 switch (Opcode) {
Reid Spencer7eb55b32006-11-02 01:53:59 +00001612 case Instruction::Add:
1613 case Instruction::Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001614 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001615 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattner03c49532007-01-15 02:27:26 +00001616 assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001617 isa<VectorType>(C1->getType())) &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001618 "Tried to create an arithmetic operation on a non-arithmetic type!");
1619 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001620 case Instruction::UDiv:
1621 case Instruction::SDiv:
1622 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001623 assert((C1->getType()->isInteger() || (isa<VectorType>(C1->getType()) &&
1624 cast<VectorType>(C1->getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001625 "Tried to create an arithmetic operation on a non-arithmetic type!");
1626 break;
1627 case Instruction::FDiv:
1628 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001629 assert((C1->getType()->isFloatingPoint() || (isa<VectorType>(C1->getType())
1630 && cast<VectorType>(C1->getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001631 && "Tried to create an arithmetic operation on a non-arithmetic type!");
1632 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001633 case Instruction::URem:
1634 case Instruction::SRem:
1635 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001636 assert((C1->getType()->isInteger() || (isa<VectorType>(C1->getType()) &&
1637 cast<VectorType>(C1->getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001638 "Tried to create an arithmetic operation on a non-arithmetic type!");
1639 break;
1640 case Instruction::FRem:
1641 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001642 assert((C1->getType()->isFloatingPoint() || (isa<VectorType>(C1->getType())
1643 && cast<VectorType>(C1->getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001644 && "Tried to create an arithmetic operation on a non-arithmetic type!");
1645 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001646 case Instruction::And:
1647 case Instruction::Or:
1648 case Instruction::Xor:
1649 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001650 assert((C1->getType()->isInteger() || isa<VectorType>(C1->getType())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001651 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001652 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001653 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001654 case Instruction::LShr:
1655 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00001656 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattner03c49532007-01-15 02:27:26 +00001657 assert(C1->getType()->isInteger() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001658 "Tried to create a shift operation on a non-integer type!");
1659 break;
1660 default:
1661 break;
1662 }
1663#endif
1664
Reid Spencera009d0d2006-12-04 21:35:24 +00001665 return getTy(C1->getType(), Opcode, C1, C2);
1666}
1667
Reid Spencer266e42b2006-12-23 06:05:41 +00001668Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00001669 Constant *C1, Constant *C2) {
1670 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001671 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00001672}
1673
Chris Lattner6e415c02004-03-12 05:54:04 +00001674Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
1675 Constant *V1, Constant *V2) {
Reid Spencer2546b762007-01-26 07:37:34 +00001676 assert(C->getType() == Type::Int1Ty && "Select condition must be i1!");
Chris Lattner6e415c02004-03-12 05:54:04 +00001677 assert(V1->getType() == V2->getType() && "Select value types must match!");
1678 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
1679
1680 if (ReqTy == V1->getType())
1681 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1682 return SC; // Fold common cases
1683
1684 std::vector<Constant*> argVec(3, C);
1685 argVec[1] = V1;
1686 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00001687 ExprMapKeyType Key(Instruction::Select, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001688 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00001689}
1690
Chris Lattnerb50d1352003-10-05 00:17:43 +00001691Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00001692 Value* const *Idxs,
1693 unsigned NumIdx) {
1694 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs, NumIdx, true) &&
Chris Lattner04b60fe2004-02-16 20:46:13 +00001695 "GEP indices invalid!");
1696
Chris Lattner302116a2007-01-31 04:40:28 +00001697 if (Constant *FC = ConstantFoldGetElementPtr(C, (Constant**)Idxs, NumIdx))
Chris Lattneracdbe712003-04-17 19:24:48 +00001698 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001699
Chris Lattnerb50d1352003-10-05 00:17:43 +00001700 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001701 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001702 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00001703 std::vector<Constant*> ArgVec;
Chris Lattner302116a2007-01-31 04:40:28 +00001704 ArgVec.reserve(NumIdx+1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001705 ArgVec.push_back(C);
Chris Lattner302116a2007-01-31 04:40:28 +00001706 for (unsigned i = 0; i != NumIdx; ++i)
1707 ArgVec.push_back(cast<Constant>(Idxs[i]));
1708 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001709 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001710}
1711
Chris Lattner302116a2007-01-31 04:40:28 +00001712Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
1713 unsigned NumIdx) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00001714 // Get the result type of the getelementptr!
Chris Lattner302116a2007-01-31 04:40:28 +00001715 const Type *Ty =
1716 GetElementPtrInst::getIndexedType(C->getType(), Idxs, NumIdx, true);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001717 assert(Ty && "GEP indices invalid!");
Chris Lattner302116a2007-01-31 04:40:28 +00001718 return getGetElementPtrTy(PointerType::get(Ty), C, Idxs, NumIdx);
Chris Lattner13128ab2004-10-11 22:52:25 +00001719}
1720
Chris Lattner302116a2007-01-31 04:40:28 +00001721Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
1722 unsigned NumIdx) {
1723 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001724}
1725
Chris Lattner302116a2007-01-31 04:40:28 +00001726
Reid Spenceree3c9912006-12-04 05:19:50 +00001727Constant *
1728ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1729 assert(LHS->getType() == RHS->getType());
1730 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
1731 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
1732
Reid Spencer266e42b2006-12-23 06:05:41 +00001733 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001734 return FC; // Fold a few common cases...
1735
1736 // Look up the constant in the table first to ensure uniqueness
1737 std::vector<Constant*> ArgVec;
1738 ArgVec.push_back(LHS);
1739 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001740 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001741 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Reid Spencer542964f2007-01-11 18:21:29 +00001742 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001743}
1744
1745Constant *
1746ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1747 assert(LHS->getType() == RHS->getType());
1748 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
1749
Reid Spencer266e42b2006-12-23 06:05:41 +00001750 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001751 return FC; // Fold a few common cases...
1752
1753 // Look up the constant in the table first to ensure uniqueness
1754 std::vector<Constant*> ArgVec;
1755 ArgVec.push_back(LHS);
1756 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001757 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001758 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Reid Spencer542964f2007-01-11 18:21:29 +00001759 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001760}
1761
Robert Bocchino23004482006-01-10 19:05:34 +00001762Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
1763 Constant *Idx) {
Robert Bocchinode7f1c92006-01-10 20:03:46 +00001764 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
1765 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00001766 // Look up the constant in the table first to ensure uniqueness
1767 std::vector<Constant*> ArgVec(1, Val);
1768 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001769 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001770 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00001771}
1772
1773Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001774 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001775 "Tried to create extractelement operation on non-vector type!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001776 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00001777 "Extractelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001778 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +00001779 Val, Idx);
1780}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001781
Robert Bocchinoca27f032006-01-17 20:07:22 +00001782Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
1783 Constant *Elt, Constant *Idx) {
1784 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
1785 return FC; // Fold a few common cases...
1786 // Look up the constant in the table first to ensure uniqueness
1787 std::vector<Constant*> ArgVec(1, Val);
1788 ArgVec.push_back(Elt);
1789 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001790 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001791 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001792}
1793
1794Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1795 Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001796 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001797 "Tried to create insertelement operation on non-vector type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001798 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoca27f032006-01-17 20:07:22 +00001799 && "Insertelement types must match!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001800 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00001801 "Insertelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001802 return getInsertElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchinoca27f032006-01-17 20:07:22 +00001803 Val, Elt, Idx);
1804}
1805
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001806Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
1807 Constant *V2, Constant *Mask) {
1808 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
1809 return FC; // Fold a few common cases...
1810 // Look up the constant in the table first to ensure uniqueness
1811 std::vector<Constant*> ArgVec(1, V1);
1812 ArgVec.push_back(V2);
1813 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00001814 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001815 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001816}
1817
1818Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
1819 Constant *Mask) {
1820 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
1821 "Invalid shuffle vector constant expr operands!");
1822 return getShuffleVectorTy(V1->getType(), V1, V2, Mask);
1823}
1824
Reid Spencer2eadb532007-01-21 00:29:26 +00001825Constant *ConstantExpr::getZeroValueForNegationExpr(const Type *Ty) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001826 if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
Reid Spencer6598ca82007-01-21 02:29:10 +00001827 if (PTy->getElementType()->isFloatingPoint()) {
1828 std::vector<Constant*> zeros(PTy->getNumElements(),
1829 ConstantFP::get(PTy->getElementType(),-0.0));
Reid Spencerd84d35b2007-02-15 02:26:10 +00001830 return ConstantVector::get(PTy, zeros);
Reid Spencer6598ca82007-01-21 02:29:10 +00001831 }
Reid Spencer2eadb532007-01-21 00:29:26 +00001832
1833 if (Ty->isFloatingPoint())
1834 return ConstantFP::get(Ty, -0.0);
1835
1836 return Constant::getNullValue(Ty);
1837}
1838
Vikram S. Adve4c485332002-07-15 18:19:33 +00001839// destroyConstant - Remove the constant from the constant table...
1840//
1841void ConstantExpr::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001842 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001843 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001844}
1845
Chris Lattner3cd8c562002-07-30 18:54:25 +00001846const char *ConstantExpr::getOpcodeName() const {
1847 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001848}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00001849
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001850//===----------------------------------------------------------------------===//
1851// replaceUsesOfWithOnConstant implementations
1852
1853void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001854 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001855 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00001856 Constant *ToC = cast<Constant>(To);
Chris Lattnerdff59112005-10-04 18:47:09 +00001857
1858 unsigned OperandToUpdate = U-OperandList;
1859 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1860
Jim Laskeyc03caef2006-07-17 17:38:29 +00001861 std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnerb64419a2005-10-03 22:51:37 +00001862 Lookup.first.first = getType();
1863 Lookup.second = this;
Chris Lattnerdff59112005-10-04 18:47:09 +00001864
Chris Lattnerb64419a2005-10-03 22:51:37 +00001865 std::vector<Constant*> &Values = Lookup.first.second;
1866 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattnerdff59112005-10-04 18:47:09 +00001867
Chris Lattner8760ec72005-10-04 01:17:50 +00001868 // Fill values with the modified operands of the constant array. Also,
1869 // compute whether this turns into an all-zeros array.
Chris Lattnerdff59112005-10-04 18:47:09 +00001870 bool isAllZeros = false;
1871 if (!ToC->isNullValue()) {
1872 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1873 Values.push_back(cast<Constant>(O->get()));
1874 } else {
1875 isAllZeros = true;
1876 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1877 Constant *Val = cast<Constant>(O->get());
1878 Values.push_back(Val);
1879 if (isAllZeros) isAllZeros = Val->isNullValue();
1880 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001881 }
Chris Lattnerdff59112005-10-04 18:47:09 +00001882 Values[OperandToUpdate] = ToC;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001883
Chris Lattnerb64419a2005-10-03 22:51:37 +00001884 Constant *Replacement = 0;
1885 if (isAllZeros) {
1886 Replacement = ConstantAggregateZero::get(getType());
1887 } else {
1888 // Check to see if we have this array type already.
1889 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00001890 ArrayConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00001891 ArrayConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001892
1893 if (Exists) {
1894 Replacement = I->second;
1895 } else {
1896 // Okay, the new shape doesn't exist in the system yet. Instead of
1897 // creating a new constant array, inserting it, replaceallusesof'ing the
1898 // old with the new, then deleting the old... just update the current one
1899 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00001900 ArrayConstants->MoveConstantToNewSlot(this, I);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001901
Chris Lattnerdff59112005-10-04 18:47:09 +00001902 // Update to the new value.
1903 setOperand(OperandToUpdate, ToC);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001904 return;
1905 }
1906 }
1907
1908 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001909 assert(Replacement != this && "I didn't contain From!");
1910
Chris Lattner7a1450d2005-10-04 18:13:04 +00001911 // Everyone using this now uses the replacement.
1912 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001913
1914 // Delete the old constant!
1915 destroyConstant();
1916}
1917
1918void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001919 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001920 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00001921 Constant *ToC = cast<Constant>(To);
1922
Chris Lattnerdff59112005-10-04 18:47:09 +00001923 unsigned OperandToUpdate = U-OperandList;
1924 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1925
Jim Laskeyc03caef2006-07-17 17:38:29 +00001926 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattner8760ec72005-10-04 01:17:50 +00001927 Lookup.first.first = getType();
1928 Lookup.second = this;
1929 std::vector<Constant*> &Values = Lookup.first.second;
1930 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001931
Chris Lattnerdff59112005-10-04 18:47:09 +00001932
Chris Lattner8760ec72005-10-04 01:17:50 +00001933 // Fill values with the modified operands of the constant struct. Also,
1934 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00001935 bool isAllZeros = false;
1936 if (!ToC->isNullValue()) {
1937 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1938 Values.push_back(cast<Constant>(O->get()));
1939 } else {
1940 isAllZeros = true;
1941 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1942 Constant *Val = cast<Constant>(O->get());
1943 Values.push_back(Val);
1944 if (isAllZeros) isAllZeros = Val->isNullValue();
1945 }
Chris Lattner8760ec72005-10-04 01:17:50 +00001946 }
Chris Lattnerdff59112005-10-04 18:47:09 +00001947 Values[OperandToUpdate] = ToC;
1948
Chris Lattner8760ec72005-10-04 01:17:50 +00001949 Constant *Replacement = 0;
1950 if (isAllZeros) {
1951 Replacement = ConstantAggregateZero::get(getType());
1952 } else {
1953 // Check to see if we have this array type already.
1954 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00001955 StructConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00001956 StructConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00001957
1958 if (Exists) {
1959 Replacement = I->second;
1960 } else {
1961 // Okay, the new shape doesn't exist in the system yet. Instead of
1962 // creating a new constant struct, inserting it, replaceallusesof'ing the
1963 // old with the new, then deleting the old... just update the current one
1964 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00001965 StructConstants->MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00001966
Chris Lattnerdff59112005-10-04 18:47:09 +00001967 // Update to the new value.
1968 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00001969 return;
1970 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001971 }
1972
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001973 assert(Replacement != this && "I didn't contain From!");
1974
Chris Lattner7a1450d2005-10-04 18:13:04 +00001975 // Everyone using this now uses the replacement.
1976 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001977
1978 // Delete the old constant!
1979 destroyConstant();
1980}
1981
Reid Spencerd84d35b2007-02-15 02:26:10 +00001982void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001983 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001984 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
1985
1986 std::vector<Constant*> Values;
1987 Values.reserve(getNumOperands()); // Build replacement array...
1988 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1989 Constant *Val = getOperand(i);
1990 if (Val == From) Val = cast<Constant>(To);
1991 Values.push_back(Val);
1992 }
1993
Reid Spencerd84d35b2007-02-15 02:26:10 +00001994 Constant *Replacement = ConstantVector::get(getType(), Values);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001995 assert(Replacement != this && "I didn't contain From!");
1996
Chris Lattner7a1450d2005-10-04 18:13:04 +00001997 // Everyone using this now uses the replacement.
1998 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001999
2000 // Delete the old constant!
2001 destroyConstant();
2002}
2003
2004void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002005 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002006 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2007 Constant *To = cast<Constant>(ToV);
2008
2009 Constant *Replacement = 0;
2010 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerb5d70302007-02-19 20:01:23 +00002011 SmallVector<Constant*, 8> Indices;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002012 Constant *Pointer = getOperand(0);
2013 Indices.reserve(getNumOperands()-1);
2014 if (Pointer == From) Pointer = To;
2015
2016 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2017 Constant *Val = getOperand(i);
2018 if (Val == From) Val = To;
2019 Indices.push_back(Val);
2020 }
Chris Lattnerb5d70302007-02-19 20:01:23 +00002021 Replacement = ConstantExpr::getGetElementPtr(Pointer,
2022 &Indices[0], Indices.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002023 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002024 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002025 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002026 } else if (getOpcode() == Instruction::Select) {
2027 Constant *C1 = getOperand(0);
2028 Constant *C2 = getOperand(1);
2029 Constant *C3 = getOperand(2);
2030 if (C1 == From) C1 = To;
2031 if (C2 == From) C2 = To;
2032 if (C3 == From) C3 = To;
2033 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00002034 } else if (getOpcode() == Instruction::ExtractElement) {
2035 Constant *C1 = getOperand(0);
2036 Constant *C2 = getOperand(1);
2037 if (C1 == From) C1 = To;
2038 if (C2 == From) C2 = To;
2039 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00002040 } else if (getOpcode() == Instruction::InsertElement) {
2041 Constant *C1 = getOperand(0);
2042 Constant *C2 = getOperand(1);
2043 Constant *C3 = getOperand(1);
2044 if (C1 == From) C1 = To;
2045 if (C2 == From) C2 = To;
2046 if (C3 == From) C3 = To;
2047 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2048 } else if (getOpcode() == Instruction::ShuffleVector) {
2049 Constant *C1 = getOperand(0);
2050 Constant *C2 = getOperand(1);
2051 Constant *C3 = getOperand(2);
2052 if (C1 == From) C1 = To;
2053 if (C2 == From) C2 = To;
2054 if (C3 == From) C3 = To;
2055 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00002056 } else if (isCompare()) {
2057 Constant *C1 = getOperand(0);
2058 Constant *C2 = getOperand(1);
2059 if (C1 == From) C1 = To;
2060 if (C2 == From) C2 = To;
2061 if (getOpcode() == Instruction::ICmp)
2062 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
2063 else
2064 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002065 } else if (getNumOperands() == 2) {
2066 Constant *C1 = getOperand(0);
2067 Constant *C2 = getOperand(1);
2068 if (C1 == From) C1 = To;
2069 if (C2 == From) C2 = To;
2070 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2071 } else {
2072 assert(0 && "Unknown ConstantExpr type!");
2073 return;
2074 }
2075
2076 assert(Replacement != this && "I didn't contain From!");
2077
Chris Lattner7a1450d2005-10-04 18:13:04 +00002078 // Everyone using this now uses the replacement.
2079 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002080
2081 // Delete the old constant!
2082 destroyConstant();
2083}
2084
2085
Jim Laskey2698f0d2006-03-08 18:11:07 +00002086/// getStringValue - Turn an LLVM constant pointer that eventually points to a
2087/// global into a string value. Return an empty string if we can't do it.
Evan Cheng38280c02006-03-10 23:52:03 +00002088/// Parameter Chop determines if the result is chopped at the first null
2089/// terminator.
Jim Laskey2698f0d2006-03-08 18:11:07 +00002090///
Evan Cheng38280c02006-03-10 23:52:03 +00002091std::string Constant::getStringValue(bool Chop, unsigned Offset) {
Jim Laskey2698f0d2006-03-08 18:11:07 +00002092 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(this)) {
2093 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
2094 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
2095 if (Init->isString()) {
2096 std::string Result = Init->getAsString();
2097 if (Offset < Result.size()) {
2098 // If we are pointing INTO The string, erase the beginning...
2099 Result.erase(Result.begin(), Result.begin()+Offset);
2100
2101 // Take off the null terminator, and any string fragments after it.
Evan Cheng38280c02006-03-10 23:52:03 +00002102 if (Chop) {
2103 std::string::size_type NullPos = Result.find_first_of((char)0);
2104 if (NullPos != std::string::npos)
2105 Result.erase(Result.begin()+NullPos, Result.end());
2106 }
Jim Laskey2698f0d2006-03-08 18:11:07 +00002107 return Result;
2108 }
2109 }
2110 }
2111 } else if (Constant *C = dyn_cast<Constant>(this)) {
2112 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
Evan Cheng2c5e5302006-03-11 00:13:10 +00002113 return GV->getStringValue(Chop, Offset);
Jim Laskey2698f0d2006-03-08 18:11:07 +00002114 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
2115 if (CE->getOpcode() == Instruction::GetElementPtr) {
2116 // Turn a gep into the specified offset.
2117 if (CE->getNumOperands() == 3 &&
2118 cast<Constant>(CE->getOperand(1))->isNullValue() &&
2119 isa<ConstantInt>(CE->getOperand(2))) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00002120 Offset += cast<ConstantInt>(CE->getOperand(2))->getZExtValue();
Evan Cheng2c5e5302006-03-11 00:13:10 +00002121 return CE->getOperand(0)->getStringValue(Chop, Offset);
Jim Laskey2698f0d2006-03-08 18:11:07 +00002122 }
2123 }
2124 }
2125 }
2126 return "";
2127}