blob: f7d6946f18120709c1df3c5ff55ce2ef80988147 [file] [log] [blame]
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001//===-- Constants.cpp - Implement Constant nodes --------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner3462ae32001-12-03 22:26:30 +000010// This file implements the Constant* classes...
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Owen Andersonedb4a702009-07-24 23:12:02 +000014#include "LLVMContextImpl.h"
Chris Lattnerca142372002-04-28 19:55:58 +000015#include "llvm/Constants.h"
Chris Lattner33e93b82007-02-27 03:05:06 +000016#include "ConstantFold.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000017#include "llvm/DerivedTypes.h"
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000018#include "llvm/GlobalValue.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000019#include "llvm/Instructions.h"
Chris Lattnerd7a73302001-10-13 06:57:33 +000020#include "llvm/Module.h"
Dan Gohman7d82e132009-07-18 01:49:22 +000021#include "llvm/Operator.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000022#include "llvm/ADT/FoldingSet.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000023#include "llvm/ADT/StringExtras.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000024#include "llvm/ADT/StringMap.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000025#include "llvm/Support/Compiler.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000026#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000027#include "llvm/Support/ErrorHandling.h"
Chris Lattner69edc982006-09-28 00:35:06 +000028#include "llvm/Support/ManagedStatic.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000029#include "llvm/Support/MathExtras.h"
Owen Anderson0d2de8c2009-06-20 00:24:58 +000030#include "llvm/System/Mutex.h"
Owen Anderson2d7231d2009-06-17 18:40:29 +000031#include "llvm/System/RWMutex.h"
Owen Anderson7d42b952009-06-18 16:54:52 +000032#include "llvm/System/Threading.h"
Chris Lattnera80bf0b2007-02-20 06:39:57 +000033#include "llvm/ADT/DenseMap.h"
Chris Lattnerb5d70302007-02-19 20:01:23 +000034#include "llvm/ADT/SmallVector.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000035#include <algorithm>
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000036#include <map>
Chris Lattner189d19f2003-11-21 20:23:48 +000037using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000038
Chris Lattner2f7c9632001-06-06 20:29:01 +000039//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000040// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000041//===----------------------------------------------------------------------===//
42
Owen Andersond830eb82009-06-18 19:10:19 +000043// Becomes a no-op when multithreading is disabled.
44ManagedStatic<sys::SmartRWMutex<true> > ConstantsLock;
Owen Anderson2d7231d2009-06-17 18:40:29 +000045
Chris Lattner3462ae32001-12-03 22:26:30 +000046void Constant::destroyConstantImpl() {
47 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000048 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000049 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-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 Lattner3462ae32001-12-03 22:26:30 +000052 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000053 //
54 while (!use_empty()) {
55 Value *V = use_back();
56#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000057 if (!isa<Constant>(V))
Bill Wendling6a462f12006-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 Lattnerd7a73302001-10-13 06:57:33 +000061#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000062 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000063 Constant *CV = cast<Constant>(V);
64 CV->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +000065
66 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000067 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000068 }
69
70 // Value has no outstanding references it is safe to delete it now...
71 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000072}
Chris Lattner2f7c9632001-06-06 20:29:01 +000073
Chris Lattner23dd1f62006-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 Spencer7e80b0b2006-10-26 06:15:43 +000091 case Instruction::UDiv:
92 case Instruction::SDiv:
93 case Instruction::FDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +000094 case Instruction::URem:
95 case Instruction::SRem:
96 case Instruction::FRem:
Chris Lattner23dd1f62006-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 Lattner4565ef52009-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 Lattner5cd4dd32009-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 Lattner4565ef52009-07-22 00:05:44 +0000117///
118/// FIXME: This really should not be in VMCore.
Chris Lattner5cd4dd32009-07-24 03:27:21 +0000119Constant::PossibleRelocationsTy Constant::getRelocationInfo() const {
120 if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
Chris Lattner4565ef52009-07-22 00:05:44 +0000121 if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
Chris Lattner5cd4dd32009-07-24 03:27:21 +0000122 return LocalRelocation; // Local to this file/library.
123 return GlobalRelocations; // Global reference.
Anton Korobeynikov7437b592009-03-29 17:13:18 +0000124 }
Chris Lattner4565ef52009-07-22 00:05:44 +0000125
Chris Lattner5cd4dd32009-07-24 03:27:21 +0000126 PossibleRelocationsTy Result = NoRelocation;
Evan Chengf9e003b2007-03-08 00:59:12 +0000127 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Chris Lattner4565ef52009-07-22 00:05:44 +0000128 Result = std::max(Result, getOperand(i)->getRelocationInfo());
129
130 return Result;
Evan Chengf9e003b2007-03-08 00:59:12 +0000131}
132
Chris Lattner4565ef52009-07-22 00:05:44 +0000133
Chris Lattner2105d662008-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 Lattnerc5098a22008-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 Anderson53a52212009-07-13 04:09:18 +0000138void Constant::getVectorElements(LLVMContext &Context,
139 SmallVectorImpl<Constant*> &Elts) const {
Chris Lattner2105d662008-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 Anderson53a52212009-07-13 04:09:18 +0000151 Context.getNullValue(VT->getElementType()));
Chris Lattner2105d662008-07-10 00:28:11 +0000152 return;
153 }
154
Chris Lattnerc5098a22008-07-14 05:10:41 +0000155 if (isa<UndefValue>(this)) {
Owen Andersonb292b8c2009-07-30 23:03:37 +0000156 Elts.assign(VT->getNumElements(), UndefValue::get(VT->getElementType()));
Chris Lattnerc5098a22008-07-14 05:10:41 +0000157 return;
158 }
159
160 // Unknown type, must be constant expr etc.
Chris Lattner2105d662008-07-10 00:28:11 +0000161}
162
163
164
Chris Lattner2f7c9632001-06-06 20:29:01 +0000165//===----------------------------------------------------------------------===//
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000166// ConstantInt
Chris Lattner2f7c9632001-06-06 20:29:01 +0000167//===----------------------------------------------------------------------===//
168
Reid Spencerb31bffe2007-02-26 23:54:03 +0000169ConstantInt::ConstantInt(const IntegerType *Ty, const APInt& V)
Chris Lattner5db2f472007-02-20 05:55:46 +0000170 : Constant(Ty, ConstantIntVal, 0, 0), Val(V) {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000171 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000172}
173
Owen Andersonedb4a702009-07-24 23:12:02 +0000174// Get a ConstantInt from an APInt. Note that the value stored in the DenseMap
175// as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the
176// operator== and operator!= to ensure that the DenseMap doesn't attempt to
177// compare APInt's of different widths, which would violate an APInt class
178// invariant which generates an assertion.
179ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt& V) {
180 // Get the corresponding integer type for the bit width of the value.
Owen Anderson4056ca92009-07-29 22:17:13 +0000181 const IntegerType *ITy = IntegerType::get(V.getBitWidth());
Owen Andersonedb4a702009-07-24 23:12:02 +0000182 // get an existing value or the insertion position
183 DenseMapAPIntKeyInfo::KeyTy Key(V, ITy);
184
185 Context.pImpl->ConstantsLock.reader_acquire();
186 ConstantInt *&Slot = Context.pImpl->IntConstants[Key];
187 Context.pImpl->ConstantsLock.reader_release();
188
189 if (!Slot) {
190 sys::SmartScopedWriter<true> Writer(Context.pImpl->ConstantsLock);
191 ConstantInt *&NewSlot = Context.pImpl->IntConstants[Key];
192 if (!Slot) {
193 NewSlot = new ConstantInt(ITy, V);
194 }
195
196 return NewSlot;
197 } else {
198 return Slot;
199 }
200}
201
202Constant* ConstantInt::get(const Type* Ty, uint64_t V, bool isSigned) {
203 Constant *C = get(cast<IntegerType>(Ty->getScalarType()),
204 V, isSigned);
205
206 // For vectors, broadcast the value.
207 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
Owen Anderson4aa32952009-07-28 21:19:26 +0000208 return ConstantVector::get(
Owen Andersonedb4a702009-07-24 23:12:02 +0000209 std::vector<Constant *>(VTy->getNumElements(), C));
210
211 return C;
212}
213
214ConstantInt* ConstantInt::get(const IntegerType* Ty, uint64_t V,
215 bool isSigned) {
216 return get(Ty->getContext(), APInt(Ty->getBitWidth(), V, isSigned));
217}
218
219ConstantInt* ConstantInt::getSigned(const IntegerType* Ty, int64_t V) {
220 return get(Ty, V, true);
221}
222
223Constant *ConstantInt::getSigned(const Type *Ty, int64_t V) {
224 return get(Ty, V, true);
225}
226
227Constant* ConstantInt::get(const Type* Ty, const APInt& V) {
228 ConstantInt *C = get(Ty->getContext(), V);
229 assert(C->getType() == Ty->getScalarType() &&
230 "ConstantInt type doesn't match the type implied by its value!");
231
232 // For vectors, broadcast the value.
233 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
Owen Anderson4aa32952009-07-28 21:19:26 +0000234 return ConstantVector::get(
Owen Andersonedb4a702009-07-24 23:12:02 +0000235 std::vector<Constant *>(VTy->getNumElements(), C));
236
237 return C;
238}
239
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000240//===----------------------------------------------------------------------===//
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000241// ConstantFP
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000242//===----------------------------------------------------------------------===//
243
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000244static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
245 if (Ty == Type::FloatTy)
246 return &APFloat::IEEEsingle;
247 if (Ty == Type::DoubleTy)
248 return &APFloat::IEEEdouble;
249 if (Ty == Type::X86_FP80Ty)
250 return &APFloat::x87DoubleExtended;
251 else if (Ty == Type::FP128Ty)
252 return &APFloat::IEEEquad;
253
254 assert(Ty == Type::PPC_FP128Ty && "Unknown FP format");
255 return &APFloat::PPCDoubleDouble;
256}
257
Owen Anderson69c464d2009-07-27 20:59:43 +0000258/// get() - This returns a constant fp for the specified value in the
259/// specified type. This should only be used for simple constant values like
260/// 2.0/1.0 etc, that are known-valid both as double and as the target format.
261Constant* ConstantFP::get(const Type* Ty, double V) {
262 LLVMContext &Context = Ty->getContext();
263
264 APFloat FV(V);
265 bool ignored;
266 FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
267 APFloat::rmNearestTiesToEven, &ignored);
268 Constant *C = get(Context, FV);
269
270 // For vectors, broadcast the value.
271 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
Owen Anderson4aa32952009-07-28 21:19:26 +0000272 return ConstantVector::get(
Owen Anderson69c464d2009-07-27 20:59:43 +0000273 std::vector<Constant *>(VTy->getNumElements(), C));
274
275 return C;
276}
277
278ConstantFP* ConstantFP::getNegativeZero(const Type* Ty) {
279 LLVMContext &Context = Ty->getContext();
280 APFloat apf = cast <ConstantFP>(Context.getNullValue(Ty))->getValueAPF();
281 apf.changeSign();
282 return get(Context, apf);
283}
284
285
286Constant* ConstantFP::getZeroValueForNegation(const Type* Ty) {
287 LLVMContext &Context = Ty->getContext();
288 if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
289 if (PTy->getElementType()->isFloatingPoint()) {
290 std::vector<Constant*> zeros(PTy->getNumElements(),
291 getNegativeZero(PTy->getElementType()));
Owen Anderson4aa32952009-07-28 21:19:26 +0000292 return ConstantVector::get(PTy, zeros);
Owen Anderson69c464d2009-07-27 20:59:43 +0000293 }
294
295 if (Ty->isFloatingPoint())
296 return getNegativeZero(Ty);
297
298 return Context.getNullValue(Ty);
299}
300
301
302// ConstantFP accessors.
303ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) {
304 DenseMapAPFloatKeyInfo::KeyTy Key(V);
305
306 LLVMContextImpl* pImpl = Context.pImpl;
307
308 pImpl->ConstantsLock.reader_acquire();
309 ConstantFP *&Slot = pImpl->FPConstants[Key];
310 pImpl->ConstantsLock.reader_release();
311
312 if (!Slot) {
313 sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
314 ConstantFP *&NewSlot = pImpl->FPConstants[Key];
315 if (!NewSlot) {
316 const Type *Ty;
317 if (&V.getSemantics() == &APFloat::IEEEsingle)
318 Ty = Type::FloatTy;
319 else if (&V.getSemantics() == &APFloat::IEEEdouble)
320 Ty = Type::DoubleTy;
321 else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
322 Ty = Type::X86_FP80Ty;
323 else if (&V.getSemantics() == &APFloat::IEEEquad)
324 Ty = Type::FP128Ty;
325 else {
326 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble &&
327 "Unknown FP format");
328 Ty = Type::PPC_FP128Ty;
329 }
330 NewSlot = new ConstantFP(Ty, V);
331 }
332
333 return NewSlot;
334 }
335
336 return Slot;
337}
338
Dale Johannesend246b2c2007-08-30 00:23:21 +0000339ConstantFP::ConstantFP(const Type *Ty, const APFloat& V)
340 : Constant(Ty, ConstantFPVal, 0, 0), Val(V) {
Chris Lattner98bd9392008-04-09 06:38:30 +0000341 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
342 "FP type Mismatch");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000343}
344
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000345bool ConstantFP::isNullValue() const {
Dale Johannesena719a602007-08-24 00:56:33 +0000346 return Val.isZero() && !Val.isNegative();
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000347}
348
Dale Johannesend246b2c2007-08-30 00:23:21 +0000349bool ConstantFP::isExactlyValue(const APFloat& V) const {
350 return Val.bitwiseIsEqual(V);
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000351}
352
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000353//===----------------------------------------------------------------------===//
354// ConstantXXX Classes
355//===----------------------------------------------------------------------===//
356
357
Chris Lattner3462ae32001-12-03 22:26:30 +0000358ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000359 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000360 : Constant(T, ConstantArrayVal,
361 OperandTraits<ConstantArray>::op_end(this) - V.size(),
362 V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000363 assert(V.size() == T->getNumElements() &&
364 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000365 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000366 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
367 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000368 Constant *C = *I;
369 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000370 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000371 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000372 "Initializer for array element doesn't match array element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000373 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000374 }
375}
376
Owen Andersonc2c79322009-07-28 18:32:17 +0000377Constant *ConstantArray::get(const ArrayType *Ty,
378 const std::vector<Constant*> &V) {
379 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
380 // If this is an all-zero array, return a ConstantAggregateZero object
381 if (!V.empty()) {
382 Constant *C = V[0];
383 if (!C->isNullValue()) {
384 // Implicitly locked.
385 return pImpl->ArrayConstants.getOrCreate(Ty, V);
386 }
387 for (unsigned i = 1, e = V.size(); i != e; ++i)
388 if (V[i] != C) {
389 // Implicitly locked.
390 return pImpl->ArrayConstants.getOrCreate(Ty, V);
391 }
392 }
393
Owen Andersonb292b8c2009-07-30 23:03:37 +0000394 return ConstantAggregateZero::get(Ty);
Owen Andersonc2c79322009-07-28 18:32:17 +0000395}
396
397
398Constant* ConstantArray::get(const ArrayType* T, Constant* const* Vals,
399 unsigned NumVals) {
400 // FIXME: make this the primary ctor method.
401 return get(T, std::vector<Constant*>(Vals, Vals+NumVals));
402}
403
404/// ConstantArray::get(const string&) - Return an array that is initialized to
405/// contain the specified string. If length is zero then a null terminator is
406/// added to the specified string so that it may be used in a natural way.
407/// Otherwise, the length parameter specifies how much of the string to use
408/// and it won't be null terminated.
409///
410Constant* ConstantArray::get(const StringRef &Str, bool AddNull) {
411 std::vector<Constant*> ElementVals;
412 for (unsigned i = 0; i < Str.size(); ++i)
413 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, Str[i]));
414
415 // Add a null terminator to the string...
416 if (AddNull) {
417 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, 0));
418 }
419
420 ArrayType *ATy = ArrayType::get(Type::Int8Ty, ElementVals.size());
421 return get(ATy, ElementVals);
422}
423
424
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000425
Chris Lattner3462ae32001-12-03 22:26:30 +0000426ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000427 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000428 : Constant(T, ConstantStructVal,
429 OperandTraits<ConstantStruct>::op_end(this) - V.size(),
430 V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000431 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000432 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000433 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000434 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
435 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000436 Constant *C = *I;
437 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000438 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000439 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000440 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000441 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000442 "Initializer for struct element doesn't match struct element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000443 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000444 }
445}
446
Owen Anderson45308b52009-07-27 22:29:26 +0000447// ConstantStruct accessors.
448Constant* ConstantStruct::get(const StructType* T,
449 const std::vector<Constant*>& V) {
450 LLVMContextImpl* pImpl = T->getContext().pImpl;
451
452 // Create a ConstantAggregateZero value if all elements are zeros...
453 for (unsigned i = 0, e = V.size(); i != e; ++i)
454 if (!V[i]->isNullValue())
455 // Implicitly locked.
456 return pImpl->StructConstants.getOrCreate(T, V);
457
Owen Andersonb292b8c2009-07-30 23:03:37 +0000458 return ConstantAggregateZero::get(T);
Owen Anderson45308b52009-07-27 22:29:26 +0000459}
460
461Constant* ConstantStruct::get(const std::vector<Constant*>& V, bool packed) {
462 std::vector<const Type*> StructEls;
463 StructEls.reserve(V.size());
464 for (unsigned i = 0, e = V.size(); i != e; ++i)
465 StructEls.push_back(V[i]->getType());
466 return get(StructType::get(StructEls, packed), V);
467}
468
469Constant* ConstantStruct::get(Constant* const *Vals, unsigned NumVals,
470 bool Packed) {
471 // FIXME: make this the primary ctor method.
472 return get(std::vector<Constant*>(Vals, Vals+NumVals), Packed);
473}
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000474
Reid Spencerd84d35b2007-02-15 02:26:10 +0000475ConstantVector::ConstantVector(const VectorType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000476 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000477 : Constant(T, ConstantVectorVal,
478 OperandTraits<ConstantVector>::op_end(this) - V.size(),
479 V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000480 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000481 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
482 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000483 Constant *C = *I;
484 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000485 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000486 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Dan Gohman30978072007-05-24 14:36:04 +0000487 "Initializer for vector element doesn't match vector element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000488 *OL = C;
Brian Gaeke02209042004-08-20 06:00:58 +0000489 }
490}
491
Owen Anderson4aa32952009-07-28 21:19:26 +0000492// ConstantVector accessors.
493Constant* ConstantVector::get(const VectorType* T,
494 const std::vector<Constant*>& V) {
495 assert(!V.empty() && "Vectors can't be empty");
496 LLVMContext &Context = T->getContext();
497 LLVMContextImpl *pImpl = Context.pImpl;
498
499 // If this is an all-undef or alll-zero vector, return a
500 // ConstantAggregateZero or UndefValue.
501 Constant *C = V[0];
502 bool isZero = C->isNullValue();
503 bool isUndef = isa<UndefValue>(C);
504
505 if (isZero || isUndef) {
506 for (unsigned i = 1, e = V.size(); i != e; ++i)
507 if (V[i] != C) {
508 isZero = isUndef = false;
509 break;
510 }
511 }
512
513 if (isZero)
Owen Andersonb292b8c2009-07-30 23:03:37 +0000514 return ConstantAggregateZero::get(T);
Owen Anderson4aa32952009-07-28 21:19:26 +0000515 if (isUndef)
Owen Andersonb292b8c2009-07-30 23:03:37 +0000516 return UndefValue::get(T);
Owen Anderson4aa32952009-07-28 21:19:26 +0000517
518 // Implicitly locked.
519 return pImpl->VectorConstants.getOrCreate(T, V);
520}
521
522Constant* ConstantVector::get(const std::vector<Constant*>& V) {
523 assert(!V.empty() && "Cannot infer type if V is empty");
524 return get(VectorType::get(V.front()->getType(),V.size()), V);
525}
526
527Constant* ConstantVector::get(Constant* const* Vals, unsigned NumVals) {
528 // FIXME: make this the primary ctor method.
529 return get(std::vector<Constant*>(Vals, Vals+NumVals));
530}
531
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000532
Gabor Greiff6caff662008-05-10 08:32:32 +0000533namespace llvm {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000534// We declare several classes private to this file, so use an anonymous
535// namespace
536namespace {
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000537
Gordon Henriksen14a55692007-12-10 02:14:30 +0000538/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
539/// behind the scenes to implement unary constant exprs.
540class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000541 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000542public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000543 // allocate space for exactly one operand
544 void *operator new(size_t s) {
545 return User::operator new(s, 1);
546 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000547 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
Gabor Greiff6caff662008-05-10 08:32:32 +0000548 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
549 Op<0>() = C;
550 }
551 /// Transparently provide more efficient getOperand methods.
552 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000553};
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000554
Gordon Henriksen14a55692007-12-10 02:14:30 +0000555/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
556/// behind the scenes to implement binary constant exprs.
557class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000558 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000559public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000560 // allocate space for exactly two operands
561 void *operator new(size_t s) {
562 return User::operator new(s, 2);
563 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000564 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Gabor Greiff6caff662008-05-10 08:32:32 +0000565 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000566 Op<0>() = C1;
567 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000568 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000569 /// Transparently provide more efficient getOperand methods.
570 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000571};
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000572
Gordon Henriksen14a55692007-12-10 02:14:30 +0000573/// SelectConstantExpr - This class is private to Constants.cpp, and is used
574/// behind the scenes to implement select constant exprs.
575class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000576 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000577public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000578 // allocate space for exactly three operands
579 void *operator new(size_t s) {
580 return User::operator new(s, 3);
581 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000582 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Gabor Greiff6caff662008-05-10 08:32:32 +0000583 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000584 Op<0>() = C1;
585 Op<1>() = C2;
586 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000587 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000588 /// Transparently provide more efficient getOperand methods.
589 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000590};
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000591
Gordon Henriksen14a55692007-12-10 02:14:30 +0000592/// ExtractElementConstantExpr - This class is private to
593/// Constants.cpp, and is used behind the scenes to implement
594/// extractelement constant exprs.
595class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000596 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000597public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000598 // allocate space for exactly two operands
599 void *operator new(size_t s) {
600 return User::operator new(s, 2);
601 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000602 ExtractElementConstantExpr(Constant *C1, Constant *C2)
603 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000604 Instruction::ExtractElement, &Op<0>(), 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000605 Op<0>() = C1;
606 Op<1>() = C2;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000607 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000608 /// Transparently provide more efficient getOperand methods.
609 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000610};
Robert Bocchino23004482006-01-10 19:05:34 +0000611
Gordon Henriksen14a55692007-12-10 02:14:30 +0000612/// InsertElementConstantExpr - This class is private to
613/// Constants.cpp, and is used behind the scenes to implement
614/// insertelement constant exprs.
615class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000616 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000617public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000618 // allocate space for exactly three operands
619 void *operator new(size_t s) {
620 return User::operator new(s, 3);
621 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000622 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
623 : ConstantExpr(C1->getType(), Instruction::InsertElement,
Gabor Greiff6caff662008-05-10 08:32:32 +0000624 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000625 Op<0>() = C1;
626 Op<1>() = C2;
627 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000628 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000629 /// Transparently provide more efficient getOperand methods.
630 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000631};
Robert Bocchinoca27f032006-01-17 20:07:22 +0000632
Gordon Henriksen14a55692007-12-10 02:14:30 +0000633/// ShuffleVectorConstantExpr - This class is private to
634/// Constants.cpp, and is used behind the scenes to implement
635/// shufflevector constant exprs.
636class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000637 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksen14a55692007-12-10 02:14:30 +0000638public:
Gabor Greife9ecc682008-04-06 20:25:17 +0000639 // allocate space for exactly three operands
640 void *operator new(size_t s) {
641 return User::operator new(s, 3);
642 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000643 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Nate Begeman94aa38d2009-02-12 21:28:33 +0000644 : ConstantExpr(VectorType::get(
645 cast<VectorType>(C1->getType())->getElementType(),
646 cast<VectorType>(C3->getType())->getNumElements()),
647 Instruction::ShuffleVector,
Gabor Greiff6caff662008-05-10 08:32:32 +0000648 &Op<0>(), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000649 Op<0>() = C1;
650 Op<1>() = C2;
651 Op<2>() = C3;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000652 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000653 /// Transparently provide more efficient getOperand methods.
654 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000655};
656
Dan Gohman12fce772008-05-15 19:50:34 +0000657/// ExtractValueConstantExpr - This class is private to
658/// Constants.cpp, and is used behind the scenes to implement
659/// extractvalue constant exprs.
660class VISIBILITY_HIDDEN ExtractValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000661 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000662public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000663 // allocate space for exactly one operand
664 void *operator new(size_t s) {
665 return User::operator new(s, 1);
Dan Gohman12fce772008-05-15 19:50:34 +0000666 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000667 ExtractValueConstantExpr(Constant *Agg,
668 const SmallVector<unsigned, 4> &IdxList,
669 const Type *DestTy)
670 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
671 Indices(IdxList) {
672 Op<0>() = Agg;
673 }
674
Dan Gohman7bb04502008-05-31 19:09:08 +0000675 /// Indices - These identify which value to extract.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000676 const SmallVector<unsigned, 4> Indices;
677
Dan Gohman12fce772008-05-15 19:50:34 +0000678 /// Transparently provide more efficient getOperand methods.
679 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
680};
681
682/// InsertValueConstantExpr - This class is private to
683/// Constants.cpp, and is used behind the scenes to implement
684/// insertvalue constant exprs.
685class VISIBILITY_HIDDEN InsertValueConstantExpr : public ConstantExpr {
Dan Gohman1ecaf452008-05-31 00:58:22 +0000686 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman12fce772008-05-15 19:50:34 +0000687public:
Dan Gohman1ecaf452008-05-31 00:58:22 +0000688 // allocate space for exactly one operand
689 void *operator new(size_t s) {
690 return User::operator new(s, 2);
Dan Gohman12fce772008-05-15 19:50:34 +0000691 }
Dan Gohman1ecaf452008-05-31 00:58:22 +0000692 InsertValueConstantExpr(Constant *Agg, Constant *Val,
693 const SmallVector<unsigned, 4> &IdxList,
694 const Type *DestTy)
695 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
696 Indices(IdxList) {
697 Op<0>() = Agg;
698 Op<1>() = Val;
699 }
700
Dan Gohman7bb04502008-05-31 19:09:08 +0000701 /// Indices - These identify the position for the insertion.
Dan Gohman1ecaf452008-05-31 00:58:22 +0000702 const SmallVector<unsigned, 4> Indices;
703
Dan Gohman12fce772008-05-15 19:50:34 +0000704 /// Transparently provide more efficient getOperand methods.
705 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
706};
707
708
Gordon Henriksen14a55692007-12-10 02:14:30 +0000709/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
710/// used behind the scenes to implement getelementpr constant exprs.
Gabor Greife9ecc682008-04-06 20:25:17 +0000711class VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000712 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000713 const Type *DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000714public:
Gabor Greif697e94c2008-05-15 10:04:30 +0000715 static GetElementPtrConstantExpr *Create(Constant *C,
716 const std::vector<Constant*>&IdxList,
Gabor Greiff6caff662008-05-10 08:32:32 +0000717 const Type *DestTy) {
Dan Gohman33a3fd02009-07-20 17:43:30 +0000718 return
719 new(IdxList.size() + 1) GetElementPtrConstantExpr(C, IdxList, DestTy);
Gabor Greife9ecc682008-04-06 20:25:17 +0000720 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000721 /// Transparently provide more efficient getOperand methods.
722 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000723};
724
725// CompareConstantExpr - This class is private to Constants.cpp, and is used
726// behind the scenes to implement ICmp and FCmp constant expressions. This is
727// needed in order to store the predicate value for these instructions.
728struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr {
Gabor Greife9ecc682008-04-06 20:25:17 +0000729 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
730 // allocate space for exactly two operands
731 void *operator new(size_t s) {
732 return User::operator new(s, 2);
733 }
Gordon Henriksen14a55692007-12-10 02:14:30 +0000734 unsigned short predicate;
Nate Begemand2195702008-05-12 19:01:56 +0000735 CompareConstantExpr(const Type *ty, Instruction::OtherOps opc,
736 unsigned short pred, Constant* LHS, Constant* RHS)
737 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000738 Op<0>() = LHS;
739 Op<1>() = RHS;
Gordon Henriksen14a55692007-12-10 02:14:30 +0000740 }
Gabor Greiff6caff662008-05-10 08:32:32 +0000741 /// Transparently provide more efficient getOperand methods.
742 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksen14a55692007-12-10 02:14:30 +0000743};
744
745} // end anonymous namespace
746
Gabor Greiff6caff662008-05-10 08:32:32 +0000747template <>
748struct OperandTraits<UnaryConstantExpr> : FixedNumOperandTraits<1> {
749};
750DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
751
752template <>
753struct OperandTraits<BinaryConstantExpr> : FixedNumOperandTraits<2> {
754};
755DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
756
757template <>
758struct OperandTraits<SelectConstantExpr> : FixedNumOperandTraits<3> {
759};
760DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
761
762template <>
763struct OperandTraits<ExtractElementConstantExpr> : FixedNumOperandTraits<2> {
764};
765DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
766
767template <>
768struct OperandTraits<InsertElementConstantExpr> : FixedNumOperandTraits<3> {
769};
770DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
771
772template <>
773struct OperandTraits<ShuffleVectorConstantExpr> : FixedNumOperandTraits<3> {
774};
775DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
776
Dan Gohman12fce772008-05-15 19:50:34 +0000777template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000778struct OperandTraits<ExtractValueConstantExpr> : FixedNumOperandTraits<1> {
Dan Gohman12fce772008-05-15 19:50:34 +0000779};
Dan Gohman12fce772008-05-15 19:50:34 +0000780DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
781
782template <>
Dan Gohman1ecaf452008-05-31 00:58:22 +0000783struct OperandTraits<InsertValueConstantExpr> : FixedNumOperandTraits<2> {
Dan Gohman12fce772008-05-15 19:50:34 +0000784};
Dan Gohman12fce772008-05-15 19:50:34 +0000785DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
786
Gabor Greiff6caff662008-05-10 08:32:32 +0000787template <>
788struct OperandTraits<GetElementPtrConstantExpr> : VariadicOperandTraits<1> {
789};
790
791GetElementPtrConstantExpr::GetElementPtrConstantExpr
792 (Constant *C,
793 const std::vector<Constant*> &IdxList,
794 const Type *DestTy)
795 : ConstantExpr(DestTy, Instruction::GetElementPtr,
796 OperandTraits<GetElementPtrConstantExpr>::op_end(this)
797 - (IdxList.size()+1),
798 IdxList.size()+1) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000799 OperandList[0] = C;
Gabor Greiff6caff662008-05-10 08:32:32 +0000800 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000801 OperandList[i+1] = IdxList[i];
Gabor Greiff6caff662008-05-10 08:32:32 +0000802}
803
804DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
805
806
807template <>
808struct OperandTraits<CompareConstantExpr> : FixedNumOperandTraits<2> {
809};
810DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
811
812
813} // End llvm namespace
814
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000815
816// Utility function for determining if a ConstantExpr is a CastOp or not. This
817// can't be inline because we don't want to #include Instruction.h into
818// Constant.h
819bool ConstantExpr::isCast() const {
820 return Instruction::isCast(getOpcode());
821}
822
Reid Spenceree3c9912006-12-04 05:19:50 +0000823bool ConstantExpr::isCompare() const {
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000824 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
Reid Spenceree3c9912006-12-04 05:19:50 +0000825}
826
Dan Gohman1ecaf452008-05-31 00:58:22 +0000827bool ConstantExpr::hasIndices() const {
828 return getOpcode() == Instruction::ExtractValue ||
829 getOpcode() == Instruction::InsertValue;
830}
831
832const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const {
833 if (const ExtractValueConstantExpr *EVCE =
834 dyn_cast<ExtractValueConstantExpr>(this))
835 return EVCE->Indices;
Dan Gohmana469bdb2008-06-23 16:39:44 +0000836
837 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman1ecaf452008-05-31 00:58:22 +0000838}
839
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000840unsigned ConstantExpr::getPredicate() const {
Nate Begemand2195702008-05-12 19:01:56 +0000841 assert(getOpcode() == Instruction::FCmp ||
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000842 getOpcode() == Instruction::ICmp);
Chris Lattneref650092007-10-18 16:26:24 +0000843 return ((const CompareConstantExpr*)this)->predicate;
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000844}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000845
Chris Lattner7c1018a2006-07-14 19:37:40 +0000846/// getWithOperandReplaced - Return a constant expression identical to this
847/// one, but with the specified operand set to the specified value.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000848Constant *
849ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +0000850 assert(OpNo < getNumOperands() && "Operand num is out of range!");
851 assert(Op->getType() == getOperand(OpNo)->getType() &&
852 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000853 if (getOperand(OpNo) == Op)
854 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000855
Chris Lattner227816342006-07-14 22:20:01 +0000856 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000857 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000858 case Instruction::Trunc:
859 case Instruction::ZExt:
860 case Instruction::SExt:
861 case Instruction::FPTrunc:
862 case Instruction::FPExt:
863 case Instruction::UIToFP:
864 case Instruction::SIToFP:
865 case Instruction::FPToUI:
866 case Instruction::FPToSI:
867 case Instruction::PtrToInt:
868 case Instruction::IntToPtr:
869 case Instruction::BitCast:
870 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000871 case Instruction::Select:
872 Op0 = (OpNo == 0) ? Op : getOperand(0);
873 Op1 = (OpNo == 1) ? Op : getOperand(1);
874 Op2 = (OpNo == 2) ? Op : getOperand(2);
875 return ConstantExpr::getSelect(Op0, Op1, Op2);
876 case Instruction::InsertElement:
877 Op0 = (OpNo == 0) ? Op : getOperand(0);
878 Op1 = (OpNo == 1) ? Op : getOperand(1);
879 Op2 = (OpNo == 2) ? Op : getOperand(2);
880 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
881 case Instruction::ExtractElement:
882 Op0 = (OpNo == 0) ? Op : getOperand(0);
883 Op1 = (OpNo == 1) ? Op : getOperand(1);
884 return ConstantExpr::getExtractElement(Op0, Op1);
885 case Instruction::ShuffleVector:
886 Op0 = (OpNo == 0) ? Op : getOperand(0);
887 Op1 = (OpNo == 1) ? Op : getOperand(1);
888 Op2 = (OpNo == 2) ? Op : getOperand(2);
889 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000890 case Instruction::GetElementPtr: {
Chris Lattnerb5d70302007-02-19 20:01:23 +0000891 SmallVector<Constant*, 8> Ops;
Dan Gohman12fce772008-05-15 19:50:34 +0000892 Ops.resize(getNumOperands()-1);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000893 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Dan Gohman12fce772008-05-15 19:50:34 +0000894 Ops[i-1] = getOperand(i);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000895 if (OpNo == 0)
Chris Lattnerb5d70302007-02-19 20:01:23 +0000896 return ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000897 Ops[OpNo-1] = Op;
Chris Lattnerb5d70302007-02-19 20:01:23 +0000898 return ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000899 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000900 default:
901 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000902 Op0 = (OpNo == 0) ? Op : getOperand(0);
903 Op1 = (OpNo == 1) ? Op : getOperand(1);
904 return ConstantExpr::get(getOpcode(), Op0, Op1);
905 }
906}
907
908/// getWithOperands - This returns the current constant expression with the
909/// operands replaced with the specified values. The specified operands must
910/// match count and type with the existing ones.
911Constant *ConstantExpr::
Chris Lattnerb078e282008-08-20 22:27:40 +0000912getWithOperands(Constant* const *Ops, unsigned NumOps) const {
913 assert(NumOps == getNumOperands() && "Operand count mismatch!");
Chris Lattner227816342006-07-14 22:20:01 +0000914 bool AnyChange = false;
Chris Lattnerb078e282008-08-20 22:27:40 +0000915 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattner227816342006-07-14 22:20:01 +0000916 assert(Ops[i]->getType() == getOperand(i)->getType() &&
917 "Operand type mismatch!");
918 AnyChange |= Ops[i] != getOperand(i);
919 }
920 if (!AnyChange) // No operands changed, return self.
921 return const_cast<ConstantExpr*>(this);
922
923 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000924 case Instruction::Trunc:
925 case Instruction::ZExt:
926 case Instruction::SExt:
927 case Instruction::FPTrunc:
928 case Instruction::FPExt:
929 case Instruction::UIToFP:
930 case Instruction::SIToFP:
931 case Instruction::FPToUI:
932 case Instruction::FPToSI:
933 case Instruction::PtrToInt:
934 case Instruction::IntToPtr:
935 case Instruction::BitCast:
936 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattner227816342006-07-14 22:20:01 +0000937 case Instruction::Select:
938 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
939 case Instruction::InsertElement:
940 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
941 case Instruction::ExtractElement:
942 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
943 case Instruction::ShuffleVector:
944 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Chris Lattnerb5d70302007-02-19 20:01:23 +0000945 case Instruction::GetElementPtr:
Chris Lattnerb078e282008-08-20 22:27:40 +0000946 return ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], NumOps-1);
Reid Spencer266e42b2006-12-23 06:05:41 +0000947 case Instruction::ICmp:
948 case Instruction::FCmp:
949 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattner227816342006-07-14 22:20:01 +0000950 default:
951 assert(getNumOperands() == 2 && "Must be binary operator?");
952 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000953 }
954}
955
Chris Lattner2f7c9632001-06-06 20:29:01 +0000956
957//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000958// isValueValidForType implementations
959
Reid Spencere7334722006-12-19 01:28:19 +0000960bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000961 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000962 if (Ty == Type::Int1Ty)
963 return Val == 0 || Val == 1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000964 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000965 return true; // always true, has to fit in largest type
966 uint64_t Max = (1ll << NumBits) - 1;
967 return Val <= Max;
Reid Spencere7334722006-12-19 01:28:19 +0000968}
969
Reid Spencere0fc4df2006-10-20 07:07:24 +0000970bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000971 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000972 if (Ty == Type::Int1Ty)
Reid Spencera94d3942007-01-19 21:13:56 +0000973 return Val == 0 || Val == 1 || Val == -1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000974 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000975 return true; // always true, has to fit in largest type
976 int64_t Min = -(1ll << (NumBits-1));
977 int64_t Max = (1ll << (NumBits-1)) - 1;
978 return (Val >= Min && Val <= Max);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000979}
980
Dale Johannesend246b2c2007-08-30 00:23:21 +0000981bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) {
982 // convert modifies in place, so make a copy.
983 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000984 bool losesInfo;
Chris Lattner6b727592004-06-17 18:19:28 +0000985 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000986 default:
987 return false; // These can't be represented as floating point!
988
Dale Johannesend246b2c2007-08-30 00:23:21 +0000989 // FIXME rounding mode needs to be more flexible
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000990 case Type::FloatTyID: {
991 if (&Val2.getSemantics() == &APFloat::IEEEsingle)
992 return true;
993 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
994 return !losesInfo;
995 }
996 case Type::DoubleTyID: {
997 if (&Val2.getSemantics() == &APFloat::IEEEsingle ||
998 &Val2.getSemantics() == &APFloat::IEEEdouble)
999 return true;
1000 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
1001 return !losesInfo;
1002 }
Dale Johannesenbdad8092007-08-09 22:51:36 +00001003 case Type::X86_FP80TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +00001004 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
1005 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1006 &Val2.getSemantics() == &APFloat::x87DoubleExtended;
Dale Johannesenbdad8092007-08-09 22:51:36 +00001007 case Type::FP128TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +00001008 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
1009 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1010 &Val2.getSemantics() == &APFloat::IEEEquad;
Dale Johannesen007aa372007-10-11 18:07:22 +00001011 case Type::PPC_FP128TyID:
1012 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
1013 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1014 &Val2.getSemantics() == &APFloat::PPCDoubleDouble;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001015 }
Chris Lattneraa2372562006-05-24 17:04:05 +00001016}
Chris Lattner9655e542001-07-20 19:16:02 +00001017
Chris Lattner49d855c2001-09-07 16:46:31 +00001018//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +00001019// Factory Function Implementation
1020
Owen Andersonb292b8c2009-07-30 23:03:37 +00001021static char getValType(ConstantAggregateZero *CPZ) { return 0; }
1022
1023ConstantAggregateZero* ConstantAggregateZero::get(const Type* Ty) {
1024 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) &&
1025 "Cannot create an aggregate zero of non-aggregate type!");
1026
1027 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
1028 // Implicitly locked.
1029 return pImpl->AggZeroConstants.getOrCreate(Ty, 0);
1030}
1031
Dan Gohman92b551b2009-03-03 02:55:14 +00001032/// destroyConstant - Remove the constant from the constant table...
1033///
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001034void ConstantAggregateZero::destroyConstant() {
Owen Anderson61794042009-06-17 20:10:08 +00001035 // Implicitly locked.
Owen Andersonb292b8c2009-07-30 23:03:37 +00001036 getType()->getContext().pImpl->AggZeroConstants.remove(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001037 destroyConstantImpl();
1038}
1039
Dan Gohman92b551b2009-03-03 02:55:14 +00001040/// destroyConstant - Remove the constant from the constant table...
1041///
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001042void ConstantArray::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001043 // Implicitly locked.
Owen Andersonc2c79322009-07-28 18:32:17 +00001044 getType()->getContext().pImpl->ArrayConstants.remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001045 destroyConstantImpl();
1046}
1047
Reid Spencer2546b762007-01-26 07:37:34 +00001048/// isString - This method returns true if the array is an array of i8, and
1049/// if the elements of the array are all ConstantInt's.
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001050bool ConstantArray::isString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001051 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001052 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001053 return false;
1054 // Check the elements to make sure they are all integers, not constant
1055 // expressions.
1056 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1057 if (!isa<ConstantInt>(getOperand(i)))
1058 return false;
1059 return true;
1060}
1061
Evan Cheng3763c5b2006-10-26 19:15:05 +00001062/// isCString - This method returns true if the array is a string (see
Dan Gohman92b551b2009-03-03 02:55:14 +00001063/// isString) and it ends in a null byte \\0 and does not contains any other
Evan Cheng3763c5b2006-10-26 19:15:05 +00001064/// null bytes except its terminator.
Owen Andersone4dcecd2009-07-13 21:27:19 +00001065bool ConstantArray::isCString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001066 // Check the element type for i8...
Reid Spencer8d9336d2006-12-31 05:26:44 +00001067 if (getType()->getElementType() != Type::Int8Ty)
Evan Chenge974da62006-10-26 21:48:03 +00001068 return false;
Owen Andersone4dcecd2009-07-13 21:27:19 +00001069
Evan Chenge974da62006-10-26 21:48:03 +00001070 // Last element must be a null.
Owen Andersone4dcecd2009-07-13 21:27:19 +00001071 if (!getOperand(getNumOperands()-1)->isNullValue())
Evan Chenge974da62006-10-26 21:48:03 +00001072 return false;
1073 // Other elements must be non-null integers.
1074 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1075 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +00001076 return false;
Owen Andersone4dcecd2009-07-13 21:27:19 +00001077 if (getOperand(i)->isNullValue())
Evan Chenge974da62006-10-26 21:48:03 +00001078 return false;
1079 }
Evan Cheng3763c5b2006-10-26 19:15:05 +00001080 return true;
1081}
1082
1083
Dan Gohman92b551b2009-03-03 02:55:14 +00001084/// getAsString - If the sub-element type of this array is i8
1085/// then this method converts the array to an std::string and returns it.
1086/// Otherwise, it asserts out.
1087///
Chris Lattner81fabb02002-08-26 17:53:56 +00001088std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001089 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001090 std::string Result;
Owen Anderson79c69bc2008-06-24 21:58:29 +00001091 Result.reserve(getNumOperands());
Chris Lattner6077c312003-07-23 15:22:26 +00001092 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonee9c30d2008-06-25 01:05:05 +00001093 Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue());
Chris Lattner81fabb02002-08-26 17:53:56 +00001094 return Result;
1095}
1096
1097
Chris Lattner3462ae32001-12-03 22:26:30 +00001098//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001099//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001100
Chris Lattner189d19f2003-11-21 20:23:48 +00001101namespace llvm {
Misha Brukmanb1c93172005-04-21 23:48:37 +00001102
Chris Lattner49d855c2001-09-07 16:46:31 +00001103}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001104
Chris Lattnerd7a73302001-10-13 06:57:33 +00001105// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001106//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001107void ConstantStruct::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001108 // Implicitly locked.
Owen Anderson45308b52009-07-27 22:29:26 +00001109 getType()->getContext().pImpl->StructConstants.remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001110 destroyConstantImpl();
1111}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001112
Brian Gaeke02209042004-08-20 06:00:58 +00001113// destroyConstant - Remove the constant from the constant table...
1114//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001115void ConstantVector::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001116 // Implicitly locked.
Owen Anderson4aa32952009-07-28 21:19:26 +00001117 getType()->getContext().pImpl->VectorConstants.remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001118 destroyConstantImpl();
1119}
1120
Dan Gohman30978072007-05-24 14:36:04 +00001121/// This function will return true iff every element in this vector constant
Jim Laskeyf0478822007-01-12 22:39:14 +00001122/// is set to all ones.
1123/// @returns true iff this constant's emements are all set to all ones.
1124/// @brief Determine if the value is all ones.
Reid Spencerd84d35b2007-02-15 02:26:10 +00001125bool ConstantVector::isAllOnesValue() const {
Jim Laskeyf0478822007-01-12 22:39:14 +00001126 // Check out first element.
1127 const Constant *Elt = getOperand(0);
1128 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1129 if (!CI || !CI->isAllOnesValue()) return false;
1130 // Then make sure all remaining elements point to the same value.
1131 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1132 if (getOperand(I) != Elt) return false;
1133 }
1134 return true;
1135}
1136
Dan Gohman07159202007-10-17 17:51:30 +00001137/// getSplatValue - If this is a splat constant, where all of the
1138/// elements have the same value, return that value. Otherwise return null.
1139Constant *ConstantVector::getSplatValue() {
1140 // Check out first element.
1141 Constant *Elt = getOperand(0);
1142 // Then make sure all remaining elements point to the same value.
1143 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
1144 if (getOperand(I) != Elt) return 0;
1145 return Elt;
1146}
1147
Chris Lattner3462ae32001-12-03 22:26:30 +00001148//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001149//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001150
Chris Lattner189d19f2003-11-21 20:23:48 +00001151namespace llvm {
1152 // ConstantPointerNull does not take extra "value" argument...
1153 template<class ValType>
1154 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1155 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1156 return new ConstantPointerNull(Ty);
1157 }
1158 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001159
Chris Lattner189d19f2003-11-21 20:23:48 +00001160 template<>
1161 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1162 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1163 // Make everyone now use a constant of the new type...
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001164 Constant *New = ConstantPointerNull::get(NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001165 assert(New != OldC && "Didn't replace constant??");
1166 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001167 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001168 }
1169 };
1170}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001171
Chris Lattner69edc982006-09-28 00:35:06 +00001172static ManagedStatic<ValueMap<char, PointerType,
1173 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001174
Chris Lattner3e650af2004-08-04 04:48:01 +00001175static char getValType(ConstantPointerNull *) {
1176 return 0;
1177}
1178
1179
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001180ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Owen Anderson61794042009-06-17 20:10:08 +00001181 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001182 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001183}
1184
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001185// destroyConstant - Remove the constant from the constant table...
1186//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001187void ConstantPointerNull::destroyConstant() {
Owen Anderson59ba8142009-06-19 18:34:09 +00001188 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001189 NullPtrConstants->remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001190 destroyConstantImpl();
1191}
1192
1193
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001194//---- UndefValue::get() implementation...
1195//
1196
1197namespace llvm {
1198 // UndefValue does not take extra "value" argument...
1199 template<class ValType>
1200 struct ConstantCreator<UndefValue, Type, ValType> {
1201 static UndefValue *create(const Type *Ty, const ValType &V) {
1202 return new UndefValue(Ty);
1203 }
1204 };
1205
1206 template<>
1207 struct ConvertConstantType<UndefValue, Type> {
1208 static void convert(UndefValue *OldC, const Type *NewTy) {
1209 // Make everyone now use a constant of the new type.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001210 Constant *New = UndefValue::get(NewTy);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001211 assert(New != OldC && "Didn't replace constant??");
1212 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001213 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001214 }
1215 };
1216}
1217
Chris Lattner69edc982006-09-28 00:35:06 +00001218static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001219
1220static char getValType(UndefValue *) {
1221 return 0;
1222}
1223
1224
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001225UndefValue *UndefValue::get(const Type *Ty) {
1226 // Implicitly locked.
1227 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001228}
1229
1230// destroyConstant - Remove the constant from the constant table.
1231//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001232void UndefValue::destroyConstant() {
Owen Anderson61794042009-06-17 20:10:08 +00001233 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001234 UndefValueConstants->remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001235 destroyConstantImpl();
1236}
1237
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001238//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001239//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001240
Dan Gohmand78c4002008-05-13 00:00:25 +00001241namespace {
1242
Reid Spenceree3c9912006-12-04 05:19:50 +00001243struct ExprMapKeyType {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001244 typedef SmallVector<unsigned, 4> IndexList;
1245
1246 ExprMapKeyType(unsigned opc,
1247 const std::vector<Constant*> &ops,
1248 unsigned short pred = 0,
1249 const IndexList &inds = IndexList())
1250 : opcode(opc), predicate(pred), operands(ops), indices(inds) {}
Reid Spencerdba6aa42006-12-04 18:38:05 +00001251 uint16_t opcode;
1252 uint16_t predicate;
Reid Spenceree3c9912006-12-04 05:19:50 +00001253 std::vector<Constant*> operands;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001254 IndexList indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001255 bool operator==(const ExprMapKeyType& that) const {
1256 return this->opcode == that.opcode &&
1257 this->predicate == that.predicate &&
Bill Wendling97f7de82008-10-26 00:19:56 +00001258 this->operands == that.operands &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001259 this->indices == that.indices;
Reid Spenceree3c9912006-12-04 05:19:50 +00001260 }
1261 bool operator<(const ExprMapKeyType & that) const {
1262 return this->opcode < that.opcode ||
1263 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1264 (this->opcode == that.opcode && this->predicate == that.predicate &&
Dan Gohman1ecaf452008-05-31 00:58:22 +00001265 this->operands < that.operands) ||
1266 (this->opcode == that.opcode && this->predicate == that.predicate &&
1267 this->operands == that.operands && this->indices < that.indices);
Reid Spenceree3c9912006-12-04 05:19:50 +00001268 }
1269
1270 bool operator!=(const ExprMapKeyType& that) const {
1271 return !(*this == that);
1272 }
1273};
Chris Lattner98fa07b2003-05-23 20:03:32 +00001274
Dan Gohmand78c4002008-05-13 00:00:25 +00001275}
1276
Chris Lattner189d19f2003-11-21 20:23:48 +00001277namespace llvm {
1278 template<>
1279 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001280 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1281 unsigned short pred = 0) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001282 if (Instruction::isCast(V.opcode))
1283 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1284 if ((V.opcode >= Instruction::BinaryOpsBegin &&
Reid Spencer2341c222007-02-02 02:16:23 +00001285 V.opcode < Instruction::BinaryOpsEnd))
Reid Spenceree3c9912006-12-04 05:19:50 +00001286 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1287 if (V.opcode == Instruction::Select)
1288 return new SelectConstantExpr(V.operands[0], V.operands[1],
1289 V.operands[2]);
1290 if (V.opcode == Instruction::ExtractElement)
1291 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1292 if (V.opcode == Instruction::InsertElement)
1293 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1294 V.operands[2]);
1295 if (V.opcode == Instruction::ShuffleVector)
1296 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1297 V.operands[2]);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001298 if (V.opcode == Instruction::InsertValue)
1299 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
1300 V.indices, Ty);
1301 if (V.opcode == Instruction::ExtractValue)
1302 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001303 if (V.opcode == Instruction::GetElementPtr) {
1304 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
Gabor Greife9ecc682008-04-06 20:25:17 +00001305 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty);
Reid Spenceree3c9912006-12-04 05:19:50 +00001306 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001307
Reid Spenceree3c9912006-12-04 05:19:50 +00001308 // The compare instructions are weird. We have to encode the predicate
1309 // value and it is combined with the instruction opcode by multiplying
1310 // the opcode by one hundred. We must decode this to get the predicate.
1311 if (V.opcode == Instruction::ICmp)
Nate Begemand2195702008-05-12 19:01:56 +00001312 return new CompareConstantExpr(Ty, Instruction::ICmp, V.predicate,
Reid Spenceree3c9912006-12-04 05:19:50 +00001313 V.operands[0], V.operands[1]);
1314 if (V.opcode == Instruction::FCmp)
Nate Begemand2195702008-05-12 19:01:56 +00001315 return new CompareConstantExpr(Ty, Instruction::FCmp, V.predicate,
1316 V.operands[0], V.operands[1]);
Torok Edwinfbcc6632009-07-14 16:55:14 +00001317 llvm_unreachable("Invalid ConstantExpr!");
Jeff Cohen9f469632006-12-15 21:47:01 +00001318 return 0;
Chris Lattnerb50d1352003-10-05 00:17:43 +00001319 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001320 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001321
Chris Lattner189d19f2003-11-21 20:23:48 +00001322 template<>
1323 struct ConvertConstantType<ConstantExpr, Type> {
1324 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1325 Constant *New;
1326 switch (OldC->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001327 case Instruction::Trunc:
1328 case Instruction::ZExt:
1329 case Instruction::SExt:
1330 case Instruction::FPTrunc:
1331 case Instruction::FPExt:
1332 case Instruction::UIToFP:
1333 case Instruction::SIToFP:
1334 case Instruction::FPToUI:
1335 case Instruction::FPToSI:
1336 case Instruction::PtrToInt:
1337 case Instruction::IntToPtr:
1338 case Instruction::BitCast:
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001339 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001340 NewTy);
Chris Lattner189d19f2003-11-21 20:23:48 +00001341 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001342 case Instruction::Select:
1343 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1344 OldC->getOperand(1),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001345 OldC->getOperand(2));
Chris Lattner6e415c02004-03-12 05:54:04 +00001346 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001347 default:
1348 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001349 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner189d19f2003-11-21 20:23:48 +00001350 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001351 OldC->getOperand(1));
Chris Lattner189d19f2003-11-21 20:23:48 +00001352 break;
1353 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001354 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001355 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
Chris Lattner302116a2007-01-31 04:40:28 +00001356 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001357 &Idx[0], Idx.size());
Chris Lattner189d19f2003-11-21 20:23:48 +00001358 break;
1359 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001360
Chris Lattner189d19f2003-11-21 20:23:48 +00001361 assert(New != OldC && "Didn't replace constant??");
1362 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001363 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner189d19f2003-11-21 20:23:48 +00001364 }
1365 };
1366} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001367
1368
Chris Lattner3e650af2004-08-04 04:48:01 +00001369static ExprMapKeyType getValType(ConstantExpr *CE) {
1370 std::vector<Constant*> Operands;
1371 Operands.reserve(CE->getNumOperands());
1372 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1373 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spenceree3c9912006-12-04 05:19:50 +00001374 return ExprMapKeyType(CE->getOpcode(), Operands,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001375 CE->isCompare() ? CE->getPredicate() : 0,
1376 CE->hasIndices() ?
1377 CE->getIndices() : SmallVector<unsigned, 4>());
Chris Lattner3e650af2004-08-04 04:48:01 +00001378}
1379
Chris Lattner69edc982006-09-28 00:35:06 +00001380static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1381 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001382
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001383/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands7d6c8ae2008-03-30 19:38:55 +00001384/// cast in the ExprConstants map. It is used by the various get* methods below.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001385static inline Constant *getFoldedCast(
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001386 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001387 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001388 // Fold a few common cases
Owen Anderson53a52212009-07-13 04:09:18 +00001389 if (Constant *FC =
1390 ConstantFoldCastInstruction(getGlobalContext(), opc, C, Ty))
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001391 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001392
Vikram S. Adve4c485332002-07-15 18:19:33 +00001393 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001394 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00001395 ExprMapKeyType Key(opc, argVec);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001396
Owen Anderson61794042009-06-17 20:10:08 +00001397 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001398 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001399}
Reid Spencerf37dc652006-12-05 19:14:13 +00001400
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001401Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001402 Instruction::CastOps opc = Instruction::CastOps(oc);
1403 assert(Instruction::isCast(opc) && "opcode out of range");
1404 assert(C && Ty && "Null arguments to getCast");
1405 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1406
1407 switch (opc) {
1408 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00001409 llvm_unreachable("Invalid cast opcode");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001410 break;
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001411 case Instruction::Trunc: return getTrunc(C, Ty);
1412 case Instruction::ZExt: return getZExt(C, Ty);
1413 case Instruction::SExt: return getSExt(C, Ty);
1414 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1415 case Instruction::FPExt: return getFPExtend(C, Ty);
1416 case Instruction::UIToFP: return getUIToFP(C, Ty);
1417 case Instruction::SIToFP: return getSIToFP(C, Ty);
1418 case Instruction::FPToUI: return getFPToUI(C, Ty);
1419 case Instruction::FPToSI: return getFPToSI(C, Ty);
1420 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1421 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1422 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001423 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001424 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00001425}
1426
Reid Spencer5c140882006-12-04 20:17:56 +00001427Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001428 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00001429 return getCast(Instruction::BitCast, C, Ty);
1430 return getCast(Instruction::ZExt, C, Ty);
1431}
1432
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001433Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001434 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001435 return getCast(Instruction::BitCast, C, Ty);
1436 return getCast(Instruction::SExt, C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001437}
1438
1439Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001440 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer5c140882006-12-04 20:17:56 +00001441 return getCast(Instruction::BitCast, C, Ty);
1442 return getCast(Instruction::Trunc, C, Ty);
1443}
1444
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001445Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
Reid Spencerbc245a02006-12-05 03:25:26 +00001446 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001447 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001448
Chris Lattner03c49532007-01-15 02:27:26 +00001449 if (Ty->isInteger())
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001450 return getCast(Instruction::PtrToInt, S, Ty);
1451 return getCast(Instruction::BitCast, S, Ty);
Reid Spencerbc245a02006-12-05 03:25:26 +00001452}
1453
Reid Spencer56521c42006-12-12 00:51:07 +00001454Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1455 bool isSigned) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001456 assert(C->getType()->isIntOrIntVector() &&
1457 Ty->isIntOrIntVector() && "Invalid cast");
1458 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1459 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer56521c42006-12-12 00:51:07 +00001460 Instruction::CastOps opcode =
1461 (SrcBits == DstBits ? Instruction::BitCast :
1462 (SrcBits > DstBits ? Instruction::Trunc :
1463 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1464 return getCast(opcode, C, Ty);
1465}
1466
1467Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001468 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer56521c42006-12-12 00:51:07 +00001469 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001470 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1471 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001472 if (SrcBits == DstBits)
1473 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001474 Instruction::CastOps opcode =
Reid Spencerca104e82006-12-12 05:38:50 +00001475 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001476 return getCast(opcode, C, Ty);
1477}
1478
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001479Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001480#ifndef NDEBUG
1481 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1482 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1483#endif
1484 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1485 assert(C->getType()->isIntOrIntVector() && "Trunc operand must be integer");
1486 assert(Ty->isIntOrIntVector() && "Trunc produces only integral");
1487 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001488 "SrcTy must be larger than DestTy for Trunc!");
1489
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001490 return getFoldedCast(Instruction::Trunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001491}
1492
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001493Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001494#ifndef NDEBUG
1495 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1496 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1497#endif
1498 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1499 assert(C->getType()->isIntOrIntVector() && "SExt operand must be integral");
1500 assert(Ty->isIntOrIntVector() && "SExt produces only integer");
1501 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001502 "SrcTy must be smaller than DestTy for SExt!");
1503
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001504 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerdd284742004-04-04 23:20:30 +00001505}
1506
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001507Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001508#ifndef NDEBUG
1509 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1510 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1511#endif
1512 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1513 assert(C->getType()->isIntOrIntVector() && "ZEXt operand must be integral");
1514 assert(Ty->isIntOrIntVector() && "ZExt produces only integer");
1515 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001516 "SrcTy must be smaller than DestTy for ZExt!");
1517
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001518 return getFoldedCast(Instruction::ZExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001519}
1520
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001521Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001522#ifndef NDEBUG
1523 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1524 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1525#endif
1526 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1527 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1528 C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001529 "This is an illegal floating point truncation!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001530 return getFoldedCast(Instruction::FPTrunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001531}
1532
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001533Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001534#ifndef NDEBUG
1535 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1536 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1537#endif
1538 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1539 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1540 C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001541 "This is an illegal floating point extension!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001542 return getFoldedCast(Instruction::FPExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001543}
1544
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001545Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001546#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001547 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1548 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001549#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001550 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1551 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
1552 "This is an illegal uint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001553 return getFoldedCast(Instruction::UIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001554}
1555
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001556Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001557#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001558 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1559 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001560#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001561 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1562 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001563 "This is an illegal sint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001564 return getFoldedCast(Instruction::SIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001565}
1566
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001567Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001568#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001569 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1570 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001571#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001572 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1573 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1574 "This is an illegal floating point to uint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001575 return getFoldedCast(Instruction::FPToUI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001576}
1577
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001578Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001579#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001580 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1581 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001582#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001583 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1584 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1585 "This is an illegal floating point to sint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001586 return getFoldedCast(Instruction::FPToSI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001587}
1588
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001589Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001590 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner03c49532007-01-15 02:27:26 +00001591 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001592 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001593}
1594
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001595Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner03c49532007-01-15 02:27:26 +00001596 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001597 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001598 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001599}
1600
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001601Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001602 // BitCast implies a no-op cast of type only. No bits change. However, you
1603 // can't cast pointers to anything but pointers.
Devang Pateld26344d2008-11-03 23:20:04 +00001604#ifndef NDEBUG
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001605 const Type *SrcTy = C->getType();
1606 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer5c140882006-12-04 20:17:56 +00001607 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001608
1609 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
1610 // or nonptr->ptr). For all the other types, the cast is okay if source and
1611 // destination bit widths are identical.
1612 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1613 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Devang Pateld26344d2008-11-03 23:20:04 +00001614#endif
Chris Lattnere4086012009-03-08 04:06:26 +00001615 assert(SrcBitSize == DstBitSize && "BitCast requires types of same width");
Chris Lattnercbeda872009-03-21 06:55:54 +00001616
1617 // It is common to ask for a bitcast of a value to its own type, handle this
1618 // speedily.
1619 if (C->getType() == DstTy) return C;
1620
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001621 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerdd284742004-04-04 23:20:30 +00001622}
1623
Chris Lattnerb50d1352003-10-05 00:17:43 +00001624Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001625 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001626 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00001627 assert(Opcode >= Instruction::BinaryOpsBegin &&
1628 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001629 "Invalid opcode in binary constant expression");
1630 assert(C1->getType() == C2->getType() &&
1631 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001632
Reid Spencer542964f2007-01-11 18:21:29 +00001633 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Owen Anderson53a52212009-07-13 04:09:18 +00001634 if (Constant *FC = ConstantFoldBinaryInstruction(
1635 getGlobalContext(), Opcode, C1, C2))
Chris Lattnerb50d1352003-10-05 00:17:43 +00001636 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001637
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001638 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001639 ExprMapKeyType Key(Opcode, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00001640
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001641 // Implicitly locked.
1642 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001643}
1644
Reid Spencer266e42b2006-12-23 06:05:41 +00001645Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Nate Begeman098cc6f2008-07-25 17:56:27 +00001646 Constant *C1, Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001647 switch (predicate) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001648 default: llvm_unreachable("Invalid CmpInst predicate");
Nate Begemanc96e2e42008-07-25 17:35:37 +00001649 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
1650 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
1651 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
1652 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
1653 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
1654 case CmpInst::FCMP_TRUE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001655 return getFCmp(predicate, C1, C2);
1656
Nate Begemanc96e2e42008-07-25 17:35:37 +00001657 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
1658 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
1659 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
1660 case CmpInst::ICMP_SLE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001661 return getICmp(predicate, C1, C2);
Reid Spencer266e42b2006-12-23 06:05:41 +00001662 }
Reid Spencera009d0d2006-12-04 21:35:24 +00001663}
1664
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001665Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001666 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1667 if (C1->getType()->isFPOrFPVector()) {
1668 if (Opcode == Instruction::Add) Opcode = Instruction::FAdd;
1669 else if (Opcode == Instruction::Sub) Opcode = Instruction::FSub;
1670 else if (Opcode == Instruction::Mul) Opcode = Instruction::FMul;
1671 }
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001672#ifndef NDEBUG
1673 switch (Opcode) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001674 case Instruction::Add:
Reid Spencer7eb55b32006-11-02 01:53:59 +00001675 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001676 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001677 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmana5b96452009-06-04 22:49:04 +00001678 assert(C1->getType()->isIntOrIntVector() &&
1679 "Tried to create an integer operation on a non-integer type!");
1680 break;
1681 case Instruction::FAdd:
1682 case Instruction::FSub:
1683 case Instruction::FMul:
1684 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1685 assert(C1->getType()->isFPOrFPVector() &&
1686 "Tried to create a floating-point operation on a "
1687 "non-floating-point type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001688 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001689 case Instruction::UDiv:
1690 case Instruction::SDiv:
1691 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001692 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001693 "Tried to create an arithmetic operation on a non-arithmetic type!");
1694 break;
1695 case Instruction::FDiv:
1696 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001697 assert(C1->getType()->isFPOrFPVector() &&
1698 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001699 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001700 case Instruction::URem:
1701 case Instruction::SRem:
1702 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001703 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001704 "Tried to create an arithmetic operation on a non-arithmetic type!");
1705 break;
1706 case Instruction::FRem:
1707 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001708 assert(C1->getType()->isFPOrFPVector() &&
1709 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001710 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001711 case Instruction::And:
1712 case Instruction::Or:
1713 case Instruction::Xor:
1714 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001715 assert(C1->getType()->isIntOrIntVector() &&
Misha Brukman3852f652005-01-27 06:46:38 +00001716 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001717 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001718 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001719 case Instruction::LShr:
1720 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00001721 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohman79975d52009-03-14 17:09:17 +00001722 assert(C1->getType()->isIntOrIntVector() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001723 "Tried to create a shift operation on a non-integer type!");
1724 break;
1725 default:
1726 break;
1727 }
1728#endif
1729
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001730 return getTy(C1->getType(), Opcode, C1, C2);
Reid Spencera009d0d2006-12-04 21:35:24 +00001731}
1732
Owen Anderson487375e2009-07-29 18:55:55 +00001733Constant* ConstantExpr::getSizeOf(const Type* Ty) {
1734 // sizeof is implemented as: (i64) gep (Ty*)null, 1
1735 // Note that a non-inbounds gep is used, as null isn't within any object.
1736 LLVMContext &Context = Ty->getContext();
1737 Constant *GEPIdx = ConstantInt::get(Type::Int32Ty, 1);
1738 Constant *GEP = getGetElementPtr(
1739 Context.getNullValue(PointerType::getUnqual(Ty)), &GEPIdx, 1);
1740 return getCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
1741}
1742
1743Constant* ConstantExpr::getAlignOf(const Type* Ty) {
1744 LLVMContext &Context = Ty->getContext();
1745 // alignof is implemented as: (i64) gep ({i8,Ty}*)null, 0, 1
1746 const Type *AligningTy = StructType::get(Type::Int8Ty, Ty, NULL);
1747 Constant *NullPtr = Context.getNullValue(AligningTy->getPointerTo());
1748 Constant *Zero = ConstantInt::get(Type::Int32Ty, 0);
1749 Constant *One = ConstantInt::get(Type::Int32Ty, 1);
1750 Constant *Indices[2] = { Zero, One };
1751 Constant *GEP = getGetElementPtr(NullPtr, Indices, 2);
1752 return getCast(Instruction::PtrToInt, GEP, Type::Int32Ty);
1753}
1754
1755
Reid Spencer266e42b2006-12-23 06:05:41 +00001756Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00001757 Constant *C1, Constant *C2) {
1758 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001759 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00001760}
1761
Chris Lattner6e415c02004-03-12 05:54:04 +00001762Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001763 Constant *V1, Constant *V2) {
Chris Lattner41632132008-12-29 00:16:12 +00001764 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner6e415c02004-03-12 05:54:04 +00001765
1766 if (ReqTy == V1->getType())
Owen Anderson53a52212009-07-13 04:09:18 +00001767 if (Constant *SC = ConstantFoldSelectInstruction(
1768 getGlobalContext(), C, V1, V2))
Chris Lattner6e415c02004-03-12 05:54:04 +00001769 return SC; // Fold common cases
1770
1771 std::vector<Constant*> argVec(3, C);
1772 argVec[1] = V1;
1773 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00001774 ExprMapKeyType Key(Instruction::Select, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00001775
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001776 // Implicitly locked.
1777 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00001778}
1779
Chris Lattnerb50d1352003-10-05 00:17:43 +00001780Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00001781 Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001782 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001783 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
1784 Idxs+NumIdx) ==
1785 cast<PointerType>(ReqTy)->getElementType() &&
1786 "GEP indices invalid!");
Chris Lattner04b60fe2004-02-16 20:46:13 +00001787
Owen Anderson53a52212009-07-13 04:09:18 +00001788 if (Constant *FC = ConstantFoldGetElementPtr(
1789 getGlobalContext(), C, (Constant**)Idxs, NumIdx))
Chris Lattneracdbe712003-04-17 19:24:48 +00001790 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001791
Chris Lattnerb50d1352003-10-05 00:17:43 +00001792 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001793 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001794 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00001795 std::vector<Constant*> ArgVec;
Chris Lattner302116a2007-01-31 04:40:28 +00001796 ArgVec.reserve(NumIdx+1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001797 ArgVec.push_back(C);
Chris Lattner302116a2007-01-31 04:40:28 +00001798 for (unsigned i = 0; i != NumIdx; ++i)
1799 ArgVec.push_back(cast<Constant>(Idxs[i]));
1800 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001801
1802 // Implicitly locked.
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001803 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001804}
1805
Chris Lattner302116a2007-01-31 04:40:28 +00001806Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001807 unsigned NumIdx) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00001808 // Get the result type of the getelementptr!
Chris Lattner302116a2007-01-31 04:40:28 +00001809 const Type *Ty =
Dan Gohman12fce772008-05-15 19:50:34 +00001810 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001811 assert(Ty && "GEP indices invalid!");
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001812 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001813 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
Chris Lattner13128ab2004-10-11 22:52:25 +00001814}
1815
Chris Lattner302116a2007-01-31 04:40:28 +00001816Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001817 unsigned NumIdx) {
1818 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001819}
1820
Chris Lattner302116a2007-01-31 04:40:28 +00001821
Reid Spenceree3c9912006-12-04 05:19:50 +00001822Constant *
1823ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1824 assert(LHS->getType() == RHS->getType());
1825 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
1826 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
1827
Owen Anderson53a52212009-07-13 04:09:18 +00001828 if (Constant *FC = ConstantFoldCompareInstruction(
1829 getGlobalContext(),pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001830 return FC; // Fold a few common cases...
1831
1832 // Look up the constant in the table first to ensure uniqueness
1833 std::vector<Constant*> ArgVec;
1834 ArgVec.push_back(LHS);
1835 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001836 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001837 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00001838
1839 // Implicitly locked.
1840 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001841}
1842
1843Constant *
1844ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1845 assert(LHS->getType() == RHS->getType());
1846 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
1847
Owen Anderson53a52212009-07-13 04:09:18 +00001848 if (Constant *FC = ConstantFoldCompareInstruction(
1849 getGlobalContext(), pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001850 return FC; // Fold a few common cases...
1851
1852 // Look up the constant in the table first to ensure uniqueness
1853 std::vector<Constant*> ArgVec;
1854 ArgVec.push_back(LHS);
1855 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001856 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001857 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00001858
1859 // Implicitly locked.
1860 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001861}
1862
Robert Bocchino23004482006-01-10 19:05:34 +00001863Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
1864 Constant *Idx) {
Owen Anderson53a52212009-07-13 04:09:18 +00001865 if (Constant *FC = ConstantFoldExtractElementInstruction(
1866 getGlobalContext(), Val, Idx))
Robert Bocchinode7f1c92006-01-10 20:03:46 +00001867 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00001868 // Look up the constant in the table first to ensure uniqueness
1869 std::vector<Constant*> ArgVec(1, Val);
1870 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001871 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001872
1873 // Implicitly locked.
1874 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00001875}
1876
1877Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001878 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001879 "Tried to create extractelement operation on non-vector type!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001880 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00001881 "Extractelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001882 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +00001883 Val, Idx);
1884}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001885
Robert Bocchinoca27f032006-01-17 20:07:22 +00001886Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
1887 Constant *Elt, Constant *Idx) {
Owen Anderson53a52212009-07-13 04:09:18 +00001888 if (Constant *FC = ConstantFoldInsertElementInstruction(
1889 getGlobalContext(), Val, Elt, Idx))
Robert Bocchinoca27f032006-01-17 20:07:22 +00001890 return FC; // Fold a few common cases...
1891 // Look up the constant in the table first to ensure uniqueness
1892 std::vector<Constant*> ArgVec(1, Val);
1893 ArgVec.push_back(Elt);
1894 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001895 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001896
1897 // Implicitly locked.
1898 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001899}
1900
1901Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1902 Constant *Idx) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001903 assert(isa<VectorType>(Val->getType()) &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001904 "Tried to create insertelement operation on non-vector type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001905 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoca27f032006-01-17 20:07:22 +00001906 && "Insertelement types must match!");
Reid Spencer8d9336d2006-12-31 05:26:44 +00001907 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer2546b762007-01-26 07:37:34 +00001908 "Insertelement index must be i32 type!");
Gordon Henriksenb52d1ed2008-08-30 15:41:51 +00001909 return getInsertElementTy(Val->getType(), Val, Elt, Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001910}
1911
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001912Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
1913 Constant *V2, Constant *Mask) {
Owen Anderson53a52212009-07-13 04:09:18 +00001914 if (Constant *FC = ConstantFoldShuffleVectorInstruction(
1915 getGlobalContext(), V1, V2, Mask))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001916 return FC; // Fold a few common cases...
1917 // Look up the constant in the table first to ensure uniqueness
1918 std::vector<Constant*> ArgVec(1, V1);
1919 ArgVec.push_back(V2);
1920 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00001921 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001922
1923 // Implicitly locked.
1924 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001925}
1926
1927Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
1928 Constant *Mask) {
1929 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
1930 "Invalid shuffle vector constant expr operands!");
Nate Begeman94aa38d2009-02-12 21:28:33 +00001931
1932 unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements();
1933 const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
1934 const Type *ShufTy = VectorType::get(EltTy, NElts);
1935 return getShuffleVectorTy(ShufTy, V1, V2, Mask);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001936}
1937
Dan Gohman12fce772008-05-15 19:50:34 +00001938Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg,
1939 Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001940 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001941 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
1942 Idxs+NumIdx) == Val->getType() &&
1943 "insertvalue indices invalid!");
1944 assert(Agg->getType() == ReqTy &&
1945 "insertvalue type invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00001946 assert(Agg->getType()->isFirstClassType() &&
1947 "Non-first-class type for constant InsertValue expression");
Owen Anderson53a52212009-07-13 04:09:18 +00001948 Constant *FC = ConstantFoldInsertValueInstruction(
1949 getGlobalContext(), Agg, Val, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00001950 assert(FC && "InsertValue constant expr couldn't be folded!");
1951 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00001952}
1953
1954Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001955 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00001956 assert(Agg->getType()->isFirstClassType() &&
1957 "Tried to create insertelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00001958
Dan Gohman0752bff2008-05-23 00:36:11 +00001959 const Type *ReqTy = Agg->getType();
Devang Pateld26344d2008-11-03 23:20:04 +00001960#ifndef NDEBUG
Dan Gohman0752bff2008-05-23 00:36:11 +00001961 const Type *ValTy =
Dan Gohman12fce772008-05-15 19:50:34 +00001962 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
Devang Pateld26344d2008-11-03 23:20:04 +00001963#endif
Dan Gohman0752bff2008-05-23 00:36:11 +00001964 assert(ValTy == Val->getType() && "insertvalue indices invalid!");
Dan Gohman12fce772008-05-15 19:50:34 +00001965 return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx);
1966}
1967
1968Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001969 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001970 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
1971 Idxs+NumIdx) == ReqTy &&
1972 "extractvalue indices invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00001973 assert(Agg->getType()->isFirstClassType() &&
1974 "Non-first-class type for constant extractvalue expression");
Owen Anderson53a52212009-07-13 04:09:18 +00001975 Constant *FC = ConstantFoldExtractValueInstruction(
1976 getGlobalContext(), Agg, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00001977 assert(FC && "ExtractValue constant expr couldn't be folded!");
1978 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00001979}
1980
1981Constant *ConstantExpr::getExtractValue(Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001982 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00001983 assert(Agg->getType()->isFirstClassType() &&
1984 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00001985
1986 const Type *ReqTy =
1987 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
1988 assert(ReqTy && "extractvalue indices invalid!");
1989 return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx);
1990}
1991
Owen Anderson487375e2009-07-29 18:55:55 +00001992Constant* ConstantExpr::getNeg(Constant* C) {
1993 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1994 if (C->getType()->isFPOrFPVector())
1995 return getFNeg(C);
1996 assert(C->getType()->isIntOrIntVector() &&
1997 "Cannot NEG a nonintegral value!");
1998 return get(Instruction::Sub,
1999 ConstantFP::getZeroValueForNegation(C->getType()),
2000 C);
2001}
2002
2003Constant* ConstantExpr::getFNeg(Constant* C) {
2004 assert(C->getType()->isFPOrFPVector() &&
2005 "Cannot FNEG a non-floating-point value!");
2006 return get(Instruction::FSub,
2007 ConstantFP::getZeroValueForNegation(C->getType()),
2008 C);
2009}
2010
2011Constant* ConstantExpr::getNot(Constant* C) {
2012 assert(C->getType()->isIntOrIntVector() &&
2013 "Cannot NOT a nonintegral value!");
2014 LLVMContext &Context = C->getType()->getContext();
2015 return get(Instruction::Xor, C, Context.getAllOnesValue(C->getType()));
2016}
2017
2018Constant* ConstantExpr::getAdd(Constant* C1, Constant* C2) {
2019 return get(Instruction::Add, C1, C2);
2020}
2021
2022Constant* ConstantExpr::getFAdd(Constant* C1, Constant* C2) {
2023 return get(Instruction::FAdd, C1, C2);
2024}
2025
2026Constant* ConstantExpr::getSub(Constant* C1, Constant* C2) {
2027 return get(Instruction::Sub, C1, C2);
2028}
2029
2030Constant* ConstantExpr::getFSub(Constant* C1, Constant* C2) {
2031 return get(Instruction::FSub, C1, C2);
2032}
2033
2034Constant* ConstantExpr::getMul(Constant* C1, Constant* C2) {
2035 return get(Instruction::Mul, C1, C2);
2036}
2037
2038Constant* ConstantExpr::getFMul(Constant* C1, Constant* C2) {
2039 return get(Instruction::FMul, C1, C2);
2040}
2041
2042Constant* ConstantExpr::getUDiv(Constant* C1, Constant* C2) {
2043 return get(Instruction::UDiv, C1, C2);
2044}
2045
2046Constant* ConstantExpr::getSDiv(Constant* C1, Constant* C2) {
2047 return get(Instruction::SDiv, C1, C2);
2048}
2049
2050Constant* ConstantExpr::getFDiv(Constant* C1, Constant* C2) {
2051 return get(Instruction::FDiv, C1, C2);
2052}
2053
2054Constant* ConstantExpr::getURem(Constant* C1, Constant* C2) {
2055 return get(Instruction::URem, C1, C2);
2056}
2057
2058Constant* ConstantExpr::getSRem(Constant* C1, Constant* C2) {
2059 return get(Instruction::SRem, C1, C2);
2060}
2061
2062Constant* ConstantExpr::getFRem(Constant* C1, Constant* C2) {
2063 return get(Instruction::FRem, C1, C2);
2064}
2065
2066Constant* ConstantExpr::getAnd(Constant* C1, Constant* C2) {
2067 return get(Instruction::And, C1, C2);
2068}
2069
2070Constant* ConstantExpr::getOr(Constant* C1, Constant* C2) {
2071 return get(Instruction::Or, C1, C2);
2072}
2073
2074Constant* ConstantExpr::getXor(Constant* C1, Constant* C2) {
2075 return get(Instruction::Xor, C1, C2);
2076}
2077
2078Constant* ConstantExpr::getShl(Constant* C1, Constant* C2) {
2079 return get(Instruction::Shl, C1, C2);
2080}
2081
2082Constant* ConstantExpr::getLShr(Constant* C1, Constant* C2) {
2083 return get(Instruction::LShr, C1, C2);
2084}
2085
2086Constant* ConstantExpr::getAShr(Constant* C1, Constant* C2) {
2087 return get(Instruction::AShr, C1, C2);
2088}
2089
Vikram S. Adve4c485332002-07-15 18:19:33 +00002090// destroyConstant - Remove the constant from the constant table...
2091//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00002092void ConstantExpr::destroyConstant() {
2093 // Implicitly locked.
2094 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00002095 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002096}
2097
Chris Lattner3cd8c562002-07-30 18:54:25 +00002098const char *ConstantExpr::getOpcodeName() const {
2099 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002100}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00002101
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002102//===----------------------------------------------------------------------===//
2103// replaceUsesOfWithOnConstant implementations
2104
Chris Lattner913849b2007-08-21 00:55:23 +00002105/// replaceUsesOfWithOnConstant - Update this constant array to change uses of
2106/// 'From' to be uses of 'To'. This must update the uniquing data structures
2107/// etc.
2108///
2109/// Note that we intentionally replace all uses of From with To here. Consider
2110/// a large array that uses 'From' 1000 times. By handling this case all here,
2111/// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that
2112/// single invocation handles all 1000 uses. Handling them one at a time would
2113/// work, but would be really slow because it would have to unique each updated
2114/// array instance.
Owen Andersonc2c79322009-07-28 18:32:17 +00002115
2116static std::vector<Constant*> getValType(ConstantArray *CA) {
2117 std::vector<Constant*> Elements;
2118 Elements.reserve(CA->getNumOperands());
2119 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
2120 Elements.push_back(cast<Constant>(CA->getOperand(i)));
2121 return Elements;
2122}
2123
2124
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002125void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002126 Use *U) {
Owen Andersonc2c79322009-07-28 18:32:17 +00002127 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2128 Constant *ToC = cast<Constant>(To);
2129
2130 LLVMContext &Context = getType()->getContext();
2131 LLVMContextImpl *pImpl = Context.pImpl;
2132
2133 std::pair<LLVMContextImpl::ArrayConstantsTy::MapKey, Constant*> Lookup;
2134 Lookup.first.first = getType();
2135 Lookup.second = this;
2136
2137 std::vector<Constant*> &Values = Lookup.first.second;
2138 Values.reserve(getNumOperands()); // Build replacement array.
2139
2140 // Fill values with the modified operands of the constant array. Also,
2141 // compute whether this turns into an all-zeros array.
2142 bool isAllZeros = false;
2143 unsigned NumUpdated = 0;
2144 if (!ToC->isNullValue()) {
2145 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2146 Constant *Val = cast<Constant>(O->get());
2147 if (Val == From) {
2148 Val = ToC;
2149 ++NumUpdated;
2150 }
2151 Values.push_back(Val);
2152 }
2153 } else {
2154 isAllZeros = true;
2155 for (Use *O = OperandList, *E = OperandList+getNumOperands();O != E; ++O) {
2156 Constant *Val = cast<Constant>(O->get());
2157 if (Val == From) {
2158 Val = ToC;
2159 ++NumUpdated;
2160 }
2161 Values.push_back(Val);
2162 if (isAllZeros) isAllZeros = Val->isNullValue();
2163 }
2164 }
2165
2166 Constant *Replacement = 0;
2167 if (isAllZeros) {
Owen Andersonb292b8c2009-07-30 23:03:37 +00002168 Replacement = ConstantAggregateZero::get(getType());
Owen Andersonc2c79322009-07-28 18:32:17 +00002169 } else {
2170 // Check to see if we have this array type already.
2171 sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
2172 bool Exists;
2173 LLVMContextImpl::ArrayConstantsTy::MapTy::iterator I =
2174 pImpl->ArrayConstants.InsertOrGetItem(Lookup, Exists);
2175
2176 if (Exists) {
2177 Replacement = I->second;
2178 } else {
2179 // Okay, the new shape doesn't exist in the system yet. Instead of
2180 // creating a new constant array, inserting it, replaceallusesof'ing the
2181 // old with the new, then deleting the old... just update the current one
2182 // in place!
2183 pImpl->ArrayConstants.MoveConstantToNewSlot(this, I);
2184
2185 // Update to the new value. Optimize for the case when we have a single
2186 // operand that we're changing, but handle bulk updates efficiently.
2187 if (NumUpdated == 1) {
2188 unsigned OperandToUpdate = U - OperandList;
2189 assert(getOperand(OperandToUpdate) == From &&
2190 "ReplaceAllUsesWith broken!");
2191 setOperand(OperandToUpdate, ToC);
2192 } else {
2193 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2194 if (getOperand(i) == From)
2195 setOperand(i, ToC);
2196 }
2197 return;
2198 }
2199 }
Chris Lattnerb64419a2005-10-03 22:51:37 +00002200
2201 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002202 assert(Replacement != this && "I didn't contain From!");
2203
Chris Lattner7a1450d2005-10-04 18:13:04 +00002204 // Everyone using this now uses the replacement.
2205 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002206
2207 // Delete the old constant!
2208 destroyConstant();
2209}
2210
Owen Anderson45308b52009-07-27 22:29:26 +00002211static std::vector<Constant*> getValType(ConstantStruct *CS) {
2212 std::vector<Constant*> Elements;
2213 Elements.reserve(CS->getNumOperands());
2214 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
2215 Elements.push_back(cast<Constant>(CS->getOperand(i)));
2216 return Elements;
2217}
2218
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002219void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002220 Use *U) {
Owen Anderson45308b52009-07-27 22:29:26 +00002221 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2222 Constant *ToC = cast<Constant>(To);
2223
2224 unsigned OperandToUpdate = U-OperandList;
2225 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
2226
2227 std::pair<LLVMContextImpl::StructConstantsTy::MapKey, Constant*> Lookup;
2228 Lookup.first.first = getType();
2229 Lookup.second = this;
2230 std::vector<Constant*> &Values = Lookup.first.second;
2231 Values.reserve(getNumOperands()); // Build replacement struct.
2232
2233
2234 // Fill values with the modified operands of the constant struct. Also,
2235 // compute whether this turns into an all-zeros struct.
2236 bool isAllZeros = false;
2237 if (!ToC->isNullValue()) {
2238 for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O)
2239 Values.push_back(cast<Constant>(O->get()));
2240 } else {
2241 isAllZeros = true;
2242 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2243 Constant *Val = cast<Constant>(O->get());
2244 Values.push_back(Val);
2245 if (isAllZeros) isAllZeros = Val->isNullValue();
2246 }
2247 }
2248 Values[OperandToUpdate] = ToC;
2249
2250 LLVMContext &Context = getType()->getContext();
2251 LLVMContextImpl *pImpl = Context.pImpl;
2252
2253 Constant *Replacement = 0;
2254 if (isAllZeros) {
Owen Andersonb292b8c2009-07-30 23:03:37 +00002255 Replacement = ConstantAggregateZero::get(getType());
Owen Anderson45308b52009-07-27 22:29:26 +00002256 } else {
2257 // Check to see if we have this array type already.
2258 sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
2259 bool Exists;
2260 LLVMContextImpl::StructConstantsTy::MapTy::iterator I =
2261 pImpl->StructConstants.InsertOrGetItem(Lookup, Exists);
2262
2263 if (Exists) {
2264 Replacement = I->second;
2265 } else {
2266 // Okay, the new shape doesn't exist in the system yet. Instead of
2267 // creating a new constant struct, inserting it, replaceallusesof'ing the
2268 // old with the new, then deleting the old... just update the current one
2269 // in place!
2270 pImpl->StructConstants.MoveConstantToNewSlot(this, I);
2271
2272 // Update to the new value.
2273 setOperand(OperandToUpdate, ToC);
2274 return;
2275 }
2276 }
2277
2278 assert(Replacement != this && "I didn't contain From!");
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002279
Chris Lattner7a1450d2005-10-04 18:13:04 +00002280 // Everyone using this now uses the replacement.
2281 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002282
2283 // Delete the old constant!
2284 destroyConstant();
2285}
2286
Owen Anderson4aa32952009-07-28 21:19:26 +00002287static std::vector<Constant*> getValType(ConstantVector *CP) {
2288 std::vector<Constant*> Elements;
2289 Elements.reserve(CP->getNumOperands());
2290 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
2291 Elements.push_back(CP->getOperand(i));
2292 return Elements;
2293}
2294
Reid Spencerd84d35b2007-02-15 02:26:10 +00002295void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002296 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002297 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2298
2299 std::vector<Constant*> Values;
2300 Values.reserve(getNumOperands()); // Build replacement array...
2301 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2302 Constant *Val = getOperand(i);
2303 if (Val == From) Val = cast<Constant>(To);
2304 Values.push_back(Val);
2305 }
2306
Owen Anderson4aa32952009-07-28 21:19:26 +00002307 Constant *Replacement = get(getType(), Values);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002308 assert(Replacement != this && "I didn't contain From!");
2309
Chris Lattner7a1450d2005-10-04 18:13:04 +00002310 // Everyone using this now uses the replacement.
2311 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002312
2313 // Delete the old constant!
2314 destroyConstant();
2315}
2316
2317void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002318 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002319 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2320 Constant *To = cast<Constant>(ToV);
2321
2322 Constant *Replacement = 0;
2323 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerb5d70302007-02-19 20:01:23 +00002324 SmallVector<Constant*, 8> Indices;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002325 Constant *Pointer = getOperand(0);
2326 Indices.reserve(getNumOperands()-1);
2327 if (Pointer == From) Pointer = To;
2328
2329 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2330 Constant *Val = getOperand(i);
2331 if (Val == From) Val = To;
2332 Indices.push_back(Val);
2333 }
Chris Lattnerb5d70302007-02-19 20:01:23 +00002334 Replacement = ConstantExpr::getGetElementPtr(Pointer,
2335 &Indices[0], Indices.size());
Dan Gohman12fce772008-05-15 19:50:34 +00002336 } else if (getOpcode() == Instruction::ExtractValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002337 Constant *Agg = getOperand(0);
Dan Gohman12fce772008-05-15 19:50:34 +00002338 if (Agg == From) Agg = To;
2339
Dan Gohman1ecaf452008-05-31 00:58:22 +00002340 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002341 Replacement = ConstantExpr::getExtractValue(Agg,
2342 &Indices[0], Indices.size());
2343 } else if (getOpcode() == Instruction::InsertValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002344 Constant *Agg = getOperand(0);
2345 Constant *Val = getOperand(1);
Dan Gohman12fce772008-05-15 19:50:34 +00002346 if (Agg == From) Agg = To;
2347 if (Val == From) Val = To;
2348
Dan Gohman1ecaf452008-05-31 00:58:22 +00002349 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002350 Replacement = ConstantExpr::getInsertValue(Agg, Val,
2351 &Indices[0], Indices.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002352 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002353 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002354 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002355 } else if (getOpcode() == Instruction::Select) {
2356 Constant *C1 = getOperand(0);
2357 Constant *C2 = getOperand(1);
2358 Constant *C3 = getOperand(2);
2359 if (C1 == From) C1 = To;
2360 if (C2 == From) C2 = To;
2361 if (C3 == From) C3 = To;
2362 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00002363 } else if (getOpcode() == Instruction::ExtractElement) {
2364 Constant *C1 = getOperand(0);
2365 Constant *C2 = getOperand(1);
2366 if (C1 == From) C1 = To;
2367 if (C2 == From) C2 = To;
2368 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00002369 } else if (getOpcode() == Instruction::InsertElement) {
2370 Constant *C1 = getOperand(0);
2371 Constant *C2 = getOperand(1);
2372 Constant *C3 = getOperand(1);
2373 if (C1 == From) C1 = To;
2374 if (C2 == From) C2 = To;
2375 if (C3 == From) C3 = To;
2376 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2377 } else if (getOpcode() == Instruction::ShuffleVector) {
2378 Constant *C1 = getOperand(0);
2379 Constant *C2 = getOperand(1);
2380 Constant *C3 = getOperand(2);
2381 if (C1 == From) C1 = To;
2382 if (C2 == From) C2 = To;
2383 if (C3 == From) C3 = To;
2384 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00002385 } else if (isCompare()) {
2386 Constant *C1 = getOperand(0);
2387 Constant *C2 = getOperand(1);
2388 if (C1 == From) C1 = To;
2389 if (C2 == From) C2 = To;
2390 if (getOpcode() == Instruction::ICmp)
2391 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002392 else {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002393 assert(getOpcode() == Instruction::FCmp);
2394 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002395 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002396 } else if (getNumOperands() == 2) {
2397 Constant *C1 = getOperand(0);
2398 Constant *C2 = getOperand(1);
2399 if (C1 == From) C1 = To;
2400 if (C2 == From) C2 = To;
2401 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2402 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002403 llvm_unreachable("Unknown ConstantExpr type!");
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002404 return;
2405 }
2406
2407 assert(Replacement != this && "I didn't contain From!");
2408
Chris Lattner7a1450d2005-10-04 18:13:04 +00002409 // Everyone using this now uses the replacement.
2410 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002411
2412 // Delete the old constant!
2413 destroyConstant();
Matthijs Kooijmanba5d7ef2008-07-03 07:46:41 +00002414}
Nick Lewycky49f89192009-04-04 07:22:01 +00002415