blob: 54d0f1f8c2fac775324a8adde792abdc13a6c338 [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
Chandler Carruth9fb823b2013-01-02 11:36:10 +000014#include "llvm/IR/Constants.h"
Chris Lattner33e93b82007-02-27 03:05:06 +000015#include "ConstantFold.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "LLVMContextImpl.h"
17#include "llvm/ADT/DenseMap.h"
18#include "llvm/ADT/FoldingSet.h"
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/SmallVector.h"
21#include "llvm/ADT/StringExtras.h"
22#include "llvm/ADT/StringMap.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/DerivedTypes.h"
Chandler Carruth03eb0de2014-03-04 10:40:04 +000024#include "llvm/IR/GetElementPtrTypeIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/GlobalValue.h"
26#include "llvm/IR/Instructions.h"
27#include "llvm/IR/Module.h"
28#include "llvm/IR/Operator.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000029#include "llvm/Support/Compiler.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000030#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000031#include "llvm/Support/ErrorHandling.h"
Chris Lattner69edc982006-09-28 00:35:06 +000032#include "llvm/Support/ManagedStatic.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000033#include "llvm/Support/MathExtras.h"
Chris Lattner78683a72009-08-23 04:02:03 +000034#include "llvm/Support/raw_ostream.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000035#include <algorithm>
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
David Blaikiea379b1812011-12-20 02:50:00 +000043void Constant::anchor() { }
44
Duncan P. N. Exon Smithb6452792016-02-21 02:39:49 +000045void ConstantData::anchor() {}
46
Chris Lattnerac5fb562011-07-15 05:58:04 +000047bool Constant::isNegativeZeroValue() const {
48 // Floating point values have an explicit -0.0 value.
49 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
50 return CFP->isZero() && CFP->isNegative();
Galina Kistanovafc259902012-07-13 01:25:27 +000051
David Tweed5493fee2013-03-18 11:54:44 +000052 // Equivalent for a vector of -0.0's.
53 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this))
54 if (ConstantFP *SplatCFP = dyn_cast_or_null<ConstantFP>(CV->getSplatValue()))
55 if (SplatCFP && SplatCFP->isZero() && SplatCFP->isNegative())
56 return true;
57
Owen Anderson8e851302015-11-20 22:34:48 +000058 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
59 if (ConstantFP *SplatCFP = dyn_cast_or_null<ConstantFP>(CV->getSplatValue()))
60 if (SplatCFP && SplatCFP->isZero() && SplatCFP->isNegative())
61 return true;
62
David Tweed298e4192013-03-19 10:16:40 +000063 // We've already handled true FP case; any other FP vectors can't represent -0.0.
64 if (getType()->isFPOrFPVectorTy())
65 return false;
David Tweed5493fee2013-03-18 11:54:44 +000066
Chris Lattnerac5fb562011-07-15 05:58:04 +000067 // Otherwise, just use +0.0.
68 return isNullValue();
69}
70
Shuxin Yang9ca562e2013-01-09 00:53:25 +000071// Return true iff this constant is positive zero (floating point), negative
72// zero (floating point), or a null value.
Shuxin Yangf0537ab2013-01-09 00:13:41 +000073bool Constant::isZeroValue() const {
74 // Floating point values have an explicit -0.0 value.
75 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
76 return CFP->isZero();
77
Owen Anderson630077e2015-11-20 08:16:13 +000078 // Equivalent for a vector of -0.0's.
79 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this))
80 if (ConstantFP *SplatCFP = dyn_cast_or_null<ConstantFP>(CV->getSplatValue()))
81 if (SplatCFP && SplatCFP->isZero())
82 return true;
83
Owen Anderson8e851302015-11-20 22:34:48 +000084 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
85 if (ConstantFP *SplatCFP = dyn_cast_or_null<ConstantFP>(CV->getSplatValue()))
86 if (SplatCFP && SplatCFP->isZero())
87 return true;
88
Shuxin Yangf0537ab2013-01-09 00:13:41 +000089 // Otherwise, just use +0.0.
90 return isNullValue();
91}
92
Chris Lattnerbe6610c2011-07-15 06:14:08 +000093bool Constant::isNullValue() const {
94 // 0 is null.
95 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
96 return CI->isZero();
Galina Kistanovafc259902012-07-13 01:25:27 +000097
Chris Lattnerbe6610c2011-07-15 06:14:08 +000098 // +0.0 is null.
99 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
100 return CFP->isZero() && !CFP->isNegative();
101
David Majnemerf0f224d2015-11-11 21:57:16 +0000102 // constant zero is zero for aggregates, cpnull is null for pointers, none for
103 // tokens.
104 return isa<ConstantAggregateZero>(this) || isa<ConstantPointerNull>(this) ||
105 isa<ConstantTokenNone>(this);
Chris Lattnerbe6610c2011-07-15 06:14:08 +0000106}
107
Nadav Rotem365af6f2011-08-24 20:18:38 +0000108bool Constant::isAllOnesValue() const {
109 // Check for -1 integers
110 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
111 return CI->isMinusOne();
112
113 // Check for FP which are bitcasted from -1 integers
114 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
115 return CFP->getValueAPF().bitcastToAPInt().isAllOnesValue();
116
Benjamin Kramer42d098e2011-11-14 19:12:20 +0000117 // Check for constant vectors which are splats of -1 values.
Nadav Rotem365af6f2011-08-24 20:18:38 +0000118 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
Benjamin Kramer42d098e2011-11-14 19:12:20 +0000119 if (Constant *Splat = CV->getSplatValue())
120 return Splat->isAllOnesValue();
Nadav Rotem365af6f2011-08-24 20:18:38 +0000121
Chris Lattnerf14a67f2012-01-26 02:31:22 +0000122 // Check for constant vectors which are splats of -1 values.
123 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this))
124 if (Constant *Splat = CV->getSplatValue())
125 return Splat->isAllOnesValue();
126
Nadav Rotem365af6f2011-08-24 20:18:38 +0000127 return false;
128}
Benjamin Kramer42d098e2011-11-14 19:12:20 +0000129
David Majnemer1a0bbc82014-08-16 09:23:42 +0000130bool Constant::isOneValue() const {
131 // Check for 1 integers
132 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
133 return CI->isOne();
134
135 // Check for FP which are bitcasted from 1 integers
136 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
137 return CFP->getValueAPF().bitcastToAPInt() == 1;
138
139 // Check for constant vectors which are splats of 1 values.
140 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
141 if (Constant *Splat = CV->getSplatValue())
142 return Splat->isOneValue();
143
144 // Check for constant vectors which are splats of 1 values.
145 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this))
146 if (Constant *Splat = CV->getSplatValue())
147 return Splat->isOneValue();
148
149 return false;
150}
151
David Majnemerbdeef602014-07-02 06:07:09 +0000152bool Constant::isMinSignedValue() const {
153 // Check for INT_MIN integers
154 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
155 return CI->isMinValue(/*isSigned=*/true);
156
157 // Check for FP which are bitcasted from INT_MIN integers
158 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
159 return CFP->getValueAPF().bitcastToAPInt().isMinSignedValue();
160
161 // Check for constant vectors which are splats of INT_MIN values.
162 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
163 if (Constant *Splat = CV->getSplatValue())
164 return Splat->isMinSignedValue();
165
166 // Check for constant vectors which are splats of INT_MIN values.
167 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this))
168 if (Constant *Splat = CV->getSplatValue())
169 return Splat->isMinSignedValue();
170
171 return false;
172}
173
David Majnemer0e6c9862014-08-22 16:41:23 +0000174bool Constant::isNotMinSignedValue() const {
175 // Check for INT_MIN integers
176 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
177 return !CI->isMinValue(/*isSigned=*/true);
178
179 // Check for FP which are bitcasted from INT_MIN integers
180 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
181 return !CFP->getValueAPF().bitcastToAPInt().isMinSignedValue();
182
183 // Check for constant vectors which are splats of INT_MIN values.
184 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
185 if (Constant *Splat = CV->getSplatValue())
186 return Splat->isNotMinSignedValue();
187
188 // Check for constant vectors which are splats of INT_MIN values.
189 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this))
190 if (Constant *Splat = CV->getSplatValue())
191 return Splat->isNotMinSignedValue();
192
193 // It *may* contain INT_MIN, we can't tell.
194 return false;
195}
196
Owen Anderson5a1acd92009-07-31 20:28:14 +0000197// Constructor to create a '0' constant of arbitrary type...
Chris Lattner229907c2011-07-18 04:54:35 +0000198Constant *Constant::getNullValue(Type *Ty) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000199 switch (Ty->getTypeID()) {
200 case Type::IntegerTyID:
201 return ConstantInt::get(Ty, 0);
Dan Gohman518cda42011-12-17 00:04:22 +0000202 case Type::HalfTyID:
203 return ConstantFP::get(Ty->getContext(),
204 APFloat::getZero(APFloat::IEEEhalf));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000205 case Type::FloatTyID:
Benjamin Kramer8ceebfa2010-12-04 14:22:24 +0000206 return ConstantFP::get(Ty->getContext(),
207 APFloat::getZero(APFloat::IEEEsingle));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000208 case Type::DoubleTyID:
Benjamin Kramer8ceebfa2010-12-04 14:22:24 +0000209 return ConstantFP::get(Ty->getContext(),
210 APFloat::getZero(APFloat::IEEEdouble));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000211 case Type::X86_FP80TyID:
Benjamin Kramer8ceebfa2010-12-04 14:22:24 +0000212 return ConstantFP::get(Ty->getContext(),
213 APFloat::getZero(APFloat::x87DoubleExtended));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000214 case Type::FP128TyID:
215 return ConstantFP::get(Ty->getContext(),
Benjamin Kramer8ceebfa2010-12-04 14:22:24 +0000216 APFloat::getZero(APFloat::IEEEquad));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000217 case Type::PPC_FP128TyID:
Benjamin Kramer8ceebfa2010-12-04 14:22:24 +0000218 return ConstantFP::get(Ty->getContext(),
Tim Northover29178a32013-01-22 09:46:31 +0000219 APFloat(APFloat::PPCDoubleDouble,
220 APInt::getNullValue(128)));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000221 case Type::PointerTyID:
222 return ConstantPointerNull::get(cast<PointerType>(Ty));
223 case Type::StructTyID:
224 case Type::ArrayTyID:
225 case Type::VectorTyID:
226 return ConstantAggregateZero::get(Ty);
David Majnemerf0f224d2015-11-11 21:57:16 +0000227 case Type::TokenTyID:
228 return ConstantTokenNone::get(Ty->getContext());
Owen Anderson5a1acd92009-07-31 20:28:14 +0000229 default:
230 // Function, Label, or Opaque type?
Craig Topperc514b542012-02-05 22:14:15 +0000231 llvm_unreachable("Cannot create a null constant of that type!");
Owen Anderson5a1acd92009-07-31 20:28:14 +0000232 }
233}
234
Chris Lattner229907c2011-07-18 04:54:35 +0000235Constant *Constant::getIntegerValue(Type *Ty, const APInt &V) {
236 Type *ScalarTy = Ty->getScalarType();
Dan Gohmanf011f5a2009-08-03 22:07:33 +0000237
238 // Create the base integer constant.
239 Constant *C = ConstantInt::get(Ty->getContext(), V);
240
241 // Convert an integer to a pointer, if necessary.
Chris Lattner229907c2011-07-18 04:54:35 +0000242 if (PointerType *PTy = dyn_cast<PointerType>(ScalarTy))
Dan Gohmanf011f5a2009-08-03 22:07:33 +0000243 C = ConstantExpr::getIntToPtr(C, PTy);
244
245 // Broadcast a scalar to a vector, if necessary.
Chris Lattner229907c2011-07-18 04:54:35 +0000246 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
Chris Lattnere9eed292012-01-25 05:19:54 +0000247 C = ConstantVector::getSplat(VTy->getNumElements(), C);
Dan Gohmanf011f5a2009-08-03 22:07:33 +0000248
249 return C;
250}
251
Chris Lattner229907c2011-07-18 04:54:35 +0000252Constant *Constant::getAllOnesValue(Type *Ty) {
253 if (IntegerType *ITy = dyn_cast<IntegerType>(Ty))
Owen Anderson5a1acd92009-07-31 20:28:14 +0000254 return ConstantInt::get(Ty->getContext(),
255 APInt::getAllOnesValue(ITy->getBitWidth()));
Nadav Rotem7cc6d122011-02-17 21:22:27 +0000256
257 if (Ty->isFloatingPointTy()) {
258 APFloat FL = APFloat::getAllOnesValue(Ty->getPrimitiveSizeInBits(),
259 !Ty->isPPC_FP128Ty());
260 return ConstantFP::get(Ty->getContext(), FL);
261 }
262
Chris Lattner229907c2011-07-18 04:54:35 +0000263 VectorType *VTy = cast<VectorType>(Ty);
Chris Lattnere9eed292012-01-25 05:19:54 +0000264 return ConstantVector::getSplat(VTy->getNumElements(),
265 getAllOnesValue(VTy->getElementType()));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000266}
267
Chris Lattner7e683d12012-01-25 06:16:32 +0000268/// getAggregateElement - For aggregates (struct/array/vector) return the
269/// constant that corresponds to the specified element if possible, or null if
270/// not. This can return null if the element index is a ConstantExpr, or if
271/// 'this' is a constant expr.
272Constant *Constant::getAggregateElement(unsigned Elt) const {
273 if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(this))
Craig Topperc6207612014-04-09 06:08:46 +0000274 return Elt < CS->getNumOperands() ? CS->getOperand(Elt) : nullptr;
Galina Kistanovafc259902012-07-13 01:25:27 +0000275
Chris Lattner7e683d12012-01-25 06:16:32 +0000276 if (const ConstantArray *CA = dyn_cast<ConstantArray>(this))
Craig Topperc6207612014-04-09 06:08:46 +0000277 return Elt < CA->getNumOperands() ? CA->getOperand(Elt) : nullptr;
Galina Kistanovafc259902012-07-13 01:25:27 +0000278
Chris Lattner7e683d12012-01-25 06:16:32 +0000279 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
Craig Topperc6207612014-04-09 06:08:46 +0000280 return Elt < CV->getNumOperands() ? CV->getOperand(Elt) : nullptr;
Galina Kistanovafc259902012-07-13 01:25:27 +0000281
David Majnemer9b529a72015-02-16 04:02:09 +0000282 if (const ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(this))
283 return Elt < CAZ->getNumElements() ? CAZ->getElementValue(Elt) : nullptr;
Galina Kistanovafc259902012-07-13 01:25:27 +0000284
Chris Lattner7e683d12012-01-25 06:16:32 +0000285 if (const UndefValue *UV = dyn_cast<UndefValue>(this))
David Majnemer9b529a72015-02-16 04:02:09 +0000286 return Elt < UV->getNumElements() ? UV->getElementValue(Elt) : nullptr;
Galina Kistanovafc259902012-07-13 01:25:27 +0000287
Chris Lattner8326bd82012-01-26 00:42:34 +0000288 if (const ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(this))
Craig Topperc6207612014-04-09 06:08:46 +0000289 return Elt < CDS->getNumElements() ? CDS->getElementAsConstant(Elt)
290 : nullptr;
291 return nullptr;
Chris Lattner7e683d12012-01-25 06:16:32 +0000292}
293
294Constant *Constant::getAggregateElement(Constant *Elt) const {
295 assert(isa<IntegerType>(Elt->getType()) && "Index must be an integer");
296 if (ConstantInt *CI = dyn_cast<ConstantInt>(Elt))
297 return getAggregateElement(CI->getZExtValue());
Craig Topperc6207612014-04-09 06:08:46 +0000298 return nullptr;
Chris Lattner7e683d12012-01-25 06:16:32 +0000299}
300
Pete Cooper86dd4cf2015-06-23 21:55:11 +0000301void Constant::destroyConstant() {
302 /// First call destroyConstantImpl on the subclass. This gives the subclass
303 /// a chance to remove the constant from any maps/pools it's contained in.
304 switch (getValueID()) {
305 default:
306 llvm_unreachable("Not a constant!");
307#define HANDLE_CONSTANT(Name) \
308 case Value::Name##Val: \
309 cast<Name>(this)->destroyConstantImpl(); \
310 break;
311#include "llvm/IR/Value.def"
312 }
Chris Lattner7e683d12012-01-25 06:16:32 +0000313
Chris Lattner3462ae32001-12-03 22:26:30 +0000314 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +0000315 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +0000316 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +0000317 // but they don't know that. Because we only find out when the CPV is
318 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +0000319 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +0000320 //
321 while (!use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000322 Value *V = user_back();
Pete Cooper86dd4cf2015-06-23 21:55:11 +0000323#ifndef NDEBUG // Only in -g mode...
Chris Lattner78683a72009-08-23 04:02:03 +0000324 if (!isa<Constant>(V)) {
David Greene1e27a132010-01-05 01:29:19 +0000325 dbgs() << "While deleting: " << *this
Pete Cooper86dd4cf2015-06-23 21:55:11 +0000326 << "\n\nUse still stuck around after Def is destroyed: " << *V
327 << "\n\n";
Chris Lattner78683a72009-08-23 04:02:03 +0000328 }
Chris Lattnerd7a73302001-10-13 06:57:33 +0000329#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000330 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Chris Lattner8326bd82012-01-26 00:42:34 +0000331 cast<Constant>(V)->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +0000332
333 // The constant should remove itself from our use list...
Chandler Carruthcdf47882014-03-09 03:16:01 +0000334 assert((use_empty() || user_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +0000335 }
336
337 // Value has no outstanding references it is safe to delete it now...
338 delete this;
Chris Lattner38569342001-10-01 20:11:19 +0000339}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000340
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000341static bool canTrapImpl(const Constant *C,
Craig Topper71b7b682014-08-21 05:55:13 +0000342 SmallPtrSetImpl<const ConstantExpr *> &NonTrappingOps) {
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000343 assert(C->getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
Chris Lattner23dd1f62006-10-20 00:27:06 +0000344 // The only thing that could possibly trap are constant exprs.
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000345 const ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
346 if (!CE)
347 return false;
Galina Kistanovafc259902012-07-13 01:25:27 +0000348
349 // ConstantExpr traps if any operands can trap.
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000350 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
351 if (ConstantExpr *Op = dyn_cast<ConstantExpr>(CE->getOperand(i))) {
David Blaikie70573dc2014-11-19 07:49:26 +0000352 if (NonTrappingOps.insert(Op).second && canTrapImpl(Op, NonTrappingOps))
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000353 return true;
354 }
355 }
Chris Lattner23dd1f62006-10-20 00:27:06 +0000356
357 // Otherwise, only specific operations can trap.
358 switch (CE->getOpcode()) {
359 default:
360 return false;
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000361 case Instruction::UDiv:
362 case Instruction::SDiv:
363 case Instruction::FDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +0000364 case Instruction::URem:
365 case Instruction::SRem:
366 case Instruction::FRem:
Chris Lattner23dd1f62006-10-20 00:27:06 +0000367 // Div and rem can trap if the RHS is not known to be non-zero.
Chris Lattnera91a5632009-10-28 05:14:34 +0000368 if (!isa<ConstantInt>(CE->getOperand(1)) ||CE->getOperand(1)->isNullValue())
Chris Lattner23dd1f62006-10-20 00:27:06 +0000369 return true;
370 return false;
371 }
372}
373
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000374/// canTrap - Return true if evaluation of this constant could trap. This is
375/// true for things like constant expressions that could divide by zero.
376bool Constant::canTrap() const {
377 SmallPtrSet<const ConstantExpr *, 4> NonTrappingOps;
378 return canTrapImpl(this, NonTrappingOps);
379}
380
Hans Wennborg4dc89512014-06-20 00:38:12 +0000381/// Check if C contains a GlobalValue for which Predicate is true.
382static bool
383ConstHasGlobalValuePredicate(const Constant *C,
384 bool (*Predicate)(const GlobalValue *)) {
385 SmallPtrSet<const Constant *, 8> Visited;
386 SmallVector<const Constant *, 8> WorkList;
387 WorkList.push_back(C);
388 Visited.insert(C);
Hans Wennborg709e0152012-11-15 11:40:00 +0000389
390 while (!WorkList.empty()) {
Hans Wennborg4dc89512014-06-20 00:38:12 +0000391 const Constant *WorkItem = WorkList.pop_back_val();
392 if (const auto *GV = dyn_cast<GlobalValue>(WorkItem))
393 if (Predicate(GV))
Hans Wennborg709e0152012-11-15 11:40:00 +0000394 return true;
Hans Wennborg4dc89512014-06-20 00:38:12 +0000395 for (const Value *Op : WorkItem->operands()) {
396 const Constant *ConstOp = dyn_cast<Constant>(Op);
397 if (!ConstOp)
Hans Wennborg18aa12402012-11-16 10:33:25 +0000398 continue;
David Blaikie70573dc2014-11-19 07:49:26 +0000399 if (Visited.insert(ConstOp).second)
Hans Wennborg4dc89512014-06-20 00:38:12 +0000400 WorkList.push_back(ConstOp);
Hans Wennborg709e0152012-11-15 11:40:00 +0000401 }
402 }
Hans Wennborg709e0152012-11-15 11:40:00 +0000403 return false;
404}
405
Hans Wennborg4dc89512014-06-20 00:38:12 +0000406/// Return true if the value can vary between threads.
407bool Constant::isThreadDependent() const {
408 auto DLLImportPredicate = [](const GlobalValue *GV) {
409 return GV->isThreadLocal();
410 };
411 return ConstHasGlobalValuePredicate(this, DLLImportPredicate);
412}
413
414bool Constant::isDLLImportDependent() const {
415 auto DLLImportPredicate = [](const GlobalValue *GV) {
416 return GV->hasDLLImportStorageClass();
417 };
418 return ConstHasGlobalValuePredicate(this, DLLImportPredicate);
419}
420
421/// Return true if the constant has users other than constant exprs and other
422/// dangling things.
Chris Lattner253bc772009-11-01 18:11:50 +0000423bool Constant::isConstantUsed() const {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000424 for (const User *U : users()) {
425 const Constant *UC = dyn_cast<Constant>(U);
Craig Topperc6207612014-04-09 06:08:46 +0000426 if (!UC || isa<GlobalValue>(UC))
Chris Lattner253bc772009-11-01 18:11:50 +0000427 return true;
Galina Kistanovafc259902012-07-13 01:25:27 +0000428
Chris Lattner253bc772009-11-01 18:11:50 +0000429 if (UC->isConstantUsed())
430 return true;
431 }
432 return false;
433}
434
Rafael Espindola65e49022015-11-17 00:51:23 +0000435bool Constant::needsRelocation() const {
436 if (isa<GlobalValue>(this))
437 return true; // Global reference.
438
Chris Lattner2cb85b42009-10-28 04:12:16 +0000439 if (const BlockAddress *BA = dyn_cast<BlockAddress>(this))
Rafael Espindola65e49022015-11-17 00:51:23 +0000440 return BA->getFunction()->needsRelocation();
441
Chris Lattnera7cfc432010-01-03 18:09:40 +0000442 // While raw uses of blockaddress need to be relocated, differences between
443 // two of them don't when they are for labels in the same function. This is a
444 // common idiom when creating a table for the indirect goto extension, so we
445 // handle it efficiently here.
446 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(this))
447 if (CE->getOpcode() == Instruction::Sub) {
448 ConstantExpr *LHS = dyn_cast<ConstantExpr>(CE->getOperand(0));
449 ConstantExpr *RHS = dyn_cast<ConstantExpr>(CE->getOperand(1));
Rafael Espindola65e49022015-11-17 00:51:23 +0000450 if (LHS && RHS && LHS->getOpcode() == Instruction::PtrToInt &&
Chris Lattnera7cfc432010-01-03 18:09:40 +0000451 RHS->getOpcode() == Instruction::PtrToInt &&
452 isa<BlockAddress>(LHS->getOperand(0)) &&
453 isa<BlockAddress>(RHS->getOperand(0)) &&
454 cast<BlockAddress>(LHS->getOperand(0))->getFunction() ==
Rafael Espindola65e49022015-11-17 00:51:23 +0000455 cast<BlockAddress>(RHS->getOperand(0))->getFunction())
456 return false;
Chris Lattnera7cfc432010-01-03 18:09:40 +0000457 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000458
Rafael Espindola65e49022015-11-17 00:51:23 +0000459 bool Result = false;
Evan Chengf9e003b2007-03-08 00:59:12 +0000460 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Rafael Espindola65e49022015-11-17 00:51:23 +0000461 Result |= cast<Constant>(getOperand(i))->needsRelocation();
Galina Kistanovafc259902012-07-13 01:25:27 +0000462
Chris Lattner4565ef52009-07-22 00:05:44 +0000463 return Result;
Evan Chengf9e003b2007-03-08 00:59:12 +0000464}
465
Chris Lattner84886402011-02-18 04:41:42 +0000466/// removeDeadUsersOfConstant - If the specified constantexpr is dead, remove
467/// it. This involves recursively eliminating any dead users of the
468/// constantexpr.
469static bool removeDeadUsersOfConstant(const Constant *C) {
470 if (isa<GlobalValue>(C)) return false; // Cannot remove this
Galina Kistanovafc259902012-07-13 01:25:27 +0000471
Chris Lattner84886402011-02-18 04:41:42 +0000472 while (!C->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000473 const Constant *User = dyn_cast<Constant>(C->user_back());
Chris Lattner84886402011-02-18 04:41:42 +0000474 if (!User) return false; // Non-constant usage;
475 if (!removeDeadUsersOfConstant(User))
476 return false; // Constant wasn't dead
477 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000478
Chris Lattner84886402011-02-18 04:41:42 +0000479 const_cast<Constant*>(C)->destroyConstant();
480 return true;
481}
482
483
484/// removeDeadConstantUsers - If there are any dead constant users dangling
485/// off of this constant, remove them. This method is useful for clients
486/// that want to check to see if a global is unused, but don't want to deal
487/// with potentially dead constants hanging off of the globals.
488void Constant::removeDeadConstantUsers() const {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000489 Value::const_user_iterator I = user_begin(), E = user_end();
490 Value::const_user_iterator LastNonDeadUser = E;
Chris Lattner84886402011-02-18 04:41:42 +0000491 while (I != E) {
492 const Constant *User = dyn_cast<Constant>(*I);
Craig Topperc6207612014-04-09 06:08:46 +0000493 if (!User) {
Chris Lattner84886402011-02-18 04:41:42 +0000494 LastNonDeadUser = I;
495 ++I;
496 continue;
497 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000498
Chris Lattner84886402011-02-18 04:41:42 +0000499 if (!removeDeadUsersOfConstant(User)) {
500 // If the constant wasn't dead, remember that this was the last live use
501 // and move on to the next constant.
502 LastNonDeadUser = I;
503 ++I;
504 continue;
505 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000506
Chris Lattner84886402011-02-18 04:41:42 +0000507 // If the constant was dead, then the iterator is invalidated.
508 if (LastNonDeadUser == E) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000509 I = user_begin();
Chris Lattner84886402011-02-18 04:41:42 +0000510 if (I == E) break;
511 } else {
512 I = LastNonDeadUser;
513 ++I;
514 }
515 }
516}
517
518
Chris Lattner2105d662008-07-10 00:28:11 +0000519
Chris Lattner2f7c9632001-06-06 20:29:01 +0000520//===----------------------------------------------------------------------===//
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000521// ConstantInt
Chris Lattner2f7c9632001-06-06 20:29:01 +0000522//===----------------------------------------------------------------------===//
523
David Blaikiea379b1812011-12-20 02:50:00 +0000524void ConstantInt::anchor() { }
525
Duncan P. N. Exon Smithb6452792016-02-21 02:39:49 +0000526ConstantInt::ConstantInt(IntegerType *Ty, const APInt &V)
527 : ConstantData(Ty, ConstantIntVal), Val(V) {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000528 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000529}
530
Nick Lewycky92db8e82011-03-06 03:36:19 +0000531ConstantInt *ConstantInt::getTrue(LLVMContext &Context) {
Owen Anderson23a204d2009-07-31 17:39:07 +0000532 LLVMContextImpl *pImpl = Context.pImpl;
Benjamin Kramerddd1b7b2010-11-20 18:43:35 +0000533 if (!pImpl->TheTrueVal)
534 pImpl->TheTrueVal = ConstantInt::get(Type::getInt1Ty(Context), 1);
535 return pImpl->TheTrueVal;
Owen Anderson23a204d2009-07-31 17:39:07 +0000536}
537
Nick Lewycky92db8e82011-03-06 03:36:19 +0000538ConstantInt *ConstantInt::getFalse(LLVMContext &Context) {
Owen Anderson23a204d2009-07-31 17:39:07 +0000539 LLVMContextImpl *pImpl = Context.pImpl;
Benjamin Kramerddd1b7b2010-11-20 18:43:35 +0000540 if (!pImpl->TheFalseVal)
541 pImpl->TheFalseVal = ConstantInt::get(Type::getInt1Ty(Context), 0);
542 return pImpl->TheFalseVal;
Owen Anderson23a204d2009-07-31 17:39:07 +0000543}
544
Chris Lattner229907c2011-07-18 04:54:35 +0000545Constant *ConstantInt::getTrue(Type *Ty) {
546 VectorType *VTy = dyn_cast<VectorType>(Ty);
Nick Lewycky92db8e82011-03-06 03:36:19 +0000547 if (!VTy) {
548 assert(Ty->isIntegerTy(1) && "True must be i1 or vector of i1.");
549 return ConstantInt::getTrue(Ty->getContext());
550 }
551 assert(VTy->getElementType()->isIntegerTy(1) &&
552 "True must be vector of i1 or i1.");
Chris Lattnere9eed292012-01-25 05:19:54 +0000553 return ConstantVector::getSplat(VTy->getNumElements(),
554 ConstantInt::getTrue(Ty->getContext()));
Nick Lewycky92db8e82011-03-06 03:36:19 +0000555}
556
Chris Lattner229907c2011-07-18 04:54:35 +0000557Constant *ConstantInt::getFalse(Type *Ty) {
558 VectorType *VTy = dyn_cast<VectorType>(Ty);
Nick Lewycky92db8e82011-03-06 03:36:19 +0000559 if (!VTy) {
560 assert(Ty->isIntegerTy(1) && "False must be i1 or vector of i1.");
561 return ConstantInt::getFalse(Ty->getContext());
562 }
563 assert(VTy->getElementType()->isIntegerTy(1) &&
564 "False must be vector of i1 or i1.");
Chris Lattnere9eed292012-01-25 05:19:54 +0000565 return ConstantVector::getSplat(VTy->getNumElements(),
566 ConstantInt::getFalse(Ty->getContext()));
Nick Lewycky92db8e82011-03-06 03:36:19 +0000567}
568
Benjamin Kramer8e5dc532014-12-06 13:12:56 +0000569// Get a ConstantInt from an APInt.
Nick Lewycky92db8e82011-03-06 03:36:19 +0000570ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt &V) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000571 // get an existing value or the insertion position
Benjamin Kramer320682f2013-06-01 17:51:03 +0000572 LLVMContextImpl *pImpl = Context.pImpl;
Benjamin Kramer8e5dc532014-12-06 13:12:56 +0000573 ConstantInt *&Slot = pImpl->IntConstants[V];
574 if (!Slot) {
575 // Get the corresponding integer type for the bit width of the value.
576 IntegerType *ITy = IntegerType::get(Context, V.getBitWidth());
NAKAMURA Takumifc3062f2014-12-06 05:57:06 +0000577 Slot = new ConstantInt(ITy, V);
Benjamin Kramer8e5dc532014-12-06 13:12:56 +0000578 }
579 assert(Slot->getType() == IntegerType::get(Context, V.getBitWidth()));
Owen Anderson5dab84c2009-10-19 20:11:52 +0000580 return Slot;
Owen Andersonedb4a702009-07-24 23:12:02 +0000581}
582
Chris Lattner229907c2011-07-18 04:54:35 +0000583Constant *ConstantInt::get(Type *Ty, uint64_t V, bool isSigned) {
Nick Lewycky92db8e82011-03-06 03:36:19 +0000584 Constant *C = get(cast<IntegerType>(Ty->getScalarType()), V, isSigned);
Owen Andersonedb4a702009-07-24 23:12:02 +0000585
586 // For vectors, broadcast the value.
Chris Lattner229907c2011-07-18 04:54:35 +0000587 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
Chris Lattnere9eed292012-01-25 05:19:54 +0000588 return ConstantVector::getSplat(VTy->getNumElements(), C);
Owen Andersonedb4a702009-07-24 23:12:02 +0000589
590 return C;
591}
592
Chris Lattner0256be92012-01-27 03:08:05 +0000593ConstantInt *ConstantInt::get(IntegerType *Ty, uint64_t V,
Owen Andersonedb4a702009-07-24 23:12:02 +0000594 bool isSigned) {
595 return get(Ty->getContext(), APInt(Ty->getBitWidth(), V, isSigned));
596}
597
Chris Lattner0256be92012-01-27 03:08:05 +0000598ConstantInt *ConstantInt::getSigned(IntegerType *Ty, int64_t V) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000599 return get(Ty, V, true);
600}
601
Chris Lattner229907c2011-07-18 04:54:35 +0000602Constant *ConstantInt::getSigned(Type *Ty, int64_t V) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000603 return get(Ty, V, true);
604}
605
Chris Lattner0256be92012-01-27 03:08:05 +0000606Constant *ConstantInt::get(Type *Ty, const APInt& V) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000607 ConstantInt *C = get(Ty->getContext(), V);
608 assert(C->getType() == Ty->getScalarType() &&
609 "ConstantInt type doesn't match the type implied by its value!");
610
611 // For vectors, broadcast the value.
Chris Lattner229907c2011-07-18 04:54:35 +0000612 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
Chris Lattnere9eed292012-01-25 05:19:54 +0000613 return ConstantVector::getSplat(VTy->getNumElements(), C);
Owen Andersonedb4a702009-07-24 23:12:02 +0000614
615 return C;
616}
617
Chris Lattner0256be92012-01-27 03:08:05 +0000618ConstantInt *ConstantInt::get(IntegerType* Ty, StringRef Str,
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000619 uint8_t radix) {
620 return get(Ty->getContext(), APInt(Ty->getBitWidth(), Str, radix));
621}
622
Pete Cooper86dd4cf2015-06-23 21:55:11 +0000623/// Remove the constant from the constant table.
624void ConstantInt::destroyConstantImpl() {
625 llvm_unreachable("You can't ConstantInt->destroyConstantImpl()!");
626}
627
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000628//===----------------------------------------------------------------------===//
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000629// ConstantFP
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000630//===----------------------------------------------------------------------===//
631
Chris Lattner229907c2011-07-18 04:54:35 +0000632static const fltSemantics *TypeToFloatSemantics(Type *Ty) {
Dan Gohman518cda42011-12-17 00:04:22 +0000633 if (Ty->isHalfTy())
634 return &APFloat::IEEEhalf;
Chris Lattnerfdd87902009-10-05 05:54:46 +0000635 if (Ty->isFloatTy())
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000636 return &APFloat::IEEEsingle;
Chris Lattnerfdd87902009-10-05 05:54:46 +0000637 if (Ty->isDoubleTy())
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000638 return &APFloat::IEEEdouble;
Chris Lattnerfdd87902009-10-05 05:54:46 +0000639 if (Ty->isX86_FP80Ty())
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000640 return &APFloat::x87DoubleExtended;
Chris Lattnerfdd87902009-10-05 05:54:46 +0000641 else if (Ty->isFP128Ty())
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000642 return &APFloat::IEEEquad;
Galina Kistanovafc259902012-07-13 01:25:27 +0000643
Chris Lattnerfdd87902009-10-05 05:54:46 +0000644 assert(Ty->isPPC_FP128Ty() && "Unknown FP format");
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000645 return &APFloat::PPCDoubleDouble;
646}
647
David Blaikiea379b1812011-12-20 02:50:00 +0000648void ConstantFP::anchor() { }
649
Owen Anderson69c464d2009-07-27 20:59:43 +0000650/// get() - This returns a constant fp for the specified value in the
651/// specified type. This should only be used for simple constant values like
652/// 2.0/1.0 etc, that are known-valid both as double and as the target format.
Chris Lattner0256be92012-01-27 03:08:05 +0000653Constant *ConstantFP::get(Type *Ty, double V) {
Owen Anderson69c464d2009-07-27 20:59:43 +0000654 LLVMContext &Context = Ty->getContext();
Galina Kistanovafc259902012-07-13 01:25:27 +0000655
Owen Anderson69c464d2009-07-27 20:59:43 +0000656 APFloat FV(V);
657 bool ignored;
658 FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
659 APFloat::rmNearestTiesToEven, &ignored);
660 Constant *C = get(Context, FV);
661
662 // For vectors, broadcast the value.
Chris Lattner229907c2011-07-18 04:54:35 +0000663 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
Chris Lattnere9eed292012-01-25 05:19:54 +0000664 return ConstantVector::getSplat(VTy->getNumElements(), C);
Owen Anderson69c464d2009-07-27 20:59:43 +0000665
666 return C;
667}
668
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000669
Chris Lattner0256be92012-01-27 03:08:05 +0000670Constant *ConstantFP::get(Type *Ty, StringRef Str) {
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000671 LLVMContext &Context = Ty->getContext();
672
673 APFloat FV(*TypeToFloatSemantics(Ty->getScalarType()), Str);
674 Constant *C = get(Context, FV);
675
676 // For vectors, broadcast the value.
Chris Lattner229907c2011-07-18 04:54:35 +0000677 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
Chris Lattnere9eed292012-01-25 05:19:54 +0000678 return ConstantVector::getSplat(VTy->getNumElements(), C);
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000679
680 return C;
681}
682
Tom Stellard67246d12015-04-20 19:38:24 +0000683Constant *ConstantFP::getNaN(Type *Ty, bool Negative, unsigned Type) {
684 const fltSemantics &Semantics = *TypeToFloatSemantics(Ty->getScalarType());
685 APFloat NaN = APFloat::getNaN(Semantics, Negative, Type);
686 Constant *C = get(Ty->getContext(), NaN);
687
688 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
689 return ConstantVector::getSplat(VTy->getNumElements(), C);
690
691 return C;
692}
693
Benjamin Kramer5d2ff222014-01-18 16:43:06 +0000694Constant *ConstantFP::getNegativeZero(Type *Ty) {
695 const fltSemantics &Semantics = *TypeToFloatSemantics(Ty->getScalarType());
696 APFloat NegZero = APFloat::getZero(Semantics, /*Negative=*/true);
697 Constant *C = get(Ty->getContext(), NegZero);
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000698
Benjamin Kramer5d2ff222014-01-18 16:43:06 +0000699 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
700 return ConstantVector::getSplat(VTy->getNumElements(), C);
701
702 return C;
Owen Anderson69c464d2009-07-27 20:59:43 +0000703}
704
705
Chris Lattnere9eed292012-01-25 05:19:54 +0000706Constant *ConstantFP::getZeroValueForNegation(Type *Ty) {
Benjamin Kramer5d2ff222014-01-18 16:43:06 +0000707 if (Ty->isFPOrFPVectorTy())
708 return getNegativeZero(Ty);
Owen Anderson69c464d2009-07-27 20:59:43 +0000709
Owen Anderson5a1acd92009-07-31 20:28:14 +0000710 return Constant::getNullValue(Ty);
Owen Anderson69c464d2009-07-27 20:59:43 +0000711}
712
713
714// ConstantFP accessors.
715ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) {
Owen Anderson69c464d2009-07-27 20:59:43 +0000716 LLVMContextImpl* pImpl = Context.pImpl;
Galina Kistanovafc259902012-07-13 01:25:27 +0000717
Benjamin Kramer8e5dc532014-12-06 13:12:56 +0000718 ConstantFP *&Slot = pImpl->FPConstants[V];
Galina Kistanovafc259902012-07-13 01:25:27 +0000719
Owen Anderson69c464d2009-07-27 20:59:43 +0000720 if (!Slot) {
Chris Lattner229907c2011-07-18 04:54:35 +0000721 Type *Ty;
Dan Gohman518cda42011-12-17 00:04:22 +0000722 if (&V.getSemantics() == &APFloat::IEEEhalf)
723 Ty = Type::getHalfTy(Context);
724 else if (&V.getSemantics() == &APFloat::IEEEsingle)
Owen Anderson5dab84c2009-10-19 20:11:52 +0000725 Ty = Type::getFloatTy(Context);
726 else if (&V.getSemantics() == &APFloat::IEEEdouble)
727 Ty = Type::getDoubleTy(Context);
728 else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
729 Ty = Type::getX86_FP80Ty(Context);
730 else if (&V.getSemantics() == &APFloat::IEEEquad)
731 Ty = Type::getFP128Ty(Context);
732 else {
733 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble &&
734 "Unknown FP format");
735 Ty = Type::getPPC_FP128Ty(Context);
Owen Anderson69c464d2009-07-27 20:59:43 +0000736 }
Owen Anderson5dab84c2009-10-19 20:11:52 +0000737 Slot = new ConstantFP(Ty, V);
Owen Anderson69c464d2009-07-27 20:59:43 +0000738 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000739
Owen Anderson69c464d2009-07-27 20:59:43 +0000740 return Slot;
741}
742
Benjamin Kramer5d2ff222014-01-18 16:43:06 +0000743Constant *ConstantFP::getInfinity(Type *Ty, bool Negative) {
744 const fltSemantics &Semantics = *TypeToFloatSemantics(Ty->getScalarType());
745 Constant *C = get(Ty->getContext(), APFloat::getInf(Semantics, Negative));
746
747 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
748 return ConstantVector::getSplat(VTy->getNumElements(), C);
749
750 return C;
Dan Gohmanfeb50212009-09-25 23:00:48 +0000751}
752
Duncan P. N. Exon Smithb6452792016-02-21 02:39:49 +0000753ConstantFP::ConstantFP(Type *Ty, const APFloat &V)
754 : ConstantData(Ty, ConstantFPVal), Val(V) {
Chris Lattner98bd9392008-04-09 06:38:30 +0000755 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
756 "FP type Mismatch");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000757}
758
Chris Lattnerbe6610c2011-07-15 06:14:08 +0000759bool ConstantFP::isExactlyValue(const APFloat &V) const {
Dale Johannesend246b2c2007-08-30 00:23:21 +0000760 return Val.bitwiseIsEqual(V);
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000761}
762
Pete Cooper86dd4cf2015-06-23 21:55:11 +0000763/// Remove the constant from the constant table.
764void ConstantFP::destroyConstantImpl() {
765 llvm_unreachable("You can't ConstantInt->destroyConstantImpl()!");
766}
767
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000768//===----------------------------------------------------------------------===//
Chris Lattner030af792012-01-24 05:42:11 +0000769// ConstantAggregateZero Implementation
770//===----------------------------------------------------------------------===//
771
772/// getSequentialElement - If this CAZ has array or vector type, return a zero
773/// with the right element type.
Chris Lattner7e683d12012-01-25 06:16:32 +0000774Constant *ConstantAggregateZero::getSequentialElement() const {
Chris Lattner8326bd82012-01-26 00:42:34 +0000775 return Constant::getNullValue(getType()->getSequentialElementType());
Chris Lattner030af792012-01-24 05:42:11 +0000776}
777
778/// getStructElement - If this CAZ has struct type, return a zero with the
779/// right element type for the specified element.
Chris Lattner7e683d12012-01-25 06:16:32 +0000780Constant *ConstantAggregateZero::getStructElement(unsigned Elt) const {
Chris Lattner8326bd82012-01-26 00:42:34 +0000781 return Constant::getNullValue(getType()->getStructElementType(Elt));
Chris Lattner030af792012-01-24 05:42:11 +0000782}
783
784/// getElementValue - Return a zero of the right value for the specified GEP
785/// index if we can, otherwise return null (e.g. if C is a ConstantExpr).
Chris Lattner7e683d12012-01-25 06:16:32 +0000786Constant *ConstantAggregateZero::getElementValue(Constant *C) const {
Chris Lattner030af792012-01-24 05:42:11 +0000787 if (isa<SequentialType>(getType()))
788 return getSequentialElement();
789 return getStructElement(cast<ConstantInt>(C)->getZExtValue());
790}
791
Chris Lattnerf7eb5432012-01-24 07:54:10 +0000792/// getElementValue - Return a zero of the right value for the specified GEP
793/// index.
Chris Lattner7e683d12012-01-25 06:16:32 +0000794Constant *ConstantAggregateZero::getElementValue(unsigned Idx) const {
Chris Lattnerf7eb5432012-01-24 07:54:10 +0000795 if (isa<SequentialType>(getType()))
796 return getSequentialElement();
797 return getStructElement(Idx);
798}
799
David Majnemer9b529a72015-02-16 04:02:09 +0000800unsigned ConstantAggregateZero::getNumElements() const {
Craig Toppere3dcce92015-08-01 22:20:21 +0000801 Type *Ty = getType();
802 if (auto *AT = dyn_cast<ArrayType>(Ty))
David Majnemer9b529a72015-02-16 04:02:09 +0000803 return AT->getNumElements();
Craig Toppere3dcce92015-08-01 22:20:21 +0000804 if (auto *VT = dyn_cast<VectorType>(Ty))
David Majnemer9b529a72015-02-16 04:02:09 +0000805 return VT->getNumElements();
806 return Ty->getStructNumElements();
807}
Chris Lattnerf7eb5432012-01-24 07:54:10 +0000808
Chris Lattner030af792012-01-24 05:42:11 +0000809//===----------------------------------------------------------------------===//
810// UndefValue Implementation
811//===----------------------------------------------------------------------===//
812
813/// getSequentialElement - If this undef has array or vector type, return an
814/// undef with the right element type.
Chris Lattner7e683d12012-01-25 06:16:32 +0000815UndefValue *UndefValue::getSequentialElement() const {
Chris Lattner8326bd82012-01-26 00:42:34 +0000816 return UndefValue::get(getType()->getSequentialElementType());
Chris Lattner030af792012-01-24 05:42:11 +0000817}
818
819/// getStructElement - If this undef has struct type, return a zero with the
820/// right element type for the specified element.
Chris Lattner7e683d12012-01-25 06:16:32 +0000821UndefValue *UndefValue::getStructElement(unsigned Elt) const {
Chris Lattner8326bd82012-01-26 00:42:34 +0000822 return UndefValue::get(getType()->getStructElementType(Elt));
Chris Lattner030af792012-01-24 05:42:11 +0000823}
824
825/// getElementValue - Return an undef of the right value for the specified GEP
826/// index if we can, otherwise return null (e.g. if C is a ConstantExpr).
Chris Lattner7e683d12012-01-25 06:16:32 +0000827UndefValue *UndefValue::getElementValue(Constant *C) const {
Chris Lattner030af792012-01-24 05:42:11 +0000828 if (isa<SequentialType>(getType()))
829 return getSequentialElement();
830 return getStructElement(cast<ConstantInt>(C)->getZExtValue());
831}
832
Chris Lattnerf7eb5432012-01-24 07:54:10 +0000833/// getElementValue - Return an undef of the right value for the specified GEP
834/// index.
Chris Lattner7e683d12012-01-25 06:16:32 +0000835UndefValue *UndefValue::getElementValue(unsigned Idx) const {
Chris Lattnerf7eb5432012-01-24 07:54:10 +0000836 if (isa<SequentialType>(getType()))
837 return getSequentialElement();
838 return getStructElement(Idx);
839}
840
David Majnemer9b529a72015-02-16 04:02:09 +0000841unsigned UndefValue::getNumElements() const {
Craig Toppere3dcce92015-08-01 22:20:21 +0000842 Type *Ty = getType();
843 if (auto *AT = dyn_cast<ArrayType>(Ty))
David Majnemer9b529a72015-02-16 04:02:09 +0000844 return AT->getNumElements();
Craig Toppere3dcce92015-08-01 22:20:21 +0000845 if (auto *VT = dyn_cast<VectorType>(Ty))
David Majnemer9b529a72015-02-16 04:02:09 +0000846 return VT->getNumElements();
847 return Ty->getStructNumElements();
848}
Chris Lattner030af792012-01-24 05:42:11 +0000849
850//===----------------------------------------------------------------------===//
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000851// ConstantXXX Classes
852//===----------------------------------------------------------------------===//
853
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000854template <typename ItTy, typename EltTy>
855static bool rangeOnlyContains(ItTy Start, ItTy End, EltTy Elt) {
856 for (; Start != End; ++Start)
857 if (*Start != Elt)
858 return false;
859 return true;
860}
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000861
Justin Bogner909e1c02015-12-01 20:20:49 +0000862template <typename SequentialTy, typename ElementTy>
863static Constant *getIntSequenceIfElementsMatch(ArrayRef<Constant *> V) {
864 assert(!V.empty() && "Cannot get empty int sequence.");
865
866 SmallVector<ElementTy, 16> Elts;
867 for (Constant *C : V)
868 if (auto *CI = dyn_cast<ConstantInt>(C))
869 Elts.push_back(CI->getZExtValue());
870 else
871 return nullptr;
872 return SequentialTy::get(V[0]->getContext(), Elts);
873}
874
875template <typename SequentialTy, typename ElementTy>
876static Constant *getFPSequenceIfElementsMatch(ArrayRef<Constant *> V) {
877 assert(!V.empty() && "Cannot get empty FP sequence.");
878
879 SmallVector<ElementTy, 16> Elts;
880 for (Constant *C : V)
881 if (auto *CFP = dyn_cast<ConstantFP>(C))
882 Elts.push_back(CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
883 else
884 return nullptr;
885 return SequentialTy::getFP(V[0]->getContext(), Elts);
886}
887
888template <typename SequenceTy>
889static Constant *getSequenceIfElementsMatch(Constant *C,
890 ArrayRef<Constant *> V) {
891 // We speculatively build the elements here even if it turns out that there is
892 // a constantexpr or something else weird, since it is so uncommon for that to
893 // happen.
894 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
895 if (CI->getType()->isIntegerTy(8))
896 return getIntSequenceIfElementsMatch<SequenceTy, uint8_t>(V);
897 else if (CI->getType()->isIntegerTy(16))
898 return getIntSequenceIfElementsMatch<SequenceTy, uint16_t>(V);
899 else if (CI->getType()->isIntegerTy(32))
900 return getIntSequenceIfElementsMatch<SequenceTy, uint32_t>(V);
901 else if (CI->getType()->isIntegerTy(64))
902 return getIntSequenceIfElementsMatch<SequenceTy, uint64_t>(V);
903 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
Justin Bogner0ebc8602015-12-08 03:01:16 +0000904 if (CFP->getType()->isHalfTy())
905 return getFPSequenceIfElementsMatch<SequenceTy, uint16_t>(V);
906 else if (CFP->getType()->isFloatTy())
Justin Bogner909e1c02015-12-01 20:20:49 +0000907 return getFPSequenceIfElementsMatch<SequenceTy, uint32_t>(V);
908 else if (CFP->getType()->isDoubleTy())
909 return getFPSequenceIfElementsMatch<SequenceTy, uint64_t>(V);
910 }
911
912 return nullptr;
913}
914
Jay Foad89d9b812011-07-25 10:14:44 +0000915ConstantArray::ConstantArray(ArrayType *T, ArrayRef<Constant *> V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000916 : Constant(T, ConstantArrayVal,
917 OperandTraits<ConstantArray>::op_end(this) - V.size(),
918 V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000919 assert(V.size() == T->getNumElements() &&
920 "Invalid initializer vector for constant array");
Jay Foad89d9b812011-07-25 10:14:44 +0000921 for (unsigned i = 0, e = V.size(); i != e; ++i)
922 assert(V[i]->getType() == T->getElementType() &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000923 "Initializer for array element doesn't match array element type!");
Jay Foad89d9b812011-07-25 10:14:44 +0000924 std::copy(V.begin(), V.end(), op_begin());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000925}
926
Chris Lattner229907c2011-07-18 04:54:35 +0000927Constant *ConstantArray::get(ArrayType *Ty, ArrayRef<Constant*> V) {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000928 if (Constant *C = getImpl(Ty, V))
929 return C;
930 return Ty->getContext().pImpl->ArrayConstants.getOrCreate(Ty, V);
931}
Justin Bogner909e1c02015-12-01 20:20:49 +0000932
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000933Constant *ConstantArray::getImpl(ArrayType *Ty, ArrayRef<Constant*> V) {
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000934 // Empty arrays are canonicalized to ConstantAggregateZero.
935 if (V.empty())
936 return ConstantAggregateZero::get(Ty);
937
Jeffrey Yasskin8ce67f82009-09-30 21:08:08 +0000938 for (unsigned i = 0, e = V.size(); i != e; ++i) {
939 assert(V[i]->getType() == Ty->getElementType() &&
940 "Wrong type in array element initializer");
941 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000942
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000943 // If this is an all-zero array, return a ConstantAggregateZero object. If
944 // all undef, return an UndefValue, if "all simple", then return a
945 // ConstantDataArray.
946 Constant *C = V[0];
947 if (isa<UndefValue>(C) && rangeOnlyContains(V.begin(), V.end(), C))
948 return UndefValue::get(Ty);
Chris Lattnerf14a67f2012-01-26 02:31:22 +0000949
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000950 if (C->isNullValue() && rangeOnlyContains(V.begin(), V.end(), C))
951 return ConstantAggregateZero::get(Ty);
952
953 // Check to see if all of the elements are ConstantFP or ConstantInt and if
954 // the element type is compatible with ConstantDataVector. If so, use it.
Justin Bogner909e1c02015-12-01 20:20:49 +0000955 if (ConstantDataSequential::isElementTypeCompatible(C->getType()))
956 return getSequenceIfElementsMatch<ConstantDataArray>(C, V);
Chris Lattnerf14a67f2012-01-26 02:31:22 +0000957
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000958 // Otherwise, we really do want to create a ConstantArray.
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000959 return nullptr;
Owen Andersonc2c79322009-07-28 18:32:17 +0000960}
961
Chris Lattnercc19efa2011-06-20 04:01:31 +0000962/// getTypeForElements - Return an anonymous struct type to use for a constant
963/// with the specified set of elements. The list must not be empty.
964StructType *ConstantStruct::getTypeForElements(LLVMContext &Context,
965 ArrayRef<Constant*> V,
966 bool Packed) {
Bill Wendling3ae7dd32012-02-07 01:27:51 +0000967 unsigned VecSize = V.size();
968 SmallVector<Type*, 16> EltTypes(VecSize);
969 for (unsigned i = 0; i != VecSize; ++i)
970 EltTypes[i] = V[i]->getType();
Galina Kistanovafc259902012-07-13 01:25:27 +0000971
Chris Lattnercc19efa2011-06-20 04:01:31 +0000972 return StructType::get(Context, EltTypes, Packed);
973}
974
975
976StructType *ConstantStruct::getTypeForElements(ArrayRef<Constant*> V,
977 bool Packed) {
978 assert(!V.empty() &&
979 "ConstantStruct::getTypeForElements cannot be called on empty list");
980 return getTypeForElements(V[0]->getContext(), V, Packed);
981}
982
983
Jay Foad89d9b812011-07-25 10:14:44 +0000984ConstantStruct::ConstantStruct(StructType *T, ArrayRef<Constant *> V)
Gabor Greiff6caff662008-05-10 08:32:32 +0000985 : Constant(T, ConstantStructVal,
986 OperandTraits<ConstantStruct>::op_end(this) - V.size(),
987 V.size()) {
Chris Lattnerc3e74cd2011-08-07 04:18:48 +0000988 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000989 "Invalid initializer vector for constant structure");
Jay Foad89d9b812011-07-25 10:14:44 +0000990 for (unsigned i = 0, e = V.size(); i != e; ++i)
991 assert((T->isOpaque() || V[i]->getType() == T->getElementType(i)) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000992 "Initializer for struct element doesn't match struct element type!");
Jay Foad89d9b812011-07-25 10:14:44 +0000993 std::copy(V.begin(), V.end(), op_begin());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000994}
995
Owen Anderson45308b52009-07-27 22:29:26 +0000996// ConstantStruct accessors.
Chris Lattner229907c2011-07-18 04:54:35 +0000997Constant *ConstantStruct::get(StructType *ST, ArrayRef<Constant*> V) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000998 assert((ST->isOpaque() || ST->getNumElements() == V.size()) &&
999 "Incorrect # elements specified to ConstantStruct::get");
Chris Lattnerf14a67f2012-01-26 02:31:22 +00001000
1001 // Create a ConstantAggregateZero value if all elements are zeros.
1002 bool isZero = true;
1003 bool isUndef = false;
1004
1005 if (!V.empty()) {
1006 isUndef = isa<UndefValue>(V[0]);
1007 isZero = V[0]->isNullValue();
1008 if (isUndef || isZero) {
1009 for (unsigned i = 0, e = V.size(); i != e; ++i) {
1010 if (!V[i]->isNullValue())
1011 isZero = false;
1012 if (!isa<UndefValue>(V[i]))
1013 isUndef = false;
1014 }
1015 }
Galina Kistanovafc259902012-07-13 01:25:27 +00001016 }
Chris Lattnerf14a67f2012-01-26 02:31:22 +00001017 if (isZero)
1018 return ConstantAggregateZero::get(ST);
1019 if (isUndef)
1020 return UndefValue::get(ST);
Galina Kistanovafc259902012-07-13 01:25:27 +00001021
Chris Lattnerf14a67f2012-01-26 02:31:22 +00001022 return ST->getContext().pImpl->StructConstants.getOrCreate(ST, V);
Owen Anderson45308b52009-07-27 22:29:26 +00001023}
1024
Chris Lattnerc3e74cd2011-08-07 04:18:48 +00001025Constant *ConstantStruct::get(StructType *T, ...) {
Talin3a0a30d2011-02-28 23:53:27 +00001026 va_list ap;
Chris Lattnercc19efa2011-06-20 04:01:31 +00001027 SmallVector<Constant*, 8> Values;
1028 va_start(ap, T);
1029 while (Constant *Val = va_arg(ap, llvm::Constant*))
Talin3a0a30d2011-02-28 23:53:27 +00001030 Values.push_back(Val);
Talinde422be2011-03-01 18:00:49 +00001031 va_end(ap);
Chris Lattnercc19efa2011-06-20 04:01:31 +00001032 return get(T, Values);
Talin3a0a30d2011-02-28 23:53:27 +00001033}
1034
Jay Foad89d9b812011-07-25 10:14:44 +00001035ConstantVector::ConstantVector(VectorType *T, ArrayRef<Constant *> V)
Gabor Greiff6caff662008-05-10 08:32:32 +00001036 : Constant(T, ConstantVectorVal,
1037 OperandTraits<ConstantVector>::op_end(this) - V.size(),
1038 V.size()) {
Jay Foad89d9b812011-07-25 10:14:44 +00001039 for (size_t i = 0, e = V.size(); i != e; i++)
1040 assert(V[i]->getType() == T->getElementType() &&
Dan Gohman30978072007-05-24 14:36:04 +00001041 "Initializer for vector element doesn't match vector element type!");
Jay Foad89d9b812011-07-25 10:14:44 +00001042 std::copy(V.begin(), V.end(), op_begin());
Brian Gaeke02209042004-08-20 06:00:58 +00001043}
1044
Owen Anderson4aa32952009-07-28 21:19:26 +00001045// ConstantVector accessors.
Jay Foadb8a8bed32011-06-22 09:10:19 +00001046Constant *ConstantVector::get(ArrayRef<Constant*> V) {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001047 if (Constant *C = getImpl(V))
1048 return C;
1049 VectorType *Ty = VectorType::get(V.front()->getType(), V.size());
1050 return Ty->getContext().pImpl->VectorConstants.getOrCreate(Ty, V);
1051}
Justin Bogner909e1c02015-12-01 20:20:49 +00001052
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001053Constant *ConstantVector::getImpl(ArrayRef<Constant*> V) {
Jay Foad9f32cfd2011-01-27 14:44:55 +00001054 assert(!V.empty() && "Vectors can't be empty");
Chris Lattner229907c2011-07-18 04:54:35 +00001055 VectorType *T = VectorType::get(V.front()->getType(), V.size());
Jay Foad9f32cfd2011-01-27 14:44:55 +00001056
Chris Lattner69229312011-02-15 00:14:00 +00001057 // If this is an all-undef or all-zero vector, return a
Owen Anderson4aa32952009-07-28 21:19:26 +00001058 // ConstantAggregateZero or UndefValue.
1059 Constant *C = V[0];
1060 bool isZero = C->isNullValue();
1061 bool isUndef = isa<UndefValue>(C);
1062
1063 if (isZero || isUndef) {
1064 for (unsigned i = 1, e = V.size(); i != e; ++i)
1065 if (V[i] != C) {
1066 isZero = isUndef = false;
1067 break;
1068 }
1069 }
Galina Kistanovafc259902012-07-13 01:25:27 +00001070
Owen Anderson4aa32952009-07-28 21:19:26 +00001071 if (isZero)
Owen Andersonb292b8c2009-07-30 23:03:37 +00001072 return ConstantAggregateZero::get(T);
Owen Anderson4aa32952009-07-28 21:19:26 +00001073 if (isUndef)
Owen Andersonb292b8c2009-07-30 23:03:37 +00001074 return UndefValue::get(T);
Galina Kistanovafc259902012-07-13 01:25:27 +00001075
Chris Lattner978fe0c2012-01-30 06:21:21 +00001076 // Check to see if all of the elements are ConstantFP or ConstantInt and if
1077 // the element type is compatible with ConstantDataVector. If so, use it.
Justin Bogner909e1c02015-12-01 20:20:49 +00001078 if (ConstantDataSequential::isElementTypeCompatible(C->getType()))
1079 return getSequenceIfElementsMatch<ConstantDataVector>(C, V);
Galina Kistanovafc259902012-07-13 01:25:27 +00001080
Chris Lattner978fe0c2012-01-30 06:21:21 +00001081 // Otherwise, the element type isn't compatible with ConstantDataVector, or
1082 // the operand list constants a ConstantExpr or something else strange.
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001083 return nullptr;
Owen Anderson4aa32952009-07-28 21:19:26 +00001084}
1085
Chris Lattnere9eed292012-01-25 05:19:54 +00001086Constant *ConstantVector::getSplat(unsigned NumElts, Constant *V) {
Chris Lattner978fe0c2012-01-30 06:21:21 +00001087 // If this splat is compatible with ConstantDataVector, use it instead of
1088 // ConstantVector.
1089 if ((isa<ConstantFP>(V) || isa<ConstantInt>(V)) &&
1090 ConstantDataSequential::isElementTypeCompatible(V->getType()))
1091 return ConstantDataVector::getSplat(NumElts, V);
Galina Kistanovafc259902012-07-13 01:25:27 +00001092
Chris Lattnere9eed292012-01-25 05:19:54 +00001093 SmallVector<Constant*, 32> Elts(NumElts, V);
1094 return get(Elts);
1095}
1096
David Majnemerf0f224d2015-11-11 21:57:16 +00001097ConstantTokenNone *ConstantTokenNone::get(LLVMContext &Context) {
1098 LLVMContextImpl *pImpl = Context.pImpl;
1099 if (!pImpl->TheNoneToken)
David Majnemer2dd41c52015-11-16 20:55:57 +00001100 pImpl->TheNoneToken.reset(new ConstantTokenNone(Context));
1101 return pImpl->TheNoneToken.get();
David Majnemerf0f224d2015-11-11 21:57:16 +00001102}
1103
1104/// Remove the constant from the constant table.
1105void ConstantTokenNone::destroyConstantImpl() {
1106 llvm_unreachable("You can't ConstantTokenNone->destroyConstantImpl()!");
1107}
Chris Lattnere9eed292012-01-25 05:19:54 +00001108
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001109// Utility function for determining if a ConstantExpr is a CastOp or not. This
1110// can't be inline because we don't want to #include Instruction.h into
1111// Constant.h
1112bool ConstantExpr::isCast() const {
1113 return Instruction::isCast(getOpcode());
1114}
1115
Reid Spenceree3c9912006-12-04 05:19:50 +00001116bool ConstantExpr::isCompare() const {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001117 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
Reid Spenceree3c9912006-12-04 05:19:50 +00001118}
1119
Dan Gohman7190d482009-09-10 23:37:55 +00001120bool ConstantExpr::isGEPWithNoNotionalOverIndexing() const {
1121 if (getOpcode() != Instruction::GetElementPtr) return false;
1122
1123 gep_type_iterator GEPI = gep_type_begin(this), E = gep_type_end(this);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001124 User::const_op_iterator OI = std::next(this->op_begin());
Dan Gohman7190d482009-09-10 23:37:55 +00001125
1126 // Skip the first index, as it has no static limit.
1127 ++GEPI;
1128 ++OI;
1129
1130 // The remaining indices must be compile-time known integers within the
1131 // bounds of the corresponding notional static array types.
1132 for (; GEPI != E; ++GEPI, ++OI) {
1133 ConstantInt *CI = dyn_cast<ConstantInt>(*OI);
1134 if (!CI) return false;
Chris Lattner229907c2011-07-18 04:54:35 +00001135 if (ArrayType *ATy = dyn_cast<ArrayType>(*GEPI))
Dan Gohman7190d482009-09-10 23:37:55 +00001136 if (CI->getValue().getActiveBits() > 64 ||
1137 CI->getZExtValue() >= ATy->getNumElements())
1138 return false;
1139 }
1140
1141 // All the indices checked out.
1142 return true;
1143}
1144
Dan Gohman1ecaf452008-05-31 00:58:22 +00001145bool ConstantExpr::hasIndices() const {
1146 return getOpcode() == Instruction::ExtractValue ||
1147 getOpcode() == Instruction::InsertValue;
1148}
1149
Jay Foad0091fe82011-04-13 15:22:40 +00001150ArrayRef<unsigned> ConstantExpr::getIndices() const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001151 if (const ExtractValueConstantExpr *EVCE =
1152 dyn_cast<ExtractValueConstantExpr>(this))
1153 return EVCE->Indices;
Dan Gohmana469bdb2008-06-23 16:39:44 +00001154
1155 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001156}
1157
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001158unsigned ConstantExpr::getPredicate() const {
Craig Toppercc03b492015-12-15 06:11:36 +00001159 return cast<CompareConstantExpr>(this)->predicate;
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001160}
Chris Lattner60e0dd72001-10-03 06:12:09 +00001161
Chris Lattner7c1018a2006-07-14 19:37:40 +00001162/// getWithOperandReplaced - Return a constant expression identical to this
1163/// one, but with the specified operand set to the specified value.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001164Constant *
1165ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +00001166 assert(Op->getType() == getOperand(OpNo)->getType() &&
1167 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +00001168 if (getOperand(OpNo) == Op)
1169 return const_cast<ConstantExpr*>(this);
Chris Lattner37e38352012-01-26 20:37:11 +00001170
1171 SmallVector<Constant*, 8> NewOps;
1172 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1173 NewOps.push_back(i == OpNo ? Op : getOperand(i));
Galina Kistanovafc259902012-07-13 01:25:27 +00001174
Chris Lattner37e38352012-01-26 20:37:11 +00001175 return getWithOperands(NewOps);
Chris Lattner227816342006-07-14 22:20:01 +00001176}
1177
1178/// getWithOperands - This returns the current constant expression with the
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001179/// operands replaced with the specified values. The specified array must
1180/// have the same number of operands as our current one.
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001181Constant *ConstantExpr::getWithOperands(ArrayRef<Constant *> Ops, Type *Ty,
David Blaikie88208842015-08-21 20:16:51 +00001182 bool OnlyIfReduced, Type *SrcTy) const {
Jay Foad5c984e562011-04-13 13:46:01 +00001183 assert(Ops.size() == getNumOperands() && "Operand count mismatch!");
Galina Kistanovafc259902012-07-13 01:25:27 +00001184
Benjamin Kramer8008e9f2015-03-02 11:57:04 +00001185 // If no operands changed return self.
1186 if (Ty == getType() && std::equal(Ops.begin(), Ops.end(), op_begin()))
Chris Lattner227816342006-07-14 22:20:01 +00001187 return const_cast<ConstantExpr*>(this);
1188
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001189 Type *OnlyIfReducedTy = OnlyIfReduced ? Ty : nullptr;
Chris Lattner227816342006-07-14 22:20:01 +00001190 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001191 case Instruction::Trunc:
1192 case Instruction::ZExt:
1193 case Instruction::SExt:
1194 case Instruction::FPTrunc:
1195 case Instruction::FPExt:
1196 case Instruction::UIToFP:
1197 case Instruction::SIToFP:
1198 case Instruction::FPToUI:
1199 case Instruction::FPToSI:
1200 case Instruction::PtrToInt:
1201 case Instruction::IntToPtr:
1202 case Instruction::BitCast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001203 case Instruction::AddrSpaceCast:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001204 return ConstantExpr::getCast(getOpcode(), Ops[0], Ty, OnlyIfReduced);
Chris Lattner227816342006-07-14 22:20:01 +00001205 case Instruction::Select:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001206 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2], OnlyIfReducedTy);
Chris Lattner227816342006-07-14 22:20:01 +00001207 case Instruction::InsertElement:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001208 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2],
1209 OnlyIfReducedTy);
Chris Lattner227816342006-07-14 22:20:01 +00001210 case Instruction::ExtractElement:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001211 return ConstantExpr::getExtractElement(Ops[0], Ops[1], OnlyIfReducedTy);
Chris Lattner37e38352012-01-26 20:37:11 +00001212 case Instruction::InsertValue:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001213 return ConstantExpr::getInsertValue(Ops[0], Ops[1], getIndices(),
1214 OnlyIfReducedTy);
Chris Lattner37e38352012-01-26 20:37:11 +00001215 case Instruction::ExtractValue:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001216 return ConstantExpr::getExtractValue(Ops[0], getIndices(), OnlyIfReducedTy);
Chris Lattner227816342006-07-14 22:20:01 +00001217 case Instruction::ShuffleVector:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001218 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2],
1219 OnlyIfReducedTy);
David Blaikie88208842015-08-21 20:16:51 +00001220 case Instruction::GetElementPtr: {
1221 auto *GEPO = cast<GEPOperator>(this);
1222 assert(SrcTy || (Ops[0]->getType() == getOperand(0)->getType()));
1223 return ConstantExpr::getGetElementPtr(
1224 SrcTy ? SrcTy : GEPO->getSourceElementType(), Ops[0], Ops.slice(1),
1225 GEPO->isInBounds(), OnlyIfReducedTy);
1226 }
Reid Spencer266e42b2006-12-23 06:05:41 +00001227 case Instruction::ICmp:
1228 case Instruction::FCmp:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001229 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1],
1230 OnlyIfReducedTy);
Chris Lattner227816342006-07-14 22:20:01 +00001231 default:
1232 assert(getNumOperands() == 2 && "Must be binary operator?");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001233 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1], SubclassOptionalData,
1234 OnlyIfReducedTy);
Chris Lattner7c1018a2006-07-14 19:37:40 +00001235 }
1236}
1237
Chris Lattner2f7c9632001-06-06 20:29:01 +00001238
1239//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00001240// isValueValidForType implementations
1241
Chris Lattner229907c2011-07-18 04:54:35 +00001242bool ConstantInt::isValueValidForType(Type *Ty, uint64_t Val) {
Chris Lattner8326bd82012-01-26 00:42:34 +00001243 unsigned NumBits = Ty->getIntegerBitWidth(); // assert okay
1244 if (Ty->isIntegerTy(1))
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001245 return Val == 0 || Val == 1;
Reid Spencerd7a00d72007-02-05 23:47:56 +00001246 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001247 return true; // always true, has to fit in largest type
1248 uint64_t Max = (1ll << NumBits) - 1;
1249 return Val <= Max;
Reid Spencere7334722006-12-19 01:28:19 +00001250}
1251
Chris Lattner229907c2011-07-18 04:54:35 +00001252bool ConstantInt::isValueValidForType(Type *Ty, int64_t Val) {
Chris Lattner8326bd82012-01-26 00:42:34 +00001253 unsigned NumBits = Ty->getIntegerBitWidth();
1254 if (Ty->isIntegerTy(1))
Reid Spencera94d3942007-01-19 21:13:56 +00001255 return Val == 0 || Val == 1 || Val == -1;
Reid Spencerd7a00d72007-02-05 23:47:56 +00001256 if (NumBits >= 64)
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001257 return true; // always true, has to fit in largest type
1258 int64_t Min = -(1ll << (NumBits-1));
1259 int64_t Max = (1ll << (NumBits-1)) - 1;
1260 return (Val >= Min && Val <= Max);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001261}
1262
Chris Lattner229907c2011-07-18 04:54:35 +00001263bool ConstantFP::isValueValidForType(Type *Ty, const APFloat& Val) {
Dale Johannesend246b2c2007-08-30 00:23:21 +00001264 // convert modifies in place, so make a copy.
1265 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001266 bool losesInfo;
Chris Lattner6b727592004-06-17 18:19:28 +00001267 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001268 default:
1269 return false; // These can't be represented as floating point!
1270
Dale Johannesend246b2c2007-08-30 00:23:21 +00001271 // FIXME rounding mode needs to be more flexible
Dan Gohman518cda42011-12-17 00:04:22 +00001272 case Type::HalfTyID: {
1273 if (&Val2.getSemantics() == &APFloat::IEEEhalf)
1274 return true;
1275 Val2.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven, &losesInfo);
1276 return !losesInfo;
1277 }
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001278 case Type::FloatTyID: {
1279 if (&Val2.getSemantics() == &APFloat::IEEEsingle)
1280 return true;
1281 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
1282 return !losesInfo;
1283 }
1284 case Type::DoubleTyID: {
Dan Gohman518cda42011-12-17 00:04:22 +00001285 if (&Val2.getSemantics() == &APFloat::IEEEhalf ||
1286 &Val2.getSemantics() == &APFloat::IEEEsingle ||
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001287 &Val2.getSemantics() == &APFloat::IEEEdouble)
1288 return true;
1289 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
1290 return !losesInfo;
1291 }
Dale Johannesenbdad8092007-08-09 22:51:36 +00001292 case Type::X86_FP80TyID:
Dan Gohman518cda42011-12-17 00:04:22 +00001293 return &Val2.getSemantics() == &APFloat::IEEEhalf ||
1294 &Val2.getSemantics() == &APFloat::IEEEsingle ||
Dale Johannesen028084e2007-09-12 03:30:33 +00001295 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1296 &Val2.getSemantics() == &APFloat::x87DoubleExtended;
Dale Johannesenbdad8092007-08-09 22:51:36 +00001297 case Type::FP128TyID:
Dan Gohman518cda42011-12-17 00:04:22 +00001298 return &Val2.getSemantics() == &APFloat::IEEEhalf ||
1299 &Val2.getSemantics() == &APFloat::IEEEsingle ||
Dale Johannesen028084e2007-09-12 03:30:33 +00001300 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1301 &Val2.getSemantics() == &APFloat::IEEEquad;
Dale Johannesen007aa372007-10-11 18:07:22 +00001302 case Type::PPC_FP128TyID:
Dan Gohman518cda42011-12-17 00:04:22 +00001303 return &Val2.getSemantics() == &APFloat::IEEEhalf ||
1304 &Val2.getSemantics() == &APFloat::IEEEsingle ||
Dale Johannesen007aa372007-10-11 18:07:22 +00001305 &Val2.getSemantics() == &APFloat::IEEEdouble ||
1306 &Val2.getSemantics() == &APFloat::PPCDoubleDouble;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001307 }
Chris Lattneraa2372562006-05-24 17:04:05 +00001308}
Chris Lattner9655e542001-07-20 19:16:02 +00001309
Chris Lattner030af792012-01-24 05:42:11 +00001310
Chris Lattner49d855c2001-09-07 16:46:31 +00001311//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +00001312// Factory Function Implementation
1313
Chris Lattnerc7f9fd42012-01-23 15:20:12 +00001314ConstantAggregateZero *ConstantAggregateZero::get(Type *Ty) {
Chris Lattner13ee7952010-08-28 04:09:24 +00001315 assert((Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy()) &&
Owen Andersonb292b8c2009-07-30 23:03:37 +00001316 "Cannot create an aggregate zero of non-aggregate type!");
David Blaikiecb2818f2014-11-25 02:26:22 +00001317
1318 ConstantAggregateZero *&Entry = Ty->getContext().pImpl->CAZConstants[Ty];
David Blaikie899b85a2014-11-25 02:13:54 +00001319 if (!Entry)
David Blaikiecb2818f2014-11-25 02:26:22 +00001320 Entry = new ConstantAggregateZero(Ty);
David Blaikie899b85a2014-11-25 02:13:54 +00001321
David Blaikiecb2818f2014-11-25 02:26:22 +00001322 return Entry;
Owen Andersonb292b8c2009-07-30 23:03:37 +00001323}
1324
Chris Lattner030af792012-01-24 05:42:11 +00001325/// destroyConstant - Remove the constant from the constant table.
Dan Gohman92b551b2009-03-03 02:55:14 +00001326///
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001327void ConstantAggregateZero::destroyConstantImpl() {
Chris Lattnerc7f9fd42012-01-23 15:20:12 +00001328 getContext().pImpl->CAZConstants.erase(getType());
Chris Lattner9fba3da2004-02-15 05:53:04 +00001329}
1330
Dan Gohman92b551b2009-03-03 02:55:14 +00001331/// destroyConstant - Remove the constant from the constant table...
1332///
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001333void ConstantArray::destroyConstantImpl() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001334 getType()->getContext().pImpl->ArrayConstants.remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001335}
1336
Chris Lattner81fabb02002-08-26 17:53:56 +00001337
Chris Lattner3462ae32001-12-03 22:26:30 +00001338//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001339//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001340
Chris Lattnerd7a73302001-10-13 06:57:33 +00001341// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001342//
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001343void ConstantStruct::destroyConstantImpl() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001344 getType()->getContext().pImpl->StructConstants.remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001345}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001346
Brian Gaeke02209042004-08-20 06:00:58 +00001347// destroyConstant - Remove the constant from the constant table...
1348//
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001349void ConstantVector::destroyConstantImpl() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001350 getType()->getContext().pImpl->VectorConstants.remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001351}
1352
Duncan Sandse6beec62012-11-13 12:59:33 +00001353/// getSplatValue - If this is a splat vector constant, meaning that all of
1354/// the elements have the same value, return that value. Otherwise return 0.
1355Constant *Constant::getSplatValue() const {
1356 assert(this->getType()->isVectorTy() && "Only valid for vectors!");
1357 if (isa<ConstantAggregateZero>(this))
1358 return getNullValue(this->getType()->getVectorElementType());
1359 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this))
1360 return CV->getSplatValue();
1361 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
1362 return CV->getSplatValue();
Craig Topperc6207612014-04-09 06:08:46 +00001363 return nullptr;
Duncan Sandse6beec62012-11-13 12:59:33 +00001364}
1365
Dan Gohman07159202007-10-17 17:51:30 +00001366/// getSplatValue - If this is a splat constant, where all of the
1367/// elements have the same value, return that value. Otherwise return null.
Duncan Sandscf0ff032011-02-01 08:39:12 +00001368Constant *ConstantVector::getSplatValue() const {
Dan Gohman07159202007-10-17 17:51:30 +00001369 // Check out first element.
1370 Constant *Elt = getOperand(0);
1371 // Then make sure all remaining elements point to the same value.
1372 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
Chris Lattnerd04e32d2011-07-17 06:01:30 +00001373 if (getOperand(I) != Elt)
Craig Topperc6207612014-04-09 06:08:46 +00001374 return nullptr;
Dan Gohman07159202007-10-17 17:51:30 +00001375 return Elt;
1376}
1377
Duncan Sandse6beec62012-11-13 12:59:33 +00001378/// If C is a constant integer then return its value, otherwise C must be a
1379/// vector of constant integers, all equal, and the common value is returned.
1380const APInt &Constant::getUniqueInteger() const {
1381 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
1382 return CI->getValue();
1383 assert(this->getSplatValue() && "Doesn't contain a unique integer!");
1384 const Constant *C = this->getAggregateElement(0U);
1385 assert(C && isa<ConstantInt>(C) && "Not a vector of numbers!");
1386 return cast<ConstantInt>(C)->getValue();
1387}
1388
Chris Lattner31b132c2009-10-28 00:01:44 +00001389//---- ConstantPointerNull::get() implementation.
Chris Lattnerd7a73302001-10-13 06:57:33 +00001390//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001391
Chris Lattner229907c2011-07-18 04:54:35 +00001392ConstantPointerNull *ConstantPointerNull::get(PointerType *Ty) {
Chris Lattnerc7f9fd42012-01-23 15:20:12 +00001393 ConstantPointerNull *&Entry = Ty->getContext().pImpl->CPNConstants[Ty];
Craig Topperc6207612014-04-09 06:08:46 +00001394 if (!Entry)
Chris Lattnerc7f9fd42012-01-23 15:20:12 +00001395 Entry = new ConstantPointerNull(Ty);
Galina Kistanovafc259902012-07-13 01:25:27 +00001396
Chris Lattnerc7f9fd42012-01-23 15:20:12 +00001397 return Entry;
Chris Lattner883ad0b2001-10-03 15:39:36 +00001398}
1399
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001400// destroyConstant - Remove the constant from the constant table...
1401//
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001402void ConstantPointerNull::destroyConstantImpl() {
Chris Lattnerc7f9fd42012-01-23 15:20:12 +00001403 getContext().pImpl->CPNConstants.erase(getType());
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001404}
1405
1406
Chris Lattner31b132c2009-10-28 00:01:44 +00001407//---- UndefValue::get() implementation.
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001408//
1409
Chris Lattner229907c2011-07-18 04:54:35 +00001410UndefValue *UndefValue::get(Type *Ty) {
Chris Lattnerc7f9fd42012-01-23 15:20:12 +00001411 UndefValue *&Entry = Ty->getContext().pImpl->UVConstants[Ty];
Craig Topperc6207612014-04-09 06:08:46 +00001412 if (!Entry)
Chris Lattnerc7f9fd42012-01-23 15:20:12 +00001413 Entry = new UndefValue(Ty);
Galina Kistanovafc259902012-07-13 01:25:27 +00001414
Chris Lattnerc7f9fd42012-01-23 15:20:12 +00001415 return Entry;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001416}
1417
1418// destroyConstant - Remove the constant from the constant table.
1419//
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001420void UndefValue::destroyConstantImpl() {
Chris Lattnerc7f9fd42012-01-23 15:20:12 +00001421 // Free the constant and any dangling references to it.
1422 getContext().pImpl->UVConstants.erase(getType());
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001423}
1424
Chris Lattner31b132c2009-10-28 00:01:44 +00001425//---- BlockAddress::get() implementation.
1426//
1427
1428BlockAddress *BlockAddress::get(BasicBlock *BB) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001429 assert(BB->getParent() && "Block must have a parent");
Chris Lattner31b132c2009-10-28 00:01:44 +00001430 return get(BB->getParent(), BB);
1431}
1432
1433BlockAddress *BlockAddress::get(Function *F, BasicBlock *BB) {
1434 BlockAddress *&BA =
1435 F->getContext().pImpl->BlockAddresses[std::make_pair(F, BB)];
Craig Topperc6207612014-04-09 06:08:46 +00001436 if (!BA)
Chris Lattner31b132c2009-10-28 00:01:44 +00001437 BA = new BlockAddress(F, BB);
Galina Kistanovafc259902012-07-13 01:25:27 +00001438
Chris Lattner31b132c2009-10-28 00:01:44 +00001439 assert(BA->getFunction() == F && "Basic block moved between functions");
1440 return BA;
1441}
1442
1443BlockAddress::BlockAddress(Function *F, BasicBlock *BB)
1444: Constant(Type::getInt8PtrTy(F->getContext()), Value::BlockAddressVal,
1445 &Op<0>(), 2) {
Chris Lattnerc559a9f2009-11-01 03:03:03 +00001446 setOperand(0, F);
1447 setOperand(1, BB);
Chris Lattneraa99c942009-11-01 01:27:45 +00001448 BB->AdjustBlockAddressRefCount(1);
Chris Lattner31b132c2009-10-28 00:01:44 +00001449}
1450
Chandler Carruth6a936922014-01-19 02:13:50 +00001451BlockAddress *BlockAddress::lookup(const BasicBlock *BB) {
1452 if (!BB->hasAddressTaken())
Craig Topperc6207612014-04-09 06:08:46 +00001453 return nullptr;
Chandler Carruth6a936922014-01-19 02:13:50 +00001454
1455 const Function *F = BB->getParent();
Craig Topper2617dcc2014-04-15 06:32:26 +00001456 assert(F && "Block must have a parent");
Chandler Carruth6a936922014-01-19 02:13:50 +00001457 BlockAddress *BA =
1458 F->getContext().pImpl->BlockAddresses.lookup(std::make_pair(F, BB));
1459 assert(BA && "Refcount and block address map disagree!");
1460 return BA;
1461}
Chris Lattner31b132c2009-10-28 00:01:44 +00001462
1463// destroyConstant - Remove the constant from the constant table.
1464//
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001465void BlockAddress::destroyConstantImpl() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001466 getFunction()->getType()->getContext().pImpl
Chris Lattner31b132c2009-10-28 00:01:44 +00001467 ->BlockAddresses.erase(std::make_pair(getFunction(), getBasicBlock()));
Chris Lattneraa99c942009-11-01 01:27:45 +00001468 getBasicBlock()->AdjustBlockAddressRefCount(-1);
Chris Lattner31b132c2009-10-28 00:01:44 +00001469}
1470
Mehdi Amini8914d292016-02-10 22:47:15 +00001471Value *BlockAddress::handleOperandChangeImpl(Value *From, Value *To) {
Chris Lattner31b132c2009-10-28 00:01:44 +00001472 // This could be replacing either the Basic Block or the Function. In either
1473 // case, we have to remove the map entry.
1474 Function *NewF = getFunction();
1475 BasicBlock *NewBB = getBasicBlock();
Galina Kistanovafc259902012-07-13 01:25:27 +00001476
Mehdi Amini8914d292016-02-10 22:47:15 +00001477 if (From == NewF)
Derek Schuffec9dc012013-06-13 19:51:17 +00001478 NewF = cast<Function>(To->stripPointerCasts());
Mehdi Amini8914d292016-02-10 22:47:15 +00001479 else {
1480 assert(From == NewBB && "From does not match any operand");
Chris Lattner31b132c2009-10-28 00:01:44 +00001481 NewBB = cast<BasicBlock>(To);
Mehdi Amini8914d292016-02-10 22:47:15 +00001482 }
Galina Kistanovafc259902012-07-13 01:25:27 +00001483
Chris Lattner31b132c2009-10-28 00:01:44 +00001484 // See if the 'new' entry already exists, if not, just update this in place
1485 // and return early.
1486 BlockAddress *&NewBA =
1487 getContext().pImpl->BlockAddresses[std::make_pair(NewF, NewBB)];
Pete Cooper5815b1f2015-06-24 18:55:24 +00001488 if (NewBA)
1489 return NewBA;
Chris Lattner31b132c2009-10-28 00:01:44 +00001490
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001491 getBasicBlock()->AdjustBlockAddressRefCount(-1);
Galina Kistanovafc259902012-07-13 01:25:27 +00001492
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001493 // Remove the old entry, this can't cause the map to rehash (just a
1494 // tombstone will get added).
1495 getContext().pImpl->BlockAddresses.erase(std::make_pair(getFunction(),
1496 getBasicBlock()));
1497 NewBA = this;
1498 setOperand(0, NewF);
1499 setOperand(1, NewBB);
1500 getBasicBlock()->AdjustBlockAddressRefCount(1);
Pete Cooper5815b1f2015-06-24 18:55:24 +00001501
1502 // If we just want to keep the existing value, then return null.
1503 // Callers know that this means we shouldn't delete this value.
1504 return nullptr;
Chris Lattner31b132c2009-10-28 00:01:44 +00001505}
1506
1507//---- ConstantExpr::get() implementations.
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001508//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001509
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001510/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands7d6c8ae2008-03-30 19:38:55 +00001511/// cast in the ExprConstants map. It is used by the various get* methods below.
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001512static Constant *getFoldedCast(Instruction::CastOps opc, Constant *C, Type *Ty,
1513 bool OnlyIfReduced = false) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001514 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001515 // Fold a few common cases
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001516 if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001517 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001518
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001519 if (OnlyIfReduced)
1520 return nullptr;
1521
Owen Anderson1584a292009-08-04 20:25:11 +00001522 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
1523
Nadav Rotem88330432013-03-07 01:30:40 +00001524 // Look up the constant in the table first to ensure uniqueness.
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001525 ConstantExprKeyType Key(opc, C);
Galina Kistanovafc259902012-07-13 01:25:27 +00001526
Owen Anderson1584a292009-08-04 20:25:11 +00001527 return pImpl->ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001528}
Galina Kistanovafc259902012-07-13 01:25:27 +00001529
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001530Constant *ConstantExpr::getCast(unsigned oc, Constant *C, Type *Ty,
1531 bool OnlyIfReduced) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001532 Instruction::CastOps opc = Instruction::CastOps(oc);
1533 assert(Instruction::isCast(opc) && "opcode out of range");
1534 assert(C && Ty && "Null arguments to getCast");
Chris Lattner37bc78a2010-01-26 21:51:43 +00001535 assert(CastInst::castIsValid(opc, C, Ty) && "Invalid constantexpr cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001536
1537 switch (opc) {
Chris Lattner37bc78a2010-01-26 21:51:43 +00001538 default:
1539 llvm_unreachable("Invalid cast opcode");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001540 case Instruction::Trunc:
1541 return getTrunc(C, Ty, OnlyIfReduced);
1542 case Instruction::ZExt:
1543 return getZExt(C, Ty, OnlyIfReduced);
1544 case Instruction::SExt:
1545 return getSExt(C, Ty, OnlyIfReduced);
1546 case Instruction::FPTrunc:
1547 return getFPTrunc(C, Ty, OnlyIfReduced);
1548 case Instruction::FPExt:
1549 return getFPExtend(C, Ty, OnlyIfReduced);
1550 case Instruction::UIToFP:
1551 return getUIToFP(C, Ty, OnlyIfReduced);
1552 case Instruction::SIToFP:
1553 return getSIToFP(C, Ty, OnlyIfReduced);
1554 case Instruction::FPToUI:
1555 return getFPToUI(C, Ty, OnlyIfReduced);
1556 case Instruction::FPToSI:
1557 return getFPToSI(C, Ty, OnlyIfReduced);
1558 case Instruction::PtrToInt:
1559 return getPtrToInt(C, Ty, OnlyIfReduced);
1560 case Instruction::IntToPtr:
1561 return getIntToPtr(C, Ty, OnlyIfReduced);
1562 case Instruction::BitCast:
1563 return getBitCast(C, Ty, OnlyIfReduced);
1564 case Instruction::AddrSpaceCast:
1565 return getAddrSpaceCast(C, Ty, OnlyIfReduced);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001566 }
Galina Kistanovafc259902012-07-13 01:25:27 +00001567}
Reid Spencerf37dc652006-12-05 19:14:13 +00001568
Chris Lattner229907c2011-07-18 04:54:35 +00001569Constant *ConstantExpr::getZExtOrBitCast(Constant *C, Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001570 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001571 return getBitCast(C, Ty);
1572 return getZExt(C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001573}
1574
Chris Lattner229907c2011-07-18 04:54:35 +00001575Constant *ConstantExpr::getSExtOrBitCast(Constant *C, Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001576 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001577 return getBitCast(C, Ty);
1578 return getSExt(C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001579}
1580
Chris Lattner229907c2011-07-18 04:54:35 +00001581Constant *ConstantExpr::getTruncOrBitCast(Constant *C, Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001582 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001583 return getBitCast(C, Ty);
1584 return getTrunc(C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001585}
1586
Chris Lattner229907c2011-07-18 04:54:35 +00001587Constant *ConstantExpr::getPointerCast(Constant *S, Type *Ty) {
Evgeniy Stepanov23382642013-01-16 14:41:46 +00001588 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
1589 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
1590 "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001591
Evgeniy Stepanov23382642013-01-16 14:41:46 +00001592 if (Ty->isIntOrIntVectorTy())
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001593 return getPtrToInt(S, Ty);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001594
1595 unsigned SrcAS = S->getType()->getPointerAddressSpace();
1596 if (Ty->isPtrOrPtrVectorTy() && SrcAS != Ty->getPointerAddressSpace())
1597 return getAddrSpaceCast(S, Ty);
1598
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001599 return getBitCast(S, Ty);
Reid Spencerbc245a02006-12-05 03:25:26 +00001600}
1601
Matt Arsenault21f38f42013-12-07 02:58:41 +00001602Constant *ConstantExpr::getPointerBitCastOrAddrSpaceCast(Constant *S,
1603 Type *Ty) {
1604 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
1605 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
1606
1607 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
1608 return getAddrSpaceCast(S, Ty);
1609
1610 return getBitCast(S, Ty);
1611}
1612
1613Constant *ConstantExpr::getIntegerCast(Constant *C, Type *Ty,
Reid Spencer56521c42006-12-12 00:51:07 +00001614 bool isSigned) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001615 assert(C->getType()->isIntOrIntVectorTy() &&
1616 Ty->isIntOrIntVectorTy() && "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001617 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1618 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer56521c42006-12-12 00:51:07 +00001619 Instruction::CastOps opcode =
1620 (SrcBits == DstBits ? Instruction::BitCast :
1621 (SrcBits > DstBits ? Instruction::Trunc :
1622 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1623 return getCast(opcode, C, Ty);
1624}
1625
Chris Lattner229907c2011-07-18 04:54:35 +00001626Constant *ConstantExpr::getFPCast(Constant *C, Type *Ty) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001627 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer56521c42006-12-12 00:51:07 +00001628 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001629 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1630 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001631 if (SrcBits == DstBits)
1632 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001633 Instruction::CastOps opcode =
Jay Foad9f32cfd2011-01-27 14:44:55 +00001634 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001635 return getCast(opcode, C, Ty);
1636}
1637
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001638Constant *ConstantExpr::getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001639#ifndef NDEBUG
1640 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1641 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1642#endif
1643 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001644 assert(C->getType()->isIntOrIntVectorTy() && "Trunc operand must be integer");
1645 assert(Ty->isIntOrIntVectorTy() && "Trunc produces only integral");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001646 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001647 "SrcTy must be larger than DestTy for Trunc!");
1648
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001649 return getFoldedCast(Instruction::Trunc, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001650}
1651
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001652Constant *ConstantExpr::getSExt(Constant *C, Type *Ty, bool OnlyIfReduced) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001653#ifndef NDEBUG
1654 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1655 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1656#endif
1657 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001658 assert(C->getType()->isIntOrIntVectorTy() && "SExt operand must be integral");
1659 assert(Ty->isIntOrIntVectorTy() && "SExt produces only integer");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001660 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001661 "SrcTy must be smaller than DestTy for SExt!");
1662
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001663 return getFoldedCast(Instruction::SExt, C, Ty, OnlyIfReduced);
Chris Lattnerdd284742004-04-04 23:20:30 +00001664}
1665
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001666Constant *ConstantExpr::getZExt(Constant *C, Type *Ty, bool OnlyIfReduced) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001667#ifndef NDEBUG
1668 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1669 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1670#endif
1671 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001672 assert(C->getType()->isIntOrIntVectorTy() && "ZEXt operand must be integral");
1673 assert(Ty->isIntOrIntVectorTy() && "ZExt produces only integer");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001674 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001675 "SrcTy must be smaller than DestTy for ZExt!");
1676
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001677 return getFoldedCast(Instruction::ZExt, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001678}
1679
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001680Constant *ConstantExpr::getFPTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001681#ifndef NDEBUG
1682 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1683 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1684#endif
1685 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001686 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001687 C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001688 "This is an illegal floating point truncation!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001689 return getFoldedCast(Instruction::FPTrunc, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001690}
1691
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001692Constant *ConstantExpr::getFPExtend(Constant *C, Type *Ty, bool OnlyIfReduced) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001693#ifndef NDEBUG
1694 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1695 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1696#endif
1697 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001698 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001699 C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001700 "This is an illegal floating point extension!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001701 return getFoldedCast(Instruction::FPExt, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001702}
1703
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001704Constant *ConstantExpr::getUIToFP(Constant *C, Type *Ty, bool OnlyIfReduced) {
Devang Pateld26344d2008-11-03 23:20:04 +00001705#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001706 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1707 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001708#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001709 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001710 assert(C->getType()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00001711 "This is an illegal uint to floating point cast!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001712 return getFoldedCast(Instruction::UIToFP, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001713}
1714
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001715Constant *ConstantExpr::getSIToFP(Constant *C, Type *Ty, bool OnlyIfReduced) {
Devang Pateld26344d2008-11-03 23:20:04 +00001716#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001717 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1718 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001719#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001720 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001721 assert(C->getType()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001722 "This is an illegal sint to floating point cast!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001723 return getFoldedCast(Instruction::SIToFP, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001724}
1725
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001726Constant *ConstantExpr::getFPToUI(Constant *C, Type *Ty, bool OnlyIfReduced) {
Devang Pateld26344d2008-11-03 23:20:04 +00001727#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001728 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1729 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001730#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001731 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001732 assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00001733 "This is an illegal floating point to uint cast!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001734 return getFoldedCast(Instruction::FPToUI, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001735}
1736
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001737Constant *ConstantExpr::getFPToSI(Constant *C, Type *Ty, bool OnlyIfReduced) {
Devang Pateld26344d2008-11-03 23:20:04 +00001738#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001739 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1740 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001741#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001742 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001743 assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00001744 "This is an illegal floating point to sint cast!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001745 return getFoldedCast(Instruction::FPToSI, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001746}
1747
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001748Constant *ConstantExpr::getPtrToInt(Constant *C, Type *DstTy,
1749 bool OnlyIfReduced) {
Nadav Rotem3924cb02011-12-05 06:29:09 +00001750 assert(C->getType()->getScalarType()->isPointerTy() &&
1751 "PtrToInt source must be pointer or pointer vector");
1752 assert(DstTy->getScalarType()->isIntegerTy() &&
1753 "PtrToInt destination must be integer or integer vector");
Chris Lattner8a3df542012-01-25 01:32:59 +00001754 assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy));
Nick Lewyckyff509622012-01-25 03:20:12 +00001755 if (isa<VectorType>(C->getType()))
Chris Lattner8326bd82012-01-26 00:42:34 +00001756 assert(C->getType()->getVectorNumElements()==DstTy->getVectorNumElements()&&
Chris Lattner8a3df542012-01-25 01:32:59 +00001757 "Invalid cast between a different number of vector elements");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001758 return getFoldedCast(Instruction::PtrToInt, C, DstTy, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001759}
1760
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001761Constant *ConstantExpr::getIntToPtr(Constant *C, Type *DstTy,
1762 bool OnlyIfReduced) {
Nadav Rotem3924cb02011-12-05 06:29:09 +00001763 assert(C->getType()->getScalarType()->isIntegerTy() &&
1764 "IntToPtr source must be integer or integer vector");
1765 assert(DstTy->getScalarType()->isPointerTy() &&
1766 "IntToPtr destination must be a pointer or pointer vector");
Chris Lattner8a3df542012-01-25 01:32:59 +00001767 assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy));
Nick Lewyckyff509622012-01-25 03:20:12 +00001768 if (isa<VectorType>(C->getType()))
Chris Lattner8326bd82012-01-26 00:42:34 +00001769 assert(C->getType()->getVectorNumElements()==DstTy->getVectorNumElements()&&
Chris Lattner8a3df542012-01-25 01:32:59 +00001770 "Invalid cast between a different number of vector elements");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001771 return getFoldedCast(Instruction::IntToPtr, C, DstTy, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001772}
1773
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001774Constant *ConstantExpr::getBitCast(Constant *C, Type *DstTy,
1775 bool OnlyIfReduced) {
Chris Lattner37bc78a2010-01-26 21:51:43 +00001776 assert(CastInst::castIsValid(Instruction::BitCast, C, DstTy) &&
1777 "Invalid constantexpr bitcast!");
Galina Kistanovafc259902012-07-13 01:25:27 +00001778
Chris Lattnercbeda872009-03-21 06:55:54 +00001779 // It is common to ask for a bitcast of a value to its own type, handle this
1780 // speedily.
1781 if (C->getType() == DstTy) return C;
Galina Kistanovafc259902012-07-13 01:25:27 +00001782
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001783 return getFoldedCast(Instruction::BitCast, C, DstTy, OnlyIfReduced);
Chris Lattnerdd284742004-04-04 23:20:30 +00001784}
1785
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001786Constant *ConstantExpr::getAddrSpaceCast(Constant *C, Type *DstTy,
1787 bool OnlyIfReduced) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001788 assert(CastInst::castIsValid(Instruction::AddrSpaceCast, C, DstTy) &&
1789 "Invalid constantexpr addrspacecast!");
1790
Jingyue Wubaabe502014-06-15 21:40:57 +00001791 // Canonicalize addrspacecasts between different pointer types by first
1792 // bitcasting the pointer type and then converting the address space.
1793 PointerType *SrcScalarTy = cast<PointerType>(C->getType()->getScalarType());
1794 PointerType *DstScalarTy = cast<PointerType>(DstTy->getScalarType());
1795 Type *DstElemTy = DstScalarTy->getElementType();
1796 if (SrcScalarTy->getElementType() != DstElemTy) {
1797 Type *MidTy = PointerType::get(DstElemTy, SrcScalarTy->getAddressSpace());
1798 if (VectorType *VT = dyn_cast<VectorType>(DstTy)) {
1799 // Handle vectors of pointers.
1800 MidTy = VectorType::get(MidTy, VT->getNumElements());
1801 }
1802 C = getBitCast(C, MidTy);
1803 }
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001804 return getFoldedCast(Instruction::AddrSpaceCast, C, DstTy, OnlyIfReduced);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001805}
1806
Chris Lattner887ecac2011-07-09 18:23:52 +00001807Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2,
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001808 unsigned Flags, Type *OnlyIfReducedTy) {
Chris Lattner887ecac2011-07-09 18:23:52 +00001809 // Check the operands for consistency first.
Reid Spencer7eb55b32006-11-02 01:53:59 +00001810 assert(Opcode >= Instruction::BinaryOpsBegin &&
1811 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001812 "Invalid opcode in binary constant expression");
1813 assert(C1->getType() == C2->getType() &&
1814 "Operand types in binary constant expression should match");
Galina Kistanovafc259902012-07-13 01:25:27 +00001815
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001816#ifndef NDEBUG
1817 switch (Opcode) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001818 case Instruction::Add:
Reid Spencer7eb55b32006-11-02 01:53:59 +00001819 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001820 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001821 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001822 assert(C1->getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001823 "Tried to create an integer operation on a non-integer type!");
1824 break;
1825 case Instruction::FAdd:
1826 case Instruction::FSub:
1827 case Instruction::FMul:
1828 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001829 assert(C1->getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001830 "Tried to create a floating-point operation on a "
1831 "non-floating-point type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001832 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001833 case Instruction::UDiv:
1834 case Instruction::SDiv:
1835 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001836 assert(C1->getType()->isIntOrIntVectorTy() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001837 "Tried to create an arithmetic operation on a non-arithmetic type!");
1838 break;
1839 case Instruction::FDiv:
1840 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001841 assert(C1->getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001842 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001843 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001844 case Instruction::URem:
1845 case Instruction::SRem:
1846 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001847 assert(C1->getType()->isIntOrIntVectorTy() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001848 "Tried to create an arithmetic operation on a non-arithmetic type!");
1849 break;
1850 case Instruction::FRem:
1851 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001852 assert(C1->getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001853 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001854 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001855 case Instruction::And:
1856 case Instruction::Or:
1857 case Instruction::Xor:
1858 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001859 assert(C1->getType()->isIntOrIntVectorTy() &&
Misha Brukman3852f652005-01-27 06:46:38 +00001860 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001861 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001862 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001863 case Instruction::LShr:
1864 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00001865 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001866 assert(C1->getType()->isIntOrIntVectorTy() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001867 "Tried to create a shift operation on a non-integer type!");
1868 break;
1869 default:
1870 break;
1871 }
1872#endif
1873
Chris Lattner887ecac2011-07-09 18:23:52 +00001874 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1875 return FC; // Fold a few common cases.
Galina Kistanovafc259902012-07-13 01:25:27 +00001876
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001877 if (OnlyIfReducedTy == C1->getType())
1878 return nullptr;
1879
Benjamin Kramer324322b2013-03-07 20:53:34 +00001880 Constant *ArgVec[] = { C1, C2 };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001881 ConstantExprKeyType Key(Opcode, ArgVec, 0, Flags);
Galina Kistanovafc259902012-07-13 01:25:27 +00001882
Chris Lattner887ecac2011-07-09 18:23:52 +00001883 LLVMContextImpl *pImpl = C1->getContext().pImpl;
1884 return pImpl->ExprConstants.getOrCreate(C1->getType(), Key);
Reid Spencera009d0d2006-12-04 21:35:24 +00001885}
1886
Chris Lattner229907c2011-07-18 04:54:35 +00001887Constant *ConstantExpr::getSizeOf(Type* Ty) {
Owen Anderson487375e2009-07-29 18:55:55 +00001888 // sizeof is implemented as: (i64) gep (Ty*)null, 1
1889 // Note that a non-inbounds gep is used, as null isn't within any object.
Owen Anderson55f1c092009-08-13 21:58:54 +00001890 Constant *GEPIdx = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
Owen Anderson487375e2009-07-29 18:55:55 +00001891 Constant *GEP = getGetElementPtr(
David Blaikie4a2e73b2015-04-02 18:55:32 +00001892 Ty, Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx);
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001893 return getPtrToInt(GEP,
1894 Type::getInt64Ty(Ty->getContext()));
Owen Anderson487375e2009-07-29 18:55:55 +00001895}
1896
Chris Lattner229907c2011-07-18 04:54:35 +00001897Constant *ConstantExpr::getAlignOf(Type* Ty) {
Dan Gohmancf913832010-01-28 02:15:55 +00001898 // alignof is implemented as: (i64) gep ({i1,Ty}*)null, 0, 1
Dan Gohman50c09d02009-08-11 17:57:01 +00001899 // Note that a non-inbounds gep is used, as null isn't within any object.
Chris Lattner229907c2011-07-18 04:54:35 +00001900 Type *AligningTy =
Reid Kleckner971c3ea2014-11-13 22:55:19 +00001901 StructType::get(Type::getInt1Ty(Ty->getContext()), Ty, nullptr);
Matt Arsenaultbe558882014-04-23 20:58:57 +00001902 Constant *NullPtr = Constant::getNullValue(AligningTy->getPointerTo(0));
Dan Gohmana9be7392010-01-28 02:43:22 +00001903 Constant *Zero = ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0);
Owen Anderson55f1c092009-08-13 21:58:54 +00001904 Constant *One = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
Owen Anderson487375e2009-07-29 18:55:55 +00001905 Constant *Indices[2] = { Zero, One };
David Blaikie4a2e73b2015-04-02 18:55:32 +00001906 Constant *GEP = getGetElementPtr(AligningTy, NullPtr, Indices);
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001907 return getPtrToInt(GEP,
1908 Type::getInt64Ty(Ty->getContext()));
Owen Anderson487375e2009-07-29 18:55:55 +00001909}
1910
Chris Lattner229907c2011-07-18 04:54:35 +00001911Constant *ConstantExpr::getOffsetOf(StructType* STy, unsigned FieldNo) {
Dan Gohmanede94e62010-02-01 16:37:38 +00001912 return getOffsetOf(STy, ConstantInt::get(Type::getInt32Ty(STy->getContext()),
1913 FieldNo));
1914}
1915
Chris Lattner229907c2011-07-18 04:54:35 +00001916Constant *ConstantExpr::getOffsetOf(Type* Ty, Constant *FieldNo) {
Dan Gohmanff3af7252009-08-16 21:26:11 +00001917 // offsetof is implemented as: (i64) gep (Ty*)null, 0, FieldNo
1918 // Note that a non-inbounds gep is used, as null isn't within any object.
1919 Constant *GEPIdx[] = {
Dan Gohmanede94e62010-02-01 16:37:38 +00001920 ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0),
1921 FieldNo
Dan Gohmanff3af7252009-08-16 21:26:11 +00001922 };
1923 Constant *GEP = getGetElementPtr(
David Blaikie4a2e73b2015-04-02 18:55:32 +00001924 Ty, Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx);
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001925 return getPtrToInt(GEP,
1926 Type::getInt64Ty(Ty->getContext()));
Dan Gohmanff3af7252009-08-16 21:26:11 +00001927}
Owen Anderson487375e2009-07-29 18:55:55 +00001928
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001929Constant *ConstantExpr::getCompare(unsigned short Predicate, Constant *C1,
1930 Constant *C2, bool OnlyIfReduced) {
Reid Spencera009d0d2006-12-04 21:35:24 +00001931 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Galina Kistanovafc259902012-07-13 01:25:27 +00001932
Chris Lattner887ecac2011-07-09 18:23:52 +00001933 switch (Predicate) {
1934 default: llvm_unreachable("Invalid CmpInst predicate");
1935 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
1936 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
1937 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
1938 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
1939 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
1940 case CmpInst::FCMP_TRUE:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001941 return getFCmp(Predicate, C1, C2, OnlyIfReduced);
Galina Kistanovafc259902012-07-13 01:25:27 +00001942
Chris Lattner887ecac2011-07-09 18:23:52 +00001943 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
1944 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
1945 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
1946 case CmpInst::ICMP_SLE:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001947 return getICmp(Predicate, C1, C2, OnlyIfReduced);
Chris Lattner887ecac2011-07-09 18:23:52 +00001948 }
Chris Lattner29ca2c62004-08-04 18:50:09 +00001949}
1950
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001951Constant *ConstantExpr::getSelect(Constant *C, Constant *V1, Constant *V2,
1952 Type *OnlyIfReducedTy) {
Chris Lattner41632132008-12-29 00:16:12 +00001953 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner6e415c02004-03-12 05:54:04 +00001954
Chris Lattner887ecac2011-07-09 18:23:52 +00001955 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1956 return SC; // Fold common cases
Chris Lattner6e415c02004-03-12 05:54:04 +00001957
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001958 if (OnlyIfReducedTy == V1->getType())
1959 return nullptr;
1960
Benjamin Kramer324322b2013-03-07 20:53:34 +00001961 Constant *ArgVec[] = { C, V1, V2 };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001962 ConstantExprKeyType Key(Instruction::Select, ArgVec);
Galina Kistanovafc259902012-07-13 01:25:27 +00001963
Chris Lattner887ecac2011-07-09 18:23:52 +00001964 LLVMContextImpl *pImpl = C->getContext().pImpl;
1965 return pImpl->ExprConstants.getOrCreate(V1->getType(), Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00001966}
1967
David Blaikie4a2e73b2015-04-02 18:55:32 +00001968Constant *ConstantExpr::getGetElementPtr(Type *Ty, Constant *C,
1969 ArrayRef<Value *> Idxs, bool InBounds,
1970 Type *OnlyIfReducedTy) {
David Blaikie4a2e73b2015-04-02 18:55:32 +00001971 if (!Ty)
1972 Ty = cast<PointerType>(C->getType()->getScalarType())->getElementType();
1973 else
David Blaikied9d900c2015-05-07 17:28:58 +00001974 assert(
1975 Ty ==
1976 cast<PointerType>(C->getType()->getScalarType())->getContainedType(0u));
1977
1978 if (Constant *FC = ConstantFoldGetElementPtr(Ty, C, InBounds, Idxs))
1979 return FC; // Fold a few common cases.
1980
Chris Lattner887ecac2011-07-09 18:23:52 +00001981 // Get the result type of the getelementptr!
David Blaikie4a2e73b2015-04-02 18:55:32 +00001982 Type *DestTy = GetElementPtrInst::getIndexedType(Ty, Idxs);
1983 assert(DestTy && "GEP indices invalid!");
Chris Lattner8326bd82012-01-26 00:42:34 +00001984 unsigned AS = C->getType()->getPointerAddressSpace();
David Blaikie4a2e73b2015-04-02 18:55:32 +00001985 Type *ReqTy = DestTy->getPointerTo(AS);
Duncan Sandse6beec62012-11-13 12:59:33 +00001986 if (VectorType *VecTy = dyn_cast<VectorType>(C->getType()))
1987 ReqTy = VectorType::get(ReqTy, VecTy->getNumElements());
Galina Kistanovafc259902012-07-13 01:25:27 +00001988
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001989 if (OnlyIfReducedTy == ReqTy)
1990 return nullptr;
1991
Dan Gohman1b849082009-09-07 23:54:19 +00001992 // Look up the constant in the table first to ensure uniqueness
1993 std::vector<Constant*> ArgVec;
Jay Foaded8db7d2011-07-21 14:31:17 +00001994 ArgVec.reserve(1 + Idxs.size());
Dan Gohman1b849082009-09-07 23:54:19 +00001995 ArgVec.push_back(C);
Duncan Sandse6beec62012-11-13 12:59:33 +00001996 for (unsigned i = 0, e = Idxs.size(); i != e; ++i) {
1997 assert(Idxs[i]->getType()->isVectorTy() == ReqTy->isVectorTy() &&
1998 "getelementptr index type missmatch");
1999 assert((!Idxs[i]->getType()->isVectorTy() ||
2000 ReqTy->getVectorNumElements() ==
2001 Idxs[i]->getType()->getVectorNumElements()) &&
2002 "getelementptr index type missmatch");
Dan Gohman1b849082009-09-07 23:54:19 +00002003 ArgVec.push_back(cast<Constant>(Idxs[i]));
Duncan Sandse6beec62012-11-13 12:59:33 +00002004 }
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002005 const ConstantExprKeyType Key(Instruction::GetElementPtr, ArgVec, 0,
David Blaikie60310f22015-05-08 00:42:26 +00002006 InBounds ? GEPOperator::IsInBounds : 0, None,
2007 Ty);
Galina Kistanovafc259902012-07-13 01:25:27 +00002008
Chris Lattner887ecac2011-07-09 18:23:52 +00002009 LLVMContextImpl *pImpl = C->getContext().pImpl;
Dan Gohman1b849082009-09-07 23:54:19 +00002010 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
2011}
2012
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002013Constant *ConstantExpr::getICmp(unsigned short pred, Constant *LHS,
2014 Constant *RHS, bool OnlyIfReduced) {
Reid Spenceree3c9912006-12-04 05:19:50 +00002015 assert(LHS->getType() == RHS->getType());
2016 assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
2017 pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
2018
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00002019 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002020 return FC; // Fold a few common cases...
2021
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002022 if (OnlyIfReduced)
2023 return nullptr;
2024
Reid Spenceree3c9912006-12-04 05:19:50 +00002025 // Look up the constant in the table first to ensure uniqueness
Benjamin Kramer324322b2013-03-07 20:53:34 +00002026 Constant *ArgVec[] = { LHS, RHS };
Reid Spencerb1537492006-12-24 18:42:29 +00002027 // Get the key type with both the opcode and predicate
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002028 const ConstantExprKeyType Key(Instruction::ICmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00002029
Chris Lattner229907c2011-07-18 04:54:35 +00002030 Type *ResultTy = Type::getInt1Ty(LHS->getContext());
2031 if (VectorType *VT = dyn_cast<VectorType>(LHS->getType()))
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00002032 ResultTy = VectorType::get(ResultTy, VT->getNumElements());
2033
Owen Anderson1584a292009-08-04 20:25:11 +00002034 LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl;
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00002035 return pImpl->ExprConstants.getOrCreate(ResultTy, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002036}
2037
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002038Constant *ConstantExpr::getFCmp(unsigned short pred, Constant *LHS,
2039 Constant *RHS, bool OnlyIfReduced) {
Reid Spenceree3c9912006-12-04 05:19:50 +00002040 assert(LHS->getType() == RHS->getType());
2041 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
2042
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00002043 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002044 return FC; // Fold a few common cases...
2045
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002046 if (OnlyIfReduced)
2047 return nullptr;
2048
Reid Spenceree3c9912006-12-04 05:19:50 +00002049 // Look up the constant in the table first to ensure uniqueness
Benjamin Kramer324322b2013-03-07 20:53:34 +00002050 Constant *ArgVec[] = { LHS, RHS };
Reid Spencerb1537492006-12-24 18:42:29 +00002051 // Get the key type with both the opcode and predicate
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002052 const ConstantExprKeyType Key(Instruction::FCmp, ArgVec, pred);
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00002053
Chris Lattner229907c2011-07-18 04:54:35 +00002054 Type *ResultTy = Type::getInt1Ty(LHS->getContext());
2055 if (VectorType *VT = dyn_cast<VectorType>(LHS->getType()))
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00002056 ResultTy = VectorType::get(ResultTy, VT->getNumElements());
2057
Owen Anderson1584a292009-08-04 20:25:11 +00002058 LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl;
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00002059 return pImpl->ExprConstants.getOrCreate(ResultTy, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002060}
2061
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002062Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx,
2063 Type *OnlyIfReducedTy) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002064 assert(Val->getType()->isVectorTy() &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002065 "Tried to create extractelement operation on non-vector type!");
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00002066 assert(Idx->getType()->isIntegerTy() &&
2067 "Extractelement index must be an integer type!");
Galina Kistanovafc259902012-07-13 01:25:27 +00002068
Chris Lattner887ecac2011-07-09 18:23:52 +00002069 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
Chris Lattner09660c92009-12-30 20:25:09 +00002070 return FC; // Fold a few common cases.
Galina Kistanovafc259902012-07-13 01:25:27 +00002071
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002072 Type *ReqTy = Val->getType()->getVectorElementType();
2073 if (OnlyIfReducedTy == ReqTy)
2074 return nullptr;
2075
Robert Bocchinoca27f032006-01-17 20:07:22 +00002076 // Look up the constant in the table first to ensure uniqueness
Benjamin Kramer324322b2013-03-07 20:53:34 +00002077 Constant *ArgVec[] = { Val, Idx };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002078 const ConstantExprKeyType Key(Instruction::ExtractElement, ArgVec);
Galina Kistanovafc259902012-07-13 01:25:27 +00002079
Chris Lattner887ecac2011-07-09 18:23:52 +00002080 LLVMContextImpl *pImpl = Val->getContext().pImpl;
Owen Anderson1584a292009-08-04 20:25:11 +00002081 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00002082}
2083
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002084Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
2085 Constant *Idx, Type *OnlyIfReducedTy) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002086 assert(Val->getType()->isVectorTy() &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002087 "Tried to create insertelement operation on non-vector type!");
Chris Lattner8326bd82012-01-26 00:42:34 +00002088 assert(Elt->getType() == Val->getType()->getVectorElementType() &&
2089 "Insertelement types must match!");
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00002090 assert(Idx->getType()->isIntegerTy() &&
Reid Spencer2546b762007-01-26 07:37:34 +00002091 "Insertelement index must be i32 type!");
Robert Bocchinoca27f032006-01-17 20:07:22 +00002092
Chris Lattner887ecac2011-07-09 18:23:52 +00002093 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
2094 return FC; // Fold a few common cases.
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002095
2096 if (OnlyIfReducedTy == Val->getType())
2097 return nullptr;
2098
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002099 // Look up the constant in the table first to ensure uniqueness
Benjamin Kramer324322b2013-03-07 20:53:34 +00002100 Constant *ArgVec[] = { Val, Elt, Idx };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002101 const ConstantExprKeyType Key(Instruction::InsertElement, ArgVec);
Galina Kistanovafc259902012-07-13 01:25:27 +00002102
Chris Lattner887ecac2011-07-09 18:23:52 +00002103 LLVMContextImpl *pImpl = Val->getContext().pImpl;
2104 return pImpl->ExprConstants.getOrCreate(Val->getType(), Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002105}
2106
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002107Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
2108 Constant *Mask, Type *OnlyIfReducedTy) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002109 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
2110 "Invalid shuffle vector constant expr operands!");
Nate Begeman94aa38d2009-02-12 21:28:33 +00002111
Chris Lattner887ecac2011-07-09 18:23:52 +00002112 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
2113 return FC; // Fold a few common cases.
2114
Chris Lattner8326bd82012-01-26 00:42:34 +00002115 unsigned NElts = Mask->getType()->getVectorNumElements();
2116 Type *EltTy = V1->getType()->getVectorElementType();
Chris Lattner229907c2011-07-18 04:54:35 +00002117 Type *ShufTy = VectorType::get(EltTy, NElts);
Chris Lattner887ecac2011-07-09 18:23:52 +00002118
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002119 if (OnlyIfReducedTy == ShufTy)
2120 return nullptr;
2121
Chris Lattner887ecac2011-07-09 18:23:52 +00002122 // Look up the constant in the table first to ensure uniqueness
Benjamin Kramer324322b2013-03-07 20:53:34 +00002123 Constant *ArgVec[] = { V1, V2, Mask };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002124 const ConstantExprKeyType Key(Instruction::ShuffleVector, ArgVec);
Galina Kistanovafc259902012-07-13 01:25:27 +00002125
Chris Lattner887ecac2011-07-09 18:23:52 +00002126 LLVMContextImpl *pImpl = ShufTy->getContext().pImpl;
2127 return pImpl->ExprConstants.getOrCreate(ShufTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002128}
2129
Chris Lattner887ecac2011-07-09 18:23:52 +00002130Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002131 ArrayRef<unsigned> Idxs,
2132 Type *OnlyIfReducedTy) {
Hal Finkelb31366d2013-07-10 22:51:01 +00002133 assert(Agg->getType()->isFirstClassType() &&
2134 "Non-first-class type for constant insertvalue expression");
2135
Jay Foad57aa6362011-07-13 10:26:04 +00002136 assert(ExtractValueInst::getIndexedType(Agg->getType(),
2137 Idxs) == Val->getType() &&
Dan Gohman12fce772008-05-15 19:50:34 +00002138 "insertvalue indices invalid!");
Hal Finkelb31366d2013-07-10 22:51:01 +00002139 Type *ReqTy = Val->getType();
2140
2141 if (Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs))
2142 return FC;
2143
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002144 if (OnlyIfReducedTy == ReqTy)
2145 return nullptr;
2146
Hal Finkelb31366d2013-07-10 22:51:01 +00002147 Constant *ArgVec[] = { Agg, Val };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002148 const ConstantExprKeyType Key(Instruction::InsertValue, ArgVec, 0, 0, Idxs);
Hal Finkelb31366d2013-07-10 22:51:01 +00002149
2150 LLVMContextImpl *pImpl = Agg->getContext().pImpl;
2151 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Dan Gohman12fce772008-05-15 19:50:34 +00002152}
2153
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002154Constant *ConstantExpr::getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs,
2155 Type *OnlyIfReducedTy) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002156 assert(Agg->getType()->isFirstClassType() &&
Chris Lattner887ecac2011-07-09 18:23:52 +00002157 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002158
Chris Lattner229907c2011-07-18 04:54:35 +00002159 Type *ReqTy = ExtractValueInst::getIndexedType(Agg->getType(), Idxs);
Chandler Carruth9db56b82011-07-10 09:45:35 +00002160 (void)ReqTy;
Chris Lattner887ecac2011-07-09 18:23:52 +00002161 assert(ReqTy && "extractvalue indices invalid!");
Galina Kistanovafc259902012-07-13 01:25:27 +00002162
Dan Gohman0752bff2008-05-23 00:36:11 +00002163 assert(Agg->getType()->isFirstClassType() &&
2164 "Non-first-class type for constant extractvalue expression");
Hal Finkelb31366d2013-07-10 22:51:01 +00002165 if (Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs))
2166 return FC;
2167
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002168 if (OnlyIfReducedTy == ReqTy)
2169 return nullptr;
2170
Hal Finkelb31366d2013-07-10 22:51:01 +00002171 Constant *ArgVec[] = { Agg };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002172 const ConstantExprKeyType Key(Instruction::ExtractValue, ArgVec, 0, 0, Idxs);
Hal Finkelb31366d2013-07-10 22:51:01 +00002173
2174 LLVMContextImpl *pImpl = Agg->getContext().pImpl;
2175 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Dan Gohman12fce772008-05-15 19:50:34 +00002176}
2177
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002178Constant *ConstantExpr::getNeg(Constant *C, bool HasNUW, bool HasNSW) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002179 assert(C->getType()->isIntOrIntVectorTy() &&
Owen Anderson487375e2009-07-29 18:55:55 +00002180 "Cannot NEG a nonintegral value!");
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002181 return getSub(ConstantFP::getZeroValueForNegation(C->getType()),
2182 C, HasNUW, HasNSW);
Owen Anderson487375e2009-07-29 18:55:55 +00002183}
2184
Chris Lattnera676c0f2011-02-07 16:40:21 +00002185Constant *ConstantExpr::getFNeg(Constant *C) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002186 assert(C->getType()->isFPOrFPVectorTy() &&
Owen Anderson487375e2009-07-29 18:55:55 +00002187 "Cannot FNEG a non-floating-point value!");
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002188 return getFSub(ConstantFP::getZeroValueForNegation(C->getType()), C);
Owen Anderson487375e2009-07-29 18:55:55 +00002189}
2190
Chris Lattnera676c0f2011-02-07 16:40:21 +00002191Constant *ConstantExpr::getNot(Constant *C) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002192 assert(C->getType()->isIntOrIntVectorTy() &&
Owen Anderson487375e2009-07-29 18:55:55 +00002193 "Cannot NOT a nonintegral value!");
Owen Anderson5a1acd92009-07-31 20:28:14 +00002194 return get(Instruction::Xor, C, Constant::getAllOnesValue(C->getType()));
Owen Anderson487375e2009-07-29 18:55:55 +00002195}
2196
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002197Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2,
2198 bool HasNUW, bool HasNSW) {
2199 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2200 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
2201 return get(Instruction::Add, C1, C2, Flags);
Owen Anderson487375e2009-07-29 18:55:55 +00002202}
2203
Chris Lattnera676c0f2011-02-07 16:40:21 +00002204Constant *ConstantExpr::getFAdd(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002205 return get(Instruction::FAdd, C1, C2);
2206}
2207
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002208Constant *ConstantExpr::getSub(Constant *C1, Constant *C2,
2209 bool HasNUW, bool HasNSW) {
2210 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2211 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
2212 return get(Instruction::Sub, C1, C2, Flags);
Owen Anderson487375e2009-07-29 18:55:55 +00002213}
2214
Chris Lattnera676c0f2011-02-07 16:40:21 +00002215Constant *ConstantExpr::getFSub(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002216 return get(Instruction::FSub, C1, C2);
2217}
2218
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002219Constant *ConstantExpr::getMul(Constant *C1, Constant *C2,
2220 bool HasNUW, bool HasNSW) {
2221 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2222 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
2223 return get(Instruction::Mul, C1, C2, Flags);
Owen Anderson487375e2009-07-29 18:55:55 +00002224}
2225
Chris Lattnera676c0f2011-02-07 16:40:21 +00002226Constant *ConstantExpr::getFMul(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002227 return get(Instruction::FMul, C1, C2);
2228}
2229
Chris Lattner0d75eac2011-02-09 16:43:07 +00002230Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2, bool isExact) {
2231 return get(Instruction::UDiv, C1, C2,
2232 isExact ? PossiblyExactOperator::IsExact : 0);
Owen Anderson487375e2009-07-29 18:55:55 +00002233}
2234
Chris Lattner0d75eac2011-02-09 16:43:07 +00002235Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2, bool isExact) {
2236 return get(Instruction::SDiv, C1, C2,
2237 isExact ? PossiblyExactOperator::IsExact : 0);
Owen Anderson487375e2009-07-29 18:55:55 +00002238}
2239
Chris Lattnera676c0f2011-02-07 16:40:21 +00002240Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002241 return get(Instruction::FDiv, C1, C2);
2242}
2243
Chris Lattnera676c0f2011-02-07 16:40:21 +00002244Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002245 return get(Instruction::URem, C1, C2);
2246}
2247
Chris Lattnera676c0f2011-02-07 16:40:21 +00002248Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002249 return get(Instruction::SRem, C1, C2);
2250}
2251
Chris Lattnera676c0f2011-02-07 16:40:21 +00002252Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002253 return get(Instruction::FRem, C1, C2);
2254}
2255
Chris Lattnera676c0f2011-02-07 16:40:21 +00002256Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002257 return get(Instruction::And, C1, C2);
2258}
2259
Chris Lattnera676c0f2011-02-07 16:40:21 +00002260Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002261 return get(Instruction::Or, C1, C2);
2262}
2263
Chris Lattnera676c0f2011-02-07 16:40:21 +00002264Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002265 return get(Instruction::Xor, C1, C2);
2266}
2267
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002268Constant *ConstantExpr::getShl(Constant *C1, Constant *C2,
2269 bool HasNUW, bool HasNSW) {
2270 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2271 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
2272 return get(Instruction::Shl, C1, C2, Flags);
Owen Anderson487375e2009-07-29 18:55:55 +00002273}
2274
Chris Lattner0d75eac2011-02-09 16:43:07 +00002275Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2, bool isExact) {
2276 return get(Instruction::LShr, C1, C2,
2277 isExact ? PossiblyExactOperator::IsExact : 0);
Owen Anderson487375e2009-07-29 18:55:55 +00002278}
2279
Chris Lattner0d75eac2011-02-09 16:43:07 +00002280Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2, bool isExact) {
2281 return get(Instruction::AShr, C1, C2,
2282 isExact ? PossiblyExactOperator::IsExact : 0);
Owen Anderson487375e2009-07-29 18:55:55 +00002283}
2284
Duncan Sandsd7aeefe2012-06-12 14:33:56 +00002285/// getBinOpIdentity - Return the identity for the given binary operation,
2286/// i.e. a constant C such that X op C = X and C op X = X for every X. It
Duncan Sands318a89d2012-06-13 09:42:13 +00002287/// returns null if the operator doesn't have an identity.
Duncan Sandsd7aeefe2012-06-12 14:33:56 +00002288Constant *ConstantExpr::getBinOpIdentity(unsigned Opcode, Type *Ty) {
2289 switch (Opcode) {
2290 default:
Duncan Sands318a89d2012-06-13 09:42:13 +00002291 // Doesn't have an identity.
Craig Topperc6207612014-04-09 06:08:46 +00002292 return nullptr;
Duncan Sands318a89d2012-06-13 09:42:13 +00002293
Duncan Sandsd7aeefe2012-06-12 14:33:56 +00002294 case Instruction::Add:
2295 case Instruction::Or:
2296 case Instruction::Xor:
2297 return Constant::getNullValue(Ty);
2298
2299 case Instruction::Mul:
2300 return ConstantInt::get(Ty, 1);
2301
2302 case Instruction::And:
2303 return Constant::getAllOnesValue(Ty);
2304 }
2305}
2306
Duncan Sands318a89d2012-06-13 09:42:13 +00002307/// getBinOpAbsorber - Return the absorbing element for the given binary
2308/// operation, i.e. a constant C such that X op C = C and C op X = C for
2309/// every X. For example, this returns zero for integer multiplication.
2310/// It returns null if the operator doesn't have an absorbing element.
2311Constant *ConstantExpr::getBinOpAbsorber(unsigned Opcode, Type *Ty) {
2312 switch (Opcode) {
2313 default:
2314 // Doesn't have an absorber.
Craig Topperc6207612014-04-09 06:08:46 +00002315 return nullptr;
Duncan Sands318a89d2012-06-13 09:42:13 +00002316
2317 case Instruction::Or:
2318 return Constant::getAllOnesValue(Ty);
2319
2320 case Instruction::And:
2321 case Instruction::Mul:
2322 return Constant::getNullValue(Ty);
2323 }
2324}
2325
Vikram S. Adve4c485332002-07-15 18:19:33 +00002326// destroyConstant - Remove the constant from the constant table...
2327//
Pete Cooper86dd4cf2015-06-23 21:55:11 +00002328void ConstantExpr::destroyConstantImpl() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002329 getType()->getContext().pImpl->ExprConstants.remove(this);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002330}
2331
Chris Lattner3cd8c562002-07-30 18:54:25 +00002332const char *ConstantExpr::getOpcodeName() const {
2333 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002334}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00002335
David Blaikie60310f22015-05-08 00:42:26 +00002336GetElementPtrConstantExpr::GetElementPtrConstantExpr(
2337 Type *SrcElementTy, Constant *C, ArrayRef<Constant *> IdxList, Type *DestTy)
2338 : ConstantExpr(DestTy, Instruction::GetElementPtr,
2339 OperandTraits<GetElementPtrConstantExpr>::op_end(this) -
2340 (IdxList.size() + 1),
2341 IdxList.size() + 1),
Eduard Burtescu19eb0312016-01-19 17:28:00 +00002342 SrcElementTy(SrcElementTy),
2343 ResElementTy(GetElementPtrInst::getIndexedType(SrcElementTy, IdxList)) {
Pete Coopereb31b682015-05-21 22:48:54 +00002344 Op<0>() = C;
Pete Cooper74510a42015-06-12 17:48:05 +00002345 Use *OperandList = getOperandList();
Chris Lattnera3b94ba2010-03-30 20:48:48 +00002346 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
2347 OperandList[i+1] = IdxList[i];
2348}
2349
David Blaikie60310f22015-05-08 00:42:26 +00002350Type *GetElementPtrConstantExpr::getSourceElementType() const {
2351 return SrcElementTy;
2352}
2353
Eduard Burtescu19eb0312016-01-19 17:28:00 +00002354Type *GetElementPtrConstantExpr::getResultElementType() const {
2355 return ResElementTy;
2356}
2357
Chris Lattner3756b912012-01-23 22:57:10 +00002358//===----------------------------------------------------------------------===//
2359// ConstantData* implementations
2360
2361void ConstantDataArray::anchor() {}
2362void ConstantDataVector::anchor() {}
2363
Chris Lattnere4f3f102012-01-24 04:43:41 +00002364/// getElementType - Return the element type of the array/vector.
2365Type *ConstantDataSequential::getElementType() const {
2366 return getType()->getElementType();
2367}
2368
Chris Lattner5d4497b2012-01-24 09:31:43 +00002369StringRef ConstantDataSequential::getRawDataValues() const {
Chris Lattner00245f42012-01-24 13:41:11 +00002370 return StringRef(DataElements, getNumElements()*getElementByteSize());
Chris Lattner5d4497b2012-01-24 09:31:43 +00002371}
2372
Chris Lattner030af792012-01-24 05:42:11 +00002373/// isElementTypeCompatible - Return true if a ConstantDataSequential can be
2374/// formed with a vector or array of the specified element type.
2375/// ConstantDataArray only works with normal float and int types that are
2376/// stored densely in memory, not with things like i42 or x86_f80.
Craig Toppere3dcce92015-08-01 22:20:21 +00002377bool ConstantDataSequential::isElementTypeCompatible(Type *Ty) {
Justin Bogner0ebc8602015-12-08 03:01:16 +00002378 if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) return true;
Craig Toppere3dcce92015-08-01 22:20:21 +00002379 if (auto *IT = dyn_cast<IntegerType>(Ty)) {
Chris Lattnere4f3f102012-01-24 04:43:41 +00002380 switch (IT->getBitWidth()) {
2381 case 8:
2382 case 16:
2383 case 32:
2384 case 64:
2385 return true;
2386 default: break;
2387 }
2388 }
2389 return false;
2390}
2391
Chris Lattner00245f42012-01-24 13:41:11 +00002392/// getNumElements - Return the number of elements in the array or vector.
2393unsigned ConstantDataSequential::getNumElements() const {
Chris Lattner8a3df542012-01-25 01:32:59 +00002394 if (ArrayType *AT = dyn_cast<ArrayType>(getType()))
2395 return AT->getNumElements();
Chris Lattner8326bd82012-01-26 00:42:34 +00002396 return getType()->getVectorNumElements();
Chris Lattner00245f42012-01-24 13:41:11 +00002397}
2398
2399
Chris Lattnere4f3f102012-01-24 04:43:41 +00002400/// getElementByteSize - Return the size in bytes of the elements in the data.
2401uint64_t ConstantDataSequential::getElementByteSize() const {
2402 return getElementType()->getPrimitiveSizeInBits()/8;
2403}
2404
2405/// getElementPointer - Return the start of the specified element.
2406const char *ConstantDataSequential::getElementPointer(unsigned Elt) const {
Chris Lattner00245f42012-01-24 13:41:11 +00002407 assert(Elt < getNumElements() && "Invalid Elt");
Chris Lattnere4f3f102012-01-24 04:43:41 +00002408 return DataElements+Elt*getElementByteSize();
2409}
2410
2411
Chris Lattner3756b912012-01-23 22:57:10 +00002412/// isAllZeros - return true if the array is empty or all zeros.
2413static bool isAllZeros(StringRef Arr) {
2414 for (StringRef::iterator I = Arr.begin(), E = Arr.end(); I != E; ++I)
2415 if (*I != 0)
2416 return false;
2417 return true;
2418}
Chris Lattner030af792012-01-24 05:42:11 +00002419
Chris Lattner3756b912012-01-23 22:57:10 +00002420/// getImpl - This is the underlying implementation of all of the
2421/// ConstantDataSequential::get methods. They all thunk down to here, providing
Chris Lattnerf06039b2012-01-30 18:19:30 +00002422/// the correct element type. We take the bytes in as a StringRef because
Chris Lattner3756b912012-01-23 22:57:10 +00002423/// we *want* an underlying "char*" to avoid TBAA type punning violations.
2424Constant *ConstantDataSequential::getImpl(StringRef Elements, Type *Ty) {
Chris Lattner8326bd82012-01-26 00:42:34 +00002425 assert(isElementTypeCompatible(Ty->getSequentialElementType()));
Chris Lattner139822f2012-01-24 14:17:05 +00002426 // If the elements are all zero or there are no elements, return a CAZ, which
2427 // is more dense and canonical.
Chris Lattner3756b912012-01-23 22:57:10 +00002428 if (isAllZeros(Elements))
2429 return ConstantAggregateZero::get(Ty);
2430
2431 // Do a lookup to see if we have already formed one of these.
David Blaikie5106ce72014-11-19 05:49:42 +00002432 auto &Slot =
2433 *Ty->getContext()
2434 .pImpl->CDSConstants.insert(std::make_pair(Elements, nullptr))
2435 .first;
Galina Kistanovafc259902012-07-13 01:25:27 +00002436
Chris Lattner3756b912012-01-23 22:57:10 +00002437 // The bucket can point to a linked list of different CDS's that have the same
2438 // body but different types. For example, 0,0,0,1 could be a 4 element array
2439 // of i8, or a 1-element array of i32. They'll both end up in the same
2440 /// StringMap bucket, linked up by their Next pointers. Walk the list.
David Blaikie5106ce72014-11-19 05:49:42 +00002441 ConstantDataSequential **Entry = &Slot.second;
Craig Topperc6207612014-04-09 06:08:46 +00002442 for (ConstantDataSequential *Node = *Entry; Node;
Chris Lattner3756b912012-01-23 22:57:10 +00002443 Entry = &Node->Next, Node = *Entry)
2444 if (Node->getType() == Ty)
2445 return Node;
Galina Kistanovafc259902012-07-13 01:25:27 +00002446
Chris Lattner3756b912012-01-23 22:57:10 +00002447 // Okay, we didn't get a hit. Create a node of the right class, link it in,
2448 // and return it.
2449 if (isa<ArrayType>(Ty))
David Blaikie5106ce72014-11-19 05:49:42 +00002450 return *Entry = new ConstantDataArray(Ty, Slot.first().data());
Chris Lattner3756b912012-01-23 22:57:10 +00002451
2452 assert(isa<VectorType>(Ty));
David Blaikie5106ce72014-11-19 05:49:42 +00002453 return *Entry = new ConstantDataVector(Ty, Slot.first().data());
Chris Lattner3756b912012-01-23 22:57:10 +00002454}
2455
Pete Cooper86dd4cf2015-06-23 21:55:11 +00002456void ConstantDataSequential::destroyConstantImpl() {
Chris Lattner3756b912012-01-23 22:57:10 +00002457 // Remove the constant from the StringMap.
2458 StringMap<ConstantDataSequential*> &CDSConstants =
2459 getType()->getContext().pImpl->CDSConstants;
Galina Kistanovafc259902012-07-13 01:25:27 +00002460
Chris Lattner3756b912012-01-23 22:57:10 +00002461 StringMap<ConstantDataSequential*>::iterator Slot =
Chris Lattner5d4497b2012-01-24 09:31:43 +00002462 CDSConstants.find(getRawDataValues());
Chris Lattner3756b912012-01-23 22:57:10 +00002463
2464 assert(Slot != CDSConstants.end() && "CDS not found in uniquing table");
2465
2466 ConstantDataSequential **Entry = &Slot->getValue();
2467
2468 // Remove the entry from the hash table.
Craig Topperc6207612014-04-09 06:08:46 +00002469 if (!(*Entry)->Next) {
Chris Lattner3756b912012-01-23 22:57:10 +00002470 // If there is only one value in the bucket (common case) it must be this
2471 // entry, and removing the entry should remove the bucket completely.
2472 assert((*Entry) == this && "Hash mismatch in ConstantDataSequential");
2473 getContext().pImpl->CDSConstants.erase(Slot);
2474 } else {
2475 // Otherwise, there are multiple entries linked off the bucket, unlink the
2476 // node we care about but keep the bucket around.
2477 for (ConstantDataSequential *Node = *Entry; ;
2478 Entry = &Node->Next, Node = *Entry) {
2479 assert(Node && "Didn't find entry in its uniquing hash table!");
2480 // If we found our entry, unlink it from the list and we're done.
2481 if (Node == this) {
2482 *Entry = Node->Next;
2483 break;
2484 }
2485 }
2486 }
Galina Kistanovafc259902012-07-13 01:25:27 +00002487
Chris Lattner3756b912012-01-23 22:57:10 +00002488 // If we were part of a list, make sure that we don't delete the list that is
2489 // still owned by the uniquing map.
Craig Topperc6207612014-04-09 06:08:46 +00002490 Next = nullptr;
Chris Lattner3756b912012-01-23 22:57:10 +00002491}
2492
2493/// get() constructors - Return a constant with array type with an element
2494/// count and element type matching the ArrayRef passed in. Note that this
2495/// can return a ConstantAggregateZero object.
Chris Lattner20683932012-01-24 14:04:40 +00002496Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint8_t> Elts) {
Chris Lattner3756b912012-01-23 22:57:10 +00002497 Type *Ty = ArrayType::get(Type::getInt8Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002498 const char *Data = reinterpret_cast<const char *>(Elts.data());
2499 return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*1), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002500}
Chris Lattner20683932012-01-24 14:04:40 +00002501Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint16_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002502 Type *Ty = ArrayType::get(Type::getInt16Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002503 const char *Data = reinterpret_cast<const char *>(Elts.data());
2504 return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*2), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002505}
Chris Lattner20683932012-01-24 14:04:40 +00002506Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint32_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002507 Type *Ty = ArrayType::get(Type::getInt32Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002508 const char *Data = reinterpret_cast<const char *>(Elts.data());
2509 return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*4), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002510}
Chris Lattner20683932012-01-24 14:04:40 +00002511Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint64_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002512 Type *Ty = ArrayType::get(Type::getInt64Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002513 const char *Data = reinterpret_cast<const char *>(Elts.data());
2514 return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*8), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002515}
Chris Lattner20683932012-01-24 14:04:40 +00002516Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<float> Elts) {
Chris Lattner3756b912012-01-23 22:57:10 +00002517 Type *Ty = ArrayType::get(Type::getFloatTy(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002518 const char *Data = reinterpret_cast<const char *>(Elts.data());
2519 return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*4), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002520}
Chris Lattner20683932012-01-24 14:04:40 +00002521Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<double> Elts) {
Chris Lattner3756b912012-01-23 22:57:10 +00002522 Type *Ty = ArrayType::get(Type::getDoubleTy(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002523 const char *Data = reinterpret_cast<const char *>(Elts.data());
Rafael Espindola8c97e192015-02-19 16:08:20 +00002524 return getImpl(StringRef(const_cast<char *>(Data), Elts.size() * 8), Ty);
2525}
2526
2527/// getFP() constructors - Return a constant with array type with an element
2528/// count and element type of float with precision matching the number of
2529/// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits,
2530/// double for 64bits) Note that this can return a ConstantAggregateZero
2531/// object.
2532Constant *ConstantDataArray::getFP(LLVMContext &Context,
2533 ArrayRef<uint16_t> Elts) {
Justin Bognerb7389d6712015-12-09 21:21:07 +00002534 Type *Ty = ArrayType::get(Type::getHalfTy(Context), Elts.size());
Rafael Espindola8c97e192015-02-19 16:08:20 +00002535 const char *Data = reinterpret_cast<const char *>(Elts.data());
2536 return getImpl(StringRef(const_cast<char *>(Data), Elts.size() * 2), Ty);
2537}
2538Constant *ConstantDataArray::getFP(LLVMContext &Context,
2539 ArrayRef<uint32_t> Elts) {
2540 Type *Ty = ArrayType::get(Type::getFloatTy(Context), Elts.size());
2541 const char *Data = reinterpret_cast<const char *>(Elts.data());
2542 return getImpl(StringRef(const_cast<char *>(Data), Elts.size() * 4), Ty);
2543}
2544Constant *ConstantDataArray::getFP(LLVMContext &Context,
2545 ArrayRef<uint64_t> Elts) {
2546 Type *Ty = ArrayType::get(Type::getDoubleTy(Context), Elts.size());
2547 const char *Data = reinterpret_cast<const char *>(Elts.data());
2548 return getImpl(StringRef(const_cast<char *>(Data), Elts.size() * 8), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002549}
2550
Chris Lattner20683932012-01-24 14:04:40 +00002551/// getString - This method constructs a CDS and initializes it with a text
2552/// string. The default behavior (AddNull==true) causes a null terminator to
2553/// be placed at the end of the array (increasing the length of the string by
2554/// one more than the StringRef would normally indicate. Pass AddNull=false
2555/// to disable this behavior.
2556Constant *ConstantDataArray::getString(LLVMContext &Context,
2557 StringRef Str, bool AddNull) {
Galina Kistanovafc259902012-07-13 01:25:27 +00002558 if (!AddNull) {
2559 const uint8_t *Data = reinterpret_cast<const uint8_t *>(Str.data());
Craig Toppere1d12942014-08-27 05:25:25 +00002560 return get(Context, makeArrayRef(const_cast<uint8_t *>(Data),
Galina Kistanovafc259902012-07-13 01:25:27 +00002561 Str.size()));
2562 }
2563
Chris Lattner20683932012-01-24 14:04:40 +00002564 SmallVector<uint8_t, 64> ElementVals;
2565 ElementVals.append(Str.begin(), Str.end());
2566 ElementVals.push_back(0);
2567 return get(Context, ElementVals);
2568}
Chris Lattner3756b912012-01-23 22:57:10 +00002569
2570/// get() constructors - Return a constant with vector type with an element
2571/// count and element type matching the ArrayRef passed in. Note that this
2572/// can return a ConstantAggregateZero object.
Chris Lattner20683932012-01-24 14:04:40 +00002573Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint8_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002574 Type *Ty = VectorType::get(Type::getInt8Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002575 const char *Data = reinterpret_cast<const char *>(Elts.data());
2576 return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*1), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002577}
Chris Lattner20683932012-01-24 14:04:40 +00002578Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint16_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002579 Type *Ty = VectorType::get(Type::getInt16Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002580 const char *Data = reinterpret_cast<const char *>(Elts.data());
2581 return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*2), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002582}
Chris Lattner20683932012-01-24 14:04:40 +00002583Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint32_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002584 Type *Ty = VectorType::get(Type::getInt32Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002585 const char *Data = reinterpret_cast<const char *>(Elts.data());
2586 return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*4), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002587}
Chris Lattner20683932012-01-24 14:04:40 +00002588Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint64_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002589 Type *Ty = VectorType::get(Type::getInt64Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002590 const char *Data = reinterpret_cast<const char *>(Elts.data());
2591 return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*8), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002592}
Chris Lattner20683932012-01-24 14:04:40 +00002593Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<float> Elts) {
Chris Lattner3756b912012-01-23 22:57:10 +00002594 Type *Ty = VectorType::get(Type::getFloatTy(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002595 const char *Data = reinterpret_cast<const char *>(Elts.data());
2596 return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*4), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002597}
Chris Lattner20683932012-01-24 14:04:40 +00002598Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<double> Elts) {
Chris Lattner3756b912012-01-23 22:57:10 +00002599 Type *Ty = VectorType::get(Type::getDoubleTy(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002600 const char *Data = reinterpret_cast<const char *>(Elts.data());
Rafael Espindola8c97e192015-02-19 16:08:20 +00002601 return getImpl(StringRef(const_cast<char *>(Data), Elts.size() * 8), Ty);
2602}
2603
2604/// getFP() constructors - Return a constant with vector type with an element
2605/// count and element type of float with the precision matching the number of
2606/// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits,
2607/// double for 64bits) Note that this can return a ConstantAggregateZero
2608/// object.
2609Constant *ConstantDataVector::getFP(LLVMContext &Context,
2610 ArrayRef<uint16_t> Elts) {
2611 Type *Ty = VectorType::get(Type::getHalfTy(Context), Elts.size());
2612 const char *Data = reinterpret_cast<const char *>(Elts.data());
2613 return getImpl(StringRef(const_cast<char *>(Data), Elts.size() * 2), Ty);
2614}
2615Constant *ConstantDataVector::getFP(LLVMContext &Context,
2616 ArrayRef<uint32_t> Elts) {
2617 Type *Ty = VectorType::get(Type::getFloatTy(Context), Elts.size());
2618 const char *Data = reinterpret_cast<const char *>(Elts.data());
2619 return getImpl(StringRef(const_cast<char *>(Data), Elts.size() * 4), Ty);
2620}
2621Constant *ConstantDataVector::getFP(LLVMContext &Context,
2622 ArrayRef<uint64_t> Elts) {
2623 Type *Ty = VectorType::get(Type::getDoubleTy(Context), Elts.size());
2624 const char *Data = reinterpret_cast<const char *>(Elts.data());
2625 return getImpl(StringRef(const_cast<char *>(Data), Elts.size() * 8), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002626}
2627
Chris Lattnere9eed292012-01-25 05:19:54 +00002628Constant *ConstantDataVector::getSplat(unsigned NumElts, Constant *V) {
2629 assert(isElementTypeCompatible(V->getType()) &&
2630 "Element type not compatible with ConstantData");
2631 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
2632 if (CI->getType()->isIntegerTy(8)) {
2633 SmallVector<uint8_t, 16> Elts(NumElts, CI->getZExtValue());
2634 return get(V->getContext(), Elts);
2635 }
2636 if (CI->getType()->isIntegerTy(16)) {
2637 SmallVector<uint16_t, 16> Elts(NumElts, CI->getZExtValue());
2638 return get(V->getContext(), Elts);
2639 }
2640 if (CI->getType()->isIntegerTy(32)) {
2641 SmallVector<uint32_t, 16> Elts(NumElts, CI->getZExtValue());
2642 return get(V->getContext(), Elts);
2643 }
2644 assert(CI->getType()->isIntegerTy(64) && "Unsupported ConstantData type");
2645 SmallVector<uint64_t, 16> Elts(NumElts, CI->getZExtValue());
2646 return get(V->getContext(), Elts);
2647 }
2648
Chris Lattner978fe0c2012-01-30 06:21:21 +00002649 if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
Justin Bogner0ebc8602015-12-08 03:01:16 +00002650 if (CFP->getType()->isHalfTy()) {
2651 SmallVector<uint16_t, 16> Elts(
2652 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
2653 return getFP(V->getContext(), Elts);
2654 }
Chris Lattner978fe0c2012-01-30 06:21:21 +00002655 if (CFP->getType()->isFloatTy()) {
Rafael Espindola8c97e192015-02-19 16:08:20 +00002656 SmallVector<uint32_t, 16> Elts(
2657 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
2658 return getFP(V->getContext(), Elts);
Chris Lattner978fe0c2012-01-30 06:21:21 +00002659 }
2660 if (CFP->getType()->isDoubleTy()) {
Rafael Espindola8c97e192015-02-19 16:08:20 +00002661 SmallVector<uint64_t, 16> Elts(
2662 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
2663 return getFP(V->getContext(), Elts);
Chris Lattner978fe0c2012-01-30 06:21:21 +00002664 }
Chris Lattnere9eed292012-01-25 05:19:54 +00002665 }
Chris Lattner978fe0c2012-01-30 06:21:21 +00002666 return ConstantVector::getSplat(NumElts, V);
Chris Lattnere9eed292012-01-25 05:19:54 +00002667}
2668
2669
Chris Lattnere4f3f102012-01-24 04:43:41 +00002670/// getElementAsInteger - If this is a sequential container of integers (of
2671/// any size), return the specified element in the low bits of a uint64_t.
2672uint64_t ConstantDataSequential::getElementAsInteger(unsigned Elt) const {
2673 assert(isa<IntegerType>(getElementType()) &&
2674 "Accessor can only be used when element is an integer");
2675 const char *EltPtr = getElementPointer(Elt);
Galina Kistanovafc259902012-07-13 01:25:27 +00002676
Chris Lattnere4f3f102012-01-24 04:43:41 +00002677 // The data is stored in host byte order, make sure to cast back to the right
2678 // type to load with the right endianness.
Chris Lattner8326bd82012-01-26 00:42:34 +00002679 switch (getElementType()->getIntegerBitWidth()) {
Craig Topperc514b542012-02-05 22:14:15 +00002680 default: llvm_unreachable("Invalid bitwidth for CDS");
Galina Kistanovafc259902012-07-13 01:25:27 +00002681 case 8:
2682 return *const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(EltPtr));
2683 case 16:
2684 return *const_cast<uint16_t *>(reinterpret_cast<const uint16_t *>(EltPtr));
2685 case 32:
2686 return *const_cast<uint32_t *>(reinterpret_cast<const uint32_t *>(EltPtr));
2687 case 64:
2688 return *const_cast<uint64_t *>(reinterpret_cast<const uint64_t *>(EltPtr));
Chris Lattnere4f3f102012-01-24 04:43:41 +00002689 }
2690}
2691
2692/// getElementAsAPFloat - If this is a sequential container of floating point
2693/// type, return the specified element as an APFloat.
2694APFloat ConstantDataSequential::getElementAsAPFloat(unsigned Elt) const {
2695 const char *EltPtr = getElementPointer(Elt);
2696
2697 switch (getElementType()->getTypeID()) {
Nick Lewyckyff509622012-01-25 03:20:12 +00002698 default:
Craig Topperc514b542012-02-05 22:14:15 +00002699 llvm_unreachable("Accessor can only be used when element is float/double!");
Justin Bogner0ebc8602015-12-08 03:01:16 +00002700 case Type::HalfTyID: {
2701 auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr);
2702 return APFloat(APFloat::IEEEhalf, APInt(16, EltVal));
2703 }
Benjamin Kramer7af984b2015-02-20 15:11:55 +00002704 case Type::FloatTyID: {
2705 auto EltVal = *reinterpret_cast<const uint32_t *>(EltPtr);
NAKAMURA Takumie86b9b72015-02-20 14:24:49 +00002706 return APFloat(APFloat::IEEEsingle, APInt(32, EltVal));
Benjamin Kramer7af984b2015-02-20 15:11:55 +00002707 }
2708 case Type::DoubleTyID: {
2709 auto EltVal = *reinterpret_cast<const uint64_t *>(EltPtr);
NAKAMURA Takumie86b9b72015-02-20 14:24:49 +00002710 return APFloat(APFloat::IEEEdouble, APInt(64, EltVal));
Chris Lattnere4f3f102012-01-24 04:43:41 +00002711 }
Benjamin Kramer7af984b2015-02-20 15:11:55 +00002712 }
Chris Lattnere4f3f102012-01-24 04:43:41 +00002713}
2714
2715/// getElementAsFloat - If this is an sequential container of floats, return
2716/// the specified element as a float.
2717float ConstantDataSequential::getElementAsFloat(unsigned Elt) const {
2718 assert(getElementType()->isFloatTy() &&
2719 "Accessor can only be used when element is a 'float'");
Galina Kistanovafc259902012-07-13 01:25:27 +00002720 const float *EltPtr = reinterpret_cast<const float *>(getElementPointer(Elt));
2721 return *const_cast<float *>(EltPtr);
Chris Lattnere4f3f102012-01-24 04:43:41 +00002722}
2723
2724/// getElementAsDouble - If this is an sequential container of doubles, return
2725/// the specified element as a float.
2726double ConstantDataSequential::getElementAsDouble(unsigned Elt) const {
2727 assert(getElementType()->isDoubleTy() &&
2728 "Accessor can only be used when element is a 'float'");
Galina Kistanovafc259902012-07-13 01:25:27 +00002729 const double *EltPtr =
2730 reinterpret_cast<const double *>(getElementPointer(Elt));
2731 return *const_cast<double *>(EltPtr);
Chris Lattnere4f3f102012-01-24 04:43:41 +00002732}
2733
2734/// getElementAsConstant - Return a Constant for a specified index's element.
2735/// Note that this has to compute a new constant to return, so it isn't as
2736/// efficient as getElementAsInteger/Float/Double.
2737Constant *ConstantDataSequential::getElementAsConstant(unsigned Elt) const {
Justin Bogner0ebc8602015-12-08 03:01:16 +00002738 if (getElementType()->isHalfTy() || getElementType()->isFloatTy() ||
2739 getElementType()->isDoubleTy())
Chris Lattnere4f3f102012-01-24 04:43:41 +00002740 return ConstantFP::get(getContext(), getElementAsAPFloat(Elt));
Galina Kistanovafc259902012-07-13 01:25:27 +00002741
Chris Lattnere4f3f102012-01-24 04:43:41 +00002742 return ConstantInt::get(getElementType(), getElementAsInteger(Elt));
2743}
2744
Chris Lattner5dd4d872012-01-24 09:01:07 +00002745/// isString - This method returns true if this is an array of i8.
2746bool ConstantDataSequential::isString() const {
2747 return isa<ArrayType>(getType()) && getElementType()->isIntegerTy(8);
2748}
Chris Lattner3756b912012-01-23 22:57:10 +00002749
Chris Lattner5dd4d872012-01-24 09:01:07 +00002750/// isCString - This method returns true if the array "isString", ends with a
2751/// nul byte, and does not contains any other nul bytes.
2752bool ConstantDataSequential::isCString() const {
2753 if (!isString())
2754 return false;
Galina Kistanovafc259902012-07-13 01:25:27 +00002755
Chris Lattner5dd4d872012-01-24 09:01:07 +00002756 StringRef Str = getAsString();
Galina Kistanovafc259902012-07-13 01:25:27 +00002757
Chris Lattner5dd4d872012-01-24 09:01:07 +00002758 // The last value must be nul.
2759 if (Str.back() != 0) return false;
Galina Kistanovafc259902012-07-13 01:25:27 +00002760
Chris Lattner5dd4d872012-01-24 09:01:07 +00002761 // Other elements must be non-nul.
2762 return Str.drop_back().find(0) == StringRef::npos;
2763}
Chris Lattner3756b912012-01-23 22:57:10 +00002764
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002765/// getSplatValue - If this is a splat constant, meaning that all of the
Reid Kleckner971c3ea2014-11-13 22:55:19 +00002766/// elements have the same value, return that value. Otherwise return nullptr.
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002767Constant *ConstantDataVector::getSplatValue() const {
2768 const char *Base = getRawDataValues().data();
Galina Kistanovafc259902012-07-13 01:25:27 +00002769
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002770 // Compare elements 1+ to the 0'th element.
2771 unsigned EltSize = getElementByteSize();
2772 for (unsigned i = 1, e = getNumElements(); i != e; ++i)
2773 if (memcmp(Base, Base+i*EltSize, EltSize))
Craig Topperc6207612014-04-09 06:08:46 +00002774 return nullptr;
Galina Kistanovafc259902012-07-13 01:25:27 +00002775
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002776 // If they're all the same, return the 0th one as a representative.
2777 return getElementAsConstant(0);
2778}
Chris Lattnera3b94ba2010-03-30 20:48:48 +00002779
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002780//===----------------------------------------------------------------------===//
Pete Cooper5815b1f2015-06-24 18:55:24 +00002781// handleOperandChange implementations
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002782
Pete Cooper5815b1f2015-06-24 18:55:24 +00002783/// Update this constant array to change uses of
Chris Lattner913849b2007-08-21 00:55:23 +00002784/// 'From' to be uses of 'To'. This must update the uniquing data structures
2785/// etc.
2786///
2787/// Note that we intentionally replace all uses of From with To here. Consider
2788/// a large array that uses 'From' 1000 times. By handling this case all here,
Pete Cooper5815b1f2015-06-24 18:55:24 +00002789/// ConstantArray::handleOperandChange is only invoked once, and that
Chris Lattner913849b2007-08-21 00:55:23 +00002790/// single invocation handles all 1000 uses. Handling them one at a time would
2791/// work, but would be really slow because it would have to unique each updated
2792/// array instance.
Chris Lattner31b132c2009-10-28 00:01:44 +00002793///
Mehdi Amini8914d292016-02-10 22:47:15 +00002794void Constant::handleOperandChange(Value *From, Value *To) {
Pete Cooper5815b1f2015-06-24 18:55:24 +00002795 Value *Replacement = nullptr;
2796 switch (getValueID()) {
2797 default:
2798 llvm_unreachable("Not a constant!");
2799#define HANDLE_CONSTANT(Name) \
2800 case Value::Name##Val: \
Mehdi Amini8914d292016-02-10 22:47:15 +00002801 Replacement = cast<Name>(this)->handleOperandChangeImpl(From, To); \
Pete Cooper5815b1f2015-06-24 18:55:24 +00002802 break;
2803#include "llvm/IR/Value.def"
2804 }
2805
2806 // If handleOperandChangeImpl returned nullptr, then it handled
2807 // replacing itself and we don't want to delete or replace anything else here.
2808 if (!Replacement)
2809 return;
2810
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002811 // I do need to replace this with an existing value.
2812 assert(Replacement != this && "I didn't contain From!");
2813
2814 // Everyone using this now uses the replacement.
2815 replaceAllUsesWith(Replacement);
2816
2817 // Delete the old constant!
2818 destroyConstant();
2819}
2820
Mehdi Amini8914d292016-02-10 22:47:15 +00002821Value *ConstantArray::handleOperandChangeImpl(Value *From, Value *To) {
Owen Andersonc2c79322009-07-28 18:32:17 +00002822 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2823 Constant *ToC = cast<Constant>(To);
2824
Talin46e9b442012-02-05 20:54:10 +00002825 SmallVector<Constant*, 8> Values;
Owen Andersonc2c79322009-07-28 18:32:17 +00002826 Values.reserve(getNumOperands()); // Build replacement array.
2827
Galina Kistanovafc259902012-07-13 01:25:27 +00002828 // Fill values with the modified operands of the constant array. Also,
Owen Andersonc2c79322009-07-28 18:32:17 +00002829 // compute whether this turns into an all-zeros array.
Owen Andersonc2c79322009-07-28 18:32:17 +00002830 unsigned NumUpdated = 0;
Galina Kistanovafc259902012-07-13 01:25:27 +00002831
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002832 // Keep track of whether all the values in the array are "ToC".
2833 bool AllSame = true;
Pete Cooper74510a42015-06-12 17:48:05 +00002834 Use *OperandList = getOperandList();
Mehdi Amini8914d292016-02-10 22:47:15 +00002835 unsigned OperandNo = 0;
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002836 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2837 Constant *Val = cast<Constant>(O->get());
2838 if (Val == From) {
Mehdi Amini8914d292016-02-10 22:47:15 +00002839 OperandNo = (O - OperandList);
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002840 Val = ToC;
2841 ++NumUpdated;
Owen Andersonc2c79322009-07-28 18:32:17 +00002842 }
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002843 Values.push_back(Val);
Talin46e9b442012-02-05 20:54:10 +00002844 AllSame &= Val == ToC;
Owen Andersonc2c79322009-07-28 18:32:17 +00002845 }
Galina Kistanovafc259902012-07-13 01:25:27 +00002846
Pete Cooper5815b1f2015-06-24 18:55:24 +00002847 if (AllSame && ToC->isNullValue())
2848 return ConstantAggregateZero::get(getType());
2849
2850 if (AllSame && isa<UndefValue>(ToC))
2851 return UndefValue::get(getType());
Aaron Ballmane4b91dc2014-08-19 14:59:02 +00002852
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002853 // Check for any other type of constant-folding.
Pete Cooper5815b1f2015-06-24 18:55:24 +00002854 if (Constant *C = getImpl(getType(), Values))
2855 return C;
Aaron Ballmane4b91dc2014-08-19 14:59:02 +00002856
Duncan P. N. Exon Smith909620a2014-08-19 19:13:30 +00002857 // Update to the new value.
Pete Cooper5815b1f2015-06-24 18:55:24 +00002858 return getContext().pImpl->ArrayConstants.replaceOperandsInPlace(
Mehdi Amini8914d292016-02-10 22:47:15 +00002859 Values, this, From, ToC, NumUpdated, OperandNo);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002860}
2861
Mehdi Amini8914d292016-02-10 22:47:15 +00002862Value *ConstantStruct::handleOperandChangeImpl(Value *From, Value *To) {
Owen Anderson45308b52009-07-27 22:29:26 +00002863 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2864 Constant *ToC = cast<Constant>(To);
2865
Pete Cooper74510a42015-06-12 17:48:05 +00002866 Use *OperandList = getOperandList();
Owen Anderson45308b52009-07-27 22:29:26 +00002867
Talin46e9b442012-02-05 20:54:10 +00002868 SmallVector<Constant*, 8> Values;
Owen Anderson45308b52009-07-27 22:29:26 +00002869 Values.reserve(getNumOperands()); // Build replacement struct.
Galina Kistanovafc259902012-07-13 01:25:27 +00002870
2871 // Fill values with the modified operands of the constant struct. Also,
Owen Anderson45308b52009-07-27 22:29:26 +00002872 // compute whether this turns into an all-zeros struct.
Mehdi Amini8914d292016-02-10 22:47:15 +00002873 unsigned NumUpdated = 0;
2874 bool AllSame = true;
2875 unsigned OperandNo = 0;
2876 for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O) {
2877 Constant *Val = cast<Constant>(O->get());
2878 if (Val == From) {
2879 OperandNo = (O - OperandList);
2880 Val = ToC;
2881 ++NumUpdated;
Owen Anderson45308b52009-07-27 22:29:26 +00002882 }
Mehdi Amini8914d292016-02-10 22:47:15 +00002883 Values.push_back(Val);
2884 AllSame &= Val == ToC;
Owen Anderson45308b52009-07-27 22:29:26 +00002885 }
Galina Kistanovafc259902012-07-13 01:25:27 +00002886
Mehdi Amini8914d292016-02-10 22:47:15 +00002887 if (AllSame && ToC->isNullValue())
Pete Cooper5815b1f2015-06-24 18:55:24 +00002888 return ConstantAggregateZero::get(getType());
2889
Mehdi Amini8914d292016-02-10 22:47:15 +00002890 if (AllSame && isa<UndefValue>(ToC))
Pete Cooper5815b1f2015-06-24 18:55:24 +00002891 return UndefValue::get(getType());
Galina Kistanovafc259902012-07-13 01:25:27 +00002892
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002893 // Update to the new value.
Pete Cooper5815b1f2015-06-24 18:55:24 +00002894 return getContext().pImpl->StructConstants.replaceOperandsInPlace(
Mehdi Amini8914d292016-02-10 22:47:15 +00002895 Values, this, From, ToC, NumUpdated, OperandNo);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002896}
2897
Mehdi Amini8914d292016-02-10 22:47:15 +00002898Value *ConstantVector::handleOperandChangeImpl(Value *From, Value *To) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002899 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002900 Constant *ToC = cast<Constant>(To);
Galina Kistanovafc259902012-07-13 01:25:27 +00002901
Chris Lattnera474bb22012-01-26 20:40:56 +00002902 SmallVector<Constant*, 8> Values;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002903 Values.reserve(getNumOperands()); // Build replacement array...
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002904 unsigned NumUpdated = 0;
Mehdi Amini8914d292016-02-10 22:47:15 +00002905 unsigned OperandNo = 0;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002906 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2907 Constant *Val = getOperand(i);
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002908 if (Val == From) {
Mehdi Amini8914d292016-02-10 22:47:15 +00002909 OperandNo = i;
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002910 ++NumUpdated;
2911 Val = ToC;
2912 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002913 Values.push_back(Val);
2914 }
Galina Kistanovafc259902012-07-13 01:25:27 +00002915
Pete Cooper5815b1f2015-06-24 18:55:24 +00002916 if (Constant *C = getImpl(Values))
2917 return C;
Duncan P. N. Exon Smith687744d2014-08-19 02:24:46 +00002918
Duncan P. N. Exon Smith909620a2014-08-19 19:13:30 +00002919 // Update to the new value.
Pete Cooper5815b1f2015-06-24 18:55:24 +00002920 return getContext().pImpl->VectorConstants.replaceOperandsInPlace(
Mehdi Amini8914d292016-02-10 22:47:15 +00002921 Values, this, From, ToC, NumUpdated, OperandNo);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002922}
2923
Mehdi Amini8914d292016-02-10 22:47:15 +00002924Value *ConstantExpr::handleOperandChangeImpl(Value *From, Value *ToV) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002925 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2926 Constant *To = cast<Constant>(ToV);
Galina Kistanovafc259902012-07-13 01:25:27 +00002927
Chris Lattner37e38352012-01-26 20:37:11 +00002928 SmallVector<Constant*, 8> NewOps;
Duncan P. N. Exon Smith33de00c2014-08-19 20:03:35 +00002929 unsigned NumUpdated = 0;
Mehdi Amini8914d292016-02-10 22:47:15 +00002930 unsigned OperandNo = 0;
Chris Lattner37e38352012-01-26 20:37:11 +00002931 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2932 Constant *Op = getOperand(i);
Duncan P. N. Exon Smith33de00c2014-08-19 20:03:35 +00002933 if (Op == From) {
Mehdi Amini8914d292016-02-10 22:47:15 +00002934 OperandNo = i;
Duncan P. N. Exon Smith33de00c2014-08-19 20:03:35 +00002935 ++NumUpdated;
2936 Op = To;
2937 }
2938 NewOps.push_back(Op);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002939 }
Duncan P. N. Exon Smith33de00c2014-08-19 20:03:35 +00002940 assert(NumUpdated && "I didn't contain From!");
Galina Kistanovafc259902012-07-13 01:25:27 +00002941
Pete Cooper5815b1f2015-06-24 18:55:24 +00002942 if (Constant *C = getWithOperands(NewOps, getType(), true))
2943 return C;
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002944
Duncan P. N. Exon Smith33de00c2014-08-19 20:03:35 +00002945 // Update to the new value.
Pete Cooper5815b1f2015-06-24 18:55:24 +00002946 return getContext().pImpl->ExprConstants.replaceOperandsInPlace(
Mehdi Amini8914d292016-02-10 22:47:15 +00002947 NewOps, this, From, To, NumUpdated, OperandNo);
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002948}
2949
James Molloyce545682012-11-17 17:56:30 +00002950Instruction *ConstantExpr::getAsInstruction() {
Benjamin Kramer5fbfe2f2015-02-28 13:20:15 +00002951 SmallVector<Value *, 4> ValueOperands(op_begin(), op_end());
James Molloyce545682012-11-17 17:56:30 +00002952 ArrayRef<Value*> Ops(ValueOperands);
2953
2954 switch (getOpcode()) {
2955 case Instruction::Trunc:
2956 case Instruction::ZExt:
2957 case Instruction::SExt:
2958 case Instruction::FPTrunc:
2959 case Instruction::FPExt:
2960 case Instruction::UIToFP:
2961 case Instruction::SIToFP:
2962 case Instruction::FPToUI:
2963 case Instruction::FPToSI:
2964 case Instruction::PtrToInt:
2965 case Instruction::IntToPtr:
2966 case Instruction::BitCast:
Eli Bendersky157a97a2014-01-18 22:54:33 +00002967 case Instruction::AddrSpaceCast:
James Molloyce545682012-11-17 17:56:30 +00002968 return CastInst::Create((Instruction::CastOps)getOpcode(),
2969 Ops[0], getType());
2970 case Instruction::Select:
2971 return SelectInst::Create(Ops[0], Ops[1], Ops[2]);
2972 case Instruction::InsertElement:
2973 return InsertElementInst::Create(Ops[0], Ops[1], Ops[2]);
2974 case Instruction::ExtractElement:
2975 return ExtractElementInst::Create(Ops[0], Ops[1]);
2976 case Instruction::InsertValue:
2977 return InsertValueInst::Create(Ops[0], Ops[1], getIndices());
2978 case Instruction::ExtractValue:
2979 return ExtractValueInst::Create(Ops[0], getIndices());
2980 case Instruction::ShuffleVector:
2981 return new ShuffleVectorInst(Ops[0], Ops[1], Ops[2]);
2982
David Blaikie741c8f82015-03-14 01:53:18 +00002983 case Instruction::GetElementPtr: {
2984 const auto *GO = cast<GEPOperator>(this);
2985 if (GO->isInBounds())
2986 return GetElementPtrInst::CreateInBounds(GO->getSourceElementType(),
2987 Ops[0], Ops.slice(1));
2988 return GetElementPtrInst::Create(GO->getSourceElementType(), Ops[0],
2989 Ops.slice(1));
2990 }
James Molloyce545682012-11-17 17:56:30 +00002991 case Instruction::ICmp:
2992 case Instruction::FCmp:
2993 return CmpInst::Create((Instruction::OtherOps)getOpcode(),
Craig Topper1c3f2832015-12-15 06:11:33 +00002994 (CmpInst::Predicate)getPredicate(), Ops[0], Ops[1]);
James Molloyce545682012-11-17 17:56:30 +00002995
2996 default:
2997 assert(getNumOperands() == 2 && "Must be binary operator?");
2998 BinaryOperator *BO =
2999 BinaryOperator::Create((Instruction::BinaryOps)getOpcode(),
3000 Ops[0], Ops[1]);
3001 if (isa<OverflowingBinaryOperator>(BO)) {
3002 BO->setHasNoUnsignedWrap(SubclassOptionalData &
3003 OverflowingBinaryOperator::NoUnsignedWrap);
3004 BO->setHasNoSignedWrap(SubclassOptionalData &
3005 OverflowingBinaryOperator::NoSignedWrap);
3006 }
3007 if (isa<PossiblyExactOperator>(BO))
3008 BO->setIsExact(SubclassOptionalData & PossiblyExactOperator::IsExact);
3009 return BO;
3010 }
3011}