blob: 816ffba4b55aacf64ff6030c32cd870ed0749692 [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 Lattnera80bf0b2007-02-20 06:39:57 +000025#include "llvm/ADT/DenseMap.h"
Chris Lattnerb5d70302007-02-19 20:01:23 +000026#include "llvm/ADT/SmallVector.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000027#include <algorithm>
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000028#include <map>
Chris Lattner189d19f2003-11-21 20:23:48 +000029using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000030
Chris Lattner2f7c9632001-06-06 20:29:01 +000031//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000032// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000033//===----------------------------------------------------------------------===//
34
Chris Lattner3462ae32001-12-03 22:26:30 +000035void Constant::destroyConstantImpl() {
36 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000037 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000038 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +000039 // but they don't know that. Because we only find out when the CPV is
40 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000041 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000042 //
43 while (!use_empty()) {
44 Value *V = use_back();
45#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000046 if (!isa<Constant>(V))
Bill Wendling6a462f12006-11-17 08:03:48 +000047 DOUT << "While deleting: " << *this
48 << "\n\nUse still stuck around after Def is destroyed: "
49 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000050#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000051 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000052 Constant *CV = cast<Constant>(V);
53 CV->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +000054
55 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000056 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000057 }
58
59 // Value has no outstanding references it is safe to delete it now...
60 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000061}
Chris Lattner2f7c9632001-06-06 20:29:01 +000062
Chris Lattner23dd1f62006-10-20 00:27:06 +000063/// canTrap - Return true if evaluation of this constant could trap. This is
64/// true for things like constant expressions that could divide by zero.
65bool Constant::canTrap() const {
66 assert(getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
67 // The only thing that could possibly trap are constant exprs.
68 const ConstantExpr *CE = dyn_cast<ConstantExpr>(this);
69 if (!CE) return false;
70
71 // ConstantExpr traps if any operands can trap.
72 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
73 if (getOperand(i)->canTrap())
74 return true;
75
76 // Otherwise, only specific operations can trap.
77 switch (CE->getOpcode()) {
78 default:
79 return false;
Reid Spencer7e80b0b2006-10-26 06:15:43 +000080 case Instruction::UDiv:
81 case Instruction::SDiv:
82 case Instruction::FDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +000083 case Instruction::URem:
84 case Instruction::SRem:
85 case Instruction::FRem:
Chris Lattner23dd1f62006-10-20 00:27:06 +000086 // Div and rem can trap if the RHS is not known to be non-zero.
87 if (!isa<ConstantInt>(getOperand(1)) || getOperand(1)->isNullValue())
88 return true;
89 return false;
90 }
91}
92
Chris Lattnerb1585a92002-08-13 17:50:20 +000093// Static constructor to create a '0' constant of arbitrary type...
94Constant *Constant::getNullValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +000095 switch (Ty->getTypeID()) {
Chris Lattnerdbcb0d32007-02-20 05:46:39 +000096 case Type::IntegerTyID:
97 return ConstantInt::get(Ty, 0);
98 case Type::FloatTyID:
99 case Type::DoubleTyID:
100 return ConstantFP::get(Ty, 0.0);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000101 case Type::PointerTyID:
Chris Lattnerb1585a92002-08-13 17:50:20 +0000102 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner9fba3da2004-02-15 05:53:04 +0000103 case Type::StructTyID:
104 case Type::ArrayTyID:
Reid Spencerd84d35b2007-02-15 02:26:10 +0000105 case Type::VectorTyID:
Chris Lattner9fba3da2004-02-15 05:53:04 +0000106 return ConstantAggregateZero::get(Ty);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000107 default:
Reid Spencercf394bf2004-07-04 11:51:24 +0000108 // Function, Label, or Opaque type?
109 assert(!"Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000110 return 0;
111 }
112}
113
Chris Lattnerb1585a92002-08-13 17:50:20 +0000114
115// Static constructor to create an integral constant with all bits set
Zhou Sheng75b871f2007-01-11 12:24:14 +0000116ConstantInt *ConstantInt::getAllOnesValue(const Type *Ty) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000117 if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty))
118 if (ITy->getBitWidth() == 1)
119 return ConstantInt::getTrue();
120 else
121 return ConstantInt::get(Ty, int64_t(-1));
122 return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000123}
124
Chris Lattnerecab54c2007-01-04 01:49:26 +0000125/// @returns the value for an packed integer constant of the given type that
126/// has all its bits set to true.
127/// @brief Get the all ones value
Reid Spencerd84d35b2007-02-15 02:26:10 +0000128ConstantVector *ConstantVector::getAllOnesValue(const VectorType *Ty) {
Chris Lattnerecab54c2007-01-04 01:49:26 +0000129 std::vector<Constant*> Elts;
130 Elts.resize(Ty->getNumElements(),
Zhou Sheng75b871f2007-01-11 12:24:14 +0000131 ConstantInt::getAllOnesValue(Ty->getElementType()));
Chris Lattnerecab54c2007-01-04 01:49:26 +0000132 assert(Elts[0] && "Not a packed integer type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +0000133 return cast<ConstantVector>(ConstantVector::get(Elts));
Chris Lattnerecab54c2007-01-04 01:49:26 +0000134}
135
136
Chris Lattner2f7c9632001-06-06 20:29:01 +0000137//===----------------------------------------------------------------------===//
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000138// ConstantInt
Chris Lattner2f7c9632001-06-06 20:29:01 +0000139//===----------------------------------------------------------------------===//
140
Chris Lattner5db2f472007-02-20 05:55:46 +0000141ConstantInt::ConstantInt(const IntegerType *Ty, uint64_t V)
142 : Constant(Ty, ConstantIntVal, 0, 0), Val(V) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000143}
144
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000145ConstantInt *ConstantInt::TheTrueVal = 0;
146ConstantInt *ConstantInt::TheFalseVal = 0;
147
148namespace llvm {
149 void CleanupTrueFalse(void *) {
150 ConstantInt::ResetTrueFalse();
151 }
152}
153
154static ManagedCleanup<llvm::CleanupTrueFalse> TrueFalseCleanup;
155
156ConstantInt *ConstantInt::CreateTrueFalseVals(bool WhichOne) {
157 assert(TheTrueVal == 0 && TheFalseVal == 0);
158 TheTrueVal = get(Type::Int1Ty, 1);
159 TheFalseVal = get(Type::Int1Ty, 0);
160
161 // Ensure that llvm_shutdown nulls out TheTrueVal/TheFalseVal.
162 TrueFalseCleanup.Register();
163
164 return WhichOne ? TheTrueVal : TheFalseVal;
165}
166
167
168//---- ConstantInt::get() implementations...
169//
170// Provide DenseMapKeyInfo for all pointers.
171namespace {
172 struct DenseMapIntegerKeyInfo {
173 typedef std::pair<uint64_t, const IntegerType*> KeyTy;
174 static inline KeyTy getEmptyKey() { return KeyTy(0, 0); }
175 static inline KeyTy getTombstoneKey() { return KeyTy(1, 0); }
176 static unsigned getHashValue(const KeyTy &Key) {
177 return DenseMapKeyInfo<void*>::getHashValue(Key.second) ^ Key.first;
178 }
179 static bool isPod() { return true; }
180 };
181}
182
183
184typedef DenseMap<DenseMapIntegerKeyInfo::KeyTy, ConstantInt*,
185DenseMapIntegerKeyInfo> IntMapTy;
186static ManagedStatic<IntMapTy> IntConstants;
187
188// Get a ConstantInt from an int64_t. Note here that we canoncialize the value
189// to a uint64_t value that has been zero extended down to the size of the
190// integer type of the ConstantInt. This allows the getZExtValue method to
191// just return the stored value while getSExtValue has to convert back to sign
192// extended. getZExtValue is more common in LLVM than getSExtValue().
193ConstantInt *ConstantInt::get(const Type *Ty, int64_t V) {
194 const IntegerType *ITy = cast<IntegerType>(Ty);
195 V &= ITy->getBitMask();
196 ConstantInt *&Slot = (*IntConstants)[std::make_pair(uint64_t(V), ITy)];
197 if (Slot) return Slot;
198 return Slot = new ConstantInt(ITy, V);
199}
200
201//===----------------------------------------------------------------------===//
202// ConstantXXX Classes
203//===----------------------------------------------------------------------===//
204
205
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000206ConstantFP::ConstantFP(const Type *Ty, double V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000207 : Constant(Ty, ConstantFPVal, 0, 0) {
Chris Lattner9655e542001-07-20 19:16:02 +0000208 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000209 Val = V;
210}
211
Chris Lattner3462ae32001-12-03 22:26:30 +0000212ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000213 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000214 : Constant(T, ConstantArrayVal, new Use[V.size()], V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000215 assert(V.size() == T->getNumElements() &&
216 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000217 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000218 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
219 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000220 Constant *C = *I;
221 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000222 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000223 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000224 "Initializer for array element doesn't match array element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000225 OL->init(C, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000226 }
227}
228
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000229ConstantArray::~ConstantArray() {
230 delete [] OperandList;
231}
232
Chris Lattner3462ae32001-12-03 22:26:30 +0000233ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000234 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000235 : Constant(T, ConstantStructVal, new Use[V.size()], V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000236 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000237 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000238 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000239 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
240 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000241 Constant *C = *I;
242 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000243 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000244 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000245 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000246 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000247 "Initializer for struct element doesn't match struct element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000248 OL->init(C, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000249 }
250}
251
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000252ConstantStruct::~ConstantStruct() {
253 delete [] OperandList;
254}
255
256
Reid Spencerd84d35b2007-02-15 02:26:10 +0000257ConstantVector::ConstantVector(const VectorType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000258 const std::vector<Constant*> &V)
Reid Spencerd84d35b2007-02-15 02:26:10 +0000259 : Constant(T, ConstantVectorVal, new Use[V.size()], V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000260 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000261 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
262 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000263 Constant *C = *I;
264 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000265 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000266 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000267 "Initializer for packed element doesn't match packed element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000268 OL->init(C, this);
Brian Gaeke02209042004-08-20 06:00:58 +0000269 }
270}
271
Reid Spencerd84d35b2007-02-15 02:26:10 +0000272ConstantVector::~ConstantVector() {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000273 delete [] OperandList;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000274}
275
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000276// We declare several classes private to this file, so use an anonymous
277// namespace
278namespace {
279
280/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
281/// behind the scenes to implement unary constant exprs.
282class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
283 Use Op;
284public:
285 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
286 : ConstantExpr(Ty, Opcode, &Op, 1), Op(C, this) {}
287};
288
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000289/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
290/// behind the scenes to implement binary constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000291class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000292 Use Ops[2];
293public:
294 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Reid Spencer266e42b2006-12-23 06:05:41 +0000295 : ConstantExpr(C1->getType(), Opcode, Ops, 2) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000296 Ops[0].init(C1, this);
297 Ops[1].init(C2, this);
298 }
299};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000300
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000301/// SelectConstantExpr - This class is private to Constants.cpp, and is used
302/// behind the scenes to implement select constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000303class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000304 Use Ops[3];
305public:
306 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
307 : ConstantExpr(C2->getType(), Instruction::Select, Ops, 3) {
308 Ops[0].init(C1, this);
309 Ops[1].init(C2, this);
310 Ops[2].init(C3, this);
311 }
312};
313
Robert Bocchinoca27f032006-01-17 20:07:22 +0000314/// ExtractElementConstantExpr - This class is private to
315/// Constants.cpp, and is used behind the scenes to implement
316/// extractelement constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000317class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Robert Bocchino23004482006-01-10 19:05:34 +0000318 Use Ops[2];
319public:
320 ExtractElementConstantExpr(Constant *C1, Constant *C2)
Reid Spencerd84d35b2007-02-15 02:26:10 +0000321 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +0000322 Instruction::ExtractElement, Ops, 2) {
323 Ops[0].init(C1, this);
324 Ops[1].init(C2, this);
325 }
326};
327
Robert Bocchinoca27f032006-01-17 20:07:22 +0000328/// InsertElementConstantExpr - This class is private to
329/// Constants.cpp, and is used behind the scenes to implement
330/// insertelement constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000331class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Robert Bocchinoca27f032006-01-17 20:07:22 +0000332 Use Ops[3];
333public:
334 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
335 : ConstantExpr(C1->getType(), Instruction::InsertElement,
336 Ops, 3) {
337 Ops[0].init(C1, this);
338 Ops[1].init(C2, this);
339 Ops[2].init(C3, this);
340 }
341};
342
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000343/// ShuffleVectorConstantExpr - This class is private to
344/// Constants.cpp, and is used behind the scenes to implement
345/// shufflevector constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000346class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000347 Use Ops[3];
348public:
349 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
350 : ConstantExpr(C1->getType(), Instruction::ShuffleVector,
351 Ops, 3) {
352 Ops[0].init(C1, this);
353 Ops[1].init(C2, this);
354 Ops[2].init(C3, this);
355 }
356};
357
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000358/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
359/// used behind the scenes to implement getelementpr constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000360struct VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000361 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
362 const Type *DestTy)
363 : ConstantExpr(DestTy, Instruction::GetElementPtr,
364 new Use[IdxList.size()+1], IdxList.size()+1) {
365 OperandList[0].init(C, this);
366 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
367 OperandList[i+1].init(IdxList[i], this);
368 }
369 ~GetElementPtrConstantExpr() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000370 delete [] OperandList;
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000371 }
372};
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000373
374// CompareConstantExpr - This class is private to Constants.cpp, and is used
375// behind the scenes to implement ICmp and FCmp constant expressions. This is
376// needed in order to store the predicate value for these instructions.
377struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr {
378 unsigned short predicate;
379 Use Ops[2];
380 CompareConstantExpr(Instruction::OtherOps opc, unsigned short pred,
381 Constant* LHS, Constant* RHS)
Reid Spencer542964f2007-01-11 18:21:29 +0000382 : ConstantExpr(Type::Int1Ty, opc, Ops, 2), predicate(pred) {
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000383 OperandList[0].init(LHS, this);
384 OperandList[1].init(RHS, this);
385 }
386};
387
388} // end anonymous namespace
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000389
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000390
391// Utility function for determining if a ConstantExpr is a CastOp or not. This
392// can't be inline because we don't want to #include Instruction.h into
393// Constant.h
394bool ConstantExpr::isCast() const {
395 return Instruction::isCast(getOpcode());
396}
397
Reid Spenceree3c9912006-12-04 05:19:50 +0000398bool ConstantExpr::isCompare() const {
399 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
400}
401
Chris Lattner817175f2004-03-29 02:37:53 +0000402/// ConstantExpr::get* - Return some common constants without having to
403/// specify the full Instruction::OPCODE identifier.
404///
405Constant *ConstantExpr::getNeg(Constant *C) {
Reid Spencer2eadb532007-01-21 00:29:26 +0000406 return get(Instruction::Sub,
407 ConstantExpr::getZeroValueForNegationExpr(C->getType()),
408 C);
Chris Lattner817175f2004-03-29 02:37:53 +0000409}
410Constant *ConstantExpr::getNot(Constant *C) {
Zhou Sheng75b871f2007-01-11 12:24:14 +0000411 assert(isa<ConstantInt>(C) && "Cannot NOT a nonintegral type!");
Chris Lattner817175f2004-03-29 02:37:53 +0000412 return get(Instruction::Xor, C,
Zhou Sheng75b871f2007-01-11 12:24:14 +0000413 ConstantInt::getAllOnesValue(C->getType()));
Chris Lattner817175f2004-03-29 02:37:53 +0000414}
415Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
416 return get(Instruction::Add, C1, C2);
417}
418Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
419 return get(Instruction::Sub, C1, C2);
420}
421Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
422 return get(Instruction::Mul, C1, C2);
423}
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000424Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2) {
425 return get(Instruction::UDiv, C1, C2);
426}
427Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2) {
428 return get(Instruction::SDiv, C1, C2);
429}
430Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) {
431 return get(Instruction::FDiv, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000432}
Reid Spencer7eb55b32006-11-02 01:53:59 +0000433Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) {
434 return get(Instruction::URem, C1, C2);
435}
436Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) {
437 return get(Instruction::SRem, C1, C2);
438}
439Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) {
440 return get(Instruction::FRem, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000441}
442Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
443 return get(Instruction::And, C1, C2);
444}
445Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
446 return get(Instruction::Or, C1, C2);
447}
448Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
449 return get(Instruction::Xor, C1, C2);
450}
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000451unsigned ConstantExpr::getPredicate() const {
452 assert(getOpcode() == Instruction::FCmp || getOpcode() == Instruction::ICmp);
453 return dynamic_cast<const CompareConstantExpr*>(this)->predicate;
454}
Chris Lattner817175f2004-03-29 02:37:53 +0000455Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
456 return get(Instruction::Shl, C1, C2);
457}
Reid Spencerfdff9382006-11-08 06:47:33 +0000458Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2) {
459 return get(Instruction::LShr, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000460}
Reid Spencerfdff9382006-11-08 06:47:33 +0000461Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2) {
462 return get(Instruction::AShr, C1, C2);
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000463}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000464
Chris Lattner7c1018a2006-07-14 19:37:40 +0000465/// getWithOperandReplaced - Return a constant expression identical to this
466/// one, but with the specified operand set to the specified value.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000467Constant *
468ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +0000469 assert(OpNo < getNumOperands() && "Operand num is out of range!");
470 assert(Op->getType() == getOperand(OpNo)->getType() &&
471 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000472 if (getOperand(OpNo) == Op)
473 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000474
Chris Lattner227816342006-07-14 22:20:01 +0000475 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000476 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000477 case Instruction::Trunc:
478 case Instruction::ZExt:
479 case Instruction::SExt:
480 case Instruction::FPTrunc:
481 case Instruction::FPExt:
482 case Instruction::UIToFP:
483 case Instruction::SIToFP:
484 case Instruction::FPToUI:
485 case Instruction::FPToSI:
486 case Instruction::PtrToInt:
487 case Instruction::IntToPtr:
488 case Instruction::BitCast:
489 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000490 case Instruction::Select:
491 Op0 = (OpNo == 0) ? Op : getOperand(0);
492 Op1 = (OpNo == 1) ? Op : getOperand(1);
493 Op2 = (OpNo == 2) ? Op : getOperand(2);
494 return ConstantExpr::getSelect(Op0, Op1, Op2);
495 case Instruction::InsertElement:
496 Op0 = (OpNo == 0) ? Op : getOperand(0);
497 Op1 = (OpNo == 1) ? Op : getOperand(1);
498 Op2 = (OpNo == 2) ? Op : getOperand(2);
499 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
500 case Instruction::ExtractElement:
501 Op0 = (OpNo == 0) ? Op : getOperand(0);
502 Op1 = (OpNo == 1) ? Op : getOperand(1);
503 return ConstantExpr::getExtractElement(Op0, Op1);
504 case Instruction::ShuffleVector:
505 Op0 = (OpNo == 0) ? Op : getOperand(0);
506 Op1 = (OpNo == 1) ? Op : getOperand(1);
507 Op2 = (OpNo == 2) ? Op : getOperand(2);
508 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000509 case Instruction::GetElementPtr: {
Chris Lattnerb5d70302007-02-19 20:01:23 +0000510 SmallVector<Constant*, 8> Ops;
511 Ops.resize(getNumOperands());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000512 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Chris Lattnerb5d70302007-02-19 20:01:23 +0000513 Ops[i] = getOperand(i);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000514 if (OpNo == 0)
Chris Lattnerb5d70302007-02-19 20:01:23 +0000515 return ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000516 Ops[OpNo-1] = Op;
Chris Lattnerb5d70302007-02-19 20:01:23 +0000517 return ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000518 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000519 default:
520 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000521 Op0 = (OpNo == 0) ? Op : getOperand(0);
522 Op1 = (OpNo == 1) ? Op : getOperand(1);
523 return ConstantExpr::get(getOpcode(), Op0, Op1);
524 }
525}
526
527/// getWithOperands - This returns the current constant expression with the
528/// operands replaced with the specified values. The specified operands must
529/// match count and type with the existing ones.
530Constant *ConstantExpr::
531getWithOperands(const std::vector<Constant*> &Ops) const {
532 assert(Ops.size() == getNumOperands() && "Operand count mismatch!");
533 bool AnyChange = false;
534 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
535 assert(Ops[i]->getType() == getOperand(i)->getType() &&
536 "Operand type mismatch!");
537 AnyChange |= Ops[i] != getOperand(i);
538 }
539 if (!AnyChange) // No operands changed, return self.
540 return const_cast<ConstantExpr*>(this);
541
542 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000543 case Instruction::Trunc:
544 case Instruction::ZExt:
545 case Instruction::SExt:
546 case Instruction::FPTrunc:
547 case Instruction::FPExt:
548 case Instruction::UIToFP:
549 case Instruction::SIToFP:
550 case Instruction::FPToUI:
551 case Instruction::FPToSI:
552 case Instruction::PtrToInt:
553 case Instruction::IntToPtr:
554 case Instruction::BitCast:
555 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattner227816342006-07-14 22:20:01 +0000556 case Instruction::Select:
557 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
558 case Instruction::InsertElement:
559 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
560 case Instruction::ExtractElement:
561 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
562 case Instruction::ShuffleVector:
563 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Chris Lattnerb5d70302007-02-19 20:01:23 +0000564 case Instruction::GetElementPtr:
565 return ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], Ops.size()-1);
Reid Spencer266e42b2006-12-23 06:05:41 +0000566 case Instruction::ICmp:
567 case Instruction::FCmp:
568 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattner227816342006-07-14 22:20:01 +0000569 default:
570 assert(getNumOperands() == 2 && "Must be binary operator?");
571 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000572 }
573}
574
Chris Lattner2f7c9632001-06-06 20:29:01 +0000575
576//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000577// isValueValidForType implementations
578
Reid Spencere7334722006-12-19 01:28:19 +0000579bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000580 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000581 if (Ty == Type::Int1Ty)
582 return Val == 0 || Val == 1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000583 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000584 return true; // always true, has to fit in largest type
585 uint64_t Max = (1ll << NumBits) - 1;
586 return Val <= Max;
Reid Spencere7334722006-12-19 01:28:19 +0000587}
588
Reid Spencere0fc4df2006-10-20 07:07:24 +0000589bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000590 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000591 if (Ty == Type::Int1Ty)
Reid Spencera94d3942007-01-19 21:13:56 +0000592 return Val == 0 || Val == 1 || Val == -1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000593 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000594 return true; // always true, has to fit in largest type
595 int64_t Min = -(1ll << (NumBits-1));
596 int64_t Max = (1ll << (NumBits-1)) - 1;
597 return (Val >= Min && Val <= Max);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000598}
599
Chris Lattner3462ae32001-12-03 22:26:30 +0000600bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000601 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000602 default:
603 return false; // These can't be represented as floating point!
604
Reid Spencerb95f8ab2004-12-07 07:38:08 +0000605 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000606 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000607 case Type::DoubleTyID:
608 return true; // This is the largest type...
609 }
Chris Lattneraa2372562006-05-24 17:04:05 +0000610}
Chris Lattner9655e542001-07-20 19:16:02 +0000611
Chris Lattner49d855c2001-09-07 16:46:31 +0000612//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000613// Factory Function Implementation
614
Chris Lattner98fa07b2003-05-23 20:03:32 +0000615// ConstantCreator - A class that is used to create constants by
616// ValueMap*. This class should be partially specialized if there is
617// something strange that needs to be done to interface to the ctor for the
618// constant.
619//
Chris Lattner189d19f2003-11-21 20:23:48 +0000620namespace llvm {
621 template<class ConstantClass, class TypeClass, class ValType>
Chris Lattner02157b02006-06-28 21:38:54 +0000622 struct VISIBILITY_HIDDEN ConstantCreator {
Chris Lattner189d19f2003-11-21 20:23:48 +0000623 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
624 return new ConstantClass(Ty, V);
625 }
626 };
Misha Brukmanb1c93172005-04-21 23:48:37 +0000627
Chris Lattner189d19f2003-11-21 20:23:48 +0000628 template<class ConstantClass, class TypeClass>
Chris Lattner02157b02006-06-28 21:38:54 +0000629 struct VISIBILITY_HIDDEN ConvertConstantType {
Chris Lattner189d19f2003-11-21 20:23:48 +0000630 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
631 assert(0 && "This type cannot be converted!\n");
632 abort();
633 }
634 };
Chris Lattnerb50d1352003-10-05 00:17:43 +0000635
Chris Lattner935aa922005-10-04 17:48:46 +0000636 template<class ValType, class TypeClass, class ConstantClass,
637 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattner02157b02006-06-28 21:38:54 +0000638 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
Chris Lattnerb64419a2005-10-03 22:51:37 +0000639 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000640 typedef std::pair<const Type*, ValType> MapKey;
641 typedef std::map<MapKey, Constant *> MapTy;
642 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
643 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
Chris Lattnerb64419a2005-10-03 22:51:37 +0000644 private:
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000645 /// Map - This is the main map from the element descriptor to the Constants.
646 /// This is the primary way we avoid creating two of the same shape
647 /// constant.
Chris Lattnerb50d1352003-10-05 00:17:43 +0000648 MapTy Map;
Chris Lattner935aa922005-10-04 17:48:46 +0000649
650 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
651 /// from the constants to their element in Map. This is important for
652 /// removal of constants from the array, which would otherwise have to scan
653 /// through the map with very large keys.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000654 InverseMapTy InverseMap;
Chris Lattnerb50d1352003-10-05 00:17:43 +0000655
Jim Laskeyc03caef2006-07-17 17:38:29 +0000656 /// AbstractTypeMap - Map for abstract type constants.
657 ///
Chris Lattnerb50d1352003-10-05 00:17:43 +0000658 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner99a669b2004-11-19 16:39:44 +0000659
Chris Lattner98fa07b2003-05-23 20:03:32 +0000660 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000661 typename MapTy::iterator map_end() { return Map.end(); }
Chris Lattnerb64419a2005-10-03 22:51:37 +0000662
663 /// InsertOrGetItem - Return an iterator for the specified element.
664 /// If the element exists in the map, the returned iterator points to the
665 /// entry and Exists=true. If not, the iterator points to the newly
666 /// inserted entry and returns Exists=false. Newly inserted entries have
667 /// I->second == 0, and should be filled in.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000668 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
669 &InsertVal,
Chris Lattnerb64419a2005-10-03 22:51:37 +0000670 bool &Exists) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000671 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
Chris Lattnerb64419a2005-10-03 22:51:37 +0000672 Exists = !IP.second;
673 return IP.first;
674 }
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000675
Chris Lattner935aa922005-10-04 17:48:46 +0000676private:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000677 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
Chris Lattner935aa922005-10-04 17:48:46 +0000678 if (HasLargeKey) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000679 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
Chris Lattner935aa922005-10-04 17:48:46 +0000680 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
681 IMI->second->second == CP &&
682 "InverseMap corrupt!");
683 return IMI->second;
684 }
685
Jim Laskeyc03caef2006-07-17 17:38:29 +0000686 typename MapTy::iterator I =
Chris Lattner935aa922005-10-04 17:48:46 +0000687 Map.find(MapKey((TypeClass*)CP->getRawType(), getValType(CP)));
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000688 if (I == Map.end() || I->second != CP) {
689 // FIXME: This should not use a linear scan. If this gets to be a
690 // performance problem, someone should look at this.
691 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
692 /* empty */;
693 }
Chris Lattner935aa922005-10-04 17:48:46 +0000694 return I;
695 }
696public:
697
Chris Lattnerb64419a2005-10-03 22:51:37 +0000698 /// getOrCreate - Return the specified constant from the map, creating it if
699 /// necessary.
Chris Lattner98fa07b2003-05-23 20:03:32 +0000700 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000701 MapKey Lookup(Ty, V);
Jim Laskeyc03caef2006-07-17 17:38:29 +0000702 typename MapTy::iterator I = Map.lower_bound(Lookup);
Reid Spencere0fc4df2006-10-20 07:07:24 +0000703 // Is it in the map?
Chris Lattner98fa07b2003-05-23 20:03:32 +0000704 if (I != Map.end() && I->first == Lookup)
Reid Spencere0fc4df2006-10-20 07:07:24 +0000705 return static_cast<ConstantClass *>(I->second);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000706
707 // If no preexisting value, create one now...
708 ConstantClass *Result =
709 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
710
Chris Lattnerb50d1352003-10-05 00:17:43 +0000711 /// FIXME: why does this assert fail when loading 176.gcc?
712 //assert(Result->getType() == Ty && "Type specified is not correct!");
713 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
714
Chris Lattner935aa922005-10-04 17:48:46 +0000715 if (HasLargeKey) // Remember the reverse mapping if needed.
716 InverseMap.insert(std::make_pair(Result, I));
717
Chris Lattnerb50d1352003-10-05 00:17:43 +0000718 // If the type of the constant is abstract, make sure that an entry exists
719 // for it in the AbstractTypeMap.
720 if (Ty->isAbstract()) {
721 typename AbstractTypeMapTy::iterator TI =
722 AbstractTypeMap.lower_bound(Ty);
723
724 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
725 // Add ourselves to the ATU list of the type.
726 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
727
728 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
729 }
730 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000731 return Result;
732 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000733
Chris Lattner98fa07b2003-05-23 20:03:32 +0000734 void remove(ConstantClass *CP) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000735 typename MapTy::iterator I = FindExistingElement(CP);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000736 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +0000737 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +0000738
Chris Lattner935aa922005-10-04 17:48:46 +0000739 if (HasLargeKey) // Remember the reverse mapping if needed.
740 InverseMap.erase(CP);
741
Chris Lattnerb50d1352003-10-05 00:17:43 +0000742 // Now that we found the entry, make sure this isn't the entry that
743 // the AbstractTypeMap points to.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000744 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000745 if (Ty->isAbstract()) {
746 assert(AbstractTypeMap.count(Ty) &&
747 "Abstract type not in AbstractTypeMap?");
Jim Laskeyc03caef2006-07-17 17:38:29 +0000748 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
Chris Lattnerb50d1352003-10-05 00:17:43 +0000749 if (ATMEntryIt == I) {
750 // Yes, we are removing the representative entry for this type.
751 // See if there are any other entries of the same type.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000752 typename MapTy::iterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000753
Chris Lattnerb50d1352003-10-05 00:17:43 +0000754 // First check the entry before this one...
755 if (TmpIt != Map.begin()) {
756 --TmpIt;
757 if (TmpIt->first.first != Ty) // Not the same type, move back...
758 ++TmpIt;
759 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000760
Chris Lattnerb50d1352003-10-05 00:17:43 +0000761 // If we didn't find the same type, try to move forward...
762 if (TmpIt == ATMEntryIt) {
763 ++TmpIt;
764 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
765 --TmpIt; // No entry afterwards with the same type
766 }
767
768 // If there is another entry in the map of the same abstract type,
769 // update the AbstractTypeMap entry now.
770 if (TmpIt != ATMEntryIt) {
771 ATMEntryIt = TmpIt;
772 } else {
773 // Otherwise, we are removing the last instance of this type
774 // from the table. Remove from the ATM, and from user list.
775 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
776 AbstractTypeMap.erase(Ty);
777 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000778 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000779 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000780
Chris Lattnerb50d1352003-10-05 00:17:43 +0000781 Map.erase(I);
782 }
783
Chris Lattner3b793c62005-10-04 21:35:50 +0000784
785 /// MoveConstantToNewSlot - If we are about to change C to be the element
786 /// specified by I, update our internal data structures to reflect this
787 /// fact.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000788 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
Chris Lattner3b793c62005-10-04 21:35:50 +0000789 // First, remove the old location of the specified constant in the map.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000790 typename MapTy::iterator OldI = FindExistingElement(C);
Chris Lattner3b793c62005-10-04 21:35:50 +0000791 assert(OldI != Map.end() && "Constant not found in constant table!");
792 assert(OldI->second == C && "Didn't find correct element?");
793
794 // If this constant is the representative element for its abstract type,
795 // update the AbstractTypeMap so that the representative element is I.
796 if (C->getType()->isAbstract()) {
797 typename AbstractTypeMapTy::iterator ATI =
798 AbstractTypeMap.find(C->getType());
799 assert(ATI != AbstractTypeMap.end() &&
800 "Abstract type not in AbstractTypeMap?");
801 if (ATI->second == OldI)
802 ATI->second = I;
803 }
804
805 // Remove the old entry from the map.
806 Map.erase(OldI);
807
808 // Update the inverse map so that we know that this constant is now
809 // located at descriptor I.
810 if (HasLargeKey) {
811 assert(I->second == C && "Bad inversemap entry!");
812 InverseMap[C] = I;
813 }
814 }
815
Chris Lattnerb50d1352003-10-05 00:17:43 +0000816 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000817 typename AbstractTypeMapTy::iterator I =
Jim Laskeyc03caef2006-07-17 17:38:29 +0000818 AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +0000819
820 assert(I != AbstractTypeMap.end() &&
821 "Abstract type not in AbstractTypeMap?");
822
823 // Convert a constant at a time until the last one is gone. The last one
824 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
825 // eliminated eventually.
826 do {
827 ConvertConstantType<ConstantClass,
Jim Laskeyc03caef2006-07-17 17:38:29 +0000828 TypeClass>::convert(
829 static_cast<ConstantClass *>(I->second->second),
Chris Lattnerb50d1352003-10-05 00:17:43 +0000830 cast<TypeClass>(NewTy));
831
Jim Laskeyc03caef2006-07-17 17:38:29 +0000832 I = AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +0000833 } while (I != AbstractTypeMap.end());
834 }
835
836 // If the type became concrete without being refined to any other existing
837 // type, we just remove ourselves from the ATU list.
838 void typeBecameConcrete(const DerivedType *AbsTy) {
839 AbsTy->removeAbstractTypeUser(this);
840 }
841
842 void dump() const {
Bill Wendling6a462f12006-11-17 08:03:48 +0000843 DOUT << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +0000844 }
845 };
846}
847
Chris Lattnera84df0a22006-09-28 23:36:21 +0000848
Chris Lattner28173502007-02-20 06:11:36 +0000849
Chris Lattner3462ae32001-12-03 22:26:30 +0000850//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000851//
Chris Lattnerac80ea42004-02-01 22:49:04 +0000852namespace llvm {
853 template<>
854 struct ConstantCreator<ConstantFP, Type, uint64_t> {
855 static ConstantFP *create(const Type *Ty, uint64_t V) {
856 assert(Ty == Type::DoubleTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000857 return new ConstantFP(Ty, BitsToDouble(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000858 }
859 };
860 template<>
861 struct ConstantCreator<ConstantFP, Type, uint32_t> {
862 static ConstantFP *create(const Type *Ty, uint32_t V) {
863 assert(Ty == Type::FloatTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000864 return new ConstantFP(Ty, BitsToFloat(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000865 }
866 };
867}
868
Chris Lattner69edc982006-09-28 00:35:06 +0000869static ManagedStatic<ValueMap<uint64_t, Type, ConstantFP> > DoubleConstants;
870static ManagedStatic<ValueMap<uint32_t, Type, ConstantFP> > FloatConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000871
Jim Laskey8ad8f712005-08-17 20:06:22 +0000872bool ConstantFP::isNullValue() const {
873 return DoubleToBits(Val) == 0;
874}
875
876bool ConstantFP::isExactlyValue(double V) const {
877 return DoubleToBits(V) == DoubleToBits(Val);
878}
879
880
Chris Lattner3462ae32001-12-03 22:26:30 +0000881ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner241ed4c2004-01-23 00:55:21 +0000882 if (Ty == Type::FloatTy) {
883 // Force the value through memory to normalize it.
Chris Lattner69edc982006-09-28 00:35:06 +0000884 return FloatConstants->getOrCreate(Ty, FloatToBits(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000885 } else {
886 assert(Ty == Type::DoubleTy);
Chris Lattner69edc982006-09-28 00:35:06 +0000887 return DoubleConstants->getOrCreate(Ty, DoubleToBits(V));
Chris Lattner241ed4c2004-01-23 00:55:21 +0000888 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000889}
890
Chris Lattner9fba3da2004-02-15 05:53:04 +0000891//---- ConstantAggregateZero::get() implementation...
892//
893namespace llvm {
894 // ConstantAggregateZero does not take extra "value" argument...
895 template<class ValType>
896 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
897 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
898 return new ConstantAggregateZero(Ty);
899 }
900 };
901
902 template<>
903 struct ConvertConstantType<ConstantAggregateZero, Type> {
904 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
905 // Make everyone now use a constant of the new type...
906 Constant *New = ConstantAggregateZero::get(NewTy);
907 assert(New != OldC && "Didn't replace constant??");
908 OldC->uncheckedReplaceAllUsesWith(New);
909 OldC->destroyConstant(); // This constant is now dead, destroy it.
910 }
911 };
912}
913
Chris Lattner69edc982006-09-28 00:35:06 +0000914static ManagedStatic<ValueMap<char, Type,
915 ConstantAggregateZero> > AggZeroConstants;
Chris Lattner9fba3da2004-02-15 05:53:04 +0000916
Chris Lattner3e650af2004-08-04 04:48:01 +0000917static char getValType(ConstantAggregateZero *CPZ) { return 0; }
918
Chris Lattner9fba3da2004-02-15 05:53:04 +0000919Constant *ConstantAggregateZero::get(const Type *Ty) {
Reid Spencerd84d35b2007-02-15 02:26:10 +0000920 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) &&
Chris Lattnerbfd0b6d2006-06-10 04:16:23 +0000921 "Cannot create an aggregate zero of non-aggregate type!");
Chris Lattner69edc982006-09-28 00:35:06 +0000922 return AggZeroConstants->getOrCreate(Ty, 0);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000923}
924
925// destroyConstant - Remove the constant from the constant table...
926//
927void ConstantAggregateZero::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +0000928 AggZeroConstants->remove(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000929 destroyConstantImpl();
930}
931
Chris Lattner3462ae32001-12-03 22:26:30 +0000932//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000933//
Chris Lattner189d19f2003-11-21 20:23:48 +0000934namespace llvm {
935 template<>
936 struct ConvertConstantType<ConstantArray, ArrayType> {
937 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
938 // Make everyone now use a constant of the new type...
939 std::vector<Constant*> C;
940 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
941 C.push_back(cast<Constant>(OldC->getOperand(i)));
942 Constant *New = ConstantArray::get(NewTy, C);
943 assert(New != OldC && "Didn't replace constant??");
944 OldC->uncheckedReplaceAllUsesWith(New);
945 OldC->destroyConstant(); // This constant is now dead, destroy it.
946 }
947 };
948}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000949
Chris Lattner3e650af2004-08-04 04:48:01 +0000950static std::vector<Constant*> getValType(ConstantArray *CA) {
951 std::vector<Constant*> Elements;
952 Elements.reserve(CA->getNumOperands());
953 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
954 Elements.push_back(cast<Constant>(CA->getOperand(i)));
955 return Elements;
956}
957
Chris Lattnerb64419a2005-10-03 22:51:37 +0000958typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattner935aa922005-10-04 17:48:46 +0000959 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +0000960static ManagedStatic<ArrayConstantsTy> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000961
Chris Lattner015e8212004-02-15 04:14:47 +0000962Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +0000963 const std::vector<Constant*> &V) {
964 // If this is an all-zero array, return a ConstantAggregateZero object
965 if (!V.empty()) {
966 Constant *C = V[0];
967 if (!C->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +0000968 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000969 for (unsigned i = 1, e = V.size(); i != e; ++i)
970 if (V[i] != C)
Chris Lattner69edc982006-09-28 00:35:06 +0000971 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000972 }
973 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +0000974}
975
Chris Lattner98fa07b2003-05-23 20:03:32 +0000976// destroyConstant - Remove the constant from the constant table...
977//
978void ConstantArray::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +0000979 ArrayConstants->remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000980 destroyConstantImpl();
981}
982
Reid Spencer6f614532006-05-30 08:23:18 +0000983/// ConstantArray::get(const string&) - Return an array that is initialized to
984/// contain the specified string. If length is zero then a null terminator is
985/// added to the specified string so that it may be used in a natural way.
986/// Otherwise, the length parameter specifies how much of the string to use
987/// and it won't be null terminated.
988///
Reid Spencer82ebaba2006-05-30 18:15:07 +0000989Constant *ConstantArray::get(const std::string &Str, bool AddNull) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000990 std::vector<Constant*> ElementVals;
Reid Spencer82ebaba2006-05-30 18:15:07 +0000991 for (unsigned i = 0; i < Str.length(); ++i)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000992 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +0000993
994 // Add a null terminator to the string...
Reid Spencer82ebaba2006-05-30 18:15:07 +0000995 if (AddNull) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000996 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, 0));
Reid Spencer6f614532006-05-30 08:23:18 +0000997 }
Chris Lattner8f80fe02001-10-14 23:54:12 +0000998
Reid Spencer8d9336d2006-12-31 05:26:44 +0000999 ArrayType *ATy = ArrayType::get(Type::Int8Ty, ElementVals.size());
Chris Lattner3462ae32001-12-03 22:26:30 +00001000 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +00001001}
1002
Reid Spencer2546b762007-01-26 07:37:34 +00001003/// isString - This method returns true if the array is an array of i8, and
1004/// if the elements of the array are all ConstantInt's.
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001005bool ConstantArray::isString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001006 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001007 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001008 return false;
1009 // Check the elements to make sure they are all integers, not constant
1010 // expressions.
1011 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1012 if (!isa<ConstantInt>(getOperand(i)))
1013 return false;
1014 return true;
1015}
1016
Evan Cheng3763c5b2006-10-26 19:15:05 +00001017/// isCString - This method returns true if the array is a string (see
1018/// isString) and it ends in a null byte \0 and does not contains any other
1019/// null bytes except its terminator.
1020bool ConstantArray::isCString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001021 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001022 if (getType()->getElementType() != Type::Int8Ty)
Evan Chenge974da62006-10-26 21:48:03 +00001023 return false;
1024 Constant *Zero = Constant::getNullValue(getOperand(0)->getType());
1025 // Last element must be a null.
1026 if (getOperand(getNumOperands()-1) != Zero)
1027 return false;
1028 // Other elements must be non-null integers.
1029 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1030 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +00001031 return false;
Evan Chenge974da62006-10-26 21:48:03 +00001032 if (getOperand(i) == Zero)
1033 return false;
1034 }
Evan Cheng3763c5b2006-10-26 19:15:05 +00001035 return true;
1036}
1037
1038
Reid Spencer2546b762007-01-26 07:37:34 +00001039// getAsString - If the sub-element type of this array is i8
Chris Lattner81fabb02002-08-26 17:53:56 +00001040// then this method converts the array to an std::string and returns it.
1041// Otherwise, it asserts out.
1042//
1043std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001044 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001045 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +00001046 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Reid Spencere0fc4df2006-10-20 07:07:24 +00001047 Result += (char)cast<ConstantInt>(getOperand(i))->getZExtValue();
Chris Lattner81fabb02002-08-26 17:53:56 +00001048 return Result;
1049}
1050
1051
Chris Lattner3462ae32001-12-03 22:26:30 +00001052//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001053//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001054
Chris Lattner189d19f2003-11-21 20:23:48 +00001055namespace llvm {
1056 template<>
1057 struct ConvertConstantType<ConstantStruct, StructType> {
1058 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1059 // Make everyone now use a constant of the new type...
1060 std::vector<Constant*> C;
1061 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1062 C.push_back(cast<Constant>(OldC->getOperand(i)));
1063 Constant *New = ConstantStruct::get(NewTy, C);
1064 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001065
Chris Lattner189d19f2003-11-21 20:23:48 +00001066 OldC->uncheckedReplaceAllUsesWith(New);
1067 OldC->destroyConstant(); // This constant is now dead, destroy it.
1068 }
1069 };
1070}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001071
Chris Lattner8760ec72005-10-04 01:17:50 +00001072typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattner935aa922005-10-04 17:48:46 +00001073 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001074static ManagedStatic<StructConstantsTy> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001075
Chris Lattner3e650af2004-08-04 04:48:01 +00001076static std::vector<Constant*> getValType(ConstantStruct *CS) {
1077 std::vector<Constant*> Elements;
1078 Elements.reserve(CS->getNumOperands());
1079 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1080 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1081 return Elements;
1082}
1083
Chris Lattner015e8212004-02-15 04:14:47 +00001084Constant *ConstantStruct::get(const StructType *Ty,
1085 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001086 // Create a ConstantAggregateZero value if all elements are zeros...
1087 for (unsigned i = 0, e = V.size(); i != e; ++i)
1088 if (!V[i]->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001089 return StructConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001090
1091 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001092}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001093
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001094Constant *ConstantStruct::get(const std::vector<Constant*> &V, bool packed) {
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001095 std::vector<const Type*> StructEls;
1096 StructEls.reserve(V.size());
1097 for (unsigned i = 0, e = V.size(); i != e; ++i)
1098 StructEls.push_back(V[i]->getType());
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001099 return get(StructType::get(StructEls, packed), V);
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001100}
1101
Chris Lattnerd7a73302001-10-13 06:57:33 +00001102// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001103//
Chris Lattner3462ae32001-12-03 22:26:30 +00001104void ConstantStruct::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001105 StructConstants->remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001106 destroyConstantImpl();
1107}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001108
Reid Spencerd84d35b2007-02-15 02:26:10 +00001109//---- ConstantVector::get() implementation...
Brian Gaeke02209042004-08-20 06:00:58 +00001110//
1111namespace llvm {
1112 template<>
Reid Spencerd84d35b2007-02-15 02:26:10 +00001113 struct ConvertConstantType<ConstantVector, VectorType> {
1114 static void convert(ConstantVector *OldC, const VectorType *NewTy) {
Brian Gaeke02209042004-08-20 06:00:58 +00001115 // Make everyone now use a constant of the new type...
1116 std::vector<Constant*> C;
1117 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1118 C.push_back(cast<Constant>(OldC->getOperand(i)));
Reid Spencerd84d35b2007-02-15 02:26:10 +00001119 Constant *New = ConstantVector::get(NewTy, C);
Brian Gaeke02209042004-08-20 06:00:58 +00001120 assert(New != OldC && "Didn't replace constant??");
1121 OldC->uncheckedReplaceAllUsesWith(New);
1122 OldC->destroyConstant(); // This constant is now dead, destroy it.
1123 }
1124 };
1125}
1126
Reid Spencerd84d35b2007-02-15 02:26:10 +00001127static std::vector<Constant*> getValType(ConstantVector *CP) {
Brian Gaeke02209042004-08-20 06:00:58 +00001128 std::vector<Constant*> Elements;
1129 Elements.reserve(CP->getNumOperands());
1130 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1131 Elements.push_back(CP->getOperand(i));
1132 return Elements;
1133}
1134
Reid Spencerd84d35b2007-02-15 02:26:10 +00001135static ManagedStatic<ValueMap<std::vector<Constant*>, VectorType,
Reid Spencer09575ba2007-02-15 03:39:18 +00001136 ConstantVector> > VectorConstants;
Brian Gaeke02209042004-08-20 06:00:58 +00001137
Reid Spencerd84d35b2007-02-15 02:26:10 +00001138Constant *ConstantVector::get(const VectorType *Ty,
Brian Gaeke02209042004-08-20 06:00:58 +00001139 const std::vector<Constant*> &V) {
1140 // If this is an all-zero packed, return a ConstantAggregateZero object
1141 if (!V.empty()) {
1142 Constant *C = V[0];
1143 if (!C->isNullValue())
Reid Spencer09575ba2007-02-15 03:39:18 +00001144 return VectorConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001145 for (unsigned i = 1, e = V.size(); i != e; ++i)
1146 if (V[i] != C)
Reid Spencer09575ba2007-02-15 03:39:18 +00001147 return VectorConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001148 }
1149 return ConstantAggregateZero::get(Ty);
1150}
1151
Reid Spencerd84d35b2007-02-15 02:26:10 +00001152Constant *ConstantVector::get(const std::vector<Constant*> &V) {
Brian Gaeke02209042004-08-20 06:00:58 +00001153 assert(!V.empty() && "Cannot infer type if V is empty");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001154 return get(VectorType::get(V.front()->getType(),V.size()), V);
Brian Gaeke02209042004-08-20 06:00:58 +00001155}
1156
1157// destroyConstant - Remove the constant from the constant table...
1158//
Reid Spencerd84d35b2007-02-15 02:26:10 +00001159void ConstantVector::destroyConstant() {
Reid Spencer09575ba2007-02-15 03:39:18 +00001160 VectorConstants->remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001161 destroyConstantImpl();
1162}
1163
Jim Laskeyf0478822007-01-12 22:39:14 +00001164/// This function will return true iff every element in this packed constant
1165/// is set to all ones.
1166/// @returns true iff this constant's emements are all set to all ones.
1167/// @brief Determine if the value is all ones.
Reid Spencerd84d35b2007-02-15 02:26:10 +00001168bool ConstantVector::isAllOnesValue() const {
Jim Laskeyf0478822007-01-12 22:39:14 +00001169 // Check out first element.
1170 const Constant *Elt = getOperand(0);
1171 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1172 if (!CI || !CI->isAllOnesValue()) return false;
1173 // Then make sure all remaining elements point to the same value.
1174 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1175 if (getOperand(I) != Elt) return false;
1176 }
1177 return true;
1178}
1179
Chris Lattner3462ae32001-12-03 22:26:30 +00001180//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001181//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001182
Chris Lattner189d19f2003-11-21 20:23:48 +00001183namespace llvm {
1184 // ConstantPointerNull does not take extra "value" argument...
1185 template<class ValType>
1186 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1187 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1188 return new ConstantPointerNull(Ty);
1189 }
1190 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001191
Chris Lattner189d19f2003-11-21 20:23:48 +00001192 template<>
1193 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1194 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1195 // Make everyone now use a constant of the new type...
1196 Constant *New = ConstantPointerNull::get(NewTy);
1197 assert(New != OldC && "Didn't replace constant??");
1198 OldC->uncheckedReplaceAllUsesWith(New);
1199 OldC->destroyConstant(); // This constant is now dead, destroy it.
1200 }
1201 };
1202}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001203
Chris Lattner69edc982006-09-28 00:35:06 +00001204static ManagedStatic<ValueMap<char, PointerType,
1205 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001206
Chris Lattner3e650af2004-08-04 04:48:01 +00001207static char getValType(ConstantPointerNull *) {
1208 return 0;
1209}
1210
1211
Chris Lattner3462ae32001-12-03 22:26:30 +00001212ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001213 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001214}
1215
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001216// destroyConstant - Remove the constant from the constant table...
1217//
1218void ConstantPointerNull::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001219 NullPtrConstants->remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001220 destroyConstantImpl();
1221}
1222
1223
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001224//---- UndefValue::get() implementation...
1225//
1226
1227namespace llvm {
1228 // UndefValue does not take extra "value" argument...
1229 template<class ValType>
1230 struct ConstantCreator<UndefValue, Type, ValType> {
1231 static UndefValue *create(const Type *Ty, const ValType &V) {
1232 return new UndefValue(Ty);
1233 }
1234 };
1235
1236 template<>
1237 struct ConvertConstantType<UndefValue, Type> {
1238 static void convert(UndefValue *OldC, const Type *NewTy) {
1239 // Make everyone now use a constant of the new type.
1240 Constant *New = UndefValue::get(NewTy);
1241 assert(New != OldC && "Didn't replace constant??");
1242 OldC->uncheckedReplaceAllUsesWith(New);
1243 OldC->destroyConstant(); // This constant is now dead, destroy it.
1244 }
1245 };
1246}
1247
Chris Lattner69edc982006-09-28 00:35:06 +00001248static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001249
1250static char getValType(UndefValue *) {
1251 return 0;
1252}
1253
1254
1255UndefValue *UndefValue::get(const Type *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001256 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001257}
1258
1259// destroyConstant - Remove the constant from the constant table.
1260//
1261void UndefValue::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001262 UndefValueConstants->remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001263 destroyConstantImpl();
1264}
1265
1266
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001267//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001268//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001269
Reid Spenceree3c9912006-12-04 05:19:50 +00001270struct ExprMapKeyType {
1271 explicit ExprMapKeyType(unsigned opc, std::vector<Constant*> ops,
Reid Spencerdba6aa42006-12-04 18:38:05 +00001272 unsigned short pred = 0) : opcode(opc), predicate(pred), operands(ops) { }
1273 uint16_t opcode;
1274 uint16_t predicate;
Reid Spenceree3c9912006-12-04 05:19:50 +00001275 std::vector<Constant*> operands;
Reid Spenceree3c9912006-12-04 05:19:50 +00001276 bool operator==(const ExprMapKeyType& that) const {
1277 return this->opcode == that.opcode &&
1278 this->predicate == that.predicate &&
1279 this->operands == that.operands;
1280 }
1281 bool operator<(const ExprMapKeyType & that) const {
1282 return this->opcode < that.opcode ||
1283 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1284 (this->opcode == that.opcode && this->predicate == that.predicate &&
1285 this->operands < that.operands);
1286 }
1287
1288 bool operator!=(const ExprMapKeyType& that) const {
1289 return !(*this == that);
1290 }
1291};
Chris Lattner98fa07b2003-05-23 20:03:32 +00001292
Chris Lattner189d19f2003-11-21 20:23:48 +00001293namespace llvm {
1294 template<>
1295 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001296 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1297 unsigned short pred = 0) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001298 if (Instruction::isCast(V.opcode))
1299 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1300 if ((V.opcode >= Instruction::BinaryOpsBegin &&
Reid Spencer2341c222007-02-02 02:16:23 +00001301 V.opcode < Instruction::BinaryOpsEnd))
Reid Spenceree3c9912006-12-04 05:19:50 +00001302 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1303 if (V.opcode == Instruction::Select)
1304 return new SelectConstantExpr(V.operands[0], V.operands[1],
1305 V.operands[2]);
1306 if (V.opcode == Instruction::ExtractElement)
1307 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1308 if (V.opcode == Instruction::InsertElement)
1309 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1310 V.operands[2]);
1311 if (V.opcode == Instruction::ShuffleVector)
1312 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1313 V.operands[2]);
1314 if (V.opcode == Instruction::GetElementPtr) {
1315 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
1316 return new GetElementPtrConstantExpr(V.operands[0], IdxList, Ty);
1317 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001318
Reid Spenceree3c9912006-12-04 05:19:50 +00001319 // The compare instructions are weird. We have to encode the predicate
1320 // value and it is combined with the instruction opcode by multiplying
1321 // the opcode by one hundred. We must decode this to get the predicate.
1322 if (V.opcode == Instruction::ICmp)
1323 return new CompareConstantExpr(Instruction::ICmp, V.predicate,
1324 V.operands[0], V.operands[1]);
1325 if (V.opcode == Instruction::FCmp)
1326 return new CompareConstantExpr(Instruction::FCmp, V.predicate,
1327 V.operands[0], V.operands[1]);
1328 assert(0 && "Invalid ConstantExpr!");
Jeff Cohen9f469632006-12-15 21:47:01 +00001329 return 0;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001330 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001331 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001332
Chris Lattner189d19f2003-11-21 20:23:48 +00001333 template<>
1334 struct ConvertConstantType<ConstantExpr, Type> {
1335 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1336 Constant *New;
1337 switch (OldC->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001338 case Instruction::Trunc:
1339 case Instruction::ZExt:
1340 case Instruction::SExt:
1341 case Instruction::FPTrunc:
1342 case Instruction::FPExt:
1343 case Instruction::UIToFP:
1344 case Instruction::SIToFP:
1345 case Instruction::FPToUI:
1346 case Instruction::FPToSI:
1347 case Instruction::PtrToInt:
1348 case Instruction::IntToPtr:
1349 case Instruction::BitCast:
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001350 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
1351 NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001352 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001353 case Instruction::Select:
1354 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1355 OldC->getOperand(1),
1356 OldC->getOperand(2));
1357 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001358 default:
1359 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001360 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner189d19f2003-11-21 20:23:48 +00001361 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1362 OldC->getOperand(1));
1363 break;
1364 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001365 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001366 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
Chris Lattner302116a2007-01-31 04:40:28 +00001367 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
1368 &Idx[0], Idx.size());
Chris Lattner189d19f2003-11-21 20:23:48 +00001369 break;
1370 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001371
Chris Lattner189d19f2003-11-21 20:23:48 +00001372 assert(New != OldC && "Didn't replace constant??");
1373 OldC->uncheckedReplaceAllUsesWith(New);
1374 OldC->destroyConstant(); // This constant is now dead, destroy it.
1375 }
1376 };
1377} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001378
1379
Chris Lattner3e650af2004-08-04 04:48:01 +00001380static ExprMapKeyType getValType(ConstantExpr *CE) {
1381 std::vector<Constant*> Operands;
1382 Operands.reserve(CE->getNumOperands());
1383 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1384 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spenceree3c9912006-12-04 05:19:50 +00001385 return ExprMapKeyType(CE->getOpcode(), Operands,
1386 CE->isCompare() ? CE->getPredicate() : 0);
Chris Lattner3e650af2004-08-04 04:48:01 +00001387}
1388
Chris Lattner69edc982006-09-28 00:35:06 +00001389static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1390 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001391
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001392/// This is a utility function to handle folding of casts and lookup of the
1393/// cast in the ExprConstants map. It is usedby the various get* methods below.
1394static inline Constant *getFoldedCast(
1395 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001396 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001397 // Fold a few common cases
1398 if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
1399 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001400
Vikram S. Adve4c485332002-07-15 18:19:33 +00001401 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001402 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00001403 ExprMapKeyType Key(opc, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001404 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001405}
Reid Spencerf37dc652006-12-05 19:14:13 +00001406
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001407Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
1408 Instruction::CastOps opc = Instruction::CastOps(oc);
1409 assert(Instruction::isCast(opc) && "opcode out of range");
1410 assert(C && Ty && "Null arguments to getCast");
1411 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1412
1413 switch (opc) {
1414 default:
1415 assert(0 && "Invalid cast opcode");
1416 break;
1417 case Instruction::Trunc: return getTrunc(C, Ty);
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001418 case Instruction::ZExt: return getZExt(C, Ty);
1419 case Instruction::SExt: return getSExt(C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001420 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1421 case Instruction::FPExt: return getFPExtend(C, Ty);
1422 case Instruction::UIToFP: return getUIToFP(C, Ty);
1423 case Instruction::SIToFP: return getSIToFP(C, Ty);
1424 case Instruction::FPToUI: return getFPToUI(C, Ty);
1425 case Instruction::FPToSI: return getFPToSI(C, Ty);
1426 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1427 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1428 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001429 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001430 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00001431}
1432
Reid Spencer5c140882006-12-04 20:17:56 +00001433Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
1434 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1435 return getCast(Instruction::BitCast, C, Ty);
1436 return getCast(Instruction::ZExt, C, Ty);
1437}
1438
1439Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
1440 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1441 return getCast(Instruction::BitCast, C, Ty);
1442 return getCast(Instruction::SExt, C, Ty);
1443}
1444
1445Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
1446 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1447 return getCast(Instruction::BitCast, C, Ty);
1448 return getCast(Instruction::Trunc, C, Ty);
1449}
1450
Reid Spencerbc245a02006-12-05 03:25:26 +00001451Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
1452 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001453 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001454
Chris Lattner03c49532007-01-15 02:27:26 +00001455 if (Ty->isInteger())
Reid Spencerbc245a02006-12-05 03:25:26 +00001456 return getCast(Instruction::PtrToInt, S, Ty);
1457 return getCast(Instruction::BitCast, S, Ty);
1458}
1459
Reid Spencer56521c42006-12-12 00:51:07 +00001460Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1461 bool isSigned) {
Chris Lattner03c49532007-01-15 02:27:26 +00001462 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer56521c42006-12-12 00:51:07 +00001463 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1464 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1465 Instruction::CastOps opcode =
1466 (SrcBits == DstBits ? Instruction::BitCast :
1467 (SrcBits > DstBits ? Instruction::Trunc :
1468 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1469 return getCast(opcode, C, Ty);
1470}
1471
1472Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
1473 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1474 "Invalid cast");
1475 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1476 unsigned DstBits = Ty->getPrimitiveSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001477 if (SrcBits == DstBits)
1478 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001479 Instruction::CastOps opcode =
Reid Spencerca104e82006-12-12 05:38:50 +00001480 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001481 return getCast(opcode, C, Ty);
1482}
1483
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001484Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001485 assert(C->getType()->isInteger() && "Trunc operand must be integer");
1486 assert(Ty->isInteger() && "Trunc produces only integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001487 assert(C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&&
1488 "SrcTy must be larger than DestTy for Trunc!");
1489
1490 return getFoldedCast(Instruction::Trunc, C, Ty);
1491}
1492
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001493Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001494 assert(C->getType()->isInteger() && "SEXt operand must be integral");
1495 assert(Ty->isInteger() && "SExt produces only integer");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001496 assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1497 "SrcTy must be smaller than DestTy for SExt!");
1498
1499 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerdd284742004-04-04 23:20:30 +00001500}
1501
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001502Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001503 assert(C->getType()->isInteger() && "ZEXt operand must be integral");
1504 assert(Ty->isInteger() && "ZExt produces only integer");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001505 assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1506 "SrcTy must be smaller than DestTy for ZExt!");
1507
1508 return getFoldedCast(Instruction::ZExt, C, Ty);
1509}
1510
1511Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
1512 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1513 C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&&
1514 "This is an illegal floating point truncation!");
1515 return getFoldedCast(Instruction::FPTrunc, C, Ty);
1516}
1517
1518Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
1519 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1520 C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1521 "This is an illegal floating point extension!");
1522 return getFoldedCast(Instruction::FPExt, C, Ty);
1523}
1524
1525Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001526 assert(C->getType()->isInteger() && Ty->isFloatingPoint() &&
Reid Spencer2546b762007-01-26 07:37:34 +00001527 "This is an illegal i32 to floating point cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001528 return getFoldedCast(Instruction::UIToFP, C, Ty);
1529}
1530
1531Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001532 assert(C->getType()->isInteger() && Ty->isFloatingPoint() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001533 "This is an illegal sint to floating point cast!");
1534 return getFoldedCast(Instruction::SIToFP, C, Ty);
1535}
1536
1537Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001538 assert(C->getType()->isFloatingPoint() && Ty->isInteger() &&
Reid Spencer2546b762007-01-26 07:37:34 +00001539 "This is an illegal floating point to i32 cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001540 return getFoldedCast(Instruction::FPToUI, C, Ty);
1541}
1542
1543Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001544 assert(C->getType()->isFloatingPoint() && Ty->isInteger() &&
Reid Spencer2546b762007-01-26 07:37:34 +00001545 "This is an illegal floating point to i32 cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001546 return getFoldedCast(Instruction::FPToSI, C, Ty);
1547}
1548
1549Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
1550 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner03c49532007-01-15 02:27:26 +00001551 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001552 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
1553}
1554
1555Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner03c49532007-01-15 02:27:26 +00001556 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001557 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
1558 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
1559}
1560
1561Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
1562 // BitCast implies a no-op cast of type only. No bits change. However, you
1563 // can't cast pointers to anything but pointers.
1564 const Type *SrcTy = C->getType();
1565 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer5c140882006-12-04 20:17:56 +00001566 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001567
1568 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
1569 // or nonptr->ptr). For all the other types, the cast is okay if source and
1570 // destination bit widths are identical.
1571 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1572 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Reid Spencer5c140882006-12-04 20:17:56 +00001573 assert(SrcBitSize == DstBitSize && "BitCast requies types of same width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001574 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerdd284742004-04-04 23:20:30 +00001575}
1576
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001577Constant *ConstantExpr::getSizeOf(const Type *Ty) {
Chris Lattneracc4e542004-12-13 19:48:51 +00001578 // sizeof is implemented as: (ulong) gep (Ty*)null, 1
Chris Lattnerb5d70302007-02-19 20:01:23 +00001579 Constant *GEPIdx = ConstantInt::get(Type::Int32Ty, 1);
1580 Constant *GEP =
1581 getGetElementPtr(getNullValue(PointerType::get(Ty)), &GEPIdx, 1);
1582 return getCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
Alkis Evlogimenos9160d5f2005-03-19 11:40:31 +00001583}
1584
Chris Lattnerb50d1352003-10-05 00:17:43 +00001585Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Reid Spencera009d0d2006-12-04 21:35:24 +00001586 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001587 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00001588 assert(Opcode >= Instruction::BinaryOpsBegin &&
1589 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001590 "Invalid opcode in binary constant expression");
1591 assert(C1->getType() == C2->getType() &&
1592 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001593
Reid Spencer542964f2007-01-11 18:21:29 +00001594 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Chris Lattnerb50d1352003-10-05 00:17:43 +00001595 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1596 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001597
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001598 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001599 ExprMapKeyType Key(Opcode, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001600 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001601}
1602
Reid Spencer266e42b2006-12-23 06:05:41 +00001603Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Reid Spencera009d0d2006-12-04 21:35:24 +00001604 Constant *C1, Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001605 switch (predicate) {
1606 default: assert(0 && "Invalid CmpInst predicate");
1607 case FCmpInst::FCMP_FALSE: case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_OGT:
1608 case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OLE:
1609 case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_ORD: case FCmpInst::FCMP_UNO:
1610 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UGT: case FCmpInst::FCMP_UGE:
1611 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_ULE: case FCmpInst::FCMP_UNE:
1612 case FCmpInst::FCMP_TRUE:
1613 return getFCmp(predicate, C1, C2);
1614 case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_UGT:
1615 case ICmpInst::ICMP_UGE: case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE:
1616 case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_SGE: case ICmpInst::ICMP_SLT:
1617 case ICmpInst::ICMP_SLE:
1618 return getICmp(predicate, C1, C2);
1619 }
Reid Spencera009d0d2006-12-04 21:35:24 +00001620}
1621
1622Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001623#ifndef NDEBUG
1624 switch (Opcode) {
Reid Spencer7eb55b32006-11-02 01:53:59 +00001625 case Instruction::Add:
1626 case Instruction::Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001627 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001628 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattner03c49532007-01-15 02:27:26 +00001629 assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001630 isa<VectorType>(C1->getType())) &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001631 "Tried to create an arithmetic operation on a non-arithmetic type!");
1632 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001633 case Instruction::UDiv:
1634 case Instruction::SDiv:
1635 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001636 assert((C1->getType()->isInteger() || (isa<VectorType>(C1->getType()) &&
1637 cast<VectorType>(C1->getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001638 "Tried to create an arithmetic operation on a non-arithmetic type!");
1639 break;
1640 case Instruction::FDiv:
1641 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001642 assert((C1->getType()->isFloatingPoint() || (isa<VectorType>(C1->getType())
1643 && cast<VectorType>(C1->getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001644 && "Tried to create an arithmetic operation on a non-arithmetic type!");
1645 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001646 case Instruction::URem:
1647 case Instruction::SRem:
1648 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001649 assert((C1->getType()->isInteger() || (isa<VectorType>(C1->getType()) &&
1650 cast<VectorType>(C1->getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001651 "Tried to create an arithmetic operation on a non-arithmetic type!");
1652 break;
1653 case Instruction::FRem:
1654 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001655 assert((C1->getType()->isFloatingPoint() || (isa<VectorType>(C1->getType())
1656 && cast<VectorType>(C1->getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001657 && "Tried to create an arithmetic operation on a non-arithmetic type!");
1658 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001659 case Instruction::And:
1660 case Instruction::Or:
1661 case Instruction::Xor:
1662 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001663 assert((C1->getType()->isInteger() || isa<VectorType>(C1->getType())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001664 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001665 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001666 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001667 case Instruction::LShr:
1668 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00001669 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattner03c49532007-01-15 02:27:26 +00001670 assert(C1->getType()->isInteger() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001671 "Tried to create a shift operation on a non-integer type!");
1672 break;
1673 default:
1674 break;
1675 }
1676#endif
1677
Reid Spencera009d0d2006-12-04 21:35:24 +00001678 return getTy(C1->getType(), Opcode, C1, C2);
1679}
1680
Reid Spencer266e42b2006-12-23 06:05:41 +00001681Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00001682 Constant *C1, Constant *C2) {
1683 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001684 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00001685}
1686
Chris Lattner6e415c02004-03-12 05:54:04 +00001687Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
1688 Constant *V1, Constant *V2) {
Reid Spencer2546b762007-01-26 07:37:34 +00001689 assert(C->getType() == Type::Int1Ty && "Select condition must be i1!");
Chris Lattner6e415c02004-03-12 05:54:04 +00001690 assert(V1->getType() == V2->getType() && "Select value types must match!");
1691 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
1692
1693 if (ReqTy == V1->getType())
1694 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1695 return SC; // Fold common cases
1696
1697 std::vector<Constant*> argVec(3, C);
1698 argVec[1] = V1;
1699 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00001700 ExprMapKeyType Key(Instruction::Select, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001701 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00001702}
1703
Chris Lattnerb50d1352003-10-05 00:17:43 +00001704Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00001705 Value* const *Idxs,
1706 unsigned NumIdx) {
1707 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs, NumIdx, true) &&
Chris Lattner04b60fe2004-02-16 20:46:13 +00001708 "GEP indices invalid!");
1709
Chris Lattner302116a2007-01-31 04:40:28 +00001710 if (Constant *FC = ConstantFoldGetElementPtr(C, (Constant**)Idxs, NumIdx))
Chris Lattneracdbe712003-04-17 19:24:48 +00001711 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001712
Chris Lattnerb50d1352003-10-05 00:17:43 +00001713 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001714 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001715 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00001716 std::vector<Constant*> ArgVec;
Chris Lattner302116a2007-01-31 04:40:28 +00001717 ArgVec.reserve(NumIdx+1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001718 ArgVec.push_back(C);
Chris Lattner302116a2007-01-31 04:40:28 +00001719 for (unsigned i = 0; i != NumIdx; ++i)
1720 ArgVec.push_back(cast<Constant>(Idxs[i]));
1721 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001722 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001723}
1724
Chris Lattner302116a2007-01-31 04:40:28 +00001725Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
1726 unsigned NumIdx) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00001727 // Get the result type of the getelementptr!
Chris Lattner302116a2007-01-31 04:40:28 +00001728 const Type *Ty =
1729 GetElementPtrInst::getIndexedType(C->getType(), Idxs, NumIdx, true);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001730 assert(Ty && "GEP indices invalid!");
Chris Lattner302116a2007-01-31 04:40:28 +00001731 return getGetElementPtrTy(PointerType::get(Ty), C, Idxs, NumIdx);
Chris Lattner13128ab2004-10-11 22:52:25 +00001732}
1733
Chris Lattner302116a2007-01-31 04:40:28 +00001734Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
1735 unsigned NumIdx) {
1736 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001737}
1738
Chris Lattner302116a2007-01-31 04:40:28 +00001739
Reid Spenceree3c9912006-12-04 05:19:50 +00001740Constant *
1741ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1742 assert(LHS->getType() == RHS->getType());
1743 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
1744 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
1745
Reid Spencer266e42b2006-12-23 06:05:41 +00001746 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001747 return FC; // Fold a few common cases...
1748
1749 // Look up the constant in the table first to ensure uniqueness
1750 std::vector<Constant*> ArgVec;
1751 ArgVec.push_back(LHS);
1752 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001753 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001754 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Reid Spencer542964f2007-01-11 18:21:29 +00001755 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001756}
1757
1758Constant *
1759ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1760 assert(LHS->getType() == RHS->getType());
1761 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
1762
Reid Spencer266e42b2006-12-23 06:05:41 +00001763 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001764 return FC; // Fold a few common cases...
1765
1766 // Look up the constant in the table first to ensure uniqueness
1767 std::vector<Constant*> ArgVec;
1768 ArgVec.push_back(LHS);
1769 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001770 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001771 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Reid Spencer542964f2007-01-11 18:21:29 +00001772 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001773}
1774
Robert Bocchino23004482006-01-10 19:05:34 +00001775Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
1776 Constant *Idx) {
Robert Bocchinode7f1c92006-01-10 20:03:46 +00001777 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
1778 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00001779 // Look up the constant in the table first to ensure uniqueness
1780 std::vector<Constant*> ArgVec(1, Val);
1781 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001782 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001783 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00001784}
1785
1786Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001787 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001788 "Tried to create extractelement operation on non-vector type!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001789 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00001790 "Extractelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001791 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +00001792 Val, Idx);
1793}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001794
Robert Bocchinoca27f032006-01-17 20:07:22 +00001795Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
1796 Constant *Elt, Constant *Idx) {
1797 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
1798 return FC; // Fold a few common cases...
1799 // Look up the constant in the table first to ensure uniqueness
1800 std::vector<Constant*> ArgVec(1, Val);
1801 ArgVec.push_back(Elt);
1802 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001803 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001804 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001805}
1806
1807Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1808 Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001809 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001810 "Tried to create insertelement operation on non-vector type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001811 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoca27f032006-01-17 20:07:22 +00001812 && "Insertelement types must match!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001813 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00001814 "Insertelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001815 return getInsertElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchinoca27f032006-01-17 20:07:22 +00001816 Val, Elt, Idx);
1817}
1818
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001819Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
1820 Constant *V2, Constant *Mask) {
1821 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
1822 return FC; // Fold a few common cases...
1823 // Look up the constant in the table first to ensure uniqueness
1824 std::vector<Constant*> ArgVec(1, V1);
1825 ArgVec.push_back(V2);
1826 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00001827 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001828 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001829}
1830
1831Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
1832 Constant *Mask) {
1833 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
1834 "Invalid shuffle vector constant expr operands!");
1835 return getShuffleVectorTy(V1->getType(), V1, V2, Mask);
1836}
1837
Reid Spencer2eadb532007-01-21 00:29:26 +00001838Constant *ConstantExpr::getZeroValueForNegationExpr(const Type *Ty) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001839 if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
Reid Spencer6598ca82007-01-21 02:29:10 +00001840 if (PTy->getElementType()->isFloatingPoint()) {
1841 std::vector<Constant*> zeros(PTy->getNumElements(),
1842 ConstantFP::get(PTy->getElementType(),-0.0));
Reid Spencerd84d35b2007-02-15 02:26:10 +00001843 return ConstantVector::get(PTy, zeros);
Reid Spencer6598ca82007-01-21 02:29:10 +00001844 }
Reid Spencer2eadb532007-01-21 00:29:26 +00001845
1846 if (Ty->isFloatingPoint())
1847 return ConstantFP::get(Ty, -0.0);
1848
1849 return Constant::getNullValue(Ty);
1850}
1851
Vikram S. Adve4c485332002-07-15 18:19:33 +00001852// destroyConstant - Remove the constant from the constant table...
1853//
1854void ConstantExpr::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001855 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001856 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001857}
1858
Chris Lattner3cd8c562002-07-30 18:54:25 +00001859const char *ConstantExpr::getOpcodeName() const {
1860 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001861}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00001862
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001863//===----------------------------------------------------------------------===//
1864// replaceUsesOfWithOnConstant implementations
1865
1866void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001867 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001868 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00001869 Constant *ToC = cast<Constant>(To);
Chris Lattnerdff59112005-10-04 18:47:09 +00001870
1871 unsigned OperandToUpdate = U-OperandList;
1872 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1873
Jim Laskeyc03caef2006-07-17 17:38:29 +00001874 std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnerb64419a2005-10-03 22:51:37 +00001875 Lookup.first.first = getType();
1876 Lookup.second = this;
Chris Lattnerdff59112005-10-04 18:47:09 +00001877
Chris Lattnerb64419a2005-10-03 22:51:37 +00001878 std::vector<Constant*> &Values = Lookup.first.second;
1879 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattnerdff59112005-10-04 18:47:09 +00001880
Chris Lattner8760ec72005-10-04 01:17:50 +00001881 // Fill values with the modified operands of the constant array. Also,
1882 // compute whether this turns into an all-zeros array.
Chris Lattnerdff59112005-10-04 18:47:09 +00001883 bool isAllZeros = false;
1884 if (!ToC->isNullValue()) {
1885 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1886 Values.push_back(cast<Constant>(O->get()));
1887 } else {
1888 isAllZeros = true;
1889 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1890 Constant *Val = cast<Constant>(O->get());
1891 Values.push_back(Val);
1892 if (isAllZeros) isAllZeros = Val->isNullValue();
1893 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001894 }
Chris Lattnerdff59112005-10-04 18:47:09 +00001895 Values[OperandToUpdate] = ToC;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001896
Chris Lattnerb64419a2005-10-03 22:51:37 +00001897 Constant *Replacement = 0;
1898 if (isAllZeros) {
1899 Replacement = ConstantAggregateZero::get(getType());
1900 } else {
1901 // Check to see if we have this array type already.
1902 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00001903 ArrayConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00001904 ArrayConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001905
1906 if (Exists) {
1907 Replacement = I->second;
1908 } else {
1909 // Okay, the new shape doesn't exist in the system yet. Instead of
1910 // creating a new constant array, inserting it, replaceallusesof'ing the
1911 // old with the new, then deleting the old... just update the current one
1912 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00001913 ArrayConstants->MoveConstantToNewSlot(this, I);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001914
Chris Lattnerdff59112005-10-04 18:47:09 +00001915 // Update to the new value.
1916 setOperand(OperandToUpdate, ToC);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001917 return;
1918 }
1919 }
1920
1921 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001922 assert(Replacement != this && "I didn't contain From!");
1923
Chris Lattner7a1450d2005-10-04 18:13:04 +00001924 // Everyone using this now uses the replacement.
1925 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001926
1927 // Delete the old constant!
1928 destroyConstant();
1929}
1930
1931void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001932 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001933 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00001934 Constant *ToC = cast<Constant>(To);
1935
Chris Lattnerdff59112005-10-04 18:47:09 +00001936 unsigned OperandToUpdate = U-OperandList;
1937 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1938
Jim Laskeyc03caef2006-07-17 17:38:29 +00001939 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattner8760ec72005-10-04 01:17:50 +00001940 Lookup.first.first = getType();
1941 Lookup.second = this;
1942 std::vector<Constant*> &Values = Lookup.first.second;
1943 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001944
Chris Lattnerdff59112005-10-04 18:47:09 +00001945
Chris Lattner8760ec72005-10-04 01:17:50 +00001946 // Fill values with the modified operands of the constant struct. Also,
1947 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00001948 bool isAllZeros = false;
1949 if (!ToC->isNullValue()) {
1950 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1951 Values.push_back(cast<Constant>(O->get()));
1952 } else {
1953 isAllZeros = true;
1954 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1955 Constant *Val = cast<Constant>(O->get());
1956 Values.push_back(Val);
1957 if (isAllZeros) isAllZeros = Val->isNullValue();
1958 }
Chris Lattner8760ec72005-10-04 01:17:50 +00001959 }
Chris Lattnerdff59112005-10-04 18:47:09 +00001960 Values[OperandToUpdate] = ToC;
1961
Chris Lattner8760ec72005-10-04 01:17:50 +00001962 Constant *Replacement = 0;
1963 if (isAllZeros) {
1964 Replacement = ConstantAggregateZero::get(getType());
1965 } else {
1966 // Check to see if we have this array type already.
1967 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00001968 StructConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00001969 StructConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00001970
1971 if (Exists) {
1972 Replacement = I->second;
1973 } else {
1974 // Okay, the new shape doesn't exist in the system yet. Instead of
1975 // creating a new constant struct, inserting it, replaceallusesof'ing the
1976 // old with the new, then deleting the old... just update the current one
1977 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00001978 StructConstants->MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00001979
Chris Lattnerdff59112005-10-04 18:47:09 +00001980 // Update to the new value.
1981 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00001982 return;
1983 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001984 }
1985
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001986 assert(Replacement != this && "I didn't contain From!");
1987
Chris Lattner7a1450d2005-10-04 18:13:04 +00001988 // Everyone using this now uses the replacement.
1989 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001990
1991 // Delete the old constant!
1992 destroyConstant();
1993}
1994
Reid Spencerd84d35b2007-02-15 02:26:10 +00001995void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001996 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001997 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
1998
1999 std::vector<Constant*> Values;
2000 Values.reserve(getNumOperands()); // Build replacement array...
2001 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2002 Constant *Val = getOperand(i);
2003 if (Val == From) Val = cast<Constant>(To);
2004 Values.push_back(Val);
2005 }
2006
Reid Spencerd84d35b2007-02-15 02:26:10 +00002007 Constant *Replacement = ConstantVector::get(getType(), Values);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002008 assert(Replacement != this && "I didn't contain From!");
2009
Chris Lattner7a1450d2005-10-04 18:13:04 +00002010 // Everyone using this now uses the replacement.
2011 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002012
2013 // Delete the old constant!
2014 destroyConstant();
2015}
2016
2017void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002018 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002019 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2020 Constant *To = cast<Constant>(ToV);
2021
2022 Constant *Replacement = 0;
2023 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerb5d70302007-02-19 20:01:23 +00002024 SmallVector<Constant*, 8> Indices;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002025 Constant *Pointer = getOperand(0);
2026 Indices.reserve(getNumOperands()-1);
2027 if (Pointer == From) Pointer = To;
2028
2029 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2030 Constant *Val = getOperand(i);
2031 if (Val == From) Val = To;
2032 Indices.push_back(Val);
2033 }
Chris Lattnerb5d70302007-02-19 20:01:23 +00002034 Replacement = ConstantExpr::getGetElementPtr(Pointer,
2035 &Indices[0], Indices.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002036 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002037 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002038 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002039 } else if (getOpcode() == Instruction::Select) {
2040 Constant *C1 = getOperand(0);
2041 Constant *C2 = getOperand(1);
2042 Constant *C3 = getOperand(2);
2043 if (C1 == From) C1 = To;
2044 if (C2 == From) C2 = To;
2045 if (C3 == From) C3 = To;
2046 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00002047 } else if (getOpcode() == Instruction::ExtractElement) {
2048 Constant *C1 = getOperand(0);
2049 Constant *C2 = getOperand(1);
2050 if (C1 == From) C1 = To;
2051 if (C2 == From) C2 = To;
2052 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00002053 } else if (getOpcode() == Instruction::InsertElement) {
2054 Constant *C1 = getOperand(0);
2055 Constant *C2 = getOperand(1);
2056 Constant *C3 = getOperand(1);
2057 if (C1 == From) C1 = To;
2058 if (C2 == From) C2 = To;
2059 if (C3 == From) C3 = To;
2060 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2061 } else if (getOpcode() == Instruction::ShuffleVector) {
2062 Constant *C1 = getOperand(0);
2063 Constant *C2 = getOperand(1);
2064 Constant *C3 = getOperand(2);
2065 if (C1 == From) C1 = To;
2066 if (C2 == From) C2 = To;
2067 if (C3 == From) C3 = To;
2068 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00002069 } else if (isCompare()) {
2070 Constant *C1 = getOperand(0);
2071 Constant *C2 = getOperand(1);
2072 if (C1 == From) C1 = To;
2073 if (C2 == From) C2 = To;
2074 if (getOpcode() == Instruction::ICmp)
2075 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
2076 else
2077 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002078 } else if (getNumOperands() == 2) {
2079 Constant *C1 = getOperand(0);
2080 Constant *C2 = getOperand(1);
2081 if (C1 == From) C1 = To;
2082 if (C2 == From) C2 = To;
2083 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2084 } else {
2085 assert(0 && "Unknown ConstantExpr type!");
2086 return;
2087 }
2088
2089 assert(Replacement != this && "I didn't contain From!");
2090
Chris Lattner7a1450d2005-10-04 18:13:04 +00002091 // Everyone using this now uses the replacement.
2092 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002093
2094 // Delete the old constant!
2095 destroyConstant();
2096}
2097
2098
Jim Laskey2698f0d2006-03-08 18:11:07 +00002099/// getStringValue - Turn an LLVM constant pointer that eventually points to a
2100/// global into a string value. Return an empty string if we can't do it.
Evan Cheng38280c02006-03-10 23:52:03 +00002101/// Parameter Chop determines if the result is chopped at the first null
2102/// terminator.
Jim Laskey2698f0d2006-03-08 18:11:07 +00002103///
Evan Cheng38280c02006-03-10 23:52:03 +00002104std::string Constant::getStringValue(bool Chop, unsigned Offset) {
Jim Laskey2698f0d2006-03-08 18:11:07 +00002105 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(this)) {
2106 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
2107 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
2108 if (Init->isString()) {
2109 std::string Result = Init->getAsString();
2110 if (Offset < Result.size()) {
2111 // If we are pointing INTO The string, erase the beginning...
2112 Result.erase(Result.begin(), Result.begin()+Offset);
2113
2114 // Take off the null terminator, and any string fragments after it.
Evan Cheng38280c02006-03-10 23:52:03 +00002115 if (Chop) {
2116 std::string::size_type NullPos = Result.find_first_of((char)0);
2117 if (NullPos != std::string::npos)
2118 Result.erase(Result.begin()+NullPos, Result.end());
2119 }
Jim Laskey2698f0d2006-03-08 18:11:07 +00002120 return Result;
2121 }
2122 }
2123 }
2124 } else if (Constant *C = dyn_cast<Constant>(this)) {
2125 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
Evan Cheng2c5e5302006-03-11 00:13:10 +00002126 return GV->getStringValue(Chop, Offset);
Jim Laskey2698f0d2006-03-08 18:11:07 +00002127 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
2128 if (CE->getOpcode() == Instruction::GetElementPtr) {
2129 // Turn a gep into the specified offset.
2130 if (CE->getNumOperands() == 3 &&
2131 cast<Constant>(CE->getOperand(1))->isNullValue() &&
2132 isa<ConstantInt>(CE->getOperand(2))) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00002133 Offset += cast<ConstantInt>(CE->getOperand(2))->getZExtValue();
Evan Cheng2c5e5302006-03-11 00:13:10 +00002134 return CE->getOperand(0)->getStringValue(Chop, Offset);
Jim Laskey2698f0d2006-03-08 18:11:07 +00002135 }
2136 }
2137 }
2138 }
2139 return "";
2140}