blob: 4f0c411e2faaeb63bfe1aa2138aaca65fc4a1fcf [file] [log] [blame]
Chris Lattner9bc02a42003-05-13 21:37:02 +00001//===-- Constants.cpp - Implement Constant nodes --------------------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
Chris Lattnere9bb2df2001-12-03 22:26:30 +000010// This file implements the Constant* classes...
Chris Lattner00950542001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Owen Andersoneed707b2009-07-24 23:12:02 +000014#include "LLVMContextImpl.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000015#include "llvm/Constants.h"
Chris Lattner92f6fea2007-02-27 03:05:06 +000016#include "ConstantFold.h"
Chris Lattner00950542001-06-06 20:29:01 +000017#include "llvm/DerivedTypes.h"
Reid Spencer1c9c8e62004-07-17 23:48:33 +000018#include "llvm/GlobalValue.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000019#include "llvm/Instructions.h"
Chris Lattnerf5ec48d2001-10-13 06:57:33 +000020#include "llvm/Module.h"
Dan Gohman5a206ee2009-07-18 01:49:22 +000021#include "llvm/Operator.h"
Nick Lewycky21cc4462009-04-04 07:22:01 +000022#include "llvm/ADT/FoldingSet.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/ADT/StringExtras.h"
Nick Lewycky21cc4462009-04-04 07:22:01 +000024#include "llvm/ADT/StringMap.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000025#include "llvm/Support/Compiler.h"
Bill Wendling2e3def12006-11-17 08:03:48 +000026#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000027#include "llvm/Support/ErrorHandling.h"
Chris Lattner8a94bf12006-09-28 00:35:06 +000028#include "llvm/Support/ManagedStatic.h"
Bill Wendling2e3def12006-11-17 08:03:48 +000029#include "llvm/Support/MathExtras.h"
Owen Anderson04fb7c32009-06-20 00:24:58 +000030#include "llvm/System/Mutex.h"
Owen Anderson3e456ab2009-06-17 18:40:29 +000031#include "llvm/System/RWMutex.h"
Owen Andersone3cd5ca2009-06-18 16:54:52 +000032#include "llvm/System/Threading.h"
Chris Lattner6b6f6ba2007-02-20 06:39:57 +000033#include "llvm/ADT/DenseMap.h"
Chris Lattnerf9021ff2007-02-19 20:01:23 +000034#include "llvm/ADT/SmallVector.h"
Chris Lattner00950542001-06-06 20:29:01 +000035#include <algorithm>
Reid Spenceref9b9a72007-02-05 20:47:22 +000036#include <map>
Chris Lattner31f84992003-11-21 20:23:48 +000037using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000038
Chris Lattner00950542001-06-06 20:29:01 +000039//===----------------------------------------------------------------------===//
Chris Lattnere9bb2df2001-12-03 22:26:30 +000040// Constant Class
Chris Lattner00950542001-06-06 20:29:01 +000041//===----------------------------------------------------------------------===//
42
Owen Andersonee6aefc2009-06-18 19:10:19 +000043// Becomes a no-op when multithreading is disabled.
44ManagedStatic<sys::SmartRWMutex<true> > ConstantsLock;
Owen Anderson3e456ab2009-06-17 18:40:29 +000045
Owen Andersona7235ea2009-07-31 20:28:14 +000046// Constructor to create a '0' constant of arbitrary type...
47static const uint64_t zero[2] = {0, 0};
48Constant* Constant::getNullValue(const Type* Ty) {
49 switch (Ty->getTypeID()) {
50 case Type::IntegerTyID:
51 return ConstantInt::get(Ty, 0);
52 case Type::FloatTyID:
53 return ConstantFP::get(Ty->getContext(), APFloat(APInt(32, 0)));
54 case Type::DoubleTyID:
55 return ConstantFP::get(Ty->getContext(), APFloat(APInt(64, 0)));
56 case Type::X86_FP80TyID:
57 return ConstantFP::get(Ty->getContext(), APFloat(APInt(80, 2, zero)));
58 case Type::FP128TyID:
59 return ConstantFP::get(Ty->getContext(),
60 APFloat(APInt(128, 2, zero), true));
61 case Type::PPC_FP128TyID:
62 return ConstantFP::get(Ty->getContext(), APFloat(APInt(128, 2, zero)));
63 case Type::PointerTyID:
64 return ConstantPointerNull::get(cast<PointerType>(Ty));
65 case Type::StructTyID:
66 case Type::ArrayTyID:
67 case Type::VectorTyID:
68 return ConstantAggregateZero::get(Ty);
69 default:
70 // Function, Label, or Opaque type?
71 assert(!"Cannot create a null constant of that type!");
72 return 0;
73 }
74}
75
76Constant* Constant::getAllOnesValue(const Type* Ty) {
77 if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty))
78 return ConstantInt::get(Ty->getContext(),
79 APInt::getAllOnesValue(ITy->getBitWidth()));
80
81 std::vector<Constant*> Elts;
82 const VectorType* VTy = cast<VectorType>(Ty);
83 Elts.resize(VTy->getNumElements(), getAllOnesValue(VTy->getElementType()));
84 assert(Elts[0] && "Not a vector integer type!");
85 return cast<ConstantVector>(ConstantVector::get(Elts));
86}
87
Chris Lattnere9bb2df2001-12-03 22:26:30 +000088void Constant::destroyConstantImpl() {
89 // When a Constant is destroyed, there may be lingering
Chris Lattnerf5ec48d2001-10-13 06:57:33 +000090 // references to the constant by other constants in the constant pool. These
Misha Brukmanef6a6a62003-08-21 22:14:26 +000091 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerf5ec48d2001-10-13 06:57:33 +000092 // but they don't know that. Because we only find out when the CPV is
93 // deleted, we must now notify all of our users (that should only be
Chris Lattnere9bb2df2001-12-03 22:26:30 +000094 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerf5ec48d2001-10-13 06:57:33 +000095 //
96 while (!use_empty()) {
97 Value *V = use_back();
98#ifndef NDEBUG // Only in -g mode...
Chris Lattner6183b922002-07-18 00:14:50 +000099 if (!isa<Constant>(V))
Bill Wendling2e3def12006-11-17 08:03:48 +0000100 DOUT << "While deleting: " << *this
101 << "\n\nUse still stuck around after Def is destroyed: "
102 << *V << "\n\n";
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000103#endif
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000104 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1c9c8e62004-07-17 23:48:33 +0000105 Constant *CV = cast<Constant>(V);
106 CV->destroyConstant();
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000107
108 // The constant should remove itself from our use list...
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000109 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000110 }
111
112 // Value has no outstanding references it is safe to delete it now...
113 delete this;
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000114}
Chris Lattner00950542001-06-06 20:29:01 +0000115
Chris Lattner35b89fa2006-10-20 00:27:06 +0000116/// canTrap - Return true if evaluation of this constant could trap. This is
117/// true for things like constant expressions that could divide by zero.
118bool Constant::canTrap() const {
119 assert(getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
120 // The only thing that could possibly trap are constant exprs.
121 const ConstantExpr *CE = dyn_cast<ConstantExpr>(this);
122 if (!CE) return false;
123
124 // ConstantExpr traps if any operands can trap.
125 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
126 if (getOperand(i)->canTrap())
127 return true;
128
129 // Otherwise, only specific operations can trap.
130 switch (CE->getOpcode()) {
131 default:
132 return false;
Reid Spencer1628cec2006-10-26 06:15:43 +0000133 case Instruction::UDiv:
134 case Instruction::SDiv:
135 case Instruction::FDiv:
Reid Spencer0a783f72006-11-02 01:53:59 +0000136 case Instruction::URem:
137 case Instruction::SRem:
138 case Instruction::FRem:
Chris Lattner35b89fa2006-10-20 00:27:06 +0000139 // Div and rem can trap if the RHS is not known to be non-zero.
140 if (!isa<ConstantInt>(getOperand(1)) || getOperand(1)->isNullValue())
141 return true;
142 return false;
143 }
144}
145
Chris Lattner7cf12c72009-07-22 00:05:44 +0000146
147/// getRelocationInfo - This method classifies the entry according to
148/// whether or not it may generate a relocation entry. This must be
149/// conservative, so if it might codegen to a relocatable entry, it should say
150/// so. The return values are:
151///
Chris Lattner083a1e02009-07-24 03:27:21 +0000152/// NoRelocation: This constant pool entry is guaranteed to never have a
153/// relocation applied to it (because it holds a simple constant like
154/// '4').
155/// LocalRelocation: This entry has relocations, but the entries are
156/// guaranteed to be resolvable by the static linker, so the dynamic
157/// linker will never see them.
158/// GlobalRelocations: This entry may have arbitrary relocations.
Chris Lattner7cf12c72009-07-22 00:05:44 +0000159///
160/// FIXME: This really should not be in VMCore.
Chris Lattner083a1e02009-07-24 03:27:21 +0000161Constant::PossibleRelocationsTy Constant::getRelocationInfo() const {
162 if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
Chris Lattner7cf12c72009-07-22 00:05:44 +0000163 if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
Chris Lattner083a1e02009-07-24 03:27:21 +0000164 return LocalRelocation; // Local to this file/library.
165 return GlobalRelocations; // Global reference.
Anton Korobeynikovab267a22009-03-29 17:13:18 +0000166 }
Chris Lattner7cf12c72009-07-22 00:05:44 +0000167
Chris Lattner083a1e02009-07-24 03:27:21 +0000168 PossibleRelocationsTy Result = NoRelocation;
Evan Chengafe15812007-03-08 00:59:12 +0000169 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Chris Lattner7cf12c72009-07-22 00:05:44 +0000170 Result = std::max(Result, getOperand(i)->getRelocationInfo());
171
172 return Result;
Evan Chengafe15812007-03-08 00:59:12 +0000173}
174
Chris Lattner7cf12c72009-07-22 00:05:44 +0000175
Chris Lattner86381442008-07-10 00:28:11 +0000176/// getVectorElements - This method, which is only valid on constant of vector
177/// type, returns the elements of the vector in the specified smallvector.
Chris Lattner071aade2008-07-14 05:10:41 +0000178/// This handles breaking down a vector undef into undef elements, etc. For
179/// constant exprs and other cases we can't handle, we return an empty vector.
Owen Anderson0a5372e2009-07-13 04:09:18 +0000180void Constant::getVectorElements(LLVMContext &Context,
181 SmallVectorImpl<Constant*> &Elts) const {
Chris Lattner86381442008-07-10 00:28:11 +0000182 assert(isa<VectorType>(getType()) && "Not a vector constant!");
183
184 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) {
185 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i)
186 Elts.push_back(CV->getOperand(i));
187 return;
188 }
189
190 const VectorType *VT = cast<VectorType>(getType());
191 if (isa<ConstantAggregateZero>(this)) {
192 Elts.assign(VT->getNumElements(),
Owen Andersona7235ea2009-07-31 20:28:14 +0000193 Constant::getNullValue(VT->getElementType()));
Chris Lattner86381442008-07-10 00:28:11 +0000194 return;
195 }
196
Chris Lattner071aade2008-07-14 05:10:41 +0000197 if (isa<UndefValue>(this)) {
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000198 Elts.assign(VT->getNumElements(), UndefValue::get(VT->getElementType()));
Chris Lattner071aade2008-07-14 05:10:41 +0000199 return;
200 }
201
202 // Unknown type, must be constant expr etc.
Chris Lattner86381442008-07-10 00:28:11 +0000203}
204
205
206
Chris Lattner00950542001-06-06 20:29:01 +0000207//===----------------------------------------------------------------------===//
Chris Lattner6b6f6ba2007-02-20 06:39:57 +0000208// ConstantInt
Chris Lattner00950542001-06-06 20:29:01 +0000209//===----------------------------------------------------------------------===//
210
Reid Spencer532d0ce2007-02-26 23:54:03 +0000211ConstantInt::ConstantInt(const IntegerType *Ty, const APInt& V)
Chris Lattnereb41bdd2007-02-20 05:55:46 +0000212 : Constant(Ty, ConstantIntVal, 0, 0), Val(V) {
Reid Spencer532d0ce2007-02-26 23:54:03 +0000213 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
Chris Lattner00950542001-06-06 20:29:01 +0000214}
215
Owen Anderson5defacc2009-07-31 17:39:07 +0000216ConstantInt* ConstantInt::getTrue(LLVMContext &Context) {
217 LLVMContextImpl *pImpl = Context.pImpl;
218 sys::SmartScopedWriter<true>(pImpl->ConstantsLock);
219 if (pImpl->TheTrueVal)
220 return pImpl->TheTrueVal;
221 else
222 return (pImpl->TheTrueVal = ConstantInt::get(IntegerType::get(1), 1));
223}
224
225ConstantInt* ConstantInt::getFalse(LLVMContext &Context) {
226 LLVMContextImpl *pImpl = Context.pImpl;
227 sys::SmartScopedWriter<true>(pImpl->ConstantsLock);
228 if (pImpl->TheFalseVal)
229 return pImpl->TheFalseVal;
230 else
231 return (pImpl->TheFalseVal = ConstantInt::get(IntegerType::get(1), 0));
232}
233
234
Owen Andersoneed707b2009-07-24 23:12:02 +0000235// Get a ConstantInt from an APInt. Note that the value stored in the DenseMap
236// as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the
237// operator== and operator!= to ensure that the DenseMap doesn't attempt to
238// compare APInt's of different widths, which would violate an APInt class
239// invariant which generates an assertion.
240ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt& V) {
241 // Get the corresponding integer type for the bit width of the value.
Owen Andersondebcb012009-07-29 22:17:13 +0000242 const IntegerType *ITy = IntegerType::get(V.getBitWidth());
Owen Andersoneed707b2009-07-24 23:12:02 +0000243 // get an existing value or the insertion position
244 DenseMapAPIntKeyInfo::KeyTy Key(V, ITy);
245
246 Context.pImpl->ConstantsLock.reader_acquire();
247 ConstantInt *&Slot = Context.pImpl->IntConstants[Key];
248 Context.pImpl->ConstantsLock.reader_release();
249
250 if (!Slot) {
251 sys::SmartScopedWriter<true> Writer(Context.pImpl->ConstantsLock);
252 ConstantInt *&NewSlot = Context.pImpl->IntConstants[Key];
253 if (!Slot) {
254 NewSlot = new ConstantInt(ITy, V);
255 }
256
257 return NewSlot;
258 } else {
259 return Slot;
260 }
261}
262
263Constant* ConstantInt::get(const Type* Ty, uint64_t V, bool isSigned) {
264 Constant *C = get(cast<IntegerType>(Ty->getScalarType()),
265 V, isSigned);
266
267 // For vectors, broadcast the value.
268 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
Owen Andersonaf7ec972009-07-28 21:19:26 +0000269 return ConstantVector::get(
Owen Andersoneed707b2009-07-24 23:12:02 +0000270 std::vector<Constant *>(VTy->getNumElements(), C));
271
272 return C;
273}
274
275ConstantInt* ConstantInt::get(const IntegerType* Ty, uint64_t V,
276 bool isSigned) {
277 return get(Ty->getContext(), APInt(Ty->getBitWidth(), V, isSigned));
278}
279
280ConstantInt* ConstantInt::getSigned(const IntegerType* Ty, int64_t V) {
281 return get(Ty, V, true);
282}
283
284Constant *ConstantInt::getSigned(const Type *Ty, int64_t V) {
285 return get(Ty, V, true);
286}
287
288Constant* ConstantInt::get(const Type* Ty, const APInt& V) {
289 ConstantInt *C = get(Ty->getContext(), V);
290 assert(C->getType() == Ty->getScalarType() &&
291 "ConstantInt type doesn't match the type implied by its value!");
292
293 // For vectors, broadcast the value.
294 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
Owen Andersonaf7ec972009-07-28 21:19:26 +0000295 return ConstantVector::get(
Owen Andersoneed707b2009-07-24 23:12:02 +0000296 std::vector<Constant *>(VTy->getNumElements(), C));
297
298 return C;
299}
300
Chris Lattner6b6f6ba2007-02-20 06:39:57 +0000301//===----------------------------------------------------------------------===//
Chris Lattner9b4ee0c2007-02-20 07:17:17 +0000302// ConstantFP
Chris Lattner6b6f6ba2007-02-20 06:39:57 +0000303//===----------------------------------------------------------------------===//
304
Rafael Espindola87d1f472009-07-15 17:40:42 +0000305static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
306 if (Ty == Type::FloatTy)
307 return &APFloat::IEEEsingle;
308 if (Ty == Type::DoubleTy)
309 return &APFloat::IEEEdouble;
310 if (Ty == Type::X86_FP80Ty)
311 return &APFloat::x87DoubleExtended;
312 else if (Ty == Type::FP128Ty)
313 return &APFloat::IEEEquad;
314
315 assert(Ty == Type::PPC_FP128Ty && "Unknown FP format");
316 return &APFloat::PPCDoubleDouble;
317}
318
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000319/// get() - This returns a constant fp for the specified value in the
320/// specified type. This should only be used for simple constant values like
321/// 2.0/1.0 etc, that are known-valid both as double and as the target format.
322Constant* ConstantFP::get(const Type* Ty, double V) {
323 LLVMContext &Context = Ty->getContext();
324
325 APFloat FV(V);
326 bool ignored;
327 FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
328 APFloat::rmNearestTiesToEven, &ignored);
329 Constant *C = get(Context, FV);
330
331 // For vectors, broadcast the value.
332 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
Owen Andersonaf7ec972009-07-28 21:19:26 +0000333 return ConstantVector::get(
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000334 std::vector<Constant *>(VTy->getNumElements(), C));
335
336 return C;
337}
338
339ConstantFP* ConstantFP::getNegativeZero(const Type* Ty) {
340 LLVMContext &Context = Ty->getContext();
Owen Andersona7235ea2009-07-31 20:28:14 +0000341 APFloat apf = cast <ConstantFP>(Constant::getNullValue(Ty))->getValueAPF();
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000342 apf.changeSign();
343 return get(Context, apf);
344}
345
346
347Constant* ConstantFP::getZeroValueForNegation(const Type* Ty) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000348 if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
349 if (PTy->getElementType()->isFloatingPoint()) {
350 std::vector<Constant*> zeros(PTy->getNumElements(),
351 getNegativeZero(PTy->getElementType()));
Owen Andersonaf7ec972009-07-28 21:19:26 +0000352 return ConstantVector::get(PTy, zeros);
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000353 }
354
355 if (Ty->isFloatingPoint())
356 return getNegativeZero(Ty);
357
Owen Andersona7235ea2009-07-31 20:28:14 +0000358 return Constant::getNullValue(Ty);
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000359}
360
361
362// ConstantFP accessors.
363ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) {
364 DenseMapAPFloatKeyInfo::KeyTy Key(V);
365
366 LLVMContextImpl* pImpl = Context.pImpl;
367
368 pImpl->ConstantsLock.reader_acquire();
369 ConstantFP *&Slot = pImpl->FPConstants[Key];
370 pImpl->ConstantsLock.reader_release();
371
372 if (!Slot) {
373 sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
374 ConstantFP *&NewSlot = pImpl->FPConstants[Key];
375 if (!NewSlot) {
376 const Type *Ty;
377 if (&V.getSemantics() == &APFloat::IEEEsingle)
378 Ty = Type::FloatTy;
379 else if (&V.getSemantics() == &APFloat::IEEEdouble)
380 Ty = Type::DoubleTy;
381 else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
382 Ty = Type::X86_FP80Ty;
383 else if (&V.getSemantics() == &APFloat::IEEEquad)
384 Ty = Type::FP128Ty;
385 else {
386 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble &&
387 "Unknown FP format");
388 Ty = Type::PPC_FP128Ty;
389 }
390 NewSlot = new ConstantFP(Ty, V);
391 }
392
393 return NewSlot;
394 }
395
396 return Slot;
397}
398
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000399ConstantFP::ConstantFP(const Type *Ty, const APFloat& V)
400 : Constant(Ty, ConstantFPVal, 0, 0), Val(V) {
Chris Lattner288e78f2008-04-09 06:38:30 +0000401 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
402 "FP type Mismatch");
Chris Lattner00950542001-06-06 20:29:01 +0000403}
404
Chris Lattner9b4ee0c2007-02-20 07:17:17 +0000405bool ConstantFP::isNullValue() const {
Dale Johannesen343e7702007-08-24 00:56:33 +0000406 return Val.isZero() && !Val.isNegative();
Chris Lattner9b4ee0c2007-02-20 07:17:17 +0000407}
408
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000409bool ConstantFP::isExactlyValue(const APFloat& V) const {
410 return Val.bitwiseIsEqual(V);
Chris Lattner9b4ee0c2007-02-20 07:17:17 +0000411}
412
Chris Lattner9b4ee0c2007-02-20 07:17:17 +0000413//===----------------------------------------------------------------------===//
414// ConstantXXX Classes
415//===----------------------------------------------------------------------===//
416
417
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000418ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnere4671472005-01-29 00:34:39 +0000419 const std::vector<Constant*> &V)
Gabor Greifefe65362008-05-10 08:32:32 +0000420 : Constant(T, ConstantArrayVal,
421 OperandTraits<ConstantArray>::op_end(this) - V.size(),
422 V.size()) {
Alkis Evlogimenose0de1d62004-09-15 02:32:15 +0000423 assert(V.size() == T->getNumElements() &&
424 "Invalid initializer vector for constant array");
Chris Lattnere4671472005-01-29 00:34:39 +0000425 Use *OL = OperandList;
Chris Lattnerdfdd6c52005-10-03 21:56:24 +0000426 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
427 I != E; ++I, ++OL) {
Chris Lattner71abaab2005-10-07 05:23:36 +0000428 Constant *C = *I;
429 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscad90ad2004-09-10 04:16:59 +0000430 (T->isAbstract() &&
Chris Lattner71abaab2005-10-07 05:23:36 +0000431 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscad90ad2004-09-10 04:16:59 +0000432 "Initializer for array element doesn't match array element type!");
Gabor Greif6c80c382008-05-26 21:33:52 +0000433 *OL = C;
Chris Lattner00950542001-06-06 20:29:01 +0000434 }
435}
436
Owen Anderson1fd70962009-07-28 18:32:17 +0000437Constant *ConstantArray::get(const ArrayType *Ty,
438 const std::vector<Constant*> &V) {
439 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
440 // If this is an all-zero array, return a ConstantAggregateZero object
441 if (!V.empty()) {
442 Constant *C = V[0];
443 if (!C->isNullValue()) {
444 // Implicitly locked.
445 return pImpl->ArrayConstants.getOrCreate(Ty, V);
446 }
447 for (unsigned i = 1, e = V.size(); i != e; ++i)
448 if (V[i] != C) {
449 // Implicitly locked.
450 return pImpl->ArrayConstants.getOrCreate(Ty, V);
451 }
452 }
453
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000454 return ConstantAggregateZero::get(Ty);
Owen Anderson1fd70962009-07-28 18:32:17 +0000455}
456
457
458Constant* ConstantArray::get(const ArrayType* T, Constant* const* Vals,
459 unsigned NumVals) {
460 // FIXME: make this the primary ctor method.
461 return get(T, std::vector<Constant*>(Vals, Vals+NumVals));
462}
463
464/// ConstantArray::get(const string&) - Return an array that is initialized to
465/// contain the specified string. If length is zero then a null terminator is
466/// added to the specified string so that it may be used in a natural way.
467/// Otherwise, the length parameter specifies how much of the string to use
468/// and it won't be null terminated.
469///
470Constant* ConstantArray::get(const StringRef &Str, bool AddNull) {
471 std::vector<Constant*> ElementVals;
472 for (unsigned i = 0; i < Str.size(); ++i)
473 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, Str[i]));
474
475 // Add a null terminator to the string...
476 if (AddNull) {
477 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, 0));
478 }
479
480 ArrayType *ATy = ArrayType::get(Type::Int8Ty, ElementVals.size());
481 return get(ATy, ElementVals);
482}
483
484
Chris Lattnere4671472005-01-29 00:34:39 +0000485
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000486ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnere4671472005-01-29 00:34:39 +0000487 const std::vector<Constant*> &V)
Gabor Greifefe65362008-05-10 08:32:32 +0000488 : Constant(T, ConstantStructVal,
489 OperandTraits<ConstantStruct>::op_end(this) - V.size(),
490 V.size()) {
Chris Lattnerd21cd802004-02-09 04:37:31 +0000491 assert(V.size() == T->getNumElements() &&
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000492 "Invalid initializer vector for constant structure");
Chris Lattnere4671472005-01-29 00:34:39 +0000493 Use *OL = OperandList;
Chris Lattnerdfdd6c52005-10-03 21:56:24 +0000494 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
495 I != E; ++I, ++OL) {
Chris Lattner71abaab2005-10-07 05:23:36 +0000496 Constant *C = *I;
497 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattnerdfdd6c52005-10-03 21:56:24 +0000498 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner71abaab2005-10-07 05:23:36 +0000499 C->getType()->isAbstract()) &&
Chris Lattnerdfdd6c52005-10-03 21:56:24 +0000500 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner71abaab2005-10-07 05:23:36 +0000501 C->getType()->getTypeID())) &&
Chris Lattnerb8438892003-06-02 17:42:47 +0000502 "Initializer for struct element doesn't match struct element type!");
Gabor Greif6c80c382008-05-26 21:33:52 +0000503 *OL = C;
Chris Lattner00950542001-06-06 20:29:01 +0000504 }
505}
506
Owen Anderson8fa33382009-07-27 22:29:26 +0000507// ConstantStruct accessors.
508Constant* ConstantStruct::get(const StructType* T,
509 const std::vector<Constant*>& V) {
510 LLVMContextImpl* pImpl = T->getContext().pImpl;
511
512 // Create a ConstantAggregateZero value if all elements are zeros...
513 for (unsigned i = 0, e = V.size(); i != e; ++i)
514 if (!V[i]->isNullValue())
515 // Implicitly locked.
516 return pImpl->StructConstants.getOrCreate(T, V);
517
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000518 return ConstantAggregateZero::get(T);
Owen Anderson8fa33382009-07-27 22:29:26 +0000519}
520
521Constant* ConstantStruct::get(const std::vector<Constant*>& V, bool packed) {
522 std::vector<const Type*> StructEls;
523 StructEls.reserve(V.size());
524 for (unsigned i = 0, e = V.size(); i != e; ++i)
525 StructEls.push_back(V[i]->getType());
526 return get(StructType::get(StructEls, packed), V);
527}
528
529Constant* ConstantStruct::get(Constant* const *Vals, unsigned NumVals,
530 bool Packed) {
531 // FIXME: make this the primary ctor method.
532 return get(std::vector<Constant*>(Vals, Vals+NumVals), Packed);
533}
Chris Lattnere4671472005-01-29 00:34:39 +0000534
Reid Spencer9d6565a2007-02-15 02:26:10 +0000535ConstantVector::ConstantVector(const VectorType *T,
Chris Lattnere4671472005-01-29 00:34:39 +0000536 const std::vector<Constant*> &V)
Gabor Greifefe65362008-05-10 08:32:32 +0000537 : Constant(T, ConstantVectorVal,
538 OperandTraits<ConstantVector>::op_end(this) - V.size(),
539 V.size()) {
Chris Lattnere4671472005-01-29 00:34:39 +0000540 Use *OL = OperandList;
Chris Lattnerdfdd6c52005-10-03 21:56:24 +0000541 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
542 I != E; ++I, ++OL) {
Chris Lattner71abaab2005-10-07 05:23:36 +0000543 Constant *C = *I;
544 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscad90ad2004-09-10 04:16:59 +0000545 (T->isAbstract() &&
Chris Lattner71abaab2005-10-07 05:23:36 +0000546 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Dan Gohmanfa73ea22007-05-24 14:36:04 +0000547 "Initializer for vector element doesn't match vector element type!");
Gabor Greif6c80c382008-05-26 21:33:52 +0000548 *OL = C;
Brian Gaeke715c90b2004-08-20 06:00:58 +0000549 }
550}
551
Owen Andersonaf7ec972009-07-28 21:19:26 +0000552// ConstantVector accessors.
553Constant* ConstantVector::get(const VectorType* T,
554 const std::vector<Constant*>& V) {
555 assert(!V.empty() && "Vectors can't be empty");
556 LLVMContext &Context = T->getContext();
557 LLVMContextImpl *pImpl = Context.pImpl;
558
559 // If this is an all-undef or alll-zero vector, return a
560 // ConstantAggregateZero or UndefValue.
561 Constant *C = V[0];
562 bool isZero = C->isNullValue();
563 bool isUndef = isa<UndefValue>(C);
564
565 if (isZero || isUndef) {
566 for (unsigned i = 1, e = V.size(); i != e; ++i)
567 if (V[i] != C) {
568 isZero = isUndef = false;
569 break;
570 }
571 }
572
573 if (isZero)
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000574 return ConstantAggregateZero::get(T);
Owen Andersonaf7ec972009-07-28 21:19:26 +0000575 if (isUndef)
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000576 return UndefValue::get(T);
Owen Andersonaf7ec972009-07-28 21:19:26 +0000577
578 // Implicitly locked.
579 return pImpl->VectorConstants.getOrCreate(T, V);
580}
581
582Constant* ConstantVector::get(const std::vector<Constant*>& V) {
583 assert(!V.empty() && "Cannot infer type if V is empty");
584 return get(VectorType::get(V.front()->getType(),V.size()), V);
585}
586
587Constant* ConstantVector::get(Constant* const* Vals, unsigned NumVals) {
588 // FIXME: make this the primary ctor method.
589 return get(std::vector<Constant*>(Vals, Vals+NumVals));
590}
591
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000592
Gabor Greifefe65362008-05-10 08:32:32 +0000593namespace llvm {
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000594// We declare several classes private to this file, so use an anonymous
595// namespace
596namespace {
Reid Spencer728b6db2006-12-03 05:48:19 +0000597
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000598/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
599/// behind the scenes to implement unary constant exprs.
600class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
Gabor Greif051a9502008-04-06 20:25:17 +0000601 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000602public:
Gabor Greif051a9502008-04-06 20:25:17 +0000603 // allocate space for exactly one operand
604 void *operator new(size_t s) {
605 return User::operator new(s, 1);
606 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000607 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
Gabor Greifefe65362008-05-10 08:32:32 +0000608 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
609 Op<0>() = C;
610 }
611 /// Transparently provide more efficient getOperand methods.
612 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000613};
Reid Spencer728b6db2006-12-03 05:48:19 +0000614
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000615/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
616/// behind the scenes to implement binary constant exprs.
617class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Gabor Greif051a9502008-04-06 20:25:17 +0000618 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000619public:
Gabor Greif051a9502008-04-06 20:25:17 +0000620 // allocate space for exactly two operands
621 void *operator new(size_t s) {
622 return User::operator new(s, 2);
623 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000624 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
Gabor Greifefe65362008-05-10 08:32:32 +0000625 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
Gabor Greif6c80c382008-05-26 21:33:52 +0000626 Op<0>() = C1;
627 Op<1>() = C2;
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000628 }
Gabor Greifefe65362008-05-10 08:32:32 +0000629 /// Transparently provide more efficient getOperand methods.
630 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000631};
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000632
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000633/// SelectConstantExpr - This class is private to Constants.cpp, and is used
634/// behind the scenes to implement select constant exprs.
635class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Gabor Greif051a9502008-04-06 20:25:17 +0000636 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000637public:
Gabor Greif051a9502008-04-06 20:25:17 +0000638 // allocate space for exactly three operands
639 void *operator new(size_t s) {
640 return User::operator new(s, 3);
641 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000642 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Gabor Greifefe65362008-05-10 08:32:32 +0000643 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
Gabor Greif6c80c382008-05-26 21:33:52 +0000644 Op<0>() = C1;
645 Op<1>() = C2;
646 Op<2>() = C3;
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000647 }
Gabor Greifefe65362008-05-10 08:32:32 +0000648 /// Transparently provide more efficient getOperand methods.
649 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000650};
Chris Lattnere4671472005-01-29 00:34:39 +0000651
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000652/// ExtractElementConstantExpr - This class is private to
653/// Constants.cpp, and is used behind the scenes to implement
654/// extractelement constant exprs.
655class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Gabor Greif051a9502008-04-06 20:25:17 +0000656 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000657public:
Gabor Greif051a9502008-04-06 20:25:17 +0000658 // allocate space for exactly two operands
659 void *operator new(size_t s) {
660 return User::operator new(s, 2);
661 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000662 ExtractElementConstantExpr(Constant *C1, Constant *C2)
663 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
Gabor Greifefe65362008-05-10 08:32:32 +0000664 Instruction::ExtractElement, &Op<0>(), 2) {
Gabor Greif6c80c382008-05-26 21:33:52 +0000665 Op<0>() = C1;
666 Op<1>() = C2;
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000667 }
Gabor Greifefe65362008-05-10 08:32:32 +0000668 /// Transparently provide more efficient getOperand methods.
669 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000670};
Robert Bocchinob52ee7f2006-01-10 19:05:34 +0000671
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000672/// InsertElementConstantExpr - This class is private to
673/// Constants.cpp, and is used behind the scenes to implement
674/// insertelement constant exprs.
675class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Gabor Greif051a9502008-04-06 20:25:17 +0000676 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000677public:
Gabor Greif051a9502008-04-06 20:25:17 +0000678 // allocate space for exactly three operands
679 void *operator new(size_t s) {
680 return User::operator new(s, 3);
681 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000682 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
683 : ConstantExpr(C1->getType(), Instruction::InsertElement,
Gabor Greifefe65362008-05-10 08:32:32 +0000684 &Op<0>(), 3) {
Gabor Greif6c80c382008-05-26 21:33:52 +0000685 Op<0>() = C1;
686 Op<1>() = C2;
687 Op<2>() = C3;
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000688 }
Gabor Greifefe65362008-05-10 08:32:32 +0000689 /// Transparently provide more efficient getOperand methods.
690 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000691};
Robert Bocchinoc152f9c2006-01-17 20:07:22 +0000692
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000693/// ShuffleVectorConstantExpr - This class is private to
694/// Constants.cpp, and is used behind the scenes to implement
695/// shufflevector constant exprs.
696class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Gabor Greif051a9502008-04-06 20:25:17 +0000697 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000698public:
Gabor Greif051a9502008-04-06 20:25:17 +0000699 // allocate space for exactly three operands
700 void *operator new(size_t s) {
701 return User::operator new(s, 3);
702 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000703 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Nate Begeman0f123cf2009-02-12 21:28:33 +0000704 : ConstantExpr(VectorType::get(
705 cast<VectorType>(C1->getType())->getElementType(),
706 cast<VectorType>(C3->getType())->getNumElements()),
707 Instruction::ShuffleVector,
Gabor Greifefe65362008-05-10 08:32:32 +0000708 &Op<0>(), 3) {
Gabor Greif6c80c382008-05-26 21:33:52 +0000709 Op<0>() = C1;
710 Op<1>() = C2;
711 Op<2>() = C3;
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000712 }
Gabor Greifefe65362008-05-10 08:32:32 +0000713 /// Transparently provide more efficient getOperand methods.
714 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000715};
716
Dan Gohman041e2eb2008-05-15 19:50:34 +0000717/// ExtractValueConstantExpr - This class is private to
718/// Constants.cpp, and is used behind the scenes to implement
719/// extractvalue constant exprs.
720class VISIBILITY_HIDDEN ExtractValueConstantExpr : public ConstantExpr {
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000721 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman041e2eb2008-05-15 19:50:34 +0000722public:
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000723 // allocate space for exactly one operand
724 void *operator new(size_t s) {
725 return User::operator new(s, 1);
Dan Gohman041e2eb2008-05-15 19:50:34 +0000726 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000727 ExtractValueConstantExpr(Constant *Agg,
728 const SmallVector<unsigned, 4> &IdxList,
729 const Type *DestTy)
730 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
731 Indices(IdxList) {
732 Op<0>() = Agg;
733 }
734
Dan Gohman35651cd2008-05-31 19:09:08 +0000735 /// Indices - These identify which value to extract.
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000736 const SmallVector<unsigned, 4> Indices;
737
Dan Gohman041e2eb2008-05-15 19:50:34 +0000738 /// Transparently provide more efficient getOperand methods.
739 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
740};
741
742/// InsertValueConstantExpr - This class is private to
743/// Constants.cpp, and is used behind the scenes to implement
744/// insertvalue constant exprs.
745class VISIBILITY_HIDDEN InsertValueConstantExpr : public ConstantExpr {
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000746 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
Dan Gohman041e2eb2008-05-15 19:50:34 +0000747public:
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000748 // allocate space for exactly one operand
749 void *operator new(size_t s) {
750 return User::operator new(s, 2);
Dan Gohman041e2eb2008-05-15 19:50:34 +0000751 }
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000752 InsertValueConstantExpr(Constant *Agg, Constant *Val,
753 const SmallVector<unsigned, 4> &IdxList,
754 const Type *DestTy)
755 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
756 Indices(IdxList) {
757 Op<0>() = Agg;
758 Op<1>() = Val;
759 }
760
Dan Gohman35651cd2008-05-31 19:09:08 +0000761 /// Indices - These identify the position for the insertion.
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000762 const SmallVector<unsigned, 4> Indices;
763
Dan Gohman041e2eb2008-05-15 19:50:34 +0000764 /// Transparently provide more efficient getOperand methods.
765 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
766};
767
768
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000769/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
770/// used behind the scenes to implement getelementpr constant exprs.
Gabor Greif051a9502008-04-06 20:25:17 +0000771class VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000772 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
Gabor Greifefe65362008-05-10 08:32:32 +0000773 const Type *DestTy);
Gabor Greif051a9502008-04-06 20:25:17 +0000774public:
Gabor Greifb1dbcd82008-05-15 10:04:30 +0000775 static GetElementPtrConstantExpr *Create(Constant *C,
776 const std::vector<Constant*>&IdxList,
Gabor Greifefe65362008-05-10 08:32:32 +0000777 const Type *DestTy) {
Dan Gohmanf2411742009-07-20 17:43:30 +0000778 return
779 new(IdxList.size() + 1) GetElementPtrConstantExpr(C, IdxList, DestTy);
Gabor Greif051a9502008-04-06 20:25:17 +0000780 }
Gabor Greifefe65362008-05-10 08:32:32 +0000781 /// Transparently provide more efficient getOperand methods.
782 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000783};
784
785// CompareConstantExpr - This class is private to Constants.cpp, and is used
786// behind the scenes to implement ICmp and FCmp constant expressions. This is
787// needed in order to store the predicate value for these instructions.
788struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr {
Gabor Greif051a9502008-04-06 20:25:17 +0000789 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
790 // allocate space for exactly two operands
791 void *operator new(size_t s) {
792 return User::operator new(s, 2);
793 }
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000794 unsigned short predicate;
Nate Begemanac80ade2008-05-12 19:01:56 +0000795 CompareConstantExpr(const Type *ty, Instruction::OtherOps opc,
796 unsigned short pred, Constant* LHS, Constant* RHS)
797 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
Gabor Greif6c80c382008-05-26 21:33:52 +0000798 Op<0>() = LHS;
799 Op<1>() = RHS;
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000800 }
Gabor Greifefe65362008-05-10 08:32:32 +0000801 /// Transparently provide more efficient getOperand methods.
802 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000803};
804
805} // end anonymous namespace
806
Gabor Greifefe65362008-05-10 08:32:32 +0000807template <>
808struct OperandTraits<UnaryConstantExpr> : FixedNumOperandTraits<1> {
809};
810DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
811
812template <>
813struct OperandTraits<BinaryConstantExpr> : FixedNumOperandTraits<2> {
814};
815DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
816
817template <>
818struct OperandTraits<SelectConstantExpr> : FixedNumOperandTraits<3> {
819};
820DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
821
822template <>
823struct OperandTraits<ExtractElementConstantExpr> : FixedNumOperandTraits<2> {
824};
825DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
826
827template <>
828struct OperandTraits<InsertElementConstantExpr> : FixedNumOperandTraits<3> {
829};
830DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
831
832template <>
833struct OperandTraits<ShuffleVectorConstantExpr> : FixedNumOperandTraits<3> {
834};
835DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
836
Dan Gohman041e2eb2008-05-15 19:50:34 +0000837template <>
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000838struct OperandTraits<ExtractValueConstantExpr> : FixedNumOperandTraits<1> {
Dan Gohman041e2eb2008-05-15 19:50:34 +0000839};
Dan Gohman041e2eb2008-05-15 19:50:34 +0000840DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
841
842template <>
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000843struct OperandTraits<InsertValueConstantExpr> : FixedNumOperandTraits<2> {
Dan Gohman041e2eb2008-05-15 19:50:34 +0000844};
Dan Gohman041e2eb2008-05-15 19:50:34 +0000845DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
846
Gabor Greifefe65362008-05-10 08:32:32 +0000847template <>
848struct OperandTraits<GetElementPtrConstantExpr> : VariadicOperandTraits<1> {
849};
850
851GetElementPtrConstantExpr::GetElementPtrConstantExpr
852 (Constant *C,
853 const std::vector<Constant*> &IdxList,
854 const Type *DestTy)
855 : ConstantExpr(DestTy, Instruction::GetElementPtr,
856 OperandTraits<GetElementPtrConstantExpr>::op_end(this)
857 - (IdxList.size()+1),
858 IdxList.size()+1) {
Gabor Greif6c80c382008-05-26 21:33:52 +0000859 OperandList[0] = C;
Gabor Greifefe65362008-05-10 08:32:32 +0000860 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
Gabor Greif6c80c382008-05-26 21:33:52 +0000861 OperandList[i+1] = IdxList[i];
Gabor Greifefe65362008-05-10 08:32:32 +0000862}
863
864DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
865
866
867template <>
868struct OperandTraits<CompareConstantExpr> : FixedNumOperandTraits<2> {
869};
870DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
871
872
873} // End llvm namespace
874
Reid Spencer3da59db2006-11-27 01:05:10 +0000875
876// Utility function for determining if a ConstantExpr is a CastOp or not. This
877// can't be inline because we don't want to #include Instruction.h into
878// Constant.h
879bool ConstantExpr::isCast() const {
880 return Instruction::isCast(getOpcode());
881}
882
Reid Spencer077d0eb2006-12-04 05:19:50 +0000883bool ConstantExpr::isCompare() const {
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +0000884 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
Reid Spencer077d0eb2006-12-04 05:19:50 +0000885}
886
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000887bool ConstantExpr::hasIndices() const {
888 return getOpcode() == Instruction::ExtractValue ||
889 getOpcode() == Instruction::InsertValue;
890}
891
892const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const {
893 if (const ExtractValueConstantExpr *EVCE =
894 dyn_cast<ExtractValueConstantExpr>(this))
895 return EVCE->Indices;
Dan Gohman1a203572008-06-23 16:39:44 +0000896
897 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000898}
899
Reid Spencer728b6db2006-12-03 05:48:19 +0000900unsigned ConstantExpr::getPredicate() const {
Nate Begemanac80ade2008-05-12 19:01:56 +0000901 assert(getOpcode() == Instruction::FCmp ||
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +0000902 getOpcode() == Instruction::ICmp);
Chris Lattnerb7daa842007-10-18 16:26:24 +0000903 return ((const CompareConstantExpr*)this)->predicate;
Reid Spencer728b6db2006-12-03 05:48:19 +0000904}
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000905
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000906/// getWithOperandReplaced - Return a constant expression identical to this
907/// one, but with the specified operand set to the specified value.
Reid Spencer3da59db2006-11-27 01:05:10 +0000908Constant *
909ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000910 assert(OpNo < getNumOperands() && "Operand num is out of range!");
911 assert(Op->getType() == getOperand(OpNo)->getType() &&
912 "Replacing operand with value of different type!");
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000913 if (getOperand(OpNo) == Op)
914 return const_cast<ConstantExpr*>(this);
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000915
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000916 Constant *Op0, *Op1, *Op2;
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000917 switch (getOpcode()) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000918 case Instruction::Trunc:
919 case Instruction::ZExt:
920 case Instruction::SExt:
921 case Instruction::FPTrunc:
922 case Instruction::FPExt:
923 case Instruction::UIToFP:
924 case Instruction::SIToFP:
925 case Instruction::FPToUI:
926 case Instruction::FPToSI:
927 case Instruction::PtrToInt:
928 case Instruction::IntToPtr:
929 case Instruction::BitCast:
930 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000931 case Instruction::Select:
932 Op0 = (OpNo == 0) ? Op : getOperand(0);
933 Op1 = (OpNo == 1) ? Op : getOperand(1);
934 Op2 = (OpNo == 2) ? Op : getOperand(2);
935 return ConstantExpr::getSelect(Op0, Op1, Op2);
936 case Instruction::InsertElement:
937 Op0 = (OpNo == 0) ? Op : getOperand(0);
938 Op1 = (OpNo == 1) ? Op : getOperand(1);
939 Op2 = (OpNo == 2) ? Op : getOperand(2);
940 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
941 case Instruction::ExtractElement:
942 Op0 = (OpNo == 0) ? Op : getOperand(0);
943 Op1 = (OpNo == 1) ? Op : getOperand(1);
944 return ConstantExpr::getExtractElement(Op0, Op1);
945 case Instruction::ShuffleVector:
946 Op0 = (OpNo == 0) ? Op : getOperand(0);
947 Op1 = (OpNo == 1) ? Op : getOperand(1);
948 Op2 = (OpNo == 2) ? Op : getOperand(2);
949 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000950 case Instruction::GetElementPtr: {
Chris Lattnerf9021ff2007-02-19 20:01:23 +0000951 SmallVector<Constant*, 8> Ops;
Dan Gohman041e2eb2008-05-15 19:50:34 +0000952 Ops.resize(getNumOperands()-1);
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000953 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Dan Gohman041e2eb2008-05-15 19:50:34 +0000954 Ops[i-1] = getOperand(i);
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000955 if (OpNo == 0)
Chris Lattnerf9021ff2007-02-19 20:01:23 +0000956 return ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000957 Ops[OpNo-1] = Op;
Chris Lattnerf9021ff2007-02-19 20:01:23 +0000958 return ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000959 }
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000960 default:
961 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000962 Op0 = (OpNo == 0) ? Op : getOperand(0);
963 Op1 = (OpNo == 1) ? Op : getOperand(1);
964 return ConstantExpr::get(getOpcode(), Op0, Op1);
965 }
966}
967
968/// getWithOperands - This returns the current constant expression with the
969/// operands replaced with the specified values. The specified operands must
970/// match count and type with the existing ones.
971Constant *ConstantExpr::
Chris Lattnerb054bfd2008-08-20 22:27:40 +0000972getWithOperands(Constant* const *Ops, unsigned NumOps) const {
973 assert(NumOps == getNumOperands() && "Operand count mismatch!");
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000974 bool AnyChange = false;
Chris Lattnerb054bfd2008-08-20 22:27:40 +0000975 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000976 assert(Ops[i]->getType() == getOperand(i)->getType() &&
977 "Operand type mismatch!");
978 AnyChange |= Ops[i] != getOperand(i);
979 }
980 if (!AnyChange) // No operands changed, return self.
981 return const_cast<ConstantExpr*>(this);
982
983 switch (getOpcode()) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000984 case Instruction::Trunc:
985 case Instruction::ZExt:
986 case Instruction::SExt:
987 case Instruction::FPTrunc:
988 case Instruction::FPExt:
989 case Instruction::UIToFP:
990 case Instruction::SIToFP:
991 case Instruction::FPToUI:
992 case Instruction::FPToSI:
993 case Instruction::PtrToInt:
994 case Instruction::IntToPtr:
995 case Instruction::BitCast:
996 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000997 case Instruction::Select:
998 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
999 case Instruction::InsertElement:
1000 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
1001 case Instruction::ExtractElement:
1002 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
1003 case Instruction::ShuffleVector:
1004 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Chris Lattnerf9021ff2007-02-19 20:01:23 +00001005 case Instruction::GetElementPtr:
Chris Lattnerb054bfd2008-08-20 22:27:40 +00001006 return ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], NumOps-1);
Reid Spencere4d87aa2006-12-23 06:05:41 +00001007 case Instruction::ICmp:
1008 case Instruction::FCmp:
1009 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattnerb88a7fb2006-07-14 22:20:01 +00001010 default:
1011 assert(getNumOperands() == 2 && "Must be binary operator?");
1012 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner1fe8f6b2006-07-14 19:37:40 +00001013 }
1014}
1015
Chris Lattner00950542001-06-06 20:29:01 +00001016
1017//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00001018// isValueValidForType implementations
1019
Reid Spencer9b11d512006-12-19 01:28:19 +00001020bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001021 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencera54b7cb2007-01-12 07:05:14 +00001022 if (Ty == Type::Int1Ty)
1023 return Val == 0 || Val == 1;
Reid Spencer554cec62007-02-05 23:47:56 +00001024 if (NumBits >= 64)
Reid Spencera54b7cb2007-01-12 07:05:14 +00001025 return true; // always true, has to fit in largest type
1026 uint64_t Max = (1ll << NumBits) - 1;
1027 return Val <= Max;
Reid Spencer9b11d512006-12-19 01:28:19 +00001028}
1029
Reid Spencerb83eb642006-10-20 07:07:24 +00001030bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001031 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Reid Spencera54b7cb2007-01-12 07:05:14 +00001032 if (Ty == Type::Int1Ty)
Reid Spencerc1030572007-01-19 21:13:56 +00001033 return Val == 0 || Val == 1 || Val == -1;
Reid Spencer554cec62007-02-05 23:47:56 +00001034 if (NumBits >= 64)
Reid Spencera54b7cb2007-01-12 07:05:14 +00001035 return true; // always true, has to fit in largest type
1036 int64_t Min = -(1ll << (NumBits-1));
1037 int64_t Max = (1ll << (NumBits-1)) - 1;
1038 return (Val >= Min && Val <= Max);
Chris Lattner00950542001-06-06 20:29:01 +00001039}
1040
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001041bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) {
1042 // convert modifies in place, so make a copy.
1043 APFloat Val2 = APFloat(Val);
Dale Johannesen23a98552008-10-09 23:00:39 +00001044 bool losesInfo;
Chris Lattnerf70c22b2004-06-17 18:19:28 +00001045 switch (Ty->getTypeID()) {
Chris Lattner00950542001-06-06 20:29:01 +00001046 default:
1047 return false; // These can't be represented as floating point!
1048
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001049 // FIXME rounding mode needs to be more flexible
Dale Johannesen23a98552008-10-09 23:00:39 +00001050 case Type::FloatTyID: {
1051 if (&Val2.getSemantics() == &APFloat::IEEEsingle)
1052 return true;
1053 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
1054 return !losesInfo;
1055 }
1056 case Type::DoubleTyID: {
1057 if (&Val2.getSemantics() == &APFloat::IEEEsingle ||
1058 &Val2.getSemantics() == &APFloat::IEEEdouble)
1059 return true;
1060 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
1061 return !losesInfo;
1062 }
Dale Johannesenebbc95d2007-08-09 22:51:36 +00001063 case Type::X86_FP80TyID:
Dale Johannesen9d5f4562007-09-12 03:30:33 +00001064 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
1065 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1066 &Val2.getSemantics() == &APFloat::x87DoubleExtended;
Dale Johannesenebbc95d2007-08-09 22:51:36 +00001067 case Type::FP128TyID:
Dale Johannesen9d5f4562007-09-12 03:30:33 +00001068 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
1069 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1070 &Val2.getSemantics() == &APFloat::IEEEquad;
Dale Johannesena471c2e2007-10-11 18:07:22 +00001071 case Type::PPC_FP128TyID:
1072 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
1073 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1074 &Val2.getSemantics() == &APFloat::PPCDoubleDouble;
Chris Lattner00950542001-06-06 20:29:01 +00001075 }
Chris Lattnerd74ea2b2006-05-24 17:04:05 +00001076}
Chris Lattner37bf6302001-07-20 19:16:02 +00001077
Chris Lattner531daef2001-09-07 16:46:31 +00001078//===----------------------------------------------------------------------===//
Chris Lattner531daef2001-09-07 16:46:31 +00001079// Factory Function Implementation
1080
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001081static char getValType(ConstantAggregateZero *CPZ) { return 0; }
1082
1083ConstantAggregateZero* ConstantAggregateZero::get(const Type* Ty) {
1084 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) &&
1085 "Cannot create an aggregate zero of non-aggregate type!");
1086
1087 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
1088 // Implicitly locked.
1089 return pImpl->AggZeroConstants.getOrCreate(Ty, 0);
1090}
1091
Dan Gohman0f8b53f2009-03-03 02:55:14 +00001092/// destroyConstant - Remove the constant from the constant table...
1093///
Owen Anderson04fb7c32009-06-20 00:24:58 +00001094void ConstantAggregateZero::destroyConstant() {
Owen Anderson31c36f02009-06-17 20:10:08 +00001095 // Implicitly locked.
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001096 getType()->getContext().pImpl->AggZeroConstants.remove(this);
Chris Lattner40bbeb52004-02-15 05:53:04 +00001097 destroyConstantImpl();
1098}
1099
Dan Gohman0f8b53f2009-03-03 02:55:14 +00001100/// destroyConstant - Remove the constant from the constant table...
1101///
Owen Anderson04fb7c32009-06-20 00:24:58 +00001102void ConstantArray::destroyConstant() {
Owen Anderson1f0ba8c2009-06-19 18:34:09 +00001103 // Implicitly locked.
Owen Anderson1fd70962009-07-28 18:32:17 +00001104 getType()->getContext().pImpl->ArrayConstants.remove(this);
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001105 destroyConstantImpl();
1106}
1107
Reid Spencer3d10b0b2007-01-26 07:37:34 +00001108/// isString - This method returns true if the array is an array of i8, and
1109/// if the elements of the array are all ConstantInt's.
Chris Lattner13cfdea2004-01-14 17:06:38 +00001110bool ConstantArray::isString() const {
Reid Spencer3d10b0b2007-01-26 07:37:34 +00001111 // Check the element type for i8...
Reid Spencer79e21d32006-12-31 05:26:44 +00001112 if (getType()->getElementType() != Type::Int8Ty)
Chris Lattner13cfdea2004-01-14 17:06:38 +00001113 return false;
1114 // Check the elements to make sure they are all integers, not constant
1115 // expressions.
1116 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1117 if (!isa<ConstantInt>(getOperand(i)))
1118 return false;
1119 return true;
1120}
1121
Evan Cheng22c70302006-10-26 19:15:05 +00001122/// isCString - This method returns true if the array is a string (see
Dan Gohman0f8b53f2009-03-03 02:55:14 +00001123/// isString) and it ends in a null byte \\0 and does not contains any other
Evan Cheng22c70302006-10-26 19:15:05 +00001124/// null bytes except its terminator.
Owen Anderson1ca29d32009-07-13 21:27:19 +00001125bool ConstantArray::isCString() const {
Reid Spencer3d10b0b2007-01-26 07:37:34 +00001126 // Check the element type for i8...
Reid Spencer79e21d32006-12-31 05:26:44 +00001127 if (getType()->getElementType() != Type::Int8Ty)
Evan Chengabf63452006-10-26 21:48:03 +00001128 return false;
Owen Anderson1ca29d32009-07-13 21:27:19 +00001129
Evan Chengabf63452006-10-26 21:48:03 +00001130 // Last element must be a null.
Owen Anderson1ca29d32009-07-13 21:27:19 +00001131 if (!getOperand(getNumOperands()-1)->isNullValue())
Evan Chengabf63452006-10-26 21:48:03 +00001132 return false;
1133 // Other elements must be non-null integers.
1134 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1135 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng22c70302006-10-26 19:15:05 +00001136 return false;
Owen Anderson1ca29d32009-07-13 21:27:19 +00001137 if (getOperand(i)->isNullValue())
Evan Chengabf63452006-10-26 21:48:03 +00001138 return false;
1139 }
Evan Cheng22c70302006-10-26 19:15:05 +00001140 return true;
1141}
1142
1143
Dan Gohman0f8b53f2009-03-03 02:55:14 +00001144/// getAsString - If the sub-element type of this array is i8
1145/// then this method converts the array to an std::string and returns it.
1146/// Otherwise, it asserts out.
1147///
Chris Lattner93aeea32002-08-26 17:53:56 +00001148std::string ConstantArray::getAsString() const {
Chris Lattner13cfdea2004-01-14 17:06:38 +00001149 assert(isString() && "Not a string!");
Chris Lattner93aeea32002-08-26 17:53:56 +00001150 std::string Result;
Owen Anderson45e39582008-06-24 21:58:29 +00001151 Result.reserve(getNumOperands());
Chris Lattnerc07736a2003-07-23 15:22:26 +00001152 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersone4f6ee52008-06-25 01:05:05 +00001153 Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue());
Chris Lattner93aeea32002-08-26 17:53:56 +00001154 return Result;
1155}
1156
1157
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001158//---- ConstantStruct::get() implementation...
Chris Lattner531daef2001-09-07 16:46:31 +00001159//
Chris Lattnered468e372003-10-05 00:17:43 +00001160
Chris Lattner31f84992003-11-21 20:23:48 +00001161namespace llvm {
Misha Brukmanfd939082005-04-21 23:48:37 +00001162
Chris Lattner531daef2001-09-07 16:46:31 +00001163}
Chris Lattner6a57baa2001-10-03 15:39:36 +00001164
Chris Lattnerf5ec48d2001-10-13 06:57:33 +00001165// destroyConstant - Remove the constant from the constant table...
Chris Lattner6a57baa2001-10-03 15:39:36 +00001166//
Owen Anderson04fb7c32009-06-20 00:24:58 +00001167void ConstantStruct::destroyConstant() {
Owen Anderson1f0ba8c2009-06-19 18:34:09 +00001168 // Implicitly locked.
Owen Anderson8fa33382009-07-27 22:29:26 +00001169 getType()->getContext().pImpl->StructConstants.remove(this);
Chris Lattnerf5ec48d2001-10-13 06:57:33 +00001170 destroyConstantImpl();
1171}
Chris Lattner6a57baa2001-10-03 15:39:36 +00001172
Brian Gaeke715c90b2004-08-20 06:00:58 +00001173// destroyConstant - Remove the constant from the constant table...
1174//
Owen Anderson04fb7c32009-06-20 00:24:58 +00001175void ConstantVector::destroyConstant() {
Owen Anderson1f0ba8c2009-06-19 18:34:09 +00001176 // Implicitly locked.
Owen Andersonaf7ec972009-07-28 21:19:26 +00001177 getType()->getContext().pImpl->VectorConstants.remove(this);
Brian Gaeke715c90b2004-08-20 06:00:58 +00001178 destroyConstantImpl();
1179}
1180
Dan Gohmanfa73ea22007-05-24 14:36:04 +00001181/// This function will return true iff every element in this vector constant
Jim Laskeyfa301822007-01-12 22:39:14 +00001182/// is set to all ones.
1183/// @returns true iff this constant's emements are all set to all ones.
1184/// @brief Determine if the value is all ones.
Reid Spencer9d6565a2007-02-15 02:26:10 +00001185bool ConstantVector::isAllOnesValue() const {
Jim Laskeyfa301822007-01-12 22:39:14 +00001186 // Check out first element.
1187 const Constant *Elt = getOperand(0);
1188 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1189 if (!CI || !CI->isAllOnesValue()) return false;
1190 // Then make sure all remaining elements point to the same value.
1191 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1192 if (getOperand(I) != Elt) return false;
1193 }
1194 return true;
1195}
1196
Dan Gohman3b7cf0a2007-10-17 17:51:30 +00001197/// getSplatValue - If this is a splat constant, where all of the
1198/// elements have the same value, return that value. Otherwise return null.
1199Constant *ConstantVector::getSplatValue() {
1200 // Check out first element.
1201 Constant *Elt = getOperand(0);
1202 // Then make sure all remaining elements point to the same value.
1203 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
1204 if (getOperand(I) != Elt) return 0;
1205 return Elt;
1206}
1207
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001208//---- ConstantPointerNull::get() implementation...
Chris Lattnerf5ec48d2001-10-13 06:57:33 +00001209//
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001210
Chris Lattner31f84992003-11-21 20:23:48 +00001211namespace llvm {
1212 // ConstantPointerNull does not take extra "value" argument...
1213 template<class ValType>
1214 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1215 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1216 return new ConstantPointerNull(Ty);
1217 }
1218 };
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001219
Chris Lattner31f84992003-11-21 20:23:48 +00001220 template<>
1221 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1222 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1223 // Make everyone now use a constant of the new type...
Owen Anderson04fb7c32009-06-20 00:24:58 +00001224 Constant *New = ConstantPointerNull::get(NewTy);
Chris Lattner31f84992003-11-21 20:23:48 +00001225 assert(New != OldC && "Didn't replace constant??");
1226 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson04fb7c32009-06-20 00:24:58 +00001227 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner31f84992003-11-21 20:23:48 +00001228 }
1229 };
1230}
Chris Lattnered468e372003-10-05 00:17:43 +00001231
Chris Lattner8a94bf12006-09-28 00:35:06 +00001232static ManagedStatic<ValueMap<char, PointerType,
1233 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerf5ec48d2001-10-13 06:57:33 +00001234
Chris Lattner6823c9f2004-08-04 04:48:01 +00001235static char getValType(ConstantPointerNull *) {
1236 return 0;
1237}
1238
1239
Owen Anderson04fb7c32009-06-20 00:24:58 +00001240ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Owen Anderson31c36f02009-06-17 20:10:08 +00001241 // Implicitly locked.
Owen Anderson04fb7c32009-06-20 00:24:58 +00001242 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner6a57baa2001-10-03 15:39:36 +00001243}
1244
Chris Lattner41661fd2002-08-18 00:40:04 +00001245// destroyConstant - Remove the constant from the constant table...
1246//
Owen Anderson04fb7c32009-06-20 00:24:58 +00001247void ConstantPointerNull::destroyConstant() {
Owen Anderson1f0ba8c2009-06-19 18:34:09 +00001248 // Implicitly locked.
Owen Anderson04fb7c32009-06-20 00:24:58 +00001249 NullPtrConstants->remove(this);
Chris Lattner41661fd2002-08-18 00:40:04 +00001250 destroyConstantImpl();
1251}
1252
1253
Chris Lattnerb9f18592004-10-16 18:07:16 +00001254//---- UndefValue::get() implementation...
1255//
1256
1257namespace llvm {
1258 // UndefValue does not take extra "value" argument...
1259 template<class ValType>
1260 struct ConstantCreator<UndefValue, Type, ValType> {
1261 static UndefValue *create(const Type *Ty, const ValType &V) {
1262 return new UndefValue(Ty);
1263 }
1264 };
1265
1266 template<>
1267 struct ConvertConstantType<UndefValue, Type> {
1268 static void convert(UndefValue *OldC, const Type *NewTy) {
1269 // Make everyone now use a constant of the new type.
Owen Anderson04fb7c32009-06-20 00:24:58 +00001270 Constant *New = UndefValue::get(NewTy);
Chris Lattnerb9f18592004-10-16 18:07:16 +00001271 assert(New != OldC && "Didn't replace constant??");
1272 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson04fb7c32009-06-20 00:24:58 +00001273 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattnerb9f18592004-10-16 18:07:16 +00001274 }
1275 };
1276}
1277
Chris Lattner8a94bf12006-09-28 00:35:06 +00001278static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerb9f18592004-10-16 18:07:16 +00001279
1280static char getValType(UndefValue *) {
1281 return 0;
1282}
1283
1284
Owen Anderson04fb7c32009-06-20 00:24:58 +00001285UndefValue *UndefValue::get(const Type *Ty) {
1286 // Implicitly locked.
1287 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerb9f18592004-10-16 18:07:16 +00001288}
1289
1290// destroyConstant - Remove the constant from the constant table.
1291//
Owen Anderson04fb7c32009-06-20 00:24:58 +00001292void UndefValue::destroyConstant() {
Owen Anderson31c36f02009-06-17 20:10:08 +00001293 // Implicitly locked.
Owen Anderson04fb7c32009-06-20 00:24:58 +00001294 UndefValueConstants->remove(this);
Chris Lattnerb9f18592004-10-16 18:07:16 +00001295 destroyConstantImpl();
1296}
1297
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001298//---- ConstantExpr::get() implementations...
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001299//
Reid Spencer79e21d32006-12-31 05:26:44 +00001300
Dan Gohman844731a2008-05-13 00:00:25 +00001301namespace {
1302
Reid Spencer077d0eb2006-12-04 05:19:50 +00001303struct ExprMapKeyType {
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001304 typedef SmallVector<unsigned, 4> IndexList;
1305
1306 ExprMapKeyType(unsigned opc,
1307 const std::vector<Constant*> &ops,
1308 unsigned short pred = 0,
1309 const IndexList &inds = IndexList())
1310 : opcode(opc), predicate(pred), operands(ops), indices(inds) {}
Reid Spencer8d5a6ae2006-12-04 18:38:05 +00001311 uint16_t opcode;
1312 uint16_t predicate;
Reid Spencer077d0eb2006-12-04 05:19:50 +00001313 std::vector<Constant*> operands;
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001314 IndexList indices;
Reid Spencer077d0eb2006-12-04 05:19:50 +00001315 bool operator==(const ExprMapKeyType& that) const {
1316 return this->opcode == that.opcode &&
1317 this->predicate == that.predicate &&
Bill Wendling9a20afd2008-10-26 00:19:56 +00001318 this->operands == that.operands &&
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001319 this->indices == that.indices;
Reid Spencer077d0eb2006-12-04 05:19:50 +00001320 }
1321 bool operator<(const ExprMapKeyType & that) const {
1322 return this->opcode < that.opcode ||
1323 (this->opcode == that.opcode && this->predicate < that.predicate) ||
1324 (this->opcode == that.opcode && this->predicate == that.predicate &&
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001325 this->operands < that.operands) ||
1326 (this->opcode == that.opcode && this->predicate == that.predicate &&
1327 this->operands == that.operands && this->indices < that.indices);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001328 }
1329
1330 bool operator!=(const ExprMapKeyType& that) const {
1331 return !(*this == that);
1332 }
1333};
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001334
Dan Gohman844731a2008-05-13 00:00:25 +00001335}
1336
Chris Lattner31f84992003-11-21 20:23:48 +00001337namespace llvm {
1338 template<>
1339 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Reid Spencer728b6db2006-12-03 05:48:19 +00001340 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
1341 unsigned short pred = 0) {
Reid Spencer077d0eb2006-12-04 05:19:50 +00001342 if (Instruction::isCast(V.opcode))
1343 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
1344 if ((V.opcode >= Instruction::BinaryOpsBegin &&
Reid Spencer832254e2007-02-02 02:16:23 +00001345 V.opcode < Instruction::BinaryOpsEnd))
Reid Spencer077d0eb2006-12-04 05:19:50 +00001346 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1]);
1347 if (V.opcode == Instruction::Select)
1348 return new SelectConstantExpr(V.operands[0], V.operands[1],
1349 V.operands[2]);
1350 if (V.opcode == Instruction::ExtractElement)
1351 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
1352 if (V.opcode == Instruction::InsertElement)
1353 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
1354 V.operands[2]);
1355 if (V.opcode == Instruction::ShuffleVector)
1356 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
1357 V.operands[2]);
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001358 if (V.opcode == Instruction::InsertValue)
1359 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
1360 V.indices, Ty);
1361 if (V.opcode == Instruction::ExtractValue)
1362 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001363 if (V.opcode == Instruction::GetElementPtr) {
1364 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
Gabor Greif051a9502008-04-06 20:25:17 +00001365 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001366 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001367
Reid Spencer077d0eb2006-12-04 05:19:50 +00001368 // The compare instructions are weird. We have to encode the predicate
1369 // value and it is combined with the instruction opcode by multiplying
1370 // the opcode by one hundred. We must decode this to get the predicate.
1371 if (V.opcode == Instruction::ICmp)
Nate Begemanac80ade2008-05-12 19:01:56 +00001372 return new CompareConstantExpr(Ty, Instruction::ICmp, V.predicate,
Reid Spencer077d0eb2006-12-04 05:19:50 +00001373 V.operands[0], V.operands[1]);
1374 if (V.opcode == Instruction::FCmp)
Nate Begemanac80ade2008-05-12 19:01:56 +00001375 return new CompareConstantExpr(Ty, Instruction::FCmp, V.predicate,
1376 V.operands[0], V.operands[1]);
Torok Edwinc23197a2009-07-14 16:55:14 +00001377 llvm_unreachable("Invalid ConstantExpr!");
Jeff Cohen6ebe2352006-12-15 21:47:01 +00001378 return 0;
Chris Lattnered468e372003-10-05 00:17:43 +00001379 }
Chris Lattner31f84992003-11-21 20:23:48 +00001380 };
Chris Lattnered468e372003-10-05 00:17:43 +00001381
Chris Lattner31f84992003-11-21 20:23:48 +00001382 template<>
1383 struct ConvertConstantType<ConstantExpr, Type> {
1384 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1385 Constant *New;
1386 switch (OldC->getOpcode()) {
Reid Spencer3da59db2006-11-27 01:05:10 +00001387 case Instruction::Trunc:
1388 case Instruction::ZExt:
1389 case Instruction::SExt:
1390 case Instruction::FPTrunc:
1391 case Instruction::FPExt:
1392 case Instruction::UIToFP:
1393 case Instruction::SIToFP:
1394 case Instruction::FPToUI:
1395 case Instruction::FPToSI:
1396 case Instruction::PtrToInt:
1397 case Instruction::IntToPtr:
1398 case Instruction::BitCast:
Reid Spencerd977d862006-12-12 23:36:14 +00001399 New = ConstantExpr::getCast(OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson04fb7c32009-06-20 00:24:58 +00001400 NewTy);
Chris Lattner31f84992003-11-21 20:23:48 +00001401 break;
Chris Lattner08a45cc2004-03-12 05:54:04 +00001402 case Instruction::Select:
1403 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1404 OldC->getOperand(1),
Owen Anderson04fb7c32009-06-20 00:24:58 +00001405 OldC->getOperand(2));
Chris Lattner08a45cc2004-03-12 05:54:04 +00001406 break;
Chris Lattner31f84992003-11-21 20:23:48 +00001407 default:
1408 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
Reid Spencer0a783f72006-11-02 01:53:59 +00001409 OldC->getOpcode() < Instruction::BinaryOpsEnd);
Chris Lattner31f84992003-11-21 20:23:48 +00001410 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
Owen Anderson04fb7c32009-06-20 00:24:58 +00001411 OldC->getOperand(1));
Chris Lattner31f84992003-11-21 20:23:48 +00001412 break;
1413 case Instruction::GetElementPtr:
Misha Brukmanfd939082005-04-21 23:48:37 +00001414 // Make everyone now use a constant of the new type...
Chris Lattner7fa6e662004-10-11 22:52:25 +00001415 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001416 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
Owen Anderson04fb7c32009-06-20 00:24:58 +00001417 &Idx[0], Idx.size());
Chris Lattner31f84992003-11-21 20:23:48 +00001418 break;
1419 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001420
Chris Lattner31f84992003-11-21 20:23:48 +00001421 assert(New != OldC && "Didn't replace constant??");
1422 OldC->uncheckedReplaceAllUsesWith(New);
Owen Anderson04fb7c32009-06-20 00:24:58 +00001423 OldC->destroyConstant(); // This constant is now dead, destroy it.
Chris Lattner31f84992003-11-21 20:23:48 +00001424 }
1425 };
1426} // end namespace llvm
Chris Lattnered468e372003-10-05 00:17:43 +00001427
1428
Chris Lattner6823c9f2004-08-04 04:48:01 +00001429static ExprMapKeyType getValType(ConstantExpr *CE) {
1430 std::vector<Constant*> Operands;
1431 Operands.reserve(CE->getNumOperands());
1432 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1433 Operands.push_back(cast<Constant>(CE->getOperand(i)));
Reid Spencer077d0eb2006-12-04 05:19:50 +00001434 return ExprMapKeyType(CE->getOpcode(), Operands,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001435 CE->isCompare() ? CE->getPredicate() : 0,
1436 CE->hasIndices() ?
1437 CE->getIndices() : SmallVector<unsigned, 4>());
Chris Lattner6823c9f2004-08-04 04:48:01 +00001438}
1439
Chris Lattner8a94bf12006-09-28 00:35:06 +00001440static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1441 ConstantExpr> > ExprConstants;
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001442
Reid Spencer3da59db2006-11-27 01:05:10 +00001443/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands66a1a052008-03-30 19:38:55 +00001444/// cast in the ExprConstants map. It is used by the various get* methods below.
Reid Spencer3da59db2006-11-27 01:05:10 +00001445static inline Constant *getFoldedCast(
Owen Anderson04fb7c32009-06-20 00:24:58 +00001446 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner9eacf8a2003-10-07 22:19:19 +00001447 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer3da59db2006-11-27 01:05:10 +00001448 // Fold a few common cases
Owen Anderson5defacc2009-07-31 17:39:07 +00001449 if (Constant *FC = ConstantFoldCastInstruction(Ty->getContext(), opc, C, Ty))
Reid Spencer3da59db2006-11-27 01:05:10 +00001450 return FC;
Chris Lattnerd628f6a2003-04-17 19:24:48 +00001451
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001452 // Look up the constant in the table first to ensure uniqueness
Chris Lattner9bc02a42003-05-13 21:37:02 +00001453 std::vector<Constant*> argVec(1, C);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001454 ExprMapKeyType Key(opc, argVec);
Owen Anderson3e456ab2009-06-17 18:40:29 +00001455
Owen Anderson31c36f02009-06-17 20:10:08 +00001456 // Implicitly locked.
Owen Anderson04fb7c32009-06-20 00:24:58 +00001457 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001458}
Reid Spencer7858b332006-12-05 19:14:13 +00001459
Owen Anderson04fb7c32009-06-20 00:24:58 +00001460Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
Reid Spencer3da59db2006-11-27 01:05:10 +00001461 Instruction::CastOps opc = Instruction::CastOps(oc);
1462 assert(Instruction::isCast(opc) && "opcode out of range");
1463 assert(C && Ty && "Null arguments to getCast");
1464 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1465
1466 switch (opc) {
1467 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00001468 llvm_unreachable("Invalid cast opcode");
Reid Spencer3da59db2006-11-27 01:05:10 +00001469 break;
Owen Anderson04fb7c32009-06-20 00:24:58 +00001470 case Instruction::Trunc: return getTrunc(C, Ty);
1471 case Instruction::ZExt: return getZExt(C, Ty);
1472 case Instruction::SExt: return getSExt(C, Ty);
1473 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1474 case Instruction::FPExt: return getFPExtend(C, Ty);
1475 case Instruction::UIToFP: return getUIToFP(C, Ty);
1476 case Instruction::SIToFP: return getSIToFP(C, Ty);
1477 case Instruction::FPToUI: return getFPToUI(C, Ty);
1478 case Instruction::FPToSI: return getFPToSI(C, Ty);
1479 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1480 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1481 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattnerf5ac6c22005-01-01 15:59:57 +00001482 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001483 return 0;
Reid Spencer7858b332006-12-05 19:14:13 +00001484}
1485
Reid Spencer848414e2006-12-04 20:17:56 +00001486Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001487 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer848414e2006-12-04 20:17:56 +00001488 return getCast(Instruction::BitCast, C, Ty);
1489 return getCast(Instruction::ZExt, C, Ty);
1490}
1491
Owen Anderson04fb7c32009-06-20 00:24:58 +00001492Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001493 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Owen Anderson04fb7c32009-06-20 00:24:58 +00001494 return getCast(Instruction::BitCast, C, Ty);
1495 return getCast(Instruction::SExt, C, Ty);
Reid Spencer848414e2006-12-04 20:17:56 +00001496}
1497
1498Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001499 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer848414e2006-12-04 20:17:56 +00001500 return getCast(Instruction::BitCast, C, Ty);
1501 return getCast(Instruction::Trunc, C, Ty);
1502}
1503
Owen Anderson04fb7c32009-06-20 00:24:58 +00001504Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
Reid Spencerc0459fb2006-12-05 03:25:26 +00001505 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner42a75512007-01-15 02:27:26 +00001506 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerc0459fb2006-12-05 03:25:26 +00001507
Chris Lattner42a75512007-01-15 02:27:26 +00001508 if (Ty->isInteger())
Owen Anderson04fb7c32009-06-20 00:24:58 +00001509 return getCast(Instruction::PtrToInt, S, Ty);
1510 return getCast(Instruction::BitCast, S, Ty);
Reid Spencerc0459fb2006-12-05 03:25:26 +00001511}
1512
Reid Spencer84f3eab2006-12-12 00:51:07 +00001513Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1514 bool isSigned) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001515 assert(C->getType()->isIntOrIntVector() &&
1516 Ty->isIntOrIntVector() && "Invalid cast");
1517 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1518 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer84f3eab2006-12-12 00:51:07 +00001519 Instruction::CastOps opcode =
1520 (SrcBits == DstBits ? Instruction::BitCast :
1521 (SrcBits > DstBits ? Instruction::Trunc :
1522 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1523 return getCast(opcode, C, Ty);
1524}
1525
1526Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001527 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer84f3eab2006-12-12 00:51:07 +00001528 "Invalid cast");
Dan Gohman6de29f82009-06-15 22:12:54 +00001529 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1530 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencerf25212a2006-12-12 05:38:50 +00001531 if (SrcBits == DstBits)
1532 return C; // Avoid a useless cast
Reid Spencer84f3eab2006-12-12 00:51:07 +00001533 Instruction::CastOps opcode =
Reid Spencerf25212a2006-12-12 05:38:50 +00001534 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer84f3eab2006-12-12 00:51:07 +00001535 return getCast(opcode, C, Ty);
1536}
1537
Owen Anderson04fb7c32009-06-20 00:24:58 +00001538Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001539#ifndef NDEBUG
1540 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1541 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1542#endif
1543 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1544 assert(C->getType()->isIntOrIntVector() && "Trunc operand must be integer");
1545 assert(Ty->isIntOrIntVector() && "Trunc produces only integral");
1546 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer3da59db2006-11-27 01:05:10 +00001547 "SrcTy must be larger than DestTy for Trunc!");
1548
Owen Anderson04fb7c32009-06-20 00:24:58 +00001549 return getFoldedCast(Instruction::Trunc, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001550}
1551
Owen Anderson04fb7c32009-06-20 00:24:58 +00001552Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001553#ifndef NDEBUG
1554 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1555 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1556#endif
1557 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1558 assert(C->getType()->isIntOrIntVector() && "SExt operand must be integral");
1559 assert(Ty->isIntOrIntVector() && "SExt produces only integer");
1560 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer3da59db2006-11-27 01:05:10 +00001561 "SrcTy must be smaller than DestTy for SExt!");
1562
Owen Anderson04fb7c32009-06-20 00:24:58 +00001563 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerd144f422004-04-04 23:20:30 +00001564}
1565
Owen Anderson04fb7c32009-06-20 00:24:58 +00001566Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001567#ifndef NDEBUG
1568 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1569 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1570#endif
1571 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1572 assert(C->getType()->isIntOrIntVector() && "ZEXt operand must be integral");
1573 assert(Ty->isIntOrIntVector() && "ZExt produces only integer");
1574 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer3da59db2006-11-27 01:05:10 +00001575 "SrcTy must be smaller than DestTy for ZExt!");
1576
Owen Anderson04fb7c32009-06-20 00:24:58 +00001577 return getFoldedCast(Instruction::ZExt, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001578}
1579
Owen Anderson04fb7c32009-06-20 00:24:58 +00001580Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001581#ifndef NDEBUG
1582 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1583 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1584#endif
1585 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1586 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1587 C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer3da59db2006-11-27 01:05:10 +00001588 "This is an illegal floating point truncation!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001589 return getFoldedCast(Instruction::FPTrunc, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001590}
1591
Owen Anderson04fb7c32009-06-20 00:24:58 +00001592Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001593#ifndef NDEBUG
1594 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1595 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1596#endif
1597 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1598 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1599 C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer3da59db2006-11-27 01:05:10 +00001600 "This is an illegal floating point extension!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001601 return getFoldedCast(Instruction::FPExt, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001602}
1603
Owen Anderson04fb7c32009-06-20 00:24:58 +00001604Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Devang Patelb6dc9352008-11-03 23:20:04 +00001605#ifndef NDEBUG
Nate Begemanb348d182007-11-17 03:58:34 +00001606 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1607 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Patelb6dc9352008-11-03 23:20:04 +00001608#endif
Nate Begemanb348d182007-11-17 03:58:34 +00001609 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1610 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
1611 "This is an illegal uint to floating point cast!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001612 return getFoldedCast(Instruction::UIToFP, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001613}
1614
Owen Anderson04fb7c32009-06-20 00:24:58 +00001615Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Devang Patelb6dc9352008-11-03 23:20:04 +00001616#ifndef NDEBUG
Nate Begemanb348d182007-11-17 03:58:34 +00001617 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1618 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Patelb6dc9352008-11-03 23:20:04 +00001619#endif
Nate Begemanb348d182007-11-17 03:58:34 +00001620 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1621 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
Reid Spencer3da59db2006-11-27 01:05:10 +00001622 "This is an illegal sint to floating point cast!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001623 return getFoldedCast(Instruction::SIToFP, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001624}
1625
Owen Anderson04fb7c32009-06-20 00:24:58 +00001626Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Devang Patelb6dc9352008-11-03 23:20:04 +00001627#ifndef NDEBUG
Nate Begemanb348d182007-11-17 03:58:34 +00001628 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1629 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Patelb6dc9352008-11-03 23:20:04 +00001630#endif
Nate Begemanb348d182007-11-17 03:58:34 +00001631 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1632 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1633 "This is an illegal floating point to uint cast!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001634 return getFoldedCast(Instruction::FPToUI, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001635}
1636
Owen Anderson04fb7c32009-06-20 00:24:58 +00001637Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Devang Patelb6dc9352008-11-03 23:20:04 +00001638#ifndef NDEBUG
Nate Begemanb348d182007-11-17 03:58:34 +00001639 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1640 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Patelb6dc9352008-11-03 23:20:04 +00001641#endif
Nate Begemanb348d182007-11-17 03:58:34 +00001642 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1643 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1644 "This is an illegal floating point to sint cast!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001645 return getFoldedCast(Instruction::FPToSI, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001646}
1647
Owen Anderson04fb7c32009-06-20 00:24:58 +00001648Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
Reid Spencer3da59db2006-11-27 01:05:10 +00001649 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner42a75512007-01-15 02:27:26 +00001650 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001651 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
Reid Spencer3da59db2006-11-27 01:05:10 +00001652}
1653
Owen Anderson04fb7c32009-06-20 00:24:58 +00001654Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner42a75512007-01-15 02:27:26 +00001655 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer3da59db2006-11-27 01:05:10 +00001656 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001657 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
Reid Spencer3da59db2006-11-27 01:05:10 +00001658}
1659
Owen Anderson04fb7c32009-06-20 00:24:58 +00001660Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
Reid Spencer3da59db2006-11-27 01:05:10 +00001661 // BitCast implies a no-op cast of type only. No bits change. However, you
1662 // can't cast pointers to anything but pointers.
Devang Patelb6dc9352008-11-03 23:20:04 +00001663#ifndef NDEBUG
Reid Spencer3da59db2006-11-27 01:05:10 +00001664 const Type *SrcTy = C->getType();
1665 assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
Reid Spencer848414e2006-12-04 20:17:56 +00001666 "BitCast cannot cast pointer to non-pointer and vice versa");
Reid Spencer3da59db2006-11-27 01:05:10 +00001667
1668 // Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
1669 // or nonptr->ptr). For all the other types, the cast is okay if source and
1670 // destination bit widths are identical.
1671 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1672 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
Devang Patelb6dc9352008-11-03 23:20:04 +00001673#endif
Chris Lattnercf1bb082009-03-08 04:06:26 +00001674 assert(SrcBitSize == DstBitSize && "BitCast requires types of same width");
Chris Lattner8c7f24a2009-03-21 06:55:54 +00001675
1676 // It is common to ask for a bitcast of a value to its own type, handle this
1677 // speedily.
1678 if (C->getType() == DstTy) return C;
1679
Owen Anderson04fb7c32009-06-20 00:24:58 +00001680 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerd144f422004-04-04 23:20:30 +00001681}
1682
Chris Lattnered468e372003-10-05 00:17:43 +00001683Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Owen Anderson04fb7c32009-06-20 00:24:58 +00001684 Constant *C1, Constant *C2) {
Chris Lattnerf31f5832003-05-21 17:49:25 +00001685 // Check the operands for consistency first
Reid Spencer0a783f72006-11-02 01:53:59 +00001686 assert(Opcode >= Instruction::BinaryOpsBegin &&
1687 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattnerf31f5832003-05-21 17:49:25 +00001688 "Invalid opcode in binary constant expression");
1689 assert(C1->getType() == C2->getType() &&
1690 "Operand types in binary constant expression should match");
Chris Lattnered468e372003-10-05 00:17:43 +00001691
Reid Spencer4fe16d62007-01-11 18:21:29 +00001692 if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
Owen Anderson5defacc2009-07-31 17:39:07 +00001693 if (Constant *FC = ConstantFoldBinaryInstruction(ReqTy->getContext(),
1694 Opcode, C1, C2))
Chris Lattnered468e372003-10-05 00:17:43 +00001695 return FC; // Fold a few common cases...
Chris Lattnerd628f6a2003-04-17 19:24:48 +00001696
Chris Lattner9bc02a42003-05-13 21:37:02 +00001697 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Reid Spencer67263fe2006-12-04 21:35:24 +00001698 ExprMapKeyType Key(Opcode, argVec);
Owen Anderson31c36f02009-06-17 20:10:08 +00001699
Owen Anderson04fb7c32009-06-20 00:24:58 +00001700 // Implicitly locked.
1701 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001702}
1703
Reid Spencere4d87aa2006-12-23 06:05:41 +00001704Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Nate Begemanff795a82008-07-25 17:56:27 +00001705 Constant *C1, Constant *C2) {
Reid Spencere4d87aa2006-12-23 06:05:41 +00001706 switch (predicate) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001707 default: llvm_unreachable("Invalid CmpInst predicate");
Nate Begemanb5557ab2008-07-25 17:35:37 +00001708 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
1709 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
1710 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
1711 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
1712 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
1713 case CmpInst::FCMP_TRUE:
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00001714 return getFCmp(predicate, C1, C2);
1715
Nate Begemanb5557ab2008-07-25 17:35:37 +00001716 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
1717 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
1718 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
1719 case CmpInst::ICMP_SLE:
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00001720 return getICmp(predicate, C1, C2);
Reid Spencere4d87aa2006-12-23 06:05:41 +00001721 }
Reid Spencer67263fe2006-12-04 21:35:24 +00001722}
1723
Owen Anderson04fb7c32009-06-20 00:24:58 +00001724Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001725 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1726 if (C1->getType()->isFPOrFPVector()) {
1727 if (Opcode == Instruction::Add) Opcode = Instruction::FAdd;
1728 else if (Opcode == Instruction::Sub) Opcode = Instruction::FSub;
1729 else if (Opcode == Instruction::Mul) Opcode = Instruction::FMul;
1730 }
Chris Lattner91b362b2004-08-17 17:28:46 +00001731#ifndef NDEBUG
1732 switch (Opcode) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001733 case Instruction::Add:
Reid Spencer0a783f72006-11-02 01:53:59 +00001734 case Instruction::Sub:
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001735 case Instruction::Mul:
Chris Lattner91b362b2004-08-17 17:28:46 +00001736 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001737 assert(C1->getType()->isIntOrIntVector() &&
1738 "Tried to create an integer operation on a non-integer type!");
1739 break;
1740 case Instruction::FAdd:
1741 case Instruction::FSub:
1742 case Instruction::FMul:
1743 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1744 assert(C1->getType()->isFPOrFPVector() &&
1745 "Tried to create a floating-point operation on a "
1746 "non-floating-point type!");
Chris Lattner91b362b2004-08-17 17:28:46 +00001747 break;
Reid Spencer1628cec2006-10-26 06:15:43 +00001748 case Instruction::UDiv:
1749 case Instruction::SDiv:
1750 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanf57478f2009-06-15 22:25:12 +00001751 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer1628cec2006-10-26 06:15:43 +00001752 "Tried to create an arithmetic operation on a non-arithmetic type!");
1753 break;
1754 case Instruction::FDiv:
1755 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanf57478f2009-06-15 22:25:12 +00001756 assert(C1->getType()->isFPOrFPVector() &&
1757 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer1628cec2006-10-26 06:15:43 +00001758 break;
Reid Spencer0a783f72006-11-02 01:53:59 +00001759 case Instruction::URem:
1760 case Instruction::SRem:
1761 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanf57478f2009-06-15 22:25:12 +00001762 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer0a783f72006-11-02 01:53:59 +00001763 "Tried to create an arithmetic operation on a non-arithmetic type!");
1764 break;
1765 case Instruction::FRem:
1766 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanf57478f2009-06-15 22:25:12 +00001767 assert(C1->getType()->isFPOrFPVector() &&
1768 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer0a783f72006-11-02 01:53:59 +00001769 break;
Chris Lattner91b362b2004-08-17 17:28:46 +00001770 case Instruction::And:
1771 case Instruction::Or:
1772 case Instruction::Xor:
1773 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanf57478f2009-06-15 22:25:12 +00001774 assert(C1->getType()->isIntOrIntVector() &&
Misha Brukman1bae2912005-01-27 06:46:38 +00001775 "Tried to create a logical operation on a non-integral type!");
Chris Lattner91b362b2004-08-17 17:28:46 +00001776 break;
Chris Lattner91b362b2004-08-17 17:28:46 +00001777 case Instruction::Shl:
Reid Spencer3822ff52006-11-08 06:47:33 +00001778 case Instruction::LShr:
1779 case Instruction::AShr:
Reid Spencer832254e2007-02-02 02:16:23 +00001780 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanc1317932009-03-14 17:09:17 +00001781 assert(C1->getType()->isIntOrIntVector() &&
Chris Lattner91b362b2004-08-17 17:28:46 +00001782 "Tried to create a shift operation on a non-integer type!");
1783 break;
1784 default:
1785 break;
1786 }
1787#endif
1788
Owen Anderson04fb7c32009-06-20 00:24:58 +00001789 return getTy(C1->getType(), Opcode, C1, C2);
Reid Spencer67263fe2006-12-04 21:35:24 +00001790}
1791
Owen Andersonbaf3c402009-07-29 18:55:55 +00001792Constant* ConstantExpr::getSizeOf(const Type* Ty) {
1793 // sizeof is implemented as: (i64) gep (Ty*)null, 1
1794 // Note that a non-inbounds gep is used, as null isn't within any object.
Owen Andersonbaf3c402009-07-29 18:55:55 +00001795 Constant *GEPIdx = ConstantInt::get(Type::Int32Ty, 1);
1796 Constant *GEP = getGetElementPtr(
Owen Andersona7235ea2009-07-31 20:28:14 +00001797 Constant::getNullValue(PointerType::getUnqual(Ty)), &GEPIdx, 1);
Owen Andersonbaf3c402009-07-29 18:55:55 +00001798 return getCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
1799}
1800
1801Constant* ConstantExpr::getAlignOf(const Type* Ty) {
Owen Andersonbaf3c402009-07-29 18:55:55 +00001802 // alignof is implemented as: (i64) gep ({i8,Ty}*)null, 0, 1
1803 const Type *AligningTy = StructType::get(Type::Int8Ty, Ty, NULL);
Owen Andersona7235ea2009-07-31 20:28:14 +00001804 Constant *NullPtr = Constant::getNullValue(AligningTy->getPointerTo());
Owen Andersonbaf3c402009-07-29 18:55:55 +00001805 Constant *Zero = ConstantInt::get(Type::Int32Ty, 0);
1806 Constant *One = ConstantInt::get(Type::Int32Ty, 1);
1807 Constant *Indices[2] = { Zero, One };
1808 Constant *GEP = getGetElementPtr(NullPtr, Indices, 2);
1809 return getCast(Instruction::PtrToInt, GEP, Type::Int32Ty);
1810}
1811
1812
Reid Spencere4d87aa2006-12-23 06:05:41 +00001813Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencer67263fe2006-12-04 21:35:24 +00001814 Constant *C1, Constant *C2) {
1815 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencere4d87aa2006-12-23 06:05:41 +00001816 return getCompareTy(pred, C1, C2);
Chris Lattnerc3d12f02004-08-04 18:50:09 +00001817}
1818
Chris Lattner08a45cc2004-03-12 05:54:04 +00001819Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
Owen Anderson04fb7c32009-06-20 00:24:58 +00001820 Constant *V1, Constant *V2) {
Chris Lattner9ace0cd2008-12-29 00:16:12 +00001821 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner08a45cc2004-03-12 05:54:04 +00001822
1823 if (ReqTy == V1->getType())
Owen Anderson0a5372e2009-07-13 04:09:18 +00001824 if (Constant *SC = ConstantFoldSelectInstruction(
Owen Anderson5defacc2009-07-31 17:39:07 +00001825 ReqTy->getContext(), C, V1, V2))
Chris Lattner08a45cc2004-03-12 05:54:04 +00001826 return SC; // Fold common cases
1827
1828 std::vector<Constant*> argVec(3, C);
1829 argVec[1] = V1;
1830 argVec[2] = V2;
Reid Spencer077d0eb2006-12-04 05:19:50 +00001831 ExprMapKeyType Key(Instruction::Select, argVec);
Owen Anderson31c36f02009-06-17 20:10:08 +00001832
Owen Anderson04fb7c32009-06-20 00:24:58 +00001833 // Implicitly locked.
1834 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner08a45cc2004-03-12 05:54:04 +00001835}
1836
Chris Lattnered468e372003-10-05 00:17:43 +00001837Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001838 Value* const *Idxs,
Owen Anderson04fb7c32009-06-20 00:24:58 +00001839 unsigned NumIdx) {
Dan Gohman041e2eb2008-05-15 19:50:34 +00001840 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
1841 Idxs+NumIdx) ==
1842 cast<PointerType>(ReqTy)->getElementType() &&
1843 "GEP indices invalid!");
Chris Lattner2e9bb1a2004-02-16 20:46:13 +00001844
Owen Anderson0a5372e2009-07-13 04:09:18 +00001845 if (Constant *FC = ConstantFoldGetElementPtr(
Owen Anderson5defacc2009-07-31 17:39:07 +00001846 ReqTy->getContext(), C, (Constant**)Idxs, NumIdx))
Chris Lattnerd628f6a2003-04-17 19:24:48 +00001847 return FC; // Fold a few common cases...
Chris Lattner2e9bb1a2004-02-16 20:46:13 +00001848
Chris Lattnered468e372003-10-05 00:17:43 +00001849 assert(isa<PointerType>(C->getType()) &&
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001850 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001851 // Look up the constant in the table first to ensure uniqueness
Chris Lattner7fa6e662004-10-11 22:52:25 +00001852 std::vector<Constant*> ArgVec;
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001853 ArgVec.reserve(NumIdx+1);
Chris Lattner7fa6e662004-10-11 22:52:25 +00001854 ArgVec.push_back(C);
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001855 for (unsigned i = 0; i != NumIdx; ++i)
1856 ArgVec.push_back(cast<Constant>(Idxs[i]));
1857 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Owen Anderson31c36f02009-06-17 20:10:08 +00001858
1859 // Implicitly locked.
Owen Anderson04fb7c32009-06-20 00:24:58 +00001860 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001861}
1862
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001863Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
Owen Anderson04fb7c32009-06-20 00:24:58 +00001864 unsigned NumIdx) {
Chris Lattnered468e372003-10-05 00:17:43 +00001865 // Get the result type of the getelementptr!
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001866 const Type *Ty =
Dan Gohman041e2eb2008-05-15 19:50:34 +00001867 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
Chris Lattnered468e372003-10-05 00:17:43 +00001868 assert(Ty && "GEP indices invalid!");
Christopher Lambfe63fb92007-12-11 08:59:05 +00001869 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
Owen Anderson04fb7c32009-06-20 00:24:58 +00001870 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
Chris Lattner7fa6e662004-10-11 22:52:25 +00001871}
1872
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001873Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
Owen Anderson04fb7c32009-06-20 00:24:58 +00001874 unsigned NumIdx) {
1875 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnered468e372003-10-05 00:17:43 +00001876}
1877
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001878
Reid Spencer077d0eb2006-12-04 05:19:50 +00001879Constant *
1880ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1881 assert(LHS->getType() == RHS->getType());
1882 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
1883 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
1884
Owen Anderson0a5372e2009-07-13 04:09:18 +00001885 if (Constant *FC = ConstantFoldCompareInstruction(
Owen Anderson5defacc2009-07-31 17:39:07 +00001886 LHS->getContext(), pred, LHS, RHS))
Reid Spencer077d0eb2006-12-04 05:19:50 +00001887 return FC; // Fold a few common cases...
1888
1889 // Look up the constant in the table first to ensure uniqueness
1890 std::vector<Constant*> ArgVec;
1891 ArgVec.push_back(LHS);
1892 ArgVec.push_back(RHS);
Reid Spencer4fa021a2006-12-24 18:42:29 +00001893 // Get the key type with both the opcode and predicate
Reid Spencer077d0eb2006-12-04 05:19:50 +00001894 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Owen Anderson31c36f02009-06-17 20:10:08 +00001895
1896 // Implicitly locked.
1897 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001898}
1899
1900Constant *
1901ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
1902 assert(LHS->getType() == RHS->getType());
1903 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
1904
Owen Anderson0a5372e2009-07-13 04:09:18 +00001905 if (Constant *FC = ConstantFoldCompareInstruction(
Owen Anderson5defacc2009-07-31 17:39:07 +00001906 LHS->getContext(), pred, LHS, RHS))
Reid Spencer077d0eb2006-12-04 05:19:50 +00001907 return FC; // Fold a few common cases...
1908
1909 // Look up the constant in the table first to ensure uniqueness
1910 std::vector<Constant*> ArgVec;
1911 ArgVec.push_back(LHS);
1912 ArgVec.push_back(RHS);
Reid Spencer4fa021a2006-12-24 18:42:29 +00001913 // Get the key type with both the opcode and predicate
Reid Spencer077d0eb2006-12-04 05:19:50 +00001914 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Owen Anderson31c36f02009-06-17 20:10:08 +00001915
1916 // Implicitly locked.
1917 return ExprConstants->getOrCreate(Type::Int1Ty, Key);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001918}
1919
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001920Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
1921 Constant *Idx) {
Owen Anderson0a5372e2009-07-13 04:09:18 +00001922 if (Constant *FC = ConstantFoldExtractElementInstruction(
Owen Anderson5defacc2009-07-31 17:39:07 +00001923 ReqTy->getContext(), Val, Idx))
Robert Bocchinobb90a7a2006-01-10 20:03:46 +00001924 return FC; // Fold a few common cases...
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001925 // Look up the constant in the table first to ensure uniqueness
1926 std::vector<Constant*> ArgVec(1, Val);
1927 ArgVec.push_back(Idx);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001928 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Owen Anderson31c36f02009-06-17 20:10:08 +00001929
1930 // Implicitly locked.
1931 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001932}
1933
1934Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001935 assert(isa<VectorType>(Val->getType()) &&
Reid Spencerac9dcb92007-02-15 03:39:18 +00001936 "Tried to create extractelement operation on non-vector type!");
Reid Spencer79e21d32006-12-31 05:26:44 +00001937 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer3d10b0b2007-01-26 07:37:34 +00001938 "Extractelement index must be i32 type!");
Reid Spencer9d6565a2007-02-15 02:26:10 +00001939 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001940 Val, Idx);
1941}
Chris Lattnered468e372003-10-05 00:17:43 +00001942
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001943Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
1944 Constant *Elt, Constant *Idx) {
Owen Anderson0a5372e2009-07-13 04:09:18 +00001945 if (Constant *FC = ConstantFoldInsertElementInstruction(
Owen Anderson5defacc2009-07-31 17:39:07 +00001946 ReqTy->getContext(), Val, Elt, Idx))
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001947 return FC; // Fold a few common cases...
1948 // Look up the constant in the table first to ensure uniqueness
1949 std::vector<Constant*> ArgVec(1, Val);
1950 ArgVec.push_back(Elt);
1951 ArgVec.push_back(Idx);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001952 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Owen Anderson31c36f02009-06-17 20:10:08 +00001953
1954 // Implicitly locked.
1955 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001956}
1957
1958Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1959 Constant *Idx) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001960 assert(isa<VectorType>(Val->getType()) &&
Reid Spencerac9dcb92007-02-15 03:39:18 +00001961 "Tried to create insertelement operation on non-vector type!");
Reid Spencer9d6565a2007-02-15 02:26:10 +00001962 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001963 && "Insertelement types must match!");
Reid Spencer79e21d32006-12-31 05:26:44 +00001964 assert(Idx->getType() == Type::Int32Ty &&
Reid Spencer3d10b0b2007-01-26 07:37:34 +00001965 "Insertelement index must be i32 type!");
Gordon Henriksen699609c2008-08-30 15:41:51 +00001966 return getInsertElementTy(Val->getType(), Val, Elt, Idx);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001967}
1968
Chris Lattner00f10232006-04-08 01:18:18 +00001969Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
1970 Constant *V2, Constant *Mask) {
Owen Anderson0a5372e2009-07-13 04:09:18 +00001971 if (Constant *FC = ConstantFoldShuffleVectorInstruction(
Owen Anderson5defacc2009-07-31 17:39:07 +00001972 ReqTy->getContext(), V1, V2, Mask))
Chris Lattner00f10232006-04-08 01:18:18 +00001973 return FC; // Fold a few common cases...
1974 // Look up the constant in the table first to ensure uniqueness
1975 std::vector<Constant*> ArgVec(1, V1);
1976 ArgVec.push_back(V2);
1977 ArgVec.push_back(Mask);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001978 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Owen Anderson31c36f02009-06-17 20:10:08 +00001979
1980 // Implicitly locked.
1981 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner00f10232006-04-08 01:18:18 +00001982}
1983
1984Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
1985 Constant *Mask) {
1986 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
1987 "Invalid shuffle vector constant expr operands!");
Nate Begeman0f123cf2009-02-12 21:28:33 +00001988
1989 unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements();
1990 const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
1991 const Type *ShufTy = VectorType::get(EltTy, NElts);
1992 return getShuffleVectorTy(ShufTy, V1, V2, Mask);
Chris Lattner00f10232006-04-08 01:18:18 +00001993}
1994
Dan Gohman041e2eb2008-05-15 19:50:34 +00001995Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg,
1996 Constant *Val,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001997 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman041e2eb2008-05-15 19:50:34 +00001998 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
1999 Idxs+NumIdx) == Val->getType() &&
2000 "insertvalue indices invalid!");
2001 assert(Agg->getType() == ReqTy &&
2002 "insertvalue type invalid!");
Dan Gohmane4569942008-05-23 00:36:11 +00002003 assert(Agg->getType()->isFirstClassType() &&
2004 "Non-first-class type for constant InsertValue expression");
Owen Anderson0a5372e2009-07-13 04:09:18 +00002005 Constant *FC = ConstantFoldInsertValueInstruction(
Owen Anderson5defacc2009-07-31 17:39:07 +00002006 ReqTy->getContext(), Agg, Val, Idxs, NumIdx);
Dan Gohmane0891602008-07-21 23:30:30 +00002007 assert(FC && "InsertValue constant expr couldn't be folded!");
2008 return FC;
Dan Gohman041e2eb2008-05-15 19:50:34 +00002009}
2010
2011Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002012 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohmane4569942008-05-23 00:36:11 +00002013 assert(Agg->getType()->isFirstClassType() &&
2014 "Tried to create insertelement operation on non-first-class type!");
Dan Gohman041e2eb2008-05-15 19:50:34 +00002015
Dan Gohmane4569942008-05-23 00:36:11 +00002016 const Type *ReqTy = Agg->getType();
Devang Patelb6dc9352008-11-03 23:20:04 +00002017#ifndef NDEBUG
Dan Gohmane4569942008-05-23 00:36:11 +00002018 const Type *ValTy =
Dan Gohman041e2eb2008-05-15 19:50:34 +00002019 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
Devang Patelb6dc9352008-11-03 23:20:04 +00002020#endif
Dan Gohmane4569942008-05-23 00:36:11 +00002021 assert(ValTy == Val->getType() && "insertvalue indices invalid!");
Dan Gohman041e2eb2008-05-15 19:50:34 +00002022 return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx);
2023}
2024
2025Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002026 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman041e2eb2008-05-15 19:50:34 +00002027 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
2028 Idxs+NumIdx) == ReqTy &&
2029 "extractvalue indices invalid!");
Dan Gohmane4569942008-05-23 00:36:11 +00002030 assert(Agg->getType()->isFirstClassType() &&
2031 "Non-first-class type for constant extractvalue expression");
Owen Anderson0a5372e2009-07-13 04:09:18 +00002032 Constant *FC = ConstantFoldExtractValueInstruction(
Owen Anderson5defacc2009-07-31 17:39:07 +00002033 ReqTy->getContext(), Agg, Idxs, NumIdx);
Dan Gohmane0891602008-07-21 23:30:30 +00002034 assert(FC && "ExtractValue constant expr couldn't be folded!");
2035 return FC;
Dan Gohman041e2eb2008-05-15 19:50:34 +00002036}
2037
2038Constant *ConstantExpr::getExtractValue(Constant *Agg,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002039 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohmane4569942008-05-23 00:36:11 +00002040 assert(Agg->getType()->isFirstClassType() &&
2041 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman041e2eb2008-05-15 19:50:34 +00002042
2043 const Type *ReqTy =
2044 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
2045 assert(ReqTy && "extractvalue indices invalid!");
2046 return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx);
2047}
2048
Owen Andersonbaf3c402009-07-29 18:55:55 +00002049Constant* ConstantExpr::getNeg(Constant* C) {
2050 // API compatibility: Adjust integer opcodes to floating-point opcodes.
2051 if (C->getType()->isFPOrFPVector())
2052 return getFNeg(C);
2053 assert(C->getType()->isIntOrIntVector() &&
2054 "Cannot NEG a nonintegral value!");
2055 return get(Instruction::Sub,
2056 ConstantFP::getZeroValueForNegation(C->getType()),
2057 C);
2058}
2059
2060Constant* ConstantExpr::getFNeg(Constant* C) {
2061 assert(C->getType()->isFPOrFPVector() &&
2062 "Cannot FNEG a non-floating-point value!");
2063 return get(Instruction::FSub,
2064 ConstantFP::getZeroValueForNegation(C->getType()),
2065 C);
2066}
2067
2068Constant* ConstantExpr::getNot(Constant* C) {
2069 assert(C->getType()->isIntOrIntVector() &&
2070 "Cannot NOT a nonintegral value!");
Owen Andersona7235ea2009-07-31 20:28:14 +00002071 return get(Instruction::Xor, C, Constant::getAllOnesValue(C->getType()));
Owen Andersonbaf3c402009-07-29 18:55:55 +00002072}
2073
2074Constant* ConstantExpr::getAdd(Constant* C1, Constant* C2) {
2075 return get(Instruction::Add, C1, C2);
2076}
2077
2078Constant* ConstantExpr::getFAdd(Constant* C1, Constant* C2) {
2079 return get(Instruction::FAdd, C1, C2);
2080}
2081
2082Constant* ConstantExpr::getSub(Constant* C1, Constant* C2) {
2083 return get(Instruction::Sub, C1, C2);
2084}
2085
2086Constant* ConstantExpr::getFSub(Constant* C1, Constant* C2) {
2087 return get(Instruction::FSub, C1, C2);
2088}
2089
2090Constant* ConstantExpr::getMul(Constant* C1, Constant* C2) {
2091 return get(Instruction::Mul, C1, C2);
2092}
2093
2094Constant* ConstantExpr::getFMul(Constant* C1, Constant* C2) {
2095 return get(Instruction::FMul, C1, C2);
2096}
2097
2098Constant* ConstantExpr::getUDiv(Constant* C1, Constant* C2) {
2099 return get(Instruction::UDiv, C1, C2);
2100}
2101
2102Constant* ConstantExpr::getSDiv(Constant* C1, Constant* C2) {
2103 return get(Instruction::SDiv, C1, C2);
2104}
2105
2106Constant* ConstantExpr::getFDiv(Constant* C1, Constant* C2) {
2107 return get(Instruction::FDiv, C1, C2);
2108}
2109
2110Constant* ConstantExpr::getURem(Constant* C1, Constant* C2) {
2111 return get(Instruction::URem, C1, C2);
2112}
2113
2114Constant* ConstantExpr::getSRem(Constant* C1, Constant* C2) {
2115 return get(Instruction::SRem, C1, C2);
2116}
2117
2118Constant* ConstantExpr::getFRem(Constant* C1, Constant* C2) {
2119 return get(Instruction::FRem, C1, C2);
2120}
2121
2122Constant* ConstantExpr::getAnd(Constant* C1, Constant* C2) {
2123 return get(Instruction::And, C1, C2);
2124}
2125
2126Constant* ConstantExpr::getOr(Constant* C1, Constant* C2) {
2127 return get(Instruction::Or, C1, C2);
2128}
2129
2130Constant* ConstantExpr::getXor(Constant* C1, Constant* C2) {
2131 return get(Instruction::Xor, C1, C2);
2132}
2133
2134Constant* ConstantExpr::getShl(Constant* C1, Constant* C2) {
2135 return get(Instruction::Shl, C1, C2);
2136}
2137
2138Constant* ConstantExpr::getLShr(Constant* C1, Constant* C2) {
2139 return get(Instruction::LShr, C1, C2);
2140}
2141
2142Constant* ConstantExpr::getAShr(Constant* C1, Constant* C2) {
2143 return get(Instruction::AShr, C1, C2);
2144}
2145
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00002146// destroyConstant - Remove the constant from the constant table...
2147//
Owen Anderson04fb7c32009-06-20 00:24:58 +00002148void ConstantExpr::destroyConstant() {
2149 // Implicitly locked.
2150 ExprConstants->remove(this);
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00002151 destroyConstantImpl();
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00002152}
2153
Chris Lattnerc188eeb2002-07-30 18:54:25 +00002154const char *ConstantExpr::getOpcodeName() const {
2155 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00002156}
Reid Spencer1c9c8e62004-07-17 23:48:33 +00002157
Chris Lattner5cbade92005-10-03 21:58:36 +00002158//===----------------------------------------------------------------------===//
2159// replaceUsesOfWithOnConstant implementations
2160
Chris Lattner54984052007-08-21 00:55:23 +00002161/// replaceUsesOfWithOnConstant - Update this constant array to change uses of
2162/// 'From' to be uses of 'To'. This must update the uniquing data structures
2163/// etc.
2164///
2165/// Note that we intentionally replace all uses of From with To here. Consider
2166/// a large array that uses 'From' 1000 times. By handling this case all here,
2167/// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that
2168/// single invocation handles all 1000 uses. Handling them one at a time would
2169/// work, but would be really slow because it would have to unique each updated
2170/// array instance.
Owen Anderson1fd70962009-07-28 18:32:17 +00002171
2172static std::vector<Constant*> getValType(ConstantArray *CA) {
2173 std::vector<Constant*> Elements;
2174 Elements.reserve(CA->getNumOperands());
2175 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
2176 Elements.push_back(cast<Constant>(CA->getOperand(i)));
2177 return Elements;
2178}
2179
2180
Chris Lattner5cbade92005-10-03 21:58:36 +00002181void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002182 Use *U) {
Owen Anderson1fd70962009-07-28 18:32:17 +00002183 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2184 Constant *ToC = cast<Constant>(To);
2185
2186 LLVMContext &Context = getType()->getContext();
2187 LLVMContextImpl *pImpl = Context.pImpl;
2188
2189 std::pair<LLVMContextImpl::ArrayConstantsTy::MapKey, Constant*> Lookup;
2190 Lookup.first.first = getType();
2191 Lookup.second = this;
2192
2193 std::vector<Constant*> &Values = Lookup.first.second;
2194 Values.reserve(getNumOperands()); // Build replacement array.
2195
2196 // Fill values with the modified operands of the constant array. Also,
2197 // compute whether this turns into an all-zeros array.
2198 bool isAllZeros = false;
2199 unsigned NumUpdated = 0;
2200 if (!ToC->isNullValue()) {
2201 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2202 Constant *Val = cast<Constant>(O->get());
2203 if (Val == From) {
2204 Val = ToC;
2205 ++NumUpdated;
2206 }
2207 Values.push_back(Val);
2208 }
2209 } else {
2210 isAllZeros = true;
2211 for (Use *O = OperandList, *E = OperandList+getNumOperands();O != E; ++O) {
2212 Constant *Val = cast<Constant>(O->get());
2213 if (Val == From) {
2214 Val = ToC;
2215 ++NumUpdated;
2216 }
2217 Values.push_back(Val);
2218 if (isAllZeros) isAllZeros = Val->isNullValue();
2219 }
2220 }
2221
2222 Constant *Replacement = 0;
2223 if (isAllZeros) {
Owen Anderson9e9a0d52009-07-30 23:03:37 +00002224 Replacement = ConstantAggregateZero::get(getType());
Owen Anderson1fd70962009-07-28 18:32:17 +00002225 } else {
2226 // Check to see if we have this array type already.
2227 sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
2228 bool Exists;
2229 LLVMContextImpl::ArrayConstantsTy::MapTy::iterator I =
2230 pImpl->ArrayConstants.InsertOrGetItem(Lookup, Exists);
2231
2232 if (Exists) {
2233 Replacement = I->second;
2234 } else {
2235 // Okay, the new shape doesn't exist in the system yet. Instead of
2236 // creating a new constant array, inserting it, replaceallusesof'ing the
2237 // old with the new, then deleting the old... just update the current one
2238 // in place!
2239 pImpl->ArrayConstants.MoveConstantToNewSlot(this, I);
2240
2241 // Update to the new value. Optimize for the case when we have a single
2242 // operand that we're changing, but handle bulk updates efficiently.
2243 if (NumUpdated == 1) {
2244 unsigned OperandToUpdate = U - OperandList;
2245 assert(getOperand(OperandToUpdate) == From &&
2246 "ReplaceAllUsesWith broken!");
2247 setOperand(OperandToUpdate, ToC);
2248 } else {
2249 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2250 if (getOperand(i) == From)
2251 setOperand(i, ToC);
2252 }
2253 return;
2254 }
2255 }
Chris Lattnercea141f2005-10-03 22:51:37 +00002256
2257 // Otherwise, I do need to replace this with an existing value.
Chris Lattner5cbade92005-10-03 21:58:36 +00002258 assert(Replacement != this && "I didn't contain From!");
2259
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002260 // Everyone using this now uses the replacement.
2261 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattner5cbade92005-10-03 21:58:36 +00002262
2263 // Delete the old constant!
2264 destroyConstant();
2265}
2266
Owen Anderson8fa33382009-07-27 22:29:26 +00002267static std::vector<Constant*> getValType(ConstantStruct *CS) {
2268 std::vector<Constant*> Elements;
2269 Elements.reserve(CS->getNumOperands());
2270 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
2271 Elements.push_back(cast<Constant>(CS->getOperand(i)));
2272 return Elements;
2273}
2274
Chris Lattner5cbade92005-10-03 21:58:36 +00002275void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002276 Use *U) {
Owen Anderson8fa33382009-07-27 22:29:26 +00002277 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2278 Constant *ToC = cast<Constant>(To);
2279
2280 unsigned OperandToUpdate = U-OperandList;
2281 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
2282
2283 std::pair<LLVMContextImpl::StructConstantsTy::MapKey, Constant*> Lookup;
2284 Lookup.first.first = getType();
2285 Lookup.second = this;
2286 std::vector<Constant*> &Values = Lookup.first.second;
2287 Values.reserve(getNumOperands()); // Build replacement struct.
2288
2289
2290 // Fill values with the modified operands of the constant struct. Also,
2291 // compute whether this turns into an all-zeros struct.
2292 bool isAllZeros = false;
2293 if (!ToC->isNullValue()) {
2294 for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O)
2295 Values.push_back(cast<Constant>(O->get()));
2296 } else {
2297 isAllZeros = true;
2298 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2299 Constant *Val = cast<Constant>(O->get());
2300 Values.push_back(Val);
2301 if (isAllZeros) isAllZeros = Val->isNullValue();
2302 }
2303 }
2304 Values[OperandToUpdate] = ToC;
2305
2306 LLVMContext &Context = getType()->getContext();
2307 LLVMContextImpl *pImpl = Context.pImpl;
2308
2309 Constant *Replacement = 0;
2310 if (isAllZeros) {
Owen Anderson9e9a0d52009-07-30 23:03:37 +00002311 Replacement = ConstantAggregateZero::get(getType());
Owen Anderson8fa33382009-07-27 22:29:26 +00002312 } else {
2313 // Check to see if we have this array type already.
2314 sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
2315 bool Exists;
2316 LLVMContextImpl::StructConstantsTy::MapTy::iterator I =
2317 pImpl->StructConstants.InsertOrGetItem(Lookup, Exists);
2318
2319 if (Exists) {
2320 Replacement = I->second;
2321 } else {
2322 // Okay, the new shape doesn't exist in the system yet. Instead of
2323 // creating a new constant struct, inserting it, replaceallusesof'ing the
2324 // old with the new, then deleting the old... just update the current one
2325 // in place!
2326 pImpl->StructConstants.MoveConstantToNewSlot(this, I);
2327
2328 // Update to the new value.
2329 setOperand(OperandToUpdate, ToC);
2330 return;
2331 }
2332 }
2333
2334 assert(Replacement != this && "I didn't contain From!");
Chris Lattner5cbade92005-10-03 21:58:36 +00002335
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002336 // Everyone using this now uses the replacement.
2337 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattner5cbade92005-10-03 21:58:36 +00002338
2339 // Delete the old constant!
2340 destroyConstant();
2341}
2342
Owen Andersonaf7ec972009-07-28 21:19:26 +00002343static std::vector<Constant*> getValType(ConstantVector *CP) {
2344 std::vector<Constant*> Elements;
2345 Elements.reserve(CP->getNumOperands());
2346 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
2347 Elements.push_back(CP->getOperand(i));
2348 return Elements;
2349}
2350
Reid Spencer9d6565a2007-02-15 02:26:10 +00002351void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002352 Use *U) {
Chris Lattner5cbade92005-10-03 21:58:36 +00002353 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2354
2355 std::vector<Constant*> Values;
2356 Values.reserve(getNumOperands()); // Build replacement array...
2357 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2358 Constant *Val = getOperand(i);
2359 if (Val == From) Val = cast<Constant>(To);
2360 Values.push_back(Val);
2361 }
2362
Owen Andersonaf7ec972009-07-28 21:19:26 +00002363 Constant *Replacement = get(getType(), Values);
Chris Lattner5cbade92005-10-03 21:58:36 +00002364 assert(Replacement != this && "I didn't contain From!");
2365
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002366 // Everyone using this now uses the replacement.
2367 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattner5cbade92005-10-03 21:58:36 +00002368
2369 // Delete the old constant!
2370 destroyConstant();
2371}
2372
2373void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002374 Use *U) {
Chris Lattner5cbade92005-10-03 21:58:36 +00002375 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2376 Constant *To = cast<Constant>(ToV);
2377
2378 Constant *Replacement = 0;
2379 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerf9021ff2007-02-19 20:01:23 +00002380 SmallVector<Constant*, 8> Indices;
Chris Lattner5cbade92005-10-03 21:58:36 +00002381 Constant *Pointer = getOperand(0);
2382 Indices.reserve(getNumOperands()-1);
2383 if (Pointer == From) Pointer = To;
2384
2385 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2386 Constant *Val = getOperand(i);
2387 if (Val == From) Val = To;
2388 Indices.push_back(Val);
2389 }
Chris Lattnerf9021ff2007-02-19 20:01:23 +00002390 Replacement = ConstantExpr::getGetElementPtr(Pointer,
2391 &Indices[0], Indices.size());
Dan Gohman041e2eb2008-05-15 19:50:34 +00002392 } else if (getOpcode() == Instruction::ExtractValue) {
Dan Gohman041e2eb2008-05-15 19:50:34 +00002393 Constant *Agg = getOperand(0);
Dan Gohman041e2eb2008-05-15 19:50:34 +00002394 if (Agg == From) Agg = To;
2395
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002396 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman041e2eb2008-05-15 19:50:34 +00002397 Replacement = ConstantExpr::getExtractValue(Agg,
2398 &Indices[0], Indices.size());
2399 } else if (getOpcode() == Instruction::InsertValue) {
Dan Gohman041e2eb2008-05-15 19:50:34 +00002400 Constant *Agg = getOperand(0);
2401 Constant *Val = getOperand(1);
Dan Gohman041e2eb2008-05-15 19:50:34 +00002402 if (Agg == From) Agg = To;
2403 if (Val == From) Val = To;
2404
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002405 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman041e2eb2008-05-15 19:50:34 +00002406 Replacement = ConstantExpr::getInsertValue(Agg, Val,
2407 &Indices[0], Indices.size());
Reid Spencer3da59db2006-11-27 01:05:10 +00002408 } else if (isCast()) {
Chris Lattner5cbade92005-10-03 21:58:36 +00002409 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer3da59db2006-11-27 01:05:10 +00002410 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattner5cbade92005-10-03 21:58:36 +00002411 } else if (getOpcode() == Instruction::Select) {
2412 Constant *C1 = getOperand(0);
2413 Constant *C2 = getOperand(1);
2414 Constant *C3 = getOperand(2);
2415 if (C1 == From) C1 = To;
2416 if (C2 == From) C2 = To;
2417 if (C3 == From) C3 = To;
2418 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00002419 } else if (getOpcode() == Instruction::ExtractElement) {
2420 Constant *C1 = getOperand(0);
2421 Constant *C2 = getOperand(1);
2422 if (C1 == From) C1 = To;
2423 if (C2 == From) C2 = To;
2424 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattner42b55802006-04-08 05:09:48 +00002425 } else if (getOpcode() == Instruction::InsertElement) {
2426 Constant *C1 = getOperand(0);
2427 Constant *C2 = getOperand(1);
2428 Constant *C3 = getOperand(1);
2429 if (C1 == From) C1 = To;
2430 if (C2 == From) C2 = To;
2431 if (C3 == From) C3 = To;
2432 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2433 } else if (getOpcode() == Instruction::ShuffleVector) {
2434 Constant *C1 = getOperand(0);
2435 Constant *C2 = getOperand(1);
2436 Constant *C3 = getOperand(2);
2437 if (C1 == From) C1 = To;
2438 if (C2 == From) C2 = To;
2439 if (C3 == From) C3 = To;
2440 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spencer077d0eb2006-12-04 05:19:50 +00002441 } else if (isCompare()) {
2442 Constant *C1 = getOperand(0);
2443 Constant *C2 = getOperand(1);
2444 if (C1 == From) C1 = To;
2445 if (C2 == From) C2 = To;
2446 if (getOpcode() == Instruction::ICmp)
2447 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
Chris Lattnerbbedb0e2008-07-14 05:17:31 +00002448 else {
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002449 assert(getOpcode() == Instruction::FCmp);
2450 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnerbbedb0e2008-07-14 05:17:31 +00002451 }
Chris Lattner5cbade92005-10-03 21:58:36 +00002452 } else if (getNumOperands() == 2) {
2453 Constant *C1 = getOperand(0);
2454 Constant *C2 = getOperand(1);
2455 if (C1 == From) C1 = To;
2456 if (C2 == From) C2 = To;
2457 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
2458 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00002459 llvm_unreachable("Unknown ConstantExpr type!");
Chris Lattner5cbade92005-10-03 21:58:36 +00002460 return;
2461 }
2462
2463 assert(Replacement != this && "I didn't contain From!");
2464
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002465 // Everyone using this now uses the replacement.
2466 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattner5cbade92005-10-03 21:58:36 +00002467
2468 // Delete the old constant!
2469 destroyConstant();
Matthijs Kooijman10b9de62008-07-03 07:46:41 +00002470}
Nick Lewycky21cc4462009-04-04 07:22:01 +00002471