blob: af3892a9bd4f9438d47201116a456064e3bf4b6c [file] [log] [blame]
Chris Lattner9bc02a42003-05-13 21:37:02 +00001//===-- Constants.cpp - Implement Constant nodes --------------------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
Chris Lattnere9bb2df2001-12-03 22:26:30 +000010// This file implements the Constant* classes...
Chris Lattner00950542001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Owen Andersoneed707b2009-07-24 23:12:02 +000014#include "LLVMContextImpl.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000015#include "llvm/Constants.h"
Chris Lattner92f6fea2007-02-27 03:05:06 +000016#include "ConstantFold.h"
Chris Lattner00950542001-06-06 20:29:01 +000017#include "llvm/DerivedTypes.h"
Reid Spencer1c9c8e62004-07-17 23:48:33 +000018#include "llvm/GlobalValue.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000019#include "llvm/Instructions.h"
Chris Lattnerf5ec48d2001-10-13 06:57:33 +000020#include "llvm/Module.h"
Dan Gohman5a206ee2009-07-18 01:49:22 +000021#include "llvm/Operator.h"
Nick Lewycky21cc4462009-04-04 07:22:01 +000022#include "llvm/ADT/FoldingSet.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/ADT/StringExtras.h"
Nick Lewycky21cc4462009-04-04 07:22:01 +000024#include "llvm/ADT/StringMap.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000025#include "llvm/Support/Compiler.h"
Bill Wendling2e3def12006-11-17 08:03:48 +000026#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000027#include "llvm/Support/ErrorHandling.h"
Chris Lattner8a94bf12006-09-28 00:35:06 +000028#include "llvm/Support/ManagedStatic.h"
Bill Wendling2e3def12006-11-17 08:03:48 +000029#include "llvm/Support/MathExtras.h"
Owen Anderson04fb7c32009-06-20 00:24:58 +000030#include "llvm/System/Mutex.h"
Owen Anderson3e456ab2009-06-17 18:40:29 +000031#include "llvm/System/RWMutex.h"
Owen Andersone3cd5ca2009-06-18 16:54:52 +000032#include "llvm/System/Threading.h"
Chris Lattner6b6f6ba2007-02-20 06:39:57 +000033#include "llvm/ADT/DenseMap.h"
Chris Lattnerf9021ff2007-02-19 20:01:23 +000034#include "llvm/ADT/SmallVector.h"
Chris Lattner00950542001-06-06 20:29:01 +000035#include <algorithm>
Reid Spenceref9b9a72007-02-05 20:47:22 +000036#include <map>
Chris Lattner31f84992003-11-21 20:23:48 +000037using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000038
Chris Lattner00950542001-06-06 20:29:01 +000039//===----------------------------------------------------------------------===//
Chris Lattnere9bb2df2001-12-03 22:26:30 +000040// Constant Class
Chris Lattner00950542001-06-06 20:29:01 +000041//===----------------------------------------------------------------------===//
42
Owen Andersonee6aefc2009-06-18 19:10:19 +000043// Becomes a no-op when multithreading is disabled.
44ManagedStatic<sys::SmartRWMutex<true> > ConstantsLock;
Owen Anderson3e456ab2009-06-17 18:40:29 +000045
Chris Lattnere9bb2df2001-12-03 22:26:30 +000046void Constant::destroyConstantImpl() {
47 // When a Constant is destroyed, there may be lingering
Chris Lattnerf5ec48d2001-10-13 06:57:33 +000048 // references to the constant by other constants in the constant pool. These
Misha Brukmanef6a6a62003-08-21 22:14:26 +000049 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerf5ec48d2001-10-13 06:57:33 +000050 // but they don't know that. Because we only find out when the CPV is
51 // deleted, we must now notify all of our users (that should only be
Chris Lattnere9bb2df2001-12-03 22:26:30 +000052 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerf5ec48d2001-10-13 06:57:33 +000053 //
54 while (!use_empty()) {
55 Value *V = use_back();
56#ifndef NDEBUG // Only in -g mode...
Chris Lattner6183b922002-07-18 00:14:50 +000057 if (!isa<Constant>(V))
Bill Wendling2e3def12006-11-17 08:03:48 +000058 DOUT << "While deleting: " << *this
59 << "\n\nUse still stuck around after Def is destroyed: "
60 << *V << "\n\n";
Chris Lattnerf5ec48d2001-10-13 06:57:33 +000061#endif
Vikram S. Adve345e0cf2002-07-14 23:13:17 +000062 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1c9c8e62004-07-17 23:48:33 +000063 Constant *CV = cast<Constant>(V);
64 CV->destroyConstant();
Chris Lattnerf5ec48d2001-10-13 06:57:33 +000065
66 // The constant should remove itself from our use list...
Vikram S. Adve345e0cf2002-07-14 23:13:17 +000067 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerf5ec48d2001-10-13 06:57:33 +000068 }
69
70 // Value has no outstanding references it is safe to delete it now...
71 delete this;
Chris Lattner1d87bcf2001-10-01 20:11:19 +000072}
Chris Lattner00950542001-06-06 20:29:01 +000073
Chris Lattner35b89fa2006-10-20 00:27:06 +000074/// canTrap - Return true if evaluation of this constant could trap. This is
75/// true for things like constant expressions that could divide by zero.
76bool Constant::canTrap() const {
77 assert(getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
78 // The only thing that could possibly trap are constant exprs.
79 const ConstantExpr *CE = dyn_cast<ConstantExpr>(this);
80 if (!CE) return false;
81
82 // ConstantExpr traps if any operands can trap.
83 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
84 if (getOperand(i)->canTrap())
85 return true;
86
87 // Otherwise, only specific operations can trap.
88 switch (CE->getOpcode()) {
89 default:
90 return false;
Reid Spencer1628cec2006-10-26 06:15:43 +000091 case Instruction::UDiv:
92 case Instruction::SDiv:
93 case Instruction::FDiv:
Reid Spencer0a783f72006-11-02 01:53:59 +000094 case Instruction::URem:
95 case Instruction::SRem:
96 case Instruction::FRem:
Chris Lattner35b89fa2006-10-20 00:27:06 +000097 // Div and rem can trap if the RHS is not known to be non-zero.
98 if (!isa<ConstantInt>(getOperand(1)) || getOperand(1)->isNullValue())
99 return true;
100 return false;
101 }
102}
103
Chris Lattner7cf12c72009-07-22 00:05:44 +0000104
105/// getRelocationInfo - This method classifies the entry according to
106/// whether or not it may generate a relocation entry. This must be
107/// conservative, so if it might codegen to a relocatable entry, it should say
108/// so. The return values are:
109///
Chris Lattner083a1e02009-07-24 03:27:21 +0000110/// NoRelocation: This constant pool entry is guaranteed to never have a
111/// relocation applied to it (because it holds a simple constant like
112/// '4').
113/// LocalRelocation: This entry has relocations, but the entries are
114/// guaranteed to be resolvable by the static linker, so the dynamic
115/// linker will never see them.
116/// GlobalRelocations: This entry may have arbitrary relocations.
Chris Lattner7cf12c72009-07-22 00:05:44 +0000117///
118/// FIXME: This really should not be in VMCore.
Chris Lattner083a1e02009-07-24 03:27:21 +0000119Constant::PossibleRelocationsTy Constant::getRelocationInfo() const {
120 if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
Chris Lattner7cf12c72009-07-22 00:05:44 +0000121 if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
Chris Lattner083a1e02009-07-24 03:27:21 +0000122 return LocalRelocation; // Local to this file/library.
123 return GlobalRelocations; // Global reference.
Anton Korobeynikovab267a22009-03-29 17:13:18 +0000124 }
Chris Lattner7cf12c72009-07-22 00:05:44 +0000125
Chris Lattner083a1e02009-07-24 03:27:21 +0000126 PossibleRelocationsTy Result = NoRelocation;
Evan Chengafe15812007-03-08 00:59:12 +0000127 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Chris Lattner7cf12c72009-07-22 00:05:44 +0000128 Result = std::max(Result, getOperand(i)->getRelocationInfo());
129
130 return Result;
Evan Chengafe15812007-03-08 00:59:12 +0000131}
132
Chris Lattner7cf12c72009-07-22 00:05:44 +0000133
Chris Lattner86381442008-07-10 00:28:11 +0000134/// getVectorElements - This method, which is only valid on constant of vector
135/// type, returns the elements of the vector in the specified smallvector.
Chris Lattner071aade2008-07-14 05:10:41 +0000136/// This handles breaking down a vector undef into undef elements, etc. For
137/// constant exprs and other cases we can't handle, we return an empty vector.
Owen Anderson0a5372e2009-07-13 04:09:18 +0000138void Constant::getVectorElements(LLVMContext &Context,
139 SmallVectorImpl<Constant*> &Elts) const {
Chris Lattner86381442008-07-10 00:28:11 +0000140 assert(isa<VectorType>(getType()) && "Not a vector constant!");
141
142 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) {
143 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i)
144 Elts.push_back(CV->getOperand(i));
145 return;
146 }
147
148 const VectorType *VT = cast<VectorType>(getType());
149 if (isa<ConstantAggregateZero>(this)) {
150 Elts.assign(VT->getNumElements(),
Owen Anderson0a5372e2009-07-13 04:09:18 +0000151 Context.getNullValue(VT->getElementType()));
Chris Lattner86381442008-07-10 00:28:11 +0000152 return;
153 }
154
Chris Lattner071aade2008-07-14 05:10:41 +0000155 if (isa<UndefValue>(this)) {
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000156 Elts.assign(VT->getNumElements(), UndefValue::get(VT->getElementType()));
Chris Lattner071aade2008-07-14 05:10:41 +0000157 return;
158 }
159
160 // Unknown type, must be constant expr etc.
Chris Lattner86381442008-07-10 00:28:11 +0000161}
162
163
164
Chris Lattner00950542001-06-06 20:29:01 +0000165//===----------------------------------------------------------------------===//
Chris Lattner6b6f6ba2007-02-20 06:39:57 +0000166// ConstantInt
Chris Lattner00950542001-06-06 20:29:01 +0000167//===----------------------------------------------------------------------===//
168
Reid Spencer532d0ce2007-02-26 23:54:03 +0000169ConstantInt::ConstantInt(const IntegerType *Ty, const APInt& V)
Chris Lattnereb41bdd2007-02-20 05:55:46 +0000170 : Constant(Ty, ConstantIntVal, 0, 0), Val(V) {
Reid Spencer532d0ce2007-02-26 23:54:03 +0000171 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
Chris Lattner00950542001-06-06 20:29:01 +0000172}
173
Owen Anderson5defacc2009-07-31 17:39:07 +0000174ConstantInt* ConstantInt::getTrue(LLVMContext &Context) {
175 LLVMContextImpl *pImpl = Context.pImpl;
176 sys::SmartScopedWriter<true>(pImpl->ConstantsLock);
177 if (pImpl->TheTrueVal)
178 return pImpl->TheTrueVal;
179 else
180 return (pImpl->TheTrueVal = ConstantInt::get(IntegerType::get(1), 1));
181}
182
183ConstantInt* ConstantInt::getFalse(LLVMContext &Context) {
184 LLVMContextImpl *pImpl = Context.pImpl;
185 sys::SmartScopedWriter<true>(pImpl->ConstantsLock);
186 if (pImpl->TheFalseVal)
187 return pImpl->TheFalseVal;
188 else
189 return (pImpl->TheFalseVal = ConstantInt::get(IntegerType::get(1), 0));
190}
191
192
Owen Andersoneed707b2009-07-24 23:12:02 +0000193// Get a ConstantInt from an APInt. Note that the value stored in the DenseMap
194// as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the
195// operator== and operator!= to ensure that the DenseMap doesn't attempt to
196// compare APInt's of different widths, which would violate an APInt class
197// invariant which generates an assertion.
198ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt& V) {
199 // Get the corresponding integer type for the bit width of the value.
Owen Andersondebcb012009-07-29 22:17:13 +0000200 const IntegerType *ITy = IntegerType::get(V.getBitWidth());
Owen Andersoneed707b2009-07-24 23:12:02 +0000201 // get an existing value or the insertion position
202 DenseMapAPIntKeyInfo::KeyTy Key(V, ITy);
203
204 Context.pImpl->ConstantsLock.reader_acquire();
205 ConstantInt *&Slot = Context.pImpl->IntConstants[Key];
206 Context.pImpl->ConstantsLock.reader_release();
207
208 if (!Slot) {
209 sys::SmartScopedWriter<true> Writer(Context.pImpl->ConstantsLock);
210 ConstantInt *&NewSlot = Context.pImpl->IntConstants[Key];
211 if (!Slot) {
212 NewSlot = new ConstantInt(ITy, V);
213 }
214
215 return NewSlot;
216 } else {
217 return Slot;
218 }
219}
220
221Constant* ConstantInt::get(const Type* Ty, uint64_t V, bool isSigned) {
222 Constant *C = get(cast<IntegerType>(Ty->getScalarType()),
223 V, isSigned);
224
225 // For vectors, broadcast the value.
226 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
Owen Andersonaf7ec972009-07-28 21:19:26 +0000227 return ConstantVector::get(
Owen Andersoneed707b2009-07-24 23:12:02 +0000228 std::vector<Constant *>(VTy->getNumElements(), C));
229
230 return C;
231}
232
233ConstantInt* ConstantInt::get(const IntegerType* Ty, uint64_t V,
234 bool isSigned) {
235 return get(Ty->getContext(), APInt(Ty->getBitWidth(), V, isSigned));
236}
237
238ConstantInt* ConstantInt::getSigned(const IntegerType* Ty, int64_t V) {
239 return get(Ty, V, true);
240}
241
242Constant *ConstantInt::getSigned(const Type *Ty, int64_t V) {
243 return get(Ty, V, true);
244}
245
246Constant* ConstantInt::get(const Type* Ty, const APInt& V) {
247 ConstantInt *C = get(Ty->getContext(), V);
248 assert(C->getType() == Ty->getScalarType() &&
249 "ConstantInt type doesn't match the type implied by its value!");
250
251 // For vectors, broadcast the value.
252 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
Owen Andersonaf7ec972009-07-28 21:19:26 +0000253 return ConstantVector::get(
Owen Andersoneed707b2009-07-24 23:12:02 +0000254 std::vector<Constant *>(VTy->getNumElements(), C));
255
256 return C;
257}
258
Chris Lattner6b6f6ba2007-02-20 06:39:57 +0000259//===----------------------------------------------------------------------===//
Chris Lattner9b4ee0c2007-02-20 07:17:17 +0000260// ConstantFP
Chris Lattner6b6f6ba2007-02-20 06:39:57 +0000261//===----------------------------------------------------------------------===//
262
Rafael Espindola87d1f472009-07-15 17:40:42 +0000263static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
264 if (Ty == Type::FloatTy)
265 return &APFloat::IEEEsingle;
266 if (Ty == Type::DoubleTy)
267 return &APFloat::IEEEdouble;
268 if (Ty == Type::X86_FP80Ty)
269 return &APFloat::x87DoubleExtended;
270 else if (Ty == Type::FP128Ty)
271 return &APFloat::IEEEquad;
272
273 assert(Ty == Type::PPC_FP128Ty && "Unknown FP format");
274 return &APFloat::PPCDoubleDouble;
275}
276
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000277/// get() - This returns a constant fp for the specified value in the
278/// specified type. This should only be used for simple constant values like
279/// 2.0/1.0 etc, that are known-valid both as double and as the target format.
280Constant* ConstantFP::get(const Type* Ty, double V) {
281 LLVMContext &Context = Ty->getContext();
282
283 APFloat FV(V);
284 bool ignored;
285 FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
286 APFloat::rmNearestTiesToEven, &ignored);
287 Constant *C = get(Context, FV);
288
289 // For vectors, broadcast the value.
290 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
Owen Andersonaf7ec972009-07-28 21:19:26 +0000291 return ConstantVector::get(
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000292 std::vector<Constant *>(VTy->getNumElements(), C));
293
294 return C;
295}
296
297ConstantFP* ConstantFP::getNegativeZero(const Type* Ty) {
298 LLVMContext &Context = Ty->getContext();
299 APFloat apf = cast <ConstantFP>(Context.getNullValue(Ty))->getValueAPF();
300 apf.changeSign();
301 return get(Context, apf);
302}
303
304
305Constant* ConstantFP::getZeroValueForNegation(const Type* Ty) {
306 LLVMContext &Context = Ty->getContext();
307 if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
308 if (PTy->getElementType()->isFloatingPoint()) {
309 std::vector<Constant*> zeros(PTy->getNumElements(),
310 getNegativeZero(PTy->getElementType()));
Owen Andersonaf7ec972009-07-28 21:19:26 +0000311 return ConstantVector::get(PTy, zeros);
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000312 }
313
314 if (Ty->isFloatingPoint())
315 return getNegativeZero(Ty);
316
317 return Context.getNullValue(Ty);
318}
319
320
321// ConstantFP accessors.
322ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) {
323 DenseMapAPFloatKeyInfo::KeyTy Key(V);
324
325 LLVMContextImpl* pImpl = Context.pImpl;
326
327 pImpl->ConstantsLock.reader_acquire();
328 ConstantFP *&Slot = pImpl->FPConstants[Key];
329 pImpl->ConstantsLock.reader_release();
330
331 if (!Slot) {
332 sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
333 ConstantFP *&NewSlot = pImpl->FPConstants[Key];
334 if (!NewSlot) {
335 const Type *Ty;
336 if (&V.getSemantics() == &APFloat::IEEEsingle)
337 Ty = Type::FloatTy;
338 else if (&V.getSemantics() == &APFloat::IEEEdouble)
339 Ty = Type::DoubleTy;
340 else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
341 Ty = Type::X86_FP80Ty;
342 else if (&V.getSemantics() == &APFloat::IEEEquad)
343 Ty = Type::FP128Ty;
344 else {
345 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble &&
346 "Unknown FP format");
347 Ty = Type::PPC_FP128Ty;
348 }
349 NewSlot = new ConstantFP(Ty, V);
350 }
351
352 return NewSlot;
353 }
354
355 return Slot;
356}
357
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000358ConstantFP::ConstantFP(const Type *Ty, const APFloat& V)
359 : Constant(Ty, ConstantFPVal, 0, 0), Val(V) {
Chris Lattner288e78f2008-04-09 06:38:30 +0000360 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
361 "FP type Mismatch");
Chris Lattner00950542001-06-06 20:29:01 +0000362}
363
Chris Lattner9b4ee0c2007-02-20 07:17:17 +0000364bool ConstantFP::isNullValue() const {
Dale Johannesen343e7702007-08-24 00:56:33 +0000365 return Val.isZero() && !Val.isNegative();
Chris Lattner9b4ee0c2007-02-20 07:17:17 +0000366}
367
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000368bool ConstantFP::isExactlyValue(const APFloat& V) const {
369 return Val.bitwiseIsEqual(V);
Chris Lattner9b4ee0c2007-02-20 07:17:17 +0000370}
371
Chris Lattner9b4ee0c2007-02-20 07:17:17 +0000372//===----------------------------------------------------------------------===//
373// ConstantXXX Classes
374//===----------------------------------------------------------------------===//
375
376
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000377ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnere4671472005-01-29 00:34:39 +0000378 const std::vector<Constant*> &V)
Gabor Greifefe65362008-05-10 08:32:32 +0000379 : Constant(T, ConstantArrayVal,
380 OperandTraits<ConstantArray>::op_end(this) - V.size(),
381 V.size()) {
Alkis Evlogimenose0de1d62004-09-15 02:32:15 +0000382 assert(V.size() == T->getNumElements() &&
383 "Invalid initializer vector for constant array");
Chris Lattnere4671472005-01-29 00:34:39 +0000384 Use *OL = OperandList;
Chris Lattnerdfdd6c52005-10-03 21:56:24 +0000385 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
386 I != E; ++I, ++OL) {
Chris Lattner71abaab2005-10-07 05:23:36 +0000387 Constant *C = *I;
388 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscad90ad2004-09-10 04:16:59 +0000389 (T->isAbstract() &&
Chris Lattner71abaab2005-10-07 05:23:36 +0000390 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscad90ad2004-09-10 04:16:59 +0000391 "Initializer for array element doesn't match array element type!");
Gabor Greif6c80c382008-05-26 21:33:52 +0000392 *OL = C;
Chris Lattner00950542001-06-06 20:29:01 +0000393 }
394}
395
Owen Anderson1fd70962009-07-28 18:32:17 +0000396Constant *ConstantArray::get(const ArrayType *Ty,
397 const std::vector<Constant*> &V) {
398 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
399 // If this is an all-zero array, return a ConstantAggregateZero object
400 if (!V.empty()) {
401 Constant *C = V[0];
402 if (!C->isNullValue()) {
403 // Implicitly locked.
404 return pImpl->ArrayConstants.getOrCreate(Ty, V);
405 }
406 for (unsigned i = 1, e = V.size(); i != e; ++i)
407 if (V[i] != C) {
408 // Implicitly locked.
409 return pImpl->ArrayConstants.getOrCreate(Ty, V);
410 }
411 }
412
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000413 return ConstantAggregateZero::get(Ty);
Owen Anderson1fd70962009-07-28 18:32:17 +0000414}
415
416
417Constant* ConstantArray::get(const ArrayType* T, Constant* const* Vals,
418 unsigned NumVals) {
419 // FIXME: make this the primary ctor method.
420 return get(T, std::vector<Constant*>(Vals, Vals+NumVals));
421}
422
423/// ConstantArray::get(const string&) - Return an array that is initialized to
424/// contain the specified string. If length is zero then a null terminator is
425/// added to the specified string so that it may be used in a natural way.
426/// Otherwise, the length parameter specifies how much of the string to use
427/// and it won't be null terminated.
428///
429Constant* ConstantArray::get(const StringRef &Str, bool AddNull) {
430 std::vector<Constant*> ElementVals;
431 for (unsigned i = 0; i < Str.size(); ++i)
432 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, Str[i]));
433
434 // Add a null terminator to the string...
435 if (AddNull) {
436 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, 0));
437 }
438
439 ArrayType *ATy = ArrayType::get(Type::Int8Ty, ElementVals.size());
440 return get(ATy, ElementVals);
441}
442
443
Chris Lattnere4671472005-01-29 00:34:39 +0000444
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000445ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnere4671472005-01-29 00:34:39 +0000446 const std::vector<Constant*> &V)
Gabor Greifefe65362008-05-10 08:32:32 +0000447 : Constant(T, ConstantStructVal,
448 OperandTraits<ConstantStruct>::op_end(this) - V.size(),
449 V.size()) {
Chris Lattnerd21cd802004-02-09 04:37:31 +0000450 assert(V.size() == T->getNumElements() &&
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000451 "Invalid initializer vector for constant structure");
Chris Lattnere4671472005-01-29 00:34:39 +0000452 Use *OL = OperandList;
Chris Lattnerdfdd6c52005-10-03 21:56:24 +0000453 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
454 I != E; ++I, ++OL) {
Chris Lattner71abaab2005-10-07 05:23:36 +0000455 Constant *C = *I;
456 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattnerdfdd6c52005-10-03 21:56:24 +0000457 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner71abaab2005-10-07 05:23:36 +0000458 C->getType()->isAbstract()) &&
Chris Lattnerdfdd6c52005-10-03 21:56:24 +0000459 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner71abaab2005-10-07 05:23:36 +0000460 C->getType()->getTypeID())) &&
Chris Lattnerb8438892003-06-02 17:42:47 +0000461 "Initializer for struct element doesn't match struct element type!");
Gabor Greif6c80c382008-05-26 21:33:52 +0000462 *OL = C;
Chris Lattner00950542001-06-06 20:29:01 +0000463 }
464}
465
Owen Anderson8fa33382009-07-27 22:29:26 +0000466// ConstantStruct accessors.
467Constant* ConstantStruct::get(const StructType* T,
468 const std::vector<Constant*>& V) {
469 LLVMContextImpl* pImpl = T->getContext().pImpl;
470
471 // Create a ConstantAggregateZero value if all elements are zeros...
472 for (unsigned i = 0, e = V.size(); i != e; ++i)
473 if (!V[i]->isNullValue())
474 // Implicitly locked.
475 return pImpl->StructConstants.getOrCreate(T, V);
476
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000477 return ConstantAggregateZero::get(T);
Owen Anderson8fa33382009-07-27 22:29:26 +0000478}
479
480Constant* ConstantStruct::get(const std::vector<Constant*>& V, bool packed) {
481 std::vector<const Type*> StructEls;
482 StructEls.reserve(V.size());
483 for (unsigned i = 0, e = V.size(); i != e; ++i)
484 StructEls.push_back(V[i]->getType());
485 return get(StructType::get(StructEls, packed), V);
486}
487
488Constant* ConstantStruct::get(Constant* const *Vals, unsigned NumVals,
489 bool Packed) {
490 // FIXME: make this the primary ctor method.
491 return get(std::vector<Constant*>(Vals, Vals+NumVals), Packed);
492}
Chris Lattnere4671472005-01-29 00:34:39 +0000493
Reid Spencer9d6565a2007-02-15 02:26:10 +0000494ConstantVector::ConstantVector(const VectorType *T,
Chris Lattnere4671472005-01-29 00:34:39 +0000495 const std::vector<Constant*> &V)
Gabor Greifefe65362008-05-10 08:32:32 +0000496 : Constant(T, ConstantVectorVal,
497 OperandTraits<ConstantVector>::op_end(this) - V.size(),
498 V.size()) {
Chris Lattnere4671472005-01-29 00:34:39 +0000499 Use *OL = OperandList;
Chris Lattnerdfdd6c52005-10-03 21:56:24 +0000500 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
501 I != E; ++I, ++OL) {
Chris Lattner71abaab2005-10-07 05:23:36 +0000502 Constant *C = *I;
503 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscad90ad2004-09-10 04:16:59 +0000504 (T->isAbstract() &&
Chris Lattner71abaab2005-10-07 05:23:36 +0000505 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Dan Gohmanfa73ea22007-05-24 14:36:04 +0000506 "Initializer for vector element doesn't match vector element type!");
Gabor Greif6c80c382008-05-26 21:33:52 +0000507 *OL = C;
Brian Gaeke715c90b2004-08-20 06:00:58 +0000508 }
509}
510
Owen Andersonaf7ec972009-07-28 21:19:26 +0000511// ConstantVector accessors.
512Constant* ConstantVector::get(const VectorType* T,
513 const std::vector<Constant*>& V) {
514 assert(!V.empty() && "Vectors can't be empty");
515 LLVMContext &Context = T->getContext();
516 LLVMContextImpl *pImpl = Context.pImpl;
517
518 // If this is an all-undef or alll-zero vector, return a
519 // ConstantAggregateZero or UndefValue.
520 Constant *C = V[0];
521 bool isZero = C->isNullValue();
522 bool isUndef = isa<UndefValue>(C);
523
524 if (isZero || isUndef) {
525 for (unsigned i = 1, e = V.size(); i != e; ++i)
526 if (V[i] != C) {
527 isZero = isUndef = false;
528 break;
529 }
530 }
531
532 if (isZero)
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000533 return ConstantAggregateZero::get(T);
Owen Andersonaf7ec972009-07-28 21:19:26 +0000534 if (isUndef)
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000535 return UndefValue::get(T);
Owen Andersonaf7ec972009-07-28 21:19:26 +0000536
537 // Implicitly locked.
538 return pImpl->VectorConstants.getOrCreate(T, V);
539}
540
541Constant* ConstantVector::get(const std::vector<Constant*>& V) {
542 assert(!V.empty() && "Cannot infer type if V is empty");
543 return get(VectorType::get(V.front()->getType(),V.size()), V);
544}
545
546Constant* ConstantVector::get(Constant* const* Vals, unsigned NumVals) {
547 // FIXME: make this the primary ctor method.
548 return get(std::vector<Constant*>(Vals, Vals+NumVals));
549}
550
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000551
Gabor Greifefe65362008-05-10 08:32:32 +0000552namespace llvm {
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000553// We declare several classes private to this file, so use an anonymous
554// namespace
555namespace {
Reid Spencer728b6db2006-12-03 05:48:19 +0000556
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000557/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
558/// behind the scenes to implement unary constant exprs.
559class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
Gabor Greif051a9502008-04-06 20:25:17 +0000560 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000561public:
Gabor Greif051a9502008-04-06 20:25:17 +0000562 // allocate space for exactly one operand
563 void *operator new(size_t s) {
564 return User::operator new(s, 1);
565 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000566 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
Gabor Greifefe65362008-05-10 08:32:32 +0000567 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
568 Op<0>() = C;
569 }
570 /// Transparently provide more efficient getOperand methods.
571 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000572};
Reid Spencer728b6db2006-12-03 05:48:19 +0000573
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000574/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
575/// behind the scenes to implement binary constant exprs.
576class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Gabor Greif051a9502008-04-06 20:25:17 +0000577 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000578public:
Gabor Greif051a9502008-04-06 20:25:17 +0000579 // allocate space for exactly two operands
580 void *operator new(size_t s) {
581 return User::operator new(s, 2);
582 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000583 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Gabor Greifefe65362008-05-10 08:32:32 +0000584 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
Gabor Greif6c80c382008-05-26 21:33:52 +0000585 Op<0>() = C1;
586 Op<1>() = C2;
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000587 }
Gabor Greifefe65362008-05-10 08:32:32 +0000588 /// Transparently provide more efficient getOperand methods.
589 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000590};
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000591
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000592/// SelectConstantExpr - This class is private to Constants.cpp, and is used
593/// behind the scenes to implement select constant exprs.
594class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Gabor Greif051a9502008-04-06 20:25:17 +0000595 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000596public:
Gabor Greif051a9502008-04-06 20:25:17 +0000597 // allocate space for exactly three operands
598 void *operator new(size_t s) {
599 return User::operator new(s, 3);
600 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000601 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Gabor Greifefe65362008-05-10 08:32:32 +0000602 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
Gabor Greif6c80c382008-05-26 21:33:52 +0000603 Op<0>() = C1;
604 Op<1>() = C2;
605 Op<2>() = C3;
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000606 }
Gabor Greifefe65362008-05-10 08:32:32 +0000607 /// Transparently provide more efficient getOperand methods.
608 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000609};
Chris Lattnere4671472005-01-29 00:34:39 +0000610
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000611/// ExtractElementConstantExpr - This class is private to
612/// Constants.cpp, and is used behind the scenes to implement
613/// extractelement constant exprs.
614class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Gabor Greif051a9502008-04-06 20:25:17 +0000615 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000616public:
Gabor Greif051a9502008-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 Henriksenafba8fe2007-12-10 02:14:30 +0000621 ExtractElementConstantExpr(Constant *C1, Constant *C2)
622 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
Gabor Greifefe65362008-05-10 08:32:32 +0000623 Instruction::ExtractElement, &Op<0>(), 2) {
Gabor Greif6c80c382008-05-26 21:33:52 +0000624 Op<0>() = C1;
625 Op<1>() = C2;
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000626 }
Gabor Greifefe65362008-05-10 08:32:32 +0000627 /// Transparently provide more efficient getOperand methods.
628 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000629};
Robert Bocchinob52ee7f2006-01-10 19:05:34 +0000630
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000631/// InsertElementConstantExpr - This class is private to
632/// Constants.cpp, and is used behind the scenes to implement
633/// insertelement constant exprs.
634class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Gabor Greif051a9502008-04-06 20:25:17 +0000635 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000636public:
Gabor Greif051a9502008-04-06 20:25:17 +0000637 // allocate space for exactly three operands
638 void *operator new(size_t s) {
639 return User::operator new(s, 3);
640 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000641 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
642 : ConstantExpr(C1->getType(), Instruction::InsertElement,
Gabor Greifefe65362008-05-10 08:32:32 +0000643 &Op<0>(), 3) {
Gabor Greif6c80c382008-05-26 21:33:52 +0000644 Op<0>() = C1;
645 Op<1>() = C2;
646 Op<2>() = C3;
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000647 }
Gabor Greifefe65362008-05-10 08:32:32 +0000648 /// Transparently provide more efficient getOperand methods.
649 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000650};
Robert Bocchinoc152f9c2006-01-17 20:07:22 +0000651
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000652/// ShuffleVectorConstantExpr - This class is private to
653/// Constants.cpp, and is used behind the scenes to implement
654/// shufflevector constant exprs.
655class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Gabor Greif051a9502008-04-06 20:25:17 +0000656 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000657public:
Gabor Greif051a9502008-04-06 20:25:17 +0000658 // allocate space for exactly three operands
659 void *operator new(size_t s) {
660 return User::operator new(s, 3);
661 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000662 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Nate Begeman0f123cf2009-02-12 21:28:33 +0000663 : ConstantExpr(VectorType::get(
664 cast<VectorType>(C1->getType())->getElementType(),
665 cast<VectorType>(C3->getType())->getNumElements()),
666 Instruction::ShuffleVector,
Gabor Greifefe65362008-05-10 08:32:32 +0000667 &Op<0>(), 3) {
Gabor Greif6c80c382008-05-26 21:33:52 +0000668 Op<0>() = C1;
669 Op<1>() = C2;
670 Op<2>() = C3;
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000671 }
Gabor Greifefe65362008-05-10 08:32:32 +0000672 /// Transparently provide more efficient getOperand methods.
673 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000674};
675
Dan Gohman041e2eb2008-05-15 19:50:34 +0000676/// ExtractValueConstantExpr - This class is private to
677/// Constants.cpp, and is used behind the scenes to implement
678/// extractvalue constant exprs.
679class VISIBILITY_HIDDEN ExtractValueConstantExpr : public ConstantExpr {
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000680 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman041e2eb2008-05-15 19:50:34 +0000681public:
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000682 // allocate space for exactly one operand
683 void *operator new(size_t s) {
684 return User::operator new(s, 1);
Dan Gohman041e2eb2008-05-15 19:50:34 +0000685 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000686 ExtractValueConstantExpr(Constant *Agg,
687 const SmallVector<unsigned, 4> &IdxList,
688 const Type *DestTy)
689 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
690 Indices(IdxList) {
691 Op<0>() = Agg;
692 }
693
Dan Gohman35651cd2008-05-31 19:09:08 +0000694 /// Indices - These identify which value to extract.
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000695 const SmallVector<unsigned, 4> Indices;
696
Dan Gohman041e2eb2008-05-15 19:50:34 +0000697 /// Transparently provide more efficient getOperand methods.
698 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
699};
700
701/// InsertValueConstantExpr - This class is private to
702/// Constants.cpp, and is used behind the scenes to implement
703/// insertvalue constant exprs.
704class VISIBILITY_HIDDEN InsertValueConstantExpr : public ConstantExpr {
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000705 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman041e2eb2008-05-15 19:50:34 +0000706public:
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000707 // allocate space for exactly one operand
708 void *operator new(size_t s) {
709 return User::operator new(s, 2);
Dan Gohman041e2eb2008-05-15 19:50:34 +0000710 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000711 InsertValueConstantExpr(Constant *Agg, Constant *Val,
712 const SmallVector<unsigned, 4> &IdxList,
713 const Type *DestTy)
714 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
715 Indices(IdxList) {
716 Op<0>() = Agg;
717 Op<1>() = Val;
718 }
719
Dan Gohman35651cd2008-05-31 19:09:08 +0000720 /// Indices - These identify the position for the insertion.
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000721 const SmallVector<unsigned, 4> Indices;
722
Dan Gohman041e2eb2008-05-15 19:50:34 +0000723 /// Transparently provide more efficient getOperand methods.
724 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
725};
726
727
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000728/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
729/// used behind the scenes to implement getelementpr constant exprs.
Gabor Greif051a9502008-04-06 20:25:17 +0000730class VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000731 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
Gabor Greifefe65362008-05-10 08:32:32 +0000732 const Type *DestTy);
Gabor Greif051a9502008-04-06 20:25:17 +0000733public:
Gabor Greifb1dbcd82008-05-15 10:04:30 +0000734 static GetElementPtrConstantExpr *Create(Constant *C,
735 const std::vector<Constant*>&IdxList,
Gabor Greifefe65362008-05-10 08:32:32 +0000736 const Type *DestTy) {
Dan Gohmanf2411742009-07-20 17:43:30 +0000737 return
738 new(IdxList.size() + 1) GetElementPtrConstantExpr(C, IdxList, DestTy);
Gabor Greif051a9502008-04-06 20:25:17 +0000739 }
Gabor Greifefe65362008-05-10 08:32:32 +0000740 /// Transparently provide more efficient getOperand methods.
741 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000742};
743
744// CompareConstantExpr - This class is private to Constants.cpp, and is used
745// behind the scenes to implement ICmp and FCmp constant expressions. This is
746// needed in order to store the predicate value for these instructions.
747struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr {
Gabor Greif051a9502008-04-06 20:25:17 +0000748 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
749 // allocate space for exactly two operands
750 void *operator new(size_t s) {
751 return User::operator new(s, 2);
752 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000753 unsigned short predicate;
Nate Begemanac80ade2008-05-12 19:01:56 +0000754 CompareConstantExpr(const Type *ty, Instruction::OtherOps opc,
755 unsigned short pred, Constant* LHS, Constant* RHS)
756 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
Gabor Greif6c80c382008-05-26 21:33:52 +0000757 Op<0>() = LHS;
758 Op<1>() = RHS;
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000759 }
Gabor Greifefe65362008-05-10 08:32:32 +0000760 /// Transparently provide more efficient getOperand methods.
761 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000762};
763
764} // end anonymous namespace
765
Gabor Greifefe65362008-05-10 08:32:32 +0000766template <>
767struct OperandTraits<UnaryConstantExpr> : FixedNumOperandTraits<1> {
768};
769DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
770
771template <>
772struct OperandTraits<BinaryConstantExpr> : FixedNumOperandTraits<2> {
773};
774DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
775
776template <>
777struct OperandTraits<SelectConstantExpr> : FixedNumOperandTraits<3> {
778};
779DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
780
781template <>
782struct OperandTraits<ExtractElementConstantExpr> : FixedNumOperandTraits<2> {
783};
784DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
785
786template <>
787struct OperandTraits<InsertElementConstantExpr> : FixedNumOperandTraits<3> {
788};
789DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
790
791template <>
792struct OperandTraits<ShuffleVectorConstantExpr> : FixedNumOperandTraits<3> {
793};
794DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
795
Dan Gohman041e2eb2008-05-15 19:50:34 +0000796template <>
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000797struct OperandTraits<ExtractValueConstantExpr> : FixedNumOperandTraits<1> {
Dan Gohman041e2eb2008-05-15 19:50:34 +0000798};
Dan Gohman041e2eb2008-05-15 19:50:34 +0000799DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
800
801template <>
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000802struct OperandTraits<InsertValueConstantExpr> : FixedNumOperandTraits<2> {
Dan Gohman041e2eb2008-05-15 19:50:34 +0000803};
Dan Gohman041e2eb2008-05-15 19:50:34 +0000804DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
805
Gabor Greifefe65362008-05-10 08:32:32 +0000806template <>
807struct OperandTraits<GetElementPtrConstantExpr> : VariadicOperandTraits<1> {
808};
809
810GetElementPtrConstantExpr::GetElementPtrConstantExpr
811 (Constant *C,
812 const std::vector<Constant*> &IdxList,
813 const Type *DestTy)
814 : ConstantExpr(DestTy, Instruction::GetElementPtr,
815 OperandTraits<GetElementPtrConstantExpr>::op_end(this)
816 - (IdxList.size()+1),
817 IdxList.size()+1) {
Gabor Greif6c80c382008-05-26 21:33:52 +0000818 OperandList[0] = C;
Gabor Greifefe65362008-05-10 08:32:32 +0000819 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
Gabor Greif6c80c382008-05-26 21:33:52 +0000820 OperandList[i+1] = IdxList[i];
Gabor Greifefe65362008-05-10 08:32:32 +0000821}
822
823DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
824
825
826template <>
827struct OperandTraits<CompareConstantExpr> : FixedNumOperandTraits<2> {
828};
829DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
830
831
832} // End llvm namespace
833
Reid Spencer3da59db2006-11-27 01:05:10 +0000834
835// Utility function for determining if a ConstantExpr is a CastOp or not. This
836// can't be inline because we don't want to #include Instruction.h into
837// Constant.h
838bool ConstantExpr::isCast() const {
839 return Instruction::isCast(getOpcode());
840}
841
Reid Spencer077d0eb2006-12-04 05:19:50 +0000842bool ConstantExpr::isCompare() const {
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +0000843 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
Reid Spencer077d0eb2006-12-04 05:19:50 +0000844}
845
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000846bool ConstantExpr::hasIndices() const {
847 return getOpcode() == Instruction::ExtractValue ||
848 getOpcode() == Instruction::InsertValue;
849}
850
851const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const {
852 if (const ExtractValueConstantExpr *EVCE =
853 dyn_cast<ExtractValueConstantExpr>(this))
854 return EVCE->Indices;
Dan Gohman1a203572008-06-23 16:39:44 +0000855
856 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000857}
858
Reid Spencer728b6db2006-12-03 05:48:19 +0000859unsigned ConstantExpr::getPredicate() const {
Nate Begemanac80ade2008-05-12 19:01:56 +0000860 assert(getOpcode() == Instruction::FCmp ||
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +0000861 getOpcode() == Instruction::ICmp);
Chris Lattnerb7daa842007-10-18 16:26:24 +0000862 return ((const CompareConstantExpr*)this)->predicate;
Reid Spencer728b6db2006-12-03 05:48:19 +0000863}
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000864
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000865/// getWithOperandReplaced - Return a constant expression identical to this
866/// one, but with the specified operand set to the specified value.
Reid Spencer3da59db2006-11-27 01:05:10 +0000867Constant *
868ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000869 assert(OpNo < getNumOperands() && "Operand num is out of range!");
870 assert(Op->getType() == getOperand(OpNo)->getType() &&
871 "Replacing operand with value of different type!");
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000872 if (getOperand(OpNo) == Op)
873 return const_cast<ConstantExpr*>(this);
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000874
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000875 Constant *Op0, *Op1, *Op2;
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000876 switch (getOpcode()) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000877 case Instruction::Trunc:
878 case Instruction::ZExt:
879 case Instruction::SExt:
880 case Instruction::FPTrunc:
881 case Instruction::FPExt:
882 case Instruction::UIToFP:
883 case Instruction::SIToFP:
884 case Instruction::FPToUI:
885 case Instruction::FPToSI:
886 case Instruction::PtrToInt:
887 case Instruction::IntToPtr:
888 case Instruction::BitCast:
889 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000890 case Instruction::Select:
891 Op0 = (OpNo == 0) ? Op : getOperand(0);
892 Op1 = (OpNo == 1) ? Op : getOperand(1);
893 Op2 = (OpNo == 2) ? Op : getOperand(2);
894 return ConstantExpr::getSelect(Op0, Op1, Op2);
895 case Instruction::InsertElement:
896 Op0 = (OpNo == 0) ? Op : getOperand(0);
897 Op1 = (OpNo == 1) ? Op : getOperand(1);
898 Op2 = (OpNo == 2) ? Op : getOperand(2);
899 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
900 case Instruction::ExtractElement:
901 Op0 = (OpNo == 0) ? Op : getOperand(0);
902 Op1 = (OpNo == 1) ? Op : getOperand(1);
903 return ConstantExpr::getExtractElement(Op0, Op1);
904 case Instruction::ShuffleVector:
905 Op0 = (OpNo == 0) ? Op : getOperand(0);
906 Op1 = (OpNo == 1) ? Op : getOperand(1);
907 Op2 = (OpNo == 2) ? Op : getOperand(2);
908 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000909 case Instruction::GetElementPtr: {
Chris Lattnerf9021ff2007-02-19 20:01:23 +0000910 SmallVector<Constant*, 8> Ops;
Dan Gohman041e2eb2008-05-15 19:50:34 +0000911 Ops.resize(getNumOperands()-1);
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000912 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Dan Gohman041e2eb2008-05-15 19:50:34 +0000913 Ops[i-1] = getOperand(i);
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000914 if (OpNo == 0)
Chris Lattnerf9021ff2007-02-19 20:01:23 +0000915 return ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000916 Ops[OpNo-1] = Op;
Chris Lattnerf9021ff2007-02-19 20:01:23 +0000917 return ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000918 }
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000919 default:
920 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000921 Op0 = (OpNo == 0) ? Op : getOperand(0);
922 Op1 = (OpNo == 1) ? Op : getOperand(1);
923 return ConstantExpr::get(getOpcode(), Op0, Op1);
924 }
925}
926
927/// getWithOperands - This returns the current constant expression with the
928/// operands replaced with the specified values. The specified operands must
929/// match count and type with the existing ones.
930Constant *ConstantExpr::
Chris Lattnerb054bfd2008-08-20 22:27:40 +0000931getWithOperands(Constant* const *Ops, unsigned NumOps) const {
932 assert(NumOps == getNumOperands() && "Operand count mismatch!");
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000933 bool AnyChange = false;
Chris Lattnerb054bfd2008-08-20 22:27:40 +0000934 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000935 assert(Ops[i]->getType() == getOperand(i)->getType() &&
936 "Operand type mismatch!");
937 AnyChange |= Ops[i] != getOperand(i);
938 }
939 if (!AnyChange) // No operands changed, return self.
940 return const_cast<ConstantExpr*>(this);
941
942 switch (getOpcode()) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000943 case Instruction::Trunc:
944 case Instruction::ZExt:
945 case Instruction::SExt:
946 case Instruction::FPTrunc:
947 case Instruction::FPExt:
948 case Instruction::UIToFP:
949 case Instruction::SIToFP:
950 case Instruction::FPToUI:
951 case Instruction::FPToSI:
952 case Instruction::PtrToInt:
953 case Instruction::IntToPtr:
954 case Instruction::BitCast:
955 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000956 case Instruction::Select:
957 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
958 case Instruction::InsertElement:
959 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
960 case Instruction::ExtractElement:
961 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
962 case Instruction::ShuffleVector:
963 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Chris Lattnerf9021ff2007-02-19 20:01:23 +0000964 case Instruction::GetElementPtr:
Chris Lattnerb054bfd2008-08-20 22:27:40 +0000965 return ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], NumOps-1);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000966 case Instruction::ICmp:
967 case Instruction::FCmp:
968 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000969 default:
970 assert(getNumOperands() == 2 && "Must be binary operator?");
971 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000972 }
973}
974
Chris Lattner00950542001-06-06 20:29:01 +0000975
976//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +0000977// isValueValidForType implementations
978
Reid Spencer9b11d512006-12-19 01:28:19 +0000979bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000980 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencera54b7cb2007-01-12 07:05:14 +0000981 if (Ty == Type::Int1Ty)
982 return Val == 0 || Val == 1;
Reid Spencer554cec62007-02-05 23:47:56 +0000983 if (NumBits >= 64)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000984 return true; // always true, has to fit in largest type
985 uint64_t Max = (1ll << NumBits) - 1;
986 return Val <= Max;
Reid Spencer9b11d512006-12-19 01:28:19 +0000987}
988
Reid Spencerb83eb642006-10-20 07:07:24 +0000989bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000990 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencera54b7cb2007-01-12 07:05:14 +0000991 if (Ty == Type::Int1Ty)
Reid Spencerc1030572007-01-19 21:13:56 +0000992 return Val == 0 || Val == 1 || Val == -1;
Reid Spencer554cec62007-02-05 23:47:56 +0000993 if (NumBits >= 64)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000994 return true; // always true, has to fit in largest type
995 int64_t Min = -(1ll << (NumBits-1));
996 int64_t Max = (1ll << (NumBits-1)) - 1;
997 return (Val >= Min && Val <= Max);
Chris Lattner00950542001-06-06 20:29:01 +0000998}
999
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001000bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) {
1001 // convert modifies in place, so make a copy.
1002 APFloat Val2 = APFloat(Val);
Dale Johannesen23a98552008-10-09 23:00:39 +00001003 bool losesInfo;
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001004 switch (Ty->getTypeID()) {
Chris Lattner00950542001-06-06 20:29:01 +00001005 default:
1006 return false; // These can't be represented as floating point!
1007
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001008 // FIXME rounding mode needs to be more flexible
Dale Johannesen23a98552008-10-09 23:00:39 +00001009 case Type::FloatTyID: {
1010 if (&Val2.getSemantics() == &APFloat::IEEEsingle)
1011 return true;
1012 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
1013 return !losesInfo;
1014 }
1015 case Type::DoubleTyID: {
1016 if (&Val2.getSemantics() == &APFloat::IEEEsingle ||
1017 &Val2.getSemantics() == &APFloat::IEEEdouble)
1018 return true;
1019 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
1020 return !losesInfo;
1021 }
Dale Johannesenebbc95d2007-08-09 22:51:36 +00001022 case Type::X86_FP80TyID:
Dale Johannesen9d5f4562007-09-12 03:30:33 +00001023 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
1024 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1025 &Val2.getSemantics() == &APFloat::x87DoubleExtended;
Dale Johannesenebbc95d2007-08-09 22:51:36 +00001026 case Type::FP128TyID:
Dale Johannesen9d5f4562007-09-12 03:30:33 +00001027 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
1028 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1029 &Val2.getSemantics() == &APFloat::IEEEquad;
Dale Johannesena471c2e2007-10-11 18:07:22 +00001030 case Type::PPC_FP128TyID:
1031 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
1032 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1033 &Val2.getSemantics() == &APFloat::PPCDoubleDouble;
Chris Lattner00950542001-06-06 20:29:01 +00001034 }
Chris Lattnerd74ea2b2006-05-24 17:04:05 +00001035}
Chris Lattner37bf6302001-07-20 19:16:02 +00001036
Chris Lattner531daef2001-09-07 16:46:31 +00001037//===----------------------------------------------------------------------===//
Chris Lattner531daef2001-09-07 16:46:31 +00001038// Factory Function Implementation
1039
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001040static char getValType(ConstantAggregateZero *CPZ) { return 0; }
1041
1042ConstantAggregateZero* ConstantAggregateZero::get(const Type* Ty) {
1043 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) &&
1044 "Cannot create an aggregate zero of non-aggregate type!");
1045
1046 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
1047 // Implicitly locked.
1048 return pImpl->AggZeroConstants.getOrCreate(Ty, 0);
1049}
1050
Dan Gohman0f8b53f2009-03-03 02:55:14 +00001051/// destroyConstant - Remove the constant from the constant table...
1052///
Owen Anderson04fb7c32009-06-20 00:24:58 +00001053void ConstantAggregateZero::destroyConstant() {
Owen Anderson31c36f02009-06-17 20:10:08 +00001054 // Implicitly locked.
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001055 getType()->getContext().pImpl->AggZeroConstants.remove(this);
Chris Lattner40bbeb52004-02-15 05:53:04 +00001056 destroyConstantImpl();
1057}
1058
Dan Gohman0f8b53f2009-03-03 02:55:14 +00001059/// destroyConstant - Remove the constant from the constant table...
1060///
Owen Anderson04fb7c32009-06-20 00:24:58 +00001061void ConstantArray::destroyConstant() {
Owen Anderson1f0ba8c2009-06-19 18:34:09 +00001062 // Implicitly locked.
Owen Anderson1fd70962009-07-28 18:32:17 +00001063 getType()->getContext().pImpl->ArrayConstants.remove(this);
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001064 destroyConstantImpl();
1065}
1066
Reid Spencer3d10b0b2007-01-26 07:37:34 +00001067/// isString - This method returns true if the array is an array of i8, and
1068/// if the elements of the array are all ConstantInt's.
Chris Lattner13cfdea2004-01-14 17:06:38 +00001069bool ConstantArray::isString() const {
Reid Spencer3d10b0b2007-01-26 07:37:34 +00001070 // Check the element type for i8...
Reid Spencer79e21d32006-12-31 05:26:44 +00001071 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattner13cfdea2004-01-14 17:06:38 +00001072 return false;
1073 // Check the elements to make sure they are all integers, not constant
1074 // expressions.
1075 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1076 if (!isa<ConstantInt>(getOperand(i)))
1077 return false;
1078 return true;
1079}
1080
Evan Cheng22c70302006-10-26 19:15:05 +00001081/// isCString - This method returns true if the array is a string (see
Dan Gohman0f8b53f2009-03-03 02:55:14 +00001082/// isString) and it ends in a null byte \\0 and does not contains any other
Evan Cheng22c70302006-10-26 19:15:05 +00001083/// null bytes except its terminator.
Owen Anderson1ca29d32009-07-13 21:27:19 +00001084bool ConstantArray::isCString() const {
Reid Spencer3d10b0b2007-01-26 07:37:34 +00001085 // Check the element type for i8...
Reid Spencer79e21d32006-12-31 05:26:44 +00001086 if (getType()->getElementType() != Type::Int8Ty)
Evan Chengabf63452006-10-26 21:48:03 +00001087 return false;
Owen Anderson1ca29d32009-07-13 21:27:19 +00001088
Evan Chengabf63452006-10-26 21:48:03 +00001089 // Last element must be a null.
Owen Anderson1ca29d32009-07-13 21:27:19 +00001090 if (!getOperand(getNumOperands()-1)->isNullValue())
Evan Chengabf63452006-10-26 21:48:03 +00001091 return false;
1092 // Other elements must be non-null integers.
1093 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1094 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng22c70302006-10-26 19:15:05 +00001095 return false;
Owen Anderson1ca29d32009-07-13 21:27:19 +00001096 if (getOperand(i)->isNullValue())
Evan Chengabf63452006-10-26 21:48:03 +00001097 return false;
1098 }
Evan Cheng22c70302006-10-26 19:15:05 +00001099 return true;
1100}
1101
1102
Dan Gohman0f8b53f2009-03-03 02:55:14 +00001103/// getAsString - If the sub-element type of this array is i8
1104/// then this method converts the array to an std::string and returns it.
1105/// Otherwise, it asserts out.
1106///
Chris Lattner93aeea32002-08-26 17:53:56 +00001107std::string ConstantArray::getAsString() const {
Chris Lattner13cfdea2004-01-14 17:06:38 +00001108 assert(isString() && "Not a string!");
Chris Lattner93aeea32002-08-26 17:53:56 +00001109 std::string Result;
Owen Anderson45e39582008-06-24 21:58:29 +00001110 Result.reserve(getNumOperands());
Chris Lattnerc07736a2003-07-23 15:22:26 +00001111 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersone4f6ee52008-06-25 01:05:05 +00001112 Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue());
Chris Lattner93aeea32002-08-26 17:53:56 +00001113 return Result;
1114}
1115
1116
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001117//---- ConstantStruct::get() implementation...
Chris Lattner531daef2001-09-07 16:46:31 +00001118//
Chris Lattnered468e372003-10-05 00:17:43 +00001119
Chris Lattner31f84992003-11-21 20:23:48 +00001120namespace llvm {
Misha Brukmanfd939082005-04-21 23:48:37 +00001121
Chris Lattner531daef2001-09-07 16:46:31 +00001122}
Chris Lattner6a57baa2001-10-03 15:39:36 +00001123
Chris Lattnerf5ec48d2001-10-13 06:57:33 +00001124// destroyConstant - Remove the constant from the constant table...
Chris Lattner6a57baa2001-10-03 15:39:36 +00001125//
Owen Anderson04fb7c32009-06-20 00:24:58 +00001126void ConstantStruct::destroyConstant() {
Owen Anderson1f0ba8c2009-06-19 18:34:09 +00001127 // Implicitly locked.
Owen Anderson8fa33382009-07-27 22:29:26 +00001128 getType()->getContext().pImpl->StructConstants.remove(this);
Chris Lattnerf5ec48d2001-10-13 06:57:33 +00001129 destroyConstantImpl();
1130}
Chris Lattner6a57baa2001-10-03 15:39:36 +00001131
Brian Gaeke715c90b2004-08-20 06:00:58 +00001132// destroyConstant - Remove the constant from the constant table...
1133//
Owen Anderson04fb7c32009-06-20 00:24:58 +00001134void ConstantVector::destroyConstant() {
Owen Anderson1f0ba8c2009-06-19 18:34:09 +00001135 // Implicitly locked.
Owen Andersonaf7ec972009-07-28 21:19:26 +00001136 getType()->getContext().pImpl->VectorConstants.remove(this);
Brian Gaeke715c90b2004-08-20 06:00:58 +00001137 destroyConstantImpl();
1138}
1139
Dan Gohmanfa73ea22007-05-24 14:36:04 +00001140/// This function will return true iff every element in this vector constant
Jim Laskeyfa301822007-01-12 22:39:14 +00001141/// is set to all ones.
1142/// @returns true iff this constant's emements are all set to all ones.
1143/// @brief Determine if the value is all ones.
Reid Spencer9d6565a2007-02-15 02:26:10 +00001144bool ConstantVector::isAllOnesValue() const {
Jim Laskeyfa301822007-01-12 22:39:14 +00001145 // Check out first element.
1146 const Constant *Elt = getOperand(0);
1147 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1148 if (!CI || !CI->isAllOnesValue()) return false;
1149 // Then make sure all remaining elements point to the same value.
1150 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1151 if (getOperand(I) != Elt) return false;
1152 }
1153 return true;
1154}
1155
Dan Gohman3b7cf0a2007-10-17 17:51:30 +00001156/// getSplatValue - If this is a splat constant, where all of the
1157/// elements have the same value, return that value. Otherwise return null.
1158Constant *ConstantVector::getSplatValue() {
1159 // Check out first element.
1160 Constant *Elt = getOperand(0);
1161 // Then make sure all remaining elements point to the same value.
1162 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
1163 if (getOperand(I) != Elt) return 0;
1164 return Elt;
1165}
1166
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001167//---- ConstantPointerNull::get() implementation...
Chris Lattnerf5ec48d2001-10-13 06:57:33 +00001168//
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001169
Chris Lattner31f84992003-11-21 20:23:48 +00001170namespace llvm {
1171 // ConstantPointerNull does not take extra "value" argument...
1172 template<class ValType>
1173 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1174 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1175 return new ConstantPointerNull(Ty);
1176 }
1177 };
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001178
Chris Lattner31f84992003-11-21 20:23:48 +00001179 template<>
1180 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1181 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1182 // Make everyone now use a constant of the new type...
Owen Anderson04fb7c32009-06-20 00:24:58 +00001183 Constant *New = ConstantPointerNull::get(NewTy);
Chris Lattner31f84992003-11-21 20:23:48 +00001184 assert(New != OldC && "Didn't replace constant??");
1185 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson04fb7c32009-06-20 00:24:58 +00001186 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner31f84992003-11-21 20:23:48 +00001187 }
1188 };
1189}
Chris Lattnered468e372003-10-05 00:17:43 +00001190
Chris Lattner8a94bf12006-09-28 00:35:06 +00001191static ManagedStatic<ValueMap<char, PointerType,
1192 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerf5ec48d2001-10-13 06:57:33 +00001193
Chris Lattner6823c9f2004-08-04 04:48:01 +00001194static char getValType(ConstantPointerNull *) {
1195 return 0;
1196}
1197
1198
Owen Anderson04fb7c32009-06-20 00:24:58 +00001199ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Owen Anderson31c36f02009-06-17 20:10:08 +00001200 // Implicitly locked.
Owen Anderson04fb7c32009-06-20 00:24:58 +00001201 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner6a57baa2001-10-03 15:39:36 +00001202}
1203
Chris Lattner41661fd2002-08-18 00:40:04 +00001204// destroyConstant - Remove the constant from the constant table...
1205//
Owen Anderson04fb7c32009-06-20 00:24:58 +00001206void ConstantPointerNull::destroyConstant() {
Owen Anderson1f0ba8c2009-06-19 18:34:09 +00001207 // Implicitly locked.
Owen Anderson04fb7c32009-06-20 00:24:58 +00001208 NullPtrConstants->remove(this);
Chris Lattner41661fd2002-08-18 00:40:04 +00001209 destroyConstantImpl();
1210}
1211
1212
Chris Lattnerb9f18592004-10-16 18:07:16 +00001213//---- UndefValue::get() implementation...
1214//
1215
1216namespace llvm {
1217 // UndefValue does not take extra "value" argument...
1218 template<class ValType>
1219 struct ConstantCreator<UndefValue, Type, ValType> {
1220 static UndefValue *create(const Type *Ty, const ValType &V) {
1221 return new UndefValue(Ty);
1222 }
1223 };
1224
1225 template<>
1226 struct ConvertConstantType<UndefValue, Type> {
1227 static void convert(UndefValue *OldC, const Type *NewTy) {
1228 // Make everyone now use a constant of the new type.
Owen Anderson04fb7c32009-06-20 00:24:58 +00001229 Constant *New = UndefValue::get(NewTy);
Chris Lattnerb9f18592004-10-16 18:07:16 +00001230 assert(New != OldC && "Didn't replace constant??");
1231 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson04fb7c32009-06-20 00:24:58 +00001232 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattnerb9f18592004-10-16 18:07:16 +00001233 }
1234 };
1235}
1236
Chris Lattner8a94bf12006-09-28 00:35:06 +00001237static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerb9f18592004-10-16 18:07:16 +00001238
1239static char getValType(UndefValue *) {
1240 return 0;
1241}
1242
1243
Owen Anderson04fb7c32009-06-20 00:24:58 +00001244UndefValue *UndefValue::get(const Type *Ty) {
1245 // Implicitly locked.
1246 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerb9f18592004-10-16 18:07:16 +00001247}
1248
1249// destroyConstant - Remove the constant from the constant table.
1250//
Owen Anderson04fb7c32009-06-20 00:24:58 +00001251void UndefValue::destroyConstant() {
Owen Anderson31c36f02009-06-17 20:10:08 +00001252 // Implicitly locked.
Owen Anderson04fb7c32009-06-20 00:24:58 +00001253 UndefValueConstants->remove(this);
Chris Lattnerb9f18592004-10-16 18:07:16 +00001254 destroyConstantImpl();
1255}
1256
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001257//---- ConstantExpr::get() implementations...
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001258//
Reid Spencer79e21d32006-12-31 05:26:44 +00001259
Dan Gohman844731a2008-05-13 00:00:25 +00001260namespace {
1261
Reid Spencer077d0eb2006-12-04 05:19:50 +00001262struct ExprMapKeyType {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001263 typedef SmallVector<unsigned, 4> IndexList;
1264
1265 ExprMapKeyType(unsigned opc,
1266 const std::vector<Constant*> &ops,
1267 unsigned short pred = 0,
1268 const IndexList &inds = IndexList())
1269 : opcode(opc), predicate(pred), operands(ops), indices(inds) {}
Reid Spencer8d5a6ae2006-12-04 18:38:05 +00001270 uint16_t opcode;
1271 uint16_t predicate;
Reid Spencer077d0eb2006-12-04 05:19:50 +00001272 std::vector<Constant*> operands;
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001273 IndexList indices;
Reid Spencer077d0eb2006-12-04 05:19:50 +00001274 bool operator==(const ExprMapKeyType& that) const {
1275 return this->opcode == that.opcode &&
1276 this->predicate == that.predicate &&
Bill Wendling9a20afd2008-10-26 00:19:56 +00001277 this->operands == that.operands &&
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001278 this->indices == that.indices;
Reid Spencer077d0eb2006-12-04 05:19:50 +00001279 }
1280 bool operator<(const ExprMapKeyType & that) const {
1281 return this->opcode < that.opcode ||
1282 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1283 (this->opcode == that.opcode && this->predicate == that.predicate &&
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001284 this->operands < that.operands) ||
1285 (this->opcode == that.opcode && this->predicate == that.predicate &&
1286 this->operands == that.operands && this->indices < that.indices);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001287 }
1288
1289 bool operator!=(const ExprMapKeyType& that) const {
1290 return !(*this == that);
1291 }
1292};
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001293
Dan Gohman844731a2008-05-13 00:00:25 +00001294}
1295
Chris Lattner31f84992003-11-21 20:23:48 +00001296namespace llvm {
1297 template<>
1298 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer728b6db2006-12-03 05:48:19 +00001299 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1300 unsigned short pred = 0) {
Reid Spencer077d0eb2006-12-04 05:19:50 +00001301 if (Instruction::isCast(V.opcode))
1302 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1303 if ((V.opcode >= Instruction::BinaryOpsBegin &&
Reid Spencer832254e2007-02-02 02:16:23 +00001304 V.opcode < Instruction::BinaryOpsEnd))
Reid Spencer077d0eb2006-12-04 05:19:50 +00001305 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1306 if (V.opcode == Instruction::Select)
1307 return new SelectConstantExpr(V.operands[0], V.operands[1],
1308 V.operands[2]);
1309 if (V.opcode == Instruction::ExtractElement)
1310 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1311 if (V.opcode == Instruction::InsertElement)
1312 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1313 V.operands[2]);
1314 if (V.opcode == Instruction::ShuffleVector)
1315 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1316 V.operands[2]);
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001317 if (V.opcode == Instruction::InsertValue)
1318 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
1319 V.indices, Ty);
1320 if (V.opcode == Instruction::ExtractValue)
1321 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001322 if (V.opcode == Instruction::GetElementPtr) {
1323 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
Gabor Greif051a9502008-04-06 20:25:17 +00001324 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001325 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001326
Reid Spencer077d0eb2006-12-04 05:19:50 +00001327 // The compare instructions are weird. We have to encode the predicate
1328 // value and it is combined with the instruction opcode by multiplying
1329 // the opcode by one hundred. We must decode this to get the predicate.
1330 if (V.opcode == Instruction::ICmp)
Nate Begemanac80ade2008-05-12 19:01:56 +00001331 return new CompareConstantExpr(Ty, Instruction::ICmp, V.predicate,
Reid Spencer077d0eb2006-12-04 05:19:50 +00001332 V.operands[0], V.operands[1]);
1333 if (V.opcode == Instruction::FCmp)
Nate Begemanac80ade2008-05-12 19:01:56 +00001334 return new CompareConstantExpr(Ty, Instruction::FCmp, V.predicate,
1335 V.operands[0], V.operands[1]);
Torok Edwinc23197a2009-07-14 16:55:14 +00001336 llvm_unreachable("Invalid ConstantExpr!");
Jeff Cohen6ebe2352006-12-15 21:47:01 +00001337 return 0;
Chris Lattnered468e372003-10-05 00:17:43 +00001338 }
Chris Lattner31f84992003-11-21 20:23:48 +00001339 };
Chris Lattnered468e372003-10-05 00:17:43 +00001340
Chris Lattner31f84992003-11-21 20:23:48 +00001341 template<>
1342 struct ConvertConstantType<ConstantExpr, Type> {
1343 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1344 Constant *New;
1345 switch (OldC->getOpcode()) {
Reid Spencer3da59db2006-11-27 01:05:10 +00001346 case Instruction::Trunc:
1347 case Instruction::ZExt:
1348 case Instruction::SExt:
1349 case Instruction::FPTrunc:
1350 case Instruction::FPExt:
1351 case Instruction::UIToFP:
1352 case Instruction::SIToFP:
1353 case Instruction::FPToUI:
1354 case Instruction::FPToSI:
1355 case Instruction::PtrToInt:
1356 case Instruction::IntToPtr:
1357 case Instruction::BitCast:
Reid Spencerd977d862006-12-12 23:36:14 +00001358 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson04fb7c32009-06-20 00:24:58 +00001359 NewTy);
Chris Lattner31f84992003-11-21 20:23:48 +00001360 break;
Chris Lattner08a45cc2004-03-12 05:54:04 +00001361 case Instruction::Select:
1362 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1363 OldC->getOperand(1),
Owen Anderson04fb7c32009-06-20 00:24:58 +00001364 OldC->getOperand(2));
Chris Lattner08a45cc2004-03-12 05:54:04 +00001365 break;
Chris Lattner31f84992003-11-21 20:23:48 +00001366 default:
1367 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer0a783f72006-11-02 01:53:59 +00001368 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner31f84992003-11-21 20:23:48 +00001369 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson04fb7c32009-06-20 00:24:58 +00001370 OldC->getOperand(1));
Chris Lattner31f84992003-11-21 20:23:48 +00001371 break;
1372 case Instruction::GetElementPtr:
Misha Brukmanfd939082005-04-21 23:48:37 +00001373 // Make everyone now use a constant of the new type...
Chris Lattner7fa6e662004-10-11 22:52:25 +00001374 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001375 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
Owen Anderson04fb7c32009-06-20 00:24:58 +00001376 &Idx[0], Idx.size());
Chris Lattner31f84992003-11-21 20:23:48 +00001377 break;
1378 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001379
Chris Lattner31f84992003-11-21 20:23:48 +00001380 assert(New != OldC && "Didn't replace constant??");
1381 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson04fb7c32009-06-20 00:24:58 +00001382 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner31f84992003-11-21 20:23:48 +00001383 }
1384 };
1385} // end namespace llvm
Chris Lattnered468e372003-10-05 00:17:43 +00001386
1387
Chris Lattner6823c9f2004-08-04 04:48:01 +00001388static ExprMapKeyType getValType(ConstantExpr *CE) {
1389 std::vector<Constant*> Operands;
1390 Operands.reserve(CE->getNumOperands());
1391 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1392 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spencer077d0eb2006-12-04 05:19:50 +00001393 return ExprMapKeyType(CE->getOpcode(), Operands,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001394 CE->isCompare() ? CE->getPredicate() : 0,
1395 CE->hasIndices() ?
1396 CE->getIndices() : SmallVector<unsigned, 4>());
Chris Lattner6823c9f2004-08-04 04:48:01 +00001397}
1398
Chris Lattner8a94bf12006-09-28 00:35:06 +00001399static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1400 ConstantExpr> > ExprConstants;
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001401
Reid Spencer3da59db2006-11-27 01:05:10 +00001402/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands66a1a052008-03-30 19:38:55 +00001403/// cast in the ExprConstants map. It is used by the various get* methods below.
Reid Spencer3da59db2006-11-27 01:05:10 +00001404static inline Constant *getFoldedCast(
Owen Anderson04fb7c32009-06-20 00:24:58 +00001405 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner9eacf8a2003-10-07 22:19:19 +00001406 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer3da59db2006-11-27 01:05:10 +00001407 // Fold a few common cases
Owen Anderson5defacc2009-07-31 17:39:07 +00001408 if (Constant *FC = ConstantFoldCastInstruction(Ty->getContext(), opc, C, Ty))
Reid Spencer3da59db2006-11-27 01:05:10 +00001409 return FC;
Chris Lattnerd628f6a2003-04-17 19:24:48 +00001410
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001411 // Look up the constant in the table first to ensure uniqueness
Chris Lattner9bc02a42003-05-13 21:37:02 +00001412 std::vector<Constant*> argVec(1, C);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001413 ExprMapKeyType Key(opc, argVec);
Owen Anderson3e456ab2009-06-17 18:40:29 +00001414
Owen Anderson31c36f02009-06-17 20:10:08 +00001415 // Implicitly locked.
Owen Anderson04fb7c32009-06-20 00:24:58 +00001416 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001417}
Reid Spencer7858b332006-12-05 19:14:13 +00001418
Owen Anderson04fb7c32009-06-20 00:24:58 +00001419Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
Reid Spencer3da59db2006-11-27 01:05:10 +00001420 Instruction::CastOps opc = Instruction::CastOps(oc);
1421 assert(Instruction::isCast(opc) && "opcode out of range");
1422 assert(C && Ty && "Null arguments to getCast");
1423 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1424
1425 switch (opc) {
1426 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00001427 llvm_unreachable("Invalid cast opcode");
Reid Spencer3da59db2006-11-27 01:05:10 +00001428 break;
Owen Anderson04fb7c32009-06-20 00:24:58 +00001429 case Instruction::Trunc: return getTrunc(C, Ty);
1430 case Instruction::ZExt: return getZExt(C, Ty);
1431 case Instruction::SExt: return getSExt(C, Ty);
1432 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1433 case Instruction::FPExt: return getFPExtend(C, Ty);
1434 case Instruction::UIToFP: return getUIToFP(C, Ty);
1435 case Instruction::SIToFP: return getSIToFP(C, Ty);
1436 case Instruction::FPToUI: return getFPToUI(C, Ty);
1437 case Instruction::FPToSI: return getFPToSI(C, Ty);
1438 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1439 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1440 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattnerf5ac6c22005-01-01 15:59:57 +00001441 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001442 return 0;
Reid Spencer7858b332006-12-05 19:14:13 +00001443}
1444
Reid Spencer848414e2006-12-04 20:17:56 +00001445Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001446 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer848414e2006-12-04 20:17:56 +00001447 return getCast(Instruction::BitCast, C, Ty);
1448 return getCast(Instruction::ZExt, C, Ty);
1449}
1450
Owen Anderson04fb7c32009-06-20 00:24:58 +00001451Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001452 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Owen Anderson04fb7c32009-06-20 00:24:58 +00001453 return getCast(Instruction::BitCast, C, Ty);
1454 return getCast(Instruction::SExt, C, Ty);
Reid Spencer848414e2006-12-04 20:17:56 +00001455}
1456
1457Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001458 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer848414e2006-12-04 20:17:56 +00001459 return getCast(Instruction::BitCast, C, Ty);
1460 return getCast(Instruction::Trunc, C, Ty);
1461}
1462
Owen Anderson04fb7c32009-06-20 00:24:58 +00001463Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
Reid Spencerc0459fb2006-12-05 03:25:26 +00001464 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner42a75512007-01-15 02:27:26 +00001465 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerc0459fb2006-12-05 03:25:26 +00001466
Chris Lattner42a75512007-01-15 02:27:26 +00001467 if (Ty->isInteger())
Owen Anderson04fb7c32009-06-20 00:24:58 +00001468 return getCast(Instruction::PtrToInt, S, Ty);
1469 return getCast(Instruction::BitCast, S, Ty);
Reid Spencerc0459fb2006-12-05 03:25:26 +00001470}
1471
Reid Spencer84f3eab2006-12-12 00:51:07 +00001472Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1473 bool isSigned) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001474 assert(C->getType()->isIntOrIntVector() &&
1475 Ty->isIntOrIntVector() && "Invalid cast");
1476 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1477 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer84f3eab2006-12-12 00:51:07 +00001478 Instruction::CastOps opcode =
1479 (SrcBits == DstBits ? Instruction::BitCast :
1480 (SrcBits > DstBits ? Instruction::Trunc :
1481 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1482 return getCast(opcode, C, Ty);
1483}
1484
1485Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001486 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer84f3eab2006-12-12 00:51:07 +00001487 "Invalid cast");
Dan Gohman6de29f82009-06-15 22:12:54 +00001488 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1489 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencerf25212a2006-12-12 05:38:50 +00001490 if (SrcBits == DstBits)
1491 return C; // Avoid a useless cast
Reid Spencer84f3eab2006-12-12 00:51:07 +00001492 Instruction::CastOps opcode =
Reid Spencerf25212a2006-12-12 05:38:50 +00001493 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer84f3eab2006-12-12 00:51:07 +00001494 return getCast(opcode, C, Ty);
1495}
1496
Owen Anderson04fb7c32009-06-20 00:24:58 +00001497Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001498#ifndef NDEBUG
1499 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1500 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1501#endif
1502 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1503 assert(C->getType()->isIntOrIntVector() && "Trunc operand must be integer");
1504 assert(Ty->isIntOrIntVector() && "Trunc produces only integral");
1505 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer3da59db2006-11-27 01:05:10 +00001506 "SrcTy must be larger than DestTy for Trunc!");
1507
Owen Anderson04fb7c32009-06-20 00:24:58 +00001508 return getFoldedCast(Instruction::Trunc, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001509}
1510
Owen Anderson04fb7c32009-06-20 00:24:58 +00001511Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001512#ifndef NDEBUG
1513 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1514 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1515#endif
1516 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1517 assert(C->getType()->isIntOrIntVector() && "SExt operand must be integral");
1518 assert(Ty->isIntOrIntVector() && "SExt produces only integer");
1519 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer3da59db2006-11-27 01:05:10 +00001520 "SrcTy must be smaller than DestTy for SExt!");
1521
Owen Anderson04fb7c32009-06-20 00:24:58 +00001522 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerd144f422004-04-04 23:20:30 +00001523}
1524
Owen Anderson04fb7c32009-06-20 00:24:58 +00001525Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001526#ifndef NDEBUG
1527 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1528 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1529#endif
1530 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1531 assert(C->getType()->isIntOrIntVector() && "ZEXt operand must be integral");
1532 assert(Ty->isIntOrIntVector() && "ZExt produces only integer");
1533 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer3da59db2006-11-27 01:05:10 +00001534 "SrcTy must be smaller than DestTy for ZExt!");
1535
Owen Anderson04fb7c32009-06-20 00:24:58 +00001536 return getFoldedCast(Instruction::ZExt, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001537}
1538
Owen Anderson04fb7c32009-06-20 00:24:58 +00001539Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001540#ifndef NDEBUG
1541 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1542 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1543#endif
1544 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1545 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1546 C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer3da59db2006-11-27 01:05:10 +00001547 "This is an illegal floating point truncation!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001548 return getFoldedCast(Instruction::FPTrunc, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001549}
1550
Owen Anderson04fb7c32009-06-20 00:24:58 +00001551Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001552#ifndef NDEBUG
1553 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1554 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1555#endif
1556 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1557 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1558 C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer3da59db2006-11-27 01:05:10 +00001559 "This is an illegal floating point extension!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001560 return getFoldedCast(Instruction::FPExt, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001561}
1562
Owen Anderson04fb7c32009-06-20 00:24:58 +00001563Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Devang Patelb6dc9352008-11-03 23:20:04 +00001564#ifndef NDEBUG
Nate Begemanb348d182007-11-17 03:58:34 +00001565 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1566 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Patelb6dc9352008-11-03 23:20:04 +00001567#endif
Nate Begemanb348d182007-11-17 03:58:34 +00001568 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1569 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
1570 "This is an illegal uint to floating point cast!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001571 return getFoldedCast(Instruction::UIToFP, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001572}
1573
Owen Anderson04fb7c32009-06-20 00:24:58 +00001574Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Devang Patelb6dc9352008-11-03 23:20:04 +00001575#ifndef NDEBUG
Nate Begemanb348d182007-11-17 03:58:34 +00001576 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1577 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Patelb6dc9352008-11-03 23:20:04 +00001578#endif
Nate Begemanb348d182007-11-17 03:58:34 +00001579 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1580 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
Reid Spencer3da59db2006-11-27 01:05:10 +00001581 "This is an illegal sint to floating point cast!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001582 return getFoldedCast(Instruction::SIToFP, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001583}
1584
Owen Anderson04fb7c32009-06-20 00:24:58 +00001585Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Devang Patelb6dc9352008-11-03 23:20:04 +00001586#ifndef NDEBUG
Nate Begemanb348d182007-11-17 03:58:34 +00001587 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1588 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Patelb6dc9352008-11-03 23:20:04 +00001589#endif
Nate Begemanb348d182007-11-17 03:58:34 +00001590 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1591 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1592 "This is an illegal floating point to uint cast!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001593 return getFoldedCast(Instruction::FPToUI, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001594}
1595
Owen Anderson04fb7c32009-06-20 00:24:58 +00001596Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Devang Patelb6dc9352008-11-03 23:20:04 +00001597#ifndef NDEBUG
Nate Begemanb348d182007-11-17 03:58:34 +00001598 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1599 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Patelb6dc9352008-11-03 23:20:04 +00001600#endif
Nate Begemanb348d182007-11-17 03:58:34 +00001601 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1602 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1603 "This is an illegal floating point to sint cast!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001604 return getFoldedCast(Instruction::FPToSI, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001605}
1606
Owen Anderson04fb7c32009-06-20 00:24:58 +00001607Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
Reid Spencer3da59db2006-11-27 01:05:10 +00001608 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner42a75512007-01-15 02:27:26 +00001609 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001610 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
Reid Spencer3da59db2006-11-27 01:05:10 +00001611}
1612
Owen Anderson04fb7c32009-06-20 00:24:58 +00001613Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner42a75512007-01-15 02:27:26 +00001614 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer3da59db2006-11-27 01:05:10 +00001615 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001616 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
Reid Spencer3da59db2006-11-27 01:05:10 +00001617}
1618
Owen Anderson04fb7c32009-06-20 00:24:58 +00001619Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
Reid Spencer3da59db2006-11-27 01:05:10 +00001620 // BitCast implies a no-op cast of type only. No bits change. However, you
1621 // can't cast pointers to anything but pointers.
Devang Patelb6dc9352008-11-03 23:20:04 +00001622#ifndef NDEBUG
Reid Spencer3da59db2006-11-27 01:05:10 +00001623 const Type *SrcTy = C->getType();
1624 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer848414e2006-12-04 20:17:56 +00001625 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer3da59db2006-11-27 01:05:10 +00001626
1627 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
1628 // or nonptr->ptr). For all the other types, the cast is okay if source and
1629 // destination bit widths are identical.
1630 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1631 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Devang Patelb6dc9352008-11-03 23:20:04 +00001632#endif
Chris Lattnercf1bb082009-03-08 04:06:26 +00001633 assert(SrcBitSize == DstBitSize && "BitCast requires types of same width");
Chris Lattner8c7f24a2009-03-21 06:55:54 +00001634
1635 // It is common to ask for a bitcast of a value to its own type, handle this
1636 // speedily.
1637 if (C->getType() == DstTy) return C;
1638
Owen Anderson04fb7c32009-06-20 00:24:58 +00001639 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerd144f422004-04-04 23:20:30 +00001640}
1641
Chris Lattnered468e372003-10-05 00:17:43 +00001642Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Owen Anderson04fb7c32009-06-20 00:24:58 +00001643 Constant *C1, Constant *C2) {
Chris Lattnerf31f5832003-05-21 17:49:25 +00001644 // Check the operands for consistency first
Reid Spencer0a783f72006-11-02 01:53:59 +00001645 assert(Opcode >= Instruction::BinaryOpsBegin &&
1646 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattnerf31f5832003-05-21 17:49:25 +00001647 "Invalid opcode in binary constant expression");
1648 assert(C1->getType() == C2->getType() &&
1649 "Operand types in binary constant expression should match");
Chris Lattnered468e372003-10-05 00:17:43 +00001650
Reid Spencer4fe16d62007-01-11 18:21:29 +00001651 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Owen Anderson5defacc2009-07-31 17:39:07 +00001652 if (Constant *FC = ConstantFoldBinaryInstruction(ReqTy->getContext(),
1653 Opcode, C1, C2))
Chris Lattnered468e372003-10-05 00:17:43 +00001654 return FC; // Fold a few common cases...
Chris Lattnerd628f6a2003-04-17 19:24:48 +00001655
Chris Lattner9bc02a42003-05-13 21:37:02 +00001656 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencer67263fe2006-12-04 21:35:24 +00001657 ExprMapKeyType Key(Opcode, argVec);
Owen Anderson31c36f02009-06-17 20:10:08 +00001658
Owen Anderson04fb7c32009-06-20 00:24:58 +00001659 // Implicitly locked.
1660 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001661}
1662
Reid Spencere4d87aa2006-12-23 06:05:41 +00001663Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Nate Begemanff795a82008-07-25 17:56:27 +00001664 Constant *C1, Constant *C2) {
Reid Spencere4d87aa2006-12-23 06:05:41 +00001665 switch (predicate) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001666 default: llvm_unreachable("Invalid CmpInst predicate");
Nate Begemanb5557ab2008-07-25 17:35:37 +00001667 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
1668 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
1669 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
1670 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
1671 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
1672 case CmpInst::FCMP_TRUE:
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00001673 return getFCmp(predicate, C1, C2);
1674
Nate Begemanb5557ab2008-07-25 17:35:37 +00001675 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
1676 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
1677 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
1678 case CmpInst::ICMP_SLE:
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00001679 return getICmp(predicate, C1, C2);
Reid Spencere4d87aa2006-12-23 06:05:41 +00001680 }
Reid Spencer67263fe2006-12-04 21:35:24 +00001681}
1682
Owen Anderson04fb7c32009-06-20 00:24:58 +00001683Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001684 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1685 if (C1->getType()->isFPOrFPVector()) {
1686 if (Opcode == Instruction::Add) Opcode = Instruction::FAdd;
1687 else if (Opcode == Instruction::Sub) Opcode = Instruction::FSub;
1688 else if (Opcode == Instruction::Mul) Opcode = Instruction::FMul;
1689 }
Chris Lattner91b362b2004-08-17 17:28:46 +00001690#ifndef NDEBUG
1691 switch (Opcode) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001692 case Instruction::Add:
Reid Spencer0a783f72006-11-02 01:53:59 +00001693 case Instruction::Sub:
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001694 case Instruction::Mul:
Chris Lattner91b362b2004-08-17 17:28:46 +00001695 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001696 assert(C1->getType()->isIntOrIntVector() &&
1697 "Tried to create an integer operation on a non-integer type!");
1698 break;
1699 case Instruction::FAdd:
1700 case Instruction::FSub:
1701 case Instruction::FMul:
1702 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1703 assert(C1->getType()->isFPOrFPVector() &&
1704 "Tried to create a floating-point operation on a "
1705 "non-floating-point type!");
Chris Lattner91b362b2004-08-17 17:28:46 +00001706 break;
Reid Spencer1628cec2006-10-26 06:15:43 +00001707 case Instruction::UDiv:
1708 case Instruction::SDiv:
1709 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanf57478f2009-06-15 22:25:12 +00001710 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer1628cec2006-10-26 06:15:43 +00001711 "Tried to create an arithmetic operation on a non-arithmetic type!");
1712 break;
1713 case Instruction::FDiv:
1714 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanf57478f2009-06-15 22:25:12 +00001715 assert(C1->getType()->isFPOrFPVector() &&
1716 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer1628cec2006-10-26 06:15:43 +00001717 break;
Reid Spencer0a783f72006-11-02 01:53:59 +00001718 case Instruction::URem:
1719 case Instruction::SRem:
1720 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanf57478f2009-06-15 22:25:12 +00001721 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer0a783f72006-11-02 01:53:59 +00001722 "Tried to create an arithmetic operation on a non-arithmetic type!");
1723 break;
1724 case Instruction::FRem:
1725 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanf57478f2009-06-15 22:25:12 +00001726 assert(C1->getType()->isFPOrFPVector() &&
1727 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer0a783f72006-11-02 01:53:59 +00001728 break;
Chris Lattner91b362b2004-08-17 17:28:46 +00001729 case Instruction::And:
1730 case Instruction::Or:
1731 case Instruction::Xor:
1732 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanf57478f2009-06-15 22:25:12 +00001733 assert(C1->getType()->isIntOrIntVector() &&
Misha Brukman1bae2912005-01-27 06:46:38 +00001734 "Tried to create a logical operation on a non-integral type!");
Chris Lattner91b362b2004-08-17 17:28:46 +00001735 break;
Chris Lattner91b362b2004-08-17 17:28:46 +00001736 case Instruction::Shl:
Reid Spencer3822ff52006-11-08 06:47:33 +00001737 case Instruction::LShr:
1738 case Instruction::AShr:
Reid Spencer832254e2007-02-02 02:16:23 +00001739 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanc1317932009-03-14 17:09:17 +00001740 assert(C1->getType()->isIntOrIntVector() &&
Chris Lattner91b362b2004-08-17 17:28:46 +00001741 "Tried to create a shift operation on a non-integer type!");
1742 break;
1743 default:
1744 break;
1745 }
1746#endif
1747
Owen Anderson04fb7c32009-06-20 00:24:58 +00001748 return getTy(C1->getType(), Opcode, C1, C2);
Reid Spencer67263fe2006-12-04 21:35:24 +00001749}
1750
Owen Andersonbaf3c402009-07-29 18:55:55 +00001751Constant* ConstantExpr::getSizeOf(const Type* Ty) {
1752 // sizeof is implemented as: (i64) gep (Ty*)null, 1
1753 // Note that a non-inbounds gep is used, as null isn't within any object.
1754 LLVMContext &Context = Ty->getContext();
1755 Constant *GEPIdx = ConstantInt::get(Type::Int32Ty, 1);
1756 Constant *GEP = getGetElementPtr(
1757 Context.getNullValue(PointerType::getUnqual(Ty)), &GEPIdx, 1);
1758 return getCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
1759}
1760
1761Constant* ConstantExpr::getAlignOf(const Type* Ty) {
1762 LLVMContext &Context = Ty->getContext();
1763 // alignof is implemented as: (i64) gep ({i8,Ty}*)null, 0, 1
1764 const Type *AligningTy = StructType::get(Type::Int8Ty, Ty, NULL);
1765 Constant *NullPtr = Context.getNullValue(AligningTy->getPointerTo());
1766 Constant *Zero = ConstantInt::get(Type::Int32Ty, 0);
1767 Constant *One = ConstantInt::get(Type::Int32Ty, 1);
1768 Constant *Indices[2] = { Zero, One };
1769 Constant *GEP = getGetElementPtr(NullPtr, Indices, 2);
1770 return getCast(Instruction::PtrToInt, GEP, Type::Int32Ty);
1771}
1772
1773
Reid Spencere4d87aa2006-12-23 06:05:41 +00001774Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencer67263fe2006-12-04 21:35:24 +00001775 Constant *C1, Constant *C2) {
1776 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencere4d87aa2006-12-23 06:05:41 +00001777 return getCompareTy(pred, C1, C2);
Chris Lattnerc3d12f02004-08-04 18:50:09 +00001778}
1779
Chris Lattner08a45cc2004-03-12 05:54:04 +00001780Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
Owen Anderson04fb7c32009-06-20 00:24:58 +00001781 Constant *V1, Constant *V2) {
Chris Lattner9ace0cd2008-12-29 00:16:12 +00001782 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner08a45cc2004-03-12 05:54:04 +00001783
1784 if (ReqTy == V1->getType())
Owen Anderson0a5372e2009-07-13 04:09:18 +00001785 if (Constant *SC = ConstantFoldSelectInstruction(
Owen Anderson5defacc2009-07-31 17:39:07 +00001786 ReqTy->getContext(), C, V1, V2))
Chris Lattner08a45cc2004-03-12 05:54:04 +00001787 return SC; // Fold common cases
1788
1789 std::vector<Constant*> argVec(3, C);
1790 argVec[1] = V1;
1791 argVec[2] = V2;
Reid Spencer077d0eb2006-12-04 05:19:50 +00001792 ExprMapKeyType Key(Instruction::Select, argVec);
Owen Anderson31c36f02009-06-17 20:10:08 +00001793
Owen Anderson04fb7c32009-06-20 00:24:58 +00001794 // Implicitly locked.
1795 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner08a45cc2004-03-12 05:54:04 +00001796}
1797
Chris Lattnered468e372003-10-05 00:17:43 +00001798Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001799 Value* const *Idxs,
Owen Anderson04fb7c32009-06-20 00:24:58 +00001800 unsigned NumIdx) {
Dan Gohman041e2eb2008-05-15 19:50:34 +00001801 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
1802 Idxs+NumIdx) ==
1803 cast<PointerType>(ReqTy)->getElementType() &&
1804 "GEP indices invalid!");
Chris Lattner2e9bb1a2004-02-16 20:46:13 +00001805
Owen Anderson0a5372e2009-07-13 04:09:18 +00001806 if (Constant *FC = ConstantFoldGetElementPtr(
Owen Anderson5defacc2009-07-31 17:39:07 +00001807 ReqTy->getContext(), C, (Constant**)Idxs, NumIdx))
Chris Lattnerd628f6a2003-04-17 19:24:48 +00001808 return FC; // Fold a few common cases...
Chris Lattner2e9bb1a2004-02-16 20:46:13 +00001809
Chris Lattnered468e372003-10-05 00:17:43 +00001810 assert(isa<PointerType>(C->getType()) &&
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001811 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001812 // Look up the constant in the table first to ensure uniqueness
Chris Lattner7fa6e662004-10-11 22:52:25 +00001813 std::vector<Constant*> ArgVec;
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001814 ArgVec.reserve(NumIdx+1);
Chris Lattner7fa6e662004-10-11 22:52:25 +00001815 ArgVec.push_back(C);
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001816 for (unsigned i = 0; i != NumIdx; ++i)
1817 ArgVec.push_back(cast<Constant>(Idxs[i]));
1818 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Owen Anderson31c36f02009-06-17 20:10:08 +00001819
1820 // Implicitly locked.
Owen Anderson04fb7c32009-06-20 00:24:58 +00001821 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001822}
1823
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001824Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
Owen Anderson04fb7c32009-06-20 00:24:58 +00001825 unsigned NumIdx) {
Chris Lattnered468e372003-10-05 00:17:43 +00001826 // Get the result type of the getelementptr!
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001827 const Type *Ty =
Dan Gohman041e2eb2008-05-15 19:50:34 +00001828 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
Chris Lattnered468e372003-10-05 00:17:43 +00001829 assert(Ty && "GEP indices invalid!");
Christopher Lambfe63fb92007-12-11 08:59:05 +00001830 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
Owen Anderson04fb7c32009-06-20 00:24:58 +00001831 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
Chris Lattner7fa6e662004-10-11 22:52:25 +00001832}
1833
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001834Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
Owen Anderson04fb7c32009-06-20 00:24:58 +00001835 unsigned NumIdx) {
1836 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnered468e372003-10-05 00:17:43 +00001837}
1838
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001839
Reid Spencer077d0eb2006-12-04 05:19:50 +00001840Constant *
1841ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1842 assert(LHS->getType() == RHS->getType());
1843 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
1844 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
1845
Owen Anderson0a5372e2009-07-13 04:09:18 +00001846 if (Constant *FC = ConstantFoldCompareInstruction(
Owen Anderson5defacc2009-07-31 17:39:07 +00001847 LHS->getContext(), pred, LHS, RHS))
Reid Spencer077d0eb2006-12-04 05:19:50 +00001848 return FC; // Fold a few common cases...
1849
1850 // Look up the constant in the table first to ensure uniqueness
1851 std::vector<Constant*> ArgVec;
1852 ArgVec.push_back(LHS);
1853 ArgVec.push_back(RHS);
Reid Spencer4fa021a2006-12-24 18:42:29 +00001854 // Get the key type with both the opcode and predicate
Reid Spencer077d0eb2006-12-04 05:19:50 +00001855 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Owen Anderson31c36f02009-06-17 20:10:08 +00001856
1857 // Implicitly locked.
1858 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001859}
1860
1861Constant *
1862ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1863 assert(LHS->getType() == RHS->getType());
1864 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
1865
Owen Anderson0a5372e2009-07-13 04:09:18 +00001866 if (Constant *FC = ConstantFoldCompareInstruction(
Owen Anderson5defacc2009-07-31 17:39:07 +00001867 LHS->getContext(), pred, LHS, RHS))
Reid Spencer077d0eb2006-12-04 05:19:50 +00001868 return FC; // Fold a few common cases...
1869
1870 // Look up the constant in the table first to ensure uniqueness
1871 std::vector<Constant*> ArgVec;
1872 ArgVec.push_back(LHS);
1873 ArgVec.push_back(RHS);
Reid Spencer4fa021a2006-12-24 18:42:29 +00001874 // Get the key type with both the opcode and predicate
Reid Spencer077d0eb2006-12-04 05:19:50 +00001875 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Owen Anderson31c36f02009-06-17 20:10:08 +00001876
1877 // Implicitly locked.
1878 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001879}
1880
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001881Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
1882 Constant *Idx) {
Owen Anderson0a5372e2009-07-13 04:09:18 +00001883 if (Constant *FC = ConstantFoldExtractElementInstruction(
Owen Anderson5defacc2009-07-31 17:39:07 +00001884 ReqTy->getContext(), Val, Idx))
Robert Bocchinobb90a7a2006-01-10 20:03:46 +00001885 return FC; // Fold a few common cases...
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001886 // Look up the constant in the table first to ensure uniqueness
1887 std::vector<Constant*> ArgVec(1, Val);
1888 ArgVec.push_back(Idx);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001889 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Owen Anderson31c36f02009-06-17 20:10:08 +00001890
1891 // Implicitly locked.
1892 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001893}
1894
1895Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001896 assert(isa<VectorType>(Val->getType()) &&
Reid Spencerac9dcb92007-02-15 03:39:18 +00001897 "Tried to create extractelement operation on non-vector type!");
Reid Spencer79e21d32006-12-31 05:26:44 +00001898 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer3d10b0b2007-01-26 07:37:34 +00001899 "Extractelement index must be i32 type!");
Reid Spencer9d6565a2007-02-15 02:26:10 +00001900 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001901 Val, Idx);
1902}
Chris Lattnered468e372003-10-05 00:17:43 +00001903
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001904Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
1905 Constant *Elt, Constant *Idx) {
Owen Anderson0a5372e2009-07-13 04:09:18 +00001906 if (Constant *FC = ConstantFoldInsertElementInstruction(
Owen Anderson5defacc2009-07-31 17:39:07 +00001907 ReqTy->getContext(), Val, Elt, Idx))
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001908 return FC; // Fold a few common cases...
1909 // Look up the constant in the table first to ensure uniqueness
1910 std::vector<Constant*> ArgVec(1, Val);
1911 ArgVec.push_back(Elt);
1912 ArgVec.push_back(Idx);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001913 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Owen Anderson31c36f02009-06-17 20:10:08 +00001914
1915 // Implicitly locked.
1916 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001917}
1918
1919Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1920 Constant *Idx) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001921 assert(isa<VectorType>(Val->getType()) &&
Reid Spencerac9dcb92007-02-15 03:39:18 +00001922 "Tried to create insertelement operation on non-vector type!");
Reid Spencer9d6565a2007-02-15 02:26:10 +00001923 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001924 && "Insertelement types must match!");
Reid Spencer79e21d32006-12-31 05:26:44 +00001925 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer3d10b0b2007-01-26 07:37:34 +00001926 "Insertelement index must be i32 type!");
Gordon Henriksen699609c2008-08-30 15:41:51 +00001927 return getInsertElementTy(Val->getType(), Val, Elt, Idx);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001928}
1929
Chris Lattner00f10232006-04-08 01:18:18 +00001930Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
1931 Constant *V2, Constant *Mask) {
Owen Anderson0a5372e2009-07-13 04:09:18 +00001932 if (Constant *FC = ConstantFoldShuffleVectorInstruction(
Owen Anderson5defacc2009-07-31 17:39:07 +00001933 ReqTy->getContext(), V1, V2, Mask))
Chris Lattner00f10232006-04-08 01:18:18 +00001934 return FC; // Fold a few common cases...
1935 // Look up the constant in the table first to ensure uniqueness
1936 std::vector<Constant*> ArgVec(1, V1);
1937 ArgVec.push_back(V2);
1938 ArgVec.push_back(Mask);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001939 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Owen Anderson31c36f02009-06-17 20:10:08 +00001940
1941 // Implicitly locked.
1942 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner00f10232006-04-08 01:18:18 +00001943}
1944
1945Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
1946 Constant *Mask) {
1947 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
1948 "Invalid shuffle vector constant expr operands!");
Nate Begeman0f123cf2009-02-12 21:28:33 +00001949
1950 unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements();
1951 const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
1952 const Type *ShufTy = VectorType::get(EltTy, NElts);
1953 return getShuffleVectorTy(ShufTy, V1, V2, Mask);
Chris Lattner00f10232006-04-08 01:18:18 +00001954}
1955
Dan Gohman041e2eb2008-05-15 19:50:34 +00001956Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg,
1957 Constant *Val,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001958 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman041e2eb2008-05-15 19:50:34 +00001959 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
1960 Idxs+NumIdx) == Val->getType() &&
1961 "insertvalue indices invalid!");
1962 assert(Agg->getType() == ReqTy &&
1963 "insertvalue type invalid!");
Dan Gohmane4569942008-05-23 00:36:11 +00001964 assert(Agg->getType()->isFirstClassType() &&
1965 "Non-first-class type for constant InsertValue expression");
Owen Anderson0a5372e2009-07-13 04:09:18 +00001966 Constant *FC = ConstantFoldInsertValueInstruction(
Owen Anderson5defacc2009-07-31 17:39:07 +00001967 ReqTy->getContext(), Agg, Val, Idxs, NumIdx);
Dan Gohmane0891602008-07-21 23:30:30 +00001968 assert(FC && "InsertValue constant expr couldn't be folded!");
1969 return FC;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001970}
1971
1972Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001973 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohmane4569942008-05-23 00:36:11 +00001974 assert(Agg->getType()->isFirstClassType() &&
1975 "Tried to create insertelement operation on non-first-class type!");
Dan Gohman041e2eb2008-05-15 19:50:34 +00001976
Dan Gohmane4569942008-05-23 00:36:11 +00001977 const Type *ReqTy = Agg->getType();
Devang Patelb6dc9352008-11-03 23:20:04 +00001978#ifndef NDEBUG
Dan Gohmane4569942008-05-23 00:36:11 +00001979 const Type *ValTy =
Dan Gohman041e2eb2008-05-15 19:50:34 +00001980 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
Devang Patelb6dc9352008-11-03 23:20:04 +00001981#endif
Dan Gohmane4569942008-05-23 00:36:11 +00001982 assert(ValTy == Val->getType() && "insertvalue indices invalid!");
Dan Gohman041e2eb2008-05-15 19:50:34 +00001983 return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx);
1984}
1985
1986Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001987 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman041e2eb2008-05-15 19:50:34 +00001988 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
1989 Idxs+NumIdx) == ReqTy &&
1990 "extractvalue indices invalid!");
Dan Gohmane4569942008-05-23 00:36:11 +00001991 assert(Agg->getType()->isFirstClassType() &&
1992 "Non-first-class type for constant extractvalue expression");
Owen Anderson0a5372e2009-07-13 04:09:18 +00001993 Constant *FC = ConstantFoldExtractValueInstruction(
Owen Anderson5defacc2009-07-31 17:39:07 +00001994 ReqTy->getContext(), Agg, Idxs, NumIdx);
Dan Gohmane0891602008-07-21 23:30:30 +00001995 assert(FC && "ExtractValue constant expr couldn't be folded!");
1996 return FC;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001997}
1998
1999Constant *ConstantExpr::getExtractValue(Constant *Agg,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002000 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohmane4569942008-05-23 00:36:11 +00002001 assert(Agg->getType()->isFirstClassType() &&
2002 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman041e2eb2008-05-15 19:50:34 +00002003
2004 const Type *ReqTy =
2005 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
2006 assert(ReqTy && "extractvalue indices invalid!");
2007 return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx);
2008}
2009
Owen Andersonbaf3c402009-07-29 18:55:55 +00002010Constant* ConstantExpr::getNeg(Constant* C) {
2011 // API compatibility: Adjust integer opcodes to floating-point opcodes.
2012 if (C->getType()->isFPOrFPVector())
2013 return getFNeg(C);
2014 assert(C->getType()->isIntOrIntVector() &&
2015 "Cannot NEG a nonintegral value!");
2016 return get(Instruction::Sub,
2017 ConstantFP::getZeroValueForNegation(C->getType()),
2018 C);
2019}
2020
2021Constant* ConstantExpr::getFNeg(Constant* C) {
2022 assert(C->getType()->isFPOrFPVector() &&
2023 "Cannot FNEG a non-floating-point value!");
2024 return get(Instruction::FSub,
2025 ConstantFP::getZeroValueForNegation(C->getType()),
2026 C);
2027}
2028
2029Constant* ConstantExpr::getNot(Constant* C) {
2030 assert(C->getType()->isIntOrIntVector() &&
2031 "Cannot NOT a nonintegral value!");
2032 LLVMContext &Context = C->getType()->getContext();
2033 return get(Instruction::Xor, C, Context.getAllOnesValue(C->getType()));
2034}
2035
2036Constant* ConstantExpr::getAdd(Constant* C1, Constant* C2) {
2037 return get(Instruction::Add, C1, C2);
2038}
2039
2040Constant* ConstantExpr::getFAdd(Constant* C1, Constant* C2) {
2041 return get(Instruction::FAdd, C1, C2);
2042}
2043
2044Constant* ConstantExpr::getSub(Constant* C1, Constant* C2) {
2045 return get(Instruction::Sub, C1, C2);
2046}
2047
2048Constant* ConstantExpr::getFSub(Constant* C1, Constant* C2) {
2049 return get(Instruction::FSub, C1, C2);
2050}
2051
2052Constant* ConstantExpr::getMul(Constant* C1, Constant* C2) {
2053 return get(Instruction::Mul, C1, C2);
2054}
2055
2056Constant* ConstantExpr::getFMul(Constant* C1, Constant* C2) {
2057 return get(Instruction::FMul, C1, C2);
2058}
2059
2060Constant* ConstantExpr::getUDiv(Constant* C1, Constant* C2) {
2061 return get(Instruction::UDiv, C1, C2);
2062}
2063
2064Constant* ConstantExpr::getSDiv(Constant* C1, Constant* C2) {
2065 return get(Instruction::SDiv, C1, C2);
2066}
2067
2068Constant* ConstantExpr::getFDiv(Constant* C1, Constant* C2) {
2069 return get(Instruction::FDiv, C1, C2);
2070}
2071
2072Constant* ConstantExpr::getURem(Constant* C1, Constant* C2) {
2073 return get(Instruction::URem, C1, C2);
2074}
2075
2076Constant* ConstantExpr::getSRem(Constant* C1, Constant* C2) {
2077 return get(Instruction::SRem, C1, C2);
2078}
2079
2080Constant* ConstantExpr::getFRem(Constant* C1, Constant* C2) {
2081 return get(Instruction::FRem, C1, C2);
2082}
2083
2084Constant* ConstantExpr::getAnd(Constant* C1, Constant* C2) {
2085 return get(Instruction::And, C1, C2);
2086}
2087
2088Constant* ConstantExpr::getOr(Constant* C1, Constant* C2) {
2089 return get(Instruction::Or, C1, C2);
2090}
2091
2092Constant* ConstantExpr::getXor(Constant* C1, Constant* C2) {
2093 return get(Instruction::Xor, C1, C2);
2094}
2095
2096Constant* ConstantExpr::getShl(Constant* C1, Constant* C2) {
2097 return get(Instruction::Shl, C1, C2);
2098}
2099
2100Constant* ConstantExpr::getLShr(Constant* C1, Constant* C2) {
2101 return get(Instruction::LShr, C1, C2);
2102}
2103
2104Constant* ConstantExpr::getAShr(Constant* C1, Constant* C2) {
2105 return get(Instruction::AShr, C1, C2);
2106}
2107
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00002108// destroyConstant - Remove the constant from the constant table...
2109//
Owen Anderson04fb7c32009-06-20 00:24:58 +00002110void ConstantExpr::destroyConstant() {
2111 // Implicitly locked.
2112 ExprConstants->remove(this);
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00002113 destroyConstantImpl();
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00002114}
2115
Chris Lattnerc188eeb2002-07-30 18:54:25 +00002116const char *ConstantExpr::getOpcodeName() const {
2117 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00002118}
Reid Spencer1c9c8e62004-07-17 23:48:33 +00002119
Chris Lattner5cbade92005-10-03 21:58:36 +00002120//===----------------------------------------------------------------------===//
2121// replaceUsesOfWithOnConstant implementations
2122
Chris Lattner54984052007-08-21 00:55:23 +00002123/// replaceUsesOfWithOnConstant - Update this constant array to change uses of
2124/// 'From' to be uses of 'To'. This must update the uniquing data structures
2125/// etc.
2126///
2127/// Note that we intentionally replace all uses of From with To here. Consider
2128/// a large array that uses 'From' 1000 times. By handling this case all here,
2129/// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that
2130/// single invocation handles all 1000 uses. Handling them one at a time would
2131/// work, but would be really slow because it would have to unique each updated
2132/// array instance.
Owen Anderson1fd70962009-07-28 18:32:17 +00002133
2134static std::vector<Constant*> getValType(ConstantArray *CA) {
2135 std::vector<Constant*> Elements;
2136 Elements.reserve(CA->getNumOperands());
2137 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
2138 Elements.push_back(cast<Constant>(CA->getOperand(i)));
2139 return Elements;
2140}
2141
2142
Chris Lattner5cbade92005-10-03 21:58:36 +00002143void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002144 Use *U) {
Owen Anderson1fd70962009-07-28 18:32:17 +00002145 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2146 Constant *ToC = cast<Constant>(To);
2147
2148 LLVMContext &Context = getType()->getContext();
2149 LLVMContextImpl *pImpl = Context.pImpl;
2150
2151 std::pair<LLVMContextImpl::ArrayConstantsTy::MapKey, Constant*> Lookup;
2152 Lookup.first.first = getType();
2153 Lookup.second = this;
2154
2155 std::vector<Constant*> &Values = Lookup.first.second;
2156 Values.reserve(getNumOperands()); // Build replacement array.
2157
2158 // Fill values with the modified operands of the constant array. Also,
2159 // compute whether this turns into an all-zeros array.
2160 bool isAllZeros = false;
2161 unsigned NumUpdated = 0;
2162 if (!ToC->isNullValue()) {
2163 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2164 Constant *Val = cast<Constant>(O->get());
2165 if (Val == From) {
2166 Val = ToC;
2167 ++NumUpdated;
2168 }
2169 Values.push_back(Val);
2170 }
2171 } else {
2172 isAllZeros = true;
2173 for (Use *O = OperandList, *E = OperandList+getNumOperands();O != E; ++O) {
2174 Constant *Val = cast<Constant>(O->get());
2175 if (Val == From) {
2176 Val = ToC;
2177 ++NumUpdated;
2178 }
2179 Values.push_back(Val);
2180 if (isAllZeros) isAllZeros = Val->isNullValue();
2181 }
2182 }
2183
2184 Constant *Replacement = 0;
2185 if (isAllZeros) {
Owen Anderson9e9a0d52009-07-30 23:03:37 +00002186 Replacement = ConstantAggregateZero::get(getType());
Owen Anderson1fd70962009-07-28 18:32:17 +00002187 } else {
2188 // Check to see if we have this array type already.
2189 sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
2190 bool Exists;
2191 LLVMContextImpl::ArrayConstantsTy::MapTy::iterator I =
2192 pImpl->ArrayConstants.InsertOrGetItem(Lookup, Exists);
2193
2194 if (Exists) {
2195 Replacement = I->second;
2196 } else {
2197 // Okay, the new shape doesn't exist in the system yet. Instead of
2198 // creating a new constant array, inserting it, replaceallusesof'ing the
2199 // old with the new, then deleting the old... just update the current one
2200 // in place!
2201 pImpl->ArrayConstants.MoveConstantToNewSlot(this, I);
2202
2203 // Update to the new value. Optimize for the case when we have a single
2204 // operand that we're changing, but handle bulk updates efficiently.
2205 if (NumUpdated == 1) {
2206 unsigned OperandToUpdate = U - OperandList;
2207 assert(getOperand(OperandToUpdate) == From &&
2208 "ReplaceAllUsesWith broken!");
2209 setOperand(OperandToUpdate, ToC);
2210 } else {
2211 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2212 if (getOperand(i) == From)
2213 setOperand(i, ToC);
2214 }
2215 return;
2216 }
2217 }
Chris Lattnercea141f2005-10-03 22:51:37 +00002218
2219 // Otherwise, I do need to replace this with an existing value.
Chris Lattner5cbade92005-10-03 21:58:36 +00002220 assert(Replacement != this && "I didn't contain From!");
2221
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002222 // Everyone using this now uses the replacement.
2223 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattner5cbade92005-10-03 21:58:36 +00002224
2225 // Delete the old constant!
2226 destroyConstant();
2227}
2228
Owen Anderson8fa33382009-07-27 22:29:26 +00002229static std::vector<Constant*> getValType(ConstantStruct *CS) {
2230 std::vector<Constant*> Elements;
2231 Elements.reserve(CS->getNumOperands());
2232 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
2233 Elements.push_back(cast<Constant>(CS->getOperand(i)));
2234 return Elements;
2235}
2236
Chris Lattner5cbade92005-10-03 21:58:36 +00002237void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002238 Use *U) {
Owen Anderson8fa33382009-07-27 22:29:26 +00002239 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2240 Constant *ToC = cast<Constant>(To);
2241
2242 unsigned OperandToUpdate = U-OperandList;
2243 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
2244
2245 std::pair<LLVMContextImpl::StructConstantsTy::MapKey, Constant*> Lookup;
2246 Lookup.first.first = getType();
2247 Lookup.second = this;
2248 std::vector<Constant*> &Values = Lookup.first.second;
2249 Values.reserve(getNumOperands()); // Build replacement struct.
2250
2251
2252 // Fill values with the modified operands of the constant struct. Also,
2253 // compute whether this turns into an all-zeros struct.
2254 bool isAllZeros = false;
2255 if (!ToC->isNullValue()) {
2256 for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O)
2257 Values.push_back(cast<Constant>(O->get()));
2258 } else {
2259 isAllZeros = true;
2260 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2261 Constant *Val = cast<Constant>(O->get());
2262 Values.push_back(Val);
2263 if (isAllZeros) isAllZeros = Val->isNullValue();
2264 }
2265 }
2266 Values[OperandToUpdate] = ToC;
2267
2268 LLVMContext &Context = getType()->getContext();
2269 LLVMContextImpl *pImpl = Context.pImpl;
2270
2271 Constant *Replacement = 0;
2272 if (isAllZeros) {
Owen Anderson9e9a0d52009-07-30 23:03:37 +00002273 Replacement = ConstantAggregateZero::get(getType());
Owen Anderson8fa33382009-07-27 22:29:26 +00002274 } else {
2275 // Check to see if we have this array type already.
2276 sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
2277 bool Exists;
2278 LLVMContextImpl::StructConstantsTy::MapTy::iterator I =
2279 pImpl->StructConstants.InsertOrGetItem(Lookup, Exists);
2280
2281 if (Exists) {
2282 Replacement = I->second;
2283 } else {
2284 // Okay, the new shape doesn't exist in the system yet. Instead of
2285 // creating a new constant struct, inserting it, replaceallusesof'ing the
2286 // old with the new, then deleting the old... just update the current one
2287 // in place!
2288 pImpl->StructConstants.MoveConstantToNewSlot(this, I);
2289
2290 // Update to the new value.
2291 setOperand(OperandToUpdate, ToC);
2292 return;
2293 }
2294 }
2295
2296 assert(Replacement != this && "I didn't contain From!");
Chris Lattner5cbade92005-10-03 21:58:36 +00002297
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002298 // Everyone using this now uses the replacement.
2299 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattner5cbade92005-10-03 21:58:36 +00002300
2301 // Delete the old constant!
2302 destroyConstant();
2303}
2304
Owen Andersonaf7ec972009-07-28 21:19:26 +00002305static std::vector<Constant*> getValType(ConstantVector *CP) {
2306 std::vector<Constant*> Elements;
2307 Elements.reserve(CP->getNumOperands());
2308 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
2309 Elements.push_back(CP->getOperand(i));
2310 return Elements;
2311}
2312
Reid Spencer9d6565a2007-02-15 02:26:10 +00002313void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002314 Use *U) {
Chris Lattner5cbade92005-10-03 21:58:36 +00002315 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2316
2317 std::vector<Constant*> Values;
2318 Values.reserve(getNumOperands()); // Build replacement array...
2319 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2320 Constant *Val = getOperand(i);
2321 if (Val == From) Val = cast<Constant>(To);
2322 Values.push_back(Val);
2323 }
2324
Owen Andersonaf7ec972009-07-28 21:19:26 +00002325 Constant *Replacement = get(getType(), Values);
Chris Lattner5cbade92005-10-03 21:58:36 +00002326 assert(Replacement != this && "I didn't contain From!");
2327
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002328 // Everyone using this now uses the replacement.
2329 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattner5cbade92005-10-03 21:58:36 +00002330
2331 // Delete the old constant!
2332 destroyConstant();
2333}
2334
2335void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002336 Use *U) {
Chris Lattner5cbade92005-10-03 21:58:36 +00002337 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2338 Constant *To = cast<Constant>(ToV);
2339
2340 Constant *Replacement = 0;
2341 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerf9021ff2007-02-19 20:01:23 +00002342 SmallVector<Constant*, 8> Indices;
Chris Lattner5cbade92005-10-03 21:58:36 +00002343 Constant *Pointer = getOperand(0);
2344 Indices.reserve(getNumOperands()-1);
2345 if (Pointer == From) Pointer = To;
2346
2347 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2348 Constant *Val = getOperand(i);
2349 if (Val == From) Val = To;
2350 Indices.push_back(Val);
2351 }
Chris Lattnerf9021ff2007-02-19 20:01:23 +00002352 Replacement = ConstantExpr::getGetElementPtr(Pointer,
2353 &Indices[0], Indices.size());
Dan Gohman041e2eb2008-05-15 19:50:34 +00002354 } else if (getOpcode() == Instruction::ExtractValue) {
Dan Gohman041e2eb2008-05-15 19:50:34 +00002355 Constant *Agg = getOperand(0);
Dan Gohman041e2eb2008-05-15 19:50:34 +00002356 if (Agg == From) Agg = To;
2357
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002358 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman041e2eb2008-05-15 19:50:34 +00002359 Replacement = ConstantExpr::getExtractValue(Agg,
2360 &Indices[0], Indices.size());
2361 } else if (getOpcode() == Instruction::InsertValue) {
Dan Gohman041e2eb2008-05-15 19:50:34 +00002362 Constant *Agg = getOperand(0);
2363 Constant *Val = getOperand(1);
Dan Gohman041e2eb2008-05-15 19:50:34 +00002364 if (Agg == From) Agg = To;
2365 if (Val == From) Val = To;
2366
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002367 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman041e2eb2008-05-15 19:50:34 +00002368 Replacement = ConstantExpr::getInsertValue(Agg, Val,
2369 &Indices[0], Indices.size());
Reid Spencer3da59db2006-11-27 01:05:10 +00002370 } else if (isCast()) {
Chris Lattner5cbade92005-10-03 21:58:36 +00002371 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer3da59db2006-11-27 01:05:10 +00002372 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattner5cbade92005-10-03 21:58:36 +00002373 } else if (getOpcode() == Instruction::Select) {
2374 Constant *C1 = getOperand(0);
2375 Constant *C2 = getOperand(1);
2376 Constant *C3 = getOperand(2);
2377 if (C1 == From) C1 = To;
2378 if (C2 == From) C2 = To;
2379 if (C3 == From) C3 = To;
2380 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00002381 } else if (getOpcode() == Instruction::ExtractElement) {
2382 Constant *C1 = getOperand(0);
2383 Constant *C2 = getOperand(1);
2384 if (C1 == From) C1 = To;
2385 if (C2 == From) C2 = To;
2386 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattner42b55802006-04-08 05:09:48 +00002387 } else if (getOpcode() == Instruction::InsertElement) {
2388 Constant *C1 = getOperand(0);
2389 Constant *C2 = getOperand(1);
2390 Constant *C3 = getOperand(1);
2391 if (C1 == From) C1 = To;
2392 if (C2 == From) C2 = To;
2393 if (C3 == From) C3 = To;
2394 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2395 } else if (getOpcode() == Instruction::ShuffleVector) {
2396 Constant *C1 = getOperand(0);
2397 Constant *C2 = getOperand(1);
2398 Constant *C3 = getOperand(2);
2399 if (C1 == From) C1 = To;
2400 if (C2 == From) C2 = To;
2401 if (C3 == From) C3 = To;
2402 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spencer077d0eb2006-12-04 05:19:50 +00002403 } else if (isCompare()) {
2404 Constant *C1 = getOperand(0);
2405 Constant *C2 = getOperand(1);
2406 if (C1 == From) C1 = To;
2407 if (C2 == From) C2 = To;
2408 if (getOpcode() == Instruction::ICmp)
2409 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
Chris Lattnerbbedb0e2008-07-14 05:17:31 +00002410 else {
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002411 assert(getOpcode() == Instruction::FCmp);
2412 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnerbbedb0e2008-07-14 05:17:31 +00002413 }
Chris Lattner5cbade92005-10-03 21:58:36 +00002414 } else if (getNumOperands() == 2) {
2415 Constant *C1 = getOperand(0);
2416 Constant *C2 = getOperand(1);
2417 if (C1 == From) C1 = To;
2418 if (C2 == From) C2 = To;
2419 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2420 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00002421 llvm_unreachable("Unknown ConstantExpr type!");
Chris Lattner5cbade92005-10-03 21:58:36 +00002422 return;
2423 }
2424
2425 assert(Replacement != this && "I didn't contain From!");
2426
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002427 // Everyone using this now uses the replacement.
2428 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattner5cbade92005-10-03 21:58:36 +00002429
2430 // Delete the old constant!
2431 destroyConstant();
Matthijs Kooijman10b9de62008-07-03 07:46:41 +00002432}
Nick Lewycky21cc4462009-04-04 07:22:01 +00002433