blob: 8cc6e94a4e818f06a180b2e1e856553815e7a04f [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.
Chris Lattnerb29d5962010-02-01 20:48:08 +0000231void Constant::getVectorElements(SmallVectorImpl<Constant*> &Elts) const {
Chris Lattner86381442008-07-10 00:28:11 +0000232 assert(isa<VectorType>(getType()) && "Not a vector constant!");
233
234 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) {
235 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i)
236 Elts.push_back(CV->getOperand(i));
237 return;
238 }
239
240 const VectorType *VT = cast<VectorType>(getType());
241 if (isa<ConstantAggregateZero>(this)) {
242 Elts.assign(VT->getNumElements(),
Owen Andersona7235ea2009-07-31 20:28:14 +0000243 Constant::getNullValue(VT->getElementType()));
Chris Lattner86381442008-07-10 00:28:11 +0000244 return;
245 }
246
Chris Lattner071aade2008-07-14 05:10:41 +0000247 if (isa<UndefValue>(this)) {
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000248 Elts.assign(VT->getNumElements(), UndefValue::get(VT->getElementType()));
Chris Lattner071aade2008-07-14 05:10:41 +0000249 return;
250 }
251
252 // Unknown type, must be constant expr etc.
Chris Lattner86381442008-07-10 00:28:11 +0000253}
254
255
256
Chris Lattner00950542001-06-06 20:29:01 +0000257//===----------------------------------------------------------------------===//
Chris Lattner6b6f6ba2007-02-20 06:39:57 +0000258// ConstantInt
Chris Lattner00950542001-06-06 20:29:01 +0000259//===----------------------------------------------------------------------===//
260
Reid Spencer532d0ce2007-02-26 23:54:03 +0000261ConstantInt::ConstantInt(const IntegerType *Ty, const APInt& V)
Chris Lattnereb41bdd2007-02-20 05:55:46 +0000262 : Constant(Ty, ConstantIntVal, 0, 0), Val(V) {
Reid Spencer532d0ce2007-02-26 23:54:03 +0000263 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
Chris Lattner00950542001-06-06 20:29:01 +0000264}
265
Owen Anderson5defacc2009-07-31 17:39:07 +0000266ConstantInt* ConstantInt::getTrue(LLVMContext &Context) {
267 LLVMContextImpl *pImpl = Context.pImpl;
Owen Anderson5defacc2009-07-31 17:39:07 +0000268 if (pImpl->TheTrueVal)
269 return pImpl->TheTrueVal;
270 else
Owen Anderson1d0be152009-08-13 21:58:54 +0000271 return (pImpl->TheTrueVal =
272 ConstantInt::get(IntegerType::get(Context, 1), 1));
Owen Anderson5defacc2009-07-31 17:39:07 +0000273}
274
275ConstantInt* ConstantInt::getFalse(LLVMContext &Context) {
276 LLVMContextImpl *pImpl = Context.pImpl;
Owen Anderson5defacc2009-07-31 17:39:07 +0000277 if (pImpl->TheFalseVal)
278 return pImpl->TheFalseVal;
279 else
Owen Anderson1d0be152009-08-13 21:58:54 +0000280 return (pImpl->TheFalseVal =
281 ConstantInt::get(IntegerType::get(Context, 1), 0));
Owen Anderson5defacc2009-07-31 17:39:07 +0000282}
283
284
Owen Andersoneed707b2009-07-24 23:12:02 +0000285// Get a ConstantInt from an APInt. Note that the value stored in the DenseMap
286// as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the
287// operator== and operator!= to ensure that the DenseMap doesn't attempt to
288// compare APInt's of different widths, which would violate an APInt class
289// invariant which generates an assertion.
290ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt& V) {
291 // Get the corresponding integer type for the bit width of the value.
Owen Anderson1d0be152009-08-13 21:58:54 +0000292 const IntegerType *ITy = IntegerType::get(Context, V.getBitWidth());
Owen Andersoneed707b2009-07-24 23:12:02 +0000293 // get an existing value or the insertion position
294 DenseMapAPIntKeyInfo::KeyTy Key(V, ITy);
Owen Andersoneed707b2009-07-24 23:12:02 +0000295 ConstantInt *&Slot = Context.pImpl->IntConstants[Key];
Owen Anderson59d5aac2009-10-19 20:11:52 +0000296 if (!Slot) Slot = new ConstantInt(ITy, V);
297 return Slot;
Owen Andersoneed707b2009-07-24 23:12:02 +0000298}
299
300Constant* ConstantInt::get(const Type* Ty, uint64_t V, bool isSigned) {
301 Constant *C = get(cast<IntegerType>(Ty->getScalarType()),
302 V, isSigned);
303
304 // For vectors, broadcast the value.
305 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
Owen Andersonaf7ec972009-07-28 21:19:26 +0000306 return ConstantVector::get(
Owen Andersoneed707b2009-07-24 23:12:02 +0000307 std::vector<Constant *>(VTy->getNumElements(), C));
308
309 return C;
310}
311
312ConstantInt* ConstantInt::get(const IntegerType* Ty, uint64_t V,
313 bool isSigned) {
314 return get(Ty->getContext(), APInt(Ty->getBitWidth(), V, isSigned));
315}
316
317ConstantInt* ConstantInt::getSigned(const IntegerType* Ty, int64_t V) {
318 return get(Ty, V, true);
319}
320
321Constant *ConstantInt::getSigned(const Type *Ty, int64_t V) {
322 return get(Ty, V, true);
323}
324
325Constant* ConstantInt::get(const Type* Ty, const APInt& V) {
326 ConstantInt *C = get(Ty->getContext(), V);
327 assert(C->getType() == Ty->getScalarType() &&
328 "ConstantInt type doesn't match the type implied by its value!");
329
330 // For vectors, broadcast the value.
331 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
Owen Andersonaf7ec972009-07-28 21:19:26 +0000332 return ConstantVector::get(
Owen Andersoneed707b2009-07-24 23:12:02 +0000333 std::vector<Constant *>(VTy->getNumElements(), C));
334
335 return C;
336}
337
Daniel Dunbar2928c832009-11-06 10:58:06 +0000338ConstantInt* ConstantInt::get(const IntegerType* Ty, StringRef Str,
Erick Tryzelaar0e81f662009-08-16 23:36:33 +0000339 uint8_t radix) {
340 return get(Ty->getContext(), APInt(Ty->getBitWidth(), Str, radix));
341}
342
Chris Lattner6b6f6ba2007-02-20 06:39:57 +0000343//===----------------------------------------------------------------------===//
Chris Lattner9b4ee0c2007-02-20 07:17:17 +0000344// ConstantFP
Chris Lattner6b6f6ba2007-02-20 06:39:57 +0000345//===----------------------------------------------------------------------===//
346
Rafael Espindola87d1f472009-07-15 17:40:42 +0000347static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000348 if (Ty->isFloatTy())
Rafael Espindola87d1f472009-07-15 17:40:42 +0000349 return &APFloat::IEEEsingle;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000350 if (Ty->isDoubleTy())
Rafael Espindola87d1f472009-07-15 17:40:42 +0000351 return &APFloat::IEEEdouble;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000352 if (Ty->isX86_FP80Ty())
Rafael Espindola87d1f472009-07-15 17:40:42 +0000353 return &APFloat::x87DoubleExtended;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000354 else if (Ty->isFP128Ty())
Rafael Espindola87d1f472009-07-15 17:40:42 +0000355 return &APFloat::IEEEquad;
356
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000357 assert(Ty->isPPC_FP128Ty() && "Unknown FP format");
Rafael Espindola87d1f472009-07-15 17:40:42 +0000358 return &APFloat::PPCDoubleDouble;
359}
360
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000361/// get() - This returns a constant fp for the specified value in the
362/// specified type. This should only be used for simple constant values like
363/// 2.0/1.0 etc, that are known-valid both as double and as the target format.
364Constant* ConstantFP::get(const Type* Ty, double V) {
365 LLVMContext &Context = Ty->getContext();
366
367 APFloat FV(V);
368 bool ignored;
369 FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
370 APFloat::rmNearestTiesToEven, &ignored);
371 Constant *C = get(Context, FV);
372
373 // For vectors, broadcast the value.
374 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
Owen Andersonaf7ec972009-07-28 21:19:26 +0000375 return ConstantVector::get(
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000376 std::vector<Constant *>(VTy->getNumElements(), C));
377
378 return C;
379}
380
Erick Tryzelaar0e81f662009-08-16 23:36:33 +0000381
Daniel Dunbar2928c832009-11-06 10:58:06 +0000382Constant* ConstantFP::get(const Type* Ty, StringRef Str) {
Erick Tryzelaar0e81f662009-08-16 23:36:33 +0000383 LLVMContext &Context = Ty->getContext();
384
385 APFloat FV(*TypeToFloatSemantics(Ty->getScalarType()), Str);
386 Constant *C = get(Context, FV);
387
388 // For vectors, broadcast the value.
389 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
390 return ConstantVector::get(
391 std::vector<Constant *>(VTy->getNumElements(), C));
392
393 return C;
394}
395
396
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000397ConstantFP* ConstantFP::getNegativeZero(const Type* Ty) {
398 LLVMContext &Context = Ty->getContext();
Owen Andersona7235ea2009-07-31 20:28:14 +0000399 APFloat apf = cast <ConstantFP>(Constant::getNullValue(Ty))->getValueAPF();
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000400 apf.changeSign();
401 return get(Context, apf);
402}
403
404
405Constant* ConstantFP::getZeroValueForNegation(const Type* Ty) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000406 if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
407 if (PTy->getElementType()->isFloatingPoint()) {
408 std::vector<Constant*> zeros(PTy->getNumElements(),
409 getNegativeZero(PTy->getElementType()));
Owen Andersonaf7ec972009-07-28 21:19:26 +0000410 return ConstantVector::get(PTy, zeros);
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000411 }
412
413 if (Ty->isFloatingPoint())
414 return getNegativeZero(Ty);
415
Owen Andersona7235ea2009-07-31 20:28:14 +0000416 return Constant::getNullValue(Ty);
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000417}
418
419
420// ConstantFP accessors.
421ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) {
422 DenseMapAPFloatKeyInfo::KeyTy Key(V);
423
424 LLVMContextImpl* pImpl = Context.pImpl;
425
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000426 ConstantFP *&Slot = pImpl->FPConstants[Key];
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000427
428 if (!Slot) {
Owen Anderson59d5aac2009-10-19 20:11:52 +0000429 const Type *Ty;
430 if (&V.getSemantics() == &APFloat::IEEEsingle)
431 Ty = Type::getFloatTy(Context);
432 else if (&V.getSemantics() == &APFloat::IEEEdouble)
433 Ty = Type::getDoubleTy(Context);
434 else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
435 Ty = Type::getX86_FP80Ty(Context);
436 else if (&V.getSemantics() == &APFloat::IEEEquad)
437 Ty = Type::getFP128Ty(Context);
438 else {
439 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble &&
440 "Unknown FP format");
441 Ty = Type::getPPC_FP128Ty(Context);
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000442 }
Owen Anderson59d5aac2009-10-19 20:11:52 +0000443 Slot = new ConstantFP(Ty, V);
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000444 }
445
446 return Slot;
447}
448
Dan Gohmana23643d2009-09-25 23:40:21 +0000449ConstantFP *ConstantFP::getInfinity(const Type *Ty, bool Negative) {
Dan Gohmanf344f7f2009-09-25 23:00:48 +0000450 const fltSemantics &Semantics = *TypeToFloatSemantics(Ty);
451 return ConstantFP::get(Ty->getContext(),
452 APFloat::getInf(Semantics, Negative));
453}
454
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000455ConstantFP::ConstantFP(const Type *Ty, const APFloat& V)
456 : Constant(Ty, ConstantFPVal, 0, 0), Val(V) {
Chris Lattner288e78f2008-04-09 06:38:30 +0000457 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
458 "FP type Mismatch");
Chris Lattner00950542001-06-06 20:29:01 +0000459}
460
Chris Lattner9b4ee0c2007-02-20 07:17:17 +0000461bool ConstantFP::isNullValue() const {
Dale Johannesen343e7702007-08-24 00:56:33 +0000462 return Val.isZero() && !Val.isNegative();
Chris Lattner9b4ee0c2007-02-20 07:17:17 +0000463}
464
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000465bool ConstantFP::isExactlyValue(const APFloat& V) const {
466 return Val.bitwiseIsEqual(V);
Chris Lattner9b4ee0c2007-02-20 07:17:17 +0000467}
468
Chris Lattner9b4ee0c2007-02-20 07:17:17 +0000469//===----------------------------------------------------------------------===//
470// ConstantXXX Classes
471//===----------------------------------------------------------------------===//
472
473
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000474ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnere4671472005-01-29 00:34:39 +0000475 const std::vector<Constant*> &V)
Gabor Greifefe65362008-05-10 08:32:32 +0000476 : Constant(T, ConstantArrayVal,
477 OperandTraits<ConstantArray>::op_end(this) - V.size(),
478 V.size()) {
Alkis Evlogimenose0de1d62004-09-15 02:32:15 +0000479 assert(V.size() == T->getNumElements() &&
480 "Invalid initializer vector for constant array");
Chris Lattnere4671472005-01-29 00:34:39 +0000481 Use *OL = OperandList;
Chris Lattnerdfdd6c52005-10-03 21:56:24 +0000482 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
483 I != E; ++I, ++OL) {
Chris Lattner71abaab2005-10-07 05:23:36 +0000484 Constant *C = *I;
Nick Lewyckyb13105f2009-10-03 19:30:43 +0000485 assert(C->getType() == T->getElementType() &&
Alkis Evlogimenoscad90ad2004-09-10 04:16:59 +0000486 "Initializer for array element doesn't match array element type!");
Gabor Greif6c80c382008-05-26 21:33:52 +0000487 *OL = C;
Chris Lattner00950542001-06-06 20:29:01 +0000488 }
489}
490
Owen Anderson1fd70962009-07-28 18:32:17 +0000491Constant *ConstantArray::get(const ArrayType *Ty,
492 const std::vector<Constant*> &V) {
Jeffrey Yasskin1fb613c2009-09-30 21:08:08 +0000493 for (unsigned i = 0, e = V.size(); i != e; ++i) {
494 assert(V[i]->getType() == Ty->getElementType() &&
495 "Wrong type in array element initializer");
496 }
Owen Anderson1fd70962009-07-28 18:32:17 +0000497 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
498 // If this is an all-zero array, return a ConstantAggregateZero object
499 if (!V.empty()) {
500 Constant *C = V[0];
Chris Lattner83738a22009-12-30 20:25:09 +0000501 if (!C->isNullValue())
Owen Anderson1fd70962009-07-28 18:32:17 +0000502 return pImpl->ArrayConstants.getOrCreate(Ty, V);
Chris Lattner83738a22009-12-30 20:25:09 +0000503
Owen Anderson1fd70962009-07-28 18:32:17 +0000504 for (unsigned i = 1, e = V.size(); i != e; ++i)
Chris Lattner83738a22009-12-30 20:25:09 +0000505 if (V[i] != C)
Owen Anderson1fd70962009-07-28 18:32:17 +0000506 return pImpl->ArrayConstants.getOrCreate(Ty, V);
Owen Anderson1fd70962009-07-28 18:32:17 +0000507 }
508
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000509 return ConstantAggregateZero::get(Ty);
Owen Anderson1fd70962009-07-28 18:32:17 +0000510}
511
512
513Constant* ConstantArray::get(const ArrayType* T, Constant* const* Vals,
514 unsigned NumVals) {
515 // FIXME: make this the primary ctor method.
516 return get(T, std::vector<Constant*>(Vals, Vals+NumVals));
517}
518
519/// ConstantArray::get(const string&) - Return an array that is initialized to
520/// contain the specified string. If length is zero then a null terminator is
521/// added to the specified string so that it may be used in a natural way.
522/// Otherwise, the length parameter specifies how much of the string to use
523/// and it won't be null terminated.
524///
Daniel Dunbar2928c832009-11-06 10:58:06 +0000525Constant* ConstantArray::get(LLVMContext &Context, StringRef Str,
Owen Anderson1d0be152009-08-13 21:58:54 +0000526 bool AddNull) {
Owen Anderson1fd70962009-07-28 18:32:17 +0000527 std::vector<Constant*> ElementVals;
528 for (unsigned i = 0; i < Str.size(); ++i)
Owen Anderson1d0be152009-08-13 21:58:54 +0000529 ElementVals.push_back(ConstantInt::get(Type::getInt8Ty(Context), Str[i]));
Owen Anderson1fd70962009-07-28 18:32:17 +0000530
531 // Add a null terminator to the string...
532 if (AddNull) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000533 ElementVals.push_back(ConstantInt::get(Type::getInt8Ty(Context), 0));
Owen Anderson1fd70962009-07-28 18:32:17 +0000534 }
535
Owen Anderson1d0be152009-08-13 21:58:54 +0000536 ArrayType *ATy = ArrayType::get(Type::getInt8Ty(Context), ElementVals.size());
Owen Anderson1fd70962009-07-28 18:32:17 +0000537 return get(ATy, ElementVals);
538}
539
540
Chris Lattnere4671472005-01-29 00:34:39 +0000541
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000542ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnere4671472005-01-29 00:34:39 +0000543 const std::vector<Constant*> &V)
Gabor Greifefe65362008-05-10 08:32:32 +0000544 : Constant(T, ConstantStructVal,
545 OperandTraits<ConstantStruct>::op_end(this) - V.size(),
546 V.size()) {
Chris Lattnerd21cd802004-02-09 04:37:31 +0000547 assert(V.size() == T->getNumElements() &&
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000548 "Invalid initializer vector for constant structure");
Chris Lattnere4671472005-01-29 00:34:39 +0000549 Use *OL = OperandList;
Chris Lattnerdfdd6c52005-10-03 21:56:24 +0000550 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
551 I != E; ++I, ++OL) {
Chris Lattner71abaab2005-10-07 05:23:36 +0000552 Constant *C = *I;
Nick Lewyckyb13105f2009-10-03 19:30:43 +0000553 assert(C->getType() == T->getElementType(I-V.begin()) &&
Chris Lattnerb8438892003-06-02 17:42:47 +0000554 "Initializer for struct element doesn't match struct element type!");
Gabor Greif6c80c382008-05-26 21:33:52 +0000555 *OL = C;
Chris Lattner00950542001-06-06 20:29:01 +0000556 }
557}
558
Owen Anderson8fa33382009-07-27 22:29:26 +0000559// ConstantStruct accessors.
560Constant* ConstantStruct::get(const StructType* T,
561 const std::vector<Constant*>& V) {
562 LLVMContextImpl* pImpl = T->getContext().pImpl;
563
564 // Create a ConstantAggregateZero value if all elements are zeros...
565 for (unsigned i = 0, e = V.size(); i != e; ++i)
566 if (!V[i]->isNullValue())
Owen Anderson8fa33382009-07-27 22:29:26 +0000567 return pImpl->StructConstants.getOrCreate(T, V);
568
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000569 return ConstantAggregateZero::get(T);
Owen Anderson8fa33382009-07-27 22:29:26 +0000570}
571
Owen Andersond7f2a6c2009-08-05 23:16:16 +0000572Constant* ConstantStruct::get(LLVMContext &Context,
573 const std::vector<Constant*>& V, bool packed) {
Owen Anderson8fa33382009-07-27 22:29:26 +0000574 std::vector<const Type*> StructEls;
575 StructEls.reserve(V.size());
576 for (unsigned i = 0, e = V.size(); i != e; ++i)
577 StructEls.push_back(V[i]->getType());
Owen Andersond7f2a6c2009-08-05 23:16:16 +0000578 return get(StructType::get(Context, StructEls, packed), V);
Owen Anderson8fa33382009-07-27 22:29:26 +0000579}
580
Owen Andersond7f2a6c2009-08-05 23:16:16 +0000581Constant* ConstantStruct::get(LLVMContext &Context,
582 Constant* const *Vals, unsigned NumVals,
Owen Anderson8fa33382009-07-27 22:29:26 +0000583 bool Packed) {
584 // FIXME: make this the primary ctor method.
Owen Andersond7f2a6c2009-08-05 23:16:16 +0000585 return get(Context, std::vector<Constant*>(Vals, Vals+NumVals), Packed);
Owen Anderson8fa33382009-07-27 22:29:26 +0000586}
Chris Lattnere4671472005-01-29 00:34:39 +0000587
Chris Lattnerfdfeb692010-02-12 20:49:41 +0000588ConstantUnion::ConstantUnion(const UnionType *T, Constant* V)
589 : Constant(T, ConstantUnionVal,
590 OperandTraits<ConstantUnion>::op_end(this) - 1, 1) {
591 Use *OL = OperandList;
592 assert(T->getElementTypeIndex(V->getType()) >= 0 &&
593 "Initializer for union element isn't a member of union type!");
594 *OL = V;
595}
596
597// ConstantUnion accessors.
598Constant* ConstantUnion::get(const UnionType* T, Constant* V) {
599 LLVMContextImpl* pImpl = T->getContext().pImpl;
600
601 // Create a ConstantAggregateZero value if all elements are zeros...
602 if (!V->isNullValue())
603 return pImpl->UnionConstants.getOrCreate(T, V);
604
605 return ConstantAggregateZero::get(T);
606}
607
608
Reid Spencer9d6565a2007-02-15 02:26:10 +0000609ConstantVector::ConstantVector(const VectorType *T,
Chris Lattnere4671472005-01-29 00:34:39 +0000610 const std::vector<Constant*> &V)
Gabor Greifefe65362008-05-10 08:32:32 +0000611 : Constant(T, ConstantVectorVal,
612 OperandTraits<ConstantVector>::op_end(this) - V.size(),
613 V.size()) {
Chris Lattnere4671472005-01-29 00:34:39 +0000614 Use *OL = OperandList;
Chris Lattnerdfdd6c52005-10-03 21:56:24 +0000615 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
616 I != E; ++I, ++OL) {
Chris Lattner71abaab2005-10-07 05:23:36 +0000617 Constant *C = *I;
Nick Lewyckyb13105f2009-10-03 19:30:43 +0000618 assert(C->getType() == T->getElementType() &&
Dan Gohmanfa73ea22007-05-24 14:36:04 +0000619 "Initializer for vector element doesn't match vector element type!");
Gabor Greif6c80c382008-05-26 21:33:52 +0000620 *OL = C;
Brian Gaeke715c90b2004-08-20 06:00:58 +0000621 }
622}
623
Owen Andersonaf7ec972009-07-28 21:19:26 +0000624// ConstantVector accessors.
625Constant* ConstantVector::get(const VectorType* T,
626 const std::vector<Constant*>& V) {
627 assert(!V.empty() && "Vectors can't be empty");
628 LLVMContext &Context = T->getContext();
629 LLVMContextImpl *pImpl = Context.pImpl;
630
631 // If this is an all-undef or alll-zero vector, return a
632 // ConstantAggregateZero or UndefValue.
633 Constant *C = V[0];
634 bool isZero = C->isNullValue();
635 bool isUndef = isa<UndefValue>(C);
636
637 if (isZero || isUndef) {
638 for (unsigned i = 1, e = V.size(); i != e; ++i)
639 if (V[i] != C) {
640 isZero = isUndef = false;
641 break;
642 }
643 }
644
645 if (isZero)
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000646 return ConstantAggregateZero::get(T);
Owen Andersonaf7ec972009-07-28 21:19:26 +0000647 if (isUndef)
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000648 return UndefValue::get(T);
Owen Andersonaf7ec972009-07-28 21:19:26 +0000649
Owen Andersonaf7ec972009-07-28 21:19:26 +0000650 return pImpl->VectorConstants.getOrCreate(T, V);
651}
652
653Constant* ConstantVector::get(const std::vector<Constant*>& V) {
654 assert(!V.empty() && "Cannot infer type if V is empty");
655 return get(VectorType::get(V.front()->getType(),V.size()), V);
656}
657
658Constant* ConstantVector::get(Constant* const* Vals, unsigned NumVals) {
659 // FIXME: make this the primary ctor method.
660 return get(std::vector<Constant*>(Vals, Vals+NumVals));
661}
662
Dan Gohmanbdc46c62009-12-18 02:58:50 +0000663Constant* ConstantExpr::getNSWNeg(Constant* C) {
664 assert(C->getType()->isIntOrIntVector() &&
665 "Cannot NEG a nonintegral value!");
666 return getNSWSub(ConstantFP::getZeroValueForNegation(C->getType()), C);
667}
668
Duncan Sands8991d512010-02-02 12:53:04 +0000669Constant* ConstantExpr::getNUWNeg(Constant* C) {
670 assert(C->getType()->isIntOrIntVector() &&
671 "Cannot NEG a nonintegral value!");
672 return getNUWSub(ConstantFP::getZeroValueForNegation(C->getType()), C);
673}
674
Dan Gohman6e7ad952009-09-03 23:34:49 +0000675Constant* ConstantExpr::getNSWAdd(Constant* C1, Constant* C2) {
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000676 return getTy(C1->getType(), Instruction::Add, C1, C2,
677 OverflowingBinaryOperator::NoSignedWrap);
Dan Gohman6e7ad952009-09-03 23:34:49 +0000678}
679
Duncan Sands8991d512010-02-02 12:53:04 +0000680Constant* ConstantExpr::getNUWAdd(Constant* C1, Constant* C2) {
681 return getTy(C1->getType(), Instruction::Add, C1, C2,
682 OverflowingBinaryOperator::NoUnsignedWrap);
683}
684
Duncan Sands3548ea82009-09-26 15:35:35 +0000685Constant* ConstantExpr::getNSWSub(Constant* C1, Constant* C2) {
686 return getTy(C1->getType(), Instruction::Sub, C1, C2,
687 OverflowingBinaryOperator::NoSignedWrap);
688}
689
Duncan Sands8991d512010-02-02 12:53:04 +0000690Constant* ConstantExpr::getNUWSub(Constant* C1, Constant* C2) {
691 return getTy(C1->getType(), Instruction::Sub, C1, C2,
Dan Gohmand75ff312010-02-01 16:38:14 +0000692 OverflowingBinaryOperator::NoUnsignedWrap);
693}
694
Dan Gohman41198482009-12-18 03:10:26 +0000695Constant* ConstantExpr::getNSWMul(Constant* C1, Constant* C2) {
696 return getTy(C1->getType(), Instruction::Mul, C1, C2,
697 OverflowingBinaryOperator::NoSignedWrap);
698}
699
Duncan Sands8991d512010-02-02 12:53:04 +0000700Constant* ConstantExpr::getNUWMul(Constant* C1, Constant* C2) {
701 return getTy(C1->getType(), Instruction::Mul, C1, C2,
702 OverflowingBinaryOperator::NoUnsignedWrap);
703}
704
Dan Gohman6e7ad952009-09-03 23:34:49 +0000705Constant* ConstantExpr::getExactSDiv(Constant* C1, Constant* C2) {
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000706 return getTy(C1->getType(), Instruction::SDiv, C1, C2,
707 SDivOperator::IsExact);
Dan Gohman6e7ad952009-09-03 23:34:49 +0000708}
709
Reid Spencer3da59db2006-11-27 01:05:10 +0000710// Utility function for determining if a ConstantExpr is a CastOp or not. This
711// can't be inline because we don't want to #include Instruction.h into
712// Constant.h
713bool ConstantExpr::isCast() const {
714 return Instruction::isCast(getOpcode());
715}
716
Reid Spencer077d0eb2006-12-04 05:19:50 +0000717bool ConstantExpr::isCompare() const {
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +0000718 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
Reid Spencer077d0eb2006-12-04 05:19:50 +0000719}
720
Dan Gohmane6992f72009-09-10 23:37:55 +0000721bool ConstantExpr::isGEPWithNoNotionalOverIndexing() const {
722 if (getOpcode() != Instruction::GetElementPtr) return false;
723
724 gep_type_iterator GEPI = gep_type_begin(this), E = gep_type_end(this);
725 User::const_op_iterator OI = next(this->op_begin());
726
727 // Skip the first index, as it has no static limit.
728 ++GEPI;
729 ++OI;
730
731 // The remaining indices must be compile-time known integers within the
732 // bounds of the corresponding notional static array types.
733 for (; GEPI != E; ++GEPI, ++OI) {
734 ConstantInt *CI = dyn_cast<ConstantInt>(*OI);
735 if (!CI) return false;
736 if (const ArrayType *ATy = dyn_cast<ArrayType>(*GEPI))
737 if (CI->getValue().getActiveBits() > 64 ||
738 CI->getZExtValue() >= ATy->getNumElements())
739 return false;
740 }
741
742 // All the indices checked out.
743 return true;
744}
745
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000746bool ConstantExpr::hasIndices() const {
747 return getOpcode() == Instruction::ExtractValue ||
748 getOpcode() == Instruction::InsertValue;
749}
750
751const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const {
752 if (const ExtractValueConstantExpr *EVCE =
753 dyn_cast<ExtractValueConstantExpr>(this))
754 return EVCE->Indices;
Dan Gohman1a203572008-06-23 16:39:44 +0000755
756 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000757}
758
Reid Spencer728b6db2006-12-03 05:48:19 +0000759unsigned ConstantExpr::getPredicate() const {
Nate Begemanac80ade2008-05-12 19:01:56 +0000760 assert(getOpcode() == Instruction::FCmp ||
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +0000761 getOpcode() == Instruction::ICmp);
Chris Lattnerb7daa842007-10-18 16:26:24 +0000762 return ((const CompareConstantExpr*)this)->predicate;
Reid Spencer728b6db2006-12-03 05:48:19 +0000763}
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000764
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000765/// getWithOperandReplaced - Return a constant expression identical to this
766/// one, but with the specified operand set to the specified value.
Reid Spencer3da59db2006-11-27 01:05:10 +0000767Constant *
768ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000769 assert(OpNo < getNumOperands() && "Operand num is out of range!");
770 assert(Op->getType() == getOperand(OpNo)->getType() &&
771 "Replacing operand with value of different type!");
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000772 if (getOperand(OpNo) == Op)
773 return const_cast<ConstantExpr*>(this);
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000774
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000775 Constant *Op0, *Op1, *Op2;
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000776 switch (getOpcode()) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000777 case Instruction::Trunc:
778 case Instruction::ZExt:
779 case Instruction::SExt:
780 case Instruction::FPTrunc:
781 case Instruction::FPExt:
782 case Instruction::UIToFP:
783 case Instruction::SIToFP:
784 case Instruction::FPToUI:
785 case Instruction::FPToSI:
786 case Instruction::PtrToInt:
787 case Instruction::IntToPtr:
788 case Instruction::BitCast:
789 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000790 case Instruction::Select:
791 Op0 = (OpNo == 0) ? Op : getOperand(0);
792 Op1 = (OpNo == 1) ? Op : getOperand(1);
793 Op2 = (OpNo == 2) ? Op : getOperand(2);
794 return ConstantExpr::getSelect(Op0, Op1, Op2);
795 case Instruction::InsertElement:
796 Op0 = (OpNo == 0) ? Op : getOperand(0);
797 Op1 = (OpNo == 1) ? Op : getOperand(1);
798 Op2 = (OpNo == 2) ? Op : getOperand(2);
799 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
800 case Instruction::ExtractElement:
801 Op0 = (OpNo == 0) ? Op : getOperand(0);
802 Op1 = (OpNo == 1) ? Op : getOperand(1);
803 return ConstantExpr::getExtractElement(Op0, Op1);
804 case Instruction::ShuffleVector:
805 Op0 = (OpNo == 0) ? Op : getOperand(0);
806 Op1 = (OpNo == 1) ? Op : getOperand(1);
807 Op2 = (OpNo == 2) ? Op : getOperand(2);
808 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000809 case Instruction::GetElementPtr: {
Chris Lattnerf9021ff2007-02-19 20:01:23 +0000810 SmallVector<Constant*, 8> Ops;
Dan Gohman041e2eb2008-05-15 19:50:34 +0000811 Ops.resize(getNumOperands()-1);
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000812 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Dan Gohman041e2eb2008-05-15 19:50:34 +0000813 Ops[i-1] = getOperand(i);
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000814 if (OpNo == 0)
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000815 return cast<GEPOperator>(this)->isInBounds() ?
816 ConstantExpr::getInBoundsGetElementPtr(Op, &Ops[0], Ops.size()) :
817 ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000818 Ops[OpNo-1] = Op;
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000819 return cast<GEPOperator>(this)->isInBounds() ?
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000820 ConstantExpr::getInBoundsGetElementPtr(getOperand(0), &Ops[0],Ops.size()):
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000821 ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000822 }
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000823 default:
824 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000825 Op0 = (OpNo == 0) ? Op : getOperand(0);
826 Op1 = (OpNo == 1) ? Op : getOperand(1);
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000827 return ConstantExpr::get(getOpcode(), Op0, Op1, SubclassOptionalData);
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000828 }
829}
830
831/// getWithOperands - This returns the current constant expression with the
832/// operands replaced with the specified values. The specified operands must
833/// match count and type with the existing ones.
834Constant *ConstantExpr::
Chris Lattnerb054bfd2008-08-20 22:27:40 +0000835getWithOperands(Constant* const *Ops, unsigned NumOps) const {
836 assert(NumOps == getNumOperands() && "Operand count mismatch!");
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000837 bool AnyChange = false;
Chris Lattnerb054bfd2008-08-20 22:27:40 +0000838 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000839 assert(Ops[i]->getType() == getOperand(i)->getType() &&
840 "Operand type mismatch!");
841 AnyChange |= Ops[i] != getOperand(i);
842 }
843 if (!AnyChange) // No operands changed, return self.
844 return const_cast<ConstantExpr*>(this);
845
846 switch (getOpcode()) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000847 case Instruction::Trunc:
848 case Instruction::ZExt:
849 case Instruction::SExt:
850 case Instruction::FPTrunc:
851 case Instruction::FPExt:
852 case Instruction::UIToFP:
853 case Instruction::SIToFP:
854 case Instruction::FPToUI:
855 case Instruction::FPToSI:
856 case Instruction::PtrToInt:
857 case Instruction::IntToPtr:
858 case Instruction::BitCast:
859 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000860 case Instruction::Select:
861 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
862 case Instruction::InsertElement:
863 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
864 case Instruction::ExtractElement:
865 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
866 case Instruction::ShuffleVector:
867 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Chris Lattnerf9021ff2007-02-19 20:01:23 +0000868 case Instruction::GetElementPtr:
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000869 return cast<GEPOperator>(this)->isInBounds() ?
870 ConstantExpr::getInBoundsGetElementPtr(Ops[0], &Ops[1], NumOps-1) :
871 ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], NumOps-1);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000872 case Instruction::ICmp:
873 case Instruction::FCmp:
874 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000875 default:
876 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000877 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1], SubclassOptionalData);
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000878 }
879}
880
Chris Lattner00950542001-06-06 20:29:01 +0000881
882//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +0000883// isValueValidForType implementations
884
Reid Spencer9b11d512006-12-19 01:28:19 +0000885bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000886 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Owen Anderson1d0be152009-08-13 21:58:54 +0000887 if (Ty == Type::getInt1Ty(Ty->getContext()))
Reid Spencera54b7cb2007-01-12 07:05:14 +0000888 return Val == 0 || Val == 1;
Reid Spencer554cec62007-02-05 23:47:56 +0000889 if (NumBits >= 64)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000890 return true; // always true, has to fit in largest type
891 uint64_t Max = (1ll << NumBits) - 1;
892 return Val <= Max;
Reid Spencer9b11d512006-12-19 01:28:19 +0000893}
894
Reid Spencerb83eb642006-10-20 07:07:24 +0000895bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000896 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Owen Anderson1d0be152009-08-13 21:58:54 +0000897 if (Ty == Type::getInt1Ty(Ty->getContext()))
Reid Spencerc1030572007-01-19 21:13:56 +0000898 return Val == 0 || Val == 1 || Val == -1;
Reid Spencer554cec62007-02-05 23:47:56 +0000899 if (NumBits >= 64)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000900 return true; // always true, has to fit in largest type
901 int64_t Min = -(1ll << (NumBits-1));
902 int64_t Max = (1ll << (NumBits-1)) - 1;
903 return (Val >= Min && Val <= Max);
Chris Lattner00950542001-06-06 20:29:01 +0000904}
905
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000906bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) {
907 // convert modifies in place, so make a copy.
908 APFloat Val2 = APFloat(Val);
Dale Johannesen23a98552008-10-09 23:00:39 +0000909 bool losesInfo;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000910 switch (Ty->getTypeID()) {
Chris Lattner00950542001-06-06 20:29:01 +0000911 default:
912 return false; // These can't be represented as floating point!
913
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000914 // FIXME rounding mode needs to be more flexible
Dale Johannesen23a98552008-10-09 23:00:39 +0000915 case Type::FloatTyID: {
916 if (&Val2.getSemantics() == &APFloat::IEEEsingle)
917 return true;
918 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
919 return !losesInfo;
920 }
921 case Type::DoubleTyID: {
922 if (&Val2.getSemantics() == &APFloat::IEEEsingle ||
923 &Val2.getSemantics() == &APFloat::IEEEdouble)
924 return true;
925 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
926 return !losesInfo;
927 }
Dale Johannesenebbc95d2007-08-09 22:51:36 +0000928 case Type::X86_FP80TyID:
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000929 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
930 &Val2.getSemantics() == &APFloat::IEEEdouble ||
931 &Val2.getSemantics() == &APFloat::x87DoubleExtended;
Dale Johannesenebbc95d2007-08-09 22:51:36 +0000932 case Type::FP128TyID:
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000933 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
934 &Val2.getSemantics() == &APFloat::IEEEdouble ||
935 &Val2.getSemantics() == &APFloat::IEEEquad;
Dale Johannesena471c2e2007-10-11 18:07:22 +0000936 case Type::PPC_FP128TyID:
937 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
938 &Val2.getSemantics() == &APFloat::IEEEdouble ||
939 &Val2.getSemantics() == &APFloat::PPCDoubleDouble;
Chris Lattner00950542001-06-06 20:29:01 +0000940 }
Chris Lattnerd74ea2b2006-05-24 17:04:05 +0000941}
Chris Lattner37bf6302001-07-20 19:16:02 +0000942
Chris Lattner531daef2001-09-07 16:46:31 +0000943//===----------------------------------------------------------------------===//
Chris Lattner531daef2001-09-07 16:46:31 +0000944// Factory Function Implementation
945
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000946ConstantAggregateZero* ConstantAggregateZero::get(const Type* Ty) {
947 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) &&
948 "Cannot create an aggregate zero of non-aggregate type!");
949
950 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000951 return pImpl->AggZeroConstants.getOrCreate(Ty, 0);
952}
953
Dan Gohman0f8b53f2009-03-03 02:55:14 +0000954/// destroyConstant - Remove the constant from the constant table...
955///
Owen Anderson04fb7c32009-06-20 00:24:58 +0000956void ConstantAggregateZero::destroyConstant() {
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000957 getType()->getContext().pImpl->AggZeroConstants.remove(this);
Chris Lattner40bbeb52004-02-15 05:53:04 +0000958 destroyConstantImpl();
959}
960
Dan Gohman0f8b53f2009-03-03 02:55:14 +0000961/// destroyConstant - Remove the constant from the constant table...
962///
Owen Anderson04fb7c32009-06-20 00:24:58 +0000963void ConstantArray::destroyConstant() {
Owen Anderson1fd70962009-07-28 18:32:17 +0000964 getType()->getContext().pImpl->ArrayConstants.remove(this);
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000965 destroyConstantImpl();
966}
967
Reid Spencer3d10b0b2007-01-26 07:37:34 +0000968/// isString - This method returns true if the array is an array of i8, and
969/// if the elements of the array are all ConstantInt's.
Chris Lattner13cfdea2004-01-14 17:06:38 +0000970bool ConstantArray::isString() const {
Reid Spencer3d10b0b2007-01-26 07:37:34 +0000971 // Check the element type for i8...
Benjamin Kramer8c65f6e2010-01-05 21:05:54 +0000972 if (!getType()->getElementType()->isInteger(8))
Chris Lattner13cfdea2004-01-14 17:06:38 +0000973 return false;
974 // Check the elements to make sure they are all integers, not constant
975 // expressions.
976 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
977 if (!isa<ConstantInt>(getOperand(i)))
978 return false;
979 return true;
980}
981
Evan Cheng22c70302006-10-26 19:15:05 +0000982/// isCString - This method returns true if the array is a string (see
Dan Gohman0f8b53f2009-03-03 02:55:14 +0000983/// isString) and it ends in a null byte \\0 and does not contains any other
Evan Cheng22c70302006-10-26 19:15:05 +0000984/// null bytes except its terminator.
Owen Anderson1ca29d32009-07-13 21:27:19 +0000985bool ConstantArray::isCString() const {
Reid Spencer3d10b0b2007-01-26 07:37:34 +0000986 // Check the element type for i8...
Benjamin Kramer8c65f6e2010-01-05 21:05:54 +0000987 if (!getType()->getElementType()->isInteger(8))
Evan Chengabf63452006-10-26 21:48:03 +0000988 return false;
Owen Anderson1ca29d32009-07-13 21:27:19 +0000989
Evan Chengabf63452006-10-26 21:48:03 +0000990 // Last element must be a null.
Owen Anderson1ca29d32009-07-13 21:27:19 +0000991 if (!getOperand(getNumOperands()-1)->isNullValue())
Evan Chengabf63452006-10-26 21:48:03 +0000992 return false;
993 // Other elements must be non-null integers.
994 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
995 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng22c70302006-10-26 19:15:05 +0000996 return false;
Owen Anderson1ca29d32009-07-13 21:27:19 +0000997 if (getOperand(i)->isNullValue())
Evan Chengabf63452006-10-26 21:48:03 +0000998 return false;
999 }
Evan Cheng22c70302006-10-26 19:15:05 +00001000 return true;
1001}
1002
1003
Dan Gohman0f8b53f2009-03-03 02:55:14 +00001004/// getAsString - If the sub-element type of this array is i8
1005/// then this method converts the array to an std::string and returns it.
1006/// Otherwise, it asserts out.
1007///
Chris Lattner93aeea32002-08-26 17:53:56 +00001008std::string ConstantArray::getAsString() const {
Chris Lattner13cfdea2004-01-14 17:06:38 +00001009 assert(isString() && "Not a string!");
Chris Lattner93aeea32002-08-26 17:53:56 +00001010 std::string Result;
Owen Anderson45e39582008-06-24 21:58:29 +00001011 Result.reserve(getNumOperands());
Chris Lattnerc07736a2003-07-23 15:22:26 +00001012 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersone4f6ee52008-06-25 01:05:05 +00001013 Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue());
Chris Lattner93aeea32002-08-26 17:53:56 +00001014 return Result;
1015}
1016
1017
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001018//---- ConstantStruct::get() implementation...
Chris Lattner531daef2001-09-07 16:46:31 +00001019//
Chris Lattnered468e372003-10-05 00:17:43 +00001020
Chris Lattner31f84992003-11-21 20:23:48 +00001021namespace llvm {
Misha Brukmanfd939082005-04-21 23:48:37 +00001022
Chris Lattner531daef2001-09-07 16:46:31 +00001023}
Chris Lattner6a57baa2001-10-03 15:39:36 +00001024
Chris Lattnerf5ec48d2001-10-13 06:57:33 +00001025// destroyConstant - Remove the constant from the constant table...
Chris Lattner6a57baa2001-10-03 15:39:36 +00001026//
Owen Anderson04fb7c32009-06-20 00:24:58 +00001027void ConstantStruct::destroyConstant() {
Owen Anderson8fa33382009-07-27 22:29:26 +00001028 getType()->getContext().pImpl->StructConstants.remove(this);
Chris Lattnerf5ec48d2001-10-13 06:57:33 +00001029 destroyConstantImpl();
1030}
Chris Lattner6a57baa2001-10-03 15:39:36 +00001031
Brian Gaeke715c90b2004-08-20 06:00:58 +00001032// destroyConstant - Remove the constant from the constant table...
1033//
Chris Lattnerfdfeb692010-02-12 20:49:41 +00001034void ConstantUnion::destroyConstant() {
1035 getType()->getContext().pImpl->UnionConstants.remove(this);
1036 destroyConstantImpl();
1037}
1038
1039// destroyConstant - Remove the constant from the constant table...
1040//
Owen Anderson04fb7c32009-06-20 00:24:58 +00001041void ConstantVector::destroyConstant() {
Owen Andersonaf7ec972009-07-28 21:19:26 +00001042 getType()->getContext().pImpl->VectorConstants.remove(this);
Brian Gaeke715c90b2004-08-20 06:00:58 +00001043 destroyConstantImpl();
1044}
1045
Dan Gohmanfa73ea22007-05-24 14:36:04 +00001046/// This function will return true iff every element in this vector constant
Jim Laskeyfa301822007-01-12 22:39:14 +00001047/// is set to all ones.
1048/// @returns true iff this constant's emements are all set to all ones.
1049/// @brief Determine if the value is all ones.
Reid Spencer9d6565a2007-02-15 02:26:10 +00001050bool ConstantVector::isAllOnesValue() const {
Jim Laskeyfa301822007-01-12 22:39:14 +00001051 // Check out first element.
1052 const Constant *Elt = getOperand(0);
1053 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1054 if (!CI || !CI->isAllOnesValue()) return false;
1055 // Then make sure all remaining elements point to the same value.
1056 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1057 if (getOperand(I) != Elt) return false;
1058 }
1059 return true;
1060}
1061
Dan Gohman3b7cf0a2007-10-17 17:51:30 +00001062/// getSplatValue - If this is a splat constant, where all of the
1063/// elements have the same value, return that value. Otherwise return null.
1064Constant *ConstantVector::getSplatValue() {
1065 // Check out first element.
1066 Constant *Elt = getOperand(0);
1067 // Then make sure all remaining elements point to the same value.
1068 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
1069 if (getOperand(I) != Elt) return 0;
1070 return Elt;
1071}
1072
Chris Lattner2ee11ec2009-10-28 00:01:44 +00001073//---- ConstantPointerNull::get() implementation.
Chris Lattnerf5ec48d2001-10-13 06:57:33 +00001074//
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001075
Owen Anderson04fb7c32009-06-20 00:24:58 +00001076ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Owen Anderson7e3142b2009-07-31 22:45:43 +00001077 return Ty->getContext().pImpl->NullPtrConstants.getOrCreate(Ty, 0);
Chris Lattner6a57baa2001-10-03 15:39:36 +00001078}
1079
Chris Lattner41661fd2002-08-18 00:40:04 +00001080// destroyConstant - Remove the constant from the constant table...
1081//
Owen Anderson04fb7c32009-06-20 00:24:58 +00001082void ConstantPointerNull::destroyConstant() {
Owen Anderson7e3142b2009-07-31 22:45:43 +00001083 getType()->getContext().pImpl->NullPtrConstants.remove(this);
Chris Lattner41661fd2002-08-18 00:40:04 +00001084 destroyConstantImpl();
1085}
1086
1087
Chris Lattner2ee11ec2009-10-28 00:01:44 +00001088//---- UndefValue::get() implementation.
Chris Lattnerb9f18592004-10-16 18:07:16 +00001089//
1090
Owen Anderson04fb7c32009-06-20 00:24:58 +00001091UndefValue *UndefValue::get(const Type *Ty) {
Owen Anderson7e3142b2009-07-31 22:45:43 +00001092 return Ty->getContext().pImpl->UndefValueConstants.getOrCreate(Ty, 0);
Chris Lattnerb9f18592004-10-16 18:07:16 +00001093}
1094
1095// destroyConstant - Remove the constant from the constant table.
1096//
Owen Anderson04fb7c32009-06-20 00:24:58 +00001097void UndefValue::destroyConstant() {
Owen Anderson7e3142b2009-07-31 22:45:43 +00001098 getType()->getContext().pImpl->UndefValueConstants.remove(this);
Chris Lattnerb9f18592004-10-16 18:07:16 +00001099 destroyConstantImpl();
1100}
1101
Chris Lattner2ee11ec2009-10-28 00:01:44 +00001102//---- BlockAddress::get() implementation.
1103//
1104
1105BlockAddress *BlockAddress::get(BasicBlock *BB) {
1106 assert(BB->getParent() != 0 && "Block must have a parent");
1107 return get(BB->getParent(), BB);
1108}
1109
1110BlockAddress *BlockAddress::get(Function *F, BasicBlock *BB) {
1111 BlockAddress *&BA =
1112 F->getContext().pImpl->BlockAddresses[std::make_pair(F, BB)];
1113 if (BA == 0)
1114 BA = new BlockAddress(F, BB);
1115
1116 assert(BA->getFunction() == F && "Basic block moved between functions");
1117 return BA;
1118}
1119
1120BlockAddress::BlockAddress(Function *F, BasicBlock *BB)
1121: Constant(Type::getInt8PtrTy(F->getContext()), Value::BlockAddressVal,
1122 &Op<0>(), 2) {
Chris Lattnerd0ec2352009-11-01 03:03:03 +00001123 setOperand(0, F);
1124 setOperand(1, BB);
Chris Lattnercdfc9402009-11-01 01:27:45 +00001125 BB->AdjustBlockAddressRefCount(1);
Chris Lattner2ee11ec2009-10-28 00:01:44 +00001126}
1127
1128
1129// destroyConstant - Remove the constant from the constant table.
1130//
1131void BlockAddress::destroyConstant() {
1132 getFunction()->getType()->getContext().pImpl
1133 ->BlockAddresses.erase(std::make_pair(getFunction(), getBasicBlock()));
Chris Lattnercdfc9402009-11-01 01:27:45 +00001134 getBasicBlock()->AdjustBlockAddressRefCount(-1);
Chris Lattner2ee11ec2009-10-28 00:01:44 +00001135 destroyConstantImpl();
1136}
1137
1138void BlockAddress::replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
1139 // This could be replacing either the Basic Block or the Function. In either
1140 // case, we have to remove the map entry.
1141 Function *NewF = getFunction();
1142 BasicBlock *NewBB = getBasicBlock();
1143
1144 if (U == &Op<0>())
1145 NewF = cast<Function>(To);
1146 else
1147 NewBB = cast<BasicBlock>(To);
1148
1149 // See if the 'new' entry already exists, if not, just update this in place
1150 // and return early.
1151 BlockAddress *&NewBA =
1152 getContext().pImpl->BlockAddresses[std::make_pair(NewF, NewBB)];
1153 if (NewBA == 0) {
Chris Lattnerd0ec2352009-11-01 03:03:03 +00001154 getBasicBlock()->AdjustBlockAddressRefCount(-1);
1155
Chris Lattner2ee11ec2009-10-28 00:01:44 +00001156 // Remove the old entry, this can't cause the map to rehash (just a
1157 // tombstone will get added).
1158 getContext().pImpl->BlockAddresses.erase(std::make_pair(getFunction(),
1159 getBasicBlock()));
1160 NewBA = this;
Chris Lattnerd0ec2352009-11-01 03:03:03 +00001161 setOperand(0, NewF);
1162 setOperand(1, NewBB);
1163 getBasicBlock()->AdjustBlockAddressRefCount(1);
Chris Lattner2ee11ec2009-10-28 00:01:44 +00001164 return;
1165 }
1166
1167 // Otherwise, I do need to replace this with an existing value.
1168 assert(NewBA != this && "I didn't contain From!");
1169
1170 // Everyone using this now uses the replacement.
1171 uncheckedReplaceAllUsesWith(NewBA);
1172
1173 destroyConstant();
1174}
1175
1176//---- ConstantExpr::get() implementations.
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001177//
Reid Spencer79e21d32006-12-31 05:26:44 +00001178
Reid Spencer3da59db2006-11-27 01:05:10 +00001179/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands66a1a052008-03-30 19:38:55 +00001180/// cast in the ExprConstants map. It is used by the various get* methods below.
Reid Spencer3da59db2006-11-27 01:05:10 +00001181static inline Constant *getFoldedCast(
Owen Anderson04fb7c32009-06-20 00:24:58 +00001182 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner9eacf8a2003-10-07 22:19:19 +00001183 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer3da59db2006-11-27 01:05:10 +00001184 // Fold a few common cases
Chris Lattnerb29d5962010-02-01 20:48:08 +00001185 if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
Reid Spencer3da59db2006-11-27 01:05:10 +00001186 return FC;
Chris Lattnerd628f6a2003-04-17 19:24:48 +00001187
Owen Andersond03eecd2009-08-04 20:25:11 +00001188 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
1189
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001190 // Look up the constant in the table first to ensure uniqueness
Chris Lattner9bc02a42003-05-13 21:37:02 +00001191 std::vector<Constant*> argVec(1, C);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001192 ExprMapKeyType Key(opc, argVec);
Owen Anderson3e456ab2009-06-17 18:40:29 +00001193
Owen Andersond03eecd2009-08-04 20:25:11 +00001194 return pImpl->ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001195}
Reid Spencer7858b332006-12-05 19:14:13 +00001196
Owen Anderson04fb7c32009-06-20 00:24:58 +00001197Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
Reid Spencer3da59db2006-11-27 01:05:10 +00001198 Instruction::CastOps opc = Instruction::CastOps(oc);
1199 assert(Instruction::isCast(opc) && "opcode out of range");
1200 assert(C && Ty && "Null arguments to getCast");
Chris Lattner0b68a002010-01-26 21:51:43 +00001201 assert(CastInst::castIsValid(opc, C, Ty) && "Invalid constantexpr cast!");
Reid Spencer3da59db2006-11-27 01:05:10 +00001202
1203 switch (opc) {
Chris Lattner0b68a002010-01-26 21:51:43 +00001204 default:
1205 llvm_unreachable("Invalid cast opcode");
1206 break;
1207 case Instruction::Trunc: return getTrunc(C, Ty);
1208 case Instruction::ZExt: return getZExt(C, Ty);
1209 case Instruction::SExt: return getSExt(C, Ty);
1210 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1211 case Instruction::FPExt: return getFPExtend(C, Ty);
1212 case Instruction::UIToFP: return getUIToFP(C, Ty);
1213 case Instruction::SIToFP: return getSIToFP(C, Ty);
1214 case Instruction::FPToUI: return getFPToUI(C, Ty);
1215 case Instruction::FPToSI: return getFPToSI(C, Ty);
1216 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1217 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1218 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattnerf5ac6c22005-01-01 15:59:57 +00001219 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001220 return 0;
Reid Spencer7858b332006-12-05 19:14:13 +00001221}
1222
Reid Spencer848414e2006-12-04 20:17:56 +00001223Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001224 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer848414e2006-12-04 20:17:56 +00001225 return getCast(Instruction::BitCast, C, Ty);
1226 return getCast(Instruction::ZExt, C, Ty);
1227}
1228
Owen Anderson04fb7c32009-06-20 00:24:58 +00001229Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001230 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Owen Anderson04fb7c32009-06-20 00:24:58 +00001231 return getCast(Instruction::BitCast, C, Ty);
1232 return getCast(Instruction::SExt, C, Ty);
Reid Spencer848414e2006-12-04 20:17:56 +00001233}
1234
1235Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001236 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Reid Spencer848414e2006-12-04 20:17:56 +00001237 return getCast(Instruction::BitCast, C, Ty);
1238 return getCast(Instruction::Trunc, C, Ty);
1239}
1240
Owen Anderson04fb7c32009-06-20 00:24:58 +00001241Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
Reid Spencerc0459fb2006-12-05 03:25:26 +00001242 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner42a75512007-01-15 02:27:26 +00001243 assert((Ty->isInteger() || isa<PointerType>(Ty)) && "Invalid cast");
Reid Spencerc0459fb2006-12-05 03:25:26 +00001244
Chris Lattner42a75512007-01-15 02:27:26 +00001245 if (Ty->isInteger())
Owen Anderson04fb7c32009-06-20 00:24:58 +00001246 return getCast(Instruction::PtrToInt, S, Ty);
1247 return getCast(Instruction::BitCast, S, Ty);
Reid Spencerc0459fb2006-12-05 03:25:26 +00001248}
1249
Reid Spencer84f3eab2006-12-12 00:51:07 +00001250Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1251 bool isSigned) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001252 assert(C->getType()->isIntOrIntVector() &&
1253 Ty->isIntOrIntVector() && "Invalid cast");
1254 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1255 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer84f3eab2006-12-12 00:51:07 +00001256 Instruction::CastOps opcode =
1257 (SrcBits == DstBits ? Instruction::BitCast :
1258 (SrcBits > DstBits ? Instruction::Trunc :
1259 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1260 return getCast(opcode, C, Ty);
1261}
1262
1263Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001264 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer84f3eab2006-12-12 00:51:07 +00001265 "Invalid cast");
Dan Gohman6de29f82009-06-15 22:12:54 +00001266 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1267 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencerf25212a2006-12-12 05:38:50 +00001268 if (SrcBits == DstBits)
1269 return C; // Avoid a useless cast
Reid Spencer84f3eab2006-12-12 00:51:07 +00001270 Instruction::CastOps opcode =
Reid Spencerf25212a2006-12-12 05:38:50 +00001271 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer84f3eab2006-12-12 00:51:07 +00001272 return getCast(opcode, C, Ty);
1273}
1274
Owen Anderson04fb7c32009-06-20 00:24:58 +00001275Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001276#ifndef NDEBUG
1277 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1278 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1279#endif
1280 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1281 assert(C->getType()->isIntOrIntVector() && "Trunc operand must be integer");
1282 assert(Ty->isIntOrIntVector() && "Trunc produces only integral");
1283 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer3da59db2006-11-27 01:05:10 +00001284 "SrcTy must be larger than DestTy for Trunc!");
1285
Owen Anderson04fb7c32009-06-20 00:24:58 +00001286 return getFoldedCast(Instruction::Trunc, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001287}
1288
Owen Anderson04fb7c32009-06-20 00:24:58 +00001289Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001290#ifndef NDEBUG
1291 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1292 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1293#endif
1294 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1295 assert(C->getType()->isIntOrIntVector() && "SExt operand must be integral");
1296 assert(Ty->isIntOrIntVector() && "SExt produces only integer");
1297 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer3da59db2006-11-27 01:05:10 +00001298 "SrcTy must be smaller than DestTy for SExt!");
1299
Owen Anderson04fb7c32009-06-20 00:24:58 +00001300 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerd144f422004-04-04 23:20:30 +00001301}
1302
Owen Anderson04fb7c32009-06-20 00:24:58 +00001303Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001304#ifndef NDEBUG
1305 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1306 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1307#endif
1308 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1309 assert(C->getType()->isIntOrIntVector() && "ZEXt operand must be integral");
1310 assert(Ty->isIntOrIntVector() && "ZExt produces only integer");
1311 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer3da59db2006-11-27 01:05:10 +00001312 "SrcTy must be smaller than DestTy for ZExt!");
1313
Owen Anderson04fb7c32009-06-20 00:24:58 +00001314 return getFoldedCast(Instruction::ZExt, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001315}
1316
Owen Anderson04fb7c32009-06-20 00:24:58 +00001317Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001318#ifndef NDEBUG
1319 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1320 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1321#endif
1322 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1323 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1324 C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer3da59db2006-11-27 01:05:10 +00001325 "This is an illegal floating point truncation!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001326 return getFoldedCast(Instruction::FPTrunc, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001327}
1328
Owen Anderson04fb7c32009-06-20 00:24:58 +00001329Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001330#ifndef NDEBUG
1331 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1332 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1333#endif
1334 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1335 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
1336 C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer3da59db2006-11-27 01:05:10 +00001337 "This is an illegal floating point extension!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001338 return getFoldedCast(Instruction::FPExt, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001339}
1340
Owen Anderson04fb7c32009-06-20 00:24:58 +00001341Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Devang Patelb6dc9352008-11-03 23:20:04 +00001342#ifndef NDEBUG
Nate Begemanb348d182007-11-17 03:58:34 +00001343 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1344 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Patelb6dc9352008-11-03 23:20:04 +00001345#endif
Nate Begemanb348d182007-11-17 03:58:34 +00001346 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1347 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
1348 "This is an illegal uint to floating point cast!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001349 return getFoldedCast(Instruction::UIToFP, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001350}
1351
Owen Anderson04fb7c32009-06-20 00:24:58 +00001352Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Devang Patelb6dc9352008-11-03 23:20:04 +00001353#ifndef NDEBUG
Nate Begemanb348d182007-11-17 03:58:34 +00001354 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1355 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Patelb6dc9352008-11-03 23:20:04 +00001356#endif
Nate Begemanb348d182007-11-17 03:58:34 +00001357 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1358 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
Reid Spencer3da59db2006-11-27 01:05:10 +00001359 "This is an illegal sint to floating point cast!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001360 return getFoldedCast(Instruction::SIToFP, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001361}
1362
Owen Anderson04fb7c32009-06-20 00:24:58 +00001363Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Devang Patelb6dc9352008-11-03 23:20:04 +00001364#ifndef NDEBUG
Nate Begemanb348d182007-11-17 03:58:34 +00001365 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1366 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Patelb6dc9352008-11-03 23:20:04 +00001367#endif
Nate Begemanb348d182007-11-17 03:58:34 +00001368 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1369 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1370 "This is an illegal floating point to uint cast!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001371 return getFoldedCast(Instruction::FPToUI, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001372}
1373
Owen Anderson04fb7c32009-06-20 00:24:58 +00001374Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Devang Patelb6dc9352008-11-03 23:20:04 +00001375#ifndef NDEBUG
Nate Begemanb348d182007-11-17 03:58:34 +00001376 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1377 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Patelb6dc9352008-11-03 23:20:04 +00001378#endif
Nate Begemanb348d182007-11-17 03:58:34 +00001379 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1380 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1381 "This is an illegal floating point to sint cast!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001382 return getFoldedCast(Instruction::FPToSI, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001383}
1384
Owen Anderson04fb7c32009-06-20 00:24:58 +00001385Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
Reid Spencer3da59db2006-11-27 01:05:10 +00001386 assert(isa<PointerType>(C->getType()) && "PtrToInt source must be pointer");
Chris Lattner42a75512007-01-15 02:27:26 +00001387 assert(DstTy->isInteger() && "PtrToInt destination must be integral");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001388 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
Reid Spencer3da59db2006-11-27 01:05:10 +00001389}
1390
Owen Anderson04fb7c32009-06-20 00:24:58 +00001391Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Chris Lattner42a75512007-01-15 02:27:26 +00001392 assert(C->getType()->isInteger() && "IntToPtr source must be integral");
Reid Spencer3da59db2006-11-27 01:05:10 +00001393 assert(isa<PointerType>(DstTy) && "IntToPtr destination must be a pointer");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001394 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
Reid Spencer3da59db2006-11-27 01:05:10 +00001395}
1396
Owen Anderson04fb7c32009-06-20 00:24:58 +00001397Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
Chris Lattner0b68a002010-01-26 21:51:43 +00001398 assert(CastInst::castIsValid(Instruction::BitCast, C, DstTy) &&
1399 "Invalid constantexpr bitcast!");
Chris Lattner8c7f24a2009-03-21 06:55:54 +00001400
1401 // It is common to ask for a bitcast of a value to its own type, handle this
1402 // speedily.
1403 if (C->getType() == DstTy) return C;
1404
Owen Anderson04fb7c32009-06-20 00:24:58 +00001405 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerd144f422004-04-04 23:20:30 +00001406}
1407
Chris Lattnered468e372003-10-05 00:17:43 +00001408Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001409 Constant *C1, Constant *C2,
1410 unsigned Flags) {
Chris Lattnerf31f5832003-05-21 17:49:25 +00001411 // Check the operands for consistency first
Reid Spencer0a783f72006-11-02 01:53:59 +00001412 assert(Opcode >= Instruction::BinaryOpsBegin &&
1413 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattnerf31f5832003-05-21 17:49:25 +00001414 "Invalid opcode in binary constant expression");
1415 assert(C1->getType() == C2->getType() &&
1416 "Operand types in binary constant expression should match");
Chris Lattnered468e372003-10-05 00:17:43 +00001417
Owen Anderson1d0be152009-08-13 21:58:54 +00001418 if (ReqTy == C1->getType() || ReqTy == Type::getInt1Ty(ReqTy->getContext()))
Chris Lattnerb29d5962010-02-01 20:48:08 +00001419 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattnered468e372003-10-05 00:17:43 +00001420 return FC; // Fold a few common cases...
Chris Lattnerd628f6a2003-04-17 19:24:48 +00001421
Chris Lattner9bc02a42003-05-13 21:37:02 +00001422 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001423 ExprMapKeyType Key(Opcode, argVec, 0, Flags);
Owen Anderson31c36f02009-06-17 20:10:08 +00001424
Owen Andersond03eecd2009-08-04 20:25:11 +00001425 LLVMContextImpl *pImpl = ReqTy->getContext().pImpl;
Owen Andersond03eecd2009-08-04 20:25:11 +00001426 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001427}
1428
Reid Spencere4d87aa2006-12-23 06:05:41 +00001429Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Nate Begemanff795a82008-07-25 17:56:27 +00001430 Constant *C1, Constant *C2) {
Reid Spencere4d87aa2006-12-23 06:05:41 +00001431 switch (predicate) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001432 default: llvm_unreachable("Invalid CmpInst predicate");
Nate Begemanb5557ab2008-07-25 17:35:37 +00001433 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
1434 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
1435 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
1436 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
1437 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
1438 case CmpInst::FCMP_TRUE:
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00001439 return getFCmp(predicate, C1, C2);
1440
Nate Begemanb5557ab2008-07-25 17:35:37 +00001441 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
1442 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
1443 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
1444 case CmpInst::ICMP_SLE:
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00001445 return getICmp(predicate, C1, C2);
Reid Spencere4d87aa2006-12-23 06:05:41 +00001446 }
Reid Spencer67263fe2006-12-04 21:35:24 +00001447}
1448
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001449Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2,
1450 unsigned Flags) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001451 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1452 if (C1->getType()->isFPOrFPVector()) {
1453 if (Opcode == Instruction::Add) Opcode = Instruction::FAdd;
1454 else if (Opcode == Instruction::Sub) Opcode = Instruction::FSub;
1455 else if (Opcode == Instruction::Mul) Opcode = Instruction::FMul;
1456 }
Chris Lattner91b362b2004-08-17 17:28:46 +00001457#ifndef NDEBUG
1458 switch (Opcode) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001459 case Instruction::Add:
Reid Spencer0a783f72006-11-02 01:53:59 +00001460 case Instruction::Sub:
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001461 case Instruction::Mul:
Chris Lattner91b362b2004-08-17 17:28:46 +00001462 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001463 assert(C1->getType()->isIntOrIntVector() &&
1464 "Tried to create an integer operation on a non-integer type!");
1465 break;
1466 case Instruction::FAdd:
1467 case Instruction::FSub:
1468 case Instruction::FMul:
1469 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1470 assert(C1->getType()->isFPOrFPVector() &&
1471 "Tried to create a floating-point operation on a "
1472 "non-floating-point type!");
Chris Lattner91b362b2004-08-17 17:28:46 +00001473 break;
Reid Spencer1628cec2006-10-26 06:15:43 +00001474 case Instruction::UDiv:
1475 case Instruction::SDiv:
1476 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanf57478f2009-06-15 22:25:12 +00001477 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer1628cec2006-10-26 06:15:43 +00001478 "Tried to create an arithmetic operation on a non-arithmetic type!");
1479 break;
1480 case Instruction::FDiv:
1481 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanf57478f2009-06-15 22:25:12 +00001482 assert(C1->getType()->isFPOrFPVector() &&
1483 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer1628cec2006-10-26 06:15:43 +00001484 break;
Reid Spencer0a783f72006-11-02 01:53:59 +00001485 case Instruction::URem:
1486 case Instruction::SRem:
1487 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanf57478f2009-06-15 22:25:12 +00001488 assert(C1->getType()->isIntOrIntVector() &&
Reid Spencer0a783f72006-11-02 01:53:59 +00001489 "Tried to create an arithmetic operation on a non-arithmetic type!");
1490 break;
1491 case Instruction::FRem:
1492 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanf57478f2009-06-15 22:25:12 +00001493 assert(C1->getType()->isFPOrFPVector() &&
1494 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer0a783f72006-11-02 01:53:59 +00001495 break;
Chris Lattner91b362b2004-08-17 17:28:46 +00001496 case Instruction::And:
1497 case Instruction::Or:
1498 case Instruction::Xor:
1499 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanf57478f2009-06-15 22:25:12 +00001500 assert(C1->getType()->isIntOrIntVector() &&
Misha Brukman1bae2912005-01-27 06:46:38 +00001501 "Tried to create a logical operation on a non-integral type!");
Chris Lattner91b362b2004-08-17 17:28:46 +00001502 break;
Chris Lattner91b362b2004-08-17 17:28:46 +00001503 case Instruction::Shl:
Reid Spencer3822ff52006-11-08 06:47:33 +00001504 case Instruction::LShr:
1505 case Instruction::AShr:
Reid Spencer832254e2007-02-02 02:16:23 +00001506 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Dan Gohmanc1317932009-03-14 17:09:17 +00001507 assert(C1->getType()->isIntOrIntVector() &&
Chris Lattner91b362b2004-08-17 17:28:46 +00001508 "Tried to create a shift operation on a non-integer type!");
1509 break;
1510 default:
1511 break;
1512 }
1513#endif
1514
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001515 return getTy(C1->getType(), Opcode, C1, C2, Flags);
Reid Spencer67263fe2006-12-04 21:35:24 +00001516}
1517
Owen Andersonbaf3c402009-07-29 18:55:55 +00001518Constant* ConstantExpr::getSizeOf(const Type* Ty) {
1519 // sizeof is implemented as: (i64) gep (Ty*)null, 1
1520 // Note that a non-inbounds gep is used, as null isn't within any object.
Owen Anderson1d0be152009-08-13 21:58:54 +00001521 Constant *GEPIdx = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
Owen Andersonbaf3c402009-07-29 18:55:55 +00001522 Constant *GEP = getGetElementPtr(
Owen Andersona7235ea2009-07-31 20:28:14 +00001523 Constant::getNullValue(PointerType::getUnqual(Ty)), &GEPIdx, 1);
Owen Anderson1d0be152009-08-13 21:58:54 +00001524 return getCast(Instruction::PtrToInt, GEP,
1525 Type::getInt64Ty(Ty->getContext()));
Owen Andersonbaf3c402009-07-29 18:55:55 +00001526}
1527
1528Constant* ConstantExpr::getAlignOf(const Type* Ty) {
Dan Gohman0f5efe52010-01-28 02:15:55 +00001529 // alignof is implemented as: (i64) gep ({i1,Ty}*)null, 0, 1
Dan Gohmane2574d32009-08-11 17:57:01 +00001530 // Note that a non-inbounds gep is used, as null isn't within any object.
Owen Andersond7f2a6c2009-08-05 23:16:16 +00001531 const Type *AligningTy = StructType::get(Ty->getContext(),
Dan Gohman0f5efe52010-01-28 02:15:55 +00001532 Type::getInt1Ty(Ty->getContext()), Ty, NULL);
Owen Andersona7235ea2009-07-31 20:28:14 +00001533 Constant *NullPtr = Constant::getNullValue(AligningTy->getPointerTo());
Dan Gohman06ed3e72010-01-28 02:43:22 +00001534 Constant *Zero = ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0);
Owen Anderson1d0be152009-08-13 21:58:54 +00001535 Constant *One = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
Owen Andersonbaf3c402009-07-29 18:55:55 +00001536 Constant *Indices[2] = { Zero, One };
1537 Constant *GEP = getGetElementPtr(NullPtr, Indices, 2);
Owen Anderson1d0be152009-08-13 21:58:54 +00001538 return getCast(Instruction::PtrToInt, GEP,
Dan Gohman06ed3e72010-01-28 02:43:22 +00001539 Type::getInt64Ty(Ty->getContext()));
Owen Andersonbaf3c402009-07-29 18:55:55 +00001540}
1541
Dan Gohman3778f212009-08-16 21:26:11 +00001542Constant* ConstantExpr::getOffsetOf(const StructType* STy, unsigned FieldNo) {
Dan Gohman2544a1d2010-02-01 16:37:38 +00001543 return getOffsetOf(STy, ConstantInt::get(Type::getInt32Ty(STy->getContext()),
1544 FieldNo));
1545}
1546
1547Constant* ConstantExpr::getOffsetOf(const Type* Ty, Constant *FieldNo) {
Dan Gohman3778f212009-08-16 21:26:11 +00001548 // offsetof is implemented as: (i64) gep (Ty*)null, 0, FieldNo
1549 // Note that a non-inbounds gep is used, as null isn't within any object.
1550 Constant *GEPIdx[] = {
Dan Gohman2544a1d2010-02-01 16:37:38 +00001551 ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0),
1552 FieldNo
Dan Gohman3778f212009-08-16 21:26:11 +00001553 };
1554 Constant *GEP = getGetElementPtr(
Dan Gohman2544a1d2010-02-01 16:37:38 +00001555 Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx, 2);
Dan Gohman3778f212009-08-16 21:26:11 +00001556 return getCast(Instruction::PtrToInt, GEP,
Dan Gohman2544a1d2010-02-01 16:37:38 +00001557 Type::getInt64Ty(Ty->getContext()));
Dan Gohman3778f212009-08-16 21:26:11 +00001558}
Owen Andersonbaf3c402009-07-29 18:55:55 +00001559
Reid Spencere4d87aa2006-12-23 06:05:41 +00001560Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencer67263fe2006-12-04 21:35:24 +00001561 Constant *C1, Constant *C2) {
1562 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencere4d87aa2006-12-23 06:05:41 +00001563 return getCompareTy(pred, C1, C2);
Chris Lattnerc3d12f02004-08-04 18:50:09 +00001564}
1565
Chris Lattner08a45cc2004-03-12 05:54:04 +00001566Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
Owen Anderson04fb7c32009-06-20 00:24:58 +00001567 Constant *V1, Constant *V2) {
Chris Lattner9ace0cd2008-12-29 00:16:12 +00001568 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner08a45cc2004-03-12 05:54:04 +00001569
1570 if (ReqTy == V1->getType())
Chris Lattnerb29d5962010-02-01 20:48:08 +00001571 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
Chris Lattner08a45cc2004-03-12 05:54:04 +00001572 return SC; // Fold common cases
1573
1574 std::vector<Constant*> argVec(3, C);
1575 argVec[1] = V1;
1576 argVec[2] = V2;
Reid Spencer077d0eb2006-12-04 05:19:50 +00001577 ExprMapKeyType Key(Instruction::Select, argVec);
Owen Anderson31c36f02009-06-17 20:10:08 +00001578
Owen Andersond03eecd2009-08-04 20:25:11 +00001579 LLVMContextImpl *pImpl = ReqTy->getContext().pImpl;
Owen Andersond03eecd2009-08-04 20:25:11 +00001580 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Chris Lattner08a45cc2004-03-12 05:54:04 +00001581}
1582
Chris Lattnered468e372003-10-05 00:17:43 +00001583Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001584 Value* const *Idxs,
Owen Anderson04fb7c32009-06-20 00:24:58 +00001585 unsigned NumIdx) {
Dan Gohman041e2eb2008-05-15 19:50:34 +00001586 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
1587 Idxs+NumIdx) ==
1588 cast<PointerType>(ReqTy)->getElementType() &&
1589 "GEP indices invalid!");
Chris Lattner2e9bb1a2004-02-16 20:46:13 +00001590
Chris Lattnerb29d5962010-02-01 20:48:08 +00001591 if (Constant *FC = ConstantFoldGetElementPtr(C, /*inBounds=*/false,
1592 (Constant**)Idxs, NumIdx))
Chris Lattnerd628f6a2003-04-17 19:24:48 +00001593 return FC; // Fold a few common cases...
Chris Lattner2e9bb1a2004-02-16 20:46:13 +00001594
Chris Lattnered468e372003-10-05 00:17:43 +00001595 assert(isa<PointerType>(C->getType()) &&
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001596 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001597 // Look up the constant in the table first to ensure uniqueness
Chris Lattner7fa6e662004-10-11 22:52:25 +00001598 std::vector<Constant*> ArgVec;
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001599 ArgVec.reserve(NumIdx+1);
Chris Lattner7fa6e662004-10-11 22:52:25 +00001600 ArgVec.push_back(C);
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001601 for (unsigned i = 0; i != NumIdx; ++i)
1602 ArgVec.push_back(cast<Constant>(Idxs[i]));
1603 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
Owen Anderson31c36f02009-06-17 20:10:08 +00001604
Owen Andersond03eecd2009-08-04 20:25:11 +00001605 LLVMContextImpl *pImpl = ReqTy->getContext().pImpl;
Owen Andersond03eecd2009-08-04 20:25:11 +00001606 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001607}
1608
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001609Constant *ConstantExpr::getInBoundsGetElementPtrTy(const Type *ReqTy,
1610 Constant *C,
Chris Lattner399ac532009-12-08 05:31:46 +00001611 Value *const *Idxs,
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001612 unsigned NumIdx) {
1613 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
1614 Idxs+NumIdx) ==
1615 cast<PointerType>(ReqTy)->getElementType() &&
1616 "GEP indices invalid!");
1617
Chris Lattnerb29d5962010-02-01 20:48:08 +00001618 if (Constant *FC = ConstantFoldGetElementPtr(C, /*inBounds=*/true,
1619 (Constant**)Idxs, NumIdx))
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001620 return FC; // Fold a few common cases...
1621
1622 assert(isa<PointerType>(C->getType()) &&
1623 "Non-pointer type for constant GetElementPtr expression");
1624 // Look up the constant in the table first to ensure uniqueness
1625 std::vector<Constant*> ArgVec;
1626 ArgVec.reserve(NumIdx+1);
1627 ArgVec.push_back(C);
1628 for (unsigned i = 0; i != NumIdx; ++i)
1629 ArgVec.push_back(cast<Constant>(Idxs[i]));
1630 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec, 0,
1631 GEPOperator::IsInBounds);
1632
1633 LLVMContextImpl *pImpl = ReqTy->getContext().pImpl;
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001634 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
1635}
1636
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001637Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
Owen Anderson04fb7c32009-06-20 00:24:58 +00001638 unsigned NumIdx) {
Chris Lattnered468e372003-10-05 00:17:43 +00001639 // Get the result type of the getelementptr!
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001640 const Type *Ty =
Dan Gohman041e2eb2008-05-15 19:50:34 +00001641 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
Chris Lattnered468e372003-10-05 00:17:43 +00001642 assert(Ty && "GEP indices invalid!");
Christopher Lambfe63fb92007-12-11 08:59:05 +00001643 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
Owen Anderson04fb7c32009-06-20 00:24:58 +00001644 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
Chris Lattner7fa6e662004-10-11 22:52:25 +00001645}
1646
Dan Gohman6e7ad952009-09-03 23:34:49 +00001647Constant *ConstantExpr::getInBoundsGetElementPtr(Constant *C,
1648 Value* const *Idxs,
1649 unsigned NumIdx) {
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001650 // Get the result type of the getelementptr!
1651 const Type *Ty =
1652 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
1653 assert(Ty && "GEP indices invalid!");
1654 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
1655 return getInBoundsGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx);
Dan Gohman6e7ad952009-09-03 23:34:49 +00001656}
1657
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001658Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
Owen Anderson04fb7c32009-06-20 00:24:58 +00001659 unsigned NumIdx) {
1660 return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
Chris Lattnered468e372003-10-05 00:17:43 +00001661}
1662
Dan Gohman6e7ad952009-09-03 23:34:49 +00001663Constant *ConstantExpr::getInBoundsGetElementPtr(Constant *C,
1664 Constant* const *Idxs,
1665 unsigned NumIdx) {
1666 return getInBoundsGetElementPtr(C, (Value* const *)Idxs, NumIdx);
1667}
1668
Reid Spencer077d0eb2006-12-04 05:19:50 +00001669Constant *
Nick Lewycky401f3252010-01-21 07:03:21 +00001670ConstantExpr::getICmp(unsigned short pred, Constant *LHS, Constant *RHS) {
Reid Spencer077d0eb2006-12-04 05:19:50 +00001671 assert(LHS->getType() == RHS->getType());
1672 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
1673 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
1674
Chris Lattnerb29d5962010-02-01 20:48:08 +00001675 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spencer077d0eb2006-12-04 05:19:50 +00001676 return FC; // Fold a few common cases...
1677
1678 // Look up the constant in the table first to ensure uniqueness
1679 std::vector<Constant*> ArgVec;
1680 ArgVec.push_back(LHS);
1681 ArgVec.push_back(RHS);
Reid Spencer4fa021a2006-12-24 18:42:29 +00001682 // Get the key type with both the opcode and predicate
Reid Spencer077d0eb2006-12-04 05:19:50 +00001683 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Owen Anderson31c36f02009-06-17 20:10:08 +00001684
Nick Lewycky401f3252010-01-21 07:03:21 +00001685 const Type *ResultTy = Type::getInt1Ty(LHS->getContext());
1686 if (const VectorType *VT = dyn_cast<VectorType>(LHS->getType()))
1687 ResultTy = VectorType::get(ResultTy, VT->getNumElements());
1688
Owen Andersond03eecd2009-08-04 20:25:11 +00001689 LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl;
Nick Lewycky401f3252010-01-21 07:03:21 +00001690 return pImpl->ExprConstants.getOrCreate(ResultTy, Key);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001691}
1692
1693Constant *
Nick Lewycky401f3252010-01-21 07:03:21 +00001694ConstantExpr::getFCmp(unsigned short pred, Constant *LHS, Constant *RHS) {
Reid Spencer077d0eb2006-12-04 05:19:50 +00001695 assert(LHS->getType() == RHS->getType());
1696 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
1697
Chris Lattnerb29d5962010-02-01 20:48:08 +00001698 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spencer077d0eb2006-12-04 05:19:50 +00001699 return FC; // Fold a few common cases...
1700
1701 // Look up the constant in the table first to ensure uniqueness
1702 std::vector<Constant*> ArgVec;
1703 ArgVec.push_back(LHS);
1704 ArgVec.push_back(RHS);
Reid Spencer4fa021a2006-12-24 18:42:29 +00001705 // Get the key type with both the opcode and predicate
Reid Spencer077d0eb2006-12-04 05:19:50 +00001706 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Nick Lewycky401f3252010-01-21 07:03:21 +00001707
1708 const Type *ResultTy = Type::getInt1Ty(LHS->getContext());
1709 if (const VectorType *VT = dyn_cast<VectorType>(LHS->getType()))
1710 ResultTy = VectorType::get(ResultTy, VT->getNumElements());
1711
Owen Andersond03eecd2009-08-04 20:25:11 +00001712 LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl;
Nick Lewycky401f3252010-01-21 07:03:21 +00001713 return pImpl->ExprConstants.getOrCreate(ResultTy, Key);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001714}
1715
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001716Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
1717 Constant *Idx) {
Chris Lattnerb29d5962010-02-01 20:48:08 +00001718 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
Chris Lattner83738a22009-12-30 20:25:09 +00001719 return FC; // Fold a few common cases.
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001720 // Look up the constant in the table first to ensure uniqueness
1721 std::vector<Constant*> ArgVec(1, Val);
1722 ArgVec.push_back(Idx);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001723 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Owen Anderson31c36f02009-06-17 20:10:08 +00001724
Owen Andersond03eecd2009-08-04 20:25:11 +00001725 LLVMContextImpl *pImpl = ReqTy->getContext().pImpl;
Owen Andersond03eecd2009-08-04 20:25:11 +00001726 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001727}
1728
1729Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001730 assert(isa<VectorType>(Val->getType()) &&
Reid Spencerac9dcb92007-02-15 03:39:18 +00001731 "Tried to create extractelement operation on non-vector type!");
Benjamin Kramer11acaa32010-01-05 20:07:06 +00001732 assert(Idx->getType()->isInteger(32) &&
Reid Spencer3d10b0b2007-01-26 07:37:34 +00001733 "Extractelement index must be i32 type!");
Reid Spencer9d6565a2007-02-15 02:26:10 +00001734 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001735 Val, Idx);
1736}
Chris Lattnered468e372003-10-05 00:17:43 +00001737
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001738Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
1739 Constant *Elt, Constant *Idx) {
Chris Lattnerb29d5962010-02-01 20:48:08 +00001740 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
Chris Lattner83738a22009-12-30 20:25:09 +00001741 return FC; // Fold a few common cases.
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001742 // Look up the constant in the table first to ensure uniqueness
1743 std::vector<Constant*> ArgVec(1, Val);
1744 ArgVec.push_back(Elt);
1745 ArgVec.push_back(Idx);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001746 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Owen Anderson31c36f02009-06-17 20:10:08 +00001747
Owen Andersond03eecd2009-08-04 20:25:11 +00001748 LLVMContextImpl *pImpl = ReqTy->getContext().pImpl;
Owen Andersond03eecd2009-08-04 20:25:11 +00001749 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001750}
1751
1752Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1753 Constant *Idx) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001754 assert(isa<VectorType>(Val->getType()) &&
Reid Spencerac9dcb92007-02-15 03:39:18 +00001755 "Tried to create insertelement operation on non-vector type!");
Reid Spencer9d6565a2007-02-15 02:26:10 +00001756 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001757 && "Insertelement types must match!");
Benjamin Kramer11acaa32010-01-05 20:07:06 +00001758 assert(Idx->getType()->isInteger(32) &&
Reid Spencer3d10b0b2007-01-26 07:37:34 +00001759 "Insertelement index must be i32 type!");
Gordon Henriksen699609c2008-08-30 15:41:51 +00001760 return getInsertElementTy(Val->getType(), Val, Elt, Idx);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001761}
1762
Chris Lattner00f10232006-04-08 01:18:18 +00001763Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
1764 Constant *V2, Constant *Mask) {
Chris Lattnerb29d5962010-02-01 20:48:08 +00001765 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
Chris Lattner00f10232006-04-08 01:18:18 +00001766 return FC; // Fold a few common cases...
1767 // Look up the constant in the table first to ensure uniqueness
1768 std::vector<Constant*> ArgVec(1, V1);
1769 ArgVec.push_back(V2);
1770 ArgVec.push_back(Mask);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001771 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Owen Anderson31c36f02009-06-17 20:10:08 +00001772
Owen Andersond03eecd2009-08-04 20:25:11 +00001773 LLVMContextImpl *pImpl = ReqTy->getContext().pImpl;
Owen Andersond03eecd2009-08-04 20:25:11 +00001774 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Chris Lattner00f10232006-04-08 01:18:18 +00001775}
1776
1777Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
1778 Constant *Mask) {
1779 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
1780 "Invalid shuffle vector constant expr operands!");
Nate Begeman0f123cf2009-02-12 21:28:33 +00001781
1782 unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements();
1783 const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
1784 const Type *ShufTy = VectorType::get(EltTy, NElts);
1785 return getShuffleVectorTy(ShufTy, V1, V2, Mask);
Chris Lattner00f10232006-04-08 01:18:18 +00001786}
1787
Dan Gohman041e2eb2008-05-15 19:50:34 +00001788Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg,
1789 Constant *Val,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001790 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman041e2eb2008-05-15 19:50:34 +00001791 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
1792 Idxs+NumIdx) == Val->getType() &&
1793 "insertvalue indices invalid!");
1794 assert(Agg->getType() == ReqTy &&
1795 "insertvalue type invalid!");
Dan Gohmane4569942008-05-23 00:36:11 +00001796 assert(Agg->getType()->isFirstClassType() &&
1797 "Non-first-class type for constant InsertValue expression");
Chris Lattnerb29d5962010-02-01 20:48:08 +00001798 Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs, NumIdx);
Dan Gohmane0891602008-07-21 23:30:30 +00001799 assert(FC && "InsertValue constant expr couldn't be folded!");
1800 return FC;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001801}
1802
1803Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001804 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohmane4569942008-05-23 00:36:11 +00001805 assert(Agg->getType()->isFirstClassType() &&
1806 "Tried to create insertelement operation on non-first-class type!");
Dan Gohman041e2eb2008-05-15 19:50:34 +00001807
Dan Gohmane4569942008-05-23 00:36:11 +00001808 const Type *ReqTy = Agg->getType();
Devang Patelb6dc9352008-11-03 23:20:04 +00001809#ifndef NDEBUG
Dan Gohmane4569942008-05-23 00:36:11 +00001810 const Type *ValTy =
Dan Gohman041e2eb2008-05-15 19:50:34 +00001811 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
Devang Patelb6dc9352008-11-03 23:20:04 +00001812#endif
Dan Gohmane4569942008-05-23 00:36:11 +00001813 assert(ValTy == Val->getType() && "insertvalue indices invalid!");
Dan Gohman041e2eb2008-05-15 19:50:34 +00001814 return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx);
1815}
1816
1817Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001818 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman041e2eb2008-05-15 19:50:34 +00001819 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
1820 Idxs+NumIdx) == ReqTy &&
1821 "extractvalue indices invalid!");
Dan Gohmane4569942008-05-23 00:36:11 +00001822 assert(Agg->getType()->isFirstClassType() &&
1823 "Non-first-class type for constant extractvalue expression");
Chris Lattnerb29d5962010-02-01 20:48:08 +00001824 Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs, NumIdx);
Dan Gohmane0891602008-07-21 23:30:30 +00001825 assert(FC && "ExtractValue constant expr couldn't be folded!");
1826 return FC;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001827}
1828
1829Constant *ConstantExpr::getExtractValue(Constant *Agg,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001830 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohmane4569942008-05-23 00:36:11 +00001831 assert(Agg->getType()->isFirstClassType() &&
1832 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman041e2eb2008-05-15 19:50:34 +00001833
1834 const Type *ReqTy =
1835 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
1836 assert(ReqTy && "extractvalue indices invalid!");
1837 return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx);
1838}
1839
Owen Andersonbaf3c402009-07-29 18:55:55 +00001840Constant* ConstantExpr::getNeg(Constant* C) {
1841 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1842 if (C->getType()->isFPOrFPVector())
1843 return getFNeg(C);
1844 assert(C->getType()->isIntOrIntVector() &&
1845 "Cannot NEG a nonintegral value!");
1846 return get(Instruction::Sub,
1847 ConstantFP::getZeroValueForNegation(C->getType()),
1848 C);
1849}
1850
1851Constant* ConstantExpr::getFNeg(Constant* C) {
1852 assert(C->getType()->isFPOrFPVector() &&
1853 "Cannot FNEG a non-floating-point value!");
1854 return get(Instruction::FSub,
1855 ConstantFP::getZeroValueForNegation(C->getType()),
1856 C);
1857}
1858
1859Constant* ConstantExpr::getNot(Constant* C) {
1860 assert(C->getType()->isIntOrIntVector() &&
1861 "Cannot NOT a nonintegral value!");
Owen Andersona7235ea2009-07-31 20:28:14 +00001862 return get(Instruction::Xor, C, Constant::getAllOnesValue(C->getType()));
Owen Andersonbaf3c402009-07-29 18:55:55 +00001863}
1864
1865Constant* ConstantExpr::getAdd(Constant* C1, Constant* C2) {
1866 return get(Instruction::Add, C1, C2);
1867}
1868
1869Constant* ConstantExpr::getFAdd(Constant* C1, Constant* C2) {
1870 return get(Instruction::FAdd, C1, C2);
1871}
1872
1873Constant* ConstantExpr::getSub(Constant* C1, Constant* C2) {
1874 return get(Instruction::Sub, C1, C2);
1875}
1876
1877Constant* ConstantExpr::getFSub(Constant* C1, Constant* C2) {
1878 return get(Instruction::FSub, C1, C2);
1879}
1880
1881Constant* ConstantExpr::getMul(Constant* C1, Constant* C2) {
1882 return get(Instruction::Mul, C1, C2);
1883}
1884
1885Constant* ConstantExpr::getFMul(Constant* C1, Constant* C2) {
1886 return get(Instruction::FMul, C1, C2);
1887}
1888
1889Constant* ConstantExpr::getUDiv(Constant* C1, Constant* C2) {
1890 return get(Instruction::UDiv, C1, C2);
1891}
1892
1893Constant* ConstantExpr::getSDiv(Constant* C1, Constant* C2) {
1894 return get(Instruction::SDiv, C1, C2);
1895}
1896
1897Constant* ConstantExpr::getFDiv(Constant* C1, Constant* C2) {
1898 return get(Instruction::FDiv, C1, C2);
1899}
1900
1901Constant* ConstantExpr::getURem(Constant* C1, Constant* C2) {
1902 return get(Instruction::URem, C1, C2);
1903}
1904
1905Constant* ConstantExpr::getSRem(Constant* C1, Constant* C2) {
1906 return get(Instruction::SRem, C1, C2);
1907}
1908
1909Constant* ConstantExpr::getFRem(Constant* C1, Constant* C2) {
1910 return get(Instruction::FRem, C1, C2);
1911}
1912
1913Constant* ConstantExpr::getAnd(Constant* C1, Constant* C2) {
1914 return get(Instruction::And, C1, C2);
1915}
1916
1917Constant* ConstantExpr::getOr(Constant* C1, Constant* C2) {
1918 return get(Instruction::Or, C1, C2);
1919}
1920
1921Constant* ConstantExpr::getXor(Constant* C1, Constant* C2) {
1922 return get(Instruction::Xor, C1, C2);
1923}
1924
1925Constant* ConstantExpr::getShl(Constant* C1, Constant* C2) {
1926 return get(Instruction::Shl, C1, C2);
1927}
1928
1929Constant* ConstantExpr::getLShr(Constant* C1, Constant* C2) {
1930 return get(Instruction::LShr, C1, C2);
1931}
1932
1933Constant* ConstantExpr::getAShr(Constant* C1, Constant* C2) {
1934 return get(Instruction::AShr, C1, C2);
1935}
1936
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001937// destroyConstant - Remove the constant from the constant table...
1938//
Owen Anderson04fb7c32009-06-20 00:24:58 +00001939void ConstantExpr::destroyConstant() {
Chris Lattner83738a22009-12-30 20:25:09 +00001940 getType()->getContext().pImpl->ExprConstants.remove(this);
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001941 destroyConstantImpl();
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001942}
1943
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001944const char *ConstantExpr::getOpcodeName() const {
1945 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001946}
Reid Spencer1c9c8e62004-07-17 23:48:33 +00001947
Chris Lattner5cbade92005-10-03 21:58:36 +00001948//===----------------------------------------------------------------------===//
1949// replaceUsesOfWithOnConstant implementations
1950
Chris Lattner54984052007-08-21 00:55:23 +00001951/// replaceUsesOfWithOnConstant - Update this constant array to change uses of
1952/// 'From' to be uses of 'To'. This must update the uniquing data structures
1953/// etc.
1954///
1955/// Note that we intentionally replace all uses of From with To here. Consider
1956/// a large array that uses 'From' 1000 times. By handling this case all here,
1957/// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that
1958/// single invocation handles all 1000 uses. Handling them one at a time would
1959/// work, but would be really slow because it would have to unique each updated
1960/// array instance.
Chris Lattner2ee11ec2009-10-28 00:01:44 +00001961///
Chris Lattner5cbade92005-10-03 21:58:36 +00001962void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00001963 Use *U) {
Owen Anderson1fd70962009-07-28 18:32:17 +00001964 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
1965 Constant *ToC = cast<Constant>(To);
1966
1967 LLVMContext &Context = getType()->getContext();
1968 LLVMContextImpl *pImpl = Context.pImpl;
1969
Dan Gohmane3394d42009-09-15 15:58:07 +00001970 std::pair<LLVMContextImpl::ArrayConstantsTy::MapKey, ConstantArray*> Lookup;
Owen Anderson1fd70962009-07-28 18:32:17 +00001971 Lookup.first.first = getType();
1972 Lookup.second = this;
1973
1974 std::vector<Constant*> &Values = Lookup.first.second;
1975 Values.reserve(getNumOperands()); // Build replacement array.
1976
1977 // Fill values with the modified operands of the constant array. Also,
1978 // compute whether this turns into an all-zeros array.
1979 bool isAllZeros = false;
1980 unsigned NumUpdated = 0;
1981 if (!ToC->isNullValue()) {
1982 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1983 Constant *Val = cast<Constant>(O->get());
1984 if (Val == From) {
1985 Val = ToC;
1986 ++NumUpdated;
1987 }
1988 Values.push_back(Val);
1989 }
1990 } else {
1991 isAllZeros = true;
1992 for (Use *O = OperandList, *E = OperandList+getNumOperands();O != E; ++O) {
1993 Constant *Val = cast<Constant>(O->get());
1994 if (Val == From) {
1995 Val = ToC;
1996 ++NumUpdated;
1997 }
1998 Values.push_back(Val);
1999 if (isAllZeros) isAllZeros = Val->isNullValue();
2000 }
2001 }
2002
2003 Constant *Replacement = 0;
2004 if (isAllZeros) {
Owen Anderson9e9a0d52009-07-30 23:03:37 +00002005 Replacement = ConstantAggregateZero::get(getType());
Owen Anderson1fd70962009-07-28 18:32:17 +00002006 } else {
2007 // Check to see if we have this array type already.
Owen Anderson1fd70962009-07-28 18:32:17 +00002008 bool Exists;
2009 LLVMContextImpl::ArrayConstantsTy::MapTy::iterator I =
2010 pImpl->ArrayConstants.InsertOrGetItem(Lookup, Exists);
2011
2012 if (Exists) {
Devang Patel5f4ac842009-09-03 01:39:20 +00002013 Replacement = I->second;
Owen Anderson1fd70962009-07-28 18:32:17 +00002014 } else {
2015 // Okay, the new shape doesn't exist in the system yet. Instead of
2016 // creating a new constant array, inserting it, replaceallusesof'ing the
2017 // old with the new, then deleting the old... just update the current one
2018 // in place!
2019 pImpl->ArrayConstants.MoveConstantToNewSlot(this, I);
2020
2021 // Update to the new value. Optimize for the case when we have a single
2022 // operand that we're changing, but handle bulk updates efficiently.
2023 if (NumUpdated == 1) {
2024 unsigned OperandToUpdate = U - OperandList;
2025 assert(getOperand(OperandToUpdate) == From &&
2026 "ReplaceAllUsesWith broken!");
2027 setOperand(OperandToUpdate, ToC);
2028 } else {
2029 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2030 if (getOperand(i) == From)
2031 setOperand(i, ToC);
2032 }
2033 return;
2034 }
2035 }
Chris Lattnercea141f2005-10-03 22:51:37 +00002036
2037 // Otherwise, I do need to replace this with an existing value.
Chris Lattner5cbade92005-10-03 21:58:36 +00002038 assert(Replacement != this && "I didn't contain From!");
2039
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002040 // Everyone using this now uses the replacement.
2041 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattner5cbade92005-10-03 21:58:36 +00002042
2043 // Delete the old constant!
2044 destroyConstant();
2045}
2046
2047void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002048 Use *U) {
Owen Anderson8fa33382009-07-27 22:29:26 +00002049 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2050 Constant *ToC = cast<Constant>(To);
2051
2052 unsigned OperandToUpdate = U-OperandList;
2053 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
2054
Dan Gohmane3394d42009-09-15 15:58:07 +00002055 std::pair<LLVMContextImpl::StructConstantsTy::MapKey, ConstantStruct*> Lookup;
Owen Anderson8fa33382009-07-27 22:29:26 +00002056 Lookup.first.first = getType();
2057 Lookup.second = this;
2058 std::vector<Constant*> &Values = Lookup.first.second;
2059 Values.reserve(getNumOperands()); // Build replacement struct.
2060
2061
2062 // Fill values with the modified operands of the constant struct. Also,
2063 // compute whether this turns into an all-zeros struct.
2064 bool isAllZeros = false;
2065 if (!ToC->isNullValue()) {
2066 for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O)
2067 Values.push_back(cast<Constant>(O->get()));
2068 } else {
2069 isAllZeros = true;
2070 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2071 Constant *Val = cast<Constant>(O->get());
2072 Values.push_back(Val);
2073 if (isAllZeros) isAllZeros = Val->isNullValue();
2074 }
2075 }
2076 Values[OperandToUpdate] = ToC;
2077
2078 LLVMContext &Context = getType()->getContext();
2079 LLVMContextImpl *pImpl = Context.pImpl;
2080
2081 Constant *Replacement = 0;
2082 if (isAllZeros) {
Owen Anderson9e9a0d52009-07-30 23:03:37 +00002083 Replacement = ConstantAggregateZero::get(getType());
Owen Anderson8fa33382009-07-27 22:29:26 +00002084 } else {
2085 // Check to see if we have this array type already.
Owen Anderson8fa33382009-07-27 22:29:26 +00002086 bool Exists;
2087 LLVMContextImpl::StructConstantsTy::MapTy::iterator I =
2088 pImpl->StructConstants.InsertOrGetItem(Lookup, Exists);
2089
2090 if (Exists) {
Devang Patel5f4ac842009-09-03 01:39:20 +00002091 Replacement = I->second;
Owen Anderson8fa33382009-07-27 22:29:26 +00002092 } else {
2093 // Okay, the new shape doesn't exist in the system yet. Instead of
2094 // creating a new constant struct, inserting it, replaceallusesof'ing the
2095 // old with the new, then deleting the old... just update the current one
2096 // in place!
2097 pImpl->StructConstants.MoveConstantToNewSlot(this, I);
2098
2099 // Update to the new value.
2100 setOperand(OperandToUpdate, ToC);
2101 return;
2102 }
2103 }
2104
2105 assert(Replacement != this && "I didn't contain From!");
Chris Lattner5cbade92005-10-03 21:58:36 +00002106
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002107 // Everyone using this now uses the replacement.
2108 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattner5cbade92005-10-03 21:58:36 +00002109
2110 // Delete the old constant!
2111 destroyConstant();
2112}
2113
Chris Lattnerfdfeb692010-02-12 20:49:41 +00002114void ConstantUnion::replaceUsesOfWithOnConstant(Value *From, Value *To,
2115 Use *U) {
2116 assert(false && "Implement replaceUsesOfWithOnConstant for unions");
2117}
2118
Reid Spencer9d6565a2007-02-15 02:26:10 +00002119void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002120 Use *U) {
Chris Lattner5cbade92005-10-03 21:58:36 +00002121 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2122
2123 std::vector<Constant*> Values;
2124 Values.reserve(getNumOperands()); // Build replacement array...
2125 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2126 Constant *Val = getOperand(i);
2127 if (Val == From) Val = cast<Constant>(To);
2128 Values.push_back(Val);
2129 }
2130
Owen Andersonaf7ec972009-07-28 21:19:26 +00002131 Constant *Replacement = get(getType(), Values);
Chris Lattner5cbade92005-10-03 21:58:36 +00002132 assert(Replacement != this && "I didn't contain From!");
2133
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002134 // Everyone using this now uses the replacement.
2135 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattner5cbade92005-10-03 21:58:36 +00002136
2137 // Delete the old constant!
2138 destroyConstant();
2139}
2140
2141void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002142 Use *U) {
Chris Lattner5cbade92005-10-03 21:58:36 +00002143 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2144 Constant *To = cast<Constant>(ToV);
2145
2146 Constant *Replacement = 0;
2147 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerf9021ff2007-02-19 20:01:23 +00002148 SmallVector<Constant*, 8> Indices;
Chris Lattner5cbade92005-10-03 21:58:36 +00002149 Constant *Pointer = getOperand(0);
2150 Indices.reserve(getNumOperands()-1);
2151 if (Pointer == From) Pointer = To;
2152
2153 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2154 Constant *Val = getOperand(i);
2155 if (Val == From) Val = To;
2156 Indices.push_back(Val);
2157 }
Chris Lattnerf9021ff2007-02-19 20:01:23 +00002158 Replacement = ConstantExpr::getGetElementPtr(Pointer,
2159 &Indices[0], Indices.size());
Dan Gohman041e2eb2008-05-15 19:50:34 +00002160 } else if (getOpcode() == Instruction::ExtractValue) {
Dan Gohman041e2eb2008-05-15 19:50:34 +00002161 Constant *Agg = getOperand(0);
Dan Gohman041e2eb2008-05-15 19:50:34 +00002162 if (Agg == From) Agg = To;
2163
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002164 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman041e2eb2008-05-15 19:50:34 +00002165 Replacement = ConstantExpr::getExtractValue(Agg,
2166 &Indices[0], Indices.size());
2167 } else if (getOpcode() == Instruction::InsertValue) {
Dan Gohman041e2eb2008-05-15 19:50:34 +00002168 Constant *Agg = getOperand(0);
2169 Constant *Val = getOperand(1);
Dan Gohman041e2eb2008-05-15 19:50:34 +00002170 if (Agg == From) Agg = To;
2171 if (Val == From) Val = To;
2172
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002173 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman041e2eb2008-05-15 19:50:34 +00002174 Replacement = ConstantExpr::getInsertValue(Agg, Val,
2175 &Indices[0], Indices.size());
Reid Spencer3da59db2006-11-27 01:05:10 +00002176 } else if (isCast()) {
Chris Lattner5cbade92005-10-03 21:58:36 +00002177 assert(getOperand(0) == From && "Cast only has one use!");
Reid Spencer3da59db2006-11-27 01:05:10 +00002178 Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
Chris Lattner5cbade92005-10-03 21:58:36 +00002179 } else if (getOpcode() == Instruction::Select) {
2180 Constant *C1 = getOperand(0);
2181 Constant *C2 = getOperand(1);
2182 Constant *C3 = getOperand(2);
2183 if (C1 == From) C1 = To;
2184 if (C2 == From) C2 = To;
2185 if (C3 == From) C3 = To;
2186 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00002187 } else if (getOpcode() == Instruction::ExtractElement) {
2188 Constant *C1 = getOperand(0);
2189 Constant *C2 = getOperand(1);
2190 if (C1 == From) C1 = To;
2191 if (C2 == From) C2 = To;
2192 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattner42b55802006-04-08 05:09:48 +00002193 } else if (getOpcode() == Instruction::InsertElement) {
2194 Constant *C1 = getOperand(0);
2195 Constant *C2 = getOperand(1);
2196 Constant *C3 = getOperand(1);
2197 if (C1 == From) C1 = To;
2198 if (C2 == From) C2 = To;
2199 if (C3 == From) C3 = To;
2200 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2201 } else if (getOpcode() == Instruction::ShuffleVector) {
2202 Constant *C1 = getOperand(0);
2203 Constant *C2 = getOperand(1);
2204 Constant *C3 = getOperand(2);
2205 if (C1 == From) C1 = To;
2206 if (C2 == From) C2 = To;
2207 if (C3 == From) C3 = To;
2208 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spencer077d0eb2006-12-04 05:19:50 +00002209 } else if (isCompare()) {
2210 Constant *C1 = getOperand(0);
2211 Constant *C2 = getOperand(1);
2212 if (C1 == From) C1 = To;
2213 if (C2 == From) C2 = To;
2214 if (getOpcode() == Instruction::ICmp)
2215 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
Chris Lattnerbbedb0e2008-07-14 05:17:31 +00002216 else {
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002217 assert(getOpcode() == Instruction::FCmp);
2218 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnerbbedb0e2008-07-14 05:17:31 +00002219 }
Chris Lattner5cbade92005-10-03 21:58:36 +00002220 } else if (getNumOperands() == 2) {
2221 Constant *C1 = getOperand(0);
2222 Constant *C2 = getOperand(1);
2223 if (C1 == From) C1 = To;
2224 if (C2 == From) C2 = To;
Chris Lattnercafe9bb2009-12-29 02:14:09 +00002225 Replacement = ConstantExpr::get(getOpcode(), C1, C2, SubclassOptionalData);
Chris Lattner5cbade92005-10-03 21:58:36 +00002226 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00002227 llvm_unreachable("Unknown ConstantExpr type!");
Chris Lattner5cbade92005-10-03 21:58:36 +00002228 return;
2229 }
2230
2231 assert(Replacement != this && "I didn't contain From!");
2232
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002233 // Everyone using this now uses the replacement.
2234 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattner5cbade92005-10-03 21:58:36 +00002235
2236 // Delete the old constant!
2237 destroyConstant();
Matthijs Kooijman10b9de62008-07-03 07:46:41 +00002238}