blob: c34234ad589ae0d84cbf44ef3a6ceec7ac0f4db1 [file] [log] [blame]
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001//===-- Constants.cpp - Implement Constant nodes --------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattnere17322b2011-02-07 20:03:14 +000010// This file implements the Constant* classes.
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerca142372002-04-28 19:55:58 +000014#include "llvm/Constants.h"
Chris Lattner78683a72009-08-23 04:02:03 +000015#include "LLVMContextImpl.h"
Chris Lattner33e93b82007-02-27 03:05:06 +000016#include "ConstantFold.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000017#include "llvm/DerivedTypes.h"
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000018#include "llvm/GlobalValue.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000019#include "llvm/Instructions.h"
Chris Lattnerd7a73302001-10-13 06:57:33 +000020#include "llvm/Module.h"
Dan Gohman7d82e132009-07-18 01:49:22 +000021#include "llvm/Operator.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000022#include "llvm/ADT/FoldingSet.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000023#include "llvm/ADT/StringExtras.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000024#include "llvm/ADT/StringMap.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000025#include "llvm/Support/Compiler.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000026#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000027#include "llvm/Support/ErrorHandling.h"
Chris Lattner69edc982006-09-28 00:35:06 +000028#include "llvm/Support/ManagedStatic.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000029#include "llvm/Support/MathExtras.h"
Chris Lattner78683a72009-08-23 04:02:03 +000030#include "llvm/Support/raw_ostream.h"
Dan Gohman7190d482009-09-10 23:37:55 +000031#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattnera80bf0b2007-02-20 06:39:57 +000032#include "llvm/ADT/DenseMap.h"
Chris Lattnerb5d70302007-02-19 20:01:23 +000033#include "llvm/ADT/SmallVector.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000034#include <algorithm>
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000035#include <map>
Talin3a0a30d2011-02-28 23:53:27 +000036#include <cstdarg>
Chris Lattner189d19f2003-11-21 20:23:48 +000037using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000038
Chris Lattner2f7c9632001-06-06 20:29:01 +000039//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000040// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000041//===----------------------------------------------------------------------===//
42
Owen Anderson5a1acd92009-07-31 20:28:14 +000043// Constructor to create a '0' constant of arbitrary type...
Chris Lattner74eb5d72009-10-30 22:33:29 +000044Constant *Constant::getNullValue(const Type *Ty) {
Owen Anderson5a1acd92009-07-31 20:28:14 +000045 switch (Ty->getTypeID()) {
46 case Type::IntegerTyID:
47 return ConstantInt::get(Ty, 0);
48 case Type::FloatTyID:
Benjamin Kramer8ceebfa2010-12-04 14:22:24 +000049 return ConstantFP::get(Ty->getContext(),
50 APFloat::getZero(APFloat::IEEEsingle));
Owen Anderson5a1acd92009-07-31 20:28:14 +000051 case Type::DoubleTyID:
Benjamin Kramer8ceebfa2010-12-04 14:22:24 +000052 return ConstantFP::get(Ty->getContext(),
53 APFloat::getZero(APFloat::IEEEdouble));
Owen Anderson5a1acd92009-07-31 20:28:14 +000054 case Type::X86_FP80TyID:
Benjamin Kramer8ceebfa2010-12-04 14:22:24 +000055 return ConstantFP::get(Ty->getContext(),
56 APFloat::getZero(APFloat::x87DoubleExtended));
Owen Anderson5a1acd92009-07-31 20:28:14 +000057 case Type::FP128TyID:
58 return ConstantFP::get(Ty->getContext(),
Benjamin Kramer8ceebfa2010-12-04 14:22:24 +000059 APFloat::getZero(APFloat::IEEEquad));
Owen Anderson5a1acd92009-07-31 20:28:14 +000060 case Type::PPC_FP128TyID:
Benjamin Kramer8ceebfa2010-12-04 14:22:24 +000061 return ConstantFP::get(Ty->getContext(),
Benjamin Kramer6f88fcb2010-12-04 14:43:08 +000062 APFloat(APInt::getNullValue(128)));
Owen Anderson5a1acd92009-07-31 20:28:14 +000063 case Type::PointerTyID:
64 return ConstantPointerNull::get(cast<PointerType>(Ty));
65 case Type::StructTyID:
66 case Type::ArrayTyID:
67 case Type::VectorTyID:
68 return ConstantAggregateZero::get(Ty);
69 default:
70 // Function, Label, or Opaque type?
71 assert(!"Cannot create a null constant of that type!");
72 return 0;
73 }
74}
75
Chris Lattnera676c0f2011-02-07 16:40:21 +000076Constant *Constant::getIntegerValue(const Type *Ty, const APInt &V) {
Dan Gohmanf011f5a2009-08-03 22:07:33 +000077 const Type *ScalarTy = Ty->getScalarType();
78
79 // Create the base integer constant.
80 Constant *C = ConstantInt::get(Ty->getContext(), V);
81
82 // Convert an integer to a pointer, if necessary.
83 if (const PointerType *PTy = dyn_cast<PointerType>(ScalarTy))
84 C = ConstantExpr::getIntToPtr(C, PTy);
85
86 // Broadcast a scalar to a vector, if necessary.
87 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
88 C = ConstantVector::get(std::vector<Constant *>(VTy->getNumElements(), C));
89
90 return C;
91}
92
Chris Lattnera676c0f2011-02-07 16:40:21 +000093Constant *Constant::getAllOnesValue(const Type *Ty) {
Chris Lattner74eb5d72009-10-30 22:33:29 +000094 if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty))
Owen Anderson5a1acd92009-07-31 20:28:14 +000095 return ConstantInt::get(Ty->getContext(),
96 APInt::getAllOnesValue(ITy->getBitWidth()));
Nadav Rotem7cc6d122011-02-17 21:22:27 +000097
98 if (Ty->isFloatingPointTy()) {
99 APFloat FL = APFloat::getAllOnesValue(Ty->getPrimitiveSizeInBits(),
100 !Ty->isPPC_FP128Ty());
101 return ConstantFP::get(Ty->getContext(), FL);
102 }
103
Chris Lattner69229312011-02-15 00:14:00 +0000104 SmallVector<Constant*, 16> Elts;
Chris Lattner74eb5d72009-10-30 22:33:29 +0000105 const VectorType *VTy = cast<VectorType>(Ty);
Owen Anderson5a1acd92009-07-31 20:28:14 +0000106 Elts.resize(VTy->getNumElements(), getAllOnesValue(VTy->getElementType()));
107 assert(Elts[0] && "Not a vector integer type!");
108 return cast<ConstantVector>(ConstantVector::get(Elts));
109}
110
Chris Lattner3462ae32001-12-03 22:26:30 +0000111void Constant::destroyConstantImpl() {
112 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +0000113 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +0000114 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +0000115 // but they don't know that. Because we only find out when the CPV is
116 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +0000117 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +0000118 //
119 while (!use_empty()) {
120 Value *V = use_back();
121#ifndef NDEBUG // Only in -g mode...
Chris Lattner78683a72009-08-23 04:02:03 +0000122 if (!isa<Constant>(V)) {
David Greene1e27a132010-01-05 01:29:19 +0000123 dbgs() << "While deleting: " << *this
Chris Lattner78683a72009-08-23 04:02:03 +0000124 << "\n\nUse still stuck around after Def is destroyed: "
125 << *V << "\n\n";
126 }
Chris Lattnerd7a73302001-10-13 06:57:33 +0000127#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000128 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1ebe1ab2004-07-17 23:48:33 +0000129 Constant *CV = cast<Constant>(V);
130 CV->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +0000131
132 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000133 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +0000134 }
135
136 // Value has no outstanding references it is safe to delete it now...
137 delete this;
Chris Lattner38569342001-10-01 20:11:19 +0000138}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000139
Chris Lattner23dd1f62006-10-20 00:27:06 +0000140/// canTrap - Return true if evaluation of this constant could trap. This is
141/// true for things like constant expressions that could divide by zero.
142bool Constant::canTrap() const {
143 assert(getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
144 // The only thing that could possibly trap are constant exprs.
145 const ConstantExpr *CE = dyn_cast<ConstantExpr>(this);
146 if (!CE) return false;
147
148 // ConstantExpr traps if any operands can trap.
149 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Chris Lattnera91a5632009-10-28 05:14:34 +0000150 if (CE->getOperand(i)->canTrap())
Chris Lattner23dd1f62006-10-20 00:27:06 +0000151 return true;
152
153 // Otherwise, only specific operations can trap.
154 switch (CE->getOpcode()) {
155 default:
156 return false;
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000157 case Instruction::UDiv:
158 case Instruction::SDiv:
159 case Instruction::FDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +0000160 case Instruction::URem:
161 case Instruction::SRem:
162 case Instruction::FRem:
Chris Lattner23dd1f62006-10-20 00:27:06 +0000163 // Div and rem can trap if the RHS is not known to be non-zero.
Chris Lattnera91a5632009-10-28 05:14:34 +0000164 if (!isa<ConstantInt>(CE->getOperand(1)) ||CE->getOperand(1)->isNullValue())
Chris Lattner23dd1f62006-10-20 00:27:06 +0000165 return true;
166 return false;
167 }
168}
169
Chris Lattner253bc772009-11-01 18:11:50 +0000170/// isConstantUsed - Return true if the constant has users other than constant
171/// exprs and other dangling things.
172bool Constant::isConstantUsed() const {
Gabor Greifc78d7202010-03-25 23:06:16 +0000173 for (const_use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Chris Lattner253bc772009-11-01 18:11:50 +0000174 const Constant *UC = dyn_cast<Constant>(*UI);
175 if (UC == 0 || isa<GlobalValue>(UC))
176 return true;
177
178 if (UC->isConstantUsed())
179 return true;
180 }
181 return false;
182}
183
184
Chris Lattner4565ef52009-07-22 00:05:44 +0000185
186/// getRelocationInfo - This method classifies the entry according to
187/// whether or not it may generate a relocation entry. This must be
188/// conservative, so if it might codegen to a relocatable entry, it should say
189/// so. The return values are:
190///
Chris Lattner5cd4dd32009-07-24 03:27:21 +0000191/// NoRelocation: This constant pool entry is guaranteed to never have a
192/// relocation applied to it (because it holds a simple constant like
193/// '4').
194/// LocalRelocation: This entry has relocations, but the entries are
195/// guaranteed to be resolvable by the static linker, so the dynamic
196/// linker will never see them.
197/// GlobalRelocations: This entry may have arbitrary relocations.
Chris Lattner4565ef52009-07-22 00:05:44 +0000198///
199/// FIXME: This really should not be in VMCore.
Chris Lattner5cd4dd32009-07-24 03:27:21 +0000200Constant::PossibleRelocationsTy Constant::getRelocationInfo() const {
201 if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
Chris Lattner4565ef52009-07-22 00:05:44 +0000202 if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
Chris Lattner5cd4dd32009-07-24 03:27:21 +0000203 return LocalRelocation; // Local to this file/library.
204 return GlobalRelocations; // Global reference.
Anton Korobeynikov7437b592009-03-29 17:13:18 +0000205 }
Chris Lattner4565ef52009-07-22 00:05:44 +0000206
Chris Lattner2cb85b42009-10-28 04:12:16 +0000207 if (const BlockAddress *BA = dyn_cast<BlockAddress>(this))
208 return BA->getFunction()->getRelocationInfo();
209
Chris Lattnera7cfc432010-01-03 18:09:40 +0000210 // While raw uses of blockaddress need to be relocated, differences between
211 // two of them don't when they are for labels in the same function. This is a
212 // common idiom when creating a table for the indirect goto extension, so we
213 // handle it efficiently here.
214 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(this))
215 if (CE->getOpcode() == Instruction::Sub) {
216 ConstantExpr *LHS = dyn_cast<ConstantExpr>(CE->getOperand(0));
217 ConstantExpr *RHS = dyn_cast<ConstantExpr>(CE->getOperand(1));
218 if (LHS && RHS &&
219 LHS->getOpcode() == Instruction::PtrToInt &&
220 RHS->getOpcode() == Instruction::PtrToInt &&
221 isa<BlockAddress>(LHS->getOperand(0)) &&
222 isa<BlockAddress>(RHS->getOperand(0)) &&
223 cast<BlockAddress>(LHS->getOperand(0))->getFunction() ==
224 cast<BlockAddress>(RHS->getOperand(0))->getFunction())
225 return NoRelocation;
226 }
227
Chris Lattner5cd4dd32009-07-24 03:27:21 +0000228 PossibleRelocationsTy Result = NoRelocation;
Evan Chengf9e003b2007-03-08 00:59:12 +0000229 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Chris Lattnera91a5632009-10-28 05:14:34 +0000230 Result = std::max(Result,
231 cast<Constant>(getOperand(i))->getRelocationInfo());
Chris Lattner4565ef52009-07-22 00:05:44 +0000232
233 return Result;
Evan Chengf9e003b2007-03-08 00:59:12 +0000234}
235
Chris Lattner4565ef52009-07-22 00:05:44 +0000236
Chris Lattner2105d662008-07-10 00:28:11 +0000237/// getVectorElements - This method, which is only valid on constant of vector
238/// type, returns the elements of the vector in the specified smallvector.
Chris Lattnerc5098a22008-07-14 05:10:41 +0000239/// This handles breaking down a vector undef into undef elements, etc. For
240/// constant exprs and other cases we can't handle, we return an empty vector.
Chris Lattnerf5edeeb2010-02-01 20:48:08 +0000241void Constant::getVectorElements(SmallVectorImpl<Constant*> &Elts) const {
Duncan Sands19d0b472010-02-16 11:11:14 +0000242 assert(getType()->isVectorTy() && "Not a vector constant!");
Chris Lattner2105d662008-07-10 00:28:11 +0000243
244 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) {
245 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i)
246 Elts.push_back(CV->getOperand(i));
247 return;
248 }
249
250 const VectorType *VT = cast<VectorType>(getType());
251 if (isa<ConstantAggregateZero>(this)) {
252 Elts.assign(VT->getNumElements(),
Owen Anderson5a1acd92009-07-31 20:28:14 +0000253 Constant::getNullValue(VT->getElementType()));
Chris Lattner2105d662008-07-10 00:28:11 +0000254 return;
255 }
256
Chris Lattnerc5098a22008-07-14 05:10:41 +0000257 if (isa<UndefValue>(this)) {
Owen Andersonb292b8c2009-07-30 23:03:37 +0000258 Elts.assign(VT->getNumElements(), UndefValue::get(VT->getElementType()));
Chris Lattnerc5098a22008-07-14 05:10:41 +0000259 return;
260 }
261
262 // Unknown type, must be constant expr etc.
Chris Lattner2105d662008-07-10 00:28:11 +0000263}
264
265
Chris Lattner84886402011-02-18 04:41:42 +0000266/// removeDeadUsersOfConstant - If the specified constantexpr is dead, remove
267/// it. This involves recursively eliminating any dead users of the
268/// constantexpr.
269static bool removeDeadUsersOfConstant(const Constant *C) {
270 if (isa<GlobalValue>(C)) return false; // Cannot remove this
271
272 while (!C->use_empty()) {
273 const Constant *User = dyn_cast<Constant>(C->use_back());
274 if (!User) return false; // Non-constant usage;
275 if (!removeDeadUsersOfConstant(User))
276 return false; // Constant wasn't dead
277 }
278
279 const_cast<Constant*>(C)->destroyConstant();
280 return true;
281}
282
283
284/// removeDeadConstantUsers - If there are any dead constant users dangling
285/// off of this constant, remove them. This method is useful for clients
286/// that want to check to see if a global is unused, but don't want to deal
287/// with potentially dead constants hanging off of the globals.
288void Constant::removeDeadConstantUsers() const {
289 Value::const_use_iterator I = use_begin(), E = use_end();
290 Value::const_use_iterator LastNonDeadUser = E;
291 while (I != E) {
292 const Constant *User = dyn_cast<Constant>(*I);
293 if (User == 0) {
294 LastNonDeadUser = I;
295 ++I;
296 continue;
297 }
298
299 if (!removeDeadUsersOfConstant(User)) {
300 // If the constant wasn't dead, remember that this was the last live use
301 // and move on to the next constant.
302 LastNonDeadUser = I;
303 ++I;
304 continue;
305 }
306
307 // If the constant was dead, then the iterator is invalidated.
308 if (LastNonDeadUser == E) {
309 I = use_begin();
310 if (I == E) break;
311 } else {
312 I = LastNonDeadUser;
313 ++I;
314 }
315 }
316}
317
318
Chris Lattner2105d662008-07-10 00:28:11 +0000319
Chris Lattner2f7c9632001-06-06 20:29:01 +0000320//===----------------------------------------------------------------------===//
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000321// ConstantInt
Chris Lattner2f7c9632001-06-06 20:29:01 +0000322//===----------------------------------------------------------------------===//
323
Reid Spencerb31bffe2007-02-26 23:54:03 +0000324ConstantInt::ConstantInt(const IntegerType *Ty, const APInt& V)
Chris Lattner5db2f472007-02-20 05:55:46 +0000325 : Constant(Ty, ConstantIntVal, 0, 0), Val(V) {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000326 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000327}
328
Nick Lewycky92db8e82011-03-06 03:36:19 +0000329ConstantInt *ConstantInt::getTrue(LLVMContext &Context) {
Owen Anderson23a204d2009-07-31 17:39:07 +0000330 LLVMContextImpl *pImpl = Context.pImpl;
Benjamin Kramerddd1b7b2010-11-20 18:43:35 +0000331 if (!pImpl->TheTrueVal)
332 pImpl->TheTrueVal = ConstantInt::get(Type::getInt1Ty(Context), 1);
333 return pImpl->TheTrueVal;
Owen Anderson23a204d2009-07-31 17:39:07 +0000334}
335
Nick Lewycky92db8e82011-03-06 03:36:19 +0000336ConstantInt *ConstantInt::getFalse(LLVMContext &Context) {
Owen Anderson23a204d2009-07-31 17:39:07 +0000337 LLVMContextImpl *pImpl = Context.pImpl;
Benjamin Kramerddd1b7b2010-11-20 18:43:35 +0000338 if (!pImpl->TheFalseVal)
339 pImpl->TheFalseVal = ConstantInt::get(Type::getInt1Ty(Context), 0);
340 return pImpl->TheFalseVal;
Owen Anderson23a204d2009-07-31 17:39:07 +0000341}
342
Nick Lewycky92db8e82011-03-06 03:36:19 +0000343Constant *ConstantInt::getTrue(const Type *Ty) {
344 const VectorType *VTy = dyn_cast<VectorType>(Ty);
345 if (!VTy) {
346 assert(Ty->isIntegerTy(1) && "True must be i1 or vector of i1.");
347 return ConstantInt::getTrue(Ty->getContext());
348 }
349 assert(VTy->getElementType()->isIntegerTy(1) &&
350 "True must be vector of i1 or i1.");
351 SmallVector<Constant*, 16> Splat(VTy->getNumElements(),
352 ConstantInt::getTrue(Ty->getContext()));
353 return ConstantVector::get(Splat);
354}
355
356Constant *ConstantInt::getFalse(const Type *Ty) {
357 const VectorType *VTy = dyn_cast<VectorType>(Ty);
358 if (!VTy) {
359 assert(Ty->isIntegerTy(1) && "False must be i1 or vector of i1.");
360 return ConstantInt::getFalse(Ty->getContext());
361 }
362 assert(VTy->getElementType()->isIntegerTy(1) &&
363 "False must be vector of i1 or i1.");
364 SmallVector<Constant*, 16> Splat(VTy->getNumElements(),
365 ConstantInt::getFalse(Ty->getContext()));
366 return ConstantVector::get(Splat);
367}
368
Owen Anderson23a204d2009-07-31 17:39:07 +0000369
Owen Andersonedb4a702009-07-24 23:12:02 +0000370// Get a ConstantInt from an APInt. Note that the value stored in the DenseMap
371// as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the
372// operator== and operator!= to ensure that the DenseMap doesn't attempt to
373// compare APInt's of different widths, which would violate an APInt class
374// invariant which generates an assertion.
Nick Lewycky92db8e82011-03-06 03:36:19 +0000375ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt &V) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000376 // Get the corresponding integer type for the bit width of the value.
Owen Anderson55f1c092009-08-13 21:58:54 +0000377 const IntegerType *ITy = IntegerType::get(Context, V.getBitWidth());
Owen Andersonedb4a702009-07-24 23:12:02 +0000378 // get an existing value or the insertion position
379 DenseMapAPIntKeyInfo::KeyTy Key(V, ITy);
Owen Andersonedb4a702009-07-24 23:12:02 +0000380 ConstantInt *&Slot = Context.pImpl->IntConstants[Key];
Owen Anderson5dab84c2009-10-19 20:11:52 +0000381 if (!Slot) Slot = new ConstantInt(ITy, V);
382 return Slot;
Owen Andersonedb4a702009-07-24 23:12:02 +0000383}
384
Nick Lewycky92db8e82011-03-06 03:36:19 +0000385Constant *ConstantInt::get(const Type *Ty, uint64_t V, bool isSigned) {
386 Constant *C = get(cast<IntegerType>(Ty->getScalarType()), V, isSigned);
Owen Andersonedb4a702009-07-24 23:12:02 +0000387
388 // For vectors, broadcast the value.
389 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
Chris Lattner69229312011-02-15 00:14:00 +0000390 return ConstantVector::get(SmallVector<Constant*,
391 16>(VTy->getNumElements(), C));
Owen Andersonedb4a702009-07-24 23:12:02 +0000392
393 return C;
394}
395
396ConstantInt* ConstantInt::get(const IntegerType* Ty, uint64_t V,
397 bool isSigned) {
398 return get(Ty->getContext(), APInt(Ty->getBitWidth(), V, isSigned));
399}
400
401ConstantInt* ConstantInt::getSigned(const IntegerType* Ty, int64_t V) {
402 return get(Ty, V, true);
403}
404
405Constant *ConstantInt::getSigned(const Type *Ty, int64_t V) {
406 return get(Ty, V, true);
407}
408
Chris Lattnera676c0f2011-02-07 16:40:21 +0000409Constant *ConstantInt::get(const Type* Ty, const APInt& V) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000410 ConstantInt *C = get(Ty->getContext(), V);
411 assert(C->getType() == Ty->getScalarType() &&
412 "ConstantInt type doesn't match the type implied by its value!");
413
414 // For vectors, broadcast the value.
415 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
Owen Anderson4aa32952009-07-28 21:19:26 +0000416 return ConstantVector::get(
Chris Lattner69229312011-02-15 00:14:00 +0000417 SmallVector<Constant *, 16>(VTy->getNumElements(), C));
Owen Andersonedb4a702009-07-24 23:12:02 +0000418
419 return C;
420}
421
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000422ConstantInt* ConstantInt::get(const IntegerType* Ty, StringRef Str,
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000423 uint8_t radix) {
424 return get(Ty->getContext(), APInt(Ty->getBitWidth(), Str, radix));
425}
426
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000427//===----------------------------------------------------------------------===//
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000428// ConstantFP
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000429//===----------------------------------------------------------------------===//
430
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000431static const fltSemantics *TypeToFloatSemantics(const Type *Ty) {
Chris Lattnerfdd87902009-10-05 05:54:46 +0000432 if (Ty->isFloatTy())
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000433 return &APFloat::IEEEsingle;
Chris Lattnerfdd87902009-10-05 05:54:46 +0000434 if (Ty->isDoubleTy())
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000435 return &APFloat::IEEEdouble;
Chris Lattnerfdd87902009-10-05 05:54:46 +0000436 if (Ty->isX86_FP80Ty())
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000437 return &APFloat::x87DoubleExtended;
Chris Lattnerfdd87902009-10-05 05:54:46 +0000438 else if (Ty->isFP128Ty())
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000439 return &APFloat::IEEEquad;
440
Chris Lattnerfdd87902009-10-05 05:54:46 +0000441 assert(Ty->isPPC_FP128Ty() && "Unknown FP format");
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000442 return &APFloat::PPCDoubleDouble;
443}
444
Owen Anderson69c464d2009-07-27 20:59:43 +0000445/// get() - This returns a constant fp for the specified value in the
446/// specified type. This should only be used for simple constant values like
447/// 2.0/1.0 etc, that are known-valid both as double and as the target format.
Chris Lattnera676c0f2011-02-07 16:40:21 +0000448Constant *ConstantFP::get(const Type* Ty, double V) {
Owen Anderson69c464d2009-07-27 20:59:43 +0000449 LLVMContext &Context = Ty->getContext();
450
451 APFloat FV(V);
452 bool ignored;
453 FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
454 APFloat::rmNearestTiesToEven, &ignored);
455 Constant *C = get(Context, FV);
456
457 // For vectors, broadcast the value.
458 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
Owen Anderson4aa32952009-07-28 21:19:26 +0000459 return ConstantVector::get(
Chris Lattner69229312011-02-15 00:14:00 +0000460 SmallVector<Constant *, 16>(VTy->getNumElements(), C));
Owen Anderson69c464d2009-07-27 20:59:43 +0000461
462 return C;
463}
464
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000465
Chris Lattnera676c0f2011-02-07 16:40:21 +0000466Constant *ConstantFP::get(const Type* Ty, StringRef Str) {
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000467 LLVMContext &Context = Ty->getContext();
468
469 APFloat FV(*TypeToFloatSemantics(Ty->getScalarType()), Str);
470 Constant *C = get(Context, FV);
471
472 // For vectors, broadcast the value.
473 if (const VectorType *VTy = dyn_cast<VectorType>(Ty))
474 return ConstantVector::get(
Chris Lattner69229312011-02-15 00:14:00 +0000475 SmallVector<Constant *, 16>(VTy->getNumElements(), C));
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000476
477 return C;
478}
479
480
Owen Anderson69c464d2009-07-27 20:59:43 +0000481ConstantFP* ConstantFP::getNegativeZero(const Type* Ty) {
482 LLVMContext &Context = Ty->getContext();
Owen Anderson5a1acd92009-07-31 20:28:14 +0000483 APFloat apf = cast <ConstantFP>(Constant::getNullValue(Ty))->getValueAPF();
Owen Anderson69c464d2009-07-27 20:59:43 +0000484 apf.changeSign();
485 return get(Context, apf);
486}
487
488
Chris Lattnera676c0f2011-02-07 16:40:21 +0000489Constant *ConstantFP::getZeroValueForNegation(const Type* Ty) {
Owen Anderson69c464d2009-07-27 20:59:43 +0000490 if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
Duncan Sands9dff9be2010-02-15 16:12:20 +0000491 if (PTy->getElementType()->isFloatingPointTy()) {
Chris Lattner69229312011-02-15 00:14:00 +0000492 SmallVector<Constant*, 16> zeros(PTy->getNumElements(),
Owen Anderson69c464d2009-07-27 20:59:43 +0000493 getNegativeZero(PTy->getElementType()));
Chris Lattner69229312011-02-15 00:14:00 +0000494 return ConstantVector::get(zeros);
Owen Anderson69c464d2009-07-27 20:59:43 +0000495 }
496
Duncan Sands9dff9be2010-02-15 16:12:20 +0000497 if (Ty->isFloatingPointTy())
Owen Anderson69c464d2009-07-27 20:59:43 +0000498 return getNegativeZero(Ty);
499
Owen Anderson5a1acd92009-07-31 20:28:14 +0000500 return Constant::getNullValue(Ty);
Owen Anderson69c464d2009-07-27 20:59:43 +0000501}
502
503
504// ConstantFP accessors.
505ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) {
506 DenseMapAPFloatKeyInfo::KeyTy Key(V);
507
508 LLVMContextImpl* pImpl = Context.pImpl;
509
Owen Anderson69c464d2009-07-27 20:59:43 +0000510 ConstantFP *&Slot = pImpl->FPConstants[Key];
Owen Anderson69c464d2009-07-27 20:59:43 +0000511
512 if (!Slot) {
Owen Anderson5dab84c2009-10-19 20:11:52 +0000513 const Type *Ty;
514 if (&V.getSemantics() == &APFloat::IEEEsingle)
515 Ty = Type::getFloatTy(Context);
516 else if (&V.getSemantics() == &APFloat::IEEEdouble)
517 Ty = Type::getDoubleTy(Context);
518 else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
519 Ty = Type::getX86_FP80Ty(Context);
520 else if (&V.getSemantics() == &APFloat::IEEEquad)
521 Ty = Type::getFP128Ty(Context);
522 else {
523 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble &&
524 "Unknown FP format");
525 Ty = Type::getPPC_FP128Ty(Context);
Owen Anderson69c464d2009-07-27 20:59:43 +0000526 }
Owen Anderson5dab84c2009-10-19 20:11:52 +0000527 Slot = new ConstantFP(Ty, V);
Owen Anderson69c464d2009-07-27 20:59:43 +0000528 }
529
530 return Slot;
531}
532
Dan Gohman394468d2009-09-25 23:40:21 +0000533ConstantFP *ConstantFP::getInfinity(const Type *Ty, bool Negative) {
Dan Gohmanfeb50212009-09-25 23:00:48 +0000534 const fltSemantics &Semantics = *TypeToFloatSemantics(Ty);
535 return ConstantFP::get(Ty->getContext(),
536 APFloat::getInf(Semantics, Negative));
537}
538
Dale Johannesend246b2c2007-08-30 00:23:21 +0000539ConstantFP::ConstantFP(const Type *Ty, const APFloat& V)
540 : Constant(Ty, ConstantFPVal, 0, 0), Val(V) {
Chris Lattner98bd9392008-04-09 06:38:30 +0000541 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
542 "FP type Mismatch");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000543}
544
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000545bool ConstantFP::isNullValue() const {
Dale Johannesena719a602007-08-24 00:56:33 +0000546 return Val.isZero() && !Val.isNegative();
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000547}
548
Dale Johannesend246b2c2007-08-30 00:23:21 +0000549bool ConstantFP::isExactlyValue(const APFloat& V) const {
550 return Val.bitwiseIsEqual(V);
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000551}
552
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000553//===----------------------------------------------------------------------===//
554// ConstantXXX Classes
555//===----------------------------------------------------------------------===//
556
557
Chris Lattner3462ae32001-12-03 22:26:30 +0000558ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000559 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000560 : Constant(T, ConstantArrayVal,
561 OperandTraits<ConstantArray>::op_end(this) - V.size(),
562 V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000563 assert(V.size() == T->getNumElements() &&
564 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000565 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000566 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
567 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000568 Constant *C = *I;
Nick Lewyckyae4617c2009-10-03 19:30:43 +0000569 assert(C->getType() == T->getElementType() &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000570 "Initializer for array element doesn't match array element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000571 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000572 }
573}
574
Owen Andersonc2c79322009-07-28 18:32:17 +0000575Constant *ConstantArray::get(const ArrayType *Ty,
576 const std::vector<Constant*> &V) {
Jeffrey Yasskin8ce67f82009-09-30 21:08:08 +0000577 for (unsigned i = 0, e = V.size(); i != e; ++i) {
578 assert(V[i]->getType() == Ty->getElementType() &&
579 "Wrong type in array element initializer");
580 }
Owen Andersonc2c79322009-07-28 18:32:17 +0000581 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
582 // If this is an all-zero array, return a ConstantAggregateZero object
583 if (!V.empty()) {
584 Constant *C = V[0];
Chris Lattner09660c92009-12-30 20:25:09 +0000585 if (!C->isNullValue())
Owen Andersonc2c79322009-07-28 18:32:17 +0000586 return pImpl->ArrayConstants.getOrCreate(Ty, V);
Chris Lattner09660c92009-12-30 20:25:09 +0000587
Owen Andersonc2c79322009-07-28 18:32:17 +0000588 for (unsigned i = 1, e = V.size(); i != e; ++i)
Chris Lattner09660c92009-12-30 20:25:09 +0000589 if (V[i] != C)
Owen Andersonc2c79322009-07-28 18:32:17 +0000590 return pImpl->ArrayConstants.getOrCreate(Ty, V);
Owen Andersonc2c79322009-07-28 18:32:17 +0000591 }
592
Owen Andersonb292b8c2009-07-30 23:03:37 +0000593 return ConstantAggregateZero::get(Ty);
Owen Andersonc2c79322009-07-28 18:32:17 +0000594}
595
596
Chris Lattnera676c0f2011-02-07 16:40:21 +0000597Constant *ConstantArray::get(const ArrayType* T, Constant *const* Vals,
Owen Andersonc2c79322009-07-28 18:32:17 +0000598 unsigned NumVals) {
599 // FIXME: make this the primary ctor method.
600 return get(T, std::vector<Constant*>(Vals, Vals+NumVals));
601}
602
603/// ConstantArray::get(const string&) - Return an array that is initialized to
604/// contain the specified string. If length is zero then a null terminator is
605/// added to the specified string so that it may be used in a natural way.
606/// Otherwise, the length parameter specifies how much of the string to use
607/// and it won't be null terminated.
608///
Chris Lattnera676c0f2011-02-07 16:40:21 +0000609Constant *ConstantArray::get(LLVMContext &Context, StringRef Str,
Owen Anderson55f1c092009-08-13 21:58:54 +0000610 bool AddNull) {
Owen Andersonc2c79322009-07-28 18:32:17 +0000611 std::vector<Constant*> ElementVals;
Benjamin Kramer3d4af4e2010-08-01 11:43:26 +0000612 ElementVals.reserve(Str.size() + size_t(AddNull));
Owen Andersonc2c79322009-07-28 18:32:17 +0000613 for (unsigned i = 0; i < Str.size(); ++i)
Owen Anderson55f1c092009-08-13 21:58:54 +0000614 ElementVals.push_back(ConstantInt::get(Type::getInt8Ty(Context), Str[i]));
Owen Andersonc2c79322009-07-28 18:32:17 +0000615
616 // Add a null terminator to the string...
617 if (AddNull) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000618 ElementVals.push_back(ConstantInt::get(Type::getInt8Ty(Context), 0));
Owen Andersonc2c79322009-07-28 18:32:17 +0000619 }
620
Owen Anderson55f1c092009-08-13 21:58:54 +0000621 ArrayType *ATy = ArrayType::get(Type::getInt8Ty(Context), ElementVals.size());
Owen Andersonc2c79322009-07-28 18:32:17 +0000622 return get(ATy, ElementVals);
623}
624
Chris Lattner3462ae32001-12-03 22:26:30 +0000625ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000626 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000627 : Constant(T, ConstantStructVal,
628 OperandTraits<ConstantStruct>::op_end(this) - V.size(),
629 V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000630 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000631 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000632 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000633 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
634 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000635 Constant *C = *I;
Nick Lewyckyae4617c2009-10-03 19:30:43 +0000636 assert(C->getType() == T->getElementType(I-V.begin()) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000637 "Initializer for struct element doesn't match struct element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000638 *OL = C;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000639 }
640}
641
Owen Anderson45308b52009-07-27 22:29:26 +0000642// ConstantStruct accessors.
Chris Lattnera676c0f2011-02-07 16:40:21 +0000643Constant *ConstantStruct::get(const StructType* T,
Owen Anderson45308b52009-07-27 22:29:26 +0000644 const std::vector<Constant*>& V) {
645 LLVMContextImpl* pImpl = T->getContext().pImpl;
646
647 // Create a ConstantAggregateZero value if all elements are zeros...
648 for (unsigned i = 0, e = V.size(); i != e; ++i)
649 if (!V[i]->isNullValue())
Owen Anderson45308b52009-07-27 22:29:26 +0000650 return pImpl->StructConstants.getOrCreate(T, V);
651
Owen Andersonb292b8c2009-07-30 23:03:37 +0000652 return ConstantAggregateZero::get(T);
Owen Anderson45308b52009-07-27 22:29:26 +0000653}
654
Chris Lattnera676c0f2011-02-07 16:40:21 +0000655Constant *ConstantStruct::get(LLVMContext &Context,
Owen Anderson03cb69f2009-08-05 23:16:16 +0000656 const std::vector<Constant*>& V, bool packed) {
Owen Anderson45308b52009-07-27 22:29:26 +0000657 std::vector<const Type*> StructEls;
658 StructEls.reserve(V.size());
659 for (unsigned i = 0, e = V.size(); i != e; ++i)
660 StructEls.push_back(V[i]->getType());
Owen Anderson03cb69f2009-08-05 23:16:16 +0000661 return get(StructType::get(Context, StructEls, packed), V);
Owen Anderson45308b52009-07-27 22:29:26 +0000662}
663
Chris Lattnera676c0f2011-02-07 16:40:21 +0000664Constant *ConstantStruct::get(LLVMContext &Context,
665 Constant *const *Vals, unsigned NumVals,
Owen Anderson45308b52009-07-27 22:29:26 +0000666 bool Packed) {
667 // FIXME: make this the primary ctor method.
Owen Anderson03cb69f2009-08-05 23:16:16 +0000668 return get(Context, std::vector<Constant*>(Vals, Vals+NumVals), Packed);
Owen Anderson45308b52009-07-27 22:29:26 +0000669}
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000670
Talin3a0a30d2011-02-28 23:53:27 +0000671Constant* ConstantStruct::get(LLVMContext &Context, bool Packed,
672 Constant * Val, ...) {
673 va_list ap;
674 std::vector<Constant*> Values;
675 va_start(ap, Val);
676 while (Val) {
677 Values.push_back(Val);
678 Val = va_arg(ap, llvm::Constant*);
679 }
Talinde422be2011-03-01 18:00:49 +0000680 va_end(ap);
Talin3a0a30d2011-02-28 23:53:27 +0000681 return get(Context, Values, Packed);
682}
683
Reid Spencerd84d35b2007-02-15 02:26:10 +0000684ConstantVector::ConstantVector(const VectorType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000685 const std::vector<Constant*> &V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000686 : Constant(T, ConstantVectorVal,
687 OperandTraits<ConstantVector>::op_end(this) - V.size(),
688 V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000689 Use *OL = OperandList;
Jay Foad9f32cfd2011-01-27 14:44:55 +0000690 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
691 I != E; ++I, ++OL) {
692 Constant *C = *I;
693 assert(C->getType() == T->getElementType() &&
Dan Gohman30978072007-05-24 14:36:04 +0000694 "Initializer for vector element doesn't match vector element type!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000695 *OL = C;
Brian Gaeke02209042004-08-20 06:00:58 +0000696 }
697}
698
Owen Anderson4aa32952009-07-28 21:19:26 +0000699// ConstantVector accessors.
Chris Lattner69229312011-02-15 00:14:00 +0000700Constant *ConstantVector::get(const VectorType *T,
701 const std::vector<Constant*> &V) {
Jay Foad9f32cfd2011-01-27 14:44:55 +0000702 assert(!V.empty() && "Vectors can't be empty");
Chris Lattner69229312011-02-15 00:14:00 +0000703 LLVMContextImpl *pImpl = T->getContext().pImpl;
Jay Foad9f32cfd2011-01-27 14:44:55 +0000704
Chris Lattner69229312011-02-15 00:14:00 +0000705 // If this is an all-undef or all-zero vector, return a
Owen Anderson4aa32952009-07-28 21:19:26 +0000706 // ConstantAggregateZero or UndefValue.
707 Constant *C = V[0];
708 bool isZero = C->isNullValue();
709 bool isUndef = isa<UndefValue>(C);
710
711 if (isZero || isUndef) {
712 for (unsigned i = 1, e = V.size(); i != e; ++i)
713 if (V[i] != C) {
714 isZero = isUndef = false;
715 break;
716 }
717 }
718
719 if (isZero)
Owen Andersonb292b8c2009-07-30 23:03:37 +0000720 return ConstantAggregateZero::get(T);
Owen Anderson4aa32952009-07-28 21:19:26 +0000721 if (isUndef)
Owen Andersonb292b8c2009-07-30 23:03:37 +0000722 return UndefValue::get(T);
Owen Anderson4aa32952009-07-28 21:19:26 +0000723
Owen Anderson4aa32952009-07-28 21:19:26 +0000724 return pImpl->VectorConstants.getOrCreate(T, V);
725}
726
Chris Lattner69229312011-02-15 00:14:00 +0000727Constant *ConstantVector::get(ArrayRef<Constant*> V) {
Owen Anderson4aa32952009-07-28 21:19:26 +0000728 // FIXME: make this the primary ctor method.
Chris Lattner69229312011-02-15 00:14:00 +0000729 assert(!V.empty() && "Vectors cannot be empty");
730 return get(VectorType::get(V.front()->getType(), V.size()), V.vec());
Owen Anderson4aa32952009-07-28 21:19:26 +0000731}
732
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000733// Utility function for determining if a ConstantExpr is a CastOp or not. This
734// can't be inline because we don't want to #include Instruction.h into
735// Constant.h
736bool ConstantExpr::isCast() const {
737 return Instruction::isCast(getOpcode());
738}
739
Reid Spenceree3c9912006-12-04 05:19:50 +0000740bool ConstantExpr::isCompare() const {
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000741 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
Reid Spenceree3c9912006-12-04 05:19:50 +0000742}
743
Dan Gohman7190d482009-09-10 23:37:55 +0000744bool ConstantExpr::isGEPWithNoNotionalOverIndexing() const {
745 if (getOpcode() != Instruction::GetElementPtr) return false;
746
747 gep_type_iterator GEPI = gep_type_begin(this), E = gep_type_end(this);
Oscar Fuentes40b31ad2010-08-02 06:00:15 +0000748 User::const_op_iterator OI = llvm::next(this->op_begin());
Dan Gohman7190d482009-09-10 23:37:55 +0000749
750 // Skip the first index, as it has no static limit.
751 ++GEPI;
752 ++OI;
753
754 // The remaining indices must be compile-time known integers within the
755 // bounds of the corresponding notional static array types.
756 for (; GEPI != E; ++GEPI, ++OI) {
757 ConstantInt *CI = dyn_cast<ConstantInt>(*OI);
758 if (!CI) return false;
759 if (const ArrayType *ATy = dyn_cast<ArrayType>(*GEPI))
760 if (CI->getValue().getActiveBits() > 64 ||
761 CI->getZExtValue() >= ATy->getNumElements())
762 return false;
763 }
764
765 // All the indices checked out.
766 return true;
767}
768
Dan Gohman1ecaf452008-05-31 00:58:22 +0000769bool ConstantExpr::hasIndices() const {
770 return getOpcode() == Instruction::ExtractValue ||
771 getOpcode() == Instruction::InsertValue;
772}
773
774const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const {
775 if (const ExtractValueConstantExpr *EVCE =
776 dyn_cast<ExtractValueConstantExpr>(this))
777 return EVCE->Indices;
Dan Gohmana469bdb2008-06-23 16:39:44 +0000778
779 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman1ecaf452008-05-31 00:58:22 +0000780}
781
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000782unsigned ConstantExpr::getPredicate() const {
Nate Begemand2195702008-05-12 19:01:56 +0000783 assert(getOpcode() == Instruction::FCmp ||
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000784 getOpcode() == Instruction::ICmp);
Chris Lattneref650092007-10-18 16:26:24 +0000785 return ((const CompareConstantExpr*)this)->predicate;
Reid Spencer10fbf0e2006-12-03 05:48:19 +0000786}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000787
Chris Lattner7c1018a2006-07-14 19:37:40 +0000788/// getWithOperandReplaced - Return a constant expression identical to this
789/// one, but with the specified operand set to the specified value.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000790Constant *
791ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +0000792 assert(OpNo < getNumOperands() && "Operand num is out of range!");
793 assert(Op->getType() == getOperand(OpNo)->getType() &&
794 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000795 if (getOperand(OpNo) == Op)
796 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000797
Chris Lattner227816342006-07-14 22:20:01 +0000798 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000799 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000800 case Instruction::Trunc:
801 case Instruction::ZExt:
802 case Instruction::SExt:
803 case Instruction::FPTrunc:
804 case Instruction::FPExt:
805 case Instruction::UIToFP:
806 case Instruction::SIToFP:
807 case Instruction::FPToUI:
808 case Instruction::FPToSI:
809 case Instruction::PtrToInt:
810 case Instruction::IntToPtr:
811 case Instruction::BitCast:
812 return ConstantExpr::getCast(getOpcode(), Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000813 case Instruction::Select:
814 Op0 = (OpNo == 0) ? Op : getOperand(0);
815 Op1 = (OpNo == 1) ? Op : getOperand(1);
816 Op2 = (OpNo == 2) ? Op : getOperand(2);
817 return ConstantExpr::getSelect(Op0, Op1, Op2);
818 case Instruction::InsertElement:
819 Op0 = (OpNo == 0) ? Op : getOperand(0);
820 Op1 = (OpNo == 1) ? Op : getOperand(1);
821 Op2 = (OpNo == 2) ? Op : getOperand(2);
822 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
823 case Instruction::ExtractElement:
824 Op0 = (OpNo == 0) ? Op : getOperand(0);
825 Op1 = (OpNo == 1) ? Op : getOperand(1);
826 return ConstantExpr::getExtractElement(Op0, Op1);
827 case Instruction::ShuffleVector:
828 Op0 = (OpNo == 0) ? Op : getOperand(0);
829 Op1 = (OpNo == 1) ? Op : getOperand(1);
830 Op2 = (OpNo == 2) ? Op : getOperand(2);
831 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000832 case Instruction::GetElementPtr: {
Chris Lattnerb5d70302007-02-19 20:01:23 +0000833 SmallVector<Constant*, 8> Ops;
Dan Gohman12fce772008-05-15 19:50:34 +0000834 Ops.resize(getNumOperands()-1);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000835 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
Dan Gohman12fce772008-05-15 19:50:34 +0000836 Ops[i-1] = getOperand(i);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000837 if (OpNo == 0)
Dan Gohman1b849082009-09-07 23:54:19 +0000838 return cast<GEPOperator>(this)->isInBounds() ?
839 ConstantExpr::getInBoundsGetElementPtr(Op, &Ops[0], Ops.size()) :
840 ConstantExpr::getGetElementPtr(Op, &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000841 Ops[OpNo-1] = Op;
Dan Gohman1b849082009-09-07 23:54:19 +0000842 return cast<GEPOperator>(this)->isInBounds() ?
Chris Lattnerb9c86512009-12-29 02:14:09 +0000843 ConstantExpr::getInBoundsGetElementPtr(getOperand(0), &Ops[0],Ops.size()):
Dan Gohman1b849082009-09-07 23:54:19 +0000844 ConstantExpr::getGetElementPtr(getOperand(0), &Ops[0], Ops.size());
Chris Lattner7c1018a2006-07-14 19:37:40 +0000845 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000846 default:
847 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000848 Op0 = (OpNo == 0) ? Op : getOperand(0);
849 Op1 = (OpNo == 1) ? Op : getOperand(1);
Chris Lattnerb9c86512009-12-29 02:14:09 +0000850 return ConstantExpr::get(getOpcode(), Op0, Op1, SubclassOptionalData);
Chris Lattner227816342006-07-14 22:20:01 +0000851 }
852}
853
854/// getWithOperands - This returns the current constant expression with the
855/// operands replaced with the specified values. The specified operands must
856/// match count and type with the existing ones.
857Constant *ConstantExpr::
Jay Foad47f89e02011-04-13 14:39:42 +0000858getWithOperands(ArrayRef<Constant*> Ops) const {
Jay Foad5c984e562011-04-13 13:46:01 +0000859 assert(Ops.size() == getNumOperands() && "Operand count mismatch!");
Chris Lattner227816342006-07-14 22:20:01 +0000860 bool AnyChange = false;
Jay Foad5c984e562011-04-13 13:46:01 +0000861 for (unsigned i = 0; i != Ops.size(); ++i) {
Chris Lattner227816342006-07-14 22:20:01 +0000862 assert(Ops[i]->getType() == getOperand(i)->getType() &&
863 "Operand type mismatch!");
864 AnyChange |= Ops[i] != getOperand(i);
865 }
866 if (!AnyChange) // No operands changed, return self.
867 return const_cast<ConstantExpr*>(this);
868
869 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000870 case Instruction::Trunc:
871 case Instruction::ZExt:
872 case Instruction::SExt:
873 case Instruction::FPTrunc:
874 case Instruction::FPExt:
875 case Instruction::UIToFP:
876 case Instruction::SIToFP:
877 case Instruction::FPToUI:
878 case Instruction::FPToSI:
879 case Instruction::PtrToInt:
880 case Instruction::IntToPtr:
881 case Instruction::BitCast:
882 return ConstantExpr::getCast(getOpcode(), Ops[0], getType());
Chris Lattner227816342006-07-14 22:20:01 +0000883 case Instruction::Select:
884 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
885 case Instruction::InsertElement:
886 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
887 case Instruction::ExtractElement:
888 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
889 case Instruction::ShuffleVector:
890 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Chris Lattnerb5d70302007-02-19 20:01:23 +0000891 case Instruction::GetElementPtr:
Dan Gohman1b849082009-09-07 23:54:19 +0000892 return cast<GEPOperator>(this)->isInBounds() ?
Jay Foad5c984e562011-04-13 13:46:01 +0000893 ConstantExpr::getInBoundsGetElementPtr(Ops[0], &Ops[1], Ops.size()-1) :
894 ConstantExpr::getGetElementPtr(Ops[0], &Ops[1], Ops.size()-1);
Reid Spencer266e42b2006-12-23 06:05:41 +0000895 case Instruction::ICmp:
896 case Instruction::FCmp:
897 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
Chris Lattner227816342006-07-14 22:20:01 +0000898 default:
899 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattnerb9c86512009-12-29 02:14:09 +0000900 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1], SubclassOptionalData);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000901 }
902}
903
Chris Lattner2f7c9632001-06-06 20:29:01 +0000904
905//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000906// isValueValidForType implementations
907
Reid Spencere7334722006-12-19 01:28:19 +0000908bool ConstantInt::isValueValidForType(const Type *Ty, uint64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000909 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Owen Anderson55f1c092009-08-13 21:58:54 +0000910 if (Ty == Type::getInt1Ty(Ty->getContext()))
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000911 return Val == 0 || Val == 1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000912 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000913 return true; // always true, has to fit in largest type
914 uint64_t Max = (1ll << NumBits) - 1;
915 return Val <= Max;
Reid Spencere7334722006-12-19 01:28:19 +0000916}
917
Reid Spencere0fc4df2006-10-20 07:07:24 +0000918bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000919 unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
Owen Anderson55f1c092009-08-13 21:58:54 +0000920 if (Ty == Type::getInt1Ty(Ty->getContext()))
Reid Spencera94d3942007-01-19 21:13:56 +0000921 return Val == 0 || Val == 1 || Val == -1;
Reid Spencerd7a00d72007-02-05 23:47:56 +0000922 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000923 return true; // always true, has to fit in largest type
924 int64_t Min = -(1ll << (NumBits-1));
925 int64_t Max = (1ll << (NumBits-1)) - 1;
926 return (Val >= Min && Val <= Max);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000927}
928
Dale Johannesend246b2c2007-08-30 00:23:21 +0000929bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) {
930 // convert modifies in place, so make a copy.
931 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000932 bool losesInfo;
Chris Lattner6b727592004-06-17 18:19:28 +0000933 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000934 default:
935 return false; // These can't be represented as floating point!
936
Dale Johannesend246b2c2007-08-30 00:23:21 +0000937 // FIXME rounding mode needs to be more flexible
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000938 case Type::FloatTyID: {
939 if (&Val2.getSemantics() == &APFloat::IEEEsingle)
940 return true;
941 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
942 return !losesInfo;
943 }
944 case Type::DoubleTyID: {
945 if (&Val2.getSemantics() == &APFloat::IEEEsingle ||
946 &Val2.getSemantics() == &APFloat::IEEEdouble)
947 return true;
948 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
949 return !losesInfo;
950 }
Dale Johannesenbdad8092007-08-09 22:51:36 +0000951 case Type::X86_FP80TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +0000952 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
953 &Val2.getSemantics() == &APFloat::IEEEdouble ||
954 &Val2.getSemantics() == &APFloat::x87DoubleExtended;
Dale Johannesenbdad8092007-08-09 22:51:36 +0000955 case Type::FP128TyID:
Dale Johannesen028084e2007-09-12 03:30:33 +0000956 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
957 &Val2.getSemantics() == &APFloat::IEEEdouble ||
958 &Val2.getSemantics() == &APFloat::IEEEquad;
Dale Johannesen007aa372007-10-11 18:07:22 +0000959 case Type::PPC_FP128TyID:
960 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
961 &Val2.getSemantics() == &APFloat::IEEEdouble ||
962 &Val2.getSemantics() == &APFloat::PPCDoubleDouble;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000963 }
Chris Lattneraa2372562006-05-24 17:04:05 +0000964}
Chris Lattner9655e542001-07-20 19:16:02 +0000965
Chris Lattner49d855c2001-09-07 16:46:31 +0000966//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000967// Factory Function Implementation
968
Owen Andersonb292b8c2009-07-30 23:03:37 +0000969ConstantAggregateZero* ConstantAggregateZero::get(const Type* Ty) {
Chris Lattner13ee7952010-08-28 04:09:24 +0000970 assert((Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy()) &&
Owen Andersonb292b8c2009-07-30 23:03:37 +0000971 "Cannot create an aggregate zero of non-aggregate type!");
972
973 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
Owen Andersonb292b8c2009-07-30 23:03:37 +0000974 return pImpl->AggZeroConstants.getOrCreate(Ty, 0);
975}
976
Dan Gohman92b551b2009-03-03 02:55:14 +0000977/// destroyConstant - Remove the constant from the constant table...
978///
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000979void ConstantAggregateZero::destroyConstant() {
Chris Lattner718da702010-07-17 06:13:52 +0000980 getRawType()->getContext().pImpl->AggZeroConstants.remove(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000981 destroyConstantImpl();
982}
983
Dan Gohman92b551b2009-03-03 02:55:14 +0000984/// destroyConstant - Remove the constant from the constant table...
985///
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000986void ConstantArray::destroyConstant() {
Chris Lattner718da702010-07-17 06:13:52 +0000987 getRawType()->getContext().pImpl->ArrayConstants.remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000988 destroyConstantImpl();
989}
990
Reid Spencer2546b762007-01-26 07:37:34 +0000991/// isString - This method returns true if the array is an array of i8, and
992/// if the elements of the array are all ConstantInt's.
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000993bool ConstantArray::isString() const {
Reid Spencer2546b762007-01-26 07:37:34 +0000994 // Check the element type for i8...
Duncan Sands9dff9be2010-02-15 16:12:20 +0000995 if (!getType()->getElementType()->isIntegerTy(8))
Chris Lattnere8dfcca2004-01-14 17:06:38 +0000996 return false;
997 // Check the elements to make sure they are all integers, not constant
998 // expressions.
999 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1000 if (!isa<ConstantInt>(getOperand(i)))
1001 return false;
1002 return true;
1003}
1004
Evan Cheng3763c5b2006-10-26 19:15:05 +00001005/// isCString - This method returns true if the array is a string (see
Dan Gohman92b551b2009-03-03 02:55:14 +00001006/// isString) and it ends in a null byte \\0 and does not contains any other
Evan Cheng3763c5b2006-10-26 19:15:05 +00001007/// null bytes except its terminator.
Owen Andersone4dcecd2009-07-13 21:27:19 +00001008bool ConstantArray::isCString() const {
Reid Spencer2546b762007-01-26 07:37:34 +00001009 // Check the element type for i8...
Duncan Sands9dff9be2010-02-15 16:12:20 +00001010 if (!getType()->getElementType()->isIntegerTy(8))
Evan Chenge974da62006-10-26 21:48:03 +00001011 return false;
Owen Andersone4dcecd2009-07-13 21:27:19 +00001012
Evan Chenge974da62006-10-26 21:48:03 +00001013 // Last element must be a null.
Owen Andersone4dcecd2009-07-13 21:27:19 +00001014 if (!getOperand(getNumOperands()-1)->isNullValue())
Evan Chenge974da62006-10-26 21:48:03 +00001015 return false;
1016 // Other elements must be non-null integers.
1017 for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1018 if (!isa<ConstantInt>(getOperand(i)))
Evan Cheng3763c5b2006-10-26 19:15:05 +00001019 return false;
Owen Andersone4dcecd2009-07-13 21:27:19 +00001020 if (getOperand(i)->isNullValue())
Evan Chenge974da62006-10-26 21:48:03 +00001021 return false;
1022 }
Evan Cheng3763c5b2006-10-26 19:15:05 +00001023 return true;
1024}
1025
1026
Dan Gohman92b551b2009-03-03 02:55:14 +00001027/// getAsString - If the sub-element type of this array is i8
1028/// then this method converts the array to an std::string and returns it.
1029/// Otherwise, it asserts out.
1030///
Chris Lattner81fabb02002-08-26 17:53:56 +00001031std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001032 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001033 std::string Result;
Owen Anderson79c69bc2008-06-24 21:58:29 +00001034 Result.reserve(getNumOperands());
Chris Lattner6077c312003-07-23 15:22:26 +00001035 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonee9c30d2008-06-25 01:05:05 +00001036 Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue());
Chris Lattner81fabb02002-08-26 17:53:56 +00001037 return Result;
1038}
1039
1040
Chris Lattner3462ae32001-12-03 22:26:30 +00001041//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001042//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001043
Chris Lattner189d19f2003-11-21 20:23:48 +00001044namespace llvm {
Misha Brukmanb1c93172005-04-21 23:48:37 +00001045
Chris Lattner49d855c2001-09-07 16:46:31 +00001046}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001047
Chris Lattnerd7a73302001-10-13 06:57:33 +00001048// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001049//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001050void ConstantStruct::destroyConstant() {
Chris Lattner718da702010-07-17 06:13:52 +00001051 getRawType()->getContext().pImpl->StructConstants.remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001052 destroyConstantImpl();
1053}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001054
Brian Gaeke02209042004-08-20 06:00:58 +00001055// destroyConstant - Remove the constant from the constant table...
1056//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001057void ConstantVector::destroyConstant() {
Chris Lattner718da702010-07-17 06:13:52 +00001058 getRawType()->getContext().pImpl->VectorConstants.remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001059 destroyConstantImpl();
1060}
1061
Dan Gohman30978072007-05-24 14:36:04 +00001062/// This function will return true iff every element in this vector constant
Jim Laskeyf0478822007-01-12 22:39:14 +00001063/// is set to all ones.
1064/// @returns true iff this constant's emements are all set to all ones.
1065/// @brief Determine if the value is all ones.
Reid Spencerd84d35b2007-02-15 02:26:10 +00001066bool ConstantVector::isAllOnesValue() const {
Jim Laskeyf0478822007-01-12 22:39:14 +00001067 // Check out first element.
1068 const Constant *Elt = getOperand(0);
1069 const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
1070 if (!CI || !CI->isAllOnesValue()) return false;
1071 // Then make sure all remaining elements point to the same value.
1072 for (unsigned I = 1, E = getNumOperands(); I < E; ++I) {
1073 if (getOperand(I) != Elt) return false;
1074 }
1075 return true;
1076}
1077
Dan Gohman07159202007-10-17 17:51:30 +00001078/// getSplatValue - If this is a splat constant, where all of the
1079/// elements have the same value, return that value. Otherwise return null.
Duncan Sandscf0ff032011-02-01 08:39:12 +00001080Constant *ConstantVector::getSplatValue() const {
Dan Gohman07159202007-10-17 17:51:30 +00001081 // Check out first element.
1082 Constant *Elt = getOperand(0);
1083 // Then make sure all remaining elements point to the same value.
1084 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
1085 if (getOperand(I) != Elt) return 0;
1086 return Elt;
1087}
1088
Chris Lattner31b132c2009-10-28 00:01:44 +00001089//---- ConstantPointerNull::get() implementation.
Chris Lattnerd7a73302001-10-13 06:57:33 +00001090//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001091
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001092ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Owen Andersonc8c30262009-07-31 22:45:43 +00001093 return Ty->getContext().pImpl->NullPtrConstants.getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001094}
1095
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001096// destroyConstant - Remove the constant from the constant table...
1097//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001098void ConstantPointerNull::destroyConstant() {
Chris Lattner718da702010-07-17 06:13:52 +00001099 getRawType()->getContext().pImpl->NullPtrConstants.remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001100 destroyConstantImpl();
1101}
1102
1103
Chris Lattner31b132c2009-10-28 00:01:44 +00001104//---- UndefValue::get() implementation.
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001105//
1106
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001107UndefValue *UndefValue::get(const Type *Ty) {
Owen Andersonc8c30262009-07-31 22:45:43 +00001108 return Ty->getContext().pImpl->UndefValueConstants.getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001109}
1110
1111// destroyConstant - Remove the constant from the constant table.
1112//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001113void UndefValue::destroyConstant() {
Chris Lattner718da702010-07-17 06:13:52 +00001114 getRawType()->getContext().pImpl->UndefValueConstants.remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001115 destroyConstantImpl();
1116}
1117
Chris Lattner31b132c2009-10-28 00:01:44 +00001118//---- BlockAddress::get() implementation.
1119//
1120
1121BlockAddress *BlockAddress::get(BasicBlock *BB) {
1122 assert(BB->getParent() != 0 && "Block must have a parent");
1123 return get(BB->getParent(), BB);
1124}
1125
1126BlockAddress *BlockAddress::get(Function *F, BasicBlock *BB) {
1127 BlockAddress *&BA =
1128 F->getContext().pImpl->BlockAddresses[std::make_pair(F, BB)];
1129 if (BA == 0)
1130 BA = new BlockAddress(F, BB);
1131
1132 assert(BA->getFunction() == F && "Basic block moved between functions");
1133 return BA;
1134}
1135
1136BlockAddress::BlockAddress(Function *F, BasicBlock *BB)
1137: Constant(Type::getInt8PtrTy(F->getContext()), Value::BlockAddressVal,
1138 &Op<0>(), 2) {
Chris Lattnerc559a9f2009-11-01 03:03:03 +00001139 setOperand(0, F);
1140 setOperand(1, BB);
Chris Lattneraa99c942009-11-01 01:27:45 +00001141 BB->AdjustBlockAddressRefCount(1);
Chris Lattner31b132c2009-10-28 00:01:44 +00001142}
1143
1144
1145// destroyConstant - Remove the constant from the constant table.
1146//
1147void BlockAddress::destroyConstant() {
Chris Lattner718da702010-07-17 06:13:52 +00001148 getFunction()->getRawType()->getContext().pImpl
Chris Lattner31b132c2009-10-28 00:01:44 +00001149 ->BlockAddresses.erase(std::make_pair(getFunction(), getBasicBlock()));
Chris Lattneraa99c942009-11-01 01:27:45 +00001150 getBasicBlock()->AdjustBlockAddressRefCount(-1);
Chris Lattner31b132c2009-10-28 00:01:44 +00001151 destroyConstantImpl();
1152}
1153
1154void BlockAddress::replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
1155 // This could be replacing either the Basic Block or the Function. In either
1156 // case, we have to remove the map entry.
1157 Function *NewF = getFunction();
1158 BasicBlock *NewBB = getBasicBlock();
1159
1160 if (U == &Op<0>())
1161 NewF = cast<Function>(To);
1162 else
1163 NewBB = cast<BasicBlock>(To);
1164
1165 // See if the 'new' entry already exists, if not, just update this in place
1166 // and return early.
1167 BlockAddress *&NewBA =
1168 getContext().pImpl->BlockAddresses[std::make_pair(NewF, NewBB)];
1169 if (NewBA == 0) {
Chris Lattnerc559a9f2009-11-01 03:03:03 +00001170 getBasicBlock()->AdjustBlockAddressRefCount(-1);
1171
Chris Lattner31b132c2009-10-28 00:01:44 +00001172 // Remove the old entry, this can't cause the map to rehash (just a
1173 // tombstone will get added).
1174 getContext().pImpl->BlockAddresses.erase(std::make_pair(getFunction(),
1175 getBasicBlock()));
1176 NewBA = this;
Chris Lattnerc559a9f2009-11-01 03:03:03 +00001177 setOperand(0, NewF);
1178 setOperand(1, NewBB);
1179 getBasicBlock()->AdjustBlockAddressRefCount(1);
Chris Lattner31b132c2009-10-28 00:01:44 +00001180 return;
1181 }
1182
1183 // Otherwise, I do need to replace this with an existing value.
1184 assert(NewBA != this && "I didn't contain From!");
1185
1186 // Everyone using this now uses the replacement.
1187 uncheckedReplaceAllUsesWith(NewBA);
1188
1189 destroyConstant();
1190}
1191
1192//---- ConstantExpr::get() implementations.
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001193//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001194
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001195/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands7d6c8ae2008-03-30 19:38:55 +00001196/// cast in the ExprConstants map. It is used by the various get* methods below.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001197static inline Constant *getFoldedCast(
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001198 Instruction::CastOps opc, Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001199 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001200 // Fold a few common cases
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001201 if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001202 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001203
Owen Anderson1584a292009-08-04 20:25:11 +00001204 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
1205
Vikram S. Adve4c485332002-07-15 18:19:33 +00001206 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001207 std::vector<Constant*> argVec(1, C);
Reid Spenceree3c9912006-12-04 05:19:50 +00001208 ExprMapKeyType Key(opc, argVec);
Owen Anderson2d7231d2009-06-17 18:40:29 +00001209
Owen Anderson1584a292009-08-04 20:25:11 +00001210 return pImpl->ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001211}
Reid Spencerf37dc652006-12-05 19:14:13 +00001212
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001213Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001214 Instruction::CastOps opc = Instruction::CastOps(oc);
1215 assert(Instruction::isCast(opc) && "opcode out of range");
1216 assert(C && Ty && "Null arguments to getCast");
Chris Lattner37bc78a2010-01-26 21:51:43 +00001217 assert(CastInst::castIsValid(opc, C, Ty) && "Invalid constantexpr cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001218
1219 switch (opc) {
Chris Lattner37bc78a2010-01-26 21:51:43 +00001220 default:
1221 llvm_unreachable("Invalid cast opcode");
1222 break;
1223 case Instruction::Trunc: return getTrunc(C, Ty);
1224 case Instruction::ZExt: return getZExt(C, Ty);
1225 case Instruction::SExt: return getSExt(C, Ty);
1226 case Instruction::FPTrunc: return getFPTrunc(C, Ty);
1227 case Instruction::FPExt: return getFPExtend(C, Ty);
1228 case Instruction::UIToFP: return getUIToFP(C, Ty);
1229 case Instruction::SIToFP: return getSIToFP(C, Ty);
1230 case Instruction::FPToUI: return getFPToUI(C, Ty);
1231 case Instruction::FPToSI: return getFPToSI(C, Ty);
1232 case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1233 case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1234 case Instruction::BitCast: return getBitCast(C, Ty);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001235 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001236 return 0;
Reid Spencerf37dc652006-12-05 19:14:13 +00001237}
1238
Reid Spencer5c140882006-12-04 20:17:56 +00001239Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001240 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001241 return getBitCast(C, Ty);
1242 return getZExt(C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001243}
1244
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001245Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001246 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001247 return getBitCast(C, Ty);
1248 return getSExt(C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001249}
1250
1251Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001252 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001253 return getBitCast(C, Ty);
1254 return getTrunc(C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001255}
1256
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001257Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001258 assert(S->getType()->isPointerTy() && "Invalid cast");
1259 assert((Ty->isIntegerTy() || Ty->isPointerTy()) && "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001260
Duncan Sands9dff9be2010-02-15 16:12:20 +00001261 if (Ty->isIntegerTy())
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001262 return getPtrToInt(S, Ty);
1263 return getBitCast(S, Ty);
Reid Spencerbc245a02006-12-05 03:25:26 +00001264}
1265
Reid Spencer56521c42006-12-12 00:51:07 +00001266Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
1267 bool isSigned) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001268 assert(C->getType()->isIntOrIntVectorTy() &&
1269 Ty->isIntOrIntVectorTy() && "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001270 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1271 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer56521c42006-12-12 00:51:07 +00001272 Instruction::CastOps opcode =
1273 (SrcBits == DstBits ? Instruction::BitCast :
1274 (SrcBits > DstBits ? Instruction::Trunc :
1275 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1276 return getCast(opcode, C, Ty);
1277}
1278
1279Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001280 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer56521c42006-12-12 00:51:07 +00001281 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001282 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1283 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001284 if (SrcBits == DstBits)
1285 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001286 Instruction::CastOps opcode =
Jay Foad9f32cfd2011-01-27 14:44:55 +00001287 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001288 return getCast(opcode, C, Ty);
1289}
1290
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001291Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001292#ifndef NDEBUG
1293 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1294 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1295#endif
1296 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001297 assert(C->getType()->isIntOrIntVectorTy() && "Trunc operand must be integer");
1298 assert(Ty->isIntOrIntVectorTy() && "Trunc produces only integral");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001299 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001300 "SrcTy must be larger than DestTy for Trunc!");
1301
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001302 return getFoldedCast(Instruction::Trunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001303}
1304
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001305Constant *ConstantExpr::getSExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001306#ifndef NDEBUG
1307 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1308 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1309#endif
1310 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001311 assert(C->getType()->isIntOrIntVectorTy() && "SExt operand must be integral");
1312 assert(Ty->isIntOrIntVectorTy() && "SExt produces only integer");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001313 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001314 "SrcTy must be smaller than DestTy for SExt!");
1315
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001316 return getFoldedCast(Instruction::SExt, C, Ty);
Chris Lattnerdd284742004-04-04 23:20:30 +00001317}
1318
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001319Constant *ConstantExpr::getZExt(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001320#ifndef NDEBUG
1321 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1322 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1323#endif
1324 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001325 assert(C->getType()->isIntOrIntVectorTy() && "ZEXt operand must be integral");
1326 assert(Ty->isIntOrIntVectorTy() && "ZExt produces only integer");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001327 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001328 "SrcTy must be smaller than DestTy for ZExt!");
1329
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001330 return getFoldedCast(Instruction::ZExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001331}
1332
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001333Constant *ConstantExpr::getFPTrunc(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001334#ifndef NDEBUG
1335 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1336 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1337#endif
1338 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001339 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001340 C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001341 "This is an illegal floating point truncation!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001342 return getFoldedCast(Instruction::FPTrunc, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001343}
1344
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001345Constant *ConstantExpr::getFPExtend(Constant *C, const Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001346#ifndef NDEBUG
1347 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1348 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1349#endif
1350 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001351 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001352 C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001353 "This is an illegal floating point extension!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001354 return getFoldedCast(Instruction::FPExt, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001355}
1356
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001357Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001358#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001359 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1360 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001361#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001362 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001363 assert(C->getType()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00001364 "This is an illegal uint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001365 return getFoldedCast(Instruction::UIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001366}
1367
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001368Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001369#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001370 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1371 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001372#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001373 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001374 assert(C->getType()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001375 "This is an illegal sint to floating point cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001376 return getFoldedCast(Instruction::SIToFP, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001377}
1378
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001379Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001380#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001381 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1382 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001383#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001384 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001385 assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00001386 "This is an illegal floating point to uint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001387 return getFoldedCast(Instruction::FPToUI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001388}
1389
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001390Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
Devang Pateld26344d2008-11-03 23:20:04 +00001391#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001392 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1393 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001394#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001395 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001396 assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00001397 "This is an illegal floating point to sint cast!");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001398 return getFoldedCast(Instruction::FPToSI, C, Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001399}
1400
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001401Constant *ConstantExpr::getPtrToInt(Constant *C, const Type *DstTy) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001402 assert(C->getType()->isPointerTy() && "PtrToInt source must be pointer");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001403 assert(DstTy->isIntegerTy() && "PtrToInt destination must be integral");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001404 return getFoldedCast(Instruction::PtrToInt, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001405}
1406
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001407Constant *ConstantExpr::getIntToPtr(Constant *C, const Type *DstTy) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001408 assert(C->getType()->isIntegerTy() && "IntToPtr source must be integral");
Duncan Sands19d0b472010-02-16 11:11:14 +00001409 assert(DstTy->isPointerTy() && "IntToPtr destination must be a pointer");
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001410 return getFoldedCast(Instruction::IntToPtr, C, DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001411}
1412
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001413Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
Chris Lattner37bc78a2010-01-26 21:51:43 +00001414 assert(CastInst::castIsValid(Instruction::BitCast, C, DstTy) &&
1415 "Invalid constantexpr bitcast!");
Chris Lattnercbeda872009-03-21 06:55:54 +00001416
1417 // It is common to ask for a bitcast of a value to its own type, handle this
1418 // speedily.
1419 if (C->getType() == DstTy) return C;
1420
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001421 return getFoldedCast(Instruction::BitCast, C, DstTy);
Chris Lattnerdd284742004-04-04 23:20:30 +00001422}
1423
Chris Lattnerb50d1352003-10-05 00:17:43 +00001424Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Dan Gohman1b849082009-09-07 23:54:19 +00001425 Constant *C1, Constant *C2,
1426 unsigned Flags) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001427 // Check the operands for consistency first
Reid Spencer7eb55b32006-11-02 01:53:59 +00001428 assert(Opcode >= Instruction::BinaryOpsBegin &&
1429 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001430 "Invalid opcode in binary constant expression");
1431 assert(C1->getType() == C2->getType() &&
1432 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001433
Owen Anderson55f1c092009-08-13 21:58:54 +00001434 if (ReqTy == C1->getType() || ReqTy == Type::getInt1Ty(ReqTy->getContext()))
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001435 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattnerb50d1352003-10-05 00:17:43 +00001436 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001437
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001438 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Dan Gohman1b849082009-09-07 23:54:19 +00001439 ExprMapKeyType Key(Opcode, argVec, 0, Flags);
Owen Anderson61794042009-06-17 20:10:08 +00001440
Owen Anderson1584a292009-08-04 20:25:11 +00001441 LLVMContextImpl *pImpl = ReqTy->getContext().pImpl;
Owen Anderson1584a292009-08-04 20:25:11 +00001442 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001443}
1444
Reid Spencer266e42b2006-12-23 06:05:41 +00001445Constant *ConstantExpr::getCompareTy(unsigned short predicate,
Nate Begeman098cc6f2008-07-25 17:56:27 +00001446 Constant *C1, Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001447 switch (predicate) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001448 default: llvm_unreachable("Invalid CmpInst predicate");
Nate Begemanc96e2e42008-07-25 17:35:37 +00001449 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
1450 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
1451 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
1452 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
1453 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
1454 case CmpInst::FCMP_TRUE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001455 return getFCmp(predicate, C1, C2);
1456
Nate Begemanc96e2e42008-07-25 17:35:37 +00001457 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
1458 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
1459 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
1460 case CmpInst::ICMP_SLE:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001461 return getICmp(predicate, C1, C2);
Reid Spencer266e42b2006-12-23 06:05:41 +00001462 }
Reid Spencera009d0d2006-12-04 21:35:24 +00001463}
1464
Dan Gohman1b849082009-09-07 23:54:19 +00001465Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2,
1466 unsigned Flags) {
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001467#ifndef NDEBUG
1468 switch (Opcode) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001469 case Instruction::Add:
Reid Spencer7eb55b32006-11-02 01:53:59 +00001470 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001471 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001472 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001473 assert(C1->getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001474 "Tried to create an integer operation on a non-integer type!");
1475 break;
1476 case Instruction::FAdd:
1477 case Instruction::FSub:
1478 case Instruction::FMul:
1479 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001480 assert(C1->getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001481 "Tried to create a floating-point operation on a "
1482 "non-floating-point type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001483 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001484 case Instruction::UDiv:
1485 case Instruction::SDiv:
1486 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001487 assert(C1->getType()->isIntOrIntVectorTy() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001488 "Tried to create an arithmetic operation on a non-arithmetic type!");
1489 break;
1490 case Instruction::FDiv:
1491 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001492 assert(C1->getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001493 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001494 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001495 case Instruction::URem:
1496 case Instruction::SRem:
1497 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001498 assert(C1->getType()->isIntOrIntVectorTy() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001499 "Tried to create an arithmetic operation on a non-arithmetic type!");
1500 break;
1501 case Instruction::FRem:
1502 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001503 assert(C1->getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001504 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001505 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001506 case Instruction::And:
1507 case Instruction::Or:
1508 case Instruction::Xor:
1509 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001510 assert(C1->getType()->isIntOrIntVectorTy() &&
Misha Brukman3852f652005-01-27 06:46:38 +00001511 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001512 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001513 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001514 case Instruction::LShr:
1515 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00001516 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001517 assert(C1->getType()->isIntOrIntVectorTy() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001518 "Tried to create a shift operation on a non-integer type!");
1519 break;
1520 default:
1521 break;
1522 }
1523#endif
1524
Dan Gohman1b849082009-09-07 23:54:19 +00001525 return getTy(C1->getType(), Opcode, C1, C2, Flags);
Reid Spencera009d0d2006-12-04 21:35:24 +00001526}
1527
Chris Lattnera676c0f2011-02-07 16:40:21 +00001528Constant *ConstantExpr::getSizeOf(const Type* Ty) {
Owen Anderson487375e2009-07-29 18:55:55 +00001529 // sizeof is implemented as: (i64) gep (Ty*)null, 1
1530 // Note that a non-inbounds gep is used, as null isn't within any object.
Owen Anderson55f1c092009-08-13 21:58:54 +00001531 Constant *GEPIdx = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
Owen Anderson487375e2009-07-29 18:55:55 +00001532 Constant *GEP = getGetElementPtr(
Owen Anderson5a1acd92009-07-31 20:28:14 +00001533 Constant::getNullValue(PointerType::getUnqual(Ty)), &GEPIdx, 1);
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001534 return getPtrToInt(GEP,
1535 Type::getInt64Ty(Ty->getContext()));
Owen Anderson487375e2009-07-29 18:55:55 +00001536}
1537
Chris Lattnera676c0f2011-02-07 16:40:21 +00001538Constant *ConstantExpr::getAlignOf(const Type* Ty) {
Dan Gohmancf913832010-01-28 02:15:55 +00001539 // alignof is implemented as: (i64) gep ({i1,Ty}*)null, 0, 1
Dan Gohman50c09d02009-08-11 17:57:01 +00001540 // Note that a non-inbounds gep is used, as null isn't within any object.
Owen Anderson03cb69f2009-08-05 23:16:16 +00001541 const Type *AligningTy = StructType::get(Ty->getContext(),
Dan Gohmancf913832010-01-28 02:15:55 +00001542 Type::getInt1Ty(Ty->getContext()), Ty, NULL);
Owen Anderson5a1acd92009-07-31 20:28:14 +00001543 Constant *NullPtr = Constant::getNullValue(AligningTy->getPointerTo());
Dan Gohmana9be7392010-01-28 02:43:22 +00001544 Constant *Zero = ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0);
Owen Anderson55f1c092009-08-13 21:58:54 +00001545 Constant *One = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
Owen Anderson487375e2009-07-29 18:55:55 +00001546 Constant *Indices[2] = { Zero, One };
1547 Constant *GEP = getGetElementPtr(NullPtr, Indices, 2);
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001548 return getPtrToInt(GEP,
1549 Type::getInt64Ty(Ty->getContext()));
Owen Anderson487375e2009-07-29 18:55:55 +00001550}
1551
Chris Lattnera676c0f2011-02-07 16:40:21 +00001552Constant *ConstantExpr::getOffsetOf(const StructType* STy, unsigned FieldNo) {
Dan Gohmanede94e62010-02-01 16:37:38 +00001553 return getOffsetOf(STy, ConstantInt::get(Type::getInt32Ty(STy->getContext()),
1554 FieldNo));
1555}
1556
Chris Lattnera676c0f2011-02-07 16:40:21 +00001557Constant *ConstantExpr::getOffsetOf(const Type* Ty, Constant *FieldNo) {
Dan Gohmanff3af7252009-08-16 21:26:11 +00001558 // offsetof is implemented as: (i64) gep (Ty*)null, 0, FieldNo
1559 // Note that a non-inbounds gep is used, as null isn't within any object.
1560 Constant *GEPIdx[] = {
Dan Gohmanede94e62010-02-01 16:37:38 +00001561 ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0),
1562 FieldNo
Dan Gohmanff3af7252009-08-16 21:26:11 +00001563 };
1564 Constant *GEP = getGetElementPtr(
Dan Gohmanede94e62010-02-01 16:37:38 +00001565 Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx, 2);
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001566 return getPtrToInt(GEP,
1567 Type::getInt64Ty(Ty->getContext()));
Dan Gohmanff3af7252009-08-16 21:26:11 +00001568}
Owen Anderson487375e2009-07-29 18:55:55 +00001569
Reid Spencer266e42b2006-12-23 06:05:41 +00001570Constant *ConstantExpr::getCompare(unsigned short pred,
Reid Spencera009d0d2006-12-04 21:35:24 +00001571 Constant *C1, Constant *C2) {
1572 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001573 return getCompareTy(pred, C1, C2);
Chris Lattner29ca2c62004-08-04 18:50:09 +00001574}
1575
Chris Lattner6e415c02004-03-12 05:54:04 +00001576Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001577 Constant *V1, Constant *V2) {
Chris Lattner41632132008-12-29 00:16:12 +00001578 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner6e415c02004-03-12 05:54:04 +00001579
1580 if (ReqTy == V1->getType())
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001581 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
Chris Lattner6e415c02004-03-12 05:54:04 +00001582 return SC; // Fold common cases
1583
1584 std::vector<Constant*> argVec(3, C);
1585 argVec[1] = V1;
1586 argVec[2] = V2;
Reid Spenceree3c9912006-12-04 05:19:50 +00001587 ExprMapKeyType Key(Instruction::Select, argVec);
Owen Anderson61794042009-06-17 20:10:08 +00001588
Owen Anderson1584a292009-08-04 20:25:11 +00001589 LLVMContextImpl *pImpl = ReqTy->getContext().pImpl;
Owen Anderson1584a292009-08-04 20:25:11 +00001590 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00001591}
1592
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001593template<typename IndexTy>
Chris Lattnerb50d1352003-10-05 00:17:43 +00001594Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001595 IndexTy const *Idxs,
Chris Lattner94c8d292011-02-11 05:34:33 +00001596 unsigned NumIdx, bool InBounds) {
Dan Gohman12fce772008-05-15 19:50:34 +00001597 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs,
1598 Idxs+NumIdx) ==
1599 cast<PointerType>(ReqTy)->getElementType() &&
1600 "GEP indices invalid!");
Chris Lattner04b60fe2004-02-16 20:46:13 +00001601
Chris Lattner94c8d292011-02-11 05:34:33 +00001602 if (Constant *FC = ConstantFoldGetElementPtr(C, InBounds, Idxs, NumIdx))
1603 return FC; // Fold a few common cases.
Dan Gohman1b849082009-09-07 23:54:19 +00001604
Duncan Sands19d0b472010-02-16 11:11:14 +00001605 assert(C->getType()->isPointerTy() &&
Dan Gohman1b849082009-09-07 23:54:19 +00001606 "Non-pointer type for constant GetElementPtr expression");
1607 // Look up the constant in the table first to ensure uniqueness
1608 std::vector<Constant*> ArgVec;
1609 ArgVec.reserve(NumIdx+1);
1610 ArgVec.push_back(C);
1611 for (unsigned i = 0; i != NumIdx; ++i)
1612 ArgVec.push_back(cast<Constant>(Idxs[i]));
1613 const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec, 0,
Chris Lattner94c8d292011-02-11 05:34:33 +00001614 InBounds ? GEPOperator::IsInBounds : 0);
Dan Gohman1b849082009-09-07 23:54:19 +00001615
1616 LLVMContextImpl *pImpl = ReqTy->getContext().pImpl;
Dan Gohman1b849082009-09-07 23:54:19 +00001617 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
1618}
1619
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001620template<typename IndexTy>
1621Constant *ConstantExpr::getGetElementPtrImpl(Constant *C, IndexTy const *Idxs,
Chris Lattner94c8d292011-02-11 05:34:33 +00001622 unsigned NumIdx, bool InBounds) {
Chris Lattnerb50d1352003-10-05 00:17:43 +00001623 // Get the result type of the getelementptr!
Chris Lattner302116a2007-01-31 04:40:28 +00001624 const Type *Ty =
Dan Gohman12fce772008-05-15 19:50:34 +00001625 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001626 assert(Ty && "GEP indices invalid!");
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001627 unsigned As = cast<PointerType>(C->getType())->getAddressSpace();
Chris Lattner94c8d292011-02-11 05:34:33 +00001628 return getGetElementPtrTy(PointerType::get(Ty, As), C, Idxs, NumIdx,InBounds);
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001629}
1630
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001631Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
Chris Lattner94c8d292011-02-11 05:34:33 +00001632 unsigned NumIdx, bool InBounds) {
1633 return getGetElementPtrImpl(C, Idxs, NumIdx, InBounds);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001634}
1635
1636Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant *const *Idxs,
Chris Lattner94c8d292011-02-11 05:34:33 +00001637 unsigned NumIdx, bool InBounds) {
1638 return getGetElementPtrImpl(C, Idxs, NumIdx, InBounds);
Dan Gohmane4ca02d2009-09-03 23:34:49 +00001639}
1640
Reid Spenceree3c9912006-12-04 05:19:50 +00001641Constant *
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00001642ConstantExpr::getICmp(unsigned short pred, Constant *LHS, Constant *RHS) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001643 assert(LHS->getType() == RHS->getType());
1644 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
1645 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
1646
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001647 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001648 return FC; // Fold a few common cases...
1649
1650 // Look up the constant in the table first to ensure uniqueness
1651 std::vector<Constant*> ArgVec;
1652 ArgVec.push_back(LHS);
1653 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001654 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001655 const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00001656
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00001657 const Type *ResultTy = Type::getInt1Ty(LHS->getContext());
1658 if (const VectorType *VT = dyn_cast<VectorType>(LHS->getType()))
1659 ResultTy = VectorType::get(ResultTy, VT->getNumElements());
1660
Owen Anderson1584a292009-08-04 20:25:11 +00001661 LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl;
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00001662 return pImpl->ExprConstants.getOrCreate(ResultTy, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001663}
1664
1665Constant *
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00001666ConstantExpr::getFCmp(unsigned short pred, Constant *LHS, Constant *RHS) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001667 assert(LHS->getType() == RHS->getType());
1668 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
1669
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001670 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001671 return FC; // Fold a few common cases...
1672
1673 // Look up the constant in the table first to ensure uniqueness
1674 std::vector<Constant*> ArgVec;
1675 ArgVec.push_back(LHS);
1676 ArgVec.push_back(RHS);
Reid Spencerb1537492006-12-24 18:42:29 +00001677 // Get the key type with both the opcode and predicate
Reid Spenceree3c9912006-12-04 05:19:50 +00001678 const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00001679
1680 const Type *ResultTy = Type::getInt1Ty(LHS->getContext());
1681 if (const VectorType *VT = dyn_cast<VectorType>(LHS->getType()))
1682 ResultTy = VectorType::get(ResultTy, VT->getNumElements());
1683
Owen Anderson1584a292009-08-04 20:25:11 +00001684 LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl;
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00001685 return pImpl->ExprConstants.getOrCreate(ResultTy, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001686}
1687
Robert Bocchino23004482006-01-10 19:05:34 +00001688Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
1689 Constant *Idx) {
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001690 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
Chris Lattner09660c92009-12-30 20:25:09 +00001691 return FC; // Fold a few common cases.
Robert Bocchino23004482006-01-10 19:05:34 +00001692 // Look up the constant in the table first to ensure uniqueness
1693 std::vector<Constant*> ArgVec(1, Val);
1694 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001695 const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001696
Owen Anderson1584a292009-08-04 20:25:11 +00001697 LLVMContextImpl *pImpl = ReqTy->getContext().pImpl;
Owen Anderson1584a292009-08-04 20:25:11 +00001698 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00001699}
1700
1701Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001702 assert(Val->getType()->isVectorTy() &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001703 "Tried to create extractelement operation on non-vector type!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001704 assert(Idx->getType()->isIntegerTy(32) &&
Reid Spencer2546b762007-01-26 07:37:34 +00001705 "Extractelement index must be i32 type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001706 return getExtractElementTy(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +00001707 Val, Idx);
1708}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001709
Robert Bocchinoca27f032006-01-17 20:07:22 +00001710Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
1711 Constant *Elt, Constant *Idx) {
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001712 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
Chris Lattner09660c92009-12-30 20:25:09 +00001713 return FC; // Fold a few common cases.
Robert Bocchinoca27f032006-01-17 20:07:22 +00001714 // Look up the constant in the table first to ensure uniqueness
1715 std::vector<Constant*> ArgVec(1, Val);
1716 ArgVec.push_back(Elt);
1717 ArgVec.push_back(Idx);
Reid Spenceree3c9912006-12-04 05:19:50 +00001718 const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001719
Owen Anderson1584a292009-08-04 20:25:11 +00001720 LLVMContextImpl *pImpl = ReqTy->getContext().pImpl;
Owen Anderson1584a292009-08-04 20:25:11 +00001721 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001722}
1723
1724Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1725 Constant *Idx) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001726 assert(Val->getType()->isVectorTy() &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001727 "Tried to create insertelement operation on non-vector type!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001728 assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
Robert Bocchinoca27f032006-01-17 20:07:22 +00001729 && "Insertelement types must match!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001730 assert(Idx->getType()->isIntegerTy(32) &&
Reid Spencer2546b762007-01-26 07:37:34 +00001731 "Insertelement index must be i32 type!");
Gordon Henriksenb52d1ed2008-08-30 15:41:51 +00001732 return getInsertElementTy(Val->getType(), Val, Elt, Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001733}
1734
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001735Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
1736 Constant *V2, Constant *Mask) {
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001737 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001738 return FC; // Fold a few common cases...
1739 // Look up the constant in the table first to ensure uniqueness
1740 std::vector<Constant*> ArgVec(1, V1);
1741 ArgVec.push_back(V2);
1742 ArgVec.push_back(Mask);
Reid Spenceree3c9912006-12-04 05:19:50 +00001743 const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
Owen Anderson61794042009-06-17 20:10:08 +00001744
Owen Anderson1584a292009-08-04 20:25:11 +00001745 LLVMContextImpl *pImpl = ReqTy->getContext().pImpl;
Owen Anderson1584a292009-08-04 20:25:11 +00001746 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001747}
1748
1749Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
1750 Constant *Mask) {
1751 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
1752 "Invalid shuffle vector constant expr operands!");
Nate Begeman94aa38d2009-02-12 21:28:33 +00001753
1754 unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements();
1755 const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
1756 const Type *ShufTy = VectorType::get(EltTy, NElts);
1757 return getShuffleVectorTy(ShufTy, V1, V2, Mask);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001758}
1759
Dan Gohman12fce772008-05-15 19:50:34 +00001760Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg,
1761 Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001762 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001763 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
1764 Idxs+NumIdx) == Val->getType() &&
1765 "insertvalue indices invalid!");
1766 assert(Agg->getType() == ReqTy &&
1767 "insertvalue type invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00001768 assert(Agg->getType()->isFirstClassType() &&
1769 "Non-first-class type for constant InsertValue expression");
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001770 Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00001771 assert(FC && "InsertValue constant expr couldn't be folded!");
1772 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00001773}
1774
1775Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001776 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00001777 assert(Agg->getType()->isFirstClassType() &&
1778 "Tried to create insertelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00001779
Dan Gohman0752bff2008-05-23 00:36:11 +00001780 const Type *ReqTy = Agg->getType();
Devang Pateld26344d2008-11-03 23:20:04 +00001781#ifndef NDEBUG
Dan Gohman0752bff2008-05-23 00:36:11 +00001782 const Type *ValTy =
Dan Gohman12fce772008-05-15 19:50:34 +00001783 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
Devang Pateld26344d2008-11-03 23:20:04 +00001784#endif
Dan Gohman0752bff2008-05-23 00:36:11 +00001785 assert(ValTy == Val->getType() && "insertvalue indices invalid!");
Dan Gohman12fce772008-05-15 19:50:34 +00001786 return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx);
1787}
1788
1789Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001790 const unsigned *Idxs, unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001791 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs,
1792 Idxs+NumIdx) == ReqTy &&
1793 "extractvalue indices invalid!");
Dan Gohman0752bff2008-05-23 00:36:11 +00001794 assert(Agg->getType()->isFirstClassType() &&
1795 "Non-first-class type for constant extractvalue expression");
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001796 Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs, NumIdx);
Dan Gohmand5d24f62008-07-21 23:30:30 +00001797 assert(FC && "ExtractValue constant expr couldn't be folded!");
1798 return FC;
Dan Gohman12fce772008-05-15 19:50:34 +00001799}
1800
1801Constant *ConstantExpr::getExtractValue(Constant *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001802 const unsigned *IdxList, unsigned NumIdx) {
Dan Gohman0752bff2008-05-23 00:36:11 +00001803 assert(Agg->getType()->isFirstClassType() &&
1804 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00001805
1806 const Type *ReqTy =
1807 ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx);
1808 assert(ReqTy && "extractvalue indices invalid!");
1809 return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx);
1810}
1811
Chris Lattnere9b4ad72011-02-10 07:01:55 +00001812Constant *ConstantExpr::getNeg(Constant *C, bool HasNUW, bool HasNSW) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001813 assert(C->getType()->isIntOrIntVectorTy() &&
Owen Anderson487375e2009-07-29 18:55:55 +00001814 "Cannot NEG a nonintegral value!");
Chris Lattnere9b4ad72011-02-10 07:01:55 +00001815 return getSub(ConstantFP::getZeroValueForNegation(C->getType()),
1816 C, HasNUW, HasNSW);
Owen Anderson487375e2009-07-29 18:55:55 +00001817}
1818
Chris Lattnera676c0f2011-02-07 16:40:21 +00001819Constant *ConstantExpr::getFNeg(Constant *C) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001820 assert(C->getType()->isFPOrFPVectorTy() &&
Owen Anderson487375e2009-07-29 18:55:55 +00001821 "Cannot FNEG a non-floating-point value!");
Chris Lattnere9b4ad72011-02-10 07:01:55 +00001822 return getFSub(ConstantFP::getZeroValueForNegation(C->getType()), C);
Owen Anderson487375e2009-07-29 18:55:55 +00001823}
1824
Chris Lattnera676c0f2011-02-07 16:40:21 +00001825Constant *ConstantExpr::getNot(Constant *C) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001826 assert(C->getType()->isIntOrIntVectorTy() &&
Owen Anderson487375e2009-07-29 18:55:55 +00001827 "Cannot NOT a nonintegral value!");
Owen Anderson5a1acd92009-07-31 20:28:14 +00001828 return get(Instruction::Xor, C, Constant::getAllOnesValue(C->getType()));
Owen Anderson487375e2009-07-29 18:55:55 +00001829}
1830
Chris Lattnere9b4ad72011-02-10 07:01:55 +00001831Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2,
1832 bool HasNUW, bool HasNSW) {
1833 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
1834 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
1835 return get(Instruction::Add, C1, C2, Flags);
Owen Anderson487375e2009-07-29 18:55:55 +00001836}
1837
Chris Lattnera676c0f2011-02-07 16:40:21 +00001838Constant *ConstantExpr::getFAdd(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00001839 return get(Instruction::FAdd, C1, C2);
1840}
1841
Chris Lattnere9b4ad72011-02-10 07:01:55 +00001842Constant *ConstantExpr::getSub(Constant *C1, Constant *C2,
1843 bool HasNUW, bool HasNSW) {
1844 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
1845 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
1846 return get(Instruction::Sub, C1, C2, Flags);
Owen Anderson487375e2009-07-29 18:55:55 +00001847}
1848
Chris Lattnera676c0f2011-02-07 16:40:21 +00001849Constant *ConstantExpr::getFSub(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00001850 return get(Instruction::FSub, C1, C2);
1851}
1852
Chris Lattnere9b4ad72011-02-10 07:01:55 +00001853Constant *ConstantExpr::getMul(Constant *C1, Constant *C2,
1854 bool HasNUW, bool HasNSW) {
1855 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
1856 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
1857 return get(Instruction::Mul, C1, C2, Flags);
Owen Anderson487375e2009-07-29 18:55:55 +00001858}
1859
Chris Lattnera676c0f2011-02-07 16:40:21 +00001860Constant *ConstantExpr::getFMul(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00001861 return get(Instruction::FMul, C1, C2);
1862}
1863
Chris Lattner0d75eac2011-02-09 16:43:07 +00001864Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2, bool isExact) {
1865 return get(Instruction::UDiv, C1, C2,
1866 isExact ? PossiblyExactOperator::IsExact : 0);
Owen Anderson487375e2009-07-29 18:55:55 +00001867}
1868
Chris Lattner0d75eac2011-02-09 16:43:07 +00001869Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2, bool isExact) {
1870 return get(Instruction::SDiv, C1, C2,
1871 isExact ? PossiblyExactOperator::IsExact : 0);
Owen Anderson487375e2009-07-29 18:55:55 +00001872}
1873
Chris Lattnera676c0f2011-02-07 16:40:21 +00001874Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00001875 return get(Instruction::FDiv, C1, C2);
1876}
1877
Chris Lattnera676c0f2011-02-07 16:40:21 +00001878Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00001879 return get(Instruction::URem, C1, C2);
1880}
1881
Chris Lattnera676c0f2011-02-07 16:40:21 +00001882Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00001883 return get(Instruction::SRem, C1, C2);
1884}
1885
Chris Lattnera676c0f2011-02-07 16:40:21 +00001886Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00001887 return get(Instruction::FRem, C1, C2);
1888}
1889
Chris Lattnera676c0f2011-02-07 16:40:21 +00001890Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00001891 return get(Instruction::And, C1, C2);
1892}
1893
Chris Lattnera676c0f2011-02-07 16:40:21 +00001894Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00001895 return get(Instruction::Or, C1, C2);
1896}
1897
Chris Lattnera676c0f2011-02-07 16:40:21 +00001898Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00001899 return get(Instruction::Xor, C1, C2);
1900}
1901
Chris Lattnere9b4ad72011-02-10 07:01:55 +00001902Constant *ConstantExpr::getShl(Constant *C1, Constant *C2,
1903 bool HasNUW, bool HasNSW) {
1904 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
1905 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
1906 return get(Instruction::Shl, C1, C2, Flags);
Owen Anderson487375e2009-07-29 18:55:55 +00001907}
1908
Chris Lattner0d75eac2011-02-09 16:43:07 +00001909Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2, bool isExact) {
1910 return get(Instruction::LShr, C1, C2,
1911 isExact ? PossiblyExactOperator::IsExact : 0);
Owen Anderson487375e2009-07-29 18:55:55 +00001912}
1913
Chris Lattner0d75eac2011-02-09 16:43:07 +00001914Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2, bool isExact) {
1915 return get(Instruction::AShr, C1, C2,
1916 isExact ? PossiblyExactOperator::IsExact : 0);
Owen Anderson487375e2009-07-29 18:55:55 +00001917}
1918
Vikram S. Adve4c485332002-07-15 18:19:33 +00001919// destroyConstant - Remove the constant from the constant table...
1920//
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001921void ConstantExpr::destroyConstant() {
Chris Lattner718da702010-07-17 06:13:52 +00001922 getRawType()->getContext().pImpl->ExprConstants.remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001923 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001924}
1925
Chris Lattner3cd8c562002-07-30 18:54:25 +00001926const char *ConstantExpr::getOpcodeName() const {
1927 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001928}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00001929
Chris Lattnera3b94ba2010-03-30 20:48:48 +00001930
1931
1932GetElementPtrConstantExpr::
1933GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
1934 const Type *DestTy)
1935 : ConstantExpr(DestTy, Instruction::GetElementPtr,
1936 OperandTraits<GetElementPtrConstantExpr>::op_end(this)
1937 - (IdxList.size()+1), IdxList.size()+1) {
1938 OperandList[0] = C;
1939 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
1940 OperandList[i+1] = IdxList[i];
1941}
1942
1943
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001944//===----------------------------------------------------------------------===//
1945// replaceUsesOfWithOnConstant implementations
1946
Chris Lattner913849b2007-08-21 00:55:23 +00001947/// replaceUsesOfWithOnConstant - Update this constant array to change uses of
1948/// 'From' to be uses of 'To'. This must update the uniquing data structures
1949/// etc.
1950///
1951/// Note that we intentionally replace all uses of From with To here. Consider
1952/// a large array that uses 'From' 1000 times. By handling this case all here,
1953/// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that
1954/// single invocation handles all 1000 uses. Handling them one at a time would
1955/// work, but would be really slow because it would have to unique each updated
1956/// array instance.
Chris Lattner31b132c2009-10-28 00:01:44 +00001957///
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001958void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001959 Use *U) {
Owen Andersonc2c79322009-07-28 18:32:17 +00001960 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
1961 Constant *ToC = cast<Constant>(To);
1962
Chris Lattner718da702010-07-17 06:13:52 +00001963 LLVMContextImpl *pImpl = getRawType()->getContext().pImpl;
Owen Andersonc2c79322009-07-28 18:32:17 +00001964
Dan Gohmane4532f32009-09-15 15:58:07 +00001965 std::pair<LLVMContextImpl::ArrayConstantsTy::MapKey, ConstantArray*> Lookup;
Chris Lattner718da702010-07-17 06:13:52 +00001966 Lookup.first.first = cast<ArrayType>(getRawType());
Owen Andersonc2c79322009-07-28 18:32:17 +00001967 Lookup.second = this;
1968
1969 std::vector<Constant*> &Values = Lookup.first.second;
1970 Values.reserve(getNumOperands()); // Build replacement array.
1971
1972 // Fill values with the modified operands of the constant array. Also,
1973 // compute whether this turns into an all-zeros array.
1974 bool isAllZeros = false;
1975 unsigned NumUpdated = 0;
1976 if (!ToC->isNullValue()) {
1977 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1978 Constant *Val = cast<Constant>(O->get());
1979 if (Val == From) {
1980 Val = ToC;
1981 ++NumUpdated;
1982 }
1983 Values.push_back(Val);
1984 }
1985 } else {
1986 isAllZeros = true;
1987 for (Use *O = OperandList, *E = OperandList+getNumOperands();O != E; ++O) {
1988 Constant *Val = cast<Constant>(O->get());
1989 if (Val == From) {
1990 Val = ToC;
1991 ++NumUpdated;
1992 }
1993 Values.push_back(Val);
1994 if (isAllZeros) isAllZeros = Val->isNullValue();
1995 }
1996 }
1997
1998 Constant *Replacement = 0;
1999 if (isAllZeros) {
Chris Lattner718da702010-07-17 06:13:52 +00002000 Replacement = ConstantAggregateZero::get(getRawType());
Owen Andersonc2c79322009-07-28 18:32:17 +00002001 } else {
2002 // Check to see if we have this array type already.
Owen Andersonc2c79322009-07-28 18:32:17 +00002003 bool Exists;
2004 LLVMContextImpl::ArrayConstantsTy::MapTy::iterator I =
2005 pImpl->ArrayConstants.InsertOrGetItem(Lookup, Exists);
2006
2007 if (Exists) {
Devang Patelf7188322009-09-03 01:39:20 +00002008 Replacement = I->second;
Owen Andersonc2c79322009-07-28 18:32:17 +00002009 } else {
2010 // Okay, the new shape doesn't exist in the system yet. Instead of
2011 // creating a new constant array, inserting it, replaceallusesof'ing the
2012 // old with the new, then deleting the old... just update the current one
2013 // in place!
2014 pImpl->ArrayConstants.MoveConstantToNewSlot(this, I);
2015
2016 // Update to the new value. Optimize for the case when we have a single
2017 // operand that we're changing, but handle bulk updates efficiently.
2018 if (NumUpdated == 1) {
2019 unsigned OperandToUpdate = U - OperandList;
2020 assert(getOperand(OperandToUpdate) == From &&
2021 "ReplaceAllUsesWith broken!");
2022 setOperand(OperandToUpdate, ToC);
2023 } else {
2024 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2025 if (getOperand(i) == From)
2026 setOperand(i, ToC);
2027 }
2028 return;
2029 }
2030 }
Chris Lattnerb64419a2005-10-03 22:51:37 +00002031
2032 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002033 assert(Replacement != this && "I didn't contain From!");
2034
Chris Lattner7a1450d2005-10-04 18:13:04 +00002035 // Everyone using this now uses the replacement.
2036 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002037
2038 // Delete the old constant!
2039 destroyConstant();
2040}
2041
2042void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002043 Use *U) {
Owen Anderson45308b52009-07-27 22:29:26 +00002044 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2045 Constant *ToC = cast<Constant>(To);
2046
2047 unsigned OperandToUpdate = U-OperandList;
2048 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
2049
Dan Gohmane4532f32009-09-15 15:58:07 +00002050 std::pair<LLVMContextImpl::StructConstantsTy::MapKey, ConstantStruct*> Lookup;
Chris Lattner718da702010-07-17 06:13:52 +00002051 Lookup.first.first = cast<StructType>(getRawType());
Owen Anderson45308b52009-07-27 22:29:26 +00002052 Lookup.second = this;
2053 std::vector<Constant*> &Values = Lookup.first.second;
2054 Values.reserve(getNumOperands()); // Build replacement struct.
2055
2056
2057 // Fill values with the modified operands of the constant struct. Also,
2058 // compute whether this turns into an all-zeros struct.
2059 bool isAllZeros = false;
2060 if (!ToC->isNullValue()) {
2061 for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O)
2062 Values.push_back(cast<Constant>(O->get()));
2063 } else {
2064 isAllZeros = true;
2065 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2066 Constant *Val = cast<Constant>(O->get());
2067 Values.push_back(Val);
2068 if (isAllZeros) isAllZeros = Val->isNullValue();
2069 }
2070 }
2071 Values[OperandToUpdate] = ToC;
2072
Chris Lattner718da702010-07-17 06:13:52 +00002073 LLVMContextImpl *pImpl = getRawType()->getContext().pImpl;
Owen Anderson45308b52009-07-27 22:29:26 +00002074
2075 Constant *Replacement = 0;
2076 if (isAllZeros) {
Chris Lattner718da702010-07-17 06:13:52 +00002077 Replacement = ConstantAggregateZero::get(getRawType());
Owen Anderson45308b52009-07-27 22:29:26 +00002078 } else {
Chris Lattner718da702010-07-17 06:13:52 +00002079 // Check to see if we have this struct type already.
Owen Anderson45308b52009-07-27 22:29:26 +00002080 bool Exists;
2081 LLVMContextImpl::StructConstantsTy::MapTy::iterator I =
2082 pImpl->StructConstants.InsertOrGetItem(Lookup, Exists);
2083
2084 if (Exists) {
Devang Patelf7188322009-09-03 01:39:20 +00002085 Replacement = I->second;
Owen Anderson45308b52009-07-27 22:29:26 +00002086 } else {
2087 // Okay, the new shape doesn't exist in the system yet. Instead of
2088 // creating a new constant struct, inserting it, replaceallusesof'ing the
2089 // old with the new, then deleting the old... just update the current one
2090 // in place!
2091 pImpl->StructConstants.MoveConstantToNewSlot(this, I);
2092
2093 // Update to the new value.
2094 setOperand(OperandToUpdate, ToC);
2095 return;
2096 }
2097 }
2098
2099 assert(Replacement != this && "I didn't contain From!");
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002100
Chris Lattner7a1450d2005-10-04 18:13:04 +00002101 // Everyone using this now uses the replacement.
2102 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002103
2104 // Delete the old constant!
2105 destroyConstant();
2106}
2107
Reid Spencerd84d35b2007-02-15 02:26:10 +00002108void ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002109 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002110 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2111
2112 std::vector<Constant*> Values;
2113 Values.reserve(getNumOperands()); // Build replacement array...
2114 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2115 Constant *Val = getOperand(i);
2116 if (Val == From) Val = cast<Constant>(To);
2117 Values.push_back(Val);
2118 }
2119
Chris Lattner718da702010-07-17 06:13:52 +00002120 Constant *Replacement = get(cast<VectorType>(getRawType()), Values);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002121 assert(Replacement != this && "I didn't contain From!");
2122
Chris Lattner7a1450d2005-10-04 18:13:04 +00002123 // Everyone using this now uses the replacement.
2124 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002125
2126 // Delete the old constant!
2127 destroyConstant();
2128}
2129
2130void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00002131 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002132 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2133 Constant *To = cast<Constant>(ToV);
2134
2135 Constant *Replacement = 0;
2136 if (getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerb5d70302007-02-19 20:01:23 +00002137 SmallVector<Constant*, 8> Indices;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002138 Constant *Pointer = getOperand(0);
2139 Indices.reserve(getNumOperands()-1);
2140 if (Pointer == From) Pointer = To;
2141
2142 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2143 Constant *Val = getOperand(i);
2144 if (Val == From) Val = To;
2145 Indices.push_back(Val);
2146 }
Chris Lattnerb5d70302007-02-19 20:01:23 +00002147 Replacement = ConstantExpr::getGetElementPtr(Pointer,
Chris Lattner603af182011-02-11 05:37:21 +00002148 &Indices[0], Indices.size(),
2149 cast<GEPOperator>(this)->isInBounds());
Dan Gohman12fce772008-05-15 19:50:34 +00002150 } else if (getOpcode() == Instruction::ExtractValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002151 Constant *Agg = getOperand(0);
Dan Gohman12fce772008-05-15 19:50:34 +00002152 if (Agg == From) Agg = To;
2153
Dan Gohman1ecaf452008-05-31 00:58:22 +00002154 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002155 Replacement = ConstantExpr::getExtractValue(Agg,
2156 &Indices[0], Indices.size());
2157 } else if (getOpcode() == Instruction::InsertValue) {
Dan Gohman12fce772008-05-15 19:50:34 +00002158 Constant *Agg = getOperand(0);
2159 Constant *Val = getOperand(1);
Dan Gohman12fce772008-05-15 19:50:34 +00002160 if (Agg == From) Agg = To;
2161 if (Val == From) Val = To;
2162
Dan Gohman1ecaf452008-05-31 00:58:22 +00002163 const SmallVector<unsigned, 4> &Indices = getIndices();
Dan Gohman12fce772008-05-15 19:50:34 +00002164 Replacement = ConstantExpr::getInsertValue(Agg, Val,
2165 &Indices[0], Indices.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002166 } else if (isCast()) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002167 assert(getOperand(0) == From && "Cast only has one use!");
Chris Lattner718da702010-07-17 06:13:52 +00002168 Replacement = ConstantExpr::getCast(getOpcode(), To, getRawType());
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002169 } else if (getOpcode() == Instruction::Select) {
2170 Constant *C1 = getOperand(0);
2171 Constant *C2 = getOperand(1);
2172 Constant *C3 = getOperand(2);
2173 if (C1 == From) C1 = To;
2174 if (C2 == From) C2 = To;
2175 if (C3 == From) C3 = To;
2176 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00002177 } else if (getOpcode() == Instruction::ExtractElement) {
2178 Constant *C1 = getOperand(0);
2179 Constant *C2 = getOperand(1);
2180 if (C1 == From) C1 = To;
2181 if (C2 == From) C2 = To;
2182 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00002183 } else if (getOpcode() == Instruction::InsertElement) {
2184 Constant *C1 = getOperand(0);
2185 Constant *C2 = getOperand(1);
2186 Constant *C3 = getOperand(1);
2187 if (C1 == From) C1 = To;
2188 if (C2 == From) C2 = To;
2189 if (C3 == From) C3 = To;
2190 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2191 } else if (getOpcode() == Instruction::ShuffleVector) {
2192 Constant *C1 = getOperand(0);
2193 Constant *C2 = getOperand(1);
2194 Constant *C3 = getOperand(2);
2195 if (C1 == From) C1 = To;
2196 if (C2 == From) C2 = To;
2197 if (C3 == From) C3 = To;
2198 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Reid Spenceree3c9912006-12-04 05:19:50 +00002199 } else if (isCompare()) {
2200 Constant *C1 = getOperand(0);
2201 Constant *C2 = getOperand(1);
2202 if (C1 == From) C1 = To;
2203 if (C2 == From) C2 = To;
2204 if (getOpcode() == Instruction::ICmp)
2205 Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002206 else {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002207 assert(getOpcode() == Instruction::FCmp);
2208 Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
Chris Lattnereab49262008-07-14 05:17:31 +00002209 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002210 } else if (getNumOperands() == 2) {
2211 Constant *C1 = getOperand(0);
2212 Constant *C2 = getOperand(1);
2213 if (C1 == From) C1 = To;
2214 if (C2 == From) C2 = To;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002215 Replacement = ConstantExpr::get(getOpcode(), C1, C2, SubclassOptionalData);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002216 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002217 llvm_unreachable("Unknown ConstantExpr type!");
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002218 return;
2219 }
2220
2221 assert(Replacement != this && "I didn't contain From!");
2222
Chris Lattner7a1450d2005-10-04 18:13:04 +00002223 // Everyone using this now uses the replacement.
2224 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002225
2226 // Delete the old constant!
2227 destroyConstant();
Matthijs Kooijmanba5d7ef2008-07-03 07:46:41 +00002228}