blob: 7908638fd6795a8b7a3dd0e5b5fbc63c7e2c07b3 [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);
Jim Laskeyc03caef2006-07-17 17:38:29 +00001103 typename MapTy::iterator I = Map.lower_bound(Lookup);
Reid Spencere0fc4df2006-10-20 07:07:24 +00001104 // Is it in the map?
Chris Lattner98fa07b2003-05-23 20:03:32 +00001105 if (I != Map.end() && I->first == Lookup)
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()) {
1122 typename AbstractTypeMapTy::iterator TI =
1123 AbstractTypeMap.lower_bound(Ty);
1124
1125 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
1126 // Add ourselves to the ATU list of the type.
1127 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
1128
1129 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
1130 }
1131 }
Chris Lattner98fa07b2003-05-23 20:03:32 +00001132 return Result;
1133 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001134
Chris Lattner98fa07b2003-05-23 20:03:32 +00001135 void remove(ConstantClass *CP) {
Jim Laskeyc03caef2006-07-17 17:38:29 +00001136 typename MapTy::iterator I = FindExistingElement(CP);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001137 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +00001138 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001139
Chris Lattner935aa922005-10-04 17:48:46 +00001140 if (HasLargeKey) // Remember the reverse mapping if needed.
1141 InverseMap.erase(CP);
1142
Chris Lattnerb50d1352003-10-05 00:17:43 +00001143 // Now that we found the entry, make sure this isn't the entry that
1144 // the AbstractTypeMap points to.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001145 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001146 if (Ty->isAbstract()) {
1147 assert(AbstractTypeMap.count(Ty) &&
1148 "Abstract type not in AbstractTypeMap?");
Jim Laskeyc03caef2006-07-17 17:38:29 +00001149 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
Chris Lattnerb50d1352003-10-05 00:17:43 +00001150 if (ATMEntryIt == I) {
1151 // Yes, we are removing the representative entry for this type.
1152 // See if there are any other entries of the same type.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001153 typename MapTy::iterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001154
Chris Lattnerb50d1352003-10-05 00:17:43 +00001155 // First check the entry before this one...
1156 if (TmpIt != Map.begin()) {
1157 --TmpIt;
1158 if (TmpIt->first.first != Ty) // Not the same type, move back...
1159 ++TmpIt;
1160 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001161
Chris Lattnerb50d1352003-10-05 00:17:43 +00001162 // If we didn't find the same type, try to move forward...
1163 if (TmpIt == ATMEntryIt) {
1164 ++TmpIt;
1165 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
1166 --TmpIt; // No entry afterwards with the same type
1167 }
1168
1169 // If there is another entry in the map of the same abstract type,
1170 // update the AbstractTypeMap entry now.
1171 if (TmpIt != ATMEntryIt) {
1172 ATMEntryIt = TmpIt;
1173 } else {
1174 // Otherwise, we are removing the last instance of this type
1175 // from the table. Remove from the ATM, and from user list.
1176 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
1177 AbstractTypeMap.erase(Ty);
1178 }
Chris Lattner98fa07b2003-05-23 20:03:32 +00001179 }
Chris Lattnerb50d1352003-10-05 00:17:43 +00001180 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001181
Chris Lattnerb50d1352003-10-05 00:17:43 +00001182 Map.erase(I);
1183 }
1184
Chris Lattner3b793c62005-10-04 21:35:50 +00001185
1186 /// MoveConstantToNewSlot - If we are about to change C to be the element
1187 /// specified by I, update our internal data structures to reflect this
1188 /// fact.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001189 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
Chris Lattner3b793c62005-10-04 21:35:50 +00001190 // First, remove the old location of the specified constant in the map.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001191 typename MapTy::iterator OldI = FindExistingElement(C);
Chris Lattner3b793c62005-10-04 21:35:50 +00001192 assert(OldI != Map.end() && "Constant not found in constant table!");
1193 assert(OldI->second == C && "Didn't find correct element?");
1194
1195 // If this constant is the representative element for its abstract type,
1196 // update the AbstractTypeMap so that the representative element is I.
1197 if (C->getType()->isAbstract()) {
1198 typename AbstractTypeMapTy::iterator ATI =
1199 AbstractTypeMap.find(C->getType());
1200 assert(ATI != AbstractTypeMap.end() &&
1201 "Abstract type not in AbstractTypeMap?");
1202 if (ATI->second == OldI)
1203 ATI->second = I;
1204 }
1205
1206 // Remove the old entry from the map.
1207 Map.erase(OldI);
1208
1209 // Update the inverse map so that we know that this constant is now
1210 // located at descriptor I.
1211 if (HasLargeKey) {
1212 assert(I->second == C && "Bad inversemap entry!");
1213 InverseMap[C] = I;
1214 }
1215 }
1216
Chris Lattnerb50d1352003-10-05 00:17:43 +00001217 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +00001218 typename AbstractTypeMapTy::iterator I =
Jim Laskeyc03caef2006-07-17 17:38:29 +00001219 AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +00001220
1221 assert(I != AbstractTypeMap.end() &&
1222 "Abstract type not in AbstractTypeMap?");
1223
1224 // Convert a constant at a time until the last one is gone. The last one
1225 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
1226 // eliminated eventually.
1227 do {
1228 ConvertConstantType<ConstantClass,
Jim Laskeyc03caef2006-07-17 17:38:29 +00001229 TypeClass>::convert(
1230 static_cast<ConstantClass *>(I->second->second),
Chris Lattnerb50d1352003-10-05 00:17:43 +00001231 cast<TypeClass>(NewTy));
1232
Jim Laskeyc03caef2006-07-17 17:38:29 +00001233 I = AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +00001234 } while (I != AbstractTypeMap.end());
1235 }
1236
1237 // If the type became concrete without being refined to any other existing
1238 // type, we just remove ourselves from the ATU list.
1239 void typeBecameConcrete(const DerivedType *AbsTy) {
1240 AbsTy->removeAbstractTypeUser(this);
1241 }
1242
1243 void dump() const {
Bill Wendling6a462f12006-11-17 08:03:48 +00001244 DOUT << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +00001245 }
1246 };
1247}
1248
Chris Lattnera84df0a22006-09-28 23:36:21 +00001249
Chris Lattner28173502007-02-20 06:11:36 +00001250
Chris Lattner9fba3da2004-02-15 05:53:04 +00001251//---- ConstantAggregateZero::get() implementation...
1252//
1253namespace llvm {
1254 // ConstantAggregateZero does not take extra "value" argument...
1255 template<class ValType>
1256 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
1257 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
1258 return new ConstantAggregateZero(Ty);
1259 }
1260 };
1261
1262 template<>
1263 struct ConvertConstantType<ConstantAggregateZero, Type> {
1264 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
1265 // Make everyone now use a constant of the new type...
1266 Constant *New = ConstantAggregateZero::get(NewTy);
1267 assert(New != OldC && "Didn't replace constant??");
1268 OldC->uncheckedReplaceAllUsesWith(New);
1269 OldC->destroyConstant(); // This constant is now dead, destroy it.
1270 }
1271 };
1272}
1273
Chris Lattner69edc982006-09-28 00:35:06 +00001274static ManagedStatic<ValueMap<char, Type,
1275 ConstantAggregateZero> > AggZeroConstants;
Chris Lattner9fba3da2004-02-15 05:53:04 +00001276
Chris Lattner3e650af2004-08-04 04:48:01 +00001277static char getValType(ConstantAggregateZero *CPZ) { return 0; }
1278
Chris Lattner9fba3da2004-02-15 05:53:04 +00001279Constant *ConstantAggregateZero::get(const Type *Ty) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001280 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) &&
Chris Lattnerbfd0b6d2006-06-10 04:16:23 +00001281 "Cannot create an aggregate zero of non-aggregate type!");
Chris Lattner69edc982006-09-28 00:35:06 +00001282 return AggZeroConstants->getOrCreate(Ty, 0);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001283}
1284
1285// destroyConstant - Remove the constant from the constant table...
1286//
1287void ConstantAggregateZero::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001288 AggZeroConstants->remove(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001289 destroyConstantImpl();
1290}
1291
Chris Lattner3462ae32001-12-03 22:26:30 +00001292//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001293//
Chris Lattner189d19f2003-11-21 20:23:48 +00001294namespace llvm {
1295 template<>
1296 struct ConvertConstantType<ConstantArray, ArrayType> {
1297 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
1298 // Make everyone now use a constant of the new type...
1299 std::vector<Constant*> C;
1300 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1301 C.push_back(cast<Constant>(OldC->getOperand(i)));
1302 Constant *New = ConstantArray::get(NewTy, C);
1303 assert(New != OldC && "Didn't replace constant??");
1304 OldC->uncheckedReplaceAllUsesWith(New);
1305 OldC->destroyConstant(); // This constant is now dead, destroy it.
1306 }
1307 };
1308}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001309
Chris Lattner3e650af2004-08-04 04:48:01 +00001310static std::vector<Constant*> getValType(ConstantArray *CA) {
1311 std::vector<Constant*> Elements;
1312 Elements.reserve(CA->getNumOperands());
1313 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1314 Elements.push_back(cast<Constant>(CA->getOperand(i)));
1315 return Elements;
1316}
1317
Chris Lattnerb64419a2005-10-03 22:51:37 +00001318typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattner935aa922005-10-04 17:48:46 +00001319 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001320static ManagedStatic<ArrayConstantsTy> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001321
Chris Lattner015e8212004-02-15 04:14:47 +00001322Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +00001323 const std::vector<Constant*> &V) {
1324 // If this is an all-zero array, return a ConstantAggregateZero object
1325 if (!V.empty()) {
1326 Constant *C = V[0];
1327 if (!C->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001328 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001329 for (unsigned i = 1, e = V.size(); i != e; ++i)
1330 if (V[i] != C)
Chris Lattner69edc982006-09-28 00:35:06 +00001331 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001332 }
1333 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001334}
1335
Chris Lattner98fa07b2003-05-23 20:03:32 +00001336// destroyConstant - Remove the constant from the constant table...
1337//
1338void ConstantArray::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001339 ArrayConstants->remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001340 destroyConstantImpl();
1341}
1342
Reid Spencer6f614532006-05-30 08:23:18 +00001343/// ConstantArray::get(const string&) - Return an array that is initialized to
1344/// contain the specified string. If length is zero then a null terminator is
1345/// added to the specified string so that it may be used in a natural way.
1346/// Otherwise, the length parameter specifies how much of the string to use
1347/// and it won't be null terminated.
1348///
Reid Spencer82ebaba2006-05-30 18:15:07 +00001349Constant *ConstantArray::get(const std::string &Str, bool AddNull) {
Chris Lattner7f74a562002-01-20 22:54:45 +00001350 std::vector<Constant*> ElementVals;
Reid Spencer82ebaba2006-05-30 18:15:07 +00001351 for (unsigned i = 0; i < Str.length(); ++i)
Reid Spencer8d9336d2006-12-31 05:26:44 +00001352 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +00001353
1354 // Add a null terminator to the string...
Reid Spencer82ebaba2006-05-30 18:15:07 +00001355 if (AddNull) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001356 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, 0));
Reid Spencer6f614532006-05-30 08:23:18 +00001357 }
Chris Lattner8f80fe02001-10-14 23:54:12 +00001358
Reid Spencer8d9336d2006-12-31 05:26:44 +00001359 ArrayType *ATy = ArrayType::get(Type::Int8Ty, ElementVals.size());
Chris Lattner3462ae32001-12-03 22:26:30 +00001360 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +00001361}
1362
Reid Spencer2546b762007-01-26 07:37:34 +00001363/// isString - This method returns true if the array is an array of i8, and
1364/// if the elements of the array are all ConstantInt's.
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001365bool ConstantArray::isString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001366 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001367 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001368 return false;
1369 // Check the elements to make sure they are all integers, not constant
1370 // expressions.
1371 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1372 if (!isa<ConstantInt>(getOperand(i)))
1373 return false;
1374 return true;
1375}
1376
Evan Cheng3763c5b2006-10-26 19:15:05 +00001377/// isCString - This method returns true if the array is a string (see
1378/// isString) and it ends in a null byte \0 and does not contains any other
1379/// null bytes except its terminator.
1380bool ConstantArray::isCString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001381 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001382 if (getType()->getElementType() != Type::Int8Ty)
Evan Chenge974da62006-10-26 21:48:03 +00001383 return false;
1384 Constant *Zero = Constant::getNullValue(getOperand(0)->getType());
1385 // Last element must be a null.
1386 if (getOperand(getNumOperands()-1) != Zero)
1387 return false;
1388 // Other elements must be non-null integers.
1389 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1390 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +00001391 return false;
Evan Chenge974da62006-10-26 21:48:03 +00001392 if (getOperand(i) == Zero)
1393 return false;
1394 }
Evan Cheng3763c5b2006-10-26 19:15:05 +00001395 return true;
1396}
1397
1398
Reid Spencer2546b762007-01-26 07:37:34 +00001399// getAsString - If the sub-element type of this array is i8
Chris Lattner81fabb02002-08-26 17:53:56 +00001400// then this method converts the array to an std::string and returns it.
1401// Otherwise, it asserts out.
1402//
1403std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001404 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001405 std::string Result;
Owen Anderson79c69bc2008-06-24 21:58:29 +00001406 Result.reserve(getNumOperands());
Chris Lattner6077c312003-07-23 15:22:26 +00001407 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonee9c30d2008-06-25 01:05:05 +00001408 Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue());
Chris Lattner81fabb02002-08-26 17:53:56 +00001409 return Result;
1410}
1411
1412
Chris Lattner3462ae32001-12-03 22:26:30 +00001413//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001414//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001415
Chris Lattner189d19f2003-11-21 20:23:48 +00001416namespace llvm {
1417 template<>
1418 struct ConvertConstantType<ConstantStruct, StructType> {
1419 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1420 // Make everyone now use a constant of the new type...
1421 std::vector<Constant*> C;
1422 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1423 C.push_back(cast<Constant>(OldC->getOperand(i)));
1424 Constant *New = ConstantStruct::get(NewTy, C);
1425 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001426
Chris Lattner189d19f2003-11-21 20:23:48 +00001427 OldC->uncheckedReplaceAllUsesWith(New);
1428 OldC->destroyConstant(); // This constant is now dead, destroy it.
1429 }
1430 };
1431}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001432
Chris Lattner8760ec72005-10-04 01:17:50 +00001433typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattner935aa922005-10-04 17:48:46 +00001434 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001435static ManagedStatic<StructConstantsTy> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001436
Chris Lattner3e650af2004-08-04 04:48:01 +00001437static std::vector<Constant*> getValType(ConstantStruct *CS) {
1438 std::vector<Constant*> Elements;
1439 Elements.reserve(CS->getNumOperands());
1440 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1441 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1442 return Elements;
1443}
1444
Chris Lattner015e8212004-02-15 04:14:47 +00001445Constant *ConstantStruct::get(const StructType *Ty,
1446 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001447 // Create a ConstantAggregateZero value if all elements are zeros...
1448 for (unsigned i = 0, e = V.size(); i != e; ++i)
1449 if (!V[i]->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001450 return StructConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001451
1452 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001453}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001454
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001455Constant *ConstantStruct::get(const std::vector<Constant*> &V, bool packed) {
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001456 std::vector<const Type*> StructEls;
1457 StructEls.reserve(V.size());
1458 for (unsigned i = 0, e = V.size(); i != e; ++i)
1459 StructEls.push_back(V[i]->getType());
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001460 return get(StructType::get(StructEls, packed), V);
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001461}
1462
Chris Lattnerd7a73302001-10-13 06:57:33 +00001463// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001464//
Chris Lattner3462ae32001-12-03 22:26:30 +00001465void ConstantStruct::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001466 StructConstants->remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001467 destroyConstantImpl();
1468}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001469
Reid Spencerd84d35b2007-02-15 02:26:10 +00001470//---- ConstantVector::get() implementation...
Brian Gaeke02209042004-08-20 06:00:58 +00001471//
1472namespace llvm {
1473 template<>
Reid Spencerd84d35b2007-02-15 02:26:10 +00001474 struct ConvertConstantType<ConstantVector, VectorType> {
1475 static void convert(ConstantVector *OldC, const VectorType *NewTy) {
Brian Gaeke02209042004-08-20 06:00:58 +00001476 // Make everyone now use a constant of the new type...
1477 std::vector<Constant*> C;
1478 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1479 C.push_back(cast<Constant>(OldC->getOperand(i)));
Reid Spencerd84d35b2007-02-15 02:26:10 +00001480 Constant *New = ConstantVector::get(NewTy, C);
Brian Gaeke02209042004-08-20 06:00:58 +00001481 assert(New != OldC && "Didn't replace constant??");
1482 OldC->uncheckedReplaceAllUsesWith(New);
1483 OldC->destroyConstant(); // This constant is now dead, destroy it.
1484 }
1485 };
1486}
1487
Reid Spencerd84d35b2007-02-15 02:26:10 +00001488static std::vector<Constant*> getValType(ConstantVector *CP) {
Brian Gaeke02209042004-08-20 06:00:58 +00001489 std::vector<Constant*> Elements;
1490 Elements.reserve(CP->getNumOperands());
1491 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1492 Elements.push_back(CP->getOperand(i));
1493 return Elements;
1494}
1495
Reid Spencerd84d35b2007-02-15 02:26:10 +00001496static ManagedStatic<ValueMap<std::vector<Constant*>, VectorType,
Reid Spencer09575ba2007-02-15 03:39:18 +00001497 ConstantVector> > VectorConstants;
Brian Gaeke02209042004-08-20 06:00:58 +00001498
Reid Spencerd84d35b2007-02-15 02:26:10 +00001499Constant *ConstantVector::get(const VectorType *Ty,
Brian Gaeke02209042004-08-20 06:00:58 +00001500 const std::vector<Constant*> &V) {
Dan Gohman30978072007-05-24 14:36:04 +00001501 // If this is an all-zero vector, return a ConstantAggregateZero object
Brian Gaeke02209042004-08-20 06:00:58 +00001502 if (!V.empty()) {
1503 Constant *C = V[0];
1504 if (!C->isNullValue())
Reid Spencer09575ba2007-02-15 03:39:18 +00001505 return VectorConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001506 for (unsigned i = 1, e = V.size(); i != e; ++i)
1507 if (V[i] != C)
Reid Spencer09575ba2007-02-15 03:39:18 +00001508 return VectorConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001509 }
1510 return ConstantAggregateZero::get(Ty);
1511}
1512
Reid Spencerd84d35b2007-02-15 02:26:10 +00001513Constant *ConstantVector::get(const std::vector<Constant*> &V) {
Brian Gaeke02209042004-08-20 06:00:58 +00001514 assert(!V.empty() && "Cannot infer type if V is empty");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001515 return get(VectorType::get(V.front()->getType(),V.size()), V);
Brian Gaeke02209042004-08-20 06:00:58 +00001516}
1517
1518// destroyConstant - Remove the constant from the constant table...
1519//
Reid Spencerd84d35b2007-02-15 02:26:10 +00001520void ConstantVector::destroyConstant() {
Reid Spencer09575ba2007-02-15 03:39:18 +00001521 VectorConstants->remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001522 destroyConstantImpl();
1523}
1524
Dan Gohman30978072007-05-24 14:36:04 +00001525/// This function will return true iff every element in this vector constant
Jim Laskeyf0478822007-01-12 22:39:14 +00001526/// is set to all ones.
1527/// @returns true iff this constant's emements are all set to all ones.
1528/// @brief Determine if the value is all ones.
Reid Spencerd84d35b2007-02-15 02:26:10 +00001529bool ConstantVector::isAllOnesValue() const {
Jim Laskeyf0478822007-01-12 22:39:14 +00001530 // Check out first element.
1531 const Constant *Elt = getOperand(0);
1532 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1533 if (!CI || !CI->isAllOnesValue()) return false;
1534 // Then make sure all remaining elements point to the same value.
1535 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1536 if (getOperand(I) != Elt) return false;
1537 }
1538 return true;
1539}
1540
Dan Gohman07159202007-10-17 17:51:30 +00001541/// getSplatValue - If this is a splat constant, where all of the
1542/// elements have the same value, return that value. Otherwise return null.
1543Constant *ConstantVector::getSplatValue() {
1544 // Check out first element.
1545 Constant *Elt = getOperand(0);
1546 // Then make sure all remaining elements point to the same value.
1547 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
1548 if (getOperand(I) != Elt) return 0;
1549 return Elt;
1550}
1551
Chris Lattner3462ae32001-12-03 22:26:30 +00001552//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001553//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001554
Chris Lattner189d19f2003-11-21 20:23:48 +00001555namespace llvm {
1556 // ConstantPointerNull does not take extra "value" argument...
1557 template<class ValType>
1558 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1559 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1560 return new ConstantPointerNull(Ty);
1561 }
1562 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001563
Chris Lattner189d19f2003-11-21 20:23:48 +00001564 template<>
1565 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1566 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1567 // Make everyone now use a constant of the new type...
1568 Constant *New = ConstantPointerNull::get(NewTy);
1569 assert(New != OldC && "Didn't replace constant??");
1570 OldC->uncheckedReplaceAllUsesWith(New);
1571 OldC->destroyConstant(); // This constant is now dead, destroy it.
1572 }
1573 };
1574}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001575
Chris Lattner69edc982006-09-28 00:35:06 +00001576static ManagedStatic<ValueMap<char, PointerType,
1577 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001578
Chris Lattner3e650af2004-08-04 04:48:01 +00001579static char getValType(ConstantPointerNull *) {
1580 return 0;
1581}
1582
1583
Chris Lattner3462ae32001-12-03 22:26:30 +00001584ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001585 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001586}
1587
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001588// destroyConstant - Remove the constant from the constant table...
1589//
1590void ConstantPointerNull::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001591 NullPtrConstants->remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001592 destroyConstantImpl();
1593}
1594
1595
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001596//---- UndefValue::get() implementation...
1597//
1598
1599namespace llvm {
1600 // UndefValue does not take extra "value" argument...
1601 template<class ValType>
1602 struct ConstantCreator<UndefValue, Type, ValType> {
1603 static UndefValue *create(const Type *Ty, const ValType &V) {
1604 return new UndefValue(Ty);
1605 }
1606 };
1607
1608 template<>
1609 struct ConvertConstantType<UndefValue, Type> {
1610 static void convert(UndefValue *OldC, const Type *NewTy) {
1611 // Make everyone now use a constant of the new type.
1612 Constant *New = UndefValue::get(NewTy);
1613 assert(New != OldC && "Didn't replace constant??");
1614 OldC->uncheckedReplaceAllUsesWith(New);
1615 OldC->destroyConstant(); // This constant is now dead, destroy it.
1616 }
1617 };
1618}
1619
Chris Lattner69edc982006-09-28 00:35:06 +00001620static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001621
1622static char getValType(UndefValue *) {
1623 return 0;
1624}
1625
1626
1627UndefValue *UndefValue::get(const Type *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001628 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001629}
1630
1631// destroyConstant - Remove the constant from the constant table.
1632//
1633void UndefValue::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001634 UndefValueConstants->remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001635 destroyConstantImpl();
1636}
1637
1638
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001639//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001640//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001641
Dan Gohmand78c4002008-05-13 00:00:25 +00001642namespace {
1643
Reid Spenceree3c9912006-12-04 05:19:50 +00001644struct ExprMapKeyType {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001645 typedef SmallVector<unsigned, 4> IndexList;
1646
1647 ExprMapKeyType(unsigned opc,
1648 const std::vector<Constant*> &ops,
1649 unsigned short pred = 0,
1650 const IndexList &inds = IndexList())
1651 : opcode(opc), predicate(pred), operands(ops), indices(inds) {}
Reid Spencerdba6aa42006-12-04 18:38:05 +00001652 uint16_t opcode;
1653 uint16_t predicate;
Reid Spenceree3c9912006-12-04 05:19:50 +00001654 std::vector<Constant*> operands;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001655 IndexList indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001656 bool operator==(const ExprMapKeyType& that) const {
1657 return this->opcode == that.opcode &&
1658 this->predicate == that.predicate &&
1659 this->operands == that.operands;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001660 this->indices == that.indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001661 }
1662 bool operator<(const ExprMapKeyType & that) const {
1663 return this->opcode < that.opcode ||
1664 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1665 (this->opcode == that.opcode && this->predicate == that.predicate &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001666 this->operands < that.operands) ||
1667 (this->opcode == that.opcode && this->predicate == that.predicate &&
1668 this->operands == that.operands && this->indices < that.indices);
Reid Spenceree3c9912006-12-04 05:19:50 +00001669 }
1670
1671 bool operator!=(const ExprMapKeyType& that) const {
1672 return !(*this == that);
1673 }
1674};
Chris Lattner98fa07b2003-05-23 20:03:32 +00001675
Dan Gohmand78c4002008-05-13 00:00:25 +00001676}
1677
Chris Lattner189d19f2003-11-21 20:23:48 +00001678namespace llvm {
1679 template<>
1680 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001681 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1682 unsigned short pred = 0) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001683 if (Instruction::isCast(V.opcode))
1684 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1685 if ((V.opcode >= Instruction::BinaryOpsBegin &&
Reid Spencer2341c222007-02-02 02:16:23 +00001686 V.opcode < Instruction::BinaryOpsEnd))
Reid Spenceree3c9912006-12-04 05:19:50 +00001687 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1688 if (V.opcode == Instruction::Select)
1689 return new SelectConstantExpr(V.operands[0], V.operands[1],
1690 V.operands[2]);
1691 if (V.opcode == Instruction::ExtractElement)
1692 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1693 if (V.opcode == Instruction::InsertElement)
1694 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1695 V.operands[2]);
1696 if (V.opcode == Instruction::ShuffleVector)
1697 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1698 V.operands[2]);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001699 if (V.opcode == Instruction::InsertValue)
1700 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
1701 V.indices, Ty);
1702 if (V.opcode == Instruction::ExtractValue)
1703 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001704 if (V.opcode == Instruction::GetElementPtr) {
1705 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
Gabor Greife9ecc682008-04-06 20:25:17 +00001706 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001707 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001708
Reid Spenceree3c9912006-12-04 05:19:50 +00001709 // The compare instructions are weird. We have to encode the predicate
1710 // value and it is combined with the instruction opcode by multiplying
1711 // the opcode by one hundred. We must decode this to get the predicate.
1712 if (V.opcode == Instruction::ICmp)
Nate Begemand2195702008-05-12 19:01:56 +00001713 return new CompareConstantExpr(Ty, Instruction::ICmp, V.predicate,
Reid Spenceree3c9912006-12-04 05:19:50 +00001714 V.operands[0], V.operands[1]);
1715 if (V.opcode == Instruction::FCmp)
Nate Begemand2195702008-05-12 19:01:56 +00001716 return new CompareConstantExpr(Ty, Instruction::FCmp, V.predicate,
1717 V.operands[0], V.operands[1]);
1718 if (V.opcode == Instruction::VICmp)
1719 return new CompareConstantExpr(Ty, Instruction::VICmp, V.predicate,
1720 V.operands[0], V.operands[1]);
1721 if (V.opcode == Instruction::VFCmp)
1722 return new CompareConstantExpr(Ty, Instruction::VFCmp, V.predicate,
Reid Spenceree3c9912006-12-04 05:19:50 +00001723 V.operands[0], V.operands[1]);
1724 assert(0 && "Invalid ConstantExpr!");
Jeff Cohen9f469632006-12-15 21:47:01 +00001725 return 0;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001726 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001727 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001728
Chris Lattner189d19f2003-11-21 20:23:48 +00001729 template<>
1730 struct ConvertConstantType<ConstantExpr, Type> {
1731 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1732 Constant *New;
1733 switch (OldC->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001734 case Instruction::Trunc:
1735 case Instruction::ZExt:
1736 case Instruction::SExt:
1737 case Instruction::FPTrunc:
1738 case Instruction::FPExt:
1739 case Instruction::UIToFP:
1740 case Instruction::SIToFP:
1741 case Instruction::FPToUI:
1742 case Instruction::FPToSI:
1743 case Instruction::PtrToInt:
1744 case Instruction::IntToPtr:
1745 case Instruction::BitCast:
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001746 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
1747 NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001748 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001749 case Instruction::Select:
1750 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1751 OldC->getOperand(1),
1752 OldC->getOperand(2));
1753 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001754 default:
1755 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001756 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner189d19f2003-11-21 20:23:48 +00001757 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1758 OldC->getOperand(1));
1759 break;
1760 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001761 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001762 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
Chris Lattner302116a2007-01-31 04:40:28 +00001763 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
1764 &Idx[0], Idx.size());
Chris Lattner189d19f2003-11-21 20:23:48 +00001765 break;
1766 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001767
Chris Lattner189d19f2003-11-21 20:23:48 +00001768 assert(New != OldC && "Didn't replace constant??");
1769 OldC->uncheckedReplaceAllUsesWith(New);
1770 OldC->destroyConstant(); // This constant is now dead, destroy it.
1771 }
1772 };
1773} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001774
1775
Chris Lattner3e650af2004-08-04 04:48:01 +00001776static ExprMapKeyType getValType(ConstantExpr *CE) {
1777 std::vector<Constant*> Operands;
1778 Operands.reserve(CE->getNumOperands());
1779 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1780 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spenceree3c9912006-12-04 05:19:50 +00001781 return ExprMapKeyType(CE->getOpcode(), Operands,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001782 CE->isCompare() ? CE->getPredicate() : 0,
1783 CE->hasIndices() ?
1784 CE->getIndices() : SmallVector<unsigned, 4>());
Chris Lattner3e650af2004-08-04 04:48:01 +00001785}
1786
Chris Lattner69edc982006-09-28 00:35:06 +00001787static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1788 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001789
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001790/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands7d6c8ae2008-03-30 19:38:55 +00001791/// cast in the ExprConstants map. It is used by the various get* methods below.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001792static inline Constant *getFoldedCast(
1793 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001794 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001795 // Fold a few common cases
1796 if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
1797 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001798
Vikram S. Adve4c485332002-07-15 18:19:33 +00001799 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001800 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00001801 ExprMapKeyType Key(opc, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001802 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001803}
Reid Spencerf37dc652006-12-05 19:14:13 +00001804
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001805Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
1806 Instruction::CastOps opc = Instruction::CastOps(oc);
1807 assert(Instruction::isCast(opc) && "opcode out of range");
1808 assert(C && Ty && "Null arguments to getCast");
1809 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1810
1811 switch (opc) {
1812 default:
1813 assert(0 && "Invalid cast opcode");
1814 break;
1815 case Instruction::Trunc: return getTrunc(C, Ty);
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001816 case Instruction::ZExt: return getZExt(C, Ty);
1817 case Instruction::SExt: return getSExt(C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001818 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1819 case Instruction::FPExt: return getFPExtend(C, Ty);
1820 case Instruction::UIToFP: return getUIToFP(C, Ty);
1821 case Instruction::SIToFP: return getSIToFP(C, Ty);
1822 case Instruction::FPToUI: return getFPToUI(C, Ty);
1823 case Instruction::FPToSI: return getFPToSI(C, Ty);
1824 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1825 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1826 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001827 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001828 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00001829}
1830
Reid Spencer5c140882006-12-04 20:17:56 +00001831Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
1832 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1833 return getCast(Instruction::BitCast, C, Ty);
1834 return getCast(Instruction::ZExt, C, Ty);
1835}
1836
1837Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
1838 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1839 return getCast(Instruction::BitCast, C, Ty);
1840 return getCast(Instruction::SExt, C, Ty);
1841}
1842
1843Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
1844 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1845 return getCast(Instruction::BitCast, C, Ty);
1846 return getCast(Instruction::Trunc, C, Ty);
1847}
1848
Reid Spencerbc245a02006-12-05 03:25:26 +00001849Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
1850 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001851 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001852
Chris Lattner03c49532007-01-15 02:27:26 +00001853 if (Ty->isInteger())
Reid Spencerbc245a02006-12-05 03:25:26 +00001854 return getCast(Instruction::PtrToInt, S, Ty);
1855 return getCast(Instruction::BitCast, S, Ty);
1856}
1857
Reid Spencer56521c42006-12-12 00:51:07 +00001858Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1859 bool isSigned) {
Chris Lattner03c49532007-01-15 02:27:26 +00001860 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer56521c42006-12-12 00:51:07 +00001861 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1862 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1863 Instruction::CastOps opcode =
1864 (SrcBits == DstBits ? Instruction::BitCast :
1865 (SrcBits > DstBits ? Instruction::Trunc :
1866 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1867 return getCast(opcode, C, Ty);
1868}
1869
1870Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
1871 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1872 "Invalid cast");
1873 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1874 unsigned DstBits = Ty->getPrimitiveSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001875 if (SrcBits == DstBits)
1876 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001877 Instruction::CastOps opcode =
Reid Spencerca104e82006-12-12 05:38:50 +00001878 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001879 return getCast(opcode, C, Ty);
1880}
1881
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001882Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001883 assert(C->getType()->isInteger() && "Trunc operand must be integer");
1884 assert(Ty->isInteger() && "Trunc produces only integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001885 assert(C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&&
1886 "SrcTy must be larger than DestTy for Trunc!");
1887
1888 return getFoldedCast(Instruction::Trunc, C, Ty);
1889}
1890
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001891Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001892 assert(C->getType()->isInteger() && "SEXt operand must be integral");
1893 assert(Ty->isInteger() && "SExt produces only integer");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001894 assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1895 "SrcTy must be smaller than DestTy for SExt!");
1896
1897 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerdd284742004-04-04 23:20:30 +00001898}
1899
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001900Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001901 assert(C->getType()->isInteger() && "ZEXt operand must be integral");
1902 assert(Ty->isInteger() && "ZExt 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 ZExt!");
1905
1906 return getFoldedCast(Instruction::ZExt, C, Ty);
1907}
1908
1909Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
1910 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1911 C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&&
1912 "This is an illegal floating point truncation!");
1913 return getFoldedCast(Instruction::FPTrunc, C, Ty);
1914}
1915
1916Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
1917 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1918 C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1919 "This is an illegal floating point extension!");
1920 return getFoldedCast(Instruction::FPExt, C, Ty);
1921}
1922
1923Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Nate Begemand4d45c22007-11-17 03:58:34 +00001924 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1925 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1926 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1927 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
1928 "This is an illegal uint to floating point cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001929 return getFoldedCast(Instruction::UIToFP, C, Ty);
1930}
1931
1932Constant *ConstantExpr::getSIToFP(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() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001937 "This is an illegal sint to floating point cast!");
1938 return getFoldedCast(Instruction::SIToFP, C, Ty);
1939}
1940
1941Constant *ConstantExpr::getFPToUI(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()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1946 "This is an illegal floating point to uint cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001947 return getFoldedCast(Instruction::FPToUI, C, Ty);
1948}
1949
1950Constant *ConstantExpr::getFPToSI(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 sint cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001956 return getFoldedCast(Instruction::FPToSI, C, Ty);
1957}
1958
1959Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
1960 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner03c49532007-01-15 02:27:26 +00001961 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001962 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
1963}
1964
1965Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner03c49532007-01-15 02:27:26 +00001966 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001967 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
1968 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
1969}
1970
1971Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
1972 // BitCast implies a no-op cast of type only. No bits change. However, you
1973 // can't cast pointers to anything but pointers.
1974 const Type *SrcTy = C->getType();
1975 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer5c140882006-12-04 20:17:56 +00001976 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001977
1978 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
1979 // or nonptr->ptr). For all the other types, the cast is okay if source and
1980 // destination bit widths are identical.
1981 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1982 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Reid Spencer5c140882006-12-04 20:17:56 +00001983 assert(SrcBitSize == DstBitSize && "BitCast requies types of same width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001984 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerdd284742004-04-04 23:20:30 +00001985}
1986
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001987Constant *ConstantExpr::getSizeOf(const Type *Ty) {
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001988 // sizeof is implemented as: (i64) gep (Ty*)null, 1
Chris Lattnerb5d70302007-02-19 20:01:23 +00001989 Constant *GEPIdx = ConstantInt::get(Type::Int32Ty, 1);
1990 Constant *GEP =
Christopher Lambedf07882007-12-17 01:12:55 +00001991 getGetElementPtr(getNullValue(PointerType::getUnqual(Ty)), &GEPIdx, 1);
Chris Lattnerb5d70302007-02-19 20:01:23 +00001992 return getCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
Alkis Evlogimenos9160d5f2005-03-19 11:40:31 +00001993}
1994
Chris Lattnerb50d1352003-10-05 00:17:43 +00001995Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Reid Spencera009d0d2006-12-04 21:35:24 +00001996 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001997 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00001998 assert(Opcode >= Instruction::BinaryOpsBegin &&
1999 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00002000 "Invalid opcode in binary constant expression");
2001 assert(C1->getType() == C2->getType() &&
2002 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00002003
Reid Spencer542964f2007-01-11 18:21:29 +00002004 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Chris Lattnerb50d1352003-10-05 00:17:43 +00002005 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
2006 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00002007
Chris Lattner2b383d2e2003-05-13 21:37:02 +00002008 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00002009 ExprMapKeyType Key(Opcode, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002010 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002011}
2012
Reid Spencer266e42b2006-12-23 06:05:41 +00002013Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Reid Spencera009d0d2006-12-04 21:35:24 +00002014 Constant *C1, Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002015 switch (predicate) {
2016 default: assert(0 && "Invalid CmpInst predicate");
2017 case FCmpInst::FCMP_FALSE: case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_OGT:
2018 case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OLE:
2019 case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_ORD: case FCmpInst::FCMP_UNO:
2020 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UGT: case FCmpInst::FCMP_UGE:
2021 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_ULE: case FCmpInst::FCMP_UNE:
2022 case FCmpInst::FCMP_TRUE:
2023 return getFCmp(predicate, C1, C2);
2024 case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_UGT:
2025 case ICmpInst::ICMP_UGE: case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE:
2026 case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_SGE: case ICmpInst::ICMP_SLT:
2027 case ICmpInst::ICMP_SLE:
2028 return getICmp(predicate, C1, C2);
2029 }
Reid Spencera009d0d2006-12-04 21:35:24 +00002030}
2031
2032Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002033#ifndef NDEBUG
2034 switch (Opcode) {
Reid Spencer7eb55b32006-11-02 01:53:59 +00002035 case Instruction::Add:
2036 case Instruction::Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002037 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002038 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattner03c49532007-01-15 02:27:26 +00002039 assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00002040 isa<VectorType>(C1->getType())) &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002041 "Tried to create an arithmetic operation on a non-arithmetic type!");
2042 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002043 case Instruction::UDiv:
2044 case Instruction::SDiv:
2045 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002046 assert((C1->getType()->isInteger() || (isa<VectorType>(C1->getType()) &&
2047 cast<VectorType>(C1->getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002048 "Tried to create an arithmetic operation on a non-arithmetic type!");
2049 break;
2050 case Instruction::FDiv:
2051 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002052 assert((C1->getType()->isFloatingPoint() || (isa<VectorType>(C1->getType())
2053 && cast<VectorType>(C1->getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002054 && "Tried to create an arithmetic operation on a non-arithmetic type!");
2055 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00002056 case Instruction::URem:
2057 case Instruction::SRem:
2058 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002059 assert((C1->getType()->isInteger() || (isa<VectorType>(C1->getType()) &&
2060 cast<VectorType>(C1->getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002061 "Tried to create an arithmetic operation on a non-arithmetic type!");
2062 break;
2063 case Instruction::FRem:
2064 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002065 assert((C1->getType()->isFloatingPoint() || (isa<VectorType>(C1->getType())
2066 && cast<VectorType>(C1->getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00002067 && "Tried to create an arithmetic operation on a non-arithmetic type!");
2068 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002069 case Instruction::And:
2070 case Instruction::Or:
2071 case Instruction::Xor:
2072 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002073 assert((C1->getType()->isInteger() || isa<VectorType>(C1->getType())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00002074 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002075 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002076 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00002077 case Instruction::LShr:
2078 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00002079 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattner03c49532007-01-15 02:27:26 +00002080 assert(C1->getType()->isInteger() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002081 "Tried to create a shift operation on a non-integer type!");
2082 break;
2083 default:
2084 break;
2085 }
2086#endif
2087
Reid Spencera009d0d2006-12-04 21:35:24 +00002088 return getTy(C1->getType(), Opcode, C1, C2);
2089}
2090
Reid Spencer266e42b2006-12-23 06:05:41 +00002091Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00002092 Constant *C1, Constant *C2) {
2093 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002094 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00002095}
2096
Chris Lattner6e415c02004-03-12 05:54:04 +00002097Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
2098 Constant *V1, Constant *V2) {
Reid Spencer2546b762007-01-26 07:37:34 +00002099 assert(C->getType() == Type::Int1Ty && "Select condition must be i1!");
Chris Lattner6e415c02004-03-12 05:54:04 +00002100 assert(V1->getType() == V2->getType() && "Select value types must match!");
2101 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
2102
2103 if (ReqTy == V1->getType())
2104 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
2105 return SC; // Fold common cases
2106
2107 std::vector<Constant*> argVec(3, C);
2108 argVec[1] = V1;
2109 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00002110 ExprMapKeyType Key(Instruction::Select, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002111 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00002112}
2113
Chris Lattnerb50d1352003-10-05 00:17:43 +00002114Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00002115 Value* const *Idxs,
2116 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002117 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
2118 Idxs+NumIdx) ==
2119 cast<PointerType>(ReqTy)->getElementType() &&
2120 "GEP indices invalid!");
Chris Lattner04b60fe2004-02-16 20:46:13 +00002121
Chris Lattner302116a2007-01-31 04:40:28 +00002122 if (Constant *FC = ConstantFoldGetElementPtr(C, (Constant**)Idxs, NumIdx))
Chris Lattneracdbe712003-04-17 19:24:48 +00002123 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00002124
Chris Lattnerb50d1352003-10-05 00:17:43 +00002125 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00002126 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00002127 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00002128 std::vector<Constant*> ArgVec;
Chris Lattner302116a2007-01-31 04:40:28 +00002129 ArgVec.reserve(NumIdx+1);
Chris Lattner13128ab2004-10-11 22:52:25 +00002130 ArgVec.push_back(C);
Chris Lattner302116a2007-01-31 04:40:28 +00002131 for (unsigned i = 0; i != NumIdx; ++i)
2132 ArgVec.push_back(cast<Constant>(Idxs[i]));
2133 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002134 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002135}
2136
Chris Lattner302116a2007-01-31 04:40:28 +00002137Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
2138 unsigned NumIdx) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00002139 // Get the result type of the getelementptr!
Chris Lattner302116a2007-01-31 04:40:28 +00002140 const Type *Ty =
Dan Gohman12fce772008-05-15 19:50:34 +00002141 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00002142 assert(Ty && "GEP indices invalid!");
Christopher Lamb54dd24c2007-12-11 08:59:05 +00002143 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
2144 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
Chris Lattner13128ab2004-10-11 22:52:25 +00002145}
2146
Chris Lattner302116a2007-01-31 04:40:28 +00002147Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
2148 unsigned NumIdx) {
2149 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00002150}
2151
Chris Lattner302116a2007-01-31 04:40:28 +00002152
Reid Spenceree3c9912006-12-04 05:19:50 +00002153Constant *
2154ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2155 assert(LHS->getType() == RHS->getType());
2156 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
2157 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
2158
Reid Spencer266e42b2006-12-23 06:05:41 +00002159 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002160 return FC; // Fold a few common cases...
2161
2162 // Look up the constant in the table first to ensure uniqueness
2163 std::vector<Constant*> ArgVec;
2164 ArgVec.push_back(LHS);
2165 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00002166 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00002167 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Reid Spencer542964f2007-01-11 18:21:29 +00002168 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002169}
2170
2171Constant *
2172ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2173 assert(LHS->getType() == RHS->getType());
2174 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
2175
Reid Spencer266e42b2006-12-23 06:05:41 +00002176 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002177 return FC; // Fold a few common cases...
2178
2179 // Look up the constant in the table first to ensure uniqueness
2180 std::vector<Constant*> ArgVec;
2181 ArgVec.push_back(LHS);
2182 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00002183 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00002184 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Reid Spencer542964f2007-01-11 18:21:29 +00002185 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002186}
2187
Nate Begemand2195702008-05-12 19:01:56 +00002188Constant *
2189ConstantExpr::getVICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2190 assert(isa<VectorType>(LHS->getType()) &&
2191 "Tried to create vicmp operation on non-vector type!");
2192 assert(LHS->getType() == RHS->getType());
2193 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
2194 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid VICmp Predicate");
2195
Nate Begemanac7f3d92008-05-12 19:23:22 +00002196 const VectorType *VTy = cast<VectorType>(LHS->getType());
Nate Begemand2195702008-05-12 19:01:56 +00002197 const Type *EltTy = VTy->getElementType();
2198 unsigned NumElts = VTy->getNumElements();
2199
2200 SmallVector<Constant *, 8> Elts;
2201 for (unsigned i = 0; i != NumElts; ++i) {
2202 Constant *FC = ConstantFoldCompareInstruction(pred, LHS->getOperand(i),
2203 RHS->getOperand(i));
2204 if (FC) {
2205 uint64_t Val = cast<ConstantInt>(FC)->getZExtValue();
2206 if (Val != 0ULL)
2207 Elts.push_back(ConstantInt::getAllOnesValue(EltTy));
2208 else
2209 Elts.push_back(ConstantInt::get(EltTy, 0ULL));
2210 }
2211 }
2212 if (Elts.size() == NumElts)
2213 return ConstantVector::get(&Elts[0], Elts.size());
2214
2215 // Look up the constant in the table first to ensure uniqueness
2216 std::vector<Constant*> ArgVec;
2217 ArgVec.push_back(LHS);
2218 ArgVec.push_back(RHS);
2219 // Get the key type with both the opcode and predicate
2220 const ExprMapKeyType Key(Instruction::VICmp, ArgVec, pred);
2221 return ExprConstants->getOrCreate(LHS->getType(), Key);
2222}
2223
2224Constant *
2225ConstantExpr::getVFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2226 assert(isa<VectorType>(LHS->getType()) &&
2227 "Tried to create vfcmp operation on non-vector type!");
2228 assert(LHS->getType() == RHS->getType());
2229 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid VFCmp Predicate");
2230
2231 const VectorType *VTy = cast<VectorType>(LHS->getType());
2232 unsigned NumElts = VTy->getNumElements();
2233 const Type *EltTy = VTy->getElementType();
2234 const Type *REltTy = IntegerType::get(EltTy->getPrimitiveSizeInBits());
2235 const Type *ResultTy = VectorType::get(REltTy, NumElts);
2236
2237 SmallVector<Constant *, 8> Elts;
2238 for (unsigned i = 0; i != NumElts; ++i) {
2239 Constant *FC = ConstantFoldCompareInstruction(pred, LHS->getOperand(i),
2240 RHS->getOperand(i));
2241 if (FC) {
2242 uint64_t Val = cast<ConstantInt>(FC)->getZExtValue();
2243 if (Val != 0ULL)
2244 Elts.push_back(ConstantInt::getAllOnesValue(REltTy));
2245 else
2246 Elts.push_back(ConstantInt::get(REltTy, 0ULL));
2247 }
2248 }
2249 if (Elts.size() == NumElts)
2250 return ConstantVector::get(&Elts[0], Elts.size());
2251
2252 // Look up the constant in the table first to ensure uniqueness
2253 std::vector<Constant*> ArgVec;
2254 ArgVec.push_back(LHS);
2255 ArgVec.push_back(RHS);
2256 // Get the key type with both the opcode and predicate
2257 const ExprMapKeyType Key(Instruction::VFCmp, ArgVec, pred);
2258 return ExprConstants->getOrCreate(ResultTy, Key);
2259}
2260
Robert Bocchino23004482006-01-10 19:05:34 +00002261Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
2262 Constant *Idx) {
Robert Bocchinode7f1c92006-01-10 20:03:46 +00002263 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
2264 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00002265 // Look up the constant in the table first to ensure uniqueness
2266 std::vector<Constant*> ArgVec(1, Val);
2267 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00002268 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002269 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00002270}
2271
2272Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002273 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002274 "Tried to create extractelement operation on non-vector type!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00002275 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00002276 "Extractelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002277 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +00002278 Val, Idx);
2279}
Chris Lattnerb50d1352003-10-05 00:17:43 +00002280
Robert Bocchinoca27f032006-01-17 20:07:22 +00002281Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
2282 Constant *Elt, Constant *Idx) {
2283 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
2284 return FC; // Fold a few common cases...
2285 // Look up the constant in the table first to ensure uniqueness
2286 std::vector<Constant*> ArgVec(1, Val);
2287 ArgVec.push_back(Elt);
2288 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00002289 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002290 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00002291}
2292
2293Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
2294 Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002295 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002296 "Tried to create insertelement operation on non-vector type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002297 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoca27f032006-01-17 20:07:22 +00002298 && "Insertelement types must match!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00002299 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00002300 "Insertelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002301 return getInsertElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchinoca27f032006-01-17 20:07:22 +00002302 Val, Elt, Idx);
2303}
2304
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002305Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
2306 Constant *V2, Constant *Mask) {
2307 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
2308 return FC; // Fold a few common cases...
2309 // Look up the constant in the table first to ensure uniqueness
2310 std::vector<Constant*> ArgVec(1, V1);
2311 ArgVec.push_back(V2);
2312 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00002313 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002314 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002315}
2316
2317Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
2318 Constant *Mask) {
2319 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
2320 "Invalid shuffle vector constant expr operands!");
2321 return getShuffleVectorTy(V1->getType(), V1, V2, Mask);
2322}
2323
Dan Gohman12fce772008-05-15 19:50:34 +00002324Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg,
2325 Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002326 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002327 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2328 Idxs+NumIdx) == Val->getType() &&
2329 "insertvalue indices invalid!");
2330 assert(Agg->getType() == ReqTy &&
2331 "insertvalue type invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002332 assert(Agg->getType()->isFirstClassType() &&
2333 "Non-first-class type for constant InsertValue expression");
Dan Gohman3db11c22008-06-03 00:15:20 +00002334 if (Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs, NumIdx))
2335 return FC; // Fold a few common cases...
Dan Gohman12fce772008-05-15 19:50:34 +00002336 // Look up the constant in the table first to ensure uniqueness
2337 std::vector<Constant*> ArgVec;
Dan Gohman12fce772008-05-15 19:50:34 +00002338 ArgVec.push_back(Agg);
2339 ArgVec.push_back(Val);
Dan Gohman1ecaf452008-05-31 00:58:22 +00002340 SmallVector<unsigned, 4> Indices(Idxs, Idxs + NumIdx);
2341 const ExprMapKeyType Key(Instruction::InsertValue, ArgVec, 0, Indices);
Dan Gohman12fce772008-05-15 19:50:34 +00002342 return ExprConstants->getOrCreate(ReqTy, Key);
2343}
2344
2345Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002346 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002347 assert(Agg->getType()->isFirstClassType() &&
2348 "Tried to create insertelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002349
Dan Gohman0752bff2008-05-23 00:36:11 +00002350 const Type *ReqTy = Agg->getType();
2351 const Type *ValTy =
Dan Gohman12fce772008-05-15 19:50:34 +00002352 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
Dan Gohman0752bff2008-05-23 00:36:11 +00002353 assert(ValTy == Val->getType() && "insertvalue indices invalid!");
Dan Gohman12fce772008-05-15 19:50:34 +00002354 return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx);
2355}
2356
2357Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002358 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002359 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2360 Idxs+NumIdx) == ReqTy &&
2361 "extractvalue indices invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002362 assert(Agg->getType()->isFirstClassType() &&
2363 "Non-first-class type for constant extractvalue expression");
Dan Gohman3db11c22008-06-03 00:15:20 +00002364 if (Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs, NumIdx))
2365 return FC; // Fold a few common cases...
Dan Gohman12fce772008-05-15 19:50:34 +00002366 // Look up the constant in the table first to ensure uniqueness
2367 std::vector<Constant*> ArgVec;
Dan Gohman12fce772008-05-15 19:50:34 +00002368 ArgVec.push_back(Agg);
Dan Gohman7bb04502008-05-31 19:09:08 +00002369 SmallVector<unsigned, 4> Indices(Idxs, Idxs + NumIdx);
Dan Gohman1ecaf452008-05-31 00:58:22 +00002370 const ExprMapKeyType Key(Instruction::ExtractValue, ArgVec, 0, Indices);
Dan Gohman12fce772008-05-15 19:50:34 +00002371 return ExprConstants->getOrCreate(ReqTy, Key);
2372}
2373
2374Constant *ConstantExpr::getExtractValue(Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002375 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002376 assert(Agg->getType()->isFirstClassType() &&
2377 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002378
2379 const Type *ReqTy =
2380 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
2381 assert(ReqTy && "extractvalue indices invalid!");
2382 return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx);
2383}
2384
Reid Spencer2eadb532007-01-21 00:29:26 +00002385Constant *ConstantExpr::getZeroValueForNegationExpr(const Type *Ty) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002386 if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
Reid Spencer6598ca82007-01-21 02:29:10 +00002387 if (PTy->getElementType()->isFloatingPoint()) {
2388 std::vector<Constant*> zeros(PTy->getNumElements(),
Dale Johannesen98d3a082007-09-14 22:26:36 +00002389 ConstantFP::getNegativeZero(PTy->getElementType()));
Reid Spencerd84d35b2007-02-15 02:26:10 +00002390 return ConstantVector::get(PTy, zeros);
Reid Spencer6598ca82007-01-21 02:29:10 +00002391 }
Reid Spencer2eadb532007-01-21 00:29:26 +00002392
Dale Johannesen98d3a082007-09-14 22:26:36 +00002393 if (Ty->isFloatingPoint())
2394 return ConstantFP::getNegativeZero(Ty);
Reid Spencer2eadb532007-01-21 00:29:26 +00002395
2396 return Constant::getNullValue(Ty);
2397}
2398
Vikram S. Adve4c485332002-07-15 18:19:33 +00002399// destroyConstant - Remove the constant from the constant table...
2400//
2401void ConstantExpr::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00002402 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002403 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002404}
2405
Chris Lattner3cd8c562002-07-30 18:54:25 +00002406const char *ConstantExpr::getOpcodeName() const {
2407 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002408}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00002409
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002410//===----------------------------------------------------------------------===//
2411// replaceUsesOfWithOnConstant implementations
2412
Chris Lattner913849b2007-08-21 00:55:23 +00002413/// replaceUsesOfWithOnConstant - Update this constant array to change uses of
2414/// 'From' to be uses of 'To'. This must update the uniquing data structures
2415/// etc.
2416///
2417/// Note that we intentionally replace all uses of From with To here. Consider
2418/// a large array that uses 'From' 1000 times. By handling this case all here,
2419/// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that
2420/// single invocation handles all 1000 uses. Handling them one at a time would
2421/// work, but would be really slow because it would have to unique each updated
2422/// array instance.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002423void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002424 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002425 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002426 Constant *ToC = cast<Constant>(To);
Chris Lattnerdff59112005-10-04 18:47:09 +00002427
Jim Laskeyc03caef2006-07-17 17:38:29 +00002428 std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnerb64419a2005-10-03 22:51:37 +00002429 Lookup.first.first = getType();
2430 Lookup.second = this;
Chris Lattnerdff59112005-10-04 18:47:09 +00002431
Chris Lattnerb64419a2005-10-03 22:51:37 +00002432 std::vector<Constant*> &Values = Lookup.first.second;
2433 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattnerdff59112005-10-04 18:47:09 +00002434
Chris Lattner8760ec72005-10-04 01:17:50 +00002435 // Fill values with the modified operands of the constant array. Also,
2436 // compute whether this turns into an all-zeros array.
Chris Lattnerdff59112005-10-04 18:47:09 +00002437 bool isAllZeros = false;
Chris Lattner913849b2007-08-21 00:55:23 +00002438 unsigned NumUpdated = 0;
Chris Lattnerdff59112005-10-04 18:47:09 +00002439 if (!ToC->isNullValue()) {
Chris Lattner913849b2007-08-21 00:55:23 +00002440 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2441 Constant *Val = cast<Constant>(O->get());
2442 if (Val == From) {
2443 Val = ToC;
2444 ++NumUpdated;
2445 }
2446 Values.push_back(Val);
2447 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002448 } else {
2449 isAllZeros = true;
2450 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2451 Constant *Val = cast<Constant>(O->get());
Chris Lattner913849b2007-08-21 00:55:23 +00002452 if (Val == From) {
2453 Val = ToC;
2454 ++NumUpdated;
2455 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002456 Values.push_back(Val);
2457 if (isAllZeros) isAllZeros = Val->isNullValue();
2458 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002459 }
2460
Chris Lattnerb64419a2005-10-03 22:51:37 +00002461 Constant *Replacement = 0;
2462 if (isAllZeros) {
2463 Replacement = ConstantAggregateZero::get(getType());
2464 } else {
2465 // Check to see if we have this array type already.
2466 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002467 ArrayConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002468 ArrayConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002469
2470 if (Exists) {
2471 Replacement = I->second;
2472 } else {
2473 // Okay, the new shape doesn't exist in the system yet. Instead of
2474 // creating a new constant array, inserting it, replaceallusesof'ing the
2475 // old with the new, then deleting the old... just update the current one
2476 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002477 ArrayConstants->MoveConstantToNewSlot(this, I);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002478
Chris Lattner913849b2007-08-21 00:55:23 +00002479 // Update to the new value. Optimize for the case when we have a single
2480 // operand that we're changing, but handle bulk updates efficiently.
2481 if (NumUpdated == 1) {
2482 unsigned OperandToUpdate = U-OperandList;
2483 assert(getOperand(OperandToUpdate) == From &&
2484 "ReplaceAllUsesWith broken!");
2485 setOperand(OperandToUpdate, ToC);
2486 } else {
2487 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2488 if (getOperand(i) == From)
2489 setOperand(i, ToC);
2490 }
Chris Lattnerb64419a2005-10-03 22:51:37 +00002491 return;
2492 }
2493 }
2494
2495 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002496 assert(Replacement != this && "I didn't contain From!");
2497
Chris Lattner7a1450d2005-10-04 18:13:04 +00002498 // Everyone using this now uses the replacement.
2499 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002500
2501 // Delete the old constant!
2502 destroyConstant();
2503}
2504
2505void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002506 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002507 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002508 Constant *ToC = cast<Constant>(To);
2509
Chris Lattnerdff59112005-10-04 18:47:09 +00002510 unsigned OperandToUpdate = U-OperandList;
2511 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
2512
Jim Laskeyc03caef2006-07-17 17:38:29 +00002513 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattner8760ec72005-10-04 01:17:50 +00002514 Lookup.first.first = getType();
2515 Lookup.second = this;
2516 std::vector<Constant*> &Values = Lookup.first.second;
2517 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002518
Chris Lattnerdff59112005-10-04 18:47:09 +00002519
Chris Lattner8760ec72005-10-04 01:17:50 +00002520 // Fill values with the modified operands of the constant struct. Also,
2521 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00002522 bool isAllZeros = false;
2523 if (!ToC->isNullValue()) {
2524 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
2525 Values.push_back(cast<Constant>(O->get()));
2526 } else {
2527 isAllZeros = true;
2528 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2529 Constant *Val = cast<Constant>(O->get());
2530 Values.push_back(Val);
2531 if (isAllZeros) isAllZeros = Val->isNullValue();
2532 }
Chris Lattner8760ec72005-10-04 01:17:50 +00002533 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002534 Values[OperandToUpdate] = ToC;
2535
Chris Lattner8760ec72005-10-04 01:17:50 +00002536 Constant *Replacement = 0;
2537 if (isAllZeros) {
2538 Replacement = ConstantAggregateZero::get(getType());
2539 } else {
2540 // Check to see if we have this array type already.
2541 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002542 StructConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002543 StructConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00002544
2545 if (Exists) {
2546 Replacement = I->second;
2547 } else {
2548 // Okay, the new shape doesn't exist in the system yet. Instead of
2549 // creating a new constant struct, inserting it, replaceallusesof'ing the
2550 // old with the new, then deleting the old... just update the current one
2551 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002552 StructConstants->MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00002553
Chris Lattnerdff59112005-10-04 18:47:09 +00002554 // Update to the new value.
2555 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00002556 return;
2557 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002558 }
2559
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002560 assert(Replacement != this && "I didn't contain From!");
2561
Chris Lattner7a1450d2005-10-04 18:13:04 +00002562 // Everyone using this now uses the replacement.
2563 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002564
2565 // Delete the old constant!
2566 destroyConstant();
2567}
2568
Reid Spencerd84d35b2007-02-15 02:26:10 +00002569void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002570 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002571 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2572
2573 std::vector<Constant*> Values;
2574 Values.reserve(getNumOperands()); // Build replacement array...
2575 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2576 Constant *Val = getOperand(i);
2577 if (Val == From) Val = cast<Constant>(To);
2578 Values.push_back(Val);
2579 }
2580
Reid Spencerd84d35b2007-02-15 02:26:10 +00002581 Constant *Replacement = ConstantVector::get(getType(), Values);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002582 assert(Replacement != this && "I didn't contain From!");
2583
Chris Lattner7a1450d2005-10-04 18:13:04 +00002584 // Everyone using this now uses the replacement.
2585 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002586
2587 // Delete the old constant!
2588 destroyConstant();
2589}
2590
2591void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002592 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002593 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2594 Constant *To = cast<Constant>(ToV);
2595
2596 Constant *Replacement = 0;
2597 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerb5d70302007-02-19 20:01:23 +00002598 SmallVector<Constant*, 8> Indices;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002599 Constant *Pointer = getOperand(0);
2600 Indices.reserve(getNumOperands()-1);
2601 if (Pointer == From) Pointer = To;
2602
2603 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2604 Constant *Val = getOperand(i);
2605 if (Val == From) Val = To;
2606 Indices.push_back(Val);
2607 }
Chris Lattnerb5d70302007-02-19 20:01:23 +00002608 Replacement = ConstantExpr::getGetElementPtr(Pointer,
2609 &Indices[0], Indices.size());
Dan Gohman12fce772008-05-15 19:50:34 +00002610 } else if (getOpcode() == Instruction::ExtractValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002611 Constant *Agg = getOperand(0);
Dan Gohman12fce772008-05-15 19:50:34 +00002612 if (Agg == From) Agg = To;
2613
Dan Gohman1ecaf452008-05-31 00:58:22 +00002614 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002615 Replacement = ConstantExpr::getExtractValue(Agg,
2616 &Indices[0], Indices.size());
2617 } else if (getOpcode() == Instruction::InsertValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002618 Constant *Agg = getOperand(0);
2619 Constant *Val = getOperand(1);
Dan Gohman12fce772008-05-15 19:50:34 +00002620 if (Agg == From) Agg = To;
2621 if (Val == From) Val = 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::getInsertValue(Agg, Val,
2625 &Indices[0], Indices.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002626 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002627 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002628 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002629 } else if (getOpcode() == Instruction::Select) {
2630 Constant *C1 = getOperand(0);
2631 Constant *C2 = getOperand(1);
2632 Constant *C3 = getOperand(2);
2633 if (C1 == From) C1 = To;
2634 if (C2 == From) C2 = To;
2635 if (C3 == From) C3 = To;
2636 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00002637 } else if (getOpcode() == Instruction::ExtractElement) {
2638 Constant *C1 = getOperand(0);
2639 Constant *C2 = getOperand(1);
2640 if (C1 == From) C1 = To;
2641 if (C2 == From) C2 = To;
2642 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00002643 } else if (getOpcode() == Instruction::InsertElement) {
2644 Constant *C1 = getOperand(0);
2645 Constant *C2 = getOperand(1);
2646 Constant *C3 = getOperand(1);
2647 if (C1 == From) C1 = To;
2648 if (C2 == From) C2 = To;
2649 if (C3 == From) C3 = To;
2650 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2651 } else if (getOpcode() == Instruction::ShuffleVector) {
2652 Constant *C1 = getOperand(0);
2653 Constant *C2 = getOperand(1);
2654 Constant *C3 = getOperand(2);
2655 if (C1 == From) C1 = To;
2656 if (C2 == From) C2 = To;
2657 if (C3 == From) C3 = To;
2658 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00002659 } else if (isCompare()) {
2660 Constant *C1 = getOperand(0);
2661 Constant *C2 = getOperand(1);
2662 if (C1 == From) C1 = To;
2663 if (C2 == From) C2 = To;
2664 if (getOpcode() == Instruction::ICmp)
2665 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
2666 else
2667 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002668 } else if (getNumOperands() == 2) {
2669 Constant *C1 = getOperand(0);
2670 Constant *C2 = getOperand(1);
2671 if (C1 == From) C1 = To;
2672 if (C2 == From) C2 = To;
2673 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2674 } else {
2675 assert(0 && "Unknown ConstantExpr type!");
2676 return;
2677 }
2678
2679 assert(Replacement != this && "I didn't contain From!");
2680
Chris Lattner7a1450d2005-10-04 18:13:04 +00002681 // Everyone using this now uses the replacement.
2682 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002683
2684 // Delete the old constant!
2685 destroyConstant();
Matthijs Kooijmanba5d7ef2008-07-03 07:46:41 +00002686}