blob: b1c942c214f4046a99e482aef3199f7f9d070aee [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 Lattnerd0ec2352009-11-01 03:03:03 +000010// This file implements the Constant* classes.
Chris Lattner00950542001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner31bcdb82002-04-28 19:55:58 +000014#include "llvm/Constants.h"
Chris Lattner37f077a2009-08-23 04:02:03 +000015#include "LLVMContextImpl.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"
Chris Lattner37f077a2009-08-23 04:02:03 +000030#include "llvm/Support/raw_ostream.h"
Dan Gohmane6992f72009-09-10 23:37:55 +000031#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattner6b6f6ba2007-02-20 06:39:57 +000032#include "llvm/ADT/DenseMap.h"
Chris Lattnerf9021ff2007-02-19 20:01:23 +000033#include "llvm/ADT/SmallVector.h"
Chris Lattner00950542001-06-06 20:29:01 +000034#include <algorithm>
Reid Spenceref9b9a72007-02-05 20:47:22 +000035#include <map>
Chris Lattner31f84992003-11-21 20:23:48 +000036using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000037
Chris Lattner00950542001-06-06 20:29:01 +000038//===----------------------------------------------------------------------===//
Chris Lattnere9bb2df2001-12-03 22:26:30 +000039// Constant Class
Chris Lattner00950542001-06-06 20:29:01 +000040//===----------------------------------------------------------------------===//
41
Owen Andersona7235ea2009-07-31 20:28:14 +000042// Constructor to create a '0' constant of arbitrary type...
43static const uint64_t zero[2] = {0, 0};
Chris Lattner38211762009-10-30 22:33:29 +000044Constant *Constant::getNullValue(const Type *Ty) {
Owen Andersona7235ea2009-07-31 20:28:14 +000045 switch (Ty->getTypeID()) {
46 case Type::IntegerTyID:
47 return ConstantInt::get(Ty, 0);
48 case Type::FloatTyID:
49 return ConstantFP::get(Ty->getContext(), APFloat(APInt(32, 0)));
50 case Type::DoubleTyID:
51 return ConstantFP::get(Ty->getContext(), APFloat(APInt(64, 0)));
52 case Type::X86_FP80TyID:
53 return ConstantFP::get(Ty->getContext(), APFloat(APInt(80, 2, zero)));
54 case Type::FP128TyID:
55 return ConstantFP::get(Ty->getContext(),
56 APFloat(APInt(128, 2, zero), true));
57 case Type::PPC_FP128TyID:
58 return ConstantFP::get(Ty->getContext(), APFloat(APInt(128, 2, zero)));
59 case Type::PointerTyID:
60 return ConstantPointerNull::get(cast<PointerType>(Ty));
61 case Type::StructTyID:
62 case Type::ArrayTyID:
63 case Type::VectorTyID:
64 return ConstantAggregateZero::get(Ty);
65 default:
66 // Function, Label, or Opaque type?
67 assert(!"Cannot create a null constant of that type!");
68 return 0;
69 }
70}
71
Chris Lattner38211762009-10-30 22:33:29 +000072Constant* Constant::getIntegerValue(const Type *Ty, const APInt &V) {
Dan Gohman43ee5f72009-08-03 22:07:33 +000073 const Type *ScalarTy = Ty->getScalarType();
74
75 // Create the base integer constant.
76 Constant *C = ConstantInt::get(Ty->getContext(), V);
77
78 // Convert an integer to a pointer, if necessary.
79 if (const PointerType *PTy = dyn_cast<PointerType>(ScalarTy))
80 C = ConstantExpr::getIntToPtr(C, PTy);
81
82 // Broadcast a scalar to a vector, if necessary.
83 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
84 C = ConstantVector::get(std::vector<Constant *>(VTy->getNumElements(), C));
85
86 return C;
87}
88
Chris Lattner38211762009-10-30 22:33:29 +000089Constant* Constant::getAllOnesValue(const Type *Ty) {
90 if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty))
Owen Andersona7235ea2009-07-31 20:28:14 +000091 return ConstantInt::get(Ty->getContext(),
92 APInt::getAllOnesValue(ITy->getBitWidth()));
93
94 std::vector<Constant*> Elts;
Chris Lattner38211762009-10-30 22:33:29 +000095 const VectorType *VTy = cast<VectorType>(Ty);
Owen Andersona7235ea2009-07-31 20:28:14 +000096 Elts.resize(VTy->getNumElements(), getAllOnesValue(VTy->getElementType()));
97 assert(Elts[0] && "Not a vector integer type!");
98 return cast<ConstantVector>(ConstantVector::get(Elts));
99}
100
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000101void Constant::destroyConstantImpl() {
102 // When a Constant is destroyed, there may be lingering
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000103 // references to the constant by other constants in the constant pool. These
Misha Brukmanef6a6a62003-08-21 22:14:26 +0000104 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000105 // but they don't know that. Because we only find out when the CPV is
106 // deleted, we must now notify all of our users (that should only be
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000107 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000108 //
109 while (!use_empty()) {
110 Value *V = use_back();
111#ifndef NDEBUG // Only in -g mode...
Chris Lattner37f077a2009-08-23 04:02:03 +0000112 if (!isa<Constant>(V)) {
David Greened2e63b72010-01-05 01:29:19 +0000113 dbgs() << "While deleting: " << *this
Chris Lattner37f077a2009-08-23 04:02:03 +0000114 << "\n\nUse still stuck around after Def is destroyed: "
115 << *V << "\n\n";
116 }
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000117#endif
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000118 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1c9c8e62004-07-17 23:48:33 +0000119 Constant *CV = cast<Constant>(V);
120 CV->destroyConstant();
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000121
122 // The constant should remove itself from our use list...
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000123 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000124 }
125
126 // Value has no outstanding references it is safe to delete it now...
127 delete this;
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000128}
Chris Lattner00950542001-06-06 20:29:01 +0000129
Chris Lattner35b89fa2006-10-20 00:27:06 +0000130/// canTrap - Return true if evaluation of this constant could trap. This is
131/// true for things like constant expressions that could divide by zero.
132bool Constant::canTrap() const {
133 assert(getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
134 // The only thing that could possibly trap are constant exprs.
135 const ConstantExpr *CE = dyn_cast<ConstantExpr>(this);
136 if (!CE) return false;
137
138 // ConstantExpr traps if any operands can trap.
139 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Chris Lattner0eeb9132009-10-28 05:14:34 +0000140 if (CE->getOperand(i)->canTrap())
Chris Lattner35b89fa2006-10-20 00:27:06 +0000141 return true;
142
143 // Otherwise, only specific operations can trap.
144 switch (CE->getOpcode()) {
145 default:
146 return false;
Reid Spencer1628cec2006-10-26 06:15:43 +0000147 case Instruction::UDiv:
148 case Instruction::SDiv:
149 case Instruction::FDiv:
Reid Spencer0a783f72006-11-02 01:53:59 +0000150 case Instruction::URem:
151 case Instruction::SRem:
152 case Instruction::FRem:
Chris Lattner35b89fa2006-10-20 00:27:06 +0000153 // Div and rem can trap if the RHS is not known to be non-zero.
Chris Lattner0eeb9132009-10-28 05:14:34 +0000154 if (!isa<ConstantInt>(CE->getOperand(1)) ||CE->getOperand(1)->isNullValue())
Chris Lattner35b89fa2006-10-20 00:27:06 +0000155 return true;
156 return false;
157 }
158}
159
Chris Lattner4a7642e2009-11-01 18:11:50 +0000160/// isConstantUsed - Return true if the constant has users other than constant
161/// exprs and other dangling things.
162bool Constant::isConstantUsed() const {
163 for (use_const_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
164 const Constant *UC = dyn_cast<Constant>(*UI);
165 if (UC == 0 || isa<GlobalValue>(UC))
166 return true;
167
168 if (UC->isConstantUsed())
169 return true;
170 }
171 return false;
172}
173
174
Chris Lattner7cf12c72009-07-22 00:05:44 +0000175
176/// getRelocationInfo - This method classifies the entry according to
177/// whether or not it may generate a relocation entry. This must be
178/// conservative, so if it might codegen to a relocatable entry, it should say
179/// so. The return values are:
180///
Chris Lattner083a1e02009-07-24 03:27:21 +0000181/// NoRelocation: This constant pool entry is guaranteed to never have a
182/// relocation applied to it (because it holds a simple constant like
183/// '4').
184/// LocalRelocation: This entry has relocations, but the entries are
185/// guaranteed to be resolvable by the static linker, so the dynamic
186/// linker will never see them.
187/// GlobalRelocations: This entry may have arbitrary relocations.
Chris Lattner7cf12c72009-07-22 00:05:44 +0000188///
189/// FIXME: This really should not be in VMCore.
Chris Lattner083a1e02009-07-24 03:27:21 +0000190Constant::PossibleRelocationsTy Constant::getRelocationInfo() const {
191 if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
Chris Lattner7cf12c72009-07-22 00:05:44 +0000192 if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
Chris Lattner083a1e02009-07-24 03:27:21 +0000193 return LocalRelocation; // Local to this file/library.
194 return GlobalRelocations; // Global reference.
Anton Korobeynikovab267a22009-03-29 17:13:18 +0000195 }
Chris Lattner7cf12c72009-07-22 00:05:44 +0000196
Chris Lattner5d81bef2009-10-28 04:12:16 +0000197 if (const BlockAddress *BA = dyn_cast<BlockAddress>(this))
198 return BA->getFunction()->getRelocationInfo();
199
Chris Lattner5099b312010-01-03 18:09:40 +0000200 // While raw uses of blockaddress need to be relocated, differences between
201 // two of them don't when they are for labels in the same function. This is a
202 // common idiom when creating a table for the indirect goto extension, so we
203 // handle it efficiently here.
204 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(this))
205 if (CE->getOpcode() == Instruction::Sub) {
206 ConstantExpr *LHS = dyn_cast<ConstantExpr>(CE->getOperand(0));
207 ConstantExpr *RHS = dyn_cast<ConstantExpr>(CE->getOperand(1));
208 if (LHS && RHS &&
209 LHS->getOpcode() == Instruction::PtrToInt &&
210 RHS->getOpcode() == Instruction::PtrToInt &&
211 isa<BlockAddress>(LHS->getOperand(0)) &&
212 isa<BlockAddress>(RHS->getOperand(0)) &&
213 cast<BlockAddress>(LHS->getOperand(0))->getFunction() ==
214 cast<BlockAddress>(RHS->getOperand(0))->getFunction())
215 return NoRelocation;
216 }
217
Chris Lattner083a1e02009-07-24 03:27:21 +0000218 PossibleRelocationsTy Result = NoRelocation;
Evan Chengafe15812007-03-08 00:59:12 +0000219 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Chris Lattner0eeb9132009-10-28 05:14:34 +0000220 Result = std::max(Result,
221 cast<Constant>(getOperand(i))->getRelocationInfo());
Chris Lattner7cf12c72009-07-22 00:05:44 +0000222
223 return Result;
Evan Chengafe15812007-03-08 00:59:12 +0000224}
225
Chris Lattner7cf12c72009-07-22 00:05:44 +0000226
Chris Lattner86381442008-07-10 00:28:11 +0000227/// getVectorElements - This method, which is only valid on constant of vector
228/// type, returns the elements of the vector in the specified smallvector.
Chris Lattner071aade2008-07-14 05:10:41 +0000229/// This handles breaking down a vector undef into undef elements, etc. For
230/// constant exprs and other cases we can't handle, we return an empty vector.
Owen Anderson0a5372e2009-07-13 04:09:18 +0000231void Constant::getVectorElements(LLVMContext &Context,
232 SmallVectorImpl<Constant*> &Elts) const {
Chris Lattner86381442008-07-10 00:28:11 +0000233 assert(isa<VectorType>(getType()) && "Not a vector constant!");
234
235 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) {
236 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i)
237 Elts.push_back(CV->getOperand(i));
238 return;
239 }
240
241 const VectorType *VT = cast<VectorType>(getType());
242 if (isa<ConstantAggregateZero>(this)) {
243 Elts.assign(VT->getNumElements(),
Owen Andersona7235ea2009-07-31 20:28:14 +0000244 Constant::getNullValue(VT->getElementType()));
Chris Lattner86381442008-07-10 00:28:11 +0000245 return;
246 }
247
Chris Lattner071aade2008-07-14 05:10:41 +0000248 if (isa<UndefValue>(this)) {
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000249 Elts.assign(VT->getNumElements(), UndefValue::get(VT->getElementType()));
Chris Lattner071aade2008-07-14 05:10:41 +0000250 return;
251 }
252
253 // Unknown type, must be constant expr etc.
Chris Lattner86381442008-07-10 00:28:11 +0000254}
255
256
257
Chris Lattner00950542001-06-06 20:29:01 +0000258//===----------------------------------------------------------------------===//
Chris Lattner6b6f6ba2007-02-20 06:39:57 +0000259// ConstantInt
Chris Lattner00950542001-06-06 20:29:01 +0000260//===----------------------------------------------------------------------===//
261
Reid Spencer532d0ce2007-02-26 23:54:03 +0000262ConstantInt::ConstantInt(const IntegerType *Ty, const APInt& V)
Chris Lattnereb41bdd2007-02-20 05:55:46 +0000263 : Constant(Ty, ConstantIntVal, 0, 0), Val(V) {
Reid Spencer532d0ce2007-02-26 23:54:03 +0000264 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
Chris Lattner00950542001-06-06 20:29:01 +0000265}
266
Owen Anderson5defacc2009-07-31 17:39:07 +0000267ConstantInt* ConstantInt::getTrue(LLVMContext &Context) {
268 LLVMContextImpl *pImpl = Context.pImpl;
Owen Anderson5defacc2009-07-31 17:39:07 +0000269 if (pImpl->TheTrueVal)
270 return pImpl->TheTrueVal;
271 else
Owen Anderson1d0be152009-08-13 21:58:54 +0000272 return (pImpl->TheTrueVal =
273 ConstantInt::get(IntegerType::get(Context, 1), 1));
Owen Anderson5defacc2009-07-31 17:39:07 +0000274}
275
276ConstantInt* ConstantInt::getFalse(LLVMContext &Context) {
277 LLVMContextImpl *pImpl = Context.pImpl;
Owen Anderson5defacc2009-07-31 17:39:07 +0000278 if (pImpl->TheFalseVal)
279 return pImpl->TheFalseVal;
280 else
Owen Anderson1d0be152009-08-13 21:58:54 +0000281 return (pImpl->TheFalseVal =
282 ConstantInt::get(IntegerType::get(Context, 1), 0));
Owen Anderson5defacc2009-07-31 17:39:07 +0000283}
284
285
Owen Andersoneed707b2009-07-24 23:12:02 +0000286// Get a ConstantInt from an APInt. Note that the value stored in the DenseMap
287// as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the
288// operator== and operator!= to ensure that the DenseMap doesn't attempt to
289// compare APInt's of different widths, which would violate an APInt class
290// invariant which generates an assertion.
291ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt& V) {
292 // Get the corresponding integer type for the bit width of the value.
Owen Anderson1d0be152009-08-13 21:58:54 +0000293 const IntegerType *ITy = IntegerType::get(Context, V.getBitWidth());
Owen Andersoneed707b2009-07-24 23:12:02 +0000294 // get an existing value or the insertion position
295 DenseMapAPIntKeyInfo::KeyTy Key(V, ITy);
Owen Andersoneed707b2009-07-24 23:12:02 +0000296 ConstantInt *&Slot = Context.pImpl->IntConstants[Key];
Owen Anderson59d5aac2009-10-19 20:11:52 +0000297 if (!Slot) Slot = new ConstantInt(ITy, V);
298 return Slot;
Owen Andersoneed707b2009-07-24 23:12:02 +0000299}
300
301Constant* ConstantInt::get(const Type* Ty, uint64_t V, bool isSigned) {
302 Constant *C = get(cast<IntegerType>(Ty->getScalarType()),
303 V, isSigned);
304
305 // For vectors, broadcast the value.
306 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
Owen Andersonaf7ec972009-07-28 21:19:26 +0000307 return ConstantVector::get(
Owen Andersoneed707b2009-07-24 23:12:02 +0000308 std::vector<Constant *>(VTy->getNumElements(), C));
309
310 return C;
311}
312
313ConstantInt* ConstantInt::get(const IntegerType* Ty, uint64_t V,
314 bool isSigned) {
315 return get(Ty->getContext(), APInt(Ty->getBitWidth(), V, isSigned));
316}
317
318ConstantInt* ConstantInt::getSigned(const IntegerType* Ty, int64_t V) {
319 return get(Ty, V, true);
320}
321
322Constant *ConstantInt::getSigned(const Type *Ty, int64_t V) {
323 return get(Ty, V, true);
324}
325
326Constant* ConstantInt::get(const Type* Ty, const APInt& V) {
327 ConstantInt *C = get(Ty->getContext(), V);
328 assert(C->getType() == Ty->getScalarType() &&
329 "ConstantInt type doesn't match the type implied by its value!");
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 Andersoneed707b2009-07-24 23:12:02 +0000334 std::vector<Constant *>(VTy->getNumElements(), C));
335
336 return C;
337}
338
Daniel Dunbar2928c832009-11-06 10:58:06 +0000339ConstantInt* ConstantInt::get(const IntegerType* Ty, StringRef Str,
Erick Tryzelaar0e81f662009-08-16 23:36:33 +0000340 uint8_t radix) {
341 return get(Ty->getContext(), APInt(Ty->getBitWidth(), Str, radix));
342}
343
Chris Lattner6b6f6ba2007-02-20 06:39:57 +0000344//===----------------------------------------------------------------------===//
Chris Lattner9b4ee0c2007-02-20 07:17:17 +0000345// ConstantFP
Chris Lattner6b6f6ba2007-02-20 06:39:57 +0000346//===----------------------------------------------------------------------===//
347
Rafael Espindola87d1f472009-07-15 17:40:42 +0000348static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000349 if (Ty->isFloatTy())
Rafael Espindola87d1f472009-07-15 17:40:42 +0000350 return &APFloat::IEEEsingle;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000351 if (Ty->isDoubleTy())
Rafael Espindola87d1f472009-07-15 17:40:42 +0000352 return &APFloat::IEEEdouble;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000353 if (Ty->isX86_FP80Ty())
Rafael Espindola87d1f472009-07-15 17:40:42 +0000354 return &APFloat::x87DoubleExtended;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000355 else if (Ty->isFP128Ty())
Rafael Espindola87d1f472009-07-15 17:40:42 +0000356 return &APFloat::IEEEquad;
357
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000358 assert(Ty->isPPC_FP128Ty() && "Unknown FP format");
Rafael Espindola87d1f472009-07-15 17:40:42 +0000359 return &APFloat::PPCDoubleDouble;
360}
361
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000362/// get() - This returns a constant fp for the specified value in the
363/// specified type. This should only be used for simple constant values like
364/// 2.0/1.0 etc, that are known-valid both as double and as the target format.
365Constant* ConstantFP::get(const Type* Ty, double V) {
366 LLVMContext &Context = Ty->getContext();
367
368 APFloat FV(V);
369 bool ignored;
370 FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
371 APFloat::rmNearestTiesToEven, &ignored);
372 Constant *C = get(Context, FV);
373
374 // For vectors, broadcast the value.
375 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
Owen Andersonaf7ec972009-07-28 21:19:26 +0000376 return ConstantVector::get(
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000377 std::vector<Constant *>(VTy->getNumElements(), C));
378
379 return C;
380}
381
Erick Tryzelaar0e81f662009-08-16 23:36:33 +0000382
Daniel Dunbar2928c832009-11-06 10:58:06 +0000383Constant* ConstantFP::get(const Type* Ty, StringRef Str) {
Erick Tryzelaar0e81f662009-08-16 23:36:33 +0000384 LLVMContext &Context = Ty->getContext();
385
386 APFloat FV(*TypeToFloatSemantics(Ty->getScalarType()), Str);
387 Constant *C = get(Context, FV);
388
389 // For vectors, broadcast the value.
390 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
391 return ConstantVector::get(
392 std::vector<Constant *>(VTy->getNumElements(), C));
393
394 return C;
395}
396
397
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000398ConstantFP* ConstantFP::getNegativeZero(const Type* Ty) {
399 LLVMContext &Context = Ty->getContext();
Owen Andersona7235ea2009-07-31 20:28:14 +0000400 APFloat apf = cast <ConstantFP>(Constant::getNullValue(Ty))->getValueAPF();
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000401 apf.changeSign();
402 return get(Context, apf);
403}
404
405
406Constant* ConstantFP::getZeroValueForNegation(const Type* Ty) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000407 if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
408 if (PTy->getElementType()->isFloatingPoint()) {
409 std::vector<Constant*> zeros(PTy->getNumElements(),
410 getNegativeZero(PTy->getElementType()));
Owen Andersonaf7ec972009-07-28 21:19:26 +0000411 return ConstantVector::get(PTy, zeros);
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000412 }
413
414 if (Ty->isFloatingPoint())
415 return getNegativeZero(Ty);
416
Owen Andersona7235ea2009-07-31 20:28:14 +0000417 return Constant::getNullValue(Ty);
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000418}
419
420
421// ConstantFP accessors.
422ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) {
423 DenseMapAPFloatKeyInfo::KeyTy Key(V);
424
425 LLVMContextImpl* pImpl = Context.pImpl;
426
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000427 ConstantFP *&Slot = pImpl->FPConstants[Key];
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000428
429 if (!Slot) {
Owen Anderson59d5aac2009-10-19 20:11:52 +0000430 const Type *Ty;
431 if (&V.getSemantics() == &APFloat::IEEEsingle)
432 Ty = Type::getFloatTy(Context);
433 else if (&V.getSemantics() == &APFloat::IEEEdouble)
434 Ty = Type::getDoubleTy(Context);
435 else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
436 Ty = Type::getX86_FP80Ty(Context);
437 else if (&V.getSemantics() == &APFloat::IEEEquad)
438 Ty = Type::getFP128Ty(Context);
439 else {
440 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble &&
441 "Unknown FP format");
442 Ty = Type::getPPC_FP128Ty(Context);
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000443 }
Owen Anderson59d5aac2009-10-19 20:11:52 +0000444 Slot = new ConstantFP(Ty, V);
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000445 }
446
447 return Slot;
448}
449
Dan Gohmana23643d2009-09-25 23:40:21 +0000450ConstantFP *ConstantFP::getInfinity(const Type *Ty, bool Negative) {
Dan Gohmanf344f7f2009-09-25 23:00:48 +0000451 const fltSemantics &Semantics = *TypeToFloatSemantics(Ty);
452 return ConstantFP::get(Ty->getContext(),
453 APFloat::getInf(Semantics, Negative));
454}
455
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000456ConstantFP::ConstantFP(const Type *Ty, const APFloat& V)
457 : Constant(Ty, ConstantFPVal, 0, 0), Val(V) {
Chris Lattner288e78f2008-04-09 06:38:30 +0000458 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
459 "FP type Mismatch");
Chris Lattner00950542001-06-06 20:29:01 +0000460}
461
Chris Lattner9b4ee0c2007-02-20 07:17:17 +0000462bool ConstantFP::isNullValue() const {
Dale Johannesen343e7702007-08-24 00:56:33 +0000463 return Val.isZero() && !Val.isNegative();
Chris Lattner9b4ee0c2007-02-20 07:17:17 +0000464}
465
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000466bool ConstantFP::isExactlyValue(const APFloat& V) const {
467 return Val.bitwiseIsEqual(V);
Chris Lattner9b4ee0c2007-02-20 07:17:17 +0000468}
469
Chris Lattner9b4ee0c2007-02-20 07:17:17 +0000470//===----------------------------------------------------------------------===//
471// ConstantXXX Classes
472//===----------------------------------------------------------------------===//
473
474
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000475ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnere4671472005-01-29 00:34:39 +0000476 const std::vector<Constant*> &V)
Gabor Greifefe65362008-05-10 08:32:32 +0000477 : Constant(T, ConstantArrayVal,
478 OperandTraits<ConstantArray>::op_end(this) - V.size(),
479 V.size()) {
Alkis Evlogimenose0de1d62004-09-15 02:32:15 +0000480 assert(V.size() == T->getNumElements() &&
481 "Invalid initializer vector for constant array");
Chris Lattnere4671472005-01-29 00:34:39 +0000482 Use *OL = OperandList;
Chris Lattnerdfdd6c52005-10-03 21:56:24 +0000483 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
484 I != E; ++I, ++OL) {
Chris Lattner71abaab2005-10-07 05:23:36 +0000485 Constant *C = *I;
Nick Lewyckyb13105f2009-10-03 19:30:43 +0000486 assert(C->getType() == T->getElementType() &&
Alkis Evlogimenoscad90ad2004-09-10 04:16:59 +0000487 "Initializer for array element doesn't match array element type!");
Gabor Greif6c80c382008-05-26 21:33:52 +0000488 *OL = C;
Chris Lattner00950542001-06-06 20:29:01 +0000489 }
490}
491
Owen Anderson1fd70962009-07-28 18:32:17 +0000492Constant *ConstantArray::get(const ArrayType *Ty,
493 const std::vector<Constant*> &V) {
Jeffrey Yasskin1fb613c2009-09-30 21:08:08 +0000494 for (unsigned i = 0, e = V.size(); i != e; ++i) {
495 assert(V[i]->getType() == Ty->getElementType() &&
496 "Wrong type in array element initializer");
497 }
Owen Anderson1fd70962009-07-28 18:32:17 +0000498 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
499 // If this is an all-zero array, return a ConstantAggregateZero object
500 if (!V.empty()) {
501 Constant *C = V[0];
Chris Lattner83738a22009-12-30 20:25:09 +0000502 if (!C->isNullValue())
Owen Anderson1fd70962009-07-28 18:32:17 +0000503 return pImpl->ArrayConstants.getOrCreate(Ty, V);
Chris Lattner83738a22009-12-30 20:25:09 +0000504
Owen Anderson1fd70962009-07-28 18:32:17 +0000505 for (unsigned i = 1, e = V.size(); i != e; ++i)
Chris Lattner83738a22009-12-30 20:25:09 +0000506 if (V[i] != C)
Owen Anderson1fd70962009-07-28 18:32:17 +0000507 return pImpl->ArrayConstants.getOrCreate(Ty, V);
Owen Anderson1fd70962009-07-28 18:32:17 +0000508 }
509
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000510 return ConstantAggregateZero::get(Ty);
Owen Anderson1fd70962009-07-28 18:32:17 +0000511}
512
513
514Constant* ConstantArray::get(const ArrayType* T, Constant* const* Vals,
515 unsigned NumVals) {
516 // FIXME: make this the primary ctor method.
517 return get(T, std::vector<Constant*>(Vals, Vals+NumVals));
518}
519
520/// ConstantArray::get(const string&) - Return an array that is initialized to
521/// contain the specified string. If length is zero then a null terminator is
522/// added to the specified string so that it may be used in a natural way.
523/// Otherwise, the length parameter specifies how much of the string to use
524/// and it won't be null terminated.
525///
Daniel Dunbar2928c832009-11-06 10:58:06 +0000526Constant* ConstantArray::get(LLVMContext &Context, StringRef Str,
Owen Anderson1d0be152009-08-13 21:58:54 +0000527 bool AddNull) {
Owen Anderson1fd70962009-07-28 18:32:17 +0000528 std::vector<Constant*> ElementVals;
529 for (unsigned i = 0; i < Str.size(); ++i)
Owen Anderson1d0be152009-08-13 21:58:54 +0000530 ElementVals.push_back(ConstantInt::get(Type::getInt8Ty(Context), Str[i]));
Owen Anderson1fd70962009-07-28 18:32:17 +0000531
532 // Add a null terminator to the string...
533 if (AddNull) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000534 ElementVals.push_back(ConstantInt::get(Type::getInt8Ty(Context), 0));
Owen Anderson1fd70962009-07-28 18:32:17 +0000535 }
536
Owen Anderson1d0be152009-08-13 21:58:54 +0000537 ArrayType *ATy = ArrayType::get(Type::getInt8Ty(Context), ElementVals.size());
Owen Anderson1fd70962009-07-28 18:32:17 +0000538 return get(ATy, ElementVals);
539}
540
541
Chris Lattnere4671472005-01-29 00:34:39 +0000542
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000543ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnere4671472005-01-29 00:34:39 +0000544 const std::vector<Constant*> &V)
Gabor Greifefe65362008-05-10 08:32:32 +0000545 : Constant(T, ConstantStructVal,
546 OperandTraits<ConstantStruct>::op_end(this) - V.size(),
547 V.size()) {
Chris Lattnerd21cd802004-02-09 04:37:31 +0000548 assert(V.size() == T->getNumElements() &&
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000549 "Invalid initializer vector for constant structure");
Chris Lattnere4671472005-01-29 00:34:39 +0000550 Use *OL = OperandList;
Chris Lattnerdfdd6c52005-10-03 21:56:24 +0000551 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
552 I != E; ++I, ++OL) {
Chris Lattner71abaab2005-10-07 05:23:36 +0000553 Constant *C = *I;
Nick Lewyckyb13105f2009-10-03 19:30:43 +0000554 assert(C->getType() == T->getElementType(I-V.begin()) &&
Chris Lattnerb8438892003-06-02 17:42:47 +0000555 "Initializer for struct element doesn't match struct element type!");
Gabor Greif6c80c382008-05-26 21:33:52 +0000556 *OL = C;
Chris Lattner00950542001-06-06 20:29:01 +0000557 }
558}
559
Owen Anderson8fa33382009-07-27 22:29:26 +0000560// ConstantStruct accessors.
561Constant* ConstantStruct::get(const StructType* T,
562 const std::vector<Constant*>& V) {
563 LLVMContextImpl* pImpl = T->getContext().pImpl;
564
565 // Create a ConstantAggregateZero value if all elements are zeros...
566 for (unsigned i = 0, e = V.size(); i != e; ++i)
567 if (!V[i]->isNullValue())
Owen Anderson8fa33382009-07-27 22:29:26 +0000568 return pImpl->StructConstants.getOrCreate(T, V);
569
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000570 return ConstantAggregateZero::get(T);
Owen Anderson8fa33382009-07-27 22:29:26 +0000571}
572
Owen Andersond7f2a6c2009-08-05 23:16:16 +0000573Constant* ConstantStruct::get(LLVMContext &Context,
574 const std::vector<Constant*>& V, bool packed) {
Owen Anderson8fa33382009-07-27 22:29:26 +0000575 std::vector<const Type*> StructEls;
576 StructEls.reserve(V.size());
577 for (unsigned i = 0, e = V.size(); i != e; ++i)
578 StructEls.push_back(V[i]->getType());
Owen Andersond7f2a6c2009-08-05 23:16:16 +0000579 return get(StructType::get(Context, StructEls, packed), V);
Owen Anderson8fa33382009-07-27 22:29:26 +0000580}
581
Owen Andersond7f2a6c2009-08-05 23:16:16 +0000582Constant* ConstantStruct::get(LLVMContext &Context,
583 Constant* const *Vals, unsigned NumVals,
Owen Anderson8fa33382009-07-27 22:29:26 +0000584 bool Packed) {
585 // FIXME: make this the primary ctor method.
Owen Andersond7f2a6c2009-08-05 23:16:16 +0000586 return get(Context, std::vector<Constant*>(Vals, Vals+NumVals), Packed);
Owen Anderson8fa33382009-07-27 22:29:26 +0000587}
Chris Lattnere4671472005-01-29 00:34:39 +0000588
Reid Spencer9d6565a2007-02-15 02:26:10 +0000589ConstantVector::ConstantVector(const VectorType *T,
Chris Lattnere4671472005-01-29 00:34:39 +0000590 const std::vector<Constant*> &V)
Gabor Greifefe65362008-05-10 08:32:32 +0000591 : Constant(T, ConstantVectorVal,
592 OperandTraits<ConstantVector>::op_end(this) - V.size(),
593 V.size()) {
Chris Lattnere4671472005-01-29 00:34:39 +0000594 Use *OL = OperandList;
Chris Lattnerdfdd6c52005-10-03 21:56:24 +0000595 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
596 I != E; ++I, ++OL) {
Chris Lattner71abaab2005-10-07 05:23:36 +0000597 Constant *C = *I;
Nick Lewyckyb13105f2009-10-03 19:30:43 +0000598 assert(C->getType() == T->getElementType() &&
Dan Gohmanfa73ea22007-05-24 14:36:04 +0000599 "Initializer for vector element doesn't match vector element type!");
Gabor Greif6c80c382008-05-26 21:33:52 +0000600 *OL = C;
Brian Gaeke715c90b2004-08-20 06:00:58 +0000601 }
602}
603
Owen Andersonaf7ec972009-07-28 21:19:26 +0000604// ConstantVector accessors.
605Constant* ConstantVector::get(const VectorType* T,
606 const std::vector<Constant*>& V) {
607 assert(!V.empty() && "Vectors can't be empty");
608 LLVMContext &Context = T->getContext();
609 LLVMContextImpl *pImpl = Context.pImpl;
610
611 // If this is an all-undef or alll-zero vector, return a
612 // ConstantAggregateZero or UndefValue.
613 Constant *C = V[0];
614 bool isZero = C->isNullValue();
615 bool isUndef = isa<UndefValue>(C);
616
617 if (isZero || isUndef) {
618 for (unsigned i = 1, e = V.size(); i != e; ++i)
619 if (V[i] != C) {
620 isZero = isUndef = false;
621 break;
622 }
623 }
624
625 if (isZero)
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000626 return ConstantAggregateZero::get(T);
Owen Andersonaf7ec972009-07-28 21:19:26 +0000627 if (isUndef)
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000628 return UndefValue::get(T);
Owen Andersonaf7ec972009-07-28 21:19:26 +0000629
Owen Andersonaf7ec972009-07-28 21:19:26 +0000630 return pImpl->VectorConstants.getOrCreate(T, V);
631}
632
633Constant* ConstantVector::get(const std::vector<Constant*>& V) {
634 assert(!V.empty() && "Cannot infer type if V is empty");
635 return get(VectorType::get(V.front()->getType(),V.size()), V);
636}
637
638Constant* ConstantVector::get(Constant* const* Vals, unsigned NumVals) {
639 // FIXME: make this the primary ctor method.
640 return get(std::vector<Constant*>(Vals, Vals+NumVals));
641}
642
Dan Gohmanbdc46c62009-12-18 02:58:50 +0000643Constant* ConstantExpr::getNSWNeg(Constant* C) {
644 assert(C->getType()->isIntOrIntVector() &&
645 "Cannot NEG a nonintegral value!");
646 return getNSWSub(ConstantFP::getZeroValueForNegation(C->getType()), C);
647}
648
Dan Gohman6e7ad952009-09-03 23:34:49 +0000649Constant* ConstantExpr::getNSWAdd(Constant* C1, Constant* C2) {
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000650 return getTy(C1->getType(), Instruction::Add, C1, C2,
651 OverflowingBinaryOperator::NoSignedWrap);
Dan Gohman6e7ad952009-09-03 23:34:49 +0000652}
653
Duncan Sands3548ea82009-09-26 15:35:35 +0000654Constant* ConstantExpr::getNSWSub(Constant* C1, Constant* C2) {
655 return getTy(C1->getType(), Instruction::Sub, C1, C2,
656 OverflowingBinaryOperator::NoSignedWrap);
657}
658
Dan Gohman41198482009-12-18 03:10:26 +0000659Constant* ConstantExpr::getNSWMul(Constant* C1, Constant* C2) {
660 return getTy(C1->getType(), Instruction::Mul, C1, C2,
661 OverflowingBinaryOperator::NoSignedWrap);
662}
663
Dan Gohman6e7ad952009-09-03 23:34:49 +0000664Constant* ConstantExpr::getExactSDiv(Constant* C1, Constant* C2) {
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000665 return getTy(C1->getType(), Instruction::SDiv, C1, C2,
666 SDivOperator::IsExact);
Dan Gohman6e7ad952009-09-03 23:34:49 +0000667}
668
Reid Spencer3da59db2006-11-27 01:05:10 +0000669// Utility function for determining if a ConstantExpr is a CastOp or not. This
670// can't be inline because we don't want to #include Instruction.h into
671// Constant.h
672bool ConstantExpr::isCast() const {
673 return Instruction::isCast(getOpcode());
674}
675
Reid Spencer077d0eb2006-12-04 05:19:50 +0000676bool ConstantExpr::isCompare() const {
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +0000677 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
Reid Spencer077d0eb2006-12-04 05:19:50 +0000678}
679
Dan Gohmane6992f72009-09-10 23:37:55 +0000680bool ConstantExpr::isGEPWithNoNotionalOverIndexing() const {
681 if (getOpcode() != Instruction::GetElementPtr) return false;
682
683 gep_type_iterator GEPI = gep_type_begin(this), E = gep_type_end(this);
684 User::const_op_iterator OI = next(this->op_begin());
685
686 // Skip the first index, as it has no static limit.
687 ++GEPI;
688 ++OI;
689
690 // The remaining indices must be compile-time known integers within the
691 // bounds of the corresponding notional static array types.
692 for (; GEPI != E; ++GEPI, ++OI) {
693 ConstantInt *CI = dyn_cast<ConstantInt>(*OI);
694 if (!CI) return false;
695 if (const ArrayType *ATy = dyn_cast<ArrayType>(*GEPI))
696 if (CI->getValue().getActiveBits() > 64 ||
697 CI->getZExtValue() >= ATy->getNumElements())
698 return false;
699 }
700
701 // All the indices checked out.
702 return true;
703}
704
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000705bool ConstantExpr::hasIndices() const {
706 return getOpcode() == Instruction::ExtractValue ||
707 getOpcode() == Instruction::InsertValue;
708}
709
710const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const {
711 if (const ExtractValueConstantExpr *EVCE =
712 dyn_cast<ExtractValueConstantExpr>(this))
713 return EVCE->Indices;
Dan Gohman1a203572008-06-23 16:39:44 +0000714
715 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000716}
717
Reid Spencer728b6db2006-12-03 05:48:19 +0000718unsigned ConstantExpr::getPredicate() const {
Nate Begemanac80ade2008-05-12 19:01:56 +0000719 assert(getOpcode() == Instruction::FCmp ||
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +0000720 getOpcode() == Instruction::ICmp);
Chris Lattnerb7daa842007-10-18 16:26:24 +0000721 return ((const CompareConstantExpr*)this)->predicate;
Reid Spencer728b6db2006-12-03 05:48:19 +0000722}
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000723
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000724/// getWithOperandReplaced - Return a constant expression identical to this
725/// one, but with the specified operand set to the specified value.
Reid Spencer3da59db2006-11-27 01:05:10 +0000726Constant *
727ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000728 assert(OpNo < getNumOperands() && "Operand num is out of range!");
729 assert(Op->getType() == getOperand(OpNo)->getType() &&
730 "Replacing operand with value of different type!");
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000731 if (getOperand(OpNo) == Op)
732 return const_cast<ConstantExpr*>(this);
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000733
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000734 Constant *Op0, *Op1, *Op2;
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000735 switch (getOpcode()) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000736 case Instruction::Trunc:
737 case Instruction::ZExt:
738 case Instruction::SExt:
739 case Instruction::FPTrunc:
740 case Instruction::FPExt:
741 case Instruction::UIToFP:
742 case Instruction::SIToFP:
743 case Instruction::FPToUI:
744 case Instruction::FPToSI:
745 case Instruction::PtrToInt:
746 case Instruction::IntToPtr:
747 case Instruction::BitCast:
748 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000749 case Instruction::Select:
750 Op0 = (OpNo == 0) ? Op : getOperand(0);
751 Op1 = (OpNo == 1) ? Op : getOperand(1);
752 Op2 = (OpNo == 2) ? Op : getOperand(2);
753 return ConstantExpr::getSelect(Op0, Op1, Op2);
754 case Instruction::InsertElement:
755 Op0 = (OpNo == 0) ? Op : getOperand(0);
756 Op1 = (OpNo == 1) ? Op : getOperand(1);
757 Op2 = (OpNo == 2) ? Op : getOperand(2);
758 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
759 case Instruction::ExtractElement:
760 Op0 = (OpNo == 0) ? Op : getOperand(0);
761 Op1 = (OpNo == 1) ? Op : getOperand(1);
762 return ConstantExpr::getExtractElement(Op0, Op1);
763 case Instruction::ShuffleVector:
764 Op0 = (OpNo == 0) ? Op : getOperand(0);
765 Op1 = (OpNo == 1) ? Op : getOperand(1);
766 Op2 = (OpNo == 2) ? Op : getOperand(2);
767 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000768 case Instruction::GetElementPtr: {
Chris Lattnerf9021ff2007-02-19 20:01:23 +0000769 SmallVector<Constant*, 8> Ops;
Dan Gohman041e2eb2008-05-15 19:50:34 +0000770 Ops.resize(getNumOperands()-1);
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000771 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Dan Gohman041e2eb2008-05-15 19:50:34 +0000772 Ops[i-1] = getOperand(i);
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000773 if (OpNo == 0)
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000774 return cast<GEPOperator>(this)->isInBounds() ?
775 ConstantExpr::getInBoundsGetElementPtr(Op, &Ops[0], Ops.size()) :
776 ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000777 Ops[OpNo-1] = Op;
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000778 return cast<GEPOperator>(this)->isInBounds() ?
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000779 ConstantExpr::getInBoundsGetElementPtr(getOperand(0), &Ops[0],Ops.size()):
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000780 ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000781 }
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000782 default:
783 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000784 Op0 = (OpNo == 0) ? Op : getOperand(0);
785 Op1 = (OpNo == 1) ? Op : getOperand(1);
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000786 return ConstantExpr::get(getOpcode(), Op0, Op1, SubclassOptionalData);
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000787 }
788}
789
790/// getWithOperands - This returns the current constant expression with the
791/// operands replaced with the specified values. The specified operands must
792/// match count and type with the existing ones.
793Constant *ConstantExpr::
Chris Lattnerb054bfd2008-08-20 22:27:40 +0000794getWithOperands(Constant* const *Ops, unsigned NumOps) const {
795 assert(NumOps == getNumOperands() && "Operand count mismatch!");
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000796 bool AnyChange = false;
Chris Lattnerb054bfd2008-08-20 22:27:40 +0000797 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000798 assert(Ops[i]->getType() == getOperand(i)->getType() &&
799 "Operand type mismatch!");
800 AnyChange |= Ops[i] != getOperand(i);
801 }
802 if (!AnyChange) // No operands changed, return self.
803 return const_cast<ConstantExpr*>(this);
804
805 switch (getOpcode()) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000806 case Instruction::Trunc:
807 case Instruction::ZExt:
808 case Instruction::SExt:
809 case Instruction::FPTrunc:
810 case Instruction::FPExt:
811 case Instruction::UIToFP:
812 case Instruction::SIToFP:
813 case Instruction::FPToUI:
814 case Instruction::FPToSI:
815 case Instruction::PtrToInt:
816 case Instruction::IntToPtr:
817 case Instruction::BitCast:
818 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000819 case Instruction::Select:
820 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
821 case Instruction::InsertElement:
822 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
823 case Instruction::ExtractElement:
824 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
825 case Instruction::ShuffleVector:
826 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Chris Lattnerf9021ff2007-02-19 20:01:23 +0000827 case Instruction::GetElementPtr:
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000828 return cast<GEPOperator>(this)->isInBounds() ?
829 ConstantExpr::getInBoundsGetElementPtr(Ops[0], &Ops[1], NumOps-1) :
830 ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], NumOps-1);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000831 case Instruction::ICmp:
832 case Instruction::FCmp:
833 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000834 default:
835 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000836 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1], SubclassOptionalData);
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000837 }
838}
839
Chris Lattner00950542001-06-06 20:29:01 +0000840
841//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +0000842// isValueValidForType implementations
843
Reid Spencer9b11d512006-12-19 01:28:19 +0000844bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000845 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Owen Anderson1d0be152009-08-13 21:58:54 +0000846 if (Ty == Type::getInt1Ty(Ty->getContext()))
Reid Spencera54b7cb2007-01-12 07:05:14 +0000847 return Val == 0 || Val == 1;
Reid Spencer554cec62007-02-05 23:47:56 +0000848 if (NumBits >= 64)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000849 return true; // always true, has to fit in largest type
850 uint64_t Max = (1ll << NumBits) - 1;
851 return Val <= Max;
Reid Spencer9b11d512006-12-19 01:28:19 +0000852}
853
Reid Spencerb83eb642006-10-20 07:07:24 +0000854bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000855 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Owen Anderson1d0be152009-08-13 21:58:54 +0000856 if (Ty == Type::getInt1Ty(Ty->getContext()))
Reid Spencerc1030572007-01-19 21:13:56 +0000857 return Val == 0 || Val == 1 || Val == -1;
Reid Spencer554cec62007-02-05 23:47:56 +0000858 if (NumBits >= 64)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000859 return true; // always true, has to fit in largest type
860 int64_t Min = -(1ll << (NumBits-1));
861 int64_t Max = (1ll << (NumBits-1)) - 1;
862 return (Val >= Min && Val <= Max);
Chris Lattner00950542001-06-06 20:29:01 +0000863}
864
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000865bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) {
866 // convert modifies in place, so make a copy.
867 APFloat Val2 = APFloat(Val);
Dale Johannesen23a98552008-10-09 23:00:39 +0000868 bool losesInfo;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000869 switch (Ty->getTypeID()) {
Chris Lattner00950542001-06-06 20:29:01 +0000870 default:
871 return false; // These can't be represented as floating point!
872
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000873 // FIXME rounding mode needs to be more flexible
Dale Johannesen23a98552008-10-09 23:00:39 +0000874 case Type::FloatTyID: {
875 if (&Val2.getSemantics() == &APFloat::IEEEsingle)
876 return true;
877 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
878 return !losesInfo;
879 }
880 case Type::DoubleTyID: {
881 if (&Val2.getSemantics() == &APFloat::IEEEsingle ||
882 &Val2.getSemantics() == &APFloat::IEEEdouble)
883 return true;
884 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
885 return !losesInfo;
886 }
Dale Johannesenebbc95d2007-08-09 22:51:36 +0000887 case Type::X86_FP80TyID:
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000888 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
889 &Val2.getSemantics() == &APFloat::IEEEdouble ||
890 &Val2.getSemantics() == &APFloat::x87DoubleExtended;
Dale Johannesenebbc95d2007-08-09 22:51:36 +0000891 case Type::FP128TyID:
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000892 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
893 &Val2.getSemantics() == &APFloat::IEEEdouble ||
894 &Val2.getSemantics() == &APFloat::IEEEquad;
Dale Johannesena471c2e2007-10-11 18:07:22 +0000895 case Type::PPC_FP128TyID:
896 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
897 &Val2.getSemantics() == &APFloat::IEEEdouble ||
898 &Val2.getSemantics() == &APFloat::PPCDoubleDouble;
Chris Lattner00950542001-06-06 20:29:01 +0000899 }
Chris Lattnerd74ea2b2006-05-24 17:04:05 +0000900}
Chris Lattner37bf6302001-07-20 19:16:02 +0000901
Chris Lattner531daef2001-09-07 16:46:31 +0000902//===----------------------------------------------------------------------===//
Chris Lattner531daef2001-09-07 16:46:31 +0000903// Factory Function Implementation
904
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000905ConstantAggregateZero* ConstantAggregateZero::get(const Type* Ty) {
906 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) &&
907 "Cannot create an aggregate zero of non-aggregate type!");
908
909 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000910 return pImpl->AggZeroConstants.getOrCreate(Ty, 0);
911}
912
Dan Gohman0f8b53f2009-03-03 02:55:14 +0000913/// destroyConstant - Remove the constant from the constant table...
914///
Owen Anderson04fb7c32009-06-20 00:24:58 +0000915void ConstantAggregateZero::destroyConstant() {
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000916 getType()->getContext().pImpl->AggZeroConstants.remove(this);
Chris Lattner40bbeb52004-02-15 05:53:04 +0000917 destroyConstantImpl();
918}
919
Dan Gohman0f8b53f2009-03-03 02:55:14 +0000920/// destroyConstant - Remove the constant from the constant table...
921///
Owen Anderson04fb7c32009-06-20 00:24:58 +0000922void ConstantArray::destroyConstant() {
Owen Anderson1fd70962009-07-28 18:32:17 +0000923 getType()->getContext().pImpl->ArrayConstants.remove(this);
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000924 destroyConstantImpl();
925}
926
Reid Spencer3d10b0b2007-01-26 07:37:34 +0000927/// isString - This method returns true if the array is an array of i8, and
928/// if the elements of the array are all ConstantInt's.
Chris Lattner13cfdea2004-01-14 17:06:38 +0000929bool ConstantArray::isString() const {
Reid Spencer3d10b0b2007-01-26 07:37:34 +0000930 // Check the element type for i8...
Benjamin Kramer8c65f6e2010-01-05 21:05:54 +0000931 if (!getType()->getElementType()->isInteger(8))
Chris Lattner13cfdea2004-01-14 17:06:38 +0000932 return false;
933 // Check the elements to make sure they are all integers, not constant
934 // expressions.
935 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
936 if (!isa<ConstantInt>(getOperand(i)))
937 return false;
938 return true;
939}
940
Evan Cheng22c70302006-10-26 19:15:05 +0000941/// isCString - This method returns true if the array is a string (see
Dan Gohman0f8b53f2009-03-03 02:55:14 +0000942/// isString) and it ends in a null byte \\0 and does not contains any other
Evan Cheng22c70302006-10-26 19:15:05 +0000943/// null bytes except its terminator.
Owen Anderson1ca29d32009-07-13 21:27:19 +0000944bool ConstantArray::isCString() const {
Reid Spencer3d10b0b2007-01-26 07:37:34 +0000945 // Check the element type for i8...
Benjamin Kramer8c65f6e2010-01-05 21:05:54 +0000946 if (!getType()->getElementType()->isInteger(8))
Evan Chengabf63452006-10-26 21:48:03 +0000947 return false;
Owen Anderson1ca29d32009-07-13 21:27:19 +0000948
Evan Chengabf63452006-10-26 21:48:03 +0000949 // Last element must be a null.
Owen Anderson1ca29d32009-07-13 21:27:19 +0000950 if (!getOperand(getNumOperands()-1)->isNullValue())
Evan Chengabf63452006-10-26 21:48:03 +0000951 return false;
952 // Other elements must be non-null integers.
953 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
954 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng22c70302006-10-26 19:15:05 +0000955 return false;
Owen Anderson1ca29d32009-07-13 21:27:19 +0000956 if (getOperand(i)->isNullValue())
Evan Chengabf63452006-10-26 21:48:03 +0000957 return false;
958 }
Evan Cheng22c70302006-10-26 19:15:05 +0000959 return true;
960}
961
962
Dan Gohman0f8b53f2009-03-03 02:55:14 +0000963/// getAsString - If the sub-element type of this array is i8
964/// then this method converts the array to an std::string and returns it.
965/// Otherwise, it asserts out.
966///
Chris Lattner93aeea32002-08-26 17:53:56 +0000967std::string ConstantArray::getAsString() const {
Chris Lattner13cfdea2004-01-14 17:06:38 +0000968 assert(isString() && "Not a string!");
Chris Lattner93aeea32002-08-26 17:53:56 +0000969 std::string Result;
Owen Anderson45e39582008-06-24 21:58:29 +0000970 Result.reserve(getNumOperands());
Chris Lattnerc07736a2003-07-23 15:22:26 +0000971 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersone4f6ee52008-06-25 01:05:05 +0000972 Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue());
Chris Lattner93aeea32002-08-26 17:53:56 +0000973 return Result;
974}
975
976
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000977//---- ConstantStruct::get() implementation...
Chris Lattner531daef2001-09-07 16:46:31 +0000978//
Chris Lattnered468e372003-10-05 00:17:43 +0000979
Chris Lattner31f84992003-11-21 20:23:48 +0000980namespace llvm {
Misha Brukmanfd939082005-04-21 23:48:37 +0000981
Chris Lattner531daef2001-09-07 16:46:31 +0000982}
Chris Lattner6a57baa2001-10-03 15:39:36 +0000983
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000984// destroyConstant - Remove the constant from the constant table...
Chris Lattner6a57baa2001-10-03 15:39:36 +0000985//
Owen Anderson04fb7c32009-06-20 00:24:58 +0000986void ConstantStruct::destroyConstant() {
Owen Anderson8fa33382009-07-27 22:29:26 +0000987 getType()->getContext().pImpl->StructConstants.remove(this);
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000988 destroyConstantImpl();
989}
Chris Lattner6a57baa2001-10-03 15:39:36 +0000990
Brian Gaeke715c90b2004-08-20 06:00:58 +0000991// destroyConstant - Remove the constant from the constant table...
992//
Owen Anderson04fb7c32009-06-20 00:24:58 +0000993void ConstantVector::destroyConstant() {
Owen Andersonaf7ec972009-07-28 21:19:26 +0000994 getType()->getContext().pImpl->VectorConstants.remove(this);
Brian Gaeke715c90b2004-08-20 06:00:58 +0000995 destroyConstantImpl();
996}
997
Dan Gohmanfa73ea22007-05-24 14:36:04 +0000998/// This function will return true iff every element in this vector constant
Jim Laskeyfa301822007-01-12 22:39:14 +0000999/// is set to all ones.
1000/// @returns true iff this constant's emements are all set to all ones.
1001/// @brief Determine if the value is all ones.
Reid Spencer9d6565a2007-02-15 02:26:10 +00001002bool ConstantVector::isAllOnesValue() const {
Jim Laskeyfa301822007-01-12 22:39:14 +00001003 // Check out first element.
1004 const Constant *Elt = getOperand(0);
1005 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1006 if (!CI || !CI->isAllOnesValue()) return false;
1007 // Then make sure all remaining elements point to the same value.
1008 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1009 if (getOperand(I) != Elt) return false;
1010 }
1011 return true;
1012}
1013
Dan Gohman3b7cf0a2007-10-17 17:51:30 +00001014/// getSplatValue - If this is a splat constant, where all of the
1015/// elements have the same value, return that value. Otherwise return null.
1016Constant *ConstantVector::getSplatValue() {
1017 // Check out first element.
1018 Constant *Elt = getOperand(0);
1019 // Then make sure all remaining elements point to the same value.
1020 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
1021 if (getOperand(I) != Elt) return 0;
1022 return Elt;
1023}
1024
Chris Lattner2ee11ec2009-10-28 00:01:44 +00001025//---- ConstantPointerNull::get() implementation.
Chris Lattnerf5ec48d2001-10-13 06:57:33 +00001026//
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001027
Owen Anderson04fb7c32009-06-20 00:24:58 +00001028ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Owen Anderson7e3142b2009-07-31 22:45:43 +00001029 return Ty->getContext().pImpl->NullPtrConstants.getOrCreate(Ty, 0);
Chris Lattner6a57baa2001-10-03 15:39:36 +00001030}
1031
Chris Lattner41661fd2002-08-18 00:40:04 +00001032// destroyConstant - Remove the constant from the constant table...
1033//
Owen Anderson04fb7c32009-06-20 00:24:58 +00001034void ConstantPointerNull::destroyConstant() {
Owen Anderson7e3142b2009-07-31 22:45:43 +00001035 getType()->getContext().pImpl->NullPtrConstants.remove(this);
Chris Lattner41661fd2002-08-18 00:40:04 +00001036 destroyConstantImpl();
1037}
1038
1039
Chris Lattner2ee11ec2009-10-28 00:01:44 +00001040//---- UndefValue::get() implementation.
Chris Lattnerb9f18592004-10-16 18:07:16 +00001041//
1042
Owen Anderson04fb7c32009-06-20 00:24:58 +00001043UndefValue *UndefValue::get(const Type *Ty) {
Owen Anderson7e3142b2009-07-31 22:45:43 +00001044 return Ty->getContext().pImpl->UndefValueConstants.getOrCreate(Ty, 0);
Chris Lattnerb9f18592004-10-16 18:07:16 +00001045}
1046
1047// destroyConstant - Remove the constant from the constant table.
1048//
Owen Anderson04fb7c32009-06-20 00:24:58 +00001049void UndefValue::destroyConstant() {
Owen Anderson7e3142b2009-07-31 22:45:43 +00001050 getType()->getContext().pImpl->UndefValueConstants.remove(this);
Chris Lattnerb9f18592004-10-16 18:07:16 +00001051 destroyConstantImpl();
1052}
1053
Chris Lattner2ee11ec2009-10-28 00:01:44 +00001054//---- BlockAddress::get() implementation.
1055//
1056
1057BlockAddress *BlockAddress::get(BasicBlock *BB) {
1058 assert(BB->getParent() != 0 && "Block must have a parent");
1059 return get(BB->getParent(), BB);
1060}
1061
1062BlockAddress *BlockAddress::get(Function *F, BasicBlock *BB) {
1063 BlockAddress *&BA =
1064 F->getContext().pImpl->BlockAddresses[std::make_pair(F, BB)];
1065 if (BA == 0)
1066 BA = new BlockAddress(F, BB);
1067
1068 assert(BA->getFunction() == F && "Basic block moved between functions");
1069 return BA;
1070}
1071
1072BlockAddress::BlockAddress(Function *F, BasicBlock *BB)
1073: Constant(Type::getInt8PtrTy(F->getContext()), Value::BlockAddressVal,
1074 &Op<0>(), 2) {
Chris Lattnerd0ec2352009-11-01 03:03:03 +00001075 setOperand(0, F);
1076 setOperand(1, BB);
Chris Lattnercdfc9402009-11-01 01:27:45 +00001077 BB->AdjustBlockAddressRefCount(1);
Chris Lattner2ee11ec2009-10-28 00:01:44 +00001078}
1079
1080
1081// destroyConstant - Remove the constant from the constant table.
1082//
1083void BlockAddress::destroyConstant() {
1084 getFunction()->getType()->getContext().pImpl
1085 ->BlockAddresses.erase(std::make_pair(getFunction(), getBasicBlock()));
Chris Lattnercdfc9402009-11-01 01:27:45 +00001086 getBasicBlock()->AdjustBlockAddressRefCount(-1);
Chris Lattner2ee11ec2009-10-28 00:01:44 +00001087 destroyConstantImpl();
1088}
1089
1090void BlockAddress::replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
1091 // This could be replacing either the Basic Block or the Function. In either
1092 // case, we have to remove the map entry.
1093 Function *NewF = getFunction();
1094 BasicBlock *NewBB = getBasicBlock();
1095
1096 if (U == &Op<0>())
1097 NewF = cast<Function>(To);
1098 else
1099 NewBB = cast<BasicBlock>(To);
1100
1101 // See if the 'new' entry already exists, if not, just update this in place
1102 // and return early.
1103 BlockAddress *&NewBA =
1104 getContext().pImpl->BlockAddresses[std::make_pair(NewF, NewBB)];
1105 if (NewBA == 0) {
Chris Lattnerd0ec2352009-11-01 03:03:03 +00001106 getBasicBlock()->AdjustBlockAddressRefCount(-1);
1107
Chris Lattner2ee11ec2009-10-28 00:01:44 +00001108 // Remove the old entry, this can't cause the map to rehash (just a
1109 // tombstone will get added).
1110 getContext().pImpl->BlockAddresses.erase(std::make_pair(getFunction(),
1111 getBasicBlock()));
1112 NewBA = this;
Chris Lattnerd0ec2352009-11-01 03:03:03 +00001113 setOperand(0, NewF);
1114 setOperand(1, NewBB);
1115 getBasicBlock()->AdjustBlockAddressRefCount(1);
Chris Lattner2ee11ec2009-10-28 00:01:44 +00001116 return;
1117 }
1118
1119 // Otherwise, I do need to replace this with an existing value.
1120 assert(NewBA != this && "I didn't contain From!");
1121
1122 // Everyone using this now uses the replacement.
1123 uncheckedReplaceAllUsesWith(NewBA);
1124
1125 destroyConstant();
1126}
1127
1128//---- ConstantExpr::get() implementations.
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001129//
Reid Spencer79e21d32006-12-31 05:26:44 +00001130
Reid Spencer3da59db2006-11-27 01:05:10 +00001131/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands66a1a052008-03-30 19:38:55 +00001132/// cast in the ExprConstants map. It is used by the various get* methods below.
Reid Spencer3da59db2006-11-27 01:05:10 +00001133static inline Constant *getFoldedCast(
Owen Anderson04fb7c32009-06-20 00:24:58 +00001134 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner9eacf8a2003-10-07 22:19:19 +00001135 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer3da59db2006-11-27 01:05:10 +00001136 // Fold a few common cases
Owen Anderson5defacc2009-07-31 17:39:07 +00001137 if (Constant *FC = ConstantFoldCastInstruction(Ty->getContext(), opc, C, Ty))
Reid Spencer3da59db2006-11-27 01:05:10 +00001138 return FC;
Chris Lattnerd628f6a2003-04-17 19:24:48 +00001139
Owen Andersond03eecd2009-08-04 20:25:11 +00001140 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
1141
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001142 // Look up the constant in the table first to ensure uniqueness
Chris Lattner9bc02a42003-05-13 21:37:02 +00001143 std::vector<Constant*> argVec(1, C);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001144 ExprMapKeyType Key(opc, argVec);
Owen Anderson3e456ab2009-06-17 18:40:29 +00001145
Owen Andersond03eecd2009-08-04 20:25:11 +00001146 return pImpl->ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001147}
Reid Spencer7858b332006-12-05 19:14:13 +00001148
Owen Anderson04fb7c32009-06-20 00:24:58 +00001149Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
Reid Spencer3da59db2006-11-27 01:05:10 +00001150 Instruction::CastOps opc = Instruction::CastOps(oc);
1151 assert(Instruction::isCast(opc) && "opcode out of range");
1152 assert(C && Ty && "Null arguments to getCast");
Chris Lattner0b68a002010-01-26 21:51:43 +00001153 assert(CastInst::castIsValid(opc, C, Ty) && "Invalid constantexpr cast!");
Reid Spencer3da59db2006-11-27 01:05:10 +00001154
1155 switch (opc) {
Chris Lattner0b68a002010-01-26 21:51:43 +00001156 default:
1157 llvm_unreachable("Invalid cast opcode");
1158 break;
1159 case Instruction::Trunc: return getTrunc(C, Ty);
1160 case Instruction::ZExt: return getZExt(C, Ty);
1161 case Instruction::SExt: return getSExt(C, Ty);
1162 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1163 case Instruction::FPExt: return getFPExtend(C, Ty);
1164 case Instruction::UIToFP: return getUIToFP(C, Ty);
1165 case Instruction::SIToFP: return getSIToFP(C, Ty);
1166 case Instruction::FPToUI: return getFPToUI(C, Ty);
1167 case Instruction::FPToSI: return getFPToSI(C, Ty);
1168 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1169 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1170 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattnerf5ac6c22005-01-01 15:59:57 +00001171 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001172 return 0;
Reid Spencer7858b332006-12-05 19:14:13 +00001173}
1174
Reid Spencer848414e2006-12-04 20:17:56 +00001175Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001176 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer848414e2006-12-04 20:17:56 +00001177 return getCast(Instruction::BitCast, C, Ty);
1178 return getCast(Instruction::ZExt, C, Ty);
1179}
1180
Owen Anderson04fb7c32009-06-20 00:24:58 +00001181Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001182 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Owen Anderson04fb7c32009-06-20 00:24:58 +00001183 return getCast(Instruction::BitCast, C, Ty);
1184 return getCast(Instruction::SExt, C, Ty);
Reid Spencer848414e2006-12-04 20:17:56 +00001185}
1186
1187Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001188 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer848414e2006-12-04 20:17:56 +00001189 return getCast(Instruction::BitCast, C, Ty);
1190 return getCast(Instruction::Trunc, C, Ty);
1191}
1192
Owen Anderson04fb7c32009-06-20 00:24:58 +00001193Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
Reid Spencerc0459fb2006-12-05 03:25:26 +00001194 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner42a75512007-01-15 02:27:26 +00001195 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerc0459fb2006-12-05 03:25:26 +00001196
Chris Lattner42a75512007-01-15 02:27:26 +00001197 if (Ty->isInteger())
Owen Anderson04fb7c32009-06-20 00:24:58 +00001198 return getCast(Instruction::PtrToInt, S, Ty);
1199 return getCast(Instruction::BitCast, S, Ty);
Reid Spencerc0459fb2006-12-05 03:25:26 +00001200}
1201
Reid Spencer84f3eab2006-12-12 00:51:07 +00001202Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1203 bool isSigned) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001204 assert(C->getType()->isIntOrIntVector() &&
1205 Ty->isIntOrIntVector() && "Invalid cast");
1206 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1207 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer84f3eab2006-12-12 00:51:07 +00001208 Instruction::CastOps opcode =
1209 (SrcBits == DstBits ? Instruction::BitCast :
1210 (SrcBits > DstBits ? Instruction::Trunc :
1211 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1212 return getCast(opcode, C, Ty);
1213}
1214
1215Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001216 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer84f3eab2006-12-12 00:51:07 +00001217 "Invalid cast");
Dan Gohman6de29f82009-06-15 22:12:54 +00001218 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1219 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencerf25212a2006-12-12 05:38:50 +00001220 if (SrcBits == DstBits)
1221 return C; // Avoid a useless cast
Reid Spencer84f3eab2006-12-12 00:51:07 +00001222 Instruction::CastOps opcode =
Reid Spencerf25212a2006-12-12 05:38:50 +00001223 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer84f3eab2006-12-12 00:51:07 +00001224 return getCast(opcode, C, Ty);
1225}
1226
Owen Anderson04fb7c32009-06-20 00:24:58 +00001227Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001228#ifndef NDEBUG
1229 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1230 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1231#endif
1232 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1233 assert(C->getType()->isIntOrIntVector() && "Trunc operand must be integer");
1234 assert(Ty->isIntOrIntVector() && "Trunc produces only integral");
1235 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer3da59db2006-11-27 01:05:10 +00001236 "SrcTy must be larger than DestTy for Trunc!");
1237
Owen Anderson04fb7c32009-06-20 00:24:58 +00001238 return getFoldedCast(Instruction::Trunc, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001239}
1240
Owen Anderson04fb7c32009-06-20 00:24:58 +00001241Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001242#ifndef NDEBUG
1243 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1244 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1245#endif
1246 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1247 assert(C->getType()->isIntOrIntVector() && "SExt operand must be integral");
1248 assert(Ty->isIntOrIntVector() && "SExt produces only integer");
1249 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer3da59db2006-11-27 01:05:10 +00001250 "SrcTy must be smaller than DestTy for SExt!");
1251
Owen Anderson04fb7c32009-06-20 00:24:58 +00001252 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerd144f422004-04-04 23:20:30 +00001253}
1254
Owen Anderson04fb7c32009-06-20 00:24:58 +00001255Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001256#ifndef NDEBUG
1257 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1258 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1259#endif
1260 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1261 assert(C->getType()->isIntOrIntVector() && "ZEXt operand must be integral");
1262 assert(Ty->isIntOrIntVector() && "ZExt produces only integer");
1263 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer3da59db2006-11-27 01:05:10 +00001264 "SrcTy must be smaller than DestTy for ZExt!");
1265
Owen Anderson04fb7c32009-06-20 00:24:58 +00001266 return getFoldedCast(Instruction::ZExt, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001267}
1268
Owen Anderson04fb7c32009-06-20 00:24:58 +00001269Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001270#ifndef NDEBUG
1271 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1272 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1273#endif
1274 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1275 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1276 C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer3da59db2006-11-27 01:05:10 +00001277 "This is an illegal floating point truncation!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001278 return getFoldedCast(Instruction::FPTrunc, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001279}
1280
Owen Anderson04fb7c32009-06-20 00:24:58 +00001281Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001282#ifndef NDEBUG
1283 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1284 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1285#endif
1286 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1287 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1288 C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer3da59db2006-11-27 01:05:10 +00001289 "This is an illegal floating point extension!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001290 return getFoldedCast(Instruction::FPExt, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001291}
1292
Owen Anderson04fb7c32009-06-20 00:24:58 +00001293Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Devang Patelb6dc9352008-11-03 23:20:04 +00001294#ifndef NDEBUG
Nate Begemanb348d182007-11-17 03:58:34 +00001295 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1296 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Patelb6dc9352008-11-03 23:20:04 +00001297#endif
Nate Begemanb348d182007-11-17 03:58:34 +00001298 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1299 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
1300 "This is an illegal uint to floating point cast!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001301 return getFoldedCast(Instruction::UIToFP, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001302}
1303
Owen Anderson04fb7c32009-06-20 00:24:58 +00001304Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Devang Patelb6dc9352008-11-03 23:20:04 +00001305#ifndef NDEBUG
Nate Begemanb348d182007-11-17 03:58:34 +00001306 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1307 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Patelb6dc9352008-11-03 23:20:04 +00001308#endif
Nate Begemanb348d182007-11-17 03:58:34 +00001309 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1310 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
Reid Spencer3da59db2006-11-27 01:05:10 +00001311 "This is an illegal sint to floating point cast!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001312 return getFoldedCast(Instruction::SIToFP, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001313}
1314
Owen Anderson04fb7c32009-06-20 00:24:58 +00001315Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Devang Patelb6dc9352008-11-03 23:20:04 +00001316#ifndef NDEBUG
Nate Begemanb348d182007-11-17 03:58:34 +00001317 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1318 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Patelb6dc9352008-11-03 23:20:04 +00001319#endif
Nate Begemanb348d182007-11-17 03:58:34 +00001320 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1321 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1322 "This is an illegal floating point to uint cast!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001323 return getFoldedCast(Instruction::FPToUI, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001324}
1325
Owen Anderson04fb7c32009-06-20 00:24:58 +00001326Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Devang Patelb6dc9352008-11-03 23:20:04 +00001327#ifndef NDEBUG
Nate Begemanb348d182007-11-17 03:58:34 +00001328 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1329 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Patelb6dc9352008-11-03 23:20:04 +00001330#endif
Nate Begemanb348d182007-11-17 03:58:34 +00001331 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1332 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1333 "This is an illegal floating point to sint cast!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001334 return getFoldedCast(Instruction::FPToSI, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001335}
1336
Owen Anderson04fb7c32009-06-20 00:24:58 +00001337Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
Reid Spencer3da59db2006-11-27 01:05:10 +00001338 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner42a75512007-01-15 02:27:26 +00001339 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001340 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
Reid Spencer3da59db2006-11-27 01:05:10 +00001341}
1342
Owen Anderson04fb7c32009-06-20 00:24:58 +00001343Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner42a75512007-01-15 02:27:26 +00001344 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer3da59db2006-11-27 01:05:10 +00001345 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001346 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
Reid Spencer3da59db2006-11-27 01:05:10 +00001347}
1348
Owen Anderson04fb7c32009-06-20 00:24:58 +00001349Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
Chris Lattner0b68a002010-01-26 21:51:43 +00001350 assert(CastInst::castIsValid(Instruction::BitCast, C, DstTy) &&
1351 "Invalid constantexpr bitcast!");
Chris Lattner8c7f24a2009-03-21 06:55:54 +00001352
1353 // It is common to ask for a bitcast of a value to its own type, handle this
1354 // speedily.
1355 if (C->getType() == DstTy) return C;
1356
Owen Anderson04fb7c32009-06-20 00:24:58 +00001357 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerd144f422004-04-04 23:20:30 +00001358}
1359
Chris Lattnered468e372003-10-05 00:17:43 +00001360Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001361 Constant *C1, Constant *C2,
1362 unsigned Flags) {
Chris Lattnerf31f5832003-05-21 17:49:25 +00001363 // Check the operands for consistency first
Reid Spencer0a783f72006-11-02 01:53:59 +00001364 assert(Opcode >= Instruction::BinaryOpsBegin &&
1365 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattnerf31f5832003-05-21 17:49:25 +00001366 "Invalid opcode in binary constant expression");
1367 assert(C1->getType() == C2->getType() &&
1368 "Operand types in binary constant expression should match");
Chris Lattnered468e372003-10-05 00:17:43 +00001369
Owen Anderson1d0be152009-08-13 21:58:54 +00001370 if (ReqTy == C1->getType() || ReqTy == Type::getInt1Ty(ReqTy->getContext()))
Owen Anderson5defacc2009-07-31 17:39:07 +00001371 if (Constant *FC = ConstantFoldBinaryInstruction(ReqTy->getContext(),
1372 Opcode, C1, C2))
Chris Lattnered468e372003-10-05 00:17:43 +00001373 return FC; // Fold a few common cases...
Chris Lattnerd628f6a2003-04-17 19:24:48 +00001374
Chris Lattner9bc02a42003-05-13 21:37:02 +00001375 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001376 ExprMapKeyType Key(Opcode, argVec, 0, Flags);
Owen Anderson31c36f02009-06-17 20:10:08 +00001377
Owen Andersond03eecd2009-08-04 20:25:11 +00001378 LLVMContextImpl *pImpl = ReqTy->getContext().pImpl;
Owen Andersond03eecd2009-08-04 20:25:11 +00001379 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001380}
1381
Reid Spencere4d87aa2006-12-23 06:05:41 +00001382Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Nate Begemanff795a82008-07-25 17:56:27 +00001383 Constant *C1, Constant *C2) {
Reid Spencere4d87aa2006-12-23 06:05:41 +00001384 switch (predicate) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001385 default: llvm_unreachable("Invalid CmpInst predicate");
Nate Begemanb5557ab2008-07-25 17:35:37 +00001386 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
1387 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
1388 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
1389 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
1390 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
1391 case CmpInst::FCMP_TRUE:
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00001392 return getFCmp(predicate, C1, C2);
1393
Nate Begemanb5557ab2008-07-25 17:35:37 +00001394 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
1395 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
1396 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
1397 case CmpInst::ICMP_SLE:
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00001398 return getICmp(predicate, C1, C2);
Reid Spencere4d87aa2006-12-23 06:05:41 +00001399 }
Reid Spencer67263fe2006-12-04 21:35:24 +00001400}
1401
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001402Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2,
1403 unsigned Flags) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001404 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1405 if (C1->getType()->isFPOrFPVector()) {
1406 if (Opcode == Instruction::Add) Opcode = Instruction::FAdd;
1407 else if (Opcode == Instruction::Sub) Opcode = Instruction::FSub;
1408 else if (Opcode == Instruction::Mul) Opcode = Instruction::FMul;
1409 }
Chris Lattner91b362b2004-08-17 17:28:46 +00001410#ifndef NDEBUG
1411 switch (Opcode) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001412 case Instruction::Add:
Reid Spencer0a783f72006-11-02 01:53:59 +00001413 case Instruction::Sub:
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001414 case Instruction::Mul:
Chris Lattner91b362b2004-08-17 17:28:46 +00001415 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001416 assert(C1->getType()->isIntOrIntVector() &&
1417 "Tried to create an integer operation on a non-integer type!");
1418 break;
1419 case Instruction::FAdd:
1420 case Instruction::FSub:
1421 case Instruction::FMul:
1422 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1423 assert(C1->getType()->isFPOrFPVector() &&
1424 "Tried to create a floating-point operation on a "
1425 "non-floating-point type!");
Chris Lattner91b362b2004-08-17 17:28:46 +00001426 break;
Reid Spencer1628cec2006-10-26 06:15:43 +00001427 case Instruction::UDiv:
1428 case Instruction::SDiv:
1429 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanf57478f2009-06-15 22:25:12 +00001430 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer1628cec2006-10-26 06:15:43 +00001431 "Tried to create an arithmetic operation on a non-arithmetic type!");
1432 break;
1433 case Instruction::FDiv:
1434 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanf57478f2009-06-15 22:25:12 +00001435 assert(C1->getType()->isFPOrFPVector() &&
1436 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer1628cec2006-10-26 06:15:43 +00001437 break;
Reid Spencer0a783f72006-11-02 01:53:59 +00001438 case Instruction::URem:
1439 case Instruction::SRem:
1440 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanf57478f2009-06-15 22:25:12 +00001441 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer0a783f72006-11-02 01:53:59 +00001442 "Tried to create an arithmetic operation on a non-arithmetic type!");
1443 break;
1444 case Instruction::FRem:
1445 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanf57478f2009-06-15 22:25:12 +00001446 assert(C1->getType()->isFPOrFPVector() &&
1447 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer0a783f72006-11-02 01:53:59 +00001448 break;
Chris Lattner91b362b2004-08-17 17:28:46 +00001449 case Instruction::And:
1450 case Instruction::Or:
1451 case Instruction::Xor:
1452 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanf57478f2009-06-15 22:25:12 +00001453 assert(C1->getType()->isIntOrIntVector() &&
Misha Brukman1bae2912005-01-27 06:46:38 +00001454 "Tried to create a logical operation on a non-integral type!");
Chris Lattner91b362b2004-08-17 17:28:46 +00001455 break;
Chris Lattner91b362b2004-08-17 17:28:46 +00001456 case Instruction::Shl:
Reid Spencer3822ff52006-11-08 06:47:33 +00001457 case Instruction::LShr:
1458 case Instruction::AShr:
Reid Spencer832254e2007-02-02 02:16:23 +00001459 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanc1317932009-03-14 17:09:17 +00001460 assert(C1->getType()->isIntOrIntVector() &&
Chris Lattner91b362b2004-08-17 17:28:46 +00001461 "Tried to create a shift operation on a non-integer type!");
1462 break;
1463 default:
1464 break;
1465 }
1466#endif
1467
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001468 return getTy(C1->getType(), Opcode, C1, C2, Flags);
Reid Spencer67263fe2006-12-04 21:35:24 +00001469}
1470
Owen Andersonbaf3c402009-07-29 18:55:55 +00001471Constant* ConstantExpr::getSizeOf(const Type* Ty) {
1472 // sizeof is implemented as: (i64) gep (Ty*)null, 1
1473 // Note that a non-inbounds gep is used, as null isn't within any object.
Owen Anderson1d0be152009-08-13 21:58:54 +00001474 Constant *GEPIdx = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
Owen Andersonbaf3c402009-07-29 18:55:55 +00001475 Constant *GEP = getGetElementPtr(
Owen Andersona7235ea2009-07-31 20:28:14 +00001476 Constant::getNullValue(PointerType::getUnqual(Ty)), &GEPIdx, 1);
Owen Anderson1d0be152009-08-13 21:58:54 +00001477 return getCast(Instruction::PtrToInt, GEP,
1478 Type::getInt64Ty(Ty->getContext()));
Owen Andersonbaf3c402009-07-29 18:55:55 +00001479}
1480
1481Constant* ConstantExpr::getAlignOf(const Type* Ty) {
Dan Gohman0f5efe52010-01-28 02:15:55 +00001482 // alignof is implemented as: (i64) gep ({i1,Ty}*)null, 0, 1
Dan Gohmane2574d32009-08-11 17:57:01 +00001483 // Note that a non-inbounds gep is used, as null isn't within any object.
Owen Andersond7f2a6c2009-08-05 23:16:16 +00001484 const Type *AligningTy = StructType::get(Ty->getContext(),
Dan Gohman0f5efe52010-01-28 02:15:55 +00001485 Type::getInt1Ty(Ty->getContext()), Ty, NULL);
Owen Andersona7235ea2009-07-31 20:28:14 +00001486 Constant *NullPtr = Constant::getNullValue(AligningTy->getPointerTo());
Dan Gohman06ed3e72010-01-28 02:43:22 +00001487 Constant *Zero = ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0);
Owen Anderson1d0be152009-08-13 21:58:54 +00001488 Constant *One = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
Owen Andersonbaf3c402009-07-29 18:55:55 +00001489 Constant *Indices[2] = { Zero, One };
1490 Constant *GEP = getGetElementPtr(NullPtr, Indices, 2);
Owen Anderson1d0be152009-08-13 21:58:54 +00001491 return getCast(Instruction::PtrToInt, GEP,
Dan Gohman06ed3e72010-01-28 02:43:22 +00001492 Type::getInt64Ty(Ty->getContext()));
Owen Andersonbaf3c402009-07-29 18:55:55 +00001493}
1494
Dan Gohman3778f212009-08-16 21:26:11 +00001495Constant* ConstantExpr::getOffsetOf(const StructType* STy, unsigned FieldNo) {
Dan Gohman2544a1d2010-02-01 16:37:38 +00001496 return getOffsetOf(STy, ConstantInt::get(Type::getInt32Ty(STy->getContext()),
1497 FieldNo));
1498}
1499
1500Constant* ConstantExpr::getOffsetOf(const Type* Ty, Constant *FieldNo) {
Dan Gohman3778f212009-08-16 21:26:11 +00001501 // offsetof is implemented as: (i64) gep (Ty*)null, 0, FieldNo
1502 // Note that a non-inbounds gep is used, as null isn't within any object.
1503 Constant *GEPIdx[] = {
Dan Gohman2544a1d2010-02-01 16:37:38 +00001504 ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0),
1505 FieldNo
Dan Gohman3778f212009-08-16 21:26:11 +00001506 };
1507 Constant *GEP = getGetElementPtr(
Dan Gohman2544a1d2010-02-01 16:37:38 +00001508 Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx, 2);
Dan Gohman3778f212009-08-16 21:26:11 +00001509 return getCast(Instruction::PtrToInt, GEP,
Dan Gohman2544a1d2010-02-01 16:37:38 +00001510 Type::getInt64Ty(Ty->getContext()));
Dan Gohman3778f212009-08-16 21:26:11 +00001511}
Owen Andersonbaf3c402009-07-29 18:55:55 +00001512
Reid Spencere4d87aa2006-12-23 06:05:41 +00001513Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencer67263fe2006-12-04 21:35:24 +00001514 Constant *C1, Constant *C2) {
1515 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencere4d87aa2006-12-23 06:05:41 +00001516 return getCompareTy(pred, C1, C2);
Chris Lattnerc3d12f02004-08-04 18:50:09 +00001517}
1518
Chris Lattner08a45cc2004-03-12 05:54:04 +00001519Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
Owen Anderson04fb7c32009-06-20 00:24:58 +00001520 Constant *V1, Constant *V2) {
Chris Lattner9ace0cd2008-12-29 00:16:12 +00001521 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner08a45cc2004-03-12 05:54:04 +00001522
1523 if (ReqTy == V1->getType())
Owen Anderson0a5372e2009-07-13 04:09:18 +00001524 if (Constant *SC = ConstantFoldSelectInstruction(
Owen Anderson5defacc2009-07-31 17:39:07 +00001525 ReqTy->getContext(), C, V1, V2))
Chris Lattner08a45cc2004-03-12 05:54:04 +00001526 return SC; // Fold common cases
1527
1528 std::vector<Constant*> argVec(3, C);
1529 argVec[1] = V1;
1530 argVec[2] = V2;
Reid Spencer077d0eb2006-12-04 05:19:50 +00001531 ExprMapKeyType Key(Instruction::Select, argVec);
Owen Anderson31c36f02009-06-17 20:10:08 +00001532
Owen Andersond03eecd2009-08-04 20:25:11 +00001533 LLVMContextImpl *pImpl = ReqTy->getContext().pImpl;
Owen Andersond03eecd2009-08-04 20:25:11 +00001534 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Chris Lattner08a45cc2004-03-12 05:54:04 +00001535}
1536
Chris Lattnered468e372003-10-05 00:17:43 +00001537Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001538 Value* const *Idxs,
Owen Anderson04fb7c32009-06-20 00:24:58 +00001539 unsigned NumIdx) {
Dan Gohman041e2eb2008-05-15 19:50:34 +00001540 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
1541 Idxs+NumIdx) ==
1542 cast<PointerType>(ReqTy)->getElementType() &&
1543 "GEP indices invalid!");
Chris Lattner2e9bb1a2004-02-16 20:46:13 +00001544
Owen Anderson0a5372e2009-07-13 04:09:18 +00001545 if (Constant *FC = ConstantFoldGetElementPtr(
Dan Gohman3bfbc452009-09-11 00:04:14 +00001546 ReqTy->getContext(), C, /*inBounds=*/false,
1547 (Constant**)Idxs, NumIdx))
Chris Lattnerd628f6a2003-04-17 19:24:48 +00001548 return FC; // Fold a few common cases...
Chris Lattner2e9bb1a2004-02-16 20:46:13 +00001549
Chris Lattnered468e372003-10-05 00:17:43 +00001550 assert(isa<PointerType>(C->getType()) &&
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001551 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001552 // Look up the constant in the table first to ensure uniqueness
Chris Lattner7fa6e662004-10-11 22:52:25 +00001553 std::vector<Constant*> ArgVec;
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001554 ArgVec.reserve(NumIdx+1);
Chris Lattner7fa6e662004-10-11 22:52:25 +00001555 ArgVec.push_back(C);
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001556 for (unsigned i = 0; i != NumIdx; ++i)
1557 ArgVec.push_back(cast<Constant>(Idxs[i]));
1558 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Owen Anderson31c36f02009-06-17 20:10:08 +00001559
Owen Andersond03eecd2009-08-04 20:25:11 +00001560 LLVMContextImpl *pImpl = ReqTy->getContext().pImpl;
Owen Andersond03eecd2009-08-04 20:25:11 +00001561 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001562}
1563
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001564Constant *ConstantExpr::getInBoundsGetElementPtrTy(const Type *ReqTy,
1565 Constant *C,
Chris Lattner399ac532009-12-08 05:31:46 +00001566 Value *const *Idxs,
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001567 unsigned NumIdx) {
1568 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
1569 Idxs+NumIdx) ==
1570 cast<PointerType>(ReqTy)->getElementType() &&
1571 "GEP indices invalid!");
1572
1573 if (Constant *FC = ConstantFoldGetElementPtr(
Dan Gohman3bfbc452009-09-11 00:04:14 +00001574 ReqTy->getContext(), C, /*inBounds=*/true,
1575 (Constant**)Idxs, NumIdx))
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001576 return FC; // Fold a few common cases...
1577
1578 assert(isa<PointerType>(C->getType()) &&
1579 "Non-pointer type for constant GetElementPtr expression");
1580 // Look up the constant in the table first to ensure uniqueness
1581 std::vector<Constant*> ArgVec;
1582 ArgVec.reserve(NumIdx+1);
1583 ArgVec.push_back(C);
1584 for (unsigned i = 0; i != NumIdx; ++i)
1585 ArgVec.push_back(cast<Constant>(Idxs[i]));
1586 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec, 0,
1587 GEPOperator::IsInBounds);
1588
1589 LLVMContextImpl *pImpl = ReqTy->getContext().pImpl;
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001590 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
1591}
1592
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001593Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
Owen Anderson04fb7c32009-06-20 00:24:58 +00001594 unsigned NumIdx) {
Chris Lattnered468e372003-10-05 00:17:43 +00001595 // Get the result type of the getelementptr!
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001596 const Type *Ty =
Dan Gohman041e2eb2008-05-15 19:50:34 +00001597 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
Chris Lattnered468e372003-10-05 00:17:43 +00001598 assert(Ty && "GEP indices invalid!");
Christopher Lambfe63fb92007-12-11 08:59:05 +00001599 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
Owen Anderson04fb7c32009-06-20 00:24:58 +00001600 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
Chris Lattner7fa6e662004-10-11 22:52:25 +00001601}
1602
Dan Gohman6e7ad952009-09-03 23:34:49 +00001603Constant *ConstantExpr::getInBoundsGetElementPtr(Constant *C,
1604 Value* const *Idxs,
1605 unsigned NumIdx) {
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001606 // Get the result type of the getelementptr!
1607 const Type *Ty =
1608 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
1609 assert(Ty && "GEP indices invalid!");
1610 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
1611 return getInBoundsGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
Dan Gohman6e7ad952009-09-03 23:34:49 +00001612}
1613
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001614Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
Owen Anderson04fb7c32009-06-20 00:24:58 +00001615 unsigned NumIdx) {
1616 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnered468e372003-10-05 00:17:43 +00001617}
1618
Dan Gohman6e7ad952009-09-03 23:34:49 +00001619Constant *ConstantExpr::getInBoundsGetElementPtr(Constant *C,
1620 Constant* const *Idxs,
1621 unsigned NumIdx) {
1622 return getInBoundsGetElementPtr(C, (Value* const *)Idxs, NumIdx);
1623}
1624
Reid Spencer077d0eb2006-12-04 05:19:50 +00001625Constant *
Nick Lewycky401f3252010-01-21 07:03:21 +00001626ConstantExpr::getICmp(unsigned short pred, Constant *LHS, Constant *RHS) {
Reid Spencer077d0eb2006-12-04 05:19:50 +00001627 assert(LHS->getType() == RHS->getType());
1628 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
1629 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
1630
Owen Anderson0a5372e2009-07-13 04:09:18 +00001631 if (Constant *FC = ConstantFoldCompareInstruction(
Owen Anderson5defacc2009-07-31 17:39:07 +00001632 LHS->getContext(), pred, LHS, RHS))
Reid Spencer077d0eb2006-12-04 05:19:50 +00001633 return FC; // Fold a few common cases...
1634
1635 // Look up the constant in the table first to ensure uniqueness
1636 std::vector<Constant*> ArgVec;
1637 ArgVec.push_back(LHS);
1638 ArgVec.push_back(RHS);
Reid Spencer4fa021a2006-12-24 18:42:29 +00001639 // Get the key type with both the opcode and predicate
Reid Spencer077d0eb2006-12-04 05:19:50 +00001640 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Owen Anderson31c36f02009-06-17 20:10:08 +00001641
Nick Lewycky401f3252010-01-21 07:03:21 +00001642 const Type *ResultTy = Type::getInt1Ty(LHS->getContext());
1643 if (const VectorType *VT = dyn_cast<VectorType>(LHS->getType()))
1644 ResultTy = VectorType::get(ResultTy, VT->getNumElements());
1645
Owen Andersond03eecd2009-08-04 20:25:11 +00001646 LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl;
Nick Lewycky401f3252010-01-21 07:03:21 +00001647 return pImpl->ExprConstants.getOrCreate(ResultTy, Key);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001648}
1649
1650Constant *
Nick Lewycky401f3252010-01-21 07:03:21 +00001651ConstantExpr::getFCmp(unsigned short pred, Constant *LHS, Constant *RHS) {
Reid Spencer077d0eb2006-12-04 05:19:50 +00001652 assert(LHS->getType() == RHS->getType());
1653 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
1654
Owen Anderson0a5372e2009-07-13 04:09:18 +00001655 if (Constant *FC = ConstantFoldCompareInstruction(
Owen Anderson5defacc2009-07-31 17:39:07 +00001656 LHS->getContext(), pred, LHS, RHS))
Reid Spencer077d0eb2006-12-04 05:19:50 +00001657 return FC; // Fold a few common cases...
1658
1659 // Look up the constant in the table first to ensure uniqueness
1660 std::vector<Constant*> ArgVec;
1661 ArgVec.push_back(LHS);
1662 ArgVec.push_back(RHS);
Reid Spencer4fa021a2006-12-24 18:42:29 +00001663 // Get the key type with both the opcode and predicate
Reid Spencer077d0eb2006-12-04 05:19:50 +00001664 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Nick Lewycky401f3252010-01-21 07:03:21 +00001665
1666 const Type *ResultTy = Type::getInt1Ty(LHS->getContext());
1667 if (const VectorType *VT = dyn_cast<VectorType>(LHS->getType()))
1668 ResultTy = VectorType::get(ResultTy, VT->getNumElements());
1669
Owen Andersond03eecd2009-08-04 20:25:11 +00001670 LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl;
Nick Lewycky401f3252010-01-21 07:03:21 +00001671 return pImpl->ExprConstants.getOrCreate(ResultTy, Key);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001672}
1673
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001674Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
1675 Constant *Idx) {
Owen Anderson0a5372e2009-07-13 04:09:18 +00001676 if (Constant *FC = ConstantFoldExtractElementInstruction(
Owen Anderson5defacc2009-07-31 17:39:07 +00001677 ReqTy->getContext(), Val, Idx))
Chris Lattner83738a22009-12-30 20:25:09 +00001678 return FC; // Fold a few common cases.
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001679 // Look up the constant in the table first to ensure uniqueness
1680 std::vector<Constant*> ArgVec(1, Val);
1681 ArgVec.push_back(Idx);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001682 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Owen Anderson31c36f02009-06-17 20:10:08 +00001683
Owen Andersond03eecd2009-08-04 20:25:11 +00001684 LLVMContextImpl *pImpl = ReqTy->getContext().pImpl;
Owen Andersond03eecd2009-08-04 20:25:11 +00001685 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001686}
1687
1688Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001689 assert(isa<VectorType>(Val->getType()) &&
Reid Spencerac9dcb92007-02-15 03:39:18 +00001690 "Tried to create extractelement operation on non-vector type!");
Benjamin Kramer11acaa32010-01-05 20:07:06 +00001691 assert(Idx->getType()->isInteger(32) &&
Reid Spencer3d10b0b2007-01-26 07:37:34 +00001692 "Extractelement index must be i32 type!");
Reid Spencer9d6565a2007-02-15 02:26:10 +00001693 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001694 Val, Idx);
1695}
Chris Lattnered468e372003-10-05 00:17:43 +00001696
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001697Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
1698 Constant *Elt, Constant *Idx) {
Owen Anderson0a5372e2009-07-13 04:09:18 +00001699 if (Constant *FC = ConstantFoldInsertElementInstruction(
Owen Anderson5defacc2009-07-31 17:39:07 +00001700 ReqTy->getContext(), Val, Elt, Idx))
Chris Lattner83738a22009-12-30 20:25:09 +00001701 return FC; // Fold a few common cases.
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001702 // Look up the constant in the table first to ensure uniqueness
1703 std::vector<Constant*> ArgVec(1, Val);
1704 ArgVec.push_back(Elt);
1705 ArgVec.push_back(Idx);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001706 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Owen Anderson31c36f02009-06-17 20:10:08 +00001707
Owen Andersond03eecd2009-08-04 20:25:11 +00001708 LLVMContextImpl *pImpl = ReqTy->getContext().pImpl;
Owen Andersond03eecd2009-08-04 20:25:11 +00001709 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001710}
1711
1712Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1713 Constant *Idx) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001714 assert(isa<VectorType>(Val->getType()) &&
Reid Spencerac9dcb92007-02-15 03:39:18 +00001715 "Tried to create insertelement operation on non-vector type!");
Reid Spencer9d6565a2007-02-15 02:26:10 +00001716 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001717 && "Insertelement types must match!");
Benjamin Kramer11acaa32010-01-05 20:07:06 +00001718 assert(Idx->getType()->isInteger(32) &&
Reid Spencer3d10b0b2007-01-26 07:37:34 +00001719 "Insertelement index must be i32 type!");
Gordon Henriksen699609c2008-08-30 15:41:51 +00001720 return getInsertElementTy(Val->getType(), Val, Elt, Idx);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001721}
1722
Chris Lattner00f10232006-04-08 01:18:18 +00001723Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
1724 Constant *V2, Constant *Mask) {
Owen Anderson0a5372e2009-07-13 04:09:18 +00001725 if (Constant *FC = ConstantFoldShuffleVectorInstruction(
Owen Anderson5defacc2009-07-31 17:39:07 +00001726 ReqTy->getContext(), V1, V2, Mask))
Chris Lattner00f10232006-04-08 01:18:18 +00001727 return FC; // Fold a few common cases...
1728 // Look up the constant in the table first to ensure uniqueness
1729 std::vector<Constant*> ArgVec(1, V1);
1730 ArgVec.push_back(V2);
1731 ArgVec.push_back(Mask);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001732 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Owen Anderson31c36f02009-06-17 20:10:08 +00001733
Owen Andersond03eecd2009-08-04 20:25:11 +00001734 LLVMContextImpl *pImpl = ReqTy->getContext().pImpl;
Owen Andersond03eecd2009-08-04 20:25:11 +00001735 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Chris Lattner00f10232006-04-08 01:18:18 +00001736}
1737
1738Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
1739 Constant *Mask) {
1740 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
1741 "Invalid shuffle vector constant expr operands!");
Nate Begeman0f123cf2009-02-12 21:28:33 +00001742
1743 unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements();
1744 const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
1745 const Type *ShufTy = VectorType::get(EltTy, NElts);
1746 return getShuffleVectorTy(ShufTy, V1, V2, Mask);
Chris Lattner00f10232006-04-08 01:18:18 +00001747}
1748
Dan Gohman041e2eb2008-05-15 19:50:34 +00001749Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg,
1750 Constant *Val,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001751 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman041e2eb2008-05-15 19:50:34 +00001752 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
1753 Idxs+NumIdx) == Val->getType() &&
1754 "insertvalue indices invalid!");
1755 assert(Agg->getType() == ReqTy &&
1756 "insertvalue type invalid!");
Dan Gohmane4569942008-05-23 00:36:11 +00001757 assert(Agg->getType()->isFirstClassType() &&
1758 "Non-first-class type for constant InsertValue expression");
Owen Anderson0a5372e2009-07-13 04:09:18 +00001759 Constant *FC = ConstantFoldInsertValueInstruction(
Owen Anderson5defacc2009-07-31 17:39:07 +00001760 ReqTy->getContext(), Agg, Val, Idxs, NumIdx);
Dan Gohmane0891602008-07-21 23:30:30 +00001761 assert(FC && "InsertValue constant expr couldn't be folded!");
1762 return FC;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001763}
1764
1765Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001766 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohmane4569942008-05-23 00:36:11 +00001767 assert(Agg->getType()->isFirstClassType() &&
1768 "Tried to create insertelement operation on non-first-class type!");
Dan Gohman041e2eb2008-05-15 19:50:34 +00001769
Dan Gohmane4569942008-05-23 00:36:11 +00001770 const Type *ReqTy = Agg->getType();
Devang Patelb6dc9352008-11-03 23:20:04 +00001771#ifndef NDEBUG
Dan Gohmane4569942008-05-23 00:36:11 +00001772 const Type *ValTy =
Dan Gohman041e2eb2008-05-15 19:50:34 +00001773 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
Devang Patelb6dc9352008-11-03 23:20:04 +00001774#endif
Dan Gohmane4569942008-05-23 00:36:11 +00001775 assert(ValTy == Val->getType() && "insertvalue indices invalid!");
Dan Gohman041e2eb2008-05-15 19:50:34 +00001776 return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx);
1777}
1778
1779Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001780 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman041e2eb2008-05-15 19:50:34 +00001781 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
1782 Idxs+NumIdx) == ReqTy &&
1783 "extractvalue indices invalid!");
Dan Gohmane4569942008-05-23 00:36:11 +00001784 assert(Agg->getType()->isFirstClassType() &&
1785 "Non-first-class type for constant extractvalue expression");
Owen Anderson0a5372e2009-07-13 04:09:18 +00001786 Constant *FC = ConstantFoldExtractValueInstruction(
Owen Anderson5defacc2009-07-31 17:39:07 +00001787 ReqTy->getContext(), Agg, Idxs, NumIdx);
Dan Gohmane0891602008-07-21 23:30:30 +00001788 assert(FC && "ExtractValue constant expr couldn't be folded!");
1789 return FC;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001790}
1791
1792Constant *ConstantExpr::getExtractValue(Constant *Agg,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001793 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohmane4569942008-05-23 00:36:11 +00001794 assert(Agg->getType()->isFirstClassType() &&
1795 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman041e2eb2008-05-15 19:50:34 +00001796
1797 const Type *ReqTy =
1798 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
1799 assert(ReqTy && "extractvalue indices invalid!");
1800 return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx);
1801}
1802
Owen Andersonbaf3c402009-07-29 18:55:55 +00001803Constant* ConstantExpr::getNeg(Constant* C) {
1804 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1805 if (C->getType()->isFPOrFPVector())
1806 return getFNeg(C);
1807 assert(C->getType()->isIntOrIntVector() &&
1808 "Cannot NEG a nonintegral value!");
1809 return get(Instruction::Sub,
1810 ConstantFP::getZeroValueForNegation(C->getType()),
1811 C);
1812}
1813
1814Constant* ConstantExpr::getFNeg(Constant* C) {
1815 assert(C->getType()->isFPOrFPVector() &&
1816 "Cannot FNEG a non-floating-point value!");
1817 return get(Instruction::FSub,
1818 ConstantFP::getZeroValueForNegation(C->getType()),
1819 C);
1820}
1821
1822Constant* ConstantExpr::getNot(Constant* C) {
1823 assert(C->getType()->isIntOrIntVector() &&
1824 "Cannot NOT a nonintegral value!");
Owen Andersona7235ea2009-07-31 20:28:14 +00001825 return get(Instruction::Xor, C, Constant::getAllOnesValue(C->getType()));
Owen Andersonbaf3c402009-07-29 18:55:55 +00001826}
1827
1828Constant* ConstantExpr::getAdd(Constant* C1, Constant* C2) {
1829 return get(Instruction::Add, C1, C2);
1830}
1831
1832Constant* ConstantExpr::getFAdd(Constant* C1, Constant* C2) {
1833 return get(Instruction::FAdd, C1, C2);
1834}
1835
1836Constant* ConstantExpr::getSub(Constant* C1, Constant* C2) {
1837 return get(Instruction::Sub, C1, C2);
1838}
1839
1840Constant* ConstantExpr::getFSub(Constant* C1, Constant* C2) {
1841 return get(Instruction::FSub, C1, C2);
1842}
1843
1844Constant* ConstantExpr::getMul(Constant* C1, Constant* C2) {
1845 return get(Instruction::Mul, C1, C2);
1846}
1847
1848Constant* ConstantExpr::getFMul(Constant* C1, Constant* C2) {
1849 return get(Instruction::FMul, C1, C2);
1850}
1851
1852Constant* ConstantExpr::getUDiv(Constant* C1, Constant* C2) {
1853 return get(Instruction::UDiv, C1, C2);
1854}
1855
1856Constant* ConstantExpr::getSDiv(Constant* C1, Constant* C2) {
1857 return get(Instruction::SDiv, C1, C2);
1858}
1859
1860Constant* ConstantExpr::getFDiv(Constant* C1, Constant* C2) {
1861 return get(Instruction::FDiv, C1, C2);
1862}
1863
1864Constant* ConstantExpr::getURem(Constant* C1, Constant* C2) {
1865 return get(Instruction::URem, C1, C2);
1866}
1867
1868Constant* ConstantExpr::getSRem(Constant* C1, Constant* C2) {
1869 return get(Instruction::SRem, C1, C2);
1870}
1871
1872Constant* ConstantExpr::getFRem(Constant* C1, Constant* C2) {
1873 return get(Instruction::FRem, C1, C2);
1874}
1875
1876Constant* ConstantExpr::getAnd(Constant* C1, Constant* C2) {
1877 return get(Instruction::And, C1, C2);
1878}
1879
1880Constant* ConstantExpr::getOr(Constant* C1, Constant* C2) {
1881 return get(Instruction::Or, C1, C2);
1882}
1883
1884Constant* ConstantExpr::getXor(Constant* C1, Constant* C2) {
1885 return get(Instruction::Xor, C1, C2);
1886}
1887
1888Constant* ConstantExpr::getShl(Constant* C1, Constant* C2) {
1889 return get(Instruction::Shl, C1, C2);
1890}
1891
1892Constant* ConstantExpr::getLShr(Constant* C1, Constant* C2) {
1893 return get(Instruction::LShr, C1, C2);
1894}
1895
1896Constant* ConstantExpr::getAShr(Constant* C1, Constant* C2) {
1897 return get(Instruction::AShr, C1, C2);
1898}
1899
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001900// destroyConstant - Remove the constant from the constant table...
1901//
Owen Anderson04fb7c32009-06-20 00:24:58 +00001902void ConstantExpr::destroyConstant() {
Chris Lattner83738a22009-12-30 20:25:09 +00001903 getType()->getContext().pImpl->ExprConstants.remove(this);
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001904 destroyConstantImpl();
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001905}
1906
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001907const char *ConstantExpr::getOpcodeName() const {
1908 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001909}
Reid Spencer1c9c8e62004-07-17 23:48:33 +00001910
Chris Lattner5cbade92005-10-03 21:58:36 +00001911//===----------------------------------------------------------------------===//
1912// replaceUsesOfWithOnConstant implementations
1913
Chris Lattner54984052007-08-21 00:55:23 +00001914/// replaceUsesOfWithOnConstant - Update this constant array to change uses of
1915/// 'From' to be uses of 'To'. This must update the uniquing data structures
1916/// etc.
1917///
1918/// Note that we intentionally replace all uses of From with To here. Consider
1919/// a large array that uses 'From' 1000 times. By handling this case all here,
1920/// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that
1921/// single invocation handles all 1000 uses. Handling them one at a time would
1922/// work, but would be really slow because it would have to unique each updated
1923/// array instance.
Chris Lattner2ee11ec2009-10-28 00:01:44 +00001924///
Chris Lattner5cbade92005-10-03 21:58:36 +00001925void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00001926 Use *U) {
Owen Anderson1fd70962009-07-28 18:32:17 +00001927 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
1928 Constant *ToC = cast<Constant>(To);
1929
1930 LLVMContext &Context = getType()->getContext();
1931 LLVMContextImpl *pImpl = Context.pImpl;
1932
Dan Gohmane3394d42009-09-15 15:58:07 +00001933 std::pair<LLVMContextImpl::ArrayConstantsTy::MapKey, ConstantArray*> Lookup;
Owen Anderson1fd70962009-07-28 18:32:17 +00001934 Lookup.first.first = getType();
1935 Lookup.second = this;
1936
1937 std::vector<Constant*> &Values = Lookup.first.second;
1938 Values.reserve(getNumOperands()); // Build replacement array.
1939
1940 // Fill values with the modified operands of the constant array. Also,
1941 // compute whether this turns into an all-zeros array.
1942 bool isAllZeros = false;
1943 unsigned NumUpdated = 0;
1944 if (!ToC->isNullValue()) {
1945 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1946 Constant *Val = cast<Constant>(O->get());
1947 if (Val == From) {
1948 Val = ToC;
1949 ++NumUpdated;
1950 }
1951 Values.push_back(Val);
1952 }
1953 } else {
1954 isAllZeros = true;
1955 for (Use *O = OperandList, *E = OperandList+getNumOperands();O != E; ++O) {
1956 Constant *Val = cast<Constant>(O->get());
1957 if (Val == From) {
1958 Val = ToC;
1959 ++NumUpdated;
1960 }
1961 Values.push_back(Val);
1962 if (isAllZeros) isAllZeros = Val->isNullValue();
1963 }
1964 }
1965
1966 Constant *Replacement = 0;
1967 if (isAllZeros) {
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001968 Replacement = ConstantAggregateZero::get(getType());
Owen Anderson1fd70962009-07-28 18:32:17 +00001969 } else {
1970 // Check to see if we have this array type already.
Owen Anderson1fd70962009-07-28 18:32:17 +00001971 bool Exists;
1972 LLVMContextImpl::ArrayConstantsTy::MapTy::iterator I =
1973 pImpl->ArrayConstants.InsertOrGetItem(Lookup, Exists);
1974
1975 if (Exists) {
Devang Patel5f4ac842009-09-03 01:39:20 +00001976 Replacement = I->second;
Owen Anderson1fd70962009-07-28 18:32:17 +00001977 } else {
1978 // Okay, the new shape doesn't exist in the system yet. Instead of
1979 // creating a new constant array, inserting it, replaceallusesof'ing the
1980 // old with the new, then deleting the old... just update the current one
1981 // in place!
1982 pImpl->ArrayConstants.MoveConstantToNewSlot(this, I);
1983
1984 // Update to the new value. Optimize for the case when we have a single
1985 // operand that we're changing, but handle bulk updates efficiently.
1986 if (NumUpdated == 1) {
1987 unsigned OperandToUpdate = U - OperandList;
1988 assert(getOperand(OperandToUpdate) == From &&
1989 "ReplaceAllUsesWith broken!");
1990 setOperand(OperandToUpdate, ToC);
1991 } else {
1992 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1993 if (getOperand(i) == From)
1994 setOperand(i, ToC);
1995 }
1996 return;
1997 }
1998 }
Chris Lattnercea141f2005-10-03 22:51:37 +00001999
2000 // Otherwise, I do need to replace this with an existing value.
Chris Lattner5cbade92005-10-03 21:58:36 +00002001 assert(Replacement != this && "I didn't contain From!");
2002
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002003 // Everyone using this now uses the replacement.
2004 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattner5cbade92005-10-03 21:58:36 +00002005
2006 // Delete the old constant!
2007 destroyConstant();
2008}
2009
2010void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002011 Use *U) {
Owen Anderson8fa33382009-07-27 22:29:26 +00002012 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2013 Constant *ToC = cast<Constant>(To);
2014
2015 unsigned OperandToUpdate = U-OperandList;
2016 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
2017
Dan Gohmane3394d42009-09-15 15:58:07 +00002018 std::pair<LLVMContextImpl::StructConstantsTy::MapKey, ConstantStruct*> Lookup;
Owen Anderson8fa33382009-07-27 22:29:26 +00002019 Lookup.first.first = getType();
2020 Lookup.second = this;
2021 std::vector<Constant*> &Values = Lookup.first.second;
2022 Values.reserve(getNumOperands()); // Build replacement struct.
2023
2024
2025 // Fill values with the modified operands of the constant struct. Also,
2026 // compute whether this turns into an all-zeros struct.
2027 bool isAllZeros = false;
2028 if (!ToC->isNullValue()) {
2029 for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O)
2030 Values.push_back(cast<Constant>(O->get()));
2031 } else {
2032 isAllZeros = true;
2033 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2034 Constant *Val = cast<Constant>(O->get());
2035 Values.push_back(Val);
2036 if (isAllZeros) isAllZeros = Val->isNullValue();
2037 }
2038 }
2039 Values[OperandToUpdate] = ToC;
2040
2041 LLVMContext &Context = getType()->getContext();
2042 LLVMContextImpl *pImpl = Context.pImpl;
2043
2044 Constant *Replacement = 0;
2045 if (isAllZeros) {
Owen Anderson9e9a0d52009-07-30 23:03:37 +00002046 Replacement = ConstantAggregateZero::get(getType());
Owen Anderson8fa33382009-07-27 22:29:26 +00002047 } else {
2048 // Check to see if we have this array type already.
Owen Anderson8fa33382009-07-27 22:29:26 +00002049 bool Exists;
2050 LLVMContextImpl::StructConstantsTy::MapTy::iterator I =
2051 pImpl->StructConstants.InsertOrGetItem(Lookup, Exists);
2052
2053 if (Exists) {
Devang Patel5f4ac842009-09-03 01:39:20 +00002054 Replacement = I->second;
Owen Anderson8fa33382009-07-27 22:29:26 +00002055 } else {
2056 // Okay, the new shape doesn't exist in the system yet. Instead of
2057 // creating a new constant struct, inserting it, replaceallusesof'ing the
2058 // old with the new, then deleting the old... just update the current one
2059 // in place!
2060 pImpl->StructConstants.MoveConstantToNewSlot(this, I);
2061
2062 // Update to the new value.
2063 setOperand(OperandToUpdate, ToC);
2064 return;
2065 }
2066 }
2067
2068 assert(Replacement != this && "I didn't contain From!");
Chris Lattner5cbade92005-10-03 21:58:36 +00002069
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002070 // Everyone using this now uses the replacement.
2071 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattner5cbade92005-10-03 21:58:36 +00002072
2073 // Delete the old constant!
2074 destroyConstant();
2075}
2076
Reid Spencer9d6565a2007-02-15 02:26:10 +00002077void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002078 Use *U) {
Chris Lattner5cbade92005-10-03 21:58:36 +00002079 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2080
2081 std::vector<Constant*> Values;
2082 Values.reserve(getNumOperands()); // Build replacement array...
2083 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2084 Constant *Val = getOperand(i);
2085 if (Val == From) Val = cast<Constant>(To);
2086 Values.push_back(Val);
2087 }
2088
Owen Andersonaf7ec972009-07-28 21:19:26 +00002089 Constant *Replacement = get(getType(), Values);
Chris Lattner5cbade92005-10-03 21:58:36 +00002090 assert(Replacement != this && "I didn't contain From!");
2091
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002092 // Everyone using this now uses the replacement.
2093 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattner5cbade92005-10-03 21:58:36 +00002094
2095 // Delete the old constant!
2096 destroyConstant();
2097}
2098
2099void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002100 Use *U) {
Chris Lattner5cbade92005-10-03 21:58:36 +00002101 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2102 Constant *To = cast<Constant>(ToV);
2103
2104 Constant *Replacement = 0;
2105 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerf9021ff2007-02-19 20:01:23 +00002106 SmallVector<Constant*, 8> Indices;
Chris Lattner5cbade92005-10-03 21:58:36 +00002107 Constant *Pointer = getOperand(0);
2108 Indices.reserve(getNumOperands()-1);
2109 if (Pointer == From) Pointer = To;
2110
2111 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2112 Constant *Val = getOperand(i);
2113 if (Val == From) Val = To;
2114 Indices.push_back(Val);
2115 }
Chris Lattnerf9021ff2007-02-19 20:01:23 +00002116 Replacement = ConstantExpr::getGetElementPtr(Pointer,
2117 &Indices[0], Indices.size());
Dan Gohman041e2eb2008-05-15 19:50:34 +00002118 } else if (getOpcode() == Instruction::ExtractValue) {
Dan Gohman041e2eb2008-05-15 19:50:34 +00002119 Constant *Agg = getOperand(0);
Dan Gohman041e2eb2008-05-15 19:50:34 +00002120 if (Agg == From) Agg = To;
2121
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002122 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman041e2eb2008-05-15 19:50:34 +00002123 Replacement = ConstantExpr::getExtractValue(Agg,
2124 &Indices[0], Indices.size());
2125 } else if (getOpcode() == Instruction::InsertValue) {
Dan Gohman041e2eb2008-05-15 19:50:34 +00002126 Constant *Agg = getOperand(0);
2127 Constant *Val = getOperand(1);
Dan Gohman041e2eb2008-05-15 19:50:34 +00002128 if (Agg == From) Agg = To;
2129 if (Val == From) Val = To;
2130
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002131 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman041e2eb2008-05-15 19:50:34 +00002132 Replacement = ConstantExpr::getInsertValue(Agg, Val,
2133 &Indices[0], Indices.size());
Reid Spencer3da59db2006-11-27 01:05:10 +00002134 } else if (isCast()) {
Chris Lattner5cbade92005-10-03 21:58:36 +00002135 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer3da59db2006-11-27 01:05:10 +00002136 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattner5cbade92005-10-03 21:58:36 +00002137 } else if (getOpcode() == Instruction::Select) {
2138 Constant *C1 = getOperand(0);
2139 Constant *C2 = getOperand(1);
2140 Constant *C3 = getOperand(2);
2141 if (C1 == From) C1 = To;
2142 if (C2 == From) C2 = To;
2143 if (C3 == From) C3 = To;
2144 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00002145 } else if (getOpcode() == Instruction::ExtractElement) {
2146 Constant *C1 = getOperand(0);
2147 Constant *C2 = getOperand(1);
2148 if (C1 == From) C1 = To;
2149 if (C2 == From) C2 = To;
2150 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattner42b55802006-04-08 05:09:48 +00002151 } else if (getOpcode() == Instruction::InsertElement) {
2152 Constant *C1 = getOperand(0);
2153 Constant *C2 = getOperand(1);
2154 Constant *C3 = getOperand(1);
2155 if (C1 == From) C1 = To;
2156 if (C2 == From) C2 = To;
2157 if (C3 == From) C3 = To;
2158 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2159 } else if (getOpcode() == Instruction::ShuffleVector) {
2160 Constant *C1 = getOperand(0);
2161 Constant *C2 = getOperand(1);
2162 Constant *C3 = getOperand(2);
2163 if (C1 == From) C1 = To;
2164 if (C2 == From) C2 = To;
2165 if (C3 == From) C3 = To;
2166 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spencer077d0eb2006-12-04 05:19:50 +00002167 } else if (isCompare()) {
2168 Constant *C1 = getOperand(0);
2169 Constant *C2 = getOperand(1);
2170 if (C1 == From) C1 = To;
2171 if (C2 == From) C2 = To;
2172 if (getOpcode() == Instruction::ICmp)
2173 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
Chris Lattnerbbedb0e2008-07-14 05:17:31 +00002174 else {
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002175 assert(getOpcode() == Instruction::FCmp);
2176 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnerbbedb0e2008-07-14 05:17:31 +00002177 }
Chris Lattner5cbade92005-10-03 21:58:36 +00002178 } else if (getNumOperands() == 2) {
2179 Constant *C1 = getOperand(0);
2180 Constant *C2 = getOperand(1);
2181 if (C1 == From) C1 = To;
2182 if (C2 == From) C2 = To;
Chris Lattnercafe9bb2009-12-29 02:14:09 +00002183 Replacement = ConstantExpr::get(getOpcode(), C1, C2, SubclassOptionalData);
Chris Lattner5cbade92005-10-03 21:58:36 +00002184 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00002185 llvm_unreachable("Unknown ConstantExpr type!");
Chris Lattner5cbade92005-10-03 21:58:36 +00002186 return;
2187 }
2188
2189 assert(Replacement != this && "I didn't contain From!");
2190
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002191 // Everyone using this now uses the replacement.
2192 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattner5cbade92005-10-03 21:58:36 +00002193
2194 // Delete the old constant!
2195 destroyConstant();
Matthijs Kooijman10b9de62008-07-03 07:46:41 +00002196}