blob: bb8fa655565e184b95f1073be697ad6f5f249edc [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.
Chris Lattnerc5098a22008-07-14 05:10:41 +0000160/// This handles breaking down a vector undef into undef elements, etc. For
161/// constant exprs and other cases we can't handle, we return an empty vector.
Chris Lattner2105d662008-07-10 00:28:11 +0000162void Constant::getVectorElements(SmallVectorImpl<Constant*> &Elts) const {
163 assert(isa<VectorType>(getType()) && "Not a vector constant!");
164
165 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) {
166 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i)
167 Elts.push_back(CV->getOperand(i));
168 return;
169 }
170
171 const VectorType *VT = cast<VectorType>(getType());
172 if (isa<ConstantAggregateZero>(this)) {
173 Elts.assign(VT->getNumElements(),
174 Constant::getNullValue(VT->getElementType()));
175 return;
176 }
177
Chris Lattnerc5098a22008-07-14 05:10:41 +0000178 if (isa<UndefValue>(this)) {
179 Elts.assign(VT->getNumElements(), UndefValue::get(VT->getElementType()));
180 return;
181 }
182
183 // Unknown type, must be constant expr etc.
Chris Lattner2105d662008-07-10 00:28:11 +0000184}
185
186
187
Chris Lattner2f7c9632001-06-06 20:29:01 +0000188//===----------------------------------------------------------------------===//
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000189// ConstantInt
Chris Lattner2f7c9632001-06-06 20:29:01 +0000190//===----------------------------------------------------------------------===//
191
Reid Spencerb31bffe2007-02-26 23:54:03 +0000192ConstantInt::ConstantInt(const IntegerType *Ty, const APInt& V)
Chris Lattner5db2f472007-02-20 05:55:46 +0000193 : Constant(Ty, ConstantIntVal, 0, 0), Val(V) {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000194 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000195}
196
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000197ConstantInt *ConstantInt::TheTrueVal = 0;
198ConstantInt *ConstantInt::TheFalseVal = 0;
199
200namespace llvm {
201 void CleanupTrueFalse(void *) {
202 ConstantInt::ResetTrueFalse();
203 }
204}
205
206static ManagedCleanup<llvm::CleanupTrueFalse> TrueFalseCleanup;
207
208ConstantInt *ConstantInt::CreateTrueFalseVals(bool WhichOne) {
209 assert(TheTrueVal == 0 && TheFalseVal == 0);
210 TheTrueVal = get(Type::Int1Ty, 1);
211 TheFalseVal = get(Type::Int1Ty, 0);
212
213 // Ensure that llvm_shutdown nulls out TheTrueVal/TheFalseVal.
214 TrueFalseCleanup.Register();
215
216 return WhichOne ? TheTrueVal : TheFalseVal;
217}
218
219
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000220namespace {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000221 struct DenseMapAPIntKeyInfo {
222 struct KeyTy {
223 APInt val;
224 const Type* type;
225 KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
226 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
227 bool operator==(const KeyTy& that) const {
228 return type == that.type && this->val == that.val;
229 }
230 bool operator!=(const KeyTy& that) const {
231 return !this->operator==(that);
232 }
233 };
234 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
235 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000236 static unsigned getHashValue(const KeyTy &Key) {
Chris Lattner0625bd62007-09-17 18:34:04 +0000237 return DenseMapInfo<void*>::getHashValue(Key.type) ^
Reid Spencerb31bffe2007-02-26 23:54:03 +0000238 Key.val.getHashValue();
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000239 }
Chris Lattner0625bd62007-09-17 18:34:04 +0000240 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
241 return LHS == RHS;
242 }
Dale Johannesena719a602007-08-24 00:56:33 +0000243 static bool isPod() { return false; }
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000244 };
245}
246
247
Reid Spencerb31bffe2007-02-26 23:54:03 +0000248typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
249 DenseMapAPIntKeyInfo> IntMapTy;
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000250static ManagedStatic<IntMapTy> IntConstants;
251
Reid Spencer362fb292007-03-19 20:39:08 +0000252ConstantInt *ConstantInt::get(const Type *Ty, uint64_t V, bool isSigned) {
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000253 const IntegerType *ITy = cast<IntegerType>(Ty);
Reid Spencer362fb292007-03-19 20:39:08 +0000254 return get(APInt(ITy->getBitWidth(), V, isSigned));
Reid Spencerb31bffe2007-02-26 23:54:03 +0000255}
256
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000257// Get a ConstantInt from an APInt. Note that the value stored in the DenseMap
Dan Gohmanb3efe032008-02-07 02:30:40 +0000258// as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the
Reid Spencerb31bffe2007-02-26 23:54:03 +0000259// operator== and operator!= to ensure that the DenseMap doesn't attempt to
260// compare APInt's of different widths, which would violate an APInt class
261// invariant which generates an assertion.
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000262ConstantInt *ConstantInt::get(const APInt& V) {
263 // Get the corresponding integer type for the bit width of the value.
264 const IntegerType *ITy = IntegerType::get(V.getBitWidth());
Reid Spencerb31bffe2007-02-26 23:54:03 +0000265 // get an existing value or the insertion position
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000266 DenseMapAPIntKeyInfo::KeyTy Key(V, ITy);
Reid Spencerb31bffe2007-02-26 23:54:03 +0000267 ConstantInt *&Slot = (*IntConstants)[Key];
268 // if it exists, return it.
269 if (Slot)
270 return Slot;
271 // otherwise create a new one, insert it, and return it.
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000272 return Slot = new ConstantInt(ITy, V);
273}
274
275//===----------------------------------------------------------------------===//
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000276// ConstantFP
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000277//===----------------------------------------------------------------------===//
278
Chris Lattner98bd9392008-04-09 06:38:30 +0000279static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
280 if (Ty == Type::FloatTy)
281 return &APFloat::IEEEsingle;
282 if (Ty == Type::DoubleTy)
283 return &APFloat::IEEEdouble;
284 if (Ty == Type::X86_FP80Ty)
285 return &APFloat::x87DoubleExtended;
286 else if (Ty == Type::FP128Ty)
287 return &APFloat::IEEEquad;
288
289 assert(Ty == Type::PPC_FP128Ty && "Unknown FP format");
290 return &APFloat::PPCDoubleDouble;
291}
292
Dale Johannesend246b2c2007-08-30 00:23:21 +0000293ConstantFP::ConstantFP(const Type *Ty, const APFloat& V)
294 : Constant(Ty, ConstantFPVal, 0, 0), Val(V) {
Chris Lattner98bd9392008-04-09 06:38:30 +0000295 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
296 "FP type Mismatch");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000297}
298
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000299bool ConstantFP::isNullValue() const {
Dale Johannesena719a602007-08-24 00:56:33 +0000300 return Val.isZero() && !Val.isNegative();
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000301}
302
Dale Johannesen98d3a082007-09-14 22:26:36 +0000303ConstantFP *ConstantFP::getNegativeZero(const Type *Ty) {
304 APFloat apf = cast <ConstantFP>(Constant::getNullValue(Ty))->getValueAPF();
305 apf.changeSign();
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000306 return ConstantFP::get(apf);
Dale Johannesen98d3a082007-09-14 22:26:36 +0000307}
308
Dale Johannesend246b2c2007-08-30 00:23:21 +0000309bool ConstantFP::isExactlyValue(const APFloat& V) const {
310 return Val.bitwiseIsEqual(V);
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000311}
312
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000313namespace {
Dale Johannesena719a602007-08-24 00:56:33 +0000314 struct DenseMapAPFloatKeyInfo {
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000315 struct KeyTy {
316 APFloat val;
317 KeyTy(const APFloat& V) : val(V){}
318 KeyTy(const KeyTy& that) : val(that.val) {}
319 bool operator==(const KeyTy& that) const {
320 return this->val.bitwiseIsEqual(that.val);
321 }
322 bool operator!=(const KeyTy& that) const {
323 return !this->operator==(that);
324 }
325 };
326 static inline KeyTy getEmptyKey() {
327 return KeyTy(APFloat(APFloat::Bogus,1));
Reid Spencerb31bffe2007-02-26 23:54:03 +0000328 }
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000329 static inline KeyTy getTombstoneKey() {
330 return KeyTy(APFloat(APFloat::Bogus,2));
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000331 }
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000332 static unsigned getHashValue(const KeyTy &Key) {
333 return Key.val.getHashValue();
Dale Johannesena719a602007-08-24 00:56:33 +0000334 }
Chris Lattner0625bd62007-09-17 18:34:04 +0000335 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
336 return LHS == RHS;
337 }
Dale Johannesena719a602007-08-24 00:56:33 +0000338 static bool isPod() { return false; }
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000339 };
340}
341
342//---- ConstantFP::get() implementation...
343//
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000344typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
Dale Johannesena719a602007-08-24 00:56:33 +0000345 DenseMapAPFloatKeyInfo> FPMapTy;
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000346
Dale Johannesena719a602007-08-24 00:56:33 +0000347static ManagedStatic<FPMapTy> FPConstants;
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000348
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000349ConstantFP *ConstantFP::get(const APFloat &V) {
Dale Johannesend246b2c2007-08-30 00:23:21 +0000350 DenseMapAPFloatKeyInfo::KeyTy Key(V);
351 ConstantFP *&Slot = (*FPConstants)[Key];
352 if (Slot) return Slot;
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000353
354 const Type *Ty;
355 if (&V.getSemantics() == &APFloat::IEEEsingle)
356 Ty = Type::FloatTy;
357 else if (&V.getSemantics() == &APFloat::IEEEdouble)
358 Ty = Type::DoubleTy;
359 else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
360 Ty = Type::X86_FP80Ty;
361 else if (&V.getSemantics() == &APFloat::IEEEquad)
362 Ty = Type::FP128Ty;
363 else {
364 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble&&"Unknown FP format");
365 Ty = Type::PPC_FP128Ty;
366 }
367
Dale Johannesend246b2c2007-08-30 00:23:21 +0000368 return Slot = new ConstantFP(Ty, V);
369}
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000370
Chris Lattner98bd9392008-04-09 06:38:30 +0000371/// get() - This returns a constant fp for the specified value in the
372/// specified type. This should only be used for simple constant values like
373/// 2.0/1.0 etc, that are known-valid both as double and as the target format.
374ConstantFP *ConstantFP::get(const Type *Ty, double V) {
375 APFloat FV(V);
376 FV.convert(*TypeToFloatSemantics(Ty), APFloat::rmNearestTiesToEven);
377 return get(FV);
378}
379
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000380//===----------------------------------------------------------------------===//
381// ConstantXXX Classes
382//===----------------------------------------------------------------------===//
383
384
Chris Lattner3462ae32001-12-03 22:26:30 +0000385ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000386 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000387 : Constant(T, ConstantArrayVal,
388 OperandTraits<ConstantArray>::op_end(this) - V.size(),
389 V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000390 assert(V.size() == T->getNumElements() &&
391 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000392 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000393 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
394 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000395 Constant *C = *I;
396 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000397 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000398 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000399 "Initializer for array element doesn't match array element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000400 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000401 }
402}
403
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000404
Chris Lattner3462ae32001-12-03 22:26:30 +0000405ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000406 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000407 : Constant(T, ConstantStructVal,
408 OperandTraits<ConstantStruct>::op_end(this) - V.size(),
409 V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000410 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000411 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000412 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000413 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
414 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000415 Constant *C = *I;
416 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000417 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000418 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000419 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000420 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000421 "Initializer for struct element doesn't match struct element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000422 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000423 }
424}
425
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000426
Reid Spencerd84d35b2007-02-15 02:26:10 +0000427ConstantVector::ConstantVector(const VectorType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000428 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000429 : Constant(T, ConstantVectorVal,
430 OperandTraits<ConstantVector>::op_end(this) - V.size(),
431 V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000432 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000433 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
434 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000435 Constant *C = *I;
436 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000437 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000438 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Dan Gohman30978072007-05-24 14:36:04 +0000439 "Initializer for vector element doesn't match vector element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000440 *OL = C;
Brian Gaeke02209042004-08-20 06:00:58 +0000441 }
442}
443
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000444
Gabor Greiff6caff662008-05-10 08:32:32 +0000445namespace llvm {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000446// We declare several classes private to this file, so use an anonymous
447// namespace
448namespace {
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000449
Gordon Henriksen14a55692007-12-10 02:14:30 +0000450/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
451/// behind the scenes to implement unary constant exprs.
452class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000453 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000454public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000455 // allocate space for exactly one operand
456 void *operator new(size_t s) {
457 return User::operator new(s, 1);
458 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000459 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
Gabor Greiff6caff662008-05-10 08:32:32 +0000460 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
461 Op<0>() = C;
462 }
463 /// Transparently provide more efficient getOperand methods.
464 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000465};
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000466
Gordon Henriksen14a55692007-12-10 02:14:30 +0000467/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
468/// behind the scenes to implement binary constant exprs.
469class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000470 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000471public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000472 // allocate space for exactly two operands
473 void *operator new(size_t s) {
474 return User::operator new(s, 2);
475 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000476 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Gabor Greiff6caff662008-05-10 08:32:32 +0000477 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000478 Op<0>() = C1;
479 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000480 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000481 /// Transparently provide more efficient getOperand methods.
482 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000483};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000484
Gordon Henriksen14a55692007-12-10 02:14:30 +0000485/// SelectConstantExpr - This class is private to Constants.cpp, and is used
486/// behind the scenes to implement select constant exprs.
487class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000488 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000489public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000490 // allocate space for exactly three operands
491 void *operator new(size_t s) {
492 return User::operator new(s, 3);
493 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000494 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Gabor Greiff6caff662008-05-10 08:32:32 +0000495 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000496 Op<0>() = C1;
497 Op<1>() = C2;
498 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000499 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000500 /// Transparently provide more efficient getOperand methods.
501 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000502};
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000503
Gordon Henriksen14a55692007-12-10 02:14:30 +0000504/// ExtractElementConstantExpr - This class is private to
505/// Constants.cpp, and is used behind the scenes to implement
506/// extractelement constant exprs.
507class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000508 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000509public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000510 // allocate space for exactly two operands
511 void *operator new(size_t s) {
512 return User::operator new(s, 2);
513 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000514 ExtractElementConstantExpr(Constant *C1, Constant *C2)
515 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000516 Instruction::ExtractElement, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000517 Op<0>() = C1;
518 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000519 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000520 /// Transparently provide more efficient getOperand methods.
521 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000522};
Robert Bocchino23004482006-01-10 19:05:34 +0000523
Gordon Henriksen14a55692007-12-10 02:14:30 +0000524/// InsertElementConstantExpr - This class is private to
525/// Constants.cpp, and is used behind the scenes to implement
526/// insertelement constant exprs.
527class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000528 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000529public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000530 // allocate space for exactly three operands
531 void *operator new(size_t s) {
532 return User::operator new(s, 3);
533 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000534 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
535 : ConstantExpr(C1->getType(), Instruction::InsertElement,
Gabor Greiff6caff662008-05-10 08:32:32 +0000536 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000537 Op<0>() = C1;
538 Op<1>() = C2;
539 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000540 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000541 /// Transparently provide more efficient getOperand methods.
542 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000543};
Robert Bocchinoca27f032006-01-17 20:07:22 +0000544
Gordon Henriksen14a55692007-12-10 02:14:30 +0000545/// ShuffleVectorConstantExpr - This class is private to
546/// Constants.cpp, and is used behind the scenes to implement
547/// shufflevector constant exprs.
548class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000549 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000550public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000551 // allocate space for exactly three operands
552 void *operator new(size_t s) {
553 return User::operator new(s, 3);
554 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000555 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
556 : ConstantExpr(C1->getType(), Instruction::ShuffleVector,
Gabor Greiff6caff662008-05-10 08:32:32 +0000557 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000558 Op<0>() = C1;
559 Op<1>() = C2;
560 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000561 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000562 /// Transparently provide more efficient getOperand methods.
563 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000564};
565
Dan Gohman12fce772008-05-15 19:50:34 +0000566/// ExtractValueConstantExpr - This class is private to
567/// Constants.cpp, and is used behind the scenes to implement
568/// extractvalue constant exprs.
569class VISIBILITY_HIDDEN ExtractValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000570 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000571public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000572 // allocate space for exactly one operand
573 void *operator new(size_t s) {
574 return User::operator new(s, 1);
Dan Gohman12fce772008-05-15 19:50:34 +0000575 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000576 ExtractValueConstantExpr(Constant *Agg,
577 const SmallVector<unsigned, 4> &IdxList,
578 const Type *DestTy)
579 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
580 Indices(IdxList) {
581 Op<0>() = Agg;
582 }
583
Dan Gohman7bb04502008-05-31 19:09:08 +0000584 /// Indices - These identify which value to extract.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000585 const SmallVector<unsigned, 4> Indices;
586
Dan Gohman12fce772008-05-15 19:50:34 +0000587 /// Transparently provide more efficient getOperand methods.
588 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
589};
590
591/// InsertValueConstantExpr - This class is private to
592/// Constants.cpp, and is used behind the scenes to implement
593/// insertvalue constant exprs.
594class VISIBILITY_HIDDEN InsertValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000595 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000596public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000597 // allocate space for exactly one operand
598 void *operator new(size_t s) {
599 return User::operator new(s, 2);
Dan Gohman12fce772008-05-15 19:50:34 +0000600 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000601 InsertValueConstantExpr(Constant *Agg, Constant *Val,
602 const SmallVector<unsigned, 4> &IdxList,
603 const Type *DestTy)
604 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
605 Indices(IdxList) {
606 Op<0>() = Agg;
607 Op<1>() = Val;
608 }
609
Dan Gohman7bb04502008-05-31 19:09:08 +0000610 /// Indices - These identify the position for the insertion.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000611 const SmallVector<unsigned, 4> Indices;
612
Dan Gohman12fce772008-05-15 19:50:34 +0000613 /// Transparently provide more efficient getOperand methods.
614 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
615};
616
617
Gordon Henriksen14a55692007-12-10 02:14:30 +0000618/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
619/// used behind the scenes to implement getelementpr constant exprs.
Gabor Greife9ecc682008-04-06 20:25:17 +0000620class VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000621 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000622 const Type *DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000623public:
Gabor Greif697e94c2008-05-15 10:04:30 +0000624 static GetElementPtrConstantExpr *Create(Constant *C,
625 const std::vector<Constant*>&IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000626 const Type *DestTy) {
Gabor Greif697e94c2008-05-15 10:04:30 +0000627 return new(IdxList.size() + 1)
628 GetElementPtrConstantExpr(C, IdxList, DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000629 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000630 /// Transparently provide more efficient getOperand methods.
631 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000632};
633
634// CompareConstantExpr - This class is private to Constants.cpp, and is used
635// behind the scenes to implement ICmp and FCmp constant expressions. This is
636// needed in order to store the predicate value for these instructions.
637struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000638 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
639 // allocate space for exactly two operands
640 void *operator new(size_t s) {
641 return User::operator new(s, 2);
642 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000643 unsigned short predicate;
Nate Begemand2195702008-05-12 19:01:56 +0000644 CompareConstantExpr(const Type *ty, Instruction::OtherOps opc,
645 unsigned short pred, Constant* LHS, Constant* RHS)
646 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000647 Op<0>() = LHS;
648 Op<1>() = RHS;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000649 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000650 /// Transparently provide more efficient getOperand methods.
651 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000652};
653
654} // end anonymous namespace
655
Gabor Greiff6caff662008-05-10 08:32:32 +0000656template <>
657struct OperandTraits<UnaryConstantExpr> : FixedNumOperandTraits<1> {
658};
659DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
660
661template <>
662struct OperandTraits<BinaryConstantExpr> : FixedNumOperandTraits<2> {
663};
664DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
665
666template <>
667struct OperandTraits<SelectConstantExpr> : FixedNumOperandTraits<3> {
668};
669DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
670
671template <>
672struct OperandTraits<ExtractElementConstantExpr> : FixedNumOperandTraits<2> {
673};
674DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
675
676template <>
677struct OperandTraits<InsertElementConstantExpr> : FixedNumOperandTraits<3> {
678};
679DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
680
681template <>
682struct OperandTraits<ShuffleVectorConstantExpr> : FixedNumOperandTraits<3> {
683};
684DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
685
Dan Gohman12fce772008-05-15 19:50:34 +0000686template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000687struct OperandTraits<ExtractValueConstantExpr> : FixedNumOperandTraits<1> {
Dan Gohman12fce772008-05-15 19:50:34 +0000688};
Dan Gohman12fce772008-05-15 19:50:34 +0000689DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
690
691template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000692struct OperandTraits<InsertValueConstantExpr> : FixedNumOperandTraits<2> {
Dan Gohman12fce772008-05-15 19:50:34 +0000693};
Dan Gohman12fce772008-05-15 19:50:34 +0000694DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
695
Gabor Greiff6caff662008-05-10 08:32:32 +0000696template <>
697struct OperandTraits<GetElementPtrConstantExpr> : VariadicOperandTraits<1> {
698};
699
700GetElementPtrConstantExpr::GetElementPtrConstantExpr
701 (Constant *C,
702 const std::vector<Constant*> &IdxList,
703 const Type *DestTy)
704 : ConstantExpr(DestTy, Instruction::GetElementPtr,
705 OperandTraits<GetElementPtrConstantExpr>::op_end(this)
706 - (IdxList.size()+1),
707 IdxList.size()+1) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000708 OperandList[0] = C;
Gabor Greiff6caff662008-05-10 08:32:32 +0000709 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000710 OperandList[i+1] = IdxList[i];
Gabor Greiff6caff662008-05-10 08:32:32 +0000711}
712
713DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
714
715
716template <>
717struct OperandTraits<CompareConstantExpr> : FixedNumOperandTraits<2> {
718};
719DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
720
721
722} // End llvm namespace
723
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000724
725// Utility function for determining if a ConstantExpr is a CastOp or not. This
726// can't be inline because we don't want to #include Instruction.h into
727// Constant.h
728bool ConstantExpr::isCast() const {
729 return Instruction::isCast(getOpcode());
730}
731
Reid Spenceree3c9912006-12-04 05:19:50 +0000732bool ConstantExpr::isCompare() const {
Chris Lattnereab49262008-07-14 05:17:31 +0000733 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp ||
734 getOpcode() == Instruction::VICmp || getOpcode() == Instruction::VFCmp;
Reid Spenceree3c9912006-12-04 05:19:50 +0000735}
736
Dan Gohman1ecaf452008-05-31 00:58:22 +0000737bool ConstantExpr::hasIndices() const {
738 return getOpcode() == Instruction::ExtractValue ||
739 getOpcode() == Instruction::InsertValue;
740}
741
742const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const {
743 if (const ExtractValueConstantExpr *EVCE =
744 dyn_cast<ExtractValueConstantExpr>(this))
745 return EVCE->Indices;
Dan Gohmana469bdb2008-06-23 16:39:44 +0000746
747 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman1ecaf452008-05-31 00:58:22 +0000748}
749
Chris Lattner817175f2004-03-29 02:37:53 +0000750/// ConstantExpr::get* - Return some common constants without having to
751/// specify the full Instruction::OPCODE identifier.
752///
753Constant *ConstantExpr::getNeg(Constant *C) {
Reid Spencer2eadb532007-01-21 00:29:26 +0000754 return get(Instruction::Sub,
755 ConstantExpr::getZeroValueForNegationExpr(C->getType()),
756 C);
Chris Lattner817175f2004-03-29 02:37:53 +0000757}
758Constant *ConstantExpr::getNot(Constant *C) {
Gordon Henriksen7ce31762007-10-06 14:29:36 +0000759 assert(isa<IntegerType>(C->getType()) && "Cannot NOT a nonintegral value!");
Chris Lattner817175f2004-03-29 02:37:53 +0000760 return get(Instruction::Xor, C,
Zhou Sheng75b871f2007-01-11 12:24:14 +0000761 ConstantInt::getAllOnesValue(C->getType()));
Chris Lattner817175f2004-03-29 02:37:53 +0000762}
763Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
764 return get(Instruction::Add, C1, C2);
765}
766Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
767 return get(Instruction::Sub, C1, C2);
768}
769Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
770 return get(Instruction::Mul, C1, C2);
771}
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000772Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2) {
773 return get(Instruction::UDiv, C1, C2);
774}
775Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2) {
776 return get(Instruction::SDiv, C1, C2);
777}
778Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) {
779 return get(Instruction::FDiv, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000780}
Reid Spencer7eb55b32006-11-02 01:53:59 +0000781Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) {
782 return get(Instruction::URem, C1, C2);
783}
784Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) {
785 return get(Instruction::SRem, C1, C2);
786}
787Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) {
788 return get(Instruction::FRem, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000789}
790Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
791 return get(Instruction::And, C1, C2);
792}
793Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
794 return get(Instruction::Or, C1, C2);
795}
796Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
797 return get(Instruction::Xor, C1, C2);
798}
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000799unsigned ConstantExpr::getPredicate() const {
Nate Begemand2195702008-05-12 19:01:56 +0000800 assert(getOpcode() == Instruction::FCmp ||
801 getOpcode() == Instruction::ICmp ||
802 getOpcode() == Instruction::VFCmp ||
803 getOpcode() == Instruction::VICmp);
Chris Lattneref650092007-10-18 16:26:24 +0000804 return ((const CompareConstantExpr*)this)->predicate;
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000805}
Chris Lattner817175f2004-03-29 02:37:53 +0000806Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
807 return get(Instruction::Shl, C1, C2);
808}
Reid Spencerfdff9382006-11-08 06:47:33 +0000809Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2) {
810 return get(Instruction::LShr, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000811}
Reid Spencerfdff9382006-11-08 06:47:33 +0000812Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2) {
813 return get(Instruction::AShr, C1, C2);
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000814}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000815
Chris Lattner7c1018a2006-07-14 19:37:40 +0000816/// getWithOperandReplaced - Return a constant expression identical to this
817/// one, but with the specified operand set to the specified value.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000818Constant *
819ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +0000820 assert(OpNo < getNumOperands() && "Operand num is out of range!");
821 assert(Op->getType() == getOperand(OpNo)->getType() &&
822 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000823 if (getOperand(OpNo) == Op)
824 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000825
Chris Lattner227816342006-07-14 22:20:01 +0000826 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000827 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000828 case Instruction::Trunc:
829 case Instruction::ZExt:
830 case Instruction::SExt:
831 case Instruction::FPTrunc:
832 case Instruction::FPExt:
833 case Instruction::UIToFP:
834 case Instruction::SIToFP:
835 case Instruction::FPToUI:
836 case Instruction::FPToSI:
837 case Instruction::PtrToInt:
838 case Instruction::IntToPtr:
839 case Instruction::BitCast:
840 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000841 case Instruction::Select:
842 Op0 = (OpNo == 0) ? Op : getOperand(0);
843 Op1 = (OpNo == 1) ? Op : getOperand(1);
844 Op2 = (OpNo == 2) ? Op : getOperand(2);
845 return ConstantExpr::getSelect(Op0, Op1, Op2);
846 case Instruction::InsertElement:
847 Op0 = (OpNo == 0) ? Op : getOperand(0);
848 Op1 = (OpNo == 1) ? Op : getOperand(1);
849 Op2 = (OpNo == 2) ? Op : getOperand(2);
850 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
851 case Instruction::ExtractElement:
852 Op0 = (OpNo == 0) ? Op : getOperand(0);
853 Op1 = (OpNo == 1) ? Op : getOperand(1);
854 return ConstantExpr::getExtractElement(Op0, Op1);
855 case Instruction::ShuffleVector:
856 Op0 = (OpNo == 0) ? Op : getOperand(0);
857 Op1 = (OpNo == 1) ? Op : getOperand(1);
858 Op2 = (OpNo == 2) ? Op : getOperand(2);
859 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000860 case Instruction::GetElementPtr: {
Chris Lattnerb5d70302007-02-19 20:01:23 +0000861 SmallVector<Constant*, 8> Ops;
Dan Gohman12fce772008-05-15 19:50:34 +0000862 Ops.resize(getNumOperands()-1);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000863 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Dan Gohman12fce772008-05-15 19:50:34 +0000864 Ops[i-1] = getOperand(i);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000865 if (OpNo == 0)
Chris Lattnerb5d70302007-02-19 20:01:23 +0000866 return ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000867 Ops[OpNo-1] = Op;
Chris Lattnerb5d70302007-02-19 20:01:23 +0000868 return ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000869 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000870 default:
871 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000872 Op0 = (OpNo == 0) ? Op : getOperand(0);
873 Op1 = (OpNo == 1) ? Op : getOperand(1);
874 return ConstantExpr::get(getOpcode(), Op0, Op1);
875 }
876}
877
878/// getWithOperands - This returns the current constant expression with the
879/// operands replaced with the specified values. The specified operands must
880/// match count and type with the existing ones.
881Constant *ConstantExpr::
882getWithOperands(const std::vector<Constant*> &Ops) const {
883 assert(Ops.size() == getNumOperands() && "Operand count mismatch!");
884 bool AnyChange = false;
885 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
886 assert(Ops[i]->getType() == getOperand(i)->getType() &&
887 "Operand type mismatch!");
888 AnyChange |= Ops[i] != getOperand(i);
889 }
890 if (!AnyChange) // No operands changed, return self.
891 return const_cast<ConstantExpr*>(this);
892
893 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000894 case Instruction::Trunc:
895 case Instruction::ZExt:
896 case Instruction::SExt:
897 case Instruction::FPTrunc:
898 case Instruction::FPExt:
899 case Instruction::UIToFP:
900 case Instruction::SIToFP:
901 case Instruction::FPToUI:
902 case Instruction::FPToSI:
903 case Instruction::PtrToInt:
904 case Instruction::IntToPtr:
905 case Instruction::BitCast:
906 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattner227816342006-07-14 22:20:01 +0000907 case Instruction::Select:
908 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
909 case Instruction::InsertElement:
910 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
911 case Instruction::ExtractElement:
912 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
913 case Instruction::ShuffleVector:
914 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Chris Lattnerb5d70302007-02-19 20:01:23 +0000915 case Instruction::GetElementPtr:
916 return ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], Ops.size()-1);
Reid Spencer266e42b2006-12-23 06:05:41 +0000917 case Instruction::ICmp:
918 case Instruction::FCmp:
Nate Begeman098cc6f2008-07-25 17:56:27 +0000919 case Instruction::VICmp:
920 case Instruction::VFCmp:
Reid Spencer266e42b2006-12-23 06:05:41 +0000921 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattner227816342006-07-14 22:20:01 +0000922 default:
923 assert(getNumOperands() == 2 && "Must be binary operator?");
924 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000925 }
926}
927
Chris Lattner2f7c9632001-06-06 20:29:01 +0000928
929//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000930// isValueValidForType implementations
931
Reid Spencere7334722006-12-19 01:28:19 +0000932bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000933 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000934 if (Ty == Type::Int1Ty)
935 return Val == 0 || Val == 1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000936 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000937 return true; // always true, has to fit in largest type
938 uint64_t Max = (1ll << NumBits) - 1;
939 return Val <= Max;
Reid Spencere7334722006-12-19 01:28:19 +0000940}
941
Reid Spencere0fc4df2006-10-20 07:07:24 +0000942bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000943 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000944 if (Ty == Type::Int1Ty)
Reid Spencera94d3942007-01-19 21:13:56 +0000945 return Val == 0 || Val == 1 || Val == -1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000946 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000947 return true; // always true, has to fit in largest type
948 int64_t Min = -(1ll << (NumBits-1));
949 int64_t Max = (1ll << (NumBits-1)) - 1;
950 return (Val >= Min && Val <= Max);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000951}
952
Dale Johannesend246b2c2007-08-30 00:23:21 +0000953bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) {
954 // convert modifies in place, so make a copy.
955 APFloat Val2 = APFloat(Val);
Chris Lattner6b727592004-06-17 18:19:28 +0000956 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000957 default:
958 return false; // These can't be represented as floating point!
959
Dale Johannesend246b2c2007-08-30 00:23:21 +0000960 // FIXME rounding mode needs to be more flexible
Chris Lattner2f7c9632001-06-06 20:29:01 +0000961 case Type::FloatTyID:
Dale Johannesend246b2c2007-08-30 00:23:21 +0000962 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
963 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven) ==
964 APFloat::opOK;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000965 case Type::DoubleTyID:
Dale Johannesend246b2c2007-08-30 00:23:21 +0000966 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
967 &Val2.getSemantics() == &APFloat::IEEEdouble ||
968 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven) ==
969 APFloat::opOK;
Dale Johannesenbdad8092007-08-09 22:51:36 +0000970 case Type::X86_FP80TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +0000971 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
972 &Val2.getSemantics() == &APFloat::IEEEdouble ||
973 &Val2.getSemantics() == &APFloat::x87DoubleExtended;
Dale Johannesenbdad8092007-08-09 22:51:36 +0000974 case Type::FP128TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +0000975 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
976 &Val2.getSemantics() == &APFloat::IEEEdouble ||
977 &Val2.getSemantics() == &APFloat::IEEEquad;
Dale Johannesen007aa372007-10-11 18:07:22 +0000978 case Type::PPC_FP128TyID:
979 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
980 &Val2.getSemantics() == &APFloat::IEEEdouble ||
981 &Val2.getSemantics() == &APFloat::PPCDoubleDouble;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000982 }
Chris Lattneraa2372562006-05-24 17:04:05 +0000983}
Chris Lattner9655e542001-07-20 19:16:02 +0000984
Chris Lattner49d855c2001-09-07 16:46:31 +0000985//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000986// Factory Function Implementation
987
Gabor Greiff6caff662008-05-10 08:32:32 +0000988
989// The number of operands for each ConstantCreator::create method is
990// determined by the ConstantTraits template.
Chris Lattner98fa07b2003-05-23 20:03:32 +0000991// ConstantCreator - A class that is used to create constants by
992// ValueMap*. This class should be partially specialized if there is
993// something strange that needs to be done to interface to the ctor for the
994// constant.
995//
Chris Lattner189d19f2003-11-21 20:23:48 +0000996namespace llvm {
Gabor Greiff6caff662008-05-10 08:32:32 +0000997 template<class ValType>
998 struct ConstantTraits;
999
1000 template<typename T, typename Alloc>
1001 struct VISIBILITY_HIDDEN ConstantTraits< std::vector<T, Alloc> > {
1002 static unsigned uses(const std::vector<T, Alloc>& v) {
1003 return v.size();
1004 }
1005 };
1006
Chris Lattner189d19f2003-11-21 20:23:48 +00001007 template<class ConstantClass, class TypeClass, class ValType>
Chris Lattner02157b02006-06-28 21:38:54 +00001008 struct VISIBILITY_HIDDEN ConstantCreator {
Chris Lattner189d19f2003-11-21 20:23:48 +00001009 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001010 return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V);
Chris Lattner189d19f2003-11-21 20:23:48 +00001011 }
1012 };
Misha Brukmanb1c93172005-04-21 23:48:37 +00001013
Chris Lattner189d19f2003-11-21 20:23:48 +00001014 template<class ConstantClass, class TypeClass>
Chris Lattner02157b02006-06-28 21:38:54 +00001015 struct VISIBILITY_HIDDEN ConvertConstantType {
Chris Lattner189d19f2003-11-21 20:23:48 +00001016 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
1017 assert(0 && "This type cannot be converted!\n");
1018 abort();
1019 }
1020 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001021
Chris Lattner935aa922005-10-04 17:48:46 +00001022 template<class ValType, class TypeClass, class ConstantClass,
1023 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattner02157b02006-06-28 21:38:54 +00001024 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
Chris Lattnerb64419a2005-10-03 22:51:37 +00001025 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +00001026 typedef std::pair<const Type*, ValType> MapKey;
1027 typedef std::map<MapKey, Constant *> MapTy;
1028 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
1029 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
Chris Lattnerb64419a2005-10-03 22:51:37 +00001030 private:
Chris Lattner5bbf60a52005-10-04 16:52:46 +00001031 /// Map - This is the main map from the element descriptor to the Constants.
1032 /// This is the primary way we avoid creating two of the same shape
1033 /// constant.
Chris Lattnerb50d1352003-10-05 00:17:43 +00001034 MapTy Map;
Chris Lattner935aa922005-10-04 17:48:46 +00001035
1036 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
1037 /// from the constants to their element in Map. This is important for
1038 /// removal of constants from the array, which would otherwise have to scan
1039 /// through the map with very large keys.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001040 InverseMapTy InverseMap;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001041
Jim Laskeyc03caef2006-07-17 17:38:29 +00001042 /// AbstractTypeMap - Map for abstract type constants.
1043 ///
Chris Lattnerb50d1352003-10-05 00:17:43 +00001044 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner99a669b2004-11-19 16:39:44 +00001045
Chris Lattner98fa07b2003-05-23 20:03:32 +00001046 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +00001047 typename MapTy::iterator map_end() { return Map.end(); }
Chris Lattnerb64419a2005-10-03 22:51:37 +00001048
1049 /// InsertOrGetItem - Return an iterator for the specified element.
1050 /// If the element exists in the map, the returned iterator points to the
1051 /// entry and Exists=true. If not, the iterator points to the newly
1052 /// inserted entry and returns Exists=false. Newly inserted entries have
1053 /// I->second == 0, and should be filled in.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001054 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
1055 &InsertVal,
Chris Lattnerb64419a2005-10-03 22:51:37 +00001056 bool &Exists) {
Jim Laskeyc03caef2006-07-17 17:38:29 +00001057 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001058 Exists = !IP.second;
1059 return IP.first;
1060 }
Chris Lattner5bbf60a52005-10-04 16:52:46 +00001061
Chris Lattner935aa922005-10-04 17:48:46 +00001062private:
Jim Laskeyc03caef2006-07-17 17:38:29 +00001063 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
Chris Lattner935aa922005-10-04 17:48:46 +00001064 if (HasLargeKey) {
Jim Laskeyc03caef2006-07-17 17:38:29 +00001065 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
Chris Lattner935aa922005-10-04 17:48:46 +00001066 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
1067 IMI->second->second == CP &&
1068 "InverseMap corrupt!");
1069 return IMI->second;
1070 }
1071
Jim Laskeyc03caef2006-07-17 17:38:29 +00001072 typename MapTy::iterator I =
Dan Gohmane955c482008-08-05 14:45:15 +00001073 Map.find(MapKey(static_cast<const TypeClass*>(CP->getRawType()),
1074 getValType(CP)));
Chris Lattner5bbf60a52005-10-04 16:52:46 +00001075 if (I == Map.end() || I->second != CP) {
1076 // FIXME: This should not use a linear scan. If this gets to be a
1077 // performance problem, someone should look at this.
1078 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
1079 /* empty */;
1080 }
Chris Lattner935aa922005-10-04 17:48:46 +00001081 return I;
1082 }
1083public:
1084
Chris Lattnerb64419a2005-10-03 22:51:37 +00001085 /// getOrCreate - Return the specified constant from the map, creating it if
1086 /// necessary.
Chris Lattner98fa07b2003-05-23 20:03:32 +00001087 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00001088 MapKey Lookup(Ty, V);
Dan Gohman3707f1d2008-07-11 20:58:19 +00001089 typename MapTy::iterator I = Map.find(Lookup);
Reid Spencere0fc4df2006-10-20 07:07:24 +00001090 // Is it in the map?
Dan Gohman3707f1d2008-07-11 20:58:19 +00001091 if (I != Map.end())
Reid Spencere0fc4df2006-10-20 07:07:24 +00001092 return static_cast<ConstantClass *>(I->second);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001093
1094 // If no preexisting value, create one now...
1095 ConstantClass *Result =
1096 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
1097
Chris Lattnerb50d1352003-10-05 00:17:43 +00001098 /// FIXME: why does this assert fail when loading 176.gcc?
1099 //assert(Result->getType() == Ty && "Type specified is not correct!");
1100 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
1101
Chris Lattner935aa922005-10-04 17:48:46 +00001102 if (HasLargeKey) // Remember the reverse mapping if needed.
1103 InverseMap.insert(std::make_pair(Result, I));
1104
Chris Lattnerb50d1352003-10-05 00:17:43 +00001105 // If the type of the constant is abstract, make sure that an entry exists
1106 // for it in the AbstractTypeMap.
1107 if (Ty->isAbstract()) {
Dan Gohman3707f1d2008-07-11 20:58:19 +00001108 typename AbstractTypeMapTy::iterator TI = AbstractTypeMap.find(Ty);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001109
Dan Gohman3707f1d2008-07-11 20:58:19 +00001110 if (TI == AbstractTypeMap.end()) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00001111 // Add ourselves to the ATU list of the type.
1112 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
1113
1114 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
1115 }
1116 }
Chris Lattner98fa07b2003-05-23 20:03:32 +00001117 return Result;
1118 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001119
Chris Lattner98fa07b2003-05-23 20:03:32 +00001120 void remove(ConstantClass *CP) {
Jim Laskeyc03caef2006-07-17 17:38:29 +00001121 typename MapTy::iterator I = FindExistingElement(CP);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001122 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +00001123 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001124
Chris Lattner935aa922005-10-04 17:48:46 +00001125 if (HasLargeKey) // Remember the reverse mapping if needed.
1126 InverseMap.erase(CP);
1127
Chris Lattnerb50d1352003-10-05 00:17:43 +00001128 // Now that we found the entry, make sure this isn't the entry that
1129 // the AbstractTypeMap points to.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001130 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001131 if (Ty->isAbstract()) {
1132 assert(AbstractTypeMap.count(Ty) &&
1133 "Abstract type not in AbstractTypeMap?");
Jim Laskeyc03caef2006-07-17 17:38:29 +00001134 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
Chris Lattnerb50d1352003-10-05 00:17:43 +00001135 if (ATMEntryIt == I) {
1136 // Yes, we are removing the representative entry for this type.
1137 // See if there are any other entries of the same type.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001138 typename MapTy::iterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001139
Chris Lattnerb50d1352003-10-05 00:17:43 +00001140 // First check the entry before this one...
1141 if (TmpIt != Map.begin()) {
1142 --TmpIt;
1143 if (TmpIt->first.first != Ty) // Not the same type, move back...
1144 ++TmpIt;
1145 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001146
Chris Lattnerb50d1352003-10-05 00:17:43 +00001147 // If we didn't find the same type, try to move forward...
1148 if (TmpIt == ATMEntryIt) {
1149 ++TmpIt;
1150 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
1151 --TmpIt; // No entry afterwards with the same type
1152 }
1153
1154 // If there is another entry in the map of the same abstract type,
1155 // update the AbstractTypeMap entry now.
1156 if (TmpIt != ATMEntryIt) {
1157 ATMEntryIt = TmpIt;
1158 } else {
1159 // Otherwise, we are removing the last instance of this type
1160 // from the table. Remove from the ATM, and from user list.
1161 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
1162 AbstractTypeMap.erase(Ty);
1163 }
Chris Lattner98fa07b2003-05-23 20:03:32 +00001164 }
Chris Lattnerb50d1352003-10-05 00:17:43 +00001165 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001166
Chris Lattnerb50d1352003-10-05 00:17:43 +00001167 Map.erase(I);
1168 }
1169
Chris Lattner3b793c62005-10-04 21:35:50 +00001170
1171 /// MoveConstantToNewSlot - If we are about to change C to be the element
1172 /// specified by I, update our internal data structures to reflect this
1173 /// fact.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001174 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
Chris Lattner3b793c62005-10-04 21:35:50 +00001175 // First, remove the old location of the specified constant in the map.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001176 typename MapTy::iterator OldI = FindExistingElement(C);
Chris Lattner3b793c62005-10-04 21:35:50 +00001177 assert(OldI != Map.end() && "Constant not found in constant table!");
1178 assert(OldI->second == C && "Didn't find correct element?");
1179
1180 // If this constant is the representative element for its abstract type,
1181 // update the AbstractTypeMap so that the representative element is I.
1182 if (C->getType()->isAbstract()) {
1183 typename AbstractTypeMapTy::iterator ATI =
1184 AbstractTypeMap.find(C->getType());
1185 assert(ATI != AbstractTypeMap.end() &&
1186 "Abstract type not in AbstractTypeMap?");
1187 if (ATI->second == OldI)
1188 ATI->second = I;
1189 }
1190
1191 // Remove the old entry from the map.
1192 Map.erase(OldI);
1193
1194 // Update the inverse map so that we know that this constant is now
1195 // located at descriptor I.
1196 if (HasLargeKey) {
1197 assert(I->second == C && "Bad inversemap entry!");
1198 InverseMap[C] = I;
1199 }
1200 }
1201
Chris Lattnerb50d1352003-10-05 00:17:43 +00001202 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +00001203 typename AbstractTypeMapTy::iterator I =
Jim Laskeyc03caef2006-07-17 17:38:29 +00001204 AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +00001205
1206 assert(I != AbstractTypeMap.end() &&
1207 "Abstract type not in AbstractTypeMap?");
1208
1209 // Convert a constant at a time until the last one is gone. The last one
1210 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
1211 // eliminated eventually.
1212 do {
1213 ConvertConstantType<ConstantClass,
Jim Laskeyc03caef2006-07-17 17:38:29 +00001214 TypeClass>::convert(
1215 static_cast<ConstantClass *>(I->second->second),
Chris Lattnerb50d1352003-10-05 00:17:43 +00001216 cast<TypeClass>(NewTy));
1217
Jim Laskeyc03caef2006-07-17 17:38:29 +00001218 I = AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +00001219 } while (I != AbstractTypeMap.end());
1220 }
1221
1222 // If the type became concrete without being refined to any other existing
1223 // type, we just remove ourselves from the ATU list.
1224 void typeBecameConcrete(const DerivedType *AbsTy) {
1225 AbsTy->removeAbstractTypeUser(this);
1226 }
1227
1228 void dump() const {
Bill Wendling6a462f12006-11-17 08:03:48 +00001229 DOUT << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +00001230 }
1231 };
1232}
1233
Chris Lattnera84df0a22006-09-28 23:36:21 +00001234
Chris Lattner28173502007-02-20 06:11:36 +00001235
Chris Lattner9fba3da2004-02-15 05:53:04 +00001236//---- ConstantAggregateZero::get() implementation...
1237//
1238namespace llvm {
1239 // ConstantAggregateZero does not take extra "value" argument...
1240 template<class ValType>
1241 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
1242 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
1243 return new ConstantAggregateZero(Ty);
1244 }
1245 };
1246
1247 template<>
1248 struct ConvertConstantType<ConstantAggregateZero, Type> {
1249 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
1250 // Make everyone now use a constant of the new type...
1251 Constant *New = ConstantAggregateZero::get(NewTy);
1252 assert(New != OldC && "Didn't replace constant??");
1253 OldC->uncheckedReplaceAllUsesWith(New);
1254 OldC->destroyConstant(); // This constant is now dead, destroy it.
1255 }
1256 };
1257}
1258
Chris Lattner69edc982006-09-28 00:35:06 +00001259static ManagedStatic<ValueMap<char, Type,
1260 ConstantAggregateZero> > AggZeroConstants;
Chris Lattner9fba3da2004-02-15 05:53:04 +00001261
Chris Lattner3e650af2004-08-04 04:48:01 +00001262static char getValType(ConstantAggregateZero *CPZ) { return 0; }
1263
Chris Lattner9fba3da2004-02-15 05:53:04 +00001264Constant *ConstantAggregateZero::get(const Type *Ty) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001265 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) &&
Chris Lattnerbfd0b6d2006-06-10 04:16:23 +00001266 "Cannot create an aggregate zero of non-aggregate type!");
Chris Lattner69edc982006-09-28 00:35:06 +00001267 return AggZeroConstants->getOrCreate(Ty, 0);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001268}
1269
1270// destroyConstant - Remove the constant from the constant table...
1271//
1272void ConstantAggregateZero::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001273 AggZeroConstants->remove(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001274 destroyConstantImpl();
1275}
1276
Chris Lattner3462ae32001-12-03 22:26:30 +00001277//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001278//
Chris Lattner189d19f2003-11-21 20:23:48 +00001279namespace llvm {
1280 template<>
1281 struct ConvertConstantType<ConstantArray, ArrayType> {
1282 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
1283 // Make everyone now use a constant of the new type...
1284 std::vector<Constant*> C;
1285 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1286 C.push_back(cast<Constant>(OldC->getOperand(i)));
1287 Constant *New = ConstantArray::get(NewTy, C);
1288 assert(New != OldC && "Didn't replace constant??");
1289 OldC->uncheckedReplaceAllUsesWith(New);
1290 OldC->destroyConstant(); // This constant is now dead, destroy it.
1291 }
1292 };
1293}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001294
Chris Lattner3e650af2004-08-04 04:48:01 +00001295static std::vector<Constant*> getValType(ConstantArray *CA) {
1296 std::vector<Constant*> Elements;
1297 Elements.reserve(CA->getNumOperands());
1298 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1299 Elements.push_back(cast<Constant>(CA->getOperand(i)));
1300 return Elements;
1301}
1302
Chris Lattnerb64419a2005-10-03 22:51:37 +00001303typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattner935aa922005-10-04 17:48:46 +00001304 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001305static ManagedStatic<ArrayConstantsTy> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001306
Chris Lattner015e8212004-02-15 04:14:47 +00001307Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +00001308 const std::vector<Constant*> &V) {
1309 // If this is an all-zero array, return a ConstantAggregateZero object
1310 if (!V.empty()) {
1311 Constant *C = V[0];
1312 if (!C->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001313 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001314 for (unsigned i = 1, e = V.size(); i != e; ++i)
1315 if (V[i] != C)
Chris Lattner69edc982006-09-28 00:35:06 +00001316 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001317 }
1318 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001319}
1320
Chris Lattner98fa07b2003-05-23 20:03:32 +00001321// destroyConstant - Remove the constant from the constant table...
1322//
1323void ConstantArray::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001324 ArrayConstants->remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001325 destroyConstantImpl();
1326}
1327
Reid Spencer6f614532006-05-30 08:23:18 +00001328/// ConstantArray::get(const string&) - Return an array that is initialized to
1329/// contain the specified string. If length is zero then a null terminator is
1330/// added to the specified string so that it may be used in a natural way.
1331/// Otherwise, the length parameter specifies how much of the string to use
1332/// and it won't be null terminated.
1333///
Reid Spencer82ebaba2006-05-30 18:15:07 +00001334Constant *ConstantArray::get(const std::string &Str, bool AddNull) {
Chris Lattner7f74a562002-01-20 22:54:45 +00001335 std::vector<Constant*> ElementVals;
Reid Spencer82ebaba2006-05-30 18:15:07 +00001336 for (unsigned i = 0; i < Str.length(); ++i)
Reid Spencer8d9336d2006-12-31 05:26:44 +00001337 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +00001338
1339 // Add a null terminator to the string...
Reid Spencer82ebaba2006-05-30 18:15:07 +00001340 if (AddNull) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001341 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, 0));
Reid Spencer6f614532006-05-30 08:23:18 +00001342 }
Chris Lattner8f80fe02001-10-14 23:54:12 +00001343
Reid Spencer8d9336d2006-12-31 05:26:44 +00001344 ArrayType *ATy = ArrayType::get(Type::Int8Ty, ElementVals.size());
Chris Lattner3462ae32001-12-03 22:26:30 +00001345 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +00001346}
1347
Reid Spencer2546b762007-01-26 07:37:34 +00001348/// isString - This method returns true if the array is an array of i8, and
1349/// if the elements of the array are all ConstantInt's.
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001350bool ConstantArray::isString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001351 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001352 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001353 return false;
1354 // Check the elements to make sure they are all integers, not constant
1355 // expressions.
1356 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1357 if (!isa<ConstantInt>(getOperand(i)))
1358 return false;
1359 return true;
1360}
1361
Evan Cheng3763c5b2006-10-26 19:15:05 +00001362/// isCString - This method returns true if the array is a string (see
1363/// isString) and it ends in a null byte \0 and does not contains any other
1364/// null bytes except its terminator.
1365bool ConstantArray::isCString() 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)
Evan Chenge974da62006-10-26 21:48:03 +00001368 return false;
1369 Constant *Zero = Constant::getNullValue(getOperand(0)->getType());
1370 // Last element must be a null.
1371 if (getOperand(getNumOperands()-1) != Zero)
1372 return false;
1373 // Other elements must be non-null integers.
1374 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1375 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +00001376 return false;
Evan Chenge974da62006-10-26 21:48:03 +00001377 if (getOperand(i) == Zero)
1378 return false;
1379 }
Evan Cheng3763c5b2006-10-26 19:15:05 +00001380 return true;
1381}
1382
1383
Reid Spencer2546b762007-01-26 07:37:34 +00001384// getAsString - If the sub-element type of this array is i8
Chris Lattner81fabb02002-08-26 17:53:56 +00001385// then this method converts the array to an std::string and returns it.
1386// Otherwise, it asserts out.
1387//
1388std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001389 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001390 std::string Result;
Owen Anderson79c69bc2008-06-24 21:58:29 +00001391 Result.reserve(getNumOperands());
Chris Lattner6077c312003-07-23 15:22:26 +00001392 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonee9c30d2008-06-25 01:05:05 +00001393 Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue());
Chris Lattner81fabb02002-08-26 17:53:56 +00001394 return Result;
1395}
1396
1397
Chris Lattner3462ae32001-12-03 22:26:30 +00001398//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001399//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001400
Chris Lattner189d19f2003-11-21 20:23:48 +00001401namespace llvm {
1402 template<>
1403 struct ConvertConstantType<ConstantStruct, StructType> {
1404 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1405 // Make everyone now use a constant of the new type...
1406 std::vector<Constant*> C;
1407 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1408 C.push_back(cast<Constant>(OldC->getOperand(i)));
1409 Constant *New = ConstantStruct::get(NewTy, C);
1410 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001411
Chris Lattner189d19f2003-11-21 20:23:48 +00001412 OldC->uncheckedReplaceAllUsesWith(New);
1413 OldC->destroyConstant(); // This constant is now dead, destroy it.
1414 }
1415 };
1416}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001417
Chris Lattner8760ec72005-10-04 01:17:50 +00001418typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattner935aa922005-10-04 17:48:46 +00001419 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001420static ManagedStatic<StructConstantsTy> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001421
Chris Lattner3e650af2004-08-04 04:48:01 +00001422static std::vector<Constant*> getValType(ConstantStruct *CS) {
1423 std::vector<Constant*> Elements;
1424 Elements.reserve(CS->getNumOperands());
1425 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1426 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1427 return Elements;
1428}
1429
Chris Lattner015e8212004-02-15 04:14:47 +00001430Constant *ConstantStruct::get(const StructType *Ty,
1431 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001432 // Create a ConstantAggregateZero value if all elements are zeros...
1433 for (unsigned i = 0, e = V.size(); i != e; ++i)
1434 if (!V[i]->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001435 return StructConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001436
1437 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001438}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001439
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001440Constant *ConstantStruct::get(const std::vector<Constant*> &V, bool packed) {
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001441 std::vector<const Type*> StructEls;
1442 StructEls.reserve(V.size());
1443 for (unsigned i = 0, e = V.size(); i != e; ++i)
1444 StructEls.push_back(V[i]->getType());
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001445 return get(StructType::get(StructEls, packed), V);
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001446}
1447
Chris Lattnerd7a73302001-10-13 06:57:33 +00001448// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001449//
Chris Lattner3462ae32001-12-03 22:26:30 +00001450void ConstantStruct::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001451 StructConstants->remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001452 destroyConstantImpl();
1453}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001454
Reid Spencerd84d35b2007-02-15 02:26:10 +00001455//---- ConstantVector::get() implementation...
Brian Gaeke02209042004-08-20 06:00:58 +00001456//
1457namespace llvm {
1458 template<>
Reid Spencerd84d35b2007-02-15 02:26:10 +00001459 struct ConvertConstantType<ConstantVector, VectorType> {
1460 static void convert(ConstantVector *OldC, const VectorType *NewTy) {
Brian Gaeke02209042004-08-20 06:00:58 +00001461 // Make everyone now use a constant of the new type...
1462 std::vector<Constant*> C;
1463 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1464 C.push_back(cast<Constant>(OldC->getOperand(i)));
Reid Spencerd84d35b2007-02-15 02:26:10 +00001465 Constant *New = ConstantVector::get(NewTy, C);
Brian Gaeke02209042004-08-20 06:00:58 +00001466 assert(New != OldC && "Didn't replace constant??");
1467 OldC->uncheckedReplaceAllUsesWith(New);
1468 OldC->destroyConstant(); // This constant is now dead, destroy it.
1469 }
1470 };
1471}
1472
Reid Spencerd84d35b2007-02-15 02:26:10 +00001473static std::vector<Constant*> getValType(ConstantVector *CP) {
Brian Gaeke02209042004-08-20 06:00:58 +00001474 std::vector<Constant*> Elements;
1475 Elements.reserve(CP->getNumOperands());
1476 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1477 Elements.push_back(CP->getOperand(i));
1478 return Elements;
1479}
1480
Reid Spencerd84d35b2007-02-15 02:26:10 +00001481static ManagedStatic<ValueMap<std::vector<Constant*>, VectorType,
Reid Spencer09575ba2007-02-15 03:39:18 +00001482 ConstantVector> > VectorConstants;
Brian Gaeke02209042004-08-20 06:00:58 +00001483
Reid Spencerd84d35b2007-02-15 02:26:10 +00001484Constant *ConstantVector::get(const VectorType *Ty,
Brian Gaeke02209042004-08-20 06:00:58 +00001485 const std::vector<Constant*> &V) {
Chris Lattnerd977c072008-07-10 00:44:03 +00001486 assert(!V.empty() && "Vectors can't be empty");
1487 // If this is an all-undef or alll-zero vector, return a
1488 // ConstantAggregateZero or UndefValue.
1489 Constant *C = V[0];
1490 bool isZero = C->isNullValue();
1491 bool isUndef = isa<UndefValue>(C);
1492
1493 if (isZero || isUndef) {
Brian Gaeke02209042004-08-20 06:00:58 +00001494 for (unsigned i = 1, e = V.size(); i != e; ++i)
Chris Lattnerd977c072008-07-10 00:44:03 +00001495 if (V[i] != C) {
1496 isZero = isUndef = false;
1497 break;
1498 }
Brian Gaeke02209042004-08-20 06:00:58 +00001499 }
Chris Lattnerd977c072008-07-10 00:44:03 +00001500
1501 if (isZero)
1502 return ConstantAggregateZero::get(Ty);
1503 if (isUndef)
1504 return UndefValue::get(Ty);
1505 return VectorConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001506}
1507
Reid Spencerd84d35b2007-02-15 02:26:10 +00001508Constant *ConstantVector::get(const std::vector<Constant*> &V) {
Brian Gaeke02209042004-08-20 06:00:58 +00001509 assert(!V.empty() && "Cannot infer type if V is empty");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001510 return get(VectorType::get(V.front()->getType(),V.size()), V);
Brian Gaeke02209042004-08-20 06:00:58 +00001511}
1512
1513// destroyConstant - Remove the constant from the constant table...
1514//
Reid Spencerd84d35b2007-02-15 02:26:10 +00001515void ConstantVector::destroyConstant() {
Reid Spencer09575ba2007-02-15 03:39:18 +00001516 VectorConstants->remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001517 destroyConstantImpl();
1518}
1519
Dan Gohman30978072007-05-24 14:36:04 +00001520/// This function will return true iff every element in this vector constant
Jim Laskeyf0478822007-01-12 22:39:14 +00001521/// is set to all ones.
1522/// @returns true iff this constant's emements are all set to all ones.
1523/// @brief Determine if the value is all ones.
Reid Spencerd84d35b2007-02-15 02:26:10 +00001524bool ConstantVector::isAllOnesValue() const {
Jim Laskeyf0478822007-01-12 22:39:14 +00001525 // Check out first element.
1526 const Constant *Elt = getOperand(0);
1527 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1528 if (!CI || !CI->isAllOnesValue()) return false;
1529 // Then make sure all remaining elements point to the same value.
1530 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1531 if (getOperand(I) != Elt) return false;
1532 }
1533 return true;
1534}
1535
Dan Gohman07159202007-10-17 17:51:30 +00001536/// getSplatValue - If this is a splat constant, where all of the
1537/// elements have the same value, return that value. Otherwise return null.
1538Constant *ConstantVector::getSplatValue() {
1539 // Check out first element.
1540 Constant *Elt = getOperand(0);
1541 // Then make sure all remaining elements point to the same value.
1542 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
1543 if (getOperand(I) != Elt) return 0;
1544 return Elt;
1545}
1546
Chris Lattner3462ae32001-12-03 22:26:30 +00001547//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001548//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001549
Chris Lattner189d19f2003-11-21 20:23:48 +00001550namespace llvm {
1551 // ConstantPointerNull does not take extra "value" argument...
1552 template<class ValType>
1553 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1554 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1555 return new ConstantPointerNull(Ty);
1556 }
1557 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001558
Chris Lattner189d19f2003-11-21 20:23:48 +00001559 template<>
1560 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1561 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1562 // Make everyone now use a constant of the new type...
1563 Constant *New = ConstantPointerNull::get(NewTy);
1564 assert(New != OldC && "Didn't replace constant??");
1565 OldC->uncheckedReplaceAllUsesWith(New);
1566 OldC->destroyConstant(); // This constant is now dead, destroy it.
1567 }
1568 };
1569}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001570
Chris Lattner69edc982006-09-28 00:35:06 +00001571static ManagedStatic<ValueMap<char, PointerType,
1572 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001573
Chris Lattner3e650af2004-08-04 04:48:01 +00001574static char getValType(ConstantPointerNull *) {
1575 return 0;
1576}
1577
1578
Chris Lattner3462ae32001-12-03 22:26:30 +00001579ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001580 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001581}
1582
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001583// destroyConstant - Remove the constant from the constant table...
1584//
1585void ConstantPointerNull::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001586 NullPtrConstants->remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001587 destroyConstantImpl();
1588}
1589
1590
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001591//---- UndefValue::get() implementation...
1592//
1593
1594namespace llvm {
1595 // UndefValue does not take extra "value" argument...
1596 template<class ValType>
1597 struct ConstantCreator<UndefValue, Type, ValType> {
1598 static UndefValue *create(const Type *Ty, const ValType &V) {
1599 return new UndefValue(Ty);
1600 }
1601 };
1602
1603 template<>
1604 struct ConvertConstantType<UndefValue, Type> {
1605 static void convert(UndefValue *OldC, const Type *NewTy) {
1606 // Make everyone now use a constant of the new type.
1607 Constant *New = UndefValue::get(NewTy);
1608 assert(New != OldC && "Didn't replace constant??");
1609 OldC->uncheckedReplaceAllUsesWith(New);
1610 OldC->destroyConstant(); // This constant is now dead, destroy it.
1611 }
1612 };
1613}
1614
Chris Lattner69edc982006-09-28 00:35:06 +00001615static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001616
1617static char getValType(UndefValue *) {
1618 return 0;
1619}
1620
1621
1622UndefValue *UndefValue::get(const Type *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001623 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001624}
1625
1626// destroyConstant - Remove the constant from the constant table.
1627//
1628void UndefValue::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001629 UndefValueConstants->remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001630 destroyConstantImpl();
1631}
1632
1633
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001634//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001635//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001636
Dan Gohmand78c4002008-05-13 00:00:25 +00001637namespace {
1638
Reid Spenceree3c9912006-12-04 05:19:50 +00001639struct ExprMapKeyType {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001640 typedef SmallVector<unsigned, 4> IndexList;
1641
1642 ExprMapKeyType(unsigned opc,
1643 const std::vector<Constant*> &ops,
1644 unsigned short pred = 0,
1645 const IndexList &inds = IndexList())
1646 : opcode(opc), predicate(pred), operands(ops), indices(inds) {}
Reid Spencerdba6aa42006-12-04 18:38:05 +00001647 uint16_t opcode;
1648 uint16_t predicate;
Reid Spenceree3c9912006-12-04 05:19:50 +00001649 std::vector<Constant*> operands;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001650 IndexList indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001651 bool operator==(const ExprMapKeyType& that) const {
1652 return this->opcode == that.opcode &&
1653 this->predicate == that.predicate &&
1654 this->operands == that.operands;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001655 this->indices == that.indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001656 }
1657 bool operator<(const ExprMapKeyType & that) const {
1658 return this->opcode < that.opcode ||
1659 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1660 (this->opcode == that.opcode && this->predicate == that.predicate &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001661 this->operands < that.operands) ||
1662 (this->opcode == that.opcode && this->predicate == that.predicate &&
1663 this->operands == that.operands && this->indices < that.indices);
Reid Spenceree3c9912006-12-04 05:19:50 +00001664 }
1665
1666 bool operator!=(const ExprMapKeyType& that) const {
1667 return !(*this == that);
1668 }
1669};
Chris Lattner98fa07b2003-05-23 20:03:32 +00001670
Dan Gohmand78c4002008-05-13 00:00:25 +00001671}
1672
Chris Lattner189d19f2003-11-21 20:23:48 +00001673namespace llvm {
1674 template<>
1675 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001676 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1677 unsigned short pred = 0) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001678 if (Instruction::isCast(V.opcode))
1679 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1680 if ((V.opcode >= Instruction::BinaryOpsBegin &&
Reid Spencer2341c222007-02-02 02:16:23 +00001681 V.opcode < Instruction::BinaryOpsEnd))
Reid Spenceree3c9912006-12-04 05:19:50 +00001682 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1683 if (V.opcode == Instruction::Select)
1684 return new SelectConstantExpr(V.operands[0], V.operands[1],
1685 V.operands[2]);
1686 if (V.opcode == Instruction::ExtractElement)
1687 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1688 if (V.opcode == Instruction::InsertElement)
1689 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1690 V.operands[2]);
1691 if (V.opcode == Instruction::ShuffleVector)
1692 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1693 V.operands[2]);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001694 if (V.opcode == Instruction::InsertValue)
1695 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
1696 V.indices, Ty);
1697 if (V.opcode == Instruction::ExtractValue)
1698 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001699 if (V.opcode == Instruction::GetElementPtr) {
1700 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
Gabor Greife9ecc682008-04-06 20:25:17 +00001701 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001702 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001703
Reid Spenceree3c9912006-12-04 05:19:50 +00001704 // The compare instructions are weird. We have to encode the predicate
1705 // value and it is combined with the instruction opcode by multiplying
1706 // the opcode by one hundred. We must decode this to get the predicate.
1707 if (V.opcode == Instruction::ICmp)
Nate Begemand2195702008-05-12 19:01:56 +00001708 return new CompareConstantExpr(Ty, Instruction::ICmp, V.predicate,
Reid Spenceree3c9912006-12-04 05:19:50 +00001709 V.operands[0], V.operands[1]);
1710 if (V.opcode == Instruction::FCmp)
Nate Begemand2195702008-05-12 19:01:56 +00001711 return new CompareConstantExpr(Ty, Instruction::FCmp, V.predicate,
1712 V.operands[0], V.operands[1]);
1713 if (V.opcode == Instruction::VICmp)
1714 return new CompareConstantExpr(Ty, Instruction::VICmp, V.predicate,
1715 V.operands[0], V.operands[1]);
1716 if (V.opcode == Instruction::VFCmp)
1717 return new CompareConstantExpr(Ty, Instruction::VFCmp, V.predicate,
Reid Spenceree3c9912006-12-04 05:19:50 +00001718 V.operands[0], V.operands[1]);
1719 assert(0 && "Invalid ConstantExpr!");
Jeff Cohen9f469632006-12-15 21:47:01 +00001720 return 0;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001721 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001722 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001723
Chris Lattner189d19f2003-11-21 20:23:48 +00001724 template<>
1725 struct ConvertConstantType<ConstantExpr, Type> {
1726 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1727 Constant *New;
1728 switch (OldC->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001729 case Instruction::Trunc:
1730 case Instruction::ZExt:
1731 case Instruction::SExt:
1732 case Instruction::FPTrunc:
1733 case Instruction::FPExt:
1734 case Instruction::UIToFP:
1735 case Instruction::SIToFP:
1736 case Instruction::FPToUI:
1737 case Instruction::FPToSI:
1738 case Instruction::PtrToInt:
1739 case Instruction::IntToPtr:
1740 case Instruction::BitCast:
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001741 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
1742 NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001743 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001744 case Instruction::Select:
1745 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1746 OldC->getOperand(1),
1747 OldC->getOperand(2));
1748 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001749 default:
1750 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001751 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner189d19f2003-11-21 20:23:48 +00001752 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1753 OldC->getOperand(1));
1754 break;
1755 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001756 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001757 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
Chris Lattner302116a2007-01-31 04:40:28 +00001758 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
1759 &Idx[0], Idx.size());
Chris Lattner189d19f2003-11-21 20:23:48 +00001760 break;
1761 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001762
Chris Lattner189d19f2003-11-21 20:23:48 +00001763 assert(New != OldC && "Didn't replace constant??");
1764 OldC->uncheckedReplaceAllUsesWith(New);
1765 OldC->destroyConstant(); // This constant is now dead, destroy it.
1766 }
1767 };
1768} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001769
1770
Chris Lattner3e650af2004-08-04 04:48:01 +00001771static ExprMapKeyType getValType(ConstantExpr *CE) {
1772 std::vector<Constant*> Operands;
1773 Operands.reserve(CE->getNumOperands());
1774 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1775 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spenceree3c9912006-12-04 05:19:50 +00001776 return ExprMapKeyType(CE->getOpcode(), Operands,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001777 CE->isCompare() ? CE->getPredicate() : 0,
1778 CE->hasIndices() ?
1779 CE->getIndices() : SmallVector<unsigned, 4>());
Chris Lattner3e650af2004-08-04 04:48:01 +00001780}
1781
Chris Lattner69edc982006-09-28 00:35:06 +00001782static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1783 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001784
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001785/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands7d6c8ae2008-03-30 19:38:55 +00001786/// cast in the ExprConstants map. It is used by the various get* methods below.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001787static inline Constant *getFoldedCast(
1788 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001789 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001790 // Fold a few common cases
1791 if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
1792 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001793
Vikram S. Adve4c485332002-07-15 18:19:33 +00001794 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001795 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00001796 ExprMapKeyType Key(opc, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001797 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001798}
Reid Spencerf37dc652006-12-05 19:14:13 +00001799
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001800Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
1801 Instruction::CastOps opc = Instruction::CastOps(oc);
1802 assert(Instruction::isCast(opc) && "opcode out of range");
1803 assert(C && Ty && "Null arguments to getCast");
1804 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1805
1806 switch (opc) {
1807 default:
1808 assert(0 && "Invalid cast opcode");
1809 break;
1810 case Instruction::Trunc: return getTrunc(C, Ty);
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001811 case Instruction::ZExt: return getZExt(C, Ty);
1812 case Instruction::SExt: return getSExt(C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001813 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1814 case Instruction::FPExt: return getFPExtend(C, Ty);
1815 case Instruction::UIToFP: return getUIToFP(C, Ty);
1816 case Instruction::SIToFP: return getSIToFP(C, Ty);
1817 case Instruction::FPToUI: return getFPToUI(C, Ty);
1818 case Instruction::FPToSI: return getFPToSI(C, Ty);
1819 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1820 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1821 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001822 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001823 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00001824}
1825
Reid Spencer5c140882006-12-04 20:17:56 +00001826Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
1827 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1828 return getCast(Instruction::BitCast, C, Ty);
1829 return getCast(Instruction::ZExt, C, Ty);
1830}
1831
1832Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
1833 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1834 return getCast(Instruction::BitCast, C, Ty);
1835 return getCast(Instruction::SExt, C, Ty);
1836}
1837
1838Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
1839 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1840 return getCast(Instruction::BitCast, C, Ty);
1841 return getCast(Instruction::Trunc, C, Ty);
1842}
1843
Reid Spencerbc245a02006-12-05 03:25:26 +00001844Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
1845 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001846 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001847
Chris Lattner03c49532007-01-15 02:27:26 +00001848 if (Ty->isInteger())
Reid Spencerbc245a02006-12-05 03:25:26 +00001849 return getCast(Instruction::PtrToInt, S, Ty);
1850 return getCast(Instruction::BitCast, S, Ty);
1851}
1852
Reid Spencer56521c42006-12-12 00:51:07 +00001853Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1854 bool isSigned) {
Chris Lattner03c49532007-01-15 02:27:26 +00001855 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer56521c42006-12-12 00:51:07 +00001856 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1857 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1858 Instruction::CastOps opcode =
1859 (SrcBits == DstBits ? Instruction::BitCast :
1860 (SrcBits > DstBits ? Instruction::Trunc :
1861 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1862 return getCast(opcode, C, Ty);
1863}
1864
1865Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
1866 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1867 "Invalid cast");
1868 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1869 unsigned DstBits = Ty->getPrimitiveSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001870 if (SrcBits == DstBits)
1871 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001872 Instruction::CastOps opcode =
Reid Spencerca104e82006-12-12 05:38:50 +00001873 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001874 return getCast(opcode, C, Ty);
1875}
1876
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001877Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001878 assert(C->getType()->isInteger() && "Trunc operand must be integer");
1879 assert(Ty->isInteger() && "Trunc produces only integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001880 assert(C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&&
1881 "SrcTy must be larger than DestTy for Trunc!");
1882
1883 return getFoldedCast(Instruction::Trunc, C, Ty);
1884}
1885
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001886Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001887 assert(C->getType()->isInteger() && "SEXt operand must be integral");
1888 assert(Ty->isInteger() && "SExt produces only integer");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001889 assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1890 "SrcTy must be smaller than DestTy for SExt!");
1891
1892 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerdd284742004-04-04 23:20:30 +00001893}
1894
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001895Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001896 assert(C->getType()->isInteger() && "ZEXt operand must be integral");
1897 assert(Ty->isInteger() && "ZExt produces only integer");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001898 assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1899 "SrcTy must be smaller than DestTy for ZExt!");
1900
1901 return getFoldedCast(Instruction::ZExt, C, Ty);
1902}
1903
1904Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
1905 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1906 C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&&
1907 "This is an illegal floating point truncation!");
1908 return getFoldedCast(Instruction::FPTrunc, C, Ty);
1909}
1910
1911Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
1912 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1913 C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1914 "This is an illegal floating point extension!");
1915 return getFoldedCast(Instruction::FPExt, C, Ty);
1916}
1917
1918Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Nate Begemand4d45c22007-11-17 03:58:34 +00001919 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1920 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1921 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1922 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
1923 "This is an illegal uint to floating point cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001924 return getFoldedCast(Instruction::UIToFP, C, Ty);
1925}
1926
1927Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Nate Begemand4d45c22007-11-17 03:58:34 +00001928 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1929 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1930 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1931 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001932 "This is an illegal sint to floating point cast!");
1933 return getFoldedCast(Instruction::SIToFP, C, Ty);
1934}
1935
1936Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Nate Begemand4d45c22007-11-17 03:58:34 +00001937 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1938 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1939 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1940 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1941 "This is an illegal floating point to uint cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001942 return getFoldedCast(Instruction::FPToUI, C, Ty);
1943}
1944
1945Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Nate Begemand4d45c22007-11-17 03:58:34 +00001946 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1947 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1948 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1949 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1950 "This is an illegal floating point to sint cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001951 return getFoldedCast(Instruction::FPToSI, C, Ty);
1952}
1953
1954Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
1955 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner03c49532007-01-15 02:27:26 +00001956 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001957 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
1958}
1959
1960Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner03c49532007-01-15 02:27:26 +00001961 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001962 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
1963 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
1964}
1965
1966Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
1967 // BitCast implies a no-op cast of type only. No bits change. However, you
1968 // can't cast pointers to anything but pointers.
1969 const Type *SrcTy = C->getType();
1970 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer5c140882006-12-04 20:17:56 +00001971 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001972
1973 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
1974 // or nonptr->ptr). For all the other types, the cast is okay if source and
1975 // destination bit widths are identical.
1976 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1977 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Reid Spencer5c140882006-12-04 20:17:56 +00001978 assert(SrcBitSize == DstBitSize && "BitCast requies types of same width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001979 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerdd284742004-04-04 23:20:30 +00001980}
1981
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001982Constant *ConstantExpr::getSizeOf(const Type *Ty) {
Gordon Henriksen7ce31762007-10-06 14:29:36 +00001983 // sizeof is implemented as: (i64) gep (Ty*)null, 1
Chris Lattnerb5d70302007-02-19 20:01:23 +00001984 Constant *GEPIdx = ConstantInt::get(Type::Int32Ty, 1);
1985 Constant *GEP =
Christopher Lambedf07882007-12-17 01:12:55 +00001986 getGetElementPtr(getNullValue(PointerType::getUnqual(Ty)), &GEPIdx, 1);
Chris Lattnerb5d70302007-02-19 20:01:23 +00001987 return getCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
Alkis Evlogimenos9160d5f2005-03-19 11:40:31 +00001988}
1989
Chris Lattnerb50d1352003-10-05 00:17:43 +00001990Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Reid Spencera009d0d2006-12-04 21:35:24 +00001991 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001992 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00001993 assert(Opcode >= Instruction::BinaryOpsBegin &&
1994 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001995 "Invalid opcode in binary constant expression");
1996 assert(C1->getType() == C2->getType() &&
1997 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001998
Reid Spencer542964f2007-01-11 18:21:29 +00001999 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Chris Lattnerb50d1352003-10-05 00:17:43 +00002000 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
2001 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00002002
Chris Lattner2b383d2e2003-05-13 21:37:02 +00002003 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00002004 ExprMapKeyType Key(Opcode, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002005 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002006}
2007
Reid Spencer266e42b2006-12-23 06:05:41 +00002008Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Nate Begeman098cc6f2008-07-25 17:56:27 +00002009 Constant *C1, Constant *C2) {
2010 bool isVectorType = C1->getType()->getTypeID() == Type::VectorTyID;
Reid Spencer266e42b2006-12-23 06:05:41 +00002011 switch (predicate) {
2012 default: assert(0 && "Invalid CmpInst predicate");
Nate Begemanc96e2e42008-07-25 17:35:37 +00002013 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
2014 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
2015 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
2016 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
2017 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
2018 case CmpInst::FCMP_TRUE:
Nate Begeman098cc6f2008-07-25 17:56:27 +00002019 return isVectorType ? getVFCmp(predicate, C1, C2)
2020 : getFCmp(predicate, C1, C2);
Nate Begemanc96e2e42008-07-25 17:35:37 +00002021 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
2022 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
2023 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
2024 case CmpInst::ICMP_SLE:
Nate Begeman098cc6f2008-07-25 17:56:27 +00002025 return isVectorType ? getVICmp(predicate, C1, C2)
2026 : getICmp(predicate, C1, C2);
Reid Spencer266e42b2006-12-23 06:05:41 +00002027 }
Reid Spencera009d0d2006-12-04 21:35:24 +00002028}
2029
2030Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002031#ifndef NDEBUG
2032 switch (Opcode) {
Reid Spencer7eb55b32006-11-02 01:53:59 +00002033 case Instruction::Add:
2034 case Instruction::Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002035 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002036 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattner03c49532007-01-15 02:27:26 +00002037 assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00002038 isa<VectorType>(C1->getType())) &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002039 "Tried to create an arithmetic operation on a non-arithmetic type!");
2040 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002041 case Instruction::UDiv:
2042 case Instruction::SDiv:
2043 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002044 assert((C1->getType()->isInteger() || (isa<VectorType>(C1->getType()) &&
2045 cast<VectorType>(C1->getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002046 "Tried to create an arithmetic operation on a non-arithmetic type!");
2047 break;
2048 case Instruction::FDiv:
2049 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002050 assert((C1->getType()->isFloatingPoint() || (isa<VectorType>(C1->getType())
2051 && cast<VectorType>(C1->getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002052 && "Tried to create an arithmetic operation on a non-arithmetic type!");
2053 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00002054 case Instruction::URem:
2055 case Instruction::SRem:
2056 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002057 assert((C1->getType()->isInteger() || (isa<VectorType>(C1->getType()) &&
2058 cast<VectorType>(C1->getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002059 "Tried to create an arithmetic operation on a non-arithmetic type!");
2060 break;
2061 case Instruction::FRem:
2062 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002063 assert((C1->getType()->isFloatingPoint() || (isa<VectorType>(C1->getType())
2064 && cast<VectorType>(C1->getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00002065 && "Tried to create an arithmetic operation on a non-arithmetic type!");
2066 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002067 case Instruction::And:
2068 case Instruction::Or:
2069 case Instruction::Xor:
2070 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002071 assert((C1->getType()->isInteger() || isa<VectorType>(C1->getType())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00002072 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002073 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002074 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00002075 case Instruction::LShr:
2076 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00002077 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattner03c49532007-01-15 02:27:26 +00002078 assert(C1->getType()->isInteger() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002079 "Tried to create a shift operation on a non-integer type!");
2080 break;
2081 default:
2082 break;
2083 }
2084#endif
2085
Reid Spencera009d0d2006-12-04 21:35:24 +00002086 return getTy(C1->getType(), Opcode, C1, C2);
2087}
2088
Reid Spencer266e42b2006-12-23 06:05:41 +00002089Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00002090 Constant *C1, Constant *C2) {
2091 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002092 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00002093}
2094
Chris Lattner6e415c02004-03-12 05:54:04 +00002095Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
2096 Constant *V1, Constant *V2) {
Reid Spencer2546b762007-01-26 07:37:34 +00002097 assert(C->getType() == Type::Int1Ty && "Select condition must be i1!");
Chris Lattner6e415c02004-03-12 05:54:04 +00002098 assert(V1->getType() == V2->getType() && "Select value types must match!");
2099 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
2100
2101 if (ReqTy == V1->getType())
2102 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
2103 return SC; // Fold common cases
2104
2105 std::vector<Constant*> argVec(3, C);
2106 argVec[1] = V1;
2107 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00002108 ExprMapKeyType Key(Instruction::Select, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002109 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00002110}
2111
Chris Lattnerb50d1352003-10-05 00:17:43 +00002112Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00002113 Value* const *Idxs,
2114 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002115 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
2116 Idxs+NumIdx) ==
2117 cast<PointerType>(ReqTy)->getElementType() &&
2118 "GEP indices invalid!");
Chris Lattner04b60fe2004-02-16 20:46:13 +00002119
Chris Lattner302116a2007-01-31 04:40:28 +00002120 if (Constant *FC = ConstantFoldGetElementPtr(C, (Constant**)Idxs, NumIdx))
Chris Lattneracdbe712003-04-17 19:24:48 +00002121 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00002122
Chris Lattnerb50d1352003-10-05 00:17:43 +00002123 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00002124 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00002125 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00002126 std::vector<Constant*> ArgVec;
Chris Lattner302116a2007-01-31 04:40:28 +00002127 ArgVec.reserve(NumIdx+1);
Chris Lattner13128ab2004-10-11 22:52:25 +00002128 ArgVec.push_back(C);
Chris Lattner302116a2007-01-31 04:40:28 +00002129 for (unsigned i = 0; i != NumIdx; ++i)
2130 ArgVec.push_back(cast<Constant>(Idxs[i]));
2131 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002132 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002133}
2134
Chris Lattner302116a2007-01-31 04:40:28 +00002135Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
2136 unsigned NumIdx) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00002137 // Get the result type of the getelementptr!
Chris Lattner302116a2007-01-31 04:40:28 +00002138 const Type *Ty =
Dan Gohman12fce772008-05-15 19:50:34 +00002139 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00002140 assert(Ty && "GEP indices invalid!");
Christopher Lamb54dd24c2007-12-11 08:59:05 +00002141 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
2142 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
Chris Lattner13128ab2004-10-11 22:52:25 +00002143}
2144
Chris Lattner302116a2007-01-31 04:40:28 +00002145Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
2146 unsigned NumIdx) {
2147 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00002148}
2149
Chris Lattner302116a2007-01-31 04:40:28 +00002150
Reid Spenceree3c9912006-12-04 05:19:50 +00002151Constant *
2152ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2153 assert(LHS->getType() == RHS->getType());
2154 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
2155 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
2156
Reid Spencer266e42b2006-12-23 06:05:41 +00002157 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002158 return FC; // Fold a few common cases...
2159
2160 // Look up the constant in the table first to ensure uniqueness
2161 std::vector<Constant*> ArgVec;
2162 ArgVec.push_back(LHS);
2163 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00002164 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00002165 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Reid Spencer542964f2007-01-11 18:21:29 +00002166 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002167}
2168
2169Constant *
2170ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2171 assert(LHS->getType() == RHS->getType());
2172 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
2173
Reid Spencer266e42b2006-12-23 06:05:41 +00002174 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002175 return FC; // Fold a few common cases...
2176
2177 // Look up the constant in the table first to ensure uniqueness
2178 std::vector<Constant*> ArgVec;
2179 ArgVec.push_back(LHS);
2180 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00002181 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00002182 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Reid Spencer542964f2007-01-11 18:21:29 +00002183 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002184}
2185
Nate Begemand2195702008-05-12 19:01:56 +00002186Constant *
2187ConstantExpr::getVICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
Chris Lattnereab49262008-07-14 05:17:31 +00002188 assert(isa<VectorType>(LHS->getType()) && LHS->getType() == RHS->getType() &&
Nate Begemand2195702008-05-12 19:01:56 +00002189 "Tried to create vicmp operation on non-vector type!");
Nate Begemand2195702008-05-12 19:01:56 +00002190 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
2191 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid VICmp Predicate");
2192
Nate Begemanac7f3d92008-05-12 19:23:22 +00002193 const VectorType *VTy = cast<VectorType>(LHS->getType());
Nate Begemand2195702008-05-12 19:01:56 +00002194 const Type *EltTy = VTy->getElementType();
2195 unsigned NumElts = VTy->getNumElements();
2196
Chris Lattnereab49262008-07-14 05:17:31 +00002197 // See if we can fold the element-wise comparison of the LHS and RHS.
2198 SmallVector<Constant *, 16> LHSElts, RHSElts;
2199 LHS->getVectorElements(LHSElts);
2200 RHS->getVectorElements(RHSElts);
2201
2202 if (!LHSElts.empty() && !RHSElts.empty()) {
2203 SmallVector<Constant *, 16> Elts;
2204 for (unsigned i = 0; i != NumElts; ++i) {
2205 Constant *FC = ConstantFoldCompareInstruction(pred, LHSElts[i],
2206 RHSElts[i]);
2207 if (ConstantInt *FCI = dyn_cast_or_null<ConstantInt>(FC)) {
2208 if (FCI->getZExtValue())
2209 Elts.push_back(ConstantInt::getAllOnesValue(EltTy));
2210 else
2211 Elts.push_back(ConstantInt::get(EltTy, 0ULL));
2212 } else if (FC && isa<UndefValue>(FC)) {
2213 Elts.push_back(UndefValue::get(EltTy));
2214 } else {
2215 break;
2216 }
Nate Begemand2195702008-05-12 19:01:56 +00002217 }
Chris Lattnereab49262008-07-14 05:17:31 +00002218 if (Elts.size() == NumElts)
2219 return ConstantVector::get(&Elts[0], Elts.size());
Nate Begemand2195702008-05-12 19:01:56 +00002220 }
Nate Begemand2195702008-05-12 19:01:56 +00002221
2222 // Look up the constant in the table first to ensure uniqueness
2223 std::vector<Constant*> ArgVec;
2224 ArgVec.push_back(LHS);
2225 ArgVec.push_back(RHS);
2226 // Get the key type with both the opcode and predicate
2227 const ExprMapKeyType Key(Instruction::VICmp, ArgVec, pred);
2228 return ExprConstants->getOrCreate(LHS->getType(), Key);
2229}
2230
2231Constant *
2232ConstantExpr::getVFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2233 assert(isa<VectorType>(LHS->getType()) &&
2234 "Tried to create vfcmp operation on non-vector type!");
2235 assert(LHS->getType() == RHS->getType());
2236 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid VFCmp Predicate");
2237
2238 const VectorType *VTy = cast<VectorType>(LHS->getType());
2239 unsigned NumElts = VTy->getNumElements();
2240 const Type *EltTy = VTy->getElementType();
2241 const Type *REltTy = IntegerType::get(EltTy->getPrimitiveSizeInBits());
2242 const Type *ResultTy = VectorType::get(REltTy, NumElts);
2243
Chris Lattnereab49262008-07-14 05:17:31 +00002244 // See if we can fold the element-wise comparison of the LHS and RHS.
2245 SmallVector<Constant *, 16> LHSElts, RHSElts;
2246 LHS->getVectorElements(LHSElts);
2247 RHS->getVectorElements(RHSElts);
2248
2249 if (!LHSElts.empty() && !RHSElts.empty()) {
2250 SmallVector<Constant *, 16> Elts;
2251 for (unsigned i = 0; i != NumElts; ++i) {
2252 Constant *FC = ConstantFoldCompareInstruction(pred, LHSElts[i],
2253 RHSElts[i]);
2254 if (ConstantInt *FCI = dyn_cast_or_null<ConstantInt>(FC)) {
2255 if (FCI->getZExtValue())
2256 Elts.push_back(ConstantInt::getAllOnesValue(REltTy));
2257 else
2258 Elts.push_back(ConstantInt::get(REltTy, 0ULL));
2259 } else if (FC && isa<UndefValue>(FC)) {
2260 Elts.push_back(UndefValue::get(REltTy));
2261 } else {
2262 break;
2263 }
Nate Begemand2195702008-05-12 19:01:56 +00002264 }
Chris Lattnereab49262008-07-14 05:17:31 +00002265 if (Elts.size() == NumElts)
2266 return ConstantVector::get(&Elts[0], Elts.size());
Nate Begemand2195702008-05-12 19:01:56 +00002267 }
Nate Begemand2195702008-05-12 19:01:56 +00002268
2269 // Look up the constant in the table first to ensure uniqueness
2270 std::vector<Constant*> ArgVec;
2271 ArgVec.push_back(LHS);
2272 ArgVec.push_back(RHS);
2273 // Get the key type with both the opcode and predicate
2274 const ExprMapKeyType Key(Instruction::VFCmp, ArgVec, pred);
2275 return ExprConstants->getOrCreate(ResultTy, Key);
2276}
2277
Robert Bocchino23004482006-01-10 19:05:34 +00002278Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
2279 Constant *Idx) {
Robert Bocchinode7f1c92006-01-10 20:03:46 +00002280 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
2281 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00002282 // Look up the constant in the table first to ensure uniqueness
2283 std::vector<Constant*> ArgVec(1, Val);
2284 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00002285 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002286 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00002287}
2288
2289Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002290 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002291 "Tried to create extractelement operation on non-vector type!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00002292 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00002293 "Extractelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002294 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +00002295 Val, Idx);
2296}
Chris Lattnerb50d1352003-10-05 00:17:43 +00002297
Robert Bocchinoca27f032006-01-17 20:07:22 +00002298Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
2299 Constant *Elt, Constant *Idx) {
2300 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
2301 return FC; // Fold a few common cases...
2302 // Look up the constant in the table first to ensure uniqueness
2303 std::vector<Constant*> ArgVec(1, Val);
2304 ArgVec.push_back(Elt);
2305 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00002306 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002307 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00002308}
2309
2310Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
2311 Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002312 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002313 "Tried to create insertelement operation on non-vector type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002314 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoca27f032006-01-17 20:07:22 +00002315 && "Insertelement types must match!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00002316 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00002317 "Insertelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002318 return getInsertElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchinoca27f032006-01-17 20:07:22 +00002319 Val, Elt, Idx);
2320}
2321
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002322Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
2323 Constant *V2, Constant *Mask) {
2324 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
2325 return FC; // Fold a few common cases...
2326 // Look up the constant in the table first to ensure uniqueness
2327 std::vector<Constant*> ArgVec(1, V1);
2328 ArgVec.push_back(V2);
2329 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00002330 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002331 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002332}
2333
2334Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
2335 Constant *Mask) {
2336 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
2337 "Invalid shuffle vector constant expr operands!");
2338 return getShuffleVectorTy(V1->getType(), V1, V2, Mask);
2339}
2340
Dan Gohman12fce772008-05-15 19:50:34 +00002341Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg,
2342 Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002343 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002344 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2345 Idxs+NumIdx) == Val->getType() &&
2346 "insertvalue indices invalid!");
2347 assert(Agg->getType() == ReqTy &&
2348 "insertvalue type invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002349 assert(Agg->getType()->isFirstClassType() &&
2350 "Non-first-class type for constant InsertValue expression");
Dan Gohmand5d24f62008-07-21 23:30:30 +00002351 Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs, NumIdx);
2352 assert(FC && "InsertValue constant expr couldn't be folded!");
2353 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00002354}
2355
2356Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002357 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002358 assert(Agg->getType()->isFirstClassType() &&
2359 "Tried to create insertelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002360
Dan Gohman0752bff2008-05-23 00:36:11 +00002361 const Type *ReqTy = Agg->getType();
2362 const Type *ValTy =
Dan Gohman12fce772008-05-15 19:50:34 +00002363 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
Dan Gohman0752bff2008-05-23 00:36:11 +00002364 assert(ValTy == Val->getType() && "insertvalue indices invalid!");
Dan Gohman12fce772008-05-15 19:50:34 +00002365 return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx);
2366}
2367
2368Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002369 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002370 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2371 Idxs+NumIdx) == ReqTy &&
2372 "extractvalue indices invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002373 assert(Agg->getType()->isFirstClassType() &&
2374 "Non-first-class type for constant extractvalue expression");
Dan Gohmand5d24f62008-07-21 23:30:30 +00002375 Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs, NumIdx);
2376 assert(FC && "ExtractValue constant expr couldn't be folded!");
2377 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00002378}
2379
2380Constant *ConstantExpr::getExtractValue(Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002381 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002382 assert(Agg->getType()->isFirstClassType() &&
2383 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002384
2385 const Type *ReqTy =
2386 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
2387 assert(ReqTy && "extractvalue indices invalid!");
2388 return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx);
2389}
2390
Reid Spencer2eadb532007-01-21 00:29:26 +00002391Constant *ConstantExpr::getZeroValueForNegationExpr(const Type *Ty) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002392 if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
Reid Spencer6598ca82007-01-21 02:29:10 +00002393 if (PTy->getElementType()->isFloatingPoint()) {
2394 std::vector<Constant*> zeros(PTy->getNumElements(),
Dale Johannesen98d3a082007-09-14 22:26:36 +00002395 ConstantFP::getNegativeZero(PTy->getElementType()));
Reid Spencerd84d35b2007-02-15 02:26:10 +00002396 return ConstantVector::get(PTy, zeros);
Reid Spencer6598ca82007-01-21 02:29:10 +00002397 }
Reid Spencer2eadb532007-01-21 00:29:26 +00002398
Dale Johannesen98d3a082007-09-14 22:26:36 +00002399 if (Ty->isFloatingPoint())
2400 return ConstantFP::getNegativeZero(Ty);
Reid Spencer2eadb532007-01-21 00:29:26 +00002401
2402 return Constant::getNullValue(Ty);
2403}
2404
Vikram S. Adve4c485332002-07-15 18:19:33 +00002405// destroyConstant - Remove the constant from the constant table...
2406//
2407void ConstantExpr::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00002408 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002409 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002410}
2411
Chris Lattner3cd8c562002-07-30 18:54:25 +00002412const char *ConstantExpr::getOpcodeName() const {
2413 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002414}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00002415
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002416//===----------------------------------------------------------------------===//
2417// replaceUsesOfWithOnConstant implementations
2418
Chris Lattner913849b2007-08-21 00:55:23 +00002419/// replaceUsesOfWithOnConstant - Update this constant array to change uses of
2420/// 'From' to be uses of 'To'. This must update the uniquing data structures
2421/// etc.
2422///
2423/// Note that we intentionally replace all uses of From with To here. Consider
2424/// a large array that uses 'From' 1000 times. By handling this case all here,
2425/// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that
2426/// single invocation handles all 1000 uses. Handling them one at a time would
2427/// work, but would be really slow because it would have to unique each updated
2428/// array instance.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002429void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002430 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002431 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002432 Constant *ToC = cast<Constant>(To);
Chris Lattnerdff59112005-10-04 18:47:09 +00002433
Jim Laskeyc03caef2006-07-17 17:38:29 +00002434 std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnerb64419a2005-10-03 22:51:37 +00002435 Lookup.first.first = getType();
2436 Lookup.second = this;
Chris Lattnerdff59112005-10-04 18:47:09 +00002437
Chris Lattnerb64419a2005-10-03 22:51:37 +00002438 std::vector<Constant*> &Values = Lookup.first.second;
2439 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattnerdff59112005-10-04 18:47:09 +00002440
Chris Lattner8760ec72005-10-04 01:17:50 +00002441 // Fill values with the modified operands of the constant array. Also,
2442 // compute whether this turns into an all-zeros array.
Chris Lattnerdff59112005-10-04 18:47:09 +00002443 bool isAllZeros = false;
Chris Lattner913849b2007-08-21 00:55:23 +00002444 unsigned NumUpdated = 0;
Chris Lattnerdff59112005-10-04 18:47:09 +00002445 if (!ToC->isNullValue()) {
Chris Lattner913849b2007-08-21 00:55:23 +00002446 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2447 Constant *Val = cast<Constant>(O->get());
2448 if (Val == From) {
2449 Val = ToC;
2450 ++NumUpdated;
2451 }
2452 Values.push_back(Val);
2453 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002454 } else {
2455 isAllZeros = true;
2456 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2457 Constant *Val = cast<Constant>(O->get());
Chris Lattner913849b2007-08-21 00:55:23 +00002458 if (Val == From) {
2459 Val = ToC;
2460 ++NumUpdated;
2461 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002462 Values.push_back(Val);
2463 if (isAllZeros) isAllZeros = Val->isNullValue();
2464 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002465 }
2466
Chris Lattnerb64419a2005-10-03 22:51:37 +00002467 Constant *Replacement = 0;
2468 if (isAllZeros) {
2469 Replacement = ConstantAggregateZero::get(getType());
2470 } else {
2471 // Check to see if we have this array type already.
2472 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002473 ArrayConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002474 ArrayConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002475
2476 if (Exists) {
2477 Replacement = I->second;
2478 } else {
2479 // Okay, the new shape doesn't exist in the system yet. Instead of
2480 // creating a new constant array, inserting it, replaceallusesof'ing the
2481 // old with the new, then deleting the old... just update the current one
2482 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002483 ArrayConstants->MoveConstantToNewSlot(this, I);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002484
Chris Lattner913849b2007-08-21 00:55:23 +00002485 // Update to the new value. Optimize for the case when we have a single
2486 // operand that we're changing, but handle bulk updates efficiently.
2487 if (NumUpdated == 1) {
2488 unsigned OperandToUpdate = U-OperandList;
2489 assert(getOperand(OperandToUpdate) == From &&
2490 "ReplaceAllUsesWith broken!");
2491 setOperand(OperandToUpdate, ToC);
2492 } else {
2493 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2494 if (getOperand(i) == From)
2495 setOperand(i, ToC);
2496 }
Chris Lattnerb64419a2005-10-03 22:51:37 +00002497 return;
2498 }
2499 }
2500
2501 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002502 assert(Replacement != this && "I didn't contain From!");
2503
Chris Lattner7a1450d2005-10-04 18:13:04 +00002504 // Everyone using this now uses the replacement.
2505 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002506
2507 // Delete the old constant!
2508 destroyConstant();
2509}
2510
2511void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002512 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002513 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002514 Constant *ToC = cast<Constant>(To);
2515
Chris Lattnerdff59112005-10-04 18:47:09 +00002516 unsigned OperandToUpdate = U-OperandList;
2517 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
2518
Jim Laskeyc03caef2006-07-17 17:38:29 +00002519 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattner8760ec72005-10-04 01:17:50 +00002520 Lookup.first.first = getType();
2521 Lookup.second = this;
2522 std::vector<Constant*> &Values = Lookup.first.second;
2523 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002524
Chris Lattnerdff59112005-10-04 18:47:09 +00002525
Chris Lattner8760ec72005-10-04 01:17:50 +00002526 // Fill values with the modified operands of the constant struct. Also,
2527 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00002528 bool isAllZeros = false;
2529 if (!ToC->isNullValue()) {
2530 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
2531 Values.push_back(cast<Constant>(O->get()));
2532 } else {
2533 isAllZeros = true;
2534 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2535 Constant *Val = cast<Constant>(O->get());
2536 Values.push_back(Val);
2537 if (isAllZeros) isAllZeros = Val->isNullValue();
2538 }
Chris Lattner8760ec72005-10-04 01:17:50 +00002539 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002540 Values[OperandToUpdate] = ToC;
2541
Chris Lattner8760ec72005-10-04 01:17:50 +00002542 Constant *Replacement = 0;
2543 if (isAllZeros) {
2544 Replacement = ConstantAggregateZero::get(getType());
2545 } else {
2546 // Check to see if we have this array type already.
2547 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002548 StructConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002549 StructConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00002550
2551 if (Exists) {
2552 Replacement = I->second;
2553 } else {
2554 // Okay, the new shape doesn't exist in the system yet. Instead of
2555 // creating a new constant struct, inserting it, replaceallusesof'ing the
2556 // old with the new, then deleting the old... just update the current one
2557 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002558 StructConstants->MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00002559
Chris Lattnerdff59112005-10-04 18:47:09 +00002560 // Update to the new value.
2561 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00002562 return;
2563 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002564 }
2565
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002566 assert(Replacement != this && "I didn't contain From!");
2567
Chris Lattner7a1450d2005-10-04 18:13:04 +00002568 // Everyone using this now uses the replacement.
2569 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002570
2571 // Delete the old constant!
2572 destroyConstant();
2573}
2574
Reid Spencerd84d35b2007-02-15 02:26:10 +00002575void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002576 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002577 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2578
2579 std::vector<Constant*> Values;
2580 Values.reserve(getNumOperands()); // Build replacement array...
2581 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2582 Constant *Val = getOperand(i);
2583 if (Val == From) Val = cast<Constant>(To);
2584 Values.push_back(Val);
2585 }
2586
Reid Spencerd84d35b2007-02-15 02:26:10 +00002587 Constant *Replacement = ConstantVector::get(getType(), Values);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002588 assert(Replacement != this && "I didn't contain From!");
2589
Chris Lattner7a1450d2005-10-04 18:13:04 +00002590 // Everyone using this now uses the replacement.
2591 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002592
2593 // Delete the old constant!
2594 destroyConstant();
2595}
2596
2597void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002598 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002599 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2600 Constant *To = cast<Constant>(ToV);
2601
2602 Constant *Replacement = 0;
2603 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerb5d70302007-02-19 20:01:23 +00002604 SmallVector<Constant*, 8> Indices;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002605 Constant *Pointer = getOperand(0);
2606 Indices.reserve(getNumOperands()-1);
2607 if (Pointer == From) Pointer = To;
2608
2609 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2610 Constant *Val = getOperand(i);
2611 if (Val == From) Val = To;
2612 Indices.push_back(Val);
2613 }
Chris Lattnerb5d70302007-02-19 20:01:23 +00002614 Replacement = ConstantExpr::getGetElementPtr(Pointer,
2615 &Indices[0], Indices.size());
Dan Gohman12fce772008-05-15 19:50:34 +00002616 } else if (getOpcode() == Instruction::ExtractValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002617 Constant *Agg = getOperand(0);
Dan Gohman12fce772008-05-15 19:50:34 +00002618 if (Agg == From) Agg = To;
2619
Dan Gohman1ecaf452008-05-31 00:58:22 +00002620 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002621 Replacement = ConstantExpr::getExtractValue(Agg,
2622 &Indices[0], Indices.size());
2623 } else if (getOpcode() == Instruction::InsertValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002624 Constant *Agg = getOperand(0);
2625 Constant *Val = getOperand(1);
Dan Gohman12fce772008-05-15 19:50:34 +00002626 if (Agg == From) Agg = To;
2627 if (Val == From) Val = To;
2628
Dan Gohman1ecaf452008-05-31 00:58:22 +00002629 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002630 Replacement = ConstantExpr::getInsertValue(Agg, Val,
2631 &Indices[0], Indices.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002632 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002633 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002634 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002635 } else if (getOpcode() == Instruction::Select) {
2636 Constant *C1 = getOperand(0);
2637 Constant *C2 = getOperand(1);
2638 Constant *C3 = getOperand(2);
2639 if (C1 == From) C1 = To;
2640 if (C2 == From) C2 = To;
2641 if (C3 == From) C3 = To;
2642 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00002643 } else if (getOpcode() == Instruction::ExtractElement) {
2644 Constant *C1 = getOperand(0);
2645 Constant *C2 = getOperand(1);
2646 if (C1 == From) C1 = To;
2647 if (C2 == From) C2 = To;
2648 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00002649 } else if (getOpcode() == Instruction::InsertElement) {
2650 Constant *C1 = getOperand(0);
2651 Constant *C2 = getOperand(1);
2652 Constant *C3 = getOperand(1);
2653 if (C1 == From) C1 = To;
2654 if (C2 == From) C2 = To;
2655 if (C3 == From) C3 = To;
2656 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2657 } else if (getOpcode() == Instruction::ShuffleVector) {
2658 Constant *C1 = getOperand(0);
2659 Constant *C2 = getOperand(1);
2660 Constant *C3 = getOperand(2);
2661 if (C1 == From) C1 = To;
2662 if (C2 == From) C2 = To;
2663 if (C3 == From) C3 = To;
2664 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00002665 } else if (isCompare()) {
2666 Constant *C1 = getOperand(0);
2667 Constant *C2 = getOperand(1);
2668 if (C1 == From) C1 = To;
2669 if (C2 == From) C2 = To;
2670 if (getOpcode() == Instruction::ICmp)
2671 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002672 else if (getOpcode() == Instruction::FCmp)
Reid Spenceree3c9912006-12-04 05:19:50 +00002673 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002674 else if (getOpcode() == Instruction::VICmp)
2675 Replacement = ConstantExpr::getVICmp(getPredicate(), C1, C2);
2676 else {
2677 assert(getOpcode() == Instruction::VFCmp);
2678 Replacement = ConstantExpr::getVFCmp(getPredicate(), C1, C2);
2679 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002680 } else if (getNumOperands() == 2) {
2681 Constant *C1 = getOperand(0);
2682 Constant *C2 = getOperand(1);
2683 if (C1 == From) C1 = To;
2684 if (C2 == From) C2 = To;
2685 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2686 } else {
2687 assert(0 && "Unknown ConstantExpr type!");
2688 return;
2689 }
2690
2691 assert(Replacement != this && "I didn't contain From!");
2692
Chris Lattner7a1450d2005-10-04 18:13:04 +00002693 // Everyone using this now uses the replacement.
2694 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002695
2696 // Delete the old constant!
2697 destroyConstant();
Matthijs Kooijmanba5d7ef2008-07-03 07:46:41 +00002698}