blob: 69c503dff956873fc9955489bad106d3a551d5db [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"
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000019#include "llvm/MDNode.h"
Chris Lattnerd7a73302001-10-13 06:57:33 +000020#include "llvm/Module.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000021#include "llvm/ADT/FoldingSet.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000022#include "llvm/ADT/StringExtras.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000023#include "llvm/ADT/StringMap.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000024#include "llvm/Support/Compiler.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000025#include "llvm/Support/Debug.h"
Chris Lattner69edc982006-09-28 00:35:06 +000026#include "llvm/Support/ManagedStatic.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000027#include "llvm/Support/MathExtras.h"
Chris Lattnera80bf0b2007-02-20 06:39:57 +000028#include "llvm/ADT/DenseMap.h"
Chris Lattnerb5d70302007-02-19 20:01:23 +000029#include "llvm/ADT/SmallVector.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000030#include <algorithm>
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000031#include <map>
Chris Lattner189d19f2003-11-21 20:23:48 +000032using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000033
Chris Lattner2f7c9632001-06-06 20:29:01 +000034//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000035// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000036//===----------------------------------------------------------------------===//
37
Chris Lattner3462ae32001-12-03 22:26:30 +000038void Constant::destroyConstantImpl() {
39 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000040 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000041 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +000042 // but they don't know that. Because we only find out when the CPV is
43 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000044 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000045 //
46 while (!use_empty()) {
47 Value *V = use_back();
48#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000049 if (!isa<Constant>(V))
Bill Wendling6a462f12006-11-17 08:03:48 +000050 DOUT << "While deleting: " << *this
51 << "\n\nUse still stuck around after Def is destroyed: "
52 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000053#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000054 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000055 Constant *CV = cast<Constant>(V);
56 CV->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +000057
58 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000059 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000060 }
61
62 // Value has no outstanding references it is safe to delete it now...
63 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000064}
Chris Lattner2f7c9632001-06-06 20:29:01 +000065
Chris Lattner23dd1f62006-10-20 00:27:06 +000066/// canTrap - Return true if evaluation of this constant could trap. This is
67/// true for things like constant expressions that could divide by zero.
68bool Constant::canTrap() const {
69 assert(getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
70 // The only thing that could possibly trap are constant exprs.
71 const ConstantExpr *CE = dyn_cast<ConstantExpr>(this);
72 if (!CE) return false;
73
74 // ConstantExpr traps if any operands can trap.
75 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
76 if (getOperand(i)->canTrap())
77 return true;
78
79 // Otherwise, only specific operations can trap.
80 switch (CE->getOpcode()) {
81 default:
82 return false;
Reid Spencer7e80b0b2006-10-26 06:15:43 +000083 case Instruction::UDiv:
84 case Instruction::SDiv:
85 case Instruction::FDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +000086 case Instruction::URem:
87 case Instruction::SRem:
88 case Instruction::FRem:
Chris Lattner23dd1f62006-10-20 00:27:06 +000089 // Div and rem can trap if the RHS is not known to be non-zero.
90 if (!isa<ConstantInt>(getOperand(1)) || getOperand(1)->isNullValue())
91 return true;
92 return false;
93 }
94}
95
Anton Korobeynikov7437b592009-03-29 17:13:18 +000096/// ContainsRelocations - Return true if the constant value contains relocations
97/// which cannot be resolved at compile time. Kind argument is used to filter
98/// only 'interesting' sorts of relocations.
99bool Constant::ContainsRelocations(unsigned Kind) const {
100 if (const GlobalValue* GV = dyn_cast<GlobalValue>(this)) {
101 bool isLocal = GV->hasLocalLinkage();
102 if ((Kind & Reloc::Local) && isLocal) {
103 // Global has local linkage and 'local' kind of relocations are
104 // requested
105 return true;
106 }
107
108 if ((Kind & Reloc::Global) && !isLocal) {
109 // Global has non-local linkage and 'global' kind of relocations are
110 // requested
111 return true;
112 }
Anton Korobeynikov255a3cb2009-03-30 15:28:21 +0000113
114 return false;
Anton Korobeynikov7437b592009-03-29 17:13:18 +0000115 }
116
Evan Chengf9e003b2007-03-08 00:59:12 +0000117 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Anton Korobeynikovd5e8e932009-03-30 15:28:00 +0000118 if (getOperand(i)->ContainsRelocations(Kind))
Evan Chengf9e003b2007-03-08 00:59:12 +0000119 return true;
Anton Korobeynikov7437b592009-03-29 17:13:18 +0000120
Evan Chengf9e003b2007-03-08 00:59:12 +0000121 return false;
122}
123
Chris Lattnerb1585a92002-08-13 17:50:20 +0000124// Static constructor to create a '0' constant of arbitrary type...
125Constant *Constant::getNullValue(const Type *Ty) {
Dale Johannesen98d3a082007-09-14 22:26:36 +0000126 static uint64_t zero[2] = {0, 0};
Chris Lattner6b727592004-06-17 18:19:28 +0000127 switch (Ty->getTypeID()) {
Chris Lattnerdbcb0d32007-02-20 05:46:39 +0000128 case Type::IntegerTyID:
129 return ConstantInt::get(Ty, 0);
130 case Type::FloatTyID:
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000131 return ConstantFP::get(APFloat(APInt(32, 0)));
Chris Lattnerdbcb0d32007-02-20 05:46:39 +0000132 case Type::DoubleTyID:
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000133 return ConstantFP::get(APFloat(APInt(64, 0)));
Dale Johannesenbdad8092007-08-09 22:51:36 +0000134 case Type::X86_FP80TyID:
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000135 return ConstantFP::get(APFloat(APInt(80, 2, zero)));
Dale Johannesenbdad8092007-08-09 22:51:36 +0000136 case Type::FP128TyID:
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000137 return ConstantFP::get(APFloat(APInt(128, 2, zero), true));
Dale Johannesen98d3a082007-09-14 22:26:36 +0000138 case Type::PPC_FP128TyID:
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000139 return ConstantFP::get(APFloat(APInt(128, 2, zero)));
Misha Brukmanb1c93172005-04-21 23:48:37 +0000140 case Type::PointerTyID:
Chris Lattnerb1585a92002-08-13 17:50:20 +0000141 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner9fba3da2004-02-15 05:53:04 +0000142 case Type::StructTyID:
143 case Type::ArrayTyID:
Reid Spencerd84d35b2007-02-15 02:26:10 +0000144 case Type::VectorTyID:
Chris Lattner9fba3da2004-02-15 05:53:04 +0000145 return ConstantAggregateZero::get(Ty);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000146 default:
Reid Spencercf394bf2004-07-04 11:51:24 +0000147 // Function, Label, or Opaque type?
148 assert(!"Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000149 return 0;
150 }
151}
152
Chris Lattner72e39582007-06-15 06:10:53 +0000153Constant *Constant::getAllOnesValue(const Type *Ty) {
154 if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty))
155 return ConstantInt::get(APInt::getAllOnesValue(ITy->getBitWidth()));
156 return ConstantVector::getAllOnesValue(cast<VectorType>(Ty));
157}
Chris Lattnerb1585a92002-08-13 17:50:20 +0000158
159// Static constructor to create an integral constant with all bits set
Zhou Sheng75b871f2007-01-11 12:24:14 +0000160ConstantInt *ConstantInt::getAllOnesValue(const Type *Ty) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000161 if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty))
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000162 return ConstantInt::get(APInt::getAllOnesValue(ITy->getBitWidth()));
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000163 return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000164}
165
Dan Gohman30978072007-05-24 14:36:04 +0000166/// @returns the value for a vector integer constant of the given type that
Chris Lattnerecab54c2007-01-04 01:49:26 +0000167/// has all its bits set to true.
168/// @brief Get the all ones value
Reid Spencerd84d35b2007-02-15 02:26:10 +0000169ConstantVector *ConstantVector::getAllOnesValue(const VectorType *Ty) {
Chris Lattnerecab54c2007-01-04 01:49:26 +0000170 std::vector<Constant*> Elts;
171 Elts.resize(Ty->getNumElements(),
Zhou Sheng75b871f2007-01-11 12:24:14 +0000172 ConstantInt::getAllOnesValue(Ty->getElementType()));
Dan Gohman30978072007-05-24 14:36:04 +0000173 assert(Elts[0] && "Not a vector integer type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +0000174 return cast<ConstantVector>(ConstantVector::get(Elts));
Chris Lattnerecab54c2007-01-04 01:49:26 +0000175}
176
177
Chris Lattner2105d662008-07-10 00:28:11 +0000178/// getVectorElements - This method, which is only valid on constant of vector
179/// type, returns the elements of the vector in the specified smallvector.
Chris Lattnerc5098a22008-07-14 05:10:41 +0000180/// This handles breaking down a vector undef into undef elements, etc. For
181/// constant exprs and other cases we can't handle, we return an empty vector.
Chris Lattner2105d662008-07-10 00:28:11 +0000182void Constant::getVectorElements(SmallVectorImpl<Constant*> &Elts) const {
183 assert(isa<VectorType>(getType()) && "Not a vector constant!");
184
185 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) {
186 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i)
187 Elts.push_back(CV->getOperand(i));
188 return;
189 }
190
191 const VectorType *VT = cast<VectorType>(getType());
192 if (isa<ConstantAggregateZero>(this)) {
193 Elts.assign(VT->getNumElements(),
194 Constant::getNullValue(VT->getElementType()));
195 return;
196 }
197
Chris Lattnerc5098a22008-07-14 05:10:41 +0000198 if (isa<UndefValue>(this)) {
199 Elts.assign(VT->getNumElements(), UndefValue::get(VT->getElementType()));
200 return;
201 }
202
203 // Unknown type, must be constant expr etc.
Chris Lattner2105d662008-07-10 00:28:11 +0000204}
205
206
207
Chris Lattner2f7c9632001-06-06 20:29:01 +0000208//===----------------------------------------------------------------------===//
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000209// ConstantInt
Chris Lattner2f7c9632001-06-06 20:29:01 +0000210//===----------------------------------------------------------------------===//
211
Reid Spencerb31bffe2007-02-26 23:54:03 +0000212ConstantInt::ConstantInt(const IntegerType *Ty, const APInt& V)
Chris Lattner5db2f472007-02-20 05:55:46 +0000213 : Constant(Ty, ConstantIntVal, 0, 0), Val(V) {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000214 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000215}
216
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000217ConstantInt *ConstantInt::TheTrueVal = 0;
218ConstantInt *ConstantInt::TheFalseVal = 0;
219
220namespace llvm {
221 void CleanupTrueFalse(void *) {
222 ConstantInt::ResetTrueFalse();
223 }
224}
225
226static ManagedCleanup<llvm::CleanupTrueFalse> TrueFalseCleanup;
227
228ConstantInt *ConstantInt::CreateTrueFalseVals(bool WhichOne) {
229 assert(TheTrueVal == 0 && TheFalseVal == 0);
230 TheTrueVal = get(Type::Int1Ty, 1);
231 TheFalseVal = get(Type::Int1Ty, 0);
232
233 // Ensure that llvm_shutdown nulls out TheTrueVal/TheFalseVal.
234 TrueFalseCleanup.Register();
235
236 return WhichOne ? TheTrueVal : TheFalseVal;
237}
238
239
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000240namespace {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000241 struct DenseMapAPIntKeyInfo {
242 struct KeyTy {
243 APInt val;
244 const Type* type;
245 KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
246 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
247 bool operator==(const KeyTy& that) const {
248 return type == that.type && this->val == that.val;
249 }
250 bool operator!=(const KeyTy& that) const {
251 return !this->operator==(that);
252 }
253 };
254 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
255 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000256 static unsigned getHashValue(const KeyTy &Key) {
Chris Lattner0625bd62007-09-17 18:34:04 +0000257 return DenseMapInfo<void*>::getHashValue(Key.type) ^
Reid Spencerb31bffe2007-02-26 23:54:03 +0000258 Key.val.getHashValue();
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000259 }
Chris Lattner0625bd62007-09-17 18:34:04 +0000260 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
261 return LHS == RHS;
262 }
Dale Johannesena719a602007-08-24 00:56:33 +0000263 static bool isPod() { return false; }
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000264 };
265}
266
267
Reid Spencerb31bffe2007-02-26 23:54:03 +0000268typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
269 DenseMapAPIntKeyInfo> IntMapTy;
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000270static ManagedStatic<IntMapTy> IntConstants;
271
Reid Spencer362fb292007-03-19 20:39:08 +0000272ConstantInt *ConstantInt::get(const Type *Ty, uint64_t V, bool isSigned) {
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000273 const IntegerType *ITy = cast<IntegerType>(Ty);
Reid Spencer362fb292007-03-19 20:39:08 +0000274 return get(APInt(ITy->getBitWidth(), V, isSigned));
Reid Spencerb31bffe2007-02-26 23:54:03 +0000275}
276
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000277// Get a ConstantInt from an APInt. Note that the value stored in the DenseMap
Dan Gohmanb3efe032008-02-07 02:30:40 +0000278// as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the
Reid Spencerb31bffe2007-02-26 23:54:03 +0000279// operator== and operator!= to ensure that the DenseMap doesn't attempt to
280// compare APInt's of different widths, which would violate an APInt class
281// invariant which generates an assertion.
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000282ConstantInt *ConstantInt::get(const APInt& V) {
283 // Get the corresponding integer type for the bit width of the value.
284 const IntegerType *ITy = IntegerType::get(V.getBitWidth());
Reid Spencerb31bffe2007-02-26 23:54:03 +0000285 // get an existing value or the insertion position
Reid Spencerd1bbfa52007-03-01 19:30:34 +0000286 DenseMapAPIntKeyInfo::KeyTy Key(V, ITy);
Reid Spencerb31bffe2007-02-26 23:54:03 +0000287 ConstantInt *&Slot = (*IntConstants)[Key];
288 // if it exists, return it.
289 if (Slot)
290 return Slot;
291 // otherwise create a new one, insert it, and return it.
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000292 return Slot = new ConstantInt(ITy, V);
293}
294
295//===----------------------------------------------------------------------===//
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000296// ConstantFP
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000297//===----------------------------------------------------------------------===//
298
Chris Lattner98bd9392008-04-09 06:38:30 +0000299static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
300 if (Ty == Type::FloatTy)
301 return &APFloat::IEEEsingle;
302 if (Ty == Type::DoubleTy)
303 return &APFloat::IEEEdouble;
304 if (Ty == Type::X86_FP80Ty)
305 return &APFloat::x87DoubleExtended;
306 else if (Ty == Type::FP128Ty)
307 return &APFloat::IEEEquad;
308
309 assert(Ty == Type::PPC_FP128Ty && "Unknown FP format");
310 return &APFloat::PPCDoubleDouble;
311}
312
Dale Johannesend246b2c2007-08-30 00:23:21 +0000313ConstantFP::ConstantFP(const Type *Ty, const APFloat& V)
314 : Constant(Ty, ConstantFPVal, 0, 0), Val(V) {
Chris Lattner98bd9392008-04-09 06:38:30 +0000315 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
316 "FP type Mismatch");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000317}
318
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000319bool ConstantFP::isNullValue() const {
Dale Johannesena719a602007-08-24 00:56:33 +0000320 return Val.isZero() && !Val.isNegative();
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000321}
322
Dale Johannesen98d3a082007-09-14 22:26:36 +0000323ConstantFP *ConstantFP::getNegativeZero(const Type *Ty) {
324 APFloat apf = cast <ConstantFP>(Constant::getNullValue(Ty))->getValueAPF();
325 apf.changeSign();
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000326 return ConstantFP::get(apf);
Dale Johannesen98d3a082007-09-14 22:26:36 +0000327}
328
Dale Johannesend246b2c2007-08-30 00:23:21 +0000329bool ConstantFP::isExactlyValue(const APFloat& V) const {
330 return Val.bitwiseIsEqual(V);
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000331}
332
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000333namespace {
Dale Johannesena719a602007-08-24 00:56:33 +0000334 struct DenseMapAPFloatKeyInfo {
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000335 struct KeyTy {
336 APFloat val;
337 KeyTy(const APFloat& V) : val(V){}
338 KeyTy(const KeyTy& that) : val(that.val) {}
339 bool operator==(const KeyTy& that) const {
340 return this->val.bitwiseIsEqual(that.val);
341 }
342 bool operator!=(const KeyTy& that) const {
343 return !this->operator==(that);
344 }
345 };
346 static inline KeyTy getEmptyKey() {
347 return KeyTy(APFloat(APFloat::Bogus,1));
Reid Spencerb31bffe2007-02-26 23:54:03 +0000348 }
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000349 static inline KeyTy getTombstoneKey() {
350 return KeyTy(APFloat(APFloat::Bogus,2));
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000351 }
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000352 static unsigned getHashValue(const KeyTy &Key) {
353 return Key.val.getHashValue();
Dale Johannesena719a602007-08-24 00:56:33 +0000354 }
Chris Lattner0625bd62007-09-17 18:34:04 +0000355 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
356 return LHS == RHS;
357 }
Dale Johannesena719a602007-08-24 00:56:33 +0000358 static bool isPod() { return false; }
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000359 };
360}
361
362//---- ConstantFP::get() implementation...
363//
Dale Johannesenbdea32d2007-08-24 22:09:56 +0000364typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
Dale Johannesena719a602007-08-24 00:56:33 +0000365 DenseMapAPFloatKeyInfo> FPMapTy;
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000366
Dale Johannesena719a602007-08-24 00:56:33 +0000367static ManagedStatic<FPMapTy> FPConstants;
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000368
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000369ConstantFP *ConstantFP::get(const APFloat &V) {
Dale Johannesend246b2c2007-08-30 00:23:21 +0000370 DenseMapAPFloatKeyInfo::KeyTy Key(V);
371 ConstantFP *&Slot = (*FPConstants)[Key];
372 if (Slot) return Slot;
Chris Lattnerb5b3e312008-04-09 00:45:01 +0000373
374 const Type *Ty;
375 if (&V.getSemantics() == &APFloat::IEEEsingle)
376 Ty = Type::FloatTy;
377 else if (&V.getSemantics() == &APFloat::IEEEdouble)
378 Ty = Type::DoubleTy;
379 else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
380 Ty = Type::X86_FP80Ty;
381 else if (&V.getSemantics() == &APFloat::IEEEquad)
382 Ty = Type::FP128Ty;
383 else {
384 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble&&"Unknown FP format");
385 Ty = Type::PPC_FP128Ty;
386 }
387
Dale Johannesend246b2c2007-08-30 00:23:21 +0000388 return Slot = new ConstantFP(Ty, V);
389}
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000390
Chris Lattner98bd9392008-04-09 06:38:30 +0000391/// get() - This returns a constant fp for the specified value in the
392/// specified type. This should only be used for simple constant values like
393/// 2.0/1.0 etc, that are known-valid both as double and as the target format.
394ConstantFP *ConstantFP::get(const Type *Ty, double V) {
395 APFloat FV(V);
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000396 bool ignored;
397 FV.convert(*TypeToFloatSemantics(Ty), APFloat::rmNearestTiesToEven, &ignored);
Chris Lattner98bd9392008-04-09 06:38:30 +0000398 return get(FV);
399}
400
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000401//===----------------------------------------------------------------------===//
402// ConstantXXX Classes
403//===----------------------------------------------------------------------===//
404
405
Chris Lattner3462ae32001-12-03 22:26:30 +0000406ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000407 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000408 : Constant(T, ConstantArrayVal,
409 OperandTraits<ConstantArray>::op_end(this) - V.size(),
410 V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000411 assert(V.size() == T->getNumElements() &&
412 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000413 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000414 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
415 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000416 Constant *C = *I;
417 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000418 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000419 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000420 "Initializer for array element doesn't match array element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000421 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000422 }
423}
424
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000425
Chris Lattner3462ae32001-12-03 22:26:30 +0000426ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000427 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000428 : Constant(T, ConstantStructVal,
429 OperandTraits<ConstantStruct>::op_end(this) - V.size(),
430 V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000431 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000432 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000433 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000434 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
435 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000436 Constant *C = *I;
437 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000438 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000439 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000440 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000441 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000442 "Initializer for struct element doesn't match struct element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000443 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000444 }
445}
446
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000447
Reid Spencerd84d35b2007-02-15 02:26:10 +0000448ConstantVector::ConstantVector(const VectorType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000449 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000450 : Constant(T, ConstantVectorVal,
451 OperandTraits<ConstantVector>::op_end(this) - V.size(),
452 V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000453 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000454 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
455 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000456 Constant *C = *I;
457 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000458 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000459 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Dan Gohman30978072007-05-24 14:36:04 +0000460 "Initializer for vector element doesn't match vector element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000461 *OL = C;
Brian Gaeke02209042004-08-20 06:00:58 +0000462 }
463}
464
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000465
Gabor Greiff6caff662008-05-10 08:32:32 +0000466namespace llvm {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000467// We declare several classes private to this file, so use an anonymous
468// namespace
469namespace {
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000470
Gordon Henriksen14a55692007-12-10 02:14:30 +0000471/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
472/// behind the scenes to implement unary constant exprs.
473class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000474 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000475public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000476 // allocate space for exactly one operand
477 void *operator new(size_t s) {
478 return User::operator new(s, 1);
479 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000480 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
Gabor Greiff6caff662008-05-10 08:32:32 +0000481 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
482 Op<0>() = C;
483 }
484 /// Transparently provide more efficient getOperand methods.
485 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000486};
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000487
Gordon Henriksen14a55692007-12-10 02:14:30 +0000488/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
489/// behind the scenes to implement binary constant exprs.
490class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000491 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000492public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000493 // allocate space for exactly two operands
494 void *operator new(size_t s) {
495 return User::operator new(s, 2);
496 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000497 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Gabor Greiff6caff662008-05-10 08:32:32 +0000498 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000499 Op<0>() = C1;
500 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000501 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000502 /// Transparently provide more efficient getOperand methods.
503 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000504};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000505
Gordon Henriksen14a55692007-12-10 02:14:30 +0000506/// SelectConstantExpr - This class is private to Constants.cpp, and is used
507/// behind the scenes to implement select constant exprs.
508class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000509 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000510public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000511 // allocate space for exactly three operands
512 void *operator new(size_t s) {
513 return User::operator new(s, 3);
514 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000515 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Gabor Greiff6caff662008-05-10 08:32:32 +0000516 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000517 Op<0>() = C1;
518 Op<1>() = C2;
519 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000520 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000521 /// Transparently provide more efficient getOperand methods.
522 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000523};
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000524
Gordon Henriksen14a55692007-12-10 02:14:30 +0000525/// ExtractElementConstantExpr - This class is private to
526/// Constants.cpp, and is used behind the scenes to implement
527/// extractelement constant exprs.
528class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000529 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000530public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000531 // allocate space for exactly two operands
532 void *operator new(size_t s) {
533 return User::operator new(s, 2);
534 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000535 ExtractElementConstantExpr(Constant *C1, Constant *C2)
536 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000537 Instruction::ExtractElement, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000538 Op<0>() = C1;
539 Op<1>() = C2;
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 Bocchino23004482006-01-10 19:05:34 +0000544
Gordon Henriksen14a55692007-12-10 02:14:30 +0000545/// InsertElementConstantExpr - This class is private to
546/// Constants.cpp, and is used behind the scenes to implement
547/// insertelement constant exprs.
548class VISIBILITY_HIDDEN InsertElementConstantExpr : 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 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
556 : ConstantExpr(C1->getType(), Instruction::InsertElement,
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};
Robert Bocchinoca27f032006-01-17 20:07:22 +0000565
Gordon Henriksen14a55692007-12-10 02:14:30 +0000566/// ShuffleVectorConstantExpr - This class is private to
567/// Constants.cpp, and is used behind the scenes to implement
568/// shufflevector constant exprs.
569class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000570 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000571public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000572 // allocate space for exactly three operands
573 void *operator new(size_t s) {
574 return User::operator new(s, 3);
575 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000576 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Nate Begeman94aa38d2009-02-12 21:28:33 +0000577 : ConstantExpr(VectorType::get(
578 cast<VectorType>(C1->getType())->getElementType(),
579 cast<VectorType>(C3->getType())->getNumElements()),
580 Instruction::ShuffleVector,
Gabor Greiff6caff662008-05-10 08:32:32 +0000581 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000582 Op<0>() = C1;
583 Op<1>() = C2;
584 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000585 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000586 /// Transparently provide more efficient getOperand methods.
587 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000588};
589
Dan Gohman12fce772008-05-15 19:50:34 +0000590/// ExtractValueConstantExpr - This class is private to
591/// Constants.cpp, and is used behind the scenes to implement
592/// extractvalue constant exprs.
593class VISIBILITY_HIDDEN ExtractValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000594 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000595public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000596 // allocate space for exactly one operand
597 void *operator new(size_t s) {
598 return User::operator new(s, 1);
Dan Gohman12fce772008-05-15 19:50:34 +0000599 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000600 ExtractValueConstantExpr(Constant *Agg,
601 const SmallVector<unsigned, 4> &IdxList,
602 const Type *DestTy)
603 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
604 Indices(IdxList) {
605 Op<0>() = Agg;
606 }
607
Dan Gohman7bb04502008-05-31 19:09:08 +0000608 /// Indices - These identify which value to extract.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000609 const SmallVector<unsigned, 4> Indices;
610
Dan Gohman12fce772008-05-15 19:50:34 +0000611 /// Transparently provide more efficient getOperand methods.
612 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
613};
614
615/// InsertValueConstantExpr - This class is private to
616/// Constants.cpp, and is used behind the scenes to implement
617/// insertvalue constant exprs.
618class VISIBILITY_HIDDEN InsertValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000619 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000620public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000621 // allocate space for exactly one operand
622 void *operator new(size_t s) {
623 return User::operator new(s, 2);
Dan Gohman12fce772008-05-15 19:50:34 +0000624 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000625 InsertValueConstantExpr(Constant *Agg, Constant *Val,
626 const SmallVector<unsigned, 4> &IdxList,
627 const Type *DestTy)
628 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
629 Indices(IdxList) {
630 Op<0>() = Agg;
631 Op<1>() = Val;
632 }
633
Dan Gohman7bb04502008-05-31 19:09:08 +0000634 /// Indices - These identify the position for the insertion.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000635 const SmallVector<unsigned, 4> Indices;
636
Dan Gohman12fce772008-05-15 19:50:34 +0000637 /// Transparently provide more efficient getOperand methods.
638 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
639};
640
641
Gordon Henriksen14a55692007-12-10 02:14:30 +0000642/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
643/// used behind the scenes to implement getelementpr constant exprs.
Gabor Greife9ecc682008-04-06 20:25:17 +0000644class VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000645 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000646 const Type *DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000647public:
Gabor Greif697e94c2008-05-15 10:04:30 +0000648 static GetElementPtrConstantExpr *Create(Constant *C,
649 const std::vector<Constant*>&IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000650 const Type *DestTy) {
Gabor Greif697e94c2008-05-15 10:04:30 +0000651 return new(IdxList.size() + 1)
652 GetElementPtrConstantExpr(C, IdxList, DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000653 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000654 /// Transparently provide more efficient getOperand methods.
655 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000656};
657
658// CompareConstantExpr - This class is private to Constants.cpp, and is used
659// behind the scenes to implement ICmp and FCmp constant expressions. This is
660// needed in order to store the predicate value for these instructions.
661struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000662 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
663 // allocate space for exactly two operands
664 void *operator new(size_t s) {
665 return User::operator new(s, 2);
666 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000667 unsigned short predicate;
Nate Begemand2195702008-05-12 19:01:56 +0000668 CompareConstantExpr(const Type *ty, Instruction::OtherOps opc,
669 unsigned short pred, Constant* LHS, Constant* RHS)
670 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000671 Op<0>() = LHS;
672 Op<1>() = RHS;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000673 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000674 /// Transparently provide more efficient getOperand methods.
675 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000676};
677
678} // end anonymous namespace
679
Gabor Greiff6caff662008-05-10 08:32:32 +0000680template <>
681struct OperandTraits<UnaryConstantExpr> : FixedNumOperandTraits<1> {
682};
683DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
684
685template <>
686struct OperandTraits<BinaryConstantExpr> : FixedNumOperandTraits<2> {
687};
688DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
689
690template <>
691struct OperandTraits<SelectConstantExpr> : FixedNumOperandTraits<3> {
692};
693DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
694
695template <>
696struct OperandTraits<ExtractElementConstantExpr> : FixedNumOperandTraits<2> {
697};
698DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
699
700template <>
701struct OperandTraits<InsertElementConstantExpr> : FixedNumOperandTraits<3> {
702};
703DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
704
705template <>
706struct OperandTraits<ShuffleVectorConstantExpr> : FixedNumOperandTraits<3> {
707};
708DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
709
Dan Gohman12fce772008-05-15 19:50:34 +0000710template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000711struct OperandTraits<ExtractValueConstantExpr> : FixedNumOperandTraits<1> {
Dan Gohman12fce772008-05-15 19:50:34 +0000712};
Dan Gohman12fce772008-05-15 19:50:34 +0000713DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
714
715template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000716struct OperandTraits<InsertValueConstantExpr> : FixedNumOperandTraits<2> {
Dan Gohman12fce772008-05-15 19:50:34 +0000717};
Dan Gohman12fce772008-05-15 19:50:34 +0000718DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
719
Gabor Greiff6caff662008-05-10 08:32:32 +0000720template <>
721struct OperandTraits<GetElementPtrConstantExpr> : VariadicOperandTraits<1> {
722};
723
724GetElementPtrConstantExpr::GetElementPtrConstantExpr
725 (Constant *C,
726 const std::vector<Constant*> &IdxList,
727 const Type *DestTy)
728 : ConstantExpr(DestTy, Instruction::GetElementPtr,
729 OperandTraits<GetElementPtrConstantExpr>::op_end(this)
730 - (IdxList.size()+1),
731 IdxList.size()+1) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000732 OperandList[0] = C;
Gabor Greiff6caff662008-05-10 08:32:32 +0000733 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000734 OperandList[i+1] = IdxList[i];
Gabor Greiff6caff662008-05-10 08:32:32 +0000735}
736
737DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
738
739
740template <>
741struct OperandTraits<CompareConstantExpr> : FixedNumOperandTraits<2> {
742};
743DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
744
745
746} // End llvm namespace
747
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000748
749// Utility function for determining if a ConstantExpr is a CastOp or not. This
750// can't be inline because we don't want to #include Instruction.h into
751// Constant.h
752bool ConstantExpr::isCast() const {
753 return Instruction::isCast(getOpcode());
754}
755
Reid Spenceree3c9912006-12-04 05:19:50 +0000756bool ConstantExpr::isCompare() const {
Chris Lattnereab49262008-07-14 05:17:31 +0000757 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp ||
758 getOpcode() == Instruction::VICmp || getOpcode() == Instruction::VFCmp;
Reid Spenceree3c9912006-12-04 05:19:50 +0000759}
760
Dan Gohman1ecaf452008-05-31 00:58:22 +0000761bool ConstantExpr::hasIndices() const {
762 return getOpcode() == Instruction::ExtractValue ||
763 getOpcode() == Instruction::InsertValue;
764}
765
766const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const {
767 if (const ExtractValueConstantExpr *EVCE =
768 dyn_cast<ExtractValueConstantExpr>(this))
769 return EVCE->Indices;
Dan Gohmana469bdb2008-06-23 16:39:44 +0000770
771 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman1ecaf452008-05-31 00:58:22 +0000772}
773
Chris Lattner817175f2004-03-29 02:37:53 +0000774/// ConstantExpr::get* - Return some common constants without having to
775/// specify the full Instruction::OPCODE identifier.
776///
777Constant *ConstantExpr::getNeg(Constant *C) {
Dan Gohmana5b96452009-06-04 22:49:04 +0000778 // API compatibility: Adjust integer opcodes to floating-point opcodes.
779 if (C->getType()->isFPOrFPVector())
780 return getFNeg(C);
781 assert(C->getType()->isIntOrIntVector() &&
782 "Cannot NEG a nonintegral value!");
Reid Spencer2eadb532007-01-21 00:29:26 +0000783 return get(Instruction::Sub,
784 ConstantExpr::getZeroValueForNegationExpr(C->getType()),
785 C);
Chris Lattner817175f2004-03-29 02:37:53 +0000786}
Dan Gohmana5b96452009-06-04 22:49:04 +0000787Constant *ConstantExpr::getFNeg(Constant *C) {
788 assert(C->getType()->isFPOrFPVector() &&
789 "Cannot FNEG a non-floating-point value!");
790 return get(Instruction::FSub,
791 ConstantExpr::getZeroValueForNegationExpr(C->getType()),
792 C);
793}
Chris Lattner817175f2004-03-29 02:37:53 +0000794Constant *ConstantExpr::getNot(Constant *C) {
Dan Gohmana5b96452009-06-04 22:49:04 +0000795 assert(C->getType()->isIntOrIntVector() &&
796 "Cannot NOT a nonintegral value!");
Chris Lattner817175f2004-03-29 02:37:53 +0000797 return get(Instruction::Xor, C,
Dale Johannesen47a5ef32008-08-21 21:20:09 +0000798 Constant::getAllOnesValue(C->getType()));
Chris Lattner817175f2004-03-29 02:37:53 +0000799}
800Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
801 return get(Instruction::Add, C1, C2);
802}
Dan Gohmana5b96452009-06-04 22:49:04 +0000803Constant *ConstantExpr::getFAdd(Constant *C1, Constant *C2) {
804 return get(Instruction::FAdd, C1, C2);
805}
Chris Lattner817175f2004-03-29 02:37:53 +0000806Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
807 return get(Instruction::Sub, C1, C2);
808}
Dan Gohmana5b96452009-06-04 22:49:04 +0000809Constant *ConstantExpr::getFSub(Constant *C1, Constant *C2) {
810 return get(Instruction::FSub, C1, C2);
811}
Chris Lattner817175f2004-03-29 02:37:53 +0000812Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
813 return get(Instruction::Mul, C1, C2);
814}
Dan Gohmana5b96452009-06-04 22:49:04 +0000815Constant *ConstantExpr::getFMul(Constant *C1, Constant *C2) {
816 return get(Instruction::FMul, C1, C2);
817}
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000818Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2) {
819 return get(Instruction::UDiv, C1, C2);
820}
821Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2) {
822 return get(Instruction::SDiv, C1, C2);
823}
824Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) {
825 return get(Instruction::FDiv, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000826}
Reid Spencer7eb55b32006-11-02 01:53:59 +0000827Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) {
828 return get(Instruction::URem, C1, C2);
829}
830Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) {
831 return get(Instruction::SRem, C1, C2);
832}
833Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) {
834 return get(Instruction::FRem, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000835}
836Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
837 return get(Instruction::And, C1, C2);
838}
839Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
840 return get(Instruction::Or, C1, C2);
841}
842Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
843 return get(Instruction::Xor, C1, C2);
844}
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000845unsigned ConstantExpr::getPredicate() const {
Nate Begemand2195702008-05-12 19:01:56 +0000846 assert(getOpcode() == Instruction::FCmp ||
847 getOpcode() == Instruction::ICmp ||
848 getOpcode() == Instruction::VFCmp ||
849 getOpcode() == Instruction::VICmp);
Chris Lattneref650092007-10-18 16:26:24 +0000850 return ((const CompareConstantExpr*)this)->predicate;
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000851}
Chris Lattner817175f2004-03-29 02:37:53 +0000852Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
853 return get(Instruction::Shl, C1, C2);
854}
Reid Spencerfdff9382006-11-08 06:47:33 +0000855Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2) {
856 return get(Instruction::LShr, C1, C2);
Chris Lattner817175f2004-03-29 02:37:53 +0000857}
Reid Spencerfdff9382006-11-08 06:47:33 +0000858Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2) {
859 return get(Instruction::AShr, C1, C2);
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000860}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000861
Chris Lattner7c1018a2006-07-14 19:37:40 +0000862/// getWithOperandReplaced - Return a constant expression identical to this
863/// one, but with the specified operand set to the specified value.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000864Constant *
865ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +0000866 assert(OpNo < getNumOperands() && "Operand num is out of range!");
867 assert(Op->getType() == getOperand(OpNo)->getType() &&
868 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000869 if (getOperand(OpNo) == Op)
870 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000871
Chris Lattner227816342006-07-14 22:20:01 +0000872 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000873 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000874 case Instruction::Trunc:
875 case Instruction::ZExt:
876 case Instruction::SExt:
877 case Instruction::FPTrunc:
878 case Instruction::FPExt:
879 case Instruction::UIToFP:
880 case Instruction::SIToFP:
881 case Instruction::FPToUI:
882 case Instruction::FPToSI:
883 case Instruction::PtrToInt:
884 case Instruction::IntToPtr:
885 case Instruction::BitCast:
886 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000887 case Instruction::Select:
888 Op0 = (OpNo == 0) ? Op : getOperand(0);
889 Op1 = (OpNo == 1) ? Op : getOperand(1);
890 Op2 = (OpNo == 2) ? Op : getOperand(2);
891 return ConstantExpr::getSelect(Op0, Op1, Op2);
892 case Instruction::InsertElement:
893 Op0 = (OpNo == 0) ? Op : getOperand(0);
894 Op1 = (OpNo == 1) ? Op : getOperand(1);
895 Op2 = (OpNo == 2) ? Op : getOperand(2);
896 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
897 case Instruction::ExtractElement:
898 Op0 = (OpNo == 0) ? Op : getOperand(0);
899 Op1 = (OpNo == 1) ? Op : getOperand(1);
900 return ConstantExpr::getExtractElement(Op0, Op1);
901 case Instruction::ShuffleVector:
902 Op0 = (OpNo == 0) ? Op : getOperand(0);
903 Op1 = (OpNo == 1) ? Op : getOperand(1);
904 Op2 = (OpNo == 2) ? Op : getOperand(2);
905 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000906 case Instruction::GetElementPtr: {
Chris Lattnerb5d70302007-02-19 20:01:23 +0000907 SmallVector<Constant*, 8> Ops;
Dan Gohman12fce772008-05-15 19:50:34 +0000908 Ops.resize(getNumOperands()-1);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000909 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Dan Gohman12fce772008-05-15 19:50:34 +0000910 Ops[i-1] = getOperand(i);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000911 if (OpNo == 0)
Chris Lattnerb5d70302007-02-19 20:01:23 +0000912 return ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000913 Ops[OpNo-1] = Op;
Chris Lattnerb5d70302007-02-19 20:01:23 +0000914 return ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000915 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000916 default:
917 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000918 Op0 = (OpNo == 0) ? Op : getOperand(0);
919 Op1 = (OpNo == 1) ? Op : getOperand(1);
920 return ConstantExpr::get(getOpcode(), Op0, Op1);
921 }
922}
923
924/// getWithOperands - This returns the current constant expression with the
925/// operands replaced with the specified values. The specified operands must
926/// match count and type with the existing ones.
927Constant *ConstantExpr::
Chris Lattnerb078e282008-08-20 22:27:40 +0000928getWithOperands(Constant* const *Ops, unsigned NumOps) const {
929 assert(NumOps == getNumOperands() && "Operand count mismatch!");
Chris Lattner227816342006-07-14 22:20:01 +0000930 bool AnyChange = false;
Chris Lattnerb078e282008-08-20 22:27:40 +0000931 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattner227816342006-07-14 22:20:01 +0000932 assert(Ops[i]->getType() == getOperand(i)->getType() &&
933 "Operand type mismatch!");
934 AnyChange |= Ops[i] != getOperand(i);
935 }
936 if (!AnyChange) // No operands changed, return self.
937 return const_cast<ConstantExpr*>(this);
938
939 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000940 case Instruction::Trunc:
941 case Instruction::ZExt:
942 case Instruction::SExt:
943 case Instruction::FPTrunc:
944 case Instruction::FPExt:
945 case Instruction::UIToFP:
946 case Instruction::SIToFP:
947 case Instruction::FPToUI:
948 case Instruction::FPToSI:
949 case Instruction::PtrToInt:
950 case Instruction::IntToPtr:
951 case Instruction::BitCast:
952 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattner227816342006-07-14 22:20:01 +0000953 case Instruction::Select:
954 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
955 case Instruction::InsertElement:
956 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
957 case Instruction::ExtractElement:
958 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
959 case Instruction::ShuffleVector:
960 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Chris Lattnerb5d70302007-02-19 20:01:23 +0000961 case Instruction::GetElementPtr:
Chris Lattnerb078e282008-08-20 22:27:40 +0000962 return ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], NumOps-1);
Reid Spencer266e42b2006-12-23 06:05:41 +0000963 case Instruction::ICmp:
964 case Instruction::FCmp:
Nate Begeman098cc6f2008-07-25 17:56:27 +0000965 case Instruction::VICmp:
966 case Instruction::VFCmp:
Reid Spencer266e42b2006-12-23 06:05:41 +0000967 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattner227816342006-07-14 22:20:01 +0000968 default:
969 assert(getNumOperands() == 2 && "Must be binary operator?");
970 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000971 }
972}
973
Chris Lattner2f7c9632001-06-06 20:29:01 +0000974
975//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000976// isValueValidForType implementations
977
Reid Spencere7334722006-12-19 01:28:19 +0000978bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000979 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000980 if (Ty == Type::Int1Ty)
981 return Val == 0 || Val == 1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000982 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000983 return true; // always true, has to fit in largest type
984 uint64_t Max = (1ll << NumBits) - 1;
985 return Val <= Max;
Reid Spencere7334722006-12-19 01:28:19 +0000986}
987
Reid Spencere0fc4df2006-10-20 07:07:24 +0000988bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000989 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000990 if (Ty == Type::Int1Ty)
Reid Spencera94d3942007-01-19 21:13:56 +0000991 return Val == 0 || Val == 1 || Val == -1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000992 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000993 return true; // always true, has to fit in largest type
994 int64_t Min = -(1ll << (NumBits-1));
995 int64_t Max = (1ll << (NumBits-1)) - 1;
996 return (Val >= Min && Val <= Max);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000997}
998
Dale Johannesend246b2c2007-08-30 00:23:21 +0000999bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) {
1000 // convert modifies in place, so make a copy.
1001 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001002 bool losesInfo;
Chris Lattner6b727592004-06-17 18:19:28 +00001003 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001004 default:
1005 return false; // These can't be represented as floating point!
1006
Dale Johannesend246b2c2007-08-30 00:23:21 +00001007 // FIXME rounding mode needs to be more flexible
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001008 case Type::FloatTyID: {
1009 if (&Val2.getSemantics() == &APFloat::IEEEsingle)
1010 return true;
1011 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
1012 return !losesInfo;
1013 }
1014 case Type::DoubleTyID: {
1015 if (&Val2.getSemantics() == &APFloat::IEEEsingle ||
1016 &Val2.getSemantics() == &APFloat::IEEEdouble)
1017 return true;
1018 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
1019 return !losesInfo;
1020 }
Dale Johannesenbdad8092007-08-09 22:51:36 +00001021 case Type::X86_FP80TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +00001022 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
1023 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1024 &Val2.getSemantics() == &APFloat::x87DoubleExtended;
Dale Johannesenbdad8092007-08-09 22:51:36 +00001025 case Type::FP128TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +00001026 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
1027 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1028 &Val2.getSemantics() == &APFloat::IEEEquad;
Dale Johannesen007aa372007-10-11 18:07:22 +00001029 case Type::PPC_FP128TyID:
1030 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
1031 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1032 &Val2.getSemantics() == &APFloat::PPCDoubleDouble;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001033 }
Chris Lattneraa2372562006-05-24 17:04:05 +00001034}
Chris Lattner9655e542001-07-20 19:16:02 +00001035
Chris Lattner49d855c2001-09-07 16:46:31 +00001036//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +00001037// Factory Function Implementation
1038
Gabor Greiff6caff662008-05-10 08:32:32 +00001039
1040// The number of operands for each ConstantCreator::create method is
1041// determined by the ConstantTraits template.
Chris Lattner98fa07b2003-05-23 20:03:32 +00001042// ConstantCreator - A class that is used to create constants by
1043// ValueMap*. This class should be partially specialized if there is
1044// something strange that needs to be done to interface to the ctor for the
1045// constant.
1046//
Chris Lattner189d19f2003-11-21 20:23:48 +00001047namespace llvm {
Gabor Greiff6caff662008-05-10 08:32:32 +00001048 template<class ValType>
1049 struct ConstantTraits;
1050
1051 template<typename T, typename Alloc>
1052 struct VISIBILITY_HIDDEN ConstantTraits< std::vector<T, Alloc> > {
1053 static unsigned uses(const std::vector<T, Alloc>& v) {
1054 return v.size();
1055 }
1056 };
1057
Chris Lattner189d19f2003-11-21 20:23:48 +00001058 template<class ConstantClass, class TypeClass, class ValType>
Chris Lattner02157b02006-06-28 21:38:54 +00001059 struct VISIBILITY_HIDDEN ConstantCreator {
Chris Lattner189d19f2003-11-21 20:23:48 +00001060 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001061 return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V);
Chris Lattner189d19f2003-11-21 20:23:48 +00001062 }
1063 };
Misha Brukmanb1c93172005-04-21 23:48:37 +00001064
Chris Lattner189d19f2003-11-21 20:23:48 +00001065 template<class ConstantClass, class TypeClass>
Chris Lattner02157b02006-06-28 21:38:54 +00001066 struct VISIBILITY_HIDDEN ConvertConstantType {
Chris Lattner189d19f2003-11-21 20:23:48 +00001067 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
1068 assert(0 && "This type cannot be converted!\n");
1069 abort();
1070 }
1071 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001072
Chris Lattner935aa922005-10-04 17:48:46 +00001073 template<class ValType, class TypeClass, class ConstantClass,
1074 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattner02157b02006-06-28 21:38:54 +00001075 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
Chris Lattnerb64419a2005-10-03 22:51:37 +00001076 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +00001077 typedef std::pair<const Type*, ValType> MapKey;
1078 typedef std::map<MapKey, Constant *> MapTy;
1079 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
1080 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
Chris Lattnerb64419a2005-10-03 22:51:37 +00001081 private:
Chris Lattner5bbf60a52005-10-04 16:52:46 +00001082 /// Map - This is the main map from the element descriptor to the Constants.
1083 /// This is the primary way we avoid creating two of the same shape
1084 /// constant.
Chris Lattnerb50d1352003-10-05 00:17:43 +00001085 MapTy Map;
Chris Lattner935aa922005-10-04 17:48:46 +00001086
1087 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
1088 /// from the constants to their element in Map. This is important for
1089 /// removal of constants from the array, which would otherwise have to scan
1090 /// through the map with very large keys.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001091 InverseMapTy InverseMap;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001092
Jim Laskeyc03caef2006-07-17 17:38:29 +00001093 /// AbstractTypeMap - Map for abstract type constants.
1094 ///
Chris Lattnerb50d1352003-10-05 00:17:43 +00001095 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner99a669b2004-11-19 16:39:44 +00001096
Chris Lattner98fa07b2003-05-23 20:03:32 +00001097 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +00001098 typename MapTy::iterator map_end() { return Map.end(); }
Chris Lattnerb64419a2005-10-03 22:51:37 +00001099
1100 /// InsertOrGetItem - Return an iterator for the specified element.
1101 /// If the element exists in the map, the returned iterator points to the
1102 /// entry and Exists=true. If not, the iterator points to the newly
1103 /// inserted entry and returns Exists=false. Newly inserted entries have
1104 /// I->second == 0, and should be filled in.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001105 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
1106 &InsertVal,
Chris Lattnerb64419a2005-10-03 22:51:37 +00001107 bool &Exists) {
Jim Laskeyc03caef2006-07-17 17:38:29 +00001108 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001109 Exists = !IP.second;
1110 return IP.first;
1111 }
Chris Lattner5bbf60a52005-10-04 16:52:46 +00001112
Chris Lattner935aa922005-10-04 17:48:46 +00001113private:
Jim Laskeyc03caef2006-07-17 17:38:29 +00001114 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
Chris Lattner935aa922005-10-04 17:48:46 +00001115 if (HasLargeKey) {
Jim Laskeyc03caef2006-07-17 17:38:29 +00001116 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
Chris Lattner935aa922005-10-04 17:48:46 +00001117 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
1118 IMI->second->second == CP &&
1119 "InverseMap corrupt!");
1120 return IMI->second;
1121 }
1122
Jim Laskeyc03caef2006-07-17 17:38:29 +00001123 typename MapTy::iterator I =
Dan Gohmane955c482008-08-05 14:45:15 +00001124 Map.find(MapKey(static_cast<const TypeClass*>(CP->getRawType()),
1125 getValType(CP)));
Chris Lattner5bbf60a52005-10-04 16:52:46 +00001126 if (I == Map.end() || I->second != CP) {
1127 // FIXME: This should not use a linear scan. If this gets to be a
1128 // performance problem, someone should look at this.
1129 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
1130 /* empty */;
1131 }
Chris Lattner935aa922005-10-04 17:48:46 +00001132 return I;
1133 }
1134public:
1135
Chris Lattnerb64419a2005-10-03 22:51:37 +00001136 /// getOrCreate - Return the specified constant from the map, creating it if
1137 /// necessary.
Chris Lattner98fa07b2003-05-23 20:03:32 +00001138 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00001139 MapKey Lookup(Ty, V);
Dan Gohman3707f1d2008-07-11 20:58:19 +00001140 typename MapTy::iterator I = Map.find(Lookup);
Reid Spencere0fc4df2006-10-20 07:07:24 +00001141 // Is it in the map?
Dan Gohman3707f1d2008-07-11 20:58:19 +00001142 if (I != Map.end())
Reid Spencere0fc4df2006-10-20 07:07:24 +00001143 return static_cast<ConstantClass *>(I->second);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001144
1145 // If no preexisting value, create one now...
1146 ConstantClass *Result =
1147 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
1148
Chris Lattnerf97ab6d2008-08-23 03:48:35 +00001149 assert(Result->getType() == Ty && "Type specified is not correct!");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001150 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
1151
Chris Lattner935aa922005-10-04 17:48:46 +00001152 if (HasLargeKey) // Remember the reverse mapping if needed.
1153 InverseMap.insert(std::make_pair(Result, I));
1154
Chris Lattnerb50d1352003-10-05 00:17:43 +00001155 // If the type of the constant is abstract, make sure that an entry exists
1156 // for it in the AbstractTypeMap.
1157 if (Ty->isAbstract()) {
Dan Gohman3707f1d2008-07-11 20:58:19 +00001158 typename AbstractTypeMapTy::iterator TI = AbstractTypeMap.find(Ty);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001159
Dan Gohman3707f1d2008-07-11 20:58:19 +00001160 if (TI == AbstractTypeMap.end()) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00001161 // Add ourselves to the ATU list of the type.
1162 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
1163
1164 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
1165 }
1166 }
Chris Lattner98fa07b2003-05-23 20:03:32 +00001167 return Result;
1168 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001169
Chris Lattner98fa07b2003-05-23 20:03:32 +00001170 void remove(ConstantClass *CP) {
Jim Laskeyc03caef2006-07-17 17:38:29 +00001171 typename MapTy::iterator I = FindExistingElement(CP);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001172 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +00001173 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001174
Chris Lattner935aa922005-10-04 17:48:46 +00001175 if (HasLargeKey) // Remember the reverse mapping if needed.
1176 InverseMap.erase(CP);
1177
Chris Lattnerb50d1352003-10-05 00:17:43 +00001178 // Now that we found the entry, make sure this isn't the entry that
1179 // the AbstractTypeMap points to.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001180 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001181 if (Ty->isAbstract()) {
1182 assert(AbstractTypeMap.count(Ty) &&
1183 "Abstract type not in AbstractTypeMap?");
Jim Laskeyc03caef2006-07-17 17:38:29 +00001184 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
Chris Lattnerb50d1352003-10-05 00:17:43 +00001185 if (ATMEntryIt == I) {
1186 // Yes, we are removing the representative entry for this type.
1187 // See if there are any other entries of the same type.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001188 typename MapTy::iterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001189
Chris Lattnerb50d1352003-10-05 00:17:43 +00001190 // First check the entry before this one...
1191 if (TmpIt != Map.begin()) {
1192 --TmpIt;
1193 if (TmpIt->first.first != Ty) // Not the same type, move back...
1194 ++TmpIt;
1195 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001196
Chris Lattnerb50d1352003-10-05 00:17:43 +00001197 // If we didn't find the same type, try to move forward...
1198 if (TmpIt == ATMEntryIt) {
1199 ++TmpIt;
1200 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
1201 --TmpIt; // No entry afterwards with the same type
1202 }
1203
1204 // If there is another entry in the map of the same abstract type,
1205 // update the AbstractTypeMap entry now.
1206 if (TmpIt != ATMEntryIt) {
1207 ATMEntryIt = TmpIt;
1208 } else {
1209 // Otherwise, we are removing the last instance of this type
1210 // from the table. Remove from the ATM, and from user list.
1211 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
1212 AbstractTypeMap.erase(Ty);
1213 }
Chris Lattner98fa07b2003-05-23 20:03:32 +00001214 }
Chris Lattnerb50d1352003-10-05 00:17:43 +00001215 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001216
Chris Lattnerb50d1352003-10-05 00:17:43 +00001217 Map.erase(I);
1218 }
1219
Chris Lattner3b793c62005-10-04 21:35:50 +00001220
1221 /// MoveConstantToNewSlot - If we are about to change C to be the element
1222 /// specified by I, update our internal data structures to reflect this
1223 /// fact.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001224 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
Chris Lattner3b793c62005-10-04 21:35:50 +00001225 // First, remove the old location of the specified constant in the map.
Jim Laskeyc03caef2006-07-17 17:38:29 +00001226 typename MapTy::iterator OldI = FindExistingElement(C);
Chris Lattner3b793c62005-10-04 21:35:50 +00001227 assert(OldI != Map.end() && "Constant not found in constant table!");
1228 assert(OldI->second == C && "Didn't find correct element?");
1229
1230 // If this constant is the representative element for its abstract type,
1231 // update the AbstractTypeMap so that the representative element is I.
1232 if (C->getType()->isAbstract()) {
1233 typename AbstractTypeMapTy::iterator ATI =
1234 AbstractTypeMap.find(C->getType());
1235 assert(ATI != AbstractTypeMap.end() &&
1236 "Abstract type not in AbstractTypeMap?");
1237 if (ATI->second == OldI)
1238 ATI->second = I;
1239 }
1240
1241 // Remove the old entry from the map.
1242 Map.erase(OldI);
1243
1244 // Update the inverse map so that we know that this constant is now
1245 // located at descriptor I.
1246 if (HasLargeKey) {
1247 assert(I->second == C && "Bad inversemap entry!");
1248 InverseMap[C] = I;
1249 }
1250 }
1251
Chris Lattnerb50d1352003-10-05 00:17:43 +00001252 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +00001253 typename AbstractTypeMapTy::iterator I =
Jim Laskeyc03caef2006-07-17 17:38:29 +00001254 AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +00001255
1256 assert(I != AbstractTypeMap.end() &&
1257 "Abstract type not in AbstractTypeMap?");
1258
1259 // Convert a constant at a time until the last one is gone. The last one
1260 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
1261 // eliminated eventually.
1262 do {
1263 ConvertConstantType<ConstantClass,
Jim Laskeyc03caef2006-07-17 17:38:29 +00001264 TypeClass>::convert(
1265 static_cast<ConstantClass *>(I->second->second),
Chris Lattnerb50d1352003-10-05 00:17:43 +00001266 cast<TypeClass>(NewTy));
1267
Jim Laskeyc03caef2006-07-17 17:38:29 +00001268 I = AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +00001269 } while (I != AbstractTypeMap.end());
1270 }
1271
1272 // If the type became concrete without being refined to any other existing
1273 // type, we just remove ourselves from the ATU list.
1274 void typeBecameConcrete(const DerivedType *AbsTy) {
1275 AbsTy->removeAbstractTypeUser(this);
1276 }
1277
1278 void dump() const {
Bill Wendling6a462f12006-11-17 08:03:48 +00001279 DOUT << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +00001280 }
1281 };
1282}
1283
Chris Lattnera84df0a22006-09-28 23:36:21 +00001284
Chris Lattner28173502007-02-20 06:11:36 +00001285
Chris Lattner9fba3da2004-02-15 05:53:04 +00001286//---- ConstantAggregateZero::get() implementation...
1287//
1288namespace llvm {
1289 // ConstantAggregateZero does not take extra "value" argument...
1290 template<class ValType>
1291 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
1292 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
1293 return new ConstantAggregateZero(Ty);
1294 }
1295 };
1296
1297 template<>
1298 struct ConvertConstantType<ConstantAggregateZero, Type> {
1299 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
1300 // Make everyone now use a constant of the new type...
1301 Constant *New = ConstantAggregateZero::get(NewTy);
1302 assert(New != OldC && "Didn't replace constant??");
1303 OldC->uncheckedReplaceAllUsesWith(New);
1304 OldC->destroyConstant(); // This constant is now dead, destroy it.
1305 }
1306 };
1307}
1308
Chris Lattner69edc982006-09-28 00:35:06 +00001309static ManagedStatic<ValueMap<char, Type,
1310 ConstantAggregateZero> > AggZeroConstants;
Chris Lattner9fba3da2004-02-15 05:53:04 +00001311
Chris Lattner3e650af2004-08-04 04:48:01 +00001312static char getValType(ConstantAggregateZero *CPZ) { return 0; }
1313
Dan Gohman8214fc12008-12-08 07:10:54 +00001314ConstantAggregateZero *ConstantAggregateZero::get(const Type *Ty) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001315 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) &&
Chris Lattnerbfd0b6d2006-06-10 04:16:23 +00001316 "Cannot create an aggregate zero of non-aggregate type!");
Chris Lattner69edc982006-09-28 00:35:06 +00001317 return AggZeroConstants->getOrCreate(Ty, 0);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001318}
1319
Dan Gohman92b551b2009-03-03 02:55:14 +00001320/// destroyConstant - Remove the constant from the constant table...
1321///
Chris Lattner9fba3da2004-02-15 05:53:04 +00001322void ConstantAggregateZero::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001323 AggZeroConstants->remove(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001324 destroyConstantImpl();
1325}
1326
Chris Lattner3462ae32001-12-03 22:26:30 +00001327//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001328//
Chris Lattner189d19f2003-11-21 20:23:48 +00001329namespace llvm {
1330 template<>
1331 struct ConvertConstantType<ConstantArray, ArrayType> {
1332 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
1333 // Make everyone now use a constant of the new type...
1334 std::vector<Constant*> C;
1335 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1336 C.push_back(cast<Constant>(OldC->getOperand(i)));
1337 Constant *New = ConstantArray::get(NewTy, C);
1338 assert(New != OldC && "Didn't replace constant??");
1339 OldC->uncheckedReplaceAllUsesWith(New);
1340 OldC->destroyConstant(); // This constant is now dead, destroy it.
1341 }
1342 };
1343}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001344
Chris Lattner3e650af2004-08-04 04:48:01 +00001345static std::vector<Constant*> getValType(ConstantArray *CA) {
1346 std::vector<Constant*> Elements;
1347 Elements.reserve(CA->getNumOperands());
1348 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1349 Elements.push_back(cast<Constant>(CA->getOperand(i)));
1350 return Elements;
1351}
1352
Chris Lattnerb64419a2005-10-03 22:51:37 +00001353typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattner935aa922005-10-04 17:48:46 +00001354 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001355static ManagedStatic<ArrayConstantsTy> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001356
Chris Lattner015e8212004-02-15 04:14:47 +00001357Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +00001358 const std::vector<Constant*> &V) {
1359 // If this is an all-zero array, return a ConstantAggregateZero object
1360 if (!V.empty()) {
1361 Constant *C = V[0];
1362 if (!C->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001363 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001364 for (unsigned i = 1, e = V.size(); i != e; ++i)
1365 if (V[i] != C)
Chris Lattner69edc982006-09-28 00:35:06 +00001366 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001367 }
1368 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001369}
1370
Dan Gohman92b551b2009-03-03 02:55:14 +00001371/// destroyConstant - Remove the constant from the constant table...
1372///
Chris Lattner98fa07b2003-05-23 20:03:32 +00001373void ConstantArray::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001374 ArrayConstants->remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001375 destroyConstantImpl();
1376}
1377
Reid Spencer6f614532006-05-30 08:23:18 +00001378/// ConstantArray::get(const string&) - Return an array that is initialized to
1379/// contain the specified string. If length is zero then a null terminator is
1380/// added to the specified string so that it may be used in a natural way.
1381/// Otherwise, the length parameter specifies how much of the string to use
1382/// and it won't be null terminated.
1383///
Reid Spencer82ebaba2006-05-30 18:15:07 +00001384Constant *ConstantArray::get(const std::string &Str, bool AddNull) {
Chris Lattner7f74a562002-01-20 22:54:45 +00001385 std::vector<Constant*> ElementVals;
Reid Spencer82ebaba2006-05-30 18:15:07 +00001386 for (unsigned i = 0; i < Str.length(); ++i)
Reid Spencer8d9336d2006-12-31 05:26:44 +00001387 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +00001388
1389 // Add a null terminator to the string...
Reid Spencer82ebaba2006-05-30 18:15:07 +00001390 if (AddNull) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001391 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, 0));
Reid Spencer6f614532006-05-30 08:23:18 +00001392 }
Chris Lattner8f80fe02001-10-14 23:54:12 +00001393
Reid Spencer8d9336d2006-12-31 05:26:44 +00001394 ArrayType *ATy = ArrayType::get(Type::Int8Ty, ElementVals.size());
Chris Lattner3462ae32001-12-03 22:26:30 +00001395 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +00001396}
1397
Reid Spencer2546b762007-01-26 07:37:34 +00001398/// isString - This method returns true if the array is an array of i8, and
1399/// if the elements of the array are all ConstantInt's.
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001400bool ConstantArray::isString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001401 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001402 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001403 return false;
1404 // Check the elements to make sure they are all integers, not constant
1405 // expressions.
1406 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1407 if (!isa<ConstantInt>(getOperand(i)))
1408 return false;
1409 return true;
1410}
1411
Evan Cheng3763c5b2006-10-26 19:15:05 +00001412/// isCString - This method returns true if the array is a string (see
Dan Gohman92b551b2009-03-03 02:55:14 +00001413/// isString) and it ends in a null byte \\0 and does not contains any other
Evan Cheng3763c5b2006-10-26 19:15:05 +00001414/// null bytes except its terminator.
1415bool ConstantArray::isCString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001416 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001417 if (getType()->getElementType() != Type::Int8Ty)
Evan Chenge974da62006-10-26 21:48:03 +00001418 return false;
1419 Constant *Zero = Constant::getNullValue(getOperand(0)->getType());
1420 // Last element must be a null.
1421 if (getOperand(getNumOperands()-1) != Zero)
1422 return false;
1423 // Other elements must be non-null integers.
1424 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1425 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +00001426 return false;
Evan Chenge974da62006-10-26 21:48:03 +00001427 if (getOperand(i) == Zero)
1428 return false;
1429 }
Evan Cheng3763c5b2006-10-26 19:15:05 +00001430 return true;
1431}
1432
1433
Dan Gohman92b551b2009-03-03 02:55:14 +00001434/// getAsString - If the sub-element type of this array is i8
1435/// then this method converts the array to an std::string and returns it.
1436/// Otherwise, it asserts out.
1437///
Chris Lattner81fabb02002-08-26 17:53:56 +00001438std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001439 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001440 std::string Result;
Owen Anderson79c69bc2008-06-24 21:58:29 +00001441 Result.reserve(getNumOperands());
Chris Lattner6077c312003-07-23 15:22:26 +00001442 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonee9c30d2008-06-25 01:05:05 +00001443 Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue());
Chris Lattner81fabb02002-08-26 17:53:56 +00001444 return Result;
1445}
1446
1447
Chris Lattner3462ae32001-12-03 22:26:30 +00001448//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001449//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001450
Chris Lattner189d19f2003-11-21 20:23:48 +00001451namespace llvm {
1452 template<>
1453 struct ConvertConstantType<ConstantStruct, StructType> {
1454 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1455 // Make everyone now use a constant of the new type...
1456 std::vector<Constant*> C;
1457 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1458 C.push_back(cast<Constant>(OldC->getOperand(i)));
1459 Constant *New = ConstantStruct::get(NewTy, C);
1460 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001461
Chris Lattner189d19f2003-11-21 20:23:48 +00001462 OldC->uncheckedReplaceAllUsesWith(New);
1463 OldC->destroyConstant(); // This constant is now dead, destroy it.
1464 }
1465 };
1466}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001467
Chris Lattner8760ec72005-10-04 01:17:50 +00001468typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattner935aa922005-10-04 17:48:46 +00001469 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001470static ManagedStatic<StructConstantsTy> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001471
Chris Lattner3e650af2004-08-04 04:48:01 +00001472static std::vector<Constant*> getValType(ConstantStruct *CS) {
1473 std::vector<Constant*> Elements;
1474 Elements.reserve(CS->getNumOperands());
1475 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1476 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1477 return Elements;
1478}
1479
Chris Lattner015e8212004-02-15 04:14:47 +00001480Constant *ConstantStruct::get(const StructType *Ty,
1481 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001482 // Create a ConstantAggregateZero value if all elements are zeros...
1483 for (unsigned i = 0, e = V.size(); i != e; ++i)
1484 if (!V[i]->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001485 return StructConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001486
1487 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001488}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001489
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001490Constant *ConstantStruct::get(const std::vector<Constant*> &V, bool packed) {
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001491 std::vector<const Type*> StructEls;
1492 StructEls.reserve(V.size());
1493 for (unsigned i = 0, e = V.size(); i != e; ++i)
1494 StructEls.push_back(V[i]->getType());
Andrew Lenharthdcb3c972006-12-08 18:06:16 +00001495 return get(StructType::get(StructEls, packed), V);
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001496}
1497
Chris Lattnerd7a73302001-10-13 06:57:33 +00001498// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001499//
Chris Lattner3462ae32001-12-03 22:26:30 +00001500void ConstantStruct::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001501 StructConstants->remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001502 destroyConstantImpl();
1503}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001504
Reid Spencerd84d35b2007-02-15 02:26:10 +00001505//---- ConstantVector::get() implementation...
Brian Gaeke02209042004-08-20 06:00:58 +00001506//
1507namespace llvm {
1508 template<>
Reid Spencerd84d35b2007-02-15 02:26:10 +00001509 struct ConvertConstantType<ConstantVector, VectorType> {
1510 static void convert(ConstantVector *OldC, const VectorType *NewTy) {
Brian Gaeke02209042004-08-20 06:00:58 +00001511 // Make everyone now use a constant of the new type...
1512 std::vector<Constant*> C;
1513 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1514 C.push_back(cast<Constant>(OldC->getOperand(i)));
Reid Spencerd84d35b2007-02-15 02:26:10 +00001515 Constant *New = ConstantVector::get(NewTy, C);
Brian Gaeke02209042004-08-20 06:00:58 +00001516 assert(New != OldC && "Didn't replace constant??");
1517 OldC->uncheckedReplaceAllUsesWith(New);
1518 OldC->destroyConstant(); // This constant is now dead, destroy it.
1519 }
1520 };
1521}
1522
Reid Spencerd84d35b2007-02-15 02:26:10 +00001523static std::vector<Constant*> getValType(ConstantVector *CP) {
Brian Gaeke02209042004-08-20 06:00:58 +00001524 std::vector<Constant*> Elements;
1525 Elements.reserve(CP->getNumOperands());
1526 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1527 Elements.push_back(CP->getOperand(i));
1528 return Elements;
1529}
1530
Reid Spencerd84d35b2007-02-15 02:26:10 +00001531static ManagedStatic<ValueMap<std::vector<Constant*>, VectorType,
Reid Spencer09575ba2007-02-15 03:39:18 +00001532 ConstantVector> > VectorConstants;
Brian Gaeke02209042004-08-20 06:00:58 +00001533
Reid Spencerd84d35b2007-02-15 02:26:10 +00001534Constant *ConstantVector::get(const VectorType *Ty,
Brian Gaeke02209042004-08-20 06:00:58 +00001535 const std::vector<Constant*> &V) {
Chris Lattnerd977c072008-07-10 00:44:03 +00001536 assert(!V.empty() && "Vectors can't be empty");
1537 // If this is an all-undef or alll-zero vector, return a
1538 // ConstantAggregateZero or UndefValue.
1539 Constant *C = V[0];
1540 bool isZero = C->isNullValue();
1541 bool isUndef = isa<UndefValue>(C);
1542
1543 if (isZero || isUndef) {
Brian Gaeke02209042004-08-20 06:00:58 +00001544 for (unsigned i = 1, e = V.size(); i != e; ++i)
Chris Lattnerd977c072008-07-10 00:44:03 +00001545 if (V[i] != C) {
1546 isZero = isUndef = false;
1547 break;
1548 }
Brian Gaeke02209042004-08-20 06:00:58 +00001549 }
Chris Lattnerd977c072008-07-10 00:44:03 +00001550
1551 if (isZero)
1552 return ConstantAggregateZero::get(Ty);
1553 if (isUndef)
1554 return UndefValue::get(Ty);
1555 return VectorConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001556}
1557
Reid Spencerd84d35b2007-02-15 02:26:10 +00001558Constant *ConstantVector::get(const std::vector<Constant*> &V) {
Brian Gaeke02209042004-08-20 06:00:58 +00001559 assert(!V.empty() && "Cannot infer type if V is empty");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001560 return get(VectorType::get(V.front()->getType(),V.size()), V);
Brian Gaeke02209042004-08-20 06:00:58 +00001561}
1562
1563// destroyConstant - Remove the constant from the constant table...
1564//
Reid Spencerd84d35b2007-02-15 02:26:10 +00001565void ConstantVector::destroyConstant() {
Reid Spencer09575ba2007-02-15 03:39:18 +00001566 VectorConstants->remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001567 destroyConstantImpl();
1568}
1569
Dan Gohman30978072007-05-24 14:36:04 +00001570/// This function will return true iff every element in this vector constant
Jim Laskeyf0478822007-01-12 22:39:14 +00001571/// is set to all ones.
1572/// @returns true iff this constant's emements are all set to all ones.
1573/// @brief Determine if the value is all ones.
Reid Spencerd84d35b2007-02-15 02:26:10 +00001574bool ConstantVector::isAllOnesValue() const {
Jim Laskeyf0478822007-01-12 22:39:14 +00001575 // Check out first element.
1576 const Constant *Elt = getOperand(0);
1577 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1578 if (!CI || !CI->isAllOnesValue()) return false;
1579 // Then make sure all remaining elements point to the same value.
1580 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1581 if (getOperand(I) != Elt) return false;
1582 }
1583 return true;
1584}
1585
Dan Gohman07159202007-10-17 17:51:30 +00001586/// getSplatValue - If this is a splat constant, where all of the
1587/// elements have the same value, return that value. Otherwise return null.
1588Constant *ConstantVector::getSplatValue() {
1589 // Check out first element.
1590 Constant *Elt = getOperand(0);
1591 // Then make sure all remaining elements point to the same value.
1592 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
1593 if (getOperand(I) != Elt) return 0;
1594 return Elt;
1595}
1596
Chris Lattner3462ae32001-12-03 22:26:30 +00001597//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001598//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001599
Chris Lattner189d19f2003-11-21 20:23:48 +00001600namespace llvm {
1601 // ConstantPointerNull does not take extra "value" argument...
1602 template<class ValType>
1603 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1604 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1605 return new ConstantPointerNull(Ty);
1606 }
1607 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001608
Chris Lattner189d19f2003-11-21 20:23:48 +00001609 template<>
1610 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1611 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1612 // Make everyone now use a constant of the new type...
1613 Constant *New = ConstantPointerNull::get(NewTy);
1614 assert(New != OldC && "Didn't replace constant??");
1615 OldC->uncheckedReplaceAllUsesWith(New);
1616 OldC->destroyConstant(); // This constant is now dead, destroy it.
1617 }
1618 };
1619}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001620
Chris Lattner69edc982006-09-28 00:35:06 +00001621static ManagedStatic<ValueMap<char, PointerType,
1622 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001623
Chris Lattner3e650af2004-08-04 04:48:01 +00001624static char getValType(ConstantPointerNull *) {
1625 return 0;
1626}
1627
1628
Chris Lattner3462ae32001-12-03 22:26:30 +00001629ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001630 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001631}
1632
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001633// destroyConstant - Remove the constant from the constant table...
1634//
1635void ConstantPointerNull::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001636 NullPtrConstants->remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001637 destroyConstantImpl();
1638}
1639
1640
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001641//---- UndefValue::get() implementation...
1642//
1643
1644namespace llvm {
1645 // UndefValue does not take extra "value" argument...
1646 template<class ValType>
1647 struct ConstantCreator<UndefValue, Type, ValType> {
1648 static UndefValue *create(const Type *Ty, const ValType &V) {
1649 return new UndefValue(Ty);
1650 }
1651 };
1652
1653 template<>
1654 struct ConvertConstantType<UndefValue, Type> {
1655 static void convert(UndefValue *OldC, const Type *NewTy) {
1656 // Make everyone now use a constant of the new type.
1657 Constant *New = UndefValue::get(NewTy);
1658 assert(New != OldC && "Didn't replace constant??");
1659 OldC->uncheckedReplaceAllUsesWith(New);
1660 OldC->destroyConstant(); // This constant is now dead, destroy it.
1661 }
1662 };
1663}
1664
Chris Lattner69edc982006-09-28 00:35:06 +00001665static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001666
1667static char getValType(UndefValue *) {
1668 return 0;
1669}
1670
1671
1672UndefValue *UndefValue::get(const Type *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001673 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001674}
1675
1676// destroyConstant - Remove the constant from the constant table.
1677//
1678void UndefValue::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001679 UndefValueConstants->remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001680 destroyConstantImpl();
1681}
1682
Nick Lewycky49f89192009-04-04 07:22:01 +00001683//---- MDString::get() implementation
1684//
1685
1686MDString::MDString(const char *begin, const char *end)
Nick Lewyckyadbc2842009-05-30 05:06:04 +00001687 : Constant(Type::MetadataTy, MDStringVal, 0, 0),
Nick Lewycky49f89192009-04-04 07:22:01 +00001688 StrBegin(begin), StrEnd(end) {}
1689
1690static ManagedStatic<StringMap<MDString*> > MDStringCache;
1691
1692MDString *MDString::get(const char *StrBegin, const char *StrEnd) {
1693 StringMapEntry<MDString *> &Entry = MDStringCache->GetOrCreateValue(StrBegin,
1694 StrEnd);
1695 MDString *&S = Entry.getValue();
1696 if (!S) S = new MDString(Entry.getKeyData(),
1697 Entry.getKeyData() + Entry.getKeyLength());
1698 return S;
1699}
1700
1701void MDString::destroyConstant() {
1702 MDStringCache->erase(MDStringCache->find(StrBegin, StrEnd));
1703 destroyConstantImpl();
1704}
1705
1706//---- MDNode::get() implementation
1707//
1708
1709static ManagedStatic<FoldingSet<MDNode> > MDNodeSet;
1710
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001711MDNode::MDNode(Value*const* Vals, unsigned NumVals)
Nick Lewyckyadbc2842009-05-30 05:06:04 +00001712 : Constant(Type::MetadataTy, MDNodeVal, 0, 0) {
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001713 for (unsigned i = 0; i != NumVals; ++i)
1714 Node.push_back(ElementVH(Vals[i], this));
Nick Lewycky49f89192009-04-04 07:22:01 +00001715}
1716
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001717void MDNode::Profile(FoldingSetNodeID &ID) const {
1718 for (const_elem_iterator I = elem_begin(), E = elem_end(); I != E; ++I)
Nick Lewycky49f89192009-04-04 07:22:01 +00001719 ID.AddPointer(*I);
1720}
1721
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001722MDNode *MDNode::get(Value*const* Vals, unsigned NumVals) {
Nick Lewycky49f89192009-04-04 07:22:01 +00001723 FoldingSetNodeID ID;
1724 for (unsigned i = 0; i != NumVals; ++i)
1725 ID.AddPointer(Vals[i]);
1726
1727 void *InsertPoint;
1728 if (MDNode *N = MDNodeSet->FindNodeOrInsertPos(ID, InsertPoint))
1729 return N;
1730
1731 // InsertPoint will have been set by the FindNodeOrInsertPos call.
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001732 MDNode *N = new(0) MDNode(Vals, NumVals);
Nick Lewycky49f89192009-04-04 07:22:01 +00001733 MDNodeSet->InsertNode(N, InsertPoint);
1734 return N;
1735}
1736
1737void MDNode::destroyConstant() {
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00001738 MDNodeSet->RemoveNode(this);
Nick Lewycky49f89192009-04-04 07:22:01 +00001739 destroyConstantImpl();
1740}
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001741
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001742//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001743//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001744
Dan Gohmand78c4002008-05-13 00:00:25 +00001745namespace {
1746
Reid Spenceree3c9912006-12-04 05:19:50 +00001747struct ExprMapKeyType {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001748 typedef SmallVector<unsigned, 4> IndexList;
1749
1750 ExprMapKeyType(unsigned opc,
1751 const std::vector<Constant*> &ops,
1752 unsigned short pred = 0,
1753 const IndexList &inds = IndexList())
1754 : opcode(opc), predicate(pred), operands(ops), indices(inds) {}
Reid Spencerdba6aa42006-12-04 18:38:05 +00001755 uint16_t opcode;
1756 uint16_t predicate;
Reid Spenceree3c9912006-12-04 05:19:50 +00001757 std::vector<Constant*> operands;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001758 IndexList indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001759 bool operator==(const ExprMapKeyType& that) const {
1760 return this->opcode == that.opcode &&
1761 this->predicate == that.predicate &&
Bill Wendling97f7de82008-10-26 00:19:56 +00001762 this->operands == that.operands &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001763 this->indices == that.indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001764 }
1765 bool operator<(const ExprMapKeyType & that) const {
1766 return this->opcode < that.opcode ||
1767 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1768 (this->opcode == that.opcode && this->predicate == that.predicate &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001769 this->operands < that.operands) ||
1770 (this->opcode == that.opcode && this->predicate == that.predicate &&
1771 this->operands == that.operands && this->indices < that.indices);
Reid Spenceree3c9912006-12-04 05:19:50 +00001772 }
1773
1774 bool operator!=(const ExprMapKeyType& that) const {
1775 return !(*this == that);
1776 }
1777};
Chris Lattner98fa07b2003-05-23 20:03:32 +00001778
Dan Gohmand78c4002008-05-13 00:00:25 +00001779}
1780
Chris Lattner189d19f2003-11-21 20:23:48 +00001781namespace llvm {
1782 template<>
1783 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001784 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1785 unsigned short pred = 0) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001786 if (Instruction::isCast(V.opcode))
1787 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1788 if ((V.opcode >= Instruction::BinaryOpsBegin &&
Reid Spencer2341c222007-02-02 02:16:23 +00001789 V.opcode < Instruction::BinaryOpsEnd))
Reid Spenceree3c9912006-12-04 05:19:50 +00001790 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1791 if (V.opcode == Instruction::Select)
1792 return new SelectConstantExpr(V.operands[0], V.operands[1],
1793 V.operands[2]);
1794 if (V.opcode == Instruction::ExtractElement)
1795 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1796 if (V.opcode == Instruction::InsertElement)
1797 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1798 V.operands[2]);
1799 if (V.opcode == Instruction::ShuffleVector)
1800 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1801 V.operands[2]);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001802 if (V.opcode == Instruction::InsertValue)
1803 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
1804 V.indices, Ty);
1805 if (V.opcode == Instruction::ExtractValue)
1806 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001807 if (V.opcode == Instruction::GetElementPtr) {
1808 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
Gabor Greife9ecc682008-04-06 20:25:17 +00001809 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001810 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001811
Reid Spenceree3c9912006-12-04 05:19:50 +00001812 // The compare instructions are weird. We have to encode the predicate
1813 // value and it is combined with the instruction opcode by multiplying
1814 // the opcode by one hundred. We must decode this to get the predicate.
1815 if (V.opcode == Instruction::ICmp)
Nate Begemand2195702008-05-12 19:01:56 +00001816 return new CompareConstantExpr(Ty, Instruction::ICmp, V.predicate,
Reid Spenceree3c9912006-12-04 05:19:50 +00001817 V.operands[0], V.operands[1]);
1818 if (V.opcode == Instruction::FCmp)
Nate Begemand2195702008-05-12 19:01:56 +00001819 return new CompareConstantExpr(Ty, Instruction::FCmp, V.predicate,
1820 V.operands[0], V.operands[1]);
1821 if (V.opcode == Instruction::VICmp)
1822 return new CompareConstantExpr(Ty, Instruction::VICmp, V.predicate,
1823 V.operands[0], V.operands[1]);
1824 if (V.opcode == Instruction::VFCmp)
1825 return new CompareConstantExpr(Ty, Instruction::VFCmp, V.predicate,
Reid Spenceree3c9912006-12-04 05:19:50 +00001826 V.operands[0], V.operands[1]);
1827 assert(0 && "Invalid ConstantExpr!");
Jeff Cohen9f469632006-12-15 21:47:01 +00001828 return 0;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001829 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001830 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001831
Chris Lattner189d19f2003-11-21 20:23:48 +00001832 template<>
1833 struct ConvertConstantType<ConstantExpr, Type> {
1834 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1835 Constant *New;
1836 switch (OldC->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001837 case Instruction::Trunc:
1838 case Instruction::ZExt:
1839 case Instruction::SExt:
1840 case Instruction::FPTrunc:
1841 case Instruction::FPExt:
1842 case Instruction::UIToFP:
1843 case Instruction::SIToFP:
1844 case Instruction::FPToUI:
1845 case Instruction::FPToSI:
1846 case Instruction::PtrToInt:
1847 case Instruction::IntToPtr:
1848 case Instruction::BitCast:
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001849 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
1850 NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001851 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001852 case Instruction::Select:
1853 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1854 OldC->getOperand(1),
1855 OldC->getOperand(2));
1856 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001857 default:
1858 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001859 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner189d19f2003-11-21 20:23:48 +00001860 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1861 OldC->getOperand(1));
1862 break;
1863 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001864 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001865 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
Chris Lattner302116a2007-01-31 04:40:28 +00001866 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
1867 &Idx[0], Idx.size());
Chris Lattner189d19f2003-11-21 20:23:48 +00001868 break;
1869 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001870
Chris Lattner189d19f2003-11-21 20:23:48 +00001871 assert(New != OldC && "Didn't replace constant??");
1872 OldC->uncheckedReplaceAllUsesWith(New);
1873 OldC->destroyConstant(); // This constant is now dead, destroy it.
1874 }
1875 };
1876} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001877
1878
Chris Lattner3e650af2004-08-04 04:48:01 +00001879static ExprMapKeyType getValType(ConstantExpr *CE) {
1880 std::vector<Constant*> Operands;
1881 Operands.reserve(CE->getNumOperands());
1882 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1883 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spenceree3c9912006-12-04 05:19:50 +00001884 return ExprMapKeyType(CE->getOpcode(), Operands,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001885 CE->isCompare() ? CE->getPredicate() : 0,
1886 CE->hasIndices() ?
1887 CE->getIndices() : SmallVector<unsigned, 4>());
Chris Lattner3e650af2004-08-04 04:48:01 +00001888}
1889
Chris Lattner69edc982006-09-28 00:35:06 +00001890static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1891 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001892
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001893/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands7d6c8ae2008-03-30 19:38:55 +00001894/// cast in the ExprConstants map. It is used by the various get* methods below.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001895static inline Constant *getFoldedCast(
1896 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001897 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001898 // Fold a few common cases
1899 if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
1900 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001901
Vikram S. Adve4c485332002-07-15 18:19:33 +00001902 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001903 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00001904 ExprMapKeyType Key(opc, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001905 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001906}
Reid Spencerf37dc652006-12-05 19:14:13 +00001907
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001908Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
1909 Instruction::CastOps opc = Instruction::CastOps(oc);
1910 assert(Instruction::isCast(opc) && "opcode out of range");
1911 assert(C && Ty && "Null arguments to getCast");
1912 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1913
1914 switch (opc) {
1915 default:
1916 assert(0 && "Invalid cast opcode");
1917 break;
1918 case Instruction::Trunc: return getTrunc(C, Ty);
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001919 case Instruction::ZExt: return getZExt(C, Ty);
1920 case Instruction::SExt: return getSExt(C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001921 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1922 case Instruction::FPExt: return getFPExtend(C, Ty);
1923 case Instruction::UIToFP: return getUIToFP(C, Ty);
1924 case Instruction::SIToFP: return getSIToFP(C, Ty);
1925 case Instruction::FPToUI: return getFPToUI(C, Ty);
1926 case Instruction::FPToSI: return getFPToSI(C, Ty);
1927 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1928 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1929 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001930 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001931 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00001932}
1933
Reid Spencer5c140882006-12-04 20:17:56 +00001934Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
1935 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1936 return getCast(Instruction::BitCast, C, Ty);
1937 return getCast(Instruction::ZExt, C, Ty);
1938}
1939
1940Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
1941 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1942 return getCast(Instruction::BitCast, C, Ty);
1943 return getCast(Instruction::SExt, C, Ty);
1944}
1945
1946Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
1947 if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1948 return getCast(Instruction::BitCast, C, Ty);
1949 return getCast(Instruction::Trunc, C, Ty);
1950}
1951
Reid Spencerbc245a02006-12-05 03:25:26 +00001952Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
1953 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001954 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001955
Chris Lattner03c49532007-01-15 02:27:26 +00001956 if (Ty->isInteger())
Reid Spencerbc245a02006-12-05 03:25:26 +00001957 return getCast(Instruction::PtrToInt, S, Ty);
1958 return getCast(Instruction::BitCast, S, Ty);
1959}
1960
Reid Spencer56521c42006-12-12 00:51:07 +00001961Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1962 bool isSigned) {
Chris Lattner03c49532007-01-15 02:27:26 +00001963 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer56521c42006-12-12 00:51:07 +00001964 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1965 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1966 Instruction::CastOps opcode =
1967 (SrcBits == DstBits ? Instruction::BitCast :
1968 (SrcBits > DstBits ? Instruction::Trunc :
1969 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1970 return getCast(opcode, C, Ty);
1971}
1972
1973Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
1974 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1975 "Invalid cast");
1976 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1977 unsigned DstBits = Ty->getPrimitiveSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001978 if (SrcBits == DstBits)
1979 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001980 Instruction::CastOps opcode =
Reid Spencerca104e82006-12-12 05:38:50 +00001981 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001982 return getCast(opcode, C, Ty);
1983}
1984
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001985Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001986 assert(C->getType()->isInteger() && "Trunc operand must be integer");
1987 assert(Ty->isInteger() && "Trunc produces only integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001988 assert(C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&&
1989 "SrcTy must be larger than DestTy for Trunc!");
1990
1991 return getFoldedCast(Instruction::Trunc, C, Ty);
1992}
1993
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001994Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00001995 assert(C->getType()->isInteger() && "SEXt operand must be integral");
1996 assert(Ty->isInteger() && "SExt produces only integer");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001997 assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
1998 "SrcTy must be smaller than DestTy for SExt!");
1999
2000 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerdd284742004-04-04 23:20:30 +00002001}
2002
Reid Spencerbb65ebf2006-12-12 23:36:14 +00002003Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Chris Lattner03c49532007-01-15 02:27:26 +00002004 assert(C->getType()->isInteger() && "ZEXt operand must be integral");
2005 assert(Ty->isInteger() && "ZExt produces only integer");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002006 assert(C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
2007 "SrcTy must be smaller than DestTy for ZExt!");
2008
2009 return getFoldedCast(Instruction::ZExt, C, Ty);
2010}
2011
2012Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
2013 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
2014 C->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()&&
2015 "This is an illegal floating point truncation!");
2016 return getFoldedCast(Instruction::FPTrunc, C, Ty);
2017}
2018
2019Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
2020 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
2021 C->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()&&
2022 "This is an illegal floating point extension!");
2023 return getFoldedCast(Instruction::FPExt, C, Ty);
2024}
2025
2026Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00002027#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00002028 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2029 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00002030#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00002031 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2032 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
2033 "This is an illegal uint to floating point cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002034 return getFoldedCast(Instruction::UIToFP, C, Ty);
2035}
2036
2037Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00002038#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00002039 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2040 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00002041#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00002042 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2043 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002044 "This is an illegal sint to floating point cast!");
2045 return getFoldedCast(Instruction::SIToFP, C, Ty);
2046}
2047
2048Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00002049#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00002050 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2051 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00002052#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00002053 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2054 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
2055 "This is an illegal floating point to uint cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002056 return getFoldedCast(Instruction::FPToUI, C, Ty);
2057}
2058
2059Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00002060#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00002061 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
2062 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00002063#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00002064 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
2065 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
2066 "This is an illegal floating point to sint cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002067 return getFoldedCast(Instruction::FPToSI, C, Ty);
2068}
2069
2070Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
2071 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner03c49532007-01-15 02:27:26 +00002072 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002073 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
2074}
2075
2076Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner03c49532007-01-15 02:27:26 +00002077 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002078 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
2079 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
2080}
2081
2082Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
2083 // BitCast implies a no-op cast of type only. No bits change. However, you
2084 // can't cast pointers to anything but pointers.
Devang Pateld26344d2008-11-03 23:20:04 +00002085#ifndef NDEBUG
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002086 const Type *SrcTy = C->getType();
2087 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer5c140882006-12-04 20:17:56 +00002088 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002089
2090 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
2091 // or nonptr->ptr). For all the other types, the cast is okay if source and
2092 // destination bit widths are identical.
2093 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
2094 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Devang Pateld26344d2008-11-03 23:20:04 +00002095#endif
Chris Lattnere4086012009-03-08 04:06:26 +00002096 assert(SrcBitSize == DstBitSize && "BitCast requires types of same width");
Chris Lattnercbeda872009-03-21 06:55:54 +00002097
2098 // It is common to ask for a bitcast of a value to its own type, handle this
2099 // speedily.
2100 if (C->getType() == DstTy) return C;
2101
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002102 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerdd284742004-04-04 23:20:30 +00002103}
2104
Duncan Sandsd334aca2009-05-21 15:52:21 +00002105Constant *ConstantExpr::getAlignOf(const Type *Ty) {
2106 // alignof is implemented as: (i64) gep ({i8,Ty}*)null, 0, 1
2107 const Type *AligningTy = StructType::get(Type::Int8Ty, Ty, NULL);
2108 Constant *NullPtr = getNullValue(AligningTy->getPointerTo());
2109 Constant *Zero = ConstantInt::get(Type::Int32Ty, 0);
2110 Constant *One = ConstantInt::get(Type::Int32Ty, 1);
2111 Constant *Indices[2] = { Zero, One };
2112 Constant *GEP = getGetElementPtr(NullPtr, Indices, 2);
2113 return getCast(Instruction::PtrToInt, GEP, Type::Int32Ty);
2114}
2115
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00002116Constant *ConstantExpr::getSizeOf(const Type *Ty) {
Gordon Henriksen7ce31762007-10-06 14:29:36 +00002117 // sizeof is implemented as: (i64) gep (Ty*)null, 1
Chris Lattnerb5d70302007-02-19 20:01:23 +00002118 Constant *GEPIdx = ConstantInt::get(Type::Int32Ty, 1);
2119 Constant *GEP =
Christopher Lambedf07882007-12-17 01:12:55 +00002120 getGetElementPtr(getNullValue(PointerType::getUnqual(Ty)), &GEPIdx, 1);
Chris Lattnerb5d70302007-02-19 20:01:23 +00002121 return getCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
Alkis Evlogimenos9160d5f2005-03-19 11:40:31 +00002122}
2123
Chris Lattnerb50d1352003-10-05 00:17:43 +00002124Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Reid Spencera009d0d2006-12-04 21:35:24 +00002125 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00002126 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00002127 assert(Opcode >= Instruction::BinaryOpsBegin &&
2128 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00002129 "Invalid opcode in binary constant expression");
2130 assert(C1->getType() == C2->getType() &&
2131 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00002132
Reid Spencer542964f2007-01-11 18:21:29 +00002133 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Chris Lattnerb50d1352003-10-05 00:17:43 +00002134 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
2135 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00002136
Chris Lattner2b383d2e2003-05-13 21:37:02 +00002137 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00002138 ExprMapKeyType Key(Opcode, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002139 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002140}
2141
Reid Spencer266e42b2006-12-23 06:05:41 +00002142Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Nate Begeman098cc6f2008-07-25 17:56:27 +00002143 Constant *C1, Constant *C2) {
2144 bool isVectorType = C1->getType()->getTypeID() == Type::VectorTyID;
Reid Spencer266e42b2006-12-23 06:05:41 +00002145 switch (predicate) {
2146 default: assert(0 && "Invalid CmpInst predicate");
Nate Begemanc96e2e42008-07-25 17:35:37 +00002147 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
2148 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
2149 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
2150 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
2151 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
2152 case CmpInst::FCMP_TRUE:
Nate Begeman098cc6f2008-07-25 17:56:27 +00002153 return isVectorType ? getVFCmp(predicate, C1, C2)
2154 : getFCmp(predicate, C1, C2);
Nate Begemanc96e2e42008-07-25 17:35:37 +00002155 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
2156 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
2157 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
2158 case CmpInst::ICMP_SLE:
Nate Begeman098cc6f2008-07-25 17:56:27 +00002159 return isVectorType ? getVICmp(predicate, C1, C2)
2160 : getICmp(predicate, C1, C2);
Reid Spencer266e42b2006-12-23 06:05:41 +00002161 }
Reid Spencera009d0d2006-12-04 21:35:24 +00002162}
2163
2164Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002165 // API compatibility: Adjust integer opcodes to floating-point opcodes.
2166 if (C1->getType()->isFPOrFPVector()) {
2167 if (Opcode == Instruction::Add) Opcode = Instruction::FAdd;
2168 else if (Opcode == Instruction::Sub) Opcode = Instruction::FSub;
2169 else if (Opcode == Instruction::Mul) Opcode = Instruction::FMul;
2170 }
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002171#ifndef NDEBUG
2172 switch (Opcode) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002173 case Instruction::Add:
Reid Spencer7eb55b32006-11-02 01:53:59 +00002174 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002175 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002176 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmana5b96452009-06-04 22:49:04 +00002177 assert(C1->getType()->isIntOrIntVector() &&
2178 "Tried to create an integer operation on a non-integer type!");
2179 break;
2180 case Instruction::FAdd:
2181 case Instruction::FSub:
2182 case Instruction::FMul:
2183 assert(C1->getType() == C2->getType() && "Op types should be identical!");
2184 assert(C1->getType()->isFPOrFPVector() &&
2185 "Tried to create a floating-point operation on a "
2186 "non-floating-point type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002187 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002188 case Instruction::UDiv:
2189 case Instruction::SDiv:
2190 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002191 assert((C1->getType()->isInteger() || (isa<VectorType>(C1->getType()) &&
2192 cast<VectorType>(C1->getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002193 "Tried to create an arithmetic operation on a non-arithmetic type!");
2194 break;
2195 case Instruction::FDiv:
2196 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002197 assert((C1->getType()->isFloatingPoint() || (isa<VectorType>(C1->getType())
2198 && cast<VectorType>(C1->getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002199 && "Tried to create an arithmetic operation on a non-arithmetic type!");
2200 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00002201 case Instruction::URem:
2202 case Instruction::SRem:
2203 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002204 assert((C1->getType()->isInteger() || (isa<VectorType>(C1->getType()) &&
2205 cast<VectorType>(C1->getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002206 "Tried to create an arithmetic operation on a non-arithmetic type!");
2207 break;
2208 case Instruction::FRem:
2209 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002210 assert((C1->getType()->isFloatingPoint() || (isa<VectorType>(C1->getType())
2211 && cast<VectorType>(C1->getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00002212 && "Tried to create an arithmetic operation on a non-arithmetic type!");
2213 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002214 case Instruction::And:
2215 case Instruction::Or:
2216 case Instruction::Xor:
2217 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002218 assert((C1->getType()->isInteger() || isa<VectorType>(C1->getType())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00002219 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002220 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002221 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00002222 case Instruction::LShr:
2223 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00002224 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman79975d52009-03-14 17:09:17 +00002225 assert(C1->getType()->isIntOrIntVector() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00002226 "Tried to create a shift operation on a non-integer type!");
2227 break;
2228 default:
2229 break;
2230 }
2231#endif
2232
Reid Spencera009d0d2006-12-04 21:35:24 +00002233 return getTy(C1->getType(), Opcode, C1, C2);
2234}
2235
Reid Spencer266e42b2006-12-23 06:05:41 +00002236Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00002237 Constant *C1, Constant *C2) {
2238 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002239 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00002240}
2241
Chris Lattner6e415c02004-03-12 05:54:04 +00002242Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
2243 Constant *V1, Constant *V2) {
Chris Lattner41632132008-12-29 00:16:12 +00002244 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner6e415c02004-03-12 05:54:04 +00002245
2246 if (ReqTy == V1->getType())
2247 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
2248 return SC; // Fold common cases
2249
2250 std::vector<Constant*> argVec(3, C);
2251 argVec[1] = V1;
2252 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00002253 ExprMapKeyType Key(Instruction::Select, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002254 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00002255}
2256
Chris Lattnerb50d1352003-10-05 00:17:43 +00002257Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00002258 Value* const *Idxs,
2259 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002260 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
2261 Idxs+NumIdx) ==
2262 cast<PointerType>(ReqTy)->getElementType() &&
2263 "GEP indices invalid!");
Chris Lattner04b60fe2004-02-16 20:46:13 +00002264
Chris Lattner302116a2007-01-31 04:40:28 +00002265 if (Constant *FC = ConstantFoldGetElementPtr(C, (Constant**)Idxs, NumIdx))
Chris Lattneracdbe712003-04-17 19:24:48 +00002266 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00002267
Chris Lattnerb50d1352003-10-05 00:17:43 +00002268 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00002269 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00002270 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00002271 std::vector<Constant*> ArgVec;
Chris Lattner302116a2007-01-31 04:40:28 +00002272 ArgVec.reserve(NumIdx+1);
Chris Lattner13128ab2004-10-11 22:52:25 +00002273 ArgVec.push_back(C);
Chris Lattner302116a2007-01-31 04:40:28 +00002274 for (unsigned i = 0; i != NumIdx; ++i)
2275 ArgVec.push_back(cast<Constant>(Idxs[i]));
2276 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002277 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002278}
2279
Chris Lattner302116a2007-01-31 04:40:28 +00002280Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
2281 unsigned NumIdx) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00002282 // Get the result type of the getelementptr!
Chris Lattner302116a2007-01-31 04:40:28 +00002283 const Type *Ty =
Dan Gohman12fce772008-05-15 19:50:34 +00002284 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00002285 assert(Ty && "GEP indices invalid!");
Christopher Lamb54dd24c2007-12-11 08:59:05 +00002286 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
2287 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
Chris Lattner13128ab2004-10-11 22:52:25 +00002288}
2289
Chris Lattner302116a2007-01-31 04:40:28 +00002290Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
2291 unsigned NumIdx) {
2292 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00002293}
2294
Chris Lattner302116a2007-01-31 04:40:28 +00002295
Reid Spenceree3c9912006-12-04 05:19:50 +00002296Constant *
2297ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2298 assert(LHS->getType() == RHS->getType());
2299 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
2300 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
2301
Reid Spencer266e42b2006-12-23 06:05:41 +00002302 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002303 return FC; // Fold a few common cases...
2304
2305 // Look up the constant in the table first to ensure uniqueness
2306 std::vector<Constant*> ArgVec;
2307 ArgVec.push_back(LHS);
2308 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00002309 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00002310 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Reid Spencer542964f2007-01-11 18:21:29 +00002311 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002312}
2313
2314Constant *
2315ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2316 assert(LHS->getType() == RHS->getType());
2317 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
2318
Reid Spencer266e42b2006-12-23 06:05:41 +00002319 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002320 return FC; // Fold a few common cases...
2321
2322 // Look up the constant in the table first to ensure uniqueness
2323 std::vector<Constant*> ArgVec;
2324 ArgVec.push_back(LHS);
2325 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00002326 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00002327 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Reid Spencer542964f2007-01-11 18:21:29 +00002328 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002329}
2330
Nate Begemand2195702008-05-12 19:01:56 +00002331Constant *
2332ConstantExpr::getVICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
Chris Lattnereab49262008-07-14 05:17:31 +00002333 assert(isa<VectorType>(LHS->getType()) && LHS->getType() == RHS->getType() &&
Nate Begemand2195702008-05-12 19:01:56 +00002334 "Tried to create vicmp operation on non-vector type!");
Nate Begemand2195702008-05-12 19:01:56 +00002335 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
2336 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid VICmp Predicate");
2337
Nate Begemanac7f3d92008-05-12 19:23:22 +00002338 const VectorType *VTy = cast<VectorType>(LHS->getType());
Nate Begemand2195702008-05-12 19:01:56 +00002339 const Type *EltTy = VTy->getElementType();
2340 unsigned NumElts = VTy->getNumElements();
2341
Chris Lattnereab49262008-07-14 05:17:31 +00002342 // See if we can fold the element-wise comparison of the LHS and RHS.
2343 SmallVector<Constant *, 16> LHSElts, RHSElts;
2344 LHS->getVectorElements(LHSElts);
2345 RHS->getVectorElements(RHSElts);
2346
2347 if (!LHSElts.empty() && !RHSElts.empty()) {
2348 SmallVector<Constant *, 16> Elts;
2349 for (unsigned i = 0; i != NumElts; ++i) {
2350 Constant *FC = ConstantFoldCompareInstruction(pred, LHSElts[i],
2351 RHSElts[i]);
2352 if (ConstantInt *FCI = dyn_cast_or_null<ConstantInt>(FC)) {
2353 if (FCI->getZExtValue())
2354 Elts.push_back(ConstantInt::getAllOnesValue(EltTy));
2355 else
2356 Elts.push_back(ConstantInt::get(EltTy, 0ULL));
2357 } else if (FC && isa<UndefValue>(FC)) {
2358 Elts.push_back(UndefValue::get(EltTy));
2359 } else {
2360 break;
2361 }
Nate Begemand2195702008-05-12 19:01:56 +00002362 }
Chris Lattnereab49262008-07-14 05:17:31 +00002363 if (Elts.size() == NumElts)
2364 return ConstantVector::get(&Elts[0], Elts.size());
Nate Begemand2195702008-05-12 19:01:56 +00002365 }
Nate Begemand2195702008-05-12 19:01:56 +00002366
2367 // Look up the constant in the table first to ensure uniqueness
2368 std::vector<Constant*> ArgVec;
2369 ArgVec.push_back(LHS);
2370 ArgVec.push_back(RHS);
2371 // Get the key type with both the opcode and predicate
2372 const ExprMapKeyType Key(Instruction::VICmp, ArgVec, pred);
2373 return ExprConstants->getOrCreate(LHS->getType(), Key);
2374}
2375
2376Constant *
2377ConstantExpr::getVFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
2378 assert(isa<VectorType>(LHS->getType()) &&
2379 "Tried to create vfcmp operation on non-vector type!");
2380 assert(LHS->getType() == RHS->getType());
2381 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid VFCmp Predicate");
2382
2383 const VectorType *VTy = cast<VectorType>(LHS->getType());
2384 unsigned NumElts = VTy->getNumElements();
2385 const Type *EltTy = VTy->getElementType();
2386 const Type *REltTy = IntegerType::get(EltTy->getPrimitiveSizeInBits());
2387 const Type *ResultTy = VectorType::get(REltTy, NumElts);
2388
Chris Lattnereab49262008-07-14 05:17:31 +00002389 // See if we can fold the element-wise comparison of the LHS and RHS.
2390 SmallVector<Constant *, 16> LHSElts, RHSElts;
2391 LHS->getVectorElements(LHSElts);
2392 RHS->getVectorElements(RHSElts);
2393
2394 if (!LHSElts.empty() && !RHSElts.empty()) {
2395 SmallVector<Constant *, 16> Elts;
2396 for (unsigned i = 0; i != NumElts; ++i) {
2397 Constant *FC = ConstantFoldCompareInstruction(pred, LHSElts[i],
2398 RHSElts[i]);
2399 if (ConstantInt *FCI = dyn_cast_or_null<ConstantInt>(FC)) {
2400 if (FCI->getZExtValue())
2401 Elts.push_back(ConstantInt::getAllOnesValue(REltTy));
2402 else
2403 Elts.push_back(ConstantInt::get(REltTy, 0ULL));
2404 } else if (FC && isa<UndefValue>(FC)) {
2405 Elts.push_back(UndefValue::get(REltTy));
2406 } else {
2407 break;
2408 }
Nate Begemand2195702008-05-12 19:01:56 +00002409 }
Chris Lattnereab49262008-07-14 05:17:31 +00002410 if (Elts.size() == NumElts)
2411 return ConstantVector::get(&Elts[0], Elts.size());
Nate Begemand2195702008-05-12 19:01:56 +00002412 }
Nate Begemand2195702008-05-12 19:01:56 +00002413
2414 // Look up the constant in the table first to ensure uniqueness
2415 std::vector<Constant*> ArgVec;
2416 ArgVec.push_back(LHS);
2417 ArgVec.push_back(RHS);
2418 // Get the key type with both the opcode and predicate
2419 const ExprMapKeyType Key(Instruction::VFCmp, ArgVec, pred);
2420 return ExprConstants->getOrCreate(ResultTy, Key);
2421}
2422
Robert Bocchino23004482006-01-10 19:05:34 +00002423Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
2424 Constant *Idx) {
Robert Bocchinode7f1c92006-01-10 20:03:46 +00002425 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
2426 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00002427 // Look up the constant in the table first to ensure uniqueness
2428 std::vector<Constant*> ArgVec(1, Val);
2429 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00002430 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002431 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00002432}
2433
2434Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002435 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002436 "Tried to create extractelement operation on non-vector type!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00002437 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00002438 "Extractelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002439 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +00002440 Val, Idx);
2441}
Chris Lattnerb50d1352003-10-05 00:17:43 +00002442
Robert Bocchinoca27f032006-01-17 20:07:22 +00002443Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
2444 Constant *Elt, Constant *Idx) {
2445 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
2446 return FC; // Fold a few common cases...
2447 // Look up the constant in the table first to ensure uniqueness
2448 std::vector<Constant*> ArgVec(1, Val);
2449 ArgVec.push_back(Elt);
2450 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00002451 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002452 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00002453}
2454
2455Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
2456 Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002457 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002458 "Tried to create insertelement operation on non-vector type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00002459 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoca27f032006-01-17 20:07:22 +00002460 && "Insertelement types must match!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00002461 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00002462 "Insertelement index must be i32 type!");
Gordon Henriksenb52d1ed2008-08-30 15:41:51 +00002463 return getInsertElementTy(Val->getType(), Val, Elt, Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +00002464}
2465
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002466Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
2467 Constant *V2, Constant *Mask) {
2468 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
2469 return FC; // Fold a few common cases...
2470 // Look up the constant in the table first to ensure uniqueness
2471 std::vector<Constant*> ArgVec(1, V1);
2472 ArgVec.push_back(V2);
2473 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00002474 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00002475 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002476}
2477
2478Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
2479 Constant *Mask) {
2480 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
2481 "Invalid shuffle vector constant expr operands!");
Nate Begeman94aa38d2009-02-12 21:28:33 +00002482
2483 unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements();
2484 const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
2485 const Type *ShufTy = VectorType::get(EltTy, NElts);
2486 return getShuffleVectorTy(ShufTy, V1, V2, Mask);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002487}
2488
Dan Gohman12fce772008-05-15 19:50:34 +00002489Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg,
2490 Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002491 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002492 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2493 Idxs+NumIdx) == Val->getType() &&
2494 "insertvalue indices invalid!");
2495 assert(Agg->getType() == ReqTy &&
2496 "insertvalue type invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002497 assert(Agg->getType()->isFirstClassType() &&
2498 "Non-first-class type for constant InsertValue expression");
Dan Gohmand5d24f62008-07-21 23:30:30 +00002499 Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs, NumIdx);
2500 assert(FC && "InsertValue constant expr couldn't be folded!");
2501 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00002502}
2503
2504Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002505 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002506 assert(Agg->getType()->isFirstClassType() &&
2507 "Tried to create insertelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002508
Dan Gohman0752bff2008-05-23 00:36:11 +00002509 const Type *ReqTy = Agg->getType();
Devang Pateld26344d2008-11-03 23:20:04 +00002510#ifndef NDEBUG
Dan Gohman0752bff2008-05-23 00:36:11 +00002511 const Type *ValTy =
Dan Gohman12fce772008-05-15 19:50:34 +00002512 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
Devang Pateld26344d2008-11-03 23:20:04 +00002513#endif
Dan Gohman0752bff2008-05-23 00:36:11 +00002514 assert(ValTy == Val->getType() && "insertvalue indices invalid!");
Dan Gohman12fce772008-05-15 19:50:34 +00002515 return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx);
2516}
2517
2518Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002519 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00002520 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2521 Idxs+NumIdx) == ReqTy &&
2522 "extractvalue indices invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00002523 assert(Agg->getType()->isFirstClassType() &&
2524 "Non-first-class type for constant extractvalue expression");
Dan Gohmand5d24f62008-07-21 23:30:30 +00002525 Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs, NumIdx);
2526 assert(FC && "ExtractValue constant expr couldn't be folded!");
2527 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00002528}
2529
2530Constant *ConstantExpr::getExtractValue(Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002531 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002532 assert(Agg->getType()->isFirstClassType() &&
2533 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002534
2535 const Type *ReqTy =
2536 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
2537 assert(ReqTy && "extractvalue indices invalid!");
2538 return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx);
2539}
2540
Reid Spencer2eadb532007-01-21 00:29:26 +00002541Constant *ConstantExpr::getZeroValueForNegationExpr(const Type *Ty) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00002542 if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
Reid Spencer6598ca82007-01-21 02:29:10 +00002543 if (PTy->getElementType()->isFloatingPoint()) {
2544 std::vector<Constant*> zeros(PTy->getNumElements(),
Dale Johannesen98d3a082007-09-14 22:26:36 +00002545 ConstantFP::getNegativeZero(PTy->getElementType()));
Reid Spencerd84d35b2007-02-15 02:26:10 +00002546 return ConstantVector::get(PTy, zeros);
Reid Spencer6598ca82007-01-21 02:29:10 +00002547 }
Reid Spencer2eadb532007-01-21 00:29:26 +00002548
Dale Johannesen98d3a082007-09-14 22:26:36 +00002549 if (Ty->isFloatingPoint())
2550 return ConstantFP::getNegativeZero(Ty);
Reid Spencer2eadb532007-01-21 00:29:26 +00002551
2552 return Constant::getNullValue(Ty);
2553}
2554
Vikram S. Adve4c485332002-07-15 18:19:33 +00002555// destroyConstant - Remove the constant from the constant table...
2556//
2557void ConstantExpr::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00002558 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002559 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002560}
2561
Chris Lattner3cd8c562002-07-30 18:54:25 +00002562const char *ConstantExpr::getOpcodeName() const {
2563 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002564}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00002565
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002566//===----------------------------------------------------------------------===//
2567// replaceUsesOfWithOnConstant implementations
2568
Chris Lattner913849b2007-08-21 00:55:23 +00002569/// replaceUsesOfWithOnConstant - Update this constant array to change uses of
2570/// 'From' to be uses of 'To'. This must update the uniquing data structures
2571/// etc.
2572///
2573/// Note that we intentionally replace all uses of From with To here. Consider
2574/// a large array that uses 'From' 1000 times. By handling this case all here,
2575/// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that
2576/// single invocation handles all 1000 uses. Handling them one at a time would
2577/// work, but would be really slow because it would have to unique each updated
2578/// array instance.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002579void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002580 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002581 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002582 Constant *ToC = cast<Constant>(To);
Chris Lattnerdff59112005-10-04 18:47:09 +00002583
Jim Laskeyc03caef2006-07-17 17:38:29 +00002584 std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnerb64419a2005-10-03 22:51:37 +00002585 Lookup.first.first = getType();
2586 Lookup.second = this;
Chris Lattnerdff59112005-10-04 18:47:09 +00002587
Chris Lattnerb64419a2005-10-03 22:51:37 +00002588 std::vector<Constant*> &Values = Lookup.first.second;
2589 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattnerdff59112005-10-04 18:47:09 +00002590
Chris Lattner8760ec72005-10-04 01:17:50 +00002591 // Fill values with the modified operands of the constant array. Also,
2592 // compute whether this turns into an all-zeros array.
Chris Lattnerdff59112005-10-04 18:47:09 +00002593 bool isAllZeros = false;
Chris Lattner913849b2007-08-21 00:55:23 +00002594 unsigned NumUpdated = 0;
Chris Lattnerdff59112005-10-04 18:47:09 +00002595 if (!ToC->isNullValue()) {
Chris Lattner913849b2007-08-21 00:55:23 +00002596 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2597 Constant *Val = cast<Constant>(O->get());
2598 if (Val == From) {
2599 Val = ToC;
2600 ++NumUpdated;
2601 }
2602 Values.push_back(Val);
2603 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002604 } else {
2605 isAllZeros = true;
2606 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2607 Constant *Val = cast<Constant>(O->get());
Chris Lattner913849b2007-08-21 00:55:23 +00002608 if (Val == From) {
2609 Val = ToC;
2610 ++NumUpdated;
2611 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002612 Values.push_back(Val);
2613 if (isAllZeros) isAllZeros = Val->isNullValue();
2614 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002615 }
2616
Chris Lattnerb64419a2005-10-03 22:51:37 +00002617 Constant *Replacement = 0;
2618 if (isAllZeros) {
2619 Replacement = ConstantAggregateZero::get(getType());
2620 } else {
2621 // Check to see if we have this array type already.
2622 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002623 ArrayConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002624 ArrayConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002625
2626 if (Exists) {
2627 Replacement = I->second;
2628 } else {
2629 // Okay, the new shape doesn't exist in the system yet. Instead of
2630 // creating a new constant array, inserting it, replaceallusesof'ing the
2631 // old with the new, then deleting the old... just update the current one
2632 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002633 ArrayConstants->MoveConstantToNewSlot(this, I);
Chris Lattnerb64419a2005-10-03 22:51:37 +00002634
Chris Lattner913849b2007-08-21 00:55:23 +00002635 // Update to the new value. Optimize for the case when we have a single
2636 // operand that we're changing, but handle bulk updates efficiently.
2637 if (NumUpdated == 1) {
2638 unsigned OperandToUpdate = U-OperandList;
2639 assert(getOperand(OperandToUpdate) == From &&
2640 "ReplaceAllUsesWith broken!");
2641 setOperand(OperandToUpdate, ToC);
2642 } else {
2643 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2644 if (getOperand(i) == From)
2645 setOperand(i, ToC);
2646 }
Chris Lattnerb64419a2005-10-03 22:51:37 +00002647 return;
2648 }
2649 }
2650
2651 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002652 assert(Replacement != this && "I didn't contain From!");
2653
Chris Lattner7a1450d2005-10-04 18:13:04 +00002654 // Everyone using this now uses the replacement.
2655 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002656
2657 // Delete the old constant!
2658 destroyConstant();
2659}
2660
2661void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002662 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002663 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00002664 Constant *ToC = cast<Constant>(To);
2665
Chris Lattnerdff59112005-10-04 18:47:09 +00002666 unsigned OperandToUpdate = U-OperandList;
2667 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
2668
Jim Laskeyc03caef2006-07-17 17:38:29 +00002669 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattner8760ec72005-10-04 01:17:50 +00002670 Lookup.first.first = getType();
2671 Lookup.second = this;
2672 std::vector<Constant*> &Values = Lookup.first.second;
2673 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002674
Chris Lattnerdff59112005-10-04 18:47:09 +00002675
Chris Lattner8760ec72005-10-04 01:17:50 +00002676 // Fill values with the modified operands of the constant struct. Also,
2677 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00002678 bool isAllZeros = false;
2679 if (!ToC->isNullValue()) {
2680 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
2681 Values.push_back(cast<Constant>(O->get()));
2682 } else {
2683 isAllZeros = true;
2684 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2685 Constant *Val = cast<Constant>(O->get());
2686 Values.push_back(Val);
2687 if (isAllZeros) isAllZeros = Val->isNullValue();
2688 }
Chris Lattner8760ec72005-10-04 01:17:50 +00002689 }
Chris Lattnerdff59112005-10-04 18:47:09 +00002690 Values[OperandToUpdate] = ToC;
2691
Chris Lattner8760ec72005-10-04 01:17:50 +00002692 Constant *Replacement = 0;
2693 if (isAllZeros) {
2694 Replacement = ConstantAggregateZero::get(getType());
2695 } else {
2696 // Check to see if we have this array type already.
2697 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00002698 StructConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00002699 StructConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00002700
2701 if (Exists) {
2702 Replacement = I->second;
2703 } else {
2704 // Okay, the new shape doesn't exist in the system yet. Instead of
2705 // creating a new constant struct, inserting it, replaceallusesof'ing the
2706 // old with the new, then deleting the old... just update the current one
2707 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00002708 StructConstants->MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00002709
Chris Lattnerdff59112005-10-04 18:47:09 +00002710 // Update to the new value.
2711 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00002712 return;
2713 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002714 }
2715
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002716 assert(Replacement != this && "I didn't contain From!");
2717
Chris Lattner7a1450d2005-10-04 18:13:04 +00002718 // Everyone using this now uses the replacement.
2719 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002720
2721 // Delete the old constant!
2722 destroyConstant();
2723}
2724
Reid Spencerd84d35b2007-02-15 02:26:10 +00002725void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002726 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002727 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2728
2729 std::vector<Constant*> Values;
2730 Values.reserve(getNumOperands()); // Build replacement array...
2731 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2732 Constant *Val = getOperand(i);
2733 if (Val == From) Val = cast<Constant>(To);
2734 Values.push_back(Val);
2735 }
2736
Reid Spencerd84d35b2007-02-15 02:26:10 +00002737 Constant *Replacement = ConstantVector::get(getType(), Values);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002738 assert(Replacement != this && "I didn't contain From!");
2739
Chris Lattner7a1450d2005-10-04 18:13:04 +00002740 // Everyone using this now uses the replacement.
2741 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002742
2743 // Delete the old constant!
2744 destroyConstant();
2745}
2746
2747void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002748 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002749 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2750 Constant *To = cast<Constant>(ToV);
2751
2752 Constant *Replacement = 0;
2753 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerb5d70302007-02-19 20:01:23 +00002754 SmallVector<Constant*, 8> Indices;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002755 Constant *Pointer = getOperand(0);
2756 Indices.reserve(getNumOperands()-1);
2757 if (Pointer == From) Pointer = To;
2758
2759 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2760 Constant *Val = getOperand(i);
2761 if (Val == From) Val = To;
2762 Indices.push_back(Val);
2763 }
Chris Lattnerb5d70302007-02-19 20:01:23 +00002764 Replacement = ConstantExpr::getGetElementPtr(Pointer,
2765 &Indices[0], Indices.size());
Dan Gohman12fce772008-05-15 19:50:34 +00002766 } else if (getOpcode() == Instruction::ExtractValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002767 Constant *Agg = getOperand(0);
Dan Gohman12fce772008-05-15 19:50:34 +00002768 if (Agg == From) Agg = To;
2769
Dan Gohman1ecaf452008-05-31 00:58:22 +00002770 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002771 Replacement = ConstantExpr::getExtractValue(Agg,
2772 &Indices[0], Indices.size());
2773 } else if (getOpcode() == Instruction::InsertValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002774 Constant *Agg = getOperand(0);
2775 Constant *Val = getOperand(1);
Dan Gohman12fce772008-05-15 19:50:34 +00002776 if (Agg == From) Agg = To;
2777 if (Val == From) Val = To;
2778
Dan Gohman1ecaf452008-05-31 00:58:22 +00002779 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002780 Replacement = ConstantExpr::getInsertValue(Agg, Val,
2781 &Indices[0], Indices.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002782 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002783 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002784 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002785 } else if (getOpcode() == Instruction::Select) {
2786 Constant *C1 = getOperand(0);
2787 Constant *C2 = getOperand(1);
2788 Constant *C3 = getOperand(2);
2789 if (C1 == From) C1 = To;
2790 if (C2 == From) C2 = To;
2791 if (C3 == From) C3 = To;
2792 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00002793 } else if (getOpcode() == Instruction::ExtractElement) {
2794 Constant *C1 = getOperand(0);
2795 Constant *C2 = getOperand(1);
2796 if (C1 == From) C1 = To;
2797 if (C2 == From) C2 = To;
2798 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00002799 } else if (getOpcode() == Instruction::InsertElement) {
2800 Constant *C1 = getOperand(0);
2801 Constant *C2 = getOperand(1);
2802 Constant *C3 = getOperand(1);
2803 if (C1 == From) C1 = To;
2804 if (C2 == From) C2 = To;
2805 if (C3 == From) C3 = To;
2806 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2807 } else if (getOpcode() == Instruction::ShuffleVector) {
2808 Constant *C1 = getOperand(0);
2809 Constant *C2 = getOperand(1);
2810 Constant *C3 = getOperand(2);
2811 if (C1 == From) C1 = To;
2812 if (C2 == From) C2 = To;
2813 if (C3 == From) C3 = To;
2814 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00002815 } else if (isCompare()) {
2816 Constant *C1 = getOperand(0);
2817 Constant *C2 = getOperand(1);
2818 if (C1 == From) C1 = To;
2819 if (C2 == From) C2 = To;
2820 if (getOpcode() == Instruction::ICmp)
2821 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002822 else if (getOpcode() == Instruction::FCmp)
Reid Spenceree3c9912006-12-04 05:19:50 +00002823 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002824 else if (getOpcode() == Instruction::VICmp)
2825 Replacement = ConstantExpr::getVICmp(getPredicate(), C1, C2);
2826 else {
2827 assert(getOpcode() == Instruction::VFCmp);
2828 Replacement = ConstantExpr::getVFCmp(getPredicate(), C1, C2);
2829 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002830 } else if (getNumOperands() == 2) {
2831 Constant *C1 = getOperand(0);
2832 Constant *C2 = getOperand(1);
2833 if (C1 == From) C1 = To;
2834 if (C2 == From) C2 = To;
2835 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2836 } else {
2837 assert(0 && "Unknown ConstantExpr type!");
2838 return;
2839 }
2840
2841 assert(Replacement != this && "I didn't contain From!");
2842
Chris Lattner7a1450d2005-10-04 18:13:04 +00002843 // Everyone using this now uses the replacement.
2844 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002845
2846 // Delete the old constant!
2847 destroyConstant();
Matthijs Kooijmanba5d7ef2008-07-03 07:46:41 +00002848}
Nick Lewycky49f89192009-04-04 07:22:01 +00002849
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002850void MDNode::replaceElement(Value *From, Value *To) {
2851 SmallVector<Value*, 4> Values;
2852 Values.reserve(getNumElements()); // Build replacement array...
2853 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2854 Value *Val = getElement(i);
2855 if (Val == From) Val = To;
Nick Lewycky49f89192009-04-04 07:22:01 +00002856 Values.push_back(Val);
2857 }
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002858
2859 MDNode *Replacement = MDNode::get(&Values[0], Values.size());
Nick Lewycky49f89192009-04-04 07:22:01 +00002860 assert(Replacement != this && "I didn't contain From!");
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002861
Nick Lewycky49f89192009-04-04 07:22:01 +00002862 uncheckedReplaceAllUsesWith(Replacement);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00002863
Nick Lewycky49f89192009-04-04 07:22:01 +00002864 destroyConstant();
2865}