blob: dccba779deb354b9f50275a0e978a03bd8c40f68 [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 Patel1d0ac7c2016-04-29 22:03:27 +0000205/// Constructor to create a '0' constant of arbitrary type.
Chris Lattner229907c2011-07-18 04:54:35 +0000206Constant *Constant::getNullValue(Type *Ty) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000207 switch (Ty->getTypeID()) {
208 case Type::IntegerTyID:
209 return ConstantInt::get(Ty, 0);
Dan Gohman518cda42011-12-17 00:04:22 +0000210 case Type::HalfTyID:
211 return ConstantFP::get(Ty->getContext(),
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000212 APFloat::getZero(APFloat::IEEEhalf()));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000213 case Type::FloatTyID:
Benjamin Kramer8ceebfa2010-12-04 14:22:24 +0000214 return ConstantFP::get(Ty->getContext(),
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000215 APFloat::getZero(APFloat::IEEEsingle()));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000216 case Type::DoubleTyID:
Benjamin Kramer8ceebfa2010-12-04 14:22:24 +0000217 return ConstantFP::get(Ty->getContext(),
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000218 APFloat::getZero(APFloat::IEEEdouble()));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000219 case Type::X86_FP80TyID:
Benjamin Kramer8ceebfa2010-12-04 14:22:24 +0000220 return ConstantFP::get(Ty->getContext(),
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000221 APFloat::getZero(APFloat::x87DoubleExtended()));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000222 case Type::FP128TyID:
223 return ConstantFP::get(Ty->getContext(),
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000224 APFloat::getZero(APFloat::IEEEquad()));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000225 case Type::PPC_FP128TyID:
Benjamin Kramer8ceebfa2010-12-04 14:22:24 +0000226 return ConstantFP::get(Ty->getContext(),
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000227 APFloat(APFloat::PPCDoubleDouble(),
Tim Northover29178a32013-01-22 09:46:31 +0000228 APInt::getNullValue(128)));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000229 case Type::PointerTyID:
230 return ConstantPointerNull::get(cast<PointerType>(Ty));
231 case Type::StructTyID:
232 case Type::ArrayTyID:
233 case Type::VectorTyID:
234 return ConstantAggregateZero::get(Ty);
David Majnemerf0f224d2015-11-11 21:57:16 +0000235 case Type::TokenTyID:
236 return ConstantTokenNone::get(Ty->getContext());
Owen Anderson5a1acd92009-07-31 20:28:14 +0000237 default:
238 // Function, Label, or Opaque type?
Craig Topperc514b542012-02-05 22:14:15 +0000239 llvm_unreachable("Cannot create a null constant of that type!");
Owen Anderson5a1acd92009-07-31 20:28:14 +0000240 }
241}
242
Chris Lattner229907c2011-07-18 04:54:35 +0000243Constant *Constant::getIntegerValue(Type *Ty, const APInt &V) {
244 Type *ScalarTy = Ty->getScalarType();
Dan Gohmanf011f5a2009-08-03 22:07:33 +0000245
246 // Create the base integer constant.
247 Constant *C = ConstantInt::get(Ty->getContext(), V);
248
249 // Convert an integer to a pointer, if necessary.
Chris Lattner229907c2011-07-18 04:54:35 +0000250 if (PointerType *PTy = dyn_cast<PointerType>(ScalarTy))
Dan Gohmanf011f5a2009-08-03 22:07:33 +0000251 C = ConstantExpr::getIntToPtr(C, PTy);
252
253 // Broadcast a scalar to a vector, if necessary.
Chris Lattner229907c2011-07-18 04:54:35 +0000254 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
Chris Lattnere9eed292012-01-25 05:19:54 +0000255 C = ConstantVector::getSplat(VTy->getNumElements(), C);
Dan Gohmanf011f5a2009-08-03 22:07:33 +0000256
257 return C;
258}
259
Chris Lattner229907c2011-07-18 04:54:35 +0000260Constant *Constant::getAllOnesValue(Type *Ty) {
261 if (IntegerType *ITy = dyn_cast<IntegerType>(Ty))
Owen Anderson5a1acd92009-07-31 20:28:14 +0000262 return ConstantInt::get(Ty->getContext(),
263 APInt::getAllOnesValue(ITy->getBitWidth()));
Nadav Rotem7cc6d122011-02-17 21:22:27 +0000264
265 if (Ty->isFloatingPointTy()) {
266 APFloat FL = APFloat::getAllOnesValue(Ty->getPrimitiveSizeInBits(),
267 !Ty->isPPC_FP128Ty());
268 return ConstantFP::get(Ty->getContext(), FL);
269 }
270
Chris Lattner229907c2011-07-18 04:54:35 +0000271 VectorType *VTy = cast<VectorType>(Ty);
Chris Lattnere9eed292012-01-25 05:19:54 +0000272 return ConstantVector::getSplat(VTy->getNumElements(),
273 getAllOnesValue(VTy->getElementType()));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000274}
275
Chris Lattner7e683d12012-01-25 06:16:32 +0000276Constant *Constant::getAggregateElement(unsigned Elt) const {
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +0000277 if (const ConstantAggregate *CC = dyn_cast<ConstantAggregate>(this))
278 return Elt < CC->getNumOperands() ? CC->getOperand(Elt) : nullptr;
Galina Kistanovafc259902012-07-13 01:25:27 +0000279
David Majnemer9b529a72015-02-16 04:02:09 +0000280 if (const ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(this))
281 return Elt < CAZ->getNumElements() ? CAZ->getElementValue(Elt) : nullptr;
Galina Kistanovafc259902012-07-13 01:25:27 +0000282
Chris Lattner7e683d12012-01-25 06:16:32 +0000283 if (const UndefValue *UV = dyn_cast<UndefValue>(this))
David Majnemer9b529a72015-02-16 04:02:09 +0000284 return Elt < UV->getNumElements() ? UV->getElementValue(Elt) : nullptr;
Galina Kistanovafc259902012-07-13 01:25:27 +0000285
Chris Lattner8326bd82012-01-26 00:42:34 +0000286 if (const ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(this))
Craig Topperc6207612014-04-09 06:08:46 +0000287 return Elt < CDS->getNumElements() ? CDS->getElementAsConstant(Elt)
288 : nullptr;
289 return nullptr;
Chris Lattner7e683d12012-01-25 06:16:32 +0000290}
291
292Constant *Constant::getAggregateElement(Constant *Elt) const {
293 assert(isa<IntegerType>(Elt->getType()) && "Index must be an integer");
294 if (ConstantInt *CI = dyn_cast<ConstantInt>(Elt))
295 return getAggregateElement(CI->getZExtValue());
Craig Topperc6207612014-04-09 06:08:46 +0000296 return nullptr;
Chris Lattner7e683d12012-01-25 06:16:32 +0000297}
298
Pete Cooper86dd4cf2015-06-23 21:55:11 +0000299void Constant::destroyConstant() {
300 /// First call destroyConstantImpl on the subclass. This gives the subclass
301 /// a chance to remove the constant from any maps/pools it's contained in.
302 switch (getValueID()) {
303 default:
304 llvm_unreachable("Not a constant!");
305#define HANDLE_CONSTANT(Name) \
306 case Value::Name##Val: \
307 cast<Name>(this)->destroyConstantImpl(); \
308 break;
309#include "llvm/IR/Value.def"
310 }
Chris Lattner7e683d12012-01-25 06:16:32 +0000311
Chris Lattner3462ae32001-12-03 22:26:30 +0000312 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +0000313 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +0000314 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +0000315 // but they don't know that. Because we only find out when the CPV is
316 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +0000317 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +0000318 //
319 while (!use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000320 Value *V = user_back();
Pete Cooper86dd4cf2015-06-23 21:55:11 +0000321#ifndef NDEBUG // Only in -g mode...
Chris Lattner78683a72009-08-23 04:02:03 +0000322 if (!isa<Constant>(V)) {
David Greene1e27a132010-01-05 01:29:19 +0000323 dbgs() << "While deleting: " << *this
Pete Cooper86dd4cf2015-06-23 21:55:11 +0000324 << "\n\nUse still stuck around after Def is destroyed: " << *V
325 << "\n\n";
Chris Lattner78683a72009-08-23 04:02:03 +0000326 }
Chris Lattnerd7a73302001-10-13 06:57:33 +0000327#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000328 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Chris Lattner8326bd82012-01-26 00:42:34 +0000329 cast<Constant>(V)->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +0000330
331 // The constant should remove itself from our use list...
Chandler Carruthcdf47882014-03-09 03:16:01 +0000332 assert((use_empty() || user_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +0000333 }
334
335 // Value has no outstanding references it is safe to delete it now...
336 delete this;
Chris Lattner38569342001-10-01 20:11:19 +0000337}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000338
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000339static bool canTrapImpl(const Constant *C,
Craig Topper71b7b682014-08-21 05:55:13 +0000340 SmallPtrSetImpl<const ConstantExpr *> &NonTrappingOps) {
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000341 assert(C->getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
Chris Lattner23dd1f62006-10-20 00:27:06 +0000342 // The only thing that could possibly trap are constant exprs.
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000343 const ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
344 if (!CE)
345 return false;
Galina Kistanovafc259902012-07-13 01:25:27 +0000346
347 // ConstantExpr traps if any operands can trap.
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000348 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
349 if (ConstantExpr *Op = dyn_cast<ConstantExpr>(CE->getOperand(i))) {
David Blaikie70573dc2014-11-19 07:49:26 +0000350 if (NonTrappingOps.insert(Op).second && canTrapImpl(Op, NonTrappingOps))
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000351 return true;
352 }
353 }
Chris Lattner23dd1f62006-10-20 00:27:06 +0000354
355 // Otherwise, only specific operations can trap.
356 switch (CE->getOpcode()) {
357 default:
358 return false;
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000359 case Instruction::UDiv:
360 case Instruction::SDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +0000361 case Instruction::URem:
362 case Instruction::SRem:
Chris Lattner23dd1f62006-10-20 00:27:06 +0000363 // Div and rem can trap if the RHS is not known to be non-zero.
Chris Lattnera91a5632009-10-28 05:14:34 +0000364 if (!isa<ConstantInt>(CE->getOperand(1)) ||CE->getOperand(1)->isNullValue())
Chris Lattner23dd1f62006-10-20 00:27:06 +0000365 return true;
366 return false;
367 }
368}
369
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000370bool Constant::canTrap() const {
371 SmallPtrSet<const ConstantExpr *, 4> NonTrappingOps;
372 return canTrapImpl(this, NonTrappingOps);
373}
374
Hans Wennborg4dc89512014-06-20 00:38:12 +0000375/// Check if C contains a GlobalValue for which Predicate is true.
376static bool
377ConstHasGlobalValuePredicate(const Constant *C,
378 bool (*Predicate)(const GlobalValue *)) {
379 SmallPtrSet<const Constant *, 8> Visited;
380 SmallVector<const Constant *, 8> WorkList;
381 WorkList.push_back(C);
382 Visited.insert(C);
Hans Wennborg709e0152012-11-15 11:40:00 +0000383
384 while (!WorkList.empty()) {
Hans Wennborg4dc89512014-06-20 00:38:12 +0000385 const Constant *WorkItem = WorkList.pop_back_val();
386 if (const auto *GV = dyn_cast<GlobalValue>(WorkItem))
387 if (Predicate(GV))
Hans Wennborg709e0152012-11-15 11:40:00 +0000388 return true;
Hans Wennborg4dc89512014-06-20 00:38:12 +0000389 for (const Value *Op : WorkItem->operands()) {
390 const Constant *ConstOp = dyn_cast<Constant>(Op);
391 if (!ConstOp)
Hans Wennborg18aa12402012-11-16 10:33:25 +0000392 continue;
David Blaikie70573dc2014-11-19 07:49:26 +0000393 if (Visited.insert(ConstOp).second)
Hans Wennborg4dc89512014-06-20 00:38:12 +0000394 WorkList.push_back(ConstOp);
Hans Wennborg709e0152012-11-15 11:40:00 +0000395 }
396 }
Hans Wennborg709e0152012-11-15 11:40:00 +0000397 return false;
398}
399
Hans Wennborg4dc89512014-06-20 00:38:12 +0000400bool Constant::isThreadDependent() const {
401 auto DLLImportPredicate = [](const GlobalValue *GV) {
402 return GV->isThreadLocal();
403 };
404 return ConstHasGlobalValuePredicate(this, DLLImportPredicate);
405}
406
407bool Constant::isDLLImportDependent() const {
408 auto DLLImportPredicate = [](const GlobalValue *GV) {
409 return GV->hasDLLImportStorageClass();
410 };
411 return ConstHasGlobalValuePredicate(this, DLLImportPredicate);
412}
413
Chris Lattner253bc772009-11-01 18:11:50 +0000414bool Constant::isConstantUsed() const {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000415 for (const User *U : users()) {
416 const Constant *UC = dyn_cast<Constant>(U);
Craig Topperc6207612014-04-09 06:08:46 +0000417 if (!UC || isa<GlobalValue>(UC))
Chris Lattner253bc772009-11-01 18:11:50 +0000418 return true;
Galina Kistanovafc259902012-07-13 01:25:27 +0000419
Chris Lattner253bc772009-11-01 18:11:50 +0000420 if (UC->isConstantUsed())
421 return true;
422 }
423 return false;
424}
425
Rafael Espindola65e49022015-11-17 00:51:23 +0000426bool Constant::needsRelocation() const {
427 if (isa<GlobalValue>(this))
428 return true; // Global reference.
429
Chris Lattner2cb85b42009-10-28 04:12:16 +0000430 if (const BlockAddress *BA = dyn_cast<BlockAddress>(this))
Rafael Espindola65e49022015-11-17 00:51:23 +0000431 return BA->getFunction()->needsRelocation();
432
Chris Lattnera7cfc432010-01-03 18:09:40 +0000433 // While raw uses of blockaddress need to be relocated, differences between
434 // two of them don't when they are for labels in the same function. This is a
435 // common idiom when creating a table for the indirect goto extension, so we
436 // handle it efficiently here.
437 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(this))
438 if (CE->getOpcode() == Instruction::Sub) {
439 ConstantExpr *LHS = dyn_cast<ConstantExpr>(CE->getOperand(0));
440 ConstantExpr *RHS = dyn_cast<ConstantExpr>(CE->getOperand(1));
Rafael Espindola65e49022015-11-17 00:51:23 +0000441 if (LHS && RHS && LHS->getOpcode() == Instruction::PtrToInt &&
Chris Lattnera7cfc432010-01-03 18:09:40 +0000442 RHS->getOpcode() == Instruction::PtrToInt &&
443 isa<BlockAddress>(LHS->getOperand(0)) &&
444 isa<BlockAddress>(RHS->getOperand(0)) &&
445 cast<BlockAddress>(LHS->getOperand(0))->getFunction() ==
Rafael Espindola65e49022015-11-17 00:51:23 +0000446 cast<BlockAddress>(RHS->getOperand(0))->getFunction())
447 return false;
Chris Lattnera7cfc432010-01-03 18:09:40 +0000448 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000449
Rafael Espindola65e49022015-11-17 00:51:23 +0000450 bool Result = false;
Evan Chengf9e003b2007-03-08 00:59:12 +0000451 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Rafael Espindola65e49022015-11-17 00:51:23 +0000452 Result |= cast<Constant>(getOperand(i))->needsRelocation();
Galina Kistanovafc259902012-07-13 01:25:27 +0000453
Chris Lattner4565ef52009-07-22 00:05:44 +0000454 return Result;
Evan Chengf9e003b2007-03-08 00:59:12 +0000455}
456
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +0000457/// If the specified constantexpr is dead, remove it. This involves recursively
458/// eliminating any dead users of the constantexpr.
Chris Lattner84886402011-02-18 04:41:42 +0000459static bool removeDeadUsersOfConstant(const Constant *C) {
460 if (isa<GlobalValue>(C)) return false; // Cannot remove this
Galina Kistanovafc259902012-07-13 01:25:27 +0000461
Chris Lattner84886402011-02-18 04:41:42 +0000462 while (!C->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000463 const Constant *User = dyn_cast<Constant>(C->user_back());
Chris Lattner84886402011-02-18 04:41:42 +0000464 if (!User) return false; // Non-constant usage;
465 if (!removeDeadUsersOfConstant(User))
466 return false; // Constant wasn't dead
467 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000468
Chris Lattner84886402011-02-18 04:41:42 +0000469 const_cast<Constant*>(C)->destroyConstant();
470 return true;
471}
472
473
Chris Lattner84886402011-02-18 04:41:42 +0000474void Constant::removeDeadConstantUsers() const {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000475 Value::const_user_iterator I = user_begin(), E = user_end();
476 Value::const_user_iterator LastNonDeadUser = E;
Chris Lattner84886402011-02-18 04:41:42 +0000477 while (I != E) {
478 const Constant *User = dyn_cast<Constant>(*I);
Craig Topperc6207612014-04-09 06:08:46 +0000479 if (!User) {
Chris Lattner84886402011-02-18 04:41:42 +0000480 LastNonDeadUser = I;
481 ++I;
482 continue;
483 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000484
Chris Lattner84886402011-02-18 04:41:42 +0000485 if (!removeDeadUsersOfConstant(User)) {
486 // If the constant wasn't dead, remember that this was the last live use
487 // and move on to the next constant.
488 LastNonDeadUser = I;
489 ++I;
490 continue;
491 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000492
Chris Lattner84886402011-02-18 04:41:42 +0000493 // If the constant was dead, then the iterator is invalidated.
494 if (LastNonDeadUser == E) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000495 I = user_begin();
Chris Lattner84886402011-02-18 04:41:42 +0000496 if (I == E) break;
497 } else {
498 I = LastNonDeadUser;
499 ++I;
500 }
501 }
502}
503
504
Chris Lattner2105d662008-07-10 00:28:11 +0000505
Chris Lattner2f7c9632001-06-06 20:29:01 +0000506//===----------------------------------------------------------------------===//
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000507// ConstantInt
Chris Lattner2f7c9632001-06-06 20:29:01 +0000508//===----------------------------------------------------------------------===//
509
Duncan P. N. Exon Smithb6452792016-02-21 02:39:49 +0000510ConstantInt::ConstantInt(IntegerType *Ty, const APInt &V)
511 : ConstantData(Ty, ConstantIntVal), Val(V) {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000512 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000513}
514
Nick Lewycky92db8e82011-03-06 03:36:19 +0000515ConstantInt *ConstantInt::getTrue(LLVMContext &Context) {
Owen Anderson23a204d2009-07-31 17:39:07 +0000516 LLVMContextImpl *pImpl = Context.pImpl;
Benjamin Kramerddd1b7b2010-11-20 18:43:35 +0000517 if (!pImpl->TheTrueVal)
518 pImpl->TheTrueVal = ConstantInt::get(Type::getInt1Ty(Context), 1);
519 return pImpl->TheTrueVal;
Owen Anderson23a204d2009-07-31 17:39:07 +0000520}
521
Nick Lewycky92db8e82011-03-06 03:36:19 +0000522ConstantInt *ConstantInt::getFalse(LLVMContext &Context) {
Owen Anderson23a204d2009-07-31 17:39:07 +0000523 LLVMContextImpl *pImpl = Context.pImpl;
Benjamin Kramerddd1b7b2010-11-20 18:43:35 +0000524 if (!pImpl->TheFalseVal)
525 pImpl->TheFalseVal = ConstantInt::get(Type::getInt1Ty(Context), 0);
526 return pImpl->TheFalseVal;
Owen Anderson23a204d2009-07-31 17:39:07 +0000527}
528
Chris Lattner229907c2011-07-18 04:54:35 +0000529Constant *ConstantInt::getTrue(Type *Ty) {
Craig Topperfde47232017-07-09 07:04:03 +0000530 assert(Ty->isIntOrIntVectorTy(1) && "Type not i1 or vector of i1.");
Sanjay Patel70a575a2017-04-16 17:00:21 +0000531 ConstantInt *TrueC = ConstantInt::getTrue(Ty->getContext());
532 if (auto *VTy = dyn_cast<VectorType>(Ty))
533 return ConstantVector::getSplat(VTy->getNumElements(), TrueC);
534 return TrueC;
Nick Lewycky92db8e82011-03-06 03:36:19 +0000535}
536
Chris Lattner229907c2011-07-18 04:54:35 +0000537Constant *ConstantInt::getFalse(Type *Ty) {
Craig Topperfde47232017-07-09 07:04:03 +0000538 assert(Ty->isIntOrIntVectorTy(1) && "Type not i1 or vector of i1.");
Sanjay Patel70a575a2017-04-16 17:00:21 +0000539 ConstantInt *FalseC = ConstantInt::getFalse(Ty->getContext());
540 if (auto *VTy = dyn_cast<VectorType>(Ty))
541 return ConstantVector::getSplat(VTy->getNumElements(), FalseC);
542 return FalseC;
Nick Lewycky92db8e82011-03-06 03:36:19 +0000543}
544
Benjamin Kramer8e5dc532014-12-06 13:12:56 +0000545// Get a ConstantInt from an APInt.
Nick Lewycky92db8e82011-03-06 03:36:19 +0000546ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt &V) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000547 // get an existing value or the insertion position
Benjamin Kramer320682f2013-06-01 17:51:03 +0000548 LLVMContextImpl *pImpl = Context.pImpl;
Justin Lebar611c5c22016-10-10 16:26:13 +0000549 std::unique_ptr<ConstantInt> &Slot = pImpl->IntConstants[V];
Benjamin Kramer8e5dc532014-12-06 13:12:56 +0000550 if (!Slot) {
551 // Get the corresponding integer type for the bit width of the value.
552 IntegerType *ITy = IntegerType::get(Context, V.getBitWidth());
Justin Lebar611c5c22016-10-10 16:26:13 +0000553 Slot.reset(new ConstantInt(ITy, V));
Benjamin Kramer8e5dc532014-12-06 13:12:56 +0000554 }
555 assert(Slot->getType() == IntegerType::get(Context, V.getBitWidth()));
Justin Lebar611c5c22016-10-10 16:26:13 +0000556 return Slot.get();
Owen Andersonedb4a702009-07-24 23:12:02 +0000557}
558
Chris Lattner229907c2011-07-18 04:54:35 +0000559Constant *ConstantInt::get(Type *Ty, uint64_t V, bool isSigned) {
Nick Lewycky92db8e82011-03-06 03:36:19 +0000560 Constant *C = get(cast<IntegerType>(Ty->getScalarType()), V, isSigned);
Owen Andersonedb4a702009-07-24 23:12:02 +0000561
562 // For vectors, broadcast the value.
Chris Lattner229907c2011-07-18 04:54:35 +0000563 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
Chris Lattnere9eed292012-01-25 05:19:54 +0000564 return ConstantVector::getSplat(VTy->getNumElements(), C);
Owen Andersonedb4a702009-07-24 23:12:02 +0000565
566 return C;
567}
568
Sanjay Patelf3587ec2016-05-18 22:05:28 +0000569ConstantInt *ConstantInt::get(IntegerType *Ty, uint64_t V, bool isSigned) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000570 return get(Ty->getContext(), APInt(Ty->getBitWidth(), V, isSigned));
571}
572
Chris Lattner0256be92012-01-27 03:08:05 +0000573ConstantInt *ConstantInt::getSigned(IntegerType *Ty, int64_t V) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000574 return get(Ty, V, true);
575}
576
Chris Lattner229907c2011-07-18 04:54:35 +0000577Constant *ConstantInt::getSigned(Type *Ty, int64_t V) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000578 return get(Ty, V, true);
579}
580
Chris Lattner0256be92012-01-27 03:08:05 +0000581Constant *ConstantInt::get(Type *Ty, const APInt& V) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000582 ConstantInt *C = get(Ty->getContext(), V);
583 assert(C->getType() == Ty->getScalarType() &&
584 "ConstantInt type doesn't match the type implied by its value!");
585
586 // For vectors, broadcast the value.
Chris Lattner229907c2011-07-18 04:54:35 +0000587 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
Chris Lattnere9eed292012-01-25 05:19:54 +0000588 return ConstantVector::getSplat(VTy->getNumElements(), C);
Owen Andersonedb4a702009-07-24 23:12:02 +0000589
590 return C;
591}
592
Sanjay Patelf3587ec2016-05-18 22:05:28 +0000593ConstantInt *ConstantInt::get(IntegerType* Ty, StringRef Str, uint8_t radix) {
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000594 return get(Ty->getContext(), APInt(Ty->getBitWidth(), Str, radix));
595}
596
Pete Cooper86dd4cf2015-06-23 21:55:11 +0000597/// Remove the constant from the constant table.
598void ConstantInt::destroyConstantImpl() {
599 llvm_unreachable("You can't ConstantInt->destroyConstantImpl()!");
600}
601
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000602//===----------------------------------------------------------------------===//
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000603// ConstantFP
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000604//===----------------------------------------------------------------------===//
605
Chris Lattner229907c2011-07-18 04:54:35 +0000606static const fltSemantics *TypeToFloatSemantics(Type *Ty) {
Dan Gohman518cda42011-12-17 00:04:22 +0000607 if (Ty->isHalfTy())
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000608 return &APFloat::IEEEhalf();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000609 if (Ty->isFloatTy())
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000610 return &APFloat::IEEEsingle();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000611 if (Ty->isDoubleTy())
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000612 return &APFloat::IEEEdouble();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000613 if (Ty->isX86_FP80Ty())
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000614 return &APFloat::x87DoubleExtended();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000615 else if (Ty->isFP128Ty())
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000616 return &APFloat::IEEEquad();
Galina Kistanovafc259902012-07-13 01:25:27 +0000617
Chris Lattnerfdd87902009-10-05 05:54:46 +0000618 assert(Ty->isPPC_FP128Ty() && "Unknown FP format");
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000619 return &APFloat::PPCDoubleDouble();
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000620}
621
Chris Lattner0256be92012-01-27 03:08:05 +0000622Constant *ConstantFP::get(Type *Ty, double V) {
Owen Anderson69c464d2009-07-27 20:59:43 +0000623 LLVMContext &Context = Ty->getContext();
Galina Kistanovafc259902012-07-13 01:25:27 +0000624
Owen Anderson69c464d2009-07-27 20:59:43 +0000625 APFloat FV(V);
626 bool ignored;
627 FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
628 APFloat::rmNearestTiesToEven, &ignored);
629 Constant *C = get(Context, FV);
630
631 // For vectors, broadcast the value.
Chris Lattner229907c2011-07-18 04:54:35 +0000632 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
Chris Lattnere9eed292012-01-25 05:19:54 +0000633 return ConstantVector::getSplat(VTy->getNumElements(), C);
Owen Anderson69c464d2009-07-27 20:59:43 +0000634
635 return C;
636}
637
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000638
Chris Lattner0256be92012-01-27 03:08:05 +0000639Constant *ConstantFP::get(Type *Ty, StringRef Str) {
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000640 LLVMContext &Context = Ty->getContext();
641
642 APFloat FV(*TypeToFloatSemantics(Ty->getScalarType()), Str);
643 Constant *C = get(Context, FV);
644
645 // For vectors, broadcast the value.
Chris Lattner229907c2011-07-18 04:54:35 +0000646 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
Chris Lattnere9eed292012-01-25 05:19:54 +0000647 return ConstantVector::getSplat(VTy->getNumElements(), C);
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000648
649 return C;
650}
651
Tom Stellard67246d12015-04-20 19:38:24 +0000652Constant *ConstantFP::getNaN(Type *Ty, bool Negative, unsigned Type) {
653 const fltSemantics &Semantics = *TypeToFloatSemantics(Ty->getScalarType());
654 APFloat NaN = APFloat::getNaN(Semantics, Negative, Type);
655 Constant *C = get(Ty->getContext(), NaN);
656
657 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
658 return ConstantVector::getSplat(VTy->getNumElements(), C);
659
660 return C;
661}
662
Benjamin Kramer5d2ff222014-01-18 16:43:06 +0000663Constant *ConstantFP::getNegativeZero(Type *Ty) {
664 const fltSemantics &Semantics = *TypeToFloatSemantics(Ty->getScalarType());
665 APFloat NegZero = APFloat::getZero(Semantics, /*Negative=*/true);
666 Constant *C = get(Ty->getContext(), NegZero);
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000667
Benjamin Kramer5d2ff222014-01-18 16:43:06 +0000668 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
669 return ConstantVector::getSplat(VTy->getNumElements(), C);
670
671 return C;
Owen Anderson69c464d2009-07-27 20:59:43 +0000672}
673
674
Chris Lattnere9eed292012-01-25 05:19:54 +0000675Constant *ConstantFP::getZeroValueForNegation(Type *Ty) {
Benjamin Kramer5d2ff222014-01-18 16:43:06 +0000676 if (Ty->isFPOrFPVectorTy())
677 return getNegativeZero(Ty);
Owen Anderson69c464d2009-07-27 20:59:43 +0000678
Owen Anderson5a1acd92009-07-31 20:28:14 +0000679 return Constant::getNullValue(Ty);
Owen Anderson69c464d2009-07-27 20:59:43 +0000680}
681
682
683// ConstantFP accessors.
684ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) {
Owen Anderson69c464d2009-07-27 20:59:43 +0000685 LLVMContextImpl* pImpl = Context.pImpl;
Galina Kistanovafc259902012-07-13 01:25:27 +0000686
Justin Lebar611c5c22016-10-10 16:26:13 +0000687 std::unique_ptr<ConstantFP> &Slot = pImpl->FPConstants[V];
Galina Kistanovafc259902012-07-13 01:25:27 +0000688
Owen Anderson69c464d2009-07-27 20:59:43 +0000689 if (!Slot) {
Chris Lattner229907c2011-07-18 04:54:35 +0000690 Type *Ty;
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000691 if (&V.getSemantics() == &APFloat::IEEEhalf())
Dan Gohman518cda42011-12-17 00:04:22 +0000692 Ty = Type::getHalfTy(Context);
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000693 else if (&V.getSemantics() == &APFloat::IEEEsingle())
Owen Anderson5dab84c2009-10-19 20:11:52 +0000694 Ty = Type::getFloatTy(Context);
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000695 else if (&V.getSemantics() == &APFloat::IEEEdouble())
Owen Anderson5dab84c2009-10-19 20:11:52 +0000696 Ty = Type::getDoubleTy(Context);
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000697 else if (&V.getSemantics() == &APFloat::x87DoubleExtended())
Owen Anderson5dab84c2009-10-19 20:11:52 +0000698 Ty = Type::getX86_FP80Ty(Context);
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000699 else if (&V.getSemantics() == &APFloat::IEEEquad())
Owen Anderson5dab84c2009-10-19 20:11:52 +0000700 Ty = Type::getFP128Ty(Context);
701 else {
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000702 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble() &&
Owen Anderson5dab84c2009-10-19 20:11:52 +0000703 "Unknown FP format");
704 Ty = Type::getPPC_FP128Ty(Context);
Owen Anderson69c464d2009-07-27 20:59:43 +0000705 }
Justin Lebar611c5c22016-10-10 16:26:13 +0000706 Slot.reset(new ConstantFP(Ty, V));
Owen Anderson69c464d2009-07-27 20:59:43 +0000707 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000708
Justin Lebar611c5c22016-10-10 16:26:13 +0000709 return Slot.get();
Owen Anderson69c464d2009-07-27 20:59:43 +0000710}
711
Benjamin Kramer5d2ff222014-01-18 16:43:06 +0000712Constant *ConstantFP::getInfinity(Type *Ty, bool Negative) {
713 const fltSemantics &Semantics = *TypeToFloatSemantics(Ty->getScalarType());
714 Constant *C = get(Ty->getContext(), APFloat::getInf(Semantics, Negative));
715
716 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
717 return ConstantVector::getSplat(VTy->getNumElements(), C);
718
719 return C;
Dan Gohmanfeb50212009-09-25 23:00:48 +0000720}
721
Duncan P. N. Exon Smithb6452792016-02-21 02:39:49 +0000722ConstantFP::ConstantFP(Type *Ty, const APFloat &V)
723 : ConstantData(Ty, ConstantFPVal), Val(V) {
Chris Lattner98bd9392008-04-09 06:38:30 +0000724 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
725 "FP type Mismatch");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000726}
727
Chris Lattnerbe6610c2011-07-15 06:14:08 +0000728bool ConstantFP::isExactlyValue(const APFloat &V) const {
Dale Johannesend246b2c2007-08-30 00:23:21 +0000729 return Val.bitwiseIsEqual(V);
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000730}
731
Pete Cooper86dd4cf2015-06-23 21:55:11 +0000732/// Remove the constant from the constant table.
733void ConstantFP::destroyConstantImpl() {
Craig Topperccbb8102017-06-27 19:57:51 +0000734 llvm_unreachable("You can't ConstantFP->destroyConstantImpl()!");
Pete Cooper86dd4cf2015-06-23 21:55:11 +0000735}
736
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000737//===----------------------------------------------------------------------===//
Chris Lattner030af792012-01-24 05:42:11 +0000738// ConstantAggregateZero Implementation
739//===----------------------------------------------------------------------===//
740
Chris Lattner7e683d12012-01-25 06:16:32 +0000741Constant *ConstantAggregateZero::getSequentialElement() const {
Chris Lattner8326bd82012-01-26 00:42:34 +0000742 return Constant::getNullValue(getType()->getSequentialElementType());
Chris Lattner030af792012-01-24 05:42:11 +0000743}
744
Chris Lattner7e683d12012-01-25 06:16:32 +0000745Constant *ConstantAggregateZero::getStructElement(unsigned Elt) const {
Chris Lattner8326bd82012-01-26 00:42:34 +0000746 return Constant::getNullValue(getType()->getStructElementType(Elt));
Chris Lattner030af792012-01-24 05:42:11 +0000747}
748
Chris Lattner7e683d12012-01-25 06:16:32 +0000749Constant *ConstantAggregateZero::getElementValue(Constant *C) const {
Chris Lattner030af792012-01-24 05:42:11 +0000750 if (isa<SequentialType>(getType()))
751 return getSequentialElement();
752 return getStructElement(cast<ConstantInt>(C)->getZExtValue());
753}
754
Chris Lattner7e683d12012-01-25 06:16:32 +0000755Constant *ConstantAggregateZero::getElementValue(unsigned Idx) const {
Chris Lattnerf7eb5432012-01-24 07:54:10 +0000756 if (isa<SequentialType>(getType()))
757 return getSequentialElement();
758 return getStructElement(Idx);
759}
760
David Majnemer9b529a72015-02-16 04:02:09 +0000761unsigned ConstantAggregateZero::getNumElements() const {
Craig Toppere3dcce92015-08-01 22:20:21 +0000762 Type *Ty = getType();
763 if (auto *AT = dyn_cast<ArrayType>(Ty))
David Majnemer9b529a72015-02-16 04:02:09 +0000764 return AT->getNumElements();
Craig Toppere3dcce92015-08-01 22:20:21 +0000765 if (auto *VT = dyn_cast<VectorType>(Ty))
David Majnemer9b529a72015-02-16 04:02:09 +0000766 return VT->getNumElements();
767 return Ty->getStructNumElements();
768}
Chris Lattnerf7eb5432012-01-24 07:54:10 +0000769
Chris Lattner030af792012-01-24 05:42:11 +0000770//===----------------------------------------------------------------------===//
771// UndefValue Implementation
772//===----------------------------------------------------------------------===//
773
Chris Lattner7e683d12012-01-25 06:16:32 +0000774UndefValue *UndefValue::getSequentialElement() const {
Chris Lattner8326bd82012-01-26 00:42:34 +0000775 return UndefValue::get(getType()->getSequentialElementType());
Chris Lattner030af792012-01-24 05:42:11 +0000776}
777
Chris Lattner7e683d12012-01-25 06:16:32 +0000778UndefValue *UndefValue::getStructElement(unsigned Elt) const {
Chris Lattner8326bd82012-01-26 00:42:34 +0000779 return UndefValue::get(getType()->getStructElementType(Elt));
Chris Lattner030af792012-01-24 05:42:11 +0000780}
781
Chris Lattner7e683d12012-01-25 06:16:32 +0000782UndefValue *UndefValue::getElementValue(Constant *C) const {
Chris Lattner030af792012-01-24 05:42:11 +0000783 if (isa<SequentialType>(getType()))
784 return getSequentialElement();
785 return getStructElement(cast<ConstantInt>(C)->getZExtValue());
786}
787
Chris Lattner7e683d12012-01-25 06:16:32 +0000788UndefValue *UndefValue::getElementValue(unsigned Idx) const {
Chris Lattnerf7eb5432012-01-24 07:54:10 +0000789 if (isa<SequentialType>(getType()))
790 return getSequentialElement();
791 return getStructElement(Idx);
792}
793
David Majnemer9b529a72015-02-16 04:02:09 +0000794unsigned UndefValue::getNumElements() const {
Craig Toppere3dcce92015-08-01 22:20:21 +0000795 Type *Ty = getType();
Peter Collingbournebc070522016-12-02 03:20:58 +0000796 if (auto *ST = dyn_cast<SequentialType>(Ty))
797 return ST->getNumElements();
David Majnemer9b529a72015-02-16 04:02:09 +0000798 return Ty->getStructNumElements();
799}
Chris Lattner030af792012-01-24 05:42:11 +0000800
801//===----------------------------------------------------------------------===//
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000802// ConstantXXX Classes
803//===----------------------------------------------------------------------===//
804
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000805template <typename ItTy, typename EltTy>
806static bool rangeOnlyContains(ItTy Start, ItTy End, EltTy Elt) {
807 for (; Start != End; ++Start)
808 if (*Start != Elt)
809 return false;
810 return true;
811}
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000812
Justin Bogner909e1c02015-12-01 20:20:49 +0000813template <typename SequentialTy, typename ElementTy>
814static Constant *getIntSequenceIfElementsMatch(ArrayRef<Constant *> V) {
815 assert(!V.empty() && "Cannot get empty int sequence.");
816
817 SmallVector<ElementTy, 16> Elts;
818 for (Constant *C : V)
819 if (auto *CI = dyn_cast<ConstantInt>(C))
820 Elts.push_back(CI->getZExtValue());
821 else
822 return nullptr;
823 return SequentialTy::get(V[0]->getContext(), Elts);
824}
825
826template <typename SequentialTy, typename ElementTy>
827static Constant *getFPSequenceIfElementsMatch(ArrayRef<Constant *> V) {
828 assert(!V.empty() && "Cannot get empty FP sequence.");
829
830 SmallVector<ElementTy, 16> Elts;
831 for (Constant *C : V)
832 if (auto *CFP = dyn_cast<ConstantFP>(C))
833 Elts.push_back(CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
834 else
835 return nullptr;
836 return SequentialTy::getFP(V[0]->getContext(), Elts);
837}
838
839template <typename SequenceTy>
840static Constant *getSequenceIfElementsMatch(Constant *C,
841 ArrayRef<Constant *> V) {
842 // We speculatively build the elements here even if it turns out that there is
843 // a constantexpr or something else weird, since it is so uncommon for that to
844 // happen.
845 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
846 if (CI->getType()->isIntegerTy(8))
847 return getIntSequenceIfElementsMatch<SequenceTy, uint8_t>(V);
848 else if (CI->getType()->isIntegerTy(16))
849 return getIntSequenceIfElementsMatch<SequenceTy, uint16_t>(V);
850 else if (CI->getType()->isIntegerTy(32))
851 return getIntSequenceIfElementsMatch<SequenceTy, uint32_t>(V);
852 else if (CI->getType()->isIntegerTy(64))
853 return getIntSequenceIfElementsMatch<SequenceTy, uint64_t>(V);
854 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
Justin Bogner0ebc8602015-12-08 03:01:16 +0000855 if (CFP->getType()->isHalfTy())
856 return getFPSequenceIfElementsMatch<SequenceTy, uint16_t>(V);
857 else if (CFP->getType()->isFloatTy())
Justin Bogner909e1c02015-12-01 20:20:49 +0000858 return getFPSequenceIfElementsMatch<SequenceTy, uint32_t>(V);
859 else if (CFP->getType()->isDoubleTy())
860 return getFPSequenceIfElementsMatch<SequenceTy, uint64_t>(V);
861 }
862
863 return nullptr;
864}
865
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +0000866ConstantAggregate::ConstantAggregate(CompositeType *T, ValueTy VT,
867 ArrayRef<Constant *> V)
868 : Constant(T, VT, OperandTraits<ConstantAggregate>::op_end(this) - V.size(),
869 V.size()) {
Jay Foad89d9b812011-07-25 10:14:44 +0000870 std::copy(V.begin(), V.end(), op_begin());
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +0000871
872 // Check that types match, unless this is an opaque struct.
873 if (auto *ST = dyn_cast<StructType>(T))
874 if (ST->isOpaque())
875 return;
876 for (unsigned I = 0, E = V.size(); I != E; ++I)
877 assert(V[I]->getType() == T->getTypeAtIndex(I) &&
878 "Initializer for composite element doesn't match!");
879}
880
881ConstantArray::ConstantArray(ArrayType *T, ArrayRef<Constant *> V)
882 : ConstantAggregate(T, ConstantArrayVal, V) {
883 assert(V.size() == T->getNumElements() &&
884 "Invalid initializer for constant array");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000885}
886
Chris Lattner229907c2011-07-18 04:54:35 +0000887Constant *ConstantArray::get(ArrayType *Ty, ArrayRef<Constant*> V) {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000888 if (Constant *C = getImpl(Ty, V))
889 return C;
890 return Ty->getContext().pImpl->ArrayConstants.getOrCreate(Ty, V);
891}
Justin Bogner909e1c02015-12-01 20:20:49 +0000892
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000893Constant *ConstantArray::getImpl(ArrayType *Ty, ArrayRef<Constant*> V) {
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000894 // Empty arrays are canonicalized to ConstantAggregateZero.
895 if (V.empty())
896 return ConstantAggregateZero::get(Ty);
897
Jeffrey Yasskin8ce67f82009-09-30 21:08:08 +0000898 for (unsigned i = 0, e = V.size(); i != e; ++i) {
899 assert(V[i]->getType() == Ty->getElementType() &&
900 "Wrong type in array element initializer");
901 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000902
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000903 // If this is an all-zero array, return a ConstantAggregateZero object. If
904 // all undef, return an UndefValue, if "all simple", then return a
905 // ConstantDataArray.
906 Constant *C = V[0];
907 if (isa<UndefValue>(C) && rangeOnlyContains(V.begin(), V.end(), C))
908 return UndefValue::get(Ty);
Chris Lattnerf14a67f2012-01-26 02:31:22 +0000909
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000910 if (C->isNullValue() && rangeOnlyContains(V.begin(), V.end(), C))
911 return ConstantAggregateZero::get(Ty);
912
913 // Check to see if all of the elements are ConstantFP or ConstantInt and if
914 // the element type is compatible with ConstantDataVector. If so, use it.
Justin Bogner909e1c02015-12-01 20:20:49 +0000915 if (ConstantDataSequential::isElementTypeCompatible(C->getType()))
916 return getSequenceIfElementsMatch<ConstantDataArray>(C, V);
Chris Lattnerf14a67f2012-01-26 02:31:22 +0000917
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000918 // Otherwise, we really do want to create a ConstantArray.
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000919 return nullptr;
Owen Andersonc2c79322009-07-28 18:32:17 +0000920}
921
Chris Lattnercc19efa2011-06-20 04:01:31 +0000922StructType *ConstantStruct::getTypeForElements(LLVMContext &Context,
923 ArrayRef<Constant*> V,
924 bool Packed) {
Bill Wendling3ae7dd32012-02-07 01:27:51 +0000925 unsigned VecSize = V.size();
926 SmallVector<Type*, 16> EltTypes(VecSize);
927 for (unsigned i = 0; i != VecSize; ++i)
928 EltTypes[i] = V[i]->getType();
Galina Kistanovafc259902012-07-13 01:25:27 +0000929
Chris Lattnercc19efa2011-06-20 04:01:31 +0000930 return StructType::get(Context, EltTypes, Packed);
931}
932
933
934StructType *ConstantStruct::getTypeForElements(ArrayRef<Constant*> V,
935 bool Packed) {
936 assert(!V.empty() &&
937 "ConstantStruct::getTypeForElements cannot be called on empty list");
938 return getTypeForElements(V[0]->getContext(), V, Packed);
939}
940
Jay Foad89d9b812011-07-25 10:14:44 +0000941ConstantStruct::ConstantStruct(StructType *T, ArrayRef<Constant *> V)
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +0000942 : ConstantAggregate(T, ConstantStructVal, V) {
943 assert((T->isOpaque() || V.size() == T->getNumElements()) &&
944 "Invalid initializer for constant struct");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000945}
946
Owen Anderson45308b52009-07-27 22:29:26 +0000947// ConstantStruct accessors.
Chris Lattner229907c2011-07-18 04:54:35 +0000948Constant *ConstantStruct::get(StructType *ST, ArrayRef<Constant*> V) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000949 assert((ST->isOpaque() || ST->getNumElements() == V.size()) &&
950 "Incorrect # elements specified to ConstantStruct::get");
Chris Lattnerf14a67f2012-01-26 02:31:22 +0000951
952 // Create a ConstantAggregateZero value if all elements are zeros.
953 bool isZero = true;
954 bool isUndef = false;
955
956 if (!V.empty()) {
957 isUndef = isa<UndefValue>(V[0]);
958 isZero = V[0]->isNullValue();
959 if (isUndef || isZero) {
960 for (unsigned i = 0, e = V.size(); i != e; ++i) {
961 if (!V[i]->isNullValue())
962 isZero = false;
963 if (!isa<UndefValue>(V[i]))
964 isUndef = false;
965 }
966 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000967 }
Chris Lattnerf14a67f2012-01-26 02:31:22 +0000968 if (isZero)
969 return ConstantAggregateZero::get(ST);
970 if (isUndef)
971 return UndefValue::get(ST);
Galina Kistanovafc259902012-07-13 01:25:27 +0000972
Chris Lattnerf14a67f2012-01-26 02:31:22 +0000973 return ST->getContext().pImpl->StructConstants.getOrCreate(ST, V);
Owen Anderson45308b52009-07-27 22:29:26 +0000974}
975
Jay Foad89d9b812011-07-25 10:14:44 +0000976ConstantVector::ConstantVector(VectorType *T, ArrayRef<Constant *> V)
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +0000977 : ConstantAggregate(T, ConstantVectorVal, V) {
Duncan P. N. Exon Smithdb63bda2016-04-05 20:53:47 +0000978 assert(V.size() == T->getNumElements() &&
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +0000979 "Invalid initializer for constant vector");
Brian Gaeke02209042004-08-20 06:00:58 +0000980}
981
Owen Anderson4aa32952009-07-28 21:19:26 +0000982// ConstantVector accessors.
Jay Foadb8a8bed32011-06-22 09:10:19 +0000983Constant *ConstantVector::get(ArrayRef<Constant*> V) {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000984 if (Constant *C = getImpl(V))
985 return C;
986 VectorType *Ty = VectorType::get(V.front()->getType(), V.size());
987 return Ty->getContext().pImpl->VectorConstants.getOrCreate(Ty, V);
988}
Justin Bogner909e1c02015-12-01 20:20:49 +0000989
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000990Constant *ConstantVector::getImpl(ArrayRef<Constant*> V) {
Jay Foad9f32cfd2011-01-27 14:44:55 +0000991 assert(!V.empty() && "Vectors can't be empty");
Chris Lattner229907c2011-07-18 04:54:35 +0000992 VectorType *T = VectorType::get(V.front()->getType(), V.size());
Jay Foad9f32cfd2011-01-27 14:44:55 +0000993
Chris Lattner69229312011-02-15 00:14:00 +0000994 // If this is an all-undef or all-zero vector, return a
Owen Anderson4aa32952009-07-28 21:19:26 +0000995 // ConstantAggregateZero or UndefValue.
996 Constant *C = V[0];
997 bool isZero = C->isNullValue();
998 bool isUndef = isa<UndefValue>(C);
999
1000 if (isZero || isUndef) {
1001 for (unsigned i = 1, e = V.size(); i != e; ++i)
1002 if (V[i] != C) {
1003 isZero = isUndef = false;
1004 break;
1005 }
1006 }
Galina Kistanovafc259902012-07-13 01:25:27 +00001007
Owen Anderson4aa32952009-07-28 21:19:26 +00001008 if (isZero)
Owen Andersonb292b8c2009-07-30 23:03:37 +00001009 return ConstantAggregateZero::get(T);
Owen Anderson4aa32952009-07-28 21:19:26 +00001010 if (isUndef)
Owen Andersonb292b8c2009-07-30 23:03:37 +00001011 return UndefValue::get(T);
Galina Kistanovafc259902012-07-13 01:25:27 +00001012
Chris Lattner978fe0c2012-01-30 06:21:21 +00001013 // Check to see if all of the elements are ConstantFP or ConstantInt and if
1014 // the element type is compatible with ConstantDataVector. If so, use it.
Justin Bogner909e1c02015-12-01 20:20:49 +00001015 if (ConstantDataSequential::isElementTypeCompatible(C->getType()))
1016 return getSequenceIfElementsMatch<ConstantDataVector>(C, V);
Galina Kistanovafc259902012-07-13 01:25:27 +00001017
Chris Lattner978fe0c2012-01-30 06:21:21 +00001018 // Otherwise, the element type isn't compatible with ConstantDataVector, or
Craig Topperdf907262017-04-11 06:41:55 +00001019 // the operand list contains a ConstantExpr or something else strange.
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001020 return nullptr;
Owen Anderson4aa32952009-07-28 21:19:26 +00001021}
1022
Chris Lattnere9eed292012-01-25 05:19:54 +00001023Constant *ConstantVector::getSplat(unsigned NumElts, Constant *V) {
Chris Lattner978fe0c2012-01-30 06:21:21 +00001024 // If this splat is compatible with ConstantDataVector, use it instead of
1025 // ConstantVector.
1026 if ((isa<ConstantFP>(V) || isa<ConstantInt>(V)) &&
1027 ConstantDataSequential::isElementTypeCompatible(V->getType()))
1028 return ConstantDataVector::getSplat(NumElts, V);
Galina Kistanovafc259902012-07-13 01:25:27 +00001029
Chris Lattnere9eed292012-01-25 05:19:54 +00001030 SmallVector<Constant*, 32> Elts(NumElts, V);
1031 return get(Elts);
1032}
1033
David Majnemerf0f224d2015-11-11 21:57:16 +00001034ConstantTokenNone *ConstantTokenNone::get(LLVMContext &Context) {
1035 LLVMContextImpl *pImpl = Context.pImpl;
1036 if (!pImpl->TheNoneToken)
David Majnemer2dd41c52015-11-16 20:55:57 +00001037 pImpl->TheNoneToken.reset(new ConstantTokenNone(Context));
1038 return pImpl->TheNoneToken.get();
David Majnemerf0f224d2015-11-11 21:57:16 +00001039}
1040
1041/// Remove the constant from the constant table.
1042void ConstantTokenNone::destroyConstantImpl() {
1043 llvm_unreachable("You can't ConstantTokenNone->destroyConstantImpl()!");
1044}
Chris Lattnere9eed292012-01-25 05:19:54 +00001045
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001046// Utility function for determining if a ConstantExpr is a CastOp or not. This
1047// can't be inline because we don't want to #include Instruction.h into
1048// Constant.h
1049bool ConstantExpr::isCast() const {
1050 return Instruction::isCast(getOpcode());
1051}
1052
Reid Spenceree3c9912006-12-04 05:19:50 +00001053bool ConstantExpr::isCompare() const {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001054 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
Reid Spenceree3c9912006-12-04 05:19:50 +00001055}
1056
Dan Gohman7190d482009-09-10 23:37:55 +00001057bool ConstantExpr::isGEPWithNoNotionalOverIndexing() const {
1058 if (getOpcode() != Instruction::GetElementPtr) return false;
1059
1060 gep_type_iterator GEPI = gep_type_begin(this), E = gep_type_end(this);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001061 User::const_op_iterator OI = std::next(this->op_begin());
Dan Gohman7190d482009-09-10 23:37:55 +00001062
Sanjay Patel81ed3492016-12-11 20:07:02 +00001063 // The remaining indices may be compile-time known integers within the bounds
1064 // of the corresponding notional static array types.
Dan Gohman7190d482009-09-10 23:37:55 +00001065 for (; GEPI != E; ++GEPI, ++OI) {
Sanjay Patel81ed3492016-12-11 20:07:02 +00001066 if (isa<UndefValue>(*OI))
1067 continue;
1068 auto *CI = dyn_cast<ConstantInt>(*OI);
1069 if (!CI || (GEPI.isBoundedSequential() &&
1070 (CI->getValue().getActiveBits() > 64 ||
1071 CI->getZExtValue() >= GEPI.getSequentialNumElements())))
Peter Collingbourneab85225b2016-12-02 02:24:42 +00001072 return false;
Dan Gohman7190d482009-09-10 23:37:55 +00001073 }
1074
1075 // All the indices checked out.
1076 return true;
1077}
1078
Dan Gohman1ecaf452008-05-31 00:58:22 +00001079bool ConstantExpr::hasIndices() const {
1080 return getOpcode() == Instruction::ExtractValue ||
1081 getOpcode() == Instruction::InsertValue;
1082}
1083
Jay Foad0091fe82011-04-13 15:22:40 +00001084ArrayRef<unsigned> ConstantExpr::getIndices() const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001085 if (const ExtractValueConstantExpr *EVCE =
1086 dyn_cast<ExtractValueConstantExpr>(this))
1087 return EVCE->Indices;
Dan Gohmana469bdb2008-06-23 16:39:44 +00001088
1089 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001090}
1091
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001092unsigned ConstantExpr::getPredicate() const {
Craig Toppercc03b492015-12-15 06:11:36 +00001093 return cast<CompareConstantExpr>(this)->predicate;
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001094}
Chris Lattner60e0dd72001-10-03 06:12:09 +00001095
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001096Constant *
1097ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +00001098 assert(Op->getType() == getOperand(OpNo)->getType() &&
1099 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +00001100 if (getOperand(OpNo) == Op)
1101 return const_cast<ConstantExpr*>(this);
Chris Lattner37e38352012-01-26 20:37:11 +00001102
1103 SmallVector<Constant*, 8> NewOps;
1104 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1105 NewOps.push_back(i == OpNo ? Op : getOperand(i));
Galina Kistanovafc259902012-07-13 01:25:27 +00001106
Chris Lattner37e38352012-01-26 20:37:11 +00001107 return getWithOperands(NewOps);
Chris Lattner227816342006-07-14 22:20:01 +00001108}
1109
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001110Constant *ConstantExpr::getWithOperands(ArrayRef<Constant *> Ops, Type *Ty,
David Blaikie88208842015-08-21 20:16:51 +00001111 bool OnlyIfReduced, Type *SrcTy) const {
Jay Foad5c984e562011-04-13 13:46:01 +00001112 assert(Ops.size() == getNumOperands() && "Operand count mismatch!");
Galina Kistanovafc259902012-07-13 01:25:27 +00001113
Benjamin Kramer8008e9f2015-03-02 11:57:04 +00001114 // If no operands changed return self.
1115 if (Ty == getType() && std::equal(Ops.begin(), Ops.end(), op_begin()))
Chris Lattner227816342006-07-14 22:20:01 +00001116 return const_cast<ConstantExpr*>(this);
1117
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001118 Type *OnlyIfReducedTy = OnlyIfReduced ? Ty : nullptr;
Chris Lattner227816342006-07-14 22:20:01 +00001119 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001120 case Instruction::Trunc:
1121 case Instruction::ZExt:
1122 case Instruction::SExt:
1123 case Instruction::FPTrunc:
1124 case Instruction::FPExt:
1125 case Instruction::UIToFP:
1126 case Instruction::SIToFP:
1127 case Instruction::FPToUI:
1128 case Instruction::FPToSI:
1129 case Instruction::PtrToInt:
1130 case Instruction::IntToPtr:
1131 case Instruction::BitCast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001132 case Instruction::AddrSpaceCast:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001133 return ConstantExpr::getCast(getOpcode(), Ops[0], Ty, OnlyIfReduced);
Chris Lattner227816342006-07-14 22:20:01 +00001134 case Instruction::Select:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001135 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2], OnlyIfReducedTy);
Chris Lattner227816342006-07-14 22:20:01 +00001136 case Instruction::InsertElement:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001137 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2],
1138 OnlyIfReducedTy);
Chris Lattner227816342006-07-14 22:20:01 +00001139 case Instruction::ExtractElement:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001140 return ConstantExpr::getExtractElement(Ops[0], Ops[1], OnlyIfReducedTy);
Chris Lattner37e38352012-01-26 20:37:11 +00001141 case Instruction::InsertValue:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001142 return ConstantExpr::getInsertValue(Ops[0], Ops[1], getIndices(),
1143 OnlyIfReducedTy);
Chris Lattner37e38352012-01-26 20:37:11 +00001144 case Instruction::ExtractValue:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001145 return ConstantExpr::getExtractValue(Ops[0], getIndices(), OnlyIfReducedTy);
Chris Lattner227816342006-07-14 22:20:01 +00001146 case Instruction::ShuffleVector:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001147 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2],
1148 OnlyIfReducedTy);
David Blaikie88208842015-08-21 20:16:51 +00001149 case Instruction::GetElementPtr: {
1150 auto *GEPO = cast<GEPOperator>(this);
1151 assert(SrcTy || (Ops[0]->getType() == getOperand(0)->getType()));
1152 return ConstantExpr::getGetElementPtr(
1153 SrcTy ? SrcTy : GEPO->getSourceElementType(), Ops[0], Ops.slice(1),
Peter Collingbourned93620b2016-11-10 22:34:55 +00001154 GEPO->isInBounds(), GEPO->getInRangeIndex(), OnlyIfReducedTy);
David Blaikie88208842015-08-21 20:16:51 +00001155 }
Reid Spencer266e42b2006-12-23 06:05:41 +00001156 case Instruction::ICmp:
1157 case Instruction::FCmp:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001158 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1],
1159 OnlyIfReducedTy);
Chris Lattner227816342006-07-14 22:20:01 +00001160 default:
1161 assert(getNumOperands() == 2 && "Must be binary operator?");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001162 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1], SubclassOptionalData,
1163 OnlyIfReducedTy);
Chris Lattner7c1018a2006-07-14 19:37:40 +00001164 }
1165}
1166
Chris Lattner2f7c9632001-06-06 20:29:01 +00001167
1168//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00001169// isValueValidForType implementations
1170
Chris Lattner229907c2011-07-18 04:54:35 +00001171bool ConstantInt::isValueValidForType(Type *Ty, uint64_t Val) {
Chris Lattner8326bd82012-01-26 00:42:34 +00001172 unsigned NumBits = Ty->getIntegerBitWidth(); // assert okay
1173 if (Ty->isIntegerTy(1))
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001174 return Val == 0 || Val == 1;
Craig Topper50b1e512017-06-07 00:58:05 +00001175 return isUIntN(NumBits, Val);
Reid Spencere7334722006-12-19 01:28:19 +00001176}
1177
Chris Lattner229907c2011-07-18 04:54:35 +00001178bool ConstantInt::isValueValidForType(Type *Ty, int64_t Val) {
Chris Lattner8326bd82012-01-26 00:42:34 +00001179 unsigned NumBits = Ty->getIntegerBitWidth();
1180 if (Ty->isIntegerTy(1))
Reid Spencera94d3942007-01-19 21:13:56 +00001181 return Val == 0 || Val == 1 || Val == -1;
Craig Topper50b1e512017-06-07 00:58:05 +00001182 return isIntN(NumBits, Val);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001183}
1184
Chris Lattner229907c2011-07-18 04:54:35 +00001185bool ConstantFP::isValueValidForType(Type *Ty, const APFloat& Val) {
Dale Johannesend246b2c2007-08-30 00:23:21 +00001186 // convert modifies in place, so make a copy.
1187 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001188 bool losesInfo;
Chris Lattner6b727592004-06-17 18:19:28 +00001189 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001190 default:
1191 return false; // These can't be represented as floating point!
1192
Dale Johannesend246b2c2007-08-30 00:23:21 +00001193 // FIXME rounding mode needs to be more flexible
Dan Gohman518cda42011-12-17 00:04:22 +00001194 case Type::HalfTyID: {
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001195 if (&Val2.getSemantics() == &APFloat::IEEEhalf())
Dan Gohman518cda42011-12-17 00:04:22 +00001196 return true;
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001197 Val2.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &losesInfo);
Dan Gohman518cda42011-12-17 00:04:22 +00001198 return !losesInfo;
1199 }
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001200 case Type::FloatTyID: {
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001201 if (&Val2.getSemantics() == &APFloat::IEEEsingle())
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001202 return true;
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001203 Val2.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &losesInfo);
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001204 return !losesInfo;
1205 }
1206 case Type::DoubleTyID: {
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001207 if (&Val2.getSemantics() == &APFloat::IEEEhalf() ||
1208 &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1209 &Val2.getSemantics() == &APFloat::IEEEdouble())
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001210 return true;
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001211 Val2.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &losesInfo);
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001212 return !losesInfo;
1213 }
Dale Johannesenbdad8092007-08-09 22:51:36 +00001214 case Type::X86_FP80TyID:
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001215 return &Val2.getSemantics() == &APFloat::IEEEhalf() ||
1216 &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1217 &Val2.getSemantics() == &APFloat::IEEEdouble() ||
1218 &Val2.getSemantics() == &APFloat::x87DoubleExtended();
Dale Johannesenbdad8092007-08-09 22:51:36 +00001219 case Type::FP128TyID:
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001220 return &Val2.getSemantics() == &APFloat::IEEEhalf() ||
1221 &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1222 &Val2.getSemantics() == &APFloat::IEEEdouble() ||
1223 &Val2.getSemantics() == &APFloat::IEEEquad();
Dale Johannesen007aa372007-10-11 18:07:22 +00001224 case Type::PPC_FP128TyID:
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001225 return &Val2.getSemantics() == &APFloat::IEEEhalf() ||
1226 &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1227 &Val2.getSemantics() == &APFloat::IEEEdouble() ||
1228 &Val2.getSemantics() == &APFloat::PPCDoubleDouble();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001229 }
Chris Lattneraa2372562006-05-24 17:04:05 +00001230}
Chris Lattner9655e542001-07-20 19:16:02 +00001231
Chris Lattner030af792012-01-24 05:42:11 +00001232
Chris Lattner49d855c2001-09-07 16:46:31 +00001233//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +00001234// Factory Function Implementation
1235
Chris Lattnerc7f9fd42012-01-23 15:20:12 +00001236ConstantAggregateZero *ConstantAggregateZero::get(Type *Ty) {
Chris Lattner13ee7952010-08-28 04:09:24 +00001237 assert((Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy()) &&
Owen Andersonb292b8c2009-07-30 23:03:37 +00001238 "Cannot create an aggregate zero of non-aggregate type!");
David Blaikie899b85a2014-11-25 02:13:54 +00001239
Justin Lebar611c5c22016-10-10 16:26:13 +00001240 std::unique_ptr<ConstantAggregateZero> &Entry =
1241 Ty->getContext().pImpl->CAZConstants[Ty];
1242 if (!Entry)
1243 Entry.reset(new ConstantAggregateZero(Ty));
1244
1245 return Entry.get();
Owen Andersonb292b8c2009-07-30 23:03:37 +00001246}
1247
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001248/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001249void ConstantAggregateZero::destroyConstantImpl() {
Chris Lattnerc7f9fd42012-01-23 15:20:12 +00001250 getContext().pImpl->CAZConstants.erase(getType());
Chris Lattner9fba3da2004-02-15 05:53:04 +00001251}
1252
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001253/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001254void ConstantArray::destroyConstantImpl() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001255 getType()->getContext().pImpl->ArrayConstants.remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001256}
1257
Chris Lattner81fabb02002-08-26 17:53:56 +00001258
Chris Lattner3462ae32001-12-03 22:26:30 +00001259//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001260//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001261
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001262/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001263void ConstantStruct::destroyConstantImpl() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001264 getType()->getContext().pImpl->StructConstants.remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001265}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001266
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001267/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001268void ConstantVector::destroyConstantImpl() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001269 getType()->getContext().pImpl->VectorConstants.remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001270}
1271
Duncan Sandse6beec62012-11-13 12:59:33 +00001272Constant *Constant::getSplatValue() const {
1273 assert(this->getType()->isVectorTy() && "Only valid for vectors!");
1274 if (isa<ConstantAggregateZero>(this))
1275 return getNullValue(this->getType()->getVectorElementType());
1276 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this))
1277 return CV->getSplatValue();
1278 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
1279 return CV->getSplatValue();
Craig Topperc6207612014-04-09 06:08:46 +00001280 return nullptr;
Duncan Sandse6beec62012-11-13 12:59:33 +00001281}
1282
Duncan Sandscf0ff032011-02-01 08:39:12 +00001283Constant *ConstantVector::getSplatValue() const {
Dan Gohman07159202007-10-17 17:51:30 +00001284 // Check out first element.
1285 Constant *Elt = getOperand(0);
1286 // Then make sure all remaining elements point to the same value.
1287 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
Chris Lattnerd04e32d2011-07-17 06:01:30 +00001288 if (getOperand(I) != Elt)
Craig Topperc6207612014-04-09 06:08:46 +00001289 return nullptr;
Dan Gohman07159202007-10-17 17:51:30 +00001290 return Elt;
1291}
1292
Duncan Sandse6beec62012-11-13 12:59:33 +00001293const APInt &Constant::getUniqueInteger() const {
1294 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
1295 return CI->getValue();
1296 assert(this->getSplatValue() && "Doesn't contain a unique integer!");
1297 const Constant *C = this->getAggregateElement(0U);
1298 assert(C && isa<ConstantInt>(C) && "Not a vector of numbers!");
1299 return cast<ConstantInt>(C)->getValue();
1300}
1301
Chris Lattner31b132c2009-10-28 00:01:44 +00001302//---- ConstantPointerNull::get() implementation.
Chris Lattnerd7a73302001-10-13 06:57:33 +00001303//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001304
Chris Lattner229907c2011-07-18 04:54:35 +00001305ConstantPointerNull *ConstantPointerNull::get(PointerType *Ty) {
Justin Lebar611c5c22016-10-10 16:26:13 +00001306 std::unique_ptr<ConstantPointerNull> &Entry =
1307 Ty->getContext().pImpl->CPNConstants[Ty];
Craig Topperc6207612014-04-09 06:08:46 +00001308 if (!Entry)
Justin Lebar611c5c22016-10-10 16:26:13 +00001309 Entry.reset(new ConstantPointerNull(Ty));
Galina Kistanovafc259902012-07-13 01:25:27 +00001310
Justin Lebar611c5c22016-10-10 16:26:13 +00001311 return Entry.get();
Chris Lattner883ad0b2001-10-03 15:39:36 +00001312}
1313
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001314/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001315void ConstantPointerNull::destroyConstantImpl() {
Chris Lattnerc7f9fd42012-01-23 15:20:12 +00001316 getContext().pImpl->CPNConstants.erase(getType());
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001317}
1318
Chris Lattner229907c2011-07-18 04:54:35 +00001319UndefValue *UndefValue::get(Type *Ty) {
Justin Lebar611c5c22016-10-10 16:26:13 +00001320 std::unique_ptr<UndefValue> &Entry = Ty->getContext().pImpl->UVConstants[Ty];
Craig Topperc6207612014-04-09 06:08:46 +00001321 if (!Entry)
Justin Lebar611c5c22016-10-10 16:26:13 +00001322 Entry.reset(new UndefValue(Ty));
Galina Kistanovafc259902012-07-13 01:25:27 +00001323
Justin Lebar611c5c22016-10-10 16:26:13 +00001324 return Entry.get();
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001325}
1326
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001327/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001328void UndefValue::destroyConstantImpl() {
Chris Lattnerc7f9fd42012-01-23 15:20:12 +00001329 // Free the constant and any dangling references to it.
1330 getContext().pImpl->UVConstants.erase(getType());
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001331}
1332
Chris Lattner31b132c2009-10-28 00:01:44 +00001333BlockAddress *BlockAddress::get(BasicBlock *BB) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001334 assert(BB->getParent() && "Block must have a parent");
Chris Lattner31b132c2009-10-28 00:01:44 +00001335 return get(BB->getParent(), BB);
1336}
1337
1338BlockAddress *BlockAddress::get(Function *F, BasicBlock *BB) {
1339 BlockAddress *&BA =
1340 F->getContext().pImpl->BlockAddresses[std::make_pair(F, BB)];
Craig Topperc6207612014-04-09 06:08:46 +00001341 if (!BA)
Chris Lattner31b132c2009-10-28 00:01:44 +00001342 BA = new BlockAddress(F, BB);
Galina Kistanovafc259902012-07-13 01:25:27 +00001343
Chris Lattner31b132c2009-10-28 00:01:44 +00001344 assert(BA->getFunction() == F && "Basic block moved between functions");
1345 return BA;
1346}
1347
1348BlockAddress::BlockAddress(Function *F, BasicBlock *BB)
1349: Constant(Type::getInt8PtrTy(F->getContext()), Value::BlockAddressVal,
1350 &Op<0>(), 2) {
Chris Lattnerc559a9f2009-11-01 03:03:03 +00001351 setOperand(0, F);
1352 setOperand(1, BB);
Chris Lattneraa99c942009-11-01 01:27:45 +00001353 BB->AdjustBlockAddressRefCount(1);
Chris Lattner31b132c2009-10-28 00:01:44 +00001354}
1355
Chandler Carruth6a936922014-01-19 02:13:50 +00001356BlockAddress *BlockAddress::lookup(const BasicBlock *BB) {
1357 if (!BB->hasAddressTaken())
Craig Topperc6207612014-04-09 06:08:46 +00001358 return nullptr;
Chandler Carruth6a936922014-01-19 02:13:50 +00001359
1360 const Function *F = BB->getParent();
Craig Topper2617dcc2014-04-15 06:32:26 +00001361 assert(F && "Block must have a parent");
Chandler Carruth6a936922014-01-19 02:13:50 +00001362 BlockAddress *BA =
1363 F->getContext().pImpl->BlockAddresses.lookup(std::make_pair(F, BB));
1364 assert(BA && "Refcount and block address map disagree!");
1365 return BA;
1366}
Chris Lattner31b132c2009-10-28 00:01:44 +00001367
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001368/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001369void BlockAddress::destroyConstantImpl() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001370 getFunction()->getType()->getContext().pImpl
Chris Lattner31b132c2009-10-28 00:01:44 +00001371 ->BlockAddresses.erase(std::make_pair(getFunction(), getBasicBlock()));
Chris Lattneraa99c942009-11-01 01:27:45 +00001372 getBasicBlock()->AdjustBlockAddressRefCount(-1);
Chris Lattner31b132c2009-10-28 00:01:44 +00001373}
1374
Mehdi Amini8914d292016-02-10 22:47:15 +00001375Value *BlockAddress::handleOperandChangeImpl(Value *From, Value *To) {
Chris Lattner31b132c2009-10-28 00:01:44 +00001376 // This could be replacing either the Basic Block or the Function. In either
1377 // case, we have to remove the map entry.
1378 Function *NewF = getFunction();
1379 BasicBlock *NewBB = getBasicBlock();
Galina Kistanovafc259902012-07-13 01:25:27 +00001380
Mehdi Amini8914d292016-02-10 22:47:15 +00001381 if (From == NewF)
Derek Schuffec9dc012013-06-13 19:51:17 +00001382 NewF = cast<Function>(To->stripPointerCasts());
Mehdi Amini8914d292016-02-10 22:47:15 +00001383 else {
1384 assert(From == NewBB && "From does not match any operand");
Chris Lattner31b132c2009-10-28 00:01:44 +00001385 NewBB = cast<BasicBlock>(To);
Mehdi Amini8914d292016-02-10 22:47:15 +00001386 }
Galina Kistanovafc259902012-07-13 01:25:27 +00001387
Chris Lattner31b132c2009-10-28 00:01:44 +00001388 // See if the 'new' entry already exists, if not, just update this in place
1389 // and return early.
1390 BlockAddress *&NewBA =
1391 getContext().pImpl->BlockAddresses[std::make_pair(NewF, NewBB)];
Pete Cooper5815b1f2015-06-24 18:55:24 +00001392 if (NewBA)
1393 return NewBA;
Chris Lattner31b132c2009-10-28 00:01:44 +00001394
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001395 getBasicBlock()->AdjustBlockAddressRefCount(-1);
Galina Kistanovafc259902012-07-13 01:25:27 +00001396
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001397 // Remove the old entry, this can't cause the map to rehash (just a
1398 // tombstone will get added).
1399 getContext().pImpl->BlockAddresses.erase(std::make_pair(getFunction(),
1400 getBasicBlock()));
1401 NewBA = this;
1402 setOperand(0, NewF);
1403 setOperand(1, NewBB);
1404 getBasicBlock()->AdjustBlockAddressRefCount(1);
Pete Cooper5815b1f2015-06-24 18:55:24 +00001405
1406 // If we just want to keep the existing value, then return null.
1407 // Callers know that this means we shouldn't delete this value.
1408 return nullptr;
Chris Lattner31b132c2009-10-28 00:01:44 +00001409}
1410
1411//---- ConstantExpr::get() implementations.
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001412//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001413
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001414/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands7d6c8ae2008-03-30 19:38:55 +00001415/// cast in the ExprConstants map. It is used by the various get* methods below.
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001416static Constant *getFoldedCast(Instruction::CastOps opc, Constant *C, Type *Ty,
1417 bool OnlyIfReduced = false) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001418 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001419 // Fold a few common cases
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001420 if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001421 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001422
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001423 if (OnlyIfReduced)
1424 return nullptr;
1425
Owen Anderson1584a292009-08-04 20:25:11 +00001426 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
1427
Nadav Rotem88330432013-03-07 01:30:40 +00001428 // Look up the constant in the table first to ensure uniqueness.
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001429 ConstantExprKeyType Key(opc, C);
Galina Kistanovafc259902012-07-13 01:25:27 +00001430
Owen Anderson1584a292009-08-04 20:25:11 +00001431 return pImpl->ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001432}
Galina Kistanovafc259902012-07-13 01:25:27 +00001433
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001434Constant *ConstantExpr::getCast(unsigned oc, Constant *C, Type *Ty,
1435 bool OnlyIfReduced) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001436 Instruction::CastOps opc = Instruction::CastOps(oc);
1437 assert(Instruction::isCast(opc) && "opcode out of range");
1438 assert(C && Ty && "Null arguments to getCast");
Chris Lattner37bc78a2010-01-26 21:51:43 +00001439 assert(CastInst::castIsValid(opc, C, Ty) && "Invalid constantexpr cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001440
1441 switch (opc) {
Chris Lattner37bc78a2010-01-26 21:51:43 +00001442 default:
1443 llvm_unreachable("Invalid cast opcode");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001444 case Instruction::Trunc:
1445 return getTrunc(C, Ty, OnlyIfReduced);
1446 case Instruction::ZExt:
1447 return getZExt(C, Ty, OnlyIfReduced);
1448 case Instruction::SExt:
1449 return getSExt(C, Ty, OnlyIfReduced);
1450 case Instruction::FPTrunc:
1451 return getFPTrunc(C, Ty, OnlyIfReduced);
1452 case Instruction::FPExt:
1453 return getFPExtend(C, Ty, OnlyIfReduced);
1454 case Instruction::UIToFP:
1455 return getUIToFP(C, Ty, OnlyIfReduced);
1456 case Instruction::SIToFP:
1457 return getSIToFP(C, Ty, OnlyIfReduced);
1458 case Instruction::FPToUI:
1459 return getFPToUI(C, Ty, OnlyIfReduced);
1460 case Instruction::FPToSI:
1461 return getFPToSI(C, Ty, OnlyIfReduced);
1462 case Instruction::PtrToInt:
1463 return getPtrToInt(C, Ty, OnlyIfReduced);
1464 case Instruction::IntToPtr:
1465 return getIntToPtr(C, Ty, OnlyIfReduced);
1466 case Instruction::BitCast:
1467 return getBitCast(C, Ty, OnlyIfReduced);
1468 case Instruction::AddrSpaceCast:
1469 return getAddrSpaceCast(C, Ty, OnlyIfReduced);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001470 }
Galina Kistanovafc259902012-07-13 01:25:27 +00001471}
Reid Spencerf37dc652006-12-05 19:14:13 +00001472
Chris Lattner229907c2011-07-18 04:54:35 +00001473Constant *ConstantExpr::getZExtOrBitCast(Constant *C, Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001474 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001475 return getBitCast(C, Ty);
1476 return getZExt(C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001477}
1478
Chris Lattner229907c2011-07-18 04:54:35 +00001479Constant *ConstantExpr::getSExtOrBitCast(Constant *C, Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001480 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001481 return getBitCast(C, Ty);
1482 return getSExt(C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001483}
1484
Chris Lattner229907c2011-07-18 04:54:35 +00001485Constant *ConstantExpr::getTruncOrBitCast(Constant *C, Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001486 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001487 return getBitCast(C, Ty);
1488 return getTrunc(C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001489}
1490
Chris Lattner229907c2011-07-18 04:54:35 +00001491Constant *ConstantExpr::getPointerCast(Constant *S, Type *Ty) {
Evgeniy Stepanov23382642013-01-16 14:41:46 +00001492 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
1493 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
1494 "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001495
Evgeniy Stepanov23382642013-01-16 14:41:46 +00001496 if (Ty->isIntOrIntVectorTy())
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001497 return getPtrToInt(S, Ty);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001498
1499 unsigned SrcAS = S->getType()->getPointerAddressSpace();
1500 if (Ty->isPtrOrPtrVectorTy() && SrcAS != Ty->getPointerAddressSpace())
1501 return getAddrSpaceCast(S, Ty);
1502
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001503 return getBitCast(S, Ty);
Reid Spencerbc245a02006-12-05 03:25:26 +00001504}
1505
Matt Arsenault21f38f42013-12-07 02:58:41 +00001506Constant *ConstantExpr::getPointerBitCastOrAddrSpaceCast(Constant *S,
1507 Type *Ty) {
1508 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
1509 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
1510
1511 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
1512 return getAddrSpaceCast(S, Ty);
1513
1514 return getBitCast(S, Ty);
1515}
1516
Sanjay Patelf3587ec2016-05-18 22:05:28 +00001517Constant *ConstantExpr::getIntegerCast(Constant *C, Type *Ty, bool isSigned) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001518 assert(C->getType()->isIntOrIntVectorTy() &&
1519 Ty->isIntOrIntVectorTy() && "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001520 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1521 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer56521c42006-12-12 00:51:07 +00001522 Instruction::CastOps opcode =
1523 (SrcBits == DstBits ? Instruction::BitCast :
1524 (SrcBits > DstBits ? Instruction::Trunc :
1525 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1526 return getCast(opcode, C, Ty);
1527}
1528
Chris Lattner229907c2011-07-18 04:54:35 +00001529Constant *ConstantExpr::getFPCast(Constant *C, Type *Ty) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001530 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer56521c42006-12-12 00:51:07 +00001531 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001532 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1533 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001534 if (SrcBits == DstBits)
1535 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001536 Instruction::CastOps opcode =
Jay Foad9f32cfd2011-01-27 14:44:55 +00001537 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001538 return getCast(opcode, C, Ty);
1539}
1540
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001541Constant *ConstantExpr::getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001542#ifndef NDEBUG
1543 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1544 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1545#endif
1546 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001547 assert(C->getType()->isIntOrIntVectorTy() && "Trunc operand must be integer");
1548 assert(Ty->isIntOrIntVectorTy() && "Trunc produces only integral");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001549 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001550 "SrcTy must be larger than DestTy for Trunc!");
1551
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001552 return getFoldedCast(Instruction::Trunc, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001553}
1554
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001555Constant *ConstantExpr::getSExt(Constant *C, Type *Ty, bool OnlyIfReduced) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001556#ifndef NDEBUG
1557 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1558 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1559#endif
1560 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001561 assert(C->getType()->isIntOrIntVectorTy() && "SExt operand must be integral");
1562 assert(Ty->isIntOrIntVectorTy() && "SExt produces only integer");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001563 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001564 "SrcTy must be smaller than DestTy for SExt!");
1565
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001566 return getFoldedCast(Instruction::SExt, C, Ty, OnlyIfReduced);
Chris Lattnerdd284742004-04-04 23:20:30 +00001567}
1568
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001569Constant *ConstantExpr::getZExt(Constant *C, Type *Ty, bool OnlyIfReduced) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001570#ifndef NDEBUG
1571 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1572 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1573#endif
1574 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001575 assert(C->getType()->isIntOrIntVectorTy() && "ZEXt operand must be integral");
1576 assert(Ty->isIntOrIntVectorTy() && "ZExt produces only integer");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001577 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001578 "SrcTy must be smaller than DestTy for ZExt!");
1579
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001580 return getFoldedCast(Instruction::ZExt, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001581}
1582
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001583Constant *ConstantExpr::getFPTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001584#ifndef NDEBUG
1585 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1586 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1587#endif
1588 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001589 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001590 C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001591 "This is an illegal floating point truncation!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001592 return getFoldedCast(Instruction::FPTrunc, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001593}
1594
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001595Constant *ConstantExpr::getFPExtend(Constant *C, Type *Ty, bool OnlyIfReduced) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001596#ifndef NDEBUG
1597 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1598 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1599#endif
1600 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001601 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001602 C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001603 "This is an illegal floating point extension!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001604 return getFoldedCast(Instruction::FPExt, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001605}
1606
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001607Constant *ConstantExpr::getUIToFP(Constant *C, Type *Ty, bool OnlyIfReduced) {
Devang Pateld26344d2008-11-03 23:20:04 +00001608#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001609 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1610 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001611#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001612 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001613 assert(C->getType()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00001614 "This is an illegal uint to floating point cast!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001615 return getFoldedCast(Instruction::UIToFP, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001616}
1617
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001618Constant *ConstantExpr::getSIToFP(Constant *C, Type *Ty, bool OnlyIfReduced) {
Devang Pateld26344d2008-11-03 23:20:04 +00001619#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001620 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1621 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001622#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001623 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001624 assert(C->getType()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001625 "This is an illegal sint to floating point cast!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001626 return getFoldedCast(Instruction::SIToFP, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001627}
1628
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001629Constant *ConstantExpr::getFPToUI(Constant *C, Type *Ty, bool OnlyIfReduced) {
Devang Pateld26344d2008-11-03 23:20:04 +00001630#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001631 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1632 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001633#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001634 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001635 assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00001636 "This is an illegal floating point to uint cast!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001637 return getFoldedCast(Instruction::FPToUI, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001638}
1639
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001640Constant *ConstantExpr::getFPToSI(Constant *C, Type *Ty, bool OnlyIfReduced) {
Devang Pateld26344d2008-11-03 23:20:04 +00001641#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001642 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1643 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001644#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001645 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001646 assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00001647 "This is an illegal floating point to sint cast!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001648 return getFoldedCast(Instruction::FPToSI, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001649}
1650
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001651Constant *ConstantExpr::getPtrToInt(Constant *C, Type *DstTy,
1652 bool OnlyIfReduced) {
Craig Topper95d23472017-07-09 07:04:00 +00001653 assert(C->getType()->isPtrOrPtrVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00001654 "PtrToInt source must be pointer or pointer vector");
Craig Topper95d23472017-07-09 07:04:00 +00001655 assert(DstTy->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00001656 "PtrToInt destination must be integer or integer vector");
Chris Lattner8a3df542012-01-25 01:32:59 +00001657 assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy));
Nick Lewyckyff509622012-01-25 03:20:12 +00001658 if (isa<VectorType>(C->getType()))
Chris Lattner8326bd82012-01-26 00:42:34 +00001659 assert(C->getType()->getVectorNumElements()==DstTy->getVectorNumElements()&&
Chris Lattner8a3df542012-01-25 01:32:59 +00001660 "Invalid cast between a different number of vector elements");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001661 return getFoldedCast(Instruction::PtrToInt, C, DstTy, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001662}
1663
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001664Constant *ConstantExpr::getIntToPtr(Constant *C, Type *DstTy,
1665 bool OnlyIfReduced) {
Craig Topper95d23472017-07-09 07:04:00 +00001666 assert(C->getType()->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00001667 "IntToPtr source must be integer or integer vector");
Craig Topper95d23472017-07-09 07:04:00 +00001668 assert(DstTy->isPtrOrPtrVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00001669 "IntToPtr destination must be a pointer or pointer vector");
Chris Lattner8a3df542012-01-25 01:32:59 +00001670 assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy));
Nick Lewyckyff509622012-01-25 03:20:12 +00001671 if (isa<VectorType>(C->getType()))
Chris Lattner8326bd82012-01-26 00:42:34 +00001672 assert(C->getType()->getVectorNumElements()==DstTy->getVectorNumElements()&&
Chris Lattner8a3df542012-01-25 01:32:59 +00001673 "Invalid cast between a different number of vector elements");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001674 return getFoldedCast(Instruction::IntToPtr, C, DstTy, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001675}
1676
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001677Constant *ConstantExpr::getBitCast(Constant *C, Type *DstTy,
1678 bool OnlyIfReduced) {
Chris Lattner37bc78a2010-01-26 21:51:43 +00001679 assert(CastInst::castIsValid(Instruction::BitCast, C, DstTy) &&
1680 "Invalid constantexpr bitcast!");
Galina Kistanovafc259902012-07-13 01:25:27 +00001681
Chris Lattnercbeda872009-03-21 06:55:54 +00001682 // It is common to ask for a bitcast of a value to its own type, handle this
1683 // speedily.
1684 if (C->getType() == DstTy) return C;
Galina Kistanovafc259902012-07-13 01:25:27 +00001685
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001686 return getFoldedCast(Instruction::BitCast, C, DstTy, OnlyIfReduced);
Chris Lattnerdd284742004-04-04 23:20:30 +00001687}
1688
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001689Constant *ConstantExpr::getAddrSpaceCast(Constant *C, Type *DstTy,
1690 bool OnlyIfReduced) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001691 assert(CastInst::castIsValid(Instruction::AddrSpaceCast, C, DstTy) &&
1692 "Invalid constantexpr addrspacecast!");
1693
Jingyue Wubaabe502014-06-15 21:40:57 +00001694 // Canonicalize addrspacecasts between different pointer types by first
1695 // bitcasting the pointer type and then converting the address space.
1696 PointerType *SrcScalarTy = cast<PointerType>(C->getType()->getScalarType());
1697 PointerType *DstScalarTy = cast<PointerType>(DstTy->getScalarType());
1698 Type *DstElemTy = DstScalarTy->getElementType();
1699 if (SrcScalarTy->getElementType() != DstElemTy) {
1700 Type *MidTy = PointerType::get(DstElemTy, SrcScalarTy->getAddressSpace());
1701 if (VectorType *VT = dyn_cast<VectorType>(DstTy)) {
1702 // Handle vectors of pointers.
1703 MidTy = VectorType::get(MidTy, VT->getNumElements());
1704 }
1705 C = getBitCast(C, MidTy);
1706 }
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001707 return getFoldedCast(Instruction::AddrSpaceCast, C, DstTy, OnlyIfReduced);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001708}
1709
Chris Lattner887ecac2011-07-09 18:23:52 +00001710Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2,
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001711 unsigned Flags, Type *OnlyIfReducedTy) {
Chris Lattner887ecac2011-07-09 18:23:52 +00001712 // Check the operands for consistency first.
Reid Spencer7eb55b32006-11-02 01:53:59 +00001713 assert(Opcode >= Instruction::BinaryOpsBegin &&
1714 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001715 "Invalid opcode in binary constant expression");
1716 assert(C1->getType() == C2->getType() &&
1717 "Operand types in binary constant expression should match");
Galina Kistanovafc259902012-07-13 01:25:27 +00001718
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001719#ifndef NDEBUG
1720 switch (Opcode) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001721 case Instruction::Add:
Reid Spencer7eb55b32006-11-02 01:53:59 +00001722 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001723 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001724 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001725 assert(C1->getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001726 "Tried to create an integer operation on a non-integer type!");
1727 break;
1728 case Instruction::FAdd:
1729 case Instruction::FSub:
1730 case Instruction::FMul:
1731 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001732 assert(C1->getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001733 "Tried to create a floating-point operation on a "
1734 "non-floating-point type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001735 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001736 case Instruction::UDiv:
1737 case Instruction::SDiv:
1738 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001739 assert(C1->getType()->isIntOrIntVectorTy() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001740 "Tried to create an arithmetic operation on a non-arithmetic type!");
1741 break;
1742 case Instruction::FDiv:
1743 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001744 assert(C1->getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001745 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001746 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001747 case Instruction::URem:
1748 case Instruction::SRem:
1749 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001750 assert(C1->getType()->isIntOrIntVectorTy() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001751 "Tried to create an arithmetic operation on a non-arithmetic type!");
1752 break;
1753 case Instruction::FRem:
1754 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001755 assert(C1->getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001756 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001757 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001758 case Instruction::And:
1759 case Instruction::Or:
1760 case Instruction::Xor:
1761 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001762 assert(C1->getType()->isIntOrIntVectorTy() &&
Misha Brukman3852f652005-01-27 06:46:38 +00001763 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001764 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001765 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001766 case Instruction::LShr:
1767 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00001768 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001769 assert(C1->getType()->isIntOrIntVectorTy() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001770 "Tried to create a shift operation on a non-integer type!");
1771 break;
1772 default:
1773 break;
1774 }
1775#endif
1776
Chris Lattner887ecac2011-07-09 18:23:52 +00001777 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1778 return FC; // Fold a few common cases.
Galina Kistanovafc259902012-07-13 01:25:27 +00001779
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001780 if (OnlyIfReducedTy == C1->getType())
1781 return nullptr;
1782
Benjamin Kramer324322b2013-03-07 20:53:34 +00001783 Constant *ArgVec[] = { C1, C2 };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001784 ConstantExprKeyType Key(Opcode, ArgVec, 0, Flags);
Galina Kistanovafc259902012-07-13 01:25:27 +00001785
Chris Lattner887ecac2011-07-09 18:23:52 +00001786 LLVMContextImpl *pImpl = C1->getContext().pImpl;
1787 return pImpl->ExprConstants.getOrCreate(C1->getType(), Key);
Reid Spencera009d0d2006-12-04 21:35:24 +00001788}
1789
Chris Lattner229907c2011-07-18 04:54:35 +00001790Constant *ConstantExpr::getSizeOf(Type* Ty) {
Owen Anderson487375e2009-07-29 18:55:55 +00001791 // sizeof is implemented as: (i64) gep (Ty*)null, 1
1792 // Note that a non-inbounds gep is used, as null isn't within any object.
Owen Anderson55f1c092009-08-13 21:58:54 +00001793 Constant *GEPIdx = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
Owen Anderson487375e2009-07-29 18:55:55 +00001794 Constant *GEP = getGetElementPtr(
David Blaikie4a2e73b2015-04-02 18:55:32 +00001795 Ty, Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx);
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001796 return getPtrToInt(GEP,
1797 Type::getInt64Ty(Ty->getContext()));
Owen Anderson487375e2009-07-29 18:55:55 +00001798}
1799
Chris Lattner229907c2011-07-18 04:54:35 +00001800Constant *ConstantExpr::getAlignOf(Type* Ty) {
Dan Gohmancf913832010-01-28 02:15:55 +00001801 // alignof is implemented as: (i64) gep ({i1,Ty}*)null, 0, 1
Dan Gohman50c09d02009-08-11 17:57:01 +00001802 // Note that a non-inbounds gep is used, as null isn't within any object.
Serge Gueltone38003f2017-05-09 19:31:13 +00001803 Type *AligningTy = StructType::get(Type::getInt1Ty(Ty->getContext()), Ty);
Matt Arsenaultbe558882014-04-23 20:58:57 +00001804 Constant *NullPtr = Constant::getNullValue(AligningTy->getPointerTo(0));
Dan Gohmana9be7392010-01-28 02:43:22 +00001805 Constant *Zero = ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0);
Owen Anderson55f1c092009-08-13 21:58:54 +00001806 Constant *One = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
Owen Anderson487375e2009-07-29 18:55:55 +00001807 Constant *Indices[2] = { Zero, One };
David Blaikie4a2e73b2015-04-02 18:55:32 +00001808 Constant *GEP = getGetElementPtr(AligningTy, NullPtr, Indices);
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001809 return getPtrToInt(GEP,
1810 Type::getInt64Ty(Ty->getContext()));
Owen Anderson487375e2009-07-29 18:55:55 +00001811}
1812
Chris Lattner229907c2011-07-18 04:54:35 +00001813Constant *ConstantExpr::getOffsetOf(StructType* STy, unsigned FieldNo) {
Dan Gohmanede94e62010-02-01 16:37:38 +00001814 return getOffsetOf(STy, ConstantInt::get(Type::getInt32Ty(STy->getContext()),
1815 FieldNo));
1816}
1817
Chris Lattner229907c2011-07-18 04:54:35 +00001818Constant *ConstantExpr::getOffsetOf(Type* Ty, Constant *FieldNo) {
Dan Gohmanff3af7252009-08-16 21:26:11 +00001819 // offsetof is implemented as: (i64) gep (Ty*)null, 0, FieldNo
1820 // Note that a non-inbounds gep is used, as null isn't within any object.
1821 Constant *GEPIdx[] = {
Dan Gohmanede94e62010-02-01 16:37:38 +00001822 ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0),
1823 FieldNo
Dan Gohmanff3af7252009-08-16 21:26:11 +00001824 };
1825 Constant *GEP = getGetElementPtr(
David Blaikie4a2e73b2015-04-02 18:55:32 +00001826 Ty, Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx);
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001827 return getPtrToInt(GEP,
1828 Type::getInt64Ty(Ty->getContext()));
Dan Gohmanff3af7252009-08-16 21:26:11 +00001829}
Owen Anderson487375e2009-07-29 18:55:55 +00001830
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001831Constant *ConstantExpr::getCompare(unsigned short Predicate, Constant *C1,
1832 Constant *C2, bool OnlyIfReduced) {
Reid Spencera009d0d2006-12-04 21:35:24 +00001833 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Galina Kistanovafc259902012-07-13 01:25:27 +00001834
Chris Lattner887ecac2011-07-09 18:23:52 +00001835 switch (Predicate) {
1836 default: llvm_unreachable("Invalid CmpInst predicate");
1837 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
1838 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
1839 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
1840 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
1841 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
1842 case CmpInst::FCMP_TRUE:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001843 return getFCmp(Predicate, C1, C2, OnlyIfReduced);
Galina Kistanovafc259902012-07-13 01:25:27 +00001844
Chris Lattner887ecac2011-07-09 18:23:52 +00001845 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
1846 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
1847 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
1848 case CmpInst::ICMP_SLE:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001849 return getICmp(Predicate, C1, C2, OnlyIfReduced);
Chris Lattner887ecac2011-07-09 18:23:52 +00001850 }
Chris Lattner29ca2c62004-08-04 18:50:09 +00001851}
1852
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001853Constant *ConstantExpr::getSelect(Constant *C, Constant *V1, Constant *V2,
1854 Type *OnlyIfReducedTy) {
Chris Lattner41632132008-12-29 00:16:12 +00001855 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner6e415c02004-03-12 05:54:04 +00001856
Chris Lattner887ecac2011-07-09 18:23:52 +00001857 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1858 return SC; // Fold common cases
Chris Lattner6e415c02004-03-12 05:54:04 +00001859
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001860 if (OnlyIfReducedTy == V1->getType())
1861 return nullptr;
1862
Benjamin Kramer324322b2013-03-07 20:53:34 +00001863 Constant *ArgVec[] = { C, V1, V2 };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001864 ConstantExprKeyType Key(Instruction::Select, ArgVec);
Galina Kistanovafc259902012-07-13 01:25:27 +00001865
Chris Lattner887ecac2011-07-09 18:23:52 +00001866 LLVMContextImpl *pImpl = C->getContext().pImpl;
1867 return pImpl->ExprConstants.getOrCreate(V1->getType(), Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00001868}
1869
David Blaikie4a2e73b2015-04-02 18:55:32 +00001870Constant *ConstantExpr::getGetElementPtr(Type *Ty, Constant *C,
1871 ArrayRef<Value *> Idxs, bool InBounds,
Peter Collingbourned93620b2016-11-10 22:34:55 +00001872 Optional<unsigned> InRangeIndex,
David Blaikie4a2e73b2015-04-02 18:55:32 +00001873 Type *OnlyIfReducedTy) {
David Blaikie4a2e73b2015-04-02 18:55:32 +00001874 if (!Ty)
1875 Ty = cast<PointerType>(C->getType()->getScalarType())->getElementType();
1876 else
David Blaikied9d900c2015-05-07 17:28:58 +00001877 assert(
1878 Ty ==
1879 cast<PointerType>(C->getType()->getScalarType())->getContainedType(0u));
1880
Peter Collingbourned93620b2016-11-10 22:34:55 +00001881 if (Constant *FC =
1882 ConstantFoldGetElementPtr(Ty, C, InBounds, InRangeIndex, Idxs))
David Blaikied9d900c2015-05-07 17:28:58 +00001883 return FC; // Fold a few common cases.
1884
Chris Lattner887ecac2011-07-09 18:23:52 +00001885 // Get the result type of the getelementptr!
David Blaikie4a2e73b2015-04-02 18:55:32 +00001886 Type *DestTy = GetElementPtrInst::getIndexedType(Ty, Idxs);
1887 assert(DestTy && "GEP indices invalid!");
Chris Lattner8326bd82012-01-26 00:42:34 +00001888 unsigned AS = C->getType()->getPointerAddressSpace();
David Blaikie4a2e73b2015-04-02 18:55:32 +00001889 Type *ReqTy = DestTy->getPointerTo(AS);
Elena Demikhovskyee004bc2016-05-15 12:30:25 +00001890
1891 unsigned NumVecElts = 0;
1892 if (C->getType()->isVectorTy())
1893 NumVecElts = C->getType()->getVectorNumElements();
1894 else for (auto Idx : Idxs)
1895 if (Idx->getType()->isVectorTy())
1896 NumVecElts = Idx->getType()->getVectorNumElements();
1897
1898 if (NumVecElts)
1899 ReqTy = VectorType::get(ReqTy, NumVecElts);
Galina Kistanovafc259902012-07-13 01:25:27 +00001900
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001901 if (OnlyIfReducedTy == ReqTy)
1902 return nullptr;
1903
Dan Gohman1b849082009-09-07 23:54:19 +00001904 // Look up the constant in the table first to ensure uniqueness
1905 std::vector<Constant*> ArgVec;
Jay Foaded8db7d2011-07-21 14:31:17 +00001906 ArgVec.reserve(1 + Idxs.size());
Dan Gohman1b849082009-09-07 23:54:19 +00001907 ArgVec.push_back(C);
Duncan Sandse6beec62012-11-13 12:59:33 +00001908 for (unsigned i = 0, e = Idxs.size(); i != e; ++i) {
Duncan Sandse6beec62012-11-13 12:59:33 +00001909 assert((!Idxs[i]->getType()->isVectorTy() ||
Elena Demikhovskyee004bc2016-05-15 12:30:25 +00001910 Idxs[i]->getType()->getVectorNumElements() == NumVecElts) &&
Duncan Sandse6beec62012-11-13 12:59:33 +00001911 "getelementptr index type missmatch");
Elena Demikhovskyee004bc2016-05-15 12:30:25 +00001912
1913 Constant *Idx = cast<Constant>(Idxs[i]);
1914 if (NumVecElts && !Idxs[i]->getType()->isVectorTy())
1915 Idx = ConstantVector::getSplat(NumVecElts, Idx);
1916 ArgVec.push_back(Idx);
Duncan Sandse6beec62012-11-13 12:59:33 +00001917 }
Peter Collingbourned93620b2016-11-10 22:34:55 +00001918
1919 unsigned SubClassOptionalData = InBounds ? GEPOperator::IsInBounds : 0;
1920 if (InRangeIndex && *InRangeIndex < 63)
1921 SubClassOptionalData |= (*InRangeIndex + 1) << 1;
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001922 const ConstantExprKeyType Key(Instruction::GetElementPtr, ArgVec, 0,
Peter Collingbourned93620b2016-11-10 22:34:55 +00001923 SubClassOptionalData, None, Ty);
Galina Kistanovafc259902012-07-13 01:25:27 +00001924
Chris Lattner887ecac2011-07-09 18:23:52 +00001925 LLVMContextImpl *pImpl = C->getContext().pImpl;
Dan Gohman1b849082009-09-07 23:54:19 +00001926 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
1927}
1928
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001929Constant *ConstantExpr::getICmp(unsigned short pred, Constant *LHS,
1930 Constant *RHS, bool OnlyIfReduced) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001931 assert(LHS->getType() == RHS->getType());
Craig Topper45844762017-07-05 23:35:46 +00001932 assert(CmpInst::isIntPredicate((CmpInst::Predicate)pred) &&
1933 "Invalid ICmp Predicate");
Reid Spenceree3c9912006-12-04 05:19:50 +00001934
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001935 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001936 return FC; // Fold a few common cases...
1937
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001938 if (OnlyIfReduced)
1939 return nullptr;
1940
Reid Spenceree3c9912006-12-04 05:19:50 +00001941 // Look up the constant in the table first to ensure uniqueness
Benjamin Kramer324322b2013-03-07 20:53:34 +00001942 Constant *ArgVec[] = { LHS, RHS };
Reid Spencerb1537492006-12-24 18:42:29 +00001943 // Get the key type with both the opcode and predicate
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001944 const ConstantExprKeyType Key(Instruction::ICmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00001945
Chris Lattner229907c2011-07-18 04:54:35 +00001946 Type *ResultTy = Type::getInt1Ty(LHS->getContext());
1947 if (VectorType *VT = dyn_cast<VectorType>(LHS->getType()))
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00001948 ResultTy = VectorType::get(ResultTy, VT->getNumElements());
1949
Owen Anderson1584a292009-08-04 20:25:11 +00001950 LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl;
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00001951 return pImpl->ExprConstants.getOrCreate(ResultTy, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001952}
1953
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001954Constant *ConstantExpr::getFCmp(unsigned short pred, Constant *LHS,
1955 Constant *RHS, bool OnlyIfReduced) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001956 assert(LHS->getType() == RHS->getType());
Craig Topper45844762017-07-05 23:35:46 +00001957 assert(CmpInst::isFPPredicate((CmpInst::Predicate)pred) &&
1958 "Invalid FCmp Predicate");
Reid Spenceree3c9912006-12-04 05:19:50 +00001959
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001960 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001961 return FC; // Fold a few common cases...
1962
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001963 if (OnlyIfReduced)
1964 return nullptr;
1965
Reid Spenceree3c9912006-12-04 05:19:50 +00001966 // Look up the constant in the table first to ensure uniqueness
Benjamin Kramer324322b2013-03-07 20:53:34 +00001967 Constant *ArgVec[] = { LHS, RHS };
Reid Spencerb1537492006-12-24 18:42:29 +00001968 // Get the key type with both the opcode and predicate
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001969 const ConstantExprKeyType Key(Instruction::FCmp, ArgVec, pred);
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00001970
Chris Lattner229907c2011-07-18 04:54:35 +00001971 Type *ResultTy = Type::getInt1Ty(LHS->getContext());
1972 if (VectorType *VT = dyn_cast<VectorType>(LHS->getType()))
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00001973 ResultTy = VectorType::get(ResultTy, VT->getNumElements());
1974
Owen Anderson1584a292009-08-04 20:25:11 +00001975 LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl;
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00001976 return pImpl->ExprConstants.getOrCreate(ResultTy, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001977}
1978
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001979Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx,
1980 Type *OnlyIfReducedTy) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001981 assert(Val->getType()->isVectorTy() &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001982 "Tried to create extractelement operation on non-vector type!");
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001983 assert(Idx->getType()->isIntegerTy() &&
1984 "Extractelement index must be an integer type!");
Galina Kistanovafc259902012-07-13 01:25:27 +00001985
Chris Lattner887ecac2011-07-09 18:23:52 +00001986 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
Chris Lattner09660c92009-12-30 20:25:09 +00001987 return FC; // Fold a few common cases.
Galina Kistanovafc259902012-07-13 01:25:27 +00001988
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001989 Type *ReqTy = Val->getType()->getVectorElementType();
1990 if (OnlyIfReducedTy == ReqTy)
1991 return nullptr;
1992
Robert Bocchinoca27f032006-01-17 20:07:22 +00001993 // Look up the constant in the table first to ensure uniqueness
Benjamin Kramer324322b2013-03-07 20:53:34 +00001994 Constant *ArgVec[] = { Val, Idx };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001995 const ConstantExprKeyType Key(Instruction::ExtractElement, ArgVec);
Galina Kistanovafc259902012-07-13 01:25:27 +00001996
Chris Lattner887ecac2011-07-09 18:23:52 +00001997 LLVMContextImpl *pImpl = Val->getContext().pImpl;
Owen Anderson1584a292009-08-04 20:25:11 +00001998 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001999}
2000
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002001Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
2002 Constant *Idx, Type *OnlyIfReducedTy) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002003 assert(Val->getType()->isVectorTy() &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002004 "Tried to create insertelement operation on non-vector type!");
Chris Lattner8326bd82012-01-26 00:42:34 +00002005 assert(Elt->getType() == Val->getType()->getVectorElementType() &&
2006 "Insertelement types must match!");
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00002007 assert(Idx->getType()->isIntegerTy() &&
Reid Spencer2546b762007-01-26 07:37:34 +00002008 "Insertelement index must be i32 type!");
Robert Bocchinoca27f032006-01-17 20:07:22 +00002009
Chris Lattner887ecac2011-07-09 18:23:52 +00002010 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
2011 return FC; // Fold a few common cases.
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002012
2013 if (OnlyIfReducedTy == Val->getType())
2014 return nullptr;
2015
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002016 // Look up the constant in the table first to ensure uniqueness
Benjamin Kramer324322b2013-03-07 20:53:34 +00002017 Constant *ArgVec[] = { Val, Elt, Idx };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002018 const ConstantExprKeyType Key(Instruction::InsertElement, ArgVec);
Galina Kistanovafc259902012-07-13 01:25:27 +00002019
Chris Lattner887ecac2011-07-09 18:23:52 +00002020 LLVMContextImpl *pImpl = Val->getContext().pImpl;
2021 return pImpl->ExprConstants.getOrCreate(Val->getType(), Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002022}
2023
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002024Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
2025 Constant *Mask, Type *OnlyIfReducedTy) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002026 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
2027 "Invalid shuffle vector constant expr operands!");
Nate Begeman94aa38d2009-02-12 21:28:33 +00002028
Chris Lattner887ecac2011-07-09 18:23:52 +00002029 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
2030 return FC; // Fold a few common cases.
2031
Chris Lattner8326bd82012-01-26 00:42:34 +00002032 unsigned NElts = Mask->getType()->getVectorNumElements();
2033 Type *EltTy = V1->getType()->getVectorElementType();
Chris Lattner229907c2011-07-18 04:54:35 +00002034 Type *ShufTy = VectorType::get(EltTy, NElts);
Chris Lattner887ecac2011-07-09 18:23:52 +00002035
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002036 if (OnlyIfReducedTy == ShufTy)
2037 return nullptr;
2038
Chris Lattner887ecac2011-07-09 18:23:52 +00002039 // Look up the constant in the table first to ensure uniqueness
Benjamin Kramer324322b2013-03-07 20:53:34 +00002040 Constant *ArgVec[] = { V1, V2, Mask };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002041 const ConstantExprKeyType Key(Instruction::ShuffleVector, ArgVec);
Galina Kistanovafc259902012-07-13 01:25:27 +00002042
Chris Lattner887ecac2011-07-09 18:23:52 +00002043 LLVMContextImpl *pImpl = ShufTy->getContext().pImpl;
2044 return pImpl->ExprConstants.getOrCreate(ShufTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002045}
2046
Chris Lattner887ecac2011-07-09 18:23:52 +00002047Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002048 ArrayRef<unsigned> Idxs,
2049 Type *OnlyIfReducedTy) {
Hal Finkelb31366d2013-07-10 22:51:01 +00002050 assert(Agg->getType()->isFirstClassType() &&
2051 "Non-first-class type for constant insertvalue expression");
2052
Jay Foad57aa6362011-07-13 10:26:04 +00002053 assert(ExtractValueInst::getIndexedType(Agg->getType(),
2054 Idxs) == Val->getType() &&
Dan Gohman12fce772008-05-15 19:50:34 +00002055 "insertvalue indices invalid!");
Hal Finkelb31366d2013-07-10 22:51:01 +00002056 Type *ReqTy = Val->getType();
2057
2058 if (Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs))
2059 return FC;
2060
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002061 if (OnlyIfReducedTy == ReqTy)
2062 return nullptr;
2063
Hal Finkelb31366d2013-07-10 22:51:01 +00002064 Constant *ArgVec[] = { Agg, Val };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002065 const ConstantExprKeyType Key(Instruction::InsertValue, ArgVec, 0, 0, Idxs);
Hal Finkelb31366d2013-07-10 22:51:01 +00002066
2067 LLVMContextImpl *pImpl = Agg->getContext().pImpl;
2068 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Dan Gohman12fce772008-05-15 19:50:34 +00002069}
2070
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002071Constant *ConstantExpr::getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs,
2072 Type *OnlyIfReducedTy) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002073 assert(Agg->getType()->isFirstClassType() &&
Chris Lattner887ecac2011-07-09 18:23:52 +00002074 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002075
Chris Lattner229907c2011-07-18 04:54:35 +00002076 Type *ReqTy = ExtractValueInst::getIndexedType(Agg->getType(), Idxs);
Chandler Carruth9db56b82011-07-10 09:45:35 +00002077 (void)ReqTy;
Chris Lattner887ecac2011-07-09 18:23:52 +00002078 assert(ReqTy && "extractvalue indices invalid!");
Galina Kistanovafc259902012-07-13 01:25:27 +00002079
Dan Gohman0752bff2008-05-23 00:36:11 +00002080 assert(Agg->getType()->isFirstClassType() &&
2081 "Non-first-class type for constant extractvalue expression");
Hal Finkelb31366d2013-07-10 22:51:01 +00002082 if (Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs))
2083 return FC;
2084
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002085 if (OnlyIfReducedTy == ReqTy)
2086 return nullptr;
2087
Hal Finkelb31366d2013-07-10 22:51:01 +00002088 Constant *ArgVec[] = { Agg };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002089 const ConstantExprKeyType Key(Instruction::ExtractValue, ArgVec, 0, 0, Idxs);
Hal Finkelb31366d2013-07-10 22:51:01 +00002090
2091 LLVMContextImpl *pImpl = Agg->getContext().pImpl;
2092 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Dan Gohman12fce772008-05-15 19:50:34 +00002093}
2094
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002095Constant *ConstantExpr::getNeg(Constant *C, bool HasNUW, bool HasNSW) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002096 assert(C->getType()->isIntOrIntVectorTy() &&
Owen Anderson487375e2009-07-29 18:55:55 +00002097 "Cannot NEG a nonintegral value!");
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002098 return getSub(ConstantFP::getZeroValueForNegation(C->getType()),
2099 C, HasNUW, HasNSW);
Owen Anderson487375e2009-07-29 18:55:55 +00002100}
2101
Chris Lattnera676c0f2011-02-07 16:40:21 +00002102Constant *ConstantExpr::getFNeg(Constant *C) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002103 assert(C->getType()->isFPOrFPVectorTy() &&
Owen Anderson487375e2009-07-29 18:55:55 +00002104 "Cannot FNEG a non-floating-point value!");
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002105 return getFSub(ConstantFP::getZeroValueForNegation(C->getType()), C);
Owen Anderson487375e2009-07-29 18:55:55 +00002106}
2107
Chris Lattnera676c0f2011-02-07 16:40:21 +00002108Constant *ConstantExpr::getNot(Constant *C) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002109 assert(C->getType()->isIntOrIntVectorTy() &&
Owen Anderson487375e2009-07-29 18:55:55 +00002110 "Cannot NOT a nonintegral value!");
Owen Anderson5a1acd92009-07-31 20:28:14 +00002111 return get(Instruction::Xor, C, Constant::getAllOnesValue(C->getType()));
Owen Anderson487375e2009-07-29 18:55:55 +00002112}
2113
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002114Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2,
2115 bool HasNUW, bool HasNSW) {
2116 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2117 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
2118 return get(Instruction::Add, C1, C2, Flags);
Owen Anderson487375e2009-07-29 18:55:55 +00002119}
2120
Chris Lattnera676c0f2011-02-07 16:40:21 +00002121Constant *ConstantExpr::getFAdd(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002122 return get(Instruction::FAdd, C1, C2);
2123}
2124
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002125Constant *ConstantExpr::getSub(Constant *C1, Constant *C2,
2126 bool HasNUW, bool HasNSW) {
2127 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2128 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
2129 return get(Instruction::Sub, C1, C2, Flags);
Owen Anderson487375e2009-07-29 18:55:55 +00002130}
2131
Chris Lattnera676c0f2011-02-07 16:40:21 +00002132Constant *ConstantExpr::getFSub(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002133 return get(Instruction::FSub, C1, C2);
2134}
2135
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002136Constant *ConstantExpr::getMul(Constant *C1, Constant *C2,
2137 bool HasNUW, bool HasNSW) {
2138 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2139 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
2140 return get(Instruction::Mul, C1, C2, Flags);
Owen Anderson487375e2009-07-29 18:55:55 +00002141}
2142
Chris Lattnera676c0f2011-02-07 16:40:21 +00002143Constant *ConstantExpr::getFMul(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002144 return get(Instruction::FMul, C1, C2);
2145}
2146
Chris Lattner0d75eac2011-02-09 16:43:07 +00002147Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2, bool isExact) {
2148 return get(Instruction::UDiv, C1, C2,
2149 isExact ? PossiblyExactOperator::IsExact : 0);
Owen Anderson487375e2009-07-29 18:55:55 +00002150}
2151
Chris Lattner0d75eac2011-02-09 16:43:07 +00002152Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2, bool isExact) {
2153 return get(Instruction::SDiv, C1, C2,
2154 isExact ? PossiblyExactOperator::IsExact : 0);
Owen Anderson487375e2009-07-29 18:55:55 +00002155}
2156
Chris Lattnera676c0f2011-02-07 16:40:21 +00002157Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002158 return get(Instruction::FDiv, C1, C2);
2159}
2160
Chris Lattnera676c0f2011-02-07 16:40:21 +00002161Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002162 return get(Instruction::URem, C1, C2);
2163}
2164
Chris Lattnera676c0f2011-02-07 16:40:21 +00002165Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002166 return get(Instruction::SRem, C1, C2);
2167}
2168
Chris Lattnera676c0f2011-02-07 16:40:21 +00002169Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002170 return get(Instruction::FRem, C1, C2);
2171}
2172
Chris Lattnera676c0f2011-02-07 16:40:21 +00002173Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002174 return get(Instruction::And, C1, C2);
2175}
2176
Chris Lattnera676c0f2011-02-07 16:40:21 +00002177Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002178 return get(Instruction::Or, C1, C2);
2179}
2180
Chris Lattnera676c0f2011-02-07 16:40:21 +00002181Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002182 return get(Instruction::Xor, C1, C2);
2183}
2184
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002185Constant *ConstantExpr::getShl(Constant *C1, Constant *C2,
2186 bool HasNUW, bool HasNSW) {
2187 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2188 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
2189 return get(Instruction::Shl, C1, C2, Flags);
Owen Anderson487375e2009-07-29 18:55:55 +00002190}
2191
Chris Lattner0d75eac2011-02-09 16:43:07 +00002192Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2, bool isExact) {
2193 return get(Instruction::LShr, C1, C2,
2194 isExact ? PossiblyExactOperator::IsExact : 0);
Owen Anderson487375e2009-07-29 18:55:55 +00002195}
2196
Chris Lattner0d75eac2011-02-09 16:43:07 +00002197Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2, bool isExact) {
2198 return get(Instruction::AShr, C1, C2,
2199 isExact ? PossiblyExactOperator::IsExact : 0);
Owen Anderson487375e2009-07-29 18:55:55 +00002200}
2201
Duncan Sandsd7aeefe2012-06-12 14:33:56 +00002202Constant *ConstantExpr::getBinOpIdentity(unsigned Opcode, Type *Ty) {
2203 switch (Opcode) {
2204 default:
Duncan Sands318a89d2012-06-13 09:42:13 +00002205 // Doesn't have an identity.
Craig Topperc6207612014-04-09 06:08:46 +00002206 return nullptr;
Duncan Sands318a89d2012-06-13 09:42:13 +00002207
Duncan Sandsd7aeefe2012-06-12 14:33:56 +00002208 case Instruction::Add:
2209 case Instruction::Or:
2210 case Instruction::Xor:
2211 return Constant::getNullValue(Ty);
2212
2213 case Instruction::Mul:
2214 return ConstantInt::get(Ty, 1);
2215
2216 case Instruction::And:
2217 return Constant::getAllOnesValue(Ty);
2218 }
2219}
2220
Duncan Sands318a89d2012-06-13 09:42:13 +00002221Constant *ConstantExpr::getBinOpAbsorber(unsigned Opcode, Type *Ty) {
2222 switch (Opcode) {
2223 default:
2224 // Doesn't have an absorber.
Craig Topperc6207612014-04-09 06:08:46 +00002225 return nullptr;
Duncan Sands318a89d2012-06-13 09:42:13 +00002226
2227 case Instruction::Or:
2228 return Constant::getAllOnesValue(Ty);
2229
2230 case Instruction::And:
2231 case Instruction::Mul:
2232 return Constant::getNullValue(Ty);
2233 }
2234}
2235
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00002236/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00002237void ConstantExpr::destroyConstantImpl() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002238 getType()->getContext().pImpl->ExprConstants.remove(this);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002239}
2240
Chris Lattner3cd8c562002-07-30 18:54:25 +00002241const char *ConstantExpr::getOpcodeName() const {
2242 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002243}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00002244
David Blaikie60310f22015-05-08 00:42:26 +00002245GetElementPtrConstantExpr::GetElementPtrConstantExpr(
2246 Type *SrcElementTy, Constant *C, ArrayRef<Constant *> IdxList, Type *DestTy)
2247 : ConstantExpr(DestTy, Instruction::GetElementPtr,
2248 OperandTraits<GetElementPtrConstantExpr>::op_end(this) -
2249 (IdxList.size() + 1),
2250 IdxList.size() + 1),
Eduard Burtescu19eb0312016-01-19 17:28:00 +00002251 SrcElementTy(SrcElementTy),
2252 ResElementTy(GetElementPtrInst::getIndexedType(SrcElementTy, IdxList)) {
Pete Coopereb31b682015-05-21 22:48:54 +00002253 Op<0>() = C;
Pete Cooper74510a42015-06-12 17:48:05 +00002254 Use *OperandList = getOperandList();
Chris Lattnera3b94ba2010-03-30 20:48:48 +00002255 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
2256 OperandList[i+1] = IdxList[i];
2257}
2258
David Blaikie60310f22015-05-08 00:42:26 +00002259Type *GetElementPtrConstantExpr::getSourceElementType() const {
2260 return SrcElementTy;
2261}
2262
Eduard Burtescu19eb0312016-01-19 17:28:00 +00002263Type *GetElementPtrConstantExpr::getResultElementType() const {
2264 return ResElementTy;
2265}
2266
Chris Lattner3756b912012-01-23 22:57:10 +00002267//===----------------------------------------------------------------------===//
2268// ConstantData* implementations
2269
Chris Lattnere4f3f102012-01-24 04:43:41 +00002270Type *ConstantDataSequential::getElementType() const {
2271 return getType()->getElementType();
2272}
2273
Chris Lattner5d4497b2012-01-24 09:31:43 +00002274StringRef ConstantDataSequential::getRawDataValues() const {
Chris Lattner00245f42012-01-24 13:41:11 +00002275 return StringRef(DataElements, getNumElements()*getElementByteSize());
Chris Lattner5d4497b2012-01-24 09:31:43 +00002276}
2277
Craig Toppere3dcce92015-08-01 22:20:21 +00002278bool ConstantDataSequential::isElementTypeCompatible(Type *Ty) {
Justin Bogner0ebc8602015-12-08 03:01:16 +00002279 if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) return true;
Craig Toppere3dcce92015-08-01 22:20:21 +00002280 if (auto *IT = dyn_cast<IntegerType>(Ty)) {
Chris Lattnere4f3f102012-01-24 04:43:41 +00002281 switch (IT->getBitWidth()) {
2282 case 8:
2283 case 16:
2284 case 32:
2285 case 64:
2286 return true;
2287 default: break;
2288 }
2289 }
2290 return false;
2291}
2292
Chris Lattner00245f42012-01-24 13:41:11 +00002293unsigned ConstantDataSequential::getNumElements() const {
Chris Lattner8a3df542012-01-25 01:32:59 +00002294 if (ArrayType *AT = dyn_cast<ArrayType>(getType()))
2295 return AT->getNumElements();
Chris Lattner8326bd82012-01-26 00:42:34 +00002296 return getType()->getVectorNumElements();
Chris Lattner00245f42012-01-24 13:41:11 +00002297}
2298
2299
Chris Lattnere4f3f102012-01-24 04:43:41 +00002300uint64_t ConstantDataSequential::getElementByteSize() const {
2301 return getElementType()->getPrimitiveSizeInBits()/8;
2302}
2303
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00002304/// Return the start of the specified element.
Chris Lattnere4f3f102012-01-24 04:43:41 +00002305const char *ConstantDataSequential::getElementPointer(unsigned Elt) const {
Chris Lattner00245f42012-01-24 13:41:11 +00002306 assert(Elt < getNumElements() && "Invalid Elt");
Chris Lattnere4f3f102012-01-24 04:43:41 +00002307 return DataElements+Elt*getElementByteSize();
2308}
2309
2310
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00002311/// Return true if the array is empty or all zeros.
Chris Lattner3756b912012-01-23 22:57:10 +00002312static bool isAllZeros(StringRef Arr) {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +00002313 for (char I : Arr)
2314 if (I != 0)
Chris Lattner3756b912012-01-23 22:57:10 +00002315 return false;
2316 return true;
2317}
Chris Lattner030af792012-01-24 05:42:11 +00002318
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00002319/// This is the underlying implementation of all of the
Chris Lattner3756b912012-01-23 22:57:10 +00002320/// ConstantDataSequential::get methods. They all thunk down to here, providing
Chris Lattnerf06039b2012-01-30 18:19:30 +00002321/// the correct element type. We take the bytes in as a StringRef because
Chris Lattner3756b912012-01-23 22:57:10 +00002322/// we *want* an underlying "char*" to avoid TBAA type punning violations.
2323Constant *ConstantDataSequential::getImpl(StringRef Elements, Type *Ty) {
Chris Lattner8326bd82012-01-26 00:42:34 +00002324 assert(isElementTypeCompatible(Ty->getSequentialElementType()));
Chris Lattner139822f2012-01-24 14:17:05 +00002325 // If the elements are all zero or there are no elements, return a CAZ, which
2326 // is more dense and canonical.
Chris Lattner3756b912012-01-23 22:57:10 +00002327 if (isAllZeros(Elements))
2328 return ConstantAggregateZero::get(Ty);
2329
2330 // Do a lookup to see if we have already formed one of these.
David Blaikie5106ce72014-11-19 05:49:42 +00002331 auto &Slot =
2332 *Ty->getContext()
2333 .pImpl->CDSConstants.insert(std::make_pair(Elements, nullptr))
2334 .first;
Galina Kistanovafc259902012-07-13 01:25:27 +00002335
Chris Lattner3756b912012-01-23 22:57:10 +00002336 // The bucket can point to a linked list of different CDS's that have the same
2337 // body but different types. For example, 0,0,0,1 could be a 4 element array
2338 // of i8, or a 1-element array of i32. They'll both end up in the same
2339 /// StringMap bucket, linked up by their Next pointers. Walk the list.
David Blaikie5106ce72014-11-19 05:49:42 +00002340 ConstantDataSequential **Entry = &Slot.second;
Craig Topperc6207612014-04-09 06:08:46 +00002341 for (ConstantDataSequential *Node = *Entry; Node;
Chris Lattner3756b912012-01-23 22:57:10 +00002342 Entry = &Node->Next, Node = *Entry)
2343 if (Node->getType() == Ty)
2344 return Node;
Galina Kistanovafc259902012-07-13 01:25:27 +00002345
Chris Lattner3756b912012-01-23 22:57:10 +00002346 // Okay, we didn't get a hit. Create a node of the right class, link it in,
2347 // and return it.
2348 if (isa<ArrayType>(Ty))
David Blaikie5106ce72014-11-19 05:49:42 +00002349 return *Entry = new ConstantDataArray(Ty, Slot.first().data());
Chris Lattner3756b912012-01-23 22:57:10 +00002350
2351 assert(isa<VectorType>(Ty));
David Blaikie5106ce72014-11-19 05:49:42 +00002352 return *Entry = new ConstantDataVector(Ty, Slot.first().data());
Chris Lattner3756b912012-01-23 22:57:10 +00002353}
2354
Pete Cooper86dd4cf2015-06-23 21:55:11 +00002355void ConstantDataSequential::destroyConstantImpl() {
Chris Lattner3756b912012-01-23 22:57:10 +00002356 // Remove the constant from the StringMap.
2357 StringMap<ConstantDataSequential*> &CDSConstants =
2358 getType()->getContext().pImpl->CDSConstants;
Galina Kistanovafc259902012-07-13 01:25:27 +00002359
Chris Lattner3756b912012-01-23 22:57:10 +00002360 StringMap<ConstantDataSequential*>::iterator Slot =
Chris Lattner5d4497b2012-01-24 09:31:43 +00002361 CDSConstants.find(getRawDataValues());
Chris Lattner3756b912012-01-23 22:57:10 +00002362
2363 assert(Slot != CDSConstants.end() && "CDS not found in uniquing table");
2364
2365 ConstantDataSequential **Entry = &Slot->getValue();
2366
2367 // Remove the entry from the hash table.
Craig Topperc6207612014-04-09 06:08:46 +00002368 if (!(*Entry)->Next) {
Chris Lattner3756b912012-01-23 22:57:10 +00002369 // If there is only one value in the bucket (common case) it must be this
2370 // entry, and removing the entry should remove the bucket completely.
2371 assert((*Entry) == this && "Hash mismatch in ConstantDataSequential");
2372 getContext().pImpl->CDSConstants.erase(Slot);
2373 } else {
2374 // Otherwise, there are multiple entries linked off the bucket, unlink the
2375 // node we care about but keep the bucket around.
2376 for (ConstantDataSequential *Node = *Entry; ;
2377 Entry = &Node->Next, Node = *Entry) {
2378 assert(Node && "Didn't find entry in its uniquing hash table!");
2379 // If we found our entry, unlink it from the list and we're done.
2380 if (Node == this) {
2381 *Entry = Node->Next;
2382 break;
2383 }
2384 }
2385 }
Galina Kistanovafc259902012-07-13 01:25:27 +00002386
Chris Lattner3756b912012-01-23 22:57:10 +00002387 // If we were part of a list, make sure that we don't delete the list that is
2388 // still owned by the uniquing map.
Craig Topperc6207612014-04-09 06:08:46 +00002389 Next = nullptr;
Chris Lattner3756b912012-01-23 22:57:10 +00002390}
2391
2392/// get() constructors - Return a constant with array type with an element
2393/// count and element type matching the ArrayRef passed in. Note that this
2394/// can return a ConstantAggregateZero object.
Chris Lattner20683932012-01-24 14:04:40 +00002395Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint8_t> Elts) {
Chris Lattner3756b912012-01-23 22:57:10 +00002396 Type *Ty = ArrayType::get(Type::getInt8Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002397 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002398 return getImpl(StringRef(Data, Elts.size() * 1), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002399}
Chris Lattner20683932012-01-24 14:04:40 +00002400Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint16_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002401 Type *Ty = ArrayType::get(Type::getInt16Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002402 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002403 return getImpl(StringRef(Data, Elts.size() * 2), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002404}
Chris Lattner20683932012-01-24 14:04:40 +00002405Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint32_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002406 Type *Ty = ArrayType::get(Type::getInt32Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002407 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002408 return getImpl(StringRef(Data, Elts.size() * 4), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002409}
Chris Lattner20683932012-01-24 14:04:40 +00002410Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint64_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002411 Type *Ty = ArrayType::get(Type::getInt64Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002412 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002413 return getImpl(StringRef(Data, Elts.size() * 8), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002414}
Chris Lattner20683932012-01-24 14:04:40 +00002415Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<float> Elts) {
Chris Lattner3756b912012-01-23 22:57:10 +00002416 Type *Ty = ArrayType::get(Type::getFloatTy(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002417 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002418 return getImpl(StringRef(Data, Elts.size() * 4), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002419}
Chris Lattner20683932012-01-24 14:04:40 +00002420Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<double> Elts) {
Chris Lattner3756b912012-01-23 22:57:10 +00002421 Type *Ty = ArrayType::get(Type::getDoubleTy(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002422 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002423 return getImpl(StringRef(Data, Elts.size() * 8), Ty);
Rafael Espindola8c97e192015-02-19 16:08:20 +00002424}
2425
2426/// getFP() constructors - Return a constant with array type with an element
2427/// count and element type of float with precision matching the number of
2428/// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits,
2429/// double for 64bits) Note that this can return a ConstantAggregateZero
2430/// object.
2431Constant *ConstantDataArray::getFP(LLVMContext &Context,
2432 ArrayRef<uint16_t> Elts) {
Justin Bognerb7389d6712015-12-09 21:21:07 +00002433 Type *Ty = ArrayType::get(Type::getHalfTy(Context), Elts.size());
Rafael Espindola8c97e192015-02-19 16:08:20 +00002434 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002435 return getImpl(StringRef(Data, Elts.size() * 2), Ty);
Rafael Espindola8c97e192015-02-19 16:08:20 +00002436}
2437Constant *ConstantDataArray::getFP(LLVMContext &Context,
2438 ArrayRef<uint32_t> Elts) {
2439 Type *Ty = ArrayType::get(Type::getFloatTy(Context), Elts.size());
2440 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002441 return getImpl(StringRef(Data, Elts.size() * 4), Ty);
Rafael Espindola8c97e192015-02-19 16:08:20 +00002442}
2443Constant *ConstantDataArray::getFP(LLVMContext &Context,
2444 ArrayRef<uint64_t> Elts) {
2445 Type *Ty = ArrayType::get(Type::getDoubleTy(Context), Elts.size());
2446 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002447 return getImpl(StringRef(Data, Elts.size() * 8), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002448}
2449
Chris Lattner20683932012-01-24 14:04:40 +00002450Constant *ConstantDataArray::getString(LLVMContext &Context,
2451 StringRef Str, bool AddNull) {
Galina Kistanovafc259902012-07-13 01:25:27 +00002452 if (!AddNull) {
2453 const uint8_t *Data = reinterpret_cast<const uint8_t *>(Str.data());
Craig Topper393ce692017-07-11 15:52:21 +00002454 return get(Context, makeArrayRef(Data, Str.size()));
Galina Kistanovafc259902012-07-13 01:25:27 +00002455 }
2456
Chris Lattner20683932012-01-24 14:04:40 +00002457 SmallVector<uint8_t, 64> ElementVals;
2458 ElementVals.append(Str.begin(), Str.end());
2459 ElementVals.push_back(0);
2460 return get(Context, ElementVals);
2461}
Chris Lattner3756b912012-01-23 22:57:10 +00002462
2463/// get() constructors - Return a constant with vector type with an element
2464/// count and element type matching the ArrayRef passed in. Note that this
2465/// can return a ConstantAggregateZero object.
Chris Lattner20683932012-01-24 14:04:40 +00002466Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint8_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002467 Type *Ty = VectorType::get(Type::getInt8Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002468 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002469 return getImpl(StringRef(Data, Elts.size() * 1), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002470}
Chris Lattner20683932012-01-24 14:04:40 +00002471Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint16_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002472 Type *Ty = VectorType::get(Type::getInt16Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002473 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002474 return getImpl(StringRef(Data, Elts.size() * 2), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002475}
Chris Lattner20683932012-01-24 14:04:40 +00002476Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint32_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002477 Type *Ty = VectorType::get(Type::getInt32Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002478 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002479 return getImpl(StringRef(Data, Elts.size() * 4), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002480}
Chris Lattner20683932012-01-24 14:04:40 +00002481Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint64_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002482 Type *Ty = VectorType::get(Type::getInt64Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002483 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002484 return getImpl(StringRef(Data, Elts.size() * 8), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002485}
Chris Lattner20683932012-01-24 14:04:40 +00002486Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<float> Elts) {
Chris Lattner3756b912012-01-23 22:57:10 +00002487 Type *Ty = VectorType::get(Type::getFloatTy(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002488 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002489 return getImpl(StringRef(Data, Elts.size() * 4), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002490}
Chris Lattner20683932012-01-24 14:04:40 +00002491Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<double> Elts) {
Chris Lattner3756b912012-01-23 22:57:10 +00002492 Type *Ty = VectorType::get(Type::getDoubleTy(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002493 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002494 return getImpl(StringRef(Data, Elts.size() * 8), Ty);
Rafael Espindola8c97e192015-02-19 16:08:20 +00002495}
2496
2497/// getFP() constructors - Return a constant with vector type with an element
2498/// count and element type of float with the precision matching the number of
2499/// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits,
2500/// double for 64bits) Note that this can return a ConstantAggregateZero
2501/// object.
2502Constant *ConstantDataVector::getFP(LLVMContext &Context,
2503 ArrayRef<uint16_t> Elts) {
2504 Type *Ty = VectorType::get(Type::getHalfTy(Context), Elts.size());
2505 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002506 return getImpl(StringRef(Data, Elts.size() * 2), Ty);
Rafael Espindola8c97e192015-02-19 16:08:20 +00002507}
2508Constant *ConstantDataVector::getFP(LLVMContext &Context,
2509 ArrayRef<uint32_t> Elts) {
2510 Type *Ty = VectorType::get(Type::getFloatTy(Context), Elts.size());
2511 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002512 return getImpl(StringRef(Data, Elts.size() * 4), Ty);
Rafael Espindola8c97e192015-02-19 16:08:20 +00002513}
2514Constant *ConstantDataVector::getFP(LLVMContext &Context,
2515 ArrayRef<uint64_t> Elts) {
2516 Type *Ty = VectorType::get(Type::getDoubleTy(Context), Elts.size());
2517 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002518 return getImpl(StringRef(Data, Elts.size() * 8), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002519}
2520
Chris Lattnere9eed292012-01-25 05:19:54 +00002521Constant *ConstantDataVector::getSplat(unsigned NumElts, Constant *V) {
2522 assert(isElementTypeCompatible(V->getType()) &&
2523 "Element type not compatible with ConstantData");
2524 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
2525 if (CI->getType()->isIntegerTy(8)) {
2526 SmallVector<uint8_t, 16> Elts(NumElts, CI->getZExtValue());
2527 return get(V->getContext(), Elts);
2528 }
2529 if (CI->getType()->isIntegerTy(16)) {
2530 SmallVector<uint16_t, 16> Elts(NumElts, CI->getZExtValue());
2531 return get(V->getContext(), Elts);
2532 }
2533 if (CI->getType()->isIntegerTy(32)) {
2534 SmallVector<uint32_t, 16> Elts(NumElts, CI->getZExtValue());
2535 return get(V->getContext(), Elts);
2536 }
2537 assert(CI->getType()->isIntegerTy(64) && "Unsupported ConstantData type");
2538 SmallVector<uint64_t, 16> Elts(NumElts, CI->getZExtValue());
2539 return get(V->getContext(), Elts);
2540 }
2541
Chris Lattner978fe0c2012-01-30 06:21:21 +00002542 if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
Justin Bogner0ebc8602015-12-08 03:01:16 +00002543 if (CFP->getType()->isHalfTy()) {
2544 SmallVector<uint16_t, 16> Elts(
2545 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
2546 return getFP(V->getContext(), Elts);
2547 }
Chris Lattner978fe0c2012-01-30 06:21:21 +00002548 if (CFP->getType()->isFloatTy()) {
Rafael Espindola8c97e192015-02-19 16:08:20 +00002549 SmallVector<uint32_t, 16> Elts(
2550 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
2551 return getFP(V->getContext(), Elts);
Chris Lattner978fe0c2012-01-30 06:21:21 +00002552 }
2553 if (CFP->getType()->isDoubleTy()) {
Rafael Espindola8c97e192015-02-19 16:08:20 +00002554 SmallVector<uint64_t, 16> Elts(
2555 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
2556 return getFP(V->getContext(), Elts);
Chris Lattner978fe0c2012-01-30 06:21:21 +00002557 }
Chris Lattnere9eed292012-01-25 05:19:54 +00002558 }
Chris Lattner978fe0c2012-01-30 06:21:21 +00002559 return ConstantVector::getSplat(NumElts, V);
Chris Lattnere9eed292012-01-25 05:19:54 +00002560}
2561
2562
Chris Lattnere4f3f102012-01-24 04:43:41 +00002563uint64_t ConstantDataSequential::getElementAsInteger(unsigned Elt) const {
2564 assert(isa<IntegerType>(getElementType()) &&
2565 "Accessor can only be used when element is an integer");
2566 const char *EltPtr = getElementPointer(Elt);
Galina Kistanovafc259902012-07-13 01:25:27 +00002567
Chris Lattnere4f3f102012-01-24 04:43:41 +00002568 // The data is stored in host byte order, make sure to cast back to the right
2569 // type to load with the right endianness.
Chris Lattner8326bd82012-01-26 00:42:34 +00002570 switch (getElementType()->getIntegerBitWidth()) {
Craig Topperc514b542012-02-05 22:14:15 +00002571 default: llvm_unreachable("Invalid bitwidth for CDS");
Galina Kistanovafc259902012-07-13 01:25:27 +00002572 case 8:
Craig Topper393ce692017-07-11 15:52:21 +00002573 return *reinterpret_cast<const uint8_t *>(EltPtr);
Galina Kistanovafc259902012-07-13 01:25:27 +00002574 case 16:
Craig Topper393ce692017-07-11 15:52:21 +00002575 return *reinterpret_cast<const uint16_t *>(EltPtr);
Galina Kistanovafc259902012-07-13 01:25:27 +00002576 case 32:
Craig Topper393ce692017-07-11 15:52:21 +00002577 return *reinterpret_cast<const uint32_t *>(EltPtr);
Galina Kistanovafc259902012-07-13 01:25:27 +00002578 case 64:
Craig Topper393ce692017-07-11 15:52:21 +00002579 return *reinterpret_cast<const uint64_t *>(EltPtr);
Chris Lattnere4f3f102012-01-24 04:43:41 +00002580 }
2581}
2582
Craig Topper0b4b4e32017-07-15 22:06:19 +00002583APInt ConstantDataSequential::getElementAsAPInt(unsigned Elt) const {
2584 assert(isa<IntegerType>(getElementType()) &&
2585 "Accessor can only be used when element is an integer");
2586 const char *EltPtr = getElementPointer(Elt);
2587
2588 // The data is stored in host byte order, make sure to cast back to the right
2589 // type to load with the right endianness.
2590 switch (getElementType()->getIntegerBitWidth()) {
2591 default: llvm_unreachable("Invalid bitwidth for CDS");
2592 case 8: {
2593 auto EltVal = *reinterpret_cast<const uint8_t *>(EltPtr);
2594 return APInt(8, EltVal);
2595 }
2596 case 16: {
2597 auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr);
2598 return APInt(16, EltVal);
2599 }
2600 case 32: {
2601 auto EltVal = *reinterpret_cast<const uint32_t *>(EltPtr);
2602 return APInt(32, EltVal);
2603 }
2604 case 64: {
2605 auto EltVal = *reinterpret_cast<const uint64_t *>(EltPtr);
2606 return APInt(64, EltVal);
2607 }
2608 }
2609}
2610
Chris Lattnere4f3f102012-01-24 04:43:41 +00002611APFloat ConstantDataSequential::getElementAsAPFloat(unsigned Elt) const {
2612 const char *EltPtr = getElementPointer(Elt);
2613
2614 switch (getElementType()->getTypeID()) {
Nick Lewyckyff509622012-01-25 03:20:12 +00002615 default:
Craig Topperc514b542012-02-05 22:14:15 +00002616 llvm_unreachable("Accessor can only be used when element is float/double!");
Justin Bogner0ebc8602015-12-08 03:01:16 +00002617 case Type::HalfTyID: {
2618 auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr);
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002619 return APFloat(APFloat::IEEEhalf(), APInt(16, EltVal));
Justin Bogner0ebc8602015-12-08 03:01:16 +00002620 }
Benjamin Kramer7af984b2015-02-20 15:11:55 +00002621 case Type::FloatTyID: {
2622 auto EltVal = *reinterpret_cast<const uint32_t *>(EltPtr);
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002623 return APFloat(APFloat::IEEEsingle(), APInt(32, EltVal));
Benjamin Kramer7af984b2015-02-20 15:11:55 +00002624 }
2625 case Type::DoubleTyID: {
2626 auto EltVal = *reinterpret_cast<const uint64_t *>(EltPtr);
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002627 return APFloat(APFloat::IEEEdouble(), APInt(64, EltVal));
Chris Lattnere4f3f102012-01-24 04:43:41 +00002628 }
Benjamin Kramer7af984b2015-02-20 15:11:55 +00002629 }
Chris Lattnere4f3f102012-01-24 04:43:41 +00002630}
2631
Chris Lattnere4f3f102012-01-24 04:43:41 +00002632float ConstantDataSequential::getElementAsFloat(unsigned Elt) const {
2633 assert(getElementType()->isFloatTy() &&
2634 "Accessor can only be used when element is a 'float'");
Craig Topper393ce692017-07-11 15:52:21 +00002635 return *reinterpret_cast<const float *>(getElementPointer(Elt));
Chris Lattnere4f3f102012-01-24 04:43:41 +00002636}
2637
Chris Lattnere4f3f102012-01-24 04:43:41 +00002638double ConstantDataSequential::getElementAsDouble(unsigned Elt) const {
2639 assert(getElementType()->isDoubleTy() &&
2640 "Accessor can only be used when element is a 'float'");
Craig Topper393ce692017-07-11 15:52:21 +00002641 return *reinterpret_cast<const double *>(getElementPointer(Elt));
Chris Lattnere4f3f102012-01-24 04:43:41 +00002642}
2643
Chris Lattnere4f3f102012-01-24 04:43:41 +00002644Constant *ConstantDataSequential::getElementAsConstant(unsigned Elt) const {
Justin Bogner0ebc8602015-12-08 03:01:16 +00002645 if (getElementType()->isHalfTy() || getElementType()->isFloatTy() ||
2646 getElementType()->isDoubleTy())
Chris Lattnere4f3f102012-01-24 04:43:41 +00002647 return ConstantFP::get(getContext(), getElementAsAPFloat(Elt));
Galina Kistanovafc259902012-07-13 01:25:27 +00002648
Chris Lattnere4f3f102012-01-24 04:43:41 +00002649 return ConstantInt::get(getElementType(), getElementAsInteger(Elt));
2650}
2651
Matthias Braun50ec0b52017-05-19 22:37:09 +00002652bool ConstantDataSequential::isString(unsigned CharSize) const {
2653 return isa<ArrayType>(getType()) && getElementType()->isIntegerTy(CharSize);
Chris Lattner5dd4d872012-01-24 09:01:07 +00002654}
Chris Lattner3756b912012-01-23 22:57:10 +00002655
Chris Lattner5dd4d872012-01-24 09:01:07 +00002656bool ConstantDataSequential::isCString() const {
2657 if (!isString())
2658 return false;
Galina Kistanovafc259902012-07-13 01:25:27 +00002659
Chris Lattner5dd4d872012-01-24 09:01:07 +00002660 StringRef Str = getAsString();
Galina Kistanovafc259902012-07-13 01:25:27 +00002661
Chris Lattner5dd4d872012-01-24 09:01:07 +00002662 // The last value must be nul.
2663 if (Str.back() != 0) return false;
Galina Kistanovafc259902012-07-13 01:25:27 +00002664
Chris Lattner5dd4d872012-01-24 09:01:07 +00002665 // Other elements must be non-nul.
2666 return Str.drop_back().find(0) == StringRef::npos;
2667}
Chris Lattner3756b912012-01-23 22:57:10 +00002668
Craig Topper0b4b4e32017-07-15 22:06:19 +00002669bool ConstantDataVector::isSplat() const {
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002670 const char *Base = getRawDataValues().data();
Galina Kistanovafc259902012-07-13 01:25:27 +00002671
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002672 // Compare elements 1+ to the 0'th element.
2673 unsigned EltSize = getElementByteSize();
2674 for (unsigned i = 1, e = getNumElements(); i != e; ++i)
2675 if (memcmp(Base, Base+i*EltSize, EltSize))
Craig Topper0b4b4e32017-07-15 22:06:19 +00002676 return false;
Galina Kistanovafc259902012-07-13 01:25:27 +00002677
Craig Topper0b4b4e32017-07-15 22:06:19 +00002678 return true;
2679}
2680
2681Constant *ConstantDataVector::getSplatValue() const {
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002682 // If they're all the same, return the 0th one as a representative.
Craig Topper0b4b4e32017-07-15 22:06:19 +00002683 return isSplat() ? getElementAsConstant(0) : nullptr;
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002684}
Chris Lattnera3b94ba2010-03-30 20:48:48 +00002685
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002686//===----------------------------------------------------------------------===//
Pete Cooper5815b1f2015-06-24 18:55:24 +00002687// handleOperandChange implementations
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002688
Pete Cooper5815b1f2015-06-24 18:55:24 +00002689/// Update this constant array to change uses of
Chris Lattner913849b2007-08-21 00:55:23 +00002690/// 'From' to be uses of 'To'. This must update the uniquing data structures
2691/// etc.
2692///
2693/// Note that we intentionally replace all uses of From with To here. Consider
2694/// a large array that uses 'From' 1000 times. By handling this case all here,
Pete Cooper5815b1f2015-06-24 18:55:24 +00002695/// ConstantArray::handleOperandChange is only invoked once, and that
Chris Lattner913849b2007-08-21 00:55:23 +00002696/// single invocation handles all 1000 uses. Handling them one at a time would
2697/// work, but would be really slow because it would have to unique each updated
2698/// array instance.
Chris Lattner31b132c2009-10-28 00:01:44 +00002699///
Mehdi Amini8914d292016-02-10 22:47:15 +00002700void Constant::handleOperandChange(Value *From, Value *To) {
Pete Cooper5815b1f2015-06-24 18:55:24 +00002701 Value *Replacement = nullptr;
2702 switch (getValueID()) {
2703 default:
2704 llvm_unreachable("Not a constant!");
2705#define HANDLE_CONSTANT(Name) \
2706 case Value::Name##Val: \
Mehdi Amini8914d292016-02-10 22:47:15 +00002707 Replacement = cast<Name>(this)->handleOperandChangeImpl(From, To); \
Pete Cooper5815b1f2015-06-24 18:55:24 +00002708 break;
2709#include "llvm/IR/Value.def"
2710 }
2711
2712 // If handleOperandChangeImpl returned nullptr, then it handled
2713 // replacing itself and we don't want to delete or replace anything else here.
2714 if (!Replacement)
2715 return;
2716
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002717 // I do need to replace this with an existing value.
2718 assert(Replacement != this && "I didn't contain From!");
2719
2720 // Everyone using this now uses the replacement.
2721 replaceAllUsesWith(Replacement);
2722
2723 // Delete the old constant!
2724 destroyConstant();
2725}
2726
Mehdi Amini8914d292016-02-10 22:47:15 +00002727Value *ConstantArray::handleOperandChangeImpl(Value *From, Value *To) {
Owen Andersonc2c79322009-07-28 18:32:17 +00002728 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2729 Constant *ToC = cast<Constant>(To);
2730
Talin46e9b442012-02-05 20:54:10 +00002731 SmallVector<Constant*, 8> Values;
Owen Andersonc2c79322009-07-28 18:32:17 +00002732 Values.reserve(getNumOperands()); // Build replacement array.
2733
Galina Kistanovafc259902012-07-13 01:25:27 +00002734 // Fill values with the modified operands of the constant array. Also,
Owen Andersonc2c79322009-07-28 18:32:17 +00002735 // compute whether this turns into an all-zeros array.
Owen Andersonc2c79322009-07-28 18:32:17 +00002736 unsigned NumUpdated = 0;
Galina Kistanovafc259902012-07-13 01:25:27 +00002737
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002738 // Keep track of whether all the values in the array are "ToC".
2739 bool AllSame = true;
Pete Cooper74510a42015-06-12 17:48:05 +00002740 Use *OperandList = getOperandList();
Mehdi Amini8914d292016-02-10 22:47:15 +00002741 unsigned OperandNo = 0;
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002742 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2743 Constant *Val = cast<Constant>(O->get());
2744 if (Val == From) {
Mehdi Amini8914d292016-02-10 22:47:15 +00002745 OperandNo = (O - OperandList);
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002746 Val = ToC;
2747 ++NumUpdated;
Owen Andersonc2c79322009-07-28 18:32:17 +00002748 }
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002749 Values.push_back(Val);
Talin46e9b442012-02-05 20:54:10 +00002750 AllSame &= Val == ToC;
Owen Andersonc2c79322009-07-28 18:32:17 +00002751 }
Galina Kistanovafc259902012-07-13 01:25:27 +00002752
Pete Cooper5815b1f2015-06-24 18:55:24 +00002753 if (AllSame && ToC->isNullValue())
2754 return ConstantAggregateZero::get(getType());
2755
2756 if (AllSame && isa<UndefValue>(ToC))
2757 return UndefValue::get(getType());
Aaron Ballmane4b91dc2014-08-19 14:59:02 +00002758
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002759 // Check for any other type of constant-folding.
Pete Cooper5815b1f2015-06-24 18:55:24 +00002760 if (Constant *C = getImpl(getType(), Values))
2761 return C;
Aaron Ballmane4b91dc2014-08-19 14:59:02 +00002762
Duncan P. N. Exon Smith909620a2014-08-19 19:13:30 +00002763 // Update to the new value.
Pete Cooper5815b1f2015-06-24 18:55:24 +00002764 return getContext().pImpl->ArrayConstants.replaceOperandsInPlace(
Mehdi Amini8914d292016-02-10 22:47:15 +00002765 Values, this, From, ToC, NumUpdated, OperandNo);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002766}
2767
Mehdi Amini8914d292016-02-10 22:47:15 +00002768Value *ConstantStruct::handleOperandChangeImpl(Value *From, Value *To) {
Owen Anderson45308b52009-07-27 22:29:26 +00002769 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2770 Constant *ToC = cast<Constant>(To);
2771
Pete Cooper74510a42015-06-12 17:48:05 +00002772 Use *OperandList = getOperandList();
Owen Anderson45308b52009-07-27 22:29:26 +00002773
Talin46e9b442012-02-05 20:54:10 +00002774 SmallVector<Constant*, 8> Values;
Owen Anderson45308b52009-07-27 22:29:26 +00002775 Values.reserve(getNumOperands()); // Build replacement struct.
Galina Kistanovafc259902012-07-13 01:25:27 +00002776
2777 // Fill values with the modified operands of the constant struct. Also,
Owen Anderson45308b52009-07-27 22:29:26 +00002778 // compute whether this turns into an all-zeros struct.
Mehdi Amini8914d292016-02-10 22:47:15 +00002779 unsigned NumUpdated = 0;
2780 bool AllSame = true;
2781 unsigned OperandNo = 0;
2782 for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O) {
2783 Constant *Val = cast<Constant>(O->get());
2784 if (Val == From) {
2785 OperandNo = (O - OperandList);
2786 Val = ToC;
2787 ++NumUpdated;
Owen Anderson45308b52009-07-27 22:29:26 +00002788 }
Mehdi Amini8914d292016-02-10 22:47:15 +00002789 Values.push_back(Val);
2790 AllSame &= Val == ToC;
Owen Anderson45308b52009-07-27 22:29:26 +00002791 }
Galina Kistanovafc259902012-07-13 01:25:27 +00002792
Mehdi Amini8914d292016-02-10 22:47:15 +00002793 if (AllSame && ToC->isNullValue())
Pete Cooper5815b1f2015-06-24 18:55:24 +00002794 return ConstantAggregateZero::get(getType());
2795
Mehdi Amini8914d292016-02-10 22:47:15 +00002796 if (AllSame && isa<UndefValue>(ToC))
Pete Cooper5815b1f2015-06-24 18:55:24 +00002797 return UndefValue::get(getType());
Galina Kistanovafc259902012-07-13 01:25:27 +00002798
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002799 // Update to the new value.
Pete Cooper5815b1f2015-06-24 18:55:24 +00002800 return getContext().pImpl->StructConstants.replaceOperandsInPlace(
Mehdi Amini8914d292016-02-10 22:47:15 +00002801 Values, this, From, ToC, NumUpdated, OperandNo);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002802}
2803
Mehdi Amini8914d292016-02-10 22:47:15 +00002804Value *ConstantVector::handleOperandChangeImpl(Value *From, Value *To) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002805 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002806 Constant *ToC = cast<Constant>(To);
Galina Kistanovafc259902012-07-13 01:25:27 +00002807
Chris Lattnera474bb22012-01-26 20:40:56 +00002808 SmallVector<Constant*, 8> Values;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002809 Values.reserve(getNumOperands()); // Build replacement array...
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002810 unsigned NumUpdated = 0;
Mehdi Amini8914d292016-02-10 22:47:15 +00002811 unsigned OperandNo = 0;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002812 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2813 Constant *Val = getOperand(i);
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002814 if (Val == From) {
Mehdi Amini8914d292016-02-10 22:47:15 +00002815 OperandNo = i;
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002816 ++NumUpdated;
2817 Val = ToC;
2818 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002819 Values.push_back(Val);
2820 }
Galina Kistanovafc259902012-07-13 01:25:27 +00002821
Pete Cooper5815b1f2015-06-24 18:55:24 +00002822 if (Constant *C = getImpl(Values))
2823 return C;
Duncan P. N. Exon Smith687744d2014-08-19 02:24:46 +00002824
Duncan P. N. Exon Smith909620a2014-08-19 19:13:30 +00002825 // Update to the new value.
Pete Cooper5815b1f2015-06-24 18:55:24 +00002826 return getContext().pImpl->VectorConstants.replaceOperandsInPlace(
Mehdi Amini8914d292016-02-10 22:47:15 +00002827 Values, this, From, ToC, NumUpdated, OperandNo);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002828}
2829
Mehdi Amini8914d292016-02-10 22:47:15 +00002830Value *ConstantExpr::handleOperandChangeImpl(Value *From, Value *ToV) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002831 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2832 Constant *To = cast<Constant>(ToV);
Galina Kistanovafc259902012-07-13 01:25:27 +00002833
Chris Lattner37e38352012-01-26 20:37:11 +00002834 SmallVector<Constant*, 8> NewOps;
Duncan P. N. Exon Smith33de00c2014-08-19 20:03:35 +00002835 unsigned NumUpdated = 0;
Mehdi Amini8914d292016-02-10 22:47:15 +00002836 unsigned OperandNo = 0;
Chris Lattner37e38352012-01-26 20:37:11 +00002837 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2838 Constant *Op = getOperand(i);
Duncan P. N. Exon Smith33de00c2014-08-19 20:03:35 +00002839 if (Op == From) {
Mehdi Amini8914d292016-02-10 22:47:15 +00002840 OperandNo = i;
Duncan P. N. Exon Smith33de00c2014-08-19 20:03:35 +00002841 ++NumUpdated;
2842 Op = To;
2843 }
2844 NewOps.push_back(Op);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002845 }
Duncan P. N. Exon Smith33de00c2014-08-19 20:03:35 +00002846 assert(NumUpdated && "I didn't contain From!");
Galina Kistanovafc259902012-07-13 01:25:27 +00002847
Pete Cooper5815b1f2015-06-24 18:55:24 +00002848 if (Constant *C = getWithOperands(NewOps, getType(), true))
2849 return C;
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002850
Duncan P. N. Exon Smith33de00c2014-08-19 20:03:35 +00002851 // Update to the new value.
Pete Cooper5815b1f2015-06-24 18:55:24 +00002852 return getContext().pImpl->ExprConstants.replaceOperandsInPlace(
Mehdi Amini8914d292016-02-10 22:47:15 +00002853 NewOps, this, From, To, NumUpdated, OperandNo);
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002854}
2855
James Molloyce545682012-11-17 17:56:30 +00002856Instruction *ConstantExpr::getAsInstruction() {
Benjamin Kramer5fbfe2f2015-02-28 13:20:15 +00002857 SmallVector<Value *, 4> ValueOperands(op_begin(), op_end());
James Molloyce545682012-11-17 17:56:30 +00002858 ArrayRef<Value*> Ops(ValueOperands);
2859
2860 switch (getOpcode()) {
2861 case Instruction::Trunc:
2862 case Instruction::ZExt:
2863 case Instruction::SExt:
2864 case Instruction::FPTrunc:
2865 case Instruction::FPExt:
2866 case Instruction::UIToFP:
2867 case Instruction::SIToFP:
2868 case Instruction::FPToUI:
2869 case Instruction::FPToSI:
2870 case Instruction::PtrToInt:
2871 case Instruction::IntToPtr:
2872 case Instruction::BitCast:
Eli Bendersky157a97a2014-01-18 22:54:33 +00002873 case Instruction::AddrSpaceCast:
James Molloyce545682012-11-17 17:56:30 +00002874 return CastInst::Create((Instruction::CastOps)getOpcode(),
2875 Ops[0], getType());
2876 case Instruction::Select:
2877 return SelectInst::Create(Ops[0], Ops[1], Ops[2]);
2878 case Instruction::InsertElement:
2879 return InsertElementInst::Create(Ops[0], Ops[1], Ops[2]);
2880 case Instruction::ExtractElement:
2881 return ExtractElementInst::Create(Ops[0], Ops[1]);
2882 case Instruction::InsertValue:
2883 return InsertValueInst::Create(Ops[0], Ops[1], getIndices());
2884 case Instruction::ExtractValue:
2885 return ExtractValueInst::Create(Ops[0], getIndices());
2886 case Instruction::ShuffleVector:
2887 return new ShuffleVectorInst(Ops[0], Ops[1], Ops[2]);
2888
David Blaikie741c8f82015-03-14 01:53:18 +00002889 case Instruction::GetElementPtr: {
2890 const auto *GO = cast<GEPOperator>(this);
2891 if (GO->isInBounds())
2892 return GetElementPtrInst::CreateInBounds(GO->getSourceElementType(),
2893 Ops[0], Ops.slice(1));
2894 return GetElementPtrInst::Create(GO->getSourceElementType(), Ops[0],
2895 Ops.slice(1));
2896 }
James Molloyce545682012-11-17 17:56:30 +00002897 case Instruction::ICmp:
2898 case Instruction::FCmp:
2899 return CmpInst::Create((Instruction::OtherOps)getOpcode(),
Craig Topper1c3f2832015-12-15 06:11:33 +00002900 (CmpInst::Predicate)getPredicate(), Ops[0], Ops[1]);
James Molloyce545682012-11-17 17:56:30 +00002901
2902 default:
2903 assert(getNumOperands() == 2 && "Must be binary operator?");
2904 BinaryOperator *BO =
2905 BinaryOperator::Create((Instruction::BinaryOps)getOpcode(),
2906 Ops[0], Ops[1]);
2907 if (isa<OverflowingBinaryOperator>(BO)) {
2908 BO->setHasNoUnsignedWrap(SubclassOptionalData &
2909 OverflowingBinaryOperator::NoUnsignedWrap);
2910 BO->setHasNoSignedWrap(SubclassOptionalData &
2911 OverflowingBinaryOperator::NoSignedWrap);
2912 }
2913 if (isa<PossiblyExactOperator>(BO))
2914 BO->setIsExact(SubclassOptionalData & PossiblyExactOperator::IsExact);
2915 return BO;
2916 }
2917}