blob: 8b375bcff768487af72c86f27536a63055088aa2 [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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/SmallVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/ADT/StringMap.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/DerivedTypes.h"
Chandler Carruth03eb0de2014-03-04 10:40:04 +000021#include "llvm/IR/GetElementPtrTypeIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/GlobalValue.h"
23#include "llvm/IR/Instructions.h"
24#include "llvm/IR/Module.h"
25#include "llvm/IR/Operator.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000026#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000027#include "llvm/Support/ErrorHandling.h"
Chris Lattner69edc982006-09-28 00:35:06 +000028#include "llvm/Support/ManagedStatic.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000029#include "llvm/Support/MathExtras.h"
Chris Lattner78683a72009-08-23 04:02:03 +000030#include "llvm/Support/raw_ostream.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000031#include <algorithm>
Serge Gueltone38003f2017-05-09 19:31:13 +000032
Chris Lattner189d19f2003-11-21 20:23:48 +000033using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000034
Chris Lattner2f7c9632001-06-06 20:29:01 +000035//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000036// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000037//===----------------------------------------------------------------------===//
38
Chris Lattnerac5fb562011-07-15 05:58:04 +000039bool Constant::isNegativeZeroValue() const {
40 // Floating point values have an explicit -0.0 value.
41 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
42 return CFP->isZero() && CFP->isNegative();
Galina Kistanovafc259902012-07-13 01:25:27 +000043
David Tweed5493fee2013-03-18 11:54:44 +000044 // Equivalent for a vector of -0.0's.
45 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this))
Craig Topper0b4b4e32017-07-15 22:06:19 +000046 if (CV->getElementType()->isFloatingPointTy() && CV->isSplat())
47 if (CV->getElementAsAPFloat(0).isNegZero())
David Tweed5493fee2013-03-18 11:54:44 +000048 return true;
49
Owen Anderson8e851302015-11-20 22:34:48 +000050 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
51 if (ConstantFP *SplatCFP = dyn_cast_or_null<ConstantFP>(CV->getSplatValue()))
52 if (SplatCFP && SplatCFP->isZero() && SplatCFP->isNegative())
53 return true;
54
David Tweed298e4192013-03-19 10:16:40 +000055 // We've already handled true FP case; any other FP vectors can't represent -0.0.
56 if (getType()->isFPOrFPVectorTy())
57 return false;
David Tweed5493fee2013-03-18 11:54:44 +000058
Chris Lattnerac5fb562011-07-15 05:58:04 +000059 // Otherwise, just use +0.0.
60 return isNullValue();
61}
62
Shuxin Yang9ca562e2013-01-09 00:53:25 +000063// Return true iff this constant is positive zero (floating point), negative
64// zero (floating point), or a null value.
Shuxin Yangf0537ab2013-01-09 00:13:41 +000065bool Constant::isZeroValue() const {
66 // Floating point values have an explicit -0.0 value.
67 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
68 return CFP->isZero();
69
Owen Anderson630077e2015-11-20 08:16:13 +000070 // Equivalent for a vector of -0.0's.
71 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this))
Craig Topper0b4b4e32017-07-15 22:06:19 +000072 if (CV->getElementType()->isFloatingPointTy() && CV->isSplat())
73 if (CV->getElementAsAPFloat(0).isZero())
Owen Anderson630077e2015-11-20 08:16:13 +000074 return true;
75
Owen Anderson8e851302015-11-20 22:34:48 +000076 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
77 if (ConstantFP *SplatCFP = dyn_cast_or_null<ConstantFP>(CV->getSplatValue()))
78 if (SplatCFP && SplatCFP->isZero())
79 return true;
80
Shuxin Yangf0537ab2013-01-09 00:13:41 +000081 // Otherwise, just use +0.0.
82 return isNullValue();
83}
84
Chris Lattnerbe6610c2011-07-15 06:14:08 +000085bool Constant::isNullValue() const {
86 // 0 is null.
87 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
88 return CI->isZero();
Galina Kistanovafc259902012-07-13 01:25:27 +000089
Chris Lattnerbe6610c2011-07-15 06:14:08 +000090 // +0.0 is null.
91 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
92 return CFP->isZero() && !CFP->isNegative();
93
David Majnemerf0f224d2015-11-11 21:57:16 +000094 // constant zero is zero for aggregates, cpnull is null for pointers, none for
95 // tokens.
96 return isa<ConstantAggregateZero>(this) || isa<ConstantPointerNull>(this) ||
97 isa<ConstantTokenNone>(this);
Chris Lattnerbe6610c2011-07-15 06:14:08 +000098}
99
Nadav Rotem365af6f2011-08-24 20:18:38 +0000100bool Constant::isAllOnesValue() const {
101 // Check for -1 integers
102 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
103 return CI->isMinusOne();
104
105 // Check for FP which are bitcasted from -1 integers
106 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
107 return CFP->getValueAPF().bitcastToAPInt().isAllOnesValue();
108
Benjamin Kramer42d098e2011-11-14 19:12:20 +0000109 // Check for constant vectors which are splats of -1 values.
Nadav Rotem365af6f2011-08-24 20:18:38 +0000110 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
Benjamin Kramer42d098e2011-11-14 19:12:20 +0000111 if (Constant *Splat = CV->getSplatValue())
112 return Splat->isAllOnesValue();
Nadav Rotem365af6f2011-08-24 20:18:38 +0000113
Chris Lattnerf14a67f2012-01-26 02:31:22 +0000114 // Check for constant vectors which are splats of -1 values.
Craig Topper0b4b4e32017-07-15 22:06:19 +0000115 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this)) {
116 if (CV->isSplat()) {
117 if (CV->getElementType()->isFloatingPointTy())
118 return CV->getElementAsAPFloat(0).bitcastToAPInt().isAllOnesValue();
119 return CV->getElementAsAPInt(0).isAllOnesValue();
120 }
121 }
Chris Lattnerf14a67f2012-01-26 02:31:22 +0000122
Nadav Rotem365af6f2011-08-24 20:18:38 +0000123 return false;
124}
Benjamin Kramer42d098e2011-11-14 19:12:20 +0000125
David Majnemer1a0bbc82014-08-16 09:23:42 +0000126bool Constant::isOneValue() const {
127 // Check for 1 integers
128 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
129 return CI->isOne();
130
131 // Check for FP which are bitcasted from 1 integers
132 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
Craig Topper93ac6e12017-06-07 00:58:02 +0000133 return CFP->getValueAPF().bitcastToAPInt().isOneValue();
David Majnemer1a0bbc82014-08-16 09:23:42 +0000134
135 // Check for constant vectors which are splats of 1 values.
136 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
137 if (Constant *Splat = CV->getSplatValue())
138 return Splat->isOneValue();
139
140 // Check for constant vectors which are splats of 1 values.
Craig Topper0b4b4e32017-07-15 22:06:19 +0000141 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this)) {
142 if (CV->isSplat()) {
143 if (CV->getElementType()->isFloatingPointTy())
144 return CV->getElementAsAPFloat(0).bitcastToAPInt().isOneValue();
145 return CV->getElementAsAPInt(0).isOneValue();
146 }
147 }
David Majnemer1a0bbc82014-08-16 09:23:42 +0000148
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.
Craig Topper0b4b4e32017-07-15 22:06:19 +0000167 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this)) {
168 if (CV->isSplat()) {
169 if (CV->getElementType()->isFloatingPointTy())
170 return CV->getElementAsAPFloat(0).bitcastToAPInt().isMinSignedValue();
171 return CV->getElementAsAPInt(0).isMinSignedValue();
172 }
173 }
David Majnemerbdeef602014-07-02 06:07:09 +0000174
175 return false;
176}
177
David Majnemer0e6c9862014-08-22 16:41:23 +0000178bool Constant::isNotMinSignedValue() const {
179 // Check for INT_MIN integers
180 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
181 return !CI->isMinValue(/*isSigned=*/true);
182
183 // Check for FP which are bitcasted from INT_MIN integers
184 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
185 return !CFP->getValueAPF().bitcastToAPInt().isMinSignedValue();
186
187 // Check for constant vectors which are splats of INT_MIN values.
188 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
189 if (Constant *Splat = CV->getSplatValue())
190 return Splat->isNotMinSignedValue();
191
192 // Check for constant vectors which are splats of INT_MIN values.
Craig Topper0b4b4e32017-07-15 22:06:19 +0000193 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this)) {
194 if (CV->isSplat()) {
195 if (CV->getElementType()->isFloatingPointTy())
196 return !CV->getElementAsAPFloat(0).bitcastToAPInt().isMinSignedValue();
197 return !CV->getElementAsAPInt(0).isMinSignedValue();
198 }
199 }
David Majnemer0e6c9862014-08-22 16:41:23 +0000200
201 // It *may* contain INT_MIN, we can't tell.
202 return false;
203}
204
Sanjay Patel08868e4942018-02-16 22:32:54 +0000205bool Constant::isFiniteNonZeroFP() const {
206 if (auto *CFP = dyn_cast<ConstantFP>(this))
207 return CFP->getValueAPF().isFiniteNonZero();
208 if (!getType()->isVectorTy())
209 return false;
210 for (unsigned i = 0, e = getType()->getVectorNumElements(); i != e; ++i) {
211 auto *CFP = dyn_cast_or_null<ConstantFP>(this->getAggregateElement(i));
212 if (!CFP || !CFP->getValueAPF().isFiniteNonZero())
213 return false;
214 }
215 return true;
216}
217
218bool Constant::isNormalFP() const {
219 if (auto *CFP = dyn_cast<ConstantFP>(this))
220 return CFP->getValueAPF().isNormal();
221 if (!getType()->isVectorTy())
222 return false;
223 for (unsigned i = 0, e = getType()->getVectorNumElements(); i != e; ++i) {
224 auto *CFP = dyn_cast_or_null<ConstantFP>(this->getAggregateElement(i));
225 if (!CFP || !CFP->getValueAPF().isNormal())
226 return false;
227 }
228 return true;
229}
230
Sanjay Patel90f4c8e2018-02-20 16:08:15 +0000231bool Constant::hasExactInverseFP() const {
232 if (auto *CFP = dyn_cast<ConstantFP>(this))
233 return CFP->getValueAPF().getExactInverse(nullptr);
234 if (!getType()->isVectorTy())
235 return false;
236 for (unsigned i = 0, e = getType()->getVectorNumElements(); i != e; ++i) {
237 auto *CFP = dyn_cast_or_null<ConstantFP>(this->getAggregateElement(i));
238 if (!CFP || !CFP->getValueAPF().getExactInverse(nullptr))
239 return false;
240 }
241 return true;
242}
243
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +0000244/// Constructor to create a '0' constant of arbitrary type.
Chris Lattner229907c2011-07-18 04:54:35 +0000245Constant *Constant::getNullValue(Type *Ty) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000246 switch (Ty->getTypeID()) {
247 case Type::IntegerTyID:
248 return ConstantInt::get(Ty, 0);
Dan Gohman518cda42011-12-17 00:04:22 +0000249 case Type::HalfTyID:
250 return ConstantFP::get(Ty->getContext(),
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000251 APFloat::getZero(APFloat::IEEEhalf()));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000252 case Type::FloatTyID:
Benjamin Kramer8ceebfa2010-12-04 14:22:24 +0000253 return ConstantFP::get(Ty->getContext(),
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000254 APFloat::getZero(APFloat::IEEEsingle()));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000255 case Type::DoubleTyID:
Benjamin Kramer8ceebfa2010-12-04 14:22:24 +0000256 return ConstantFP::get(Ty->getContext(),
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000257 APFloat::getZero(APFloat::IEEEdouble()));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000258 case Type::X86_FP80TyID:
Benjamin Kramer8ceebfa2010-12-04 14:22:24 +0000259 return ConstantFP::get(Ty->getContext(),
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000260 APFloat::getZero(APFloat::x87DoubleExtended()));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000261 case Type::FP128TyID:
262 return ConstantFP::get(Ty->getContext(),
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000263 APFloat::getZero(APFloat::IEEEquad()));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000264 case Type::PPC_FP128TyID:
Benjamin Kramer8ceebfa2010-12-04 14:22:24 +0000265 return ConstantFP::get(Ty->getContext(),
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000266 APFloat(APFloat::PPCDoubleDouble(),
Tim Northover29178a32013-01-22 09:46:31 +0000267 APInt::getNullValue(128)));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000268 case Type::PointerTyID:
269 return ConstantPointerNull::get(cast<PointerType>(Ty));
270 case Type::StructTyID:
271 case Type::ArrayTyID:
272 case Type::VectorTyID:
273 return ConstantAggregateZero::get(Ty);
David Majnemerf0f224d2015-11-11 21:57:16 +0000274 case Type::TokenTyID:
275 return ConstantTokenNone::get(Ty->getContext());
Owen Anderson5a1acd92009-07-31 20:28:14 +0000276 default:
277 // Function, Label, or Opaque type?
Craig Topperc514b542012-02-05 22:14:15 +0000278 llvm_unreachable("Cannot create a null constant of that type!");
Owen Anderson5a1acd92009-07-31 20:28:14 +0000279 }
280}
281
Chris Lattner229907c2011-07-18 04:54:35 +0000282Constant *Constant::getIntegerValue(Type *Ty, const APInt &V) {
283 Type *ScalarTy = Ty->getScalarType();
Dan Gohmanf011f5a2009-08-03 22:07:33 +0000284
285 // Create the base integer constant.
286 Constant *C = ConstantInt::get(Ty->getContext(), V);
287
288 // Convert an integer to a pointer, if necessary.
Chris Lattner229907c2011-07-18 04:54:35 +0000289 if (PointerType *PTy = dyn_cast<PointerType>(ScalarTy))
Dan Gohmanf011f5a2009-08-03 22:07:33 +0000290 C = ConstantExpr::getIntToPtr(C, PTy);
291
292 // Broadcast a scalar to a vector, if necessary.
Chris Lattner229907c2011-07-18 04:54:35 +0000293 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
Chris Lattnere9eed292012-01-25 05:19:54 +0000294 C = ConstantVector::getSplat(VTy->getNumElements(), C);
Dan Gohmanf011f5a2009-08-03 22:07:33 +0000295
296 return C;
297}
298
Chris Lattner229907c2011-07-18 04:54:35 +0000299Constant *Constant::getAllOnesValue(Type *Ty) {
300 if (IntegerType *ITy = dyn_cast<IntegerType>(Ty))
Owen Anderson5a1acd92009-07-31 20:28:14 +0000301 return ConstantInt::get(Ty->getContext(),
302 APInt::getAllOnesValue(ITy->getBitWidth()));
Nadav Rotem7cc6d122011-02-17 21:22:27 +0000303
304 if (Ty->isFloatingPointTy()) {
305 APFloat FL = APFloat::getAllOnesValue(Ty->getPrimitiveSizeInBits(),
306 !Ty->isPPC_FP128Ty());
307 return ConstantFP::get(Ty->getContext(), FL);
308 }
309
Chris Lattner229907c2011-07-18 04:54:35 +0000310 VectorType *VTy = cast<VectorType>(Ty);
Chris Lattnere9eed292012-01-25 05:19:54 +0000311 return ConstantVector::getSplat(VTy->getNumElements(),
312 getAllOnesValue(VTy->getElementType()));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000313}
314
Chris Lattner7e683d12012-01-25 06:16:32 +0000315Constant *Constant::getAggregateElement(unsigned Elt) const {
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +0000316 if (const ConstantAggregate *CC = dyn_cast<ConstantAggregate>(this))
317 return Elt < CC->getNumOperands() ? CC->getOperand(Elt) : nullptr;
Galina Kistanovafc259902012-07-13 01:25:27 +0000318
David Majnemer9b529a72015-02-16 04:02:09 +0000319 if (const ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(this))
320 return Elt < CAZ->getNumElements() ? CAZ->getElementValue(Elt) : nullptr;
Galina Kistanovafc259902012-07-13 01:25:27 +0000321
Chris Lattner7e683d12012-01-25 06:16:32 +0000322 if (const UndefValue *UV = dyn_cast<UndefValue>(this))
David Majnemer9b529a72015-02-16 04:02:09 +0000323 return Elt < UV->getNumElements() ? UV->getElementValue(Elt) : nullptr;
Galina Kistanovafc259902012-07-13 01:25:27 +0000324
Chris Lattner8326bd82012-01-26 00:42:34 +0000325 if (const ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(this))
Craig Topperc6207612014-04-09 06:08:46 +0000326 return Elt < CDS->getNumElements() ? CDS->getElementAsConstant(Elt)
327 : nullptr;
328 return nullptr;
Chris Lattner7e683d12012-01-25 06:16:32 +0000329}
330
331Constant *Constant::getAggregateElement(Constant *Elt) const {
332 assert(isa<IntegerType>(Elt->getType()) && "Index must be an integer");
333 if (ConstantInt *CI = dyn_cast<ConstantInt>(Elt))
334 return getAggregateElement(CI->getZExtValue());
Craig Topperc6207612014-04-09 06:08:46 +0000335 return nullptr;
Chris Lattner7e683d12012-01-25 06:16:32 +0000336}
337
Pete Cooper86dd4cf2015-06-23 21:55:11 +0000338void Constant::destroyConstant() {
339 /// First call destroyConstantImpl on the subclass. This gives the subclass
340 /// a chance to remove the constant from any maps/pools it's contained in.
341 switch (getValueID()) {
342 default:
343 llvm_unreachable("Not a constant!");
344#define HANDLE_CONSTANT(Name) \
345 case Value::Name##Val: \
346 cast<Name>(this)->destroyConstantImpl(); \
347 break;
348#include "llvm/IR/Value.def"
349 }
Chris Lattner7e683d12012-01-25 06:16:32 +0000350
Chris Lattner3462ae32001-12-03 22:26:30 +0000351 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +0000352 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +0000353 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +0000354 // but they don't know that. Because we only find out when the CPV is
355 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +0000356 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +0000357 //
358 while (!use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000359 Value *V = user_back();
Pete Cooper86dd4cf2015-06-23 21:55:11 +0000360#ifndef NDEBUG // Only in -g mode...
Chris Lattner78683a72009-08-23 04:02:03 +0000361 if (!isa<Constant>(V)) {
David Greene1e27a132010-01-05 01:29:19 +0000362 dbgs() << "While deleting: " << *this
Pete Cooper86dd4cf2015-06-23 21:55:11 +0000363 << "\n\nUse still stuck around after Def is destroyed: " << *V
364 << "\n\n";
Chris Lattner78683a72009-08-23 04:02:03 +0000365 }
Chris Lattnerd7a73302001-10-13 06:57:33 +0000366#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000367 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Chris Lattner8326bd82012-01-26 00:42:34 +0000368 cast<Constant>(V)->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +0000369
370 // The constant should remove itself from our use list...
Chandler Carruthcdf47882014-03-09 03:16:01 +0000371 assert((use_empty() || user_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +0000372 }
373
374 // Value has no outstanding references it is safe to delete it now...
375 delete this;
Chris Lattner38569342001-10-01 20:11:19 +0000376}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000377
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000378static bool canTrapImpl(const Constant *C,
Craig Topper71b7b682014-08-21 05:55:13 +0000379 SmallPtrSetImpl<const ConstantExpr *> &NonTrappingOps) {
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000380 assert(C->getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
Chris Lattner23dd1f62006-10-20 00:27:06 +0000381 // The only thing that could possibly trap are constant exprs.
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000382 const ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
383 if (!CE)
384 return false;
Galina Kistanovafc259902012-07-13 01:25:27 +0000385
386 // ConstantExpr traps if any operands can trap.
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000387 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
388 if (ConstantExpr *Op = dyn_cast<ConstantExpr>(CE->getOperand(i))) {
David Blaikie70573dc2014-11-19 07:49:26 +0000389 if (NonTrappingOps.insert(Op).second && canTrapImpl(Op, NonTrappingOps))
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000390 return true;
391 }
392 }
Chris Lattner23dd1f62006-10-20 00:27:06 +0000393
394 // Otherwise, only specific operations can trap.
395 switch (CE->getOpcode()) {
396 default:
397 return false;
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000398 case Instruction::UDiv:
399 case Instruction::SDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +0000400 case Instruction::URem:
401 case Instruction::SRem:
Chris Lattner23dd1f62006-10-20 00:27:06 +0000402 // Div and rem can trap if the RHS is not known to be non-zero.
Chris Lattnera91a5632009-10-28 05:14:34 +0000403 if (!isa<ConstantInt>(CE->getOperand(1)) ||CE->getOperand(1)->isNullValue())
Chris Lattner23dd1f62006-10-20 00:27:06 +0000404 return true;
405 return false;
406 }
407}
408
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000409bool Constant::canTrap() const {
410 SmallPtrSet<const ConstantExpr *, 4> NonTrappingOps;
411 return canTrapImpl(this, NonTrappingOps);
412}
413
Hans Wennborg4dc89512014-06-20 00:38:12 +0000414/// Check if C contains a GlobalValue for which Predicate is true.
415static bool
416ConstHasGlobalValuePredicate(const Constant *C,
417 bool (*Predicate)(const GlobalValue *)) {
418 SmallPtrSet<const Constant *, 8> Visited;
419 SmallVector<const Constant *, 8> WorkList;
420 WorkList.push_back(C);
421 Visited.insert(C);
Hans Wennborg709e0152012-11-15 11:40:00 +0000422
423 while (!WorkList.empty()) {
Hans Wennborg4dc89512014-06-20 00:38:12 +0000424 const Constant *WorkItem = WorkList.pop_back_val();
425 if (const auto *GV = dyn_cast<GlobalValue>(WorkItem))
426 if (Predicate(GV))
Hans Wennborg709e0152012-11-15 11:40:00 +0000427 return true;
Hans Wennborg4dc89512014-06-20 00:38:12 +0000428 for (const Value *Op : WorkItem->operands()) {
429 const Constant *ConstOp = dyn_cast<Constant>(Op);
430 if (!ConstOp)
Hans Wennborg18aa12402012-11-16 10:33:25 +0000431 continue;
David Blaikie70573dc2014-11-19 07:49:26 +0000432 if (Visited.insert(ConstOp).second)
Hans Wennborg4dc89512014-06-20 00:38:12 +0000433 WorkList.push_back(ConstOp);
Hans Wennborg709e0152012-11-15 11:40:00 +0000434 }
435 }
Hans Wennborg709e0152012-11-15 11:40:00 +0000436 return false;
437}
438
Hans Wennborg4dc89512014-06-20 00:38:12 +0000439bool Constant::isThreadDependent() const {
440 auto DLLImportPredicate = [](const GlobalValue *GV) {
441 return GV->isThreadLocal();
442 };
443 return ConstHasGlobalValuePredicate(this, DLLImportPredicate);
444}
445
446bool Constant::isDLLImportDependent() const {
447 auto DLLImportPredicate = [](const GlobalValue *GV) {
448 return GV->hasDLLImportStorageClass();
449 };
450 return ConstHasGlobalValuePredicate(this, DLLImportPredicate);
451}
452
Chris Lattner253bc772009-11-01 18:11:50 +0000453bool Constant::isConstantUsed() const {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000454 for (const User *U : users()) {
455 const Constant *UC = dyn_cast<Constant>(U);
Craig Topperc6207612014-04-09 06:08:46 +0000456 if (!UC || isa<GlobalValue>(UC))
Chris Lattner253bc772009-11-01 18:11:50 +0000457 return true;
Galina Kistanovafc259902012-07-13 01:25:27 +0000458
Chris Lattner253bc772009-11-01 18:11:50 +0000459 if (UC->isConstantUsed())
460 return true;
461 }
462 return false;
463}
464
Rafael Espindola65e49022015-11-17 00:51:23 +0000465bool Constant::needsRelocation() const {
466 if (isa<GlobalValue>(this))
467 return true; // Global reference.
468
Chris Lattner2cb85b42009-10-28 04:12:16 +0000469 if (const BlockAddress *BA = dyn_cast<BlockAddress>(this))
Rafael Espindola65e49022015-11-17 00:51:23 +0000470 return BA->getFunction()->needsRelocation();
471
Chris Lattnera7cfc432010-01-03 18:09:40 +0000472 // While raw uses of blockaddress need to be relocated, differences between
473 // two of them don't when they are for labels in the same function. This is a
474 // common idiom when creating a table for the indirect goto extension, so we
475 // handle it efficiently here.
476 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(this))
477 if (CE->getOpcode() == Instruction::Sub) {
478 ConstantExpr *LHS = dyn_cast<ConstantExpr>(CE->getOperand(0));
479 ConstantExpr *RHS = dyn_cast<ConstantExpr>(CE->getOperand(1));
Rafael Espindola65e49022015-11-17 00:51:23 +0000480 if (LHS && RHS && LHS->getOpcode() == Instruction::PtrToInt &&
Chris Lattnera7cfc432010-01-03 18:09:40 +0000481 RHS->getOpcode() == Instruction::PtrToInt &&
482 isa<BlockAddress>(LHS->getOperand(0)) &&
483 isa<BlockAddress>(RHS->getOperand(0)) &&
484 cast<BlockAddress>(LHS->getOperand(0))->getFunction() ==
Rafael Espindola65e49022015-11-17 00:51:23 +0000485 cast<BlockAddress>(RHS->getOperand(0))->getFunction())
486 return false;
Chris Lattnera7cfc432010-01-03 18:09:40 +0000487 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000488
Rafael Espindola65e49022015-11-17 00:51:23 +0000489 bool Result = false;
Evan Chengf9e003b2007-03-08 00:59:12 +0000490 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Rafael Espindola65e49022015-11-17 00:51:23 +0000491 Result |= cast<Constant>(getOperand(i))->needsRelocation();
Galina Kistanovafc259902012-07-13 01:25:27 +0000492
Chris Lattner4565ef52009-07-22 00:05:44 +0000493 return Result;
Evan Chengf9e003b2007-03-08 00:59:12 +0000494}
495
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +0000496/// If the specified constantexpr is dead, remove it. This involves recursively
497/// eliminating any dead users of the constantexpr.
Chris Lattner84886402011-02-18 04:41:42 +0000498static bool removeDeadUsersOfConstant(const Constant *C) {
499 if (isa<GlobalValue>(C)) return false; // Cannot remove this
Galina Kistanovafc259902012-07-13 01:25:27 +0000500
Chris Lattner84886402011-02-18 04:41:42 +0000501 while (!C->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000502 const Constant *User = dyn_cast<Constant>(C->user_back());
Chris Lattner84886402011-02-18 04:41:42 +0000503 if (!User) return false; // Non-constant usage;
504 if (!removeDeadUsersOfConstant(User))
505 return false; // Constant wasn't dead
506 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000507
Chris Lattner84886402011-02-18 04:41:42 +0000508 const_cast<Constant*>(C)->destroyConstant();
509 return true;
510}
511
512
Chris Lattner84886402011-02-18 04:41:42 +0000513void Constant::removeDeadConstantUsers() const {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000514 Value::const_user_iterator I = user_begin(), E = user_end();
515 Value::const_user_iterator LastNonDeadUser = E;
Chris Lattner84886402011-02-18 04:41:42 +0000516 while (I != E) {
517 const Constant *User = dyn_cast<Constant>(*I);
Craig Topperc6207612014-04-09 06:08:46 +0000518 if (!User) {
Chris Lattner84886402011-02-18 04:41:42 +0000519 LastNonDeadUser = I;
520 ++I;
521 continue;
522 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000523
Chris Lattner84886402011-02-18 04:41:42 +0000524 if (!removeDeadUsersOfConstant(User)) {
525 // If the constant wasn't dead, remember that this was the last live use
526 // and move on to the next constant.
527 LastNonDeadUser = I;
528 ++I;
529 continue;
530 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000531
Chris Lattner84886402011-02-18 04:41:42 +0000532 // If the constant was dead, then the iterator is invalidated.
533 if (LastNonDeadUser == E) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000534 I = user_begin();
Chris Lattner84886402011-02-18 04:41:42 +0000535 if (I == E) break;
536 } else {
537 I = LastNonDeadUser;
538 ++I;
539 }
540 }
541}
542
543
Chris Lattner2105d662008-07-10 00:28:11 +0000544
Chris Lattner2f7c9632001-06-06 20:29:01 +0000545//===----------------------------------------------------------------------===//
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000546// ConstantInt
Chris Lattner2f7c9632001-06-06 20:29:01 +0000547//===----------------------------------------------------------------------===//
548
Duncan P. N. Exon Smithb6452792016-02-21 02:39:49 +0000549ConstantInt::ConstantInt(IntegerType *Ty, const APInt &V)
550 : ConstantData(Ty, ConstantIntVal), Val(V) {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000551 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000552}
553
Nick Lewycky92db8e82011-03-06 03:36:19 +0000554ConstantInt *ConstantInt::getTrue(LLVMContext &Context) {
Owen Anderson23a204d2009-07-31 17:39:07 +0000555 LLVMContextImpl *pImpl = Context.pImpl;
Benjamin Kramerddd1b7b2010-11-20 18:43:35 +0000556 if (!pImpl->TheTrueVal)
557 pImpl->TheTrueVal = ConstantInt::get(Type::getInt1Ty(Context), 1);
558 return pImpl->TheTrueVal;
Owen Anderson23a204d2009-07-31 17:39:07 +0000559}
560
Nick Lewycky92db8e82011-03-06 03:36:19 +0000561ConstantInt *ConstantInt::getFalse(LLVMContext &Context) {
Owen Anderson23a204d2009-07-31 17:39:07 +0000562 LLVMContextImpl *pImpl = Context.pImpl;
Benjamin Kramerddd1b7b2010-11-20 18:43:35 +0000563 if (!pImpl->TheFalseVal)
564 pImpl->TheFalseVal = ConstantInt::get(Type::getInt1Ty(Context), 0);
565 return pImpl->TheFalseVal;
Owen Anderson23a204d2009-07-31 17:39:07 +0000566}
567
Chris Lattner229907c2011-07-18 04:54:35 +0000568Constant *ConstantInt::getTrue(Type *Ty) {
Craig Topperfde47232017-07-09 07:04:03 +0000569 assert(Ty->isIntOrIntVectorTy(1) && "Type not i1 or vector of i1.");
Sanjay Patel70a575a2017-04-16 17:00:21 +0000570 ConstantInt *TrueC = ConstantInt::getTrue(Ty->getContext());
571 if (auto *VTy = dyn_cast<VectorType>(Ty))
572 return ConstantVector::getSplat(VTy->getNumElements(), TrueC);
573 return TrueC;
Nick Lewycky92db8e82011-03-06 03:36:19 +0000574}
575
Chris Lattner229907c2011-07-18 04:54:35 +0000576Constant *ConstantInt::getFalse(Type *Ty) {
Craig Topperfde47232017-07-09 07:04:03 +0000577 assert(Ty->isIntOrIntVectorTy(1) && "Type not i1 or vector of i1.");
Sanjay Patel70a575a2017-04-16 17:00:21 +0000578 ConstantInt *FalseC = ConstantInt::getFalse(Ty->getContext());
579 if (auto *VTy = dyn_cast<VectorType>(Ty))
580 return ConstantVector::getSplat(VTy->getNumElements(), FalseC);
581 return FalseC;
Nick Lewycky92db8e82011-03-06 03:36:19 +0000582}
583
Benjamin Kramer8e5dc532014-12-06 13:12:56 +0000584// Get a ConstantInt from an APInt.
Nick Lewycky92db8e82011-03-06 03:36:19 +0000585ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt &V) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000586 // get an existing value or the insertion position
Benjamin Kramer320682f2013-06-01 17:51:03 +0000587 LLVMContextImpl *pImpl = Context.pImpl;
Justin Lebar611c5c22016-10-10 16:26:13 +0000588 std::unique_ptr<ConstantInt> &Slot = pImpl->IntConstants[V];
Benjamin Kramer8e5dc532014-12-06 13:12:56 +0000589 if (!Slot) {
590 // Get the corresponding integer type for the bit width of the value.
591 IntegerType *ITy = IntegerType::get(Context, V.getBitWidth());
Justin Lebar611c5c22016-10-10 16:26:13 +0000592 Slot.reset(new ConstantInt(ITy, V));
Benjamin Kramer8e5dc532014-12-06 13:12:56 +0000593 }
594 assert(Slot->getType() == IntegerType::get(Context, V.getBitWidth()));
Justin Lebar611c5c22016-10-10 16:26:13 +0000595 return Slot.get();
Owen Andersonedb4a702009-07-24 23:12:02 +0000596}
597
Chris Lattner229907c2011-07-18 04:54:35 +0000598Constant *ConstantInt::get(Type *Ty, uint64_t V, bool isSigned) {
Nick Lewycky92db8e82011-03-06 03:36:19 +0000599 Constant *C = get(cast<IntegerType>(Ty->getScalarType()), V, isSigned);
Owen Andersonedb4a702009-07-24 23:12:02 +0000600
601 // For vectors, broadcast the value.
Chris Lattner229907c2011-07-18 04:54:35 +0000602 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
Chris Lattnere9eed292012-01-25 05:19:54 +0000603 return ConstantVector::getSplat(VTy->getNumElements(), C);
Owen Andersonedb4a702009-07-24 23:12:02 +0000604
605 return C;
606}
607
Sanjay Patelf3587ec2016-05-18 22:05:28 +0000608ConstantInt *ConstantInt::get(IntegerType *Ty, uint64_t V, bool isSigned) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000609 return get(Ty->getContext(), APInt(Ty->getBitWidth(), V, isSigned));
610}
611
Chris Lattner0256be92012-01-27 03:08:05 +0000612ConstantInt *ConstantInt::getSigned(IntegerType *Ty, int64_t V) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000613 return get(Ty, V, true);
614}
615
Chris Lattner229907c2011-07-18 04:54:35 +0000616Constant *ConstantInt::getSigned(Type *Ty, int64_t V) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000617 return get(Ty, V, true);
618}
619
Chris Lattner0256be92012-01-27 03:08:05 +0000620Constant *ConstantInt::get(Type *Ty, const APInt& V) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000621 ConstantInt *C = get(Ty->getContext(), V);
622 assert(C->getType() == Ty->getScalarType() &&
623 "ConstantInt type doesn't match the type implied by its value!");
624
625 // For vectors, broadcast the value.
Chris Lattner229907c2011-07-18 04:54:35 +0000626 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
Chris Lattnere9eed292012-01-25 05:19:54 +0000627 return ConstantVector::getSplat(VTy->getNumElements(), C);
Owen Andersonedb4a702009-07-24 23:12:02 +0000628
629 return C;
630}
631
Sanjay Patelf3587ec2016-05-18 22:05:28 +0000632ConstantInt *ConstantInt::get(IntegerType* Ty, StringRef Str, uint8_t radix) {
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000633 return get(Ty->getContext(), APInt(Ty->getBitWidth(), Str, radix));
634}
635
Pete Cooper86dd4cf2015-06-23 21:55:11 +0000636/// Remove the constant from the constant table.
637void ConstantInt::destroyConstantImpl() {
638 llvm_unreachable("You can't ConstantInt->destroyConstantImpl()!");
639}
640
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000641//===----------------------------------------------------------------------===//
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000642// ConstantFP
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000643//===----------------------------------------------------------------------===//
644
Chris Lattner229907c2011-07-18 04:54:35 +0000645static const fltSemantics *TypeToFloatSemantics(Type *Ty) {
Dan Gohman518cda42011-12-17 00:04:22 +0000646 if (Ty->isHalfTy())
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000647 return &APFloat::IEEEhalf();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000648 if (Ty->isFloatTy())
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000649 return &APFloat::IEEEsingle();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000650 if (Ty->isDoubleTy())
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000651 return &APFloat::IEEEdouble();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000652 if (Ty->isX86_FP80Ty())
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000653 return &APFloat::x87DoubleExtended();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000654 else if (Ty->isFP128Ty())
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000655 return &APFloat::IEEEquad();
Galina Kistanovafc259902012-07-13 01:25:27 +0000656
Chris Lattnerfdd87902009-10-05 05:54:46 +0000657 assert(Ty->isPPC_FP128Ty() && "Unknown FP format");
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000658 return &APFloat::PPCDoubleDouble();
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000659}
660
Chris Lattner0256be92012-01-27 03:08:05 +0000661Constant *ConstantFP::get(Type *Ty, double V) {
Owen Anderson69c464d2009-07-27 20:59:43 +0000662 LLVMContext &Context = Ty->getContext();
Galina Kistanovafc259902012-07-13 01:25:27 +0000663
Owen Anderson69c464d2009-07-27 20:59:43 +0000664 APFloat FV(V);
665 bool ignored;
666 FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
667 APFloat::rmNearestTiesToEven, &ignored);
668 Constant *C = get(Context, FV);
669
670 // For vectors, broadcast the value.
Chris Lattner229907c2011-07-18 04:54:35 +0000671 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
Chris Lattnere9eed292012-01-25 05:19:54 +0000672 return ConstantVector::getSplat(VTy->getNumElements(), C);
Owen Anderson69c464d2009-07-27 20:59:43 +0000673
674 return C;
675}
676
Sanjay Patel6a0f6672018-02-15 13:55:52 +0000677Constant *ConstantFP::get(Type *Ty, const APFloat &V) {
678 ConstantFP *C = get(Ty->getContext(), V);
679 assert(C->getType() == Ty->getScalarType() &&
680 "ConstantFP type doesn't match the type implied by its value!");
681
682 // For vectors, broadcast the value.
683 if (auto *VTy = dyn_cast<VectorType>(Ty))
684 return ConstantVector::getSplat(VTy->getNumElements(), C);
685
686 return C;
687}
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000688
Chris Lattner0256be92012-01-27 03:08:05 +0000689Constant *ConstantFP::get(Type *Ty, StringRef Str) {
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000690 LLVMContext &Context = Ty->getContext();
691
692 APFloat FV(*TypeToFloatSemantics(Ty->getScalarType()), Str);
693 Constant *C = get(Context, FV);
694
695 // For vectors, broadcast the value.
Chris Lattner229907c2011-07-18 04:54:35 +0000696 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
Chris Lattnere9eed292012-01-25 05:19:54 +0000697 return ConstantVector::getSplat(VTy->getNumElements(), C);
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000698
699 return C;
700}
701
Tom Stellard67246d12015-04-20 19:38:24 +0000702Constant *ConstantFP::getNaN(Type *Ty, bool Negative, unsigned Type) {
703 const fltSemantics &Semantics = *TypeToFloatSemantics(Ty->getScalarType());
704 APFloat NaN = APFloat::getNaN(Semantics, Negative, Type);
705 Constant *C = get(Ty->getContext(), NaN);
706
707 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
708 return ConstantVector::getSplat(VTy->getNumElements(), C);
709
710 return C;
711}
712
Benjamin Kramer5d2ff222014-01-18 16:43:06 +0000713Constant *ConstantFP::getNegativeZero(Type *Ty) {
714 const fltSemantics &Semantics = *TypeToFloatSemantics(Ty->getScalarType());
715 APFloat NegZero = APFloat::getZero(Semantics, /*Negative=*/true);
716 Constant *C = get(Ty->getContext(), NegZero);
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000717
Benjamin Kramer5d2ff222014-01-18 16:43:06 +0000718 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
719 return ConstantVector::getSplat(VTy->getNumElements(), C);
720
721 return C;
Owen Anderson69c464d2009-07-27 20:59:43 +0000722}
723
724
Chris Lattnere9eed292012-01-25 05:19:54 +0000725Constant *ConstantFP::getZeroValueForNegation(Type *Ty) {
Benjamin Kramer5d2ff222014-01-18 16:43:06 +0000726 if (Ty->isFPOrFPVectorTy())
727 return getNegativeZero(Ty);
Owen Anderson69c464d2009-07-27 20:59:43 +0000728
Owen Anderson5a1acd92009-07-31 20:28:14 +0000729 return Constant::getNullValue(Ty);
Owen Anderson69c464d2009-07-27 20:59:43 +0000730}
731
732
733// ConstantFP accessors.
734ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) {
Owen Anderson69c464d2009-07-27 20:59:43 +0000735 LLVMContextImpl* pImpl = Context.pImpl;
Galina Kistanovafc259902012-07-13 01:25:27 +0000736
Justin Lebar611c5c22016-10-10 16:26:13 +0000737 std::unique_ptr<ConstantFP> &Slot = pImpl->FPConstants[V];
Galina Kistanovafc259902012-07-13 01:25:27 +0000738
Owen Anderson69c464d2009-07-27 20:59:43 +0000739 if (!Slot) {
Chris Lattner229907c2011-07-18 04:54:35 +0000740 Type *Ty;
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000741 if (&V.getSemantics() == &APFloat::IEEEhalf())
Dan Gohman518cda42011-12-17 00:04:22 +0000742 Ty = Type::getHalfTy(Context);
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000743 else if (&V.getSemantics() == &APFloat::IEEEsingle())
Owen Anderson5dab84c2009-10-19 20:11:52 +0000744 Ty = Type::getFloatTy(Context);
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000745 else if (&V.getSemantics() == &APFloat::IEEEdouble())
Owen Anderson5dab84c2009-10-19 20:11:52 +0000746 Ty = Type::getDoubleTy(Context);
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000747 else if (&V.getSemantics() == &APFloat::x87DoubleExtended())
Owen Anderson5dab84c2009-10-19 20:11:52 +0000748 Ty = Type::getX86_FP80Ty(Context);
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000749 else if (&V.getSemantics() == &APFloat::IEEEquad())
Owen Anderson5dab84c2009-10-19 20:11:52 +0000750 Ty = Type::getFP128Ty(Context);
751 else {
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000752 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble() &&
Owen Anderson5dab84c2009-10-19 20:11:52 +0000753 "Unknown FP format");
754 Ty = Type::getPPC_FP128Ty(Context);
Owen Anderson69c464d2009-07-27 20:59:43 +0000755 }
Justin Lebar611c5c22016-10-10 16:26:13 +0000756 Slot.reset(new ConstantFP(Ty, V));
Owen Anderson69c464d2009-07-27 20:59:43 +0000757 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000758
Justin Lebar611c5c22016-10-10 16:26:13 +0000759 return Slot.get();
Owen Anderson69c464d2009-07-27 20:59:43 +0000760}
761
Benjamin Kramer5d2ff222014-01-18 16:43:06 +0000762Constant *ConstantFP::getInfinity(Type *Ty, bool Negative) {
763 const fltSemantics &Semantics = *TypeToFloatSemantics(Ty->getScalarType());
764 Constant *C = get(Ty->getContext(), APFloat::getInf(Semantics, Negative));
765
766 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
767 return ConstantVector::getSplat(VTy->getNumElements(), C);
768
769 return C;
Dan Gohmanfeb50212009-09-25 23:00:48 +0000770}
771
Duncan P. N. Exon Smithb6452792016-02-21 02:39:49 +0000772ConstantFP::ConstantFP(Type *Ty, const APFloat &V)
773 : ConstantData(Ty, ConstantFPVal), Val(V) {
Chris Lattner98bd9392008-04-09 06:38:30 +0000774 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
775 "FP type Mismatch");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000776}
777
Chris Lattnerbe6610c2011-07-15 06:14:08 +0000778bool ConstantFP::isExactlyValue(const APFloat &V) const {
Dale Johannesend246b2c2007-08-30 00:23:21 +0000779 return Val.bitwiseIsEqual(V);
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000780}
781
Pete Cooper86dd4cf2015-06-23 21:55:11 +0000782/// Remove the constant from the constant table.
783void ConstantFP::destroyConstantImpl() {
Craig Topperccbb8102017-06-27 19:57:51 +0000784 llvm_unreachable("You can't ConstantFP->destroyConstantImpl()!");
Pete Cooper86dd4cf2015-06-23 21:55:11 +0000785}
786
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000787//===----------------------------------------------------------------------===//
Chris Lattner030af792012-01-24 05:42:11 +0000788// ConstantAggregateZero Implementation
789//===----------------------------------------------------------------------===//
790
Chris Lattner7e683d12012-01-25 06:16:32 +0000791Constant *ConstantAggregateZero::getSequentialElement() const {
Chris Lattner8326bd82012-01-26 00:42:34 +0000792 return Constant::getNullValue(getType()->getSequentialElementType());
Chris Lattner030af792012-01-24 05:42:11 +0000793}
794
Chris Lattner7e683d12012-01-25 06:16:32 +0000795Constant *ConstantAggregateZero::getStructElement(unsigned Elt) const {
Chris Lattner8326bd82012-01-26 00:42:34 +0000796 return Constant::getNullValue(getType()->getStructElementType(Elt));
Chris Lattner030af792012-01-24 05:42:11 +0000797}
798
Chris Lattner7e683d12012-01-25 06:16:32 +0000799Constant *ConstantAggregateZero::getElementValue(Constant *C) const {
Chris Lattner030af792012-01-24 05:42:11 +0000800 if (isa<SequentialType>(getType()))
801 return getSequentialElement();
802 return getStructElement(cast<ConstantInt>(C)->getZExtValue());
803}
804
Chris Lattner7e683d12012-01-25 06:16:32 +0000805Constant *ConstantAggregateZero::getElementValue(unsigned Idx) const {
Chris Lattnerf7eb5432012-01-24 07:54:10 +0000806 if (isa<SequentialType>(getType()))
807 return getSequentialElement();
808 return getStructElement(Idx);
809}
810
David Majnemer9b529a72015-02-16 04:02:09 +0000811unsigned ConstantAggregateZero::getNumElements() const {
Craig Toppere3dcce92015-08-01 22:20:21 +0000812 Type *Ty = getType();
813 if (auto *AT = dyn_cast<ArrayType>(Ty))
David Majnemer9b529a72015-02-16 04:02:09 +0000814 return AT->getNumElements();
Craig Toppere3dcce92015-08-01 22:20:21 +0000815 if (auto *VT = dyn_cast<VectorType>(Ty))
David Majnemer9b529a72015-02-16 04:02:09 +0000816 return VT->getNumElements();
817 return Ty->getStructNumElements();
818}
Chris Lattnerf7eb5432012-01-24 07:54:10 +0000819
Chris Lattner030af792012-01-24 05:42:11 +0000820//===----------------------------------------------------------------------===//
821// UndefValue Implementation
822//===----------------------------------------------------------------------===//
823
Chris Lattner7e683d12012-01-25 06:16:32 +0000824UndefValue *UndefValue::getSequentialElement() const {
Chris Lattner8326bd82012-01-26 00:42:34 +0000825 return UndefValue::get(getType()->getSequentialElementType());
Chris Lattner030af792012-01-24 05:42:11 +0000826}
827
Chris Lattner7e683d12012-01-25 06:16:32 +0000828UndefValue *UndefValue::getStructElement(unsigned Elt) const {
Chris Lattner8326bd82012-01-26 00:42:34 +0000829 return UndefValue::get(getType()->getStructElementType(Elt));
Chris Lattner030af792012-01-24 05:42:11 +0000830}
831
Chris Lattner7e683d12012-01-25 06:16:32 +0000832UndefValue *UndefValue::getElementValue(Constant *C) const {
Chris Lattner030af792012-01-24 05:42:11 +0000833 if (isa<SequentialType>(getType()))
834 return getSequentialElement();
835 return getStructElement(cast<ConstantInt>(C)->getZExtValue());
836}
837
Chris Lattner7e683d12012-01-25 06:16:32 +0000838UndefValue *UndefValue::getElementValue(unsigned Idx) const {
Chris Lattnerf7eb5432012-01-24 07:54:10 +0000839 if (isa<SequentialType>(getType()))
840 return getSequentialElement();
841 return getStructElement(Idx);
842}
843
David Majnemer9b529a72015-02-16 04:02:09 +0000844unsigned UndefValue::getNumElements() const {
Craig Toppere3dcce92015-08-01 22:20:21 +0000845 Type *Ty = getType();
Peter Collingbournebc070522016-12-02 03:20:58 +0000846 if (auto *ST = dyn_cast<SequentialType>(Ty))
847 return ST->getNumElements();
David Majnemer9b529a72015-02-16 04:02:09 +0000848 return Ty->getStructNumElements();
849}
Chris Lattner030af792012-01-24 05:42:11 +0000850
851//===----------------------------------------------------------------------===//
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000852// ConstantXXX Classes
853//===----------------------------------------------------------------------===//
854
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000855template <typename ItTy, typename EltTy>
856static bool rangeOnlyContains(ItTy Start, ItTy End, EltTy Elt) {
857 for (; Start != End; ++Start)
858 if (*Start != Elt)
859 return false;
860 return true;
861}
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000862
Justin Bogner909e1c02015-12-01 20:20:49 +0000863template <typename SequentialTy, typename ElementTy>
864static Constant *getIntSequenceIfElementsMatch(ArrayRef<Constant *> V) {
865 assert(!V.empty() && "Cannot get empty int sequence.");
866
867 SmallVector<ElementTy, 16> Elts;
868 for (Constant *C : V)
869 if (auto *CI = dyn_cast<ConstantInt>(C))
870 Elts.push_back(CI->getZExtValue());
871 else
872 return nullptr;
873 return SequentialTy::get(V[0]->getContext(), Elts);
874}
875
876template <typename SequentialTy, typename ElementTy>
877static Constant *getFPSequenceIfElementsMatch(ArrayRef<Constant *> V) {
878 assert(!V.empty() && "Cannot get empty FP sequence.");
879
880 SmallVector<ElementTy, 16> Elts;
881 for (Constant *C : V)
882 if (auto *CFP = dyn_cast<ConstantFP>(C))
883 Elts.push_back(CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
884 else
885 return nullptr;
886 return SequentialTy::getFP(V[0]->getContext(), Elts);
887}
888
889template <typename SequenceTy>
890static Constant *getSequenceIfElementsMatch(Constant *C,
891 ArrayRef<Constant *> V) {
892 // We speculatively build the elements here even if it turns out that there is
893 // a constantexpr or something else weird, since it is so uncommon for that to
894 // happen.
895 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
896 if (CI->getType()->isIntegerTy(8))
897 return getIntSequenceIfElementsMatch<SequenceTy, uint8_t>(V);
898 else if (CI->getType()->isIntegerTy(16))
899 return getIntSequenceIfElementsMatch<SequenceTy, uint16_t>(V);
900 else if (CI->getType()->isIntegerTy(32))
901 return getIntSequenceIfElementsMatch<SequenceTy, uint32_t>(V);
902 else if (CI->getType()->isIntegerTy(64))
903 return getIntSequenceIfElementsMatch<SequenceTy, uint64_t>(V);
904 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
Justin Bogner0ebc8602015-12-08 03:01:16 +0000905 if (CFP->getType()->isHalfTy())
906 return getFPSequenceIfElementsMatch<SequenceTy, uint16_t>(V);
907 else if (CFP->getType()->isFloatTy())
Justin Bogner909e1c02015-12-01 20:20:49 +0000908 return getFPSequenceIfElementsMatch<SequenceTy, uint32_t>(V);
909 else if (CFP->getType()->isDoubleTy())
910 return getFPSequenceIfElementsMatch<SequenceTy, uint64_t>(V);
911 }
912
913 return nullptr;
914}
915
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +0000916ConstantAggregate::ConstantAggregate(CompositeType *T, ValueTy VT,
917 ArrayRef<Constant *> V)
918 : Constant(T, VT, OperandTraits<ConstantAggregate>::op_end(this) - V.size(),
919 V.size()) {
Jay Foad89d9b812011-07-25 10:14:44 +0000920 std::copy(V.begin(), V.end(), op_begin());
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +0000921
922 // Check that types match, unless this is an opaque struct.
923 if (auto *ST = dyn_cast<StructType>(T))
924 if (ST->isOpaque())
925 return;
926 for (unsigned I = 0, E = V.size(); I != E; ++I)
927 assert(V[I]->getType() == T->getTypeAtIndex(I) &&
928 "Initializer for composite element doesn't match!");
929}
930
931ConstantArray::ConstantArray(ArrayType *T, ArrayRef<Constant *> V)
932 : ConstantAggregate(T, ConstantArrayVal, V) {
933 assert(V.size() == T->getNumElements() &&
934 "Invalid initializer for constant array");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000935}
936
Chris Lattner229907c2011-07-18 04:54:35 +0000937Constant *ConstantArray::get(ArrayType *Ty, ArrayRef<Constant*> V) {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000938 if (Constant *C = getImpl(Ty, V))
939 return C;
940 return Ty->getContext().pImpl->ArrayConstants.getOrCreate(Ty, V);
941}
Justin Bogner909e1c02015-12-01 20:20:49 +0000942
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000943Constant *ConstantArray::getImpl(ArrayType *Ty, ArrayRef<Constant*> V) {
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000944 // Empty arrays are canonicalized to ConstantAggregateZero.
945 if (V.empty())
946 return ConstantAggregateZero::get(Ty);
947
Jeffrey Yasskin8ce67f82009-09-30 21:08:08 +0000948 for (unsigned i = 0, e = V.size(); i != e; ++i) {
949 assert(V[i]->getType() == Ty->getElementType() &&
950 "Wrong type in array element initializer");
951 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000952
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000953 // If this is an all-zero array, return a ConstantAggregateZero object. If
954 // all undef, return an UndefValue, if "all simple", then return a
955 // ConstantDataArray.
956 Constant *C = V[0];
957 if (isa<UndefValue>(C) && rangeOnlyContains(V.begin(), V.end(), C))
958 return UndefValue::get(Ty);
Chris Lattnerf14a67f2012-01-26 02:31:22 +0000959
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000960 if (C->isNullValue() && rangeOnlyContains(V.begin(), V.end(), C))
961 return ConstantAggregateZero::get(Ty);
962
963 // Check to see if all of the elements are ConstantFP or ConstantInt and if
964 // the element type is compatible with ConstantDataVector. If so, use it.
Justin Bogner909e1c02015-12-01 20:20:49 +0000965 if (ConstantDataSequential::isElementTypeCompatible(C->getType()))
966 return getSequenceIfElementsMatch<ConstantDataArray>(C, V);
Chris Lattnerf14a67f2012-01-26 02:31:22 +0000967
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000968 // Otherwise, we really do want to create a ConstantArray.
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000969 return nullptr;
Owen Andersonc2c79322009-07-28 18:32:17 +0000970}
971
Chris Lattnercc19efa2011-06-20 04:01:31 +0000972StructType *ConstantStruct::getTypeForElements(LLVMContext &Context,
973 ArrayRef<Constant*> V,
974 bool Packed) {
Bill Wendling3ae7dd32012-02-07 01:27:51 +0000975 unsigned VecSize = V.size();
976 SmallVector<Type*, 16> EltTypes(VecSize);
977 for (unsigned i = 0; i != VecSize; ++i)
978 EltTypes[i] = V[i]->getType();
Galina Kistanovafc259902012-07-13 01:25:27 +0000979
Chris Lattnercc19efa2011-06-20 04:01:31 +0000980 return StructType::get(Context, EltTypes, Packed);
981}
982
983
984StructType *ConstantStruct::getTypeForElements(ArrayRef<Constant*> V,
985 bool Packed) {
986 assert(!V.empty() &&
987 "ConstantStruct::getTypeForElements cannot be called on empty list");
988 return getTypeForElements(V[0]->getContext(), V, Packed);
989}
990
Jay Foad89d9b812011-07-25 10:14:44 +0000991ConstantStruct::ConstantStruct(StructType *T, ArrayRef<Constant *> V)
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +0000992 : ConstantAggregate(T, ConstantStructVal, V) {
993 assert((T->isOpaque() || V.size() == T->getNumElements()) &&
994 "Invalid initializer for constant struct");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000995}
996
Owen Anderson45308b52009-07-27 22:29:26 +0000997// ConstantStruct accessors.
Chris Lattner229907c2011-07-18 04:54:35 +0000998Constant *ConstantStruct::get(StructType *ST, ArrayRef<Constant*> V) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000999 assert((ST->isOpaque() || ST->getNumElements() == V.size()) &&
1000 "Incorrect # elements specified to ConstantStruct::get");
Chris Lattnerf14a67f2012-01-26 02:31:22 +00001001
1002 // Create a ConstantAggregateZero value if all elements are zeros.
1003 bool isZero = true;
1004 bool isUndef = false;
1005
1006 if (!V.empty()) {
1007 isUndef = isa<UndefValue>(V[0]);
1008 isZero = V[0]->isNullValue();
1009 if (isUndef || isZero) {
1010 for (unsigned i = 0, e = V.size(); i != e; ++i) {
1011 if (!V[i]->isNullValue())
1012 isZero = false;
1013 if (!isa<UndefValue>(V[i]))
1014 isUndef = false;
1015 }
1016 }
Galina Kistanovafc259902012-07-13 01:25:27 +00001017 }
Chris Lattnerf14a67f2012-01-26 02:31:22 +00001018 if (isZero)
1019 return ConstantAggregateZero::get(ST);
1020 if (isUndef)
1021 return UndefValue::get(ST);
Galina Kistanovafc259902012-07-13 01:25:27 +00001022
Chris Lattnerf14a67f2012-01-26 02:31:22 +00001023 return ST->getContext().pImpl->StructConstants.getOrCreate(ST, V);
Owen Anderson45308b52009-07-27 22:29:26 +00001024}
1025
Jay Foad89d9b812011-07-25 10:14:44 +00001026ConstantVector::ConstantVector(VectorType *T, ArrayRef<Constant *> V)
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +00001027 : ConstantAggregate(T, ConstantVectorVal, V) {
Duncan P. N. Exon Smithdb63bda2016-04-05 20:53:47 +00001028 assert(V.size() == T->getNumElements() &&
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +00001029 "Invalid initializer for constant vector");
Brian Gaeke02209042004-08-20 06:00:58 +00001030}
1031
Owen Anderson4aa32952009-07-28 21:19:26 +00001032// ConstantVector accessors.
Jay Foadb8a8bed32011-06-22 09:10:19 +00001033Constant *ConstantVector::get(ArrayRef<Constant*> V) {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001034 if (Constant *C = getImpl(V))
1035 return C;
1036 VectorType *Ty = VectorType::get(V.front()->getType(), V.size());
1037 return Ty->getContext().pImpl->VectorConstants.getOrCreate(Ty, V);
1038}
Justin Bogner909e1c02015-12-01 20:20:49 +00001039
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001040Constant *ConstantVector::getImpl(ArrayRef<Constant*> V) {
Jay Foad9f32cfd2011-01-27 14:44:55 +00001041 assert(!V.empty() && "Vectors can't be empty");
Chris Lattner229907c2011-07-18 04:54:35 +00001042 VectorType *T = VectorType::get(V.front()->getType(), V.size());
Jay Foad9f32cfd2011-01-27 14:44:55 +00001043
Chris Lattner69229312011-02-15 00:14:00 +00001044 // If this is an all-undef or all-zero vector, return a
Owen Anderson4aa32952009-07-28 21:19:26 +00001045 // ConstantAggregateZero or UndefValue.
1046 Constant *C = V[0];
1047 bool isZero = C->isNullValue();
1048 bool isUndef = isa<UndefValue>(C);
1049
1050 if (isZero || isUndef) {
1051 for (unsigned i = 1, e = V.size(); i != e; ++i)
1052 if (V[i] != C) {
1053 isZero = isUndef = false;
1054 break;
1055 }
1056 }
Galina Kistanovafc259902012-07-13 01:25:27 +00001057
Owen Anderson4aa32952009-07-28 21:19:26 +00001058 if (isZero)
Owen Andersonb292b8c2009-07-30 23:03:37 +00001059 return ConstantAggregateZero::get(T);
Owen Anderson4aa32952009-07-28 21:19:26 +00001060 if (isUndef)
Owen Andersonb292b8c2009-07-30 23:03:37 +00001061 return UndefValue::get(T);
Galina Kistanovafc259902012-07-13 01:25:27 +00001062
Chris Lattner978fe0c2012-01-30 06:21:21 +00001063 // Check to see if all of the elements are ConstantFP or ConstantInt and if
1064 // the element type is compatible with ConstantDataVector. If so, use it.
Justin Bogner909e1c02015-12-01 20:20:49 +00001065 if (ConstantDataSequential::isElementTypeCompatible(C->getType()))
1066 return getSequenceIfElementsMatch<ConstantDataVector>(C, V);
Galina Kistanovafc259902012-07-13 01:25:27 +00001067
Chris Lattner978fe0c2012-01-30 06:21:21 +00001068 // Otherwise, the element type isn't compatible with ConstantDataVector, or
Craig Topperdf907262017-04-11 06:41:55 +00001069 // the operand list contains a ConstantExpr or something else strange.
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001070 return nullptr;
Owen Anderson4aa32952009-07-28 21:19:26 +00001071}
1072
Chris Lattnere9eed292012-01-25 05:19:54 +00001073Constant *ConstantVector::getSplat(unsigned NumElts, Constant *V) {
Chris Lattner978fe0c2012-01-30 06:21:21 +00001074 // If this splat is compatible with ConstantDataVector, use it instead of
1075 // ConstantVector.
1076 if ((isa<ConstantFP>(V) || isa<ConstantInt>(V)) &&
1077 ConstantDataSequential::isElementTypeCompatible(V->getType()))
1078 return ConstantDataVector::getSplat(NumElts, V);
Galina Kistanovafc259902012-07-13 01:25:27 +00001079
Chris Lattnere9eed292012-01-25 05:19:54 +00001080 SmallVector<Constant*, 32> Elts(NumElts, V);
1081 return get(Elts);
1082}
1083
David Majnemerf0f224d2015-11-11 21:57:16 +00001084ConstantTokenNone *ConstantTokenNone::get(LLVMContext &Context) {
1085 LLVMContextImpl *pImpl = Context.pImpl;
1086 if (!pImpl->TheNoneToken)
David Majnemer2dd41c52015-11-16 20:55:57 +00001087 pImpl->TheNoneToken.reset(new ConstantTokenNone(Context));
1088 return pImpl->TheNoneToken.get();
David Majnemerf0f224d2015-11-11 21:57:16 +00001089}
1090
1091/// Remove the constant from the constant table.
1092void ConstantTokenNone::destroyConstantImpl() {
1093 llvm_unreachable("You can't ConstantTokenNone->destroyConstantImpl()!");
1094}
Chris Lattnere9eed292012-01-25 05:19:54 +00001095
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001096// Utility function for determining if a ConstantExpr is a CastOp or not. This
1097// can't be inline because we don't want to #include Instruction.h into
1098// Constant.h
1099bool ConstantExpr::isCast() const {
1100 return Instruction::isCast(getOpcode());
1101}
1102
Reid Spenceree3c9912006-12-04 05:19:50 +00001103bool ConstantExpr::isCompare() const {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001104 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
Reid Spenceree3c9912006-12-04 05:19:50 +00001105}
1106
Dan Gohman7190d482009-09-10 23:37:55 +00001107bool ConstantExpr::isGEPWithNoNotionalOverIndexing() const {
1108 if (getOpcode() != Instruction::GetElementPtr) return false;
1109
1110 gep_type_iterator GEPI = gep_type_begin(this), E = gep_type_end(this);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001111 User::const_op_iterator OI = std::next(this->op_begin());
Dan Gohman7190d482009-09-10 23:37:55 +00001112
Sanjay Patel81ed3492016-12-11 20:07:02 +00001113 // The remaining indices may be compile-time known integers within the bounds
1114 // of the corresponding notional static array types.
Dan Gohman7190d482009-09-10 23:37:55 +00001115 for (; GEPI != E; ++GEPI, ++OI) {
Sanjay Patel81ed3492016-12-11 20:07:02 +00001116 if (isa<UndefValue>(*OI))
1117 continue;
1118 auto *CI = dyn_cast<ConstantInt>(*OI);
1119 if (!CI || (GEPI.isBoundedSequential() &&
1120 (CI->getValue().getActiveBits() > 64 ||
1121 CI->getZExtValue() >= GEPI.getSequentialNumElements())))
Peter Collingbourneab85225b2016-12-02 02:24:42 +00001122 return false;
Dan Gohman7190d482009-09-10 23:37:55 +00001123 }
1124
1125 // All the indices checked out.
1126 return true;
1127}
1128
Dan Gohman1ecaf452008-05-31 00:58:22 +00001129bool ConstantExpr::hasIndices() const {
1130 return getOpcode() == Instruction::ExtractValue ||
1131 getOpcode() == Instruction::InsertValue;
1132}
1133
Jay Foad0091fe82011-04-13 15:22:40 +00001134ArrayRef<unsigned> ConstantExpr::getIndices() const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001135 if (const ExtractValueConstantExpr *EVCE =
1136 dyn_cast<ExtractValueConstantExpr>(this))
1137 return EVCE->Indices;
Dan Gohmana469bdb2008-06-23 16:39:44 +00001138
1139 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001140}
1141
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001142unsigned ConstantExpr::getPredicate() const {
Craig Toppercc03b492015-12-15 06:11:36 +00001143 return cast<CompareConstantExpr>(this)->predicate;
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001144}
Chris Lattner60e0dd72001-10-03 06:12:09 +00001145
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001146Constant *
1147ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +00001148 assert(Op->getType() == getOperand(OpNo)->getType() &&
1149 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +00001150 if (getOperand(OpNo) == Op)
1151 return const_cast<ConstantExpr*>(this);
Chris Lattner37e38352012-01-26 20:37:11 +00001152
1153 SmallVector<Constant*, 8> NewOps;
1154 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1155 NewOps.push_back(i == OpNo ? Op : getOperand(i));
Galina Kistanovafc259902012-07-13 01:25:27 +00001156
Chris Lattner37e38352012-01-26 20:37:11 +00001157 return getWithOperands(NewOps);
Chris Lattner227816342006-07-14 22:20:01 +00001158}
1159
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001160Constant *ConstantExpr::getWithOperands(ArrayRef<Constant *> Ops, Type *Ty,
David Blaikie88208842015-08-21 20:16:51 +00001161 bool OnlyIfReduced, Type *SrcTy) const {
Jay Foad5c984e562011-04-13 13:46:01 +00001162 assert(Ops.size() == getNumOperands() && "Operand count mismatch!");
Galina Kistanovafc259902012-07-13 01:25:27 +00001163
Benjamin Kramer8008e9f2015-03-02 11:57:04 +00001164 // If no operands changed return self.
1165 if (Ty == getType() && std::equal(Ops.begin(), Ops.end(), op_begin()))
Chris Lattner227816342006-07-14 22:20:01 +00001166 return const_cast<ConstantExpr*>(this);
1167
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001168 Type *OnlyIfReducedTy = OnlyIfReduced ? Ty : nullptr;
Chris Lattner227816342006-07-14 22:20:01 +00001169 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001170 case Instruction::Trunc:
1171 case Instruction::ZExt:
1172 case Instruction::SExt:
1173 case Instruction::FPTrunc:
1174 case Instruction::FPExt:
1175 case Instruction::UIToFP:
1176 case Instruction::SIToFP:
1177 case Instruction::FPToUI:
1178 case Instruction::FPToSI:
1179 case Instruction::PtrToInt:
1180 case Instruction::IntToPtr:
1181 case Instruction::BitCast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001182 case Instruction::AddrSpaceCast:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001183 return ConstantExpr::getCast(getOpcode(), Ops[0], Ty, OnlyIfReduced);
Chris Lattner227816342006-07-14 22:20:01 +00001184 case Instruction::Select:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001185 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2], OnlyIfReducedTy);
Chris Lattner227816342006-07-14 22:20:01 +00001186 case Instruction::InsertElement:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001187 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2],
1188 OnlyIfReducedTy);
Chris Lattner227816342006-07-14 22:20:01 +00001189 case Instruction::ExtractElement:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001190 return ConstantExpr::getExtractElement(Ops[0], Ops[1], OnlyIfReducedTy);
Chris Lattner37e38352012-01-26 20:37:11 +00001191 case Instruction::InsertValue:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001192 return ConstantExpr::getInsertValue(Ops[0], Ops[1], getIndices(),
1193 OnlyIfReducedTy);
Chris Lattner37e38352012-01-26 20:37:11 +00001194 case Instruction::ExtractValue:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001195 return ConstantExpr::getExtractValue(Ops[0], getIndices(), OnlyIfReducedTy);
Chris Lattner227816342006-07-14 22:20:01 +00001196 case Instruction::ShuffleVector:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001197 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2],
1198 OnlyIfReducedTy);
David Blaikie88208842015-08-21 20:16:51 +00001199 case Instruction::GetElementPtr: {
1200 auto *GEPO = cast<GEPOperator>(this);
1201 assert(SrcTy || (Ops[0]->getType() == getOperand(0)->getType()));
1202 return ConstantExpr::getGetElementPtr(
1203 SrcTy ? SrcTy : GEPO->getSourceElementType(), Ops[0], Ops.slice(1),
Peter Collingbourned93620b2016-11-10 22:34:55 +00001204 GEPO->isInBounds(), GEPO->getInRangeIndex(), OnlyIfReducedTy);
David Blaikie88208842015-08-21 20:16:51 +00001205 }
Reid Spencer266e42b2006-12-23 06:05:41 +00001206 case Instruction::ICmp:
1207 case Instruction::FCmp:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001208 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1],
1209 OnlyIfReducedTy);
Chris Lattner227816342006-07-14 22:20:01 +00001210 default:
1211 assert(getNumOperands() == 2 && "Must be binary operator?");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001212 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1], SubclassOptionalData,
1213 OnlyIfReducedTy);
Chris Lattner7c1018a2006-07-14 19:37:40 +00001214 }
1215}
1216
Chris Lattner2f7c9632001-06-06 20:29:01 +00001217
1218//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00001219// isValueValidForType implementations
1220
Chris Lattner229907c2011-07-18 04:54:35 +00001221bool ConstantInt::isValueValidForType(Type *Ty, uint64_t Val) {
Chris Lattner8326bd82012-01-26 00:42:34 +00001222 unsigned NumBits = Ty->getIntegerBitWidth(); // assert okay
1223 if (Ty->isIntegerTy(1))
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001224 return Val == 0 || Val == 1;
Craig Topper50b1e512017-06-07 00:58:05 +00001225 return isUIntN(NumBits, Val);
Reid Spencere7334722006-12-19 01:28:19 +00001226}
1227
Chris Lattner229907c2011-07-18 04:54:35 +00001228bool ConstantInt::isValueValidForType(Type *Ty, int64_t Val) {
Chris Lattner8326bd82012-01-26 00:42:34 +00001229 unsigned NumBits = Ty->getIntegerBitWidth();
1230 if (Ty->isIntegerTy(1))
Reid Spencera94d3942007-01-19 21:13:56 +00001231 return Val == 0 || Val == 1 || Val == -1;
Craig Topper50b1e512017-06-07 00:58:05 +00001232 return isIntN(NumBits, Val);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001233}
1234
Chris Lattner229907c2011-07-18 04:54:35 +00001235bool ConstantFP::isValueValidForType(Type *Ty, const APFloat& Val) {
Dale Johannesend246b2c2007-08-30 00:23:21 +00001236 // convert modifies in place, so make a copy.
1237 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001238 bool losesInfo;
Chris Lattner6b727592004-06-17 18:19:28 +00001239 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001240 default:
1241 return false; // These can't be represented as floating point!
1242
Dale Johannesend246b2c2007-08-30 00:23:21 +00001243 // FIXME rounding mode needs to be more flexible
Dan Gohman518cda42011-12-17 00:04:22 +00001244 case Type::HalfTyID: {
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001245 if (&Val2.getSemantics() == &APFloat::IEEEhalf())
Dan Gohman518cda42011-12-17 00:04:22 +00001246 return true;
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001247 Val2.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &losesInfo);
Dan Gohman518cda42011-12-17 00:04:22 +00001248 return !losesInfo;
1249 }
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001250 case Type::FloatTyID: {
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001251 if (&Val2.getSemantics() == &APFloat::IEEEsingle())
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001252 return true;
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001253 Val2.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &losesInfo);
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001254 return !losesInfo;
1255 }
1256 case Type::DoubleTyID: {
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001257 if (&Val2.getSemantics() == &APFloat::IEEEhalf() ||
1258 &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1259 &Val2.getSemantics() == &APFloat::IEEEdouble())
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001260 return true;
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001261 Val2.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &losesInfo);
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001262 return !losesInfo;
1263 }
Dale Johannesenbdad8092007-08-09 22:51:36 +00001264 case Type::X86_FP80TyID:
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001265 return &Val2.getSemantics() == &APFloat::IEEEhalf() ||
1266 &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1267 &Val2.getSemantics() == &APFloat::IEEEdouble() ||
1268 &Val2.getSemantics() == &APFloat::x87DoubleExtended();
Dale Johannesenbdad8092007-08-09 22:51:36 +00001269 case Type::FP128TyID:
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001270 return &Val2.getSemantics() == &APFloat::IEEEhalf() ||
1271 &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1272 &Val2.getSemantics() == &APFloat::IEEEdouble() ||
1273 &Val2.getSemantics() == &APFloat::IEEEquad();
Dale Johannesen007aa372007-10-11 18:07:22 +00001274 case Type::PPC_FP128TyID:
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001275 return &Val2.getSemantics() == &APFloat::IEEEhalf() ||
1276 &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1277 &Val2.getSemantics() == &APFloat::IEEEdouble() ||
1278 &Val2.getSemantics() == &APFloat::PPCDoubleDouble();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001279 }
Chris Lattneraa2372562006-05-24 17:04:05 +00001280}
Chris Lattner9655e542001-07-20 19:16:02 +00001281
Chris Lattner030af792012-01-24 05:42:11 +00001282
Chris Lattner49d855c2001-09-07 16:46:31 +00001283//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +00001284// Factory Function Implementation
1285
Chris Lattnerc7f9fd42012-01-23 15:20:12 +00001286ConstantAggregateZero *ConstantAggregateZero::get(Type *Ty) {
Chris Lattner13ee7952010-08-28 04:09:24 +00001287 assert((Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy()) &&
Owen Andersonb292b8c2009-07-30 23:03:37 +00001288 "Cannot create an aggregate zero of non-aggregate type!");
David Blaikie899b85a2014-11-25 02:13:54 +00001289
Justin Lebar611c5c22016-10-10 16:26:13 +00001290 std::unique_ptr<ConstantAggregateZero> &Entry =
1291 Ty->getContext().pImpl->CAZConstants[Ty];
1292 if (!Entry)
1293 Entry.reset(new ConstantAggregateZero(Ty));
1294
1295 return Entry.get();
Owen Andersonb292b8c2009-07-30 23:03:37 +00001296}
1297
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001298/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001299void ConstantAggregateZero::destroyConstantImpl() {
Chris Lattnerc7f9fd42012-01-23 15:20:12 +00001300 getContext().pImpl->CAZConstants.erase(getType());
Chris Lattner9fba3da2004-02-15 05:53:04 +00001301}
1302
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001303/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001304void ConstantArray::destroyConstantImpl() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001305 getType()->getContext().pImpl->ArrayConstants.remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001306}
1307
Chris Lattner81fabb02002-08-26 17:53:56 +00001308
Chris Lattner3462ae32001-12-03 22:26:30 +00001309//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001310//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001311
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001312/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001313void ConstantStruct::destroyConstantImpl() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001314 getType()->getContext().pImpl->StructConstants.remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001315}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001316
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001317/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001318void ConstantVector::destroyConstantImpl() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001319 getType()->getContext().pImpl->VectorConstants.remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001320}
1321
Duncan Sandse6beec62012-11-13 12:59:33 +00001322Constant *Constant::getSplatValue() const {
1323 assert(this->getType()->isVectorTy() && "Only valid for vectors!");
1324 if (isa<ConstantAggregateZero>(this))
1325 return getNullValue(this->getType()->getVectorElementType());
1326 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this))
1327 return CV->getSplatValue();
1328 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
1329 return CV->getSplatValue();
Craig Topperc6207612014-04-09 06:08:46 +00001330 return nullptr;
Duncan Sandse6beec62012-11-13 12:59:33 +00001331}
1332
Duncan Sandscf0ff032011-02-01 08:39:12 +00001333Constant *ConstantVector::getSplatValue() const {
Dan Gohman07159202007-10-17 17:51:30 +00001334 // Check out first element.
1335 Constant *Elt = getOperand(0);
1336 // Then make sure all remaining elements point to the same value.
1337 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
Chris Lattnerd04e32d2011-07-17 06:01:30 +00001338 if (getOperand(I) != Elt)
Craig Topperc6207612014-04-09 06:08:46 +00001339 return nullptr;
Dan Gohman07159202007-10-17 17:51:30 +00001340 return Elt;
1341}
1342
Duncan Sandse6beec62012-11-13 12:59:33 +00001343const APInt &Constant::getUniqueInteger() const {
1344 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
1345 return CI->getValue();
1346 assert(this->getSplatValue() && "Doesn't contain a unique integer!");
1347 const Constant *C = this->getAggregateElement(0U);
1348 assert(C && isa<ConstantInt>(C) && "Not a vector of numbers!");
1349 return cast<ConstantInt>(C)->getValue();
1350}
1351
Chris Lattner31b132c2009-10-28 00:01:44 +00001352//---- ConstantPointerNull::get() implementation.
Chris Lattnerd7a73302001-10-13 06:57:33 +00001353//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001354
Chris Lattner229907c2011-07-18 04:54:35 +00001355ConstantPointerNull *ConstantPointerNull::get(PointerType *Ty) {
Justin Lebar611c5c22016-10-10 16:26:13 +00001356 std::unique_ptr<ConstantPointerNull> &Entry =
1357 Ty->getContext().pImpl->CPNConstants[Ty];
Craig Topperc6207612014-04-09 06:08:46 +00001358 if (!Entry)
Justin Lebar611c5c22016-10-10 16:26:13 +00001359 Entry.reset(new ConstantPointerNull(Ty));
Galina Kistanovafc259902012-07-13 01:25:27 +00001360
Justin Lebar611c5c22016-10-10 16:26:13 +00001361 return Entry.get();
Chris Lattner883ad0b2001-10-03 15:39:36 +00001362}
1363
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001364/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001365void ConstantPointerNull::destroyConstantImpl() {
Chris Lattnerc7f9fd42012-01-23 15:20:12 +00001366 getContext().pImpl->CPNConstants.erase(getType());
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001367}
1368
Chris Lattner229907c2011-07-18 04:54:35 +00001369UndefValue *UndefValue::get(Type *Ty) {
Justin Lebar611c5c22016-10-10 16:26:13 +00001370 std::unique_ptr<UndefValue> &Entry = Ty->getContext().pImpl->UVConstants[Ty];
Craig Topperc6207612014-04-09 06:08:46 +00001371 if (!Entry)
Justin Lebar611c5c22016-10-10 16:26:13 +00001372 Entry.reset(new UndefValue(Ty));
Galina Kistanovafc259902012-07-13 01:25:27 +00001373
Justin Lebar611c5c22016-10-10 16:26:13 +00001374 return Entry.get();
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001375}
1376
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001377/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001378void UndefValue::destroyConstantImpl() {
Chris Lattnerc7f9fd42012-01-23 15:20:12 +00001379 // Free the constant and any dangling references to it.
1380 getContext().pImpl->UVConstants.erase(getType());
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001381}
1382
Chris Lattner31b132c2009-10-28 00:01:44 +00001383BlockAddress *BlockAddress::get(BasicBlock *BB) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001384 assert(BB->getParent() && "Block must have a parent");
Chris Lattner31b132c2009-10-28 00:01:44 +00001385 return get(BB->getParent(), BB);
1386}
1387
1388BlockAddress *BlockAddress::get(Function *F, BasicBlock *BB) {
1389 BlockAddress *&BA =
1390 F->getContext().pImpl->BlockAddresses[std::make_pair(F, BB)];
Craig Topperc6207612014-04-09 06:08:46 +00001391 if (!BA)
Chris Lattner31b132c2009-10-28 00:01:44 +00001392 BA = new BlockAddress(F, BB);
Galina Kistanovafc259902012-07-13 01:25:27 +00001393
Chris Lattner31b132c2009-10-28 00:01:44 +00001394 assert(BA->getFunction() == F && "Basic block moved between functions");
1395 return BA;
1396}
1397
1398BlockAddress::BlockAddress(Function *F, BasicBlock *BB)
1399: Constant(Type::getInt8PtrTy(F->getContext()), Value::BlockAddressVal,
1400 &Op<0>(), 2) {
Chris Lattnerc559a9f2009-11-01 03:03:03 +00001401 setOperand(0, F);
1402 setOperand(1, BB);
Chris Lattneraa99c942009-11-01 01:27:45 +00001403 BB->AdjustBlockAddressRefCount(1);
Chris Lattner31b132c2009-10-28 00:01:44 +00001404}
1405
Chandler Carruth6a936922014-01-19 02:13:50 +00001406BlockAddress *BlockAddress::lookup(const BasicBlock *BB) {
1407 if (!BB->hasAddressTaken())
Craig Topperc6207612014-04-09 06:08:46 +00001408 return nullptr;
Chandler Carruth6a936922014-01-19 02:13:50 +00001409
1410 const Function *F = BB->getParent();
Craig Topper2617dcc2014-04-15 06:32:26 +00001411 assert(F && "Block must have a parent");
Chandler Carruth6a936922014-01-19 02:13:50 +00001412 BlockAddress *BA =
1413 F->getContext().pImpl->BlockAddresses.lookup(std::make_pair(F, BB));
1414 assert(BA && "Refcount and block address map disagree!");
1415 return BA;
1416}
Chris Lattner31b132c2009-10-28 00:01:44 +00001417
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001418/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001419void BlockAddress::destroyConstantImpl() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001420 getFunction()->getType()->getContext().pImpl
Chris Lattner31b132c2009-10-28 00:01:44 +00001421 ->BlockAddresses.erase(std::make_pair(getFunction(), getBasicBlock()));
Chris Lattneraa99c942009-11-01 01:27:45 +00001422 getBasicBlock()->AdjustBlockAddressRefCount(-1);
Chris Lattner31b132c2009-10-28 00:01:44 +00001423}
1424
Mehdi Amini8914d292016-02-10 22:47:15 +00001425Value *BlockAddress::handleOperandChangeImpl(Value *From, Value *To) {
Chris Lattner31b132c2009-10-28 00:01:44 +00001426 // This could be replacing either the Basic Block or the Function. In either
1427 // case, we have to remove the map entry.
1428 Function *NewF = getFunction();
1429 BasicBlock *NewBB = getBasicBlock();
Galina Kistanovafc259902012-07-13 01:25:27 +00001430
Mehdi Amini8914d292016-02-10 22:47:15 +00001431 if (From == NewF)
Derek Schuffec9dc012013-06-13 19:51:17 +00001432 NewF = cast<Function>(To->stripPointerCasts());
Mehdi Amini8914d292016-02-10 22:47:15 +00001433 else {
1434 assert(From == NewBB && "From does not match any operand");
Chris Lattner31b132c2009-10-28 00:01:44 +00001435 NewBB = cast<BasicBlock>(To);
Mehdi Amini8914d292016-02-10 22:47:15 +00001436 }
Galina Kistanovafc259902012-07-13 01:25:27 +00001437
Chris Lattner31b132c2009-10-28 00:01:44 +00001438 // See if the 'new' entry already exists, if not, just update this in place
1439 // and return early.
1440 BlockAddress *&NewBA =
1441 getContext().pImpl->BlockAddresses[std::make_pair(NewF, NewBB)];
Pete Cooper5815b1f2015-06-24 18:55:24 +00001442 if (NewBA)
1443 return NewBA;
Chris Lattner31b132c2009-10-28 00:01:44 +00001444
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001445 getBasicBlock()->AdjustBlockAddressRefCount(-1);
Galina Kistanovafc259902012-07-13 01:25:27 +00001446
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001447 // Remove the old entry, this can't cause the map to rehash (just a
1448 // tombstone will get added).
1449 getContext().pImpl->BlockAddresses.erase(std::make_pair(getFunction(),
1450 getBasicBlock()));
1451 NewBA = this;
1452 setOperand(0, NewF);
1453 setOperand(1, NewBB);
1454 getBasicBlock()->AdjustBlockAddressRefCount(1);
Pete Cooper5815b1f2015-06-24 18:55:24 +00001455
1456 // If we just want to keep the existing value, then return null.
1457 // Callers know that this means we shouldn't delete this value.
1458 return nullptr;
Chris Lattner31b132c2009-10-28 00:01:44 +00001459}
1460
1461//---- ConstantExpr::get() implementations.
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001462//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001463
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001464/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands7d6c8ae2008-03-30 19:38:55 +00001465/// cast in the ExprConstants map. It is used by the various get* methods below.
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001466static Constant *getFoldedCast(Instruction::CastOps opc, Constant *C, Type *Ty,
1467 bool OnlyIfReduced = false) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001468 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001469 // Fold a few common cases
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001470 if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001471 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001472
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001473 if (OnlyIfReduced)
1474 return nullptr;
1475
Owen Anderson1584a292009-08-04 20:25:11 +00001476 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
1477
Nadav Rotem88330432013-03-07 01:30:40 +00001478 // Look up the constant in the table first to ensure uniqueness.
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001479 ConstantExprKeyType Key(opc, C);
Galina Kistanovafc259902012-07-13 01:25:27 +00001480
Owen Anderson1584a292009-08-04 20:25:11 +00001481 return pImpl->ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001482}
Galina Kistanovafc259902012-07-13 01:25:27 +00001483
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001484Constant *ConstantExpr::getCast(unsigned oc, Constant *C, Type *Ty,
1485 bool OnlyIfReduced) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001486 Instruction::CastOps opc = Instruction::CastOps(oc);
1487 assert(Instruction::isCast(opc) && "opcode out of range");
1488 assert(C && Ty && "Null arguments to getCast");
Chris Lattner37bc78a2010-01-26 21:51:43 +00001489 assert(CastInst::castIsValid(opc, C, Ty) && "Invalid constantexpr cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001490
1491 switch (opc) {
Chris Lattner37bc78a2010-01-26 21:51:43 +00001492 default:
1493 llvm_unreachable("Invalid cast opcode");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001494 case Instruction::Trunc:
1495 return getTrunc(C, Ty, OnlyIfReduced);
1496 case Instruction::ZExt:
1497 return getZExt(C, Ty, OnlyIfReduced);
1498 case Instruction::SExt:
1499 return getSExt(C, Ty, OnlyIfReduced);
1500 case Instruction::FPTrunc:
1501 return getFPTrunc(C, Ty, OnlyIfReduced);
1502 case Instruction::FPExt:
1503 return getFPExtend(C, Ty, OnlyIfReduced);
1504 case Instruction::UIToFP:
1505 return getUIToFP(C, Ty, OnlyIfReduced);
1506 case Instruction::SIToFP:
1507 return getSIToFP(C, Ty, OnlyIfReduced);
1508 case Instruction::FPToUI:
1509 return getFPToUI(C, Ty, OnlyIfReduced);
1510 case Instruction::FPToSI:
1511 return getFPToSI(C, Ty, OnlyIfReduced);
1512 case Instruction::PtrToInt:
1513 return getPtrToInt(C, Ty, OnlyIfReduced);
1514 case Instruction::IntToPtr:
1515 return getIntToPtr(C, Ty, OnlyIfReduced);
1516 case Instruction::BitCast:
1517 return getBitCast(C, Ty, OnlyIfReduced);
1518 case Instruction::AddrSpaceCast:
1519 return getAddrSpaceCast(C, Ty, OnlyIfReduced);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001520 }
Galina Kistanovafc259902012-07-13 01:25:27 +00001521}
Reid Spencerf37dc652006-12-05 19:14:13 +00001522
Chris Lattner229907c2011-07-18 04:54:35 +00001523Constant *ConstantExpr::getZExtOrBitCast(Constant *C, Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001524 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001525 return getBitCast(C, Ty);
1526 return getZExt(C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001527}
1528
Chris Lattner229907c2011-07-18 04:54:35 +00001529Constant *ConstantExpr::getSExtOrBitCast(Constant *C, Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001530 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001531 return getBitCast(C, Ty);
1532 return getSExt(C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001533}
1534
Chris Lattner229907c2011-07-18 04:54:35 +00001535Constant *ConstantExpr::getTruncOrBitCast(Constant *C, Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001536 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001537 return getBitCast(C, Ty);
1538 return getTrunc(C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001539}
1540
Chris Lattner229907c2011-07-18 04:54:35 +00001541Constant *ConstantExpr::getPointerCast(Constant *S, Type *Ty) {
Evgeniy Stepanov23382642013-01-16 14:41:46 +00001542 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
1543 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
1544 "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001545
Evgeniy Stepanov23382642013-01-16 14:41:46 +00001546 if (Ty->isIntOrIntVectorTy())
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001547 return getPtrToInt(S, Ty);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001548
1549 unsigned SrcAS = S->getType()->getPointerAddressSpace();
1550 if (Ty->isPtrOrPtrVectorTy() && SrcAS != Ty->getPointerAddressSpace())
1551 return getAddrSpaceCast(S, Ty);
1552
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001553 return getBitCast(S, Ty);
Reid Spencerbc245a02006-12-05 03:25:26 +00001554}
1555
Matt Arsenault21f38f42013-12-07 02:58:41 +00001556Constant *ConstantExpr::getPointerBitCastOrAddrSpaceCast(Constant *S,
1557 Type *Ty) {
1558 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
1559 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
1560
1561 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
1562 return getAddrSpaceCast(S, Ty);
1563
1564 return getBitCast(S, Ty);
1565}
1566
Sanjay Patelf3587ec2016-05-18 22:05:28 +00001567Constant *ConstantExpr::getIntegerCast(Constant *C, Type *Ty, bool isSigned) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001568 assert(C->getType()->isIntOrIntVectorTy() &&
1569 Ty->isIntOrIntVectorTy() && "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001570 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1571 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer56521c42006-12-12 00:51:07 +00001572 Instruction::CastOps opcode =
1573 (SrcBits == DstBits ? Instruction::BitCast :
1574 (SrcBits > DstBits ? Instruction::Trunc :
1575 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1576 return getCast(opcode, C, Ty);
1577}
1578
Chris Lattner229907c2011-07-18 04:54:35 +00001579Constant *ConstantExpr::getFPCast(Constant *C, Type *Ty) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001580 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer56521c42006-12-12 00:51:07 +00001581 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001582 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1583 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001584 if (SrcBits == DstBits)
1585 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001586 Instruction::CastOps opcode =
Jay Foad9f32cfd2011-01-27 14:44:55 +00001587 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001588 return getCast(opcode, C, Ty);
1589}
1590
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001591Constant *ConstantExpr::getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001592#ifndef NDEBUG
1593 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1594 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1595#endif
1596 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001597 assert(C->getType()->isIntOrIntVectorTy() && "Trunc operand must be integer");
1598 assert(Ty->isIntOrIntVectorTy() && "Trunc produces only integral");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001599 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001600 "SrcTy must be larger than DestTy for Trunc!");
1601
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001602 return getFoldedCast(Instruction::Trunc, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001603}
1604
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001605Constant *ConstantExpr::getSExt(Constant *C, Type *Ty, bool OnlyIfReduced) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001606#ifndef NDEBUG
1607 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1608 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1609#endif
1610 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001611 assert(C->getType()->isIntOrIntVectorTy() && "SExt operand must be integral");
1612 assert(Ty->isIntOrIntVectorTy() && "SExt produces only integer");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001613 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001614 "SrcTy must be smaller than DestTy for SExt!");
1615
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001616 return getFoldedCast(Instruction::SExt, C, Ty, OnlyIfReduced);
Chris Lattnerdd284742004-04-04 23:20:30 +00001617}
1618
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001619Constant *ConstantExpr::getZExt(Constant *C, Type *Ty, bool OnlyIfReduced) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001620#ifndef NDEBUG
1621 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1622 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1623#endif
1624 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001625 assert(C->getType()->isIntOrIntVectorTy() && "ZEXt operand must be integral");
1626 assert(Ty->isIntOrIntVectorTy() && "ZExt produces only integer");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001627 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001628 "SrcTy must be smaller than DestTy for ZExt!");
1629
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001630 return getFoldedCast(Instruction::ZExt, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001631}
1632
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001633Constant *ConstantExpr::getFPTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001634#ifndef NDEBUG
1635 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1636 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1637#endif
1638 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001639 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001640 C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001641 "This is an illegal floating point truncation!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001642 return getFoldedCast(Instruction::FPTrunc, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001643}
1644
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001645Constant *ConstantExpr::getFPExtend(Constant *C, Type *Ty, bool OnlyIfReduced) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001646#ifndef NDEBUG
1647 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1648 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1649#endif
1650 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001651 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001652 C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001653 "This is an illegal floating point extension!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001654 return getFoldedCast(Instruction::FPExt, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001655}
1656
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001657Constant *ConstantExpr::getUIToFP(Constant *C, Type *Ty, bool OnlyIfReduced) {
Devang Pateld26344d2008-11-03 23:20:04 +00001658#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001659 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1660 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001661#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001662 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001663 assert(C->getType()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00001664 "This is an illegal uint to floating point cast!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001665 return getFoldedCast(Instruction::UIToFP, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001666}
1667
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001668Constant *ConstantExpr::getSIToFP(Constant *C, Type *Ty, bool OnlyIfReduced) {
Devang Pateld26344d2008-11-03 23:20:04 +00001669#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001670 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1671 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001672#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001673 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001674 assert(C->getType()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001675 "This is an illegal sint to floating point cast!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001676 return getFoldedCast(Instruction::SIToFP, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001677}
1678
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001679Constant *ConstantExpr::getFPToUI(Constant *C, Type *Ty, bool OnlyIfReduced) {
Devang Pateld26344d2008-11-03 23:20:04 +00001680#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001681 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1682 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001683#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001684 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001685 assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00001686 "This is an illegal floating point to uint cast!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001687 return getFoldedCast(Instruction::FPToUI, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001688}
1689
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001690Constant *ConstantExpr::getFPToSI(Constant *C, Type *Ty, bool OnlyIfReduced) {
Devang Pateld26344d2008-11-03 23:20:04 +00001691#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001692 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1693 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001694#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001695 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001696 assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00001697 "This is an illegal floating point to sint cast!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001698 return getFoldedCast(Instruction::FPToSI, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001699}
1700
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001701Constant *ConstantExpr::getPtrToInt(Constant *C, Type *DstTy,
1702 bool OnlyIfReduced) {
Craig Topper95d23472017-07-09 07:04:00 +00001703 assert(C->getType()->isPtrOrPtrVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00001704 "PtrToInt source must be pointer or pointer vector");
Craig Topper95d23472017-07-09 07:04:00 +00001705 assert(DstTy->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00001706 "PtrToInt destination must be integer or integer vector");
Chris Lattner8a3df542012-01-25 01:32:59 +00001707 assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy));
Nick Lewyckyff509622012-01-25 03:20:12 +00001708 if (isa<VectorType>(C->getType()))
Chris Lattner8326bd82012-01-26 00:42:34 +00001709 assert(C->getType()->getVectorNumElements()==DstTy->getVectorNumElements()&&
Chris Lattner8a3df542012-01-25 01:32:59 +00001710 "Invalid cast between a different number of vector elements");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001711 return getFoldedCast(Instruction::PtrToInt, C, DstTy, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001712}
1713
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001714Constant *ConstantExpr::getIntToPtr(Constant *C, Type *DstTy,
1715 bool OnlyIfReduced) {
Craig Topper95d23472017-07-09 07:04:00 +00001716 assert(C->getType()->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00001717 "IntToPtr source must be integer or integer vector");
Craig Topper95d23472017-07-09 07:04:00 +00001718 assert(DstTy->isPtrOrPtrVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00001719 "IntToPtr destination must be a pointer or pointer vector");
Chris Lattner8a3df542012-01-25 01:32:59 +00001720 assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy));
Nick Lewyckyff509622012-01-25 03:20:12 +00001721 if (isa<VectorType>(C->getType()))
Chris Lattner8326bd82012-01-26 00:42:34 +00001722 assert(C->getType()->getVectorNumElements()==DstTy->getVectorNumElements()&&
Chris Lattner8a3df542012-01-25 01:32:59 +00001723 "Invalid cast between a different number of vector elements");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001724 return getFoldedCast(Instruction::IntToPtr, C, DstTy, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001725}
1726
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001727Constant *ConstantExpr::getBitCast(Constant *C, Type *DstTy,
1728 bool OnlyIfReduced) {
Chris Lattner37bc78a2010-01-26 21:51:43 +00001729 assert(CastInst::castIsValid(Instruction::BitCast, C, DstTy) &&
1730 "Invalid constantexpr bitcast!");
Galina Kistanovafc259902012-07-13 01:25:27 +00001731
Chris Lattnercbeda872009-03-21 06:55:54 +00001732 // It is common to ask for a bitcast of a value to its own type, handle this
1733 // speedily.
1734 if (C->getType() == DstTy) return C;
Galina Kistanovafc259902012-07-13 01:25:27 +00001735
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001736 return getFoldedCast(Instruction::BitCast, C, DstTy, OnlyIfReduced);
Chris Lattnerdd284742004-04-04 23:20:30 +00001737}
1738
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001739Constant *ConstantExpr::getAddrSpaceCast(Constant *C, Type *DstTy,
1740 bool OnlyIfReduced) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001741 assert(CastInst::castIsValid(Instruction::AddrSpaceCast, C, DstTy) &&
1742 "Invalid constantexpr addrspacecast!");
1743
Jingyue Wubaabe502014-06-15 21:40:57 +00001744 // Canonicalize addrspacecasts between different pointer types by first
1745 // bitcasting the pointer type and then converting the address space.
1746 PointerType *SrcScalarTy = cast<PointerType>(C->getType()->getScalarType());
1747 PointerType *DstScalarTy = cast<PointerType>(DstTy->getScalarType());
1748 Type *DstElemTy = DstScalarTy->getElementType();
1749 if (SrcScalarTy->getElementType() != DstElemTy) {
1750 Type *MidTy = PointerType::get(DstElemTy, SrcScalarTy->getAddressSpace());
1751 if (VectorType *VT = dyn_cast<VectorType>(DstTy)) {
1752 // Handle vectors of pointers.
1753 MidTy = VectorType::get(MidTy, VT->getNumElements());
1754 }
1755 C = getBitCast(C, MidTy);
1756 }
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001757 return getFoldedCast(Instruction::AddrSpaceCast, C, DstTy, OnlyIfReduced);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001758}
1759
Chris Lattner887ecac2011-07-09 18:23:52 +00001760Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2,
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001761 unsigned Flags, Type *OnlyIfReducedTy) {
Chris Lattner887ecac2011-07-09 18:23:52 +00001762 // Check the operands for consistency first.
Reid Spencer7eb55b32006-11-02 01:53:59 +00001763 assert(Opcode >= Instruction::BinaryOpsBegin &&
1764 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001765 "Invalid opcode in binary constant expression");
1766 assert(C1->getType() == C2->getType() &&
1767 "Operand types in binary constant expression should match");
Galina Kistanovafc259902012-07-13 01:25:27 +00001768
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001769#ifndef NDEBUG
1770 switch (Opcode) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001771 case Instruction::Add:
Reid Spencer7eb55b32006-11-02 01:53:59 +00001772 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001773 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001774 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001775 assert(C1->getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001776 "Tried to create an integer operation on a non-integer type!");
1777 break;
1778 case Instruction::FAdd:
1779 case Instruction::FSub:
1780 case Instruction::FMul:
1781 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001782 assert(C1->getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001783 "Tried to create a floating-point operation on a "
1784 "non-floating-point type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001785 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001786 case Instruction::UDiv:
1787 case Instruction::SDiv:
1788 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001789 assert(C1->getType()->isIntOrIntVectorTy() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001790 "Tried to create an arithmetic operation on a non-arithmetic type!");
1791 break;
1792 case Instruction::FDiv:
1793 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001794 assert(C1->getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001795 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001796 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001797 case Instruction::URem:
1798 case Instruction::SRem:
1799 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001800 assert(C1->getType()->isIntOrIntVectorTy() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001801 "Tried to create an arithmetic operation on a non-arithmetic type!");
1802 break;
1803 case Instruction::FRem:
1804 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001805 assert(C1->getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001806 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001807 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001808 case Instruction::And:
1809 case Instruction::Or:
1810 case Instruction::Xor:
1811 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001812 assert(C1->getType()->isIntOrIntVectorTy() &&
Misha Brukman3852f652005-01-27 06:46:38 +00001813 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001814 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001815 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001816 case Instruction::LShr:
1817 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00001818 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001819 assert(C1->getType()->isIntOrIntVectorTy() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001820 "Tried to create a shift operation on a non-integer type!");
1821 break;
1822 default:
1823 break;
1824 }
1825#endif
1826
Chris Lattner887ecac2011-07-09 18:23:52 +00001827 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1828 return FC; // Fold a few common cases.
Galina Kistanovafc259902012-07-13 01:25:27 +00001829
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001830 if (OnlyIfReducedTy == C1->getType())
1831 return nullptr;
1832
Benjamin Kramer324322b2013-03-07 20:53:34 +00001833 Constant *ArgVec[] = { C1, C2 };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001834 ConstantExprKeyType Key(Opcode, ArgVec, 0, Flags);
Galina Kistanovafc259902012-07-13 01:25:27 +00001835
Chris Lattner887ecac2011-07-09 18:23:52 +00001836 LLVMContextImpl *pImpl = C1->getContext().pImpl;
1837 return pImpl->ExprConstants.getOrCreate(C1->getType(), Key);
Reid Spencera009d0d2006-12-04 21:35:24 +00001838}
1839
Chris Lattner229907c2011-07-18 04:54:35 +00001840Constant *ConstantExpr::getSizeOf(Type* Ty) {
Owen Anderson487375e2009-07-29 18:55:55 +00001841 // sizeof is implemented as: (i64) gep (Ty*)null, 1
1842 // Note that a non-inbounds gep is used, as null isn't within any object.
Owen Anderson55f1c092009-08-13 21:58:54 +00001843 Constant *GEPIdx = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
Owen Anderson487375e2009-07-29 18:55:55 +00001844 Constant *GEP = getGetElementPtr(
David Blaikie4a2e73b2015-04-02 18:55:32 +00001845 Ty, Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx);
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001846 return getPtrToInt(GEP,
1847 Type::getInt64Ty(Ty->getContext()));
Owen Anderson487375e2009-07-29 18:55:55 +00001848}
1849
Chris Lattner229907c2011-07-18 04:54:35 +00001850Constant *ConstantExpr::getAlignOf(Type* Ty) {
Dan Gohmancf913832010-01-28 02:15:55 +00001851 // alignof is implemented as: (i64) gep ({i1,Ty}*)null, 0, 1
Dan Gohman50c09d02009-08-11 17:57:01 +00001852 // Note that a non-inbounds gep is used, as null isn't within any object.
Serge Gueltone38003f2017-05-09 19:31:13 +00001853 Type *AligningTy = StructType::get(Type::getInt1Ty(Ty->getContext()), Ty);
Matt Arsenaultbe558882014-04-23 20:58:57 +00001854 Constant *NullPtr = Constant::getNullValue(AligningTy->getPointerTo(0));
Dan Gohmana9be7392010-01-28 02:43:22 +00001855 Constant *Zero = ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0);
Owen Anderson55f1c092009-08-13 21:58:54 +00001856 Constant *One = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
Owen Anderson487375e2009-07-29 18:55:55 +00001857 Constant *Indices[2] = { Zero, One };
David Blaikie4a2e73b2015-04-02 18:55:32 +00001858 Constant *GEP = getGetElementPtr(AligningTy, NullPtr, Indices);
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001859 return getPtrToInt(GEP,
1860 Type::getInt64Ty(Ty->getContext()));
Owen Anderson487375e2009-07-29 18:55:55 +00001861}
1862
Chris Lattner229907c2011-07-18 04:54:35 +00001863Constant *ConstantExpr::getOffsetOf(StructType* STy, unsigned FieldNo) {
Dan Gohmanede94e62010-02-01 16:37:38 +00001864 return getOffsetOf(STy, ConstantInt::get(Type::getInt32Ty(STy->getContext()),
1865 FieldNo));
1866}
1867
Chris Lattner229907c2011-07-18 04:54:35 +00001868Constant *ConstantExpr::getOffsetOf(Type* Ty, Constant *FieldNo) {
Dan Gohmanff3af7252009-08-16 21:26:11 +00001869 // offsetof is implemented as: (i64) gep (Ty*)null, 0, FieldNo
1870 // Note that a non-inbounds gep is used, as null isn't within any object.
1871 Constant *GEPIdx[] = {
Dan Gohmanede94e62010-02-01 16:37:38 +00001872 ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0),
1873 FieldNo
Dan Gohmanff3af7252009-08-16 21:26:11 +00001874 };
1875 Constant *GEP = getGetElementPtr(
David Blaikie4a2e73b2015-04-02 18:55:32 +00001876 Ty, Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx);
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001877 return getPtrToInt(GEP,
1878 Type::getInt64Ty(Ty->getContext()));
Dan Gohmanff3af7252009-08-16 21:26:11 +00001879}
Owen Anderson487375e2009-07-29 18:55:55 +00001880
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001881Constant *ConstantExpr::getCompare(unsigned short Predicate, Constant *C1,
1882 Constant *C2, bool OnlyIfReduced) {
Reid Spencera009d0d2006-12-04 21:35:24 +00001883 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Galina Kistanovafc259902012-07-13 01:25:27 +00001884
Chris Lattner887ecac2011-07-09 18:23:52 +00001885 switch (Predicate) {
1886 default: llvm_unreachable("Invalid CmpInst predicate");
1887 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
1888 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
1889 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
1890 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
1891 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
1892 case CmpInst::FCMP_TRUE:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001893 return getFCmp(Predicate, C1, C2, OnlyIfReduced);
Galina Kistanovafc259902012-07-13 01:25:27 +00001894
Chris Lattner887ecac2011-07-09 18:23:52 +00001895 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
1896 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
1897 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
1898 case CmpInst::ICMP_SLE:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001899 return getICmp(Predicate, C1, C2, OnlyIfReduced);
Chris Lattner887ecac2011-07-09 18:23:52 +00001900 }
Chris Lattner29ca2c62004-08-04 18:50:09 +00001901}
1902
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001903Constant *ConstantExpr::getSelect(Constant *C, Constant *V1, Constant *V2,
1904 Type *OnlyIfReducedTy) {
Chris Lattner41632132008-12-29 00:16:12 +00001905 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner6e415c02004-03-12 05:54:04 +00001906
Chris Lattner887ecac2011-07-09 18:23:52 +00001907 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1908 return SC; // Fold common cases
Chris Lattner6e415c02004-03-12 05:54:04 +00001909
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001910 if (OnlyIfReducedTy == V1->getType())
1911 return nullptr;
1912
Benjamin Kramer324322b2013-03-07 20:53:34 +00001913 Constant *ArgVec[] = { C, V1, V2 };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001914 ConstantExprKeyType Key(Instruction::Select, ArgVec);
Galina Kistanovafc259902012-07-13 01:25:27 +00001915
Chris Lattner887ecac2011-07-09 18:23:52 +00001916 LLVMContextImpl *pImpl = C->getContext().pImpl;
1917 return pImpl->ExprConstants.getOrCreate(V1->getType(), Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00001918}
1919
David Blaikie4a2e73b2015-04-02 18:55:32 +00001920Constant *ConstantExpr::getGetElementPtr(Type *Ty, Constant *C,
1921 ArrayRef<Value *> Idxs, bool InBounds,
Peter Collingbourned93620b2016-11-10 22:34:55 +00001922 Optional<unsigned> InRangeIndex,
David Blaikie4a2e73b2015-04-02 18:55:32 +00001923 Type *OnlyIfReducedTy) {
David Blaikie4a2e73b2015-04-02 18:55:32 +00001924 if (!Ty)
1925 Ty = cast<PointerType>(C->getType()->getScalarType())->getElementType();
1926 else
David Blaikied9d900c2015-05-07 17:28:58 +00001927 assert(
1928 Ty ==
1929 cast<PointerType>(C->getType()->getScalarType())->getContainedType(0u));
1930
Peter Collingbourned93620b2016-11-10 22:34:55 +00001931 if (Constant *FC =
1932 ConstantFoldGetElementPtr(Ty, C, InBounds, InRangeIndex, Idxs))
David Blaikied9d900c2015-05-07 17:28:58 +00001933 return FC; // Fold a few common cases.
1934
Chris Lattner887ecac2011-07-09 18:23:52 +00001935 // Get the result type of the getelementptr!
David Blaikie4a2e73b2015-04-02 18:55:32 +00001936 Type *DestTy = GetElementPtrInst::getIndexedType(Ty, Idxs);
1937 assert(DestTy && "GEP indices invalid!");
Chris Lattner8326bd82012-01-26 00:42:34 +00001938 unsigned AS = C->getType()->getPointerAddressSpace();
David Blaikie4a2e73b2015-04-02 18:55:32 +00001939 Type *ReqTy = DestTy->getPointerTo(AS);
Elena Demikhovskyee004bc2016-05-15 12:30:25 +00001940
1941 unsigned NumVecElts = 0;
1942 if (C->getType()->isVectorTy())
1943 NumVecElts = C->getType()->getVectorNumElements();
1944 else for (auto Idx : Idxs)
1945 if (Idx->getType()->isVectorTy())
1946 NumVecElts = Idx->getType()->getVectorNumElements();
1947
1948 if (NumVecElts)
1949 ReqTy = VectorType::get(ReqTy, NumVecElts);
Galina Kistanovafc259902012-07-13 01:25:27 +00001950
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001951 if (OnlyIfReducedTy == ReqTy)
1952 return nullptr;
1953
Dan Gohman1b849082009-09-07 23:54:19 +00001954 // Look up the constant in the table first to ensure uniqueness
1955 std::vector<Constant*> ArgVec;
Jay Foaded8db7d2011-07-21 14:31:17 +00001956 ArgVec.reserve(1 + Idxs.size());
Dan Gohman1b849082009-09-07 23:54:19 +00001957 ArgVec.push_back(C);
Duncan Sandse6beec62012-11-13 12:59:33 +00001958 for (unsigned i = 0, e = Idxs.size(); i != e; ++i) {
Duncan Sandse6beec62012-11-13 12:59:33 +00001959 assert((!Idxs[i]->getType()->isVectorTy() ||
Elena Demikhovskyee004bc2016-05-15 12:30:25 +00001960 Idxs[i]->getType()->getVectorNumElements() == NumVecElts) &&
Duncan Sandse6beec62012-11-13 12:59:33 +00001961 "getelementptr index type missmatch");
Elena Demikhovskyee004bc2016-05-15 12:30:25 +00001962
1963 Constant *Idx = cast<Constant>(Idxs[i]);
1964 if (NumVecElts && !Idxs[i]->getType()->isVectorTy())
1965 Idx = ConstantVector::getSplat(NumVecElts, Idx);
1966 ArgVec.push_back(Idx);
Duncan Sandse6beec62012-11-13 12:59:33 +00001967 }
Peter Collingbourned93620b2016-11-10 22:34:55 +00001968
1969 unsigned SubClassOptionalData = InBounds ? GEPOperator::IsInBounds : 0;
1970 if (InRangeIndex && *InRangeIndex < 63)
1971 SubClassOptionalData |= (*InRangeIndex + 1) << 1;
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001972 const ConstantExprKeyType Key(Instruction::GetElementPtr, ArgVec, 0,
Peter Collingbourned93620b2016-11-10 22:34:55 +00001973 SubClassOptionalData, None, Ty);
Galina Kistanovafc259902012-07-13 01:25:27 +00001974
Chris Lattner887ecac2011-07-09 18:23:52 +00001975 LLVMContextImpl *pImpl = C->getContext().pImpl;
Dan Gohman1b849082009-09-07 23:54:19 +00001976 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
1977}
1978
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001979Constant *ConstantExpr::getICmp(unsigned short pred, Constant *LHS,
1980 Constant *RHS, bool OnlyIfReduced) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001981 assert(LHS->getType() == RHS->getType());
Craig Topper45844762017-07-05 23:35:46 +00001982 assert(CmpInst::isIntPredicate((CmpInst::Predicate)pred) &&
1983 "Invalid ICmp Predicate");
Reid Spenceree3c9912006-12-04 05:19:50 +00001984
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001985 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001986 return FC; // Fold a few common cases...
1987
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001988 if (OnlyIfReduced)
1989 return nullptr;
1990
Reid Spenceree3c9912006-12-04 05:19:50 +00001991 // Look up the constant in the table first to ensure uniqueness
Benjamin Kramer324322b2013-03-07 20:53:34 +00001992 Constant *ArgVec[] = { LHS, RHS };
Reid Spencerb1537492006-12-24 18:42:29 +00001993 // Get the key type with both the opcode and predicate
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001994 const ConstantExprKeyType Key(Instruction::ICmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00001995
Chris Lattner229907c2011-07-18 04:54:35 +00001996 Type *ResultTy = Type::getInt1Ty(LHS->getContext());
1997 if (VectorType *VT = dyn_cast<VectorType>(LHS->getType()))
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00001998 ResultTy = VectorType::get(ResultTy, VT->getNumElements());
1999
Owen Anderson1584a292009-08-04 20:25:11 +00002000 LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl;
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00002001 return pImpl->ExprConstants.getOrCreate(ResultTy, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002002}
2003
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002004Constant *ConstantExpr::getFCmp(unsigned short pred, Constant *LHS,
2005 Constant *RHS, bool OnlyIfReduced) {
Reid Spenceree3c9912006-12-04 05:19:50 +00002006 assert(LHS->getType() == RHS->getType());
Craig Topper45844762017-07-05 23:35:46 +00002007 assert(CmpInst::isFPPredicate((CmpInst::Predicate)pred) &&
2008 "Invalid FCmp Predicate");
Reid Spenceree3c9912006-12-04 05:19:50 +00002009
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00002010 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00002011 return FC; // Fold a few common cases...
2012
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002013 if (OnlyIfReduced)
2014 return nullptr;
2015
Reid Spenceree3c9912006-12-04 05:19:50 +00002016 // Look up the constant in the table first to ensure uniqueness
Benjamin Kramer324322b2013-03-07 20:53:34 +00002017 Constant *ArgVec[] = { LHS, RHS };
Reid Spencerb1537492006-12-24 18:42:29 +00002018 // Get the key type with both the opcode and predicate
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002019 const ConstantExprKeyType Key(Instruction::FCmp, ArgVec, pred);
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00002020
Chris Lattner229907c2011-07-18 04:54:35 +00002021 Type *ResultTy = Type::getInt1Ty(LHS->getContext());
2022 if (VectorType *VT = dyn_cast<VectorType>(LHS->getType()))
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00002023 ResultTy = VectorType::get(ResultTy, VT->getNumElements());
2024
Owen Anderson1584a292009-08-04 20:25:11 +00002025 LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl;
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00002026 return pImpl->ExprConstants.getOrCreate(ResultTy, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00002027}
2028
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002029Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx,
2030 Type *OnlyIfReducedTy) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002031 assert(Val->getType()->isVectorTy() &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002032 "Tried to create extractelement operation on non-vector type!");
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00002033 assert(Idx->getType()->isIntegerTy() &&
2034 "Extractelement index must be an integer type!");
Galina Kistanovafc259902012-07-13 01:25:27 +00002035
Chris Lattner887ecac2011-07-09 18:23:52 +00002036 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
Chris Lattner09660c92009-12-30 20:25:09 +00002037 return FC; // Fold a few common cases.
Galina Kistanovafc259902012-07-13 01:25:27 +00002038
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002039 Type *ReqTy = Val->getType()->getVectorElementType();
2040 if (OnlyIfReducedTy == ReqTy)
2041 return nullptr;
2042
Robert Bocchinoca27f032006-01-17 20:07:22 +00002043 // Look up the constant in the table first to ensure uniqueness
Benjamin Kramer324322b2013-03-07 20:53:34 +00002044 Constant *ArgVec[] = { Val, Idx };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002045 const ConstantExprKeyType Key(Instruction::ExtractElement, ArgVec);
Galina Kistanovafc259902012-07-13 01:25:27 +00002046
Chris Lattner887ecac2011-07-09 18:23:52 +00002047 LLVMContextImpl *pImpl = Val->getContext().pImpl;
Owen Anderson1584a292009-08-04 20:25:11 +00002048 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00002049}
2050
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002051Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
2052 Constant *Idx, Type *OnlyIfReducedTy) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002053 assert(Val->getType()->isVectorTy() &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002054 "Tried to create insertelement operation on non-vector type!");
Chris Lattner8326bd82012-01-26 00:42:34 +00002055 assert(Elt->getType() == Val->getType()->getVectorElementType() &&
2056 "Insertelement types must match!");
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00002057 assert(Idx->getType()->isIntegerTy() &&
Reid Spencer2546b762007-01-26 07:37:34 +00002058 "Insertelement index must be i32 type!");
Robert Bocchinoca27f032006-01-17 20:07:22 +00002059
Chris Lattner887ecac2011-07-09 18:23:52 +00002060 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
2061 return FC; // Fold a few common cases.
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002062
2063 if (OnlyIfReducedTy == Val->getType())
2064 return nullptr;
2065
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002066 // Look up the constant in the table first to ensure uniqueness
Benjamin Kramer324322b2013-03-07 20:53:34 +00002067 Constant *ArgVec[] = { Val, Elt, Idx };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002068 const ConstantExprKeyType Key(Instruction::InsertElement, ArgVec);
Galina Kistanovafc259902012-07-13 01:25:27 +00002069
Chris Lattner887ecac2011-07-09 18:23:52 +00002070 LLVMContextImpl *pImpl = Val->getContext().pImpl;
2071 return pImpl->ExprConstants.getOrCreate(Val->getType(), Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002072}
2073
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002074Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
2075 Constant *Mask, Type *OnlyIfReducedTy) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002076 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
2077 "Invalid shuffle vector constant expr operands!");
Nate Begeman94aa38d2009-02-12 21:28:33 +00002078
Chris Lattner887ecac2011-07-09 18:23:52 +00002079 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
2080 return FC; // Fold a few common cases.
2081
Chris Lattner8326bd82012-01-26 00:42:34 +00002082 unsigned NElts = Mask->getType()->getVectorNumElements();
2083 Type *EltTy = V1->getType()->getVectorElementType();
Chris Lattner229907c2011-07-18 04:54:35 +00002084 Type *ShufTy = VectorType::get(EltTy, NElts);
Chris Lattner887ecac2011-07-09 18:23:52 +00002085
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002086 if (OnlyIfReducedTy == ShufTy)
2087 return nullptr;
2088
Chris Lattner887ecac2011-07-09 18:23:52 +00002089 // Look up the constant in the table first to ensure uniqueness
Benjamin Kramer324322b2013-03-07 20:53:34 +00002090 Constant *ArgVec[] = { V1, V2, Mask };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002091 const ConstantExprKeyType Key(Instruction::ShuffleVector, ArgVec);
Galina Kistanovafc259902012-07-13 01:25:27 +00002092
Chris Lattner887ecac2011-07-09 18:23:52 +00002093 LLVMContextImpl *pImpl = ShufTy->getContext().pImpl;
2094 return pImpl->ExprConstants.getOrCreate(ShufTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002095}
2096
Chris Lattner887ecac2011-07-09 18:23:52 +00002097Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002098 ArrayRef<unsigned> Idxs,
2099 Type *OnlyIfReducedTy) {
Hal Finkelb31366d2013-07-10 22:51:01 +00002100 assert(Agg->getType()->isFirstClassType() &&
2101 "Non-first-class type for constant insertvalue expression");
2102
Jay Foad57aa6362011-07-13 10:26:04 +00002103 assert(ExtractValueInst::getIndexedType(Agg->getType(),
2104 Idxs) == Val->getType() &&
Dan Gohman12fce772008-05-15 19:50:34 +00002105 "insertvalue indices invalid!");
Hal Finkelb31366d2013-07-10 22:51:01 +00002106 Type *ReqTy = Val->getType();
2107
2108 if (Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs))
2109 return FC;
2110
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002111 if (OnlyIfReducedTy == ReqTy)
2112 return nullptr;
2113
Hal Finkelb31366d2013-07-10 22:51:01 +00002114 Constant *ArgVec[] = { Agg, Val };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002115 const ConstantExprKeyType Key(Instruction::InsertValue, ArgVec, 0, 0, Idxs);
Hal Finkelb31366d2013-07-10 22:51:01 +00002116
2117 LLVMContextImpl *pImpl = Agg->getContext().pImpl;
2118 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Dan Gohman12fce772008-05-15 19:50:34 +00002119}
2120
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002121Constant *ConstantExpr::getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs,
2122 Type *OnlyIfReducedTy) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002123 assert(Agg->getType()->isFirstClassType() &&
Chris Lattner887ecac2011-07-09 18:23:52 +00002124 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002125
Chris Lattner229907c2011-07-18 04:54:35 +00002126 Type *ReqTy = ExtractValueInst::getIndexedType(Agg->getType(), Idxs);
Chandler Carruth9db56b82011-07-10 09:45:35 +00002127 (void)ReqTy;
Chris Lattner887ecac2011-07-09 18:23:52 +00002128 assert(ReqTy && "extractvalue indices invalid!");
Galina Kistanovafc259902012-07-13 01:25:27 +00002129
Dan Gohman0752bff2008-05-23 00:36:11 +00002130 assert(Agg->getType()->isFirstClassType() &&
2131 "Non-first-class type for constant extractvalue expression");
Hal Finkelb31366d2013-07-10 22:51:01 +00002132 if (Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs))
2133 return FC;
2134
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002135 if (OnlyIfReducedTy == ReqTy)
2136 return nullptr;
2137
Hal Finkelb31366d2013-07-10 22:51:01 +00002138 Constant *ArgVec[] = { Agg };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002139 const ConstantExprKeyType Key(Instruction::ExtractValue, ArgVec, 0, 0, Idxs);
Hal Finkelb31366d2013-07-10 22:51:01 +00002140
2141 LLVMContextImpl *pImpl = Agg->getContext().pImpl;
2142 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Dan Gohman12fce772008-05-15 19:50:34 +00002143}
2144
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002145Constant *ConstantExpr::getNeg(Constant *C, bool HasNUW, bool HasNSW) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002146 assert(C->getType()->isIntOrIntVectorTy() &&
Owen Anderson487375e2009-07-29 18:55:55 +00002147 "Cannot NEG a nonintegral value!");
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002148 return getSub(ConstantFP::getZeroValueForNegation(C->getType()),
2149 C, HasNUW, HasNSW);
Owen Anderson487375e2009-07-29 18:55:55 +00002150}
2151
Chris Lattnera676c0f2011-02-07 16:40:21 +00002152Constant *ConstantExpr::getFNeg(Constant *C) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002153 assert(C->getType()->isFPOrFPVectorTy() &&
Owen Anderson487375e2009-07-29 18:55:55 +00002154 "Cannot FNEG a non-floating-point value!");
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002155 return getFSub(ConstantFP::getZeroValueForNegation(C->getType()), C);
Owen Anderson487375e2009-07-29 18:55:55 +00002156}
2157
Chris Lattnera676c0f2011-02-07 16:40:21 +00002158Constant *ConstantExpr::getNot(Constant *C) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002159 assert(C->getType()->isIntOrIntVectorTy() &&
Owen Anderson487375e2009-07-29 18:55:55 +00002160 "Cannot NOT a nonintegral value!");
Owen Anderson5a1acd92009-07-31 20:28:14 +00002161 return get(Instruction::Xor, C, Constant::getAllOnesValue(C->getType()));
Owen Anderson487375e2009-07-29 18:55:55 +00002162}
2163
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002164Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2,
2165 bool HasNUW, bool HasNSW) {
2166 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2167 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
2168 return get(Instruction::Add, C1, C2, Flags);
Owen Anderson487375e2009-07-29 18:55:55 +00002169}
2170
Chris Lattnera676c0f2011-02-07 16:40:21 +00002171Constant *ConstantExpr::getFAdd(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002172 return get(Instruction::FAdd, C1, C2);
2173}
2174
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002175Constant *ConstantExpr::getSub(Constant *C1, Constant *C2,
2176 bool HasNUW, bool HasNSW) {
2177 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2178 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
2179 return get(Instruction::Sub, C1, C2, Flags);
Owen Anderson487375e2009-07-29 18:55:55 +00002180}
2181
Chris Lattnera676c0f2011-02-07 16:40:21 +00002182Constant *ConstantExpr::getFSub(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002183 return get(Instruction::FSub, C1, C2);
2184}
2185
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002186Constant *ConstantExpr::getMul(Constant *C1, Constant *C2,
2187 bool HasNUW, bool HasNSW) {
2188 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2189 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
2190 return get(Instruction::Mul, C1, C2, Flags);
Owen Anderson487375e2009-07-29 18:55:55 +00002191}
2192
Chris Lattnera676c0f2011-02-07 16:40:21 +00002193Constant *ConstantExpr::getFMul(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002194 return get(Instruction::FMul, C1, C2);
2195}
2196
Chris Lattner0d75eac2011-02-09 16:43:07 +00002197Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2, bool isExact) {
2198 return get(Instruction::UDiv, C1, C2,
2199 isExact ? PossiblyExactOperator::IsExact : 0);
Owen Anderson487375e2009-07-29 18:55:55 +00002200}
2201
Chris Lattner0d75eac2011-02-09 16:43:07 +00002202Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2, bool isExact) {
2203 return get(Instruction::SDiv, C1, C2,
2204 isExact ? PossiblyExactOperator::IsExact : 0);
Owen Anderson487375e2009-07-29 18:55:55 +00002205}
2206
Chris Lattnera676c0f2011-02-07 16:40:21 +00002207Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002208 return get(Instruction::FDiv, C1, C2);
2209}
2210
Chris Lattnera676c0f2011-02-07 16:40:21 +00002211Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002212 return get(Instruction::URem, C1, C2);
2213}
2214
Chris Lattnera676c0f2011-02-07 16:40:21 +00002215Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002216 return get(Instruction::SRem, C1, C2);
2217}
2218
Chris Lattnera676c0f2011-02-07 16:40:21 +00002219Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002220 return get(Instruction::FRem, C1, C2);
2221}
2222
Chris Lattnera676c0f2011-02-07 16:40:21 +00002223Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002224 return get(Instruction::And, C1, C2);
2225}
2226
Chris Lattnera676c0f2011-02-07 16:40:21 +00002227Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002228 return get(Instruction::Or, C1, C2);
2229}
2230
Chris Lattnera676c0f2011-02-07 16:40:21 +00002231Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002232 return get(Instruction::Xor, C1, C2);
2233}
2234
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002235Constant *ConstantExpr::getShl(Constant *C1, Constant *C2,
2236 bool HasNUW, bool HasNSW) {
2237 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2238 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
2239 return get(Instruction::Shl, C1, C2, Flags);
Owen Anderson487375e2009-07-29 18:55:55 +00002240}
2241
Chris Lattner0d75eac2011-02-09 16:43:07 +00002242Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2, bool isExact) {
2243 return get(Instruction::LShr, C1, C2,
2244 isExact ? PossiblyExactOperator::IsExact : 0);
Owen Anderson487375e2009-07-29 18:55:55 +00002245}
2246
Chris Lattner0d75eac2011-02-09 16:43:07 +00002247Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2, bool isExact) {
2248 return get(Instruction::AShr, C1, C2,
2249 isExact ? PossiblyExactOperator::IsExact : 0);
Owen Anderson487375e2009-07-29 18:55:55 +00002250}
2251
Duncan Sandsd7aeefe2012-06-12 14:33:56 +00002252Constant *ConstantExpr::getBinOpIdentity(unsigned Opcode, Type *Ty) {
2253 switch (Opcode) {
2254 default:
Duncan Sands318a89d2012-06-13 09:42:13 +00002255 // Doesn't have an identity.
Craig Topperc6207612014-04-09 06:08:46 +00002256 return nullptr;
Duncan Sands318a89d2012-06-13 09:42:13 +00002257
Duncan Sandsd7aeefe2012-06-12 14:33:56 +00002258 case Instruction::Add:
2259 case Instruction::Or:
2260 case Instruction::Xor:
2261 return Constant::getNullValue(Ty);
2262
2263 case Instruction::Mul:
2264 return ConstantInt::get(Ty, 1);
2265
2266 case Instruction::And:
2267 return Constant::getAllOnesValue(Ty);
2268 }
2269}
2270
Duncan Sands318a89d2012-06-13 09:42:13 +00002271Constant *ConstantExpr::getBinOpAbsorber(unsigned Opcode, Type *Ty) {
2272 switch (Opcode) {
2273 default:
2274 // Doesn't have an absorber.
Craig Topperc6207612014-04-09 06:08:46 +00002275 return nullptr;
Duncan Sands318a89d2012-06-13 09:42:13 +00002276
2277 case Instruction::Or:
2278 return Constant::getAllOnesValue(Ty);
2279
2280 case Instruction::And:
2281 case Instruction::Mul:
2282 return Constant::getNullValue(Ty);
2283 }
2284}
2285
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00002286/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00002287void ConstantExpr::destroyConstantImpl() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002288 getType()->getContext().pImpl->ExprConstants.remove(this);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002289}
2290
Chris Lattner3cd8c562002-07-30 18:54:25 +00002291const char *ConstantExpr::getOpcodeName() const {
2292 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002293}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00002294
David Blaikie60310f22015-05-08 00:42:26 +00002295GetElementPtrConstantExpr::GetElementPtrConstantExpr(
2296 Type *SrcElementTy, Constant *C, ArrayRef<Constant *> IdxList, Type *DestTy)
2297 : ConstantExpr(DestTy, Instruction::GetElementPtr,
2298 OperandTraits<GetElementPtrConstantExpr>::op_end(this) -
2299 (IdxList.size() + 1),
2300 IdxList.size() + 1),
Eduard Burtescu19eb0312016-01-19 17:28:00 +00002301 SrcElementTy(SrcElementTy),
2302 ResElementTy(GetElementPtrInst::getIndexedType(SrcElementTy, IdxList)) {
Pete Coopereb31b682015-05-21 22:48:54 +00002303 Op<0>() = C;
Pete Cooper74510a42015-06-12 17:48:05 +00002304 Use *OperandList = getOperandList();
Chris Lattnera3b94ba2010-03-30 20:48:48 +00002305 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
2306 OperandList[i+1] = IdxList[i];
2307}
2308
David Blaikie60310f22015-05-08 00:42:26 +00002309Type *GetElementPtrConstantExpr::getSourceElementType() const {
2310 return SrcElementTy;
2311}
2312
Eduard Burtescu19eb0312016-01-19 17:28:00 +00002313Type *GetElementPtrConstantExpr::getResultElementType() const {
2314 return ResElementTy;
2315}
2316
Chris Lattner3756b912012-01-23 22:57:10 +00002317//===----------------------------------------------------------------------===//
2318// ConstantData* implementations
2319
Chris Lattnere4f3f102012-01-24 04:43:41 +00002320Type *ConstantDataSequential::getElementType() const {
2321 return getType()->getElementType();
2322}
2323
Chris Lattner5d4497b2012-01-24 09:31:43 +00002324StringRef ConstantDataSequential::getRawDataValues() const {
Chris Lattner00245f42012-01-24 13:41:11 +00002325 return StringRef(DataElements, getNumElements()*getElementByteSize());
Chris Lattner5d4497b2012-01-24 09:31:43 +00002326}
2327
Craig Toppere3dcce92015-08-01 22:20:21 +00002328bool ConstantDataSequential::isElementTypeCompatible(Type *Ty) {
Justin Bogner0ebc8602015-12-08 03:01:16 +00002329 if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) return true;
Craig Toppere3dcce92015-08-01 22:20:21 +00002330 if (auto *IT = dyn_cast<IntegerType>(Ty)) {
Chris Lattnere4f3f102012-01-24 04:43:41 +00002331 switch (IT->getBitWidth()) {
2332 case 8:
2333 case 16:
2334 case 32:
2335 case 64:
2336 return true;
2337 default: break;
2338 }
2339 }
2340 return false;
2341}
2342
Chris Lattner00245f42012-01-24 13:41:11 +00002343unsigned ConstantDataSequential::getNumElements() const {
Chris Lattner8a3df542012-01-25 01:32:59 +00002344 if (ArrayType *AT = dyn_cast<ArrayType>(getType()))
2345 return AT->getNumElements();
Chris Lattner8326bd82012-01-26 00:42:34 +00002346 return getType()->getVectorNumElements();
Chris Lattner00245f42012-01-24 13:41:11 +00002347}
2348
2349
Chris Lattnere4f3f102012-01-24 04:43:41 +00002350uint64_t ConstantDataSequential::getElementByteSize() const {
2351 return getElementType()->getPrimitiveSizeInBits()/8;
2352}
2353
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00002354/// Return the start of the specified element.
Chris Lattnere4f3f102012-01-24 04:43:41 +00002355const char *ConstantDataSequential::getElementPointer(unsigned Elt) const {
Chris Lattner00245f42012-01-24 13:41:11 +00002356 assert(Elt < getNumElements() && "Invalid Elt");
Chris Lattnere4f3f102012-01-24 04:43:41 +00002357 return DataElements+Elt*getElementByteSize();
2358}
2359
2360
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00002361/// Return true if the array is empty or all zeros.
Chris Lattner3756b912012-01-23 22:57:10 +00002362static bool isAllZeros(StringRef Arr) {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +00002363 for (char I : Arr)
2364 if (I != 0)
Chris Lattner3756b912012-01-23 22:57:10 +00002365 return false;
2366 return true;
2367}
Chris Lattner030af792012-01-24 05:42:11 +00002368
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00002369/// This is the underlying implementation of all of the
Chris Lattner3756b912012-01-23 22:57:10 +00002370/// ConstantDataSequential::get methods. They all thunk down to here, providing
Chris Lattnerf06039b2012-01-30 18:19:30 +00002371/// the correct element type. We take the bytes in as a StringRef because
Chris Lattner3756b912012-01-23 22:57:10 +00002372/// we *want* an underlying "char*" to avoid TBAA type punning violations.
2373Constant *ConstantDataSequential::getImpl(StringRef Elements, Type *Ty) {
Chris Lattner8326bd82012-01-26 00:42:34 +00002374 assert(isElementTypeCompatible(Ty->getSequentialElementType()));
Chris Lattner139822f2012-01-24 14:17:05 +00002375 // If the elements are all zero or there are no elements, return a CAZ, which
2376 // is more dense and canonical.
Chris Lattner3756b912012-01-23 22:57:10 +00002377 if (isAllZeros(Elements))
2378 return ConstantAggregateZero::get(Ty);
2379
2380 // Do a lookup to see if we have already formed one of these.
David Blaikie5106ce72014-11-19 05:49:42 +00002381 auto &Slot =
2382 *Ty->getContext()
2383 .pImpl->CDSConstants.insert(std::make_pair(Elements, nullptr))
2384 .first;
Galina Kistanovafc259902012-07-13 01:25:27 +00002385
Chris Lattner3756b912012-01-23 22:57:10 +00002386 // The bucket can point to a linked list of different CDS's that have the same
2387 // body but different types. For example, 0,0,0,1 could be a 4 element array
2388 // of i8, or a 1-element array of i32. They'll both end up in the same
2389 /// StringMap bucket, linked up by their Next pointers. Walk the list.
David Blaikie5106ce72014-11-19 05:49:42 +00002390 ConstantDataSequential **Entry = &Slot.second;
Craig Topperc6207612014-04-09 06:08:46 +00002391 for (ConstantDataSequential *Node = *Entry; Node;
Chris Lattner3756b912012-01-23 22:57:10 +00002392 Entry = &Node->Next, Node = *Entry)
2393 if (Node->getType() == Ty)
2394 return Node;
Galina Kistanovafc259902012-07-13 01:25:27 +00002395
Chris Lattner3756b912012-01-23 22:57:10 +00002396 // Okay, we didn't get a hit. Create a node of the right class, link it in,
2397 // and return it.
2398 if (isa<ArrayType>(Ty))
David Blaikie5106ce72014-11-19 05:49:42 +00002399 return *Entry = new ConstantDataArray(Ty, Slot.first().data());
Chris Lattner3756b912012-01-23 22:57:10 +00002400
2401 assert(isa<VectorType>(Ty));
David Blaikie5106ce72014-11-19 05:49:42 +00002402 return *Entry = new ConstantDataVector(Ty, Slot.first().data());
Chris Lattner3756b912012-01-23 22:57:10 +00002403}
2404
Pete Cooper86dd4cf2015-06-23 21:55:11 +00002405void ConstantDataSequential::destroyConstantImpl() {
Chris Lattner3756b912012-01-23 22:57:10 +00002406 // Remove the constant from the StringMap.
2407 StringMap<ConstantDataSequential*> &CDSConstants =
2408 getType()->getContext().pImpl->CDSConstants;
Galina Kistanovafc259902012-07-13 01:25:27 +00002409
Chris Lattner3756b912012-01-23 22:57:10 +00002410 StringMap<ConstantDataSequential*>::iterator Slot =
Chris Lattner5d4497b2012-01-24 09:31:43 +00002411 CDSConstants.find(getRawDataValues());
Chris Lattner3756b912012-01-23 22:57:10 +00002412
2413 assert(Slot != CDSConstants.end() && "CDS not found in uniquing table");
2414
2415 ConstantDataSequential **Entry = &Slot->getValue();
2416
2417 // Remove the entry from the hash table.
Craig Topperc6207612014-04-09 06:08:46 +00002418 if (!(*Entry)->Next) {
Chris Lattner3756b912012-01-23 22:57:10 +00002419 // If there is only one value in the bucket (common case) it must be this
2420 // entry, and removing the entry should remove the bucket completely.
2421 assert((*Entry) == this && "Hash mismatch in ConstantDataSequential");
2422 getContext().pImpl->CDSConstants.erase(Slot);
2423 } else {
2424 // Otherwise, there are multiple entries linked off the bucket, unlink the
2425 // node we care about but keep the bucket around.
2426 for (ConstantDataSequential *Node = *Entry; ;
2427 Entry = &Node->Next, Node = *Entry) {
2428 assert(Node && "Didn't find entry in its uniquing hash table!");
2429 // If we found our entry, unlink it from the list and we're done.
2430 if (Node == this) {
2431 *Entry = Node->Next;
2432 break;
2433 }
2434 }
2435 }
Galina Kistanovafc259902012-07-13 01:25:27 +00002436
Chris Lattner3756b912012-01-23 22:57:10 +00002437 // If we were part of a list, make sure that we don't delete the list that is
2438 // still owned by the uniquing map.
Craig Topperc6207612014-04-09 06:08:46 +00002439 Next = nullptr;
Chris Lattner3756b912012-01-23 22:57:10 +00002440}
2441
2442/// get() constructors - Return a constant with array type with an element
2443/// count and element type matching the ArrayRef passed in. Note that this
2444/// can return a ConstantAggregateZero object.
Chris Lattner20683932012-01-24 14:04:40 +00002445Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint8_t> Elts) {
Chris Lattner3756b912012-01-23 22:57:10 +00002446 Type *Ty = ArrayType::get(Type::getInt8Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002447 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002448 return getImpl(StringRef(Data, Elts.size() * 1), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002449}
Chris Lattner20683932012-01-24 14:04:40 +00002450Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint16_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002451 Type *Ty = ArrayType::get(Type::getInt16Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002452 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002453 return getImpl(StringRef(Data, Elts.size() * 2), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002454}
Chris Lattner20683932012-01-24 14:04:40 +00002455Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint32_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002456 Type *Ty = ArrayType::get(Type::getInt32Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002457 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002458 return getImpl(StringRef(Data, Elts.size() * 4), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002459}
Chris Lattner20683932012-01-24 14:04:40 +00002460Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint64_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002461 Type *Ty = ArrayType::get(Type::getInt64Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002462 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002463 return getImpl(StringRef(Data, Elts.size() * 8), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002464}
Chris Lattner20683932012-01-24 14:04:40 +00002465Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<float> Elts) {
Chris Lattner3756b912012-01-23 22:57:10 +00002466 Type *Ty = ArrayType::get(Type::getFloatTy(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002467 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002468 return getImpl(StringRef(Data, Elts.size() * 4), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002469}
Chris Lattner20683932012-01-24 14:04:40 +00002470Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<double> Elts) {
Chris Lattner3756b912012-01-23 22:57:10 +00002471 Type *Ty = ArrayType::get(Type::getDoubleTy(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002472 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002473 return getImpl(StringRef(Data, Elts.size() * 8), Ty);
Rafael Espindola8c97e192015-02-19 16:08:20 +00002474}
2475
2476/// getFP() constructors - Return a constant with array type with an element
2477/// count and element type of float with precision matching the number of
2478/// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits,
2479/// double for 64bits) Note that this can return a ConstantAggregateZero
2480/// object.
2481Constant *ConstantDataArray::getFP(LLVMContext &Context,
2482 ArrayRef<uint16_t> Elts) {
Justin Bognerb7389d6712015-12-09 21:21:07 +00002483 Type *Ty = ArrayType::get(Type::getHalfTy(Context), Elts.size());
Rafael Espindola8c97e192015-02-19 16:08:20 +00002484 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002485 return getImpl(StringRef(Data, Elts.size() * 2), Ty);
Rafael Espindola8c97e192015-02-19 16:08:20 +00002486}
2487Constant *ConstantDataArray::getFP(LLVMContext &Context,
2488 ArrayRef<uint32_t> Elts) {
2489 Type *Ty = ArrayType::get(Type::getFloatTy(Context), Elts.size());
2490 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002491 return getImpl(StringRef(Data, Elts.size() * 4), Ty);
Rafael Espindola8c97e192015-02-19 16:08:20 +00002492}
2493Constant *ConstantDataArray::getFP(LLVMContext &Context,
2494 ArrayRef<uint64_t> Elts) {
2495 Type *Ty = ArrayType::get(Type::getDoubleTy(Context), Elts.size());
2496 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002497 return getImpl(StringRef(Data, Elts.size() * 8), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002498}
2499
Chris Lattner20683932012-01-24 14:04:40 +00002500Constant *ConstantDataArray::getString(LLVMContext &Context,
2501 StringRef Str, bool AddNull) {
Galina Kistanovafc259902012-07-13 01:25:27 +00002502 if (!AddNull) {
2503 const uint8_t *Data = reinterpret_cast<const uint8_t *>(Str.data());
Craig Topper393ce692017-07-11 15:52:21 +00002504 return get(Context, makeArrayRef(Data, Str.size()));
Galina Kistanovafc259902012-07-13 01:25:27 +00002505 }
2506
Chris Lattner20683932012-01-24 14:04:40 +00002507 SmallVector<uint8_t, 64> ElementVals;
2508 ElementVals.append(Str.begin(), Str.end());
2509 ElementVals.push_back(0);
2510 return get(Context, ElementVals);
2511}
Chris Lattner3756b912012-01-23 22:57:10 +00002512
2513/// get() constructors - Return a constant with vector type with an element
2514/// count and element type matching the ArrayRef passed in. Note that this
2515/// can return a ConstantAggregateZero object.
Chris Lattner20683932012-01-24 14:04:40 +00002516Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint8_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002517 Type *Ty = VectorType::get(Type::getInt8Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002518 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002519 return getImpl(StringRef(Data, Elts.size() * 1), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002520}
Chris Lattner20683932012-01-24 14:04:40 +00002521Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint16_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002522 Type *Ty = VectorType::get(Type::getInt16Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002523 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002524 return getImpl(StringRef(Data, Elts.size() * 2), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002525}
Chris Lattner20683932012-01-24 14:04:40 +00002526Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint32_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002527 Type *Ty = VectorType::get(Type::getInt32Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002528 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002529 return getImpl(StringRef(Data, Elts.size() * 4), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002530}
Chris Lattner20683932012-01-24 14:04:40 +00002531Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint64_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002532 Type *Ty = VectorType::get(Type::getInt64Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002533 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002534 return getImpl(StringRef(Data, Elts.size() * 8), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002535}
Chris Lattner20683932012-01-24 14:04:40 +00002536Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<float> Elts) {
Chris Lattner3756b912012-01-23 22:57:10 +00002537 Type *Ty = VectorType::get(Type::getFloatTy(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002538 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002539 return getImpl(StringRef(Data, Elts.size() * 4), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002540}
Chris Lattner20683932012-01-24 14:04:40 +00002541Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<double> Elts) {
Chris Lattner3756b912012-01-23 22:57:10 +00002542 Type *Ty = VectorType::get(Type::getDoubleTy(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002543 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002544 return getImpl(StringRef(Data, Elts.size() * 8), Ty);
Rafael Espindola8c97e192015-02-19 16:08:20 +00002545}
2546
2547/// getFP() constructors - Return a constant with vector type with an element
2548/// count and element type of float with the precision matching the number of
2549/// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits,
2550/// double for 64bits) Note that this can return a ConstantAggregateZero
2551/// object.
2552Constant *ConstantDataVector::getFP(LLVMContext &Context,
2553 ArrayRef<uint16_t> Elts) {
2554 Type *Ty = VectorType::get(Type::getHalfTy(Context), Elts.size());
2555 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002556 return getImpl(StringRef(Data, Elts.size() * 2), Ty);
Rafael Espindola8c97e192015-02-19 16:08:20 +00002557}
2558Constant *ConstantDataVector::getFP(LLVMContext &Context,
2559 ArrayRef<uint32_t> Elts) {
2560 Type *Ty = VectorType::get(Type::getFloatTy(Context), Elts.size());
2561 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002562 return getImpl(StringRef(Data, Elts.size() * 4), Ty);
Rafael Espindola8c97e192015-02-19 16:08:20 +00002563}
2564Constant *ConstantDataVector::getFP(LLVMContext &Context,
2565 ArrayRef<uint64_t> Elts) {
2566 Type *Ty = VectorType::get(Type::getDoubleTy(Context), Elts.size());
2567 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002568 return getImpl(StringRef(Data, Elts.size() * 8), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002569}
2570
Chris Lattnere9eed292012-01-25 05:19:54 +00002571Constant *ConstantDataVector::getSplat(unsigned NumElts, Constant *V) {
2572 assert(isElementTypeCompatible(V->getType()) &&
2573 "Element type not compatible with ConstantData");
2574 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
2575 if (CI->getType()->isIntegerTy(8)) {
2576 SmallVector<uint8_t, 16> Elts(NumElts, CI->getZExtValue());
2577 return get(V->getContext(), Elts);
2578 }
2579 if (CI->getType()->isIntegerTy(16)) {
2580 SmallVector<uint16_t, 16> Elts(NumElts, CI->getZExtValue());
2581 return get(V->getContext(), Elts);
2582 }
2583 if (CI->getType()->isIntegerTy(32)) {
2584 SmallVector<uint32_t, 16> Elts(NumElts, CI->getZExtValue());
2585 return get(V->getContext(), Elts);
2586 }
2587 assert(CI->getType()->isIntegerTy(64) && "Unsupported ConstantData type");
2588 SmallVector<uint64_t, 16> Elts(NumElts, CI->getZExtValue());
2589 return get(V->getContext(), Elts);
2590 }
2591
Chris Lattner978fe0c2012-01-30 06:21:21 +00002592 if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
Justin Bogner0ebc8602015-12-08 03:01:16 +00002593 if (CFP->getType()->isHalfTy()) {
2594 SmallVector<uint16_t, 16> Elts(
2595 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
2596 return getFP(V->getContext(), Elts);
2597 }
Chris Lattner978fe0c2012-01-30 06:21:21 +00002598 if (CFP->getType()->isFloatTy()) {
Rafael Espindola8c97e192015-02-19 16:08:20 +00002599 SmallVector<uint32_t, 16> Elts(
2600 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
2601 return getFP(V->getContext(), Elts);
Chris Lattner978fe0c2012-01-30 06:21:21 +00002602 }
2603 if (CFP->getType()->isDoubleTy()) {
Rafael Espindola8c97e192015-02-19 16:08:20 +00002604 SmallVector<uint64_t, 16> Elts(
2605 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
2606 return getFP(V->getContext(), Elts);
Chris Lattner978fe0c2012-01-30 06:21:21 +00002607 }
Chris Lattnere9eed292012-01-25 05:19:54 +00002608 }
Chris Lattner978fe0c2012-01-30 06:21:21 +00002609 return ConstantVector::getSplat(NumElts, V);
Chris Lattnere9eed292012-01-25 05:19:54 +00002610}
2611
2612
Chris Lattnere4f3f102012-01-24 04:43:41 +00002613uint64_t ConstantDataSequential::getElementAsInteger(unsigned Elt) const {
2614 assert(isa<IntegerType>(getElementType()) &&
2615 "Accessor can only be used when element is an integer");
2616 const char *EltPtr = getElementPointer(Elt);
Galina Kistanovafc259902012-07-13 01:25:27 +00002617
Chris Lattnere4f3f102012-01-24 04:43:41 +00002618 // The data is stored in host byte order, make sure to cast back to the right
2619 // type to load with the right endianness.
Chris Lattner8326bd82012-01-26 00:42:34 +00002620 switch (getElementType()->getIntegerBitWidth()) {
Craig Topperc514b542012-02-05 22:14:15 +00002621 default: llvm_unreachable("Invalid bitwidth for CDS");
Galina Kistanovafc259902012-07-13 01:25:27 +00002622 case 8:
Craig Topper393ce692017-07-11 15:52:21 +00002623 return *reinterpret_cast<const uint8_t *>(EltPtr);
Galina Kistanovafc259902012-07-13 01:25:27 +00002624 case 16:
Craig Topper393ce692017-07-11 15:52:21 +00002625 return *reinterpret_cast<const uint16_t *>(EltPtr);
Galina Kistanovafc259902012-07-13 01:25:27 +00002626 case 32:
Craig Topper393ce692017-07-11 15:52:21 +00002627 return *reinterpret_cast<const uint32_t *>(EltPtr);
Galina Kistanovafc259902012-07-13 01:25:27 +00002628 case 64:
Craig Topper393ce692017-07-11 15:52:21 +00002629 return *reinterpret_cast<const uint64_t *>(EltPtr);
Chris Lattnere4f3f102012-01-24 04:43:41 +00002630 }
2631}
2632
Craig Topper0b4b4e32017-07-15 22:06:19 +00002633APInt ConstantDataSequential::getElementAsAPInt(unsigned Elt) const {
2634 assert(isa<IntegerType>(getElementType()) &&
2635 "Accessor can only be used when element is an integer");
2636 const char *EltPtr = getElementPointer(Elt);
2637
2638 // The data is stored in host byte order, make sure to cast back to the right
2639 // type to load with the right endianness.
2640 switch (getElementType()->getIntegerBitWidth()) {
2641 default: llvm_unreachable("Invalid bitwidth for CDS");
2642 case 8: {
2643 auto EltVal = *reinterpret_cast<const uint8_t *>(EltPtr);
2644 return APInt(8, EltVal);
2645 }
2646 case 16: {
2647 auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr);
2648 return APInt(16, EltVal);
2649 }
2650 case 32: {
2651 auto EltVal = *reinterpret_cast<const uint32_t *>(EltPtr);
2652 return APInt(32, EltVal);
2653 }
2654 case 64: {
2655 auto EltVal = *reinterpret_cast<const uint64_t *>(EltPtr);
2656 return APInt(64, EltVal);
2657 }
2658 }
2659}
2660
Chris Lattnere4f3f102012-01-24 04:43:41 +00002661APFloat ConstantDataSequential::getElementAsAPFloat(unsigned Elt) const {
2662 const char *EltPtr = getElementPointer(Elt);
2663
2664 switch (getElementType()->getTypeID()) {
Nick Lewyckyff509622012-01-25 03:20:12 +00002665 default:
Craig Topperc514b542012-02-05 22:14:15 +00002666 llvm_unreachable("Accessor can only be used when element is float/double!");
Justin Bogner0ebc8602015-12-08 03:01:16 +00002667 case Type::HalfTyID: {
2668 auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr);
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002669 return APFloat(APFloat::IEEEhalf(), APInt(16, EltVal));
Justin Bogner0ebc8602015-12-08 03:01:16 +00002670 }
Benjamin Kramer7af984b2015-02-20 15:11:55 +00002671 case Type::FloatTyID: {
2672 auto EltVal = *reinterpret_cast<const uint32_t *>(EltPtr);
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002673 return APFloat(APFloat::IEEEsingle(), APInt(32, EltVal));
Benjamin Kramer7af984b2015-02-20 15:11:55 +00002674 }
2675 case Type::DoubleTyID: {
2676 auto EltVal = *reinterpret_cast<const uint64_t *>(EltPtr);
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002677 return APFloat(APFloat::IEEEdouble(), APInt(64, EltVal));
Chris Lattnere4f3f102012-01-24 04:43:41 +00002678 }
Benjamin Kramer7af984b2015-02-20 15:11:55 +00002679 }
Chris Lattnere4f3f102012-01-24 04:43:41 +00002680}
2681
Chris Lattnere4f3f102012-01-24 04:43:41 +00002682float ConstantDataSequential::getElementAsFloat(unsigned Elt) const {
2683 assert(getElementType()->isFloatTy() &&
2684 "Accessor can only be used when element is a 'float'");
Craig Topper393ce692017-07-11 15:52:21 +00002685 return *reinterpret_cast<const float *>(getElementPointer(Elt));
Chris Lattnere4f3f102012-01-24 04:43:41 +00002686}
2687
Chris Lattnere4f3f102012-01-24 04:43:41 +00002688double ConstantDataSequential::getElementAsDouble(unsigned Elt) const {
2689 assert(getElementType()->isDoubleTy() &&
2690 "Accessor can only be used when element is a 'float'");
Craig Topper393ce692017-07-11 15:52:21 +00002691 return *reinterpret_cast<const double *>(getElementPointer(Elt));
Chris Lattnere4f3f102012-01-24 04:43:41 +00002692}
2693
Chris Lattnere4f3f102012-01-24 04:43:41 +00002694Constant *ConstantDataSequential::getElementAsConstant(unsigned Elt) const {
Justin Bogner0ebc8602015-12-08 03:01:16 +00002695 if (getElementType()->isHalfTy() || getElementType()->isFloatTy() ||
2696 getElementType()->isDoubleTy())
Chris Lattnere4f3f102012-01-24 04:43:41 +00002697 return ConstantFP::get(getContext(), getElementAsAPFloat(Elt));
Galina Kistanovafc259902012-07-13 01:25:27 +00002698
Chris Lattnere4f3f102012-01-24 04:43:41 +00002699 return ConstantInt::get(getElementType(), getElementAsInteger(Elt));
2700}
2701
Matthias Braun50ec0b52017-05-19 22:37:09 +00002702bool ConstantDataSequential::isString(unsigned CharSize) const {
2703 return isa<ArrayType>(getType()) && getElementType()->isIntegerTy(CharSize);
Chris Lattner5dd4d872012-01-24 09:01:07 +00002704}
Chris Lattner3756b912012-01-23 22:57:10 +00002705
Chris Lattner5dd4d872012-01-24 09:01:07 +00002706bool ConstantDataSequential::isCString() const {
2707 if (!isString())
2708 return false;
Galina Kistanovafc259902012-07-13 01:25:27 +00002709
Chris Lattner5dd4d872012-01-24 09:01:07 +00002710 StringRef Str = getAsString();
Galina Kistanovafc259902012-07-13 01:25:27 +00002711
Chris Lattner5dd4d872012-01-24 09:01:07 +00002712 // The last value must be nul.
2713 if (Str.back() != 0) return false;
Galina Kistanovafc259902012-07-13 01:25:27 +00002714
Chris Lattner5dd4d872012-01-24 09:01:07 +00002715 // Other elements must be non-nul.
2716 return Str.drop_back().find(0) == StringRef::npos;
2717}
Chris Lattner3756b912012-01-23 22:57:10 +00002718
Craig Topper0b4b4e32017-07-15 22:06:19 +00002719bool ConstantDataVector::isSplat() const {
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002720 const char *Base = getRawDataValues().data();
Galina Kistanovafc259902012-07-13 01:25:27 +00002721
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002722 // Compare elements 1+ to the 0'th element.
2723 unsigned EltSize = getElementByteSize();
2724 for (unsigned i = 1, e = getNumElements(); i != e; ++i)
2725 if (memcmp(Base, Base+i*EltSize, EltSize))
Craig Topper0b4b4e32017-07-15 22:06:19 +00002726 return false;
Galina Kistanovafc259902012-07-13 01:25:27 +00002727
Craig Topper0b4b4e32017-07-15 22:06:19 +00002728 return true;
2729}
2730
2731Constant *ConstantDataVector::getSplatValue() const {
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002732 // If they're all the same, return the 0th one as a representative.
Craig Topper0b4b4e32017-07-15 22:06:19 +00002733 return isSplat() ? getElementAsConstant(0) : nullptr;
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002734}
Chris Lattnera3b94ba2010-03-30 20:48:48 +00002735
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002736//===----------------------------------------------------------------------===//
Pete Cooper5815b1f2015-06-24 18:55:24 +00002737// handleOperandChange implementations
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002738
Pete Cooper5815b1f2015-06-24 18:55:24 +00002739/// Update this constant array to change uses of
Chris Lattner913849b2007-08-21 00:55:23 +00002740/// 'From' to be uses of 'To'. This must update the uniquing data structures
2741/// etc.
2742///
2743/// Note that we intentionally replace all uses of From with To here. Consider
2744/// a large array that uses 'From' 1000 times. By handling this case all here,
Pete Cooper5815b1f2015-06-24 18:55:24 +00002745/// ConstantArray::handleOperandChange is only invoked once, and that
Chris Lattner913849b2007-08-21 00:55:23 +00002746/// single invocation handles all 1000 uses. Handling them one at a time would
2747/// work, but would be really slow because it would have to unique each updated
2748/// array instance.
Chris Lattner31b132c2009-10-28 00:01:44 +00002749///
Mehdi Amini8914d292016-02-10 22:47:15 +00002750void Constant::handleOperandChange(Value *From, Value *To) {
Pete Cooper5815b1f2015-06-24 18:55:24 +00002751 Value *Replacement = nullptr;
2752 switch (getValueID()) {
2753 default:
2754 llvm_unreachable("Not a constant!");
2755#define HANDLE_CONSTANT(Name) \
2756 case Value::Name##Val: \
Mehdi Amini8914d292016-02-10 22:47:15 +00002757 Replacement = cast<Name>(this)->handleOperandChangeImpl(From, To); \
Pete Cooper5815b1f2015-06-24 18:55:24 +00002758 break;
2759#include "llvm/IR/Value.def"
2760 }
2761
2762 // If handleOperandChangeImpl returned nullptr, then it handled
2763 // replacing itself and we don't want to delete or replace anything else here.
2764 if (!Replacement)
2765 return;
2766
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002767 // I do need to replace this with an existing value.
2768 assert(Replacement != this && "I didn't contain From!");
2769
2770 // Everyone using this now uses the replacement.
2771 replaceAllUsesWith(Replacement);
2772
2773 // Delete the old constant!
2774 destroyConstant();
2775}
2776
Mehdi Amini8914d292016-02-10 22:47:15 +00002777Value *ConstantArray::handleOperandChangeImpl(Value *From, Value *To) {
Owen Andersonc2c79322009-07-28 18:32:17 +00002778 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2779 Constant *ToC = cast<Constant>(To);
2780
Talin46e9b442012-02-05 20:54:10 +00002781 SmallVector<Constant*, 8> Values;
Owen Andersonc2c79322009-07-28 18:32:17 +00002782 Values.reserve(getNumOperands()); // Build replacement array.
2783
Galina Kistanovafc259902012-07-13 01:25:27 +00002784 // Fill values with the modified operands of the constant array. Also,
Owen Andersonc2c79322009-07-28 18:32:17 +00002785 // compute whether this turns into an all-zeros array.
Owen Andersonc2c79322009-07-28 18:32:17 +00002786 unsigned NumUpdated = 0;
Galina Kistanovafc259902012-07-13 01:25:27 +00002787
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002788 // Keep track of whether all the values in the array are "ToC".
2789 bool AllSame = true;
Pete Cooper74510a42015-06-12 17:48:05 +00002790 Use *OperandList = getOperandList();
Mehdi Amini8914d292016-02-10 22:47:15 +00002791 unsigned OperandNo = 0;
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002792 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2793 Constant *Val = cast<Constant>(O->get());
2794 if (Val == From) {
Mehdi Amini8914d292016-02-10 22:47:15 +00002795 OperandNo = (O - OperandList);
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002796 Val = ToC;
2797 ++NumUpdated;
Owen Andersonc2c79322009-07-28 18:32:17 +00002798 }
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002799 Values.push_back(Val);
Talin46e9b442012-02-05 20:54:10 +00002800 AllSame &= Val == ToC;
Owen Andersonc2c79322009-07-28 18:32:17 +00002801 }
Galina Kistanovafc259902012-07-13 01:25:27 +00002802
Pete Cooper5815b1f2015-06-24 18:55:24 +00002803 if (AllSame && ToC->isNullValue())
2804 return ConstantAggregateZero::get(getType());
2805
2806 if (AllSame && isa<UndefValue>(ToC))
2807 return UndefValue::get(getType());
Aaron Ballmane4b91dc2014-08-19 14:59:02 +00002808
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002809 // Check for any other type of constant-folding.
Pete Cooper5815b1f2015-06-24 18:55:24 +00002810 if (Constant *C = getImpl(getType(), Values))
2811 return C;
Aaron Ballmane4b91dc2014-08-19 14:59:02 +00002812
Duncan P. N. Exon Smith909620a2014-08-19 19:13:30 +00002813 // Update to the new value.
Pete Cooper5815b1f2015-06-24 18:55:24 +00002814 return getContext().pImpl->ArrayConstants.replaceOperandsInPlace(
Mehdi Amini8914d292016-02-10 22:47:15 +00002815 Values, this, From, ToC, NumUpdated, OperandNo);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002816}
2817
Mehdi Amini8914d292016-02-10 22:47:15 +00002818Value *ConstantStruct::handleOperandChangeImpl(Value *From, Value *To) {
Owen Anderson45308b52009-07-27 22:29:26 +00002819 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2820 Constant *ToC = cast<Constant>(To);
2821
Pete Cooper74510a42015-06-12 17:48:05 +00002822 Use *OperandList = getOperandList();
Owen Anderson45308b52009-07-27 22:29:26 +00002823
Talin46e9b442012-02-05 20:54:10 +00002824 SmallVector<Constant*, 8> Values;
Owen Anderson45308b52009-07-27 22:29:26 +00002825 Values.reserve(getNumOperands()); // Build replacement struct.
Galina Kistanovafc259902012-07-13 01:25:27 +00002826
2827 // Fill values with the modified operands of the constant struct. Also,
Owen Anderson45308b52009-07-27 22:29:26 +00002828 // compute whether this turns into an all-zeros struct.
Mehdi Amini8914d292016-02-10 22:47:15 +00002829 unsigned NumUpdated = 0;
2830 bool AllSame = true;
2831 unsigned OperandNo = 0;
2832 for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O) {
2833 Constant *Val = cast<Constant>(O->get());
2834 if (Val == From) {
2835 OperandNo = (O - OperandList);
2836 Val = ToC;
2837 ++NumUpdated;
Owen Anderson45308b52009-07-27 22:29:26 +00002838 }
Mehdi Amini8914d292016-02-10 22:47:15 +00002839 Values.push_back(Val);
2840 AllSame &= Val == ToC;
Owen Anderson45308b52009-07-27 22:29:26 +00002841 }
Galina Kistanovafc259902012-07-13 01:25:27 +00002842
Mehdi Amini8914d292016-02-10 22:47:15 +00002843 if (AllSame && ToC->isNullValue())
Pete Cooper5815b1f2015-06-24 18:55:24 +00002844 return ConstantAggregateZero::get(getType());
2845
Mehdi Amini8914d292016-02-10 22:47:15 +00002846 if (AllSame && isa<UndefValue>(ToC))
Pete Cooper5815b1f2015-06-24 18:55:24 +00002847 return UndefValue::get(getType());
Galina Kistanovafc259902012-07-13 01:25:27 +00002848
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002849 // Update to the new value.
Pete Cooper5815b1f2015-06-24 18:55:24 +00002850 return getContext().pImpl->StructConstants.replaceOperandsInPlace(
Mehdi Amini8914d292016-02-10 22:47:15 +00002851 Values, this, From, ToC, NumUpdated, OperandNo);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002852}
2853
Mehdi Amini8914d292016-02-10 22:47:15 +00002854Value *ConstantVector::handleOperandChangeImpl(Value *From, Value *To) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002855 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002856 Constant *ToC = cast<Constant>(To);
Galina Kistanovafc259902012-07-13 01:25:27 +00002857
Chris Lattnera474bb22012-01-26 20:40:56 +00002858 SmallVector<Constant*, 8> Values;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002859 Values.reserve(getNumOperands()); // Build replacement array...
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002860 unsigned NumUpdated = 0;
Mehdi Amini8914d292016-02-10 22:47:15 +00002861 unsigned OperandNo = 0;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002862 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2863 Constant *Val = getOperand(i);
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002864 if (Val == From) {
Mehdi Amini8914d292016-02-10 22:47:15 +00002865 OperandNo = i;
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002866 ++NumUpdated;
2867 Val = ToC;
2868 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002869 Values.push_back(Val);
2870 }
Galina Kistanovafc259902012-07-13 01:25:27 +00002871
Pete Cooper5815b1f2015-06-24 18:55:24 +00002872 if (Constant *C = getImpl(Values))
2873 return C;
Duncan P. N. Exon Smith687744d2014-08-19 02:24:46 +00002874
Duncan P. N. Exon Smith909620a2014-08-19 19:13:30 +00002875 // Update to the new value.
Pete Cooper5815b1f2015-06-24 18:55:24 +00002876 return getContext().pImpl->VectorConstants.replaceOperandsInPlace(
Mehdi Amini8914d292016-02-10 22:47:15 +00002877 Values, this, From, ToC, NumUpdated, OperandNo);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002878}
2879
Mehdi Amini8914d292016-02-10 22:47:15 +00002880Value *ConstantExpr::handleOperandChangeImpl(Value *From, Value *ToV) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002881 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2882 Constant *To = cast<Constant>(ToV);
Galina Kistanovafc259902012-07-13 01:25:27 +00002883
Chris Lattner37e38352012-01-26 20:37:11 +00002884 SmallVector<Constant*, 8> NewOps;
Duncan P. N. Exon Smith33de00c2014-08-19 20:03:35 +00002885 unsigned NumUpdated = 0;
Mehdi Amini8914d292016-02-10 22:47:15 +00002886 unsigned OperandNo = 0;
Chris Lattner37e38352012-01-26 20:37:11 +00002887 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2888 Constant *Op = getOperand(i);
Duncan P. N. Exon Smith33de00c2014-08-19 20:03:35 +00002889 if (Op == From) {
Mehdi Amini8914d292016-02-10 22:47:15 +00002890 OperandNo = i;
Duncan P. N. Exon Smith33de00c2014-08-19 20:03:35 +00002891 ++NumUpdated;
2892 Op = To;
2893 }
2894 NewOps.push_back(Op);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002895 }
Duncan P. N. Exon Smith33de00c2014-08-19 20:03:35 +00002896 assert(NumUpdated && "I didn't contain From!");
Galina Kistanovafc259902012-07-13 01:25:27 +00002897
Pete Cooper5815b1f2015-06-24 18:55:24 +00002898 if (Constant *C = getWithOperands(NewOps, getType(), true))
2899 return C;
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002900
Duncan P. N. Exon Smith33de00c2014-08-19 20:03:35 +00002901 // Update to the new value.
Pete Cooper5815b1f2015-06-24 18:55:24 +00002902 return getContext().pImpl->ExprConstants.replaceOperandsInPlace(
Mehdi Amini8914d292016-02-10 22:47:15 +00002903 NewOps, this, From, To, NumUpdated, OperandNo);
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002904}
2905
James Molloyce545682012-11-17 17:56:30 +00002906Instruction *ConstantExpr::getAsInstruction() {
Benjamin Kramer5fbfe2f2015-02-28 13:20:15 +00002907 SmallVector<Value *, 4> ValueOperands(op_begin(), op_end());
James Molloyce545682012-11-17 17:56:30 +00002908 ArrayRef<Value*> Ops(ValueOperands);
2909
2910 switch (getOpcode()) {
2911 case Instruction::Trunc:
2912 case Instruction::ZExt:
2913 case Instruction::SExt:
2914 case Instruction::FPTrunc:
2915 case Instruction::FPExt:
2916 case Instruction::UIToFP:
2917 case Instruction::SIToFP:
2918 case Instruction::FPToUI:
2919 case Instruction::FPToSI:
2920 case Instruction::PtrToInt:
2921 case Instruction::IntToPtr:
2922 case Instruction::BitCast:
Eli Bendersky157a97a2014-01-18 22:54:33 +00002923 case Instruction::AddrSpaceCast:
James Molloyce545682012-11-17 17:56:30 +00002924 return CastInst::Create((Instruction::CastOps)getOpcode(),
2925 Ops[0], getType());
2926 case Instruction::Select:
2927 return SelectInst::Create(Ops[0], Ops[1], Ops[2]);
2928 case Instruction::InsertElement:
2929 return InsertElementInst::Create(Ops[0], Ops[1], Ops[2]);
2930 case Instruction::ExtractElement:
2931 return ExtractElementInst::Create(Ops[0], Ops[1]);
2932 case Instruction::InsertValue:
2933 return InsertValueInst::Create(Ops[0], Ops[1], getIndices());
2934 case Instruction::ExtractValue:
2935 return ExtractValueInst::Create(Ops[0], getIndices());
2936 case Instruction::ShuffleVector:
2937 return new ShuffleVectorInst(Ops[0], Ops[1], Ops[2]);
2938
David Blaikie741c8f82015-03-14 01:53:18 +00002939 case Instruction::GetElementPtr: {
2940 const auto *GO = cast<GEPOperator>(this);
2941 if (GO->isInBounds())
2942 return GetElementPtrInst::CreateInBounds(GO->getSourceElementType(),
2943 Ops[0], Ops.slice(1));
2944 return GetElementPtrInst::Create(GO->getSourceElementType(), Ops[0],
2945 Ops.slice(1));
2946 }
James Molloyce545682012-11-17 17:56:30 +00002947 case Instruction::ICmp:
2948 case Instruction::FCmp:
2949 return CmpInst::Create((Instruction::OtherOps)getOpcode(),
Craig Topper1c3f2832015-12-15 06:11:33 +00002950 (CmpInst::Predicate)getPredicate(), Ops[0], Ops[1]);
James Molloyce545682012-11-17 17:56:30 +00002951
2952 default:
2953 assert(getNumOperands() == 2 && "Must be binary operator?");
2954 BinaryOperator *BO =
2955 BinaryOperator::Create((Instruction::BinaryOps)getOpcode(),
2956 Ops[0], Ops[1]);
2957 if (isa<OverflowingBinaryOperator>(BO)) {
2958 BO->setHasNoUnsignedWrap(SubclassOptionalData &
2959 OverflowingBinaryOperator::NoUnsignedWrap);
2960 BO->setHasNoSignedWrap(SubclassOptionalData &
2961 OverflowingBinaryOperator::NoSignedWrap);
2962 }
2963 if (isa<PossiblyExactOperator>(BO))
2964 BO->setIsExact(SubclassOptionalData & PossiblyExactOperator::IsExact);
2965 return BO;
2966 }
2967}