blob: a1158e34de1974448174d82dd4119ddd715244b1 [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//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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 Lattner33e93b82007-02-27 03:05:06 +000015#include "ConstantFold.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
Evan Chengf9e003b2007-03-08 00:59:12 +000093/// ContaintsRelocations - Return true if the constant value contains
94/// relocations which cannot be resolved at compile time.
95bool Constant::ContainsRelocations() const {
96 if (isa<GlobalValue>(this))
97 return true;
98 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
99 if (getOperand(i)->ContainsRelocations())
100 return true;
101 return false;
102}
103
Chris Lattnerb1585a92002-08-13 17:50:20 +0000104// Static constructor to create a '0' constant of arbitrary type...
105Constant *Constant::getNullValue(const Type *Ty) {
Dale Johannesen98d3a082007-09-14 22:26:36 +0000106 static uint64_t zero[2] = {0, 0};
Chris Lattner6b727592004-06-17 18:19:28 +0000107 switch (Ty->getTypeID()) {
Chris Lattnerdbcb0d32007-02-20 05:46:39 +0000108 case Type::IntegerTyID:
109 return ConstantInt::get(Ty, 0);
110 case Type::FloatTyID:
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000111 return ConstantFP::get(APFloat(APInt(32, 0)));
Chris Lattnerdbcb0d32007-02-20 05:46:39 +0000112 case Type::DoubleTyID:
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000113 return ConstantFP::get(APFloat(APInt(64, 0)));
Dale Johannesenbdad8092007-08-09 22:51:36 +0000114 case Type::X86_FP80TyID:
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000115 return ConstantFP::get(APFloat(APInt(80, 2, zero)));
Dale Johannesenbdad8092007-08-09 22:51:36 +0000116 case Type::FP128TyID:
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000117 return ConstantFP::get(APFloat(APInt(128, 2, zero), true));
Dale Johannesen98d3a082007-09-14 22:26:36 +0000118 case Type::PPC_FP128TyID:
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000119 return ConstantFP::get(APFloat(APInt(128, 2, zero)));
Misha Brukmanb1c93172005-04-21 23:48:37 +0000120 case Type::PointerTyID:
Chris Lattnerb1585a92002-08-13 17:50:20 +0000121 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner9fba3da2004-02-15 05:53:04 +0000122 case Type::StructTyID:
123 case Type::ArrayTyID:
Reid Spencerd84d35b2007-02-15 02:26:10 +0000124 case Type::VectorTyID:
Chris Lattner9fba3da2004-02-15 05:53:04 +0000125 return ConstantAggregateZero::get(Ty);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000126 default:
Reid Spencercf394bf2004-07-04 11:51:24 +0000127 // Function, Label, or Opaque type?
128 assert(!"Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000129 return 0;
130 }
131}
132
Chris Lattner72e39582007-06-15 06:10:53 +0000133Constant *Constant::getAllOnesValue(const Type *Ty) {
134 if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty))
135 return ConstantInt::get(APInt::getAllOnesValue(ITy->getBitWidth()));
136 return ConstantVector::getAllOnesValue(cast<VectorType>(Ty));
137}
Chris Lattnerb1585a92002-08-13 17:50:20 +0000138
139// Static constructor to create an integral constant with all bits set
Zhou Sheng75b871f2007-01-11 12:24:14 +0000140ConstantInt *ConstantInt::getAllOnesValue(const Type *Ty) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000141 if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty))
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000142 return ConstantInt::get(APInt::getAllOnesValue(ITy->getBitWidth()));
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000143 return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000144}
145
Dan Gohman30978072007-05-24 14:36:04 +0000146/// @returns the value for a vector integer constant of the given type that
Chris Lattnerecab54c2007-01-04 01:49:26 +0000147/// has all its bits set to true.
148/// @brief Get the all ones value
Reid Spencerd84d35b2007-02-15 02:26:10 +0000149ConstantVector *ConstantVector::getAllOnesValue(const VectorType *Ty) {
Chris Lattnerecab54c2007-01-04 01:49:26 +0000150 std::vector<Constant*> Elts;
151 Elts.resize(Ty->getNumElements(),
Zhou Sheng75b871f2007-01-11 12:24:14 +0000152 ConstantInt::getAllOnesValue(Ty->getElementType()));
Dan Gohman30978072007-05-24 14:36:04 +0000153 assert(Elts[0] && "Not a vector integer type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +0000154 return cast<ConstantVector>(ConstantVector::get(Elts));
Chris Lattnerecab54c2007-01-04 01:49:26 +0000155}
156
157
Chris Lattner2105d662008-07-10 00:28:11 +0000158/// getVectorElements - This method, which is only valid on constant of vector
159/// type, returns the elements of the vector in the specified smallvector.
160/// This handles breaking down a vector undef into undef elements, etc.
161void Constant::getVectorElements(SmallVectorImpl<Constant*> &Elts) const {
162 assert(isa<VectorType>(getType()) && "Not a vector constant!");
163
164 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) {
165 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i)
166 Elts.push_back(CV->getOperand(i));
167 return;
168 }
169
170 const VectorType *VT = cast<VectorType>(getType());
171 if (isa<ConstantAggregateZero>(this)) {
172 Elts.assign(VT->getNumElements(),
173 Constant::getNullValue(VT->getElementType()));
174 return;
175 }
176
177 assert(isa<UndefValue>(this) && "Unknown vector constant type!");
178 Elts.assign(VT->getNumElements(), UndefValue::get(VT->getElementType()));
179}
180
181
182
Chris Lattner2f7c9632001-06-06 20:29:01 +0000183//===----------------------------------------------------------------------===//
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000184// ConstantInt
Chris Lattner2f7c9632001-06-06 20:29:01 +0000185//===----------------------------------------------------------------------===//
186
Reid Spencerb31bffe2007-02-26 23:54:03 +0000187ConstantInt::ConstantInt(const IntegerType *Ty, const APInt& V)
Chris Lattner5db2f472007-02-20 05:55:46 +0000188 : Constant(Ty, ConstantIntVal, 0, 0), Val(V) {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000189 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000190}
191
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000192ConstantInt *ConstantInt::TheTrueVal = 0;
193ConstantInt *ConstantInt::TheFalseVal = 0;
194
195namespace llvm {
196 void CleanupTrueFalse(void *) {
197 ConstantInt::ResetTrueFalse();
198 }
199}
200
201static ManagedCleanup<llvm::CleanupTrueFalse> TrueFalseCleanup;
202
203ConstantInt *ConstantInt::CreateTrueFalseVals(bool WhichOne) {
204 assert(TheTrueVal == 0 && TheFalseVal == 0);
205 TheTrueVal = get(Type::Int1Ty, 1);
206 TheFalseVal = get(Type::Int1Ty, 0);
207
208 // Ensure that llvm_shutdown nulls out TheTrueVal/TheFalseVal.
209 TrueFalseCleanup.Register();
210
211 return WhichOne ? TheTrueVal : TheFalseVal;
212}
213
214
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000215namespace {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000216 struct DenseMapAPIntKeyInfo {
217 struct KeyTy {
218 APInt val;
219 const Type* type;
220 KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
221 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
222 bool operator==(const KeyTy& that) const {
223 return type == that.type && this->val == that.val;
224 }
225 bool operator!=(const KeyTy& that) const {
226 return !this->operator==(that);
227 }
228 };
229 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
230 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000231 static unsigned getHashValue(const KeyTy &Key) {
Chris Lattner0625bd62007-09-17 18:34:04 +0000232 return DenseMapInfo<void*>::getHashValue(Key.type) ^
Reid Spencerb31bffe2007-02-26 23:54:03 +0000233 Key.val.getHashValue();
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000234 }
Chris Lattner0625bd62007-09-17 18:34:04 +0000235 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
236 return LHS == RHS;
237 }
Dale Johannesena719a602007-08-24 00:56:33 +0000238 static bool isPod() { return false; }
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000239 };
240}
241
242
Reid Spencerb31bffe2007-02-26 23:54:03 +0000243typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
244 DenseMapAPIntKeyInfo> IntMapTy;
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000245static ManagedStatic<IntMapTy> IntConstants;
246
Reid Spencer362fb292007-03-19 20:39:08 +0000247ConstantInt *ConstantInt::get(const Type *Ty, uint64_t V, bool isSigned) {
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000248 const IntegerType *ITy = cast<IntegerType>(Ty);
Reid Spencer362fb292007-03-19 20:39:08 +0000249 return get(APInt(ITy->getBitWidth(), V, isSigned));
Reid Spencerb31bffe2007-02-26 23:54:03 +0000250}
251
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000252// Get a ConstantInt from an APInt. Note that the value stored in the DenseMap
Dan Gohmanb3efe032008-02-07 02:30:40 +0000253// as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the
Reid Spencerb31bffe2007-02-26 23:54:03 +0000254// operator== and operator!= to ensure that the DenseMap doesn't attempt to
255// compare APInt's of different widths, which would violate an APInt class
256// invariant which generates an assertion.
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000257ConstantInt *ConstantInt::get(const APInt& V) {
258 // Get the corresponding integer type for the bit width of the value.
259 const IntegerType *ITy = IntegerType::get(V.getBitWidth());
Reid Spencerb31bffe2007-02-26 23:54:03 +0000260 // get an existing value or the insertion position
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000261 DenseMapAPIntKeyInfo::KeyTy Key(V, ITy);
Reid Spencerb31bffe2007-02-26 23:54:03 +0000262 ConstantInt *&Slot = (*IntConstants)[Key];
263 // if it exists, return it.
264 if (Slot)
265 return Slot;
266 // otherwise create a new one, insert it, and return it.
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000267 return Slot = new ConstantInt(ITy, V);
268}
269
270//===----------------------------------------------------------------------===//
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000271// ConstantFP
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000272//===----------------------------------------------------------------------===//
273
Chris Lattner98bd9392008-04-09 06:38:30 +0000274static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
275 if (Ty == Type::FloatTy)
276 return &APFloat::IEEEsingle;
277 if (Ty == Type::DoubleTy)
278 return &APFloat::IEEEdouble;
279 if (Ty == Type::X86_FP80Ty)
280 return &APFloat::x87DoubleExtended;
281 else if (Ty == Type::FP128Ty)
282 return &APFloat::IEEEquad;
283
284 assert(Ty == Type::PPC_FP128Ty && "Unknown FP format");
285 return &APFloat::PPCDoubleDouble;
286}
287
Dale Johannesend246b2c2007-08-30 00:23:21 +0000288ConstantFP::ConstantFP(const Type *Ty, const APFloat& V)
289 : Constant(Ty, ConstantFPVal, 0, 0), Val(V) {
Chris Lattner98bd9392008-04-09 06:38:30 +0000290 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
291 "FP type Mismatch");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000292}
293
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000294bool ConstantFP::isNullValue() const {
Dale Johannesena719a602007-08-24 00:56:33 +0000295 return Val.isZero() && !Val.isNegative();
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000296}
297
Dale Johannesen98d3a082007-09-14 22:26:36 +0000298ConstantFP *ConstantFP::getNegativeZero(const Type *Ty) {
299 APFloat apf = cast <ConstantFP>(Constant::getNullValue(Ty))->getValueAPF();
300 apf.changeSign();
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000301 return ConstantFP::get(apf);
Dale Johannesen98d3a082007-09-14 22:26:36 +0000302}
303
Dale Johannesend246b2c2007-08-30 00:23:21 +0000304bool ConstantFP::isExactlyValue(const APFloat& V) const {
305 return Val.bitwiseIsEqual(V);
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000306}
307
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000308namespace {
Dale Johannesena719a602007-08-24 00:56:33 +0000309 struct DenseMapAPFloatKeyInfo {
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000310 struct KeyTy {
311 APFloat val;
312 KeyTy(const APFloat& V) : val(V){}
313 KeyTy(const KeyTy& that) : val(that.val) {}
314 bool operator==(const KeyTy& that) const {
315 return this->val.bitwiseIsEqual(that.val);
316 }
317 bool operator!=(const KeyTy& that) const {
318 return !this->operator==(that);
319 }
320 };
321 static inline KeyTy getEmptyKey() {
322 return KeyTy(APFloat(APFloat::Bogus,1));
Reid Spencerb31bffe2007-02-26 23:54:03 +0000323 }
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000324 static inline KeyTy getTombstoneKey() {
325 return KeyTy(APFloat(APFloat::Bogus,2));
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000326 }
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000327 static unsigned getHashValue(const KeyTy &Key) {
328 return Key.val.getHashValue();
Dale Johannesena719a602007-08-24 00:56:33 +0000329 }
Chris Lattner0625bd62007-09-17 18:34:04 +0000330 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
331 return LHS == RHS;
332 }
Dale Johannesena719a602007-08-24 00:56:33 +0000333 static bool isPod() { return false; }
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000334 };
335}
336
337//---- ConstantFP::get() implementation...
338//
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000339typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
Dale Johannesena719a602007-08-24 00:56:33 +0000340 DenseMapAPFloatKeyInfo> FPMapTy;
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000341
Dale Johannesena719a602007-08-24 00:56:33 +0000342static ManagedStatic<FPMapTy> FPConstants;
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000343
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000344ConstantFP *ConstantFP::get(const APFloat &V) {
Dale Johannesend246b2c2007-08-30 00:23:21 +0000345 DenseMapAPFloatKeyInfo::KeyTy Key(V);
346 ConstantFP *&Slot = (*FPConstants)[Key];
347 if (Slot) return Slot;
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000348
349 const Type *Ty;
350 if (&V.getSemantics() == &APFloat::IEEEsingle)
351 Ty = Type::FloatTy;
352 else if (&V.getSemantics() == &APFloat::IEEEdouble)
353 Ty = Type::DoubleTy;
354 else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
355 Ty = Type::X86_FP80Ty;
356 else if (&V.getSemantics() == &APFloat::IEEEquad)
357 Ty = Type::FP128Ty;
358 else {
359 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble&&"Unknown FP format");
360 Ty = Type::PPC_FP128Ty;
361 }
362
Dale Johannesend246b2c2007-08-30 00:23:21 +0000363 return Slot = new ConstantFP(Ty, V);
364}
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000365
Chris Lattner98bd9392008-04-09 06:38:30 +0000366/// get() - This returns a constant fp for the specified value in the
367/// specified type. This should only be used for simple constant values like
368/// 2.0/1.0 etc, that are known-valid both as double and as the target format.
369ConstantFP *ConstantFP::get(const Type *Ty, double V) {
370 APFloat FV(V);
371 FV.convert(*TypeToFloatSemantics(Ty), APFloat::rmNearestTiesToEven);
372 return get(FV);
373}
374
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000375//===----------------------------------------------------------------------===//
376// ConstantXXX Classes
377//===----------------------------------------------------------------------===//
378
379
Chris Lattner3462ae32001-12-03 22:26:30 +0000380ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000381 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000382 : Constant(T, ConstantArrayVal,
383 OperandTraits<ConstantArray>::op_end(this) - V.size(),
384 V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000385 assert(V.size() == T->getNumElements() &&
386 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000387 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000388 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
389 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000390 Constant *C = *I;
391 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000392 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000393 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000394 "Initializer for array element doesn't match array element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000395 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000396 }
397}
398
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000399
Chris Lattner3462ae32001-12-03 22:26:30 +0000400ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000401 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000402 : Constant(T, ConstantStructVal,
403 OperandTraits<ConstantStruct>::op_end(this) - V.size(),
404 V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000405 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000406 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000407 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000408 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
409 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000410 Constant *C = *I;
411 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000412 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000413 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000414 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000415 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000416 "Initializer for struct element doesn't match struct element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000417 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000418 }
419}
420
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000421
Reid Spencerd84d35b2007-02-15 02:26:10 +0000422ConstantVector::ConstantVector(const VectorType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000423 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000424 : Constant(T, ConstantVectorVal,
425 OperandTraits<ConstantVector>::op_end(this) - V.size(),
426 V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000427 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000428 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
429 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000430 Constant *C = *I;
431 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000432 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000433 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Dan Gohman30978072007-05-24 14:36:04 +0000434 "Initializer for vector element doesn't match vector element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000435 *OL = C;
Brian Gaeke02209042004-08-20 06:00:58 +0000436 }
437}
438
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000439
Gabor Greiff6caff662008-05-10 08:32:32 +0000440namespace llvm {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000441// We declare several classes private to this file, so use an anonymous
442// namespace
443namespace {
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000444
Gordon Henriksen14a55692007-12-10 02:14:30 +0000445/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
446/// behind the scenes to implement unary constant exprs.
447class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000448 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000449public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000450 // allocate space for exactly one operand
451 void *operator new(size_t s) {
452 return User::operator new(s, 1);
453 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000454 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
Gabor Greiff6caff662008-05-10 08:32:32 +0000455 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
456 Op<0>() = C;
457 }
458 /// Transparently provide more efficient getOperand methods.
459 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000460};
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000461
Gordon Henriksen14a55692007-12-10 02:14:30 +0000462/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
463/// behind the scenes to implement binary constant exprs.
464class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000465 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000466public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000467 // allocate space for exactly two operands
468 void *operator new(size_t s) {
469 return User::operator new(s, 2);
470 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000471 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Gabor Greiff6caff662008-05-10 08:32:32 +0000472 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000473 Op<0>() = C1;
474 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000475 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000476 /// Transparently provide more efficient getOperand methods.
477 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000478};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000479
Gordon Henriksen14a55692007-12-10 02:14:30 +0000480/// SelectConstantExpr - This class is private to Constants.cpp, and is used
481/// behind the scenes to implement select constant exprs.
482class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000483 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000484public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000485 // allocate space for exactly three operands
486 void *operator new(size_t s) {
487 return User::operator new(s, 3);
488 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000489 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Gabor Greiff6caff662008-05-10 08:32:32 +0000490 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000491 Op<0>() = C1;
492 Op<1>() = C2;
493 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000494 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000495 /// Transparently provide more efficient getOperand methods.
496 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000497};
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000498
Gordon Henriksen14a55692007-12-10 02:14:30 +0000499/// ExtractElementConstantExpr - This class is private to
500/// Constants.cpp, and is used behind the scenes to implement
501/// extractelement constant exprs.
502class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000503 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000504public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000505 // allocate space for exactly two operands
506 void *operator new(size_t s) {
507 return User::operator new(s, 2);
508 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000509 ExtractElementConstantExpr(Constant *C1, Constant *C2)
510 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000511 Instruction::ExtractElement, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000512 Op<0>() = C1;
513 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000514 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000515 /// Transparently provide more efficient getOperand methods.
516 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000517};
Robert Bocchino23004482006-01-10 19:05:34 +0000518
Gordon Henriksen14a55692007-12-10 02:14:30 +0000519/// InsertElementConstantExpr - This class is private to
520/// Constants.cpp, and is used behind the scenes to implement
521/// insertelement constant exprs.
522class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000523 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000524public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000525 // allocate space for exactly three operands
526 void *operator new(size_t s) {
527 return User::operator new(s, 3);
528 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000529 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
530 : ConstantExpr(C1->getType(), Instruction::InsertElement,
Gabor Greiff6caff662008-05-10 08:32:32 +0000531 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000532 Op<0>() = C1;
533 Op<1>() = C2;
534 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000535 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000536 /// Transparently provide more efficient getOperand methods.
537 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000538};
Robert Bocchinoca27f032006-01-17 20:07:22 +0000539
Gordon Henriksen14a55692007-12-10 02:14:30 +0000540/// ShuffleVectorConstantExpr - This class is private to
541/// Constants.cpp, and is used behind the scenes to implement
542/// shufflevector constant exprs.
543class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000544 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000545public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000546 // allocate space for exactly three operands
547 void *operator new(size_t s) {
548 return User::operator new(s, 3);
549 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000550 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
551 : ConstantExpr(C1->getType(), Instruction::ShuffleVector,
Gabor Greiff6caff662008-05-10 08:32:32 +0000552 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000553 Op<0>() = C1;
554 Op<1>() = C2;
555 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000556 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000557 /// Transparently provide more efficient getOperand methods.
558 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000559};
560
Dan Gohman12fce772008-05-15 19:50:34 +0000561/// ExtractValueConstantExpr - This class is private to
562/// Constants.cpp, and is used behind the scenes to implement
563/// extractvalue constant exprs.
564class VISIBILITY_HIDDEN ExtractValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000565 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000566public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000567 // allocate space for exactly one operand
568 void *operator new(size_t s) {
569 return User::operator new(s, 1);
Dan Gohman12fce772008-05-15 19:50:34 +0000570 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000571 ExtractValueConstantExpr(Constant *Agg,
572 const SmallVector<unsigned, 4> &IdxList,
573 const Type *DestTy)
574 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
575 Indices(IdxList) {
576 Op<0>() = Agg;
577 }
578
Dan Gohman7bb04502008-05-31 19:09:08 +0000579 /// Indices - These identify which value to extract.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000580 const SmallVector<unsigned, 4> Indices;
581
Dan Gohman12fce772008-05-15 19:50:34 +0000582 /// Transparently provide more efficient getOperand methods.
583 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
584};
585
586/// InsertValueConstantExpr - This class is private to
587/// Constants.cpp, and is used behind the scenes to implement
588/// insertvalue constant exprs.
589class VISIBILITY_HIDDEN InsertValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000590 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000591public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000592 // allocate space for exactly one operand
593 void *operator new(size_t s) {
594 return User::operator new(s, 2);
Dan Gohman12fce772008-05-15 19:50:34 +0000595 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000596 InsertValueConstantExpr(Constant *Agg, Constant *Val,
597 const SmallVector<unsigned, 4> &IdxList,
598 const Type *DestTy)
599 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
600 Indices(IdxList) {
601 Op<0>() = Agg;
602 Op<1>() = Val;
603 }
604
Dan Gohman7bb04502008-05-31 19:09:08 +0000605 /// Indices - These identify the position for the insertion.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000606 const SmallVector<unsigned, 4> Indices;
607
Dan Gohman12fce772008-05-15 19:50:34 +0000608 /// Transparently provide more efficient getOperand methods.
609 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
610};
611
612
Gordon Henriksen14a55692007-12-10 02:14:30 +0000613/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
614/// used behind the scenes to implement getelementpr constant exprs.
Gabor Greife9ecc682008-04-06 20:25:17 +0000615class VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000616 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000617 const Type *DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000618public:
Gabor Greif697e94c2008-05-15 10:04:30 +0000619 static GetElementPtrConstantExpr *Create(Constant *C,
620 const std::vector<Constant*>&IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000621 const Type *DestTy) {
Gabor Greif697e94c2008-05-15 10:04:30 +0000622 return new(IdxList.size() + 1)
623 GetElementPtrConstantExpr(C, IdxList, DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000624 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000625 /// Transparently provide more efficient getOperand methods.
626 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000627};
628
629// CompareConstantExpr - This class is private to Constants.cpp, and is used
630// behind the scenes to implement ICmp and FCmp constant expressions. This is
631// needed in order to store the predicate value for these instructions.
632struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000633 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
634 // allocate space for exactly two operands
635 void *operator new(size_t s) {
636 return User::operator new(s, 2);
637 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000638 unsigned short predicate;
Nate Begemand2195702008-05-12 19:01:56 +0000639 CompareConstantExpr(const Type *ty, Instruction::OtherOps opc,
640 unsigned short pred, Constant* LHS, Constant* RHS)
641 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000642 Op<0>() = LHS;
643 Op<1>() = RHS;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000644 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000645 /// Transparently provide more efficient getOperand methods.
646 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000647};
648
649} // end anonymous namespace
650
Gabor Greiff6caff662008-05-10 08:32:32 +0000651template <>
652struct OperandTraits<UnaryConstantExpr> : FixedNumOperandTraits<1> {
653};
654DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
655
656template <>
657struct OperandTraits<BinaryConstantExpr> : FixedNumOperandTraits<2> {
658};
659DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
660
661template <>
662struct OperandTraits<SelectConstantExpr> : FixedNumOperandTraits<3> {
663};
664DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
665
666template <>
667struct OperandTraits<ExtractElementConstantExpr> : FixedNumOperandTraits<2> {
668};
669DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
670
671template <>
672struct OperandTraits<InsertElementConstantExpr> : FixedNumOperandTraits<3> {
673};
674DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
675
676template <>
677struct OperandTraits<ShuffleVectorConstantExpr> : FixedNumOperandTraits<3> {
678};
679DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
680
Dan Gohman12fce772008-05-15 19:50:34 +0000681template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000682struct OperandTraits<ExtractValueConstantExpr> : FixedNumOperandTraits<1> {
Dan Gohman12fce772008-05-15 19:50:34 +0000683};
Dan Gohman12fce772008-05-15 19:50:34 +0000684DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
685
686template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000687struct OperandTraits<InsertValueConstantExpr> : FixedNumOperandTraits<2> {
Dan Gohman12fce772008-05-15 19:50:34 +0000688};
Dan Gohman12fce772008-05-15 19:50:34 +0000689DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
690
Gabor Greiff6caff662008-05-10 08:32:32 +0000691template <>
692struct OperandTraits<GetElementPtrConstantExpr> : VariadicOperandTraits<1> {
693};
694
695GetElementPtrConstantExpr::GetElementPtrConstantExpr
696 (Constant *C,
697 const std::vector<Constant*> &IdxList,
698 const Type *DestTy)
699 : ConstantExpr(DestTy, Instruction::GetElementPtr,
700 OperandTraits<GetElementPtrConstantExpr>::op_end(this)
701 - (IdxList.size()+1),
702 IdxList.size()+1) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000703 OperandList[0] = C;
Gabor Greiff6caff662008-05-10 08:32:32 +0000704 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000705 OperandList[i+1] = IdxList[i];
Gabor Greiff6caff662008-05-10 08:32:32 +0000706}
707
708DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
709
710
711template <>
712struct OperandTraits<CompareConstantExpr> : FixedNumOperandTraits<2> {
713};
714DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
715
716
717} // End llvm namespace
718
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000719
720// Utility function for determining if a ConstantExpr is a CastOp or not. This
721// can't be inline because we don't want to #include Instruction.h into
722// Constant.h
723bool ConstantExpr::isCast() const {
724 return Instruction::isCast(getOpcode());
725}
726
Reid Spenceree3c9912006-12-04 05:19:50 +0000727bool ConstantExpr::isCompare() const {
728 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
729}
730
Dan Gohman1ecaf452008-05-31 00:58:22 +0000731bool ConstantExpr::hasIndices() const {
732 return getOpcode() == Instruction::ExtractValue ||
733 getOpcode() == Instruction::InsertValue;
734}
735
736const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const {
737 if (const ExtractValueConstantExpr *EVCE =
738 dyn_cast<ExtractValueConstantExpr>(this))
739 return EVCE->Indices;
Dan Gohmana469bdb2008-06-23 16:39:44 +0000740
741 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman1ecaf452008-05-31 00:58:22 +0000742}
743
Chris Lattner817175f2004-03-29 02:37:53 +0000744/// ConstantExpr::get* - Return some common constants without having to
745/// specify the full Instruction::OPCODE identifier.
746///
747Constant *ConstantExpr::getNeg(Constant *C) {
Reid Spencer2eadb532007-01-21 00:29:26 +0000748 return get(Instruction::Sub,
749 ConstantExpr::getZeroValueForNegationExpr(C->getType()),
750 C);
Chris Lattner817175f2004-03-29 02:37:53 +0000751}
752Constant *ConstantExpr::getNot(Constant *C) {
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000753 assert(isa<IntegerType>(C->getType()) && "Cannot NOT a nonintegral value!");
Chris Lattner817175f2004-03-29 02:37:53 +0000754 return get(Instruction::Xor, C,
Zhou Sheng75b871f2007-01-11 12:24:14 +0000755 ConstantInt::getAllOnesValue(C->getType()));
Chris Lattner817175f2004-03-29 02:37:53 +0000756}
757Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
758 return get(Instruction::Add, C1, C2);
759}
760Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
761 return get(Instruction::Sub, C1, C2);
762}
763Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
764 return get(Instruction::Mul, C1, C2);
765}
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000766Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2) {
767 return get(Instruction::UDiv, C1, C2);
768}
769Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2) {
770 return get(Instruction::SDiv, C1, C2);
771}
772Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) {
773 return get(Instruction::FDiv, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000774}
Reid Spencer7eb55b32006-11-02 01:53:59 +0000775Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) {
776 return get(Instruction::URem, C1, C2);
777}
778Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) {
779 return get(Instruction::SRem, C1, C2);
780}
781Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) {
782 return get(Instruction::FRem, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000783}
784Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
785 return get(Instruction::And, C1, C2);
786}
787Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
788 return get(Instruction::Or, C1, C2);
789}
790Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
791 return get(Instruction::Xor, C1, C2);
792}
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000793unsigned ConstantExpr::getPredicate() const {
Nate Begemand2195702008-05-12 19:01:56 +0000794 assert(getOpcode() == Instruction::FCmp ||
795 getOpcode() == Instruction::ICmp ||
796 getOpcode() == Instruction::VFCmp ||
797 getOpcode() == Instruction::VICmp);
Chris Lattneref650092007-10-18 16:26:24 +0000798 return ((const CompareConstantExpr*)this)->predicate;
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000799}
Chris Lattner817175f2004-03-29 02:37:53 +0000800Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
801 return get(Instruction::Shl, C1, C2);
802}
Reid Spencerfdff9382006-11-08 06:47:33 +0000803Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2) {
804 return get(Instruction::LShr, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000805}
Reid Spencerfdff9382006-11-08 06:47:33 +0000806Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2) {
807 return get(Instruction::AShr, C1, C2);
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000808}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000809
Chris Lattner7c1018a2006-07-14 19:37:40 +0000810/// getWithOperandReplaced - Return a constant expression identical to this
811/// one, but with the specified operand set to the specified value.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000812Constant *
813ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +0000814 assert(OpNo < getNumOperands() && "Operand num is out of range!");
815 assert(Op->getType() == getOperand(OpNo)->getType() &&
816 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000817 if (getOperand(OpNo) == Op)
818 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000819
Chris Lattner227816342006-07-14 22:20:01 +0000820 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000821 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000822 case Instruction::Trunc:
823 case Instruction::ZExt:
824 case Instruction::SExt:
825 case Instruction::FPTrunc:
826 case Instruction::FPExt:
827 case Instruction::UIToFP:
828 case Instruction::SIToFP:
829 case Instruction::FPToUI:
830 case Instruction::FPToSI:
831 case Instruction::PtrToInt:
832 case Instruction::IntToPtr:
833 case Instruction::BitCast:
834 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000835 case Instruction::Select:
836 Op0 = (OpNo == 0) ? Op : getOperand(0);
837 Op1 = (OpNo == 1) ? Op : getOperand(1);
838 Op2 = (OpNo == 2) ? Op : getOperand(2);
839 return ConstantExpr::getSelect(Op0, Op1, Op2);
840 case Instruction::InsertElement:
841 Op0 = (OpNo == 0) ? Op : getOperand(0);
842 Op1 = (OpNo == 1) ? Op : getOperand(1);
843 Op2 = (OpNo == 2) ? Op : getOperand(2);
844 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
845 case Instruction::ExtractElement:
846 Op0 = (OpNo == 0) ? Op : getOperand(0);
847 Op1 = (OpNo == 1) ? Op : getOperand(1);
848 return ConstantExpr::getExtractElement(Op0, Op1);
849 case Instruction::ShuffleVector:
850 Op0 = (OpNo == 0) ? Op : getOperand(0);
851 Op1 = (OpNo == 1) ? Op : getOperand(1);
852 Op2 = (OpNo == 2) ? Op : getOperand(2);
853 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Dan Gohman12fce772008-05-15 19:50:34 +0000854 case Instruction::InsertValue: {
Dan Gohmana469bdb2008-06-23 16:39:44 +0000855 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman1ecaf452008-05-31 00:58:22 +0000856 Op0 = (OpNo == 0) ? Op : getOperand(0);
857 Op1 = (OpNo == 1) ? Op : getOperand(1);
858 return ConstantExpr::getInsertValue(Op0, Op1,
859 &Indices[0], Indices.size());
Dan Gohman12fce772008-05-15 19:50:34 +0000860 }
861 case Instruction::ExtractValue: {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000862 assert(OpNo == 0 && "ExtractaValue has only one operand!");
Dan Gohmana469bdb2008-06-23 16:39:44 +0000863 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman1ecaf452008-05-31 00:58:22 +0000864 return
865 ConstantExpr::getExtractValue(Op, &Indices[0], Indices.size());
Dan Gohman12fce772008-05-15 19:50:34 +0000866 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000867 case Instruction::GetElementPtr: {
Chris Lattnerb5d70302007-02-19 20:01:23 +0000868 SmallVector<Constant*, 8> Ops;
Dan Gohman12fce772008-05-15 19:50:34 +0000869 Ops.resize(getNumOperands()-1);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000870 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Dan Gohman12fce772008-05-15 19:50:34 +0000871 Ops[i-1] = getOperand(i);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000872 if (OpNo == 0)
Chris Lattnerb5d70302007-02-19 20:01:23 +0000873 return ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000874 Ops[OpNo-1] = Op;
Chris Lattnerb5d70302007-02-19 20:01:23 +0000875 return ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000876 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000877 default:
878 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000879 Op0 = (OpNo == 0) ? Op : getOperand(0);
880 Op1 = (OpNo == 1) ? Op : getOperand(1);
881 return ConstantExpr::get(getOpcode(), Op0, Op1);
882 }
883}
884
885/// getWithOperands - This returns the current constant expression with the
886/// operands replaced with the specified values. The specified operands must
887/// match count and type with the existing ones.
888Constant *ConstantExpr::
889getWithOperands(const std::vector<Constant*> &Ops) const {
890 assert(Ops.size() == getNumOperands() && "Operand count mismatch!");
891 bool AnyChange = false;
892 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
893 assert(Ops[i]->getType() == getOperand(i)->getType() &&
894 "Operand type mismatch!");
895 AnyChange |= Ops[i] != getOperand(i);
896 }
897 if (!AnyChange) // No operands changed, return self.
898 return const_cast<ConstantExpr*>(this);
899
900 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000901 case Instruction::Trunc:
902 case Instruction::ZExt:
903 case Instruction::SExt:
904 case Instruction::FPTrunc:
905 case Instruction::FPExt:
906 case Instruction::UIToFP:
907 case Instruction::SIToFP:
908 case Instruction::FPToUI:
909 case Instruction::FPToSI:
910 case Instruction::PtrToInt:
911 case Instruction::IntToPtr:
912 case Instruction::BitCast:
913 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattner227816342006-07-14 22:20:01 +0000914 case Instruction::Select:
915 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
916 case Instruction::InsertElement:
917 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
918 case Instruction::ExtractElement:
919 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
920 case Instruction::ShuffleVector:
921 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Dan Gohman1ecaf452008-05-31 00:58:22 +0000922 case Instruction::InsertValue: {
Dan Gohmana469bdb2008-06-23 16:39:44 +0000923 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman1ecaf452008-05-31 00:58:22 +0000924 return ConstantExpr::getInsertValue(Ops[0], Ops[1],
925 &Indices[0], Indices.size());
926 }
927 case Instruction::ExtractValue: {
Dan Gohmana469bdb2008-06-23 16:39:44 +0000928 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman1ecaf452008-05-31 00:58:22 +0000929 return ConstantExpr::getExtractValue(Ops[0],
930 &Indices[0], Indices.size());
931 }
Chris Lattnerb5d70302007-02-19 20:01:23 +0000932 case Instruction::GetElementPtr:
933 return ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], Ops.size()-1);
Reid Spencer266e42b2006-12-23 06:05:41 +0000934 case Instruction::ICmp:
935 case Instruction::FCmp:
936 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattner227816342006-07-14 22:20:01 +0000937 default:
938 assert(getNumOperands() == 2 && "Must be binary operator?");
939 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000940 }
941}
942
Chris Lattner2f7c9632001-06-06 20:29:01 +0000943
944//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000945// isValueValidForType implementations
946
Reid Spencere7334722006-12-19 01:28:19 +0000947bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000948 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000949 if (Ty == Type::Int1Ty)
950 return Val == 0 || Val == 1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000951 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000952 return true; // always true, has to fit in largest type
953 uint64_t Max = (1ll << NumBits) - 1;
954 return Val <= Max;
Reid Spencere7334722006-12-19 01:28:19 +0000955}
956
Reid Spencere0fc4df2006-10-20 07:07:24 +0000957bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000958 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000959 if (Ty == Type::Int1Ty)
Reid Spencera94d3942007-01-19 21:13:56 +0000960 return Val == 0 || Val == 1 || Val == -1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000961 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000962 return true; // always true, has to fit in largest type
963 int64_t Min = -(1ll << (NumBits-1));
964 int64_t Max = (1ll << (NumBits-1)) - 1;
965 return (Val >= Min && Val <= Max);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000966}
967
Dale Johannesend246b2c2007-08-30 00:23:21 +0000968bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) {
969 // convert modifies in place, so make a copy.
970 APFloat Val2 = APFloat(Val);
Chris Lattner6b727592004-06-17 18:19:28 +0000971 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000972 default:
973 return false; // These can't be represented as floating point!
974
Dale Johannesend246b2c2007-08-30 00:23:21 +0000975 // FIXME rounding mode needs to be more flexible
Chris Lattner2f7c9632001-06-06 20:29:01 +0000976 case Type::FloatTyID:
Dale Johannesend246b2c2007-08-30 00:23:21 +0000977 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
978 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven) ==
979 APFloat::opOK;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000980 case Type::DoubleTyID:
Dale Johannesend246b2c2007-08-30 00:23:21 +0000981 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
982 &Val2.getSemantics() == &APFloat::IEEEdouble ||
983 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven) ==
984 APFloat::opOK;
Dale Johannesenbdad8092007-08-09 22:51:36 +0000985 case Type::X86_FP80TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +0000986 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
987 &Val2.getSemantics() == &APFloat::IEEEdouble ||
988 &Val2.getSemantics() == &APFloat::x87DoubleExtended;
Dale Johannesenbdad8092007-08-09 22:51:36 +0000989 case Type::FP128TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +0000990 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
991 &Val2.getSemantics() == &APFloat::IEEEdouble ||
992 &Val2.getSemantics() == &APFloat::IEEEquad;
Dale Johannesen007aa372007-10-11 18:07:22 +0000993 case Type::PPC_FP128TyID:
994 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
995 &Val2.getSemantics() == &APFloat::IEEEdouble ||
996 &Val2.getSemantics() == &APFloat::PPCDoubleDouble;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000997 }
Chris Lattneraa2372562006-05-24 17:04:05 +0000998}
Chris Lattner9655e542001-07-20 19:16:02 +0000999
Chris Lattner49d855c2001-09-07 16:46:31 +00001000//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +00001001// Factory Function Implementation
1002
Gabor Greiff6caff662008-05-10 08:32:32 +00001003
1004// The number of operands for each ConstantCreator::create method is
1005// determined by the ConstantTraits template.
Chris Lattner98fa07b2003-05-23 20:03:32 +00001006// ConstantCreator - A class that is used to create constants by
1007// ValueMap*. This class should be partially specialized if there is
1008// something strange that needs to be done to interface to the ctor for the
1009// constant.
1010//
Chris Lattner189d19f2003-11-21 20:23:48 +00001011namespace llvm {
Gabor Greiff6caff662008-05-10 08:32:32 +00001012 template<class ValType>
1013 struct ConstantTraits;
1014
1015 template<typename T, typename Alloc>
1016 struct VISIBILITY_HIDDEN ConstantTraits< std::vector<T, Alloc> > {
1017 static unsigned uses(const std::vector<T, Alloc>& v) {
1018 return v.size();
1019 }
1020 };
1021
Chris Lattner189d19f2003-11-21 20:23:48 +00001022 template<class ConstantClass, class TypeClass, class ValType>
Chris Lattner02157b02006-06-28 21:38:54 +00001023 struct VISIBILITY_HIDDEN ConstantCreator {
Chris Lattner189d19f2003-11-21 20:23:48 +00001024 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001025 return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V);
Chris Lattner189d19f2003-11-21 20:23:48 +00001026 }
1027 };
Misha Brukmanb1c93172005-04-21 23:48:37 +00001028
Chris Lattner189d19f2003-11-21 20:23:48 +00001029 template<class ConstantClass, class TypeClass>
Chris Lattner02157b02006-06-28 21:38:54 +00001030 struct VISIBILITY_HIDDEN ConvertConstantType {
Chris Lattner189d19f2003-11-21 20:23:48 +00001031 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
1032 assert(0 && "This type cannot be converted!\n");
1033 abort();
1034 }
1035 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001036
Chris Lattner935aa922005-10-04 17:48:46 +00001037 template<class ValType, class TypeClass, class ConstantClass,
1038 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattner02157b02006-06-28 21:38:54 +00001039 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
Chris Lattnerb64419a2005-10-03 22:51:37 +00001040 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +00001041 typedef std::pair<const Type*, ValType> MapKey;
1042 typedef std::map<MapKey, Constant *> MapTy;
1043 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
1044 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
Chris Lattnerb64419a2005-10-03 22:51:37 +00001045 private:
Chris Lattner5bbf60a52005-10-04 16:52:46 +00001046 /// Map - This is the main map from the element descriptor to the Constants.
1047 /// This is the primary way we avoid creating two of the same shape
1048 /// constant.
Chris Lattnerb50d1352003-10-05 00:17:43 +00001049 MapTy Map;
Chris Lattner935aa922005-10-04 17:48:46 +00001050
1051 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
1052 /// from the constants to their element in Map. This is important for
1053 /// removal of constants from the array, which would otherwise have to scan
1054 /// through the map with very large keys.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001055 InverseMapTy InverseMap;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001056
Jim Laskeyc03caef2006-07-17 17:38:29 +00001057 /// AbstractTypeMap - Map for abstract type constants.
1058 ///
Chris Lattnerb50d1352003-10-05 00:17:43 +00001059 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner99a669b2004-11-19 16:39:44 +00001060
Chris Lattner98fa07b2003-05-23 20:03:32 +00001061 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +00001062 typename MapTy::iterator map_end() { return Map.end(); }
Chris Lattnerb64419a2005-10-03 22:51:37 +00001063
1064 /// InsertOrGetItem - Return an iterator for the specified element.
1065 /// If the element exists in the map, the returned iterator points to the
1066 /// entry and Exists=true. If not, the iterator points to the newly
1067 /// inserted entry and returns Exists=false. Newly inserted entries have
1068 /// I->second == 0, and should be filled in.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001069 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
1070 &InsertVal,
Chris Lattnerb64419a2005-10-03 22:51:37 +00001071 bool &Exists) {
Jim Laskeyc03caef2006-07-17 17:38:29 +00001072 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001073 Exists = !IP.second;
1074 return IP.first;
1075 }
Chris Lattner5bbf60a52005-10-04 16:52:46 +00001076
Chris Lattner935aa922005-10-04 17:48:46 +00001077private:
Jim Laskeyc03caef2006-07-17 17:38:29 +00001078 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
Chris Lattner935aa922005-10-04 17:48:46 +00001079 if (HasLargeKey) {
Jim Laskeyc03caef2006-07-17 17:38:29 +00001080 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
Chris Lattner935aa922005-10-04 17:48:46 +00001081 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
1082 IMI->second->second == CP &&
1083 "InverseMap corrupt!");
1084 return IMI->second;
1085 }
1086
Jim Laskeyc03caef2006-07-17 17:38:29 +00001087 typename MapTy::iterator I =
Chris Lattner935aa922005-10-04 17:48:46 +00001088 Map.find(MapKey((TypeClass*)CP->getRawType(), getValType(CP)));
Chris Lattner5bbf60a52005-10-04 16:52:46 +00001089 if (I == Map.end() || I->second != CP) {
1090 // FIXME: This should not use a linear scan. If this gets to be a
1091 // performance problem, someone should look at this.
1092 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
1093 /* empty */;
1094 }
Chris Lattner935aa922005-10-04 17:48:46 +00001095 return I;
1096 }
1097public:
1098
Chris Lattnerb64419a2005-10-03 22:51:37 +00001099 /// getOrCreate - Return the specified constant from the map, creating it if
1100 /// necessary.
Chris Lattner98fa07b2003-05-23 20:03:32 +00001101 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00001102 MapKey Lookup(Ty, V);
Dan Gohman3707f1d2008-07-11 20:58:19 +00001103 typename MapTy::iterator I = Map.find(Lookup);
Reid Spencere0fc4df2006-10-20 07:07:24 +00001104 // Is it in the map?
Dan Gohman3707f1d2008-07-11 20:58:19 +00001105 if (I != Map.end())
Reid Spencere0fc4df2006-10-20 07:07:24 +00001106 return static_cast<ConstantClass *>(I->second);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001107
1108 // If no preexisting value, create one now...
1109 ConstantClass *Result =
1110 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
1111
Chris Lattnerb50d1352003-10-05 00:17:43 +00001112 /// FIXME: why does this assert fail when loading 176.gcc?
1113 //assert(Result->getType() == Ty && "Type specified is not correct!");
1114 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
1115
Chris Lattner935aa922005-10-04 17:48:46 +00001116 if (HasLargeKey) // Remember the reverse mapping if needed.
1117 InverseMap.insert(std::make_pair(Result, I));
1118
Chris Lattnerb50d1352003-10-05 00:17:43 +00001119 // If the type of the constant is abstract, make sure that an entry exists
1120 // for it in the AbstractTypeMap.
1121 if (Ty->isAbstract()) {
Dan Gohman3707f1d2008-07-11 20:58:19 +00001122 typename AbstractTypeMapTy::iterator TI = AbstractTypeMap.find(Ty);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001123
Dan Gohman3707f1d2008-07-11 20:58:19 +00001124 if (TI == AbstractTypeMap.end()) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00001125 // Add ourselves to the ATU list of the type.
1126 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
1127
1128 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
1129 }
1130 }
Chris Lattner98fa07b2003-05-23 20:03:32 +00001131 return Result;
1132 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001133
Chris Lattner98fa07b2003-05-23 20:03:32 +00001134 void remove(ConstantClass *CP) {
Jim Laskeyc03caef2006-07-17 17:38:29 +00001135 typename MapTy::iterator I = FindExistingElement(CP);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001136 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +00001137 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001138
Chris Lattner935aa922005-10-04 17:48:46 +00001139 if (HasLargeKey) // Remember the reverse mapping if needed.
1140 InverseMap.erase(CP);
1141
Chris Lattnerb50d1352003-10-05 00:17:43 +00001142 // Now that we found the entry, make sure this isn't the entry that
1143 // the AbstractTypeMap points to.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001144 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001145 if (Ty->isAbstract()) {
1146 assert(AbstractTypeMap.count(Ty) &&
1147 "Abstract type not in AbstractTypeMap?");
Jim Laskeyc03caef2006-07-17 17:38:29 +00001148 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
Chris Lattnerb50d1352003-10-05 00:17:43 +00001149 if (ATMEntryIt == I) {
1150 // Yes, we are removing the representative entry for this type.
1151 // See if there are any other entries of the same type.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001152 typename MapTy::iterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001153
Chris Lattnerb50d1352003-10-05 00:17:43 +00001154 // First check the entry before this one...
1155 if (TmpIt != Map.begin()) {
1156 --TmpIt;
1157 if (TmpIt->first.first != Ty) // Not the same type, move back...
1158 ++TmpIt;
1159 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001160
Chris Lattnerb50d1352003-10-05 00:17:43 +00001161 // If we didn't find the same type, try to move forward...
1162 if (TmpIt == ATMEntryIt) {
1163 ++TmpIt;
1164 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
1165 --TmpIt; // No entry afterwards with the same type
1166 }
1167
1168 // If there is another entry in the map of the same abstract type,
1169 // update the AbstractTypeMap entry now.
1170 if (TmpIt != ATMEntryIt) {
1171 ATMEntryIt = TmpIt;
1172 } else {
1173 // Otherwise, we are removing the last instance of this type
1174 // from the table. Remove from the ATM, and from user list.
1175 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
1176 AbstractTypeMap.erase(Ty);
1177 }
Chris Lattner98fa07b2003-05-23 20:03:32 +00001178 }
Chris Lattnerb50d1352003-10-05 00:17:43 +00001179 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001180
Chris Lattnerb50d1352003-10-05 00:17:43 +00001181 Map.erase(I);
1182 }
1183
Chris Lattner3b793c62005-10-04 21:35:50 +00001184
1185 /// MoveConstantToNewSlot - If we are about to change C to be the element
1186 /// specified by I, update our internal data structures to reflect this
1187 /// fact.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001188 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
Chris Lattner3b793c62005-10-04 21:35:50 +00001189 // First, remove the old location of the specified constant in the map.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001190 typename MapTy::iterator OldI = FindExistingElement(C);
Chris Lattner3b793c62005-10-04 21:35:50 +00001191 assert(OldI != Map.end() && "Constant not found in constant table!");
1192 assert(OldI->second == C && "Didn't find correct element?");
1193
1194 // If this constant is the representative element for its abstract type,
1195 // update the AbstractTypeMap so that the representative element is I.
1196 if (C->getType()->isAbstract()) {
1197 typename AbstractTypeMapTy::iterator ATI =
1198 AbstractTypeMap.find(C->getType());
1199 assert(ATI != AbstractTypeMap.end() &&
1200 "Abstract type not in AbstractTypeMap?");
1201 if (ATI->second == OldI)
1202 ATI->second = I;
1203 }
1204
1205 // Remove the old entry from the map.
1206 Map.erase(OldI);
1207
1208 // Update the inverse map so that we know that this constant is now
1209 // located at descriptor I.
1210 if (HasLargeKey) {
1211 assert(I->second == C && "Bad inversemap entry!");
1212 InverseMap[C] = I;
1213 }
1214 }
1215
Chris Lattnerb50d1352003-10-05 00:17:43 +00001216 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +00001217 typename AbstractTypeMapTy::iterator I =
Jim Laskeyc03caef2006-07-17 17:38:29 +00001218 AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +00001219
1220 assert(I != AbstractTypeMap.end() &&
1221 "Abstract type not in AbstractTypeMap?");
1222
1223 // Convert a constant at a time until the last one is gone. The last one
1224 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
1225 // eliminated eventually.
1226 do {
1227 ConvertConstantType<ConstantClass,
Jim Laskeyc03caef2006-07-17 17:38:29 +00001228 TypeClass>::convert(
1229 static_cast<ConstantClass *>(I->second->second),
Chris Lattnerb50d1352003-10-05 00:17:43 +00001230 cast<TypeClass>(NewTy));
1231
Jim Laskeyc03caef2006-07-17 17:38:29 +00001232 I = AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +00001233 } while (I != AbstractTypeMap.end());
1234 }
1235
1236 // If the type became concrete without being refined to any other existing
1237 // type, we just remove ourselves from the ATU list.
1238 void typeBecameConcrete(const DerivedType *AbsTy) {
1239 AbsTy->removeAbstractTypeUser(this);
1240 }
1241
1242 void dump() const {
Bill Wendling6a462f12006-11-17 08:03:48 +00001243 DOUT << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +00001244 }
1245 };
1246}
1247
Chris Lattnera84df0a22006-09-28 23:36:21 +00001248
Chris Lattner28173502007-02-20 06:11:36 +00001249
Chris Lattner9fba3da2004-02-15 05:53:04 +00001250//---- ConstantAggregateZero::get() implementation...
1251//
1252namespace llvm {
1253 // ConstantAggregateZero does not take extra "value" argument...
1254 template<class ValType>
1255 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
1256 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
1257 return new ConstantAggregateZero(Ty);
1258 }
1259 };
1260
1261 template<>
1262 struct ConvertConstantType<ConstantAggregateZero, Type> {
1263 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
1264 // Make everyone now use a constant of the new type...
1265 Constant *New = ConstantAggregateZero::get(NewTy);
1266 assert(New != OldC && "Didn't replace constant??");
1267 OldC->uncheckedReplaceAllUsesWith(New);
1268 OldC->destroyConstant(); // This constant is now dead, destroy it.
1269 }
1270 };
1271}
1272
Chris Lattner69edc982006-09-28 00:35:06 +00001273static ManagedStatic<ValueMap<char, Type,
1274 ConstantAggregateZero> > AggZeroConstants;
Chris Lattner9fba3da2004-02-15 05:53:04 +00001275
Chris Lattner3e650af2004-08-04 04:48:01 +00001276static char getValType(ConstantAggregateZero *CPZ) { return 0; }
1277
Chris Lattner9fba3da2004-02-15 05:53:04 +00001278Constant *ConstantAggregateZero::get(const Type *Ty) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001279 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) &&
Chris Lattnerbfd0b6d2006-06-10 04:16:23 +00001280 "Cannot create an aggregate zero of non-aggregate type!");
Chris Lattner69edc982006-09-28 00:35:06 +00001281 return AggZeroConstants->getOrCreate(Ty, 0);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001282}
1283
1284// destroyConstant - Remove the constant from the constant table...
1285//
1286void ConstantAggregateZero::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001287 AggZeroConstants->remove(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001288 destroyConstantImpl();
1289}
1290
Chris Lattner3462ae32001-12-03 22:26:30 +00001291//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001292//
Chris Lattner189d19f2003-11-21 20:23:48 +00001293namespace llvm {
1294 template<>
1295 struct ConvertConstantType<ConstantArray, ArrayType> {
1296 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
1297 // Make everyone now use a constant of the new type...
1298 std::vector<Constant*> C;
1299 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1300 C.push_back(cast<Constant>(OldC->getOperand(i)));
1301 Constant *New = ConstantArray::get(NewTy, C);
1302 assert(New != OldC && "Didn't replace constant??");
1303 OldC->uncheckedReplaceAllUsesWith(New);
1304 OldC->destroyConstant(); // This constant is now dead, destroy it.
1305 }
1306 };
1307}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001308
Chris Lattner3e650af2004-08-04 04:48:01 +00001309static std::vector<Constant*> getValType(ConstantArray *CA) {
1310 std::vector<Constant*> Elements;
1311 Elements.reserve(CA->getNumOperands());
1312 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1313 Elements.push_back(cast<Constant>(CA->getOperand(i)));
1314 return Elements;
1315}
1316
Chris Lattnerb64419a2005-10-03 22:51:37 +00001317typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattner935aa922005-10-04 17:48:46 +00001318 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001319static ManagedStatic<ArrayConstantsTy> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001320
Chris Lattner015e8212004-02-15 04:14:47 +00001321Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +00001322 const std::vector<Constant*> &V) {
1323 // If this is an all-zero array, return a ConstantAggregateZero object
1324 if (!V.empty()) {
1325 Constant *C = V[0];
1326 if (!C->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001327 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001328 for (unsigned i = 1, e = V.size(); i != e; ++i)
1329 if (V[i] != C)
Chris Lattner69edc982006-09-28 00:35:06 +00001330 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001331 }
1332 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001333}
1334
Chris Lattner98fa07b2003-05-23 20:03:32 +00001335// destroyConstant - Remove the constant from the constant table...
1336//
1337void ConstantArray::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001338 ArrayConstants->remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001339 destroyConstantImpl();
1340}
1341
Reid Spencer6f614532006-05-30 08:23:18 +00001342/// ConstantArray::get(const string&) - Return an array that is initialized to
1343/// contain the specified string. If length is zero then a null terminator is
1344/// added to the specified string so that it may be used in a natural way.
1345/// Otherwise, the length parameter specifies how much of the string to use
1346/// and it won't be null terminated.
1347///
Reid Spencer82ebaba2006-05-30 18:15:07 +00001348Constant *ConstantArray::get(const std::string &Str, bool AddNull) {
Chris Lattner7f74a562002-01-20 22:54:45 +00001349 std::vector<Constant*> ElementVals;
Reid Spencer82ebaba2006-05-30 18:15:07 +00001350 for (unsigned i = 0; i < Str.length(); ++i)
Reid Spencer8d9336d2006-12-31 05:26:44 +00001351 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +00001352
1353 // Add a null terminator to the string...
Reid Spencer82ebaba2006-05-30 18:15:07 +00001354 if (AddNull) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001355 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, 0));
Reid Spencer6f614532006-05-30 08:23:18 +00001356 }
Chris Lattner8f80fe02001-10-14 23:54:12 +00001357
Reid Spencer8d9336d2006-12-31 05:26:44 +00001358 ArrayType *ATy = ArrayType::get(Type::Int8Ty, ElementVals.size());
Chris Lattner3462ae32001-12-03 22:26:30 +00001359 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +00001360}
1361
Reid Spencer2546b762007-01-26 07:37:34 +00001362/// isString - This method returns true if the array is an array of i8, and
1363/// if the elements of the array are all ConstantInt's.
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001364bool ConstantArray::isString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001365 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001366 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001367 return false;
1368 // Check the elements to make sure they are all integers, not constant
1369 // expressions.
1370 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1371 if (!isa<ConstantInt>(getOperand(i)))
1372 return false;
1373 return true;
1374}
1375
Evan Cheng3763c5b2006-10-26 19:15:05 +00001376/// isCString - This method returns true if the array is a string (see
1377/// isString) and it ends in a null byte \0 and does not contains any other
1378/// null bytes except its terminator.
1379bool ConstantArray::isCString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001380 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001381 if (getType()->getElementType() != Type::Int8Ty)
Evan Chenge974da62006-10-26 21:48:03 +00001382 return false;
1383 Constant *Zero = Constant::getNullValue(getOperand(0)->getType());
1384 // Last element must be a null.
1385 if (getOperand(getNumOperands()-1) != Zero)
1386 return false;
1387 // Other elements must be non-null integers.
1388 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1389 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +00001390 return false;
Evan Chenge974da62006-10-26 21:48:03 +00001391 if (getOperand(i) == Zero)
1392 return false;
1393 }
Evan Cheng3763c5b2006-10-26 19:15:05 +00001394 return true;
1395}
1396
1397
Reid Spencer2546b762007-01-26 07:37:34 +00001398// getAsString - If the sub-element type of this array is i8
Chris Lattner81fabb02002-08-26 17:53:56 +00001399// then this method converts the array to an std::string and returns it.
1400// Otherwise, it asserts out.
1401//
1402std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001403 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001404 std::string Result;
Owen Anderson79c69bc2008-06-24 21:58:29 +00001405 Result.reserve(getNumOperands());
Chris Lattner6077c312003-07-23 15:22:26 +00001406 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonee9c30d2008-06-25 01:05:05 +00001407 Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue());
Chris Lattner81fabb02002-08-26 17:53:56 +00001408 return Result;
1409}
1410
1411
Chris Lattner3462ae32001-12-03 22:26:30 +00001412//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001413//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001414
Chris Lattner189d19f2003-11-21 20:23:48 +00001415namespace llvm {
1416 template<>
1417 struct ConvertConstantType<ConstantStruct, StructType> {
1418 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1419 // Make everyone now use a constant of the new type...
1420 std::vector<Constant*> C;
1421 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1422 C.push_back(cast<Constant>(OldC->getOperand(i)));
1423 Constant *New = ConstantStruct::get(NewTy, C);
1424 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001425
Chris Lattner189d19f2003-11-21 20:23:48 +00001426 OldC->uncheckedReplaceAllUsesWith(New);
1427 OldC->destroyConstant(); // This constant is now dead, destroy it.
1428 }
1429 };
1430}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001431
Chris Lattner8760ec72005-10-04 01:17:50 +00001432typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattner935aa922005-10-04 17:48:46 +00001433 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001434static ManagedStatic<StructConstantsTy> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001435
Chris Lattner3e650af2004-08-04 04:48:01 +00001436static std::vector<Constant*> getValType(ConstantStruct *CS) {
1437 std::vector<Constant*> Elements;
1438 Elements.reserve(CS->getNumOperands());
1439 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1440 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1441 return Elements;
1442}
1443
Chris Lattner015e8212004-02-15 04:14:47 +00001444Constant *ConstantStruct::get(const StructType *Ty,
1445 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001446 // Create a ConstantAggregateZero value if all elements are zeros...
1447 for (unsigned i = 0, e = V.size(); i != e; ++i)
1448 if (!V[i]->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001449 return StructConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001450
1451 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001452}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001453
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001454Constant *ConstantStruct::get(const std::vector<Constant*> &V, bool packed) {
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001455 std::vector<const Type*> StructEls;
1456 StructEls.reserve(V.size());
1457 for (unsigned i = 0, e = V.size(); i != e; ++i)
1458 StructEls.push_back(V[i]->getType());
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001459 return get(StructType::get(StructEls, packed), V);
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001460}
1461
Chris Lattnerd7a73302001-10-13 06:57:33 +00001462// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001463//
Chris Lattner3462ae32001-12-03 22:26:30 +00001464void ConstantStruct::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001465 StructConstants->remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001466 destroyConstantImpl();
1467}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001468
Reid Spencerd84d35b2007-02-15 02:26:10 +00001469//---- ConstantVector::get() implementation...
Brian Gaeke02209042004-08-20 06:00:58 +00001470//
1471namespace llvm {
1472 template<>
Reid Spencerd84d35b2007-02-15 02:26:10 +00001473 struct ConvertConstantType<ConstantVector, VectorType> {
1474 static void convert(ConstantVector *OldC, const VectorType *NewTy) {
Brian Gaeke02209042004-08-20 06:00:58 +00001475 // Make everyone now use a constant of the new type...
1476 std::vector<Constant*> C;
1477 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1478 C.push_back(cast<Constant>(OldC->getOperand(i)));
Reid Spencerd84d35b2007-02-15 02:26:10 +00001479 Constant *New = ConstantVector::get(NewTy, C);
Brian Gaeke02209042004-08-20 06:00:58 +00001480 assert(New != OldC && "Didn't replace constant??");
1481 OldC->uncheckedReplaceAllUsesWith(New);
1482 OldC->destroyConstant(); // This constant is now dead, destroy it.
1483 }
1484 };
1485}
1486
Reid Spencerd84d35b2007-02-15 02:26:10 +00001487static std::vector<Constant*> getValType(ConstantVector *CP) {
Brian Gaeke02209042004-08-20 06:00:58 +00001488 std::vector<Constant*> Elements;
1489 Elements.reserve(CP->getNumOperands());
1490 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1491 Elements.push_back(CP->getOperand(i));
1492 return Elements;
1493}
1494
Reid Spencerd84d35b2007-02-15 02:26:10 +00001495static ManagedStatic<ValueMap<std::vector<Constant*>, VectorType,
Reid Spencer09575ba2007-02-15 03:39:18 +00001496 ConstantVector> > VectorConstants;
Brian Gaeke02209042004-08-20 06:00:58 +00001497
Reid Spencerd84d35b2007-02-15 02:26:10 +00001498Constant *ConstantVector::get(const VectorType *Ty,
Brian Gaeke02209042004-08-20 06:00:58 +00001499 const std::vector<Constant*> &V) {
Chris Lattnerd977c072008-07-10 00:44:03 +00001500 assert(!V.empty() && "Vectors can't be empty");
1501 // If this is an all-undef or alll-zero vector, return a
1502 // ConstantAggregateZero or UndefValue.
1503 Constant *C = V[0];
1504 bool isZero = C->isNullValue();
1505 bool isUndef = isa<UndefValue>(C);
1506
1507 if (isZero || isUndef) {
Brian Gaeke02209042004-08-20 06:00:58 +00001508 for (unsigned i = 1, e = V.size(); i != e; ++i)
Chris Lattnerd977c072008-07-10 00:44:03 +00001509 if (V[i] != C) {
1510 isZero = isUndef = false;
1511 break;
1512 }
Brian Gaeke02209042004-08-20 06:00:58 +00001513 }
Chris Lattnerd977c072008-07-10 00:44:03 +00001514
1515 if (isZero)
1516 return ConstantAggregateZero::get(Ty);
1517 if (isUndef)
1518 return UndefValue::get(Ty);
1519 return VectorConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001520}
1521
Reid Spencerd84d35b2007-02-15 02:26:10 +00001522Constant *ConstantVector::get(const std::vector<Constant*> &V) {
Brian Gaeke02209042004-08-20 06:00:58 +00001523 assert(!V.empty() && "Cannot infer type if V is empty");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001524 return get(VectorType::get(V.front()->getType(),V.size()), V);
Brian Gaeke02209042004-08-20 06:00:58 +00001525}
1526
1527// destroyConstant - Remove the constant from the constant table...
1528//
Reid Spencerd84d35b2007-02-15 02:26:10 +00001529void ConstantVector::destroyConstant() {
Reid Spencer09575ba2007-02-15 03:39:18 +00001530 VectorConstants->remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001531 destroyConstantImpl();
1532}
1533
Dan Gohman30978072007-05-24 14:36:04 +00001534/// This function will return true iff every element in this vector constant
Jim Laskeyf0478822007-01-12 22:39:14 +00001535/// is set to all ones.
1536/// @returns true iff this constant's emements are all set to all ones.
1537/// @brief Determine if the value is all ones.
Reid Spencerd84d35b2007-02-15 02:26:10 +00001538bool ConstantVector::isAllOnesValue() const {
Jim Laskeyf0478822007-01-12 22:39:14 +00001539 // Check out first element.
1540 const Constant *Elt = getOperand(0);
1541 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1542 if (!CI || !CI->isAllOnesValue()) return false;
1543 // Then make sure all remaining elements point to the same value.
1544 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1545 if (getOperand(I) != Elt) return false;
1546 }
1547 return true;
1548}
1549
Dan Gohman07159202007-10-17 17:51:30 +00001550/// getSplatValue - If this is a splat constant, where all of the
1551/// elements have the same value, return that value. Otherwise return null.
1552Constant *ConstantVector::getSplatValue() {
1553 // Check out first element.
1554 Constant *Elt = getOperand(0);
1555 // Then make sure all remaining elements point to the same value.
1556 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
1557 if (getOperand(I) != Elt) return 0;
1558 return Elt;
1559}
1560
Chris Lattner3462ae32001-12-03 22:26:30 +00001561//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001562//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001563
Chris Lattner189d19f2003-11-21 20:23:48 +00001564namespace llvm {
1565 // ConstantPointerNull does not take extra "value" argument...
1566 template<class ValType>
1567 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1568 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1569 return new ConstantPointerNull(Ty);
1570 }
1571 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001572
Chris Lattner189d19f2003-11-21 20:23:48 +00001573 template<>
1574 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1575 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1576 // Make everyone now use a constant of the new type...
1577 Constant *New = ConstantPointerNull::get(NewTy);
1578 assert(New != OldC && "Didn't replace constant??");
1579 OldC->uncheckedReplaceAllUsesWith(New);
1580 OldC->destroyConstant(); // This constant is now dead, destroy it.
1581 }
1582 };
1583}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001584
Chris Lattner69edc982006-09-28 00:35:06 +00001585static ManagedStatic<ValueMap<char, PointerType,
1586 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001587
Chris Lattner3e650af2004-08-04 04:48:01 +00001588static char getValType(ConstantPointerNull *) {
1589 return 0;
1590}
1591
1592
Chris Lattner3462ae32001-12-03 22:26:30 +00001593ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001594 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001595}
1596
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001597// destroyConstant - Remove the constant from the constant table...
1598//
1599void ConstantPointerNull::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001600 NullPtrConstants->remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001601 destroyConstantImpl();
1602}
1603
1604
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001605//---- UndefValue::get() implementation...
1606//
1607
1608namespace llvm {
1609 // UndefValue does not take extra "value" argument...
1610 template<class ValType>
1611 struct ConstantCreator<UndefValue, Type, ValType> {
1612 static UndefValue *create(const Type *Ty, const ValType &V) {
1613 return new UndefValue(Ty);
1614 }
1615 };
1616
1617 template<>
1618 struct ConvertConstantType<UndefValue, Type> {
1619 static void convert(UndefValue *OldC, const Type *NewTy) {
1620 // Make everyone now use a constant of the new type.
1621 Constant *New = UndefValue::get(NewTy);
1622 assert(New != OldC && "Didn't replace constant??");
1623 OldC->uncheckedReplaceAllUsesWith(New);
1624 OldC->destroyConstant(); // This constant is now dead, destroy it.
1625 }
1626 };
1627}
1628
Chris Lattner69edc982006-09-28 00:35:06 +00001629static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001630
1631static char getValType(UndefValue *) {
1632 return 0;
1633}
1634
1635
1636UndefValue *UndefValue::get(const Type *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001637 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001638}
1639
1640// destroyConstant - Remove the constant from the constant table.
1641//
1642void UndefValue::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001643 UndefValueConstants->remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001644 destroyConstantImpl();
1645}
1646
1647
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001648//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001649//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001650
Dan Gohmand78c4002008-05-13 00:00:25 +00001651namespace {
1652
Reid Spenceree3c9912006-12-04 05:19:50 +00001653struct ExprMapKeyType {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001654 typedef SmallVector<unsigned, 4> IndexList;
1655
1656 ExprMapKeyType(unsigned opc,
1657 const std::vector<Constant*> &ops,
1658 unsigned short pred = 0,
1659 const IndexList &inds = IndexList())
1660 : opcode(opc), predicate(pred), operands(ops), indices(inds) {}
Reid Spencerdba6aa42006-12-04 18:38:05 +00001661 uint16_t opcode;
1662 uint16_t predicate;
Reid Spenceree3c9912006-12-04 05:19:50 +00001663 std::vector<Constant*> operands;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001664 IndexList indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001665 bool operator==(const ExprMapKeyType& that) const {
1666 return this->opcode == that.opcode &&
1667 this->predicate == that.predicate &&
1668 this->operands == that.operands;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001669 this->indices == that.indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001670 }
1671 bool operator<(const ExprMapKeyType & that) const {
1672 return this->opcode < that.opcode ||
1673 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1674 (this->opcode == that.opcode && this->predicate == that.predicate &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001675 this->operands < that.operands) ||
1676 (this->opcode == that.opcode && this->predicate == that.predicate &&
1677 this->operands == that.operands && this->indices < that.indices);
Reid Spenceree3c9912006-12-04 05:19:50 +00001678 }
1679
1680 bool operator!=(const ExprMapKeyType& that) const {
1681 return !(*this == that);
1682 }
1683};
Chris Lattner98fa07b2003-05-23 20:03:32 +00001684
Dan Gohmand78c4002008-05-13 00:00:25 +00001685}
1686
Chris Lattner189d19f2003-11-21 20:23:48 +00001687namespace llvm {
1688 template<>
1689 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001690 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1691 unsigned short pred = 0) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001692 if (Instruction::isCast(V.opcode))
1693 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1694 if ((V.opcode >= Instruction::BinaryOpsBegin &&
Reid Spencer2341c222007-02-02 02:16:23 +00001695 V.opcode < Instruction::BinaryOpsEnd))
Reid Spenceree3c9912006-12-04 05:19:50 +00001696 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1697 if (V.opcode == Instruction::Select)
1698 return new SelectConstantExpr(V.operands[0], V.operands[1],
1699 V.operands[2]);
1700 if (V.opcode == Instruction::ExtractElement)
1701 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1702 if (V.opcode == Instruction::InsertElement)
1703 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1704 V.operands[2]);
1705 if (V.opcode == Instruction::ShuffleVector)
1706 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1707 V.operands[2]);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001708 if (V.opcode == Instruction::InsertValue)
1709 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
1710 V.indices, Ty);
1711 if (V.opcode == Instruction::ExtractValue)
1712 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001713 if (V.opcode == Instruction::GetElementPtr) {
1714 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
Gabor Greife9ecc682008-04-06 20:25:17 +00001715 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001716 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001717
Reid Spenceree3c9912006-12-04 05:19:50 +00001718 // The compare instructions are weird. We have to encode the predicate
1719 // value and it is combined with the instruction opcode by multiplying
1720 // the opcode by one hundred. We must decode this to get the predicate.
1721 if (V.opcode == Instruction::ICmp)
Nate Begemand2195702008-05-12 19:01:56 +00001722 return new CompareConstantExpr(Ty, Instruction::ICmp, V.predicate,
Reid Spenceree3c9912006-12-04 05:19:50 +00001723 V.operands[0], V.operands[1]);
1724 if (V.opcode == Instruction::FCmp)
Nate Begemand2195702008-05-12 19:01:56 +00001725 return new CompareConstantExpr(Ty, Instruction::FCmp, V.predicate,
1726 V.operands[0], V.operands[1]);
1727 if (V.opcode == Instruction::VICmp)
1728 return new CompareConstantExpr(Ty, Instruction::VICmp, V.predicate,
1729 V.operands[0], V.operands[1]);
1730 if (V.opcode == Instruction::VFCmp)
1731 return new CompareConstantExpr(Ty, Instruction::VFCmp, V.predicate,
Reid Spenceree3c9912006-12-04 05:19:50 +00001732 V.operands[0], V.operands[1]);
1733 assert(0 && "Invalid ConstantExpr!");
Jeff Cohen9f469632006-12-15 21:47:01 +00001734 return 0;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001735 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001736 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001737
Chris Lattner189d19f2003-11-21 20:23:48 +00001738 template<>
1739 struct ConvertConstantType<ConstantExpr, Type> {
1740 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1741 Constant *New;
1742 switch (OldC->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001743 case Instruction::Trunc:
1744 case Instruction::ZExt:
1745 case Instruction::SExt:
1746 case Instruction::FPTrunc:
1747 case Instruction::FPExt:
1748 case Instruction::UIToFP:
1749 case Instruction::SIToFP:
1750 case Instruction::FPToUI:
1751 case Instruction::FPToSI:
1752 case Instruction::PtrToInt:
1753 case Instruction::IntToPtr:
1754 case Instruction::BitCast:
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001755 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
1756 NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001757 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001758 case Instruction::Select:
1759 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1760 OldC->getOperand(1),
1761 OldC->getOperand(2));
1762 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001763 default:
1764 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001765 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner189d19f2003-11-21 20:23:48 +00001766 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1767 OldC->getOperand(1));
1768 break;
1769 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001770 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001771 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
Chris Lattner302116a2007-01-31 04:40:28 +00001772 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
1773 &Idx[0], Idx.size());
Chris Lattner189d19f2003-11-21 20:23:48 +00001774 break;
1775 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001776
Chris Lattner189d19f2003-11-21 20:23:48 +00001777 assert(New != OldC && "Didn't replace constant??");
1778 OldC->uncheckedReplaceAllUsesWith(New);
1779 OldC->destroyConstant(); // This constant is now dead, destroy it.
1780 }
1781 };
1782} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001783
1784
Chris Lattner3e650af2004-08-04 04:48:01 +00001785static ExprMapKeyType getValType(ConstantExpr *CE) {
1786 std::vector<Constant*> Operands;
1787 Operands.reserve(CE->getNumOperands());
1788 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1789 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spenceree3c9912006-12-04 05:19:50 +00001790 return ExprMapKeyType(CE->getOpcode(), Operands,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001791 CE->isCompare() ? CE->getPredicate() : 0,
1792 CE->hasIndices() ?
1793 CE->getIndices() : SmallVector<unsigned, 4>());
Chris Lattner3e650af2004-08-04 04:48:01 +00001794}
1795
Chris Lattner69edc982006-09-28 00:35:06 +00001796static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1797 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001798
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001799/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands7d6c8ae2008-03-30 19:38:55 +00001800/// cast in the ExprConstants map. It is used by the various get* methods below.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001801static inline Constant *getFoldedCast(
1802 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001803 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001804 // Fold a few common cases
1805 if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
1806 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001807
Vikram S. Adve4c485332002-07-15 18:19:33 +00001808 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001809 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00001810 ExprMapKeyType Key(opc, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001811 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001812}
Reid Spencerf37dc652006-12-05 19:14:13 +00001813
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001814Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
1815 Instruction::CastOps opc = Instruction::CastOps(oc);
1816 assert(Instruction::isCast(opc) && "opcode out of range");
1817 assert(C && Ty && "Null arguments to getCast");
1818 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1819
1820 switch (opc) {
1821 default:
1822 assert(0 && "Invalid cast opcode");
1823 break;
1824 case Instruction::Trunc: return getTrunc(C, Ty);
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001825 case Instruction::ZExt: return getZExt(C, Ty);
1826 case Instruction::SExt: return getSExt(C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001827 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1828 case Instruction::FPExt: return getFPExtend(C, Ty);
1829 case Instruction::UIToFP: return getUIToFP(C, Ty);
1830 case Instruction::SIToFP: return getSIToFP(C, Ty);
1831 case Instruction::FPToUI: return getFPToUI(C, Ty);
1832 case Instruction::FPToSI: return getFPToSI(C, Ty);
1833 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1834 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1835 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001836 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001837 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00001838}
1839
Reid Spencer5c140882006-12-04 20:17:56 +00001840Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
1841 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1842 return getCast(Instruction::BitCast, C, Ty);
1843 return getCast(Instruction::ZExt, C, Ty);
1844}
1845
1846Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
1847 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1848 return getCast(Instruction::BitCast, C, Ty);
1849 return getCast(Instruction::SExt, C, Ty);
1850}
1851
1852Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
1853 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1854 return getCast(Instruction::BitCast, C, Ty);
1855 return getCast(Instruction::Trunc, C, Ty);
1856}
1857
Reid Spencerbc245a02006-12-05 03:25:26 +00001858Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
1859 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001860 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001861
Chris Lattner03c49532007-01-15 02:27:26 +00001862 if (Ty->isInteger())
Reid Spencerbc245a02006-12-05 03:25:26 +00001863 return getCast(Instruction::PtrToInt, S, Ty);
1864 return getCast(Instruction::BitCast, S, Ty);
1865}
1866
Reid Spencer56521c42006-12-12 00:51:07 +00001867Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1868 bool isSigned) {
Chris Lattner03c49532007-01-15 02:27:26 +00001869 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer56521c42006-12-12 00:51:07 +00001870 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1871 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1872 Instruction::CastOps opcode =
1873 (SrcBits == DstBits ? Instruction::BitCast :
1874 (SrcBits > DstBits ? Instruction::Trunc :
1875 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1876 return getCast(opcode, C, Ty);
1877}
1878
1879Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
1880 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1881 "Invalid cast");
1882 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1883 unsigned DstBits = Ty->getPrimitiveSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001884 if (SrcBits == DstBits)
1885 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001886 Instruction::CastOps opcode =
Reid Spencerca104e82006-12-12 05:38:50 +00001887 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001888 return getCast(opcode, C, Ty);
1889}
1890
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001891Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001892 assert(C->getType()->isInteger() && "Trunc operand must be integer");
1893 assert(Ty->isInteger() && "Trunc produces only integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001894 assert(C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&&
1895 "SrcTy must be larger than DestTy for Trunc!");
1896
1897 return getFoldedCast(Instruction::Trunc, C, Ty);
1898}
1899
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001900Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001901 assert(C->getType()->isInteger() && "SEXt operand must be integral");
1902 assert(Ty->isInteger() && "SExt produces only integer");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001903 assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1904 "SrcTy must be smaller than DestTy for SExt!");
1905
1906 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerdd284742004-04-04 23:20:30 +00001907}
1908
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001909Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001910 assert(C->getType()->isInteger() && "ZEXt operand must be integral");
1911 assert(Ty->isInteger() && "ZExt produces only integer");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001912 assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1913 "SrcTy must be smaller than DestTy for ZExt!");
1914
1915 return getFoldedCast(Instruction::ZExt, C, Ty);
1916}
1917
1918Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
1919 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1920 C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&&
1921 "This is an illegal floating point truncation!");
1922 return getFoldedCast(Instruction::FPTrunc, C, Ty);
1923}
1924
1925Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
1926 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1927 C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1928 "This is an illegal floating point extension!");
1929 return getFoldedCast(Instruction::FPExt, C, Ty);
1930}
1931
1932Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Nate Begemand4d45c22007-11-17 03:58:34 +00001933 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1934 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1935 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1936 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
1937 "This is an illegal uint to floating point cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001938 return getFoldedCast(Instruction::UIToFP, C, Ty);
1939}
1940
1941Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Nate Begemand4d45c22007-11-17 03:58:34 +00001942 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1943 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1944 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1945 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001946 "This is an illegal sint to floating point cast!");
1947 return getFoldedCast(Instruction::SIToFP, C, Ty);
1948}
1949
1950Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Nate Begemand4d45c22007-11-17 03:58:34 +00001951 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1952 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1953 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1954 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1955 "This is an illegal floating point to uint cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001956 return getFoldedCast(Instruction::FPToUI, C, Ty);
1957}
1958
1959Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Nate Begemand4d45c22007-11-17 03:58:34 +00001960 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1961 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1962 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1963 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1964 "This is an illegal floating point to sint cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001965 return getFoldedCast(Instruction::FPToSI, C, Ty);
1966}
1967
1968Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
1969 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner03c49532007-01-15 02:27:26 +00001970 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001971 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
1972}
1973
1974Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner03c49532007-01-15 02:27:26 +00001975 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001976 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
1977 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
1978}
1979
1980Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
1981 // BitCast implies a no-op cast of type only. No bits change. However, you
1982 // can't cast pointers to anything but pointers.
1983 const Type *SrcTy = C->getType();
1984 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer5c140882006-12-04 20:17:56 +00001985 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001986
1987 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
1988 // or nonptr->ptr). For all the other types, the cast is okay if source and
1989 // destination bit widths are identical.
1990 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1991 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Reid Spencer5c140882006-12-04 20:17:56 +00001992 assert(SrcBitSize == DstBitSize && "BitCast requies types of same width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001993 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerdd284742004-04-04 23:20:30 +00001994}
1995
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001996Constant *ConstantExpr::getSizeOf(const Type *Ty) {
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001997 // sizeof is implemented as: (i64) gep (Ty*)null, 1
Chris Lattnerb5d70302007-02-19 20:01:23 +00001998 Constant *GEPIdx = ConstantInt::get(Type::Int32Ty, 1);
1999 Constant *GEP =
Christopher Lambedf07882007-12-17 01:12:55 +00002000 getGetElementPtr(getNullValue(PointerType::getUnqual(Ty)), &GEPIdx, 1);
Chris Lattnerb5d70302007-02-19 20:01:23 +00002001 return getCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
Alkis Evlogimenos9160d5f2005-03-19 11:40:31 +00002002}
2003
Chris Lattnerb50d1352003-10-05 00:17:43 +00002004Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Reid Spencera009d0d2006-12-04 21:35:24 +00002005 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00002006 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00002007 assert(Opcode >= Instruction::BinaryOpsBegin &&
2008 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00002009 "Invalid opcode in binary constant expression");
2010 assert(C1->getType() == C2->getType() &&
2011 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00002012
Reid Spencer542964f2007-01-11 18:21:29 +00002013 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Chris Lattnerb50d1352003-10-05 00:17:43 +00002014 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
2015 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00002016
Chris Lattner2b383d2e2003-05-13 21:37:02 +00002017 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00002018 ExprMapKeyType Key(Opcode, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002019 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002020}
2021
Reid Spencer266e42b2006-12-23 06:05:41 +00002022Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Reid Spencera009d0d2006-12-04 21:35:24 +00002023 Constant *C1, Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002024 switch (predicate) {
2025 default: assert(0 && "Invalid CmpInst predicate");
2026 case FCmpInst::FCMP_FALSE: case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_OGT:
2027 case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OLE:
2028 case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_ORD: case FCmpInst::FCMP_UNO:
2029 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UGT: case FCmpInst::FCMP_UGE:
2030 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_ULE: case FCmpInst::FCMP_UNE:
2031 case FCmpInst::FCMP_TRUE:
2032 return getFCmp(predicate, C1, C2);
2033 case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_UGT:
2034 case ICmpInst::ICMP_UGE: case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE:
2035 case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_SGE: case ICmpInst::ICMP_SLT:
2036 case ICmpInst::ICMP_SLE:
2037 return getICmp(predicate, C1, C2);
2038 }
Reid Spencera009d0d2006-12-04 21:35:24 +00002039}
2040
2041Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002042#ifndef NDEBUG
2043 switch (Opcode) {
Reid Spencer7eb55b32006-11-02 01:53:59 +00002044 case Instruction::Add:
2045 case Instruction::Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002046 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002047 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattner03c49532007-01-15 02:27:26 +00002048 assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00002049 isa<VectorType>(C1->getType())) &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002050 "Tried to create an arithmetic operation on a non-arithmetic type!");
2051 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002052 case Instruction::UDiv:
2053 case Instruction::SDiv:
2054 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002055 assert((C1->getType()->isInteger() || (isa<VectorType>(C1->getType()) &&
2056 cast<VectorType>(C1->getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002057 "Tried to create an arithmetic operation on a non-arithmetic type!");
2058 break;
2059 case Instruction::FDiv:
2060 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002061 assert((C1->getType()->isFloatingPoint() || (isa<VectorType>(C1->getType())
2062 && cast<VectorType>(C1->getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002063 && "Tried to create an arithmetic operation on a non-arithmetic type!");
2064 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00002065 case Instruction::URem:
2066 case Instruction::SRem:
2067 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002068 assert((C1->getType()->isInteger() || (isa<VectorType>(C1->getType()) &&
2069 cast<VectorType>(C1->getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002070 "Tried to create an arithmetic operation on a non-arithmetic type!");
2071 break;
2072 case Instruction::FRem:
2073 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002074 assert((C1->getType()->isFloatingPoint() || (isa<VectorType>(C1->getType())
2075 && cast<VectorType>(C1->getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00002076 && "Tried to create an arithmetic operation on a non-arithmetic type!");
2077 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002078 case Instruction::And:
2079 case Instruction::Or:
2080 case Instruction::Xor:
2081 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002082 assert((C1->getType()->isInteger() || isa<VectorType>(C1->getType())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00002083 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002084 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002085 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00002086 case Instruction::LShr:
2087 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00002088 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattner03c49532007-01-15 02:27:26 +00002089 assert(C1->getType()->isInteger() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002090 "Tried to create a shift operation on a non-integer type!");
2091 break;
2092 default:
2093 break;
2094 }
2095#endif
2096
Reid Spencera009d0d2006-12-04 21:35:24 +00002097 return getTy(C1->getType(), Opcode, C1, C2);
2098}
2099
Reid Spencer266e42b2006-12-23 06:05:41 +00002100Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00002101 Constant *C1, Constant *C2) {
2102 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002103 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00002104}
2105
Chris Lattner6e415c02004-03-12 05:54:04 +00002106Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
2107 Constant *V1, Constant *V2) {
Reid Spencer2546b762007-01-26 07:37:34 +00002108 assert(C->getType() == Type::Int1Ty && "Select condition must be i1!");
Chris Lattner6e415c02004-03-12 05:54:04 +00002109 assert(V1->getType() == V2->getType() && "Select value types must match!");
2110 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
2111
2112 if (ReqTy == V1->getType())
2113 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
2114 return SC; // Fold common cases
2115
2116 std::vector<Constant*> argVec(3, C);
2117 argVec[1] = V1;
2118 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00002119 ExprMapKeyType Key(Instruction::Select, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002120 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00002121}
2122
Chris Lattnerb50d1352003-10-05 00:17:43 +00002123Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00002124 Value* const *Idxs,
2125 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002126 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
2127 Idxs+NumIdx) ==
2128 cast<PointerType>(ReqTy)->getElementType() &&
2129 "GEP indices invalid!");
Chris Lattner04b60fe2004-02-16 20:46:13 +00002130
Chris Lattner302116a2007-01-31 04:40:28 +00002131 if (Constant *FC = ConstantFoldGetElementPtr(C, (Constant**)Idxs, NumIdx))
Chris Lattneracdbe712003-04-17 19:24:48 +00002132 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00002133
Chris Lattnerb50d1352003-10-05 00:17:43 +00002134 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00002135 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00002136 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00002137 std::vector<Constant*> ArgVec;
Chris Lattner302116a2007-01-31 04:40:28 +00002138 ArgVec.reserve(NumIdx+1);
Chris Lattner13128ab2004-10-11 22:52:25 +00002139 ArgVec.push_back(C);
Chris Lattner302116a2007-01-31 04:40:28 +00002140 for (unsigned i = 0; i != NumIdx; ++i)
2141 ArgVec.push_back(cast<Constant>(Idxs[i]));
2142 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002143 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002144}
2145
Chris Lattner302116a2007-01-31 04:40:28 +00002146Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
2147 unsigned NumIdx) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00002148 // Get the result type of the getelementptr!
Chris Lattner302116a2007-01-31 04:40:28 +00002149 const Type *Ty =
Dan Gohman12fce772008-05-15 19:50:34 +00002150 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00002151 assert(Ty && "GEP indices invalid!");
Christopher Lamb54dd24c2007-12-11 08:59:05 +00002152 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
2153 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
Chris Lattner13128ab2004-10-11 22:52:25 +00002154}
2155
Chris Lattner302116a2007-01-31 04:40:28 +00002156Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
2157 unsigned NumIdx) {
2158 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00002159}
2160
Chris Lattner302116a2007-01-31 04:40:28 +00002161
Reid Spenceree3c9912006-12-04 05:19:50 +00002162Constant *
2163ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2164 assert(LHS->getType() == RHS->getType());
2165 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
2166 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
2167
Reid Spencer266e42b2006-12-23 06:05:41 +00002168 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002169 return FC; // Fold a few common cases...
2170
2171 // Look up the constant in the table first to ensure uniqueness
2172 std::vector<Constant*> ArgVec;
2173 ArgVec.push_back(LHS);
2174 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00002175 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00002176 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Reid Spencer542964f2007-01-11 18:21:29 +00002177 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002178}
2179
2180Constant *
2181ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2182 assert(LHS->getType() == RHS->getType());
2183 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
2184
Reid Spencer266e42b2006-12-23 06:05:41 +00002185 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002186 return FC; // Fold a few common cases...
2187
2188 // Look up the constant in the table first to ensure uniqueness
2189 std::vector<Constant*> ArgVec;
2190 ArgVec.push_back(LHS);
2191 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00002192 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00002193 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Reid Spencer542964f2007-01-11 18:21:29 +00002194 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002195}
2196
Nate Begemand2195702008-05-12 19:01:56 +00002197Constant *
2198ConstantExpr::getVICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2199 assert(isa<VectorType>(LHS->getType()) &&
2200 "Tried to create vicmp operation on non-vector type!");
2201 assert(LHS->getType() == RHS->getType());
2202 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
2203 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid VICmp Predicate");
2204
Nate Begemanac7f3d92008-05-12 19:23:22 +00002205 const VectorType *VTy = cast<VectorType>(LHS->getType());
Nate Begemand2195702008-05-12 19:01:56 +00002206 const Type *EltTy = VTy->getElementType();
2207 unsigned NumElts = VTy->getNumElements();
2208
2209 SmallVector<Constant *, 8> Elts;
2210 for (unsigned i = 0; i != NumElts; ++i) {
2211 Constant *FC = ConstantFoldCompareInstruction(pred, LHS->getOperand(i),
2212 RHS->getOperand(i));
2213 if (FC) {
2214 uint64_t Val = cast<ConstantInt>(FC)->getZExtValue();
2215 if (Val != 0ULL)
2216 Elts.push_back(ConstantInt::getAllOnesValue(EltTy));
2217 else
2218 Elts.push_back(ConstantInt::get(EltTy, 0ULL));
2219 }
2220 }
2221 if (Elts.size() == NumElts)
2222 return ConstantVector::get(&Elts[0], Elts.size());
2223
2224 // Look up the constant in the table first to ensure uniqueness
2225 std::vector<Constant*> ArgVec;
2226 ArgVec.push_back(LHS);
2227 ArgVec.push_back(RHS);
2228 // Get the key type with both the opcode and predicate
2229 const ExprMapKeyType Key(Instruction::VICmp, ArgVec, pred);
2230 return ExprConstants->getOrCreate(LHS->getType(), Key);
2231}
2232
2233Constant *
2234ConstantExpr::getVFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2235 assert(isa<VectorType>(LHS->getType()) &&
2236 "Tried to create vfcmp operation on non-vector type!");
2237 assert(LHS->getType() == RHS->getType());
2238 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid VFCmp Predicate");
2239
2240 const VectorType *VTy = cast<VectorType>(LHS->getType());
2241 unsigned NumElts = VTy->getNumElements();
2242 const Type *EltTy = VTy->getElementType();
2243 const Type *REltTy = IntegerType::get(EltTy->getPrimitiveSizeInBits());
2244 const Type *ResultTy = VectorType::get(REltTy, NumElts);
2245
2246 SmallVector<Constant *, 8> Elts;
2247 for (unsigned i = 0; i != NumElts; ++i) {
2248 Constant *FC = ConstantFoldCompareInstruction(pred, LHS->getOperand(i),
2249 RHS->getOperand(i));
2250 if (FC) {
2251 uint64_t Val = cast<ConstantInt>(FC)->getZExtValue();
2252 if (Val != 0ULL)
2253 Elts.push_back(ConstantInt::getAllOnesValue(REltTy));
2254 else
2255 Elts.push_back(ConstantInt::get(REltTy, 0ULL));
2256 }
2257 }
2258 if (Elts.size() == NumElts)
2259 return ConstantVector::get(&Elts[0], Elts.size());
2260
2261 // Look up the constant in the table first to ensure uniqueness
2262 std::vector<Constant*> ArgVec;
2263 ArgVec.push_back(LHS);
2264 ArgVec.push_back(RHS);
2265 // Get the key type with both the opcode and predicate
2266 const ExprMapKeyType Key(Instruction::VFCmp, ArgVec, pred);
2267 return ExprConstants->getOrCreate(ResultTy, Key);
2268}
2269
Robert Bocchino23004482006-01-10 19:05:34 +00002270Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
2271 Constant *Idx) {
Robert Bocchinode7f1c92006-01-10 20:03:46 +00002272 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
2273 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00002274 // Look up the constant in the table first to ensure uniqueness
2275 std::vector<Constant*> ArgVec(1, Val);
2276 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00002277 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002278 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00002279}
2280
2281Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002282 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002283 "Tried to create extractelement operation on non-vector type!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00002284 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00002285 "Extractelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002286 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +00002287 Val, Idx);
2288}
Chris Lattnerb50d1352003-10-05 00:17:43 +00002289
Robert Bocchinoca27f032006-01-17 20:07:22 +00002290Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
2291 Constant *Elt, Constant *Idx) {
2292 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
2293 return FC; // Fold a few common cases...
2294 // Look up the constant in the table first to ensure uniqueness
2295 std::vector<Constant*> ArgVec(1, Val);
2296 ArgVec.push_back(Elt);
2297 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00002298 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002299 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00002300}
2301
2302Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
2303 Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002304 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002305 "Tried to create insertelement operation on non-vector type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002306 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoca27f032006-01-17 20:07:22 +00002307 && "Insertelement types must match!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00002308 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00002309 "Insertelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002310 return getInsertElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchinoca27f032006-01-17 20:07:22 +00002311 Val, Elt, Idx);
2312}
2313
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002314Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
2315 Constant *V2, Constant *Mask) {
2316 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
2317 return FC; // Fold a few common cases...
2318 // Look up the constant in the table first to ensure uniqueness
2319 std::vector<Constant*> ArgVec(1, V1);
2320 ArgVec.push_back(V2);
2321 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00002322 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002323 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002324}
2325
2326Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
2327 Constant *Mask) {
2328 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
2329 "Invalid shuffle vector constant expr operands!");
2330 return getShuffleVectorTy(V1->getType(), V1, V2, Mask);
2331}
2332
Dan Gohman12fce772008-05-15 19:50:34 +00002333Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg,
2334 Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002335 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002336 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2337 Idxs+NumIdx) == Val->getType() &&
2338 "insertvalue indices invalid!");
2339 assert(Agg->getType() == ReqTy &&
2340 "insertvalue type invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002341 assert(Agg->getType()->isFirstClassType() &&
2342 "Non-first-class type for constant InsertValue expression");
Dan Gohman3db11c22008-06-03 00:15:20 +00002343 if (Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs, NumIdx))
2344 return FC; // Fold a few common cases...
Dan Gohman12fce772008-05-15 19:50:34 +00002345 // Look up the constant in the table first to ensure uniqueness
2346 std::vector<Constant*> ArgVec;
Dan Gohman12fce772008-05-15 19:50:34 +00002347 ArgVec.push_back(Agg);
2348 ArgVec.push_back(Val);
Dan Gohman1ecaf452008-05-31 00:58:22 +00002349 SmallVector<unsigned, 4> Indices(Idxs, Idxs + NumIdx);
2350 const ExprMapKeyType Key(Instruction::InsertValue, ArgVec, 0, Indices);
Dan Gohman12fce772008-05-15 19:50:34 +00002351 return ExprConstants->getOrCreate(ReqTy, Key);
2352}
2353
2354Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002355 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002356 assert(Agg->getType()->isFirstClassType() &&
2357 "Tried to create insertelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002358
Dan Gohman0752bff2008-05-23 00:36:11 +00002359 const Type *ReqTy = Agg->getType();
2360 const Type *ValTy =
Dan Gohman12fce772008-05-15 19:50:34 +00002361 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
Dan Gohman0752bff2008-05-23 00:36:11 +00002362 assert(ValTy == Val->getType() && "insertvalue indices invalid!");
Dan Gohman12fce772008-05-15 19:50:34 +00002363 return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx);
2364}
2365
2366Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002367 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002368 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2369 Idxs+NumIdx) == ReqTy &&
2370 "extractvalue indices invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002371 assert(Agg->getType()->isFirstClassType() &&
2372 "Non-first-class type for constant extractvalue expression");
Dan Gohman3db11c22008-06-03 00:15:20 +00002373 if (Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs, NumIdx))
2374 return FC; // Fold a few common cases...
Dan Gohman12fce772008-05-15 19:50:34 +00002375 // Look up the constant in the table first to ensure uniqueness
2376 std::vector<Constant*> ArgVec;
Dan Gohman12fce772008-05-15 19:50:34 +00002377 ArgVec.push_back(Agg);
Dan Gohman7bb04502008-05-31 19:09:08 +00002378 SmallVector<unsigned, 4> Indices(Idxs, Idxs + NumIdx);
Dan Gohman1ecaf452008-05-31 00:58:22 +00002379 const ExprMapKeyType Key(Instruction::ExtractValue, ArgVec, 0, Indices);
Dan Gohman12fce772008-05-15 19:50:34 +00002380 return ExprConstants->getOrCreate(ReqTy, Key);
2381}
2382
2383Constant *ConstantExpr::getExtractValue(Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002384 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002385 assert(Agg->getType()->isFirstClassType() &&
2386 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002387
2388 const Type *ReqTy =
2389 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
2390 assert(ReqTy && "extractvalue indices invalid!");
2391 return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx);
2392}
2393
Reid Spencer2eadb532007-01-21 00:29:26 +00002394Constant *ConstantExpr::getZeroValueForNegationExpr(const Type *Ty) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002395 if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
Reid Spencer6598ca82007-01-21 02:29:10 +00002396 if (PTy->getElementType()->isFloatingPoint()) {
2397 std::vector<Constant*> zeros(PTy->getNumElements(),
Dale Johannesen98d3a082007-09-14 22:26:36 +00002398 ConstantFP::getNegativeZero(PTy->getElementType()));
Reid Spencerd84d35b2007-02-15 02:26:10 +00002399 return ConstantVector::get(PTy, zeros);
Reid Spencer6598ca82007-01-21 02:29:10 +00002400 }
Reid Spencer2eadb532007-01-21 00:29:26 +00002401
Dale Johannesen98d3a082007-09-14 22:26:36 +00002402 if (Ty->isFloatingPoint())
2403 return ConstantFP::getNegativeZero(Ty);
Reid Spencer2eadb532007-01-21 00:29:26 +00002404
2405 return Constant::getNullValue(Ty);
2406}
2407
Vikram S. Adve4c485332002-07-15 18:19:33 +00002408// destroyConstant - Remove the constant from the constant table...
2409//
2410void ConstantExpr::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00002411 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002412 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002413}
2414
Chris Lattner3cd8c562002-07-30 18:54:25 +00002415const char *ConstantExpr::getOpcodeName() const {
2416 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002417}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00002418
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002419//===----------------------------------------------------------------------===//
2420// replaceUsesOfWithOnConstant implementations
2421
Chris Lattner913849b2007-08-21 00:55:23 +00002422/// replaceUsesOfWithOnConstant - Update this constant array to change uses of
2423/// 'From' to be uses of 'To'. This must update the uniquing data structures
2424/// etc.
2425///
2426/// Note that we intentionally replace all uses of From with To here. Consider
2427/// a large array that uses 'From' 1000 times. By handling this case all here,
2428/// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that
2429/// single invocation handles all 1000 uses. Handling them one at a time would
2430/// work, but would be really slow because it would have to unique each updated
2431/// array instance.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002432void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002433 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002434 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002435 Constant *ToC = cast<Constant>(To);
Chris Lattnerdff59112005-10-04 18:47:09 +00002436
Jim Laskeyc03caef2006-07-17 17:38:29 +00002437 std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnerb64419a2005-10-03 22:51:37 +00002438 Lookup.first.first = getType();
2439 Lookup.second = this;
Chris Lattnerdff59112005-10-04 18:47:09 +00002440
Chris Lattnerb64419a2005-10-03 22:51:37 +00002441 std::vector<Constant*> &Values = Lookup.first.second;
2442 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattnerdff59112005-10-04 18:47:09 +00002443
Chris Lattner8760ec72005-10-04 01:17:50 +00002444 // Fill values with the modified operands of the constant array. Also,
2445 // compute whether this turns into an all-zeros array.
Chris Lattnerdff59112005-10-04 18:47:09 +00002446 bool isAllZeros = false;
Chris Lattner913849b2007-08-21 00:55:23 +00002447 unsigned NumUpdated = 0;
Chris Lattnerdff59112005-10-04 18:47:09 +00002448 if (!ToC->isNullValue()) {
Chris Lattner913849b2007-08-21 00:55:23 +00002449 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2450 Constant *Val = cast<Constant>(O->get());
2451 if (Val == From) {
2452 Val = ToC;
2453 ++NumUpdated;
2454 }
2455 Values.push_back(Val);
2456 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002457 } else {
2458 isAllZeros = true;
2459 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2460 Constant *Val = cast<Constant>(O->get());
Chris Lattner913849b2007-08-21 00:55:23 +00002461 if (Val == From) {
2462 Val = ToC;
2463 ++NumUpdated;
2464 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002465 Values.push_back(Val);
2466 if (isAllZeros) isAllZeros = Val->isNullValue();
2467 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002468 }
2469
Chris Lattnerb64419a2005-10-03 22:51:37 +00002470 Constant *Replacement = 0;
2471 if (isAllZeros) {
2472 Replacement = ConstantAggregateZero::get(getType());
2473 } else {
2474 // Check to see if we have this array type already.
2475 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002476 ArrayConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002477 ArrayConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002478
2479 if (Exists) {
2480 Replacement = I->second;
2481 } else {
2482 // Okay, the new shape doesn't exist in the system yet. Instead of
2483 // creating a new constant array, inserting it, replaceallusesof'ing the
2484 // old with the new, then deleting the old... just update the current one
2485 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002486 ArrayConstants->MoveConstantToNewSlot(this, I);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002487
Chris Lattner913849b2007-08-21 00:55:23 +00002488 // Update to the new value. Optimize for the case when we have a single
2489 // operand that we're changing, but handle bulk updates efficiently.
2490 if (NumUpdated == 1) {
2491 unsigned OperandToUpdate = U-OperandList;
2492 assert(getOperand(OperandToUpdate) == From &&
2493 "ReplaceAllUsesWith broken!");
2494 setOperand(OperandToUpdate, ToC);
2495 } else {
2496 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2497 if (getOperand(i) == From)
2498 setOperand(i, ToC);
2499 }
Chris Lattnerb64419a2005-10-03 22:51:37 +00002500 return;
2501 }
2502 }
2503
2504 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002505 assert(Replacement != this && "I didn't contain From!");
2506
Chris Lattner7a1450d2005-10-04 18:13:04 +00002507 // Everyone using this now uses the replacement.
2508 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002509
2510 // Delete the old constant!
2511 destroyConstant();
2512}
2513
2514void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002515 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002516 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002517 Constant *ToC = cast<Constant>(To);
2518
Chris Lattnerdff59112005-10-04 18:47:09 +00002519 unsigned OperandToUpdate = U-OperandList;
2520 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
2521
Jim Laskeyc03caef2006-07-17 17:38:29 +00002522 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattner8760ec72005-10-04 01:17:50 +00002523 Lookup.first.first = getType();
2524 Lookup.second = this;
2525 std::vector<Constant*> &Values = Lookup.first.second;
2526 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002527
Chris Lattnerdff59112005-10-04 18:47:09 +00002528
Chris Lattner8760ec72005-10-04 01:17:50 +00002529 // Fill values with the modified operands of the constant struct. Also,
2530 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00002531 bool isAllZeros = false;
2532 if (!ToC->isNullValue()) {
2533 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
2534 Values.push_back(cast<Constant>(O->get()));
2535 } else {
2536 isAllZeros = true;
2537 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2538 Constant *Val = cast<Constant>(O->get());
2539 Values.push_back(Val);
2540 if (isAllZeros) isAllZeros = Val->isNullValue();
2541 }
Chris Lattner8760ec72005-10-04 01:17:50 +00002542 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002543 Values[OperandToUpdate] = ToC;
2544
Chris Lattner8760ec72005-10-04 01:17:50 +00002545 Constant *Replacement = 0;
2546 if (isAllZeros) {
2547 Replacement = ConstantAggregateZero::get(getType());
2548 } else {
2549 // Check to see if we have this array type already.
2550 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002551 StructConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002552 StructConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00002553
2554 if (Exists) {
2555 Replacement = I->second;
2556 } else {
2557 // Okay, the new shape doesn't exist in the system yet. Instead of
2558 // creating a new constant struct, inserting it, replaceallusesof'ing the
2559 // old with the new, then deleting the old... just update the current one
2560 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002561 StructConstants->MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00002562
Chris Lattnerdff59112005-10-04 18:47:09 +00002563 // Update to the new value.
2564 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00002565 return;
2566 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002567 }
2568
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002569 assert(Replacement != this && "I didn't contain From!");
2570
Chris Lattner7a1450d2005-10-04 18:13:04 +00002571 // Everyone using this now uses the replacement.
2572 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002573
2574 // Delete the old constant!
2575 destroyConstant();
2576}
2577
Reid Spencerd84d35b2007-02-15 02:26:10 +00002578void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002579 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002580 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2581
2582 std::vector<Constant*> Values;
2583 Values.reserve(getNumOperands()); // Build replacement array...
2584 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2585 Constant *Val = getOperand(i);
2586 if (Val == From) Val = cast<Constant>(To);
2587 Values.push_back(Val);
2588 }
2589
Reid Spencerd84d35b2007-02-15 02:26:10 +00002590 Constant *Replacement = ConstantVector::get(getType(), Values);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002591 assert(Replacement != this && "I didn't contain From!");
2592
Chris Lattner7a1450d2005-10-04 18:13:04 +00002593 // Everyone using this now uses the replacement.
2594 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002595
2596 // Delete the old constant!
2597 destroyConstant();
2598}
2599
2600void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002601 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002602 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2603 Constant *To = cast<Constant>(ToV);
2604
2605 Constant *Replacement = 0;
2606 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerb5d70302007-02-19 20:01:23 +00002607 SmallVector<Constant*, 8> Indices;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002608 Constant *Pointer = getOperand(0);
2609 Indices.reserve(getNumOperands()-1);
2610 if (Pointer == From) Pointer = To;
2611
2612 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2613 Constant *Val = getOperand(i);
2614 if (Val == From) Val = To;
2615 Indices.push_back(Val);
2616 }
Chris Lattnerb5d70302007-02-19 20:01:23 +00002617 Replacement = ConstantExpr::getGetElementPtr(Pointer,
2618 &Indices[0], Indices.size());
Dan Gohman12fce772008-05-15 19:50:34 +00002619 } else if (getOpcode() == Instruction::ExtractValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002620 Constant *Agg = getOperand(0);
Dan Gohman12fce772008-05-15 19:50:34 +00002621 if (Agg == From) Agg = To;
2622
Dan Gohman1ecaf452008-05-31 00:58:22 +00002623 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002624 Replacement = ConstantExpr::getExtractValue(Agg,
2625 &Indices[0], Indices.size());
2626 } else if (getOpcode() == Instruction::InsertValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002627 Constant *Agg = getOperand(0);
2628 Constant *Val = getOperand(1);
Dan Gohman12fce772008-05-15 19:50:34 +00002629 if (Agg == From) Agg = To;
2630 if (Val == From) Val = To;
2631
Dan Gohman1ecaf452008-05-31 00:58:22 +00002632 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002633 Replacement = ConstantExpr::getInsertValue(Agg, Val,
2634 &Indices[0], Indices.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002635 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002636 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002637 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002638 } else if (getOpcode() == Instruction::Select) {
2639 Constant *C1 = getOperand(0);
2640 Constant *C2 = getOperand(1);
2641 Constant *C3 = getOperand(2);
2642 if (C1 == From) C1 = To;
2643 if (C2 == From) C2 = To;
2644 if (C3 == From) C3 = To;
2645 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00002646 } else if (getOpcode() == Instruction::ExtractElement) {
2647 Constant *C1 = getOperand(0);
2648 Constant *C2 = getOperand(1);
2649 if (C1 == From) C1 = To;
2650 if (C2 == From) C2 = To;
2651 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00002652 } else if (getOpcode() == Instruction::InsertElement) {
2653 Constant *C1 = getOperand(0);
2654 Constant *C2 = getOperand(1);
2655 Constant *C3 = getOperand(1);
2656 if (C1 == From) C1 = To;
2657 if (C2 == From) C2 = To;
2658 if (C3 == From) C3 = To;
2659 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2660 } else if (getOpcode() == Instruction::ShuffleVector) {
2661 Constant *C1 = getOperand(0);
2662 Constant *C2 = getOperand(1);
2663 Constant *C3 = getOperand(2);
2664 if (C1 == From) C1 = To;
2665 if (C2 == From) C2 = To;
2666 if (C3 == From) C3 = To;
2667 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00002668 } else if (isCompare()) {
2669 Constant *C1 = getOperand(0);
2670 Constant *C2 = getOperand(1);
2671 if (C1 == From) C1 = To;
2672 if (C2 == From) C2 = To;
2673 if (getOpcode() == Instruction::ICmp)
2674 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
2675 else
2676 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002677 } else if (getNumOperands() == 2) {
2678 Constant *C1 = getOperand(0);
2679 Constant *C2 = getOperand(1);
2680 if (C1 == From) C1 = To;
2681 if (C2 == From) C2 = To;
2682 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2683 } else {
2684 assert(0 && "Unknown ConstantExpr type!");
2685 return;
2686 }
2687
2688 assert(Replacement != this && "I didn't contain From!");
2689
Chris Lattner7a1450d2005-10-04 18:13:04 +00002690 // Everyone using this now uses the replacement.
2691 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002692
2693 // Delete the old constant!
2694 destroyConstant();
Matthijs Kooijmanba5d7ef2008-07-03 07:46:41 +00002695}