blob: 58fe8882c8afd4e1c74881657301c873d138ac0c [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
Zhou Sheng75b871f2007-01-11 12:24:14 +0000143ConstantInt::ConstantInt(bool V)
Reid Spencer542964f2007-01-11 18:21:29 +0000144 : Constant(Type::Int1Ty, ConstantIntVal, 0, 0), Val(uint64_t(V)) {
Chris Lattner265eb642004-06-21 12:12:12 +0000145}
146
Reid Spencere0fc4df2006-10-20 07:07:24 +0000147ConstantInt::ConstantInt(const Type *Ty, uint64_t V)
Reid Spencer542964f2007-01-11 18:21:29 +0000148 : Constant(Ty, ConstantIntVal, 0, 0), Val(Ty == Type::Int1Ty ? bool(V) : V) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000149}
150
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000151ConstantFP::ConstantFP(const Type *Ty, double V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000152 : Constant(Ty, ConstantFPVal, 0, 0) {
Chris Lattner9655e542001-07-20 19:16:02 +0000153 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000154 Val = V;
155}
156
Chris Lattner3462ae32001-12-03 22:26:30 +0000157ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000158 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000159 : Constant(T, ConstantArrayVal, new Use[V.size()], V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000160 assert(V.size() == T->getNumElements() &&
161 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000162 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000163 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
164 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000165 Constant *C = *I;
166 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000167 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000168 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000169 "Initializer for array element doesn't match array element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000170 OL->init(C, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000171 }
172}
173
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000174ConstantArray::~ConstantArray() {
175 delete [] OperandList;
176}
177
Chris Lattner3462ae32001-12-03 22:26:30 +0000178ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000179 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000180 : Constant(T, ConstantStructVal, new Use[V.size()], V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000181 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000182 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000183 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000184 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
185 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000186 Constant *C = *I;
187 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000188 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000189 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000190 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000191 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000192 "Initializer for struct element doesn't match struct element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000193 OL->init(C, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000194 }
195}
196
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000197ConstantStruct::~ConstantStruct() {
198 delete [] OperandList;
199}
200
201
Reid Spencerd84d35b2007-02-15 02:26:10 +0000202ConstantVector::ConstantVector(const VectorType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000203 const std::vector<Constant*> &V)
Reid Spencerd84d35b2007-02-15 02:26:10 +0000204 : Constant(T, ConstantVectorVal, new Use[V.size()], V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000205 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000206 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
207 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000208 Constant *C = *I;
209 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000210 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000211 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000212 "Initializer for packed element doesn't match packed element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000213 OL->init(C, this);
Brian Gaeke02209042004-08-20 06:00:58 +0000214 }
215}
216
Reid Spencerd84d35b2007-02-15 02:26:10 +0000217ConstantVector::~ConstantVector() {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000218 delete [] OperandList;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000219}
220
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000221// We declare several classes private to this file, so use an anonymous
222// namespace
223namespace {
224
225/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
226/// behind the scenes to implement unary constant exprs.
227class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
228 Use Op;
229public:
230 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
231 : ConstantExpr(Ty, Opcode, &Op, 1), Op(C, this) {}
232};
233
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000234/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
235/// behind the scenes to implement binary constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000236class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000237 Use Ops[2];
238public:
239 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Reid Spencer266e42b2006-12-23 06:05:41 +0000240 : ConstantExpr(C1->getType(), Opcode, Ops, 2) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000241 Ops[0].init(C1, this);
242 Ops[1].init(C2, this);
243 }
244};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000245
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000246/// SelectConstantExpr - This class is private to Constants.cpp, and is used
247/// behind the scenes to implement select constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000248class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000249 Use Ops[3];
250public:
251 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
252 : ConstantExpr(C2->getType(), Instruction::Select, Ops, 3) {
253 Ops[0].init(C1, this);
254 Ops[1].init(C2, this);
255 Ops[2].init(C3, this);
256 }
257};
258
Robert Bocchinoca27f032006-01-17 20:07:22 +0000259/// ExtractElementConstantExpr - This class is private to
260/// Constants.cpp, and is used behind the scenes to implement
261/// extractelement constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000262class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Robert Bocchino23004482006-01-10 19:05:34 +0000263 Use Ops[2];
264public:
265 ExtractElementConstantExpr(Constant *C1, Constant *C2)
Reid Spencerd84d35b2007-02-15 02:26:10 +0000266 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +0000267 Instruction::ExtractElement, Ops, 2) {
268 Ops[0].init(C1, this);
269 Ops[1].init(C2, this);
270 }
271};
272
Robert Bocchinoca27f032006-01-17 20:07:22 +0000273/// InsertElementConstantExpr - This class is private to
274/// Constants.cpp, and is used behind the scenes to implement
275/// insertelement constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000276class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Robert Bocchinoca27f032006-01-17 20:07:22 +0000277 Use Ops[3];
278public:
279 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
280 : ConstantExpr(C1->getType(), Instruction::InsertElement,
281 Ops, 3) {
282 Ops[0].init(C1, this);
283 Ops[1].init(C2, this);
284 Ops[2].init(C3, this);
285 }
286};
287
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000288/// ShuffleVectorConstantExpr - This class is private to
289/// Constants.cpp, and is used behind the scenes to implement
290/// shufflevector constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000291class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000292 Use Ops[3];
293public:
294 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
295 : ConstantExpr(C1->getType(), Instruction::ShuffleVector,
296 Ops, 3) {
297 Ops[0].init(C1, this);
298 Ops[1].init(C2, this);
299 Ops[2].init(C3, this);
300 }
301};
302
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000303/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
304/// used behind the scenes to implement getelementpr constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000305struct VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000306 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
307 const Type *DestTy)
308 : ConstantExpr(DestTy, Instruction::GetElementPtr,
309 new Use[IdxList.size()+1], IdxList.size()+1) {
310 OperandList[0].init(C, this);
311 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
312 OperandList[i+1].init(IdxList[i], this);
313 }
314 ~GetElementPtrConstantExpr() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000315 delete [] OperandList;
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000316 }
317};
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000318
319// CompareConstantExpr - This class is private to Constants.cpp, and is used
320// behind the scenes to implement ICmp and FCmp constant expressions. This is
321// needed in order to store the predicate value for these instructions.
322struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr {
323 unsigned short predicate;
324 Use Ops[2];
325 CompareConstantExpr(Instruction::OtherOps opc, unsigned short pred,
326 Constant* LHS, Constant* RHS)
Reid Spencer542964f2007-01-11 18:21:29 +0000327 : ConstantExpr(Type::Int1Ty, opc, Ops, 2), predicate(pred) {
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000328 OperandList[0].init(LHS, this);
329 OperandList[1].init(RHS, this);
330 }
331};
332
333} // end anonymous namespace
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000334
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000335
336// Utility function for determining if a ConstantExpr is a CastOp or not. This
337// can't be inline because we don't want to #include Instruction.h into
338// Constant.h
339bool ConstantExpr::isCast() const {
340 return Instruction::isCast(getOpcode());
341}
342
Reid Spenceree3c9912006-12-04 05:19:50 +0000343bool ConstantExpr::isCompare() const {
344 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
345}
346
Chris Lattner817175f2004-03-29 02:37:53 +0000347/// ConstantExpr::get* - Return some common constants without having to
348/// specify the full Instruction::OPCODE identifier.
349///
350Constant *ConstantExpr::getNeg(Constant *C) {
Reid Spencer2eadb532007-01-21 00:29:26 +0000351 return get(Instruction::Sub,
352 ConstantExpr::getZeroValueForNegationExpr(C->getType()),
353 C);
Chris Lattner817175f2004-03-29 02:37:53 +0000354}
355Constant *ConstantExpr::getNot(Constant *C) {
Zhou Sheng75b871f2007-01-11 12:24:14 +0000356 assert(isa<ConstantInt>(C) && "Cannot NOT a nonintegral type!");
Chris Lattner817175f2004-03-29 02:37:53 +0000357 return get(Instruction::Xor, C,
Zhou Sheng75b871f2007-01-11 12:24:14 +0000358 ConstantInt::getAllOnesValue(C->getType()));
Chris Lattner817175f2004-03-29 02:37:53 +0000359}
360Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
361 return get(Instruction::Add, C1, C2);
362}
363Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
364 return get(Instruction::Sub, C1, C2);
365}
366Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
367 return get(Instruction::Mul, C1, C2);
368}
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000369Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2) {
370 return get(Instruction::UDiv, C1, C2);
371}
372Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2) {
373 return get(Instruction::SDiv, C1, C2);
374}
375Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) {
376 return get(Instruction::FDiv, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000377}
Reid Spencer7eb55b32006-11-02 01:53:59 +0000378Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) {
379 return get(Instruction::URem, C1, C2);
380}
381Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) {
382 return get(Instruction::SRem, C1, C2);
383}
384Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) {
385 return get(Instruction::FRem, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000386}
387Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
388 return get(Instruction::And, C1, C2);
389}
390Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
391 return get(Instruction::Or, C1, C2);
392}
393Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
394 return get(Instruction::Xor, C1, C2);
395}
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000396unsigned ConstantExpr::getPredicate() const {
397 assert(getOpcode() == Instruction::FCmp || getOpcode() == Instruction::ICmp);
398 return dynamic_cast<const CompareConstantExpr*>(this)->predicate;
399}
Chris Lattner817175f2004-03-29 02:37:53 +0000400Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
401 return get(Instruction::Shl, C1, C2);
402}
Reid Spencerfdff9382006-11-08 06:47:33 +0000403Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2) {
404 return get(Instruction::LShr, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000405}
Reid Spencerfdff9382006-11-08 06:47:33 +0000406Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2) {
407 return get(Instruction::AShr, C1, C2);
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000408}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000409
Chris Lattner7c1018a2006-07-14 19:37:40 +0000410/// getWithOperandReplaced - Return a constant expression identical to this
411/// one, but with the specified operand set to the specified value.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000412Constant *
413ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +0000414 assert(OpNo < getNumOperands() && "Operand num is out of range!");
415 assert(Op->getType() == getOperand(OpNo)->getType() &&
416 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000417 if (getOperand(OpNo) == Op)
418 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000419
Chris Lattner227816342006-07-14 22:20:01 +0000420 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000421 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000422 case Instruction::Trunc:
423 case Instruction::ZExt:
424 case Instruction::SExt:
425 case Instruction::FPTrunc:
426 case Instruction::FPExt:
427 case Instruction::UIToFP:
428 case Instruction::SIToFP:
429 case Instruction::FPToUI:
430 case Instruction::FPToSI:
431 case Instruction::PtrToInt:
432 case Instruction::IntToPtr:
433 case Instruction::BitCast:
434 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000435 case Instruction::Select:
436 Op0 = (OpNo == 0) ? Op : getOperand(0);
437 Op1 = (OpNo == 1) ? Op : getOperand(1);
438 Op2 = (OpNo == 2) ? Op : getOperand(2);
439 return ConstantExpr::getSelect(Op0, Op1, Op2);
440 case Instruction::InsertElement:
441 Op0 = (OpNo == 0) ? Op : getOperand(0);
442 Op1 = (OpNo == 1) ? Op : getOperand(1);
443 Op2 = (OpNo == 2) ? Op : getOperand(2);
444 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
445 case Instruction::ExtractElement:
446 Op0 = (OpNo == 0) ? Op : getOperand(0);
447 Op1 = (OpNo == 1) ? Op : getOperand(1);
448 return ConstantExpr::getExtractElement(Op0, Op1);
449 case Instruction::ShuffleVector:
450 Op0 = (OpNo == 0) ? Op : getOperand(0);
451 Op1 = (OpNo == 1) ? Op : getOperand(1);
452 Op2 = (OpNo == 2) ? Op : getOperand(2);
453 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000454 case Instruction::GetElementPtr: {
Chris Lattnerb5d70302007-02-19 20:01:23 +0000455 SmallVector<Constant*, 8> Ops;
456 Ops.resize(getNumOperands());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000457 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Chris Lattnerb5d70302007-02-19 20:01:23 +0000458 Ops[i] = getOperand(i);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000459 if (OpNo == 0)
Chris Lattnerb5d70302007-02-19 20:01:23 +0000460 return ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000461 Ops[OpNo-1] = Op;
Chris Lattnerb5d70302007-02-19 20:01:23 +0000462 return ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000463 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000464 default:
465 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000466 Op0 = (OpNo == 0) ? Op : getOperand(0);
467 Op1 = (OpNo == 1) ? Op : getOperand(1);
468 return ConstantExpr::get(getOpcode(), Op0, Op1);
469 }
470}
471
472/// getWithOperands - This returns the current constant expression with the
473/// operands replaced with the specified values. The specified operands must
474/// match count and type with the existing ones.
475Constant *ConstantExpr::
476getWithOperands(const std::vector<Constant*> &Ops) const {
477 assert(Ops.size() == getNumOperands() && "Operand count mismatch!");
478 bool AnyChange = false;
479 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
480 assert(Ops[i]->getType() == getOperand(i)->getType() &&
481 "Operand type mismatch!");
482 AnyChange |= Ops[i] != getOperand(i);
483 }
484 if (!AnyChange) // No operands changed, return self.
485 return const_cast<ConstantExpr*>(this);
486
487 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000488 case Instruction::Trunc:
489 case Instruction::ZExt:
490 case Instruction::SExt:
491 case Instruction::FPTrunc:
492 case Instruction::FPExt:
493 case Instruction::UIToFP:
494 case Instruction::SIToFP:
495 case Instruction::FPToUI:
496 case Instruction::FPToSI:
497 case Instruction::PtrToInt:
498 case Instruction::IntToPtr:
499 case Instruction::BitCast:
500 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattner227816342006-07-14 22:20:01 +0000501 case Instruction::Select:
502 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
503 case Instruction::InsertElement:
504 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
505 case Instruction::ExtractElement:
506 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
507 case Instruction::ShuffleVector:
508 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Chris Lattnerb5d70302007-02-19 20:01:23 +0000509 case Instruction::GetElementPtr:
510 return ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], Ops.size()-1);
Reid Spencer266e42b2006-12-23 06:05:41 +0000511 case Instruction::ICmp:
512 case Instruction::FCmp:
513 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattner227816342006-07-14 22:20:01 +0000514 default:
515 assert(getNumOperands() == 2 && "Must be binary operator?");
516 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000517 }
518}
519
Chris Lattner2f7c9632001-06-06 20:29:01 +0000520
521//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000522// isValueValidForType implementations
523
Reid Spencere7334722006-12-19 01:28:19 +0000524bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000525 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000526 if (Ty == Type::Int1Ty)
527 return Val == 0 || Val == 1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000528 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000529 return true; // always true, has to fit in largest type
530 uint64_t Max = (1ll << NumBits) - 1;
531 return Val <= Max;
Reid Spencere7334722006-12-19 01:28:19 +0000532}
533
Reid Spencere0fc4df2006-10-20 07:07:24 +0000534bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000535 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000536 if (Ty == Type::Int1Ty)
Reid Spencera94d3942007-01-19 21:13:56 +0000537 return Val == 0 || Val == 1 || Val == -1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000538 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000539 return true; // always true, has to fit in largest type
540 int64_t Min = -(1ll << (NumBits-1));
541 int64_t Max = (1ll << (NumBits-1)) - 1;
542 return (Val >= Min && Val <= Max);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000543}
544
Chris Lattner3462ae32001-12-03 22:26:30 +0000545bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000546 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000547 default:
548 return false; // These can't be represented as floating point!
549
Reid Spencerb95f8ab2004-12-07 07:38:08 +0000550 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000551 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000552 case Type::DoubleTyID:
553 return true; // This is the largest type...
554 }
Chris Lattneraa2372562006-05-24 17:04:05 +0000555}
Chris Lattner9655e542001-07-20 19:16:02 +0000556
Chris Lattner49d855c2001-09-07 16:46:31 +0000557//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000558// Factory Function Implementation
559
Chris Lattner98fa07b2003-05-23 20:03:32 +0000560// ConstantCreator - A class that is used to create constants by
561// ValueMap*. This class should be partially specialized if there is
562// something strange that needs to be done to interface to the ctor for the
563// constant.
564//
Chris Lattner189d19f2003-11-21 20:23:48 +0000565namespace llvm {
566 template<class ConstantClass, class TypeClass, class ValType>
Chris Lattner02157b02006-06-28 21:38:54 +0000567 struct VISIBILITY_HIDDEN ConstantCreator {
Chris Lattner189d19f2003-11-21 20:23:48 +0000568 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
569 return new ConstantClass(Ty, V);
570 }
571 };
Misha Brukmanb1c93172005-04-21 23:48:37 +0000572
Chris Lattner189d19f2003-11-21 20:23:48 +0000573 template<class ConstantClass, class TypeClass>
Chris Lattner02157b02006-06-28 21:38:54 +0000574 struct VISIBILITY_HIDDEN ConvertConstantType {
Chris Lattner189d19f2003-11-21 20:23:48 +0000575 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
576 assert(0 && "This type cannot be converted!\n");
577 abort();
578 }
579 };
Chris Lattnerb50d1352003-10-05 00:17:43 +0000580
Chris Lattner935aa922005-10-04 17:48:46 +0000581 template<class ValType, class TypeClass, class ConstantClass,
582 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattner02157b02006-06-28 21:38:54 +0000583 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
Chris Lattnerb64419a2005-10-03 22:51:37 +0000584 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000585 typedef std::pair<const Type*, ValType> MapKey;
586 typedef std::map<MapKey, Constant *> MapTy;
587 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
588 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
Chris Lattnerb64419a2005-10-03 22:51:37 +0000589 private:
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000590 /// Map - This is the main map from the element descriptor to the Constants.
591 /// This is the primary way we avoid creating two of the same shape
592 /// constant.
Chris Lattnerb50d1352003-10-05 00:17:43 +0000593 MapTy Map;
Chris Lattner935aa922005-10-04 17:48:46 +0000594
595 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
596 /// from the constants to their element in Map. This is important for
597 /// removal of constants from the array, which would otherwise have to scan
598 /// through the map with very large keys.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000599 InverseMapTy InverseMap;
Chris Lattnerb50d1352003-10-05 00:17:43 +0000600
Jim Laskeyc03caef2006-07-17 17:38:29 +0000601 /// AbstractTypeMap - Map for abstract type constants.
602 ///
Chris Lattnerb50d1352003-10-05 00:17:43 +0000603 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner99a669b2004-11-19 16:39:44 +0000604
Chris Lattner99a669b2004-11-19 16:39:44 +0000605 private:
606 void clear(std::vector<Constant *> &Constants) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000607 for(typename MapTy::iterator I = Map.begin(); I != Map.end(); ++I)
Chris Lattner99a669b2004-11-19 16:39:44 +0000608 Constants.push_back(I->second);
609 Map.clear();
610 AbstractTypeMap.clear();
Chris Lattner935aa922005-10-04 17:48:46 +0000611 InverseMap.clear();
Chris Lattner99a669b2004-11-19 16:39:44 +0000612 }
613
Chris Lattner98fa07b2003-05-23 20:03:32 +0000614 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000615 typename MapTy::iterator map_end() { return Map.end(); }
Chris Lattnerb64419a2005-10-03 22:51:37 +0000616
617 /// InsertOrGetItem - Return an iterator for the specified element.
618 /// If the element exists in the map, the returned iterator points to the
619 /// entry and Exists=true. If not, the iterator points to the newly
620 /// inserted entry and returns Exists=false. Newly inserted entries have
621 /// I->second == 0, and should be filled in.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000622 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
623 &InsertVal,
Chris Lattnerb64419a2005-10-03 22:51:37 +0000624 bool &Exists) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000625 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
Chris Lattnerb64419a2005-10-03 22:51:37 +0000626 Exists = !IP.second;
627 return IP.first;
628 }
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000629
Chris Lattner935aa922005-10-04 17:48:46 +0000630private:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000631 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
Chris Lattner935aa922005-10-04 17:48:46 +0000632 if (HasLargeKey) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000633 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
Chris Lattner935aa922005-10-04 17:48:46 +0000634 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
635 IMI->second->second == CP &&
636 "InverseMap corrupt!");
637 return IMI->second;
638 }
639
Jim Laskeyc03caef2006-07-17 17:38:29 +0000640 typename MapTy::iterator I =
Chris Lattner935aa922005-10-04 17:48:46 +0000641 Map.find(MapKey((TypeClass*)CP->getRawType(), getValType(CP)));
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000642 if (I == Map.end() || I->second != CP) {
643 // FIXME: This should not use a linear scan. If this gets to be a
644 // performance problem, someone should look at this.
645 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
646 /* empty */;
647 }
Chris Lattner935aa922005-10-04 17:48:46 +0000648 return I;
649 }
650public:
651
Chris Lattnerb64419a2005-10-03 22:51:37 +0000652 /// getOrCreate - Return the specified constant from the map, creating it if
653 /// necessary.
Chris Lattner98fa07b2003-05-23 20:03:32 +0000654 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000655 MapKey Lookup(Ty, V);
Jim Laskeyc03caef2006-07-17 17:38:29 +0000656 typename MapTy::iterator I = Map.lower_bound(Lookup);
Reid Spencere0fc4df2006-10-20 07:07:24 +0000657 // Is it in the map?
Chris Lattner98fa07b2003-05-23 20:03:32 +0000658 if (I != Map.end() && I->first == Lookup)
Reid Spencere0fc4df2006-10-20 07:07:24 +0000659 return static_cast<ConstantClass *>(I->second);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000660
661 // If no preexisting value, create one now...
662 ConstantClass *Result =
663 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
664
Chris Lattnerb50d1352003-10-05 00:17:43 +0000665 /// FIXME: why does this assert fail when loading 176.gcc?
666 //assert(Result->getType() == Ty && "Type specified is not correct!");
667 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
668
Chris Lattner935aa922005-10-04 17:48:46 +0000669 if (HasLargeKey) // Remember the reverse mapping if needed.
670 InverseMap.insert(std::make_pair(Result, I));
671
Chris Lattnerb50d1352003-10-05 00:17:43 +0000672 // If the type of the constant is abstract, make sure that an entry exists
673 // for it in the AbstractTypeMap.
674 if (Ty->isAbstract()) {
675 typename AbstractTypeMapTy::iterator TI =
676 AbstractTypeMap.lower_bound(Ty);
677
678 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
679 // Add ourselves to the ATU list of the type.
680 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
681
682 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
683 }
684 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000685 return Result;
686 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000687
Chris Lattner98fa07b2003-05-23 20:03:32 +0000688 void remove(ConstantClass *CP) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000689 typename MapTy::iterator I = FindExistingElement(CP);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000690 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +0000691 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +0000692
Chris Lattner935aa922005-10-04 17:48:46 +0000693 if (HasLargeKey) // Remember the reverse mapping if needed.
694 InverseMap.erase(CP);
695
Chris Lattnerb50d1352003-10-05 00:17:43 +0000696 // Now that we found the entry, make sure this isn't the entry that
697 // the AbstractTypeMap points to.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000698 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000699 if (Ty->isAbstract()) {
700 assert(AbstractTypeMap.count(Ty) &&
701 "Abstract type not in AbstractTypeMap?");
Jim Laskeyc03caef2006-07-17 17:38:29 +0000702 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
Chris Lattnerb50d1352003-10-05 00:17:43 +0000703 if (ATMEntryIt == I) {
704 // Yes, we are removing the representative entry for this type.
705 // See if there are any other entries of the same type.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000706 typename MapTy::iterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000707
Chris Lattnerb50d1352003-10-05 00:17:43 +0000708 // First check the entry before this one...
709 if (TmpIt != Map.begin()) {
710 --TmpIt;
711 if (TmpIt->first.first != Ty) // Not the same type, move back...
712 ++TmpIt;
713 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000714
Chris Lattnerb50d1352003-10-05 00:17:43 +0000715 // If we didn't find the same type, try to move forward...
716 if (TmpIt == ATMEntryIt) {
717 ++TmpIt;
718 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
719 --TmpIt; // No entry afterwards with the same type
720 }
721
722 // If there is another entry in the map of the same abstract type,
723 // update the AbstractTypeMap entry now.
724 if (TmpIt != ATMEntryIt) {
725 ATMEntryIt = TmpIt;
726 } else {
727 // Otherwise, we are removing the last instance of this type
728 // from the table. Remove from the ATM, and from user list.
729 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
730 AbstractTypeMap.erase(Ty);
731 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000732 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000733 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000734
Chris Lattnerb50d1352003-10-05 00:17:43 +0000735 Map.erase(I);
736 }
737
Chris Lattner3b793c62005-10-04 21:35:50 +0000738
739 /// MoveConstantToNewSlot - If we are about to change C to be the element
740 /// specified by I, update our internal data structures to reflect this
741 /// fact.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000742 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
Chris Lattner3b793c62005-10-04 21:35:50 +0000743 // First, remove the old location of the specified constant in the map.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000744 typename MapTy::iterator OldI = FindExistingElement(C);
Chris Lattner3b793c62005-10-04 21:35:50 +0000745 assert(OldI != Map.end() && "Constant not found in constant table!");
746 assert(OldI->second == C && "Didn't find correct element?");
747
748 // If this constant is the representative element for its abstract type,
749 // update the AbstractTypeMap so that the representative element is I.
750 if (C->getType()->isAbstract()) {
751 typename AbstractTypeMapTy::iterator ATI =
752 AbstractTypeMap.find(C->getType());
753 assert(ATI != AbstractTypeMap.end() &&
754 "Abstract type not in AbstractTypeMap?");
755 if (ATI->second == OldI)
756 ATI->second = I;
757 }
758
759 // Remove the old entry from the map.
760 Map.erase(OldI);
761
762 // Update the inverse map so that we know that this constant is now
763 // located at descriptor I.
764 if (HasLargeKey) {
765 assert(I->second == C && "Bad inversemap entry!");
766 InverseMap[C] = I;
767 }
768 }
769
Chris Lattnerb50d1352003-10-05 00:17:43 +0000770 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000771 typename AbstractTypeMapTy::iterator I =
Jim Laskeyc03caef2006-07-17 17:38:29 +0000772 AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +0000773
774 assert(I != AbstractTypeMap.end() &&
775 "Abstract type not in AbstractTypeMap?");
776
777 // Convert a constant at a time until the last one is gone. The last one
778 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
779 // eliminated eventually.
780 do {
781 ConvertConstantType<ConstantClass,
Jim Laskeyc03caef2006-07-17 17:38:29 +0000782 TypeClass>::convert(
783 static_cast<ConstantClass *>(I->second->second),
Chris Lattnerb50d1352003-10-05 00:17:43 +0000784 cast<TypeClass>(NewTy));
785
Jim Laskeyc03caef2006-07-17 17:38:29 +0000786 I = AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +0000787 } while (I != AbstractTypeMap.end());
788 }
789
790 // If the type became concrete without being refined to any other existing
791 // type, we just remove ourselves from the ATU list.
792 void typeBecameConcrete(const DerivedType *AbsTy) {
793 AbsTy->removeAbstractTypeUser(this);
794 }
795
796 void dump() const {
Bill Wendling6a462f12006-11-17 08:03:48 +0000797 DOUT << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +0000798 }
799 };
800}
801
Chris Lattnera84df0a22006-09-28 23:36:21 +0000802
Reid Spencere0fc4df2006-10-20 07:07:24 +0000803//---- ConstantInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000804//
Reid Spencere0fc4df2006-10-20 07:07:24 +0000805static ManagedStatic<ValueMap<uint64_t, Type, ConstantInt> > IntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000806
Reid Spencere0fc4df2006-10-20 07:07:24 +0000807// Get a ConstantInt from an int64_t. Note here that we canoncialize the value
808// to a uint64_t value that has been zero extended down to the size of the
809// integer type of the ConstantInt. This allows the getZExtValue method to
810// just return the stored value while getSExtValue has to convert back to sign
811// extended. getZExtValue is more common in LLVM than getSExtValue().
812ConstantInt *ConstantInt::get(const Type *Ty, int64_t V) {
Reid Spencercddc9df2007-01-12 04:24:46 +0000813 if (Ty == Type::Int1Ty)
814 if (V & 1)
815 return getTrue();
816 else
817 return getFalse();
Reid Spencera94d3942007-01-19 21:13:56 +0000818 return IntConstants->getOrCreate(Ty, V & cast<IntegerType>(Ty)->getBitMask());
Chris Lattner49d855c2001-09-07 16:46:31 +0000819}
820
Chris Lattner3462ae32001-12-03 22:26:30 +0000821//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000822//
Chris Lattnerac80ea42004-02-01 22:49:04 +0000823namespace llvm {
824 template<>
825 struct ConstantCreator<ConstantFP, Type, uint64_t> {
826 static ConstantFP *create(const Type *Ty, uint64_t V) {
827 assert(Ty == Type::DoubleTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000828 return new ConstantFP(Ty, BitsToDouble(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000829 }
830 };
831 template<>
832 struct ConstantCreator<ConstantFP, Type, uint32_t> {
833 static ConstantFP *create(const Type *Ty, uint32_t V) {
834 assert(Ty == Type::FloatTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000835 return new ConstantFP(Ty, BitsToFloat(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000836 }
837 };
838}
839
Chris Lattner69edc982006-09-28 00:35:06 +0000840static ManagedStatic<ValueMap<uint64_t, Type, ConstantFP> > DoubleConstants;
841static ManagedStatic<ValueMap<uint32_t, Type, ConstantFP> > FloatConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000842
Jim Laskey8ad8f712005-08-17 20:06:22 +0000843bool ConstantFP::isNullValue() const {
844 return DoubleToBits(Val) == 0;
845}
846
847bool ConstantFP::isExactlyValue(double V) const {
848 return DoubleToBits(V) == DoubleToBits(Val);
849}
850
851
Chris Lattner3462ae32001-12-03 22:26:30 +0000852ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner241ed4c2004-01-23 00:55:21 +0000853 if (Ty == Type::FloatTy) {
854 // Force the value through memory to normalize it.
Chris Lattner69edc982006-09-28 00:35:06 +0000855 return FloatConstants->getOrCreate(Ty, FloatToBits(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000856 } else {
857 assert(Ty == Type::DoubleTy);
Chris Lattner69edc982006-09-28 00:35:06 +0000858 return DoubleConstants->getOrCreate(Ty, DoubleToBits(V));
Chris Lattner241ed4c2004-01-23 00:55:21 +0000859 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000860}
861
Chris Lattner9fba3da2004-02-15 05:53:04 +0000862//---- ConstantAggregateZero::get() implementation...
863//
864namespace llvm {
865 // ConstantAggregateZero does not take extra "value" argument...
866 template<class ValType>
867 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
868 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
869 return new ConstantAggregateZero(Ty);
870 }
871 };
872
873 template<>
874 struct ConvertConstantType<ConstantAggregateZero, Type> {
875 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
876 // Make everyone now use a constant of the new type...
877 Constant *New = ConstantAggregateZero::get(NewTy);
878 assert(New != OldC && "Didn't replace constant??");
879 OldC->uncheckedReplaceAllUsesWith(New);
880 OldC->destroyConstant(); // This constant is now dead, destroy it.
881 }
882 };
883}
884
Chris Lattner69edc982006-09-28 00:35:06 +0000885static ManagedStatic<ValueMap<char, Type,
886 ConstantAggregateZero> > AggZeroConstants;
Chris Lattner9fba3da2004-02-15 05:53:04 +0000887
Chris Lattner3e650af2004-08-04 04:48:01 +0000888static char getValType(ConstantAggregateZero *CPZ) { return 0; }
889
Chris Lattner9fba3da2004-02-15 05:53:04 +0000890Constant *ConstantAggregateZero::get(const Type *Ty) {
Reid Spencerd84d35b2007-02-15 02:26:10 +0000891 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) &&
Chris Lattnerbfd0b6d2006-06-10 04:16:23 +0000892 "Cannot create an aggregate zero of non-aggregate type!");
Chris Lattner69edc982006-09-28 00:35:06 +0000893 return AggZeroConstants->getOrCreate(Ty, 0);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000894}
895
896// destroyConstant - Remove the constant from the constant table...
897//
898void ConstantAggregateZero::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +0000899 AggZeroConstants->remove(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000900 destroyConstantImpl();
901}
902
Chris Lattner3462ae32001-12-03 22:26:30 +0000903//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000904//
Chris Lattner189d19f2003-11-21 20:23:48 +0000905namespace llvm {
906 template<>
907 struct ConvertConstantType<ConstantArray, ArrayType> {
908 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
909 // Make everyone now use a constant of the new type...
910 std::vector<Constant*> C;
911 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
912 C.push_back(cast<Constant>(OldC->getOperand(i)));
913 Constant *New = ConstantArray::get(NewTy, C);
914 assert(New != OldC && "Didn't replace constant??");
915 OldC->uncheckedReplaceAllUsesWith(New);
916 OldC->destroyConstant(); // This constant is now dead, destroy it.
917 }
918 };
919}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000920
Chris Lattner3e650af2004-08-04 04:48:01 +0000921static std::vector<Constant*> getValType(ConstantArray *CA) {
922 std::vector<Constant*> Elements;
923 Elements.reserve(CA->getNumOperands());
924 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
925 Elements.push_back(cast<Constant>(CA->getOperand(i)));
926 return Elements;
927}
928
Chris Lattnerb64419a2005-10-03 22:51:37 +0000929typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattner935aa922005-10-04 17:48:46 +0000930 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +0000931static ManagedStatic<ArrayConstantsTy> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000932
Chris Lattner015e8212004-02-15 04:14:47 +0000933Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +0000934 const std::vector<Constant*> &V) {
935 // If this is an all-zero array, return a ConstantAggregateZero object
936 if (!V.empty()) {
937 Constant *C = V[0];
938 if (!C->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +0000939 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000940 for (unsigned i = 1, e = V.size(); i != e; ++i)
941 if (V[i] != C)
Chris Lattner69edc982006-09-28 00:35:06 +0000942 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000943 }
944 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000945}
946
Chris Lattner98fa07b2003-05-23 20:03:32 +0000947// destroyConstant - Remove the constant from the constant table...
948//
949void ConstantArray::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +0000950 ArrayConstants->remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000951 destroyConstantImpl();
952}
953
Reid Spencer6f614532006-05-30 08:23:18 +0000954/// ConstantArray::get(const string&) - Return an array that is initialized to
955/// contain the specified string. If length is zero then a null terminator is
956/// added to the specified string so that it may be used in a natural way.
957/// Otherwise, the length parameter specifies how much of the string to use
958/// and it won't be null terminated.
959///
Reid Spencer82ebaba2006-05-30 18:15:07 +0000960Constant *ConstantArray::get(const std::string &Str, bool AddNull) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000961 std::vector<Constant*> ElementVals;
Reid Spencer82ebaba2006-05-30 18:15:07 +0000962 for (unsigned i = 0; i < Str.length(); ++i)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000963 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000964
965 // Add a null terminator to the string...
Reid Spencer82ebaba2006-05-30 18:15:07 +0000966 if (AddNull) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000967 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, 0));
Reid Spencer6f614532006-05-30 08:23:18 +0000968 }
Chris Lattner8f80fe02001-10-14 23:54:12 +0000969
Reid Spencer8d9336d2006-12-31 05:26:44 +0000970 ArrayType *ATy = ArrayType::get(Type::Int8Ty, ElementVals.size());
Chris Lattner3462ae32001-12-03 22:26:30 +0000971 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +0000972}
973
Reid Spencer2546b762007-01-26 07:37:34 +0000974/// isString - This method returns true if the array is an array of i8, and
975/// if the elements of the array are all ConstantInt's.
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000976bool ConstantArray::isString() const {
Reid Spencer2546b762007-01-26 07:37:34 +0000977 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +0000978 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000979 return false;
980 // Check the elements to make sure they are all integers, not constant
981 // expressions.
982 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
983 if (!isa<ConstantInt>(getOperand(i)))
984 return false;
985 return true;
986}
987
Evan Cheng3763c5b2006-10-26 19:15:05 +0000988/// isCString - This method returns true if the array is a string (see
989/// isString) and it ends in a null byte \0 and does not contains any other
990/// null bytes except its terminator.
991bool ConstantArray::isCString() const {
Reid Spencer2546b762007-01-26 07:37:34 +0000992 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +0000993 if (getType()->getElementType() != Type::Int8Ty)
Evan Chenge974da62006-10-26 21:48:03 +0000994 return false;
995 Constant *Zero = Constant::getNullValue(getOperand(0)->getType());
996 // Last element must be a null.
997 if (getOperand(getNumOperands()-1) != Zero)
998 return false;
999 // Other elements must be non-null integers.
1000 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1001 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +00001002 return false;
Evan Chenge974da62006-10-26 21:48:03 +00001003 if (getOperand(i) == Zero)
1004 return false;
1005 }
Evan Cheng3763c5b2006-10-26 19:15:05 +00001006 return true;
1007}
1008
1009
Reid Spencer2546b762007-01-26 07:37:34 +00001010// getAsString - If the sub-element type of this array is i8
Chris Lattner81fabb02002-08-26 17:53:56 +00001011// then this method converts the array to an std::string and returns it.
1012// Otherwise, it asserts out.
1013//
1014std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001015 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001016 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +00001017 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Reid Spencere0fc4df2006-10-20 07:07:24 +00001018 Result += (char)cast<ConstantInt>(getOperand(i))->getZExtValue();
Chris Lattner81fabb02002-08-26 17:53:56 +00001019 return Result;
1020}
1021
1022
Chris Lattner3462ae32001-12-03 22:26:30 +00001023//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001024//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001025
Chris Lattner189d19f2003-11-21 20:23:48 +00001026namespace llvm {
1027 template<>
1028 struct ConvertConstantType<ConstantStruct, StructType> {
1029 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1030 // Make everyone now use a constant of the new type...
1031 std::vector<Constant*> C;
1032 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1033 C.push_back(cast<Constant>(OldC->getOperand(i)));
1034 Constant *New = ConstantStruct::get(NewTy, C);
1035 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001036
Chris Lattner189d19f2003-11-21 20:23:48 +00001037 OldC->uncheckedReplaceAllUsesWith(New);
1038 OldC->destroyConstant(); // This constant is now dead, destroy it.
1039 }
1040 };
1041}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001042
Chris Lattner8760ec72005-10-04 01:17:50 +00001043typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattner935aa922005-10-04 17:48:46 +00001044 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001045static ManagedStatic<StructConstantsTy> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001046
Chris Lattner3e650af2004-08-04 04:48:01 +00001047static std::vector<Constant*> getValType(ConstantStruct *CS) {
1048 std::vector<Constant*> Elements;
1049 Elements.reserve(CS->getNumOperands());
1050 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1051 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1052 return Elements;
1053}
1054
Chris Lattner015e8212004-02-15 04:14:47 +00001055Constant *ConstantStruct::get(const StructType *Ty,
1056 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001057 // Create a ConstantAggregateZero value if all elements are zeros...
1058 for (unsigned i = 0, e = V.size(); i != e; ++i)
1059 if (!V[i]->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001060 return StructConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001061
1062 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001063}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001064
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001065Constant *ConstantStruct::get(const std::vector<Constant*> &V, bool packed) {
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001066 std::vector<const Type*> StructEls;
1067 StructEls.reserve(V.size());
1068 for (unsigned i = 0, e = V.size(); i != e; ++i)
1069 StructEls.push_back(V[i]->getType());
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001070 return get(StructType::get(StructEls, packed), V);
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001071}
1072
Chris Lattnerd7a73302001-10-13 06:57:33 +00001073// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001074//
Chris Lattner3462ae32001-12-03 22:26:30 +00001075void ConstantStruct::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001076 StructConstants->remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001077 destroyConstantImpl();
1078}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001079
Reid Spencerd84d35b2007-02-15 02:26:10 +00001080//---- ConstantVector::get() implementation...
Brian Gaeke02209042004-08-20 06:00:58 +00001081//
1082namespace llvm {
1083 template<>
Reid Spencerd84d35b2007-02-15 02:26:10 +00001084 struct ConvertConstantType<ConstantVector, VectorType> {
1085 static void convert(ConstantVector *OldC, const VectorType *NewTy) {
Brian Gaeke02209042004-08-20 06:00:58 +00001086 // Make everyone now use a constant of the new type...
1087 std::vector<Constant*> C;
1088 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1089 C.push_back(cast<Constant>(OldC->getOperand(i)));
Reid Spencerd84d35b2007-02-15 02:26:10 +00001090 Constant *New = ConstantVector::get(NewTy, C);
Brian Gaeke02209042004-08-20 06:00:58 +00001091 assert(New != OldC && "Didn't replace constant??");
1092 OldC->uncheckedReplaceAllUsesWith(New);
1093 OldC->destroyConstant(); // This constant is now dead, destroy it.
1094 }
1095 };
1096}
1097
Reid Spencerd84d35b2007-02-15 02:26:10 +00001098static std::vector<Constant*> getValType(ConstantVector *CP) {
Brian Gaeke02209042004-08-20 06:00:58 +00001099 std::vector<Constant*> Elements;
1100 Elements.reserve(CP->getNumOperands());
1101 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1102 Elements.push_back(CP->getOperand(i));
1103 return Elements;
1104}
1105
Reid Spencerd84d35b2007-02-15 02:26:10 +00001106static ManagedStatic<ValueMap<std::vector<Constant*>, VectorType,
Reid Spencer09575ba2007-02-15 03:39:18 +00001107 ConstantVector> > VectorConstants;
Brian Gaeke02209042004-08-20 06:00:58 +00001108
Reid Spencerd84d35b2007-02-15 02:26:10 +00001109Constant *ConstantVector::get(const VectorType *Ty,
Brian Gaeke02209042004-08-20 06:00:58 +00001110 const std::vector<Constant*> &V) {
1111 // If this is an all-zero packed, return a ConstantAggregateZero object
1112 if (!V.empty()) {
1113 Constant *C = V[0];
1114 if (!C->isNullValue())
Reid Spencer09575ba2007-02-15 03:39:18 +00001115 return VectorConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001116 for (unsigned i = 1, e = V.size(); i != e; ++i)
1117 if (V[i] != C)
Reid Spencer09575ba2007-02-15 03:39:18 +00001118 return VectorConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001119 }
1120 return ConstantAggregateZero::get(Ty);
1121}
1122
Reid Spencerd84d35b2007-02-15 02:26:10 +00001123Constant *ConstantVector::get(const std::vector<Constant*> &V) {
Brian Gaeke02209042004-08-20 06:00:58 +00001124 assert(!V.empty() && "Cannot infer type if V is empty");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001125 return get(VectorType::get(V.front()->getType(),V.size()), V);
Brian Gaeke02209042004-08-20 06:00:58 +00001126}
1127
1128// destroyConstant - Remove the constant from the constant table...
1129//
Reid Spencerd84d35b2007-02-15 02:26:10 +00001130void ConstantVector::destroyConstant() {
Reid Spencer09575ba2007-02-15 03:39:18 +00001131 VectorConstants->remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001132 destroyConstantImpl();
1133}
1134
Jim Laskeyf0478822007-01-12 22:39:14 +00001135/// This function will return true iff every element in this packed constant
1136/// is set to all ones.
1137/// @returns true iff this constant's emements are all set to all ones.
1138/// @brief Determine if the value is all ones.
Reid Spencerd84d35b2007-02-15 02:26:10 +00001139bool ConstantVector::isAllOnesValue() const {
Jim Laskeyf0478822007-01-12 22:39:14 +00001140 // Check out first element.
1141 const Constant *Elt = getOperand(0);
1142 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1143 if (!CI || !CI->isAllOnesValue()) return false;
1144 // Then make sure all remaining elements point to the same value.
1145 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1146 if (getOperand(I) != Elt) return false;
1147 }
1148 return true;
1149}
1150
Chris Lattner3462ae32001-12-03 22:26:30 +00001151//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001152//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001153
Chris Lattner189d19f2003-11-21 20:23:48 +00001154namespace llvm {
1155 // ConstantPointerNull does not take extra "value" argument...
1156 template<class ValType>
1157 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1158 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1159 return new ConstantPointerNull(Ty);
1160 }
1161 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001162
Chris Lattner189d19f2003-11-21 20:23:48 +00001163 template<>
1164 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1165 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1166 // Make everyone now use a constant of the new type...
1167 Constant *New = ConstantPointerNull::get(NewTy);
1168 assert(New != OldC && "Didn't replace constant??");
1169 OldC->uncheckedReplaceAllUsesWith(New);
1170 OldC->destroyConstant(); // This constant is now dead, destroy it.
1171 }
1172 };
1173}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001174
Chris Lattner69edc982006-09-28 00:35:06 +00001175static ManagedStatic<ValueMap<char, PointerType,
1176 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001177
Chris Lattner3e650af2004-08-04 04:48:01 +00001178static char getValType(ConstantPointerNull *) {
1179 return 0;
1180}
1181
1182
Chris Lattner3462ae32001-12-03 22:26:30 +00001183ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001184 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001185}
1186
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001187// destroyConstant - Remove the constant from the constant table...
1188//
1189void ConstantPointerNull::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001190 NullPtrConstants->remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001191 destroyConstantImpl();
1192}
1193
1194
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001195//---- UndefValue::get() implementation...
1196//
1197
1198namespace llvm {
1199 // UndefValue does not take extra "value" argument...
1200 template<class ValType>
1201 struct ConstantCreator<UndefValue, Type, ValType> {
1202 static UndefValue *create(const Type *Ty, const ValType &V) {
1203 return new UndefValue(Ty);
1204 }
1205 };
1206
1207 template<>
1208 struct ConvertConstantType<UndefValue, Type> {
1209 static void convert(UndefValue *OldC, const Type *NewTy) {
1210 // Make everyone now use a constant of the new type.
1211 Constant *New = UndefValue::get(NewTy);
1212 assert(New != OldC && "Didn't replace constant??");
1213 OldC->uncheckedReplaceAllUsesWith(New);
1214 OldC->destroyConstant(); // This constant is now dead, destroy it.
1215 }
1216 };
1217}
1218
Chris Lattner69edc982006-09-28 00:35:06 +00001219static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001220
1221static char getValType(UndefValue *) {
1222 return 0;
1223}
1224
1225
1226UndefValue *UndefValue::get(const Type *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001227 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001228}
1229
1230// destroyConstant - Remove the constant from the constant table.
1231//
1232void UndefValue::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001233 UndefValueConstants->remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001234 destroyConstantImpl();
1235}
1236
1237
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001238//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001239//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001240
Reid Spenceree3c9912006-12-04 05:19:50 +00001241struct ExprMapKeyType {
1242 explicit ExprMapKeyType(unsigned opc, std::vector<Constant*> ops,
Reid Spencerdba6aa42006-12-04 18:38:05 +00001243 unsigned short pred = 0) : opcode(opc), predicate(pred), operands(ops) { }
1244 uint16_t opcode;
1245 uint16_t predicate;
Reid Spenceree3c9912006-12-04 05:19:50 +00001246 std::vector<Constant*> operands;
Reid Spenceree3c9912006-12-04 05:19:50 +00001247 bool operator==(const ExprMapKeyType& that) const {
1248 return this->opcode == that.opcode &&
1249 this->predicate == that.predicate &&
1250 this->operands == that.operands;
1251 }
1252 bool operator<(const ExprMapKeyType & that) const {
1253 return this->opcode < that.opcode ||
1254 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1255 (this->opcode == that.opcode && this->predicate == that.predicate &&
1256 this->operands < that.operands);
1257 }
1258
1259 bool operator!=(const ExprMapKeyType& that) const {
1260 return !(*this == that);
1261 }
1262};
Chris Lattner98fa07b2003-05-23 20:03:32 +00001263
Chris Lattner189d19f2003-11-21 20:23:48 +00001264namespace llvm {
1265 template<>
1266 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001267 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1268 unsigned short pred = 0) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001269 if (Instruction::isCast(V.opcode))
1270 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1271 if ((V.opcode >= Instruction::BinaryOpsBegin &&
Reid Spencer2341c222007-02-02 02:16:23 +00001272 V.opcode < Instruction::BinaryOpsEnd))
Reid Spenceree3c9912006-12-04 05:19:50 +00001273 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1274 if (V.opcode == Instruction::Select)
1275 return new SelectConstantExpr(V.operands[0], V.operands[1],
1276 V.operands[2]);
1277 if (V.opcode == Instruction::ExtractElement)
1278 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1279 if (V.opcode == Instruction::InsertElement)
1280 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1281 V.operands[2]);
1282 if (V.opcode == Instruction::ShuffleVector)
1283 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1284 V.operands[2]);
1285 if (V.opcode == Instruction::GetElementPtr) {
1286 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
1287 return new GetElementPtrConstantExpr(V.operands[0], IdxList, Ty);
1288 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001289
Reid Spenceree3c9912006-12-04 05:19:50 +00001290 // The compare instructions are weird. We have to encode the predicate
1291 // value and it is combined with the instruction opcode by multiplying
1292 // the opcode by one hundred. We must decode this to get the predicate.
1293 if (V.opcode == Instruction::ICmp)
1294 return new CompareConstantExpr(Instruction::ICmp, V.predicate,
1295 V.operands[0], V.operands[1]);
1296 if (V.opcode == Instruction::FCmp)
1297 return new CompareConstantExpr(Instruction::FCmp, V.predicate,
1298 V.operands[0], V.operands[1]);
1299 assert(0 && "Invalid ConstantExpr!");
Jeff Cohen9f469632006-12-15 21:47:01 +00001300 return 0;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001301 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001302 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001303
Chris Lattner189d19f2003-11-21 20:23:48 +00001304 template<>
1305 struct ConvertConstantType<ConstantExpr, Type> {
1306 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1307 Constant *New;
1308 switch (OldC->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001309 case Instruction::Trunc:
1310 case Instruction::ZExt:
1311 case Instruction::SExt:
1312 case Instruction::FPTrunc:
1313 case Instruction::FPExt:
1314 case Instruction::UIToFP:
1315 case Instruction::SIToFP:
1316 case Instruction::FPToUI:
1317 case Instruction::FPToSI:
1318 case Instruction::PtrToInt:
1319 case Instruction::IntToPtr:
1320 case Instruction::BitCast:
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001321 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
1322 NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001323 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001324 case Instruction::Select:
1325 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1326 OldC->getOperand(1),
1327 OldC->getOperand(2));
1328 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001329 default:
1330 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001331 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner189d19f2003-11-21 20:23:48 +00001332 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1333 OldC->getOperand(1));
1334 break;
1335 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001336 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001337 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
Chris Lattner302116a2007-01-31 04:40:28 +00001338 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
1339 &Idx[0], Idx.size());
Chris Lattner189d19f2003-11-21 20:23:48 +00001340 break;
1341 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001342
Chris Lattner189d19f2003-11-21 20:23:48 +00001343 assert(New != OldC && "Didn't replace constant??");
1344 OldC->uncheckedReplaceAllUsesWith(New);
1345 OldC->destroyConstant(); // This constant is now dead, destroy it.
1346 }
1347 };
1348} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001349
1350
Chris Lattner3e650af2004-08-04 04:48:01 +00001351static ExprMapKeyType getValType(ConstantExpr *CE) {
1352 std::vector<Constant*> Operands;
1353 Operands.reserve(CE->getNumOperands());
1354 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1355 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spenceree3c9912006-12-04 05:19:50 +00001356 return ExprMapKeyType(CE->getOpcode(), Operands,
1357 CE->isCompare() ? CE->getPredicate() : 0);
Chris Lattner3e650af2004-08-04 04:48:01 +00001358}
1359
Chris Lattner69edc982006-09-28 00:35:06 +00001360static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1361 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001362
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001363/// This is a utility function to handle folding of casts and lookup of the
1364/// cast in the ExprConstants map. It is usedby the various get* methods below.
1365static inline Constant *getFoldedCast(
1366 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001367 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001368 // Fold a few common cases
1369 if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
1370 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001371
Vikram S. Adve4c485332002-07-15 18:19:33 +00001372 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001373 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00001374 ExprMapKeyType Key(opc, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001375 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001376}
Reid Spencerf37dc652006-12-05 19:14:13 +00001377
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001378Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
1379 Instruction::CastOps opc = Instruction::CastOps(oc);
1380 assert(Instruction::isCast(opc) && "opcode out of range");
1381 assert(C && Ty && "Null arguments to getCast");
1382 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1383
1384 switch (opc) {
1385 default:
1386 assert(0 && "Invalid cast opcode");
1387 break;
1388 case Instruction::Trunc: return getTrunc(C, Ty);
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001389 case Instruction::ZExt: return getZExt(C, Ty);
1390 case Instruction::SExt: return getSExt(C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001391 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1392 case Instruction::FPExt: return getFPExtend(C, Ty);
1393 case Instruction::UIToFP: return getUIToFP(C, Ty);
1394 case Instruction::SIToFP: return getSIToFP(C, Ty);
1395 case Instruction::FPToUI: return getFPToUI(C, Ty);
1396 case Instruction::FPToSI: return getFPToSI(C, Ty);
1397 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1398 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1399 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001400 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001401 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00001402}
1403
Reid Spencer5c140882006-12-04 20:17:56 +00001404Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
1405 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1406 return getCast(Instruction::BitCast, C, Ty);
1407 return getCast(Instruction::ZExt, C, Ty);
1408}
1409
1410Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
1411 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1412 return getCast(Instruction::BitCast, C, Ty);
1413 return getCast(Instruction::SExt, C, Ty);
1414}
1415
1416Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
1417 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1418 return getCast(Instruction::BitCast, C, Ty);
1419 return getCast(Instruction::Trunc, C, Ty);
1420}
1421
Reid Spencerbc245a02006-12-05 03:25:26 +00001422Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
1423 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001424 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001425
Chris Lattner03c49532007-01-15 02:27:26 +00001426 if (Ty->isInteger())
Reid Spencerbc245a02006-12-05 03:25:26 +00001427 return getCast(Instruction::PtrToInt, S, Ty);
1428 return getCast(Instruction::BitCast, S, Ty);
1429}
1430
Reid Spencer56521c42006-12-12 00:51:07 +00001431Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1432 bool isSigned) {
Chris Lattner03c49532007-01-15 02:27:26 +00001433 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer56521c42006-12-12 00:51:07 +00001434 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1435 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1436 Instruction::CastOps opcode =
1437 (SrcBits == DstBits ? Instruction::BitCast :
1438 (SrcBits > DstBits ? Instruction::Trunc :
1439 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1440 return getCast(opcode, C, Ty);
1441}
1442
1443Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
1444 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1445 "Invalid cast");
1446 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1447 unsigned DstBits = Ty->getPrimitiveSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001448 if (SrcBits == DstBits)
1449 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001450 Instruction::CastOps opcode =
Reid Spencerca104e82006-12-12 05:38:50 +00001451 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001452 return getCast(opcode, C, Ty);
1453}
1454
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001455Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001456 assert(C->getType()->isInteger() && "Trunc operand must be integer");
1457 assert(Ty->isInteger() && "Trunc produces only integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001458 assert(C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&&
1459 "SrcTy must be larger than DestTy for Trunc!");
1460
1461 return getFoldedCast(Instruction::Trunc, C, Ty);
1462}
1463
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001464Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001465 assert(C->getType()->isInteger() && "SEXt operand must be integral");
1466 assert(Ty->isInteger() && "SExt produces only integer");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001467 assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1468 "SrcTy must be smaller than DestTy for SExt!");
1469
1470 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerdd284742004-04-04 23:20:30 +00001471}
1472
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001473Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001474 assert(C->getType()->isInteger() && "ZEXt operand must be integral");
1475 assert(Ty->isInteger() && "ZExt produces only integer");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001476 assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1477 "SrcTy must be smaller than DestTy for ZExt!");
1478
1479 return getFoldedCast(Instruction::ZExt, C, Ty);
1480}
1481
1482Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
1483 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1484 C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&&
1485 "This is an illegal floating point truncation!");
1486 return getFoldedCast(Instruction::FPTrunc, C, Ty);
1487}
1488
1489Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
1490 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1491 C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1492 "This is an illegal floating point extension!");
1493 return getFoldedCast(Instruction::FPExt, C, Ty);
1494}
1495
1496Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001497 assert(C->getType()->isInteger() && Ty->isFloatingPoint() &&
Reid Spencer2546b762007-01-26 07:37:34 +00001498 "This is an illegal i32 to floating point cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001499 return getFoldedCast(Instruction::UIToFP, C, Ty);
1500}
1501
1502Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001503 assert(C->getType()->isInteger() && Ty->isFloatingPoint() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001504 "This is an illegal sint to floating point cast!");
1505 return getFoldedCast(Instruction::SIToFP, C, Ty);
1506}
1507
1508Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001509 assert(C->getType()->isFloatingPoint() && Ty->isInteger() &&
Reid Spencer2546b762007-01-26 07:37:34 +00001510 "This is an illegal floating point to i32 cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001511 return getFoldedCast(Instruction::FPToUI, C, Ty);
1512}
1513
1514Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001515 assert(C->getType()->isFloatingPoint() && Ty->isInteger() &&
Reid Spencer2546b762007-01-26 07:37:34 +00001516 "This is an illegal floating point to i32 cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001517 return getFoldedCast(Instruction::FPToSI, C, Ty);
1518}
1519
1520Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
1521 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner03c49532007-01-15 02:27:26 +00001522 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001523 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
1524}
1525
1526Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner03c49532007-01-15 02:27:26 +00001527 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001528 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
1529 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
1530}
1531
1532Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
1533 // BitCast implies a no-op cast of type only. No bits change. However, you
1534 // can't cast pointers to anything but pointers.
1535 const Type *SrcTy = C->getType();
1536 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer5c140882006-12-04 20:17:56 +00001537 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001538
1539 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
1540 // or nonptr->ptr). For all the other types, the cast is okay if source and
1541 // destination bit widths are identical.
1542 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1543 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Reid Spencer5c140882006-12-04 20:17:56 +00001544 assert(SrcBitSize == DstBitSize && "BitCast requies types of same width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001545 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerdd284742004-04-04 23:20:30 +00001546}
1547
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001548Constant *ConstantExpr::getSizeOf(const Type *Ty) {
Chris Lattneracc4e542004-12-13 19:48:51 +00001549 // sizeof is implemented as: (ulong) gep (Ty*)null, 1
Chris Lattnerb5d70302007-02-19 20:01:23 +00001550 Constant *GEPIdx = ConstantInt::get(Type::Int32Ty, 1);
1551 Constant *GEP =
1552 getGetElementPtr(getNullValue(PointerType::get(Ty)), &GEPIdx, 1);
1553 return getCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
Alkis Evlogimenos9160d5f2005-03-19 11:40:31 +00001554}
1555
Chris Lattnerb50d1352003-10-05 00:17:43 +00001556Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Reid Spencera009d0d2006-12-04 21:35:24 +00001557 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001558 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00001559 assert(Opcode >= Instruction::BinaryOpsBegin &&
1560 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001561 "Invalid opcode in binary constant expression");
1562 assert(C1->getType() == C2->getType() &&
1563 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001564
Reid Spencer542964f2007-01-11 18:21:29 +00001565 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Chris Lattnerb50d1352003-10-05 00:17:43 +00001566 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1567 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001568
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001569 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001570 ExprMapKeyType Key(Opcode, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001571 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001572}
1573
Reid Spencer266e42b2006-12-23 06:05:41 +00001574Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Reid Spencera009d0d2006-12-04 21:35:24 +00001575 Constant *C1, Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001576 switch (predicate) {
1577 default: assert(0 && "Invalid CmpInst predicate");
1578 case FCmpInst::FCMP_FALSE: case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_OGT:
1579 case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OLE:
1580 case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_ORD: case FCmpInst::FCMP_UNO:
1581 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UGT: case FCmpInst::FCMP_UGE:
1582 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_ULE: case FCmpInst::FCMP_UNE:
1583 case FCmpInst::FCMP_TRUE:
1584 return getFCmp(predicate, C1, C2);
1585 case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_UGT:
1586 case ICmpInst::ICMP_UGE: case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE:
1587 case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_SGE: case ICmpInst::ICMP_SLT:
1588 case ICmpInst::ICMP_SLE:
1589 return getICmp(predicate, C1, C2);
1590 }
Reid Spencera009d0d2006-12-04 21:35:24 +00001591}
1592
1593Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001594#ifndef NDEBUG
1595 switch (Opcode) {
Reid Spencer7eb55b32006-11-02 01:53:59 +00001596 case Instruction::Add:
1597 case Instruction::Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001598 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001599 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattner03c49532007-01-15 02:27:26 +00001600 assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001601 isa<VectorType>(C1->getType())) &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001602 "Tried to create an arithmetic operation on a non-arithmetic type!");
1603 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001604 case Instruction::UDiv:
1605 case Instruction::SDiv:
1606 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001607 assert((C1->getType()->isInteger() || (isa<VectorType>(C1->getType()) &&
1608 cast<VectorType>(C1->getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001609 "Tried to create an arithmetic operation on a non-arithmetic type!");
1610 break;
1611 case Instruction::FDiv:
1612 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001613 assert((C1->getType()->isFloatingPoint() || (isa<VectorType>(C1->getType())
1614 && cast<VectorType>(C1->getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001615 && "Tried to create an arithmetic operation on a non-arithmetic type!");
1616 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001617 case Instruction::URem:
1618 case Instruction::SRem:
1619 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001620 assert((C1->getType()->isInteger() || (isa<VectorType>(C1->getType()) &&
1621 cast<VectorType>(C1->getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001622 "Tried to create an arithmetic operation on a non-arithmetic type!");
1623 break;
1624 case Instruction::FRem:
1625 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001626 assert((C1->getType()->isFloatingPoint() || (isa<VectorType>(C1->getType())
1627 && cast<VectorType>(C1->getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001628 && "Tried to create an arithmetic operation on a non-arithmetic type!");
1629 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001630 case Instruction::And:
1631 case Instruction::Or:
1632 case Instruction::Xor:
1633 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001634 assert((C1->getType()->isInteger() || isa<VectorType>(C1->getType())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001635 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001636 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001637 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001638 case Instruction::LShr:
1639 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00001640 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattner03c49532007-01-15 02:27:26 +00001641 assert(C1->getType()->isInteger() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001642 "Tried to create a shift operation on a non-integer type!");
1643 break;
1644 default:
1645 break;
1646 }
1647#endif
1648
Reid Spencera009d0d2006-12-04 21:35:24 +00001649 return getTy(C1->getType(), Opcode, C1, C2);
1650}
1651
Reid Spencer266e42b2006-12-23 06:05:41 +00001652Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00001653 Constant *C1, Constant *C2) {
1654 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001655 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00001656}
1657
Chris Lattner6e415c02004-03-12 05:54:04 +00001658Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
1659 Constant *V1, Constant *V2) {
Reid Spencer2546b762007-01-26 07:37:34 +00001660 assert(C->getType() == Type::Int1Ty && "Select condition must be i1!");
Chris Lattner6e415c02004-03-12 05:54:04 +00001661 assert(V1->getType() == V2->getType() && "Select value types must match!");
1662 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
1663
1664 if (ReqTy == V1->getType())
1665 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1666 return SC; // Fold common cases
1667
1668 std::vector<Constant*> argVec(3, C);
1669 argVec[1] = V1;
1670 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00001671 ExprMapKeyType Key(Instruction::Select, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001672 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00001673}
1674
Chris Lattnerb50d1352003-10-05 00:17:43 +00001675Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00001676 Value* const *Idxs,
1677 unsigned NumIdx) {
1678 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs, NumIdx, true) &&
Chris Lattner04b60fe2004-02-16 20:46:13 +00001679 "GEP indices invalid!");
1680
Chris Lattner302116a2007-01-31 04:40:28 +00001681 if (Constant *FC = ConstantFoldGetElementPtr(C, (Constant**)Idxs, NumIdx))
Chris Lattneracdbe712003-04-17 19:24:48 +00001682 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001683
Chris Lattnerb50d1352003-10-05 00:17:43 +00001684 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001685 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001686 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00001687 std::vector<Constant*> ArgVec;
Chris Lattner302116a2007-01-31 04:40:28 +00001688 ArgVec.reserve(NumIdx+1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001689 ArgVec.push_back(C);
Chris Lattner302116a2007-01-31 04:40:28 +00001690 for (unsigned i = 0; i != NumIdx; ++i)
1691 ArgVec.push_back(cast<Constant>(Idxs[i]));
1692 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001693 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001694}
1695
Chris Lattner302116a2007-01-31 04:40:28 +00001696Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
1697 unsigned NumIdx) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00001698 // Get the result type of the getelementptr!
Chris Lattner302116a2007-01-31 04:40:28 +00001699 const Type *Ty =
1700 GetElementPtrInst::getIndexedType(C->getType(), Idxs, NumIdx, true);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001701 assert(Ty && "GEP indices invalid!");
Chris Lattner302116a2007-01-31 04:40:28 +00001702 return getGetElementPtrTy(PointerType::get(Ty), C, Idxs, NumIdx);
Chris Lattner13128ab2004-10-11 22:52:25 +00001703}
1704
Chris Lattner302116a2007-01-31 04:40:28 +00001705Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
1706 unsigned NumIdx) {
1707 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001708}
1709
Chris Lattner302116a2007-01-31 04:40:28 +00001710
Reid Spenceree3c9912006-12-04 05:19:50 +00001711Constant *
1712ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1713 assert(LHS->getType() == RHS->getType());
1714 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
1715 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
1716
Reid Spencer266e42b2006-12-23 06:05:41 +00001717 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001718 return FC; // Fold a few common cases...
1719
1720 // Look up the constant in the table first to ensure uniqueness
1721 std::vector<Constant*> ArgVec;
1722 ArgVec.push_back(LHS);
1723 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001724 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001725 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Reid Spencer542964f2007-01-11 18:21:29 +00001726 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001727}
1728
1729Constant *
1730ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1731 assert(LHS->getType() == RHS->getType());
1732 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
1733
Reid Spencer266e42b2006-12-23 06:05:41 +00001734 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001735 return FC; // Fold a few common cases...
1736
1737 // Look up the constant in the table first to ensure uniqueness
1738 std::vector<Constant*> ArgVec;
1739 ArgVec.push_back(LHS);
1740 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001741 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001742 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Reid Spencer542964f2007-01-11 18:21:29 +00001743 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001744}
1745
Robert Bocchino23004482006-01-10 19:05:34 +00001746Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
1747 Constant *Idx) {
Robert Bocchinode7f1c92006-01-10 20:03:46 +00001748 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
1749 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00001750 // Look up the constant in the table first to ensure uniqueness
1751 std::vector<Constant*> ArgVec(1, Val);
1752 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001753 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001754 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00001755}
1756
1757Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001758 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001759 "Tried to create extractelement operation on non-vector type!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001760 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00001761 "Extractelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001762 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +00001763 Val, Idx);
1764}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001765
Robert Bocchinoca27f032006-01-17 20:07:22 +00001766Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
1767 Constant *Elt, Constant *Idx) {
1768 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
1769 return FC; // Fold a few common cases...
1770 // Look up the constant in the table first to ensure uniqueness
1771 std::vector<Constant*> ArgVec(1, Val);
1772 ArgVec.push_back(Elt);
1773 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001774 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001775 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001776}
1777
1778Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1779 Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001780 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001781 "Tried to create insertelement operation on non-vector type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001782 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoca27f032006-01-17 20:07:22 +00001783 && "Insertelement types must match!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001784 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00001785 "Insertelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001786 return getInsertElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchinoca27f032006-01-17 20:07:22 +00001787 Val, Elt, Idx);
1788}
1789
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001790Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
1791 Constant *V2, Constant *Mask) {
1792 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
1793 return FC; // Fold a few common cases...
1794 // Look up the constant in the table first to ensure uniqueness
1795 std::vector<Constant*> ArgVec(1, V1);
1796 ArgVec.push_back(V2);
1797 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00001798 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001799 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001800}
1801
1802Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
1803 Constant *Mask) {
1804 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
1805 "Invalid shuffle vector constant expr operands!");
1806 return getShuffleVectorTy(V1->getType(), V1, V2, Mask);
1807}
1808
Reid Spencer2eadb532007-01-21 00:29:26 +00001809Constant *ConstantExpr::getZeroValueForNegationExpr(const Type *Ty) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001810 if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
Reid Spencer6598ca82007-01-21 02:29:10 +00001811 if (PTy->getElementType()->isFloatingPoint()) {
1812 std::vector<Constant*> zeros(PTy->getNumElements(),
1813 ConstantFP::get(PTy->getElementType(),-0.0));
Reid Spencerd84d35b2007-02-15 02:26:10 +00001814 return ConstantVector::get(PTy, zeros);
Reid Spencer6598ca82007-01-21 02:29:10 +00001815 }
Reid Spencer2eadb532007-01-21 00:29:26 +00001816
1817 if (Ty->isFloatingPoint())
1818 return ConstantFP::get(Ty, -0.0);
1819
1820 return Constant::getNullValue(Ty);
1821}
1822
Vikram S. Adve4c485332002-07-15 18:19:33 +00001823// destroyConstant - Remove the constant from the constant table...
1824//
1825void ConstantExpr::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001826 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001827 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001828}
1829
Chris Lattner3cd8c562002-07-30 18:54:25 +00001830const char *ConstantExpr::getOpcodeName() const {
1831 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001832}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00001833
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001834//===----------------------------------------------------------------------===//
1835// replaceUsesOfWithOnConstant implementations
1836
1837void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001838 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001839 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00001840 Constant *ToC = cast<Constant>(To);
Chris Lattnerdff59112005-10-04 18:47:09 +00001841
1842 unsigned OperandToUpdate = U-OperandList;
1843 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1844
Jim Laskeyc03caef2006-07-17 17:38:29 +00001845 std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnerb64419a2005-10-03 22:51:37 +00001846 Lookup.first.first = getType();
1847 Lookup.second = this;
Chris Lattnerdff59112005-10-04 18:47:09 +00001848
Chris Lattnerb64419a2005-10-03 22:51:37 +00001849 std::vector<Constant*> &Values = Lookup.first.second;
1850 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattnerdff59112005-10-04 18:47:09 +00001851
Chris Lattner8760ec72005-10-04 01:17:50 +00001852 // Fill values with the modified operands of the constant array. Also,
1853 // compute whether this turns into an all-zeros array.
Chris Lattnerdff59112005-10-04 18:47:09 +00001854 bool isAllZeros = false;
1855 if (!ToC->isNullValue()) {
1856 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1857 Values.push_back(cast<Constant>(O->get()));
1858 } else {
1859 isAllZeros = true;
1860 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1861 Constant *Val = cast<Constant>(O->get());
1862 Values.push_back(Val);
1863 if (isAllZeros) isAllZeros = Val->isNullValue();
1864 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001865 }
Chris Lattnerdff59112005-10-04 18:47:09 +00001866 Values[OperandToUpdate] = ToC;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001867
Chris Lattnerb64419a2005-10-03 22:51:37 +00001868 Constant *Replacement = 0;
1869 if (isAllZeros) {
1870 Replacement = ConstantAggregateZero::get(getType());
1871 } else {
1872 // Check to see if we have this array type already.
1873 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00001874 ArrayConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00001875 ArrayConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001876
1877 if (Exists) {
1878 Replacement = I->second;
1879 } else {
1880 // Okay, the new shape doesn't exist in the system yet. Instead of
1881 // creating a new constant array, inserting it, replaceallusesof'ing the
1882 // old with the new, then deleting the old... just update the current one
1883 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00001884 ArrayConstants->MoveConstantToNewSlot(this, I);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001885
Chris Lattnerdff59112005-10-04 18:47:09 +00001886 // Update to the new value.
1887 setOperand(OperandToUpdate, ToC);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001888 return;
1889 }
1890 }
1891
1892 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001893 assert(Replacement != this && "I didn't contain From!");
1894
Chris Lattner7a1450d2005-10-04 18:13:04 +00001895 // Everyone using this now uses the replacement.
1896 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001897
1898 // Delete the old constant!
1899 destroyConstant();
1900}
1901
1902void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001903 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001904 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00001905 Constant *ToC = cast<Constant>(To);
1906
Chris Lattnerdff59112005-10-04 18:47:09 +00001907 unsigned OperandToUpdate = U-OperandList;
1908 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1909
Jim Laskeyc03caef2006-07-17 17:38:29 +00001910 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattner8760ec72005-10-04 01:17:50 +00001911 Lookup.first.first = getType();
1912 Lookup.second = this;
1913 std::vector<Constant*> &Values = Lookup.first.second;
1914 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001915
Chris Lattnerdff59112005-10-04 18:47:09 +00001916
Chris Lattner8760ec72005-10-04 01:17:50 +00001917 // Fill values with the modified operands of the constant struct. Also,
1918 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00001919 bool isAllZeros = false;
1920 if (!ToC->isNullValue()) {
1921 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1922 Values.push_back(cast<Constant>(O->get()));
1923 } else {
1924 isAllZeros = true;
1925 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1926 Constant *Val = cast<Constant>(O->get());
1927 Values.push_back(Val);
1928 if (isAllZeros) isAllZeros = Val->isNullValue();
1929 }
Chris Lattner8760ec72005-10-04 01:17:50 +00001930 }
Chris Lattnerdff59112005-10-04 18:47:09 +00001931 Values[OperandToUpdate] = ToC;
1932
Chris Lattner8760ec72005-10-04 01:17:50 +00001933 Constant *Replacement = 0;
1934 if (isAllZeros) {
1935 Replacement = ConstantAggregateZero::get(getType());
1936 } else {
1937 // Check to see if we have this array type already.
1938 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00001939 StructConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00001940 StructConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00001941
1942 if (Exists) {
1943 Replacement = I->second;
1944 } else {
1945 // Okay, the new shape doesn't exist in the system yet. Instead of
1946 // creating a new constant struct, inserting it, replaceallusesof'ing the
1947 // old with the new, then deleting the old... just update the current one
1948 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00001949 StructConstants->MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00001950
Chris Lattnerdff59112005-10-04 18:47:09 +00001951 // Update to the new value.
1952 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00001953 return;
1954 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001955 }
1956
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001957 assert(Replacement != this && "I didn't contain From!");
1958
Chris Lattner7a1450d2005-10-04 18:13:04 +00001959 // Everyone using this now uses the replacement.
1960 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001961
1962 // Delete the old constant!
1963 destroyConstant();
1964}
1965
Reid Spencerd84d35b2007-02-15 02:26:10 +00001966void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001967 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001968 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
1969
1970 std::vector<Constant*> Values;
1971 Values.reserve(getNumOperands()); // Build replacement array...
1972 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1973 Constant *Val = getOperand(i);
1974 if (Val == From) Val = cast<Constant>(To);
1975 Values.push_back(Val);
1976 }
1977
Reid Spencerd84d35b2007-02-15 02:26:10 +00001978 Constant *Replacement = ConstantVector::get(getType(), Values);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001979 assert(Replacement != this && "I didn't contain From!");
1980
Chris Lattner7a1450d2005-10-04 18:13:04 +00001981 // Everyone using this now uses the replacement.
1982 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001983
1984 // Delete the old constant!
1985 destroyConstant();
1986}
1987
1988void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001989 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001990 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
1991 Constant *To = cast<Constant>(ToV);
1992
1993 Constant *Replacement = 0;
1994 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerb5d70302007-02-19 20:01:23 +00001995 SmallVector<Constant*, 8> Indices;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001996 Constant *Pointer = getOperand(0);
1997 Indices.reserve(getNumOperands()-1);
1998 if (Pointer == From) Pointer = To;
1999
2000 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2001 Constant *Val = getOperand(i);
2002 if (Val == From) Val = To;
2003 Indices.push_back(Val);
2004 }
Chris Lattnerb5d70302007-02-19 20:01:23 +00002005 Replacement = ConstantExpr::getGetElementPtr(Pointer,
2006 &Indices[0], Indices.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002007 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002008 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002009 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002010 } else if (getOpcode() == Instruction::Select) {
2011 Constant *C1 = getOperand(0);
2012 Constant *C2 = getOperand(1);
2013 Constant *C3 = getOperand(2);
2014 if (C1 == From) C1 = To;
2015 if (C2 == From) C2 = To;
2016 if (C3 == From) C3 = To;
2017 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00002018 } else if (getOpcode() == Instruction::ExtractElement) {
2019 Constant *C1 = getOperand(0);
2020 Constant *C2 = getOperand(1);
2021 if (C1 == From) C1 = To;
2022 if (C2 == From) C2 = To;
2023 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00002024 } else if (getOpcode() == Instruction::InsertElement) {
2025 Constant *C1 = getOperand(0);
2026 Constant *C2 = getOperand(1);
2027 Constant *C3 = getOperand(1);
2028 if (C1 == From) C1 = To;
2029 if (C2 == From) C2 = To;
2030 if (C3 == From) C3 = To;
2031 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2032 } else if (getOpcode() == Instruction::ShuffleVector) {
2033 Constant *C1 = getOperand(0);
2034 Constant *C2 = getOperand(1);
2035 Constant *C3 = getOperand(2);
2036 if (C1 == From) C1 = To;
2037 if (C2 == From) C2 = To;
2038 if (C3 == From) C3 = To;
2039 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00002040 } else if (isCompare()) {
2041 Constant *C1 = getOperand(0);
2042 Constant *C2 = getOperand(1);
2043 if (C1 == From) C1 = To;
2044 if (C2 == From) C2 = To;
2045 if (getOpcode() == Instruction::ICmp)
2046 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
2047 else
2048 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002049 } else if (getNumOperands() == 2) {
2050 Constant *C1 = getOperand(0);
2051 Constant *C2 = getOperand(1);
2052 if (C1 == From) C1 = To;
2053 if (C2 == From) C2 = To;
2054 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2055 } else {
2056 assert(0 && "Unknown ConstantExpr type!");
2057 return;
2058 }
2059
2060 assert(Replacement != this && "I didn't contain From!");
2061
Chris Lattner7a1450d2005-10-04 18:13:04 +00002062 // Everyone using this now uses the replacement.
2063 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002064
2065 // Delete the old constant!
2066 destroyConstant();
2067}
2068
2069
Jim Laskey2698f0d2006-03-08 18:11:07 +00002070/// getStringValue - Turn an LLVM constant pointer that eventually points to a
2071/// global into a string value. Return an empty string if we can't do it.
Evan Cheng38280c02006-03-10 23:52:03 +00002072/// Parameter Chop determines if the result is chopped at the first null
2073/// terminator.
Jim Laskey2698f0d2006-03-08 18:11:07 +00002074///
Evan Cheng38280c02006-03-10 23:52:03 +00002075std::string Constant::getStringValue(bool Chop, unsigned Offset) {
Jim Laskey2698f0d2006-03-08 18:11:07 +00002076 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(this)) {
2077 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
2078 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
2079 if (Init->isString()) {
2080 std::string Result = Init->getAsString();
2081 if (Offset < Result.size()) {
2082 // If we are pointing INTO The string, erase the beginning...
2083 Result.erase(Result.begin(), Result.begin()+Offset);
2084
2085 // Take off the null terminator, and any string fragments after it.
Evan Cheng38280c02006-03-10 23:52:03 +00002086 if (Chop) {
2087 std::string::size_type NullPos = Result.find_first_of((char)0);
2088 if (NullPos != std::string::npos)
2089 Result.erase(Result.begin()+NullPos, Result.end());
2090 }
Jim Laskey2698f0d2006-03-08 18:11:07 +00002091 return Result;
2092 }
2093 }
2094 }
2095 } else if (Constant *C = dyn_cast<Constant>(this)) {
2096 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
Evan Cheng2c5e5302006-03-11 00:13:10 +00002097 return GV->getStringValue(Chop, Offset);
Jim Laskey2698f0d2006-03-08 18:11:07 +00002098 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
2099 if (CE->getOpcode() == Instruction::GetElementPtr) {
2100 // Turn a gep into the specified offset.
2101 if (CE->getNumOperands() == 3 &&
2102 cast<Constant>(CE->getOperand(1))->isNullValue() &&
2103 isa<ConstantInt>(CE->getOperand(2))) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00002104 Offset += cast<ConstantInt>(CE->getOperand(2))->getZExtValue();
Evan Cheng2c5e5302006-03-11 00:13:10 +00002105 return CE->getOperand(0)->getStringValue(Chop, Offset);
Jim Laskey2698f0d2006-03-08 18:11:07 +00002106 }
2107 }
2108 }
2109 }
2110 return "";
2111}