blob: a601f54a359197a1f11c1180dff7ee8b9ea793eb [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
Sanjay Patel6a0f6672018-02-15 13:55:52 +0000638Constant *ConstantFP::get(Type *Ty, const APFloat &V) {
639 ConstantFP *C = get(Ty->getContext(), V);
640 assert(C->getType() == Ty->getScalarType() &&
641 "ConstantFP type doesn't match the type implied by its value!");
642
643 // For vectors, broadcast the value.
644 if (auto *VTy = dyn_cast<VectorType>(Ty))
645 return ConstantVector::getSplat(VTy->getNumElements(), C);
646
647 return C;
648}
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000649
Chris Lattner0256be92012-01-27 03:08:05 +0000650Constant *ConstantFP::get(Type *Ty, StringRef Str) {
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000651 LLVMContext &Context = Ty->getContext();
652
653 APFloat FV(*TypeToFloatSemantics(Ty->getScalarType()), Str);
654 Constant *C = get(Context, FV);
655
656 // For vectors, broadcast the value.
Chris Lattner229907c2011-07-18 04:54:35 +0000657 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
Chris Lattnere9eed292012-01-25 05:19:54 +0000658 return ConstantVector::getSplat(VTy->getNumElements(), C);
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000659
660 return C;
661}
662
Tom Stellard67246d12015-04-20 19:38:24 +0000663Constant *ConstantFP::getNaN(Type *Ty, bool Negative, unsigned Type) {
664 const fltSemantics &Semantics = *TypeToFloatSemantics(Ty->getScalarType());
665 APFloat NaN = APFloat::getNaN(Semantics, Negative, Type);
666 Constant *C = get(Ty->getContext(), NaN);
667
668 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
669 return ConstantVector::getSplat(VTy->getNumElements(), C);
670
671 return C;
672}
673
Benjamin Kramer5d2ff222014-01-18 16:43:06 +0000674Constant *ConstantFP::getNegativeZero(Type *Ty) {
675 const fltSemantics &Semantics = *TypeToFloatSemantics(Ty->getScalarType());
676 APFloat NegZero = APFloat::getZero(Semantics, /*Negative=*/true);
677 Constant *C = get(Ty->getContext(), NegZero);
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000678
Benjamin Kramer5d2ff222014-01-18 16:43:06 +0000679 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
680 return ConstantVector::getSplat(VTy->getNumElements(), C);
681
682 return C;
Owen Anderson69c464d2009-07-27 20:59:43 +0000683}
684
685
Chris Lattnere9eed292012-01-25 05:19:54 +0000686Constant *ConstantFP::getZeroValueForNegation(Type *Ty) {
Benjamin Kramer5d2ff222014-01-18 16:43:06 +0000687 if (Ty->isFPOrFPVectorTy())
688 return getNegativeZero(Ty);
Owen Anderson69c464d2009-07-27 20:59:43 +0000689
Owen Anderson5a1acd92009-07-31 20:28:14 +0000690 return Constant::getNullValue(Ty);
Owen Anderson69c464d2009-07-27 20:59:43 +0000691}
692
693
694// ConstantFP accessors.
695ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) {
Owen Anderson69c464d2009-07-27 20:59:43 +0000696 LLVMContextImpl* pImpl = Context.pImpl;
Galina Kistanovafc259902012-07-13 01:25:27 +0000697
Justin Lebar611c5c22016-10-10 16:26:13 +0000698 std::unique_ptr<ConstantFP> &Slot = pImpl->FPConstants[V];
Galina Kistanovafc259902012-07-13 01:25:27 +0000699
Owen Anderson69c464d2009-07-27 20:59:43 +0000700 if (!Slot) {
Chris Lattner229907c2011-07-18 04:54:35 +0000701 Type *Ty;
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000702 if (&V.getSemantics() == &APFloat::IEEEhalf())
Dan Gohman518cda42011-12-17 00:04:22 +0000703 Ty = Type::getHalfTy(Context);
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000704 else if (&V.getSemantics() == &APFloat::IEEEsingle())
Owen Anderson5dab84c2009-10-19 20:11:52 +0000705 Ty = Type::getFloatTy(Context);
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000706 else if (&V.getSemantics() == &APFloat::IEEEdouble())
Owen Anderson5dab84c2009-10-19 20:11:52 +0000707 Ty = Type::getDoubleTy(Context);
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000708 else if (&V.getSemantics() == &APFloat::x87DoubleExtended())
Owen Anderson5dab84c2009-10-19 20:11:52 +0000709 Ty = Type::getX86_FP80Ty(Context);
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000710 else if (&V.getSemantics() == &APFloat::IEEEquad())
Owen Anderson5dab84c2009-10-19 20:11:52 +0000711 Ty = Type::getFP128Ty(Context);
712 else {
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000713 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble() &&
Owen Anderson5dab84c2009-10-19 20:11:52 +0000714 "Unknown FP format");
715 Ty = Type::getPPC_FP128Ty(Context);
Owen Anderson69c464d2009-07-27 20:59:43 +0000716 }
Justin Lebar611c5c22016-10-10 16:26:13 +0000717 Slot.reset(new ConstantFP(Ty, V));
Owen Anderson69c464d2009-07-27 20:59:43 +0000718 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000719
Justin Lebar611c5c22016-10-10 16:26:13 +0000720 return Slot.get();
Owen Anderson69c464d2009-07-27 20:59:43 +0000721}
722
Benjamin Kramer5d2ff222014-01-18 16:43:06 +0000723Constant *ConstantFP::getInfinity(Type *Ty, bool Negative) {
724 const fltSemantics &Semantics = *TypeToFloatSemantics(Ty->getScalarType());
725 Constant *C = get(Ty->getContext(), APFloat::getInf(Semantics, Negative));
726
727 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
728 return ConstantVector::getSplat(VTy->getNumElements(), C);
729
730 return C;
Dan Gohmanfeb50212009-09-25 23:00:48 +0000731}
732
Duncan P. N. Exon Smithb6452792016-02-21 02:39:49 +0000733ConstantFP::ConstantFP(Type *Ty, const APFloat &V)
734 : ConstantData(Ty, ConstantFPVal), Val(V) {
Chris Lattner98bd9392008-04-09 06:38:30 +0000735 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
736 "FP type Mismatch");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000737}
738
Chris Lattnerbe6610c2011-07-15 06:14:08 +0000739bool ConstantFP::isExactlyValue(const APFloat &V) const {
Dale Johannesend246b2c2007-08-30 00:23:21 +0000740 return Val.bitwiseIsEqual(V);
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000741}
742
Pete Cooper86dd4cf2015-06-23 21:55:11 +0000743/// Remove the constant from the constant table.
744void ConstantFP::destroyConstantImpl() {
Craig Topperccbb8102017-06-27 19:57:51 +0000745 llvm_unreachable("You can't ConstantFP->destroyConstantImpl()!");
Pete Cooper86dd4cf2015-06-23 21:55:11 +0000746}
747
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000748//===----------------------------------------------------------------------===//
Chris Lattner030af792012-01-24 05:42:11 +0000749// ConstantAggregateZero Implementation
750//===----------------------------------------------------------------------===//
751
Chris Lattner7e683d12012-01-25 06:16:32 +0000752Constant *ConstantAggregateZero::getSequentialElement() const {
Chris Lattner8326bd82012-01-26 00:42:34 +0000753 return Constant::getNullValue(getType()->getSequentialElementType());
Chris Lattner030af792012-01-24 05:42:11 +0000754}
755
Chris Lattner7e683d12012-01-25 06:16:32 +0000756Constant *ConstantAggregateZero::getStructElement(unsigned Elt) const {
Chris Lattner8326bd82012-01-26 00:42:34 +0000757 return Constant::getNullValue(getType()->getStructElementType(Elt));
Chris Lattner030af792012-01-24 05:42:11 +0000758}
759
Chris Lattner7e683d12012-01-25 06:16:32 +0000760Constant *ConstantAggregateZero::getElementValue(Constant *C) const {
Chris Lattner030af792012-01-24 05:42:11 +0000761 if (isa<SequentialType>(getType()))
762 return getSequentialElement();
763 return getStructElement(cast<ConstantInt>(C)->getZExtValue());
764}
765
Chris Lattner7e683d12012-01-25 06:16:32 +0000766Constant *ConstantAggregateZero::getElementValue(unsigned Idx) const {
Chris Lattnerf7eb5432012-01-24 07:54:10 +0000767 if (isa<SequentialType>(getType()))
768 return getSequentialElement();
769 return getStructElement(Idx);
770}
771
David Majnemer9b529a72015-02-16 04:02:09 +0000772unsigned ConstantAggregateZero::getNumElements() const {
Craig Toppere3dcce92015-08-01 22:20:21 +0000773 Type *Ty = getType();
774 if (auto *AT = dyn_cast<ArrayType>(Ty))
David Majnemer9b529a72015-02-16 04:02:09 +0000775 return AT->getNumElements();
Craig Toppere3dcce92015-08-01 22:20:21 +0000776 if (auto *VT = dyn_cast<VectorType>(Ty))
David Majnemer9b529a72015-02-16 04:02:09 +0000777 return VT->getNumElements();
778 return Ty->getStructNumElements();
779}
Chris Lattnerf7eb5432012-01-24 07:54:10 +0000780
Chris Lattner030af792012-01-24 05:42:11 +0000781//===----------------------------------------------------------------------===//
782// UndefValue Implementation
783//===----------------------------------------------------------------------===//
784
Chris Lattner7e683d12012-01-25 06:16:32 +0000785UndefValue *UndefValue::getSequentialElement() const {
Chris Lattner8326bd82012-01-26 00:42:34 +0000786 return UndefValue::get(getType()->getSequentialElementType());
Chris Lattner030af792012-01-24 05:42:11 +0000787}
788
Chris Lattner7e683d12012-01-25 06:16:32 +0000789UndefValue *UndefValue::getStructElement(unsigned Elt) const {
Chris Lattner8326bd82012-01-26 00:42:34 +0000790 return UndefValue::get(getType()->getStructElementType(Elt));
Chris Lattner030af792012-01-24 05:42:11 +0000791}
792
Chris Lattner7e683d12012-01-25 06:16:32 +0000793UndefValue *UndefValue::getElementValue(Constant *C) const {
Chris Lattner030af792012-01-24 05:42:11 +0000794 if (isa<SequentialType>(getType()))
795 return getSequentialElement();
796 return getStructElement(cast<ConstantInt>(C)->getZExtValue());
797}
798
Chris Lattner7e683d12012-01-25 06:16:32 +0000799UndefValue *UndefValue::getElementValue(unsigned Idx) const {
Chris Lattnerf7eb5432012-01-24 07:54:10 +0000800 if (isa<SequentialType>(getType()))
801 return getSequentialElement();
802 return getStructElement(Idx);
803}
804
David Majnemer9b529a72015-02-16 04:02:09 +0000805unsigned UndefValue::getNumElements() const {
Craig Toppere3dcce92015-08-01 22:20:21 +0000806 Type *Ty = getType();
Peter Collingbournebc070522016-12-02 03:20:58 +0000807 if (auto *ST = dyn_cast<SequentialType>(Ty))
808 return ST->getNumElements();
David Majnemer9b529a72015-02-16 04:02:09 +0000809 return Ty->getStructNumElements();
810}
Chris Lattner030af792012-01-24 05:42:11 +0000811
812//===----------------------------------------------------------------------===//
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000813// ConstantXXX Classes
814//===----------------------------------------------------------------------===//
815
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000816template <typename ItTy, typename EltTy>
817static bool rangeOnlyContains(ItTy Start, ItTy End, EltTy Elt) {
818 for (; Start != End; ++Start)
819 if (*Start != Elt)
820 return false;
821 return true;
822}
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000823
Justin Bogner909e1c02015-12-01 20:20:49 +0000824template <typename SequentialTy, typename ElementTy>
825static Constant *getIntSequenceIfElementsMatch(ArrayRef<Constant *> V) {
826 assert(!V.empty() && "Cannot get empty int sequence.");
827
828 SmallVector<ElementTy, 16> Elts;
829 for (Constant *C : V)
830 if (auto *CI = dyn_cast<ConstantInt>(C))
831 Elts.push_back(CI->getZExtValue());
832 else
833 return nullptr;
834 return SequentialTy::get(V[0]->getContext(), Elts);
835}
836
837template <typename SequentialTy, typename ElementTy>
838static Constant *getFPSequenceIfElementsMatch(ArrayRef<Constant *> V) {
839 assert(!V.empty() && "Cannot get empty FP sequence.");
840
841 SmallVector<ElementTy, 16> Elts;
842 for (Constant *C : V)
843 if (auto *CFP = dyn_cast<ConstantFP>(C))
844 Elts.push_back(CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
845 else
846 return nullptr;
847 return SequentialTy::getFP(V[0]->getContext(), Elts);
848}
849
850template <typename SequenceTy>
851static Constant *getSequenceIfElementsMatch(Constant *C,
852 ArrayRef<Constant *> V) {
853 // We speculatively build the elements here even if it turns out that there is
854 // a constantexpr or something else weird, since it is so uncommon for that to
855 // happen.
856 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
857 if (CI->getType()->isIntegerTy(8))
858 return getIntSequenceIfElementsMatch<SequenceTy, uint8_t>(V);
859 else if (CI->getType()->isIntegerTy(16))
860 return getIntSequenceIfElementsMatch<SequenceTy, uint16_t>(V);
861 else if (CI->getType()->isIntegerTy(32))
862 return getIntSequenceIfElementsMatch<SequenceTy, uint32_t>(V);
863 else if (CI->getType()->isIntegerTy(64))
864 return getIntSequenceIfElementsMatch<SequenceTy, uint64_t>(V);
865 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
Justin Bogner0ebc8602015-12-08 03:01:16 +0000866 if (CFP->getType()->isHalfTy())
867 return getFPSequenceIfElementsMatch<SequenceTy, uint16_t>(V);
868 else if (CFP->getType()->isFloatTy())
Justin Bogner909e1c02015-12-01 20:20:49 +0000869 return getFPSequenceIfElementsMatch<SequenceTy, uint32_t>(V);
870 else if (CFP->getType()->isDoubleTy())
871 return getFPSequenceIfElementsMatch<SequenceTy, uint64_t>(V);
872 }
873
874 return nullptr;
875}
876
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +0000877ConstantAggregate::ConstantAggregate(CompositeType *T, ValueTy VT,
878 ArrayRef<Constant *> V)
879 : Constant(T, VT, OperandTraits<ConstantAggregate>::op_end(this) - V.size(),
880 V.size()) {
Jay Foad89d9b812011-07-25 10:14:44 +0000881 std::copy(V.begin(), V.end(), op_begin());
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +0000882
883 // Check that types match, unless this is an opaque struct.
884 if (auto *ST = dyn_cast<StructType>(T))
885 if (ST->isOpaque())
886 return;
887 for (unsigned I = 0, E = V.size(); I != E; ++I)
888 assert(V[I]->getType() == T->getTypeAtIndex(I) &&
889 "Initializer for composite element doesn't match!");
890}
891
892ConstantArray::ConstantArray(ArrayType *T, ArrayRef<Constant *> V)
893 : ConstantAggregate(T, ConstantArrayVal, V) {
894 assert(V.size() == T->getNumElements() &&
895 "Invalid initializer for constant array");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000896}
897
Chris Lattner229907c2011-07-18 04:54:35 +0000898Constant *ConstantArray::get(ArrayType *Ty, ArrayRef<Constant*> V) {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000899 if (Constant *C = getImpl(Ty, V))
900 return C;
901 return Ty->getContext().pImpl->ArrayConstants.getOrCreate(Ty, V);
902}
Justin Bogner909e1c02015-12-01 20:20:49 +0000903
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000904Constant *ConstantArray::getImpl(ArrayType *Ty, ArrayRef<Constant*> V) {
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000905 // Empty arrays are canonicalized to ConstantAggregateZero.
906 if (V.empty())
907 return ConstantAggregateZero::get(Ty);
908
Jeffrey Yasskin8ce67f82009-09-30 21:08:08 +0000909 for (unsigned i = 0, e = V.size(); i != e; ++i) {
910 assert(V[i]->getType() == Ty->getElementType() &&
911 "Wrong type in array element initializer");
912 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000913
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000914 // If this is an all-zero array, return a ConstantAggregateZero object. If
915 // all undef, return an UndefValue, if "all simple", then return a
916 // ConstantDataArray.
917 Constant *C = V[0];
918 if (isa<UndefValue>(C) && rangeOnlyContains(V.begin(), V.end(), C))
919 return UndefValue::get(Ty);
Chris Lattnerf14a67f2012-01-26 02:31:22 +0000920
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000921 if (C->isNullValue() && rangeOnlyContains(V.begin(), V.end(), C))
922 return ConstantAggregateZero::get(Ty);
923
924 // Check to see if all of the elements are ConstantFP or ConstantInt and if
925 // the element type is compatible with ConstantDataVector. If so, use it.
Justin Bogner909e1c02015-12-01 20:20:49 +0000926 if (ConstantDataSequential::isElementTypeCompatible(C->getType()))
927 return getSequenceIfElementsMatch<ConstantDataArray>(C, V);
Chris Lattnerf14a67f2012-01-26 02:31:22 +0000928
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000929 // Otherwise, we really do want to create a ConstantArray.
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000930 return nullptr;
Owen Andersonc2c79322009-07-28 18:32:17 +0000931}
932
Chris Lattnercc19efa2011-06-20 04:01:31 +0000933StructType *ConstantStruct::getTypeForElements(LLVMContext &Context,
934 ArrayRef<Constant*> V,
935 bool Packed) {
Bill Wendling3ae7dd32012-02-07 01:27:51 +0000936 unsigned VecSize = V.size();
937 SmallVector<Type*, 16> EltTypes(VecSize);
938 for (unsigned i = 0; i != VecSize; ++i)
939 EltTypes[i] = V[i]->getType();
Galina Kistanovafc259902012-07-13 01:25:27 +0000940
Chris Lattnercc19efa2011-06-20 04:01:31 +0000941 return StructType::get(Context, EltTypes, Packed);
942}
943
944
945StructType *ConstantStruct::getTypeForElements(ArrayRef<Constant*> V,
946 bool Packed) {
947 assert(!V.empty() &&
948 "ConstantStruct::getTypeForElements cannot be called on empty list");
949 return getTypeForElements(V[0]->getContext(), V, Packed);
950}
951
Jay Foad89d9b812011-07-25 10:14:44 +0000952ConstantStruct::ConstantStruct(StructType *T, ArrayRef<Constant *> V)
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +0000953 : ConstantAggregate(T, ConstantStructVal, V) {
954 assert((T->isOpaque() || V.size() == T->getNumElements()) &&
955 "Invalid initializer for constant struct");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000956}
957
Owen Anderson45308b52009-07-27 22:29:26 +0000958// ConstantStruct accessors.
Chris Lattner229907c2011-07-18 04:54:35 +0000959Constant *ConstantStruct::get(StructType *ST, ArrayRef<Constant*> V) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000960 assert((ST->isOpaque() || ST->getNumElements() == V.size()) &&
961 "Incorrect # elements specified to ConstantStruct::get");
Chris Lattnerf14a67f2012-01-26 02:31:22 +0000962
963 // Create a ConstantAggregateZero value if all elements are zeros.
964 bool isZero = true;
965 bool isUndef = false;
966
967 if (!V.empty()) {
968 isUndef = isa<UndefValue>(V[0]);
969 isZero = V[0]->isNullValue();
970 if (isUndef || isZero) {
971 for (unsigned i = 0, e = V.size(); i != e; ++i) {
972 if (!V[i]->isNullValue())
973 isZero = false;
974 if (!isa<UndefValue>(V[i]))
975 isUndef = false;
976 }
977 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000978 }
Chris Lattnerf14a67f2012-01-26 02:31:22 +0000979 if (isZero)
980 return ConstantAggregateZero::get(ST);
981 if (isUndef)
982 return UndefValue::get(ST);
Galina Kistanovafc259902012-07-13 01:25:27 +0000983
Chris Lattnerf14a67f2012-01-26 02:31:22 +0000984 return ST->getContext().pImpl->StructConstants.getOrCreate(ST, V);
Owen Anderson45308b52009-07-27 22:29:26 +0000985}
986
Jay Foad89d9b812011-07-25 10:14:44 +0000987ConstantVector::ConstantVector(VectorType *T, ArrayRef<Constant *> V)
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +0000988 : ConstantAggregate(T, ConstantVectorVal, V) {
Duncan P. N. Exon Smithdb63bda2016-04-05 20:53:47 +0000989 assert(V.size() == T->getNumElements() &&
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +0000990 "Invalid initializer for constant vector");
Brian Gaeke02209042004-08-20 06:00:58 +0000991}
992
Owen Anderson4aa32952009-07-28 21:19:26 +0000993// ConstantVector accessors.
Jay Foadb8a8bed32011-06-22 09:10:19 +0000994Constant *ConstantVector::get(ArrayRef<Constant*> V) {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000995 if (Constant *C = getImpl(V))
996 return C;
997 VectorType *Ty = VectorType::get(V.front()->getType(), V.size());
998 return Ty->getContext().pImpl->VectorConstants.getOrCreate(Ty, V);
999}
Justin Bogner909e1c02015-12-01 20:20:49 +00001000
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001001Constant *ConstantVector::getImpl(ArrayRef<Constant*> V) {
Jay Foad9f32cfd2011-01-27 14:44:55 +00001002 assert(!V.empty() && "Vectors can't be empty");
Chris Lattner229907c2011-07-18 04:54:35 +00001003 VectorType *T = VectorType::get(V.front()->getType(), V.size());
Jay Foad9f32cfd2011-01-27 14:44:55 +00001004
Chris Lattner69229312011-02-15 00:14:00 +00001005 // If this is an all-undef or all-zero vector, return a
Owen Anderson4aa32952009-07-28 21:19:26 +00001006 // ConstantAggregateZero or UndefValue.
1007 Constant *C = V[0];
1008 bool isZero = C->isNullValue();
1009 bool isUndef = isa<UndefValue>(C);
1010
1011 if (isZero || isUndef) {
1012 for (unsigned i = 1, e = V.size(); i != e; ++i)
1013 if (V[i] != C) {
1014 isZero = isUndef = false;
1015 break;
1016 }
1017 }
Galina Kistanovafc259902012-07-13 01:25:27 +00001018
Owen Anderson4aa32952009-07-28 21:19:26 +00001019 if (isZero)
Owen Andersonb292b8c2009-07-30 23:03:37 +00001020 return ConstantAggregateZero::get(T);
Owen Anderson4aa32952009-07-28 21:19:26 +00001021 if (isUndef)
Owen Andersonb292b8c2009-07-30 23:03:37 +00001022 return UndefValue::get(T);
Galina Kistanovafc259902012-07-13 01:25:27 +00001023
Chris Lattner978fe0c2012-01-30 06:21:21 +00001024 // Check to see if all of the elements are ConstantFP or ConstantInt and if
1025 // the element type is compatible with ConstantDataVector. If so, use it.
Justin Bogner909e1c02015-12-01 20:20:49 +00001026 if (ConstantDataSequential::isElementTypeCompatible(C->getType()))
1027 return getSequenceIfElementsMatch<ConstantDataVector>(C, V);
Galina Kistanovafc259902012-07-13 01:25:27 +00001028
Chris Lattner978fe0c2012-01-30 06:21:21 +00001029 // Otherwise, the element type isn't compatible with ConstantDataVector, or
Craig Topperdf907262017-04-11 06:41:55 +00001030 // the operand list contains a ConstantExpr or something else strange.
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001031 return nullptr;
Owen Anderson4aa32952009-07-28 21:19:26 +00001032}
1033
Chris Lattnere9eed292012-01-25 05:19:54 +00001034Constant *ConstantVector::getSplat(unsigned NumElts, Constant *V) {
Chris Lattner978fe0c2012-01-30 06:21:21 +00001035 // If this splat is compatible with ConstantDataVector, use it instead of
1036 // ConstantVector.
1037 if ((isa<ConstantFP>(V) || isa<ConstantInt>(V)) &&
1038 ConstantDataSequential::isElementTypeCompatible(V->getType()))
1039 return ConstantDataVector::getSplat(NumElts, V);
Galina Kistanovafc259902012-07-13 01:25:27 +00001040
Chris Lattnere9eed292012-01-25 05:19:54 +00001041 SmallVector<Constant*, 32> Elts(NumElts, V);
1042 return get(Elts);
1043}
1044
David Majnemerf0f224d2015-11-11 21:57:16 +00001045ConstantTokenNone *ConstantTokenNone::get(LLVMContext &Context) {
1046 LLVMContextImpl *pImpl = Context.pImpl;
1047 if (!pImpl->TheNoneToken)
David Majnemer2dd41c52015-11-16 20:55:57 +00001048 pImpl->TheNoneToken.reset(new ConstantTokenNone(Context));
1049 return pImpl->TheNoneToken.get();
David Majnemerf0f224d2015-11-11 21:57:16 +00001050}
1051
1052/// Remove the constant from the constant table.
1053void ConstantTokenNone::destroyConstantImpl() {
1054 llvm_unreachable("You can't ConstantTokenNone->destroyConstantImpl()!");
1055}
Chris Lattnere9eed292012-01-25 05:19:54 +00001056
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001057// Utility function for determining if a ConstantExpr is a CastOp or not. This
1058// can't be inline because we don't want to #include Instruction.h into
1059// Constant.h
1060bool ConstantExpr::isCast() const {
1061 return Instruction::isCast(getOpcode());
1062}
1063
Reid Spenceree3c9912006-12-04 05:19:50 +00001064bool ConstantExpr::isCompare() const {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001065 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
Reid Spenceree3c9912006-12-04 05:19:50 +00001066}
1067
Dan Gohman7190d482009-09-10 23:37:55 +00001068bool ConstantExpr::isGEPWithNoNotionalOverIndexing() const {
1069 if (getOpcode() != Instruction::GetElementPtr) return false;
1070
1071 gep_type_iterator GEPI = gep_type_begin(this), E = gep_type_end(this);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001072 User::const_op_iterator OI = std::next(this->op_begin());
Dan Gohman7190d482009-09-10 23:37:55 +00001073
Sanjay Patel81ed3492016-12-11 20:07:02 +00001074 // The remaining indices may be compile-time known integers within the bounds
1075 // of the corresponding notional static array types.
Dan Gohman7190d482009-09-10 23:37:55 +00001076 for (; GEPI != E; ++GEPI, ++OI) {
Sanjay Patel81ed3492016-12-11 20:07:02 +00001077 if (isa<UndefValue>(*OI))
1078 continue;
1079 auto *CI = dyn_cast<ConstantInt>(*OI);
1080 if (!CI || (GEPI.isBoundedSequential() &&
1081 (CI->getValue().getActiveBits() > 64 ||
1082 CI->getZExtValue() >= GEPI.getSequentialNumElements())))
Peter Collingbourneab85225b2016-12-02 02:24:42 +00001083 return false;
Dan Gohman7190d482009-09-10 23:37:55 +00001084 }
1085
1086 // All the indices checked out.
1087 return true;
1088}
1089
Dan Gohman1ecaf452008-05-31 00:58:22 +00001090bool ConstantExpr::hasIndices() const {
1091 return getOpcode() == Instruction::ExtractValue ||
1092 getOpcode() == Instruction::InsertValue;
1093}
1094
Jay Foad0091fe82011-04-13 15:22:40 +00001095ArrayRef<unsigned> ConstantExpr::getIndices() const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001096 if (const ExtractValueConstantExpr *EVCE =
1097 dyn_cast<ExtractValueConstantExpr>(this))
1098 return EVCE->Indices;
Dan Gohmana469bdb2008-06-23 16:39:44 +00001099
1100 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001101}
1102
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001103unsigned ConstantExpr::getPredicate() const {
Craig Toppercc03b492015-12-15 06:11:36 +00001104 return cast<CompareConstantExpr>(this)->predicate;
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001105}
Chris Lattner60e0dd72001-10-03 06:12:09 +00001106
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001107Constant *
1108ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +00001109 assert(Op->getType() == getOperand(OpNo)->getType() &&
1110 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +00001111 if (getOperand(OpNo) == Op)
1112 return const_cast<ConstantExpr*>(this);
Chris Lattner37e38352012-01-26 20:37:11 +00001113
1114 SmallVector<Constant*, 8> NewOps;
1115 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1116 NewOps.push_back(i == OpNo ? Op : getOperand(i));
Galina Kistanovafc259902012-07-13 01:25:27 +00001117
Chris Lattner37e38352012-01-26 20:37:11 +00001118 return getWithOperands(NewOps);
Chris Lattner227816342006-07-14 22:20:01 +00001119}
1120
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001121Constant *ConstantExpr::getWithOperands(ArrayRef<Constant *> Ops, Type *Ty,
David Blaikie88208842015-08-21 20:16:51 +00001122 bool OnlyIfReduced, Type *SrcTy) const {
Jay Foad5c984e562011-04-13 13:46:01 +00001123 assert(Ops.size() == getNumOperands() && "Operand count mismatch!");
Galina Kistanovafc259902012-07-13 01:25:27 +00001124
Benjamin Kramer8008e9f2015-03-02 11:57:04 +00001125 // If no operands changed return self.
1126 if (Ty == getType() && std::equal(Ops.begin(), Ops.end(), op_begin()))
Chris Lattner227816342006-07-14 22:20:01 +00001127 return const_cast<ConstantExpr*>(this);
1128
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001129 Type *OnlyIfReducedTy = OnlyIfReduced ? Ty : nullptr;
Chris Lattner227816342006-07-14 22:20:01 +00001130 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001131 case Instruction::Trunc:
1132 case Instruction::ZExt:
1133 case Instruction::SExt:
1134 case Instruction::FPTrunc:
1135 case Instruction::FPExt:
1136 case Instruction::UIToFP:
1137 case Instruction::SIToFP:
1138 case Instruction::FPToUI:
1139 case Instruction::FPToSI:
1140 case Instruction::PtrToInt:
1141 case Instruction::IntToPtr:
1142 case Instruction::BitCast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001143 case Instruction::AddrSpaceCast:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001144 return ConstantExpr::getCast(getOpcode(), Ops[0], Ty, OnlyIfReduced);
Chris Lattner227816342006-07-14 22:20:01 +00001145 case Instruction::Select:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001146 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2], OnlyIfReducedTy);
Chris Lattner227816342006-07-14 22:20:01 +00001147 case Instruction::InsertElement:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001148 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2],
1149 OnlyIfReducedTy);
Chris Lattner227816342006-07-14 22:20:01 +00001150 case Instruction::ExtractElement:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001151 return ConstantExpr::getExtractElement(Ops[0], Ops[1], OnlyIfReducedTy);
Chris Lattner37e38352012-01-26 20:37:11 +00001152 case Instruction::InsertValue:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001153 return ConstantExpr::getInsertValue(Ops[0], Ops[1], getIndices(),
1154 OnlyIfReducedTy);
Chris Lattner37e38352012-01-26 20:37:11 +00001155 case Instruction::ExtractValue:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001156 return ConstantExpr::getExtractValue(Ops[0], getIndices(), OnlyIfReducedTy);
Chris Lattner227816342006-07-14 22:20:01 +00001157 case Instruction::ShuffleVector:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001158 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2],
1159 OnlyIfReducedTy);
David Blaikie88208842015-08-21 20:16:51 +00001160 case Instruction::GetElementPtr: {
1161 auto *GEPO = cast<GEPOperator>(this);
1162 assert(SrcTy || (Ops[0]->getType() == getOperand(0)->getType()));
1163 return ConstantExpr::getGetElementPtr(
1164 SrcTy ? SrcTy : GEPO->getSourceElementType(), Ops[0], Ops.slice(1),
Peter Collingbourned93620b2016-11-10 22:34:55 +00001165 GEPO->isInBounds(), GEPO->getInRangeIndex(), OnlyIfReducedTy);
David Blaikie88208842015-08-21 20:16:51 +00001166 }
Reid Spencer266e42b2006-12-23 06:05:41 +00001167 case Instruction::ICmp:
1168 case Instruction::FCmp:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001169 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1],
1170 OnlyIfReducedTy);
Chris Lattner227816342006-07-14 22:20:01 +00001171 default:
1172 assert(getNumOperands() == 2 && "Must be binary operator?");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001173 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1], SubclassOptionalData,
1174 OnlyIfReducedTy);
Chris Lattner7c1018a2006-07-14 19:37:40 +00001175 }
1176}
1177
Chris Lattner2f7c9632001-06-06 20:29:01 +00001178
1179//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00001180// isValueValidForType implementations
1181
Chris Lattner229907c2011-07-18 04:54:35 +00001182bool ConstantInt::isValueValidForType(Type *Ty, uint64_t Val) {
Chris Lattner8326bd82012-01-26 00:42:34 +00001183 unsigned NumBits = Ty->getIntegerBitWidth(); // assert okay
1184 if (Ty->isIntegerTy(1))
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001185 return Val == 0 || Val == 1;
Craig Topper50b1e512017-06-07 00:58:05 +00001186 return isUIntN(NumBits, Val);
Reid Spencere7334722006-12-19 01:28:19 +00001187}
1188
Chris Lattner229907c2011-07-18 04:54:35 +00001189bool ConstantInt::isValueValidForType(Type *Ty, int64_t Val) {
Chris Lattner8326bd82012-01-26 00:42:34 +00001190 unsigned NumBits = Ty->getIntegerBitWidth();
1191 if (Ty->isIntegerTy(1))
Reid Spencera94d3942007-01-19 21:13:56 +00001192 return Val == 0 || Val == 1 || Val == -1;
Craig Topper50b1e512017-06-07 00:58:05 +00001193 return isIntN(NumBits, Val);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001194}
1195
Chris Lattner229907c2011-07-18 04:54:35 +00001196bool ConstantFP::isValueValidForType(Type *Ty, const APFloat& Val) {
Dale Johannesend246b2c2007-08-30 00:23:21 +00001197 // convert modifies in place, so make a copy.
1198 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001199 bool losesInfo;
Chris Lattner6b727592004-06-17 18:19:28 +00001200 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001201 default:
1202 return false; // These can't be represented as floating point!
1203
Dale Johannesend246b2c2007-08-30 00:23:21 +00001204 // FIXME rounding mode needs to be more flexible
Dan Gohman518cda42011-12-17 00:04:22 +00001205 case Type::HalfTyID: {
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001206 if (&Val2.getSemantics() == &APFloat::IEEEhalf())
Dan Gohman518cda42011-12-17 00:04:22 +00001207 return true;
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001208 Val2.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &losesInfo);
Dan Gohman518cda42011-12-17 00:04:22 +00001209 return !losesInfo;
1210 }
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001211 case Type::FloatTyID: {
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001212 if (&Val2.getSemantics() == &APFloat::IEEEsingle())
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001213 return true;
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001214 Val2.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &losesInfo);
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001215 return !losesInfo;
1216 }
1217 case Type::DoubleTyID: {
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001218 if (&Val2.getSemantics() == &APFloat::IEEEhalf() ||
1219 &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1220 &Val2.getSemantics() == &APFloat::IEEEdouble())
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001221 return true;
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001222 Val2.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &losesInfo);
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001223 return !losesInfo;
1224 }
Dale Johannesenbdad8092007-08-09 22:51:36 +00001225 case Type::X86_FP80TyID:
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001226 return &Val2.getSemantics() == &APFloat::IEEEhalf() ||
1227 &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1228 &Val2.getSemantics() == &APFloat::IEEEdouble() ||
1229 &Val2.getSemantics() == &APFloat::x87DoubleExtended();
Dale Johannesenbdad8092007-08-09 22:51:36 +00001230 case Type::FP128TyID:
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001231 return &Val2.getSemantics() == &APFloat::IEEEhalf() ||
1232 &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1233 &Val2.getSemantics() == &APFloat::IEEEdouble() ||
1234 &Val2.getSemantics() == &APFloat::IEEEquad();
Dale Johannesen007aa372007-10-11 18:07:22 +00001235 case Type::PPC_FP128TyID:
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001236 return &Val2.getSemantics() == &APFloat::IEEEhalf() ||
1237 &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1238 &Val2.getSemantics() == &APFloat::IEEEdouble() ||
1239 &Val2.getSemantics() == &APFloat::PPCDoubleDouble();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001240 }
Chris Lattneraa2372562006-05-24 17:04:05 +00001241}
Chris Lattner9655e542001-07-20 19:16:02 +00001242
Chris Lattner030af792012-01-24 05:42:11 +00001243
Chris Lattner49d855c2001-09-07 16:46:31 +00001244//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +00001245// Factory Function Implementation
1246
Chris Lattnerc7f9fd42012-01-23 15:20:12 +00001247ConstantAggregateZero *ConstantAggregateZero::get(Type *Ty) {
Chris Lattner13ee7952010-08-28 04:09:24 +00001248 assert((Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy()) &&
Owen Andersonb292b8c2009-07-30 23:03:37 +00001249 "Cannot create an aggregate zero of non-aggregate type!");
David Blaikie899b85a2014-11-25 02:13:54 +00001250
Justin Lebar611c5c22016-10-10 16:26:13 +00001251 std::unique_ptr<ConstantAggregateZero> &Entry =
1252 Ty->getContext().pImpl->CAZConstants[Ty];
1253 if (!Entry)
1254 Entry.reset(new ConstantAggregateZero(Ty));
1255
1256 return Entry.get();
Owen Andersonb292b8c2009-07-30 23:03:37 +00001257}
1258
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001259/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001260void ConstantAggregateZero::destroyConstantImpl() {
Chris Lattnerc7f9fd42012-01-23 15:20:12 +00001261 getContext().pImpl->CAZConstants.erase(getType());
Chris Lattner9fba3da2004-02-15 05:53:04 +00001262}
1263
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001264/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001265void ConstantArray::destroyConstantImpl() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001266 getType()->getContext().pImpl->ArrayConstants.remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001267}
1268
Chris Lattner81fabb02002-08-26 17:53:56 +00001269
Chris Lattner3462ae32001-12-03 22:26:30 +00001270//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001271//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001272
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001273/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001274void ConstantStruct::destroyConstantImpl() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001275 getType()->getContext().pImpl->StructConstants.remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001276}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001277
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001278/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001279void ConstantVector::destroyConstantImpl() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001280 getType()->getContext().pImpl->VectorConstants.remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001281}
1282
Duncan Sandse6beec62012-11-13 12:59:33 +00001283Constant *Constant::getSplatValue() const {
1284 assert(this->getType()->isVectorTy() && "Only valid for vectors!");
1285 if (isa<ConstantAggregateZero>(this))
1286 return getNullValue(this->getType()->getVectorElementType());
1287 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this))
1288 return CV->getSplatValue();
1289 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
1290 return CV->getSplatValue();
Craig Topperc6207612014-04-09 06:08:46 +00001291 return nullptr;
Duncan Sandse6beec62012-11-13 12:59:33 +00001292}
1293
Duncan Sandscf0ff032011-02-01 08:39:12 +00001294Constant *ConstantVector::getSplatValue() const {
Dan Gohman07159202007-10-17 17:51:30 +00001295 // Check out first element.
1296 Constant *Elt = getOperand(0);
1297 // Then make sure all remaining elements point to the same value.
1298 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
Chris Lattnerd04e32d2011-07-17 06:01:30 +00001299 if (getOperand(I) != Elt)
Craig Topperc6207612014-04-09 06:08:46 +00001300 return nullptr;
Dan Gohman07159202007-10-17 17:51:30 +00001301 return Elt;
1302}
1303
Duncan Sandse6beec62012-11-13 12:59:33 +00001304const APInt &Constant::getUniqueInteger() const {
1305 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
1306 return CI->getValue();
1307 assert(this->getSplatValue() && "Doesn't contain a unique integer!");
1308 const Constant *C = this->getAggregateElement(0U);
1309 assert(C && isa<ConstantInt>(C) && "Not a vector of numbers!");
1310 return cast<ConstantInt>(C)->getValue();
1311}
1312
Chris Lattner31b132c2009-10-28 00:01:44 +00001313//---- ConstantPointerNull::get() implementation.
Chris Lattnerd7a73302001-10-13 06:57:33 +00001314//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001315
Chris Lattner229907c2011-07-18 04:54:35 +00001316ConstantPointerNull *ConstantPointerNull::get(PointerType *Ty) {
Justin Lebar611c5c22016-10-10 16:26:13 +00001317 std::unique_ptr<ConstantPointerNull> &Entry =
1318 Ty->getContext().pImpl->CPNConstants[Ty];
Craig Topperc6207612014-04-09 06:08:46 +00001319 if (!Entry)
Justin Lebar611c5c22016-10-10 16:26:13 +00001320 Entry.reset(new ConstantPointerNull(Ty));
Galina Kistanovafc259902012-07-13 01:25:27 +00001321
Justin Lebar611c5c22016-10-10 16:26:13 +00001322 return Entry.get();
Chris Lattner883ad0b2001-10-03 15:39:36 +00001323}
1324
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001325/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001326void ConstantPointerNull::destroyConstantImpl() {
Chris Lattnerc7f9fd42012-01-23 15:20:12 +00001327 getContext().pImpl->CPNConstants.erase(getType());
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001328}
1329
Chris Lattner229907c2011-07-18 04:54:35 +00001330UndefValue *UndefValue::get(Type *Ty) {
Justin Lebar611c5c22016-10-10 16:26:13 +00001331 std::unique_ptr<UndefValue> &Entry = Ty->getContext().pImpl->UVConstants[Ty];
Craig Topperc6207612014-04-09 06:08:46 +00001332 if (!Entry)
Justin Lebar611c5c22016-10-10 16:26:13 +00001333 Entry.reset(new UndefValue(Ty));
Galina Kistanovafc259902012-07-13 01:25:27 +00001334
Justin Lebar611c5c22016-10-10 16:26:13 +00001335 return Entry.get();
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001336}
1337
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001338/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001339void UndefValue::destroyConstantImpl() {
Chris Lattnerc7f9fd42012-01-23 15:20:12 +00001340 // Free the constant and any dangling references to it.
1341 getContext().pImpl->UVConstants.erase(getType());
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001342}
1343
Chris Lattner31b132c2009-10-28 00:01:44 +00001344BlockAddress *BlockAddress::get(BasicBlock *BB) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001345 assert(BB->getParent() && "Block must have a parent");
Chris Lattner31b132c2009-10-28 00:01:44 +00001346 return get(BB->getParent(), BB);
1347}
1348
1349BlockAddress *BlockAddress::get(Function *F, BasicBlock *BB) {
1350 BlockAddress *&BA =
1351 F->getContext().pImpl->BlockAddresses[std::make_pair(F, BB)];
Craig Topperc6207612014-04-09 06:08:46 +00001352 if (!BA)
Chris Lattner31b132c2009-10-28 00:01:44 +00001353 BA = new BlockAddress(F, BB);
Galina Kistanovafc259902012-07-13 01:25:27 +00001354
Chris Lattner31b132c2009-10-28 00:01:44 +00001355 assert(BA->getFunction() == F && "Basic block moved between functions");
1356 return BA;
1357}
1358
1359BlockAddress::BlockAddress(Function *F, BasicBlock *BB)
1360: Constant(Type::getInt8PtrTy(F->getContext()), Value::BlockAddressVal,
1361 &Op<0>(), 2) {
Chris Lattnerc559a9f2009-11-01 03:03:03 +00001362 setOperand(0, F);
1363 setOperand(1, BB);
Chris Lattneraa99c942009-11-01 01:27:45 +00001364 BB->AdjustBlockAddressRefCount(1);
Chris Lattner31b132c2009-10-28 00:01:44 +00001365}
1366
Chandler Carruth6a936922014-01-19 02:13:50 +00001367BlockAddress *BlockAddress::lookup(const BasicBlock *BB) {
1368 if (!BB->hasAddressTaken())
Craig Topperc6207612014-04-09 06:08:46 +00001369 return nullptr;
Chandler Carruth6a936922014-01-19 02:13:50 +00001370
1371 const Function *F = BB->getParent();
Craig Topper2617dcc2014-04-15 06:32:26 +00001372 assert(F && "Block must have a parent");
Chandler Carruth6a936922014-01-19 02:13:50 +00001373 BlockAddress *BA =
1374 F->getContext().pImpl->BlockAddresses.lookup(std::make_pair(F, BB));
1375 assert(BA && "Refcount and block address map disagree!");
1376 return BA;
1377}
Chris Lattner31b132c2009-10-28 00:01:44 +00001378
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001379/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001380void BlockAddress::destroyConstantImpl() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001381 getFunction()->getType()->getContext().pImpl
Chris Lattner31b132c2009-10-28 00:01:44 +00001382 ->BlockAddresses.erase(std::make_pair(getFunction(), getBasicBlock()));
Chris Lattneraa99c942009-11-01 01:27:45 +00001383 getBasicBlock()->AdjustBlockAddressRefCount(-1);
Chris Lattner31b132c2009-10-28 00:01:44 +00001384}
1385
Mehdi Amini8914d292016-02-10 22:47:15 +00001386Value *BlockAddress::handleOperandChangeImpl(Value *From, Value *To) {
Chris Lattner31b132c2009-10-28 00:01:44 +00001387 // This could be replacing either the Basic Block or the Function. In either
1388 // case, we have to remove the map entry.
1389 Function *NewF = getFunction();
1390 BasicBlock *NewBB = getBasicBlock();
Galina Kistanovafc259902012-07-13 01:25:27 +00001391
Mehdi Amini8914d292016-02-10 22:47:15 +00001392 if (From == NewF)
Derek Schuffec9dc012013-06-13 19:51:17 +00001393 NewF = cast<Function>(To->stripPointerCasts());
Mehdi Amini8914d292016-02-10 22:47:15 +00001394 else {
1395 assert(From == NewBB && "From does not match any operand");
Chris Lattner31b132c2009-10-28 00:01:44 +00001396 NewBB = cast<BasicBlock>(To);
Mehdi Amini8914d292016-02-10 22:47:15 +00001397 }
Galina Kistanovafc259902012-07-13 01:25:27 +00001398
Chris Lattner31b132c2009-10-28 00:01:44 +00001399 // See if the 'new' entry already exists, if not, just update this in place
1400 // and return early.
1401 BlockAddress *&NewBA =
1402 getContext().pImpl->BlockAddresses[std::make_pair(NewF, NewBB)];
Pete Cooper5815b1f2015-06-24 18:55:24 +00001403 if (NewBA)
1404 return NewBA;
Chris Lattner31b132c2009-10-28 00:01:44 +00001405
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001406 getBasicBlock()->AdjustBlockAddressRefCount(-1);
Galina Kistanovafc259902012-07-13 01:25:27 +00001407
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001408 // Remove the old entry, this can't cause the map to rehash (just a
1409 // tombstone will get added).
1410 getContext().pImpl->BlockAddresses.erase(std::make_pair(getFunction(),
1411 getBasicBlock()));
1412 NewBA = this;
1413 setOperand(0, NewF);
1414 setOperand(1, NewBB);
1415 getBasicBlock()->AdjustBlockAddressRefCount(1);
Pete Cooper5815b1f2015-06-24 18:55:24 +00001416
1417 // If we just want to keep the existing value, then return null.
1418 // Callers know that this means we shouldn't delete this value.
1419 return nullptr;
Chris Lattner31b132c2009-10-28 00:01:44 +00001420}
1421
1422//---- ConstantExpr::get() implementations.
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001423//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001424
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001425/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands7d6c8ae2008-03-30 19:38:55 +00001426/// cast in the ExprConstants map. It is used by the various get* methods below.
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001427static Constant *getFoldedCast(Instruction::CastOps opc, Constant *C, Type *Ty,
1428 bool OnlyIfReduced = false) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001429 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001430 // Fold a few common cases
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001431 if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001432 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001433
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001434 if (OnlyIfReduced)
1435 return nullptr;
1436
Owen Anderson1584a292009-08-04 20:25:11 +00001437 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
1438
Nadav Rotem88330432013-03-07 01:30:40 +00001439 // Look up the constant in the table first to ensure uniqueness.
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001440 ConstantExprKeyType Key(opc, C);
Galina Kistanovafc259902012-07-13 01:25:27 +00001441
Owen Anderson1584a292009-08-04 20:25:11 +00001442 return pImpl->ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001443}
Galina Kistanovafc259902012-07-13 01:25:27 +00001444
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001445Constant *ConstantExpr::getCast(unsigned oc, Constant *C, Type *Ty,
1446 bool OnlyIfReduced) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001447 Instruction::CastOps opc = Instruction::CastOps(oc);
1448 assert(Instruction::isCast(opc) && "opcode out of range");
1449 assert(C && Ty && "Null arguments to getCast");
Chris Lattner37bc78a2010-01-26 21:51:43 +00001450 assert(CastInst::castIsValid(opc, C, Ty) && "Invalid constantexpr cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001451
1452 switch (opc) {
Chris Lattner37bc78a2010-01-26 21:51:43 +00001453 default:
1454 llvm_unreachable("Invalid cast opcode");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001455 case Instruction::Trunc:
1456 return getTrunc(C, Ty, OnlyIfReduced);
1457 case Instruction::ZExt:
1458 return getZExt(C, Ty, OnlyIfReduced);
1459 case Instruction::SExt:
1460 return getSExt(C, Ty, OnlyIfReduced);
1461 case Instruction::FPTrunc:
1462 return getFPTrunc(C, Ty, OnlyIfReduced);
1463 case Instruction::FPExt:
1464 return getFPExtend(C, Ty, OnlyIfReduced);
1465 case Instruction::UIToFP:
1466 return getUIToFP(C, Ty, OnlyIfReduced);
1467 case Instruction::SIToFP:
1468 return getSIToFP(C, Ty, OnlyIfReduced);
1469 case Instruction::FPToUI:
1470 return getFPToUI(C, Ty, OnlyIfReduced);
1471 case Instruction::FPToSI:
1472 return getFPToSI(C, Ty, OnlyIfReduced);
1473 case Instruction::PtrToInt:
1474 return getPtrToInt(C, Ty, OnlyIfReduced);
1475 case Instruction::IntToPtr:
1476 return getIntToPtr(C, Ty, OnlyIfReduced);
1477 case Instruction::BitCast:
1478 return getBitCast(C, Ty, OnlyIfReduced);
1479 case Instruction::AddrSpaceCast:
1480 return getAddrSpaceCast(C, Ty, OnlyIfReduced);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001481 }
Galina Kistanovafc259902012-07-13 01:25:27 +00001482}
Reid Spencerf37dc652006-12-05 19:14:13 +00001483
Chris Lattner229907c2011-07-18 04:54:35 +00001484Constant *ConstantExpr::getZExtOrBitCast(Constant *C, Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001485 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001486 return getBitCast(C, Ty);
1487 return getZExt(C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001488}
1489
Chris Lattner229907c2011-07-18 04:54:35 +00001490Constant *ConstantExpr::getSExtOrBitCast(Constant *C, Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001491 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001492 return getBitCast(C, Ty);
1493 return getSExt(C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001494}
1495
Chris Lattner229907c2011-07-18 04:54:35 +00001496Constant *ConstantExpr::getTruncOrBitCast(Constant *C, Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001497 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001498 return getBitCast(C, Ty);
1499 return getTrunc(C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001500}
1501
Chris Lattner229907c2011-07-18 04:54:35 +00001502Constant *ConstantExpr::getPointerCast(Constant *S, Type *Ty) {
Evgeniy Stepanov23382642013-01-16 14:41:46 +00001503 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
1504 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
1505 "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001506
Evgeniy Stepanov23382642013-01-16 14:41:46 +00001507 if (Ty->isIntOrIntVectorTy())
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001508 return getPtrToInt(S, Ty);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001509
1510 unsigned SrcAS = S->getType()->getPointerAddressSpace();
1511 if (Ty->isPtrOrPtrVectorTy() && SrcAS != Ty->getPointerAddressSpace())
1512 return getAddrSpaceCast(S, Ty);
1513
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001514 return getBitCast(S, Ty);
Reid Spencerbc245a02006-12-05 03:25:26 +00001515}
1516
Matt Arsenault21f38f42013-12-07 02:58:41 +00001517Constant *ConstantExpr::getPointerBitCastOrAddrSpaceCast(Constant *S,
1518 Type *Ty) {
1519 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
1520 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
1521
1522 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
1523 return getAddrSpaceCast(S, Ty);
1524
1525 return getBitCast(S, Ty);
1526}
1527
Sanjay Patelf3587ec2016-05-18 22:05:28 +00001528Constant *ConstantExpr::getIntegerCast(Constant *C, Type *Ty, bool isSigned) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001529 assert(C->getType()->isIntOrIntVectorTy() &&
1530 Ty->isIntOrIntVectorTy() && "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001531 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1532 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer56521c42006-12-12 00:51:07 +00001533 Instruction::CastOps opcode =
1534 (SrcBits == DstBits ? Instruction::BitCast :
1535 (SrcBits > DstBits ? Instruction::Trunc :
1536 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1537 return getCast(opcode, C, Ty);
1538}
1539
Chris Lattner229907c2011-07-18 04:54:35 +00001540Constant *ConstantExpr::getFPCast(Constant *C, Type *Ty) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001541 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer56521c42006-12-12 00:51:07 +00001542 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001543 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1544 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001545 if (SrcBits == DstBits)
1546 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001547 Instruction::CastOps opcode =
Jay Foad9f32cfd2011-01-27 14:44:55 +00001548 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001549 return getCast(opcode, C, Ty);
1550}
1551
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001552Constant *ConstantExpr::getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001553#ifndef NDEBUG
1554 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1555 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1556#endif
1557 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001558 assert(C->getType()->isIntOrIntVectorTy() && "Trunc operand must be integer");
1559 assert(Ty->isIntOrIntVectorTy() && "Trunc produces only integral");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001560 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001561 "SrcTy must be larger than DestTy for Trunc!");
1562
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001563 return getFoldedCast(Instruction::Trunc, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001564}
1565
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001566Constant *ConstantExpr::getSExt(Constant *C, Type *Ty, bool OnlyIfReduced) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001567#ifndef NDEBUG
1568 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1569 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1570#endif
1571 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001572 assert(C->getType()->isIntOrIntVectorTy() && "SExt operand must be integral");
1573 assert(Ty->isIntOrIntVectorTy() && "SExt produces only integer");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001574 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001575 "SrcTy must be smaller than DestTy for SExt!");
1576
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001577 return getFoldedCast(Instruction::SExt, C, Ty, OnlyIfReduced);
Chris Lattnerdd284742004-04-04 23:20:30 +00001578}
1579
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001580Constant *ConstantExpr::getZExt(Constant *C, Type *Ty, bool OnlyIfReduced) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001581#ifndef NDEBUG
1582 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1583 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1584#endif
1585 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001586 assert(C->getType()->isIntOrIntVectorTy() && "ZEXt operand must be integral");
1587 assert(Ty->isIntOrIntVectorTy() && "ZExt produces only integer");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001588 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001589 "SrcTy must be smaller than DestTy for ZExt!");
1590
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001591 return getFoldedCast(Instruction::ZExt, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001592}
1593
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001594Constant *ConstantExpr::getFPTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001595#ifndef NDEBUG
1596 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1597 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1598#endif
1599 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001600 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001601 C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001602 "This is an illegal floating point truncation!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001603 return getFoldedCast(Instruction::FPTrunc, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001604}
1605
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001606Constant *ConstantExpr::getFPExtend(Constant *C, Type *Ty, bool OnlyIfReduced) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001607#ifndef NDEBUG
1608 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1609 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1610#endif
1611 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001612 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001613 C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001614 "This is an illegal floating point extension!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001615 return getFoldedCast(Instruction::FPExt, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001616}
1617
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001618Constant *ConstantExpr::getUIToFP(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() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00001625 "This is an illegal uint to floating point cast!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001626 return getFoldedCast(Instruction::UIToFP, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001627}
1628
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001629Constant *ConstantExpr::getSIToFP(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()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001636 "This is an illegal sint to floating point cast!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001637 return getFoldedCast(Instruction::SIToFP, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001638}
1639
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001640Constant *ConstantExpr::getFPToUI(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 uint cast!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001648 return getFoldedCast(Instruction::FPToUI, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001649}
1650
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001651Constant *ConstantExpr::getFPToSI(Constant *C, Type *Ty, bool OnlyIfReduced) {
Devang Pateld26344d2008-11-03 23:20:04 +00001652#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001653 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1654 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001655#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001656 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001657 assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00001658 "This is an illegal floating point to sint cast!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001659 return getFoldedCast(Instruction::FPToSI, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001660}
1661
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001662Constant *ConstantExpr::getPtrToInt(Constant *C, Type *DstTy,
1663 bool OnlyIfReduced) {
Craig Topper95d23472017-07-09 07:04:00 +00001664 assert(C->getType()->isPtrOrPtrVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00001665 "PtrToInt source must be pointer or pointer vector");
Craig Topper95d23472017-07-09 07:04:00 +00001666 assert(DstTy->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00001667 "PtrToInt destination must be integer or integer vector");
Chris Lattner8a3df542012-01-25 01:32:59 +00001668 assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy));
Nick Lewyckyff509622012-01-25 03:20:12 +00001669 if (isa<VectorType>(C->getType()))
Chris Lattner8326bd82012-01-26 00:42:34 +00001670 assert(C->getType()->getVectorNumElements()==DstTy->getVectorNumElements()&&
Chris Lattner8a3df542012-01-25 01:32:59 +00001671 "Invalid cast between a different number of vector elements");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001672 return getFoldedCast(Instruction::PtrToInt, C, DstTy, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001673}
1674
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001675Constant *ConstantExpr::getIntToPtr(Constant *C, Type *DstTy,
1676 bool OnlyIfReduced) {
Craig Topper95d23472017-07-09 07:04:00 +00001677 assert(C->getType()->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00001678 "IntToPtr source must be integer or integer vector");
Craig Topper95d23472017-07-09 07:04:00 +00001679 assert(DstTy->isPtrOrPtrVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00001680 "IntToPtr destination must be a pointer or pointer vector");
Chris Lattner8a3df542012-01-25 01:32:59 +00001681 assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy));
Nick Lewyckyff509622012-01-25 03:20:12 +00001682 if (isa<VectorType>(C->getType()))
Chris Lattner8326bd82012-01-26 00:42:34 +00001683 assert(C->getType()->getVectorNumElements()==DstTy->getVectorNumElements()&&
Chris Lattner8a3df542012-01-25 01:32:59 +00001684 "Invalid cast between a different number of vector elements");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001685 return getFoldedCast(Instruction::IntToPtr, C, DstTy, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001686}
1687
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001688Constant *ConstantExpr::getBitCast(Constant *C, Type *DstTy,
1689 bool OnlyIfReduced) {
Chris Lattner37bc78a2010-01-26 21:51:43 +00001690 assert(CastInst::castIsValid(Instruction::BitCast, C, DstTy) &&
1691 "Invalid constantexpr bitcast!");
Galina Kistanovafc259902012-07-13 01:25:27 +00001692
Chris Lattnercbeda872009-03-21 06:55:54 +00001693 // It is common to ask for a bitcast of a value to its own type, handle this
1694 // speedily.
1695 if (C->getType() == DstTy) return C;
Galina Kistanovafc259902012-07-13 01:25:27 +00001696
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001697 return getFoldedCast(Instruction::BitCast, C, DstTy, OnlyIfReduced);
Chris Lattnerdd284742004-04-04 23:20:30 +00001698}
1699
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001700Constant *ConstantExpr::getAddrSpaceCast(Constant *C, Type *DstTy,
1701 bool OnlyIfReduced) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001702 assert(CastInst::castIsValid(Instruction::AddrSpaceCast, C, DstTy) &&
1703 "Invalid constantexpr addrspacecast!");
1704
Jingyue Wubaabe502014-06-15 21:40:57 +00001705 // Canonicalize addrspacecasts between different pointer types by first
1706 // bitcasting the pointer type and then converting the address space.
1707 PointerType *SrcScalarTy = cast<PointerType>(C->getType()->getScalarType());
1708 PointerType *DstScalarTy = cast<PointerType>(DstTy->getScalarType());
1709 Type *DstElemTy = DstScalarTy->getElementType();
1710 if (SrcScalarTy->getElementType() != DstElemTy) {
1711 Type *MidTy = PointerType::get(DstElemTy, SrcScalarTy->getAddressSpace());
1712 if (VectorType *VT = dyn_cast<VectorType>(DstTy)) {
1713 // Handle vectors of pointers.
1714 MidTy = VectorType::get(MidTy, VT->getNumElements());
1715 }
1716 C = getBitCast(C, MidTy);
1717 }
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001718 return getFoldedCast(Instruction::AddrSpaceCast, C, DstTy, OnlyIfReduced);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001719}
1720
Chris Lattner887ecac2011-07-09 18:23:52 +00001721Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2,
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001722 unsigned Flags, Type *OnlyIfReducedTy) {
Chris Lattner887ecac2011-07-09 18:23:52 +00001723 // Check the operands for consistency first.
Reid Spencer7eb55b32006-11-02 01:53:59 +00001724 assert(Opcode >= Instruction::BinaryOpsBegin &&
1725 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001726 "Invalid opcode in binary constant expression");
1727 assert(C1->getType() == C2->getType() &&
1728 "Operand types in binary constant expression should match");
Galina Kistanovafc259902012-07-13 01:25:27 +00001729
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001730#ifndef NDEBUG
1731 switch (Opcode) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001732 case Instruction::Add:
Reid Spencer7eb55b32006-11-02 01:53:59 +00001733 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001734 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001735 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001736 assert(C1->getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001737 "Tried to create an integer operation on a non-integer type!");
1738 break;
1739 case Instruction::FAdd:
1740 case Instruction::FSub:
1741 case Instruction::FMul:
1742 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001743 assert(C1->getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001744 "Tried to create a floating-point operation on a "
1745 "non-floating-point type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001746 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001747 case Instruction::UDiv:
1748 case Instruction::SDiv:
1749 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001750 assert(C1->getType()->isIntOrIntVectorTy() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001751 "Tried to create an arithmetic operation on a non-arithmetic type!");
1752 break;
1753 case Instruction::FDiv:
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 Spencer7e80b0b2006-10-26 06:15:43 +00001757 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001758 case Instruction::URem:
1759 case Instruction::SRem:
1760 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001761 assert(C1->getType()->isIntOrIntVectorTy() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001762 "Tried to create an arithmetic operation on a non-arithmetic type!");
1763 break;
1764 case Instruction::FRem:
1765 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001766 assert(C1->getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001767 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001768 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001769 case Instruction::And:
1770 case Instruction::Or:
1771 case Instruction::Xor:
1772 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001773 assert(C1->getType()->isIntOrIntVectorTy() &&
Misha Brukman3852f652005-01-27 06:46:38 +00001774 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001775 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001776 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001777 case Instruction::LShr:
1778 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00001779 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001780 assert(C1->getType()->isIntOrIntVectorTy() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001781 "Tried to create a shift operation on a non-integer type!");
1782 break;
1783 default:
1784 break;
1785 }
1786#endif
1787
Chris Lattner887ecac2011-07-09 18:23:52 +00001788 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1789 return FC; // Fold a few common cases.
Galina Kistanovafc259902012-07-13 01:25:27 +00001790
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001791 if (OnlyIfReducedTy == C1->getType())
1792 return nullptr;
1793
Benjamin Kramer324322b2013-03-07 20:53:34 +00001794 Constant *ArgVec[] = { C1, C2 };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001795 ConstantExprKeyType Key(Opcode, ArgVec, 0, Flags);
Galina Kistanovafc259902012-07-13 01:25:27 +00001796
Chris Lattner887ecac2011-07-09 18:23:52 +00001797 LLVMContextImpl *pImpl = C1->getContext().pImpl;
1798 return pImpl->ExprConstants.getOrCreate(C1->getType(), Key);
Reid Spencera009d0d2006-12-04 21:35:24 +00001799}
1800
Chris Lattner229907c2011-07-18 04:54:35 +00001801Constant *ConstantExpr::getSizeOf(Type* Ty) {
Owen Anderson487375e2009-07-29 18:55:55 +00001802 // sizeof is implemented as: (i64) gep (Ty*)null, 1
1803 // Note that a non-inbounds gep is used, as null isn't within any object.
Owen Anderson55f1c092009-08-13 21:58:54 +00001804 Constant *GEPIdx = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
Owen Anderson487375e2009-07-29 18:55:55 +00001805 Constant *GEP = getGetElementPtr(
David Blaikie4a2e73b2015-04-02 18:55:32 +00001806 Ty, Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx);
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001807 return getPtrToInt(GEP,
1808 Type::getInt64Ty(Ty->getContext()));
Owen Anderson487375e2009-07-29 18:55:55 +00001809}
1810
Chris Lattner229907c2011-07-18 04:54:35 +00001811Constant *ConstantExpr::getAlignOf(Type* Ty) {
Dan Gohmancf913832010-01-28 02:15:55 +00001812 // alignof is implemented as: (i64) gep ({i1,Ty}*)null, 0, 1
Dan Gohman50c09d02009-08-11 17:57:01 +00001813 // Note that a non-inbounds gep is used, as null isn't within any object.
Serge Gueltone38003f2017-05-09 19:31:13 +00001814 Type *AligningTy = StructType::get(Type::getInt1Ty(Ty->getContext()), Ty);
Matt Arsenaultbe558882014-04-23 20:58:57 +00001815 Constant *NullPtr = Constant::getNullValue(AligningTy->getPointerTo(0));
Dan Gohmana9be7392010-01-28 02:43:22 +00001816 Constant *Zero = ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0);
Owen Anderson55f1c092009-08-13 21:58:54 +00001817 Constant *One = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
Owen Anderson487375e2009-07-29 18:55:55 +00001818 Constant *Indices[2] = { Zero, One };
David Blaikie4a2e73b2015-04-02 18:55:32 +00001819 Constant *GEP = getGetElementPtr(AligningTy, NullPtr, Indices);
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001820 return getPtrToInt(GEP,
1821 Type::getInt64Ty(Ty->getContext()));
Owen Anderson487375e2009-07-29 18:55:55 +00001822}
1823
Chris Lattner229907c2011-07-18 04:54:35 +00001824Constant *ConstantExpr::getOffsetOf(StructType* STy, unsigned FieldNo) {
Dan Gohmanede94e62010-02-01 16:37:38 +00001825 return getOffsetOf(STy, ConstantInt::get(Type::getInt32Ty(STy->getContext()),
1826 FieldNo));
1827}
1828
Chris Lattner229907c2011-07-18 04:54:35 +00001829Constant *ConstantExpr::getOffsetOf(Type* Ty, Constant *FieldNo) {
Dan Gohmanff3af7252009-08-16 21:26:11 +00001830 // offsetof is implemented as: (i64) gep (Ty*)null, 0, FieldNo
1831 // Note that a non-inbounds gep is used, as null isn't within any object.
1832 Constant *GEPIdx[] = {
Dan Gohmanede94e62010-02-01 16:37:38 +00001833 ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0),
1834 FieldNo
Dan Gohmanff3af7252009-08-16 21:26:11 +00001835 };
1836 Constant *GEP = getGetElementPtr(
David Blaikie4a2e73b2015-04-02 18:55:32 +00001837 Ty, Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx);
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001838 return getPtrToInt(GEP,
1839 Type::getInt64Ty(Ty->getContext()));
Dan Gohmanff3af7252009-08-16 21:26:11 +00001840}
Owen Anderson487375e2009-07-29 18:55:55 +00001841
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001842Constant *ConstantExpr::getCompare(unsigned short Predicate, Constant *C1,
1843 Constant *C2, bool OnlyIfReduced) {
Reid Spencera009d0d2006-12-04 21:35:24 +00001844 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Galina Kistanovafc259902012-07-13 01:25:27 +00001845
Chris Lattner887ecac2011-07-09 18:23:52 +00001846 switch (Predicate) {
1847 default: llvm_unreachable("Invalid CmpInst predicate");
1848 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
1849 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
1850 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
1851 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
1852 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
1853 case CmpInst::FCMP_TRUE:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001854 return getFCmp(Predicate, C1, C2, OnlyIfReduced);
Galina Kistanovafc259902012-07-13 01:25:27 +00001855
Chris Lattner887ecac2011-07-09 18:23:52 +00001856 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
1857 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
1858 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
1859 case CmpInst::ICMP_SLE:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001860 return getICmp(Predicate, C1, C2, OnlyIfReduced);
Chris Lattner887ecac2011-07-09 18:23:52 +00001861 }
Chris Lattner29ca2c62004-08-04 18:50:09 +00001862}
1863
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001864Constant *ConstantExpr::getSelect(Constant *C, Constant *V1, Constant *V2,
1865 Type *OnlyIfReducedTy) {
Chris Lattner41632132008-12-29 00:16:12 +00001866 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner6e415c02004-03-12 05:54:04 +00001867
Chris Lattner887ecac2011-07-09 18:23:52 +00001868 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1869 return SC; // Fold common cases
Chris Lattner6e415c02004-03-12 05:54:04 +00001870
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001871 if (OnlyIfReducedTy == V1->getType())
1872 return nullptr;
1873
Benjamin Kramer324322b2013-03-07 20:53:34 +00001874 Constant *ArgVec[] = { C, V1, V2 };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001875 ConstantExprKeyType Key(Instruction::Select, ArgVec);
Galina Kistanovafc259902012-07-13 01:25:27 +00001876
Chris Lattner887ecac2011-07-09 18:23:52 +00001877 LLVMContextImpl *pImpl = C->getContext().pImpl;
1878 return pImpl->ExprConstants.getOrCreate(V1->getType(), Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00001879}
1880
David Blaikie4a2e73b2015-04-02 18:55:32 +00001881Constant *ConstantExpr::getGetElementPtr(Type *Ty, Constant *C,
1882 ArrayRef<Value *> Idxs, bool InBounds,
Peter Collingbourned93620b2016-11-10 22:34:55 +00001883 Optional<unsigned> InRangeIndex,
David Blaikie4a2e73b2015-04-02 18:55:32 +00001884 Type *OnlyIfReducedTy) {
David Blaikie4a2e73b2015-04-02 18:55:32 +00001885 if (!Ty)
1886 Ty = cast<PointerType>(C->getType()->getScalarType())->getElementType();
1887 else
David Blaikied9d900c2015-05-07 17:28:58 +00001888 assert(
1889 Ty ==
1890 cast<PointerType>(C->getType()->getScalarType())->getContainedType(0u));
1891
Peter Collingbourned93620b2016-11-10 22:34:55 +00001892 if (Constant *FC =
1893 ConstantFoldGetElementPtr(Ty, C, InBounds, InRangeIndex, Idxs))
David Blaikied9d900c2015-05-07 17:28:58 +00001894 return FC; // Fold a few common cases.
1895
Chris Lattner887ecac2011-07-09 18:23:52 +00001896 // Get the result type of the getelementptr!
David Blaikie4a2e73b2015-04-02 18:55:32 +00001897 Type *DestTy = GetElementPtrInst::getIndexedType(Ty, Idxs);
1898 assert(DestTy && "GEP indices invalid!");
Chris Lattner8326bd82012-01-26 00:42:34 +00001899 unsigned AS = C->getType()->getPointerAddressSpace();
David Blaikie4a2e73b2015-04-02 18:55:32 +00001900 Type *ReqTy = DestTy->getPointerTo(AS);
Elena Demikhovskyee004bc2016-05-15 12:30:25 +00001901
1902 unsigned NumVecElts = 0;
1903 if (C->getType()->isVectorTy())
1904 NumVecElts = C->getType()->getVectorNumElements();
1905 else for (auto Idx : Idxs)
1906 if (Idx->getType()->isVectorTy())
1907 NumVecElts = Idx->getType()->getVectorNumElements();
1908
1909 if (NumVecElts)
1910 ReqTy = VectorType::get(ReqTy, NumVecElts);
Galina Kistanovafc259902012-07-13 01:25:27 +00001911
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001912 if (OnlyIfReducedTy == ReqTy)
1913 return nullptr;
1914
Dan Gohman1b849082009-09-07 23:54:19 +00001915 // Look up the constant in the table first to ensure uniqueness
1916 std::vector<Constant*> ArgVec;
Jay Foaded8db7d2011-07-21 14:31:17 +00001917 ArgVec.reserve(1 + Idxs.size());
Dan Gohman1b849082009-09-07 23:54:19 +00001918 ArgVec.push_back(C);
Duncan Sandse6beec62012-11-13 12:59:33 +00001919 for (unsigned i = 0, e = Idxs.size(); i != e; ++i) {
Duncan Sandse6beec62012-11-13 12:59:33 +00001920 assert((!Idxs[i]->getType()->isVectorTy() ||
Elena Demikhovskyee004bc2016-05-15 12:30:25 +00001921 Idxs[i]->getType()->getVectorNumElements() == NumVecElts) &&
Duncan Sandse6beec62012-11-13 12:59:33 +00001922 "getelementptr index type missmatch");
Elena Demikhovskyee004bc2016-05-15 12:30:25 +00001923
1924 Constant *Idx = cast<Constant>(Idxs[i]);
1925 if (NumVecElts && !Idxs[i]->getType()->isVectorTy())
1926 Idx = ConstantVector::getSplat(NumVecElts, Idx);
1927 ArgVec.push_back(Idx);
Duncan Sandse6beec62012-11-13 12:59:33 +00001928 }
Peter Collingbourned93620b2016-11-10 22:34:55 +00001929
1930 unsigned SubClassOptionalData = InBounds ? GEPOperator::IsInBounds : 0;
1931 if (InRangeIndex && *InRangeIndex < 63)
1932 SubClassOptionalData |= (*InRangeIndex + 1) << 1;
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001933 const ConstantExprKeyType Key(Instruction::GetElementPtr, ArgVec, 0,
Peter Collingbourned93620b2016-11-10 22:34:55 +00001934 SubClassOptionalData, None, Ty);
Galina Kistanovafc259902012-07-13 01:25:27 +00001935
Chris Lattner887ecac2011-07-09 18:23:52 +00001936 LLVMContextImpl *pImpl = C->getContext().pImpl;
Dan Gohman1b849082009-09-07 23:54:19 +00001937 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
1938}
1939
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001940Constant *ConstantExpr::getICmp(unsigned short pred, Constant *LHS,
1941 Constant *RHS, bool OnlyIfReduced) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001942 assert(LHS->getType() == RHS->getType());
Craig Topper45844762017-07-05 23:35:46 +00001943 assert(CmpInst::isIntPredicate((CmpInst::Predicate)pred) &&
1944 "Invalid ICmp Predicate");
Reid Spenceree3c9912006-12-04 05:19:50 +00001945
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001946 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001947 return FC; // Fold a few common cases...
1948
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001949 if (OnlyIfReduced)
1950 return nullptr;
1951
Reid Spenceree3c9912006-12-04 05:19:50 +00001952 // Look up the constant in the table first to ensure uniqueness
Benjamin Kramer324322b2013-03-07 20:53:34 +00001953 Constant *ArgVec[] = { LHS, RHS };
Reid Spencerb1537492006-12-24 18:42:29 +00001954 // Get the key type with both the opcode and predicate
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001955 const ConstantExprKeyType Key(Instruction::ICmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00001956
Chris Lattner229907c2011-07-18 04:54:35 +00001957 Type *ResultTy = Type::getInt1Ty(LHS->getContext());
1958 if (VectorType *VT = dyn_cast<VectorType>(LHS->getType()))
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00001959 ResultTy = VectorType::get(ResultTy, VT->getNumElements());
1960
Owen Anderson1584a292009-08-04 20:25:11 +00001961 LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl;
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00001962 return pImpl->ExprConstants.getOrCreate(ResultTy, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001963}
1964
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001965Constant *ConstantExpr::getFCmp(unsigned short pred, Constant *LHS,
1966 Constant *RHS, bool OnlyIfReduced) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001967 assert(LHS->getType() == RHS->getType());
Craig Topper45844762017-07-05 23:35:46 +00001968 assert(CmpInst::isFPPredicate((CmpInst::Predicate)pred) &&
1969 "Invalid FCmp Predicate");
Reid Spenceree3c9912006-12-04 05:19:50 +00001970
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001971 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001972 return FC; // Fold a few common cases...
1973
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001974 if (OnlyIfReduced)
1975 return nullptr;
1976
Reid Spenceree3c9912006-12-04 05:19:50 +00001977 // Look up the constant in the table first to ensure uniqueness
Benjamin Kramer324322b2013-03-07 20:53:34 +00001978 Constant *ArgVec[] = { LHS, RHS };
Reid Spencerb1537492006-12-24 18:42:29 +00001979 // Get the key type with both the opcode and predicate
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001980 const ConstantExprKeyType Key(Instruction::FCmp, ArgVec, pred);
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00001981
Chris Lattner229907c2011-07-18 04:54:35 +00001982 Type *ResultTy = Type::getInt1Ty(LHS->getContext());
1983 if (VectorType *VT = dyn_cast<VectorType>(LHS->getType()))
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00001984 ResultTy = VectorType::get(ResultTy, VT->getNumElements());
1985
Owen Anderson1584a292009-08-04 20:25:11 +00001986 LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl;
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00001987 return pImpl->ExprConstants.getOrCreate(ResultTy, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001988}
1989
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001990Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx,
1991 Type *OnlyIfReducedTy) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001992 assert(Val->getType()->isVectorTy() &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001993 "Tried to create extractelement operation on non-vector type!");
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001994 assert(Idx->getType()->isIntegerTy() &&
1995 "Extractelement index must be an integer type!");
Galina Kistanovafc259902012-07-13 01:25:27 +00001996
Chris Lattner887ecac2011-07-09 18:23:52 +00001997 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
Chris Lattner09660c92009-12-30 20:25:09 +00001998 return FC; // Fold a few common cases.
Galina Kistanovafc259902012-07-13 01:25:27 +00001999
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002000 Type *ReqTy = Val->getType()->getVectorElementType();
2001 if (OnlyIfReducedTy == ReqTy)
2002 return nullptr;
2003
Robert Bocchinoca27f032006-01-17 20:07:22 +00002004 // Look up the constant in the table first to ensure uniqueness
Benjamin Kramer324322b2013-03-07 20:53:34 +00002005 Constant *ArgVec[] = { Val, Idx };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002006 const ConstantExprKeyType Key(Instruction::ExtractElement, ArgVec);
Galina Kistanovafc259902012-07-13 01:25:27 +00002007
Chris Lattner887ecac2011-07-09 18:23:52 +00002008 LLVMContextImpl *pImpl = Val->getContext().pImpl;
Owen Anderson1584a292009-08-04 20:25:11 +00002009 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00002010}
2011
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002012Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
2013 Constant *Idx, Type *OnlyIfReducedTy) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002014 assert(Val->getType()->isVectorTy() &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002015 "Tried to create insertelement operation on non-vector type!");
Chris Lattner8326bd82012-01-26 00:42:34 +00002016 assert(Elt->getType() == Val->getType()->getVectorElementType() &&
2017 "Insertelement types must match!");
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00002018 assert(Idx->getType()->isIntegerTy() &&
Reid Spencer2546b762007-01-26 07:37:34 +00002019 "Insertelement index must be i32 type!");
Robert Bocchinoca27f032006-01-17 20:07:22 +00002020
Chris Lattner887ecac2011-07-09 18:23:52 +00002021 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
2022 return FC; // Fold a few common cases.
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002023
2024 if (OnlyIfReducedTy == Val->getType())
2025 return nullptr;
2026
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002027 // Look up the constant in the table first to ensure uniqueness
Benjamin Kramer324322b2013-03-07 20:53:34 +00002028 Constant *ArgVec[] = { Val, Elt, Idx };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002029 const ConstantExprKeyType Key(Instruction::InsertElement, ArgVec);
Galina Kistanovafc259902012-07-13 01:25:27 +00002030
Chris Lattner887ecac2011-07-09 18:23:52 +00002031 LLVMContextImpl *pImpl = Val->getContext().pImpl;
2032 return pImpl->ExprConstants.getOrCreate(Val->getType(), Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002033}
2034
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002035Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
2036 Constant *Mask, Type *OnlyIfReducedTy) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002037 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
2038 "Invalid shuffle vector constant expr operands!");
Nate Begeman94aa38d2009-02-12 21:28:33 +00002039
Chris Lattner887ecac2011-07-09 18:23:52 +00002040 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
2041 return FC; // Fold a few common cases.
2042
Chris Lattner8326bd82012-01-26 00:42:34 +00002043 unsigned NElts = Mask->getType()->getVectorNumElements();
2044 Type *EltTy = V1->getType()->getVectorElementType();
Chris Lattner229907c2011-07-18 04:54:35 +00002045 Type *ShufTy = VectorType::get(EltTy, NElts);
Chris Lattner887ecac2011-07-09 18:23:52 +00002046
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002047 if (OnlyIfReducedTy == ShufTy)
2048 return nullptr;
2049
Chris Lattner887ecac2011-07-09 18:23:52 +00002050 // Look up the constant in the table first to ensure uniqueness
Benjamin Kramer324322b2013-03-07 20:53:34 +00002051 Constant *ArgVec[] = { V1, V2, Mask };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002052 const ConstantExprKeyType Key(Instruction::ShuffleVector, ArgVec);
Galina Kistanovafc259902012-07-13 01:25:27 +00002053
Chris Lattner887ecac2011-07-09 18:23:52 +00002054 LLVMContextImpl *pImpl = ShufTy->getContext().pImpl;
2055 return pImpl->ExprConstants.getOrCreate(ShufTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002056}
2057
Chris Lattner887ecac2011-07-09 18:23:52 +00002058Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002059 ArrayRef<unsigned> Idxs,
2060 Type *OnlyIfReducedTy) {
Hal Finkelb31366d2013-07-10 22:51:01 +00002061 assert(Agg->getType()->isFirstClassType() &&
2062 "Non-first-class type for constant insertvalue expression");
2063
Jay Foad57aa6362011-07-13 10:26:04 +00002064 assert(ExtractValueInst::getIndexedType(Agg->getType(),
2065 Idxs) == Val->getType() &&
Dan Gohman12fce772008-05-15 19:50:34 +00002066 "insertvalue indices invalid!");
Hal Finkelb31366d2013-07-10 22:51:01 +00002067 Type *ReqTy = Val->getType();
2068
2069 if (Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs))
2070 return FC;
2071
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002072 if (OnlyIfReducedTy == ReqTy)
2073 return nullptr;
2074
Hal Finkelb31366d2013-07-10 22:51:01 +00002075 Constant *ArgVec[] = { Agg, Val };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002076 const ConstantExprKeyType Key(Instruction::InsertValue, ArgVec, 0, 0, Idxs);
Hal Finkelb31366d2013-07-10 22:51:01 +00002077
2078 LLVMContextImpl *pImpl = Agg->getContext().pImpl;
2079 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Dan Gohman12fce772008-05-15 19:50:34 +00002080}
2081
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002082Constant *ConstantExpr::getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs,
2083 Type *OnlyIfReducedTy) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002084 assert(Agg->getType()->isFirstClassType() &&
Chris Lattner887ecac2011-07-09 18:23:52 +00002085 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002086
Chris Lattner229907c2011-07-18 04:54:35 +00002087 Type *ReqTy = ExtractValueInst::getIndexedType(Agg->getType(), Idxs);
Chandler Carruth9db56b82011-07-10 09:45:35 +00002088 (void)ReqTy;
Chris Lattner887ecac2011-07-09 18:23:52 +00002089 assert(ReqTy && "extractvalue indices invalid!");
Galina Kistanovafc259902012-07-13 01:25:27 +00002090
Dan Gohman0752bff2008-05-23 00:36:11 +00002091 assert(Agg->getType()->isFirstClassType() &&
2092 "Non-first-class type for constant extractvalue expression");
Hal Finkelb31366d2013-07-10 22:51:01 +00002093 if (Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs))
2094 return FC;
2095
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002096 if (OnlyIfReducedTy == ReqTy)
2097 return nullptr;
2098
Hal Finkelb31366d2013-07-10 22:51:01 +00002099 Constant *ArgVec[] = { Agg };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002100 const ConstantExprKeyType Key(Instruction::ExtractValue, ArgVec, 0, 0, Idxs);
Hal Finkelb31366d2013-07-10 22:51:01 +00002101
2102 LLVMContextImpl *pImpl = Agg->getContext().pImpl;
2103 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Dan Gohman12fce772008-05-15 19:50:34 +00002104}
2105
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002106Constant *ConstantExpr::getNeg(Constant *C, bool HasNUW, bool HasNSW) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002107 assert(C->getType()->isIntOrIntVectorTy() &&
Owen Anderson487375e2009-07-29 18:55:55 +00002108 "Cannot NEG a nonintegral value!");
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002109 return getSub(ConstantFP::getZeroValueForNegation(C->getType()),
2110 C, HasNUW, HasNSW);
Owen Anderson487375e2009-07-29 18:55:55 +00002111}
2112
Chris Lattnera676c0f2011-02-07 16:40:21 +00002113Constant *ConstantExpr::getFNeg(Constant *C) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002114 assert(C->getType()->isFPOrFPVectorTy() &&
Owen Anderson487375e2009-07-29 18:55:55 +00002115 "Cannot FNEG a non-floating-point value!");
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002116 return getFSub(ConstantFP::getZeroValueForNegation(C->getType()), C);
Owen Anderson487375e2009-07-29 18:55:55 +00002117}
2118
Chris Lattnera676c0f2011-02-07 16:40:21 +00002119Constant *ConstantExpr::getNot(Constant *C) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002120 assert(C->getType()->isIntOrIntVectorTy() &&
Owen Anderson487375e2009-07-29 18:55:55 +00002121 "Cannot NOT a nonintegral value!");
Owen Anderson5a1acd92009-07-31 20:28:14 +00002122 return get(Instruction::Xor, C, Constant::getAllOnesValue(C->getType()));
Owen Anderson487375e2009-07-29 18:55:55 +00002123}
2124
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002125Constant *ConstantExpr::getAdd(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::Add, C1, C2, Flags);
Owen Anderson487375e2009-07-29 18:55:55 +00002130}
2131
Chris Lattnera676c0f2011-02-07 16:40:21 +00002132Constant *ConstantExpr::getFAdd(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002133 return get(Instruction::FAdd, C1, C2);
2134}
2135
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002136Constant *ConstantExpr::getSub(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::Sub, C1, C2, Flags);
Owen Anderson487375e2009-07-29 18:55:55 +00002141}
2142
Chris Lattnera676c0f2011-02-07 16:40:21 +00002143Constant *ConstantExpr::getFSub(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002144 return get(Instruction::FSub, C1, C2);
2145}
2146
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002147Constant *ConstantExpr::getMul(Constant *C1, Constant *C2,
2148 bool HasNUW, bool HasNSW) {
2149 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2150 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
2151 return get(Instruction::Mul, C1, C2, Flags);
Owen Anderson487375e2009-07-29 18:55:55 +00002152}
2153
Chris Lattnera676c0f2011-02-07 16:40:21 +00002154Constant *ConstantExpr::getFMul(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002155 return get(Instruction::FMul, C1, C2);
2156}
2157
Chris Lattner0d75eac2011-02-09 16:43:07 +00002158Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2, bool isExact) {
2159 return get(Instruction::UDiv, C1, C2,
2160 isExact ? PossiblyExactOperator::IsExact : 0);
Owen Anderson487375e2009-07-29 18:55:55 +00002161}
2162
Chris Lattner0d75eac2011-02-09 16:43:07 +00002163Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2, bool isExact) {
2164 return get(Instruction::SDiv, C1, C2,
2165 isExact ? PossiblyExactOperator::IsExact : 0);
Owen Anderson487375e2009-07-29 18:55:55 +00002166}
2167
Chris Lattnera676c0f2011-02-07 16:40:21 +00002168Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002169 return get(Instruction::FDiv, C1, C2);
2170}
2171
Chris Lattnera676c0f2011-02-07 16:40:21 +00002172Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002173 return get(Instruction::URem, C1, C2);
2174}
2175
Chris Lattnera676c0f2011-02-07 16:40:21 +00002176Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002177 return get(Instruction::SRem, C1, C2);
2178}
2179
Chris Lattnera676c0f2011-02-07 16:40:21 +00002180Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002181 return get(Instruction::FRem, C1, C2);
2182}
2183
Chris Lattnera676c0f2011-02-07 16:40:21 +00002184Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002185 return get(Instruction::And, C1, C2);
2186}
2187
Chris Lattnera676c0f2011-02-07 16:40:21 +00002188Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002189 return get(Instruction::Or, C1, C2);
2190}
2191
Chris Lattnera676c0f2011-02-07 16:40:21 +00002192Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002193 return get(Instruction::Xor, C1, C2);
2194}
2195
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002196Constant *ConstantExpr::getShl(Constant *C1, Constant *C2,
2197 bool HasNUW, bool HasNSW) {
2198 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2199 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
2200 return get(Instruction::Shl, C1, C2, Flags);
Owen Anderson487375e2009-07-29 18:55:55 +00002201}
2202
Chris Lattner0d75eac2011-02-09 16:43:07 +00002203Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2, bool isExact) {
2204 return get(Instruction::LShr, C1, C2,
2205 isExact ? PossiblyExactOperator::IsExact : 0);
Owen Anderson487375e2009-07-29 18:55:55 +00002206}
2207
Chris Lattner0d75eac2011-02-09 16:43:07 +00002208Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2, bool isExact) {
2209 return get(Instruction::AShr, C1, C2,
2210 isExact ? PossiblyExactOperator::IsExact : 0);
Owen Anderson487375e2009-07-29 18:55:55 +00002211}
2212
Duncan Sandsd7aeefe2012-06-12 14:33:56 +00002213Constant *ConstantExpr::getBinOpIdentity(unsigned Opcode, Type *Ty) {
2214 switch (Opcode) {
2215 default:
Duncan Sands318a89d2012-06-13 09:42:13 +00002216 // Doesn't have an identity.
Craig Topperc6207612014-04-09 06:08:46 +00002217 return nullptr;
Duncan Sands318a89d2012-06-13 09:42:13 +00002218
Duncan Sandsd7aeefe2012-06-12 14:33:56 +00002219 case Instruction::Add:
2220 case Instruction::Or:
2221 case Instruction::Xor:
2222 return Constant::getNullValue(Ty);
2223
2224 case Instruction::Mul:
2225 return ConstantInt::get(Ty, 1);
2226
2227 case Instruction::And:
2228 return Constant::getAllOnesValue(Ty);
2229 }
2230}
2231
Duncan Sands318a89d2012-06-13 09:42:13 +00002232Constant *ConstantExpr::getBinOpAbsorber(unsigned Opcode, Type *Ty) {
2233 switch (Opcode) {
2234 default:
2235 // Doesn't have an absorber.
Craig Topperc6207612014-04-09 06:08:46 +00002236 return nullptr;
Duncan Sands318a89d2012-06-13 09:42:13 +00002237
2238 case Instruction::Or:
2239 return Constant::getAllOnesValue(Ty);
2240
2241 case Instruction::And:
2242 case Instruction::Mul:
2243 return Constant::getNullValue(Ty);
2244 }
2245}
2246
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00002247/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00002248void ConstantExpr::destroyConstantImpl() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002249 getType()->getContext().pImpl->ExprConstants.remove(this);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002250}
2251
Chris Lattner3cd8c562002-07-30 18:54:25 +00002252const char *ConstantExpr::getOpcodeName() const {
2253 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002254}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00002255
David Blaikie60310f22015-05-08 00:42:26 +00002256GetElementPtrConstantExpr::GetElementPtrConstantExpr(
2257 Type *SrcElementTy, Constant *C, ArrayRef<Constant *> IdxList, Type *DestTy)
2258 : ConstantExpr(DestTy, Instruction::GetElementPtr,
2259 OperandTraits<GetElementPtrConstantExpr>::op_end(this) -
2260 (IdxList.size() + 1),
2261 IdxList.size() + 1),
Eduard Burtescu19eb0312016-01-19 17:28:00 +00002262 SrcElementTy(SrcElementTy),
2263 ResElementTy(GetElementPtrInst::getIndexedType(SrcElementTy, IdxList)) {
Pete Coopereb31b682015-05-21 22:48:54 +00002264 Op<0>() = C;
Pete Cooper74510a42015-06-12 17:48:05 +00002265 Use *OperandList = getOperandList();
Chris Lattnera3b94ba2010-03-30 20:48:48 +00002266 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
2267 OperandList[i+1] = IdxList[i];
2268}
2269
David Blaikie60310f22015-05-08 00:42:26 +00002270Type *GetElementPtrConstantExpr::getSourceElementType() const {
2271 return SrcElementTy;
2272}
2273
Eduard Burtescu19eb0312016-01-19 17:28:00 +00002274Type *GetElementPtrConstantExpr::getResultElementType() const {
2275 return ResElementTy;
2276}
2277
Chris Lattner3756b912012-01-23 22:57:10 +00002278//===----------------------------------------------------------------------===//
2279// ConstantData* implementations
2280
Chris Lattnere4f3f102012-01-24 04:43:41 +00002281Type *ConstantDataSequential::getElementType() const {
2282 return getType()->getElementType();
2283}
2284
Chris Lattner5d4497b2012-01-24 09:31:43 +00002285StringRef ConstantDataSequential::getRawDataValues() const {
Chris Lattner00245f42012-01-24 13:41:11 +00002286 return StringRef(DataElements, getNumElements()*getElementByteSize());
Chris Lattner5d4497b2012-01-24 09:31:43 +00002287}
2288
Craig Toppere3dcce92015-08-01 22:20:21 +00002289bool ConstantDataSequential::isElementTypeCompatible(Type *Ty) {
Justin Bogner0ebc8602015-12-08 03:01:16 +00002290 if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) return true;
Craig Toppere3dcce92015-08-01 22:20:21 +00002291 if (auto *IT = dyn_cast<IntegerType>(Ty)) {
Chris Lattnere4f3f102012-01-24 04:43:41 +00002292 switch (IT->getBitWidth()) {
2293 case 8:
2294 case 16:
2295 case 32:
2296 case 64:
2297 return true;
2298 default: break;
2299 }
2300 }
2301 return false;
2302}
2303
Chris Lattner00245f42012-01-24 13:41:11 +00002304unsigned ConstantDataSequential::getNumElements() const {
Chris Lattner8a3df542012-01-25 01:32:59 +00002305 if (ArrayType *AT = dyn_cast<ArrayType>(getType()))
2306 return AT->getNumElements();
Chris Lattner8326bd82012-01-26 00:42:34 +00002307 return getType()->getVectorNumElements();
Chris Lattner00245f42012-01-24 13:41:11 +00002308}
2309
2310
Chris Lattnere4f3f102012-01-24 04:43:41 +00002311uint64_t ConstantDataSequential::getElementByteSize() const {
2312 return getElementType()->getPrimitiveSizeInBits()/8;
2313}
2314
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00002315/// Return the start of the specified element.
Chris Lattnere4f3f102012-01-24 04:43:41 +00002316const char *ConstantDataSequential::getElementPointer(unsigned Elt) const {
Chris Lattner00245f42012-01-24 13:41:11 +00002317 assert(Elt < getNumElements() && "Invalid Elt");
Chris Lattnere4f3f102012-01-24 04:43:41 +00002318 return DataElements+Elt*getElementByteSize();
2319}
2320
2321
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00002322/// Return true if the array is empty or all zeros.
Chris Lattner3756b912012-01-23 22:57:10 +00002323static bool isAllZeros(StringRef Arr) {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +00002324 for (char I : Arr)
2325 if (I != 0)
Chris Lattner3756b912012-01-23 22:57:10 +00002326 return false;
2327 return true;
2328}
Chris Lattner030af792012-01-24 05:42:11 +00002329
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00002330/// This is the underlying implementation of all of the
Chris Lattner3756b912012-01-23 22:57:10 +00002331/// ConstantDataSequential::get methods. They all thunk down to here, providing
Chris Lattnerf06039b2012-01-30 18:19:30 +00002332/// the correct element type. We take the bytes in as a StringRef because
Chris Lattner3756b912012-01-23 22:57:10 +00002333/// we *want* an underlying "char*" to avoid TBAA type punning violations.
2334Constant *ConstantDataSequential::getImpl(StringRef Elements, Type *Ty) {
Chris Lattner8326bd82012-01-26 00:42:34 +00002335 assert(isElementTypeCompatible(Ty->getSequentialElementType()));
Chris Lattner139822f2012-01-24 14:17:05 +00002336 // If the elements are all zero or there are no elements, return a CAZ, which
2337 // is more dense and canonical.
Chris Lattner3756b912012-01-23 22:57:10 +00002338 if (isAllZeros(Elements))
2339 return ConstantAggregateZero::get(Ty);
2340
2341 // Do a lookup to see if we have already formed one of these.
David Blaikie5106ce72014-11-19 05:49:42 +00002342 auto &Slot =
2343 *Ty->getContext()
2344 .pImpl->CDSConstants.insert(std::make_pair(Elements, nullptr))
2345 .first;
Galina Kistanovafc259902012-07-13 01:25:27 +00002346
Chris Lattner3756b912012-01-23 22:57:10 +00002347 // The bucket can point to a linked list of different CDS's that have the same
2348 // body but different types. For example, 0,0,0,1 could be a 4 element array
2349 // of i8, or a 1-element array of i32. They'll both end up in the same
2350 /// StringMap bucket, linked up by their Next pointers. Walk the list.
David Blaikie5106ce72014-11-19 05:49:42 +00002351 ConstantDataSequential **Entry = &Slot.second;
Craig Topperc6207612014-04-09 06:08:46 +00002352 for (ConstantDataSequential *Node = *Entry; Node;
Chris Lattner3756b912012-01-23 22:57:10 +00002353 Entry = &Node->Next, Node = *Entry)
2354 if (Node->getType() == Ty)
2355 return Node;
Galina Kistanovafc259902012-07-13 01:25:27 +00002356
Chris Lattner3756b912012-01-23 22:57:10 +00002357 // Okay, we didn't get a hit. Create a node of the right class, link it in,
2358 // and return it.
2359 if (isa<ArrayType>(Ty))
David Blaikie5106ce72014-11-19 05:49:42 +00002360 return *Entry = new ConstantDataArray(Ty, Slot.first().data());
Chris Lattner3756b912012-01-23 22:57:10 +00002361
2362 assert(isa<VectorType>(Ty));
David Blaikie5106ce72014-11-19 05:49:42 +00002363 return *Entry = new ConstantDataVector(Ty, Slot.first().data());
Chris Lattner3756b912012-01-23 22:57:10 +00002364}
2365
Pete Cooper86dd4cf2015-06-23 21:55:11 +00002366void ConstantDataSequential::destroyConstantImpl() {
Chris Lattner3756b912012-01-23 22:57:10 +00002367 // Remove the constant from the StringMap.
2368 StringMap<ConstantDataSequential*> &CDSConstants =
2369 getType()->getContext().pImpl->CDSConstants;
Galina Kistanovafc259902012-07-13 01:25:27 +00002370
Chris Lattner3756b912012-01-23 22:57:10 +00002371 StringMap<ConstantDataSequential*>::iterator Slot =
Chris Lattner5d4497b2012-01-24 09:31:43 +00002372 CDSConstants.find(getRawDataValues());
Chris Lattner3756b912012-01-23 22:57:10 +00002373
2374 assert(Slot != CDSConstants.end() && "CDS not found in uniquing table");
2375
2376 ConstantDataSequential **Entry = &Slot->getValue();
2377
2378 // Remove the entry from the hash table.
Craig Topperc6207612014-04-09 06:08:46 +00002379 if (!(*Entry)->Next) {
Chris Lattner3756b912012-01-23 22:57:10 +00002380 // If there is only one value in the bucket (common case) it must be this
2381 // entry, and removing the entry should remove the bucket completely.
2382 assert((*Entry) == this && "Hash mismatch in ConstantDataSequential");
2383 getContext().pImpl->CDSConstants.erase(Slot);
2384 } else {
2385 // Otherwise, there are multiple entries linked off the bucket, unlink the
2386 // node we care about but keep the bucket around.
2387 for (ConstantDataSequential *Node = *Entry; ;
2388 Entry = &Node->Next, Node = *Entry) {
2389 assert(Node && "Didn't find entry in its uniquing hash table!");
2390 // If we found our entry, unlink it from the list and we're done.
2391 if (Node == this) {
2392 *Entry = Node->Next;
2393 break;
2394 }
2395 }
2396 }
Galina Kistanovafc259902012-07-13 01:25:27 +00002397
Chris Lattner3756b912012-01-23 22:57:10 +00002398 // If we were part of a list, make sure that we don't delete the list that is
2399 // still owned by the uniquing map.
Craig Topperc6207612014-04-09 06:08:46 +00002400 Next = nullptr;
Chris Lattner3756b912012-01-23 22:57:10 +00002401}
2402
2403/// get() constructors - Return a constant with array type with an element
2404/// count and element type matching the ArrayRef passed in. Note that this
2405/// can return a ConstantAggregateZero object.
Chris Lattner20683932012-01-24 14:04:40 +00002406Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint8_t> Elts) {
Chris Lattner3756b912012-01-23 22:57:10 +00002407 Type *Ty = ArrayType::get(Type::getInt8Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002408 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002409 return getImpl(StringRef(Data, Elts.size() * 1), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002410}
Chris Lattner20683932012-01-24 14:04:40 +00002411Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint16_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002412 Type *Ty = ArrayType::get(Type::getInt16Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002413 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002414 return getImpl(StringRef(Data, Elts.size() * 2), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002415}
Chris Lattner20683932012-01-24 14:04:40 +00002416Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint32_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002417 Type *Ty = ArrayType::get(Type::getInt32Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002418 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002419 return getImpl(StringRef(Data, Elts.size() * 4), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002420}
Chris Lattner20683932012-01-24 14:04:40 +00002421Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint64_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002422 Type *Ty = ArrayType::get(Type::getInt64Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002423 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002424 return getImpl(StringRef(Data, Elts.size() * 8), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002425}
Chris Lattner20683932012-01-24 14:04:40 +00002426Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<float> Elts) {
Chris Lattner3756b912012-01-23 22:57:10 +00002427 Type *Ty = ArrayType::get(Type::getFloatTy(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002428 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002429 return getImpl(StringRef(Data, Elts.size() * 4), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002430}
Chris Lattner20683932012-01-24 14:04:40 +00002431Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<double> Elts) {
Chris Lattner3756b912012-01-23 22:57:10 +00002432 Type *Ty = ArrayType::get(Type::getDoubleTy(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002433 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002434 return getImpl(StringRef(Data, Elts.size() * 8), Ty);
Rafael Espindola8c97e192015-02-19 16:08:20 +00002435}
2436
2437/// getFP() constructors - Return a constant with array type with an element
2438/// count and element type of float with precision matching the number of
2439/// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits,
2440/// double for 64bits) Note that this can return a ConstantAggregateZero
2441/// object.
2442Constant *ConstantDataArray::getFP(LLVMContext &Context,
2443 ArrayRef<uint16_t> Elts) {
Justin Bognerb7389d6712015-12-09 21:21:07 +00002444 Type *Ty = ArrayType::get(Type::getHalfTy(Context), Elts.size());
Rafael Espindola8c97e192015-02-19 16:08:20 +00002445 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002446 return getImpl(StringRef(Data, Elts.size() * 2), Ty);
Rafael Espindola8c97e192015-02-19 16:08:20 +00002447}
2448Constant *ConstantDataArray::getFP(LLVMContext &Context,
2449 ArrayRef<uint32_t> Elts) {
2450 Type *Ty = ArrayType::get(Type::getFloatTy(Context), Elts.size());
2451 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002452 return getImpl(StringRef(Data, Elts.size() * 4), Ty);
Rafael Espindola8c97e192015-02-19 16:08:20 +00002453}
2454Constant *ConstantDataArray::getFP(LLVMContext &Context,
2455 ArrayRef<uint64_t> Elts) {
2456 Type *Ty = ArrayType::get(Type::getDoubleTy(Context), Elts.size());
2457 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002458 return getImpl(StringRef(Data, Elts.size() * 8), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002459}
2460
Chris Lattner20683932012-01-24 14:04:40 +00002461Constant *ConstantDataArray::getString(LLVMContext &Context,
2462 StringRef Str, bool AddNull) {
Galina Kistanovafc259902012-07-13 01:25:27 +00002463 if (!AddNull) {
2464 const uint8_t *Data = reinterpret_cast<const uint8_t *>(Str.data());
Craig Topper393ce692017-07-11 15:52:21 +00002465 return get(Context, makeArrayRef(Data, Str.size()));
Galina Kistanovafc259902012-07-13 01:25:27 +00002466 }
2467
Chris Lattner20683932012-01-24 14:04:40 +00002468 SmallVector<uint8_t, 64> ElementVals;
2469 ElementVals.append(Str.begin(), Str.end());
2470 ElementVals.push_back(0);
2471 return get(Context, ElementVals);
2472}
Chris Lattner3756b912012-01-23 22:57:10 +00002473
2474/// get() constructors - Return a constant with vector type with an element
2475/// count and element type matching the ArrayRef passed in. Note that this
2476/// can return a ConstantAggregateZero object.
Chris Lattner20683932012-01-24 14:04:40 +00002477Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint8_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002478 Type *Ty = VectorType::get(Type::getInt8Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002479 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002480 return getImpl(StringRef(Data, Elts.size() * 1), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002481}
Chris Lattner20683932012-01-24 14:04:40 +00002482Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint16_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002483 Type *Ty = VectorType::get(Type::getInt16Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002484 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002485 return getImpl(StringRef(Data, Elts.size() * 2), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002486}
Chris Lattner20683932012-01-24 14:04:40 +00002487Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint32_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002488 Type *Ty = VectorType::get(Type::getInt32Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002489 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002490 return getImpl(StringRef(Data, Elts.size() * 4), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002491}
Chris Lattner20683932012-01-24 14:04:40 +00002492Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint64_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002493 Type *Ty = VectorType::get(Type::getInt64Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002494 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002495 return getImpl(StringRef(Data, Elts.size() * 8), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002496}
Chris Lattner20683932012-01-24 14:04:40 +00002497Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<float> Elts) {
Chris Lattner3756b912012-01-23 22:57:10 +00002498 Type *Ty = VectorType::get(Type::getFloatTy(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002499 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002500 return getImpl(StringRef(Data, Elts.size() * 4), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002501}
Chris Lattner20683932012-01-24 14:04:40 +00002502Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<double> Elts) {
Chris Lattner3756b912012-01-23 22:57:10 +00002503 Type *Ty = VectorType::get(Type::getDoubleTy(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002504 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002505 return getImpl(StringRef(Data, Elts.size() * 8), Ty);
Rafael Espindola8c97e192015-02-19 16:08:20 +00002506}
2507
2508/// getFP() constructors - Return a constant with vector type with an element
2509/// count and element type of float with the precision matching the number of
2510/// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits,
2511/// double for 64bits) Note that this can return a ConstantAggregateZero
2512/// object.
2513Constant *ConstantDataVector::getFP(LLVMContext &Context,
2514 ArrayRef<uint16_t> Elts) {
2515 Type *Ty = VectorType::get(Type::getHalfTy(Context), Elts.size());
2516 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002517 return getImpl(StringRef(Data, Elts.size() * 2), Ty);
Rafael Espindola8c97e192015-02-19 16:08:20 +00002518}
2519Constant *ConstantDataVector::getFP(LLVMContext &Context,
2520 ArrayRef<uint32_t> Elts) {
2521 Type *Ty = VectorType::get(Type::getFloatTy(Context), Elts.size());
2522 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002523 return getImpl(StringRef(Data, Elts.size() * 4), Ty);
Rafael Espindola8c97e192015-02-19 16:08:20 +00002524}
2525Constant *ConstantDataVector::getFP(LLVMContext &Context,
2526 ArrayRef<uint64_t> Elts) {
2527 Type *Ty = VectorType::get(Type::getDoubleTy(Context), Elts.size());
2528 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002529 return getImpl(StringRef(Data, Elts.size() * 8), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002530}
2531
Chris Lattnere9eed292012-01-25 05:19:54 +00002532Constant *ConstantDataVector::getSplat(unsigned NumElts, Constant *V) {
2533 assert(isElementTypeCompatible(V->getType()) &&
2534 "Element type not compatible with ConstantData");
2535 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
2536 if (CI->getType()->isIntegerTy(8)) {
2537 SmallVector<uint8_t, 16> Elts(NumElts, CI->getZExtValue());
2538 return get(V->getContext(), Elts);
2539 }
2540 if (CI->getType()->isIntegerTy(16)) {
2541 SmallVector<uint16_t, 16> Elts(NumElts, CI->getZExtValue());
2542 return get(V->getContext(), Elts);
2543 }
2544 if (CI->getType()->isIntegerTy(32)) {
2545 SmallVector<uint32_t, 16> Elts(NumElts, CI->getZExtValue());
2546 return get(V->getContext(), Elts);
2547 }
2548 assert(CI->getType()->isIntegerTy(64) && "Unsupported ConstantData type");
2549 SmallVector<uint64_t, 16> Elts(NumElts, CI->getZExtValue());
2550 return get(V->getContext(), Elts);
2551 }
2552
Chris Lattner978fe0c2012-01-30 06:21:21 +00002553 if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
Justin Bogner0ebc8602015-12-08 03:01:16 +00002554 if (CFP->getType()->isHalfTy()) {
2555 SmallVector<uint16_t, 16> Elts(
2556 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
2557 return getFP(V->getContext(), Elts);
2558 }
Chris Lattner978fe0c2012-01-30 06:21:21 +00002559 if (CFP->getType()->isFloatTy()) {
Rafael Espindola8c97e192015-02-19 16:08:20 +00002560 SmallVector<uint32_t, 16> Elts(
2561 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
2562 return getFP(V->getContext(), Elts);
Chris Lattner978fe0c2012-01-30 06:21:21 +00002563 }
2564 if (CFP->getType()->isDoubleTy()) {
Rafael Espindola8c97e192015-02-19 16:08:20 +00002565 SmallVector<uint64_t, 16> Elts(
2566 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
2567 return getFP(V->getContext(), Elts);
Chris Lattner978fe0c2012-01-30 06:21:21 +00002568 }
Chris Lattnere9eed292012-01-25 05:19:54 +00002569 }
Chris Lattner978fe0c2012-01-30 06:21:21 +00002570 return ConstantVector::getSplat(NumElts, V);
Chris Lattnere9eed292012-01-25 05:19:54 +00002571}
2572
2573
Chris Lattnere4f3f102012-01-24 04:43:41 +00002574uint64_t ConstantDataSequential::getElementAsInteger(unsigned Elt) const {
2575 assert(isa<IntegerType>(getElementType()) &&
2576 "Accessor can only be used when element is an integer");
2577 const char *EltPtr = getElementPointer(Elt);
Galina Kistanovafc259902012-07-13 01:25:27 +00002578
Chris Lattnere4f3f102012-01-24 04:43:41 +00002579 // The data is stored in host byte order, make sure to cast back to the right
2580 // type to load with the right endianness.
Chris Lattner8326bd82012-01-26 00:42:34 +00002581 switch (getElementType()->getIntegerBitWidth()) {
Craig Topperc514b542012-02-05 22:14:15 +00002582 default: llvm_unreachable("Invalid bitwidth for CDS");
Galina Kistanovafc259902012-07-13 01:25:27 +00002583 case 8:
Craig Topper393ce692017-07-11 15:52:21 +00002584 return *reinterpret_cast<const uint8_t *>(EltPtr);
Galina Kistanovafc259902012-07-13 01:25:27 +00002585 case 16:
Craig Topper393ce692017-07-11 15:52:21 +00002586 return *reinterpret_cast<const uint16_t *>(EltPtr);
Galina Kistanovafc259902012-07-13 01:25:27 +00002587 case 32:
Craig Topper393ce692017-07-11 15:52:21 +00002588 return *reinterpret_cast<const uint32_t *>(EltPtr);
Galina Kistanovafc259902012-07-13 01:25:27 +00002589 case 64:
Craig Topper393ce692017-07-11 15:52:21 +00002590 return *reinterpret_cast<const uint64_t *>(EltPtr);
Chris Lattnere4f3f102012-01-24 04:43:41 +00002591 }
2592}
2593
Craig Topper0b4b4e32017-07-15 22:06:19 +00002594APInt ConstantDataSequential::getElementAsAPInt(unsigned Elt) const {
2595 assert(isa<IntegerType>(getElementType()) &&
2596 "Accessor can only be used when element is an integer");
2597 const char *EltPtr = getElementPointer(Elt);
2598
2599 // The data is stored in host byte order, make sure to cast back to the right
2600 // type to load with the right endianness.
2601 switch (getElementType()->getIntegerBitWidth()) {
2602 default: llvm_unreachable("Invalid bitwidth for CDS");
2603 case 8: {
2604 auto EltVal = *reinterpret_cast<const uint8_t *>(EltPtr);
2605 return APInt(8, EltVal);
2606 }
2607 case 16: {
2608 auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr);
2609 return APInt(16, EltVal);
2610 }
2611 case 32: {
2612 auto EltVal = *reinterpret_cast<const uint32_t *>(EltPtr);
2613 return APInt(32, EltVal);
2614 }
2615 case 64: {
2616 auto EltVal = *reinterpret_cast<const uint64_t *>(EltPtr);
2617 return APInt(64, EltVal);
2618 }
2619 }
2620}
2621
Chris Lattnere4f3f102012-01-24 04:43:41 +00002622APFloat ConstantDataSequential::getElementAsAPFloat(unsigned Elt) const {
2623 const char *EltPtr = getElementPointer(Elt);
2624
2625 switch (getElementType()->getTypeID()) {
Nick Lewyckyff509622012-01-25 03:20:12 +00002626 default:
Craig Topperc514b542012-02-05 22:14:15 +00002627 llvm_unreachable("Accessor can only be used when element is float/double!");
Justin Bogner0ebc8602015-12-08 03:01:16 +00002628 case Type::HalfTyID: {
2629 auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr);
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002630 return APFloat(APFloat::IEEEhalf(), APInt(16, EltVal));
Justin Bogner0ebc8602015-12-08 03:01:16 +00002631 }
Benjamin Kramer7af984b2015-02-20 15:11:55 +00002632 case Type::FloatTyID: {
2633 auto EltVal = *reinterpret_cast<const uint32_t *>(EltPtr);
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002634 return APFloat(APFloat::IEEEsingle(), APInt(32, EltVal));
Benjamin Kramer7af984b2015-02-20 15:11:55 +00002635 }
2636 case Type::DoubleTyID: {
2637 auto EltVal = *reinterpret_cast<const uint64_t *>(EltPtr);
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002638 return APFloat(APFloat::IEEEdouble(), APInt(64, EltVal));
Chris Lattnere4f3f102012-01-24 04:43:41 +00002639 }
Benjamin Kramer7af984b2015-02-20 15:11:55 +00002640 }
Chris Lattnere4f3f102012-01-24 04:43:41 +00002641}
2642
Chris Lattnere4f3f102012-01-24 04:43:41 +00002643float ConstantDataSequential::getElementAsFloat(unsigned Elt) const {
2644 assert(getElementType()->isFloatTy() &&
2645 "Accessor can only be used when element is a 'float'");
Craig Topper393ce692017-07-11 15:52:21 +00002646 return *reinterpret_cast<const float *>(getElementPointer(Elt));
Chris Lattnere4f3f102012-01-24 04:43:41 +00002647}
2648
Chris Lattnere4f3f102012-01-24 04:43:41 +00002649double ConstantDataSequential::getElementAsDouble(unsigned Elt) const {
2650 assert(getElementType()->isDoubleTy() &&
2651 "Accessor can only be used when element is a 'float'");
Craig Topper393ce692017-07-11 15:52:21 +00002652 return *reinterpret_cast<const double *>(getElementPointer(Elt));
Chris Lattnere4f3f102012-01-24 04:43:41 +00002653}
2654
Chris Lattnere4f3f102012-01-24 04:43:41 +00002655Constant *ConstantDataSequential::getElementAsConstant(unsigned Elt) const {
Justin Bogner0ebc8602015-12-08 03:01:16 +00002656 if (getElementType()->isHalfTy() || getElementType()->isFloatTy() ||
2657 getElementType()->isDoubleTy())
Chris Lattnere4f3f102012-01-24 04:43:41 +00002658 return ConstantFP::get(getContext(), getElementAsAPFloat(Elt));
Galina Kistanovafc259902012-07-13 01:25:27 +00002659
Chris Lattnere4f3f102012-01-24 04:43:41 +00002660 return ConstantInt::get(getElementType(), getElementAsInteger(Elt));
2661}
2662
Matthias Braun50ec0b52017-05-19 22:37:09 +00002663bool ConstantDataSequential::isString(unsigned CharSize) const {
2664 return isa<ArrayType>(getType()) && getElementType()->isIntegerTy(CharSize);
Chris Lattner5dd4d872012-01-24 09:01:07 +00002665}
Chris Lattner3756b912012-01-23 22:57:10 +00002666
Chris Lattner5dd4d872012-01-24 09:01:07 +00002667bool ConstantDataSequential::isCString() const {
2668 if (!isString())
2669 return false;
Galina Kistanovafc259902012-07-13 01:25:27 +00002670
Chris Lattner5dd4d872012-01-24 09:01:07 +00002671 StringRef Str = getAsString();
Galina Kistanovafc259902012-07-13 01:25:27 +00002672
Chris Lattner5dd4d872012-01-24 09:01:07 +00002673 // The last value must be nul.
2674 if (Str.back() != 0) return false;
Galina Kistanovafc259902012-07-13 01:25:27 +00002675
Chris Lattner5dd4d872012-01-24 09:01:07 +00002676 // Other elements must be non-nul.
2677 return Str.drop_back().find(0) == StringRef::npos;
2678}
Chris Lattner3756b912012-01-23 22:57:10 +00002679
Craig Topper0b4b4e32017-07-15 22:06:19 +00002680bool ConstantDataVector::isSplat() const {
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002681 const char *Base = getRawDataValues().data();
Galina Kistanovafc259902012-07-13 01:25:27 +00002682
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002683 // Compare elements 1+ to the 0'th element.
2684 unsigned EltSize = getElementByteSize();
2685 for (unsigned i = 1, e = getNumElements(); i != e; ++i)
2686 if (memcmp(Base, Base+i*EltSize, EltSize))
Craig Topper0b4b4e32017-07-15 22:06:19 +00002687 return false;
Galina Kistanovafc259902012-07-13 01:25:27 +00002688
Craig Topper0b4b4e32017-07-15 22:06:19 +00002689 return true;
2690}
2691
2692Constant *ConstantDataVector::getSplatValue() const {
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002693 // If they're all the same, return the 0th one as a representative.
Craig Topper0b4b4e32017-07-15 22:06:19 +00002694 return isSplat() ? getElementAsConstant(0) : nullptr;
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002695}
Chris Lattnera3b94ba2010-03-30 20:48:48 +00002696
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002697//===----------------------------------------------------------------------===//
Pete Cooper5815b1f2015-06-24 18:55:24 +00002698// handleOperandChange implementations
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002699
Pete Cooper5815b1f2015-06-24 18:55:24 +00002700/// Update this constant array to change uses of
Chris Lattner913849b2007-08-21 00:55:23 +00002701/// 'From' to be uses of 'To'. This must update the uniquing data structures
2702/// etc.
2703///
2704/// Note that we intentionally replace all uses of From with To here. Consider
2705/// a large array that uses 'From' 1000 times. By handling this case all here,
Pete Cooper5815b1f2015-06-24 18:55:24 +00002706/// ConstantArray::handleOperandChange is only invoked once, and that
Chris Lattner913849b2007-08-21 00:55:23 +00002707/// single invocation handles all 1000 uses. Handling them one at a time would
2708/// work, but would be really slow because it would have to unique each updated
2709/// array instance.
Chris Lattner31b132c2009-10-28 00:01:44 +00002710///
Mehdi Amini8914d292016-02-10 22:47:15 +00002711void Constant::handleOperandChange(Value *From, Value *To) {
Pete Cooper5815b1f2015-06-24 18:55:24 +00002712 Value *Replacement = nullptr;
2713 switch (getValueID()) {
2714 default:
2715 llvm_unreachable("Not a constant!");
2716#define HANDLE_CONSTANT(Name) \
2717 case Value::Name##Val: \
Mehdi Amini8914d292016-02-10 22:47:15 +00002718 Replacement = cast<Name>(this)->handleOperandChangeImpl(From, To); \
Pete Cooper5815b1f2015-06-24 18:55:24 +00002719 break;
2720#include "llvm/IR/Value.def"
2721 }
2722
2723 // If handleOperandChangeImpl returned nullptr, then it handled
2724 // replacing itself and we don't want to delete or replace anything else here.
2725 if (!Replacement)
2726 return;
2727
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002728 // I do need to replace this with an existing value.
2729 assert(Replacement != this && "I didn't contain From!");
2730
2731 // Everyone using this now uses the replacement.
2732 replaceAllUsesWith(Replacement);
2733
2734 // Delete the old constant!
2735 destroyConstant();
2736}
2737
Mehdi Amini8914d292016-02-10 22:47:15 +00002738Value *ConstantArray::handleOperandChangeImpl(Value *From, Value *To) {
Owen Andersonc2c79322009-07-28 18:32:17 +00002739 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2740 Constant *ToC = cast<Constant>(To);
2741
Talin46e9b442012-02-05 20:54:10 +00002742 SmallVector<Constant*, 8> Values;
Owen Andersonc2c79322009-07-28 18:32:17 +00002743 Values.reserve(getNumOperands()); // Build replacement array.
2744
Galina Kistanovafc259902012-07-13 01:25:27 +00002745 // Fill values with the modified operands of the constant array. Also,
Owen Andersonc2c79322009-07-28 18:32:17 +00002746 // compute whether this turns into an all-zeros array.
Owen Andersonc2c79322009-07-28 18:32:17 +00002747 unsigned NumUpdated = 0;
Galina Kistanovafc259902012-07-13 01:25:27 +00002748
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002749 // Keep track of whether all the values in the array are "ToC".
2750 bool AllSame = true;
Pete Cooper74510a42015-06-12 17:48:05 +00002751 Use *OperandList = getOperandList();
Mehdi Amini8914d292016-02-10 22:47:15 +00002752 unsigned OperandNo = 0;
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002753 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2754 Constant *Val = cast<Constant>(O->get());
2755 if (Val == From) {
Mehdi Amini8914d292016-02-10 22:47:15 +00002756 OperandNo = (O - OperandList);
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002757 Val = ToC;
2758 ++NumUpdated;
Owen Andersonc2c79322009-07-28 18:32:17 +00002759 }
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002760 Values.push_back(Val);
Talin46e9b442012-02-05 20:54:10 +00002761 AllSame &= Val == ToC;
Owen Andersonc2c79322009-07-28 18:32:17 +00002762 }
Galina Kistanovafc259902012-07-13 01:25:27 +00002763
Pete Cooper5815b1f2015-06-24 18:55:24 +00002764 if (AllSame && ToC->isNullValue())
2765 return ConstantAggregateZero::get(getType());
2766
2767 if (AllSame && isa<UndefValue>(ToC))
2768 return UndefValue::get(getType());
Aaron Ballmane4b91dc2014-08-19 14:59:02 +00002769
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002770 // Check for any other type of constant-folding.
Pete Cooper5815b1f2015-06-24 18:55:24 +00002771 if (Constant *C = getImpl(getType(), Values))
2772 return C;
Aaron Ballmane4b91dc2014-08-19 14:59:02 +00002773
Duncan P. N. Exon Smith909620a2014-08-19 19:13:30 +00002774 // Update to the new value.
Pete Cooper5815b1f2015-06-24 18:55:24 +00002775 return getContext().pImpl->ArrayConstants.replaceOperandsInPlace(
Mehdi Amini8914d292016-02-10 22:47:15 +00002776 Values, this, From, ToC, NumUpdated, OperandNo);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002777}
2778
Mehdi Amini8914d292016-02-10 22:47:15 +00002779Value *ConstantStruct::handleOperandChangeImpl(Value *From, Value *To) {
Owen Anderson45308b52009-07-27 22:29:26 +00002780 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2781 Constant *ToC = cast<Constant>(To);
2782
Pete Cooper74510a42015-06-12 17:48:05 +00002783 Use *OperandList = getOperandList();
Owen Anderson45308b52009-07-27 22:29:26 +00002784
Talin46e9b442012-02-05 20:54:10 +00002785 SmallVector<Constant*, 8> Values;
Owen Anderson45308b52009-07-27 22:29:26 +00002786 Values.reserve(getNumOperands()); // Build replacement struct.
Galina Kistanovafc259902012-07-13 01:25:27 +00002787
2788 // Fill values with the modified operands of the constant struct. Also,
Owen Anderson45308b52009-07-27 22:29:26 +00002789 // compute whether this turns into an all-zeros struct.
Mehdi Amini8914d292016-02-10 22:47:15 +00002790 unsigned NumUpdated = 0;
2791 bool AllSame = true;
2792 unsigned OperandNo = 0;
2793 for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O) {
2794 Constant *Val = cast<Constant>(O->get());
2795 if (Val == From) {
2796 OperandNo = (O - OperandList);
2797 Val = ToC;
2798 ++NumUpdated;
Owen Anderson45308b52009-07-27 22:29:26 +00002799 }
Mehdi Amini8914d292016-02-10 22:47:15 +00002800 Values.push_back(Val);
2801 AllSame &= Val == ToC;
Owen Anderson45308b52009-07-27 22:29:26 +00002802 }
Galina Kistanovafc259902012-07-13 01:25:27 +00002803
Mehdi Amini8914d292016-02-10 22:47:15 +00002804 if (AllSame && ToC->isNullValue())
Pete Cooper5815b1f2015-06-24 18:55:24 +00002805 return ConstantAggregateZero::get(getType());
2806
Mehdi Amini8914d292016-02-10 22:47:15 +00002807 if (AllSame && isa<UndefValue>(ToC))
Pete Cooper5815b1f2015-06-24 18:55:24 +00002808 return UndefValue::get(getType());
Galina Kistanovafc259902012-07-13 01:25:27 +00002809
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002810 // Update to the new value.
Pete Cooper5815b1f2015-06-24 18:55:24 +00002811 return getContext().pImpl->StructConstants.replaceOperandsInPlace(
Mehdi Amini8914d292016-02-10 22:47:15 +00002812 Values, this, From, ToC, NumUpdated, OperandNo);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002813}
2814
Mehdi Amini8914d292016-02-10 22:47:15 +00002815Value *ConstantVector::handleOperandChangeImpl(Value *From, Value *To) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002816 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002817 Constant *ToC = cast<Constant>(To);
Galina Kistanovafc259902012-07-13 01:25:27 +00002818
Chris Lattnera474bb22012-01-26 20:40:56 +00002819 SmallVector<Constant*, 8> Values;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002820 Values.reserve(getNumOperands()); // Build replacement array...
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002821 unsigned NumUpdated = 0;
Mehdi Amini8914d292016-02-10 22:47:15 +00002822 unsigned OperandNo = 0;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002823 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2824 Constant *Val = getOperand(i);
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002825 if (Val == From) {
Mehdi Amini8914d292016-02-10 22:47:15 +00002826 OperandNo = i;
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002827 ++NumUpdated;
2828 Val = ToC;
2829 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002830 Values.push_back(Val);
2831 }
Galina Kistanovafc259902012-07-13 01:25:27 +00002832
Pete Cooper5815b1f2015-06-24 18:55:24 +00002833 if (Constant *C = getImpl(Values))
2834 return C;
Duncan P. N. Exon Smith687744d2014-08-19 02:24:46 +00002835
Duncan P. N. Exon Smith909620a2014-08-19 19:13:30 +00002836 // Update to the new value.
Pete Cooper5815b1f2015-06-24 18:55:24 +00002837 return getContext().pImpl->VectorConstants.replaceOperandsInPlace(
Mehdi Amini8914d292016-02-10 22:47:15 +00002838 Values, this, From, ToC, NumUpdated, OperandNo);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002839}
2840
Mehdi Amini8914d292016-02-10 22:47:15 +00002841Value *ConstantExpr::handleOperandChangeImpl(Value *From, Value *ToV) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002842 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2843 Constant *To = cast<Constant>(ToV);
Galina Kistanovafc259902012-07-13 01:25:27 +00002844
Chris Lattner37e38352012-01-26 20:37:11 +00002845 SmallVector<Constant*, 8> NewOps;
Duncan P. N. Exon Smith33de00c2014-08-19 20:03:35 +00002846 unsigned NumUpdated = 0;
Mehdi Amini8914d292016-02-10 22:47:15 +00002847 unsigned OperandNo = 0;
Chris Lattner37e38352012-01-26 20:37:11 +00002848 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2849 Constant *Op = getOperand(i);
Duncan P. N. Exon Smith33de00c2014-08-19 20:03:35 +00002850 if (Op == From) {
Mehdi Amini8914d292016-02-10 22:47:15 +00002851 OperandNo = i;
Duncan P. N. Exon Smith33de00c2014-08-19 20:03:35 +00002852 ++NumUpdated;
2853 Op = To;
2854 }
2855 NewOps.push_back(Op);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002856 }
Duncan P. N. Exon Smith33de00c2014-08-19 20:03:35 +00002857 assert(NumUpdated && "I didn't contain From!");
Galina Kistanovafc259902012-07-13 01:25:27 +00002858
Pete Cooper5815b1f2015-06-24 18:55:24 +00002859 if (Constant *C = getWithOperands(NewOps, getType(), true))
2860 return C;
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002861
Duncan P. N. Exon Smith33de00c2014-08-19 20:03:35 +00002862 // Update to the new value.
Pete Cooper5815b1f2015-06-24 18:55:24 +00002863 return getContext().pImpl->ExprConstants.replaceOperandsInPlace(
Mehdi Amini8914d292016-02-10 22:47:15 +00002864 NewOps, this, From, To, NumUpdated, OperandNo);
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002865}
2866
James Molloyce545682012-11-17 17:56:30 +00002867Instruction *ConstantExpr::getAsInstruction() {
Benjamin Kramer5fbfe2f2015-02-28 13:20:15 +00002868 SmallVector<Value *, 4> ValueOperands(op_begin(), op_end());
James Molloyce545682012-11-17 17:56:30 +00002869 ArrayRef<Value*> Ops(ValueOperands);
2870
2871 switch (getOpcode()) {
2872 case Instruction::Trunc:
2873 case Instruction::ZExt:
2874 case Instruction::SExt:
2875 case Instruction::FPTrunc:
2876 case Instruction::FPExt:
2877 case Instruction::UIToFP:
2878 case Instruction::SIToFP:
2879 case Instruction::FPToUI:
2880 case Instruction::FPToSI:
2881 case Instruction::PtrToInt:
2882 case Instruction::IntToPtr:
2883 case Instruction::BitCast:
Eli Bendersky157a97a2014-01-18 22:54:33 +00002884 case Instruction::AddrSpaceCast:
James Molloyce545682012-11-17 17:56:30 +00002885 return CastInst::Create((Instruction::CastOps)getOpcode(),
2886 Ops[0], getType());
2887 case Instruction::Select:
2888 return SelectInst::Create(Ops[0], Ops[1], Ops[2]);
2889 case Instruction::InsertElement:
2890 return InsertElementInst::Create(Ops[0], Ops[1], Ops[2]);
2891 case Instruction::ExtractElement:
2892 return ExtractElementInst::Create(Ops[0], Ops[1]);
2893 case Instruction::InsertValue:
2894 return InsertValueInst::Create(Ops[0], Ops[1], getIndices());
2895 case Instruction::ExtractValue:
2896 return ExtractValueInst::Create(Ops[0], getIndices());
2897 case Instruction::ShuffleVector:
2898 return new ShuffleVectorInst(Ops[0], Ops[1], Ops[2]);
2899
David Blaikie741c8f82015-03-14 01:53:18 +00002900 case Instruction::GetElementPtr: {
2901 const auto *GO = cast<GEPOperator>(this);
2902 if (GO->isInBounds())
2903 return GetElementPtrInst::CreateInBounds(GO->getSourceElementType(),
2904 Ops[0], Ops.slice(1));
2905 return GetElementPtrInst::Create(GO->getSourceElementType(), Ops[0],
2906 Ops.slice(1));
2907 }
James Molloyce545682012-11-17 17:56:30 +00002908 case Instruction::ICmp:
2909 case Instruction::FCmp:
2910 return CmpInst::Create((Instruction::OtherOps)getOpcode(),
Craig Topper1c3f2832015-12-15 06:11:33 +00002911 (CmpInst::Predicate)getPredicate(), Ops[0], Ops[1]);
James Molloyce545682012-11-17 17:56:30 +00002912
2913 default:
2914 assert(getNumOperands() == 2 && "Must be binary operator?");
2915 BinaryOperator *BO =
2916 BinaryOperator::Create((Instruction::BinaryOps)getOpcode(),
2917 Ops[0], Ops[1]);
2918 if (isa<OverflowingBinaryOperator>(BO)) {
2919 BO->setHasNoUnsignedWrap(SubclassOptionalData &
2920 OverflowingBinaryOperator::NoUnsignedWrap);
2921 BO->setHasNoSignedWrap(SubclassOptionalData &
2922 OverflowingBinaryOperator::NoSignedWrap);
2923 }
2924 if (isa<PossiblyExactOperator>(BO))
2925 BO->setIsExact(SubclassOptionalData & PossiblyExactOperator::IsExact);
2926 return BO;
2927 }
2928}