blob: 246fde1569aebeafe5e0db88c2fadeb7692e65cd [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 Lattnereb59ca92011-02-07 20:03:14 +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...
Chris Lattner38211762009-10-30 22:33:29 +000043Constant *Constant::getNullValue(const Type *Ty) {
Owen Andersona7235ea2009-07-31 20:28:14 +000044 switch (Ty->getTypeID()) {
45 case Type::IntegerTyID:
46 return ConstantInt::get(Ty, 0);
47 case Type::FloatTyID:
Benjamin Kramer98383962010-12-04 14:22:24 +000048 return ConstantFP::get(Ty->getContext(),
49 APFloat::getZero(APFloat::IEEEsingle));
Owen Andersona7235ea2009-07-31 20:28:14 +000050 case Type::DoubleTyID:
Benjamin Kramer98383962010-12-04 14:22:24 +000051 return ConstantFP::get(Ty->getContext(),
52 APFloat::getZero(APFloat::IEEEdouble));
Owen Andersona7235ea2009-07-31 20:28:14 +000053 case Type::X86_FP80TyID:
Benjamin Kramer98383962010-12-04 14:22:24 +000054 return ConstantFP::get(Ty->getContext(),
55 APFloat::getZero(APFloat::x87DoubleExtended));
Owen Andersona7235ea2009-07-31 20:28:14 +000056 case Type::FP128TyID:
57 return ConstantFP::get(Ty->getContext(),
Benjamin Kramer98383962010-12-04 14:22:24 +000058 APFloat::getZero(APFloat::IEEEquad));
Owen Andersona7235ea2009-07-31 20:28:14 +000059 case Type::PPC_FP128TyID:
Benjamin Kramer98383962010-12-04 14:22:24 +000060 return ConstantFP::get(Ty->getContext(),
Benjamin Kramer299ee182010-12-04 14:43:08 +000061 APFloat(APInt::getNullValue(128)));
Owen Andersona7235ea2009-07-31 20:28:14 +000062 case Type::PointerTyID:
63 return ConstantPointerNull::get(cast<PointerType>(Ty));
64 case Type::StructTyID:
65 case Type::ArrayTyID:
66 case Type::VectorTyID:
67 return ConstantAggregateZero::get(Ty);
68 default:
69 // Function, Label, or Opaque type?
70 assert(!"Cannot create a null constant of that type!");
71 return 0;
72 }
73}
74
Chris Lattnerf067d582011-02-07 16:40:21 +000075Constant *Constant::getIntegerValue(const Type *Ty, const APInt &V) {
Dan Gohman43ee5f72009-08-03 22:07:33 +000076 const Type *ScalarTy = Ty->getScalarType();
77
78 // Create the base integer constant.
79 Constant *C = ConstantInt::get(Ty->getContext(), V);
80
81 // Convert an integer to a pointer, if necessary.
82 if (const PointerType *PTy = dyn_cast<PointerType>(ScalarTy))
83 C = ConstantExpr::getIntToPtr(C, PTy);
84
85 // Broadcast a scalar to a vector, if necessary.
86 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
87 C = ConstantVector::get(std::vector<Constant *>(VTy->getNumElements(), C));
88
89 return C;
90}
91
Chris Lattnerf067d582011-02-07 16:40:21 +000092Constant *Constant::getAllOnesValue(const Type *Ty) {
Chris Lattner38211762009-10-30 22:33:29 +000093 if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty))
Owen Andersona7235ea2009-07-31 20:28:14 +000094 return ConstantInt::get(Ty->getContext(),
95 APInt::getAllOnesValue(ITy->getBitWidth()));
Nadav Rotem093399c2011-02-17 21:22:27 +000096
97 if (Ty->isFloatingPointTy()) {
98 APFloat FL = APFloat::getAllOnesValue(Ty->getPrimitiveSizeInBits(),
99 !Ty->isPPC_FP128Ty());
100 return ConstantFP::get(Ty->getContext(), FL);
101 }
102
Chris Lattner2ca5c862011-02-15 00:14:00 +0000103 SmallVector<Constant*, 16> Elts;
Chris Lattner38211762009-10-30 22:33:29 +0000104 const VectorType *VTy = cast<VectorType>(Ty);
Owen Andersona7235ea2009-07-31 20:28:14 +0000105 Elts.resize(VTy->getNumElements(), getAllOnesValue(VTy->getElementType()));
106 assert(Elts[0] && "Not a vector integer type!");
107 return cast<ConstantVector>(ConstantVector::get(Elts));
108}
109
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000110void Constant::destroyConstantImpl() {
111 // When a Constant is destroyed, there may be lingering
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000112 // references to the constant by other constants in the constant pool. These
Misha Brukmanef6a6a62003-08-21 22:14:26 +0000113 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000114 // but they don't know that. Because we only find out when the CPV is
115 // deleted, we must now notify all of our users (that should only be
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000116 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000117 //
118 while (!use_empty()) {
119 Value *V = use_back();
120#ifndef NDEBUG // Only in -g mode...
Chris Lattner37f077a2009-08-23 04:02:03 +0000121 if (!isa<Constant>(V)) {
David Greened2e63b72010-01-05 01:29:19 +0000122 dbgs() << "While deleting: " << *this
Chris Lattner37f077a2009-08-23 04:02:03 +0000123 << "\n\nUse still stuck around after Def is destroyed: "
124 << *V << "\n\n";
125 }
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000126#endif
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000127 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1c9c8e62004-07-17 23:48:33 +0000128 Constant *CV = cast<Constant>(V);
129 CV->destroyConstant();
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000130
131 // The constant should remove itself from our use list...
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000132 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerf5ec48d2001-10-13 06:57:33 +0000133 }
134
135 // Value has no outstanding references it is safe to delete it now...
136 delete this;
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000137}
Chris Lattner00950542001-06-06 20:29:01 +0000138
Chris Lattner35b89fa2006-10-20 00:27:06 +0000139/// canTrap - Return true if evaluation of this constant could trap. This is
140/// true for things like constant expressions that could divide by zero.
141bool Constant::canTrap() const {
142 assert(getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
143 // The only thing that could possibly trap are constant exprs.
144 const ConstantExpr *CE = dyn_cast<ConstantExpr>(this);
145 if (!CE) return false;
146
147 // ConstantExpr traps if any operands can trap.
148 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Chris Lattner0eeb9132009-10-28 05:14:34 +0000149 if (CE->getOperand(i)->canTrap())
Chris Lattner35b89fa2006-10-20 00:27:06 +0000150 return true;
151
152 // Otherwise, only specific operations can trap.
153 switch (CE->getOpcode()) {
154 default:
155 return false;
Reid Spencer1628cec2006-10-26 06:15:43 +0000156 case Instruction::UDiv:
157 case Instruction::SDiv:
158 case Instruction::FDiv:
Reid Spencer0a783f72006-11-02 01:53:59 +0000159 case Instruction::URem:
160 case Instruction::SRem:
161 case Instruction::FRem:
Chris Lattner35b89fa2006-10-20 00:27:06 +0000162 // Div and rem can trap if the RHS is not known to be non-zero.
Chris Lattner0eeb9132009-10-28 05:14:34 +0000163 if (!isa<ConstantInt>(CE->getOperand(1)) ||CE->getOperand(1)->isNullValue())
Chris Lattner35b89fa2006-10-20 00:27:06 +0000164 return true;
165 return false;
166 }
167}
168
Chris Lattner4a7642e2009-11-01 18:11:50 +0000169/// isConstantUsed - Return true if the constant has users other than constant
170/// exprs and other dangling things.
171bool Constant::isConstantUsed() const {
Gabor Greif60ad7812010-03-25 23:06:16 +0000172 for (const_use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Chris Lattner4a7642e2009-11-01 18:11:50 +0000173 const Constant *UC = dyn_cast<Constant>(*UI);
174 if (UC == 0 || isa<GlobalValue>(UC))
175 return true;
176
177 if (UC->isConstantUsed())
178 return true;
179 }
180 return false;
181}
182
183
Chris Lattner7cf12c72009-07-22 00:05:44 +0000184
185/// getRelocationInfo - This method classifies the entry according to
186/// whether or not it may generate a relocation entry. This must be
187/// conservative, so if it might codegen to a relocatable entry, it should say
188/// so. The return values are:
189///
Chris Lattner083a1e02009-07-24 03:27:21 +0000190/// NoRelocation: This constant pool entry is guaranteed to never have a
191/// relocation applied to it (because it holds a simple constant like
192/// '4').
193/// LocalRelocation: This entry has relocations, but the entries are
194/// guaranteed to be resolvable by the static linker, so the dynamic
195/// linker will never see them.
196/// GlobalRelocations: This entry may have arbitrary relocations.
Chris Lattner7cf12c72009-07-22 00:05:44 +0000197///
198/// FIXME: This really should not be in VMCore.
Chris Lattner083a1e02009-07-24 03:27:21 +0000199Constant::PossibleRelocationsTy Constant::getRelocationInfo() const {
200 if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
Chris Lattner7cf12c72009-07-22 00:05:44 +0000201 if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
Chris Lattner083a1e02009-07-24 03:27:21 +0000202 return LocalRelocation; // Local to this file/library.
203 return GlobalRelocations; // Global reference.
Anton Korobeynikovab267a22009-03-29 17:13:18 +0000204 }
Chris Lattner7cf12c72009-07-22 00:05:44 +0000205
Chris Lattner5d81bef2009-10-28 04:12:16 +0000206 if (const BlockAddress *BA = dyn_cast<BlockAddress>(this))
207 return BA->getFunction()->getRelocationInfo();
208
Chris Lattner5099b312010-01-03 18:09:40 +0000209 // While raw uses of blockaddress need to be relocated, differences between
210 // two of them don't when they are for labels in the same function. This is a
211 // common idiom when creating a table for the indirect goto extension, so we
212 // handle it efficiently here.
213 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(this))
214 if (CE->getOpcode() == Instruction::Sub) {
215 ConstantExpr *LHS = dyn_cast<ConstantExpr>(CE->getOperand(0));
216 ConstantExpr *RHS = dyn_cast<ConstantExpr>(CE->getOperand(1));
217 if (LHS && RHS &&
218 LHS->getOpcode() == Instruction::PtrToInt &&
219 RHS->getOpcode() == Instruction::PtrToInt &&
220 isa<BlockAddress>(LHS->getOperand(0)) &&
221 isa<BlockAddress>(RHS->getOperand(0)) &&
222 cast<BlockAddress>(LHS->getOperand(0))->getFunction() ==
223 cast<BlockAddress>(RHS->getOperand(0))->getFunction())
224 return NoRelocation;
225 }
226
Chris Lattner083a1e02009-07-24 03:27:21 +0000227 PossibleRelocationsTy Result = NoRelocation;
Evan Chengafe15812007-03-08 00:59:12 +0000228 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Chris Lattner0eeb9132009-10-28 05:14:34 +0000229 Result = std::max(Result,
230 cast<Constant>(getOperand(i))->getRelocationInfo());
Chris Lattner7cf12c72009-07-22 00:05:44 +0000231
232 return Result;
Evan Chengafe15812007-03-08 00:59:12 +0000233}
234
Chris Lattner7cf12c72009-07-22 00:05:44 +0000235
Chris Lattner86381442008-07-10 00:28:11 +0000236/// getVectorElements - This method, which is only valid on constant of vector
237/// type, returns the elements of the vector in the specified smallvector.
Chris Lattner071aade2008-07-14 05:10:41 +0000238/// This handles breaking down a vector undef into undef elements, etc. For
239/// constant exprs and other cases we can't handle, we return an empty vector.
Chris Lattnerb29d5962010-02-01 20:48:08 +0000240void Constant::getVectorElements(SmallVectorImpl<Constant*> &Elts) const {
Duncan Sands1df98592010-02-16 11:11:14 +0000241 assert(getType()->isVectorTy() && "Not a vector constant!");
Chris Lattner86381442008-07-10 00:28:11 +0000242
243 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) {
244 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i)
245 Elts.push_back(CV->getOperand(i));
246 return;
247 }
248
249 const VectorType *VT = cast<VectorType>(getType());
250 if (isa<ConstantAggregateZero>(this)) {
251 Elts.assign(VT->getNumElements(),
Owen Andersona7235ea2009-07-31 20:28:14 +0000252 Constant::getNullValue(VT->getElementType()));
Chris Lattner86381442008-07-10 00:28:11 +0000253 return;
254 }
255
Chris Lattner071aade2008-07-14 05:10:41 +0000256 if (isa<UndefValue>(this)) {
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000257 Elts.assign(VT->getNumElements(), UndefValue::get(VT->getElementType()));
Chris Lattner071aade2008-07-14 05:10:41 +0000258 return;
259 }
260
261 // Unknown type, must be constant expr etc.
Chris Lattner86381442008-07-10 00:28:11 +0000262}
263
264
Chris Lattner13fb0db2011-02-18 04:41:42 +0000265/// removeDeadUsersOfConstant - If the specified constantexpr is dead, remove
266/// it. This involves recursively eliminating any dead users of the
267/// constantexpr.
268static bool removeDeadUsersOfConstant(const Constant *C) {
269 if (isa<GlobalValue>(C)) return false; // Cannot remove this
270
271 while (!C->use_empty()) {
272 const Constant *User = dyn_cast<Constant>(C->use_back());
273 if (!User) return false; // Non-constant usage;
274 if (!removeDeadUsersOfConstant(User))
275 return false; // Constant wasn't dead
276 }
277
278 const_cast<Constant*>(C)->destroyConstant();
279 return true;
280}
281
282
283/// removeDeadConstantUsers - If there are any dead constant users dangling
284/// off of this constant, remove them. This method is useful for clients
285/// that want to check to see if a global is unused, but don't want to deal
286/// with potentially dead constants hanging off of the globals.
287void Constant::removeDeadConstantUsers() const {
288 Value::const_use_iterator I = use_begin(), E = use_end();
289 Value::const_use_iterator LastNonDeadUser = E;
290 while (I != E) {
291 const Constant *User = dyn_cast<Constant>(*I);
292 if (User == 0) {
293 LastNonDeadUser = I;
294 ++I;
295 continue;
296 }
297
298 if (!removeDeadUsersOfConstant(User)) {
299 // If the constant wasn't dead, remember that this was the last live use
300 // and move on to the next constant.
301 LastNonDeadUser = I;
302 ++I;
303 continue;
304 }
305
306 // If the constant was dead, then the iterator is invalidated.
307 if (LastNonDeadUser == E) {
308 I = use_begin();
309 if (I == E) break;
310 } else {
311 I = LastNonDeadUser;
312 ++I;
313 }
314 }
315}
316
317
Chris Lattner86381442008-07-10 00:28:11 +0000318
Chris Lattner00950542001-06-06 20:29:01 +0000319//===----------------------------------------------------------------------===//
Chris Lattner6b6f6ba2007-02-20 06:39:57 +0000320// ConstantInt
Chris Lattner00950542001-06-06 20:29:01 +0000321//===----------------------------------------------------------------------===//
322
Reid Spencer532d0ce2007-02-26 23:54:03 +0000323ConstantInt::ConstantInt(const IntegerType *Ty, const APInt& V)
Chris Lattnereb41bdd2007-02-20 05:55:46 +0000324 : Constant(Ty, ConstantIntVal, 0, 0), Val(V) {
Reid Spencer532d0ce2007-02-26 23:54:03 +0000325 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
Chris Lattner00950542001-06-06 20:29:01 +0000326}
327
Owen Anderson5defacc2009-07-31 17:39:07 +0000328ConstantInt* ConstantInt::getTrue(LLVMContext &Context) {
329 LLVMContextImpl *pImpl = Context.pImpl;
Benjamin Kramerf601d6d2010-11-20 18:43:35 +0000330 if (!pImpl->TheTrueVal)
331 pImpl->TheTrueVal = ConstantInt::get(Type::getInt1Ty(Context), 1);
332 return pImpl->TheTrueVal;
Owen Anderson5defacc2009-07-31 17:39:07 +0000333}
334
335ConstantInt* ConstantInt::getFalse(LLVMContext &Context) {
336 LLVMContextImpl *pImpl = Context.pImpl;
Benjamin Kramerf601d6d2010-11-20 18:43:35 +0000337 if (!pImpl->TheFalseVal)
338 pImpl->TheFalseVal = ConstantInt::get(Type::getInt1Ty(Context), 0);
339 return pImpl->TheFalseVal;
Owen Anderson5defacc2009-07-31 17:39:07 +0000340}
341
342
Owen Andersoneed707b2009-07-24 23:12:02 +0000343// Get a ConstantInt from an APInt. Note that the value stored in the DenseMap
344// as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the
345// operator== and operator!= to ensure that the DenseMap doesn't attempt to
346// compare APInt's of different widths, which would violate an APInt class
347// invariant which generates an assertion.
348ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt& V) {
349 // Get the corresponding integer type for the bit width of the value.
Owen Anderson1d0be152009-08-13 21:58:54 +0000350 const IntegerType *ITy = IntegerType::get(Context, V.getBitWidth());
Owen Andersoneed707b2009-07-24 23:12:02 +0000351 // get an existing value or the insertion position
352 DenseMapAPIntKeyInfo::KeyTy Key(V, ITy);
Owen Andersoneed707b2009-07-24 23:12:02 +0000353 ConstantInt *&Slot = Context.pImpl->IntConstants[Key];
Owen Anderson59d5aac2009-10-19 20:11:52 +0000354 if (!Slot) Slot = new ConstantInt(ITy, V);
355 return Slot;
Owen Andersoneed707b2009-07-24 23:12:02 +0000356}
357
Chris Lattnerf067d582011-02-07 16:40:21 +0000358Constant *ConstantInt::get(const Type* Ty, uint64_t V, bool isSigned) {
Owen Andersoneed707b2009-07-24 23:12:02 +0000359 Constant *C = get(cast<IntegerType>(Ty->getScalarType()),
360 V, isSigned);
361
362 // For vectors, broadcast the value.
363 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
Chris Lattner2ca5c862011-02-15 00:14:00 +0000364 return ConstantVector::get(SmallVector<Constant*,
365 16>(VTy->getNumElements(), C));
Owen Andersoneed707b2009-07-24 23:12:02 +0000366
367 return C;
368}
369
370ConstantInt* ConstantInt::get(const IntegerType* Ty, uint64_t V,
371 bool isSigned) {
372 return get(Ty->getContext(), APInt(Ty->getBitWidth(), V, isSigned));
373}
374
375ConstantInt* ConstantInt::getSigned(const IntegerType* Ty, int64_t V) {
376 return get(Ty, V, true);
377}
378
379Constant *ConstantInt::getSigned(const Type *Ty, int64_t V) {
380 return get(Ty, V, true);
381}
382
Chris Lattnerf067d582011-02-07 16:40:21 +0000383Constant *ConstantInt::get(const Type* Ty, const APInt& V) {
Owen Andersoneed707b2009-07-24 23:12:02 +0000384 ConstantInt *C = get(Ty->getContext(), V);
385 assert(C->getType() == Ty->getScalarType() &&
386 "ConstantInt type doesn't match the type implied by its value!");
387
388 // For vectors, broadcast the value.
389 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
Owen Andersonaf7ec972009-07-28 21:19:26 +0000390 return ConstantVector::get(
Chris Lattner2ca5c862011-02-15 00:14:00 +0000391 SmallVector<Constant *, 16>(VTy->getNumElements(), C));
Owen Andersoneed707b2009-07-24 23:12:02 +0000392
393 return C;
394}
395
Daniel Dunbar2928c832009-11-06 10:58:06 +0000396ConstantInt* ConstantInt::get(const IntegerType* Ty, StringRef Str,
Erick Tryzelaar0e81f662009-08-16 23:36:33 +0000397 uint8_t radix) {
398 return get(Ty->getContext(), APInt(Ty->getBitWidth(), Str, radix));
399}
400
Chris Lattner6b6f6ba2007-02-20 06:39:57 +0000401//===----------------------------------------------------------------------===//
Chris Lattner9b4ee0c2007-02-20 07:17:17 +0000402// ConstantFP
Chris Lattner6b6f6ba2007-02-20 06:39:57 +0000403//===----------------------------------------------------------------------===//
404
Rafael Espindola87d1f472009-07-15 17:40:42 +0000405static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000406 if (Ty->isFloatTy())
Rafael Espindola87d1f472009-07-15 17:40:42 +0000407 return &APFloat::IEEEsingle;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000408 if (Ty->isDoubleTy())
Rafael Espindola87d1f472009-07-15 17:40:42 +0000409 return &APFloat::IEEEdouble;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000410 if (Ty->isX86_FP80Ty())
Rafael Espindola87d1f472009-07-15 17:40:42 +0000411 return &APFloat::x87DoubleExtended;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000412 else if (Ty->isFP128Ty())
Rafael Espindola87d1f472009-07-15 17:40:42 +0000413 return &APFloat::IEEEquad;
414
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000415 assert(Ty->isPPC_FP128Ty() && "Unknown FP format");
Rafael Espindola87d1f472009-07-15 17:40:42 +0000416 return &APFloat::PPCDoubleDouble;
417}
418
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000419/// get() - This returns a constant fp for the specified value in the
420/// specified type. This should only be used for simple constant values like
421/// 2.0/1.0 etc, that are known-valid both as double and as the target format.
Chris Lattnerf067d582011-02-07 16:40:21 +0000422Constant *ConstantFP::get(const Type* Ty, double V) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000423 LLVMContext &Context = Ty->getContext();
424
425 APFloat FV(V);
426 bool ignored;
427 FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
428 APFloat::rmNearestTiesToEven, &ignored);
429 Constant *C = get(Context, FV);
430
431 // For vectors, broadcast the value.
432 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
Owen Andersonaf7ec972009-07-28 21:19:26 +0000433 return ConstantVector::get(
Chris Lattner2ca5c862011-02-15 00:14:00 +0000434 SmallVector<Constant *, 16>(VTy->getNumElements(), C));
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000435
436 return C;
437}
438
Erick Tryzelaar0e81f662009-08-16 23:36:33 +0000439
Chris Lattnerf067d582011-02-07 16:40:21 +0000440Constant *ConstantFP::get(const Type* Ty, StringRef Str) {
Erick Tryzelaar0e81f662009-08-16 23:36:33 +0000441 LLVMContext &Context = Ty->getContext();
442
443 APFloat FV(*TypeToFloatSemantics(Ty->getScalarType()), Str);
444 Constant *C = get(Context, FV);
445
446 // For vectors, broadcast the value.
447 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
448 return ConstantVector::get(
Chris Lattner2ca5c862011-02-15 00:14:00 +0000449 SmallVector<Constant *, 16>(VTy->getNumElements(), C));
Erick Tryzelaar0e81f662009-08-16 23:36:33 +0000450
451 return C;
452}
453
454
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000455ConstantFP* ConstantFP::getNegativeZero(const Type* Ty) {
456 LLVMContext &Context = Ty->getContext();
Owen Andersona7235ea2009-07-31 20:28:14 +0000457 APFloat apf = cast <ConstantFP>(Constant::getNullValue(Ty))->getValueAPF();
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000458 apf.changeSign();
459 return get(Context, apf);
460}
461
462
Chris Lattnerf067d582011-02-07 16:40:21 +0000463Constant *ConstantFP::getZeroValueForNegation(const Type* Ty) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000464 if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000465 if (PTy->getElementType()->isFloatingPointTy()) {
Chris Lattner2ca5c862011-02-15 00:14:00 +0000466 SmallVector<Constant*, 16> zeros(PTy->getNumElements(),
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000467 getNegativeZero(PTy->getElementType()));
Chris Lattner2ca5c862011-02-15 00:14:00 +0000468 return ConstantVector::get(zeros);
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000469 }
470
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000471 if (Ty->isFloatingPointTy())
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000472 return getNegativeZero(Ty);
473
Owen Andersona7235ea2009-07-31 20:28:14 +0000474 return Constant::getNullValue(Ty);
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000475}
476
477
478// ConstantFP accessors.
479ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) {
480 DenseMapAPFloatKeyInfo::KeyTy Key(V);
481
482 LLVMContextImpl* pImpl = Context.pImpl;
483
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000484 ConstantFP *&Slot = pImpl->FPConstants[Key];
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000485
486 if (!Slot) {
Owen Anderson59d5aac2009-10-19 20:11:52 +0000487 const Type *Ty;
488 if (&V.getSemantics() == &APFloat::IEEEsingle)
489 Ty = Type::getFloatTy(Context);
490 else if (&V.getSemantics() == &APFloat::IEEEdouble)
491 Ty = Type::getDoubleTy(Context);
492 else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
493 Ty = Type::getX86_FP80Ty(Context);
494 else if (&V.getSemantics() == &APFloat::IEEEquad)
495 Ty = Type::getFP128Ty(Context);
496 else {
497 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble &&
498 "Unknown FP format");
499 Ty = Type::getPPC_FP128Ty(Context);
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000500 }
Owen Anderson59d5aac2009-10-19 20:11:52 +0000501 Slot = new ConstantFP(Ty, V);
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000502 }
503
504 return Slot;
505}
506
Dan Gohmana23643d2009-09-25 23:40:21 +0000507ConstantFP *ConstantFP::getInfinity(const Type *Ty, bool Negative) {
Dan Gohmanf344f7f2009-09-25 23:00:48 +0000508 const fltSemantics &Semantics = *TypeToFloatSemantics(Ty);
509 return ConstantFP::get(Ty->getContext(),
510 APFloat::getInf(Semantics, Negative));
511}
512
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000513ConstantFP::ConstantFP(const Type *Ty, const APFloat& V)
514 : Constant(Ty, ConstantFPVal, 0, 0), Val(V) {
Chris Lattner288e78f2008-04-09 06:38:30 +0000515 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
516 "FP type Mismatch");
Chris Lattner00950542001-06-06 20:29:01 +0000517}
518
Chris Lattner9b4ee0c2007-02-20 07:17:17 +0000519bool ConstantFP::isNullValue() const {
Dale Johannesen343e7702007-08-24 00:56:33 +0000520 return Val.isZero() && !Val.isNegative();
Chris Lattner9b4ee0c2007-02-20 07:17:17 +0000521}
522
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000523bool ConstantFP::isExactlyValue(const APFloat& V) const {
524 return Val.bitwiseIsEqual(V);
Chris Lattner9b4ee0c2007-02-20 07:17:17 +0000525}
526
Chris Lattner9b4ee0c2007-02-20 07:17:17 +0000527//===----------------------------------------------------------------------===//
528// ConstantXXX Classes
529//===----------------------------------------------------------------------===//
530
531
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000532ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnere4671472005-01-29 00:34:39 +0000533 const std::vector<Constant*> &V)
Gabor Greifefe65362008-05-10 08:32:32 +0000534 : Constant(T, ConstantArrayVal,
535 OperandTraits<ConstantArray>::op_end(this) - V.size(),
536 V.size()) {
Alkis Evlogimenose0de1d62004-09-15 02:32:15 +0000537 assert(V.size() == T->getNumElements() &&
538 "Invalid initializer vector for constant array");
Chris Lattnere4671472005-01-29 00:34:39 +0000539 Use *OL = OperandList;
Chris Lattnerdfdd6c52005-10-03 21:56:24 +0000540 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
541 I != E; ++I, ++OL) {
Chris Lattner71abaab2005-10-07 05:23:36 +0000542 Constant *C = *I;
Nick Lewyckyb13105f2009-10-03 19:30:43 +0000543 assert(C->getType() == T->getElementType() &&
Alkis Evlogimenoscad90ad2004-09-10 04:16:59 +0000544 "Initializer for array element doesn't match array element type!");
Gabor Greif6c80c382008-05-26 21:33:52 +0000545 *OL = C;
Chris Lattner00950542001-06-06 20:29:01 +0000546 }
547}
548
Owen Anderson1fd70962009-07-28 18:32:17 +0000549Constant *ConstantArray::get(const ArrayType *Ty,
550 const std::vector<Constant*> &V) {
Jeffrey Yasskin1fb613c2009-09-30 21:08:08 +0000551 for (unsigned i = 0, e = V.size(); i != e; ++i) {
552 assert(V[i]->getType() == Ty->getElementType() &&
553 "Wrong type in array element initializer");
554 }
Owen Anderson1fd70962009-07-28 18:32:17 +0000555 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
556 // If this is an all-zero array, return a ConstantAggregateZero object
557 if (!V.empty()) {
558 Constant *C = V[0];
Chris Lattner83738a22009-12-30 20:25:09 +0000559 if (!C->isNullValue())
Owen Anderson1fd70962009-07-28 18:32:17 +0000560 return pImpl->ArrayConstants.getOrCreate(Ty, V);
Chris Lattner83738a22009-12-30 20:25:09 +0000561
Owen Anderson1fd70962009-07-28 18:32:17 +0000562 for (unsigned i = 1, e = V.size(); i != e; ++i)
Chris Lattner83738a22009-12-30 20:25:09 +0000563 if (V[i] != C)
Owen Anderson1fd70962009-07-28 18:32:17 +0000564 return pImpl->ArrayConstants.getOrCreate(Ty, V);
Owen Anderson1fd70962009-07-28 18:32:17 +0000565 }
566
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000567 return ConstantAggregateZero::get(Ty);
Owen Anderson1fd70962009-07-28 18:32:17 +0000568}
569
570
Chris Lattnerf067d582011-02-07 16:40:21 +0000571Constant *ConstantArray::get(const ArrayType* T, Constant *const* Vals,
Owen Anderson1fd70962009-07-28 18:32:17 +0000572 unsigned NumVals) {
573 // FIXME: make this the primary ctor method.
574 return get(T, std::vector<Constant*>(Vals, Vals+NumVals));
575}
576
577/// ConstantArray::get(const string&) - Return an array that is initialized to
578/// contain the specified string. If length is zero then a null terminator is
579/// added to the specified string so that it may be used in a natural way.
580/// Otherwise, the length parameter specifies how much of the string to use
581/// and it won't be null terminated.
582///
Chris Lattnerf067d582011-02-07 16:40:21 +0000583Constant *ConstantArray::get(LLVMContext &Context, StringRef Str,
Owen Anderson1d0be152009-08-13 21:58:54 +0000584 bool AddNull) {
Owen Anderson1fd70962009-07-28 18:32:17 +0000585 std::vector<Constant*> ElementVals;
Benjamin Kramerad2b04c2010-08-01 11:43:26 +0000586 ElementVals.reserve(Str.size() + size_t(AddNull));
Owen Anderson1fd70962009-07-28 18:32:17 +0000587 for (unsigned i = 0; i < Str.size(); ++i)
Owen Anderson1d0be152009-08-13 21:58:54 +0000588 ElementVals.push_back(ConstantInt::get(Type::getInt8Ty(Context), Str[i]));
Owen Anderson1fd70962009-07-28 18:32:17 +0000589
590 // Add a null terminator to the string...
591 if (AddNull) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000592 ElementVals.push_back(ConstantInt::get(Type::getInt8Ty(Context), 0));
Owen Anderson1fd70962009-07-28 18:32:17 +0000593 }
594
Owen Anderson1d0be152009-08-13 21:58:54 +0000595 ArrayType *ATy = ArrayType::get(Type::getInt8Ty(Context), ElementVals.size());
Owen Anderson1fd70962009-07-28 18:32:17 +0000596 return get(ATy, ElementVals);
597}
598
599
Chris Lattnere4671472005-01-29 00:34:39 +0000600
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000601ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnere4671472005-01-29 00:34:39 +0000602 const std::vector<Constant*> &V)
Gabor Greifefe65362008-05-10 08:32:32 +0000603 : Constant(T, ConstantStructVal,
604 OperandTraits<ConstantStruct>::op_end(this) - V.size(),
605 V.size()) {
Chris Lattnerd21cd802004-02-09 04:37:31 +0000606 assert(V.size() == T->getNumElements() &&
Vikram S. Adve345e0cf2002-07-14 23:13:17 +0000607 "Invalid initializer vector for constant structure");
Chris Lattnere4671472005-01-29 00:34:39 +0000608 Use *OL = OperandList;
Chris Lattnerdfdd6c52005-10-03 21:56:24 +0000609 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
610 I != E; ++I, ++OL) {
Chris Lattner71abaab2005-10-07 05:23:36 +0000611 Constant *C = *I;
Nick Lewyckyb13105f2009-10-03 19:30:43 +0000612 assert(C->getType() == T->getElementType(I-V.begin()) &&
Chris Lattnerb8438892003-06-02 17:42:47 +0000613 "Initializer for struct element doesn't match struct element type!");
Gabor Greif6c80c382008-05-26 21:33:52 +0000614 *OL = C;
Chris Lattner00950542001-06-06 20:29:01 +0000615 }
616}
617
Owen Anderson8fa33382009-07-27 22:29:26 +0000618// ConstantStruct accessors.
Chris Lattnerf067d582011-02-07 16:40:21 +0000619Constant *ConstantStruct::get(const StructType* T,
Owen Anderson8fa33382009-07-27 22:29:26 +0000620 const std::vector<Constant*>& V) {
621 LLVMContextImpl* pImpl = T->getContext().pImpl;
622
623 // Create a ConstantAggregateZero value if all elements are zeros...
624 for (unsigned i = 0, e = V.size(); i != e; ++i)
625 if (!V[i]->isNullValue())
Owen Anderson8fa33382009-07-27 22:29:26 +0000626 return pImpl->StructConstants.getOrCreate(T, V);
627
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000628 return ConstantAggregateZero::get(T);
Owen Anderson8fa33382009-07-27 22:29:26 +0000629}
630
Chris Lattnerf067d582011-02-07 16:40:21 +0000631Constant *ConstantStruct::get(LLVMContext &Context,
Owen Andersond7f2a6c2009-08-05 23:16:16 +0000632 const std::vector<Constant*>& V, bool packed) {
Owen Anderson8fa33382009-07-27 22:29:26 +0000633 std::vector<const Type*> StructEls;
634 StructEls.reserve(V.size());
635 for (unsigned i = 0, e = V.size(); i != e; ++i)
636 StructEls.push_back(V[i]->getType());
Owen Andersond7f2a6c2009-08-05 23:16:16 +0000637 return get(StructType::get(Context, StructEls, packed), V);
Owen Anderson8fa33382009-07-27 22:29:26 +0000638}
639
Chris Lattnerf067d582011-02-07 16:40:21 +0000640Constant *ConstantStruct::get(LLVMContext &Context,
641 Constant *const *Vals, unsigned NumVals,
Owen Anderson8fa33382009-07-27 22:29:26 +0000642 bool Packed) {
643 // FIXME: make this the primary ctor method.
Owen Andersond7f2a6c2009-08-05 23:16:16 +0000644 return get(Context, std::vector<Constant*>(Vals, Vals+NumVals), Packed);
Owen Anderson8fa33382009-07-27 22:29:26 +0000645}
Chris Lattnere4671472005-01-29 00:34:39 +0000646
Reid Spencer9d6565a2007-02-15 02:26:10 +0000647ConstantVector::ConstantVector(const VectorType *T,
Chris Lattnere4671472005-01-29 00:34:39 +0000648 const std::vector<Constant*> &V)
Gabor Greifefe65362008-05-10 08:32:32 +0000649 : Constant(T, ConstantVectorVal,
650 OperandTraits<ConstantVector>::op_end(this) - V.size(),
651 V.size()) {
Chris Lattnere4671472005-01-29 00:34:39 +0000652 Use *OL = OperandList;
Jay Foad9afc5272011-01-27 14:44:55 +0000653 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
654 I != E; ++I, ++OL) {
655 Constant *C = *I;
656 assert(C->getType() == T->getElementType() &&
Dan Gohmanfa73ea22007-05-24 14:36:04 +0000657 "Initializer for vector element doesn't match vector element type!");
Gabor Greif6c80c382008-05-26 21:33:52 +0000658 *OL = C;
Brian Gaeke715c90b2004-08-20 06:00:58 +0000659 }
660}
661
Owen Andersonaf7ec972009-07-28 21:19:26 +0000662// ConstantVector accessors.
Chris Lattner2ca5c862011-02-15 00:14:00 +0000663Constant *ConstantVector::get(const VectorType *T,
664 const std::vector<Constant*> &V) {
Jay Foad9afc5272011-01-27 14:44:55 +0000665 assert(!V.empty() && "Vectors can't be empty");
Chris Lattner2ca5c862011-02-15 00:14:00 +0000666 LLVMContextImpl *pImpl = T->getContext().pImpl;
Jay Foad9afc5272011-01-27 14:44:55 +0000667
Chris Lattner2ca5c862011-02-15 00:14:00 +0000668 // If this is an all-undef or all-zero vector, return a
Owen Andersonaf7ec972009-07-28 21:19:26 +0000669 // ConstantAggregateZero or UndefValue.
670 Constant *C = V[0];
671 bool isZero = C->isNullValue();
672 bool isUndef = isa<UndefValue>(C);
673
674 if (isZero || isUndef) {
675 for (unsigned i = 1, e = V.size(); i != e; ++i)
676 if (V[i] != C) {
677 isZero = isUndef = false;
678 break;
679 }
680 }
681
682 if (isZero)
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000683 return ConstantAggregateZero::get(T);
Owen Andersonaf7ec972009-07-28 21:19:26 +0000684 if (isUndef)
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000685 return UndefValue::get(T);
Owen Andersonaf7ec972009-07-28 21:19:26 +0000686
Owen Andersonaf7ec972009-07-28 21:19:26 +0000687 return pImpl->VectorConstants.getOrCreate(T, V);
688}
689
Chris Lattner2ca5c862011-02-15 00:14:00 +0000690Constant *ConstantVector::get(ArrayRef<Constant*> V) {
Owen Andersonaf7ec972009-07-28 21:19:26 +0000691 // FIXME: make this the primary ctor method.
Chris Lattner2ca5c862011-02-15 00:14:00 +0000692 assert(!V.empty() && "Vectors cannot be empty");
693 return get(VectorType::get(V.front()->getType(), V.size()), V.vec());
Owen Andersonaf7ec972009-07-28 21:19:26 +0000694}
695
Reid Spencer3da59db2006-11-27 01:05:10 +0000696// Utility function for determining if a ConstantExpr is a CastOp or not. This
697// can't be inline because we don't want to #include Instruction.h into
698// Constant.h
699bool ConstantExpr::isCast() const {
700 return Instruction::isCast(getOpcode());
701}
702
Reid Spencer077d0eb2006-12-04 05:19:50 +0000703bool ConstantExpr::isCompare() const {
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +0000704 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
Reid Spencer077d0eb2006-12-04 05:19:50 +0000705}
706
Dan Gohmane6992f72009-09-10 23:37:55 +0000707bool ConstantExpr::isGEPWithNoNotionalOverIndexing() const {
708 if (getOpcode() != Instruction::GetElementPtr) return false;
709
710 gep_type_iterator GEPI = gep_type_begin(this), E = gep_type_end(this);
Oscar Fuentesee56c422010-08-02 06:00:15 +0000711 User::const_op_iterator OI = llvm::next(this->op_begin());
Dan Gohmane6992f72009-09-10 23:37:55 +0000712
713 // Skip the first index, as it has no static limit.
714 ++GEPI;
715 ++OI;
716
717 // The remaining indices must be compile-time known integers within the
718 // bounds of the corresponding notional static array types.
719 for (; GEPI != E; ++GEPI, ++OI) {
720 ConstantInt *CI = dyn_cast<ConstantInt>(*OI);
721 if (!CI) return false;
722 if (const ArrayType *ATy = dyn_cast<ArrayType>(*GEPI))
723 if (CI->getValue().getActiveBits() > 64 ||
724 CI->getZExtValue() >= ATy->getNumElements())
725 return false;
726 }
727
728 // All the indices checked out.
729 return true;
730}
731
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000732bool ConstantExpr::hasIndices() const {
733 return getOpcode() == Instruction::ExtractValue ||
734 getOpcode() == Instruction::InsertValue;
735}
736
737const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const {
738 if (const ExtractValueConstantExpr *EVCE =
739 dyn_cast<ExtractValueConstantExpr>(this))
740 return EVCE->Indices;
Dan Gohman1a203572008-06-23 16:39:44 +0000741
742 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman81a0c0b2008-05-31 00:58:22 +0000743}
744
Reid Spencer728b6db2006-12-03 05:48:19 +0000745unsigned ConstantExpr::getPredicate() const {
Nate Begemanac80ade2008-05-12 19:01:56 +0000746 assert(getOpcode() == Instruction::FCmp ||
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +0000747 getOpcode() == Instruction::ICmp);
Chris Lattnerb7daa842007-10-18 16:26:24 +0000748 return ((const CompareConstantExpr*)this)->predicate;
Reid Spencer728b6db2006-12-03 05:48:19 +0000749}
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000750
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000751/// getWithOperandReplaced - Return a constant expression identical to this
752/// one, but with the specified operand set to the specified value.
Reid Spencer3da59db2006-11-27 01:05:10 +0000753Constant *
754ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000755 assert(OpNo < getNumOperands() && "Operand num is out of range!");
756 assert(Op->getType() == getOperand(OpNo)->getType() &&
757 "Replacing operand with value of different type!");
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000758 if (getOperand(OpNo) == Op)
759 return const_cast<ConstantExpr*>(this);
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000760
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000761 Constant *Op0, *Op1, *Op2;
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000762 switch (getOpcode()) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000763 case Instruction::Trunc:
764 case Instruction::ZExt:
765 case Instruction::SExt:
766 case Instruction::FPTrunc:
767 case Instruction::FPExt:
768 case Instruction::UIToFP:
769 case Instruction::SIToFP:
770 case Instruction::FPToUI:
771 case Instruction::FPToSI:
772 case Instruction::PtrToInt:
773 case Instruction::IntToPtr:
774 case Instruction::BitCast:
775 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000776 case Instruction::Select:
777 Op0 = (OpNo == 0) ? Op : getOperand(0);
778 Op1 = (OpNo == 1) ? Op : getOperand(1);
779 Op2 = (OpNo == 2) ? Op : getOperand(2);
780 return ConstantExpr::getSelect(Op0, Op1, Op2);
781 case Instruction::InsertElement:
782 Op0 = (OpNo == 0) ? Op : getOperand(0);
783 Op1 = (OpNo == 1) ? Op : getOperand(1);
784 Op2 = (OpNo == 2) ? Op : getOperand(2);
785 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
786 case Instruction::ExtractElement:
787 Op0 = (OpNo == 0) ? Op : getOperand(0);
788 Op1 = (OpNo == 1) ? Op : getOperand(1);
789 return ConstantExpr::getExtractElement(Op0, Op1);
790 case Instruction::ShuffleVector:
791 Op0 = (OpNo == 0) ? Op : getOperand(0);
792 Op1 = (OpNo == 1) ? Op : getOperand(1);
793 Op2 = (OpNo == 2) ? Op : getOperand(2);
794 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000795 case Instruction::GetElementPtr: {
Chris Lattnerf9021ff2007-02-19 20:01:23 +0000796 SmallVector<Constant*, 8> Ops;
Dan Gohman041e2eb2008-05-15 19:50:34 +0000797 Ops.resize(getNumOperands()-1);
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000798 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Dan Gohman041e2eb2008-05-15 19:50:34 +0000799 Ops[i-1] = getOperand(i);
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000800 if (OpNo == 0)
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000801 return cast<GEPOperator>(this)->isInBounds() ?
802 ConstantExpr::getInBoundsGetElementPtr(Op, &Ops[0], Ops.size()) :
803 ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000804 Ops[OpNo-1] = Op;
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000805 return cast<GEPOperator>(this)->isInBounds() ?
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000806 ConstantExpr::getInBoundsGetElementPtr(getOperand(0), &Ops[0],Ops.size()):
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000807 ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000808 }
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000809 default:
810 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000811 Op0 = (OpNo == 0) ? Op : getOperand(0);
812 Op1 = (OpNo == 1) ? Op : getOperand(1);
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000813 return ConstantExpr::get(getOpcode(), Op0, Op1, SubclassOptionalData);
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000814 }
815}
816
817/// getWithOperands - This returns the current constant expression with the
818/// operands replaced with the specified values. The specified operands must
819/// match count and type with the existing ones.
820Constant *ConstantExpr::
Chris Lattnerf067d582011-02-07 16:40:21 +0000821getWithOperands(Constant *const *Ops, unsigned NumOps) const {
Chris Lattnerb054bfd2008-08-20 22:27:40 +0000822 assert(NumOps == getNumOperands() && "Operand count mismatch!");
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000823 bool AnyChange = false;
Chris Lattnerb054bfd2008-08-20 22:27:40 +0000824 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000825 assert(Ops[i]->getType() == getOperand(i)->getType() &&
826 "Operand type mismatch!");
827 AnyChange |= Ops[i] != getOperand(i);
828 }
829 if (!AnyChange) // No operands changed, return self.
830 return const_cast<ConstantExpr*>(this);
831
832 switch (getOpcode()) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000833 case Instruction::Trunc:
834 case Instruction::ZExt:
835 case Instruction::SExt:
836 case Instruction::FPTrunc:
837 case Instruction::FPExt:
838 case Instruction::UIToFP:
839 case Instruction::SIToFP:
840 case Instruction::FPToUI:
841 case Instruction::FPToSI:
842 case Instruction::PtrToInt:
843 case Instruction::IntToPtr:
844 case Instruction::BitCast:
845 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000846 case Instruction::Select:
847 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
848 case Instruction::InsertElement:
849 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
850 case Instruction::ExtractElement:
851 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
852 case Instruction::ShuffleVector:
853 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Chris Lattnerf9021ff2007-02-19 20:01:23 +0000854 case Instruction::GetElementPtr:
Dan Gohmanf8dbee72009-09-07 23:54:19 +0000855 return cast<GEPOperator>(this)->isInBounds() ?
856 ConstantExpr::getInBoundsGetElementPtr(Ops[0], &Ops[1], NumOps-1) :
857 ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], NumOps-1);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000858 case Instruction::ICmp:
859 case Instruction::FCmp:
860 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattnerb88a7fb2006-07-14 22:20:01 +0000861 default:
862 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000863 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1], SubclassOptionalData);
Chris Lattner1fe8f6b2006-07-14 19:37:40 +0000864 }
865}
866
Chris Lattner00950542001-06-06 20:29:01 +0000867
868//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +0000869// isValueValidForType implementations
870
Reid Spencer9b11d512006-12-19 01:28:19 +0000871bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000872 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Owen Anderson1d0be152009-08-13 21:58:54 +0000873 if (Ty == Type::getInt1Ty(Ty->getContext()))
Reid Spencera54b7cb2007-01-12 07:05:14 +0000874 return Val == 0 || Val == 1;
Reid Spencer554cec62007-02-05 23:47:56 +0000875 if (NumBits >= 64)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000876 return true; // always true, has to fit in largest type
877 uint64_t Max = (1ll << NumBits) - 1;
878 return Val <= Max;
Reid Spencer9b11d512006-12-19 01:28:19 +0000879}
880
Reid Spencerb83eb642006-10-20 07:07:24 +0000881bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000882 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Owen Anderson1d0be152009-08-13 21:58:54 +0000883 if (Ty == Type::getInt1Ty(Ty->getContext()))
Reid Spencerc1030572007-01-19 21:13:56 +0000884 return Val == 0 || Val == 1 || Val == -1;
Reid Spencer554cec62007-02-05 23:47:56 +0000885 if (NumBits >= 64)
Reid Spencera54b7cb2007-01-12 07:05:14 +0000886 return true; // always true, has to fit in largest type
887 int64_t Min = -(1ll << (NumBits-1));
888 int64_t Max = (1ll << (NumBits-1)) - 1;
889 return (Val >= Min && Val <= Max);
Chris Lattner00950542001-06-06 20:29:01 +0000890}
891
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000892bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) {
893 // convert modifies in place, so make a copy.
894 APFloat Val2 = APFloat(Val);
Dale Johannesen23a98552008-10-09 23:00:39 +0000895 bool losesInfo;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000896 switch (Ty->getTypeID()) {
Chris Lattner00950542001-06-06 20:29:01 +0000897 default:
898 return false; // These can't be represented as floating point!
899
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000900 // FIXME rounding mode needs to be more flexible
Dale Johannesen23a98552008-10-09 23:00:39 +0000901 case Type::FloatTyID: {
902 if (&Val2.getSemantics() == &APFloat::IEEEsingle)
903 return true;
904 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
905 return !losesInfo;
906 }
907 case Type::DoubleTyID: {
908 if (&Val2.getSemantics() == &APFloat::IEEEsingle ||
909 &Val2.getSemantics() == &APFloat::IEEEdouble)
910 return true;
911 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
912 return !losesInfo;
913 }
Dale Johannesenebbc95d2007-08-09 22:51:36 +0000914 case Type::X86_FP80TyID:
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000915 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
916 &Val2.getSemantics() == &APFloat::IEEEdouble ||
917 &Val2.getSemantics() == &APFloat::x87DoubleExtended;
Dale Johannesenebbc95d2007-08-09 22:51:36 +0000918 case Type::FP128TyID:
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000919 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
920 &Val2.getSemantics() == &APFloat::IEEEdouble ||
921 &Val2.getSemantics() == &APFloat::IEEEquad;
Dale Johannesena471c2e2007-10-11 18:07:22 +0000922 case Type::PPC_FP128TyID:
923 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
924 &Val2.getSemantics() == &APFloat::IEEEdouble ||
925 &Val2.getSemantics() == &APFloat::PPCDoubleDouble;
Chris Lattner00950542001-06-06 20:29:01 +0000926 }
Chris Lattnerd74ea2b2006-05-24 17:04:05 +0000927}
Chris Lattner37bf6302001-07-20 19:16:02 +0000928
Chris Lattner531daef2001-09-07 16:46:31 +0000929//===----------------------------------------------------------------------===//
Chris Lattner531daef2001-09-07 16:46:31 +0000930// Factory Function Implementation
931
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000932ConstantAggregateZero* ConstantAggregateZero::get(const Type* Ty) {
Chris Lattner61c70e92010-08-28 04:09:24 +0000933 assert((Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy()) &&
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000934 "Cannot create an aggregate zero of non-aggregate type!");
935
936 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000937 return pImpl->AggZeroConstants.getOrCreate(Ty, 0);
938}
939
Dan Gohman0f8b53f2009-03-03 02:55:14 +0000940/// destroyConstant - Remove the constant from the constant table...
941///
Owen Anderson04fb7c32009-06-20 00:24:58 +0000942void ConstantAggregateZero::destroyConstant() {
Chris Lattner93604b62010-07-17 06:13:52 +0000943 getRawType()->getContext().pImpl->AggZeroConstants.remove(this);
Chris Lattner40bbeb52004-02-15 05:53:04 +0000944 destroyConstantImpl();
945}
946
Dan Gohman0f8b53f2009-03-03 02:55:14 +0000947/// destroyConstant - Remove the constant from the constant table...
948///
Owen Anderson04fb7c32009-06-20 00:24:58 +0000949void ConstantArray::destroyConstant() {
Chris Lattner93604b62010-07-17 06:13:52 +0000950 getRawType()->getContext().pImpl->ArrayConstants.remove(this);
Chris Lattner02ec5ed2003-05-23 20:03:32 +0000951 destroyConstantImpl();
952}
953
Reid Spencer3d10b0b2007-01-26 07:37:34 +0000954/// isString - This method returns true if the array is an array of i8, and
955/// if the elements of the array are all ConstantInt's.
Chris Lattner13cfdea2004-01-14 17:06:38 +0000956bool ConstantArray::isString() const {
Reid Spencer3d10b0b2007-01-26 07:37:34 +0000957 // Check the element type for i8...
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000958 if (!getType()->getElementType()->isIntegerTy(8))
Chris Lattner13cfdea2004-01-14 17:06:38 +0000959 return false;
960 // Check the elements to make sure they are all integers, not constant
961 // expressions.
962 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
963 if (!isa<ConstantInt>(getOperand(i)))
964 return false;
965 return true;
966}
967
Evan Cheng22c70302006-10-26 19:15:05 +0000968/// isCString - This method returns true if the array is a string (see
Dan Gohman0f8b53f2009-03-03 02:55:14 +0000969/// isString) and it ends in a null byte \\0 and does not contains any other
Evan Cheng22c70302006-10-26 19:15:05 +0000970/// null bytes except its terminator.
Owen Anderson1ca29d32009-07-13 21:27:19 +0000971bool ConstantArray::isCString() const {
Reid Spencer3d10b0b2007-01-26 07:37:34 +0000972 // Check the element type for i8...
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000973 if (!getType()->getElementType()->isIntegerTy(8))
Evan Chengabf63452006-10-26 21:48:03 +0000974 return false;
Owen Anderson1ca29d32009-07-13 21:27:19 +0000975
Evan Chengabf63452006-10-26 21:48:03 +0000976 // Last element must be a null.
Owen Anderson1ca29d32009-07-13 21:27:19 +0000977 if (!getOperand(getNumOperands()-1)->isNullValue())
Evan Chengabf63452006-10-26 21:48:03 +0000978 return false;
979 // Other elements must be non-null integers.
980 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
981 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng22c70302006-10-26 19:15:05 +0000982 return false;
Owen Anderson1ca29d32009-07-13 21:27:19 +0000983 if (getOperand(i)->isNullValue())
Evan Chengabf63452006-10-26 21:48:03 +0000984 return false;
985 }
Evan Cheng22c70302006-10-26 19:15:05 +0000986 return true;
987}
988
989
Dan Gohman0f8b53f2009-03-03 02:55:14 +0000990/// getAsString - If the sub-element type of this array is i8
991/// then this method converts the array to an std::string and returns it.
992/// Otherwise, it asserts out.
993///
Chris Lattner93aeea32002-08-26 17:53:56 +0000994std::string ConstantArray::getAsString() const {
Chris Lattner13cfdea2004-01-14 17:06:38 +0000995 assert(isString() && "Not a string!");
Chris Lattner93aeea32002-08-26 17:53:56 +0000996 std::string Result;
Owen Anderson45e39582008-06-24 21:58:29 +0000997 Result.reserve(getNumOperands());
Chris Lattnerc07736a2003-07-23 15:22:26 +0000998 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersone4f6ee52008-06-25 01:05:05 +0000999 Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue());
Chris Lattner93aeea32002-08-26 17:53:56 +00001000 return Result;
1001}
1002
1003
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001004//---- ConstantStruct::get() implementation...
Chris Lattner531daef2001-09-07 16:46:31 +00001005//
Chris Lattnered468e372003-10-05 00:17:43 +00001006
Chris Lattner31f84992003-11-21 20:23:48 +00001007namespace llvm {
Misha Brukmanfd939082005-04-21 23:48:37 +00001008
Chris Lattner531daef2001-09-07 16:46:31 +00001009}
Chris Lattner6a57baa2001-10-03 15:39:36 +00001010
Chris Lattnerf5ec48d2001-10-13 06:57:33 +00001011// destroyConstant - Remove the constant from the constant table...
Chris Lattner6a57baa2001-10-03 15:39:36 +00001012//
Owen Anderson04fb7c32009-06-20 00:24:58 +00001013void ConstantStruct::destroyConstant() {
Chris Lattner93604b62010-07-17 06:13:52 +00001014 getRawType()->getContext().pImpl->StructConstants.remove(this);
Chris Lattnerf5ec48d2001-10-13 06:57:33 +00001015 destroyConstantImpl();
1016}
Chris Lattner6a57baa2001-10-03 15:39:36 +00001017
Brian Gaeke715c90b2004-08-20 06:00:58 +00001018// destroyConstant - Remove the constant from the constant table...
1019//
Owen Anderson04fb7c32009-06-20 00:24:58 +00001020void ConstantVector::destroyConstant() {
Chris Lattner93604b62010-07-17 06:13:52 +00001021 getRawType()->getContext().pImpl->VectorConstants.remove(this);
Brian Gaeke715c90b2004-08-20 06:00:58 +00001022 destroyConstantImpl();
1023}
1024
Dan Gohmanfa73ea22007-05-24 14:36:04 +00001025/// This function will return true iff every element in this vector constant
Jim Laskeyfa301822007-01-12 22:39:14 +00001026/// is set to all ones.
1027/// @returns true iff this constant's emements are all set to all ones.
1028/// @brief Determine if the value is all ones.
Reid Spencer9d6565a2007-02-15 02:26:10 +00001029bool ConstantVector::isAllOnesValue() const {
Jim Laskeyfa301822007-01-12 22:39:14 +00001030 // Check out first element.
1031 const Constant *Elt = getOperand(0);
1032 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1033 if (!CI || !CI->isAllOnesValue()) return false;
1034 // Then make sure all remaining elements point to the same value.
1035 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1036 if (getOperand(I) != Elt) return false;
1037 }
1038 return true;
1039}
1040
Dan Gohman3b7cf0a2007-10-17 17:51:30 +00001041/// getSplatValue - If this is a splat constant, where all of the
1042/// elements have the same value, return that value. Otherwise return null.
Duncan Sands7681c6d2011-02-01 08:39:12 +00001043Constant *ConstantVector::getSplatValue() const {
Dan Gohman3b7cf0a2007-10-17 17:51:30 +00001044 // Check out first element.
1045 Constant *Elt = getOperand(0);
1046 // Then make sure all remaining elements point to the same value.
1047 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
1048 if (getOperand(I) != Elt) return 0;
1049 return Elt;
1050}
1051
Chris Lattner2ee11ec2009-10-28 00:01:44 +00001052//---- ConstantPointerNull::get() implementation.
Chris Lattnerf5ec48d2001-10-13 06:57:33 +00001053//
Chris Lattner02ec5ed2003-05-23 20:03:32 +00001054
Owen Anderson04fb7c32009-06-20 00:24:58 +00001055ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Owen Anderson7e3142b2009-07-31 22:45:43 +00001056 return Ty->getContext().pImpl->NullPtrConstants.getOrCreate(Ty, 0);
Chris Lattner6a57baa2001-10-03 15:39:36 +00001057}
1058
Chris Lattner41661fd2002-08-18 00:40:04 +00001059// destroyConstant - Remove the constant from the constant table...
1060//
Owen Anderson04fb7c32009-06-20 00:24:58 +00001061void ConstantPointerNull::destroyConstant() {
Chris Lattner93604b62010-07-17 06:13:52 +00001062 getRawType()->getContext().pImpl->NullPtrConstants.remove(this);
Chris Lattner41661fd2002-08-18 00:40:04 +00001063 destroyConstantImpl();
1064}
1065
1066
Chris Lattner2ee11ec2009-10-28 00:01:44 +00001067//---- UndefValue::get() implementation.
Chris Lattnerb9f18592004-10-16 18:07:16 +00001068//
1069
Owen Anderson04fb7c32009-06-20 00:24:58 +00001070UndefValue *UndefValue::get(const Type *Ty) {
Owen Anderson7e3142b2009-07-31 22:45:43 +00001071 return Ty->getContext().pImpl->UndefValueConstants.getOrCreate(Ty, 0);
Chris Lattnerb9f18592004-10-16 18:07:16 +00001072}
1073
1074// destroyConstant - Remove the constant from the constant table.
1075//
Owen Anderson04fb7c32009-06-20 00:24:58 +00001076void UndefValue::destroyConstant() {
Chris Lattner93604b62010-07-17 06:13:52 +00001077 getRawType()->getContext().pImpl->UndefValueConstants.remove(this);
Chris Lattnerb9f18592004-10-16 18:07:16 +00001078 destroyConstantImpl();
1079}
1080
Chris Lattner2ee11ec2009-10-28 00:01:44 +00001081//---- BlockAddress::get() implementation.
1082//
1083
1084BlockAddress *BlockAddress::get(BasicBlock *BB) {
1085 assert(BB->getParent() != 0 && "Block must have a parent");
1086 return get(BB->getParent(), BB);
1087}
1088
1089BlockAddress *BlockAddress::get(Function *F, BasicBlock *BB) {
1090 BlockAddress *&BA =
1091 F->getContext().pImpl->BlockAddresses[std::make_pair(F, BB)];
1092 if (BA == 0)
1093 BA = new BlockAddress(F, BB);
1094
1095 assert(BA->getFunction() == F && "Basic block moved between functions");
1096 return BA;
1097}
1098
1099BlockAddress::BlockAddress(Function *F, BasicBlock *BB)
1100: Constant(Type::getInt8PtrTy(F->getContext()), Value::BlockAddressVal,
1101 &Op<0>(), 2) {
Chris Lattnerd0ec2352009-11-01 03:03:03 +00001102 setOperand(0, F);
1103 setOperand(1, BB);
Chris Lattnercdfc9402009-11-01 01:27:45 +00001104 BB->AdjustBlockAddressRefCount(1);
Chris Lattner2ee11ec2009-10-28 00:01:44 +00001105}
1106
1107
1108// destroyConstant - Remove the constant from the constant table.
1109//
1110void BlockAddress::destroyConstant() {
Chris Lattner93604b62010-07-17 06:13:52 +00001111 getFunction()->getRawType()->getContext().pImpl
Chris Lattner2ee11ec2009-10-28 00:01:44 +00001112 ->BlockAddresses.erase(std::make_pair(getFunction(), getBasicBlock()));
Chris Lattnercdfc9402009-11-01 01:27:45 +00001113 getBasicBlock()->AdjustBlockAddressRefCount(-1);
Chris Lattner2ee11ec2009-10-28 00:01:44 +00001114 destroyConstantImpl();
1115}
1116
1117void BlockAddress::replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
1118 // This could be replacing either the Basic Block or the Function. In either
1119 // case, we have to remove the map entry.
1120 Function *NewF = getFunction();
1121 BasicBlock *NewBB = getBasicBlock();
1122
1123 if (U == &Op<0>())
1124 NewF = cast<Function>(To);
1125 else
1126 NewBB = cast<BasicBlock>(To);
1127
1128 // See if the 'new' entry already exists, if not, just update this in place
1129 // and return early.
1130 BlockAddress *&NewBA =
1131 getContext().pImpl->BlockAddresses[std::make_pair(NewF, NewBB)];
1132 if (NewBA == 0) {
Chris Lattnerd0ec2352009-11-01 03:03:03 +00001133 getBasicBlock()->AdjustBlockAddressRefCount(-1);
1134
Chris Lattner2ee11ec2009-10-28 00:01:44 +00001135 // Remove the old entry, this can't cause the map to rehash (just a
1136 // tombstone will get added).
1137 getContext().pImpl->BlockAddresses.erase(std::make_pair(getFunction(),
1138 getBasicBlock()));
1139 NewBA = this;
Chris Lattnerd0ec2352009-11-01 03:03:03 +00001140 setOperand(0, NewF);
1141 setOperand(1, NewBB);
1142 getBasicBlock()->AdjustBlockAddressRefCount(1);
Chris Lattner2ee11ec2009-10-28 00:01:44 +00001143 return;
1144 }
1145
1146 // Otherwise, I do need to replace this with an existing value.
1147 assert(NewBA != this && "I didn't contain From!");
1148
1149 // Everyone using this now uses the replacement.
1150 uncheckedReplaceAllUsesWith(NewBA);
1151
1152 destroyConstant();
1153}
1154
1155//---- ConstantExpr::get() implementations.
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001156//
Reid Spencer79e21d32006-12-31 05:26:44 +00001157
Reid Spencer3da59db2006-11-27 01:05:10 +00001158/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands66a1a052008-03-30 19:38:55 +00001159/// cast in the ExprConstants map. It is used by the various get* methods below.
Reid Spencer3da59db2006-11-27 01:05:10 +00001160static inline Constant *getFoldedCast(
Owen Anderson04fb7c32009-06-20 00:24:58 +00001161 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner9eacf8a2003-10-07 22:19:19 +00001162 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer3da59db2006-11-27 01:05:10 +00001163 // Fold a few common cases
Chris Lattnerb29d5962010-02-01 20:48:08 +00001164 if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
Reid Spencer3da59db2006-11-27 01:05:10 +00001165 return FC;
Chris Lattnerd628f6a2003-04-17 19:24:48 +00001166
Owen Andersond03eecd2009-08-04 20:25:11 +00001167 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
1168
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001169 // Look up the constant in the table first to ensure uniqueness
Chris Lattner9bc02a42003-05-13 21:37:02 +00001170 std::vector<Constant*> argVec(1, C);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001171 ExprMapKeyType Key(opc, argVec);
Owen Anderson3e456ab2009-06-17 18:40:29 +00001172
Owen Andersond03eecd2009-08-04 20:25:11 +00001173 return pImpl->ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001174}
Reid Spencer7858b332006-12-05 19:14:13 +00001175
Owen Anderson04fb7c32009-06-20 00:24:58 +00001176Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
Reid Spencer3da59db2006-11-27 01:05:10 +00001177 Instruction::CastOps opc = Instruction::CastOps(oc);
1178 assert(Instruction::isCast(opc) && "opcode out of range");
1179 assert(C && Ty && "Null arguments to getCast");
Chris Lattner0b68a002010-01-26 21:51:43 +00001180 assert(CastInst::castIsValid(opc, C, Ty) && "Invalid constantexpr cast!");
Reid Spencer3da59db2006-11-27 01:05:10 +00001181
1182 switch (opc) {
Chris Lattner0b68a002010-01-26 21:51:43 +00001183 default:
1184 llvm_unreachable("Invalid cast opcode");
1185 break;
1186 case Instruction::Trunc: return getTrunc(C, Ty);
1187 case Instruction::ZExt: return getZExt(C, Ty);
1188 case Instruction::SExt: return getSExt(C, Ty);
1189 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1190 case Instruction::FPExt: return getFPExtend(C, Ty);
1191 case Instruction::UIToFP: return getUIToFP(C, Ty);
1192 case Instruction::SIToFP: return getSIToFP(C, Ty);
1193 case Instruction::FPToUI: return getFPToUI(C, Ty);
1194 case Instruction::FPToSI: return getFPToSI(C, Ty);
1195 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1196 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1197 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattnerf5ac6c22005-01-01 15:59:57 +00001198 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001199 return 0;
Reid Spencer7858b332006-12-05 19:14:13 +00001200}
1201
Reid Spencer848414e2006-12-04 20:17:56 +00001202Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001203 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Dan Gohman3b490632010-04-12 22:12:29 +00001204 return getBitCast(C, Ty);
1205 return getZExt(C, Ty);
Reid Spencer848414e2006-12-04 20:17:56 +00001206}
1207
Owen Anderson04fb7c32009-06-20 00:24:58 +00001208Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001209 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Dan Gohman3b490632010-04-12 22:12:29 +00001210 return getBitCast(C, Ty);
1211 return getSExt(C, Ty);
Reid Spencer848414e2006-12-04 20:17:56 +00001212}
1213
1214Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001215 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Dan Gohman3b490632010-04-12 22:12:29 +00001216 return getBitCast(C, Ty);
1217 return getTrunc(C, Ty);
Reid Spencer848414e2006-12-04 20:17:56 +00001218}
1219
Owen Anderson04fb7c32009-06-20 00:24:58 +00001220Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
Duncan Sands1df98592010-02-16 11:11:14 +00001221 assert(S->getType()->isPointerTy() && "Invalid cast");
1222 assert((Ty->isIntegerTy() || Ty->isPointerTy()) && "Invalid cast");
Reid Spencerc0459fb2006-12-05 03:25:26 +00001223
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001224 if (Ty->isIntegerTy())
Dan Gohman3b490632010-04-12 22:12:29 +00001225 return getPtrToInt(S, Ty);
1226 return getBitCast(S, Ty);
Reid Spencerc0459fb2006-12-05 03:25:26 +00001227}
1228
Reid Spencer84f3eab2006-12-12 00:51:07 +00001229Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1230 bool isSigned) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001231 assert(C->getType()->isIntOrIntVectorTy() &&
1232 Ty->isIntOrIntVectorTy() && "Invalid cast");
Dan Gohman6de29f82009-06-15 22:12:54 +00001233 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1234 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer84f3eab2006-12-12 00:51:07 +00001235 Instruction::CastOps opcode =
1236 (SrcBits == DstBits ? Instruction::BitCast :
1237 (SrcBits > DstBits ? Instruction::Trunc :
1238 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1239 return getCast(opcode, C, Ty);
1240}
1241
1242Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001243 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer84f3eab2006-12-12 00:51:07 +00001244 "Invalid cast");
Dan Gohman6de29f82009-06-15 22:12:54 +00001245 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1246 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencerf25212a2006-12-12 05:38:50 +00001247 if (SrcBits == DstBits)
1248 return C; // Avoid a useless cast
Reid Spencer84f3eab2006-12-12 00:51:07 +00001249 Instruction::CastOps opcode =
Jay Foad9afc5272011-01-27 14:44:55 +00001250 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer84f3eab2006-12-12 00:51:07 +00001251 return getCast(opcode, C, Ty);
1252}
1253
Owen Anderson04fb7c32009-06-20 00:24:58 +00001254Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001255#ifndef NDEBUG
1256 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1257 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1258#endif
1259 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001260 assert(C->getType()->isIntOrIntVectorTy() && "Trunc operand must be integer");
1261 assert(Ty->isIntOrIntVectorTy() && "Trunc produces only integral");
Dan Gohman6de29f82009-06-15 22:12:54 +00001262 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer3da59db2006-11-27 01:05:10 +00001263 "SrcTy must be larger than DestTy for Trunc!");
1264
Owen Anderson04fb7c32009-06-20 00:24:58 +00001265 return getFoldedCast(Instruction::Trunc, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001266}
1267
Owen Anderson04fb7c32009-06-20 00:24:58 +00001268Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001269#ifndef NDEBUG
1270 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1271 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1272#endif
1273 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001274 assert(C->getType()->isIntOrIntVectorTy() && "SExt operand must be integral");
1275 assert(Ty->isIntOrIntVectorTy() && "SExt produces only integer");
Dan Gohman6de29f82009-06-15 22:12:54 +00001276 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer3da59db2006-11-27 01:05:10 +00001277 "SrcTy must be smaller than DestTy for SExt!");
1278
Owen Anderson04fb7c32009-06-20 00:24:58 +00001279 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerd144f422004-04-04 23:20:30 +00001280}
1281
Owen Anderson04fb7c32009-06-20 00:24:58 +00001282Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001283#ifndef NDEBUG
1284 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1285 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1286#endif
1287 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001288 assert(C->getType()->isIntOrIntVectorTy() && "ZEXt operand must be integral");
1289 assert(Ty->isIntOrIntVectorTy() && "ZExt produces only integer");
Dan Gohman6de29f82009-06-15 22:12:54 +00001290 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer3da59db2006-11-27 01:05:10 +00001291 "SrcTy must be smaller than DestTy for ZExt!");
1292
Owen Anderson04fb7c32009-06-20 00:24:58 +00001293 return getFoldedCast(Instruction::ZExt, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001294}
1295
Owen Anderson04fb7c32009-06-20 00:24:58 +00001296Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001297#ifndef NDEBUG
1298 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1299 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1300#endif
1301 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001302 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Dan Gohman6de29f82009-06-15 22:12:54 +00001303 C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer3da59db2006-11-27 01:05:10 +00001304 "This is an illegal floating point truncation!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001305 return getFoldedCast(Instruction::FPTrunc, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001306}
1307
Owen Anderson04fb7c32009-06-20 00:24:58 +00001308Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
Dan Gohman6de29f82009-06-15 22:12:54 +00001309#ifndef NDEBUG
1310 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1311 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1312#endif
1313 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001314 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Dan Gohman6de29f82009-06-15 22:12:54 +00001315 C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer3da59db2006-11-27 01:05:10 +00001316 "This is an illegal floating point extension!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001317 return getFoldedCast(Instruction::FPExt, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001318}
1319
Owen Anderson04fb7c32009-06-20 00:24:58 +00001320Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Devang Patelb6dc9352008-11-03 23:20:04 +00001321#ifndef NDEBUG
Nate Begemanb348d182007-11-17 03:58:34 +00001322 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1323 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Patelb6dc9352008-11-03 23:20:04 +00001324#endif
Nate Begemanb348d182007-11-17 03:58:34 +00001325 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001326 assert(C->getType()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() &&
Nate Begemanb348d182007-11-17 03:58:34 +00001327 "This is an illegal uint to floating point cast!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001328 return getFoldedCast(Instruction::UIToFP, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001329}
1330
Owen Anderson04fb7c32009-06-20 00:24:58 +00001331Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Devang Patelb6dc9352008-11-03 23:20:04 +00001332#ifndef NDEBUG
Nate Begemanb348d182007-11-17 03:58:34 +00001333 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1334 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Patelb6dc9352008-11-03 23:20:04 +00001335#endif
Nate Begemanb348d182007-11-17 03:58:34 +00001336 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001337 assert(C->getType()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer3da59db2006-11-27 01:05:10 +00001338 "This is an illegal sint to floating point cast!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001339 return getFoldedCast(Instruction::SIToFP, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001340}
1341
Owen Anderson04fb7c32009-06-20 00:24:58 +00001342Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Devang Patelb6dc9352008-11-03 23:20:04 +00001343#ifndef NDEBUG
Nate Begemanb348d182007-11-17 03:58:34 +00001344 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1345 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Patelb6dc9352008-11-03 23:20:04 +00001346#endif
Nate Begemanb348d182007-11-17 03:58:34 +00001347 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001348 assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() &&
Nate Begemanb348d182007-11-17 03:58:34 +00001349 "This is an illegal floating point to uint cast!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001350 return getFoldedCast(Instruction::FPToUI, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001351}
1352
Owen Anderson04fb7c32009-06-20 00:24:58 +00001353Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Devang Patelb6dc9352008-11-03 23:20:04 +00001354#ifndef NDEBUG
Nate Begemanb348d182007-11-17 03:58:34 +00001355 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1356 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Patelb6dc9352008-11-03 23:20:04 +00001357#endif
Nate Begemanb348d182007-11-17 03:58:34 +00001358 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001359 assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() &&
Nate Begemanb348d182007-11-17 03:58:34 +00001360 "This is an illegal floating point to sint cast!");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001361 return getFoldedCast(Instruction::FPToSI, C, Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +00001362}
1363
Owen Anderson04fb7c32009-06-20 00:24:58 +00001364Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
Duncan Sands1df98592010-02-16 11:11:14 +00001365 assert(C->getType()->isPointerTy() && "PtrToInt source must be pointer");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001366 assert(DstTy->isIntegerTy() && "PtrToInt destination must be integral");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001367 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
Reid Spencer3da59db2006-11-27 01:05:10 +00001368}
1369
Owen Anderson04fb7c32009-06-20 00:24:58 +00001370Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001371 assert(C->getType()->isIntegerTy() && "IntToPtr source must be integral");
Duncan Sands1df98592010-02-16 11:11:14 +00001372 assert(DstTy->isPointerTy() && "IntToPtr destination must be a pointer");
Owen Anderson04fb7c32009-06-20 00:24:58 +00001373 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
Reid Spencer3da59db2006-11-27 01:05:10 +00001374}
1375
Owen Anderson04fb7c32009-06-20 00:24:58 +00001376Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
Chris Lattner0b68a002010-01-26 21:51:43 +00001377 assert(CastInst::castIsValid(Instruction::BitCast, C, DstTy) &&
1378 "Invalid constantexpr bitcast!");
Chris Lattner8c7f24a2009-03-21 06:55:54 +00001379
1380 // It is common to ask for a bitcast of a value to its own type, handle this
1381 // speedily.
1382 if (C->getType() == DstTy) return C;
1383
Owen Anderson04fb7c32009-06-20 00:24:58 +00001384 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerd144f422004-04-04 23:20:30 +00001385}
1386
Chris Lattnered468e372003-10-05 00:17:43 +00001387Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001388 Constant *C1, Constant *C2,
1389 unsigned Flags) {
Chris Lattnerf31f5832003-05-21 17:49:25 +00001390 // Check the operands for consistency first
Reid Spencer0a783f72006-11-02 01:53:59 +00001391 assert(Opcode >= Instruction::BinaryOpsBegin &&
1392 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattnerf31f5832003-05-21 17:49:25 +00001393 "Invalid opcode in binary constant expression");
1394 assert(C1->getType() == C2->getType() &&
1395 "Operand types in binary constant expression should match");
Chris Lattnered468e372003-10-05 00:17:43 +00001396
Owen Anderson1d0be152009-08-13 21:58:54 +00001397 if (ReqTy == C1->getType() || ReqTy == Type::getInt1Ty(ReqTy->getContext()))
Chris Lattnerb29d5962010-02-01 20:48:08 +00001398 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattnered468e372003-10-05 00:17:43 +00001399 return FC; // Fold a few common cases...
Chris Lattnerd628f6a2003-04-17 19:24:48 +00001400
Chris Lattner9bc02a42003-05-13 21:37:02 +00001401 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001402 ExprMapKeyType Key(Opcode, argVec, 0, Flags);
Owen Anderson31c36f02009-06-17 20:10:08 +00001403
Owen Andersond03eecd2009-08-04 20:25:11 +00001404 LLVMContextImpl *pImpl = ReqTy->getContext().pImpl;
Owen Andersond03eecd2009-08-04 20:25:11 +00001405 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001406}
1407
Reid Spencere4d87aa2006-12-23 06:05:41 +00001408Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Nate Begemanff795a82008-07-25 17:56:27 +00001409 Constant *C1, Constant *C2) {
Reid Spencere4d87aa2006-12-23 06:05:41 +00001410 switch (predicate) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001411 default: llvm_unreachable("Invalid CmpInst predicate");
Nate Begemanb5557ab2008-07-25 17:35:37 +00001412 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
1413 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
1414 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
1415 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
1416 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
1417 case CmpInst::FCMP_TRUE:
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00001418 return getFCmp(predicate, C1, C2);
1419
Nate Begemanb5557ab2008-07-25 17:35:37 +00001420 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
1421 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
1422 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
1423 case CmpInst::ICMP_SLE:
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00001424 return getICmp(predicate, C1, C2);
Reid Spencere4d87aa2006-12-23 06:05:41 +00001425 }
Reid Spencer67263fe2006-12-04 21:35:24 +00001426}
1427
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001428Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2,
1429 unsigned Flags) {
Chris Lattner91b362b2004-08-17 17:28:46 +00001430#ifndef NDEBUG
1431 switch (Opcode) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001432 case Instruction::Add:
Reid Spencer0a783f72006-11-02 01:53:59 +00001433 case Instruction::Sub:
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001434 case Instruction::Mul:
Chris Lattner91b362b2004-08-17 17:28:46 +00001435 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001436 assert(C1->getType()->isIntOrIntVectorTy() &&
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001437 "Tried to create an integer operation on a non-integer type!");
1438 break;
1439 case Instruction::FAdd:
1440 case Instruction::FSub:
1441 case Instruction::FMul:
1442 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001443 assert(C1->getType()->isFPOrFPVectorTy() &&
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001444 "Tried to create a floating-point operation on a "
1445 "non-floating-point type!");
Chris Lattner91b362b2004-08-17 17:28:46 +00001446 break;
Reid Spencer1628cec2006-10-26 06:15:43 +00001447 case Instruction::UDiv:
1448 case Instruction::SDiv:
1449 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001450 assert(C1->getType()->isIntOrIntVectorTy() &&
Reid Spencer1628cec2006-10-26 06:15:43 +00001451 "Tried to create an arithmetic operation on a non-arithmetic type!");
1452 break;
1453 case Instruction::FDiv:
1454 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001455 assert(C1->getType()->isFPOrFPVectorTy() &&
Dan Gohmanf57478f2009-06-15 22:25:12 +00001456 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer1628cec2006-10-26 06:15:43 +00001457 break;
Reid Spencer0a783f72006-11-02 01:53:59 +00001458 case Instruction::URem:
1459 case Instruction::SRem:
1460 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001461 assert(C1->getType()->isIntOrIntVectorTy() &&
Reid Spencer0a783f72006-11-02 01:53:59 +00001462 "Tried to create an arithmetic operation on a non-arithmetic type!");
1463 break;
1464 case Instruction::FRem:
1465 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001466 assert(C1->getType()->isFPOrFPVectorTy() &&
Dan Gohmanf57478f2009-06-15 22:25:12 +00001467 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer0a783f72006-11-02 01:53:59 +00001468 break;
Chris Lattner91b362b2004-08-17 17:28:46 +00001469 case Instruction::And:
1470 case Instruction::Or:
1471 case Instruction::Xor:
1472 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001473 assert(C1->getType()->isIntOrIntVectorTy() &&
Misha Brukman1bae2912005-01-27 06:46:38 +00001474 "Tried to create a logical operation on a non-integral type!");
Chris Lattner91b362b2004-08-17 17:28:46 +00001475 break;
Chris Lattner91b362b2004-08-17 17:28:46 +00001476 case Instruction::Shl:
Reid Spencer3822ff52006-11-08 06:47:33 +00001477 case Instruction::LShr:
1478 case Instruction::AShr:
Reid Spencer832254e2007-02-02 02:16:23 +00001479 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001480 assert(C1->getType()->isIntOrIntVectorTy() &&
Chris Lattner91b362b2004-08-17 17:28:46 +00001481 "Tried to create a shift operation on a non-integer type!");
1482 break;
1483 default:
1484 break;
1485 }
1486#endif
1487
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001488 return getTy(C1->getType(), Opcode, C1, C2, Flags);
Reid Spencer67263fe2006-12-04 21:35:24 +00001489}
1490
Chris Lattnerf067d582011-02-07 16:40:21 +00001491Constant *ConstantExpr::getSizeOf(const Type* Ty) {
Owen Andersonbaf3c402009-07-29 18:55:55 +00001492 // sizeof is implemented as: (i64) gep (Ty*)null, 1
1493 // Note that a non-inbounds gep is used, as null isn't within any object.
Owen Anderson1d0be152009-08-13 21:58:54 +00001494 Constant *GEPIdx = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
Owen Andersonbaf3c402009-07-29 18:55:55 +00001495 Constant *GEP = getGetElementPtr(
Owen Andersona7235ea2009-07-31 20:28:14 +00001496 Constant::getNullValue(PointerType::getUnqual(Ty)), &GEPIdx, 1);
Dan Gohman3b490632010-04-12 22:12:29 +00001497 return getPtrToInt(GEP,
1498 Type::getInt64Ty(Ty->getContext()));
Owen Andersonbaf3c402009-07-29 18:55:55 +00001499}
1500
Chris Lattnerf067d582011-02-07 16:40:21 +00001501Constant *ConstantExpr::getAlignOf(const Type* Ty) {
Dan Gohman0f5efe52010-01-28 02:15:55 +00001502 // alignof is implemented as: (i64) gep ({i1,Ty}*)null, 0, 1
Dan Gohmane2574d32009-08-11 17:57:01 +00001503 // Note that a non-inbounds gep is used, as null isn't within any object.
Owen Andersond7f2a6c2009-08-05 23:16:16 +00001504 const Type *AligningTy = StructType::get(Ty->getContext(),
Dan Gohman0f5efe52010-01-28 02:15:55 +00001505 Type::getInt1Ty(Ty->getContext()), Ty, NULL);
Owen Andersona7235ea2009-07-31 20:28:14 +00001506 Constant *NullPtr = Constant::getNullValue(AligningTy->getPointerTo());
Dan Gohman06ed3e72010-01-28 02:43:22 +00001507 Constant *Zero = ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0);
Owen Anderson1d0be152009-08-13 21:58:54 +00001508 Constant *One = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
Owen Andersonbaf3c402009-07-29 18:55:55 +00001509 Constant *Indices[2] = { Zero, One };
1510 Constant *GEP = getGetElementPtr(NullPtr, Indices, 2);
Dan Gohman3b490632010-04-12 22:12:29 +00001511 return getPtrToInt(GEP,
1512 Type::getInt64Ty(Ty->getContext()));
Owen Andersonbaf3c402009-07-29 18:55:55 +00001513}
1514
Chris Lattnerf067d582011-02-07 16:40:21 +00001515Constant *ConstantExpr::getOffsetOf(const StructType* STy, unsigned FieldNo) {
Dan Gohman2544a1d2010-02-01 16:37:38 +00001516 return getOffsetOf(STy, ConstantInt::get(Type::getInt32Ty(STy->getContext()),
1517 FieldNo));
1518}
1519
Chris Lattnerf067d582011-02-07 16:40:21 +00001520Constant *ConstantExpr::getOffsetOf(const Type* Ty, Constant *FieldNo) {
Dan Gohman3778f212009-08-16 21:26:11 +00001521 // offsetof is implemented as: (i64) gep (Ty*)null, 0, FieldNo
1522 // Note that a non-inbounds gep is used, as null isn't within any object.
1523 Constant *GEPIdx[] = {
Dan Gohman2544a1d2010-02-01 16:37:38 +00001524 ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0),
1525 FieldNo
Dan Gohman3778f212009-08-16 21:26:11 +00001526 };
1527 Constant *GEP = getGetElementPtr(
Dan Gohman2544a1d2010-02-01 16:37:38 +00001528 Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx, 2);
Dan Gohman3b490632010-04-12 22:12:29 +00001529 return getPtrToInt(GEP,
1530 Type::getInt64Ty(Ty->getContext()));
Dan Gohman3778f212009-08-16 21:26:11 +00001531}
Owen Andersonbaf3c402009-07-29 18:55:55 +00001532
Reid Spencere4d87aa2006-12-23 06:05:41 +00001533Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencer67263fe2006-12-04 21:35:24 +00001534 Constant *C1, Constant *C2) {
1535 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencere4d87aa2006-12-23 06:05:41 +00001536 return getCompareTy(pred, C1, C2);
Chris Lattnerc3d12f02004-08-04 18:50:09 +00001537}
1538
Chris Lattner08a45cc2004-03-12 05:54:04 +00001539Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
Owen Anderson04fb7c32009-06-20 00:24:58 +00001540 Constant *V1, Constant *V2) {
Chris Lattner9ace0cd2008-12-29 00:16:12 +00001541 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner08a45cc2004-03-12 05:54:04 +00001542
1543 if (ReqTy == V1->getType())
Chris Lattnerb29d5962010-02-01 20:48:08 +00001544 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
Chris Lattner08a45cc2004-03-12 05:54:04 +00001545 return SC; // Fold common cases
1546
1547 std::vector<Constant*> argVec(3, C);
1548 argVec[1] = V1;
1549 argVec[2] = V2;
Reid Spencer077d0eb2006-12-04 05:19:50 +00001550 ExprMapKeyType Key(Instruction::Select, argVec);
Owen Anderson31c36f02009-06-17 20:10:08 +00001551
Owen Andersond03eecd2009-08-04 20:25:11 +00001552 LLVMContextImpl *pImpl = ReqTy->getContext().pImpl;
Owen Andersond03eecd2009-08-04 20:25:11 +00001553 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Chris Lattner08a45cc2004-03-12 05:54:04 +00001554}
1555
Jay Foad25052d82011-01-14 08:07:43 +00001556template<typename IndexTy>
Chris Lattnered468e372003-10-05 00:17:43 +00001557Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Jay Foad25052d82011-01-14 08:07:43 +00001558 IndexTy const *Idxs,
Chris Lattner1f78d512011-02-11 05:34:33 +00001559 unsigned NumIdx, bool InBounds) {
Dan Gohman041e2eb2008-05-15 19:50:34 +00001560 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
1561 Idxs+NumIdx) ==
1562 cast<PointerType>(ReqTy)->getElementType() &&
1563 "GEP indices invalid!");
Chris Lattner2e9bb1a2004-02-16 20:46:13 +00001564
Chris Lattner1f78d512011-02-11 05:34:33 +00001565 if (Constant *FC = ConstantFoldGetElementPtr(C, InBounds, Idxs, NumIdx))
1566 return FC; // Fold a few common cases.
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001567
Duncan Sands1df98592010-02-16 11:11:14 +00001568 assert(C->getType()->isPointerTy() &&
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001569 "Non-pointer type for constant GetElementPtr expression");
1570 // Look up the constant in the table first to ensure uniqueness
1571 std::vector<Constant*> ArgVec;
1572 ArgVec.reserve(NumIdx+1);
1573 ArgVec.push_back(C);
1574 for (unsigned i = 0; i != NumIdx; ++i)
1575 ArgVec.push_back(cast<Constant>(Idxs[i]));
1576 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec, 0,
Chris Lattner1f78d512011-02-11 05:34:33 +00001577 InBounds ? GEPOperator::IsInBounds : 0);
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001578
1579 LLVMContextImpl *pImpl = ReqTy->getContext().pImpl;
Dan Gohmanf8dbee72009-09-07 23:54:19 +00001580 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
1581}
1582
Jay Foad25052d82011-01-14 08:07:43 +00001583template<typename IndexTy>
1584Constant *ConstantExpr::getGetElementPtrImpl(Constant *C, IndexTy const *Idxs,
Chris Lattner1f78d512011-02-11 05:34:33 +00001585 unsigned NumIdx, bool InBounds) {
Chris Lattnered468e372003-10-05 00:17:43 +00001586 // Get the result type of the getelementptr!
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001587 const Type *Ty =
Dan Gohman041e2eb2008-05-15 19:50:34 +00001588 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
Chris Lattnered468e372003-10-05 00:17:43 +00001589 assert(Ty && "GEP indices invalid!");
Christopher Lambfe63fb92007-12-11 08:59:05 +00001590 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
Chris Lattner1f78d512011-02-11 05:34:33 +00001591 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx,InBounds);
Dan Gohman6e7ad952009-09-03 23:34:49 +00001592}
1593
Jay Foad25052d82011-01-14 08:07:43 +00001594Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
Chris Lattner1f78d512011-02-11 05:34:33 +00001595 unsigned NumIdx, bool InBounds) {
1596 return getGetElementPtrImpl(C, Idxs, NumIdx, InBounds);
Jay Foad25052d82011-01-14 08:07:43 +00001597}
1598
1599Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant *const *Idxs,
Chris Lattner1f78d512011-02-11 05:34:33 +00001600 unsigned NumIdx, bool InBounds) {
1601 return getGetElementPtrImpl(C, Idxs, NumIdx, InBounds);
Dan Gohman6e7ad952009-09-03 23:34:49 +00001602}
1603
Reid Spencer077d0eb2006-12-04 05:19:50 +00001604Constant *
Nick Lewycky401f3252010-01-21 07:03:21 +00001605ConstantExpr::getICmp(unsigned short pred, Constant *LHS, Constant *RHS) {
Reid Spencer077d0eb2006-12-04 05:19:50 +00001606 assert(LHS->getType() == RHS->getType());
1607 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
1608 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
1609
Chris Lattnerb29d5962010-02-01 20:48:08 +00001610 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spencer077d0eb2006-12-04 05:19:50 +00001611 return FC; // Fold a few common cases...
1612
1613 // Look up the constant in the table first to ensure uniqueness
1614 std::vector<Constant*> ArgVec;
1615 ArgVec.push_back(LHS);
1616 ArgVec.push_back(RHS);
Reid Spencer4fa021a2006-12-24 18:42:29 +00001617 // Get the key type with both the opcode and predicate
Reid Spencer077d0eb2006-12-04 05:19:50 +00001618 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Owen Anderson31c36f02009-06-17 20:10:08 +00001619
Nick Lewycky401f3252010-01-21 07:03:21 +00001620 const Type *ResultTy = Type::getInt1Ty(LHS->getContext());
1621 if (const VectorType *VT = dyn_cast<VectorType>(LHS->getType()))
1622 ResultTy = VectorType::get(ResultTy, VT->getNumElements());
1623
Owen Andersond03eecd2009-08-04 20:25:11 +00001624 LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl;
Nick Lewycky401f3252010-01-21 07:03:21 +00001625 return pImpl->ExprConstants.getOrCreate(ResultTy, Key);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001626}
1627
1628Constant *
Nick Lewycky401f3252010-01-21 07:03:21 +00001629ConstantExpr::getFCmp(unsigned short pred, Constant *LHS, Constant *RHS) {
Reid Spencer077d0eb2006-12-04 05:19:50 +00001630 assert(LHS->getType() == RHS->getType());
1631 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
1632
Chris Lattnerb29d5962010-02-01 20:48:08 +00001633 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spencer077d0eb2006-12-04 05:19:50 +00001634 return FC; // Fold a few common cases...
1635
1636 // Look up the constant in the table first to ensure uniqueness
1637 std::vector<Constant*> ArgVec;
1638 ArgVec.push_back(LHS);
1639 ArgVec.push_back(RHS);
Reid Spencer4fa021a2006-12-24 18:42:29 +00001640 // Get the key type with both the opcode and predicate
Reid Spencer077d0eb2006-12-04 05:19:50 +00001641 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Nick Lewycky401f3252010-01-21 07:03:21 +00001642
1643 const Type *ResultTy = Type::getInt1Ty(LHS->getContext());
1644 if (const VectorType *VT = dyn_cast<VectorType>(LHS->getType()))
1645 ResultTy = VectorType::get(ResultTy, VT->getNumElements());
1646
Owen Andersond03eecd2009-08-04 20:25:11 +00001647 LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl;
Nick Lewycky401f3252010-01-21 07:03:21 +00001648 return pImpl->ExprConstants.getOrCreate(ResultTy, Key);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001649}
1650
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001651Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
1652 Constant *Idx) {
Chris Lattnerb29d5962010-02-01 20:48:08 +00001653 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
Chris Lattner83738a22009-12-30 20:25:09 +00001654 return FC; // Fold a few common cases.
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001655 // Look up the constant in the table first to ensure uniqueness
1656 std::vector<Constant*> ArgVec(1, Val);
1657 ArgVec.push_back(Idx);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001658 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Owen Anderson31c36f02009-06-17 20:10:08 +00001659
Owen Andersond03eecd2009-08-04 20:25:11 +00001660 LLVMContextImpl *pImpl = ReqTy->getContext().pImpl;
Owen Andersond03eecd2009-08-04 20:25:11 +00001661 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001662}
1663
1664Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Duncan Sands1df98592010-02-16 11:11:14 +00001665 assert(Val->getType()->isVectorTy() &&
Reid Spencerac9dcb92007-02-15 03:39:18 +00001666 "Tried to create extractelement operation on non-vector type!");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001667 assert(Idx->getType()->isIntegerTy(32) &&
Reid Spencer3d10b0b2007-01-26 07:37:34 +00001668 "Extractelement index must be i32 type!");
Reid Spencer9d6565a2007-02-15 02:26:10 +00001669 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001670 Val, Idx);
1671}
Chris Lattnered468e372003-10-05 00:17:43 +00001672
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001673Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
1674 Constant *Elt, Constant *Idx) {
Chris Lattnerb29d5962010-02-01 20:48:08 +00001675 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
Chris Lattner83738a22009-12-30 20:25:09 +00001676 return FC; // Fold a few common cases.
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001677 // Look up the constant in the table first to ensure uniqueness
1678 std::vector<Constant*> ArgVec(1, Val);
1679 ArgVec.push_back(Elt);
1680 ArgVec.push_back(Idx);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001681 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Owen Anderson31c36f02009-06-17 20:10:08 +00001682
Owen Andersond03eecd2009-08-04 20:25:11 +00001683 LLVMContextImpl *pImpl = ReqTy->getContext().pImpl;
Owen Andersond03eecd2009-08-04 20:25:11 +00001684 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001685}
1686
1687Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1688 Constant *Idx) {
Duncan Sands1df98592010-02-16 11:11:14 +00001689 assert(Val->getType()->isVectorTy() &&
Reid Spencerac9dcb92007-02-15 03:39:18 +00001690 "Tried to create insertelement operation on non-vector type!");
Reid Spencer9d6565a2007-02-15 02:26:10 +00001691 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001692 && "Insertelement types must match!");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001693 assert(Idx->getType()->isIntegerTy(32) &&
Reid Spencer3d10b0b2007-01-26 07:37:34 +00001694 "Insertelement index must be i32 type!");
Gordon Henriksen699609c2008-08-30 15:41:51 +00001695 return getInsertElementTy(Val->getType(), Val, Elt, Idx);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001696}
1697
Chris Lattner00f10232006-04-08 01:18:18 +00001698Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
1699 Constant *V2, Constant *Mask) {
Chris Lattnerb29d5962010-02-01 20:48:08 +00001700 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
Chris Lattner00f10232006-04-08 01:18:18 +00001701 return FC; // Fold a few common cases...
1702 // Look up the constant in the table first to ensure uniqueness
1703 std::vector<Constant*> ArgVec(1, V1);
1704 ArgVec.push_back(V2);
1705 ArgVec.push_back(Mask);
Reid Spencer077d0eb2006-12-04 05:19:50 +00001706 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Owen Anderson31c36f02009-06-17 20:10:08 +00001707
Owen Andersond03eecd2009-08-04 20:25:11 +00001708 LLVMContextImpl *pImpl = ReqTy->getContext().pImpl;
Owen Andersond03eecd2009-08-04 20:25:11 +00001709 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Chris Lattner00f10232006-04-08 01:18:18 +00001710}
1711
1712Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
1713 Constant *Mask) {
1714 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
1715 "Invalid shuffle vector constant expr operands!");
Nate Begeman0f123cf2009-02-12 21:28:33 +00001716
1717 unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements();
1718 const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
1719 const Type *ShufTy = VectorType::get(EltTy, NElts);
1720 return getShuffleVectorTy(ShufTy, V1, V2, Mask);
Chris Lattner00f10232006-04-08 01:18:18 +00001721}
1722
Dan Gohman041e2eb2008-05-15 19:50:34 +00001723Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg,
1724 Constant *Val,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001725 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman041e2eb2008-05-15 19:50:34 +00001726 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
1727 Idxs+NumIdx) == Val->getType() &&
1728 "insertvalue indices invalid!");
1729 assert(Agg->getType() == ReqTy &&
1730 "insertvalue type invalid!");
Dan Gohmane4569942008-05-23 00:36:11 +00001731 assert(Agg->getType()->isFirstClassType() &&
1732 "Non-first-class type for constant InsertValue expression");
Chris Lattnerb29d5962010-02-01 20:48:08 +00001733 Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs, NumIdx);
Dan Gohmane0891602008-07-21 23:30:30 +00001734 assert(FC && "InsertValue constant expr couldn't be folded!");
1735 return FC;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001736}
1737
1738Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001739 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohmane4569942008-05-23 00:36:11 +00001740 assert(Agg->getType()->isFirstClassType() &&
1741 "Tried to create insertelement operation on non-first-class type!");
Dan Gohman041e2eb2008-05-15 19:50:34 +00001742
Dan Gohmane4569942008-05-23 00:36:11 +00001743 const Type *ReqTy = Agg->getType();
Devang Patelb6dc9352008-11-03 23:20:04 +00001744#ifndef NDEBUG
Dan Gohmane4569942008-05-23 00:36:11 +00001745 const Type *ValTy =
Dan Gohman041e2eb2008-05-15 19:50:34 +00001746 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
Devang Patelb6dc9352008-11-03 23:20:04 +00001747#endif
Dan Gohmane4569942008-05-23 00:36:11 +00001748 assert(ValTy == Val->getType() && "insertvalue indices invalid!");
Dan Gohman041e2eb2008-05-15 19:50:34 +00001749 return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx);
1750}
1751
1752Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001753 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman041e2eb2008-05-15 19:50:34 +00001754 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
1755 Idxs+NumIdx) == ReqTy &&
1756 "extractvalue indices invalid!");
Dan Gohmane4569942008-05-23 00:36:11 +00001757 assert(Agg->getType()->isFirstClassType() &&
1758 "Non-first-class type for constant extractvalue expression");
Chris Lattnerb29d5962010-02-01 20:48:08 +00001759 Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs, NumIdx);
Dan Gohmane0891602008-07-21 23:30:30 +00001760 assert(FC && "ExtractValue constant expr couldn't be folded!");
1761 return FC;
Dan Gohman041e2eb2008-05-15 19:50:34 +00001762}
1763
1764Constant *ConstantExpr::getExtractValue(Constant *Agg,
Dan Gohman81a0c0b2008-05-31 00:58:22 +00001765 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohmane4569942008-05-23 00:36:11 +00001766 assert(Agg->getType()->isFirstClassType() &&
1767 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman041e2eb2008-05-15 19:50:34 +00001768
1769 const Type *ReqTy =
1770 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
1771 assert(ReqTy && "extractvalue indices invalid!");
1772 return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx);
1773}
1774
Chris Lattner81baf142011-02-10 07:01:55 +00001775Constant *ConstantExpr::getNeg(Constant *C, bool HasNUW, bool HasNSW) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001776 assert(C->getType()->isIntOrIntVectorTy() &&
Owen Andersonbaf3c402009-07-29 18:55:55 +00001777 "Cannot NEG a nonintegral value!");
Chris Lattner81baf142011-02-10 07:01:55 +00001778 return getSub(ConstantFP::getZeroValueForNegation(C->getType()),
1779 C, HasNUW, HasNSW);
Owen Andersonbaf3c402009-07-29 18:55:55 +00001780}
1781
Chris Lattnerf067d582011-02-07 16:40:21 +00001782Constant *ConstantExpr::getFNeg(Constant *C) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001783 assert(C->getType()->isFPOrFPVectorTy() &&
Owen Andersonbaf3c402009-07-29 18:55:55 +00001784 "Cannot FNEG a non-floating-point value!");
Chris Lattner81baf142011-02-10 07:01:55 +00001785 return getFSub(ConstantFP::getZeroValueForNegation(C->getType()), C);
Owen Andersonbaf3c402009-07-29 18:55:55 +00001786}
1787
Chris Lattnerf067d582011-02-07 16:40:21 +00001788Constant *ConstantExpr::getNot(Constant *C) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001789 assert(C->getType()->isIntOrIntVectorTy() &&
Owen Andersonbaf3c402009-07-29 18:55:55 +00001790 "Cannot NOT a nonintegral value!");
Owen Andersona7235ea2009-07-31 20:28:14 +00001791 return get(Instruction::Xor, C, Constant::getAllOnesValue(C->getType()));
Owen Andersonbaf3c402009-07-29 18:55:55 +00001792}
1793
Chris Lattner81baf142011-02-10 07:01:55 +00001794Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2,
1795 bool HasNUW, bool HasNSW) {
1796 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
1797 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
1798 return get(Instruction::Add, C1, C2, Flags);
Owen Andersonbaf3c402009-07-29 18:55:55 +00001799}
1800
Chris Lattnerf067d582011-02-07 16:40:21 +00001801Constant *ConstantExpr::getFAdd(Constant *C1, Constant *C2) {
Owen Andersonbaf3c402009-07-29 18:55:55 +00001802 return get(Instruction::FAdd, C1, C2);
1803}
1804
Chris Lattner81baf142011-02-10 07:01:55 +00001805Constant *ConstantExpr::getSub(Constant *C1, Constant *C2,
1806 bool HasNUW, bool HasNSW) {
1807 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
1808 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
1809 return get(Instruction::Sub, C1, C2, Flags);
Owen Andersonbaf3c402009-07-29 18:55:55 +00001810}
1811
Chris Lattnerf067d582011-02-07 16:40:21 +00001812Constant *ConstantExpr::getFSub(Constant *C1, Constant *C2) {
Owen Andersonbaf3c402009-07-29 18:55:55 +00001813 return get(Instruction::FSub, C1, C2);
1814}
1815
Chris Lattner81baf142011-02-10 07:01:55 +00001816Constant *ConstantExpr::getMul(Constant *C1, Constant *C2,
1817 bool HasNUW, bool HasNSW) {
1818 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
1819 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
1820 return get(Instruction::Mul, C1, C2, Flags);
Owen Andersonbaf3c402009-07-29 18:55:55 +00001821}
1822
Chris Lattnerf067d582011-02-07 16:40:21 +00001823Constant *ConstantExpr::getFMul(Constant *C1, Constant *C2) {
Owen Andersonbaf3c402009-07-29 18:55:55 +00001824 return get(Instruction::FMul, C1, C2);
1825}
1826
Chris Lattner74f5c5a2011-02-09 16:43:07 +00001827Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2, bool isExact) {
1828 return get(Instruction::UDiv, C1, C2,
1829 isExact ? PossiblyExactOperator::IsExact : 0);
Owen Andersonbaf3c402009-07-29 18:55:55 +00001830}
1831
Chris Lattner74f5c5a2011-02-09 16:43:07 +00001832Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2, bool isExact) {
1833 return get(Instruction::SDiv, C1, C2,
1834 isExact ? PossiblyExactOperator::IsExact : 0);
Owen Andersonbaf3c402009-07-29 18:55:55 +00001835}
1836
Chris Lattnerf067d582011-02-07 16:40:21 +00001837Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) {
Owen Andersonbaf3c402009-07-29 18:55:55 +00001838 return get(Instruction::FDiv, C1, C2);
1839}
1840
Chris Lattnerf067d582011-02-07 16:40:21 +00001841Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) {
Owen Andersonbaf3c402009-07-29 18:55:55 +00001842 return get(Instruction::URem, C1, C2);
1843}
1844
Chris Lattnerf067d582011-02-07 16:40:21 +00001845Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) {
Owen Andersonbaf3c402009-07-29 18:55:55 +00001846 return get(Instruction::SRem, C1, C2);
1847}
1848
Chris Lattnerf067d582011-02-07 16:40:21 +00001849Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) {
Owen Andersonbaf3c402009-07-29 18:55:55 +00001850 return get(Instruction::FRem, C1, C2);
1851}
1852
Chris Lattnerf067d582011-02-07 16:40:21 +00001853Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
Owen Andersonbaf3c402009-07-29 18:55:55 +00001854 return get(Instruction::And, C1, C2);
1855}
1856
Chris Lattnerf067d582011-02-07 16:40:21 +00001857Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
Owen Andersonbaf3c402009-07-29 18:55:55 +00001858 return get(Instruction::Or, C1, C2);
1859}
1860
Chris Lattnerf067d582011-02-07 16:40:21 +00001861Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
Owen Andersonbaf3c402009-07-29 18:55:55 +00001862 return get(Instruction::Xor, C1, C2);
1863}
1864
Chris Lattner81baf142011-02-10 07:01:55 +00001865Constant *ConstantExpr::getShl(Constant *C1, Constant *C2,
1866 bool HasNUW, bool HasNSW) {
1867 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
1868 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
1869 return get(Instruction::Shl, C1, C2, Flags);
Owen Andersonbaf3c402009-07-29 18:55:55 +00001870}
1871
Chris Lattner74f5c5a2011-02-09 16:43:07 +00001872Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2, bool isExact) {
1873 return get(Instruction::LShr, C1, C2,
1874 isExact ? PossiblyExactOperator::IsExact : 0);
Owen Andersonbaf3c402009-07-29 18:55:55 +00001875}
1876
Chris Lattner74f5c5a2011-02-09 16:43:07 +00001877Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2, bool isExact) {
1878 return get(Instruction::AShr, C1, C2,
1879 isExact ? PossiblyExactOperator::IsExact : 0);
Owen Andersonbaf3c402009-07-29 18:55:55 +00001880}
1881
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001882// destroyConstant - Remove the constant from the constant table...
1883//
Owen Anderson04fb7c32009-06-20 00:24:58 +00001884void ConstantExpr::destroyConstant() {
Chris Lattner93604b62010-07-17 06:13:52 +00001885 getRawType()->getContext().pImpl->ExprConstants.remove(this);
Vikram S. Adved0b1bb02002-07-15 18:19:33 +00001886 destroyConstantImpl();
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001887}
1888
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001889const char *ConstantExpr::getOpcodeName() const {
1890 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve345e0cf2002-07-14 23:13:17 +00001891}
Reid Spencer1c9c8e62004-07-17 23:48:33 +00001892
Chris Lattner04e3b1e2010-03-30 20:48:48 +00001893
1894
1895GetElementPtrConstantExpr::
1896GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
1897 const Type *DestTy)
1898 : ConstantExpr(DestTy, Instruction::GetElementPtr,
1899 OperandTraits<GetElementPtrConstantExpr>::op_end(this)
1900 - (IdxList.size()+1), IdxList.size()+1) {
1901 OperandList[0] = C;
1902 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
1903 OperandList[i+1] = IdxList[i];
1904}
1905
1906
Chris Lattner5cbade92005-10-03 21:58:36 +00001907//===----------------------------------------------------------------------===//
1908// replaceUsesOfWithOnConstant implementations
1909
Chris Lattner54984052007-08-21 00:55:23 +00001910/// replaceUsesOfWithOnConstant - Update this constant array to change uses of
1911/// 'From' to be uses of 'To'. This must update the uniquing data structures
1912/// etc.
1913///
1914/// Note that we intentionally replace all uses of From with To here. Consider
1915/// a large array that uses 'From' 1000 times. By handling this case all here,
1916/// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that
1917/// single invocation handles all 1000 uses. Handling them one at a time would
1918/// work, but would be really slow because it would have to unique each updated
1919/// array instance.
Chris Lattner2ee11ec2009-10-28 00:01:44 +00001920///
Chris Lattner5cbade92005-10-03 21:58:36 +00001921void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00001922 Use *U) {
Owen Anderson1fd70962009-07-28 18:32:17 +00001923 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
1924 Constant *ToC = cast<Constant>(To);
1925
Chris Lattner93604b62010-07-17 06:13:52 +00001926 LLVMContextImpl *pImpl = getRawType()->getContext().pImpl;
Owen Anderson1fd70962009-07-28 18:32:17 +00001927
Dan Gohmane3394d42009-09-15 15:58:07 +00001928 std::pair<LLVMContextImpl::ArrayConstantsTy::MapKey, ConstantArray*> Lookup;
Chris Lattner93604b62010-07-17 06:13:52 +00001929 Lookup.first.first = cast<ArrayType>(getRawType());
Owen Anderson1fd70962009-07-28 18:32:17 +00001930 Lookup.second = this;
1931
1932 std::vector<Constant*> &Values = Lookup.first.second;
1933 Values.reserve(getNumOperands()); // Build replacement array.
1934
1935 // Fill values with the modified operands of the constant array. Also,
1936 // compute whether this turns into an all-zeros array.
1937 bool isAllZeros = false;
1938 unsigned NumUpdated = 0;
1939 if (!ToC->isNullValue()) {
1940 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1941 Constant *Val = cast<Constant>(O->get());
1942 if (Val == From) {
1943 Val = ToC;
1944 ++NumUpdated;
1945 }
1946 Values.push_back(Val);
1947 }
1948 } else {
1949 isAllZeros = true;
1950 for (Use *O = OperandList, *E = OperandList+getNumOperands();O != E; ++O) {
1951 Constant *Val = cast<Constant>(O->get());
1952 if (Val == From) {
1953 Val = ToC;
1954 ++NumUpdated;
1955 }
1956 Values.push_back(Val);
1957 if (isAllZeros) isAllZeros = Val->isNullValue();
1958 }
1959 }
1960
1961 Constant *Replacement = 0;
1962 if (isAllZeros) {
Chris Lattner93604b62010-07-17 06:13:52 +00001963 Replacement = ConstantAggregateZero::get(getRawType());
Owen Anderson1fd70962009-07-28 18:32:17 +00001964 } else {
1965 // Check to see if we have this array type already.
Owen Anderson1fd70962009-07-28 18:32:17 +00001966 bool Exists;
1967 LLVMContextImpl::ArrayConstantsTy::MapTy::iterator I =
1968 pImpl->ArrayConstants.InsertOrGetItem(Lookup, Exists);
1969
1970 if (Exists) {
Devang Patel5f4ac842009-09-03 01:39:20 +00001971 Replacement = I->second;
Owen Anderson1fd70962009-07-28 18:32:17 +00001972 } else {
1973 // Okay, the new shape doesn't exist in the system yet. Instead of
1974 // creating a new constant array, inserting it, replaceallusesof'ing the
1975 // old with the new, then deleting the old... just update the current one
1976 // in place!
1977 pImpl->ArrayConstants.MoveConstantToNewSlot(this, I);
1978
1979 // Update to the new value. Optimize for the case when we have a single
1980 // operand that we're changing, but handle bulk updates efficiently.
1981 if (NumUpdated == 1) {
1982 unsigned OperandToUpdate = U - OperandList;
1983 assert(getOperand(OperandToUpdate) == From &&
1984 "ReplaceAllUsesWith broken!");
1985 setOperand(OperandToUpdate, ToC);
1986 } else {
1987 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1988 if (getOperand(i) == From)
1989 setOperand(i, ToC);
1990 }
1991 return;
1992 }
1993 }
Chris Lattnercea141f2005-10-03 22:51:37 +00001994
1995 // Otherwise, I do need to replace this with an existing value.
Chris Lattner5cbade92005-10-03 21:58:36 +00001996 assert(Replacement != this && "I didn't contain From!");
1997
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00001998 // Everyone using this now uses the replacement.
1999 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattner5cbade92005-10-03 21:58:36 +00002000
2001 // Delete the old constant!
2002 destroyConstant();
2003}
2004
2005void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002006 Use *U) {
Owen Anderson8fa33382009-07-27 22:29:26 +00002007 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2008 Constant *ToC = cast<Constant>(To);
2009
2010 unsigned OperandToUpdate = U-OperandList;
2011 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
2012
Dan Gohmane3394d42009-09-15 15:58:07 +00002013 std::pair<LLVMContextImpl::StructConstantsTy::MapKey, ConstantStruct*> Lookup;
Chris Lattner93604b62010-07-17 06:13:52 +00002014 Lookup.first.first = cast<StructType>(getRawType());
Owen Anderson8fa33382009-07-27 22:29:26 +00002015 Lookup.second = this;
2016 std::vector<Constant*> &Values = Lookup.first.second;
2017 Values.reserve(getNumOperands()); // Build replacement struct.
2018
2019
2020 // Fill values with the modified operands of the constant struct. Also,
2021 // compute whether this turns into an all-zeros struct.
2022 bool isAllZeros = false;
2023 if (!ToC->isNullValue()) {
2024 for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O)
2025 Values.push_back(cast<Constant>(O->get()));
2026 } else {
2027 isAllZeros = true;
2028 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2029 Constant *Val = cast<Constant>(O->get());
2030 Values.push_back(Val);
2031 if (isAllZeros) isAllZeros = Val->isNullValue();
2032 }
2033 }
2034 Values[OperandToUpdate] = ToC;
2035
Chris Lattner93604b62010-07-17 06:13:52 +00002036 LLVMContextImpl *pImpl = getRawType()->getContext().pImpl;
Owen Anderson8fa33382009-07-27 22:29:26 +00002037
2038 Constant *Replacement = 0;
2039 if (isAllZeros) {
Chris Lattner93604b62010-07-17 06:13:52 +00002040 Replacement = ConstantAggregateZero::get(getRawType());
Owen Anderson8fa33382009-07-27 22:29:26 +00002041 } else {
Chris Lattner93604b62010-07-17 06:13:52 +00002042 // Check to see if we have this struct type already.
Owen Anderson8fa33382009-07-27 22:29:26 +00002043 bool Exists;
2044 LLVMContextImpl::StructConstantsTy::MapTy::iterator I =
2045 pImpl->StructConstants.InsertOrGetItem(Lookup, Exists);
2046
2047 if (Exists) {
Devang Patel5f4ac842009-09-03 01:39:20 +00002048 Replacement = I->second;
Owen Anderson8fa33382009-07-27 22:29:26 +00002049 } else {
2050 // Okay, the new shape doesn't exist in the system yet. Instead of
2051 // creating a new constant struct, inserting it, replaceallusesof'ing the
2052 // old with the new, then deleting the old... just update the current one
2053 // in place!
2054 pImpl->StructConstants.MoveConstantToNewSlot(this, I);
2055
2056 // Update to the new value.
2057 setOperand(OperandToUpdate, ToC);
2058 return;
2059 }
2060 }
2061
2062 assert(Replacement != this && "I didn't contain From!");
Chris Lattner5cbade92005-10-03 21:58:36 +00002063
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002064 // Everyone using this now uses the replacement.
2065 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattner5cbade92005-10-03 21:58:36 +00002066
2067 // Delete the old constant!
2068 destroyConstant();
2069}
2070
Reid Spencer9d6565a2007-02-15 02:26:10 +00002071void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002072 Use *U) {
Chris Lattner5cbade92005-10-03 21:58:36 +00002073 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2074
2075 std::vector<Constant*> Values;
2076 Values.reserve(getNumOperands()); // Build replacement array...
2077 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2078 Constant *Val = getOperand(i);
2079 if (Val == From) Val = cast<Constant>(To);
2080 Values.push_back(Val);
2081 }
2082
Chris Lattner93604b62010-07-17 06:13:52 +00002083 Constant *Replacement = get(cast<VectorType>(getRawType()), Values);
Chris Lattner5cbade92005-10-03 21:58:36 +00002084 assert(Replacement != this && "I didn't contain From!");
2085
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002086 // Everyone using this now uses the replacement.
2087 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattner5cbade92005-10-03 21:58:36 +00002088
2089 // Delete the old constant!
2090 destroyConstant();
2091}
2092
2093void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002094 Use *U) {
Chris Lattner5cbade92005-10-03 21:58:36 +00002095 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2096 Constant *To = cast<Constant>(ToV);
2097
2098 Constant *Replacement = 0;
2099 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerf9021ff2007-02-19 20:01:23 +00002100 SmallVector<Constant*, 8> Indices;
Chris Lattner5cbade92005-10-03 21:58:36 +00002101 Constant *Pointer = getOperand(0);
2102 Indices.reserve(getNumOperands()-1);
2103 if (Pointer == From) Pointer = To;
2104
2105 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2106 Constant *Val = getOperand(i);
2107 if (Val == From) Val = To;
2108 Indices.push_back(Val);
2109 }
Chris Lattnerf9021ff2007-02-19 20:01:23 +00002110 Replacement = ConstantExpr::getGetElementPtr(Pointer,
Chris Lattner33a8f332011-02-11 05:37:21 +00002111 &Indices[0], Indices.size(),
2112 cast<GEPOperator>(this)->isInBounds());
Dan Gohman041e2eb2008-05-15 19:50:34 +00002113 } else if (getOpcode() == Instruction::ExtractValue) {
Dan Gohman041e2eb2008-05-15 19:50:34 +00002114 Constant *Agg = getOperand(0);
Dan Gohman041e2eb2008-05-15 19:50:34 +00002115 if (Agg == From) Agg = To;
2116
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002117 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman041e2eb2008-05-15 19:50:34 +00002118 Replacement = ConstantExpr::getExtractValue(Agg,
2119 &Indices[0], Indices.size());
2120 } else if (getOpcode() == Instruction::InsertValue) {
Dan Gohman041e2eb2008-05-15 19:50:34 +00002121 Constant *Agg = getOperand(0);
2122 Constant *Val = getOperand(1);
Dan Gohman041e2eb2008-05-15 19:50:34 +00002123 if (Agg == From) Agg = To;
2124 if (Val == From) Val = To;
2125
Dan Gohman81a0c0b2008-05-31 00:58:22 +00002126 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman041e2eb2008-05-15 19:50:34 +00002127 Replacement = ConstantExpr::getInsertValue(Agg, Val,
2128 &Indices[0], Indices.size());
Reid Spencer3da59db2006-11-27 01:05:10 +00002129 } else if (isCast()) {
Chris Lattner5cbade92005-10-03 21:58:36 +00002130 assert(getOperand(0) == From && "Cast only has one use!");
Chris Lattner93604b62010-07-17 06:13:52 +00002131 Replacement = ConstantExpr::getCast(getOpcode(), To, getRawType());
Chris Lattner5cbade92005-10-03 21:58:36 +00002132 } else if (getOpcode() == Instruction::Select) {
2133 Constant *C1 = getOperand(0);
2134 Constant *C2 = getOperand(1);
2135 Constant *C3 = getOperand(2);
2136 if (C1 == From) C1 = To;
2137 if (C2 == From) C2 = To;
2138 if (C3 == From) C3 = To;
2139 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00002140 } else if (getOpcode() == Instruction::ExtractElement) {
2141 Constant *C1 = getOperand(0);
2142 Constant *C2 = getOperand(1);
2143 if (C1 == From) C1 = To;
2144 if (C2 == From) C2 = To;
2145 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattner42b55802006-04-08 05:09:48 +00002146 } else if (getOpcode() == Instruction::InsertElement) {
2147 Constant *C1 = getOperand(0);
2148 Constant *C2 = getOperand(1);
2149 Constant *C3 = getOperand(1);
2150 if (C1 == From) C1 = To;
2151 if (C2 == From) C2 = To;
2152 if (C3 == From) C3 = To;
2153 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2154 } else if (getOpcode() == Instruction::ShuffleVector) {
2155 Constant *C1 = getOperand(0);
2156 Constant *C2 = getOperand(1);
2157 Constant *C3 = getOperand(2);
2158 if (C1 == From) C1 = To;
2159 if (C2 == From) C2 = To;
2160 if (C3 == From) C3 = To;
2161 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spencer077d0eb2006-12-04 05:19:50 +00002162 } else if (isCompare()) {
2163 Constant *C1 = getOperand(0);
2164 Constant *C2 = getOperand(1);
2165 if (C1 == From) C1 = To;
2166 if (C2 == From) C2 = To;
2167 if (getOpcode() == Instruction::ICmp)
2168 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
Chris Lattnerbbedb0e2008-07-14 05:17:31 +00002169 else {
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002170 assert(getOpcode() == Instruction::FCmp);
2171 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnerbbedb0e2008-07-14 05:17:31 +00002172 }
Chris Lattner5cbade92005-10-03 21:58:36 +00002173 } else if (getNumOperands() == 2) {
2174 Constant *C1 = getOperand(0);
2175 Constant *C2 = getOperand(1);
2176 if (C1 == From) C1 = To;
2177 if (C2 == From) C2 = To;
Chris Lattnercafe9bb2009-12-29 02:14:09 +00002178 Replacement = ConstantExpr::get(getOpcode(), C1, C2, SubclassOptionalData);
Chris Lattner5cbade92005-10-03 21:58:36 +00002179 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00002180 llvm_unreachable("Unknown ConstantExpr type!");
Chris Lattner5cbade92005-10-03 21:58:36 +00002181 return;
2182 }
2183
2184 assert(Replacement != this && "I didn't contain From!");
2185
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +00002186 // Everyone using this now uses the replacement.
2187 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattner5cbade92005-10-03 21:58:36 +00002188
2189 // Delete the old constant!
2190 destroyConstant();
Matthijs Kooijman10b9de62008-07-03 07:46:41 +00002191}