blob: 0d595314a53890adeeef750b6c641fb4f2aa044f [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
Reid Spencere0fc4df2006-10-20 07:07:24 +0000803// Get a ConstantInt from an int64_t. Note here that we canoncialize the value
804// to a uint64_t value that has been zero extended down to the size of the
805// integer type of the ConstantInt. This allows the getZExtValue method to
806// just return the stored value while getSExtValue has to convert back to sign
807// extended. getZExtValue is more common in LLVM than getSExtValue().
808ConstantInt *ConstantInt::get(const Type *Ty, int64_t V) {
Chris Lattner5db2f472007-02-20 05:55:46 +0000809 const IntegerType *ITy = cast<IntegerType>(Ty);
810 if (Ty == Type::Int1Ty)
Reid Spencercddc9df2007-01-12 04:24:46 +0000811 if (V & 1)
812 return getTrue();
813 else
814 return getFalse();
Chris Lattner5db2f472007-02-20 05:55:46 +0000815 return IntConstants->getOrCreate(ITy, V & ITy->getBitMask());
Chris Lattner49d855c2001-09-07 16:46:31 +0000816}
817
Chris Lattner3462ae32001-12-03 22:26:30 +0000818//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000819//
Chris Lattnerac80ea42004-02-01 22:49:04 +0000820namespace llvm {
821 template<>
822 struct ConstantCreator<ConstantFP, Type, uint64_t> {
823 static ConstantFP *create(const Type *Ty, uint64_t V) {
824 assert(Ty == Type::DoubleTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000825 return new ConstantFP(Ty, BitsToDouble(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000826 }
827 };
828 template<>
829 struct ConstantCreator<ConstantFP, Type, uint32_t> {
830 static ConstantFP *create(const Type *Ty, uint32_t V) {
831 assert(Ty == Type::FloatTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000832 return new ConstantFP(Ty, BitsToFloat(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000833 }
834 };
835}
836
Chris Lattner69edc982006-09-28 00:35:06 +0000837static ManagedStatic<ValueMap<uint64_t, Type, ConstantFP> > DoubleConstants;
838static ManagedStatic<ValueMap<uint32_t, Type, ConstantFP> > FloatConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000839
Jim Laskey8ad8f712005-08-17 20:06:22 +0000840bool ConstantFP::isNullValue() const {
841 return DoubleToBits(Val) == 0;
842}
843
844bool ConstantFP::isExactlyValue(double V) const {
845 return DoubleToBits(V) == DoubleToBits(Val);
846}
847
848
Chris Lattner3462ae32001-12-03 22:26:30 +0000849ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner241ed4c2004-01-23 00:55:21 +0000850 if (Ty == Type::FloatTy) {
851 // Force the value through memory to normalize it.
Chris Lattner69edc982006-09-28 00:35:06 +0000852 return FloatConstants->getOrCreate(Ty, FloatToBits(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000853 } else {
854 assert(Ty == Type::DoubleTy);
Chris Lattner69edc982006-09-28 00:35:06 +0000855 return DoubleConstants->getOrCreate(Ty, DoubleToBits(V));
Chris Lattner241ed4c2004-01-23 00:55:21 +0000856 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000857}
858
Chris Lattner9fba3da2004-02-15 05:53:04 +0000859//---- ConstantAggregateZero::get() implementation...
860//
861namespace llvm {
862 // ConstantAggregateZero does not take extra "value" argument...
863 template<class ValType>
864 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
865 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
866 return new ConstantAggregateZero(Ty);
867 }
868 };
869
870 template<>
871 struct ConvertConstantType<ConstantAggregateZero, Type> {
872 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
873 // Make everyone now use a constant of the new type...
874 Constant *New = ConstantAggregateZero::get(NewTy);
875 assert(New != OldC && "Didn't replace constant??");
876 OldC->uncheckedReplaceAllUsesWith(New);
877 OldC->destroyConstant(); // This constant is now dead, destroy it.
878 }
879 };
880}
881
Chris Lattner69edc982006-09-28 00:35:06 +0000882static ManagedStatic<ValueMap<char, Type,
883 ConstantAggregateZero> > AggZeroConstants;
Chris Lattner9fba3da2004-02-15 05:53:04 +0000884
Chris Lattner3e650af2004-08-04 04:48:01 +0000885static char getValType(ConstantAggregateZero *CPZ) { return 0; }
886
Chris Lattner9fba3da2004-02-15 05:53:04 +0000887Constant *ConstantAggregateZero::get(const Type *Ty) {
Reid Spencerd84d35b2007-02-15 02:26:10 +0000888 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) &&
Chris Lattnerbfd0b6d2006-06-10 04:16:23 +0000889 "Cannot create an aggregate zero of non-aggregate type!");
Chris Lattner69edc982006-09-28 00:35:06 +0000890 return AggZeroConstants->getOrCreate(Ty, 0);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000891}
892
893// destroyConstant - Remove the constant from the constant table...
894//
895void ConstantAggregateZero::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +0000896 AggZeroConstants->remove(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000897 destroyConstantImpl();
898}
899
Chris Lattner3462ae32001-12-03 22:26:30 +0000900//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000901//
Chris Lattner189d19f2003-11-21 20:23:48 +0000902namespace llvm {
903 template<>
904 struct ConvertConstantType<ConstantArray, ArrayType> {
905 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
906 // Make everyone now use a constant of the new type...
907 std::vector<Constant*> C;
908 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
909 C.push_back(cast<Constant>(OldC->getOperand(i)));
910 Constant *New = ConstantArray::get(NewTy, C);
911 assert(New != OldC && "Didn't replace constant??");
912 OldC->uncheckedReplaceAllUsesWith(New);
913 OldC->destroyConstant(); // This constant is now dead, destroy it.
914 }
915 };
916}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000917
Chris Lattner3e650af2004-08-04 04:48:01 +0000918static std::vector<Constant*> getValType(ConstantArray *CA) {
919 std::vector<Constant*> Elements;
920 Elements.reserve(CA->getNumOperands());
921 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
922 Elements.push_back(cast<Constant>(CA->getOperand(i)));
923 return Elements;
924}
925
Chris Lattnerb64419a2005-10-03 22:51:37 +0000926typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattner935aa922005-10-04 17:48:46 +0000927 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +0000928static ManagedStatic<ArrayConstantsTy> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000929
Chris Lattner015e8212004-02-15 04:14:47 +0000930Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +0000931 const std::vector<Constant*> &V) {
932 // If this is an all-zero array, return a ConstantAggregateZero object
933 if (!V.empty()) {
934 Constant *C = V[0];
935 if (!C->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +0000936 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000937 for (unsigned i = 1, e = V.size(); i != e; ++i)
938 if (V[i] != C)
Chris Lattner69edc982006-09-28 00:35:06 +0000939 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000940 }
941 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000942}
943
Chris Lattner98fa07b2003-05-23 20:03:32 +0000944// destroyConstant - Remove the constant from the constant table...
945//
946void ConstantArray::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +0000947 ArrayConstants->remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000948 destroyConstantImpl();
949}
950
Reid Spencer6f614532006-05-30 08:23:18 +0000951/// ConstantArray::get(const string&) - Return an array that is initialized to
952/// contain the specified string. If length is zero then a null terminator is
953/// added to the specified string so that it may be used in a natural way.
954/// Otherwise, the length parameter specifies how much of the string to use
955/// and it won't be null terminated.
956///
Reid Spencer82ebaba2006-05-30 18:15:07 +0000957Constant *ConstantArray::get(const std::string &Str, bool AddNull) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000958 std::vector<Constant*> ElementVals;
Reid Spencer82ebaba2006-05-30 18:15:07 +0000959 for (unsigned i = 0; i < Str.length(); ++i)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000960 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000961
962 // Add a null terminator to the string...
Reid Spencer82ebaba2006-05-30 18:15:07 +0000963 if (AddNull) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000964 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, 0));
Reid Spencer6f614532006-05-30 08:23:18 +0000965 }
Chris Lattner8f80fe02001-10-14 23:54:12 +0000966
Reid Spencer8d9336d2006-12-31 05:26:44 +0000967 ArrayType *ATy = ArrayType::get(Type::Int8Ty, ElementVals.size());
Chris Lattner3462ae32001-12-03 22:26:30 +0000968 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000969}
970
Reid Spencer2546b762007-01-26 07:37:34 +0000971/// isString - This method returns true if the array is an array of i8, and
972/// if the elements of the array are all ConstantInt's.
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000973bool ConstantArray::isString() const {
Reid Spencer2546b762007-01-26 07:37:34 +0000974 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +0000975 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000976 return false;
977 // Check the elements to make sure they are all integers, not constant
978 // expressions.
979 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
980 if (!isa<ConstantInt>(getOperand(i)))
981 return false;
982 return true;
983}
984
Evan Cheng3763c5b2006-10-26 19:15:05 +0000985/// isCString - This method returns true if the array is a string (see
986/// isString) and it ends in a null byte \0 and does not contains any other
987/// null bytes except its terminator.
988bool ConstantArray::isCString() const {
Reid Spencer2546b762007-01-26 07:37:34 +0000989 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +0000990 if (getType()->getElementType() != Type::Int8Ty)
Evan Chenge974da62006-10-26 21:48:03 +0000991 return false;
992 Constant *Zero = Constant::getNullValue(getOperand(0)->getType());
993 // Last element must be a null.
994 if (getOperand(getNumOperands()-1) != Zero)
995 return false;
996 // Other elements must be non-null integers.
997 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
998 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +0000999 return false;
Evan Chenge974da62006-10-26 21:48:03 +00001000 if (getOperand(i) == Zero)
1001 return false;
1002 }
Evan Cheng3763c5b2006-10-26 19:15:05 +00001003 return true;
1004}
1005
1006
Reid Spencer2546b762007-01-26 07:37:34 +00001007// getAsString - If the sub-element type of this array is i8
Chris Lattner81fabb02002-08-26 17:53:56 +00001008// then this method converts the array to an std::string and returns it.
1009// Otherwise, it asserts out.
1010//
1011std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001012 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001013 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +00001014 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Reid Spencere0fc4df2006-10-20 07:07:24 +00001015 Result += (char)cast<ConstantInt>(getOperand(i))->getZExtValue();
Chris Lattner81fabb02002-08-26 17:53:56 +00001016 return Result;
1017}
1018
1019
Chris Lattner3462ae32001-12-03 22:26:30 +00001020//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001021//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001022
Chris Lattner189d19f2003-11-21 20:23:48 +00001023namespace llvm {
1024 template<>
1025 struct ConvertConstantType<ConstantStruct, StructType> {
1026 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1027 // Make everyone now use a constant of the new type...
1028 std::vector<Constant*> C;
1029 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1030 C.push_back(cast<Constant>(OldC->getOperand(i)));
1031 Constant *New = ConstantStruct::get(NewTy, C);
1032 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001033
Chris Lattner189d19f2003-11-21 20:23:48 +00001034 OldC->uncheckedReplaceAllUsesWith(New);
1035 OldC->destroyConstant(); // This constant is now dead, destroy it.
1036 }
1037 };
1038}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001039
Chris Lattner8760ec72005-10-04 01:17:50 +00001040typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattner935aa922005-10-04 17:48:46 +00001041 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001042static ManagedStatic<StructConstantsTy> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001043
Chris Lattner3e650af2004-08-04 04:48:01 +00001044static std::vector<Constant*> getValType(ConstantStruct *CS) {
1045 std::vector<Constant*> Elements;
1046 Elements.reserve(CS->getNumOperands());
1047 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1048 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1049 return Elements;
1050}
1051
Chris Lattner015e8212004-02-15 04:14:47 +00001052Constant *ConstantStruct::get(const StructType *Ty,
1053 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001054 // Create a ConstantAggregateZero value if all elements are zeros...
1055 for (unsigned i = 0, e = V.size(); i != e; ++i)
1056 if (!V[i]->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001057 return StructConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001058
1059 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001060}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001061
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001062Constant *ConstantStruct::get(const std::vector<Constant*> &V, bool packed) {
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001063 std::vector<const Type*> StructEls;
1064 StructEls.reserve(V.size());
1065 for (unsigned i = 0, e = V.size(); i != e; ++i)
1066 StructEls.push_back(V[i]->getType());
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001067 return get(StructType::get(StructEls, packed), V);
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001068}
1069
Chris Lattnerd7a73302001-10-13 06:57:33 +00001070// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001071//
Chris Lattner3462ae32001-12-03 22:26:30 +00001072void ConstantStruct::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001073 StructConstants->remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001074 destroyConstantImpl();
1075}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001076
Reid Spencerd84d35b2007-02-15 02:26:10 +00001077//---- ConstantVector::get() implementation...
Brian Gaeke02209042004-08-20 06:00:58 +00001078//
1079namespace llvm {
1080 template<>
Reid Spencerd84d35b2007-02-15 02:26:10 +00001081 struct ConvertConstantType<ConstantVector, VectorType> {
1082 static void convert(ConstantVector *OldC, const VectorType *NewTy) {
Brian Gaeke02209042004-08-20 06:00:58 +00001083 // Make everyone now use a constant of the new type...
1084 std::vector<Constant*> C;
1085 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1086 C.push_back(cast<Constant>(OldC->getOperand(i)));
Reid Spencerd84d35b2007-02-15 02:26:10 +00001087 Constant *New = ConstantVector::get(NewTy, C);
Brian Gaeke02209042004-08-20 06:00:58 +00001088 assert(New != OldC && "Didn't replace constant??");
1089 OldC->uncheckedReplaceAllUsesWith(New);
1090 OldC->destroyConstant(); // This constant is now dead, destroy it.
1091 }
1092 };
1093}
1094
Reid Spencerd84d35b2007-02-15 02:26:10 +00001095static std::vector<Constant*> getValType(ConstantVector *CP) {
Brian Gaeke02209042004-08-20 06:00:58 +00001096 std::vector<Constant*> Elements;
1097 Elements.reserve(CP->getNumOperands());
1098 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1099 Elements.push_back(CP->getOperand(i));
1100 return Elements;
1101}
1102
Reid Spencerd84d35b2007-02-15 02:26:10 +00001103static ManagedStatic<ValueMap<std::vector<Constant*>, VectorType,
Reid Spencer09575ba2007-02-15 03:39:18 +00001104 ConstantVector> > VectorConstants;
Brian Gaeke02209042004-08-20 06:00:58 +00001105
Reid Spencerd84d35b2007-02-15 02:26:10 +00001106Constant *ConstantVector::get(const VectorType *Ty,
Brian Gaeke02209042004-08-20 06:00:58 +00001107 const std::vector<Constant*> &V) {
1108 // If this is an all-zero packed, return a ConstantAggregateZero object
1109 if (!V.empty()) {
1110 Constant *C = V[0];
1111 if (!C->isNullValue())
Reid Spencer09575ba2007-02-15 03:39:18 +00001112 return VectorConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001113 for (unsigned i = 1, e = V.size(); i != e; ++i)
1114 if (V[i] != C)
Reid Spencer09575ba2007-02-15 03:39:18 +00001115 return VectorConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001116 }
1117 return ConstantAggregateZero::get(Ty);
1118}
1119
Reid Spencerd84d35b2007-02-15 02:26:10 +00001120Constant *ConstantVector::get(const std::vector<Constant*> &V) {
Brian Gaeke02209042004-08-20 06:00:58 +00001121 assert(!V.empty() && "Cannot infer type if V is empty");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001122 return get(VectorType::get(V.front()->getType(),V.size()), V);
Brian Gaeke02209042004-08-20 06:00:58 +00001123}
1124
1125// destroyConstant - Remove the constant from the constant table...
1126//
Reid Spencerd84d35b2007-02-15 02:26:10 +00001127void ConstantVector::destroyConstant() {
Reid Spencer09575ba2007-02-15 03:39:18 +00001128 VectorConstants->remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001129 destroyConstantImpl();
1130}
1131
Jim Laskeyf0478822007-01-12 22:39:14 +00001132/// This function will return true iff every element in this packed constant
1133/// is set to all ones.
1134/// @returns true iff this constant's emements are all set to all ones.
1135/// @brief Determine if the value is all ones.
Reid Spencerd84d35b2007-02-15 02:26:10 +00001136bool ConstantVector::isAllOnesValue() const {
Jim Laskeyf0478822007-01-12 22:39:14 +00001137 // Check out first element.
1138 const Constant *Elt = getOperand(0);
1139 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1140 if (!CI || !CI->isAllOnesValue()) return false;
1141 // Then make sure all remaining elements point to the same value.
1142 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1143 if (getOperand(I) != Elt) return false;
1144 }
1145 return true;
1146}
1147
Chris Lattner3462ae32001-12-03 22:26:30 +00001148//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001149//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001150
Chris Lattner189d19f2003-11-21 20:23:48 +00001151namespace llvm {
1152 // ConstantPointerNull does not take extra "value" argument...
1153 template<class ValType>
1154 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1155 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1156 return new ConstantPointerNull(Ty);
1157 }
1158 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001159
Chris Lattner189d19f2003-11-21 20:23:48 +00001160 template<>
1161 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1162 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1163 // Make everyone now use a constant of the new type...
1164 Constant *New = ConstantPointerNull::get(NewTy);
1165 assert(New != OldC && "Didn't replace constant??");
1166 OldC->uncheckedReplaceAllUsesWith(New);
1167 OldC->destroyConstant(); // This constant is now dead, destroy it.
1168 }
1169 };
1170}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001171
Chris Lattner69edc982006-09-28 00:35:06 +00001172static ManagedStatic<ValueMap<char, PointerType,
1173 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001174
Chris Lattner3e650af2004-08-04 04:48:01 +00001175static char getValType(ConstantPointerNull *) {
1176 return 0;
1177}
1178
1179
Chris Lattner3462ae32001-12-03 22:26:30 +00001180ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001181 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001182}
1183
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001184// destroyConstant - Remove the constant from the constant table...
1185//
1186void ConstantPointerNull::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001187 NullPtrConstants->remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001188 destroyConstantImpl();
1189}
1190
1191
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001192//---- UndefValue::get() implementation...
1193//
1194
1195namespace llvm {
1196 // UndefValue does not take extra "value" argument...
1197 template<class ValType>
1198 struct ConstantCreator<UndefValue, Type, ValType> {
1199 static UndefValue *create(const Type *Ty, const ValType &V) {
1200 return new UndefValue(Ty);
1201 }
1202 };
1203
1204 template<>
1205 struct ConvertConstantType<UndefValue, Type> {
1206 static void convert(UndefValue *OldC, const Type *NewTy) {
1207 // Make everyone now use a constant of the new type.
1208 Constant *New = UndefValue::get(NewTy);
1209 assert(New != OldC && "Didn't replace constant??");
1210 OldC->uncheckedReplaceAllUsesWith(New);
1211 OldC->destroyConstant(); // This constant is now dead, destroy it.
1212 }
1213 };
1214}
1215
Chris Lattner69edc982006-09-28 00:35:06 +00001216static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001217
1218static char getValType(UndefValue *) {
1219 return 0;
1220}
1221
1222
1223UndefValue *UndefValue::get(const Type *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001224 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001225}
1226
1227// destroyConstant - Remove the constant from the constant table.
1228//
1229void UndefValue::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001230 UndefValueConstants->remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001231 destroyConstantImpl();
1232}
1233
1234
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001235//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001236//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001237
Reid Spenceree3c9912006-12-04 05:19:50 +00001238struct ExprMapKeyType {
1239 explicit ExprMapKeyType(unsigned opc, std::vector<Constant*> ops,
Reid Spencerdba6aa42006-12-04 18:38:05 +00001240 unsigned short pred = 0) : opcode(opc), predicate(pred), operands(ops) { }
1241 uint16_t opcode;
1242 uint16_t predicate;
Reid Spenceree3c9912006-12-04 05:19:50 +00001243 std::vector<Constant*> operands;
Reid Spenceree3c9912006-12-04 05:19:50 +00001244 bool operator==(const ExprMapKeyType& that) const {
1245 return this->opcode == that.opcode &&
1246 this->predicate == that.predicate &&
1247 this->operands == that.operands;
1248 }
1249 bool operator<(const ExprMapKeyType & that) const {
1250 return this->opcode < that.opcode ||
1251 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1252 (this->opcode == that.opcode && this->predicate == that.predicate &&
1253 this->operands < that.operands);
1254 }
1255
1256 bool operator!=(const ExprMapKeyType& that) const {
1257 return !(*this == that);
1258 }
1259};
Chris Lattner98fa07b2003-05-23 20:03:32 +00001260
Chris Lattner189d19f2003-11-21 20:23:48 +00001261namespace llvm {
1262 template<>
1263 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001264 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1265 unsigned short pred = 0) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001266 if (Instruction::isCast(V.opcode))
1267 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1268 if ((V.opcode >= Instruction::BinaryOpsBegin &&
Reid Spencer2341c222007-02-02 02:16:23 +00001269 V.opcode < Instruction::BinaryOpsEnd))
Reid Spenceree3c9912006-12-04 05:19:50 +00001270 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1271 if (V.opcode == Instruction::Select)
1272 return new SelectConstantExpr(V.operands[0], V.operands[1],
1273 V.operands[2]);
1274 if (V.opcode == Instruction::ExtractElement)
1275 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1276 if (V.opcode == Instruction::InsertElement)
1277 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1278 V.operands[2]);
1279 if (V.opcode == Instruction::ShuffleVector)
1280 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1281 V.operands[2]);
1282 if (V.opcode == Instruction::GetElementPtr) {
1283 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
1284 return new GetElementPtrConstantExpr(V.operands[0], IdxList, Ty);
1285 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001286
Reid Spenceree3c9912006-12-04 05:19:50 +00001287 // The compare instructions are weird. We have to encode the predicate
1288 // value and it is combined with the instruction opcode by multiplying
1289 // the opcode by one hundred. We must decode this to get the predicate.
1290 if (V.opcode == Instruction::ICmp)
1291 return new CompareConstantExpr(Instruction::ICmp, V.predicate,
1292 V.operands[0], V.operands[1]);
1293 if (V.opcode == Instruction::FCmp)
1294 return new CompareConstantExpr(Instruction::FCmp, V.predicate,
1295 V.operands[0], V.operands[1]);
1296 assert(0 && "Invalid ConstantExpr!");
Jeff Cohen9f469632006-12-15 21:47:01 +00001297 return 0;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001298 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001299 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001300
Chris Lattner189d19f2003-11-21 20:23:48 +00001301 template<>
1302 struct ConvertConstantType<ConstantExpr, Type> {
1303 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1304 Constant *New;
1305 switch (OldC->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001306 case Instruction::Trunc:
1307 case Instruction::ZExt:
1308 case Instruction::SExt:
1309 case Instruction::FPTrunc:
1310 case Instruction::FPExt:
1311 case Instruction::UIToFP:
1312 case Instruction::SIToFP:
1313 case Instruction::FPToUI:
1314 case Instruction::FPToSI:
1315 case Instruction::PtrToInt:
1316 case Instruction::IntToPtr:
1317 case Instruction::BitCast:
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001318 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
1319 NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001320 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001321 case Instruction::Select:
1322 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1323 OldC->getOperand(1),
1324 OldC->getOperand(2));
1325 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001326 default:
1327 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001328 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner189d19f2003-11-21 20:23:48 +00001329 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1330 OldC->getOperand(1));
1331 break;
1332 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001333 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001334 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
Chris Lattner302116a2007-01-31 04:40:28 +00001335 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
1336 &Idx[0], Idx.size());
Chris Lattner189d19f2003-11-21 20:23:48 +00001337 break;
1338 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001339
Chris Lattner189d19f2003-11-21 20:23:48 +00001340 assert(New != OldC && "Didn't replace constant??");
1341 OldC->uncheckedReplaceAllUsesWith(New);
1342 OldC->destroyConstant(); // This constant is now dead, destroy it.
1343 }
1344 };
1345} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001346
1347
Chris Lattner3e650af2004-08-04 04:48:01 +00001348static ExprMapKeyType getValType(ConstantExpr *CE) {
1349 std::vector<Constant*> Operands;
1350 Operands.reserve(CE->getNumOperands());
1351 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1352 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spenceree3c9912006-12-04 05:19:50 +00001353 return ExprMapKeyType(CE->getOpcode(), Operands,
1354 CE->isCompare() ? CE->getPredicate() : 0);
Chris Lattner3e650af2004-08-04 04:48:01 +00001355}
1356
Chris Lattner69edc982006-09-28 00:35:06 +00001357static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1358 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001359
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001360/// This is a utility function to handle folding of casts and lookup of the
1361/// cast in the ExprConstants map. It is usedby the various get* methods below.
1362static inline Constant *getFoldedCast(
1363 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001364 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001365 // Fold a few common cases
1366 if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
1367 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001368
Vikram S. Adve4c485332002-07-15 18:19:33 +00001369 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001370 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00001371 ExprMapKeyType Key(opc, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001372 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001373}
Reid Spencerf37dc652006-12-05 19:14:13 +00001374
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001375Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
1376 Instruction::CastOps opc = Instruction::CastOps(oc);
1377 assert(Instruction::isCast(opc) && "opcode out of range");
1378 assert(C && Ty && "Null arguments to getCast");
1379 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1380
1381 switch (opc) {
1382 default:
1383 assert(0 && "Invalid cast opcode");
1384 break;
1385 case Instruction::Trunc: return getTrunc(C, Ty);
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001386 case Instruction::ZExt: return getZExt(C, Ty);
1387 case Instruction::SExt: return getSExt(C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001388 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1389 case Instruction::FPExt: return getFPExtend(C, Ty);
1390 case Instruction::UIToFP: return getUIToFP(C, Ty);
1391 case Instruction::SIToFP: return getSIToFP(C, Ty);
1392 case Instruction::FPToUI: return getFPToUI(C, Ty);
1393 case Instruction::FPToSI: return getFPToSI(C, Ty);
1394 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1395 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1396 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001397 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001398 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00001399}
1400
Reid Spencer5c140882006-12-04 20:17:56 +00001401Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
1402 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1403 return getCast(Instruction::BitCast, C, Ty);
1404 return getCast(Instruction::ZExt, C, Ty);
1405}
1406
1407Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
1408 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1409 return getCast(Instruction::BitCast, C, Ty);
1410 return getCast(Instruction::SExt, C, Ty);
1411}
1412
1413Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
1414 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1415 return getCast(Instruction::BitCast, C, Ty);
1416 return getCast(Instruction::Trunc, C, Ty);
1417}
1418
Reid Spencerbc245a02006-12-05 03:25:26 +00001419Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
1420 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001421 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001422
Chris Lattner03c49532007-01-15 02:27:26 +00001423 if (Ty->isInteger())
Reid Spencerbc245a02006-12-05 03:25:26 +00001424 return getCast(Instruction::PtrToInt, S, Ty);
1425 return getCast(Instruction::BitCast, S, Ty);
1426}
1427
Reid Spencer56521c42006-12-12 00:51:07 +00001428Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1429 bool isSigned) {
Chris Lattner03c49532007-01-15 02:27:26 +00001430 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer56521c42006-12-12 00:51:07 +00001431 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1432 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1433 Instruction::CastOps opcode =
1434 (SrcBits == DstBits ? Instruction::BitCast :
1435 (SrcBits > DstBits ? Instruction::Trunc :
1436 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1437 return getCast(opcode, C, Ty);
1438}
1439
1440Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
1441 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1442 "Invalid cast");
1443 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1444 unsigned DstBits = Ty->getPrimitiveSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001445 if (SrcBits == DstBits)
1446 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001447 Instruction::CastOps opcode =
Reid Spencerca104e82006-12-12 05:38:50 +00001448 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001449 return getCast(opcode, C, Ty);
1450}
1451
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001452Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001453 assert(C->getType()->isInteger() && "Trunc operand must be integer");
1454 assert(Ty->isInteger() && "Trunc produces only integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001455 assert(C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&&
1456 "SrcTy must be larger than DestTy for Trunc!");
1457
1458 return getFoldedCast(Instruction::Trunc, C, Ty);
1459}
1460
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001461Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001462 assert(C->getType()->isInteger() && "SEXt operand must be integral");
1463 assert(Ty->isInteger() && "SExt produces only integer");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001464 assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1465 "SrcTy must be smaller than DestTy for SExt!");
1466
1467 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerdd284742004-04-04 23:20:30 +00001468}
1469
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001470Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001471 assert(C->getType()->isInteger() && "ZEXt operand must be integral");
1472 assert(Ty->isInteger() && "ZExt produces only integer");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001473 assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1474 "SrcTy must be smaller than DestTy for ZExt!");
1475
1476 return getFoldedCast(Instruction::ZExt, C, Ty);
1477}
1478
1479Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
1480 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1481 C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&&
1482 "This is an illegal floating point truncation!");
1483 return getFoldedCast(Instruction::FPTrunc, C, Ty);
1484}
1485
1486Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
1487 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1488 C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1489 "This is an illegal floating point extension!");
1490 return getFoldedCast(Instruction::FPExt, C, Ty);
1491}
1492
1493Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001494 assert(C->getType()->isInteger() && Ty->isFloatingPoint() &&
Reid Spencer2546b762007-01-26 07:37:34 +00001495 "This is an illegal i32 to floating point cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001496 return getFoldedCast(Instruction::UIToFP, C, Ty);
1497}
1498
1499Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001500 assert(C->getType()->isInteger() && Ty->isFloatingPoint() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001501 "This is an illegal sint to floating point cast!");
1502 return getFoldedCast(Instruction::SIToFP, C, Ty);
1503}
1504
1505Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001506 assert(C->getType()->isFloatingPoint() && Ty->isInteger() &&
Reid Spencer2546b762007-01-26 07:37:34 +00001507 "This is an illegal floating point to i32 cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001508 return getFoldedCast(Instruction::FPToUI, C, Ty);
1509}
1510
1511Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001512 assert(C->getType()->isFloatingPoint() && Ty->isInteger() &&
Reid Spencer2546b762007-01-26 07:37:34 +00001513 "This is an illegal floating point to i32 cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001514 return getFoldedCast(Instruction::FPToSI, C, Ty);
1515}
1516
1517Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
1518 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner03c49532007-01-15 02:27:26 +00001519 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001520 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
1521}
1522
1523Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner03c49532007-01-15 02:27:26 +00001524 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001525 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
1526 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
1527}
1528
1529Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
1530 // BitCast implies a no-op cast of type only. No bits change. However, you
1531 // can't cast pointers to anything but pointers.
1532 const Type *SrcTy = C->getType();
1533 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer5c140882006-12-04 20:17:56 +00001534 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001535
1536 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
1537 // or nonptr->ptr). For all the other types, the cast is okay if source and
1538 // destination bit widths are identical.
1539 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1540 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Reid Spencer5c140882006-12-04 20:17:56 +00001541 assert(SrcBitSize == DstBitSize && "BitCast requies types of same width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001542 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerdd284742004-04-04 23:20:30 +00001543}
1544
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001545Constant *ConstantExpr::getSizeOf(const Type *Ty) {
Chris Lattneracc4e542004-12-13 19:48:51 +00001546 // sizeof is implemented as: (ulong) gep (Ty*)null, 1
Chris Lattnerb5d70302007-02-19 20:01:23 +00001547 Constant *GEPIdx = ConstantInt::get(Type::Int32Ty, 1);
1548 Constant *GEP =
1549 getGetElementPtr(getNullValue(PointerType::get(Ty)), &GEPIdx, 1);
1550 return getCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
Alkis Evlogimenos9160d5f2005-03-19 11:40:31 +00001551}
1552
Chris Lattnerb50d1352003-10-05 00:17:43 +00001553Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Reid Spencera009d0d2006-12-04 21:35:24 +00001554 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001555 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00001556 assert(Opcode >= Instruction::BinaryOpsBegin &&
1557 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001558 "Invalid opcode in binary constant expression");
1559 assert(C1->getType() == C2->getType() &&
1560 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001561
Reid Spencer542964f2007-01-11 18:21:29 +00001562 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Chris Lattnerb50d1352003-10-05 00:17:43 +00001563 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1564 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001565
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001566 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001567 ExprMapKeyType Key(Opcode, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001568 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001569}
1570
Reid Spencer266e42b2006-12-23 06:05:41 +00001571Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Reid Spencera009d0d2006-12-04 21:35:24 +00001572 Constant *C1, Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001573 switch (predicate) {
1574 default: assert(0 && "Invalid CmpInst predicate");
1575 case FCmpInst::FCMP_FALSE: case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_OGT:
1576 case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OLE:
1577 case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_ORD: case FCmpInst::FCMP_UNO:
1578 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UGT: case FCmpInst::FCMP_UGE:
1579 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_ULE: case FCmpInst::FCMP_UNE:
1580 case FCmpInst::FCMP_TRUE:
1581 return getFCmp(predicate, C1, C2);
1582 case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_UGT:
1583 case ICmpInst::ICMP_UGE: case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE:
1584 case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_SGE: case ICmpInst::ICMP_SLT:
1585 case ICmpInst::ICMP_SLE:
1586 return getICmp(predicate, C1, C2);
1587 }
Reid Spencera009d0d2006-12-04 21:35:24 +00001588}
1589
1590Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001591#ifndef NDEBUG
1592 switch (Opcode) {
Reid Spencer7eb55b32006-11-02 01:53:59 +00001593 case Instruction::Add:
1594 case Instruction::Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001595 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001596 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattner03c49532007-01-15 02:27:26 +00001597 assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001598 isa<VectorType>(C1->getType())) &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001599 "Tried to create an arithmetic operation on a non-arithmetic type!");
1600 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001601 case Instruction::UDiv:
1602 case Instruction::SDiv:
1603 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001604 assert((C1->getType()->isInteger() || (isa<VectorType>(C1->getType()) &&
1605 cast<VectorType>(C1->getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001606 "Tried to create an arithmetic operation on a non-arithmetic type!");
1607 break;
1608 case Instruction::FDiv:
1609 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001610 assert((C1->getType()->isFloatingPoint() || (isa<VectorType>(C1->getType())
1611 && cast<VectorType>(C1->getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001612 && "Tried to create an arithmetic operation on a non-arithmetic type!");
1613 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001614 case Instruction::URem:
1615 case Instruction::SRem:
1616 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001617 assert((C1->getType()->isInteger() || (isa<VectorType>(C1->getType()) &&
1618 cast<VectorType>(C1->getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001619 "Tried to create an arithmetic operation on a non-arithmetic type!");
1620 break;
1621 case Instruction::FRem:
1622 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001623 assert((C1->getType()->isFloatingPoint() || (isa<VectorType>(C1->getType())
1624 && cast<VectorType>(C1->getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001625 && "Tried to create an arithmetic operation on a non-arithmetic type!");
1626 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001627 case Instruction::And:
1628 case Instruction::Or:
1629 case Instruction::Xor:
1630 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001631 assert((C1->getType()->isInteger() || isa<VectorType>(C1->getType())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001632 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001633 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001634 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001635 case Instruction::LShr:
1636 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00001637 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattner03c49532007-01-15 02:27:26 +00001638 assert(C1->getType()->isInteger() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001639 "Tried to create a shift operation on a non-integer type!");
1640 break;
1641 default:
1642 break;
1643 }
1644#endif
1645
Reid Spencera009d0d2006-12-04 21:35:24 +00001646 return getTy(C1->getType(), Opcode, C1, C2);
1647}
1648
Reid Spencer266e42b2006-12-23 06:05:41 +00001649Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00001650 Constant *C1, Constant *C2) {
1651 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001652 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00001653}
1654
Chris Lattner6e415c02004-03-12 05:54:04 +00001655Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
1656 Constant *V1, Constant *V2) {
Reid Spencer2546b762007-01-26 07:37:34 +00001657 assert(C->getType() == Type::Int1Ty && "Select condition must be i1!");
Chris Lattner6e415c02004-03-12 05:54:04 +00001658 assert(V1->getType() == V2->getType() && "Select value types must match!");
1659 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
1660
1661 if (ReqTy == V1->getType())
1662 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1663 return SC; // Fold common cases
1664
1665 std::vector<Constant*> argVec(3, C);
1666 argVec[1] = V1;
1667 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00001668 ExprMapKeyType Key(Instruction::Select, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001669 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00001670}
1671
Chris Lattnerb50d1352003-10-05 00:17:43 +00001672Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00001673 Value* const *Idxs,
1674 unsigned NumIdx) {
1675 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs, NumIdx, true) &&
Chris Lattner04b60fe2004-02-16 20:46:13 +00001676 "GEP indices invalid!");
1677
Chris Lattner302116a2007-01-31 04:40:28 +00001678 if (Constant *FC = ConstantFoldGetElementPtr(C, (Constant**)Idxs, NumIdx))
Chris Lattneracdbe712003-04-17 19:24:48 +00001679 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001680
Chris Lattnerb50d1352003-10-05 00:17:43 +00001681 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001682 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001683 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00001684 std::vector<Constant*> ArgVec;
Chris Lattner302116a2007-01-31 04:40:28 +00001685 ArgVec.reserve(NumIdx+1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001686 ArgVec.push_back(C);
Chris Lattner302116a2007-01-31 04:40:28 +00001687 for (unsigned i = 0; i != NumIdx; ++i)
1688 ArgVec.push_back(cast<Constant>(Idxs[i]));
1689 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001690 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001691}
1692
Chris Lattner302116a2007-01-31 04:40:28 +00001693Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
1694 unsigned NumIdx) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00001695 // Get the result type of the getelementptr!
Chris Lattner302116a2007-01-31 04:40:28 +00001696 const Type *Ty =
1697 GetElementPtrInst::getIndexedType(C->getType(), Idxs, NumIdx, true);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001698 assert(Ty && "GEP indices invalid!");
Chris Lattner302116a2007-01-31 04:40:28 +00001699 return getGetElementPtrTy(PointerType::get(Ty), C, Idxs, NumIdx);
Chris Lattner13128ab2004-10-11 22:52:25 +00001700}
1701
Chris Lattner302116a2007-01-31 04:40:28 +00001702Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
1703 unsigned NumIdx) {
1704 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001705}
1706
Chris Lattner302116a2007-01-31 04:40:28 +00001707
Reid Spenceree3c9912006-12-04 05:19:50 +00001708Constant *
1709ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1710 assert(LHS->getType() == RHS->getType());
1711 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
1712 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
1713
Reid Spencer266e42b2006-12-23 06:05:41 +00001714 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001715 return FC; // Fold a few common cases...
1716
1717 // Look up the constant in the table first to ensure uniqueness
1718 std::vector<Constant*> ArgVec;
1719 ArgVec.push_back(LHS);
1720 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001721 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001722 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Reid Spencer542964f2007-01-11 18:21:29 +00001723 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001724}
1725
1726Constant *
1727ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1728 assert(LHS->getType() == RHS->getType());
1729 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
1730
Reid Spencer266e42b2006-12-23 06:05:41 +00001731 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001732 return FC; // Fold a few common cases...
1733
1734 // Look up the constant in the table first to ensure uniqueness
1735 std::vector<Constant*> ArgVec;
1736 ArgVec.push_back(LHS);
1737 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001738 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001739 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Reid Spencer542964f2007-01-11 18:21:29 +00001740 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001741}
1742
Robert Bocchino23004482006-01-10 19:05:34 +00001743Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
1744 Constant *Idx) {
Robert Bocchinode7f1c92006-01-10 20:03:46 +00001745 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
1746 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00001747 // Look up the constant in the table first to ensure uniqueness
1748 std::vector<Constant*> ArgVec(1, Val);
1749 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001750 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001751 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00001752}
1753
1754Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001755 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001756 "Tried to create extractelement operation on non-vector type!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001757 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00001758 "Extractelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001759 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +00001760 Val, Idx);
1761}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001762
Robert Bocchinoca27f032006-01-17 20:07:22 +00001763Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
1764 Constant *Elt, Constant *Idx) {
1765 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
1766 return FC; // Fold a few common cases...
1767 // Look up the constant in the table first to ensure uniqueness
1768 std::vector<Constant*> ArgVec(1, Val);
1769 ArgVec.push_back(Elt);
1770 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001771 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001772 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001773}
1774
1775Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1776 Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001777 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001778 "Tried to create insertelement operation on non-vector type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001779 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoca27f032006-01-17 20:07:22 +00001780 && "Insertelement types must match!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001781 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00001782 "Insertelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001783 return getInsertElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchinoca27f032006-01-17 20:07:22 +00001784 Val, Elt, Idx);
1785}
1786
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001787Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
1788 Constant *V2, Constant *Mask) {
1789 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
1790 return FC; // Fold a few common cases...
1791 // Look up the constant in the table first to ensure uniqueness
1792 std::vector<Constant*> ArgVec(1, V1);
1793 ArgVec.push_back(V2);
1794 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00001795 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001796 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001797}
1798
1799Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
1800 Constant *Mask) {
1801 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
1802 "Invalid shuffle vector constant expr operands!");
1803 return getShuffleVectorTy(V1->getType(), V1, V2, Mask);
1804}
1805
Reid Spencer2eadb532007-01-21 00:29:26 +00001806Constant *ConstantExpr::getZeroValueForNegationExpr(const Type *Ty) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001807 if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
Reid Spencer6598ca82007-01-21 02:29:10 +00001808 if (PTy->getElementType()->isFloatingPoint()) {
1809 std::vector<Constant*> zeros(PTy->getNumElements(),
1810 ConstantFP::get(PTy->getElementType(),-0.0));
Reid Spencerd84d35b2007-02-15 02:26:10 +00001811 return ConstantVector::get(PTy, zeros);
Reid Spencer6598ca82007-01-21 02:29:10 +00001812 }
Reid Spencer2eadb532007-01-21 00:29:26 +00001813
1814 if (Ty->isFloatingPoint())
1815 return ConstantFP::get(Ty, -0.0);
1816
1817 return Constant::getNullValue(Ty);
1818}
1819
Vikram S. Adve4c485332002-07-15 18:19:33 +00001820// destroyConstant - Remove the constant from the constant table...
1821//
1822void ConstantExpr::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001823 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001824 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001825}
1826
Chris Lattner3cd8c562002-07-30 18:54:25 +00001827const char *ConstantExpr::getOpcodeName() const {
1828 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001829}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00001830
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001831//===----------------------------------------------------------------------===//
1832// replaceUsesOfWithOnConstant implementations
1833
1834void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001835 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001836 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00001837 Constant *ToC = cast<Constant>(To);
Chris Lattnerdff59112005-10-04 18:47:09 +00001838
1839 unsigned OperandToUpdate = U-OperandList;
1840 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1841
Jim Laskeyc03caef2006-07-17 17:38:29 +00001842 std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnerb64419a2005-10-03 22:51:37 +00001843 Lookup.first.first = getType();
1844 Lookup.second = this;
Chris Lattnerdff59112005-10-04 18:47:09 +00001845
Chris Lattnerb64419a2005-10-03 22:51:37 +00001846 std::vector<Constant*> &Values = Lookup.first.second;
1847 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattnerdff59112005-10-04 18:47:09 +00001848
Chris Lattner8760ec72005-10-04 01:17:50 +00001849 // Fill values with the modified operands of the constant array. Also,
1850 // compute whether this turns into an all-zeros array.
Chris Lattnerdff59112005-10-04 18:47:09 +00001851 bool isAllZeros = false;
1852 if (!ToC->isNullValue()) {
1853 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1854 Values.push_back(cast<Constant>(O->get()));
1855 } else {
1856 isAllZeros = true;
1857 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1858 Constant *Val = cast<Constant>(O->get());
1859 Values.push_back(Val);
1860 if (isAllZeros) isAllZeros = Val->isNullValue();
1861 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001862 }
Chris Lattnerdff59112005-10-04 18:47:09 +00001863 Values[OperandToUpdate] = ToC;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001864
Chris Lattnerb64419a2005-10-03 22:51:37 +00001865 Constant *Replacement = 0;
1866 if (isAllZeros) {
1867 Replacement = ConstantAggregateZero::get(getType());
1868 } else {
1869 // Check to see if we have this array type already.
1870 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00001871 ArrayConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00001872 ArrayConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001873
1874 if (Exists) {
1875 Replacement = I->second;
1876 } else {
1877 // Okay, the new shape doesn't exist in the system yet. Instead of
1878 // creating a new constant array, inserting it, replaceallusesof'ing the
1879 // old with the new, then deleting the old... just update the current one
1880 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00001881 ArrayConstants->MoveConstantToNewSlot(this, I);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001882
Chris Lattnerdff59112005-10-04 18:47:09 +00001883 // Update to the new value.
1884 setOperand(OperandToUpdate, ToC);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001885 return;
1886 }
1887 }
1888
1889 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001890 assert(Replacement != this && "I didn't contain From!");
1891
Chris Lattner7a1450d2005-10-04 18:13:04 +00001892 // Everyone using this now uses the replacement.
1893 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001894
1895 // Delete the old constant!
1896 destroyConstant();
1897}
1898
1899void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001900 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001901 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00001902 Constant *ToC = cast<Constant>(To);
1903
Chris Lattnerdff59112005-10-04 18:47:09 +00001904 unsigned OperandToUpdate = U-OperandList;
1905 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1906
Jim Laskeyc03caef2006-07-17 17:38:29 +00001907 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattner8760ec72005-10-04 01:17:50 +00001908 Lookup.first.first = getType();
1909 Lookup.second = this;
1910 std::vector<Constant*> &Values = Lookup.first.second;
1911 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001912
Chris Lattnerdff59112005-10-04 18:47:09 +00001913
Chris Lattner8760ec72005-10-04 01:17:50 +00001914 // Fill values with the modified operands of the constant struct. Also,
1915 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00001916 bool isAllZeros = false;
1917 if (!ToC->isNullValue()) {
1918 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1919 Values.push_back(cast<Constant>(O->get()));
1920 } else {
1921 isAllZeros = true;
1922 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1923 Constant *Val = cast<Constant>(O->get());
1924 Values.push_back(Val);
1925 if (isAllZeros) isAllZeros = Val->isNullValue();
1926 }
Chris Lattner8760ec72005-10-04 01:17:50 +00001927 }
Chris Lattnerdff59112005-10-04 18:47:09 +00001928 Values[OperandToUpdate] = ToC;
1929
Chris Lattner8760ec72005-10-04 01:17:50 +00001930 Constant *Replacement = 0;
1931 if (isAllZeros) {
1932 Replacement = ConstantAggregateZero::get(getType());
1933 } else {
1934 // Check to see if we have this array type already.
1935 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00001936 StructConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00001937 StructConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00001938
1939 if (Exists) {
1940 Replacement = I->second;
1941 } else {
1942 // Okay, the new shape doesn't exist in the system yet. Instead of
1943 // creating a new constant struct, inserting it, replaceallusesof'ing the
1944 // old with the new, then deleting the old... just update the current one
1945 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00001946 StructConstants->MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00001947
Chris Lattnerdff59112005-10-04 18:47:09 +00001948 // Update to the new value.
1949 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00001950 return;
1951 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001952 }
1953
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001954 assert(Replacement != this && "I didn't contain From!");
1955
Chris Lattner7a1450d2005-10-04 18:13:04 +00001956 // Everyone using this now uses the replacement.
1957 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001958
1959 // Delete the old constant!
1960 destroyConstant();
1961}
1962
Reid Spencerd84d35b2007-02-15 02:26:10 +00001963void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001964 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001965 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
1966
1967 std::vector<Constant*> Values;
1968 Values.reserve(getNumOperands()); // Build replacement array...
1969 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1970 Constant *Val = getOperand(i);
1971 if (Val == From) Val = cast<Constant>(To);
1972 Values.push_back(Val);
1973 }
1974
Reid Spencerd84d35b2007-02-15 02:26:10 +00001975 Constant *Replacement = ConstantVector::get(getType(), Values);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001976 assert(Replacement != this && "I didn't contain From!");
1977
Chris Lattner7a1450d2005-10-04 18:13:04 +00001978 // Everyone using this now uses the replacement.
1979 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001980
1981 // Delete the old constant!
1982 destroyConstant();
1983}
1984
1985void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001986 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001987 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
1988 Constant *To = cast<Constant>(ToV);
1989
1990 Constant *Replacement = 0;
1991 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerb5d70302007-02-19 20:01:23 +00001992 SmallVector<Constant*, 8> Indices;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001993 Constant *Pointer = getOperand(0);
1994 Indices.reserve(getNumOperands()-1);
1995 if (Pointer == From) Pointer = To;
1996
1997 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1998 Constant *Val = getOperand(i);
1999 if (Val == From) Val = To;
2000 Indices.push_back(Val);
2001 }
Chris Lattnerb5d70302007-02-19 20:01:23 +00002002 Replacement = ConstantExpr::getGetElementPtr(Pointer,
2003 &Indices[0], Indices.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002004 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002005 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002006 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002007 } else if (getOpcode() == Instruction::Select) {
2008 Constant *C1 = getOperand(0);
2009 Constant *C2 = getOperand(1);
2010 Constant *C3 = getOperand(2);
2011 if (C1 == From) C1 = To;
2012 if (C2 == From) C2 = To;
2013 if (C3 == From) C3 = To;
2014 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00002015 } else if (getOpcode() == Instruction::ExtractElement) {
2016 Constant *C1 = getOperand(0);
2017 Constant *C2 = getOperand(1);
2018 if (C1 == From) C1 = To;
2019 if (C2 == From) C2 = To;
2020 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00002021 } else if (getOpcode() == Instruction::InsertElement) {
2022 Constant *C1 = getOperand(0);
2023 Constant *C2 = getOperand(1);
2024 Constant *C3 = getOperand(1);
2025 if (C1 == From) C1 = To;
2026 if (C2 == From) C2 = To;
2027 if (C3 == From) C3 = To;
2028 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2029 } else if (getOpcode() == Instruction::ShuffleVector) {
2030 Constant *C1 = getOperand(0);
2031 Constant *C2 = getOperand(1);
2032 Constant *C3 = getOperand(2);
2033 if (C1 == From) C1 = To;
2034 if (C2 == From) C2 = To;
2035 if (C3 == From) C3 = To;
2036 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00002037 } else if (isCompare()) {
2038 Constant *C1 = getOperand(0);
2039 Constant *C2 = getOperand(1);
2040 if (C1 == From) C1 = To;
2041 if (C2 == From) C2 = To;
2042 if (getOpcode() == Instruction::ICmp)
2043 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
2044 else
2045 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002046 } else if (getNumOperands() == 2) {
2047 Constant *C1 = getOperand(0);
2048 Constant *C2 = getOperand(1);
2049 if (C1 == From) C1 = To;
2050 if (C2 == From) C2 = To;
2051 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2052 } else {
2053 assert(0 && "Unknown ConstantExpr type!");
2054 return;
2055 }
2056
2057 assert(Replacement != this && "I didn't contain From!");
2058
Chris Lattner7a1450d2005-10-04 18:13:04 +00002059 // Everyone using this now uses the replacement.
2060 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002061
2062 // Delete the old constant!
2063 destroyConstant();
2064}
2065
2066
Jim Laskey2698f0d2006-03-08 18:11:07 +00002067/// getStringValue - Turn an LLVM constant pointer that eventually points to a
2068/// global into a string value. Return an empty string if we can't do it.
Evan Cheng38280c02006-03-10 23:52:03 +00002069/// Parameter Chop determines if the result is chopped at the first null
2070/// terminator.
Jim Laskey2698f0d2006-03-08 18:11:07 +00002071///
Evan Cheng38280c02006-03-10 23:52:03 +00002072std::string Constant::getStringValue(bool Chop, unsigned Offset) {
Jim Laskey2698f0d2006-03-08 18:11:07 +00002073 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(this)) {
2074 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
2075 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
2076 if (Init->isString()) {
2077 std::string Result = Init->getAsString();
2078 if (Offset < Result.size()) {
2079 // If we are pointing INTO The string, erase the beginning...
2080 Result.erase(Result.begin(), Result.begin()+Offset);
2081
2082 // Take off the null terminator, and any string fragments after it.
Evan Cheng38280c02006-03-10 23:52:03 +00002083 if (Chop) {
2084 std::string::size_type NullPos = Result.find_first_of((char)0);
2085 if (NullPos != std::string::npos)
2086 Result.erase(Result.begin()+NullPos, Result.end());
2087 }
Jim Laskey2698f0d2006-03-08 18:11:07 +00002088 return Result;
2089 }
2090 }
2091 }
2092 } else if (Constant *C = dyn_cast<Constant>(this)) {
2093 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
Evan Cheng2c5e5302006-03-11 00:13:10 +00002094 return GV->getStringValue(Chop, Offset);
Jim Laskey2698f0d2006-03-08 18:11:07 +00002095 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
2096 if (CE->getOpcode() == Instruction::GetElementPtr) {
2097 // Turn a gep into the specified offset.
2098 if (CE->getNumOperands() == 3 &&
2099 cast<Constant>(CE->getOperand(1))->isNullValue() &&
2100 isa<ConstantInt>(CE->getOperand(2))) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00002101 Offset += cast<ConstantInt>(CE->getOperand(2))->getZExtValue();
Evan Cheng2c5e5302006-03-11 00:13:10 +00002102 return CE->getOperand(0)->getStringValue(Chop, Offset);
Jim Laskey2698f0d2006-03-08 18:11:07 +00002103 }
2104 }
2105 }
2106 }
2107 return "";
2108}