blob: 7482154d839561f17dc7638e2272821091360b10 [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
Owen Andersonedb4a702009-07-24 23:12:02 +000014#include "LLVMContextImpl.h"
Chris Lattnerca142372002-04-28 19:55:58 +000015#include "llvm/Constants.h"
Chris Lattner33e93b82007-02-27 03:05:06 +000016#include "ConstantFold.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000017#include "llvm/DerivedTypes.h"
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000018#include "llvm/GlobalValue.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000019#include "llvm/Instructions.h"
Chris Lattnerd7a73302001-10-13 06:57:33 +000020#include "llvm/Module.h"
Dan Gohman7d82e132009-07-18 01:49:22 +000021#include "llvm/Operator.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000022#include "llvm/ADT/FoldingSet.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000023#include "llvm/ADT/StringExtras.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000024#include "llvm/ADT/StringMap.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000025#include "llvm/Support/Compiler.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000026#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000027#include "llvm/Support/ErrorHandling.h"
Chris Lattner69edc982006-09-28 00:35:06 +000028#include "llvm/Support/ManagedStatic.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000029#include "llvm/Support/MathExtras.h"
Owen Anderson0d2de8c2009-06-20 00:24:58 +000030#include "llvm/System/Mutex.h"
Owen Anderson2d7231d2009-06-17 18:40:29 +000031#include "llvm/System/RWMutex.h"
Owen Anderson7d42b952009-06-18 16:54:52 +000032#include "llvm/System/Threading.h"
Chris Lattnera80bf0b2007-02-20 06:39:57 +000033#include "llvm/ADT/DenseMap.h"
Chris Lattnerb5d70302007-02-19 20:01:23 +000034#include "llvm/ADT/SmallVector.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000035#include <algorithm>
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000036#include <map>
Chris Lattner189d19f2003-11-21 20:23:48 +000037using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000038
Chris Lattner2f7c9632001-06-06 20:29:01 +000039//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000040// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000041//===----------------------------------------------------------------------===//
42
Owen Anderson5a1acd92009-07-31 20:28:14 +000043// Constructor to create a '0' constant of arbitrary type...
44static const uint64_t zero[2] = {0, 0};
45Constant* Constant::getNullValue(const Type* Ty) {
46 switch (Ty->getTypeID()) {
47 case Type::IntegerTyID:
48 return ConstantInt::get(Ty, 0);
49 case Type::FloatTyID:
50 return ConstantFP::get(Ty->getContext(), APFloat(APInt(32, 0)));
51 case Type::DoubleTyID:
52 return ConstantFP::get(Ty->getContext(), APFloat(APInt(64, 0)));
53 case Type::X86_FP80TyID:
54 return ConstantFP::get(Ty->getContext(), APFloat(APInt(80, 2, zero)));
55 case Type::FP128TyID:
56 return ConstantFP::get(Ty->getContext(),
57 APFloat(APInt(128, 2, zero), true));
58 case Type::PPC_FP128TyID:
59 return ConstantFP::get(Ty->getContext(), APFloat(APInt(128, 2, zero)));
60 case Type::PointerTyID:
61 return ConstantPointerNull::get(cast<PointerType>(Ty));
62 case Type::StructTyID:
63 case Type::ArrayTyID:
64 case Type::VectorTyID:
65 return ConstantAggregateZero::get(Ty);
66 default:
67 // Function, Label, or Opaque type?
68 assert(!"Cannot create a null constant of that type!");
69 return 0;
70 }
71}
72
73Constant* Constant::getAllOnesValue(const Type* Ty) {
74 if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty))
75 return ConstantInt::get(Ty->getContext(),
76 APInt::getAllOnesValue(ITy->getBitWidth()));
77
78 std::vector<Constant*> Elts;
79 const VectorType* VTy = cast<VectorType>(Ty);
80 Elts.resize(VTy->getNumElements(), getAllOnesValue(VTy->getElementType()));
81 assert(Elts[0] && "Not a vector integer type!");
82 return cast<ConstantVector>(ConstantVector::get(Elts));
83}
84
Chris Lattner3462ae32001-12-03 22:26:30 +000085void Constant::destroyConstantImpl() {
86 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000087 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000088 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +000089 // but they don't know that. Because we only find out when the CPV is
90 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000091 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000092 //
93 while (!use_empty()) {
94 Value *V = use_back();
95#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000096 if (!isa<Constant>(V))
Bill Wendling6a462f12006-11-17 08:03:48 +000097 DOUT << "While deleting: " << *this
98 << "\n\nUse still stuck around after Def is destroyed: "
99 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +0000100#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000101 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1ebe1ab2004-07-17 23:48:33 +0000102 Constant *CV = cast<Constant>(V);
103 CV->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +0000104
105 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000106 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +0000107 }
108
109 // Value has no outstanding references it is safe to delete it now...
110 delete this;
Chris Lattner38569342001-10-01 20:11:19 +0000111}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000112
Chris Lattner23dd1f62006-10-20 00:27:06 +0000113/// canTrap - Return true if evaluation of this constant could trap. This is
114/// true for things like constant expressions that could divide by zero.
115bool Constant::canTrap() const {
116 assert(getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
117 // The only thing that could possibly trap are constant exprs.
118 const ConstantExpr *CE = dyn_cast<ConstantExpr>(this);
119 if (!CE) return false;
120
121 // ConstantExpr traps if any operands can trap.
122 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
123 if (getOperand(i)->canTrap())
124 return true;
125
126 // Otherwise, only specific operations can trap.
127 switch (CE->getOpcode()) {
128 default:
129 return false;
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000130 case Instruction::UDiv:
131 case Instruction::SDiv:
132 case Instruction::FDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +0000133 case Instruction::URem:
134 case Instruction::SRem:
135 case Instruction::FRem:
Chris Lattner23dd1f62006-10-20 00:27:06 +0000136 // Div and rem can trap if the RHS is not known to be non-zero.
137 if (!isa<ConstantInt>(getOperand(1)) || getOperand(1)->isNullValue())
138 return true;
139 return false;
140 }
141}
142
Chris Lattner4565ef52009-07-22 00:05:44 +0000143
144/// getRelocationInfo - This method classifies the entry according to
145/// whether or not it may generate a relocation entry. This must be
146/// conservative, so if it might codegen to a relocatable entry, it should say
147/// so. The return values are:
148///
Chris Lattner5cd4dd32009-07-24 03:27:21 +0000149/// NoRelocation: This constant pool entry is guaranteed to never have a
150/// relocation applied to it (because it holds a simple constant like
151/// '4').
152/// LocalRelocation: This entry has relocations, but the entries are
153/// guaranteed to be resolvable by the static linker, so the dynamic
154/// linker will never see them.
155/// GlobalRelocations: This entry may have arbitrary relocations.
Chris Lattner4565ef52009-07-22 00:05:44 +0000156///
157/// FIXME: This really should not be in VMCore.
Chris Lattner5cd4dd32009-07-24 03:27:21 +0000158Constant::PossibleRelocationsTy Constant::getRelocationInfo() const {
159 if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
Chris Lattner4565ef52009-07-22 00:05:44 +0000160 if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
Chris Lattner5cd4dd32009-07-24 03:27:21 +0000161 return LocalRelocation; // Local to this file/library.
162 return GlobalRelocations; // Global reference.
Anton Korobeynikov7437b592009-03-29 17:13:18 +0000163 }
Chris Lattner4565ef52009-07-22 00:05:44 +0000164
Chris Lattner5cd4dd32009-07-24 03:27:21 +0000165 PossibleRelocationsTy Result = NoRelocation;
Evan Chengf9e003b2007-03-08 00:59:12 +0000166 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Chris Lattner4565ef52009-07-22 00:05:44 +0000167 Result = std::max(Result, getOperand(i)->getRelocationInfo());
168
169 return Result;
Evan Chengf9e003b2007-03-08 00:59:12 +0000170}
171
Chris Lattner4565ef52009-07-22 00:05:44 +0000172
Chris Lattner2105d662008-07-10 00:28:11 +0000173/// getVectorElements - This method, which is only valid on constant of vector
174/// type, returns the elements of the vector in the specified smallvector.
Chris Lattnerc5098a22008-07-14 05:10:41 +0000175/// This handles breaking down a vector undef into undef elements, etc. For
176/// constant exprs and other cases we can't handle, we return an empty vector.
Owen Anderson53a52212009-07-13 04:09:18 +0000177void Constant::getVectorElements(LLVMContext &Context,
178 SmallVectorImpl<Constant*> &Elts) const {
Chris Lattner2105d662008-07-10 00:28:11 +0000179 assert(isa<VectorType>(getType()) && "Not a vector constant!");
180
181 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) {
182 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i)
183 Elts.push_back(CV->getOperand(i));
184 return;
185 }
186
187 const VectorType *VT = cast<VectorType>(getType());
188 if (isa<ConstantAggregateZero>(this)) {
189 Elts.assign(VT->getNumElements(),
Owen Anderson5a1acd92009-07-31 20:28:14 +0000190 Constant::getNullValue(VT->getElementType()));
Chris Lattner2105d662008-07-10 00:28:11 +0000191 return;
192 }
193
Chris Lattnerc5098a22008-07-14 05:10:41 +0000194 if (isa<UndefValue>(this)) {
Owen Andersonb292b8c2009-07-30 23:03:37 +0000195 Elts.assign(VT->getNumElements(), UndefValue::get(VT->getElementType()));
Chris Lattnerc5098a22008-07-14 05:10:41 +0000196 return;
197 }
198
199 // Unknown type, must be constant expr etc.
Chris Lattner2105d662008-07-10 00:28:11 +0000200}
201
202
203
Chris Lattner2f7c9632001-06-06 20:29:01 +0000204//===----------------------------------------------------------------------===//
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000205// ConstantInt
Chris Lattner2f7c9632001-06-06 20:29:01 +0000206//===----------------------------------------------------------------------===//
207
Reid Spencerb31bffe2007-02-26 23:54:03 +0000208ConstantInt::ConstantInt(const IntegerType *Ty, const APInt& V)
Chris Lattner5db2f472007-02-20 05:55:46 +0000209 : Constant(Ty, ConstantIntVal, 0, 0), Val(V) {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000210 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000211}
212
Owen Anderson23a204d2009-07-31 17:39:07 +0000213ConstantInt* ConstantInt::getTrue(LLVMContext &Context) {
214 LLVMContextImpl *pImpl = Context.pImpl;
215 sys::SmartScopedWriter<true>(pImpl->ConstantsLock);
216 if (pImpl->TheTrueVal)
217 return pImpl->TheTrueVal;
218 else
219 return (pImpl->TheTrueVal = ConstantInt::get(IntegerType::get(1), 1));
220}
221
222ConstantInt* ConstantInt::getFalse(LLVMContext &Context) {
223 LLVMContextImpl *pImpl = Context.pImpl;
224 sys::SmartScopedWriter<true>(pImpl->ConstantsLock);
225 if (pImpl->TheFalseVal)
226 return pImpl->TheFalseVal;
227 else
228 return (pImpl->TheFalseVal = ConstantInt::get(IntegerType::get(1), 0));
229}
230
231
Owen Andersonedb4a702009-07-24 23:12:02 +0000232// Get a ConstantInt from an APInt. Note that the value stored in the DenseMap
233// as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the
234// operator== and operator!= to ensure that the DenseMap doesn't attempt to
235// compare APInt's of different widths, which would violate an APInt class
236// invariant which generates an assertion.
237ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt& V) {
238 // Get the corresponding integer type for the bit width of the value.
Owen Anderson4056ca92009-07-29 22:17:13 +0000239 const IntegerType *ITy = IntegerType::get(V.getBitWidth());
Owen Andersonedb4a702009-07-24 23:12:02 +0000240 // get an existing value or the insertion position
241 DenseMapAPIntKeyInfo::KeyTy Key(V, ITy);
242
243 Context.pImpl->ConstantsLock.reader_acquire();
244 ConstantInt *&Slot = Context.pImpl->IntConstants[Key];
245 Context.pImpl->ConstantsLock.reader_release();
246
247 if (!Slot) {
248 sys::SmartScopedWriter<true> Writer(Context.pImpl->ConstantsLock);
249 ConstantInt *&NewSlot = Context.pImpl->IntConstants[Key];
250 if (!Slot) {
251 NewSlot = new ConstantInt(ITy, V);
252 }
253
254 return NewSlot;
255 } else {
256 return Slot;
257 }
258}
259
260Constant* ConstantInt::get(const Type* Ty, uint64_t V, bool isSigned) {
261 Constant *C = get(cast<IntegerType>(Ty->getScalarType()),
262 V, isSigned);
263
264 // For vectors, broadcast the value.
265 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
Owen Anderson4aa32952009-07-28 21:19:26 +0000266 return ConstantVector::get(
Owen Andersonedb4a702009-07-24 23:12:02 +0000267 std::vector<Constant *>(VTy->getNumElements(), C));
268
269 return C;
270}
271
272ConstantInt* ConstantInt::get(const IntegerType* Ty, uint64_t V,
273 bool isSigned) {
274 return get(Ty->getContext(), APInt(Ty->getBitWidth(), V, isSigned));
275}
276
277ConstantInt* ConstantInt::getSigned(const IntegerType* Ty, int64_t V) {
278 return get(Ty, V, true);
279}
280
281Constant *ConstantInt::getSigned(const Type *Ty, int64_t V) {
282 return get(Ty, V, true);
283}
284
285Constant* ConstantInt::get(const Type* Ty, const APInt& V) {
286 ConstantInt *C = get(Ty->getContext(), V);
287 assert(C->getType() == Ty->getScalarType() &&
288 "ConstantInt type doesn't match the type implied by its value!");
289
290 // For vectors, broadcast the value.
291 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
Owen Anderson4aa32952009-07-28 21:19:26 +0000292 return ConstantVector::get(
Owen Andersonedb4a702009-07-24 23:12:02 +0000293 std::vector<Constant *>(VTy->getNumElements(), C));
294
295 return C;
296}
297
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000298//===----------------------------------------------------------------------===//
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000299// ConstantFP
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000300//===----------------------------------------------------------------------===//
301
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000302static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
303 if (Ty == Type::FloatTy)
304 return &APFloat::IEEEsingle;
305 if (Ty == Type::DoubleTy)
306 return &APFloat::IEEEdouble;
307 if (Ty == Type::X86_FP80Ty)
308 return &APFloat::x87DoubleExtended;
309 else if (Ty == Type::FP128Ty)
310 return &APFloat::IEEEquad;
311
312 assert(Ty == Type::PPC_FP128Ty && "Unknown FP format");
313 return &APFloat::PPCDoubleDouble;
314}
315
Owen Anderson69c464d2009-07-27 20:59:43 +0000316/// get() - This returns a constant fp for the specified value in the
317/// specified type. This should only be used for simple constant values like
318/// 2.0/1.0 etc, that are known-valid both as double and as the target format.
319Constant* ConstantFP::get(const Type* Ty, double V) {
320 LLVMContext &Context = Ty->getContext();
321
322 APFloat FV(V);
323 bool ignored;
324 FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
325 APFloat::rmNearestTiesToEven, &ignored);
326 Constant *C = get(Context, FV);
327
328 // For vectors, broadcast the value.
329 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
Owen Anderson4aa32952009-07-28 21:19:26 +0000330 return ConstantVector::get(
Owen Anderson69c464d2009-07-27 20:59:43 +0000331 std::vector<Constant *>(VTy->getNumElements(), C));
332
333 return C;
334}
335
336ConstantFP* ConstantFP::getNegativeZero(const Type* Ty) {
337 LLVMContext &Context = Ty->getContext();
Owen Anderson5a1acd92009-07-31 20:28:14 +0000338 APFloat apf = cast <ConstantFP>(Constant::getNullValue(Ty))->getValueAPF();
Owen Anderson69c464d2009-07-27 20:59:43 +0000339 apf.changeSign();
340 return get(Context, apf);
341}
342
343
344Constant* ConstantFP::getZeroValueForNegation(const Type* Ty) {
Owen Anderson69c464d2009-07-27 20:59:43 +0000345 if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
346 if (PTy->getElementType()->isFloatingPoint()) {
347 std::vector<Constant*> zeros(PTy->getNumElements(),
348 getNegativeZero(PTy->getElementType()));
Owen Anderson4aa32952009-07-28 21:19:26 +0000349 return ConstantVector::get(PTy, zeros);
Owen Anderson69c464d2009-07-27 20:59:43 +0000350 }
351
352 if (Ty->isFloatingPoint())
353 return getNegativeZero(Ty);
354
Owen Anderson5a1acd92009-07-31 20:28:14 +0000355 return Constant::getNullValue(Ty);
Owen Anderson69c464d2009-07-27 20:59:43 +0000356}
357
358
359// ConstantFP accessors.
360ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) {
361 DenseMapAPFloatKeyInfo::KeyTy Key(V);
362
363 LLVMContextImpl* pImpl = Context.pImpl;
364
365 pImpl->ConstantsLock.reader_acquire();
366 ConstantFP *&Slot = pImpl->FPConstants[Key];
367 pImpl->ConstantsLock.reader_release();
368
369 if (!Slot) {
370 sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
371 ConstantFP *&NewSlot = pImpl->FPConstants[Key];
372 if (!NewSlot) {
373 const Type *Ty;
374 if (&V.getSemantics() == &APFloat::IEEEsingle)
375 Ty = Type::FloatTy;
376 else if (&V.getSemantics() == &APFloat::IEEEdouble)
377 Ty = Type::DoubleTy;
378 else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
379 Ty = Type::X86_FP80Ty;
380 else if (&V.getSemantics() == &APFloat::IEEEquad)
381 Ty = Type::FP128Ty;
382 else {
383 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble &&
384 "Unknown FP format");
385 Ty = Type::PPC_FP128Ty;
386 }
387 NewSlot = new ConstantFP(Ty, V);
388 }
389
390 return NewSlot;
391 }
392
393 return Slot;
394}
395
Dale Johannesend246b2c2007-08-30 00:23:21 +0000396ConstantFP::ConstantFP(const Type *Ty, const APFloat& V)
397 : Constant(Ty, ConstantFPVal, 0, 0), Val(V) {
Chris Lattner98bd9392008-04-09 06:38:30 +0000398 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
399 "FP type Mismatch");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000400}
401
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000402bool ConstantFP::isNullValue() const {
Dale Johannesena719a602007-08-24 00:56:33 +0000403 return Val.isZero() && !Val.isNegative();
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000404}
405
Dale Johannesend246b2c2007-08-30 00:23:21 +0000406bool ConstantFP::isExactlyValue(const APFloat& V) const {
407 return Val.bitwiseIsEqual(V);
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000408}
409
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000410//===----------------------------------------------------------------------===//
411// ConstantXXX Classes
412//===----------------------------------------------------------------------===//
413
414
Chris Lattner3462ae32001-12-03 22:26:30 +0000415ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000416 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000417 : Constant(T, ConstantArrayVal,
418 OperandTraits<ConstantArray>::op_end(this) - V.size(),
419 V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000420 assert(V.size() == T->getNumElements() &&
421 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000422 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000423 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
424 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000425 Constant *C = *I;
426 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000427 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000428 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000429 "Initializer for array element doesn't match array element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000430 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000431 }
432}
433
Owen Andersonc2c79322009-07-28 18:32:17 +0000434Constant *ConstantArray::get(const ArrayType *Ty,
435 const std::vector<Constant*> &V) {
436 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
437 // If this is an all-zero array, return a ConstantAggregateZero object
438 if (!V.empty()) {
439 Constant *C = V[0];
440 if (!C->isNullValue()) {
441 // Implicitly locked.
442 return pImpl->ArrayConstants.getOrCreate(Ty, V);
443 }
444 for (unsigned i = 1, e = V.size(); i != e; ++i)
445 if (V[i] != C) {
446 // Implicitly locked.
447 return pImpl->ArrayConstants.getOrCreate(Ty, V);
448 }
449 }
450
Owen Andersonb292b8c2009-07-30 23:03:37 +0000451 return ConstantAggregateZero::get(Ty);
Owen Andersonc2c79322009-07-28 18:32:17 +0000452}
453
454
455Constant* ConstantArray::get(const ArrayType* T, Constant* const* Vals,
456 unsigned NumVals) {
457 // FIXME: make this the primary ctor method.
458 return get(T, std::vector<Constant*>(Vals, Vals+NumVals));
459}
460
461/// ConstantArray::get(const string&) - Return an array that is initialized to
462/// contain the specified string. If length is zero then a null terminator is
463/// added to the specified string so that it may be used in a natural way.
464/// Otherwise, the length parameter specifies how much of the string to use
465/// and it won't be null terminated.
466///
467Constant* ConstantArray::get(const StringRef &Str, bool AddNull) {
468 std::vector<Constant*> ElementVals;
469 for (unsigned i = 0; i < Str.size(); ++i)
470 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, Str[i]));
471
472 // Add a null terminator to the string...
473 if (AddNull) {
474 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, 0));
475 }
476
477 ArrayType *ATy = ArrayType::get(Type::Int8Ty, ElementVals.size());
478 return get(ATy, ElementVals);
479}
480
481
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000482
Chris Lattner3462ae32001-12-03 22:26:30 +0000483ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000484 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000485 : Constant(T, ConstantStructVal,
486 OperandTraits<ConstantStruct>::op_end(this) - V.size(),
487 V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000488 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000489 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000490 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000491 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
492 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000493 Constant *C = *I;
494 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000495 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000496 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000497 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000498 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000499 "Initializer for struct element doesn't match struct element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000500 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000501 }
502}
503
Owen Anderson45308b52009-07-27 22:29:26 +0000504// ConstantStruct accessors.
505Constant* ConstantStruct::get(const StructType* T,
506 const std::vector<Constant*>& V) {
507 LLVMContextImpl* pImpl = T->getContext().pImpl;
508
509 // Create a ConstantAggregateZero value if all elements are zeros...
510 for (unsigned i = 0, e = V.size(); i != e; ++i)
511 if (!V[i]->isNullValue())
512 // Implicitly locked.
513 return pImpl->StructConstants.getOrCreate(T, V);
514
Owen Andersonb292b8c2009-07-30 23:03:37 +0000515 return ConstantAggregateZero::get(T);
Owen Anderson45308b52009-07-27 22:29:26 +0000516}
517
518Constant* ConstantStruct::get(const std::vector<Constant*>& V, bool packed) {
519 std::vector<const Type*> StructEls;
520 StructEls.reserve(V.size());
521 for (unsigned i = 0, e = V.size(); i != e; ++i)
522 StructEls.push_back(V[i]->getType());
523 return get(StructType::get(StructEls, packed), V);
524}
525
526Constant* ConstantStruct::get(Constant* const *Vals, unsigned NumVals,
527 bool Packed) {
528 // FIXME: make this the primary ctor method.
529 return get(std::vector<Constant*>(Vals, Vals+NumVals), Packed);
530}
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000531
Reid Spencerd84d35b2007-02-15 02:26:10 +0000532ConstantVector::ConstantVector(const VectorType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000533 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000534 : Constant(T, ConstantVectorVal,
535 OperandTraits<ConstantVector>::op_end(this) - V.size(),
536 V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000537 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000538 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
539 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000540 Constant *C = *I;
541 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000542 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000543 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Dan Gohman30978072007-05-24 14:36:04 +0000544 "Initializer for vector element doesn't match vector element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000545 *OL = C;
Brian Gaeke02209042004-08-20 06:00:58 +0000546 }
547}
548
Owen Anderson4aa32952009-07-28 21:19:26 +0000549// ConstantVector accessors.
550Constant* ConstantVector::get(const VectorType* T,
551 const std::vector<Constant*>& V) {
552 assert(!V.empty() && "Vectors can't be empty");
553 LLVMContext &Context = T->getContext();
554 LLVMContextImpl *pImpl = Context.pImpl;
555
556 // If this is an all-undef or alll-zero vector, return a
557 // ConstantAggregateZero or UndefValue.
558 Constant *C = V[0];
559 bool isZero = C->isNullValue();
560 bool isUndef = isa<UndefValue>(C);
561
562 if (isZero || isUndef) {
563 for (unsigned i = 1, e = V.size(); i != e; ++i)
564 if (V[i] != C) {
565 isZero = isUndef = false;
566 break;
567 }
568 }
569
570 if (isZero)
Owen Andersonb292b8c2009-07-30 23:03:37 +0000571 return ConstantAggregateZero::get(T);
Owen Anderson4aa32952009-07-28 21:19:26 +0000572 if (isUndef)
Owen Andersonb292b8c2009-07-30 23:03:37 +0000573 return UndefValue::get(T);
Owen Anderson4aa32952009-07-28 21:19:26 +0000574
575 // Implicitly locked.
576 return pImpl->VectorConstants.getOrCreate(T, V);
577}
578
579Constant* ConstantVector::get(const std::vector<Constant*>& V) {
580 assert(!V.empty() && "Cannot infer type if V is empty");
581 return get(VectorType::get(V.front()->getType(),V.size()), V);
582}
583
584Constant* ConstantVector::get(Constant* const* Vals, unsigned NumVals) {
585 // FIXME: make this the primary ctor method.
586 return get(std::vector<Constant*>(Vals, Vals+NumVals));
587}
588
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000589
Gabor Greiff6caff662008-05-10 08:32:32 +0000590namespace llvm {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000591// We declare several classes private to this file, so use an anonymous
592// namespace
593namespace {
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000594
Gordon Henriksen14a55692007-12-10 02:14:30 +0000595/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
596/// behind the scenes to implement unary constant exprs.
597class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000598 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000599public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000600 // allocate space for exactly one operand
601 void *operator new(size_t s) {
602 return User::operator new(s, 1);
603 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000604 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
Gabor Greiff6caff662008-05-10 08:32:32 +0000605 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
606 Op<0>() = C;
607 }
608 /// Transparently provide more efficient getOperand methods.
609 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000610};
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000611
Gordon Henriksen14a55692007-12-10 02:14:30 +0000612/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
613/// behind the scenes to implement binary constant exprs.
614class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000615 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000616public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000617 // allocate space for exactly two operands
618 void *operator new(size_t s) {
619 return User::operator new(s, 2);
620 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000621 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Gabor Greiff6caff662008-05-10 08:32:32 +0000622 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000623 Op<0>() = C1;
624 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000625 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000626 /// Transparently provide more efficient getOperand methods.
627 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000628};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000629
Gordon Henriksen14a55692007-12-10 02:14:30 +0000630/// SelectConstantExpr - This class is private to Constants.cpp, and is used
631/// behind the scenes to implement select constant exprs.
632class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000633 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000634public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000635 // allocate space for exactly three operands
636 void *operator new(size_t s) {
637 return User::operator new(s, 3);
638 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000639 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Gabor Greiff6caff662008-05-10 08:32:32 +0000640 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000641 Op<0>() = C1;
642 Op<1>() = C2;
643 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000644 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000645 /// Transparently provide more efficient getOperand methods.
646 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000647};
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000648
Gordon Henriksen14a55692007-12-10 02:14:30 +0000649/// ExtractElementConstantExpr - This class is private to
650/// Constants.cpp, and is used behind the scenes to implement
651/// extractelement constant exprs.
652class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000653 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000654public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000655 // allocate space for exactly two operands
656 void *operator new(size_t s) {
657 return User::operator new(s, 2);
658 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000659 ExtractElementConstantExpr(Constant *C1, Constant *C2)
660 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000661 Instruction::ExtractElement, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000662 Op<0>() = C1;
663 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000664 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000665 /// Transparently provide more efficient getOperand methods.
666 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000667};
Robert Bocchino23004482006-01-10 19:05:34 +0000668
Gordon Henriksen14a55692007-12-10 02:14:30 +0000669/// InsertElementConstantExpr - This class is private to
670/// Constants.cpp, and is used behind the scenes to implement
671/// insertelement constant exprs.
672class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000673 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000674public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000675 // allocate space for exactly three operands
676 void *operator new(size_t s) {
677 return User::operator new(s, 3);
678 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000679 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
680 : ConstantExpr(C1->getType(), Instruction::InsertElement,
Gabor Greiff6caff662008-05-10 08:32:32 +0000681 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000682 Op<0>() = C1;
683 Op<1>() = C2;
684 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000685 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000686 /// Transparently provide more efficient getOperand methods.
687 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000688};
Robert Bocchinoca27f032006-01-17 20:07:22 +0000689
Gordon Henriksen14a55692007-12-10 02:14:30 +0000690/// ShuffleVectorConstantExpr - This class is private to
691/// Constants.cpp, and is used behind the scenes to implement
692/// shufflevector constant exprs.
693class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000694 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000695public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000696 // allocate space for exactly three operands
697 void *operator new(size_t s) {
698 return User::operator new(s, 3);
699 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000700 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Nate Begeman94aa38d2009-02-12 21:28:33 +0000701 : ConstantExpr(VectorType::get(
702 cast<VectorType>(C1->getType())->getElementType(),
703 cast<VectorType>(C3->getType())->getNumElements()),
704 Instruction::ShuffleVector,
Gabor Greiff6caff662008-05-10 08:32:32 +0000705 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000706 Op<0>() = C1;
707 Op<1>() = C2;
708 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000709 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000710 /// Transparently provide more efficient getOperand methods.
711 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000712};
713
Dan Gohman12fce772008-05-15 19:50:34 +0000714/// ExtractValueConstantExpr - This class is private to
715/// Constants.cpp, and is used behind the scenes to implement
716/// extractvalue constant exprs.
717class VISIBILITY_HIDDEN ExtractValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000718 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000719public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000720 // allocate space for exactly one operand
721 void *operator new(size_t s) {
722 return User::operator new(s, 1);
Dan Gohman12fce772008-05-15 19:50:34 +0000723 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000724 ExtractValueConstantExpr(Constant *Agg,
725 const SmallVector<unsigned, 4> &IdxList,
726 const Type *DestTy)
727 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
728 Indices(IdxList) {
729 Op<0>() = Agg;
730 }
731
Dan Gohman7bb04502008-05-31 19:09:08 +0000732 /// Indices - These identify which value to extract.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000733 const SmallVector<unsigned, 4> Indices;
734
Dan Gohman12fce772008-05-15 19:50:34 +0000735 /// Transparently provide more efficient getOperand methods.
736 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
737};
738
739/// InsertValueConstantExpr - This class is private to
740/// Constants.cpp, and is used behind the scenes to implement
741/// insertvalue constant exprs.
742class VISIBILITY_HIDDEN InsertValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000743 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000744public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000745 // allocate space for exactly one operand
746 void *operator new(size_t s) {
747 return User::operator new(s, 2);
Dan Gohman12fce772008-05-15 19:50:34 +0000748 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000749 InsertValueConstantExpr(Constant *Agg, Constant *Val,
750 const SmallVector<unsigned, 4> &IdxList,
751 const Type *DestTy)
752 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
753 Indices(IdxList) {
754 Op<0>() = Agg;
755 Op<1>() = Val;
756 }
757
Dan Gohman7bb04502008-05-31 19:09:08 +0000758 /// Indices - These identify the position for the insertion.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000759 const SmallVector<unsigned, 4> Indices;
760
Dan Gohman12fce772008-05-15 19:50:34 +0000761 /// Transparently provide more efficient getOperand methods.
762 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
763};
764
765
Gordon Henriksen14a55692007-12-10 02:14:30 +0000766/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
767/// used behind the scenes to implement getelementpr constant exprs.
Gabor Greife9ecc682008-04-06 20:25:17 +0000768class VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000769 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000770 const Type *DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000771public:
Gabor Greif697e94c2008-05-15 10:04:30 +0000772 static GetElementPtrConstantExpr *Create(Constant *C,
773 const std::vector<Constant*>&IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000774 const Type *DestTy) {
Dan Gohman33a3fd02009-07-20 17:43:30 +0000775 return
776 new(IdxList.size() + 1) GetElementPtrConstantExpr(C, IdxList, DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000777 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000778 /// Transparently provide more efficient getOperand methods.
779 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000780};
781
782// CompareConstantExpr - This class is private to Constants.cpp, and is used
783// behind the scenes to implement ICmp and FCmp constant expressions. This is
784// needed in order to store the predicate value for these instructions.
785struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000786 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
787 // allocate space for exactly two operands
788 void *operator new(size_t s) {
789 return User::operator new(s, 2);
790 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000791 unsigned short predicate;
Nate Begemand2195702008-05-12 19:01:56 +0000792 CompareConstantExpr(const Type *ty, Instruction::OtherOps opc,
793 unsigned short pred, Constant* LHS, Constant* RHS)
794 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000795 Op<0>() = LHS;
796 Op<1>() = RHS;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000797 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000798 /// Transparently provide more efficient getOperand methods.
799 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000800};
801
802} // end anonymous namespace
803
Gabor Greiff6caff662008-05-10 08:32:32 +0000804template <>
805struct OperandTraits<UnaryConstantExpr> : FixedNumOperandTraits<1> {
806};
807DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
808
809template <>
810struct OperandTraits<BinaryConstantExpr> : FixedNumOperandTraits<2> {
811};
812DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
813
814template <>
815struct OperandTraits<SelectConstantExpr> : FixedNumOperandTraits<3> {
816};
817DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
818
819template <>
820struct OperandTraits<ExtractElementConstantExpr> : FixedNumOperandTraits<2> {
821};
822DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
823
824template <>
825struct OperandTraits<InsertElementConstantExpr> : FixedNumOperandTraits<3> {
826};
827DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
828
829template <>
830struct OperandTraits<ShuffleVectorConstantExpr> : FixedNumOperandTraits<3> {
831};
832DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
833
Dan Gohman12fce772008-05-15 19:50:34 +0000834template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000835struct OperandTraits<ExtractValueConstantExpr> : FixedNumOperandTraits<1> {
Dan Gohman12fce772008-05-15 19:50:34 +0000836};
Dan Gohman12fce772008-05-15 19:50:34 +0000837DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
838
839template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000840struct OperandTraits<InsertValueConstantExpr> : FixedNumOperandTraits<2> {
Dan Gohman12fce772008-05-15 19:50:34 +0000841};
Dan Gohman12fce772008-05-15 19:50:34 +0000842DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
843
Gabor Greiff6caff662008-05-10 08:32:32 +0000844template <>
845struct OperandTraits<GetElementPtrConstantExpr> : VariadicOperandTraits<1> {
846};
847
848GetElementPtrConstantExpr::GetElementPtrConstantExpr
849 (Constant *C,
850 const std::vector<Constant*> &IdxList,
851 const Type *DestTy)
852 : ConstantExpr(DestTy, Instruction::GetElementPtr,
853 OperandTraits<GetElementPtrConstantExpr>::op_end(this)
854 - (IdxList.size()+1),
855 IdxList.size()+1) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000856 OperandList[0] = C;
Gabor Greiff6caff662008-05-10 08:32:32 +0000857 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000858 OperandList[i+1] = IdxList[i];
Gabor Greiff6caff662008-05-10 08:32:32 +0000859}
860
861DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
862
863
864template <>
865struct OperandTraits<CompareConstantExpr> : FixedNumOperandTraits<2> {
866};
867DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
868
869
870} // End llvm namespace
871
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000872
873// Utility function for determining if a ConstantExpr is a CastOp or not. This
874// can't be inline because we don't want to #include Instruction.h into
875// Constant.h
876bool ConstantExpr::isCast() const {
877 return Instruction::isCast(getOpcode());
878}
879
Reid Spenceree3c9912006-12-04 05:19:50 +0000880bool ConstantExpr::isCompare() const {
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000881 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
Reid Spenceree3c9912006-12-04 05:19:50 +0000882}
883
Dan Gohman1ecaf452008-05-31 00:58:22 +0000884bool ConstantExpr::hasIndices() const {
885 return getOpcode() == Instruction::ExtractValue ||
886 getOpcode() == Instruction::InsertValue;
887}
888
889const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const {
890 if (const ExtractValueConstantExpr *EVCE =
891 dyn_cast<ExtractValueConstantExpr>(this))
892 return EVCE->Indices;
Dan Gohmana469bdb2008-06-23 16:39:44 +0000893
894 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman1ecaf452008-05-31 00:58:22 +0000895}
896
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000897unsigned ConstantExpr::getPredicate() const {
Nate Begemand2195702008-05-12 19:01:56 +0000898 assert(getOpcode() == Instruction::FCmp ||
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000899 getOpcode() == Instruction::ICmp);
Chris Lattneref650092007-10-18 16:26:24 +0000900 return ((const CompareConstantExpr*)this)->predicate;
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000901}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000902
Chris Lattner7c1018a2006-07-14 19:37:40 +0000903/// getWithOperandReplaced - Return a constant expression identical to this
904/// one, but with the specified operand set to the specified value.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000905Constant *
906ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +0000907 assert(OpNo < getNumOperands() && "Operand num is out of range!");
908 assert(Op->getType() == getOperand(OpNo)->getType() &&
909 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000910 if (getOperand(OpNo) == Op)
911 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000912
Chris Lattner227816342006-07-14 22:20:01 +0000913 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000914 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000915 case Instruction::Trunc:
916 case Instruction::ZExt:
917 case Instruction::SExt:
918 case Instruction::FPTrunc:
919 case Instruction::FPExt:
920 case Instruction::UIToFP:
921 case Instruction::SIToFP:
922 case Instruction::FPToUI:
923 case Instruction::FPToSI:
924 case Instruction::PtrToInt:
925 case Instruction::IntToPtr:
926 case Instruction::BitCast:
927 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000928 case Instruction::Select:
929 Op0 = (OpNo == 0) ? Op : getOperand(0);
930 Op1 = (OpNo == 1) ? Op : getOperand(1);
931 Op2 = (OpNo == 2) ? Op : getOperand(2);
932 return ConstantExpr::getSelect(Op0, Op1, Op2);
933 case Instruction::InsertElement:
934 Op0 = (OpNo == 0) ? Op : getOperand(0);
935 Op1 = (OpNo == 1) ? Op : getOperand(1);
936 Op2 = (OpNo == 2) ? Op : getOperand(2);
937 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
938 case Instruction::ExtractElement:
939 Op0 = (OpNo == 0) ? Op : getOperand(0);
940 Op1 = (OpNo == 1) ? Op : getOperand(1);
941 return ConstantExpr::getExtractElement(Op0, Op1);
942 case Instruction::ShuffleVector:
943 Op0 = (OpNo == 0) ? Op : getOperand(0);
944 Op1 = (OpNo == 1) ? Op : getOperand(1);
945 Op2 = (OpNo == 2) ? Op : getOperand(2);
946 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000947 case Instruction::GetElementPtr: {
Chris Lattnerb5d70302007-02-19 20:01:23 +0000948 SmallVector<Constant*, 8> Ops;
Dan Gohman12fce772008-05-15 19:50:34 +0000949 Ops.resize(getNumOperands()-1);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000950 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Dan Gohman12fce772008-05-15 19:50:34 +0000951 Ops[i-1] = getOperand(i);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000952 if (OpNo == 0)
Chris Lattnerb5d70302007-02-19 20:01:23 +0000953 return ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000954 Ops[OpNo-1] = Op;
Chris Lattnerb5d70302007-02-19 20:01:23 +0000955 return ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000956 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000957 default:
958 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000959 Op0 = (OpNo == 0) ? Op : getOperand(0);
960 Op1 = (OpNo == 1) ? Op : getOperand(1);
961 return ConstantExpr::get(getOpcode(), Op0, Op1);
962 }
963}
964
965/// getWithOperands - This returns the current constant expression with the
966/// operands replaced with the specified values. The specified operands must
967/// match count and type with the existing ones.
968Constant *ConstantExpr::
Chris Lattnerb078e282008-08-20 22:27:40 +0000969getWithOperands(Constant* const *Ops, unsigned NumOps) const {
970 assert(NumOps == getNumOperands() && "Operand count mismatch!");
Chris Lattner227816342006-07-14 22:20:01 +0000971 bool AnyChange = false;
Chris Lattnerb078e282008-08-20 22:27:40 +0000972 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattner227816342006-07-14 22:20:01 +0000973 assert(Ops[i]->getType() == getOperand(i)->getType() &&
974 "Operand type mismatch!");
975 AnyChange |= Ops[i] != getOperand(i);
976 }
977 if (!AnyChange) // No operands changed, return self.
978 return const_cast<ConstantExpr*>(this);
979
980 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000981 case Instruction::Trunc:
982 case Instruction::ZExt:
983 case Instruction::SExt:
984 case Instruction::FPTrunc:
985 case Instruction::FPExt:
986 case Instruction::UIToFP:
987 case Instruction::SIToFP:
988 case Instruction::FPToUI:
989 case Instruction::FPToSI:
990 case Instruction::PtrToInt:
991 case Instruction::IntToPtr:
992 case Instruction::BitCast:
993 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattner227816342006-07-14 22:20:01 +0000994 case Instruction::Select:
995 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
996 case Instruction::InsertElement:
997 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
998 case Instruction::ExtractElement:
999 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
1000 case Instruction::ShuffleVector:
1001 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Chris Lattnerb5d70302007-02-19 20:01:23 +00001002 case Instruction::GetElementPtr:
Chris Lattnerb078e282008-08-20 22:27:40 +00001003 return ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], NumOps-1);
Reid Spencer266e42b2006-12-23 06:05:41 +00001004 case Instruction::ICmp:
1005 case Instruction::FCmp:
1006 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattner227816342006-07-14 22:20:01 +00001007 default:
1008 assert(getNumOperands() == 2 && "Must be binary operator?");
1009 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +00001010 }
1011}
1012
Chris Lattner2f7c9632001-06-06 20:29:01 +00001013
1014//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00001015// isValueValidForType implementations
1016
Reid Spencere7334722006-12-19 01:28:19 +00001017bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001018 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001019 if (Ty == Type::Int1Ty)
1020 return Val == 0 || Val == 1;
Reid Spencerd7a00d72007-02-05 23:47:56 +00001021 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001022 return true; // always true, has to fit in largest type
1023 uint64_t Max = (1ll << NumBits) - 1;
1024 return Val <= Max;
Reid Spencere7334722006-12-19 01:28:19 +00001025}
1026
Reid Spencere0fc4df2006-10-20 07:07:24 +00001027bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001028 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001029 if (Ty == Type::Int1Ty)
Reid Spencera94d3942007-01-19 21:13:56 +00001030 return Val == 0 || Val == 1 || Val == -1;
Reid Spencerd7a00d72007-02-05 23:47:56 +00001031 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001032 return true; // always true, has to fit in largest type
1033 int64_t Min = -(1ll << (NumBits-1));
1034 int64_t Max = (1ll << (NumBits-1)) - 1;
1035 return (Val >= Min && Val <= Max);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001036}
1037
Dale Johannesend246b2c2007-08-30 00:23:21 +00001038bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) {
1039 // convert modifies in place, so make a copy.
1040 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001041 bool losesInfo;
Chris Lattner6b727592004-06-17 18:19:28 +00001042 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001043 default:
1044 return false; // These can't be represented as floating point!
1045
Dale Johannesend246b2c2007-08-30 00:23:21 +00001046 // FIXME rounding mode needs to be more flexible
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001047 case Type::FloatTyID: {
1048 if (&Val2.getSemantics() == &APFloat::IEEEsingle)
1049 return true;
1050 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
1051 return !losesInfo;
1052 }
1053 case Type::DoubleTyID: {
1054 if (&Val2.getSemantics() == &APFloat::IEEEsingle ||
1055 &Val2.getSemantics() == &APFloat::IEEEdouble)
1056 return true;
1057 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
1058 return !losesInfo;
1059 }
Dale Johannesenbdad8092007-08-09 22:51:36 +00001060 case Type::X86_FP80TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +00001061 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
1062 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1063 &Val2.getSemantics() == &APFloat::x87DoubleExtended;
Dale Johannesenbdad8092007-08-09 22:51:36 +00001064 case Type::FP128TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +00001065 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
1066 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1067 &Val2.getSemantics() == &APFloat::IEEEquad;
Dale Johannesen007aa372007-10-11 18:07:22 +00001068 case Type::PPC_FP128TyID:
1069 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
1070 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1071 &Val2.getSemantics() == &APFloat::PPCDoubleDouble;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001072 }
Chris Lattneraa2372562006-05-24 17:04:05 +00001073}
Chris Lattner9655e542001-07-20 19:16:02 +00001074
Chris Lattner49d855c2001-09-07 16:46:31 +00001075//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +00001076// Factory Function Implementation
1077
Owen Andersonb292b8c2009-07-30 23:03:37 +00001078static char getValType(ConstantAggregateZero *CPZ) { return 0; }
1079
1080ConstantAggregateZero* ConstantAggregateZero::get(const Type* Ty) {
1081 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) &&
1082 "Cannot create an aggregate zero of non-aggregate type!");
1083
1084 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
1085 // Implicitly locked.
1086 return pImpl->AggZeroConstants.getOrCreate(Ty, 0);
1087}
1088
Dan Gohman92b551b2009-03-03 02:55:14 +00001089/// destroyConstant - Remove the constant from the constant table...
1090///
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001091void ConstantAggregateZero::destroyConstant() {
Owen Anderson61794042009-06-17 20:10:08 +00001092 // Implicitly locked.
Owen Andersonb292b8c2009-07-30 23:03:37 +00001093 getType()->getContext().pImpl->AggZeroConstants.remove(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001094 destroyConstantImpl();
1095}
1096
Dan Gohman92b551b2009-03-03 02:55:14 +00001097/// destroyConstant - Remove the constant from the constant table...
1098///
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001099void ConstantArray::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001100 // Implicitly locked.
Owen Andersonc2c79322009-07-28 18:32:17 +00001101 getType()->getContext().pImpl->ArrayConstants.remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001102 destroyConstantImpl();
1103}
1104
Reid Spencer2546b762007-01-26 07:37:34 +00001105/// isString - This method returns true if the array is an array of i8, and
1106/// if the elements of the array are all ConstantInt's.
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001107bool ConstantArray::isString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001108 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001109 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001110 return false;
1111 // Check the elements to make sure they are all integers, not constant
1112 // expressions.
1113 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1114 if (!isa<ConstantInt>(getOperand(i)))
1115 return false;
1116 return true;
1117}
1118
Evan Cheng3763c5b2006-10-26 19:15:05 +00001119/// isCString - This method returns true if the array is a string (see
Dan Gohman92b551b2009-03-03 02:55:14 +00001120/// isString) and it ends in a null byte \\0 and does not contains any other
Evan Cheng3763c5b2006-10-26 19:15:05 +00001121/// null bytes except its terminator.
Owen Andersone4dcecd2009-07-13 21:27:19 +00001122bool ConstantArray::isCString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001123 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001124 if (getType()->getElementType() != Type::Int8Ty)
Evan Chenge974da62006-10-26 21:48:03 +00001125 return false;
Owen Andersone4dcecd2009-07-13 21:27:19 +00001126
Evan Chenge974da62006-10-26 21:48:03 +00001127 // Last element must be a null.
Owen Andersone4dcecd2009-07-13 21:27:19 +00001128 if (!getOperand(getNumOperands()-1)->isNullValue())
Evan Chenge974da62006-10-26 21:48:03 +00001129 return false;
1130 // Other elements must be non-null integers.
1131 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1132 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +00001133 return false;
Owen Andersone4dcecd2009-07-13 21:27:19 +00001134 if (getOperand(i)->isNullValue())
Evan Chenge974da62006-10-26 21:48:03 +00001135 return false;
1136 }
Evan Cheng3763c5b2006-10-26 19:15:05 +00001137 return true;
1138}
1139
1140
Dan Gohman92b551b2009-03-03 02:55:14 +00001141/// getAsString - If the sub-element type of this array is i8
1142/// then this method converts the array to an std::string and returns it.
1143/// Otherwise, it asserts out.
1144///
Chris Lattner81fabb02002-08-26 17:53:56 +00001145std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001146 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001147 std::string Result;
Owen Anderson79c69bc2008-06-24 21:58:29 +00001148 Result.reserve(getNumOperands());
Chris Lattner6077c312003-07-23 15:22:26 +00001149 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonee9c30d2008-06-25 01:05:05 +00001150 Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue());
Chris Lattner81fabb02002-08-26 17:53:56 +00001151 return Result;
1152}
1153
1154
Chris Lattner3462ae32001-12-03 22:26:30 +00001155//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001156//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001157
Chris Lattner189d19f2003-11-21 20:23:48 +00001158namespace llvm {
Misha Brukmanb1c93172005-04-21 23:48:37 +00001159
Chris Lattner49d855c2001-09-07 16:46:31 +00001160}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001161
Chris Lattnerd7a73302001-10-13 06:57:33 +00001162// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001163//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001164void ConstantStruct::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001165 // Implicitly locked.
Owen Anderson45308b52009-07-27 22:29:26 +00001166 getType()->getContext().pImpl->StructConstants.remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001167 destroyConstantImpl();
1168}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001169
Brian Gaeke02209042004-08-20 06:00:58 +00001170// destroyConstant - Remove the constant from the constant table...
1171//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001172void ConstantVector::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001173 // Implicitly locked.
Owen Anderson4aa32952009-07-28 21:19:26 +00001174 getType()->getContext().pImpl->VectorConstants.remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001175 destroyConstantImpl();
1176}
1177
Dan Gohman30978072007-05-24 14:36:04 +00001178/// This function will return true iff every element in this vector constant
Jim Laskeyf0478822007-01-12 22:39:14 +00001179/// is set to all ones.
1180/// @returns true iff this constant's emements are all set to all ones.
1181/// @brief Determine if the value is all ones.
Reid Spencerd84d35b2007-02-15 02:26:10 +00001182bool ConstantVector::isAllOnesValue() const {
Jim Laskeyf0478822007-01-12 22:39:14 +00001183 // Check out first element.
1184 const Constant *Elt = getOperand(0);
1185 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1186 if (!CI || !CI->isAllOnesValue()) return false;
1187 // Then make sure all remaining elements point to the same value.
1188 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1189 if (getOperand(I) != Elt) return false;
1190 }
1191 return true;
1192}
1193
Dan Gohman07159202007-10-17 17:51:30 +00001194/// getSplatValue - If this is a splat constant, where all of the
1195/// elements have the same value, return that value. Otherwise return null.
1196Constant *ConstantVector::getSplatValue() {
1197 // Check out first element.
1198 Constant *Elt = getOperand(0);
1199 // Then make sure all remaining elements point to the same value.
1200 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
1201 if (getOperand(I) != Elt) return 0;
1202 return Elt;
1203}
1204
Chris Lattner3462ae32001-12-03 22:26:30 +00001205//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001206//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001207
Chris Lattner3e650af2004-08-04 04:48:01 +00001208static char getValType(ConstantPointerNull *) {
1209 return 0;
1210}
1211
1212
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001213ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Owen Anderson61794042009-06-17 20:10:08 +00001214 // Implicitly locked.
Owen Andersonc8c30262009-07-31 22:45:43 +00001215 return Ty->getContext().pImpl->NullPtrConstants.getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001216}
1217
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001218// destroyConstant - Remove the constant from the constant table...
1219//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001220void ConstantPointerNull::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001221 // Implicitly locked.
Owen Andersonc8c30262009-07-31 22:45:43 +00001222 getType()->getContext().pImpl->NullPtrConstants.remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001223 destroyConstantImpl();
1224}
1225
1226
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001227//---- UndefValue::get() implementation...
1228//
1229
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001230static char getValType(UndefValue *) {
1231 return 0;
1232}
1233
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001234UndefValue *UndefValue::get(const Type *Ty) {
1235 // Implicitly locked.
Owen Andersonc8c30262009-07-31 22:45:43 +00001236 return Ty->getContext().pImpl->UndefValueConstants.getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001237}
1238
1239// destroyConstant - Remove the constant from the constant table.
1240//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001241void UndefValue::destroyConstant() {
Owen Anderson61794042009-06-17 20:10:08 +00001242 // Implicitly locked.
Owen Andersonc8c30262009-07-31 22:45:43 +00001243 getType()->getContext().pImpl->UndefValueConstants.remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001244 destroyConstantImpl();
1245}
1246
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001247//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001248//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001249
Dan Gohmand78c4002008-05-13 00:00:25 +00001250namespace {
1251
Reid Spenceree3c9912006-12-04 05:19:50 +00001252struct ExprMapKeyType {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001253 typedef SmallVector<unsigned, 4> IndexList;
1254
1255 ExprMapKeyType(unsigned opc,
1256 const std::vector<Constant*> &ops,
1257 unsigned short pred = 0,
1258 const IndexList &inds = IndexList())
1259 : opcode(opc), predicate(pred), operands(ops), indices(inds) {}
Reid Spencerdba6aa42006-12-04 18:38:05 +00001260 uint16_t opcode;
1261 uint16_t predicate;
Reid Spenceree3c9912006-12-04 05:19:50 +00001262 std::vector<Constant*> operands;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001263 IndexList indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001264 bool operator==(const ExprMapKeyType& that) const {
1265 return this->opcode == that.opcode &&
1266 this->predicate == that.predicate &&
Bill Wendling97f7de82008-10-26 00:19:56 +00001267 this->operands == that.operands &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001268 this->indices == that.indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001269 }
1270 bool operator<(const ExprMapKeyType & that) const {
1271 return this->opcode < that.opcode ||
1272 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1273 (this->opcode == that.opcode && this->predicate == that.predicate &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001274 this->operands < that.operands) ||
1275 (this->opcode == that.opcode && this->predicate == that.predicate &&
1276 this->operands == that.operands && this->indices < that.indices);
Reid Spenceree3c9912006-12-04 05:19:50 +00001277 }
1278
1279 bool operator!=(const ExprMapKeyType& that) const {
1280 return !(*this == that);
1281 }
1282};
Chris Lattner98fa07b2003-05-23 20:03:32 +00001283
Dan Gohmand78c4002008-05-13 00:00:25 +00001284}
1285
Chris Lattner189d19f2003-11-21 20:23:48 +00001286namespace llvm {
1287 template<>
1288 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001289 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1290 unsigned short pred = 0) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001291 if (Instruction::isCast(V.opcode))
1292 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1293 if ((V.opcode >= Instruction::BinaryOpsBegin &&
Reid Spencer2341c222007-02-02 02:16:23 +00001294 V.opcode < Instruction::BinaryOpsEnd))
Reid Spenceree3c9912006-12-04 05:19:50 +00001295 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1296 if (V.opcode == Instruction::Select)
1297 return new SelectConstantExpr(V.operands[0], V.operands[1],
1298 V.operands[2]);
1299 if (V.opcode == Instruction::ExtractElement)
1300 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1301 if (V.opcode == Instruction::InsertElement)
1302 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1303 V.operands[2]);
1304 if (V.opcode == Instruction::ShuffleVector)
1305 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1306 V.operands[2]);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001307 if (V.opcode == Instruction::InsertValue)
1308 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
1309 V.indices, Ty);
1310 if (V.opcode == Instruction::ExtractValue)
1311 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001312 if (V.opcode == Instruction::GetElementPtr) {
1313 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
Gabor Greife9ecc682008-04-06 20:25:17 +00001314 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001315 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001316
Reid Spenceree3c9912006-12-04 05:19:50 +00001317 // The compare instructions are weird. We have to encode the predicate
1318 // value and it is combined with the instruction opcode by multiplying
1319 // the opcode by one hundred. We must decode this to get the predicate.
1320 if (V.opcode == Instruction::ICmp)
Nate Begemand2195702008-05-12 19:01:56 +00001321 return new CompareConstantExpr(Ty, Instruction::ICmp, V.predicate,
Reid Spenceree3c9912006-12-04 05:19:50 +00001322 V.operands[0], V.operands[1]);
1323 if (V.opcode == Instruction::FCmp)
Nate Begemand2195702008-05-12 19:01:56 +00001324 return new CompareConstantExpr(Ty, Instruction::FCmp, V.predicate,
1325 V.operands[0], V.operands[1]);
Torok Edwinfbcc6632009-07-14 16:55:14 +00001326 llvm_unreachable("Invalid ConstantExpr!");
Jeff Cohen9f469632006-12-15 21:47:01 +00001327 return 0;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001328 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001329 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001330
Chris Lattner189d19f2003-11-21 20:23:48 +00001331 template<>
1332 struct ConvertConstantType<ConstantExpr, Type> {
1333 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1334 Constant *New;
1335 switch (OldC->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001336 case Instruction::Trunc:
1337 case Instruction::ZExt:
1338 case Instruction::SExt:
1339 case Instruction::FPTrunc:
1340 case Instruction::FPExt:
1341 case Instruction::UIToFP:
1342 case Instruction::SIToFP:
1343 case Instruction::FPToUI:
1344 case Instruction::FPToSI:
1345 case Instruction::PtrToInt:
1346 case Instruction::IntToPtr:
1347 case Instruction::BitCast:
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001348 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001349 NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001350 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001351 case Instruction::Select:
1352 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1353 OldC->getOperand(1),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001354 OldC->getOperand(2));
Chris Lattner6e415c02004-03-12 05:54:04 +00001355 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001356 default:
1357 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001358 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner189d19f2003-11-21 20:23:48 +00001359 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001360 OldC->getOperand(1));
Chris Lattner189d19f2003-11-21 20:23:48 +00001361 break;
1362 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001363 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001364 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
Chris Lattner302116a2007-01-31 04:40:28 +00001365 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001366 &Idx[0], Idx.size());
Chris Lattner189d19f2003-11-21 20:23:48 +00001367 break;
1368 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001369
Chris Lattner189d19f2003-11-21 20:23:48 +00001370 assert(New != OldC && "Didn't replace constant??");
1371 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001372 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001373 }
1374 };
1375} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001376
1377
Chris Lattner3e650af2004-08-04 04:48:01 +00001378static ExprMapKeyType getValType(ConstantExpr *CE) {
1379 std::vector<Constant*> Operands;
1380 Operands.reserve(CE->getNumOperands());
1381 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1382 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spenceree3c9912006-12-04 05:19:50 +00001383 return ExprMapKeyType(CE->getOpcode(), Operands,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001384 CE->isCompare() ? CE->getPredicate() : 0,
1385 CE->hasIndices() ?
1386 CE->getIndices() : SmallVector<unsigned, 4>());
Chris Lattner3e650af2004-08-04 04:48:01 +00001387}
1388
Chris Lattner69edc982006-09-28 00:35:06 +00001389static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1390 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001391
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001392/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands7d6c8ae2008-03-30 19:38:55 +00001393/// cast in the ExprConstants map. It is used by the various get* methods below.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001394static inline Constant *getFoldedCast(
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001395 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001396 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001397 // Fold a few common cases
Owen Anderson23a204d2009-07-31 17:39:07 +00001398 if (Constant *FC = ConstantFoldCastInstruction(Ty->getContext(), opc, C, Ty))
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001399 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001400
Vikram S. Adve4c485332002-07-15 18:19:33 +00001401 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001402 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00001403 ExprMapKeyType Key(opc, argVec);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001404
Owen Anderson61794042009-06-17 20:10:08 +00001405 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001406 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001407}
Reid Spencerf37dc652006-12-05 19:14:13 +00001408
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001409Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001410 Instruction::CastOps opc = Instruction::CastOps(oc);
1411 assert(Instruction::isCast(opc) && "opcode out of range");
1412 assert(C && Ty && "Null arguments to getCast");
1413 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1414
1415 switch (opc) {
1416 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00001417 llvm_unreachable("Invalid cast opcode");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001418 break;
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001419 case Instruction::Trunc: return getTrunc(C, Ty);
1420 case Instruction::ZExt: return getZExt(C, Ty);
1421 case Instruction::SExt: return getSExt(C, Ty);
1422 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1423 case Instruction::FPExt: return getFPExtend(C, Ty);
1424 case Instruction::UIToFP: return getUIToFP(C, Ty);
1425 case Instruction::SIToFP: return getSIToFP(C, Ty);
1426 case Instruction::FPToUI: return getFPToUI(C, Ty);
1427 case Instruction::FPToSI: return getFPToSI(C, Ty);
1428 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1429 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1430 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001431 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001432 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00001433}
1434
Reid Spencer5c140882006-12-04 20:17:56 +00001435Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001436 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00001437 return getCast(Instruction::BitCast, C, Ty);
1438 return getCast(Instruction::ZExt, C, Ty);
1439}
1440
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001441Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001442 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001443 return getCast(Instruction::BitCast, C, Ty);
1444 return getCast(Instruction::SExt, C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001445}
1446
1447Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001448 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00001449 return getCast(Instruction::BitCast, C, Ty);
1450 return getCast(Instruction::Trunc, C, Ty);
1451}
1452
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001453Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
Reid Spencerbc245a02006-12-05 03:25:26 +00001454 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001455 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001456
Chris Lattner03c49532007-01-15 02:27:26 +00001457 if (Ty->isInteger())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001458 return getCast(Instruction::PtrToInt, S, Ty);
1459 return getCast(Instruction::BitCast, S, Ty);
Reid Spencerbc245a02006-12-05 03:25:26 +00001460}
1461
Reid Spencer56521c42006-12-12 00:51:07 +00001462Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1463 bool isSigned) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001464 assert(C->getType()->isIntOrIntVector() &&
1465 Ty->isIntOrIntVector() && "Invalid cast");
1466 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1467 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer56521c42006-12-12 00:51:07 +00001468 Instruction::CastOps opcode =
1469 (SrcBits == DstBits ? Instruction::BitCast :
1470 (SrcBits > DstBits ? Instruction::Trunc :
1471 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1472 return getCast(opcode, C, Ty);
1473}
1474
1475Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001476 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer56521c42006-12-12 00:51:07 +00001477 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001478 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1479 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001480 if (SrcBits == DstBits)
1481 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001482 Instruction::CastOps opcode =
Reid Spencerca104e82006-12-12 05:38:50 +00001483 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001484 return getCast(opcode, C, Ty);
1485}
1486
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001487Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001488#ifndef NDEBUG
1489 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1490 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1491#endif
1492 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1493 assert(C->getType()->isIntOrIntVector() && "Trunc operand must be integer");
1494 assert(Ty->isIntOrIntVector() && "Trunc produces only integral");
1495 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001496 "SrcTy must be larger than DestTy for Trunc!");
1497
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001498 return getFoldedCast(Instruction::Trunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001499}
1500
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001501Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001502#ifndef NDEBUG
1503 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1504 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1505#endif
1506 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1507 assert(C->getType()->isIntOrIntVector() && "SExt operand must be integral");
1508 assert(Ty->isIntOrIntVector() && "SExt produces only integer");
1509 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001510 "SrcTy must be smaller than DestTy for SExt!");
1511
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001512 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerdd284742004-04-04 23:20:30 +00001513}
1514
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001515Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001516#ifndef NDEBUG
1517 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1518 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1519#endif
1520 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1521 assert(C->getType()->isIntOrIntVector() && "ZEXt operand must be integral");
1522 assert(Ty->isIntOrIntVector() && "ZExt produces only integer");
1523 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001524 "SrcTy must be smaller than DestTy for ZExt!");
1525
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001526 return getFoldedCast(Instruction::ZExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001527}
1528
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001529Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001530#ifndef NDEBUG
1531 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1532 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1533#endif
1534 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1535 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1536 C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001537 "This is an illegal floating point truncation!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001538 return getFoldedCast(Instruction::FPTrunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001539}
1540
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001541Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001542#ifndef NDEBUG
1543 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1544 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1545#endif
1546 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1547 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1548 C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001549 "This is an illegal floating point extension!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001550 return getFoldedCast(Instruction::FPExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001551}
1552
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001553Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001554#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001555 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1556 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001557#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001558 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1559 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
1560 "This is an illegal uint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001561 return getFoldedCast(Instruction::UIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001562}
1563
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001564Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001565#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001566 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1567 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001568#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001569 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1570 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001571 "This is an illegal sint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001572 return getFoldedCast(Instruction::SIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001573}
1574
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001575Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001576#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001577 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1578 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001579#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001580 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1581 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1582 "This is an illegal floating point to uint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001583 return getFoldedCast(Instruction::FPToUI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001584}
1585
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001586Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001587#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001588 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1589 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001590#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001591 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1592 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1593 "This is an illegal floating point to sint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001594 return getFoldedCast(Instruction::FPToSI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001595}
1596
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001597Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001598 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner03c49532007-01-15 02:27:26 +00001599 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001600 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001601}
1602
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001603Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner03c49532007-01-15 02:27:26 +00001604 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001605 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001606 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001607}
1608
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001609Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001610 // BitCast implies a no-op cast of type only. No bits change. However, you
1611 // can't cast pointers to anything but pointers.
Devang Pateld26344d2008-11-03 23:20:04 +00001612#ifndef NDEBUG
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001613 const Type *SrcTy = C->getType();
1614 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer5c140882006-12-04 20:17:56 +00001615 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001616
1617 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
1618 // or nonptr->ptr). For all the other types, the cast is okay if source and
1619 // destination bit widths are identical.
1620 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1621 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Devang Pateld26344d2008-11-03 23:20:04 +00001622#endif
Chris Lattnere4086012009-03-08 04:06:26 +00001623 assert(SrcBitSize == DstBitSize && "BitCast requires types of same width");
Chris Lattnercbeda872009-03-21 06:55:54 +00001624
1625 // It is common to ask for a bitcast of a value to its own type, handle this
1626 // speedily.
1627 if (C->getType() == DstTy) return C;
1628
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001629 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerdd284742004-04-04 23:20:30 +00001630}
1631
Chris Lattnerb50d1352003-10-05 00:17:43 +00001632Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001633 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001634 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00001635 assert(Opcode >= Instruction::BinaryOpsBegin &&
1636 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001637 "Invalid opcode in binary constant expression");
1638 assert(C1->getType() == C2->getType() &&
1639 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001640
Reid Spencer542964f2007-01-11 18:21:29 +00001641 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Owen Anderson23a204d2009-07-31 17:39:07 +00001642 if (Constant *FC = ConstantFoldBinaryInstruction(ReqTy->getContext(),
1643 Opcode, C1, C2))
Chris Lattnerb50d1352003-10-05 00:17:43 +00001644 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001645
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001646 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001647 ExprMapKeyType Key(Opcode, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00001648
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001649 // Implicitly locked.
1650 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001651}
1652
Reid Spencer266e42b2006-12-23 06:05:41 +00001653Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Nate Begeman098cc6f2008-07-25 17:56:27 +00001654 Constant *C1, Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001655 switch (predicate) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001656 default: llvm_unreachable("Invalid CmpInst predicate");
Nate Begemanc96e2e42008-07-25 17:35:37 +00001657 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
1658 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
1659 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
1660 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
1661 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
1662 case CmpInst::FCMP_TRUE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001663 return getFCmp(predicate, C1, C2);
1664
Nate Begemanc96e2e42008-07-25 17:35:37 +00001665 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
1666 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
1667 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
1668 case CmpInst::ICMP_SLE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001669 return getICmp(predicate, C1, C2);
Reid Spencer266e42b2006-12-23 06:05:41 +00001670 }
Reid Spencera009d0d2006-12-04 21:35:24 +00001671}
1672
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001673Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001674 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1675 if (C1->getType()->isFPOrFPVector()) {
1676 if (Opcode == Instruction::Add) Opcode = Instruction::FAdd;
1677 else if (Opcode == Instruction::Sub) Opcode = Instruction::FSub;
1678 else if (Opcode == Instruction::Mul) Opcode = Instruction::FMul;
1679 }
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001680#ifndef NDEBUG
1681 switch (Opcode) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001682 case Instruction::Add:
Reid Spencer7eb55b32006-11-02 01:53:59 +00001683 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001684 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001685 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmana5b96452009-06-04 22:49:04 +00001686 assert(C1->getType()->isIntOrIntVector() &&
1687 "Tried to create an integer operation on a non-integer type!");
1688 break;
1689 case Instruction::FAdd:
1690 case Instruction::FSub:
1691 case Instruction::FMul:
1692 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1693 assert(C1->getType()->isFPOrFPVector() &&
1694 "Tried to create a floating-point operation on a "
1695 "non-floating-point type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001696 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001697 case Instruction::UDiv:
1698 case Instruction::SDiv:
1699 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001700 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001701 "Tried to create an arithmetic operation on a non-arithmetic type!");
1702 break;
1703 case Instruction::FDiv:
1704 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001705 assert(C1->getType()->isFPOrFPVector() &&
1706 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001707 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001708 case Instruction::URem:
1709 case Instruction::SRem:
1710 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001711 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001712 "Tried to create an arithmetic operation on a non-arithmetic type!");
1713 break;
1714 case Instruction::FRem:
1715 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001716 assert(C1->getType()->isFPOrFPVector() &&
1717 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001718 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001719 case Instruction::And:
1720 case Instruction::Or:
1721 case Instruction::Xor:
1722 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001723 assert(C1->getType()->isIntOrIntVector() &&
Misha Brukman3852f652005-01-27 06:46:38 +00001724 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001725 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001726 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001727 case Instruction::LShr:
1728 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00001729 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman79975d52009-03-14 17:09:17 +00001730 assert(C1->getType()->isIntOrIntVector() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001731 "Tried to create a shift operation on a non-integer type!");
1732 break;
1733 default:
1734 break;
1735 }
1736#endif
1737
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001738 return getTy(C1->getType(), Opcode, C1, C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001739}
1740
Owen Anderson487375e2009-07-29 18:55:55 +00001741Constant* ConstantExpr::getSizeOf(const Type* Ty) {
1742 // sizeof is implemented as: (i64) gep (Ty*)null, 1
1743 // Note that a non-inbounds gep is used, as null isn't within any object.
Owen Anderson487375e2009-07-29 18:55:55 +00001744 Constant *GEPIdx = ConstantInt::get(Type::Int32Ty, 1);
1745 Constant *GEP = getGetElementPtr(
Owen Anderson5a1acd92009-07-31 20:28:14 +00001746 Constant::getNullValue(PointerType::getUnqual(Ty)), &GEPIdx, 1);
Owen Anderson487375e2009-07-29 18:55:55 +00001747 return getCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
1748}
1749
1750Constant* ConstantExpr::getAlignOf(const Type* Ty) {
Owen Anderson487375e2009-07-29 18:55:55 +00001751 // alignof is implemented as: (i64) gep ({i8,Ty}*)null, 0, 1
1752 const Type *AligningTy = StructType::get(Type::Int8Ty, Ty, NULL);
Owen Anderson5a1acd92009-07-31 20:28:14 +00001753 Constant *NullPtr = Constant::getNullValue(AligningTy->getPointerTo());
Owen Anderson487375e2009-07-29 18:55:55 +00001754 Constant *Zero = ConstantInt::get(Type::Int32Ty, 0);
1755 Constant *One = ConstantInt::get(Type::Int32Ty, 1);
1756 Constant *Indices[2] = { Zero, One };
1757 Constant *GEP = getGetElementPtr(NullPtr, Indices, 2);
1758 return getCast(Instruction::PtrToInt, GEP, Type::Int32Ty);
1759}
1760
1761
Reid Spencer266e42b2006-12-23 06:05:41 +00001762Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00001763 Constant *C1, Constant *C2) {
1764 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001765 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00001766}
1767
Chris Lattner6e415c02004-03-12 05:54:04 +00001768Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001769 Constant *V1, Constant *V2) {
Chris Lattner41632132008-12-29 00:16:12 +00001770 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner6e415c02004-03-12 05:54:04 +00001771
1772 if (ReqTy == V1->getType())
Owen Anderson53a52212009-07-13 04:09:18 +00001773 if (Constant *SC = ConstantFoldSelectInstruction(
Owen Anderson23a204d2009-07-31 17:39:07 +00001774 ReqTy->getContext(), C, V1, V2))
Chris Lattner6e415c02004-03-12 05:54:04 +00001775 return SC; // Fold common cases
1776
1777 std::vector<Constant*> argVec(3, C);
1778 argVec[1] = V1;
1779 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00001780 ExprMapKeyType Key(Instruction::Select, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00001781
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001782 // Implicitly locked.
1783 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00001784}
1785
Chris Lattnerb50d1352003-10-05 00:17:43 +00001786Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00001787 Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001788 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001789 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
1790 Idxs+NumIdx) ==
1791 cast<PointerType>(ReqTy)->getElementType() &&
1792 "GEP indices invalid!");
Chris Lattner04b60fe2004-02-16 20:46:13 +00001793
Owen Anderson53a52212009-07-13 04:09:18 +00001794 if (Constant *FC = ConstantFoldGetElementPtr(
Owen Anderson23a204d2009-07-31 17:39:07 +00001795 ReqTy->getContext(), C, (Constant**)Idxs, NumIdx))
Chris Lattneracdbe712003-04-17 19:24:48 +00001796 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001797
Chris Lattnerb50d1352003-10-05 00:17:43 +00001798 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001799 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001800 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00001801 std::vector<Constant*> ArgVec;
Chris Lattner302116a2007-01-31 04:40:28 +00001802 ArgVec.reserve(NumIdx+1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001803 ArgVec.push_back(C);
Chris Lattner302116a2007-01-31 04:40:28 +00001804 for (unsigned i = 0; i != NumIdx; ++i)
1805 ArgVec.push_back(cast<Constant>(Idxs[i]));
1806 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001807
1808 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001809 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001810}
1811
Chris Lattner302116a2007-01-31 04:40:28 +00001812Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001813 unsigned NumIdx) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00001814 // Get the result type of the getelementptr!
Chris Lattner302116a2007-01-31 04:40:28 +00001815 const Type *Ty =
Dan Gohman12fce772008-05-15 19:50:34 +00001816 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001817 assert(Ty && "GEP indices invalid!");
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001818 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001819 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
Chris Lattner13128ab2004-10-11 22:52:25 +00001820}
1821
Chris Lattner302116a2007-01-31 04:40:28 +00001822Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001823 unsigned NumIdx) {
1824 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001825}
1826
Chris Lattner302116a2007-01-31 04:40:28 +00001827
Reid Spenceree3c9912006-12-04 05:19:50 +00001828Constant *
1829ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1830 assert(LHS->getType() == RHS->getType());
1831 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
1832 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
1833
Owen Anderson53a52212009-07-13 04:09:18 +00001834 if (Constant *FC = ConstantFoldCompareInstruction(
Owen Anderson23a204d2009-07-31 17:39:07 +00001835 LHS->getContext(), pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001836 return FC; // Fold a few common cases...
1837
1838 // Look up the constant in the table first to ensure uniqueness
1839 std::vector<Constant*> ArgVec;
1840 ArgVec.push_back(LHS);
1841 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001842 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001843 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00001844
1845 // Implicitly locked.
1846 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001847}
1848
1849Constant *
1850ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1851 assert(LHS->getType() == RHS->getType());
1852 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
1853
Owen Anderson53a52212009-07-13 04:09:18 +00001854 if (Constant *FC = ConstantFoldCompareInstruction(
Owen Anderson23a204d2009-07-31 17:39:07 +00001855 LHS->getContext(), pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001856 return FC; // Fold a few common cases...
1857
1858 // Look up the constant in the table first to ensure uniqueness
1859 std::vector<Constant*> ArgVec;
1860 ArgVec.push_back(LHS);
1861 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001862 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001863 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00001864
1865 // Implicitly locked.
1866 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001867}
1868
Robert Bocchino23004482006-01-10 19:05:34 +00001869Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
1870 Constant *Idx) {
Owen Anderson53a52212009-07-13 04:09:18 +00001871 if (Constant *FC = ConstantFoldExtractElementInstruction(
Owen Anderson23a204d2009-07-31 17:39:07 +00001872 ReqTy->getContext(), Val, Idx))
Robert Bocchinode7f1c92006-01-10 20:03:46 +00001873 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00001874 // Look up the constant in the table first to ensure uniqueness
1875 std::vector<Constant*> ArgVec(1, Val);
1876 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001877 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001878
1879 // Implicitly locked.
1880 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00001881}
1882
1883Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001884 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001885 "Tried to create extractelement operation on non-vector type!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001886 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00001887 "Extractelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001888 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +00001889 Val, Idx);
1890}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001891
Robert Bocchinoca27f032006-01-17 20:07:22 +00001892Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
1893 Constant *Elt, Constant *Idx) {
Owen Anderson53a52212009-07-13 04:09:18 +00001894 if (Constant *FC = ConstantFoldInsertElementInstruction(
Owen Anderson23a204d2009-07-31 17:39:07 +00001895 ReqTy->getContext(), Val, Elt, Idx))
Robert Bocchinoca27f032006-01-17 20:07:22 +00001896 return FC; // Fold a few common cases...
1897 // Look up the constant in the table first to ensure uniqueness
1898 std::vector<Constant*> ArgVec(1, Val);
1899 ArgVec.push_back(Elt);
1900 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001901 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001902
1903 // Implicitly locked.
1904 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001905}
1906
1907Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1908 Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001909 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001910 "Tried to create insertelement operation on non-vector type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001911 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoca27f032006-01-17 20:07:22 +00001912 && "Insertelement types must match!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001913 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00001914 "Insertelement index must be i32 type!");
Gordon Henriksenb52d1ed2008-08-30 15:41:51 +00001915 return getInsertElementTy(Val->getType(), Val, Elt, Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001916}
1917
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001918Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
1919 Constant *V2, Constant *Mask) {
Owen Anderson53a52212009-07-13 04:09:18 +00001920 if (Constant *FC = ConstantFoldShuffleVectorInstruction(
Owen Anderson23a204d2009-07-31 17:39:07 +00001921 ReqTy->getContext(), V1, V2, Mask))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001922 return FC; // Fold a few common cases...
1923 // Look up the constant in the table first to ensure uniqueness
1924 std::vector<Constant*> ArgVec(1, V1);
1925 ArgVec.push_back(V2);
1926 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00001927 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001928
1929 // Implicitly locked.
1930 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001931}
1932
1933Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
1934 Constant *Mask) {
1935 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
1936 "Invalid shuffle vector constant expr operands!");
Nate Begeman94aa38d2009-02-12 21:28:33 +00001937
1938 unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements();
1939 const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
1940 const Type *ShufTy = VectorType::get(EltTy, NElts);
1941 return getShuffleVectorTy(ShufTy, V1, V2, Mask);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001942}
1943
Dan Gohman12fce772008-05-15 19:50:34 +00001944Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg,
1945 Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001946 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001947 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
1948 Idxs+NumIdx) == Val->getType() &&
1949 "insertvalue indices invalid!");
1950 assert(Agg->getType() == ReqTy &&
1951 "insertvalue type invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00001952 assert(Agg->getType()->isFirstClassType() &&
1953 "Non-first-class type for constant InsertValue expression");
Owen Anderson53a52212009-07-13 04:09:18 +00001954 Constant *FC = ConstantFoldInsertValueInstruction(
Owen Anderson23a204d2009-07-31 17:39:07 +00001955 ReqTy->getContext(), Agg, Val, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00001956 assert(FC && "InsertValue constant expr couldn't be folded!");
1957 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00001958}
1959
1960Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001961 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00001962 assert(Agg->getType()->isFirstClassType() &&
1963 "Tried to create insertelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00001964
Dan Gohman0752bff2008-05-23 00:36:11 +00001965 const Type *ReqTy = Agg->getType();
Devang Pateld26344d2008-11-03 23:20:04 +00001966#ifndef NDEBUG
Dan Gohman0752bff2008-05-23 00:36:11 +00001967 const Type *ValTy =
Dan Gohman12fce772008-05-15 19:50:34 +00001968 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
Devang Pateld26344d2008-11-03 23:20:04 +00001969#endif
Dan Gohman0752bff2008-05-23 00:36:11 +00001970 assert(ValTy == Val->getType() && "insertvalue indices invalid!");
Dan Gohman12fce772008-05-15 19:50:34 +00001971 return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx);
1972}
1973
1974Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001975 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001976 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
1977 Idxs+NumIdx) == ReqTy &&
1978 "extractvalue indices invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00001979 assert(Agg->getType()->isFirstClassType() &&
1980 "Non-first-class type for constant extractvalue expression");
Owen Anderson53a52212009-07-13 04:09:18 +00001981 Constant *FC = ConstantFoldExtractValueInstruction(
Owen Anderson23a204d2009-07-31 17:39:07 +00001982 ReqTy->getContext(), Agg, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00001983 assert(FC && "ExtractValue constant expr couldn't be folded!");
1984 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00001985}
1986
1987Constant *ConstantExpr::getExtractValue(Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001988 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00001989 assert(Agg->getType()->isFirstClassType() &&
1990 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00001991
1992 const Type *ReqTy =
1993 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
1994 assert(ReqTy && "extractvalue indices invalid!");
1995 return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx);
1996}
1997
Owen Anderson487375e2009-07-29 18:55:55 +00001998Constant* ConstantExpr::getNeg(Constant* C) {
1999 // API compatibility: Adjust integer opcodes to floating-point opcodes.
2000 if (C->getType()->isFPOrFPVector())
2001 return getFNeg(C);
2002 assert(C->getType()->isIntOrIntVector() &&
2003 "Cannot NEG a nonintegral value!");
2004 return get(Instruction::Sub,
2005 ConstantFP::getZeroValueForNegation(C->getType()),
2006 C);
2007}
2008
2009Constant* ConstantExpr::getFNeg(Constant* C) {
2010 assert(C->getType()->isFPOrFPVector() &&
2011 "Cannot FNEG a non-floating-point value!");
2012 return get(Instruction::FSub,
2013 ConstantFP::getZeroValueForNegation(C->getType()),
2014 C);
2015}
2016
2017Constant* ConstantExpr::getNot(Constant* C) {
2018 assert(C->getType()->isIntOrIntVector() &&
2019 "Cannot NOT a nonintegral value!");
Owen Anderson5a1acd92009-07-31 20:28:14 +00002020 return get(Instruction::Xor, C, Constant::getAllOnesValue(C->getType()));
Owen Anderson487375e2009-07-29 18:55:55 +00002021}
2022
2023Constant* ConstantExpr::getAdd(Constant* C1, Constant* C2) {
2024 return get(Instruction::Add, C1, C2);
2025}
2026
2027Constant* ConstantExpr::getFAdd(Constant* C1, Constant* C2) {
2028 return get(Instruction::FAdd, C1, C2);
2029}
2030
2031Constant* ConstantExpr::getSub(Constant* C1, Constant* C2) {
2032 return get(Instruction::Sub, C1, C2);
2033}
2034
2035Constant* ConstantExpr::getFSub(Constant* C1, Constant* C2) {
2036 return get(Instruction::FSub, C1, C2);
2037}
2038
2039Constant* ConstantExpr::getMul(Constant* C1, Constant* C2) {
2040 return get(Instruction::Mul, C1, C2);
2041}
2042
2043Constant* ConstantExpr::getFMul(Constant* C1, Constant* C2) {
2044 return get(Instruction::FMul, C1, C2);
2045}
2046
2047Constant* ConstantExpr::getUDiv(Constant* C1, Constant* C2) {
2048 return get(Instruction::UDiv, C1, C2);
2049}
2050
2051Constant* ConstantExpr::getSDiv(Constant* C1, Constant* C2) {
2052 return get(Instruction::SDiv, C1, C2);
2053}
2054
2055Constant* ConstantExpr::getFDiv(Constant* C1, Constant* C2) {
2056 return get(Instruction::FDiv, C1, C2);
2057}
2058
2059Constant* ConstantExpr::getURem(Constant* C1, Constant* C2) {
2060 return get(Instruction::URem, C1, C2);
2061}
2062
2063Constant* ConstantExpr::getSRem(Constant* C1, Constant* C2) {
2064 return get(Instruction::SRem, C1, C2);
2065}
2066
2067Constant* ConstantExpr::getFRem(Constant* C1, Constant* C2) {
2068 return get(Instruction::FRem, C1, C2);
2069}
2070
2071Constant* ConstantExpr::getAnd(Constant* C1, Constant* C2) {
2072 return get(Instruction::And, C1, C2);
2073}
2074
2075Constant* ConstantExpr::getOr(Constant* C1, Constant* C2) {
2076 return get(Instruction::Or, C1, C2);
2077}
2078
2079Constant* ConstantExpr::getXor(Constant* C1, Constant* C2) {
2080 return get(Instruction::Xor, C1, C2);
2081}
2082
2083Constant* ConstantExpr::getShl(Constant* C1, Constant* C2) {
2084 return get(Instruction::Shl, C1, C2);
2085}
2086
2087Constant* ConstantExpr::getLShr(Constant* C1, Constant* C2) {
2088 return get(Instruction::LShr, C1, C2);
2089}
2090
2091Constant* ConstantExpr::getAShr(Constant* C1, Constant* C2) {
2092 return get(Instruction::AShr, C1, C2);
2093}
2094
Vikram S. Adve4c485332002-07-15 18:19:33 +00002095// destroyConstant - Remove the constant from the constant table...
2096//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002097void ConstantExpr::destroyConstant() {
2098 // Implicitly locked.
2099 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002100 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002101}
2102
Chris Lattner3cd8c562002-07-30 18:54:25 +00002103const char *ConstantExpr::getOpcodeName() const {
2104 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002105}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00002106
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002107//===----------------------------------------------------------------------===//
2108// replaceUsesOfWithOnConstant implementations
2109
Chris Lattner913849b2007-08-21 00:55:23 +00002110/// replaceUsesOfWithOnConstant - Update this constant array to change uses of
2111/// 'From' to be uses of 'To'. This must update the uniquing data structures
2112/// etc.
2113///
2114/// Note that we intentionally replace all uses of From with To here. Consider
2115/// a large array that uses 'From' 1000 times. By handling this case all here,
2116/// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that
2117/// single invocation handles all 1000 uses. Handling them one at a time would
2118/// work, but would be really slow because it would have to unique each updated
2119/// array instance.
Owen Andersonc2c79322009-07-28 18:32:17 +00002120
2121static std::vector<Constant*> getValType(ConstantArray *CA) {
2122 std::vector<Constant*> Elements;
2123 Elements.reserve(CA->getNumOperands());
2124 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
2125 Elements.push_back(cast<Constant>(CA->getOperand(i)));
2126 return Elements;
2127}
2128
2129
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002130void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002131 Use *U) {
Owen Andersonc2c79322009-07-28 18:32:17 +00002132 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2133 Constant *ToC = cast<Constant>(To);
2134
2135 LLVMContext &Context = getType()->getContext();
2136 LLVMContextImpl *pImpl = Context.pImpl;
2137
2138 std::pair<LLVMContextImpl::ArrayConstantsTy::MapKey, Constant*> Lookup;
2139 Lookup.first.first = getType();
2140 Lookup.second = this;
2141
2142 std::vector<Constant*> &Values = Lookup.first.second;
2143 Values.reserve(getNumOperands()); // Build replacement array.
2144
2145 // Fill values with the modified operands of the constant array. Also,
2146 // compute whether this turns into an all-zeros array.
2147 bool isAllZeros = false;
2148 unsigned NumUpdated = 0;
2149 if (!ToC->isNullValue()) {
2150 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2151 Constant *Val = cast<Constant>(O->get());
2152 if (Val == From) {
2153 Val = ToC;
2154 ++NumUpdated;
2155 }
2156 Values.push_back(Val);
2157 }
2158 } else {
2159 isAllZeros = true;
2160 for (Use *O = OperandList, *E = OperandList+getNumOperands();O != E; ++O) {
2161 Constant *Val = cast<Constant>(O->get());
2162 if (Val == From) {
2163 Val = ToC;
2164 ++NumUpdated;
2165 }
2166 Values.push_back(Val);
2167 if (isAllZeros) isAllZeros = Val->isNullValue();
2168 }
2169 }
2170
2171 Constant *Replacement = 0;
2172 if (isAllZeros) {
Owen Andersonb292b8c2009-07-30 23:03:37 +00002173 Replacement = ConstantAggregateZero::get(getType());
Owen Andersonc2c79322009-07-28 18:32:17 +00002174 } else {
2175 // Check to see if we have this array type already.
2176 sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
2177 bool Exists;
2178 LLVMContextImpl::ArrayConstantsTy::MapTy::iterator I =
2179 pImpl->ArrayConstants.InsertOrGetItem(Lookup, Exists);
2180
2181 if (Exists) {
2182 Replacement = I->second;
2183 } else {
2184 // Okay, the new shape doesn't exist in the system yet. Instead of
2185 // creating a new constant array, inserting it, replaceallusesof'ing the
2186 // old with the new, then deleting the old... just update the current one
2187 // in place!
2188 pImpl->ArrayConstants.MoveConstantToNewSlot(this, I);
2189
2190 // Update to the new value. Optimize for the case when we have a single
2191 // operand that we're changing, but handle bulk updates efficiently.
2192 if (NumUpdated == 1) {
2193 unsigned OperandToUpdate = U - OperandList;
2194 assert(getOperand(OperandToUpdate) == From &&
2195 "ReplaceAllUsesWith broken!");
2196 setOperand(OperandToUpdate, ToC);
2197 } else {
2198 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2199 if (getOperand(i) == From)
2200 setOperand(i, ToC);
2201 }
2202 return;
2203 }
2204 }
Chris Lattnerb64419a2005-10-03 22:51:37 +00002205
2206 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002207 assert(Replacement != this && "I didn't contain From!");
2208
Chris Lattner7a1450d2005-10-04 18:13:04 +00002209 // Everyone using this now uses the replacement.
2210 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002211
2212 // Delete the old constant!
2213 destroyConstant();
2214}
2215
Owen Anderson45308b52009-07-27 22:29:26 +00002216static std::vector<Constant*> getValType(ConstantStruct *CS) {
2217 std::vector<Constant*> Elements;
2218 Elements.reserve(CS->getNumOperands());
2219 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
2220 Elements.push_back(cast<Constant>(CS->getOperand(i)));
2221 return Elements;
2222}
2223
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002224void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002225 Use *U) {
Owen Anderson45308b52009-07-27 22:29:26 +00002226 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2227 Constant *ToC = cast<Constant>(To);
2228
2229 unsigned OperandToUpdate = U-OperandList;
2230 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
2231
2232 std::pair<LLVMContextImpl::StructConstantsTy::MapKey, Constant*> Lookup;
2233 Lookup.first.first = getType();
2234 Lookup.second = this;
2235 std::vector<Constant*> &Values = Lookup.first.second;
2236 Values.reserve(getNumOperands()); // Build replacement struct.
2237
2238
2239 // Fill values with the modified operands of the constant struct. Also,
2240 // compute whether this turns into an all-zeros struct.
2241 bool isAllZeros = false;
2242 if (!ToC->isNullValue()) {
2243 for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O)
2244 Values.push_back(cast<Constant>(O->get()));
2245 } else {
2246 isAllZeros = true;
2247 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2248 Constant *Val = cast<Constant>(O->get());
2249 Values.push_back(Val);
2250 if (isAllZeros) isAllZeros = Val->isNullValue();
2251 }
2252 }
2253 Values[OperandToUpdate] = ToC;
2254
2255 LLVMContext &Context = getType()->getContext();
2256 LLVMContextImpl *pImpl = Context.pImpl;
2257
2258 Constant *Replacement = 0;
2259 if (isAllZeros) {
Owen Andersonb292b8c2009-07-30 23:03:37 +00002260 Replacement = ConstantAggregateZero::get(getType());
Owen Anderson45308b52009-07-27 22:29:26 +00002261 } else {
2262 // Check to see if we have this array type already.
2263 sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
2264 bool Exists;
2265 LLVMContextImpl::StructConstantsTy::MapTy::iterator I =
2266 pImpl->StructConstants.InsertOrGetItem(Lookup, Exists);
2267
2268 if (Exists) {
2269 Replacement = I->second;
2270 } else {
2271 // Okay, the new shape doesn't exist in the system yet. Instead of
2272 // creating a new constant struct, inserting it, replaceallusesof'ing the
2273 // old with the new, then deleting the old... just update the current one
2274 // in place!
2275 pImpl->StructConstants.MoveConstantToNewSlot(this, I);
2276
2277 // Update to the new value.
2278 setOperand(OperandToUpdate, ToC);
2279 return;
2280 }
2281 }
2282
2283 assert(Replacement != this && "I didn't contain From!");
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002284
Chris Lattner7a1450d2005-10-04 18:13:04 +00002285 // Everyone using this now uses the replacement.
2286 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002287
2288 // Delete the old constant!
2289 destroyConstant();
2290}
2291
Owen Anderson4aa32952009-07-28 21:19:26 +00002292static std::vector<Constant*> getValType(ConstantVector *CP) {
2293 std::vector<Constant*> Elements;
2294 Elements.reserve(CP->getNumOperands());
2295 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
2296 Elements.push_back(CP->getOperand(i));
2297 return Elements;
2298}
2299
Reid Spencerd84d35b2007-02-15 02:26:10 +00002300void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002301 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002302 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2303
2304 std::vector<Constant*> Values;
2305 Values.reserve(getNumOperands()); // Build replacement array...
2306 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2307 Constant *Val = getOperand(i);
2308 if (Val == From) Val = cast<Constant>(To);
2309 Values.push_back(Val);
2310 }
2311
Owen Anderson4aa32952009-07-28 21:19:26 +00002312 Constant *Replacement = get(getType(), Values);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002313 assert(Replacement != this && "I didn't contain From!");
2314
Chris Lattner7a1450d2005-10-04 18:13:04 +00002315 // Everyone using this now uses the replacement.
2316 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002317
2318 // Delete the old constant!
2319 destroyConstant();
2320}
2321
2322void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002323 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002324 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2325 Constant *To = cast<Constant>(ToV);
2326
2327 Constant *Replacement = 0;
2328 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerb5d70302007-02-19 20:01:23 +00002329 SmallVector<Constant*, 8> Indices;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002330 Constant *Pointer = getOperand(0);
2331 Indices.reserve(getNumOperands()-1);
2332 if (Pointer == From) Pointer = To;
2333
2334 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2335 Constant *Val = getOperand(i);
2336 if (Val == From) Val = To;
2337 Indices.push_back(Val);
2338 }
Chris Lattnerb5d70302007-02-19 20:01:23 +00002339 Replacement = ConstantExpr::getGetElementPtr(Pointer,
2340 &Indices[0], Indices.size());
Dan Gohman12fce772008-05-15 19:50:34 +00002341 } else if (getOpcode() == Instruction::ExtractValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002342 Constant *Agg = getOperand(0);
Dan Gohman12fce772008-05-15 19:50:34 +00002343 if (Agg == From) Agg = To;
2344
Dan Gohman1ecaf452008-05-31 00:58:22 +00002345 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002346 Replacement = ConstantExpr::getExtractValue(Agg,
2347 &Indices[0], Indices.size());
2348 } else if (getOpcode() == Instruction::InsertValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002349 Constant *Agg = getOperand(0);
2350 Constant *Val = getOperand(1);
Dan Gohman12fce772008-05-15 19:50:34 +00002351 if (Agg == From) Agg = To;
2352 if (Val == From) Val = To;
2353
Dan Gohman1ecaf452008-05-31 00:58:22 +00002354 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002355 Replacement = ConstantExpr::getInsertValue(Agg, Val,
2356 &Indices[0], Indices.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002357 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002358 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002359 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002360 } else if (getOpcode() == Instruction::Select) {
2361 Constant *C1 = getOperand(0);
2362 Constant *C2 = getOperand(1);
2363 Constant *C3 = getOperand(2);
2364 if (C1 == From) C1 = To;
2365 if (C2 == From) C2 = To;
2366 if (C3 == From) C3 = To;
2367 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00002368 } else if (getOpcode() == Instruction::ExtractElement) {
2369 Constant *C1 = getOperand(0);
2370 Constant *C2 = getOperand(1);
2371 if (C1 == From) C1 = To;
2372 if (C2 == From) C2 = To;
2373 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00002374 } else if (getOpcode() == Instruction::InsertElement) {
2375 Constant *C1 = getOperand(0);
2376 Constant *C2 = getOperand(1);
2377 Constant *C3 = getOperand(1);
2378 if (C1 == From) C1 = To;
2379 if (C2 == From) C2 = To;
2380 if (C3 == From) C3 = To;
2381 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2382 } else if (getOpcode() == Instruction::ShuffleVector) {
2383 Constant *C1 = getOperand(0);
2384 Constant *C2 = getOperand(1);
2385 Constant *C3 = getOperand(2);
2386 if (C1 == From) C1 = To;
2387 if (C2 == From) C2 = To;
2388 if (C3 == From) C3 = To;
2389 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00002390 } else if (isCompare()) {
2391 Constant *C1 = getOperand(0);
2392 Constant *C2 = getOperand(1);
2393 if (C1 == From) C1 = To;
2394 if (C2 == From) C2 = To;
2395 if (getOpcode() == Instruction::ICmp)
2396 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002397 else {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002398 assert(getOpcode() == Instruction::FCmp);
2399 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002400 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002401 } else if (getNumOperands() == 2) {
2402 Constant *C1 = getOperand(0);
2403 Constant *C2 = getOperand(1);
2404 if (C1 == From) C1 = To;
2405 if (C2 == From) C2 = To;
2406 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2407 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002408 llvm_unreachable("Unknown ConstantExpr type!");
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002409 return;
2410 }
2411
2412 assert(Replacement != this && "I didn't contain From!");
2413
Chris Lattner7a1450d2005-10-04 18:13:04 +00002414 // Everyone using this now uses the replacement.
2415 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002416
2417 // Delete the old constant!
2418 destroyConstant();
Matthijs Kooijmanba5d7ef2008-07-03 07:46:41 +00002419}
Nick Lewycky49f89192009-04-04 07:22:01 +00002420