blob: 86871105d2b40ca0d3d42604d84e22efc372472d [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"
19#include "llvm/ADT/StringExtras.h"
20#include "llvm/ADT/StringMap.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/DerivedTypes.h"
Chandler Carruth03eb0de2014-03-04 10:40:04 +000022#include "llvm/IR/GetElementPtrTypeIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/GlobalValue.h"
24#include "llvm/IR/Instructions.h"
25#include "llvm/IR/Module.h"
26#include "llvm/IR/Operator.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000027#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000028#include "llvm/Support/ErrorHandling.h"
Chris Lattner69edc982006-09-28 00:35:06 +000029#include "llvm/Support/ManagedStatic.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000030#include "llvm/Support/MathExtras.h"
Chris Lattner78683a72009-08-23 04:02:03 +000031#include "llvm/Support/raw_ostream.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000032#include <algorithm>
Serge Gueltone38003f2017-05-09 19:31:13 +000033
Chris Lattner189d19f2003-11-21 20:23:48 +000034using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000035
Chris Lattner2f7c9632001-06-06 20:29:01 +000036//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000037// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000038//===----------------------------------------------------------------------===//
39
Chris Lattnerac5fb562011-07-15 05:58:04 +000040bool Constant::isNegativeZeroValue() const {
41 // Floating point values have an explicit -0.0 value.
42 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
43 return CFP->isZero() && CFP->isNegative();
Galina Kistanovafc259902012-07-13 01:25:27 +000044
David Tweed5493fee2013-03-18 11:54:44 +000045 // Equivalent for a vector of -0.0's.
46 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this))
47 if (ConstantFP *SplatCFP = dyn_cast_or_null<ConstantFP>(CV->getSplatValue()))
48 if (SplatCFP && SplatCFP->isZero() && SplatCFP->isNegative())
49 return true;
50
Owen Anderson8e851302015-11-20 22:34:48 +000051 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
52 if (ConstantFP *SplatCFP = dyn_cast_or_null<ConstantFP>(CV->getSplatValue()))
53 if (SplatCFP && SplatCFP->isZero() && SplatCFP->isNegative())
54 return true;
55
David Tweed298e4192013-03-19 10:16:40 +000056 // We've already handled true FP case; any other FP vectors can't represent -0.0.
57 if (getType()->isFPOrFPVectorTy())
58 return false;
David Tweed5493fee2013-03-18 11:54:44 +000059
Chris Lattnerac5fb562011-07-15 05:58:04 +000060 // Otherwise, just use +0.0.
61 return isNullValue();
62}
63
Shuxin Yang9ca562e2013-01-09 00:53:25 +000064// Return true iff this constant is positive zero (floating point), negative
65// zero (floating point), or a null value.
Shuxin Yangf0537ab2013-01-09 00:13:41 +000066bool Constant::isZeroValue() const {
67 // Floating point values have an explicit -0.0 value.
68 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
69 return CFP->isZero();
70
Owen Anderson630077e2015-11-20 08:16:13 +000071 // Equivalent for a vector of -0.0's.
72 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this))
73 if (ConstantFP *SplatCFP = dyn_cast_or_null<ConstantFP>(CV->getSplatValue()))
74 if (SplatCFP && SplatCFP->isZero())
75 return true;
76
Owen Anderson8e851302015-11-20 22:34:48 +000077 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
78 if (ConstantFP *SplatCFP = dyn_cast_or_null<ConstantFP>(CV->getSplatValue()))
79 if (SplatCFP && SplatCFP->isZero())
80 return true;
81
Shuxin Yangf0537ab2013-01-09 00:13:41 +000082 // Otherwise, just use +0.0.
83 return isNullValue();
84}
85
Chris Lattnerbe6610c2011-07-15 06:14:08 +000086bool Constant::isNullValue() const {
87 // 0 is null.
88 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
89 return CI->isZero();
Galina Kistanovafc259902012-07-13 01:25:27 +000090
Chris Lattnerbe6610c2011-07-15 06:14:08 +000091 // +0.0 is null.
92 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
93 return CFP->isZero() && !CFP->isNegative();
94
David Majnemerf0f224d2015-11-11 21:57:16 +000095 // constant zero is zero for aggregates, cpnull is null for pointers, none for
96 // tokens.
97 return isa<ConstantAggregateZero>(this) || isa<ConstantPointerNull>(this) ||
98 isa<ConstantTokenNone>(this);
Chris Lattnerbe6610c2011-07-15 06:14:08 +000099}
100
Nadav Rotem365af6f2011-08-24 20:18:38 +0000101bool Constant::isAllOnesValue() const {
102 // Check for -1 integers
103 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
104 return CI->isMinusOne();
105
106 // Check for FP which are bitcasted from -1 integers
107 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
108 return CFP->getValueAPF().bitcastToAPInt().isAllOnesValue();
109
Benjamin Kramer42d098e2011-11-14 19:12:20 +0000110 // Check for constant vectors which are splats of -1 values.
Nadav Rotem365af6f2011-08-24 20:18:38 +0000111 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
Benjamin Kramer42d098e2011-11-14 19:12:20 +0000112 if (Constant *Splat = CV->getSplatValue())
113 return Splat->isAllOnesValue();
Nadav Rotem365af6f2011-08-24 20:18:38 +0000114
Chris Lattnerf14a67f2012-01-26 02:31:22 +0000115 // Check for constant vectors which are splats of -1 values.
116 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this))
117 if (Constant *Splat = CV->getSplatValue())
118 return Splat->isAllOnesValue();
119
Nadav Rotem365af6f2011-08-24 20:18:38 +0000120 return false;
121}
Benjamin Kramer42d098e2011-11-14 19:12:20 +0000122
David Majnemer1a0bbc82014-08-16 09:23:42 +0000123bool Constant::isOneValue() const {
124 // Check for 1 integers
125 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
126 return CI->isOne();
127
128 // Check for FP which are bitcasted from 1 integers
129 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
Craig Topper93ac6e12017-06-07 00:58:02 +0000130 return CFP->getValueAPF().bitcastToAPInt().isOneValue();
David Majnemer1a0bbc82014-08-16 09:23:42 +0000131
132 // Check for constant vectors which are splats of 1 values.
133 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
134 if (Constant *Splat = CV->getSplatValue())
135 return Splat->isOneValue();
136
137 // Check for constant vectors which are splats of 1 values.
138 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this))
139 if (Constant *Splat = CV->getSplatValue())
140 return Splat->isOneValue();
141
142 return false;
143}
144
David Majnemerbdeef602014-07-02 06:07:09 +0000145bool Constant::isMinSignedValue() const {
146 // Check for INT_MIN integers
147 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
148 return CI->isMinValue(/*isSigned=*/true);
149
150 // Check for FP which are bitcasted from INT_MIN integers
151 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
152 return CFP->getValueAPF().bitcastToAPInt().isMinSignedValue();
153
154 // Check for constant vectors which are splats of INT_MIN values.
155 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
156 if (Constant *Splat = CV->getSplatValue())
157 return Splat->isMinSignedValue();
158
159 // Check for constant vectors which are splats of INT_MIN values.
160 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this))
161 if (Constant *Splat = CV->getSplatValue())
162 return Splat->isMinSignedValue();
163
164 return false;
165}
166
David Majnemer0e6c9862014-08-22 16:41:23 +0000167bool Constant::isNotMinSignedValue() const {
168 // Check for INT_MIN integers
169 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
170 return !CI->isMinValue(/*isSigned=*/true);
171
172 // Check for FP which are bitcasted from INT_MIN integers
173 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
174 return !CFP->getValueAPF().bitcastToAPInt().isMinSignedValue();
175
176 // Check for constant vectors which are splats of INT_MIN values.
177 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
178 if (Constant *Splat = CV->getSplatValue())
179 return Splat->isNotMinSignedValue();
180
181 // Check for constant vectors which are splats of INT_MIN values.
182 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this))
183 if (Constant *Splat = CV->getSplatValue())
184 return Splat->isNotMinSignedValue();
185
186 // It *may* contain INT_MIN, we can't tell.
187 return false;
188}
189
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +0000190/// Constructor to create a '0' constant of arbitrary type.
Chris Lattner229907c2011-07-18 04:54:35 +0000191Constant *Constant::getNullValue(Type *Ty) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000192 switch (Ty->getTypeID()) {
193 case Type::IntegerTyID:
194 return ConstantInt::get(Ty, 0);
Dan Gohman518cda42011-12-17 00:04:22 +0000195 case Type::HalfTyID:
196 return ConstantFP::get(Ty->getContext(),
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000197 APFloat::getZero(APFloat::IEEEhalf()));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000198 case Type::FloatTyID:
Benjamin Kramer8ceebfa2010-12-04 14:22:24 +0000199 return ConstantFP::get(Ty->getContext(),
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000200 APFloat::getZero(APFloat::IEEEsingle()));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000201 case Type::DoubleTyID:
Benjamin Kramer8ceebfa2010-12-04 14:22:24 +0000202 return ConstantFP::get(Ty->getContext(),
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000203 APFloat::getZero(APFloat::IEEEdouble()));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000204 case Type::X86_FP80TyID:
Benjamin Kramer8ceebfa2010-12-04 14:22:24 +0000205 return ConstantFP::get(Ty->getContext(),
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000206 APFloat::getZero(APFloat::x87DoubleExtended()));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000207 case Type::FP128TyID:
208 return ConstantFP::get(Ty->getContext(),
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000209 APFloat::getZero(APFloat::IEEEquad()));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000210 case Type::PPC_FP128TyID:
Benjamin Kramer8ceebfa2010-12-04 14:22:24 +0000211 return ConstantFP::get(Ty->getContext(),
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000212 APFloat(APFloat::PPCDoubleDouble(),
Tim Northover29178a32013-01-22 09:46:31 +0000213 APInt::getNullValue(128)));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000214 case Type::PointerTyID:
215 return ConstantPointerNull::get(cast<PointerType>(Ty));
216 case Type::StructTyID:
217 case Type::ArrayTyID:
218 case Type::VectorTyID:
219 return ConstantAggregateZero::get(Ty);
David Majnemerf0f224d2015-11-11 21:57:16 +0000220 case Type::TokenTyID:
221 return ConstantTokenNone::get(Ty->getContext());
Owen Anderson5a1acd92009-07-31 20:28:14 +0000222 default:
223 // Function, Label, or Opaque type?
Craig Topperc514b542012-02-05 22:14:15 +0000224 llvm_unreachable("Cannot create a null constant of that type!");
Owen Anderson5a1acd92009-07-31 20:28:14 +0000225 }
226}
227
Chris Lattner229907c2011-07-18 04:54:35 +0000228Constant *Constant::getIntegerValue(Type *Ty, const APInt &V) {
229 Type *ScalarTy = Ty->getScalarType();
Dan Gohmanf011f5a2009-08-03 22:07:33 +0000230
231 // Create the base integer constant.
232 Constant *C = ConstantInt::get(Ty->getContext(), V);
233
234 // Convert an integer to a pointer, if necessary.
Chris Lattner229907c2011-07-18 04:54:35 +0000235 if (PointerType *PTy = dyn_cast<PointerType>(ScalarTy))
Dan Gohmanf011f5a2009-08-03 22:07:33 +0000236 C = ConstantExpr::getIntToPtr(C, PTy);
237
238 // Broadcast a scalar to a vector, if necessary.
Chris Lattner229907c2011-07-18 04:54:35 +0000239 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
Chris Lattnere9eed292012-01-25 05:19:54 +0000240 C = ConstantVector::getSplat(VTy->getNumElements(), C);
Dan Gohmanf011f5a2009-08-03 22:07:33 +0000241
242 return C;
243}
244
Chris Lattner229907c2011-07-18 04:54:35 +0000245Constant *Constant::getAllOnesValue(Type *Ty) {
246 if (IntegerType *ITy = dyn_cast<IntegerType>(Ty))
Owen Anderson5a1acd92009-07-31 20:28:14 +0000247 return ConstantInt::get(Ty->getContext(),
248 APInt::getAllOnesValue(ITy->getBitWidth()));
Nadav Rotem7cc6d122011-02-17 21:22:27 +0000249
250 if (Ty->isFloatingPointTy()) {
251 APFloat FL = APFloat::getAllOnesValue(Ty->getPrimitiveSizeInBits(),
252 !Ty->isPPC_FP128Ty());
253 return ConstantFP::get(Ty->getContext(), FL);
254 }
255
Chris Lattner229907c2011-07-18 04:54:35 +0000256 VectorType *VTy = cast<VectorType>(Ty);
Chris Lattnere9eed292012-01-25 05:19:54 +0000257 return ConstantVector::getSplat(VTy->getNumElements(),
258 getAllOnesValue(VTy->getElementType()));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000259}
260
Chris Lattner7e683d12012-01-25 06:16:32 +0000261Constant *Constant::getAggregateElement(unsigned Elt) const {
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +0000262 if (const ConstantAggregate *CC = dyn_cast<ConstantAggregate>(this))
263 return Elt < CC->getNumOperands() ? CC->getOperand(Elt) : nullptr;
Galina Kistanovafc259902012-07-13 01:25:27 +0000264
David Majnemer9b529a72015-02-16 04:02:09 +0000265 if (const ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(this))
266 return Elt < CAZ->getNumElements() ? CAZ->getElementValue(Elt) : nullptr;
Galina Kistanovafc259902012-07-13 01:25:27 +0000267
Chris Lattner7e683d12012-01-25 06:16:32 +0000268 if (const UndefValue *UV = dyn_cast<UndefValue>(this))
David Majnemer9b529a72015-02-16 04:02:09 +0000269 return Elt < UV->getNumElements() ? UV->getElementValue(Elt) : nullptr;
Galina Kistanovafc259902012-07-13 01:25:27 +0000270
Chris Lattner8326bd82012-01-26 00:42:34 +0000271 if (const ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(this))
Craig Topperc6207612014-04-09 06:08:46 +0000272 return Elt < CDS->getNumElements() ? CDS->getElementAsConstant(Elt)
273 : nullptr;
274 return nullptr;
Chris Lattner7e683d12012-01-25 06:16:32 +0000275}
276
277Constant *Constant::getAggregateElement(Constant *Elt) const {
278 assert(isa<IntegerType>(Elt->getType()) && "Index must be an integer");
279 if (ConstantInt *CI = dyn_cast<ConstantInt>(Elt))
280 return getAggregateElement(CI->getZExtValue());
Craig Topperc6207612014-04-09 06:08:46 +0000281 return nullptr;
Chris Lattner7e683d12012-01-25 06:16:32 +0000282}
283
Pete Cooper86dd4cf2015-06-23 21:55:11 +0000284void Constant::destroyConstant() {
285 /// First call destroyConstantImpl on the subclass. This gives the subclass
286 /// a chance to remove the constant from any maps/pools it's contained in.
287 switch (getValueID()) {
288 default:
289 llvm_unreachable("Not a constant!");
290#define HANDLE_CONSTANT(Name) \
291 case Value::Name##Val: \
292 cast<Name>(this)->destroyConstantImpl(); \
293 break;
294#include "llvm/IR/Value.def"
295 }
Chris Lattner7e683d12012-01-25 06:16:32 +0000296
Chris Lattner3462ae32001-12-03 22:26:30 +0000297 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +0000298 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +0000299 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +0000300 // but they don't know that. Because we only find out when the CPV is
301 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +0000302 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +0000303 //
304 while (!use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000305 Value *V = user_back();
Pete Cooper86dd4cf2015-06-23 21:55:11 +0000306#ifndef NDEBUG // Only in -g mode...
Chris Lattner78683a72009-08-23 04:02:03 +0000307 if (!isa<Constant>(V)) {
David Greene1e27a132010-01-05 01:29:19 +0000308 dbgs() << "While deleting: " << *this
Pete Cooper86dd4cf2015-06-23 21:55:11 +0000309 << "\n\nUse still stuck around after Def is destroyed: " << *V
310 << "\n\n";
Chris Lattner78683a72009-08-23 04:02:03 +0000311 }
Chris Lattnerd7a73302001-10-13 06:57:33 +0000312#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000313 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Chris Lattner8326bd82012-01-26 00:42:34 +0000314 cast<Constant>(V)->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +0000315
316 // The constant should remove itself from our use list...
Chandler Carruthcdf47882014-03-09 03:16:01 +0000317 assert((use_empty() || user_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +0000318 }
319
320 // Value has no outstanding references it is safe to delete it now...
321 delete this;
Chris Lattner38569342001-10-01 20:11:19 +0000322}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000323
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000324static bool canTrapImpl(const Constant *C,
Craig Topper71b7b682014-08-21 05:55:13 +0000325 SmallPtrSetImpl<const ConstantExpr *> &NonTrappingOps) {
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000326 assert(C->getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
Chris Lattner23dd1f62006-10-20 00:27:06 +0000327 // The only thing that could possibly trap are constant exprs.
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000328 const ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
329 if (!CE)
330 return false;
Galina Kistanovafc259902012-07-13 01:25:27 +0000331
332 // ConstantExpr traps if any operands can trap.
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000333 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
334 if (ConstantExpr *Op = dyn_cast<ConstantExpr>(CE->getOperand(i))) {
David Blaikie70573dc2014-11-19 07:49:26 +0000335 if (NonTrappingOps.insert(Op).second && canTrapImpl(Op, NonTrappingOps))
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000336 return true;
337 }
338 }
Chris Lattner23dd1f62006-10-20 00:27:06 +0000339
340 // Otherwise, only specific operations can trap.
341 switch (CE->getOpcode()) {
342 default:
343 return false;
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000344 case Instruction::UDiv:
345 case Instruction::SDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +0000346 case Instruction::URem:
347 case Instruction::SRem:
Chris Lattner23dd1f62006-10-20 00:27:06 +0000348 // Div and rem can trap if the RHS is not known to be non-zero.
Chris Lattnera91a5632009-10-28 05:14:34 +0000349 if (!isa<ConstantInt>(CE->getOperand(1)) ||CE->getOperand(1)->isNullValue())
Chris Lattner23dd1f62006-10-20 00:27:06 +0000350 return true;
351 return false;
352 }
353}
354
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000355bool Constant::canTrap() const {
356 SmallPtrSet<const ConstantExpr *, 4> NonTrappingOps;
357 return canTrapImpl(this, NonTrappingOps);
358}
359
Hans Wennborg4dc89512014-06-20 00:38:12 +0000360/// Check if C contains a GlobalValue for which Predicate is true.
361static bool
362ConstHasGlobalValuePredicate(const Constant *C,
363 bool (*Predicate)(const GlobalValue *)) {
364 SmallPtrSet<const Constant *, 8> Visited;
365 SmallVector<const Constant *, 8> WorkList;
366 WorkList.push_back(C);
367 Visited.insert(C);
Hans Wennborg709e0152012-11-15 11:40:00 +0000368
369 while (!WorkList.empty()) {
Hans Wennborg4dc89512014-06-20 00:38:12 +0000370 const Constant *WorkItem = WorkList.pop_back_val();
371 if (const auto *GV = dyn_cast<GlobalValue>(WorkItem))
372 if (Predicate(GV))
Hans Wennborg709e0152012-11-15 11:40:00 +0000373 return true;
Hans Wennborg4dc89512014-06-20 00:38:12 +0000374 for (const Value *Op : WorkItem->operands()) {
375 const Constant *ConstOp = dyn_cast<Constant>(Op);
376 if (!ConstOp)
Hans Wennborg18aa12402012-11-16 10:33:25 +0000377 continue;
David Blaikie70573dc2014-11-19 07:49:26 +0000378 if (Visited.insert(ConstOp).second)
Hans Wennborg4dc89512014-06-20 00:38:12 +0000379 WorkList.push_back(ConstOp);
Hans Wennborg709e0152012-11-15 11:40:00 +0000380 }
381 }
Hans Wennborg709e0152012-11-15 11:40:00 +0000382 return false;
383}
384
Hans Wennborg4dc89512014-06-20 00:38:12 +0000385bool Constant::isThreadDependent() const {
386 auto DLLImportPredicate = [](const GlobalValue *GV) {
387 return GV->isThreadLocal();
388 };
389 return ConstHasGlobalValuePredicate(this, DLLImportPredicate);
390}
391
392bool Constant::isDLLImportDependent() const {
393 auto DLLImportPredicate = [](const GlobalValue *GV) {
394 return GV->hasDLLImportStorageClass();
395 };
396 return ConstHasGlobalValuePredicate(this, DLLImportPredicate);
397}
398
Chris Lattner253bc772009-11-01 18:11:50 +0000399bool Constant::isConstantUsed() const {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000400 for (const User *U : users()) {
401 const Constant *UC = dyn_cast<Constant>(U);
Craig Topperc6207612014-04-09 06:08:46 +0000402 if (!UC || isa<GlobalValue>(UC))
Chris Lattner253bc772009-11-01 18:11:50 +0000403 return true;
Galina Kistanovafc259902012-07-13 01:25:27 +0000404
Chris Lattner253bc772009-11-01 18:11:50 +0000405 if (UC->isConstantUsed())
406 return true;
407 }
408 return false;
409}
410
Rafael Espindola65e49022015-11-17 00:51:23 +0000411bool Constant::needsRelocation() const {
412 if (isa<GlobalValue>(this))
413 return true; // Global reference.
414
Chris Lattner2cb85b42009-10-28 04:12:16 +0000415 if (const BlockAddress *BA = dyn_cast<BlockAddress>(this))
Rafael Espindola65e49022015-11-17 00:51:23 +0000416 return BA->getFunction()->needsRelocation();
417
Chris Lattnera7cfc432010-01-03 18:09:40 +0000418 // While raw uses of blockaddress need to be relocated, differences between
419 // two of them don't when they are for labels in the same function. This is a
420 // common idiom when creating a table for the indirect goto extension, so we
421 // handle it efficiently here.
422 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(this))
423 if (CE->getOpcode() == Instruction::Sub) {
424 ConstantExpr *LHS = dyn_cast<ConstantExpr>(CE->getOperand(0));
425 ConstantExpr *RHS = dyn_cast<ConstantExpr>(CE->getOperand(1));
Rafael Espindola65e49022015-11-17 00:51:23 +0000426 if (LHS && RHS && LHS->getOpcode() == Instruction::PtrToInt &&
Chris Lattnera7cfc432010-01-03 18:09:40 +0000427 RHS->getOpcode() == Instruction::PtrToInt &&
428 isa<BlockAddress>(LHS->getOperand(0)) &&
429 isa<BlockAddress>(RHS->getOperand(0)) &&
430 cast<BlockAddress>(LHS->getOperand(0))->getFunction() ==
Rafael Espindola65e49022015-11-17 00:51:23 +0000431 cast<BlockAddress>(RHS->getOperand(0))->getFunction())
432 return false;
Chris Lattnera7cfc432010-01-03 18:09:40 +0000433 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000434
Rafael Espindola65e49022015-11-17 00:51:23 +0000435 bool Result = false;
Evan Chengf9e003b2007-03-08 00:59:12 +0000436 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Rafael Espindola65e49022015-11-17 00:51:23 +0000437 Result |= cast<Constant>(getOperand(i))->needsRelocation();
Galina Kistanovafc259902012-07-13 01:25:27 +0000438
Chris Lattner4565ef52009-07-22 00:05:44 +0000439 return Result;
Evan Chengf9e003b2007-03-08 00:59:12 +0000440}
441
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +0000442/// If the specified constantexpr is dead, remove it. This involves recursively
443/// eliminating any dead users of the constantexpr.
Chris Lattner84886402011-02-18 04:41:42 +0000444static bool removeDeadUsersOfConstant(const Constant *C) {
445 if (isa<GlobalValue>(C)) return false; // Cannot remove this
Galina Kistanovafc259902012-07-13 01:25:27 +0000446
Chris Lattner84886402011-02-18 04:41:42 +0000447 while (!C->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000448 const Constant *User = dyn_cast<Constant>(C->user_back());
Chris Lattner84886402011-02-18 04:41:42 +0000449 if (!User) return false; // Non-constant usage;
450 if (!removeDeadUsersOfConstant(User))
451 return false; // Constant wasn't dead
452 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000453
Chris Lattner84886402011-02-18 04:41:42 +0000454 const_cast<Constant*>(C)->destroyConstant();
455 return true;
456}
457
458
Chris Lattner84886402011-02-18 04:41:42 +0000459void Constant::removeDeadConstantUsers() const {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000460 Value::const_user_iterator I = user_begin(), E = user_end();
461 Value::const_user_iterator LastNonDeadUser = E;
Chris Lattner84886402011-02-18 04:41:42 +0000462 while (I != E) {
463 const Constant *User = dyn_cast<Constant>(*I);
Craig Topperc6207612014-04-09 06:08:46 +0000464 if (!User) {
Chris Lattner84886402011-02-18 04:41:42 +0000465 LastNonDeadUser = I;
466 ++I;
467 continue;
468 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000469
Chris Lattner84886402011-02-18 04:41:42 +0000470 if (!removeDeadUsersOfConstant(User)) {
471 // If the constant wasn't dead, remember that this was the last live use
472 // and move on to the next constant.
473 LastNonDeadUser = I;
474 ++I;
475 continue;
476 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000477
Chris Lattner84886402011-02-18 04:41:42 +0000478 // If the constant was dead, then the iterator is invalidated.
479 if (LastNonDeadUser == E) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000480 I = user_begin();
Chris Lattner84886402011-02-18 04:41:42 +0000481 if (I == E) break;
482 } else {
483 I = LastNonDeadUser;
484 ++I;
485 }
486 }
487}
488
489
Chris Lattner2105d662008-07-10 00:28:11 +0000490
Chris Lattner2f7c9632001-06-06 20:29:01 +0000491//===----------------------------------------------------------------------===//
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000492// ConstantInt
Chris Lattner2f7c9632001-06-06 20:29:01 +0000493//===----------------------------------------------------------------------===//
494
Duncan P. N. Exon Smithb6452792016-02-21 02:39:49 +0000495ConstantInt::ConstantInt(IntegerType *Ty, const APInt &V)
496 : ConstantData(Ty, ConstantIntVal), Val(V) {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000497 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000498}
499
Nick Lewycky92db8e82011-03-06 03:36:19 +0000500ConstantInt *ConstantInt::getTrue(LLVMContext &Context) {
Owen Anderson23a204d2009-07-31 17:39:07 +0000501 LLVMContextImpl *pImpl = Context.pImpl;
Benjamin Kramerddd1b7b2010-11-20 18:43:35 +0000502 if (!pImpl->TheTrueVal)
503 pImpl->TheTrueVal = ConstantInt::get(Type::getInt1Ty(Context), 1);
504 return pImpl->TheTrueVal;
Owen Anderson23a204d2009-07-31 17:39:07 +0000505}
506
Nick Lewycky92db8e82011-03-06 03:36:19 +0000507ConstantInt *ConstantInt::getFalse(LLVMContext &Context) {
Owen Anderson23a204d2009-07-31 17:39:07 +0000508 LLVMContextImpl *pImpl = Context.pImpl;
Benjamin Kramerddd1b7b2010-11-20 18:43:35 +0000509 if (!pImpl->TheFalseVal)
510 pImpl->TheFalseVal = ConstantInt::get(Type::getInt1Ty(Context), 0);
511 return pImpl->TheFalseVal;
Owen Anderson23a204d2009-07-31 17:39:07 +0000512}
513
Chris Lattner229907c2011-07-18 04:54:35 +0000514Constant *ConstantInt::getTrue(Type *Ty) {
Craig Topperfde47232017-07-09 07:04:03 +0000515 assert(Ty->isIntOrIntVectorTy(1) && "Type not i1 or vector of i1.");
Sanjay Patel70a575a2017-04-16 17:00:21 +0000516 ConstantInt *TrueC = ConstantInt::getTrue(Ty->getContext());
517 if (auto *VTy = dyn_cast<VectorType>(Ty))
518 return ConstantVector::getSplat(VTy->getNumElements(), TrueC);
519 return TrueC;
Nick Lewycky92db8e82011-03-06 03:36:19 +0000520}
521
Chris Lattner229907c2011-07-18 04:54:35 +0000522Constant *ConstantInt::getFalse(Type *Ty) {
Craig Topperfde47232017-07-09 07:04:03 +0000523 assert(Ty->isIntOrIntVectorTy(1) && "Type not i1 or vector of i1.");
Sanjay Patel70a575a2017-04-16 17:00:21 +0000524 ConstantInt *FalseC = ConstantInt::getFalse(Ty->getContext());
525 if (auto *VTy = dyn_cast<VectorType>(Ty))
526 return ConstantVector::getSplat(VTy->getNumElements(), FalseC);
527 return FalseC;
Nick Lewycky92db8e82011-03-06 03:36:19 +0000528}
529
Benjamin Kramer8e5dc532014-12-06 13:12:56 +0000530// Get a ConstantInt from an APInt.
Nick Lewycky92db8e82011-03-06 03:36:19 +0000531ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt &V) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000532 // get an existing value or the insertion position
Benjamin Kramer320682f2013-06-01 17:51:03 +0000533 LLVMContextImpl *pImpl = Context.pImpl;
Justin Lebar611c5c22016-10-10 16:26:13 +0000534 std::unique_ptr<ConstantInt> &Slot = pImpl->IntConstants[V];
Benjamin Kramer8e5dc532014-12-06 13:12:56 +0000535 if (!Slot) {
536 // Get the corresponding integer type for the bit width of the value.
537 IntegerType *ITy = IntegerType::get(Context, V.getBitWidth());
Justin Lebar611c5c22016-10-10 16:26:13 +0000538 Slot.reset(new ConstantInt(ITy, V));
Benjamin Kramer8e5dc532014-12-06 13:12:56 +0000539 }
540 assert(Slot->getType() == IntegerType::get(Context, V.getBitWidth()));
Justin Lebar611c5c22016-10-10 16:26:13 +0000541 return Slot.get();
Owen Andersonedb4a702009-07-24 23:12:02 +0000542}
543
Chris Lattner229907c2011-07-18 04:54:35 +0000544Constant *ConstantInt::get(Type *Ty, uint64_t V, bool isSigned) {
Nick Lewycky92db8e82011-03-06 03:36:19 +0000545 Constant *C = get(cast<IntegerType>(Ty->getScalarType()), V, isSigned);
Owen Andersonedb4a702009-07-24 23:12:02 +0000546
547 // For vectors, broadcast the value.
Chris Lattner229907c2011-07-18 04:54:35 +0000548 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
Chris Lattnere9eed292012-01-25 05:19:54 +0000549 return ConstantVector::getSplat(VTy->getNumElements(), C);
Owen Andersonedb4a702009-07-24 23:12:02 +0000550
551 return C;
552}
553
Sanjay Patelf3587ec2016-05-18 22:05:28 +0000554ConstantInt *ConstantInt::get(IntegerType *Ty, uint64_t V, bool isSigned) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000555 return get(Ty->getContext(), APInt(Ty->getBitWidth(), V, isSigned));
556}
557
Chris Lattner0256be92012-01-27 03:08:05 +0000558ConstantInt *ConstantInt::getSigned(IntegerType *Ty, int64_t V) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000559 return get(Ty, V, true);
560}
561
Chris Lattner229907c2011-07-18 04:54:35 +0000562Constant *ConstantInt::getSigned(Type *Ty, int64_t V) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000563 return get(Ty, V, true);
564}
565
Chris Lattner0256be92012-01-27 03:08:05 +0000566Constant *ConstantInt::get(Type *Ty, const APInt& V) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000567 ConstantInt *C = get(Ty->getContext(), V);
568 assert(C->getType() == Ty->getScalarType() &&
569 "ConstantInt type doesn't match the type implied by its value!");
570
571 // For vectors, broadcast the value.
Chris Lattner229907c2011-07-18 04:54:35 +0000572 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
Chris Lattnere9eed292012-01-25 05:19:54 +0000573 return ConstantVector::getSplat(VTy->getNumElements(), C);
Owen Andersonedb4a702009-07-24 23:12:02 +0000574
575 return C;
576}
577
Sanjay Patelf3587ec2016-05-18 22:05:28 +0000578ConstantInt *ConstantInt::get(IntegerType* Ty, StringRef Str, uint8_t radix) {
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000579 return get(Ty->getContext(), APInt(Ty->getBitWidth(), Str, radix));
580}
581
Pete Cooper86dd4cf2015-06-23 21:55:11 +0000582/// Remove the constant from the constant table.
583void ConstantInt::destroyConstantImpl() {
584 llvm_unreachable("You can't ConstantInt->destroyConstantImpl()!");
585}
586
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000587//===----------------------------------------------------------------------===//
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000588// ConstantFP
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000589//===----------------------------------------------------------------------===//
590
Chris Lattner229907c2011-07-18 04:54:35 +0000591static const fltSemantics *TypeToFloatSemantics(Type *Ty) {
Dan Gohman518cda42011-12-17 00:04:22 +0000592 if (Ty->isHalfTy())
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000593 return &APFloat::IEEEhalf();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000594 if (Ty->isFloatTy())
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000595 return &APFloat::IEEEsingle();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000596 if (Ty->isDoubleTy())
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000597 return &APFloat::IEEEdouble();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000598 if (Ty->isX86_FP80Ty())
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000599 return &APFloat::x87DoubleExtended();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000600 else if (Ty->isFP128Ty())
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000601 return &APFloat::IEEEquad();
Galina Kistanovafc259902012-07-13 01:25:27 +0000602
Chris Lattnerfdd87902009-10-05 05:54:46 +0000603 assert(Ty->isPPC_FP128Ty() && "Unknown FP format");
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000604 return &APFloat::PPCDoubleDouble();
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000605}
606
Chris Lattner0256be92012-01-27 03:08:05 +0000607Constant *ConstantFP::get(Type *Ty, double V) {
Owen Anderson69c464d2009-07-27 20:59:43 +0000608 LLVMContext &Context = Ty->getContext();
Galina Kistanovafc259902012-07-13 01:25:27 +0000609
Owen Anderson69c464d2009-07-27 20:59:43 +0000610 APFloat FV(V);
611 bool ignored;
612 FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
613 APFloat::rmNearestTiesToEven, &ignored);
614 Constant *C = get(Context, FV);
615
616 // For vectors, broadcast the value.
Chris Lattner229907c2011-07-18 04:54:35 +0000617 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
Chris Lattnere9eed292012-01-25 05:19:54 +0000618 return ConstantVector::getSplat(VTy->getNumElements(), C);
Owen Anderson69c464d2009-07-27 20:59:43 +0000619
620 return C;
621}
622
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000623
Chris Lattner0256be92012-01-27 03:08:05 +0000624Constant *ConstantFP::get(Type *Ty, StringRef Str) {
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000625 LLVMContext &Context = Ty->getContext();
626
627 APFloat FV(*TypeToFloatSemantics(Ty->getScalarType()), Str);
628 Constant *C = get(Context, FV);
629
630 // For vectors, broadcast the value.
Chris Lattner229907c2011-07-18 04:54:35 +0000631 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
Chris Lattnere9eed292012-01-25 05:19:54 +0000632 return ConstantVector::getSplat(VTy->getNumElements(), C);
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000633
634 return C;
635}
636
Tom Stellard67246d12015-04-20 19:38:24 +0000637Constant *ConstantFP::getNaN(Type *Ty, bool Negative, unsigned Type) {
638 const fltSemantics &Semantics = *TypeToFloatSemantics(Ty->getScalarType());
639 APFloat NaN = APFloat::getNaN(Semantics, Negative, Type);
640 Constant *C = get(Ty->getContext(), NaN);
641
642 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
643 return ConstantVector::getSplat(VTy->getNumElements(), C);
644
645 return C;
646}
647
Benjamin Kramer5d2ff222014-01-18 16:43:06 +0000648Constant *ConstantFP::getNegativeZero(Type *Ty) {
649 const fltSemantics &Semantics = *TypeToFloatSemantics(Ty->getScalarType());
650 APFloat NegZero = APFloat::getZero(Semantics, /*Negative=*/true);
651 Constant *C = get(Ty->getContext(), NegZero);
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000652
Benjamin Kramer5d2ff222014-01-18 16:43:06 +0000653 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
654 return ConstantVector::getSplat(VTy->getNumElements(), C);
655
656 return C;
Owen Anderson69c464d2009-07-27 20:59:43 +0000657}
658
659
Chris Lattnere9eed292012-01-25 05:19:54 +0000660Constant *ConstantFP::getZeroValueForNegation(Type *Ty) {
Benjamin Kramer5d2ff222014-01-18 16:43:06 +0000661 if (Ty->isFPOrFPVectorTy())
662 return getNegativeZero(Ty);
Owen Anderson69c464d2009-07-27 20:59:43 +0000663
Owen Anderson5a1acd92009-07-31 20:28:14 +0000664 return Constant::getNullValue(Ty);
Owen Anderson69c464d2009-07-27 20:59:43 +0000665}
666
667
668// ConstantFP accessors.
669ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) {
Owen Anderson69c464d2009-07-27 20:59:43 +0000670 LLVMContextImpl* pImpl = Context.pImpl;
Galina Kistanovafc259902012-07-13 01:25:27 +0000671
Justin Lebar611c5c22016-10-10 16:26:13 +0000672 std::unique_ptr<ConstantFP> &Slot = pImpl->FPConstants[V];
Galina Kistanovafc259902012-07-13 01:25:27 +0000673
Owen Anderson69c464d2009-07-27 20:59:43 +0000674 if (!Slot) {
Chris Lattner229907c2011-07-18 04:54:35 +0000675 Type *Ty;
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000676 if (&V.getSemantics() == &APFloat::IEEEhalf())
Dan Gohman518cda42011-12-17 00:04:22 +0000677 Ty = Type::getHalfTy(Context);
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000678 else if (&V.getSemantics() == &APFloat::IEEEsingle())
Owen Anderson5dab84c2009-10-19 20:11:52 +0000679 Ty = Type::getFloatTy(Context);
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000680 else if (&V.getSemantics() == &APFloat::IEEEdouble())
Owen Anderson5dab84c2009-10-19 20:11:52 +0000681 Ty = Type::getDoubleTy(Context);
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000682 else if (&V.getSemantics() == &APFloat::x87DoubleExtended())
Owen Anderson5dab84c2009-10-19 20:11:52 +0000683 Ty = Type::getX86_FP80Ty(Context);
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000684 else if (&V.getSemantics() == &APFloat::IEEEquad())
Owen Anderson5dab84c2009-10-19 20:11:52 +0000685 Ty = Type::getFP128Ty(Context);
686 else {
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000687 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble() &&
Owen Anderson5dab84c2009-10-19 20:11:52 +0000688 "Unknown FP format");
689 Ty = Type::getPPC_FP128Ty(Context);
Owen Anderson69c464d2009-07-27 20:59:43 +0000690 }
Justin Lebar611c5c22016-10-10 16:26:13 +0000691 Slot.reset(new ConstantFP(Ty, V));
Owen Anderson69c464d2009-07-27 20:59:43 +0000692 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000693
Justin Lebar611c5c22016-10-10 16:26:13 +0000694 return Slot.get();
Owen Anderson69c464d2009-07-27 20:59:43 +0000695}
696
Benjamin Kramer5d2ff222014-01-18 16:43:06 +0000697Constant *ConstantFP::getInfinity(Type *Ty, bool Negative) {
698 const fltSemantics &Semantics = *TypeToFloatSemantics(Ty->getScalarType());
699 Constant *C = get(Ty->getContext(), APFloat::getInf(Semantics, Negative));
700
701 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
702 return ConstantVector::getSplat(VTy->getNumElements(), C);
703
704 return C;
Dan Gohmanfeb50212009-09-25 23:00:48 +0000705}
706
Duncan P. N. Exon Smithb6452792016-02-21 02:39:49 +0000707ConstantFP::ConstantFP(Type *Ty, const APFloat &V)
708 : ConstantData(Ty, ConstantFPVal), Val(V) {
Chris Lattner98bd9392008-04-09 06:38:30 +0000709 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
710 "FP type Mismatch");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000711}
712
Chris Lattnerbe6610c2011-07-15 06:14:08 +0000713bool ConstantFP::isExactlyValue(const APFloat &V) const {
Dale Johannesend246b2c2007-08-30 00:23:21 +0000714 return Val.bitwiseIsEqual(V);
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000715}
716
Pete Cooper86dd4cf2015-06-23 21:55:11 +0000717/// Remove the constant from the constant table.
718void ConstantFP::destroyConstantImpl() {
Craig Topperccbb8102017-06-27 19:57:51 +0000719 llvm_unreachable("You can't ConstantFP->destroyConstantImpl()!");
Pete Cooper86dd4cf2015-06-23 21:55:11 +0000720}
721
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000722//===----------------------------------------------------------------------===//
Chris Lattner030af792012-01-24 05:42:11 +0000723// ConstantAggregateZero Implementation
724//===----------------------------------------------------------------------===//
725
Chris Lattner7e683d12012-01-25 06:16:32 +0000726Constant *ConstantAggregateZero::getSequentialElement() const {
Chris Lattner8326bd82012-01-26 00:42:34 +0000727 return Constant::getNullValue(getType()->getSequentialElementType());
Chris Lattner030af792012-01-24 05:42:11 +0000728}
729
Chris Lattner7e683d12012-01-25 06:16:32 +0000730Constant *ConstantAggregateZero::getStructElement(unsigned Elt) const {
Chris Lattner8326bd82012-01-26 00:42:34 +0000731 return Constant::getNullValue(getType()->getStructElementType(Elt));
Chris Lattner030af792012-01-24 05:42:11 +0000732}
733
Chris Lattner7e683d12012-01-25 06:16:32 +0000734Constant *ConstantAggregateZero::getElementValue(Constant *C) const {
Chris Lattner030af792012-01-24 05:42:11 +0000735 if (isa<SequentialType>(getType()))
736 return getSequentialElement();
737 return getStructElement(cast<ConstantInt>(C)->getZExtValue());
738}
739
Chris Lattner7e683d12012-01-25 06:16:32 +0000740Constant *ConstantAggregateZero::getElementValue(unsigned Idx) const {
Chris Lattnerf7eb5432012-01-24 07:54:10 +0000741 if (isa<SequentialType>(getType()))
742 return getSequentialElement();
743 return getStructElement(Idx);
744}
745
David Majnemer9b529a72015-02-16 04:02:09 +0000746unsigned ConstantAggregateZero::getNumElements() const {
Craig Toppere3dcce92015-08-01 22:20:21 +0000747 Type *Ty = getType();
748 if (auto *AT = dyn_cast<ArrayType>(Ty))
David Majnemer9b529a72015-02-16 04:02:09 +0000749 return AT->getNumElements();
Craig Toppere3dcce92015-08-01 22:20:21 +0000750 if (auto *VT = dyn_cast<VectorType>(Ty))
David Majnemer9b529a72015-02-16 04:02:09 +0000751 return VT->getNumElements();
752 return Ty->getStructNumElements();
753}
Chris Lattnerf7eb5432012-01-24 07:54:10 +0000754
Chris Lattner030af792012-01-24 05:42:11 +0000755//===----------------------------------------------------------------------===//
756// UndefValue Implementation
757//===----------------------------------------------------------------------===//
758
Chris Lattner7e683d12012-01-25 06:16:32 +0000759UndefValue *UndefValue::getSequentialElement() const {
Chris Lattner8326bd82012-01-26 00:42:34 +0000760 return UndefValue::get(getType()->getSequentialElementType());
Chris Lattner030af792012-01-24 05:42:11 +0000761}
762
Chris Lattner7e683d12012-01-25 06:16:32 +0000763UndefValue *UndefValue::getStructElement(unsigned Elt) const {
Chris Lattner8326bd82012-01-26 00:42:34 +0000764 return UndefValue::get(getType()->getStructElementType(Elt));
Chris Lattner030af792012-01-24 05:42:11 +0000765}
766
Chris Lattner7e683d12012-01-25 06:16:32 +0000767UndefValue *UndefValue::getElementValue(Constant *C) const {
Chris Lattner030af792012-01-24 05:42:11 +0000768 if (isa<SequentialType>(getType()))
769 return getSequentialElement();
770 return getStructElement(cast<ConstantInt>(C)->getZExtValue());
771}
772
Chris Lattner7e683d12012-01-25 06:16:32 +0000773UndefValue *UndefValue::getElementValue(unsigned Idx) const {
Chris Lattnerf7eb5432012-01-24 07:54:10 +0000774 if (isa<SequentialType>(getType()))
775 return getSequentialElement();
776 return getStructElement(Idx);
777}
778
David Majnemer9b529a72015-02-16 04:02:09 +0000779unsigned UndefValue::getNumElements() const {
Craig Toppere3dcce92015-08-01 22:20:21 +0000780 Type *Ty = getType();
Peter Collingbournebc070522016-12-02 03:20:58 +0000781 if (auto *ST = dyn_cast<SequentialType>(Ty))
782 return ST->getNumElements();
David Majnemer9b529a72015-02-16 04:02:09 +0000783 return Ty->getStructNumElements();
784}
Chris Lattner030af792012-01-24 05:42:11 +0000785
786//===----------------------------------------------------------------------===//
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000787// ConstantXXX Classes
788//===----------------------------------------------------------------------===//
789
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000790template <typename ItTy, typename EltTy>
791static bool rangeOnlyContains(ItTy Start, ItTy End, EltTy Elt) {
792 for (; Start != End; ++Start)
793 if (*Start != Elt)
794 return false;
795 return true;
796}
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000797
Justin Bogner909e1c02015-12-01 20:20:49 +0000798template <typename SequentialTy, typename ElementTy>
799static Constant *getIntSequenceIfElementsMatch(ArrayRef<Constant *> V) {
800 assert(!V.empty() && "Cannot get empty int sequence.");
801
802 SmallVector<ElementTy, 16> Elts;
803 for (Constant *C : V)
804 if (auto *CI = dyn_cast<ConstantInt>(C))
805 Elts.push_back(CI->getZExtValue());
806 else
807 return nullptr;
808 return SequentialTy::get(V[0]->getContext(), Elts);
809}
810
811template <typename SequentialTy, typename ElementTy>
812static Constant *getFPSequenceIfElementsMatch(ArrayRef<Constant *> V) {
813 assert(!V.empty() && "Cannot get empty FP sequence.");
814
815 SmallVector<ElementTy, 16> Elts;
816 for (Constant *C : V)
817 if (auto *CFP = dyn_cast<ConstantFP>(C))
818 Elts.push_back(CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
819 else
820 return nullptr;
821 return SequentialTy::getFP(V[0]->getContext(), Elts);
822}
823
824template <typename SequenceTy>
825static Constant *getSequenceIfElementsMatch(Constant *C,
826 ArrayRef<Constant *> V) {
827 // We speculatively build the elements here even if it turns out that there is
828 // a constantexpr or something else weird, since it is so uncommon for that to
829 // happen.
830 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
831 if (CI->getType()->isIntegerTy(8))
832 return getIntSequenceIfElementsMatch<SequenceTy, uint8_t>(V);
833 else if (CI->getType()->isIntegerTy(16))
834 return getIntSequenceIfElementsMatch<SequenceTy, uint16_t>(V);
835 else if (CI->getType()->isIntegerTy(32))
836 return getIntSequenceIfElementsMatch<SequenceTy, uint32_t>(V);
837 else if (CI->getType()->isIntegerTy(64))
838 return getIntSequenceIfElementsMatch<SequenceTy, uint64_t>(V);
839 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
Justin Bogner0ebc8602015-12-08 03:01:16 +0000840 if (CFP->getType()->isHalfTy())
841 return getFPSequenceIfElementsMatch<SequenceTy, uint16_t>(V);
842 else if (CFP->getType()->isFloatTy())
Justin Bogner909e1c02015-12-01 20:20:49 +0000843 return getFPSequenceIfElementsMatch<SequenceTy, uint32_t>(V);
844 else if (CFP->getType()->isDoubleTy())
845 return getFPSequenceIfElementsMatch<SequenceTy, uint64_t>(V);
846 }
847
848 return nullptr;
849}
850
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +0000851ConstantAggregate::ConstantAggregate(CompositeType *T, ValueTy VT,
852 ArrayRef<Constant *> V)
853 : Constant(T, VT, OperandTraits<ConstantAggregate>::op_end(this) - V.size(),
854 V.size()) {
Jay Foad89d9b812011-07-25 10:14:44 +0000855 std::copy(V.begin(), V.end(), op_begin());
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +0000856
857 // Check that types match, unless this is an opaque struct.
858 if (auto *ST = dyn_cast<StructType>(T))
859 if (ST->isOpaque())
860 return;
861 for (unsigned I = 0, E = V.size(); I != E; ++I)
862 assert(V[I]->getType() == T->getTypeAtIndex(I) &&
863 "Initializer for composite element doesn't match!");
864}
865
866ConstantArray::ConstantArray(ArrayType *T, ArrayRef<Constant *> V)
867 : ConstantAggregate(T, ConstantArrayVal, V) {
868 assert(V.size() == T->getNumElements() &&
869 "Invalid initializer for constant array");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000870}
871
Chris Lattner229907c2011-07-18 04:54:35 +0000872Constant *ConstantArray::get(ArrayType *Ty, ArrayRef<Constant*> V) {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000873 if (Constant *C = getImpl(Ty, V))
874 return C;
875 return Ty->getContext().pImpl->ArrayConstants.getOrCreate(Ty, V);
876}
Justin Bogner909e1c02015-12-01 20:20:49 +0000877
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000878Constant *ConstantArray::getImpl(ArrayType *Ty, ArrayRef<Constant*> V) {
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000879 // Empty arrays are canonicalized to ConstantAggregateZero.
880 if (V.empty())
881 return ConstantAggregateZero::get(Ty);
882
Jeffrey Yasskin8ce67f82009-09-30 21:08:08 +0000883 for (unsigned i = 0, e = V.size(); i != e; ++i) {
884 assert(V[i]->getType() == Ty->getElementType() &&
885 "Wrong type in array element initializer");
886 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000887
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000888 // If this is an all-zero array, return a ConstantAggregateZero object. If
889 // all undef, return an UndefValue, if "all simple", then return a
890 // ConstantDataArray.
891 Constant *C = V[0];
892 if (isa<UndefValue>(C) && rangeOnlyContains(V.begin(), V.end(), C))
893 return UndefValue::get(Ty);
Chris Lattnerf14a67f2012-01-26 02:31:22 +0000894
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000895 if (C->isNullValue() && rangeOnlyContains(V.begin(), V.end(), C))
896 return ConstantAggregateZero::get(Ty);
897
898 // Check to see if all of the elements are ConstantFP or ConstantInt and if
899 // the element type is compatible with ConstantDataVector. If so, use it.
Justin Bogner909e1c02015-12-01 20:20:49 +0000900 if (ConstantDataSequential::isElementTypeCompatible(C->getType()))
901 return getSequenceIfElementsMatch<ConstantDataArray>(C, V);
Chris Lattnerf14a67f2012-01-26 02:31:22 +0000902
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000903 // Otherwise, we really do want to create a ConstantArray.
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000904 return nullptr;
Owen Andersonc2c79322009-07-28 18:32:17 +0000905}
906
Chris Lattnercc19efa2011-06-20 04:01:31 +0000907StructType *ConstantStruct::getTypeForElements(LLVMContext &Context,
908 ArrayRef<Constant*> V,
909 bool Packed) {
Bill Wendling3ae7dd32012-02-07 01:27:51 +0000910 unsigned VecSize = V.size();
911 SmallVector<Type*, 16> EltTypes(VecSize);
912 for (unsigned i = 0; i != VecSize; ++i)
913 EltTypes[i] = V[i]->getType();
Galina Kistanovafc259902012-07-13 01:25:27 +0000914
Chris Lattnercc19efa2011-06-20 04:01:31 +0000915 return StructType::get(Context, EltTypes, Packed);
916}
917
918
919StructType *ConstantStruct::getTypeForElements(ArrayRef<Constant*> V,
920 bool Packed) {
921 assert(!V.empty() &&
922 "ConstantStruct::getTypeForElements cannot be called on empty list");
923 return getTypeForElements(V[0]->getContext(), V, Packed);
924}
925
Jay Foad89d9b812011-07-25 10:14:44 +0000926ConstantStruct::ConstantStruct(StructType *T, ArrayRef<Constant *> V)
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +0000927 : ConstantAggregate(T, ConstantStructVal, V) {
928 assert((T->isOpaque() || V.size() == T->getNumElements()) &&
929 "Invalid initializer for constant struct");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000930}
931
Owen Anderson45308b52009-07-27 22:29:26 +0000932// ConstantStruct accessors.
Chris Lattner229907c2011-07-18 04:54:35 +0000933Constant *ConstantStruct::get(StructType *ST, ArrayRef<Constant*> V) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000934 assert((ST->isOpaque() || ST->getNumElements() == V.size()) &&
935 "Incorrect # elements specified to ConstantStruct::get");
Chris Lattnerf14a67f2012-01-26 02:31:22 +0000936
937 // Create a ConstantAggregateZero value if all elements are zeros.
938 bool isZero = true;
939 bool isUndef = false;
940
941 if (!V.empty()) {
942 isUndef = isa<UndefValue>(V[0]);
943 isZero = V[0]->isNullValue();
944 if (isUndef || isZero) {
945 for (unsigned i = 0, e = V.size(); i != e; ++i) {
946 if (!V[i]->isNullValue())
947 isZero = false;
948 if (!isa<UndefValue>(V[i]))
949 isUndef = false;
950 }
951 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000952 }
Chris Lattnerf14a67f2012-01-26 02:31:22 +0000953 if (isZero)
954 return ConstantAggregateZero::get(ST);
955 if (isUndef)
956 return UndefValue::get(ST);
Galina Kistanovafc259902012-07-13 01:25:27 +0000957
Chris Lattnerf14a67f2012-01-26 02:31:22 +0000958 return ST->getContext().pImpl->StructConstants.getOrCreate(ST, V);
Owen Anderson45308b52009-07-27 22:29:26 +0000959}
960
Jay Foad89d9b812011-07-25 10:14:44 +0000961ConstantVector::ConstantVector(VectorType *T, ArrayRef<Constant *> V)
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +0000962 : ConstantAggregate(T, ConstantVectorVal, V) {
Duncan P. N. Exon Smithdb63bda2016-04-05 20:53:47 +0000963 assert(V.size() == T->getNumElements() &&
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +0000964 "Invalid initializer for constant vector");
Brian Gaeke02209042004-08-20 06:00:58 +0000965}
966
Owen Anderson4aa32952009-07-28 21:19:26 +0000967// ConstantVector accessors.
Jay Foadb8a8bed32011-06-22 09:10:19 +0000968Constant *ConstantVector::get(ArrayRef<Constant*> V) {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000969 if (Constant *C = getImpl(V))
970 return C;
971 VectorType *Ty = VectorType::get(V.front()->getType(), V.size());
972 return Ty->getContext().pImpl->VectorConstants.getOrCreate(Ty, V);
973}
Justin Bogner909e1c02015-12-01 20:20:49 +0000974
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000975Constant *ConstantVector::getImpl(ArrayRef<Constant*> V) {
Jay Foad9f32cfd2011-01-27 14:44:55 +0000976 assert(!V.empty() && "Vectors can't be empty");
Chris Lattner229907c2011-07-18 04:54:35 +0000977 VectorType *T = VectorType::get(V.front()->getType(), V.size());
Jay Foad9f32cfd2011-01-27 14:44:55 +0000978
Chris Lattner69229312011-02-15 00:14:00 +0000979 // If this is an all-undef or all-zero vector, return a
Owen Anderson4aa32952009-07-28 21:19:26 +0000980 // ConstantAggregateZero or UndefValue.
981 Constant *C = V[0];
982 bool isZero = C->isNullValue();
983 bool isUndef = isa<UndefValue>(C);
984
985 if (isZero || isUndef) {
986 for (unsigned i = 1, e = V.size(); i != e; ++i)
987 if (V[i] != C) {
988 isZero = isUndef = false;
989 break;
990 }
991 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000992
Owen Anderson4aa32952009-07-28 21:19:26 +0000993 if (isZero)
Owen Andersonb292b8c2009-07-30 23:03:37 +0000994 return ConstantAggregateZero::get(T);
Owen Anderson4aa32952009-07-28 21:19:26 +0000995 if (isUndef)
Owen Andersonb292b8c2009-07-30 23:03:37 +0000996 return UndefValue::get(T);
Galina Kistanovafc259902012-07-13 01:25:27 +0000997
Chris Lattner978fe0c2012-01-30 06:21:21 +0000998 // Check to see if all of the elements are ConstantFP or ConstantInt and if
999 // the element type is compatible with ConstantDataVector. If so, use it.
Justin Bogner909e1c02015-12-01 20:20:49 +00001000 if (ConstantDataSequential::isElementTypeCompatible(C->getType()))
1001 return getSequenceIfElementsMatch<ConstantDataVector>(C, V);
Galina Kistanovafc259902012-07-13 01:25:27 +00001002
Chris Lattner978fe0c2012-01-30 06:21:21 +00001003 // Otherwise, the element type isn't compatible with ConstantDataVector, or
Craig Topperdf907262017-04-11 06:41:55 +00001004 // the operand list contains a ConstantExpr or something else strange.
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001005 return nullptr;
Owen Anderson4aa32952009-07-28 21:19:26 +00001006}
1007
Chris Lattnere9eed292012-01-25 05:19:54 +00001008Constant *ConstantVector::getSplat(unsigned NumElts, Constant *V) {
Chris Lattner978fe0c2012-01-30 06:21:21 +00001009 // If this splat is compatible with ConstantDataVector, use it instead of
1010 // ConstantVector.
1011 if ((isa<ConstantFP>(V) || isa<ConstantInt>(V)) &&
1012 ConstantDataSequential::isElementTypeCompatible(V->getType()))
1013 return ConstantDataVector::getSplat(NumElts, V);
Galina Kistanovafc259902012-07-13 01:25:27 +00001014
Chris Lattnere9eed292012-01-25 05:19:54 +00001015 SmallVector<Constant*, 32> Elts(NumElts, V);
1016 return get(Elts);
1017}
1018
David Majnemerf0f224d2015-11-11 21:57:16 +00001019ConstantTokenNone *ConstantTokenNone::get(LLVMContext &Context) {
1020 LLVMContextImpl *pImpl = Context.pImpl;
1021 if (!pImpl->TheNoneToken)
David Majnemer2dd41c52015-11-16 20:55:57 +00001022 pImpl->TheNoneToken.reset(new ConstantTokenNone(Context));
1023 return pImpl->TheNoneToken.get();
David Majnemerf0f224d2015-11-11 21:57:16 +00001024}
1025
1026/// Remove the constant from the constant table.
1027void ConstantTokenNone::destroyConstantImpl() {
1028 llvm_unreachable("You can't ConstantTokenNone->destroyConstantImpl()!");
1029}
Chris Lattnere9eed292012-01-25 05:19:54 +00001030
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001031// Utility function for determining if a ConstantExpr is a CastOp or not. This
1032// can't be inline because we don't want to #include Instruction.h into
1033// Constant.h
1034bool ConstantExpr::isCast() const {
1035 return Instruction::isCast(getOpcode());
1036}
1037
Reid Spenceree3c9912006-12-04 05:19:50 +00001038bool ConstantExpr::isCompare() const {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001039 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
Reid Spenceree3c9912006-12-04 05:19:50 +00001040}
1041
Dan Gohman7190d482009-09-10 23:37:55 +00001042bool ConstantExpr::isGEPWithNoNotionalOverIndexing() const {
1043 if (getOpcode() != Instruction::GetElementPtr) return false;
1044
1045 gep_type_iterator GEPI = gep_type_begin(this), E = gep_type_end(this);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001046 User::const_op_iterator OI = std::next(this->op_begin());
Dan Gohman7190d482009-09-10 23:37:55 +00001047
Sanjay Patel81ed3492016-12-11 20:07:02 +00001048 // The remaining indices may be compile-time known integers within the bounds
1049 // of the corresponding notional static array types.
Dan Gohman7190d482009-09-10 23:37:55 +00001050 for (; GEPI != E; ++GEPI, ++OI) {
Sanjay Patel81ed3492016-12-11 20:07:02 +00001051 if (isa<UndefValue>(*OI))
1052 continue;
1053 auto *CI = dyn_cast<ConstantInt>(*OI);
1054 if (!CI || (GEPI.isBoundedSequential() &&
1055 (CI->getValue().getActiveBits() > 64 ||
1056 CI->getZExtValue() >= GEPI.getSequentialNumElements())))
Peter Collingbourneab85225b2016-12-02 02:24:42 +00001057 return false;
Dan Gohman7190d482009-09-10 23:37:55 +00001058 }
1059
1060 // All the indices checked out.
1061 return true;
1062}
1063
Dan Gohman1ecaf452008-05-31 00:58:22 +00001064bool ConstantExpr::hasIndices() const {
1065 return getOpcode() == Instruction::ExtractValue ||
1066 getOpcode() == Instruction::InsertValue;
1067}
1068
Jay Foad0091fe82011-04-13 15:22:40 +00001069ArrayRef<unsigned> ConstantExpr::getIndices() const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001070 if (const ExtractValueConstantExpr *EVCE =
1071 dyn_cast<ExtractValueConstantExpr>(this))
1072 return EVCE->Indices;
Dan Gohmana469bdb2008-06-23 16:39:44 +00001073
1074 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001075}
1076
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001077unsigned ConstantExpr::getPredicate() const {
Craig Toppercc03b492015-12-15 06:11:36 +00001078 return cast<CompareConstantExpr>(this)->predicate;
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001079}
Chris Lattner60e0dd72001-10-03 06:12:09 +00001080
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001081Constant *
1082ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +00001083 assert(Op->getType() == getOperand(OpNo)->getType() &&
1084 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +00001085 if (getOperand(OpNo) == Op)
1086 return const_cast<ConstantExpr*>(this);
Chris Lattner37e38352012-01-26 20:37:11 +00001087
1088 SmallVector<Constant*, 8> NewOps;
1089 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1090 NewOps.push_back(i == OpNo ? Op : getOperand(i));
Galina Kistanovafc259902012-07-13 01:25:27 +00001091
Chris Lattner37e38352012-01-26 20:37:11 +00001092 return getWithOperands(NewOps);
Chris Lattner227816342006-07-14 22:20:01 +00001093}
1094
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001095Constant *ConstantExpr::getWithOperands(ArrayRef<Constant *> Ops, Type *Ty,
David Blaikie88208842015-08-21 20:16:51 +00001096 bool OnlyIfReduced, Type *SrcTy) const {
Jay Foad5c984e562011-04-13 13:46:01 +00001097 assert(Ops.size() == getNumOperands() && "Operand count mismatch!");
Galina Kistanovafc259902012-07-13 01:25:27 +00001098
Benjamin Kramer8008e9f2015-03-02 11:57:04 +00001099 // If no operands changed return self.
1100 if (Ty == getType() && std::equal(Ops.begin(), Ops.end(), op_begin()))
Chris Lattner227816342006-07-14 22:20:01 +00001101 return const_cast<ConstantExpr*>(this);
1102
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001103 Type *OnlyIfReducedTy = OnlyIfReduced ? Ty : nullptr;
Chris Lattner227816342006-07-14 22:20:01 +00001104 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001105 case Instruction::Trunc:
1106 case Instruction::ZExt:
1107 case Instruction::SExt:
1108 case Instruction::FPTrunc:
1109 case Instruction::FPExt:
1110 case Instruction::UIToFP:
1111 case Instruction::SIToFP:
1112 case Instruction::FPToUI:
1113 case Instruction::FPToSI:
1114 case Instruction::PtrToInt:
1115 case Instruction::IntToPtr:
1116 case Instruction::BitCast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001117 case Instruction::AddrSpaceCast:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001118 return ConstantExpr::getCast(getOpcode(), Ops[0], Ty, OnlyIfReduced);
Chris Lattner227816342006-07-14 22:20:01 +00001119 case Instruction::Select:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001120 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2], OnlyIfReducedTy);
Chris Lattner227816342006-07-14 22:20:01 +00001121 case Instruction::InsertElement:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001122 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2],
1123 OnlyIfReducedTy);
Chris Lattner227816342006-07-14 22:20:01 +00001124 case Instruction::ExtractElement:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001125 return ConstantExpr::getExtractElement(Ops[0], Ops[1], OnlyIfReducedTy);
Chris Lattner37e38352012-01-26 20:37:11 +00001126 case Instruction::InsertValue:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001127 return ConstantExpr::getInsertValue(Ops[0], Ops[1], getIndices(),
1128 OnlyIfReducedTy);
Chris Lattner37e38352012-01-26 20:37:11 +00001129 case Instruction::ExtractValue:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001130 return ConstantExpr::getExtractValue(Ops[0], getIndices(), OnlyIfReducedTy);
Chris Lattner227816342006-07-14 22:20:01 +00001131 case Instruction::ShuffleVector:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001132 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2],
1133 OnlyIfReducedTy);
David Blaikie88208842015-08-21 20:16:51 +00001134 case Instruction::GetElementPtr: {
1135 auto *GEPO = cast<GEPOperator>(this);
1136 assert(SrcTy || (Ops[0]->getType() == getOperand(0)->getType()));
1137 return ConstantExpr::getGetElementPtr(
1138 SrcTy ? SrcTy : GEPO->getSourceElementType(), Ops[0], Ops.slice(1),
Peter Collingbourned93620b2016-11-10 22:34:55 +00001139 GEPO->isInBounds(), GEPO->getInRangeIndex(), OnlyIfReducedTy);
David Blaikie88208842015-08-21 20:16:51 +00001140 }
Reid Spencer266e42b2006-12-23 06:05:41 +00001141 case Instruction::ICmp:
1142 case Instruction::FCmp:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001143 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1],
1144 OnlyIfReducedTy);
Chris Lattner227816342006-07-14 22:20:01 +00001145 default:
1146 assert(getNumOperands() == 2 && "Must be binary operator?");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001147 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1], SubclassOptionalData,
1148 OnlyIfReducedTy);
Chris Lattner7c1018a2006-07-14 19:37:40 +00001149 }
1150}
1151
Chris Lattner2f7c9632001-06-06 20:29:01 +00001152
1153//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00001154// isValueValidForType implementations
1155
Chris Lattner229907c2011-07-18 04:54:35 +00001156bool ConstantInt::isValueValidForType(Type *Ty, uint64_t Val) {
Chris Lattner8326bd82012-01-26 00:42:34 +00001157 unsigned NumBits = Ty->getIntegerBitWidth(); // assert okay
1158 if (Ty->isIntegerTy(1))
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001159 return Val == 0 || Val == 1;
Craig Topper50b1e512017-06-07 00:58:05 +00001160 return isUIntN(NumBits, Val);
Reid Spencere7334722006-12-19 01:28:19 +00001161}
1162
Chris Lattner229907c2011-07-18 04:54:35 +00001163bool ConstantInt::isValueValidForType(Type *Ty, int64_t Val) {
Chris Lattner8326bd82012-01-26 00:42:34 +00001164 unsigned NumBits = Ty->getIntegerBitWidth();
1165 if (Ty->isIntegerTy(1))
Reid Spencera94d3942007-01-19 21:13:56 +00001166 return Val == 0 || Val == 1 || Val == -1;
Craig Topper50b1e512017-06-07 00:58:05 +00001167 return isIntN(NumBits, Val);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001168}
1169
Chris Lattner229907c2011-07-18 04:54:35 +00001170bool ConstantFP::isValueValidForType(Type *Ty, const APFloat& Val) {
Dale Johannesend246b2c2007-08-30 00:23:21 +00001171 // convert modifies in place, so make a copy.
1172 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001173 bool losesInfo;
Chris Lattner6b727592004-06-17 18:19:28 +00001174 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001175 default:
1176 return false; // These can't be represented as floating point!
1177
Dale Johannesend246b2c2007-08-30 00:23:21 +00001178 // FIXME rounding mode needs to be more flexible
Dan Gohman518cda42011-12-17 00:04:22 +00001179 case Type::HalfTyID: {
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001180 if (&Val2.getSemantics() == &APFloat::IEEEhalf())
Dan Gohman518cda42011-12-17 00:04:22 +00001181 return true;
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001182 Val2.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &losesInfo);
Dan Gohman518cda42011-12-17 00:04:22 +00001183 return !losesInfo;
1184 }
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001185 case Type::FloatTyID: {
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001186 if (&Val2.getSemantics() == &APFloat::IEEEsingle())
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001187 return true;
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001188 Val2.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &losesInfo);
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001189 return !losesInfo;
1190 }
1191 case Type::DoubleTyID: {
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001192 if (&Val2.getSemantics() == &APFloat::IEEEhalf() ||
1193 &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1194 &Val2.getSemantics() == &APFloat::IEEEdouble())
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001195 return true;
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001196 Val2.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &losesInfo);
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001197 return !losesInfo;
1198 }
Dale Johannesenbdad8092007-08-09 22:51:36 +00001199 case Type::X86_FP80TyID:
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001200 return &Val2.getSemantics() == &APFloat::IEEEhalf() ||
1201 &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1202 &Val2.getSemantics() == &APFloat::IEEEdouble() ||
1203 &Val2.getSemantics() == &APFloat::x87DoubleExtended();
Dale Johannesenbdad8092007-08-09 22:51:36 +00001204 case Type::FP128TyID:
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001205 return &Val2.getSemantics() == &APFloat::IEEEhalf() ||
1206 &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1207 &Val2.getSemantics() == &APFloat::IEEEdouble() ||
1208 &Val2.getSemantics() == &APFloat::IEEEquad();
Dale Johannesen007aa372007-10-11 18:07:22 +00001209 case Type::PPC_FP128TyID:
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001210 return &Val2.getSemantics() == &APFloat::IEEEhalf() ||
1211 &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1212 &Val2.getSemantics() == &APFloat::IEEEdouble() ||
1213 &Val2.getSemantics() == &APFloat::PPCDoubleDouble();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001214 }
Chris Lattneraa2372562006-05-24 17:04:05 +00001215}
Chris Lattner9655e542001-07-20 19:16:02 +00001216
Chris Lattner030af792012-01-24 05:42:11 +00001217
Chris Lattner49d855c2001-09-07 16:46:31 +00001218//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +00001219// Factory Function Implementation
1220
Chris Lattnerc7f9fd42012-01-23 15:20:12 +00001221ConstantAggregateZero *ConstantAggregateZero::get(Type *Ty) {
Chris Lattner13ee7952010-08-28 04:09:24 +00001222 assert((Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy()) &&
Owen Andersonb292b8c2009-07-30 23:03:37 +00001223 "Cannot create an aggregate zero of non-aggregate type!");
David Blaikie899b85a2014-11-25 02:13:54 +00001224
Justin Lebar611c5c22016-10-10 16:26:13 +00001225 std::unique_ptr<ConstantAggregateZero> &Entry =
1226 Ty->getContext().pImpl->CAZConstants[Ty];
1227 if (!Entry)
1228 Entry.reset(new ConstantAggregateZero(Ty));
1229
1230 return Entry.get();
Owen Andersonb292b8c2009-07-30 23:03:37 +00001231}
1232
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001233/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001234void ConstantAggregateZero::destroyConstantImpl() {
Chris Lattnerc7f9fd42012-01-23 15:20:12 +00001235 getContext().pImpl->CAZConstants.erase(getType());
Chris Lattner9fba3da2004-02-15 05:53:04 +00001236}
1237
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001238/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001239void ConstantArray::destroyConstantImpl() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001240 getType()->getContext().pImpl->ArrayConstants.remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001241}
1242
Chris Lattner81fabb02002-08-26 17:53:56 +00001243
Chris Lattner3462ae32001-12-03 22:26:30 +00001244//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001245//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001246
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001247/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001248void ConstantStruct::destroyConstantImpl() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001249 getType()->getContext().pImpl->StructConstants.remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001250}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001251
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001252/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001253void ConstantVector::destroyConstantImpl() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001254 getType()->getContext().pImpl->VectorConstants.remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001255}
1256
Duncan Sandse6beec62012-11-13 12:59:33 +00001257Constant *Constant::getSplatValue() const {
1258 assert(this->getType()->isVectorTy() && "Only valid for vectors!");
1259 if (isa<ConstantAggregateZero>(this))
1260 return getNullValue(this->getType()->getVectorElementType());
1261 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this))
1262 return CV->getSplatValue();
1263 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
1264 return CV->getSplatValue();
Craig Topperc6207612014-04-09 06:08:46 +00001265 return nullptr;
Duncan Sandse6beec62012-11-13 12:59:33 +00001266}
1267
Duncan Sandscf0ff032011-02-01 08:39:12 +00001268Constant *ConstantVector::getSplatValue() const {
Dan Gohman07159202007-10-17 17:51:30 +00001269 // Check out first element.
1270 Constant *Elt = getOperand(0);
1271 // Then make sure all remaining elements point to the same value.
1272 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
Chris Lattnerd04e32d2011-07-17 06:01:30 +00001273 if (getOperand(I) != Elt)
Craig Topperc6207612014-04-09 06:08:46 +00001274 return nullptr;
Dan Gohman07159202007-10-17 17:51:30 +00001275 return Elt;
1276}
1277
Duncan Sandse6beec62012-11-13 12:59:33 +00001278const APInt &Constant::getUniqueInteger() const {
1279 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
1280 return CI->getValue();
1281 assert(this->getSplatValue() && "Doesn't contain a unique integer!");
1282 const Constant *C = this->getAggregateElement(0U);
1283 assert(C && isa<ConstantInt>(C) && "Not a vector of numbers!");
1284 return cast<ConstantInt>(C)->getValue();
1285}
1286
Chris Lattner31b132c2009-10-28 00:01:44 +00001287//---- ConstantPointerNull::get() implementation.
Chris Lattnerd7a73302001-10-13 06:57:33 +00001288//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001289
Chris Lattner229907c2011-07-18 04:54:35 +00001290ConstantPointerNull *ConstantPointerNull::get(PointerType *Ty) {
Justin Lebar611c5c22016-10-10 16:26:13 +00001291 std::unique_ptr<ConstantPointerNull> &Entry =
1292 Ty->getContext().pImpl->CPNConstants[Ty];
Craig Topperc6207612014-04-09 06:08:46 +00001293 if (!Entry)
Justin Lebar611c5c22016-10-10 16:26:13 +00001294 Entry.reset(new ConstantPointerNull(Ty));
Galina Kistanovafc259902012-07-13 01:25:27 +00001295
Justin Lebar611c5c22016-10-10 16:26:13 +00001296 return Entry.get();
Chris Lattner883ad0b2001-10-03 15:39:36 +00001297}
1298
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001299/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001300void ConstantPointerNull::destroyConstantImpl() {
Chris Lattnerc7f9fd42012-01-23 15:20:12 +00001301 getContext().pImpl->CPNConstants.erase(getType());
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001302}
1303
Chris Lattner229907c2011-07-18 04:54:35 +00001304UndefValue *UndefValue::get(Type *Ty) {
Justin Lebar611c5c22016-10-10 16:26:13 +00001305 std::unique_ptr<UndefValue> &Entry = Ty->getContext().pImpl->UVConstants[Ty];
Craig Topperc6207612014-04-09 06:08:46 +00001306 if (!Entry)
Justin Lebar611c5c22016-10-10 16:26:13 +00001307 Entry.reset(new UndefValue(Ty));
Galina Kistanovafc259902012-07-13 01:25:27 +00001308
Justin Lebar611c5c22016-10-10 16:26:13 +00001309 return Entry.get();
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001310}
1311
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001312/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001313void UndefValue::destroyConstantImpl() {
Chris Lattnerc7f9fd42012-01-23 15:20:12 +00001314 // Free the constant and any dangling references to it.
1315 getContext().pImpl->UVConstants.erase(getType());
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001316}
1317
Chris Lattner31b132c2009-10-28 00:01:44 +00001318BlockAddress *BlockAddress::get(BasicBlock *BB) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001319 assert(BB->getParent() && "Block must have a parent");
Chris Lattner31b132c2009-10-28 00:01:44 +00001320 return get(BB->getParent(), BB);
1321}
1322
1323BlockAddress *BlockAddress::get(Function *F, BasicBlock *BB) {
1324 BlockAddress *&BA =
1325 F->getContext().pImpl->BlockAddresses[std::make_pair(F, BB)];
Craig Topperc6207612014-04-09 06:08:46 +00001326 if (!BA)
Chris Lattner31b132c2009-10-28 00:01:44 +00001327 BA = new BlockAddress(F, BB);
Galina Kistanovafc259902012-07-13 01:25:27 +00001328
Chris Lattner31b132c2009-10-28 00:01:44 +00001329 assert(BA->getFunction() == F && "Basic block moved between functions");
1330 return BA;
1331}
1332
1333BlockAddress::BlockAddress(Function *F, BasicBlock *BB)
1334: Constant(Type::getInt8PtrTy(F->getContext()), Value::BlockAddressVal,
1335 &Op<0>(), 2) {
Chris Lattnerc559a9f2009-11-01 03:03:03 +00001336 setOperand(0, F);
1337 setOperand(1, BB);
Chris Lattneraa99c942009-11-01 01:27:45 +00001338 BB->AdjustBlockAddressRefCount(1);
Chris Lattner31b132c2009-10-28 00:01:44 +00001339}
1340
Chandler Carruth6a936922014-01-19 02:13:50 +00001341BlockAddress *BlockAddress::lookup(const BasicBlock *BB) {
1342 if (!BB->hasAddressTaken())
Craig Topperc6207612014-04-09 06:08:46 +00001343 return nullptr;
Chandler Carruth6a936922014-01-19 02:13:50 +00001344
1345 const Function *F = BB->getParent();
Craig Topper2617dcc2014-04-15 06:32:26 +00001346 assert(F && "Block must have a parent");
Chandler Carruth6a936922014-01-19 02:13:50 +00001347 BlockAddress *BA =
1348 F->getContext().pImpl->BlockAddresses.lookup(std::make_pair(F, BB));
1349 assert(BA && "Refcount and block address map disagree!");
1350 return BA;
1351}
Chris Lattner31b132c2009-10-28 00:01:44 +00001352
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001353/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001354void BlockAddress::destroyConstantImpl() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001355 getFunction()->getType()->getContext().pImpl
Chris Lattner31b132c2009-10-28 00:01:44 +00001356 ->BlockAddresses.erase(std::make_pair(getFunction(), getBasicBlock()));
Chris Lattneraa99c942009-11-01 01:27:45 +00001357 getBasicBlock()->AdjustBlockAddressRefCount(-1);
Chris Lattner31b132c2009-10-28 00:01:44 +00001358}
1359
Mehdi Amini8914d292016-02-10 22:47:15 +00001360Value *BlockAddress::handleOperandChangeImpl(Value *From, Value *To) {
Chris Lattner31b132c2009-10-28 00:01:44 +00001361 // This could be replacing either the Basic Block or the Function. In either
1362 // case, we have to remove the map entry.
1363 Function *NewF = getFunction();
1364 BasicBlock *NewBB = getBasicBlock();
Galina Kistanovafc259902012-07-13 01:25:27 +00001365
Mehdi Amini8914d292016-02-10 22:47:15 +00001366 if (From == NewF)
Derek Schuffec9dc012013-06-13 19:51:17 +00001367 NewF = cast<Function>(To->stripPointerCasts());
Mehdi Amini8914d292016-02-10 22:47:15 +00001368 else {
1369 assert(From == NewBB && "From does not match any operand");
Chris Lattner31b132c2009-10-28 00:01:44 +00001370 NewBB = cast<BasicBlock>(To);
Mehdi Amini8914d292016-02-10 22:47:15 +00001371 }
Galina Kistanovafc259902012-07-13 01:25:27 +00001372
Chris Lattner31b132c2009-10-28 00:01:44 +00001373 // See if the 'new' entry already exists, if not, just update this in place
1374 // and return early.
1375 BlockAddress *&NewBA =
1376 getContext().pImpl->BlockAddresses[std::make_pair(NewF, NewBB)];
Pete Cooper5815b1f2015-06-24 18:55:24 +00001377 if (NewBA)
1378 return NewBA;
Chris Lattner31b132c2009-10-28 00:01:44 +00001379
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001380 getBasicBlock()->AdjustBlockAddressRefCount(-1);
Galina Kistanovafc259902012-07-13 01:25:27 +00001381
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001382 // Remove the old entry, this can't cause the map to rehash (just a
1383 // tombstone will get added).
1384 getContext().pImpl->BlockAddresses.erase(std::make_pair(getFunction(),
1385 getBasicBlock()));
1386 NewBA = this;
1387 setOperand(0, NewF);
1388 setOperand(1, NewBB);
1389 getBasicBlock()->AdjustBlockAddressRefCount(1);
Pete Cooper5815b1f2015-06-24 18:55:24 +00001390
1391 // If we just want to keep the existing value, then return null.
1392 // Callers know that this means we shouldn't delete this value.
1393 return nullptr;
Chris Lattner31b132c2009-10-28 00:01:44 +00001394}
1395
1396//---- ConstantExpr::get() implementations.
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001397//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001398
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001399/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands7d6c8ae2008-03-30 19:38:55 +00001400/// cast in the ExprConstants map. It is used by the various get* methods below.
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001401static Constant *getFoldedCast(Instruction::CastOps opc, Constant *C, Type *Ty,
1402 bool OnlyIfReduced = false) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001403 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001404 // Fold a few common cases
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001405 if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001406 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001407
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001408 if (OnlyIfReduced)
1409 return nullptr;
1410
Owen Anderson1584a292009-08-04 20:25:11 +00001411 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
1412
Nadav Rotem88330432013-03-07 01:30:40 +00001413 // Look up the constant in the table first to ensure uniqueness.
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001414 ConstantExprKeyType Key(opc, C);
Galina Kistanovafc259902012-07-13 01:25:27 +00001415
Owen Anderson1584a292009-08-04 20:25:11 +00001416 return pImpl->ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001417}
Galina Kistanovafc259902012-07-13 01:25:27 +00001418
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001419Constant *ConstantExpr::getCast(unsigned oc, Constant *C, Type *Ty,
1420 bool OnlyIfReduced) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001421 Instruction::CastOps opc = Instruction::CastOps(oc);
1422 assert(Instruction::isCast(opc) && "opcode out of range");
1423 assert(C && Ty && "Null arguments to getCast");
Chris Lattner37bc78a2010-01-26 21:51:43 +00001424 assert(CastInst::castIsValid(opc, C, Ty) && "Invalid constantexpr cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001425
1426 switch (opc) {
Chris Lattner37bc78a2010-01-26 21:51:43 +00001427 default:
1428 llvm_unreachable("Invalid cast opcode");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001429 case Instruction::Trunc:
1430 return getTrunc(C, Ty, OnlyIfReduced);
1431 case Instruction::ZExt:
1432 return getZExt(C, Ty, OnlyIfReduced);
1433 case Instruction::SExt:
1434 return getSExt(C, Ty, OnlyIfReduced);
1435 case Instruction::FPTrunc:
1436 return getFPTrunc(C, Ty, OnlyIfReduced);
1437 case Instruction::FPExt:
1438 return getFPExtend(C, Ty, OnlyIfReduced);
1439 case Instruction::UIToFP:
1440 return getUIToFP(C, Ty, OnlyIfReduced);
1441 case Instruction::SIToFP:
1442 return getSIToFP(C, Ty, OnlyIfReduced);
1443 case Instruction::FPToUI:
1444 return getFPToUI(C, Ty, OnlyIfReduced);
1445 case Instruction::FPToSI:
1446 return getFPToSI(C, Ty, OnlyIfReduced);
1447 case Instruction::PtrToInt:
1448 return getPtrToInt(C, Ty, OnlyIfReduced);
1449 case Instruction::IntToPtr:
1450 return getIntToPtr(C, Ty, OnlyIfReduced);
1451 case Instruction::BitCast:
1452 return getBitCast(C, Ty, OnlyIfReduced);
1453 case Instruction::AddrSpaceCast:
1454 return getAddrSpaceCast(C, Ty, OnlyIfReduced);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001455 }
Galina Kistanovafc259902012-07-13 01:25:27 +00001456}
Reid Spencerf37dc652006-12-05 19:14:13 +00001457
Chris Lattner229907c2011-07-18 04:54:35 +00001458Constant *ConstantExpr::getZExtOrBitCast(Constant *C, Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001459 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001460 return getBitCast(C, Ty);
1461 return getZExt(C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001462}
1463
Chris Lattner229907c2011-07-18 04:54:35 +00001464Constant *ConstantExpr::getSExtOrBitCast(Constant *C, Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001465 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001466 return getBitCast(C, Ty);
1467 return getSExt(C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001468}
1469
Chris Lattner229907c2011-07-18 04:54:35 +00001470Constant *ConstantExpr::getTruncOrBitCast(Constant *C, Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001471 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001472 return getBitCast(C, Ty);
1473 return getTrunc(C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001474}
1475
Chris Lattner229907c2011-07-18 04:54:35 +00001476Constant *ConstantExpr::getPointerCast(Constant *S, Type *Ty) {
Evgeniy Stepanov23382642013-01-16 14:41:46 +00001477 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
1478 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
1479 "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001480
Evgeniy Stepanov23382642013-01-16 14:41:46 +00001481 if (Ty->isIntOrIntVectorTy())
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001482 return getPtrToInt(S, Ty);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001483
1484 unsigned SrcAS = S->getType()->getPointerAddressSpace();
1485 if (Ty->isPtrOrPtrVectorTy() && SrcAS != Ty->getPointerAddressSpace())
1486 return getAddrSpaceCast(S, Ty);
1487
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001488 return getBitCast(S, Ty);
Reid Spencerbc245a02006-12-05 03:25:26 +00001489}
1490
Matt Arsenault21f38f42013-12-07 02:58:41 +00001491Constant *ConstantExpr::getPointerBitCastOrAddrSpaceCast(Constant *S,
1492 Type *Ty) {
1493 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
1494 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
1495
1496 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
1497 return getAddrSpaceCast(S, Ty);
1498
1499 return getBitCast(S, Ty);
1500}
1501
Sanjay Patelf3587ec2016-05-18 22:05:28 +00001502Constant *ConstantExpr::getIntegerCast(Constant *C, Type *Ty, bool isSigned) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001503 assert(C->getType()->isIntOrIntVectorTy() &&
1504 Ty->isIntOrIntVectorTy() && "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001505 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1506 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer56521c42006-12-12 00:51:07 +00001507 Instruction::CastOps opcode =
1508 (SrcBits == DstBits ? Instruction::BitCast :
1509 (SrcBits > DstBits ? Instruction::Trunc :
1510 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1511 return getCast(opcode, C, Ty);
1512}
1513
Chris Lattner229907c2011-07-18 04:54:35 +00001514Constant *ConstantExpr::getFPCast(Constant *C, Type *Ty) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001515 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer56521c42006-12-12 00:51:07 +00001516 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001517 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1518 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001519 if (SrcBits == DstBits)
1520 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001521 Instruction::CastOps opcode =
Jay Foad9f32cfd2011-01-27 14:44:55 +00001522 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001523 return getCast(opcode, C, Ty);
1524}
1525
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001526Constant *ConstantExpr::getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001527#ifndef NDEBUG
1528 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1529 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1530#endif
1531 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001532 assert(C->getType()->isIntOrIntVectorTy() && "Trunc operand must be integer");
1533 assert(Ty->isIntOrIntVectorTy() && "Trunc produces only integral");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001534 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001535 "SrcTy must be larger than DestTy for Trunc!");
1536
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001537 return getFoldedCast(Instruction::Trunc, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001538}
1539
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001540Constant *ConstantExpr::getSExt(Constant *C, Type *Ty, bool OnlyIfReduced) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001541#ifndef NDEBUG
1542 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1543 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1544#endif
1545 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001546 assert(C->getType()->isIntOrIntVectorTy() && "SExt operand must be integral");
1547 assert(Ty->isIntOrIntVectorTy() && "SExt produces only integer");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001548 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001549 "SrcTy must be smaller than DestTy for SExt!");
1550
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001551 return getFoldedCast(Instruction::SExt, C, Ty, OnlyIfReduced);
Chris Lattnerdd284742004-04-04 23:20:30 +00001552}
1553
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001554Constant *ConstantExpr::getZExt(Constant *C, Type *Ty, bool OnlyIfReduced) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001555#ifndef NDEBUG
1556 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1557 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1558#endif
1559 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001560 assert(C->getType()->isIntOrIntVectorTy() && "ZEXt operand must be integral");
1561 assert(Ty->isIntOrIntVectorTy() && "ZExt produces only integer");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001562 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001563 "SrcTy must be smaller than DestTy for ZExt!");
1564
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001565 return getFoldedCast(Instruction::ZExt, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001566}
1567
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001568Constant *ConstantExpr::getFPTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001569#ifndef NDEBUG
1570 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1571 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1572#endif
1573 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001574 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001575 C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001576 "This is an illegal floating point truncation!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001577 return getFoldedCast(Instruction::FPTrunc, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001578}
1579
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001580Constant *ConstantExpr::getFPExtend(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()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001587 C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001588 "This is an illegal floating point extension!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001589 return getFoldedCast(Instruction::FPExt, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001590}
1591
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001592Constant *ConstantExpr::getUIToFP(Constant *C, Type *Ty, bool OnlyIfReduced) {
Devang Pateld26344d2008-11-03 23:20:04 +00001593#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001594 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1595 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001596#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001597 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001598 assert(C->getType()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00001599 "This is an illegal uint to floating point cast!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001600 return getFoldedCast(Instruction::UIToFP, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001601}
1602
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001603Constant *ConstantExpr::getSIToFP(Constant *C, Type *Ty, bool OnlyIfReduced) {
Devang Pateld26344d2008-11-03 23:20:04 +00001604#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001605 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1606 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001607#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001608 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001609 assert(C->getType()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001610 "This is an illegal sint to floating point cast!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001611 return getFoldedCast(Instruction::SIToFP, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001612}
1613
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001614Constant *ConstantExpr::getFPToUI(Constant *C, Type *Ty, bool OnlyIfReduced) {
Devang Pateld26344d2008-11-03 23:20:04 +00001615#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001616 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1617 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001618#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001619 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001620 assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00001621 "This is an illegal floating point to uint cast!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001622 return getFoldedCast(Instruction::FPToUI, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001623}
1624
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001625Constant *ConstantExpr::getFPToSI(Constant *C, Type *Ty, bool OnlyIfReduced) {
Devang Pateld26344d2008-11-03 23:20:04 +00001626#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001627 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1628 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001629#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001630 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001631 assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00001632 "This is an illegal floating point to sint cast!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001633 return getFoldedCast(Instruction::FPToSI, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001634}
1635
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001636Constant *ConstantExpr::getPtrToInt(Constant *C, Type *DstTy,
1637 bool OnlyIfReduced) {
Craig Topper95d23472017-07-09 07:04:00 +00001638 assert(C->getType()->isPtrOrPtrVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00001639 "PtrToInt source must be pointer or pointer vector");
Craig Topper95d23472017-07-09 07:04:00 +00001640 assert(DstTy->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00001641 "PtrToInt destination must be integer or integer vector");
Chris Lattner8a3df542012-01-25 01:32:59 +00001642 assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy));
Nick Lewyckyff509622012-01-25 03:20:12 +00001643 if (isa<VectorType>(C->getType()))
Chris Lattner8326bd82012-01-26 00:42:34 +00001644 assert(C->getType()->getVectorNumElements()==DstTy->getVectorNumElements()&&
Chris Lattner8a3df542012-01-25 01:32:59 +00001645 "Invalid cast between a different number of vector elements");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001646 return getFoldedCast(Instruction::PtrToInt, C, DstTy, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001647}
1648
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001649Constant *ConstantExpr::getIntToPtr(Constant *C, Type *DstTy,
1650 bool OnlyIfReduced) {
Craig Topper95d23472017-07-09 07:04:00 +00001651 assert(C->getType()->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00001652 "IntToPtr source must be integer or integer vector");
Craig Topper95d23472017-07-09 07:04:00 +00001653 assert(DstTy->isPtrOrPtrVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00001654 "IntToPtr destination must be a pointer or pointer vector");
Chris Lattner8a3df542012-01-25 01:32:59 +00001655 assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy));
Nick Lewyckyff509622012-01-25 03:20:12 +00001656 if (isa<VectorType>(C->getType()))
Chris Lattner8326bd82012-01-26 00:42:34 +00001657 assert(C->getType()->getVectorNumElements()==DstTy->getVectorNumElements()&&
Chris Lattner8a3df542012-01-25 01:32:59 +00001658 "Invalid cast between a different number of vector elements");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001659 return getFoldedCast(Instruction::IntToPtr, C, DstTy, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001660}
1661
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001662Constant *ConstantExpr::getBitCast(Constant *C, Type *DstTy,
1663 bool OnlyIfReduced) {
Chris Lattner37bc78a2010-01-26 21:51:43 +00001664 assert(CastInst::castIsValid(Instruction::BitCast, C, DstTy) &&
1665 "Invalid constantexpr bitcast!");
Galina Kistanovafc259902012-07-13 01:25:27 +00001666
Chris Lattnercbeda872009-03-21 06:55:54 +00001667 // It is common to ask for a bitcast of a value to its own type, handle this
1668 // speedily.
1669 if (C->getType() == DstTy) return C;
Galina Kistanovafc259902012-07-13 01:25:27 +00001670
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001671 return getFoldedCast(Instruction::BitCast, C, DstTy, OnlyIfReduced);
Chris Lattnerdd284742004-04-04 23:20:30 +00001672}
1673
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001674Constant *ConstantExpr::getAddrSpaceCast(Constant *C, Type *DstTy,
1675 bool OnlyIfReduced) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001676 assert(CastInst::castIsValid(Instruction::AddrSpaceCast, C, DstTy) &&
1677 "Invalid constantexpr addrspacecast!");
1678
Jingyue Wubaabe502014-06-15 21:40:57 +00001679 // Canonicalize addrspacecasts between different pointer types by first
1680 // bitcasting the pointer type and then converting the address space.
1681 PointerType *SrcScalarTy = cast<PointerType>(C->getType()->getScalarType());
1682 PointerType *DstScalarTy = cast<PointerType>(DstTy->getScalarType());
1683 Type *DstElemTy = DstScalarTy->getElementType();
1684 if (SrcScalarTy->getElementType() != DstElemTy) {
1685 Type *MidTy = PointerType::get(DstElemTy, SrcScalarTy->getAddressSpace());
1686 if (VectorType *VT = dyn_cast<VectorType>(DstTy)) {
1687 // Handle vectors of pointers.
1688 MidTy = VectorType::get(MidTy, VT->getNumElements());
1689 }
1690 C = getBitCast(C, MidTy);
1691 }
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001692 return getFoldedCast(Instruction::AddrSpaceCast, C, DstTy, OnlyIfReduced);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001693}
1694
Chris Lattner887ecac2011-07-09 18:23:52 +00001695Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2,
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001696 unsigned Flags, Type *OnlyIfReducedTy) {
Chris Lattner887ecac2011-07-09 18:23:52 +00001697 // Check the operands for consistency first.
Reid Spencer7eb55b32006-11-02 01:53:59 +00001698 assert(Opcode >= Instruction::BinaryOpsBegin &&
1699 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001700 "Invalid opcode in binary constant expression");
1701 assert(C1->getType() == C2->getType() &&
1702 "Operand types in binary constant expression should match");
Galina Kistanovafc259902012-07-13 01:25:27 +00001703
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001704#ifndef NDEBUG
1705 switch (Opcode) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001706 case Instruction::Add:
Reid Spencer7eb55b32006-11-02 01:53:59 +00001707 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001708 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001709 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001710 assert(C1->getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001711 "Tried to create an integer operation on a non-integer type!");
1712 break;
1713 case Instruction::FAdd:
1714 case Instruction::FSub:
1715 case Instruction::FMul:
1716 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001717 assert(C1->getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001718 "Tried to create a floating-point operation on a "
1719 "non-floating-point type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001720 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001721 case Instruction::UDiv:
1722 case Instruction::SDiv:
1723 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001724 assert(C1->getType()->isIntOrIntVectorTy() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001725 "Tried to create an arithmetic operation on a non-arithmetic type!");
1726 break;
1727 case Instruction::FDiv:
1728 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001729 assert(C1->getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001730 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001731 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001732 case Instruction::URem:
1733 case Instruction::SRem:
1734 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001735 assert(C1->getType()->isIntOrIntVectorTy() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001736 "Tried to create an arithmetic operation on a non-arithmetic type!");
1737 break;
1738 case Instruction::FRem:
1739 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001740 assert(C1->getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001741 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001742 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001743 case Instruction::And:
1744 case Instruction::Or:
1745 case Instruction::Xor:
1746 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001747 assert(C1->getType()->isIntOrIntVectorTy() &&
Misha Brukman3852f652005-01-27 06:46:38 +00001748 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001749 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001750 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001751 case Instruction::LShr:
1752 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00001753 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001754 assert(C1->getType()->isIntOrIntVectorTy() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001755 "Tried to create a shift operation on a non-integer type!");
1756 break;
1757 default:
1758 break;
1759 }
1760#endif
1761
Chris Lattner887ecac2011-07-09 18:23:52 +00001762 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1763 return FC; // Fold a few common cases.
Galina Kistanovafc259902012-07-13 01:25:27 +00001764
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001765 if (OnlyIfReducedTy == C1->getType())
1766 return nullptr;
1767
Benjamin Kramer324322b2013-03-07 20:53:34 +00001768 Constant *ArgVec[] = { C1, C2 };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001769 ConstantExprKeyType Key(Opcode, ArgVec, 0, Flags);
Galina Kistanovafc259902012-07-13 01:25:27 +00001770
Chris Lattner887ecac2011-07-09 18:23:52 +00001771 LLVMContextImpl *pImpl = C1->getContext().pImpl;
1772 return pImpl->ExprConstants.getOrCreate(C1->getType(), Key);
Reid Spencera009d0d2006-12-04 21:35:24 +00001773}
1774
Chris Lattner229907c2011-07-18 04:54:35 +00001775Constant *ConstantExpr::getSizeOf(Type* Ty) {
Owen Anderson487375e2009-07-29 18:55:55 +00001776 // sizeof is implemented as: (i64) gep (Ty*)null, 1
1777 // Note that a non-inbounds gep is used, as null isn't within any object.
Owen Anderson55f1c092009-08-13 21:58:54 +00001778 Constant *GEPIdx = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
Owen Anderson487375e2009-07-29 18:55:55 +00001779 Constant *GEP = getGetElementPtr(
David Blaikie4a2e73b2015-04-02 18:55:32 +00001780 Ty, Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx);
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001781 return getPtrToInt(GEP,
1782 Type::getInt64Ty(Ty->getContext()));
Owen Anderson487375e2009-07-29 18:55:55 +00001783}
1784
Chris Lattner229907c2011-07-18 04:54:35 +00001785Constant *ConstantExpr::getAlignOf(Type* Ty) {
Dan Gohmancf913832010-01-28 02:15:55 +00001786 // alignof is implemented as: (i64) gep ({i1,Ty}*)null, 0, 1
Dan Gohman50c09d02009-08-11 17:57:01 +00001787 // Note that a non-inbounds gep is used, as null isn't within any object.
Serge Gueltone38003f2017-05-09 19:31:13 +00001788 Type *AligningTy = StructType::get(Type::getInt1Ty(Ty->getContext()), Ty);
Matt Arsenaultbe558882014-04-23 20:58:57 +00001789 Constant *NullPtr = Constant::getNullValue(AligningTy->getPointerTo(0));
Dan Gohmana9be7392010-01-28 02:43:22 +00001790 Constant *Zero = ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0);
Owen Anderson55f1c092009-08-13 21:58:54 +00001791 Constant *One = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
Owen Anderson487375e2009-07-29 18:55:55 +00001792 Constant *Indices[2] = { Zero, One };
David Blaikie4a2e73b2015-04-02 18:55:32 +00001793 Constant *GEP = getGetElementPtr(AligningTy, NullPtr, Indices);
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001794 return getPtrToInt(GEP,
1795 Type::getInt64Ty(Ty->getContext()));
Owen Anderson487375e2009-07-29 18:55:55 +00001796}
1797
Chris Lattner229907c2011-07-18 04:54:35 +00001798Constant *ConstantExpr::getOffsetOf(StructType* STy, unsigned FieldNo) {
Dan Gohmanede94e62010-02-01 16:37:38 +00001799 return getOffsetOf(STy, ConstantInt::get(Type::getInt32Ty(STy->getContext()),
1800 FieldNo));
1801}
1802
Chris Lattner229907c2011-07-18 04:54:35 +00001803Constant *ConstantExpr::getOffsetOf(Type* Ty, Constant *FieldNo) {
Dan Gohmanff3af7252009-08-16 21:26:11 +00001804 // offsetof is implemented as: (i64) gep (Ty*)null, 0, FieldNo
1805 // Note that a non-inbounds gep is used, as null isn't within any object.
1806 Constant *GEPIdx[] = {
Dan Gohmanede94e62010-02-01 16:37:38 +00001807 ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0),
1808 FieldNo
Dan Gohmanff3af7252009-08-16 21:26:11 +00001809 };
1810 Constant *GEP = getGetElementPtr(
David Blaikie4a2e73b2015-04-02 18:55:32 +00001811 Ty, Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx);
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001812 return getPtrToInt(GEP,
1813 Type::getInt64Ty(Ty->getContext()));
Dan Gohmanff3af7252009-08-16 21:26:11 +00001814}
Owen Anderson487375e2009-07-29 18:55:55 +00001815
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001816Constant *ConstantExpr::getCompare(unsigned short Predicate, Constant *C1,
1817 Constant *C2, bool OnlyIfReduced) {
Reid Spencera009d0d2006-12-04 21:35:24 +00001818 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Galina Kistanovafc259902012-07-13 01:25:27 +00001819
Chris Lattner887ecac2011-07-09 18:23:52 +00001820 switch (Predicate) {
1821 default: llvm_unreachable("Invalid CmpInst predicate");
1822 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
1823 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
1824 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
1825 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
1826 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
1827 case CmpInst::FCMP_TRUE:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001828 return getFCmp(Predicate, C1, C2, OnlyIfReduced);
Galina Kistanovafc259902012-07-13 01:25:27 +00001829
Chris Lattner887ecac2011-07-09 18:23:52 +00001830 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
1831 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
1832 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
1833 case CmpInst::ICMP_SLE:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001834 return getICmp(Predicate, C1, C2, OnlyIfReduced);
Chris Lattner887ecac2011-07-09 18:23:52 +00001835 }
Chris Lattner29ca2c62004-08-04 18:50:09 +00001836}
1837
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001838Constant *ConstantExpr::getSelect(Constant *C, Constant *V1, Constant *V2,
1839 Type *OnlyIfReducedTy) {
Chris Lattner41632132008-12-29 00:16:12 +00001840 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner6e415c02004-03-12 05:54:04 +00001841
Chris Lattner887ecac2011-07-09 18:23:52 +00001842 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1843 return SC; // Fold common cases
Chris Lattner6e415c02004-03-12 05:54:04 +00001844
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001845 if (OnlyIfReducedTy == V1->getType())
1846 return nullptr;
1847
Benjamin Kramer324322b2013-03-07 20:53:34 +00001848 Constant *ArgVec[] = { C, V1, V2 };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001849 ConstantExprKeyType Key(Instruction::Select, ArgVec);
Galina Kistanovafc259902012-07-13 01:25:27 +00001850
Chris Lattner887ecac2011-07-09 18:23:52 +00001851 LLVMContextImpl *pImpl = C->getContext().pImpl;
1852 return pImpl->ExprConstants.getOrCreate(V1->getType(), Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00001853}
1854
David Blaikie4a2e73b2015-04-02 18:55:32 +00001855Constant *ConstantExpr::getGetElementPtr(Type *Ty, Constant *C,
1856 ArrayRef<Value *> Idxs, bool InBounds,
Peter Collingbourned93620b2016-11-10 22:34:55 +00001857 Optional<unsigned> InRangeIndex,
David Blaikie4a2e73b2015-04-02 18:55:32 +00001858 Type *OnlyIfReducedTy) {
David Blaikie4a2e73b2015-04-02 18:55:32 +00001859 if (!Ty)
1860 Ty = cast<PointerType>(C->getType()->getScalarType())->getElementType();
1861 else
David Blaikied9d900c2015-05-07 17:28:58 +00001862 assert(
1863 Ty ==
1864 cast<PointerType>(C->getType()->getScalarType())->getContainedType(0u));
1865
Peter Collingbourned93620b2016-11-10 22:34:55 +00001866 if (Constant *FC =
1867 ConstantFoldGetElementPtr(Ty, C, InBounds, InRangeIndex, Idxs))
David Blaikied9d900c2015-05-07 17:28:58 +00001868 return FC; // Fold a few common cases.
1869
Chris Lattner887ecac2011-07-09 18:23:52 +00001870 // Get the result type of the getelementptr!
David Blaikie4a2e73b2015-04-02 18:55:32 +00001871 Type *DestTy = GetElementPtrInst::getIndexedType(Ty, Idxs);
1872 assert(DestTy && "GEP indices invalid!");
Chris Lattner8326bd82012-01-26 00:42:34 +00001873 unsigned AS = C->getType()->getPointerAddressSpace();
David Blaikie4a2e73b2015-04-02 18:55:32 +00001874 Type *ReqTy = DestTy->getPointerTo(AS);
Elena Demikhovskyee004bc2016-05-15 12:30:25 +00001875
1876 unsigned NumVecElts = 0;
1877 if (C->getType()->isVectorTy())
1878 NumVecElts = C->getType()->getVectorNumElements();
1879 else for (auto Idx : Idxs)
1880 if (Idx->getType()->isVectorTy())
1881 NumVecElts = Idx->getType()->getVectorNumElements();
1882
1883 if (NumVecElts)
1884 ReqTy = VectorType::get(ReqTy, NumVecElts);
Galina Kistanovafc259902012-07-13 01:25:27 +00001885
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001886 if (OnlyIfReducedTy == ReqTy)
1887 return nullptr;
1888
Dan Gohman1b849082009-09-07 23:54:19 +00001889 // Look up the constant in the table first to ensure uniqueness
1890 std::vector<Constant*> ArgVec;
Jay Foaded8db7d2011-07-21 14:31:17 +00001891 ArgVec.reserve(1 + Idxs.size());
Dan Gohman1b849082009-09-07 23:54:19 +00001892 ArgVec.push_back(C);
Duncan Sandse6beec62012-11-13 12:59:33 +00001893 for (unsigned i = 0, e = Idxs.size(); i != e; ++i) {
Duncan Sandse6beec62012-11-13 12:59:33 +00001894 assert((!Idxs[i]->getType()->isVectorTy() ||
Elena Demikhovskyee004bc2016-05-15 12:30:25 +00001895 Idxs[i]->getType()->getVectorNumElements() == NumVecElts) &&
Duncan Sandse6beec62012-11-13 12:59:33 +00001896 "getelementptr index type missmatch");
Elena Demikhovskyee004bc2016-05-15 12:30:25 +00001897
1898 Constant *Idx = cast<Constant>(Idxs[i]);
1899 if (NumVecElts && !Idxs[i]->getType()->isVectorTy())
1900 Idx = ConstantVector::getSplat(NumVecElts, Idx);
1901 ArgVec.push_back(Idx);
Duncan Sandse6beec62012-11-13 12:59:33 +00001902 }
Peter Collingbourned93620b2016-11-10 22:34:55 +00001903
1904 unsigned SubClassOptionalData = InBounds ? GEPOperator::IsInBounds : 0;
1905 if (InRangeIndex && *InRangeIndex < 63)
1906 SubClassOptionalData |= (*InRangeIndex + 1) << 1;
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001907 const ConstantExprKeyType Key(Instruction::GetElementPtr, ArgVec, 0,
Peter Collingbourned93620b2016-11-10 22:34:55 +00001908 SubClassOptionalData, None, Ty);
Galina Kistanovafc259902012-07-13 01:25:27 +00001909
Chris Lattner887ecac2011-07-09 18:23:52 +00001910 LLVMContextImpl *pImpl = C->getContext().pImpl;
Dan Gohman1b849082009-09-07 23:54:19 +00001911 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
1912}
1913
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001914Constant *ConstantExpr::getICmp(unsigned short pred, Constant *LHS,
1915 Constant *RHS, bool OnlyIfReduced) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001916 assert(LHS->getType() == RHS->getType());
Craig Topper45844762017-07-05 23:35:46 +00001917 assert(CmpInst::isIntPredicate((CmpInst::Predicate)pred) &&
1918 "Invalid ICmp Predicate");
Reid Spenceree3c9912006-12-04 05:19:50 +00001919
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001920 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001921 return FC; // Fold a few common cases...
1922
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001923 if (OnlyIfReduced)
1924 return nullptr;
1925
Reid Spenceree3c9912006-12-04 05:19:50 +00001926 // Look up the constant in the table first to ensure uniqueness
Benjamin Kramer324322b2013-03-07 20:53:34 +00001927 Constant *ArgVec[] = { LHS, RHS };
Reid Spencerb1537492006-12-24 18:42:29 +00001928 // Get the key type with both the opcode and predicate
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001929 const ConstantExprKeyType Key(Instruction::ICmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00001930
Chris Lattner229907c2011-07-18 04:54:35 +00001931 Type *ResultTy = Type::getInt1Ty(LHS->getContext());
1932 if (VectorType *VT = dyn_cast<VectorType>(LHS->getType()))
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00001933 ResultTy = VectorType::get(ResultTy, VT->getNumElements());
1934
Owen Anderson1584a292009-08-04 20:25:11 +00001935 LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl;
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00001936 return pImpl->ExprConstants.getOrCreate(ResultTy, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001937}
1938
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001939Constant *ConstantExpr::getFCmp(unsigned short pred, Constant *LHS,
1940 Constant *RHS, bool OnlyIfReduced) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001941 assert(LHS->getType() == RHS->getType());
Craig Topper45844762017-07-05 23:35:46 +00001942 assert(CmpInst::isFPPredicate((CmpInst::Predicate)pred) &&
1943 "Invalid FCmp Predicate");
Reid Spenceree3c9912006-12-04 05:19:50 +00001944
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001945 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001946 return FC; // Fold a few common cases...
1947
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001948 if (OnlyIfReduced)
1949 return nullptr;
1950
Reid Spenceree3c9912006-12-04 05:19:50 +00001951 // Look up the constant in the table first to ensure uniqueness
Benjamin Kramer324322b2013-03-07 20:53:34 +00001952 Constant *ArgVec[] = { LHS, RHS };
Reid Spencerb1537492006-12-24 18:42:29 +00001953 // Get the key type with both the opcode and predicate
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001954 const ConstantExprKeyType Key(Instruction::FCmp, ArgVec, pred);
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00001955
Chris Lattner229907c2011-07-18 04:54:35 +00001956 Type *ResultTy = Type::getInt1Ty(LHS->getContext());
1957 if (VectorType *VT = dyn_cast<VectorType>(LHS->getType()))
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00001958 ResultTy = VectorType::get(ResultTy, VT->getNumElements());
1959
Owen Anderson1584a292009-08-04 20:25:11 +00001960 LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl;
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00001961 return pImpl->ExprConstants.getOrCreate(ResultTy, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001962}
1963
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001964Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx,
1965 Type *OnlyIfReducedTy) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001966 assert(Val->getType()->isVectorTy() &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001967 "Tried to create extractelement operation on non-vector type!");
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001968 assert(Idx->getType()->isIntegerTy() &&
1969 "Extractelement index must be an integer type!");
Galina Kistanovafc259902012-07-13 01:25:27 +00001970
Chris Lattner887ecac2011-07-09 18:23:52 +00001971 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
Chris Lattner09660c92009-12-30 20:25:09 +00001972 return FC; // Fold a few common cases.
Galina Kistanovafc259902012-07-13 01:25:27 +00001973
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001974 Type *ReqTy = Val->getType()->getVectorElementType();
1975 if (OnlyIfReducedTy == ReqTy)
1976 return nullptr;
1977
Robert Bocchinoca27f032006-01-17 20:07:22 +00001978 // Look up the constant in the table first to ensure uniqueness
Benjamin Kramer324322b2013-03-07 20:53:34 +00001979 Constant *ArgVec[] = { Val, Idx };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001980 const ConstantExprKeyType Key(Instruction::ExtractElement, ArgVec);
Galina Kistanovafc259902012-07-13 01:25:27 +00001981
Chris Lattner887ecac2011-07-09 18:23:52 +00001982 LLVMContextImpl *pImpl = Val->getContext().pImpl;
Owen Anderson1584a292009-08-04 20:25:11 +00001983 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001984}
1985
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001986Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1987 Constant *Idx, Type *OnlyIfReducedTy) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001988 assert(Val->getType()->isVectorTy() &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001989 "Tried to create insertelement operation on non-vector type!");
Chris Lattner8326bd82012-01-26 00:42:34 +00001990 assert(Elt->getType() == Val->getType()->getVectorElementType() &&
1991 "Insertelement types must match!");
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001992 assert(Idx->getType()->isIntegerTy() &&
Reid Spencer2546b762007-01-26 07:37:34 +00001993 "Insertelement index must be i32 type!");
Robert Bocchinoca27f032006-01-17 20:07:22 +00001994
Chris Lattner887ecac2011-07-09 18:23:52 +00001995 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
1996 return FC; // Fold a few common cases.
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001997
1998 if (OnlyIfReducedTy == Val->getType())
1999 return nullptr;
2000
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002001 // Look up the constant in the table first to ensure uniqueness
Benjamin Kramer324322b2013-03-07 20:53:34 +00002002 Constant *ArgVec[] = { Val, Elt, Idx };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002003 const ConstantExprKeyType Key(Instruction::InsertElement, ArgVec);
Galina Kistanovafc259902012-07-13 01:25:27 +00002004
Chris Lattner887ecac2011-07-09 18:23:52 +00002005 LLVMContextImpl *pImpl = Val->getContext().pImpl;
2006 return pImpl->ExprConstants.getOrCreate(Val->getType(), Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002007}
2008
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002009Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
2010 Constant *Mask, Type *OnlyIfReducedTy) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002011 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
2012 "Invalid shuffle vector constant expr operands!");
Nate Begeman94aa38d2009-02-12 21:28:33 +00002013
Chris Lattner887ecac2011-07-09 18:23:52 +00002014 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
2015 return FC; // Fold a few common cases.
2016
Chris Lattner8326bd82012-01-26 00:42:34 +00002017 unsigned NElts = Mask->getType()->getVectorNumElements();
2018 Type *EltTy = V1->getType()->getVectorElementType();
Chris Lattner229907c2011-07-18 04:54:35 +00002019 Type *ShufTy = VectorType::get(EltTy, NElts);
Chris Lattner887ecac2011-07-09 18:23:52 +00002020
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002021 if (OnlyIfReducedTy == ShufTy)
2022 return nullptr;
2023
Chris Lattner887ecac2011-07-09 18:23:52 +00002024 // Look up the constant in the table first to ensure uniqueness
Benjamin Kramer324322b2013-03-07 20:53:34 +00002025 Constant *ArgVec[] = { V1, V2, Mask };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002026 const ConstantExprKeyType Key(Instruction::ShuffleVector, ArgVec);
Galina Kistanovafc259902012-07-13 01:25:27 +00002027
Chris Lattner887ecac2011-07-09 18:23:52 +00002028 LLVMContextImpl *pImpl = ShufTy->getContext().pImpl;
2029 return pImpl->ExprConstants.getOrCreate(ShufTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002030}
2031
Chris Lattner887ecac2011-07-09 18:23:52 +00002032Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002033 ArrayRef<unsigned> Idxs,
2034 Type *OnlyIfReducedTy) {
Hal Finkelb31366d2013-07-10 22:51:01 +00002035 assert(Agg->getType()->isFirstClassType() &&
2036 "Non-first-class type for constant insertvalue expression");
2037
Jay Foad57aa6362011-07-13 10:26:04 +00002038 assert(ExtractValueInst::getIndexedType(Agg->getType(),
2039 Idxs) == Val->getType() &&
Dan Gohman12fce772008-05-15 19:50:34 +00002040 "insertvalue indices invalid!");
Hal Finkelb31366d2013-07-10 22:51:01 +00002041 Type *ReqTy = Val->getType();
2042
2043 if (Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs))
2044 return FC;
2045
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002046 if (OnlyIfReducedTy == ReqTy)
2047 return nullptr;
2048
Hal Finkelb31366d2013-07-10 22:51:01 +00002049 Constant *ArgVec[] = { Agg, Val };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002050 const ConstantExprKeyType Key(Instruction::InsertValue, ArgVec, 0, 0, Idxs);
Hal Finkelb31366d2013-07-10 22:51:01 +00002051
2052 LLVMContextImpl *pImpl = Agg->getContext().pImpl;
2053 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Dan Gohman12fce772008-05-15 19:50:34 +00002054}
2055
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002056Constant *ConstantExpr::getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs,
2057 Type *OnlyIfReducedTy) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002058 assert(Agg->getType()->isFirstClassType() &&
Chris Lattner887ecac2011-07-09 18:23:52 +00002059 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002060
Chris Lattner229907c2011-07-18 04:54:35 +00002061 Type *ReqTy = ExtractValueInst::getIndexedType(Agg->getType(), Idxs);
Chandler Carruth9db56b82011-07-10 09:45:35 +00002062 (void)ReqTy;
Chris Lattner887ecac2011-07-09 18:23:52 +00002063 assert(ReqTy && "extractvalue indices invalid!");
Galina Kistanovafc259902012-07-13 01:25:27 +00002064
Dan Gohman0752bff2008-05-23 00:36:11 +00002065 assert(Agg->getType()->isFirstClassType() &&
2066 "Non-first-class type for constant extractvalue expression");
Hal Finkelb31366d2013-07-10 22:51:01 +00002067 if (Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs))
2068 return FC;
2069
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002070 if (OnlyIfReducedTy == ReqTy)
2071 return nullptr;
2072
Hal Finkelb31366d2013-07-10 22:51:01 +00002073 Constant *ArgVec[] = { Agg };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002074 const ConstantExprKeyType Key(Instruction::ExtractValue, ArgVec, 0, 0, Idxs);
Hal Finkelb31366d2013-07-10 22:51:01 +00002075
2076 LLVMContextImpl *pImpl = Agg->getContext().pImpl;
2077 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Dan Gohman12fce772008-05-15 19:50:34 +00002078}
2079
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002080Constant *ConstantExpr::getNeg(Constant *C, bool HasNUW, bool HasNSW) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002081 assert(C->getType()->isIntOrIntVectorTy() &&
Owen Anderson487375e2009-07-29 18:55:55 +00002082 "Cannot NEG a nonintegral value!");
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002083 return getSub(ConstantFP::getZeroValueForNegation(C->getType()),
2084 C, HasNUW, HasNSW);
Owen Anderson487375e2009-07-29 18:55:55 +00002085}
2086
Chris Lattnera676c0f2011-02-07 16:40:21 +00002087Constant *ConstantExpr::getFNeg(Constant *C) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002088 assert(C->getType()->isFPOrFPVectorTy() &&
Owen Anderson487375e2009-07-29 18:55:55 +00002089 "Cannot FNEG a non-floating-point value!");
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002090 return getFSub(ConstantFP::getZeroValueForNegation(C->getType()), C);
Owen Anderson487375e2009-07-29 18:55:55 +00002091}
2092
Chris Lattnera676c0f2011-02-07 16:40:21 +00002093Constant *ConstantExpr::getNot(Constant *C) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002094 assert(C->getType()->isIntOrIntVectorTy() &&
Owen Anderson487375e2009-07-29 18:55:55 +00002095 "Cannot NOT a nonintegral value!");
Owen Anderson5a1acd92009-07-31 20:28:14 +00002096 return get(Instruction::Xor, C, Constant::getAllOnesValue(C->getType()));
Owen Anderson487375e2009-07-29 18:55:55 +00002097}
2098
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002099Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2,
2100 bool HasNUW, bool HasNSW) {
2101 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2102 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
2103 return get(Instruction::Add, C1, C2, Flags);
Owen Anderson487375e2009-07-29 18:55:55 +00002104}
2105
Chris Lattnera676c0f2011-02-07 16:40:21 +00002106Constant *ConstantExpr::getFAdd(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002107 return get(Instruction::FAdd, C1, C2);
2108}
2109
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002110Constant *ConstantExpr::getSub(Constant *C1, Constant *C2,
2111 bool HasNUW, bool HasNSW) {
2112 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2113 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
2114 return get(Instruction::Sub, C1, C2, Flags);
Owen Anderson487375e2009-07-29 18:55:55 +00002115}
2116
Chris Lattnera676c0f2011-02-07 16:40:21 +00002117Constant *ConstantExpr::getFSub(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002118 return get(Instruction::FSub, C1, C2);
2119}
2120
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002121Constant *ConstantExpr::getMul(Constant *C1, Constant *C2,
2122 bool HasNUW, bool HasNSW) {
2123 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2124 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
2125 return get(Instruction::Mul, C1, C2, Flags);
Owen Anderson487375e2009-07-29 18:55:55 +00002126}
2127
Chris Lattnera676c0f2011-02-07 16:40:21 +00002128Constant *ConstantExpr::getFMul(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002129 return get(Instruction::FMul, C1, C2);
2130}
2131
Chris Lattner0d75eac2011-02-09 16:43:07 +00002132Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2, bool isExact) {
2133 return get(Instruction::UDiv, C1, C2,
2134 isExact ? PossiblyExactOperator::IsExact : 0);
Owen Anderson487375e2009-07-29 18:55:55 +00002135}
2136
Chris Lattner0d75eac2011-02-09 16:43:07 +00002137Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2, bool isExact) {
2138 return get(Instruction::SDiv, C1, C2,
2139 isExact ? PossiblyExactOperator::IsExact : 0);
Owen Anderson487375e2009-07-29 18:55:55 +00002140}
2141
Chris Lattnera676c0f2011-02-07 16:40:21 +00002142Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002143 return get(Instruction::FDiv, C1, C2);
2144}
2145
Chris Lattnera676c0f2011-02-07 16:40:21 +00002146Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002147 return get(Instruction::URem, C1, C2);
2148}
2149
Chris Lattnera676c0f2011-02-07 16:40:21 +00002150Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002151 return get(Instruction::SRem, C1, C2);
2152}
2153
Chris Lattnera676c0f2011-02-07 16:40:21 +00002154Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002155 return get(Instruction::FRem, C1, C2);
2156}
2157
Chris Lattnera676c0f2011-02-07 16:40:21 +00002158Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002159 return get(Instruction::And, C1, C2);
2160}
2161
Chris Lattnera676c0f2011-02-07 16:40:21 +00002162Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002163 return get(Instruction::Or, C1, C2);
2164}
2165
Chris Lattnera676c0f2011-02-07 16:40:21 +00002166Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002167 return get(Instruction::Xor, C1, C2);
2168}
2169
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002170Constant *ConstantExpr::getShl(Constant *C1, Constant *C2,
2171 bool HasNUW, bool HasNSW) {
2172 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2173 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
2174 return get(Instruction::Shl, C1, C2, Flags);
Owen Anderson487375e2009-07-29 18:55:55 +00002175}
2176
Chris Lattner0d75eac2011-02-09 16:43:07 +00002177Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2, bool isExact) {
2178 return get(Instruction::LShr, C1, C2,
2179 isExact ? PossiblyExactOperator::IsExact : 0);
Owen Anderson487375e2009-07-29 18:55:55 +00002180}
2181
Chris Lattner0d75eac2011-02-09 16:43:07 +00002182Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2, bool isExact) {
2183 return get(Instruction::AShr, C1, C2,
2184 isExact ? PossiblyExactOperator::IsExact : 0);
Owen Anderson487375e2009-07-29 18:55:55 +00002185}
2186
Duncan Sandsd7aeefe2012-06-12 14:33:56 +00002187Constant *ConstantExpr::getBinOpIdentity(unsigned Opcode, Type *Ty) {
2188 switch (Opcode) {
2189 default:
Duncan Sands318a89d2012-06-13 09:42:13 +00002190 // Doesn't have an identity.
Craig Topperc6207612014-04-09 06:08:46 +00002191 return nullptr;
Duncan Sands318a89d2012-06-13 09:42:13 +00002192
Duncan Sandsd7aeefe2012-06-12 14:33:56 +00002193 case Instruction::Add:
2194 case Instruction::Or:
2195 case Instruction::Xor:
2196 return Constant::getNullValue(Ty);
2197
2198 case Instruction::Mul:
2199 return ConstantInt::get(Ty, 1);
2200
2201 case Instruction::And:
2202 return Constant::getAllOnesValue(Ty);
2203 }
2204}
2205
Duncan Sands318a89d2012-06-13 09:42:13 +00002206Constant *ConstantExpr::getBinOpAbsorber(unsigned Opcode, Type *Ty) {
2207 switch (Opcode) {
2208 default:
2209 // Doesn't have an absorber.
Craig Topperc6207612014-04-09 06:08:46 +00002210 return nullptr;
Duncan Sands318a89d2012-06-13 09:42:13 +00002211
2212 case Instruction::Or:
2213 return Constant::getAllOnesValue(Ty);
2214
2215 case Instruction::And:
2216 case Instruction::Mul:
2217 return Constant::getNullValue(Ty);
2218 }
2219}
2220
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00002221/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00002222void ConstantExpr::destroyConstantImpl() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002223 getType()->getContext().pImpl->ExprConstants.remove(this);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002224}
2225
Chris Lattner3cd8c562002-07-30 18:54:25 +00002226const char *ConstantExpr::getOpcodeName() const {
2227 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002228}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00002229
David Blaikie60310f22015-05-08 00:42:26 +00002230GetElementPtrConstantExpr::GetElementPtrConstantExpr(
2231 Type *SrcElementTy, Constant *C, ArrayRef<Constant *> IdxList, Type *DestTy)
2232 : ConstantExpr(DestTy, Instruction::GetElementPtr,
2233 OperandTraits<GetElementPtrConstantExpr>::op_end(this) -
2234 (IdxList.size() + 1),
2235 IdxList.size() + 1),
Eduard Burtescu19eb0312016-01-19 17:28:00 +00002236 SrcElementTy(SrcElementTy),
2237 ResElementTy(GetElementPtrInst::getIndexedType(SrcElementTy, IdxList)) {
Pete Coopereb31b682015-05-21 22:48:54 +00002238 Op<0>() = C;
Pete Cooper74510a42015-06-12 17:48:05 +00002239 Use *OperandList = getOperandList();
Chris Lattnera3b94ba2010-03-30 20:48:48 +00002240 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
2241 OperandList[i+1] = IdxList[i];
2242}
2243
David Blaikie60310f22015-05-08 00:42:26 +00002244Type *GetElementPtrConstantExpr::getSourceElementType() const {
2245 return SrcElementTy;
2246}
2247
Eduard Burtescu19eb0312016-01-19 17:28:00 +00002248Type *GetElementPtrConstantExpr::getResultElementType() const {
2249 return ResElementTy;
2250}
2251
Chris Lattner3756b912012-01-23 22:57:10 +00002252//===----------------------------------------------------------------------===//
2253// ConstantData* implementations
2254
Chris Lattnere4f3f102012-01-24 04:43:41 +00002255Type *ConstantDataSequential::getElementType() const {
2256 return getType()->getElementType();
2257}
2258
Chris Lattner5d4497b2012-01-24 09:31:43 +00002259StringRef ConstantDataSequential::getRawDataValues() const {
Chris Lattner00245f42012-01-24 13:41:11 +00002260 return StringRef(DataElements, getNumElements()*getElementByteSize());
Chris Lattner5d4497b2012-01-24 09:31:43 +00002261}
2262
Craig Toppere3dcce92015-08-01 22:20:21 +00002263bool ConstantDataSequential::isElementTypeCompatible(Type *Ty) {
Justin Bogner0ebc8602015-12-08 03:01:16 +00002264 if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) return true;
Craig Toppere3dcce92015-08-01 22:20:21 +00002265 if (auto *IT = dyn_cast<IntegerType>(Ty)) {
Chris Lattnere4f3f102012-01-24 04:43:41 +00002266 switch (IT->getBitWidth()) {
2267 case 8:
2268 case 16:
2269 case 32:
2270 case 64:
2271 return true;
2272 default: break;
2273 }
2274 }
2275 return false;
2276}
2277
Chris Lattner00245f42012-01-24 13:41:11 +00002278unsigned ConstantDataSequential::getNumElements() const {
Chris Lattner8a3df542012-01-25 01:32:59 +00002279 if (ArrayType *AT = dyn_cast<ArrayType>(getType()))
2280 return AT->getNumElements();
Chris Lattner8326bd82012-01-26 00:42:34 +00002281 return getType()->getVectorNumElements();
Chris Lattner00245f42012-01-24 13:41:11 +00002282}
2283
2284
Chris Lattnere4f3f102012-01-24 04:43:41 +00002285uint64_t ConstantDataSequential::getElementByteSize() const {
2286 return getElementType()->getPrimitiveSizeInBits()/8;
2287}
2288
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00002289/// Return the start of the specified element.
Chris Lattnere4f3f102012-01-24 04:43:41 +00002290const char *ConstantDataSequential::getElementPointer(unsigned Elt) const {
Chris Lattner00245f42012-01-24 13:41:11 +00002291 assert(Elt < getNumElements() && "Invalid Elt");
Chris Lattnere4f3f102012-01-24 04:43:41 +00002292 return DataElements+Elt*getElementByteSize();
2293}
2294
2295
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00002296/// Return true if the array is empty or all zeros.
Chris Lattner3756b912012-01-23 22:57:10 +00002297static bool isAllZeros(StringRef Arr) {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +00002298 for (char I : Arr)
2299 if (I != 0)
Chris Lattner3756b912012-01-23 22:57:10 +00002300 return false;
2301 return true;
2302}
Chris Lattner030af792012-01-24 05:42:11 +00002303
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00002304/// This is the underlying implementation of all of the
Chris Lattner3756b912012-01-23 22:57:10 +00002305/// ConstantDataSequential::get methods. They all thunk down to here, providing
Chris Lattnerf06039b2012-01-30 18:19:30 +00002306/// the correct element type. We take the bytes in as a StringRef because
Chris Lattner3756b912012-01-23 22:57:10 +00002307/// we *want* an underlying "char*" to avoid TBAA type punning violations.
2308Constant *ConstantDataSequential::getImpl(StringRef Elements, Type *Ty) {
Chris Lattner8326bd82012-01-26 00:42:34 +00002309 assert(isElementTypeCompatible(Ty->getSequentialElementType()));
Chris Lattner139822f2012-01-24 14:17:05 +00002310 // If the elements are all zero or there are no elements, return a CAZ, which
2311 // is more dense and canonical.
Chris Lattner3756b912012-01-23 22:57:10 +00002312 if (isAllZeros(Elements))
2313 return ConstantAggregateZero::get(Ty);
2314
2315 // Do a lookup to see if we have already formed one of these.
David Blaikie5106ce72014-11-19 05:49:42 +00002316 auto &Slot =
2317 *Ty->getContext()
2318 .pImpl->CDSConstants.insert(std::make_pair(Elements, nullptr))
2319 .first;
Galina Kistanovafc259902012-07-13 01:25:27 +00002320
Chris Lattner3756b912012-01-23 22:57:10 +00002321 // The bucket can point to a linked list of different CDS's that have the same
2322 // body but different types. For example, 0,0,0,1 could be a 4 element array
2323 // of i8, or a 1-element array of i32. They'll both end up in the same
2324 /// StringMap bucket, linked up by their Next pointers. Walk the list.
David Blaikie5106ce72014-11-19 05:49:42 +00002325 ConstantDataSequential **Entry = &Slot.second;
Craig Topperc6207612014-04-09 06:08:46 +00002326 for (ConstantDataSequential *Node = *Entry; Node;
Chris Lattner3756b912012-01-23 22:57:10 +00002327 Entry = &Node->Next, Node = *Entry)
2328 if (Node->getType() == Ty)
2329 return Node;
Galina Kistanovafc259902012-07-13 01:25:27 +00002330
Chris Lattner3756b912012-01-23 22:57:10 +00002331 // Okay, we didn't get a hit. Create a node of the right class, link it in,
2332 // and return it.
2333 if (isa<ArrayType>(Ty))
David Blaikie5106ce72014-11-19 05:49:42 +00002334 return *Entry = new ConstantDataArray(Ty, Slot.first().data());
Chris Lattner3756b912012-01-23 22:57:10 +00002335
2336 assert(isa<VectorType>(Ty));
David Blaikie5106ce72014-11-19 05:49:42 +00002337 return *Entry = new ConstantDataVector(Ty, Slot.first().data());
Chris Lattner3756b912012-01-23 22:57:10 +00002338}
2339
Pete Cooper86dd4cf2015-06-23 21:55:11 +00002340void ConstantDataSequential::destroyConstantImpl() {
Chris Lattner3756b912012-01-23 22:57:10 +00002341 // Remove the constant from the StringMap.
2342 StringMap<ConstantDataSequential*> &CDSConstants =
2343 getType()->getContext().pImpl->CDSConstants;
Galina Kistanovafc259902012-07-13 01:25:27 +00002344
Chris Lattner3756b912012-01-23 22:57:10 +00002345 StringMap<ConstantDataSequential*>::iterator Slot =
Chris Lattner5d4497b2012-01-24 09:31:43 +00002346 CDSConstants.find(getRawDataValues());
Chris Lattner3756b912012-01-23 22:57:10 +00002347
2348 assert(Slot != CDSConstants.end() && "CDS not found in uniquing table");
2349
2350 ConstantDataSequential **Entry = &Slot->getValue();
2351
2352 // Remove the entry from the hash table.
Craig Topperc6207612014-04-09 06:08:46 +00002353 if (!(*Entry)->Next) {
Chris Lattner3756b912012-01-23 22:57:10 +00002354 // If there is only one value in the bucket (common case) it must be this
2355 // entry, and removing the entry should remove the bucket completely.
2356 assert((*Entry) == this && "Hash mismatch in ConstantDataSequential");
2357 getContext().pImpl->CDSConstants.erase(Slot);
2358 } else {
2359 // Otherwise, there are multiple entries linked off the bucket, unlink the
2360 // node we care about but keep the bucket around.
2361 for (ConstantDataSequential *Node = *Entry; ;
2362 Entry = &Node->Next, Node = *Entry) {
2363 assert(Node && "Didn't find entry in its uniquing hash table!");
2364 // If we found our entry, unlink it from the list and we're done.
2365 if (Node == this) {
2366 *Entry = Node->Next;
2367 break;
2368 }
2369 }
2370 }
Galina Kistanovafc259902012-07-13 01:25:27 +00002371
Chris Lattner3756b912012-01-23 22:57:10 +00002372 // If we were part of a list, make sure that we don't delete the list that is
2373 // still owned by the uniquing map.
Craig Topperc6207612014-04-09 06:08:46 +00002374 Next = nullptr;
Chris Lattner3756b912012-01-23 22:57:10 +00002375}
2376
2377/// get() constructors - Return a constant with array type with an element
2378/// count and element type matching the ArrayRef passed in. Note that this
2379/// can return a ConstantAggregateZero object.
Chris Lattner20683932012-01-24 14:04:40 +00002380Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint8_t> Elts) {
Chris Lattner3756b912012-01-23 22:57:10 +00002381 Type *Ty = ArrayType::get(Type::getInt8Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002382 const char *Data = reinterpret_cast<const char *>(Elts.data());
2383 return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*1), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002384}
Chris Lattner20683932012-01-24 14:04:40 +00002385Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint16_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002386 Type *Ty = ArrayType::get(Type::getInt16Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002387 const char *Data = reinterpret_cast<const char *>(Elts.data());
2388 return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*2), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002389}
Chris Lattner20683932012-01-24 14:04:40 +00002390Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint32_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002391 Type *Ty = ArrayType::get(Type::getInt32Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002392 const char *Data = reinterpret_cast<const char *>(Elts.data());
2393 return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*4), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002394}
Chris Lattner20683932012-01-24 14:04:40 +00002395Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint64_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002396 Type *Ty = ArrayType::get(Type::getInt64Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002397 const char *Data = reinterpret_cast<const char *>(Elts.data());
2398 return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*8), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002399}
Chris Lattner20683932012-01-24 14:04:40 +00002400Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<float> Elts) {
Chris Lattner3756b912012-01-23 22:57:10 +00002401 Type *Ty = ArrayType::get(Type::getFloatTy(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002402 const char *Data = reinterpret_cast<const char *>(Elts.data());
2403 return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*4), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002404}
Chris Lattner20683932012-01-24 14:04:40 +00002405Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<double> Elts) {
Chris Lattner3756b912012-01-23 22:57:10 +00002406 Type *Ty = ArrayType::get(Type::getDoubleTy(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002407 const char *Data = reinterpret_cast<const char *>(Elts.data());
Rafael Espindola8c97e192015-02-19 16:08:20 +00002408 return getImpl(StringRef(const_cast<char *>(Data), Elts.size() * 8), Ty);
2409}
2410
2411/// getFP() constructors - Return a constant with array type with an element
2412/// count and element type of float with precision matching the number of
2413/// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits,
2414/// double for 64bits) Note that this can return a ConstantAggregateZero
2415/// object.
2416Constant *ConstantDataArray::getFP(LLVMContext &Context,
2417 ArrayRef<uint16_t> Elts) {
Justin Bognerb7389d6712015-12-09 21:21:07 +00002418 Type *Ty = ArrayType::get(Type::getHalfTy(Context), Elts.size());
Rafael Espindola8c97e192015-02-19 16:08:20 +00002419 const char *Data = reinterpret_cast<const char *>(Elts.data());
2420 return getImpl(StringRef(const_cast<char *>(Data), Elts.size() * 2), Ty);
2421}
2422Constant *ConstantDataArray::getFP(LLVMContext &Context,
2423 ArrayRef<uint32_t> Elts) {
2424 Type *Ty = ArrayType::get(Type::getFloatTy(Context), Elts.size());
2425 const char *Data = reinterpret_cast<const char *>(Elts.data());
2426 return getImpl(StringRef(const_cast<char *>(Data), Elts.size() * 4), Ty);
2427}
2428Constant *ConstantDataArray::getFP(LLVMContext &Context,
2429 ArrayRef<uint64_t> Elts) {
2430 Type *Ty = ArrayType::get(Type::getDoubleTy(Context), Elts.size());
2431 const char *Data = reinterpret_cast<const char *>(Elts.data());
2432 return getImpl(StringRef(const_cast<char *>(Data), Elts.size() * 8), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002433}
2434
Chris Lattner20683932012-01-24 14:04:40 +00002435Constant *ConstantDataArray::getString(LLVMContext &Context,
2436 StringRef Str, bool AddNull) {
Galina Kistanovafc259902012-07-13 01:25:27 +00002437 if (!AddNull) {
2438 const uint8_t *Data = reinterpret_cast<const uint8_t *>(Str.data());
Craig Toppere1d12942014-08-27 05:25:25 +00002439 return get(Context, makeArrayRef(const_cast<uint8_t *>(Data),
Galina Kistanovafc259902012-07-13 01:25:27 +00002440 Str.size()));
2441 }
2442
Chris Lattner20683932012-01-24 14:04:40 +00002443 SmallVector<uint8_t, 64> ElementVals;
2444 ElementVals.append(Str.begin(), Str.end());
2445 ElementVals.push_back(0);
2446 return get(Context, ElementVals);
2447}
Chris Lattner3756b912012-01-23 22:57:10 +00002448
2449/// get() constructors - Return a constant with vector type with an element
2450/// count and element type matching the ArrayRef passed in. Note that this
2451/// can return a ConstantAggregateZero object.
Chris Lattner20683932012-01-24 14:04:40 +00002452Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint8_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002453 Type *Ty = VectorType::get(Type::getInt8Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002454 const char *Data = reinterpret_cast<const char *>(Elts.data());
2455 return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*1), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002456}
Chris Lattner20683932012-01-24 14:04:40 +00002457Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint16_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002458 Type *Ty = VectorType::get(Type::getInt16Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002459 const char *Data = reinterpret_cast<const char *>(Elts.data());
2460 return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*2), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002461}
Chris Lattner20683932012-01-24 14:04:40 +00002462Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint32_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002463 Type *Ty = VectorType::get(Type::getInt32Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002464 const char *Data = reinterpret_cast<const char *>(Elts.data());
2465 return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*4), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002466}
Chris Lattner20683932012-01-24 14:04:40 +00002467Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint64_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002468 Type *Ty = VectorType::get(Type::getInt64Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002469 const char *Data = reinterpret_cast<const char *>(Elts.data());
2470 return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*8), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002471}
Chris Lattner20683932012-01-24 14:04:40 +00002472Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<float> Elts) {
Chris Lattner3756b912012-01-23 22:57:10 +00002473 Type *Ty = VectorType::get(Type::getFloatTy(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002474 const char *Data = reinterpret_cast<const char *>(Elts.data());
2475 return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*4), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002476}
Chris Lattner20683932012-01-24 14:04:40 +00002477Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<double> Elts) {
Chris Lattner3756b912012-01-23 22:57:10 +00002478 Type *Ty = VectorType::get(Type::getDoubleTy(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002479 const char *Data = reinterpret_cast<const char *>(Elts.data());
Rafael Espindola8c97e192015-02-19 16:08:20 +00002480 return getImpl(StringRef(const_cast<char *>(Data), Elts.size() * 8), Ty);
2481}
2482
2483/// getFP() constructors - Return a constant with vector type with an element
2484/// count and element type of float with the precision matching the number of
2485/// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits,
2486/// double for 64bits) Note that this can return a ConstantAggregateZero
2487/// object.
2488Constant *ConstantDataVector::getFP(LLVMContext &Context,
2489 ArrayRef<uint16_t> Elts) {
2490 Type *Ty = VectorType::get(Type::getHalfTy(Context), Elts.size());
2491 const char *Data = reinterpret_cast<const char *>(Elts.data());
2492 return getImpl(StringRef(const_cast<char *>(Data), Elts.size() * 2), Ty);
2493}
2494Constant *ConstantDataVector::getFP(LLVMContext &Context,
2495 ArrayRef<uint32_t> Elts) {
2496 Type *Ty = VectorType::get(Type::getFloatTy(Context), Elts.size());
2497 const char *Data = reinterpret_cast<const char *>(Elts.data());
2498 return getImpl(StringRef(const_cast<char *>(Data), Elts.size() * 4), Ty);
2499}
2500Constant *ConstantDataVector::getFP(LLVMContext &Context,
2501 ArrayRef<uint64_t> Elts) {
2502 Type *Ty = VectorType::get(Type::getDoubleTy(Context), Elts.size());
2503 const char *Data = reinterpret_cast<const char *>(Elts.data());
2504 return getImpl(StringRef(const_cast<char *>(Data), Elts.size() * 8), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002505}
2506
Chris Lattnere9eed292012-01-25 05:19:54 +00002507Constant *ConstantDataVector::getSplat(unsigned NumElts, Constant *V) {
2508 assert(isElementTypeCompatible(V->getType()) &&
2509 "Element type not compatible with ConstantData");
2510 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
2511 if (CI->getType()->isIntegerTy(8)) {
2512 SmallVector<uint8_t, 16> Elts(NumElts, CI->getZExtValue());
2513 return get(V->getContext(), Elts);
2514 }
2515 if (CI->getType()->isIntegerTy(16)) {
2516 SmallVector<uint16_t, 16> Elts(NumElts, CI->getZExtValue());
2517 return get(V->getContext(), Elts);
2518 }
2519 if (CI->getType()->isIntegerTy(32)) {
2520 SmallVector<uint32_t, 16> Elts(NumElts, CI->getZExtValue());
2521 return get(V->getContext(), Elts);
2522 }
2523 assert(CI->getType()->isIntegerTy(64) && "Unsupported ConstantData type");
2524 SmallVector<uint64_t, 16> Elts(NumElts, CI->getZExtValue());
2525 return get(V->getContext(), Elts);
2526 }
2527
Chris Lattner978fe0c2012-01-30 06:21:21 +00002528 if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
Justin Bogner0ebc8602015-12-08 03:01:16 +00002529 if (CFP->getType()->isHalfTy()) {
2530 SmallVector<uint16_t, 16> Elts(
2531 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
2532 return getFP(V->getContext(), Elts);
2533 }
Chris Lattner978fe0c2012-01-30 06:21:21 +00002534 if (CFP->getType()->isFloatTy()) {
Rafael Espindola8c97e192015-02-19 16:08:20 +00002535 SmallVector<uint32_t, 16> Elts(
2536 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
2537 return getFP(V->getContext(), Elts);
Chris Lattner978fe0c2012-01-30 06:21:21 +00002538 }
2539 if (CFP->getType()->isDoubleTy()) {
Rafael Espindola8c97e192015-02-19 16:08:20 +00002540 SmallVector<uint64_t, 16> Elts(
2541 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
2542 return getFP(V->getContext(), Elts);
Chris Lattner978fe0c2012-01-30 06:21:21 +00002543 }
Chris Lattnere9eed292012-01-25 05:19:54 +00002544 }
Chris Lattner978fe0c2012-01-30 06:21:21 +00002545 return ConstantVector::getSplat(NumElts, V);
Chris Lattnere9eed292012-01-25 05:19:54 +00002546}
2547
2548
Chris Lattnere4f3f102012-01-24 04:43:41 +00002549uint64_t ConstantDataSequential::getElementAsInteger(unsigned Elt) const {
2550 assert(isa<IntegerType>(getElementType()) &&
2551 "Accessor can only be used when element is an integer");
2552 const char *EltPtr = getElementPointer(Elt);
Galina Kistanovafc259902012-07-13 01:25:27 +00002553
Chris Lattnere4f3f102012-01-24 04:43:41 +00002554 // The data is stored in host byte order, make sure to cast back to the right
2555 // type to load with the right endianness.
Chris Lattner8326bd82012-01-26 00:42:34 +00002556 switch (getElementType()->getIntegerBitWidth()) {
Craig Topperc514b542012-02-05 22:14:15 +00002557 default: llvm_unreachable("Invalid bitwidth for CDS");
Galina Kistanovafc259902012-07-13 01:25:27 +00002558 case 8:
2559 return *const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(EltPtr));
2560 case 16:
2561 return *const_cast<uint16_t *>(reinterpret_cast<const uint16_t *>(EltPtr));
2562 case 32:
2563 return *const_cast<uint32_t *>(reinterpret_cast<const uint32_t *>(EltPtr));
2564 case 64:
2565 return *const_cast<uint64_t *>(reinterpret_cast<const uint64_t *>(EltPtr));
Chris Lattnere4f3f102012-01-24 04:43:41 +00002566 }
2567}
2568
Chris Lattnere4f3f102012-01-24 04:43:41 +00002569APFloat ConstantDataSequential::getElementAsAPFloat(unsigned Elt) const {
2570 const char *EltPtr = getElementPointer(Elt);
2571
2572 switch (getElementType()->getTypeID()) {
Nick Lewyckyff509622012-01-25 03:20:12 +00002573 default:
Craig Topperc514b542012-02-05 22:14:15 +00002574 llvm_unreachable("Accessor can only be used when element is float/double!");
Justin Bogner0ebc8602015-12-08 03:01:16 +00002575 case Type::HalfTyID: {
2576 auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr);
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002577 return APFloat(APFloat::IEEEhalf(), APInt(16, EltVal));
Justin Bogner0ebc8602015-12-08 03:01:16 +00002578 }
Benjamin Kramer7af984b2015-02-20 15:11:55 +00002579 case Type::FloatTyID: {
2580 auto EltVal = *reinterpret_cast<const uint32_t *>(EltPtr);
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002581 return APFloat(APFloat::IEEEsingle(), APInt(32, EltVal));
Benjamin Kramer7af984b2015-02-20 15:11:55 +00002582 }
2583 case Type::DoubleTyID: {
2584 auto EltVal = *reinterpret_cast<const uint64_t *>(EltPtr);
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002585 return APFloat(APFloat::IEEEdouble(), APInt(64, EltVal));
Chris Lattnere4f3f102012-01-24 04:43:41 +00002586 }
Benjamin Kramer7af984b2015-02-20 15:11:55 +00002587 }
Chris Lattnere4f3f102012-01-24 04:43:41 +00002588}
2589
Chris Lattnere4f3f102012-01-24 04:43:41 +00002590float ConstantDataSequential::getElementAsFloat(unsigned Elt) const {
2591 assert(getElementType()->isFloatTy() &&
2592 "Accessor can only be used when element is a 'float'");
Galina Kistanovafc259902012-07-13 01:25:27 +00002593 const float *EltPtr = reinterpret_cast<const float *>(getElementPointer(Elt));
2594 return *const_cast<float *>(EltPtr);
Chris Lattnere4f3f102012-01-24 04:43:41 +00002595}
2596
Chris Lattnere4f3f102012-01-24 04:43:41 +00002597double ConstantDataSequential::getElementAsDouble(unsigned Elt) const {
2598 assert(getElementType()->isDoubleTy() &&
2599 "Accessor can only be used when element is a 'float'");
Galina Kistanovafc259902012-07-13 01:25:27 +00002600 const double *EltPtr =
2601 reinterpret_cast<const double *>(getElementPointer(Elt));
2602 return *const_cast<double *>(EltPtr);
Chris Lattnere4f3f102012-01-24 04:43:41 +00002603}
2604
Chris Lattnere4f3f102012-01-24 04:43:41 +00002605Constant *ConstantDataSequential::getElementAsConstant(unsigned Elt) const {
Justin Bogner0ebc8602015-12-08 03:01:16 +00002606 if (getElementType()->isHalfTy() || getElementType()->isFloatTy() ||
2607 getElementType()->isDoubleTy())
Chris Lattnere4f3f102012-01-24 04:43:41 +00002608 return ConstantFP::get(getContext(), getElementAsAPFloat(Elt));
Galina Kistanovafc259902012-07-13 01:25:27 +00002609
Chris Lattnere4f3f102012-01-24 04:43:41 +00002610 return ConstantInt::get(getElementType(), getElementAsInteger(Elt));
2611}
2612
Matthias Braun50ec0b52017-05-19 22:37:09 +00002613bool ConstantDataSequential::isString(unsigned CharSize) const {
2614 return isa<ArrayType>(getType()) && getElementType()->isIntegerTy(CharSize);
Chris Lattner5dd4d872012-01-24 09:01:07 +00002615}
Chris Lattner3756b912012-01-23 22:57:10 +00002616
Chris Lattner5dd4d872012-01-24 09:01:07 +00002617bool ConstantDataSequential::isCString() const {
2618 if (!isString())
2619 return false;
Galina Kistanovafc259902012-07-13 01:25:27 +00002620
Chris Lattner5dd4d872012-01-24 09:01:07 +00002621 StringRef Str = getAsString();
Galina Kistanovafc259902012-07-13 01:25:27 +00002622
Chris Lattner5dd4d872012-01-24 09:01:07 +00002623 // The last value must be nul.
2624 if (Str.back() != 0) return false;
Galina Kistanovafc259902012-07-13 01:25:27 +00002625
Chris Lattner5dd4d872012-01-24 09:01:07 +00002626 // Other elements must be non-nul.
2627 return Str.drop_back().find(0) == StringRef::npos;
2628}
Chris Lattner3756b912012-01-23 22:57:10 +00002629
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002630Constant *ConstantDataVector::getSplatValue() const {
2631 const char *Base = getRawDataValues().data();
Galina Kistanovafc259902012-07-13 01:25:27 +00002632
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002633 // Compare elements 1+ to the 0'th element.
2634 unsigned EltSize = getElementByteSize();
2635 for (unsigned i = 1, e = getNumElements(); i != e; ++i)
2636 if (memcmp(Base, Base+i*EltSize, EltSize))
Craig Topperc6207612014-04-09 06:08:46 +00002637 return nullptr;
Galina Kistanovafc259902012-07-13 01:25:27 +00002638
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002639 // If they're all the same, return the 0th one as a representative.
2640 return getElementAsConstant(0);
2641}
Chris Lattnera3b94ba2010-03-30 20:48:48 +00002642
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002643//===----------------------------------------------------------------------===//
Pete Cooper5815b1f2015-06-24 18:55:24 +00002644// handleOperandChange implementations
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002645
Pete Cooper5815b1f2015-06-24 18:55:24 +00002646/// Update this constant array to change uses of
Chris Lattner913849b2007-08-21 00:55:23 +00002647/// 'From' to be uses of 'To'. This must update the uniquing data structures
2648/// etc.
2649///
2650/// Note that we intentionally replace all uses of From with To here. Consider
2651/// a large array that uses 'From' 1000 times. By handling this case all here,
Pete Cooper5815b1f2015-06-24 18:55:24 +00002652/// ConstantArray::handleOperandChange is only invoked once, and that
Chris Lattner913849b2007-08-21 00:55:23 +00002653/// single invocation handles all 1000 uses. Handling them one at a time would
2654/// work, but would be really slow because it would have to unique each updated
2655/// array instance.
Chris Lattner31b132c2009-10-28 00:01:44 +00002656///
Mehdi Amini8914d292016-02-10 22:47:15 +00002657void Constant::handleOperandChange(Value *From, Value *To) {
Pete Cooper5815b1f2015-06-24 18:55:24 +00002658 Value *Replacement = nullptr;
2659 switch (getValueID()) {
2660 default:
2661 llvm_unreachable("Not a constant!");
2662#define HANDLE_CONSTANT(Name) \
2663 case Value::Name##Val: \
Mehdi Amini8914d292016-02-10 22:47:15 +00002664 Replacement = cast<Name>(this)->handleOperandChangeImpl(From, To); \
Pete Cooper5815b1f2015-06-24 18:55:24 +00002665 break;
2666#include "llvm/IR/Value.def"
2667 }
2668
2669 // If handleOperandChangeImpl returned nullptr, then it handled
2670 // replacing itself and we don't want to delete or replace anything else here.
2671 if (!Replacement)
2672 return;
2673
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002674 // I do need to replace this with an existing value.
2675 assert(Replacement != this && "I didn't contain From!");
2676
2677 // Everyone using this now uses the replacement.
2678 replaceAllUsesWith(Replacement);
2679
2680 // Delete the old constant!
2681 destroyConstant();
2682}
2683
Mehdi Amini8914d292016-02-10 22:47:15 +00002684Value *ConstantArray::handleOperandChangeImpl(Value *From, Value *To) {
Owen Andersonc2c79322009-07-28 18:32:17 +00002685 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2686 Constant *ToC = cast<Constant>(To);
2687
Talin46e9b442012-02-05 20:54:10 +00002688 SmallVector<Constant*, 8> Values;
Owen Andersonc2c79322009-07-28 18:32:17 +00002689 Values.reserve(getNumOperands()); // Build replacement array.
2690
Galina Kistanovafc259902012-07-13 01:25:27 +00002691 // Fill values with the modified operands of the constant array. Also,
Owen Andersonc2c79322009-07-28 18:32:17 +00002692 // compute whether this turns into an all-zeros array.
Owen Andersonc2c79322009-07-28 18:32:17 +00002693 unsigned NumUpdated = 0;
Galina Kistanovafc259902012-07-13 01:25:27 +00002694
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002695 // Keep track of whether all the values in the array are "ToC".
2696 bool AllSame = true;
Pete Cooper74510a42015-06-12 17:48:05 +00002697 Use *OperandList = getOperandList();
Mehdi Amini8914d292016-02-10 22:47:15 +00002698 unsigned OperandNo = 0;
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002699 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2700 Constant *Val = cast<Constant>(O->get());
2701 if (Val == From) {
Mehdi Amini8914d292016-02-10 22:47:15 +00002702 OperandNo = (O - OperandList);
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002703 Val = ToC;
2704 ++NumUpdated;
Owen Andersonc2c79322009-07-28 18:32:17 +00002705 }
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002706 Values.push_back(Val);
Talin46e9b442012-02-05 20:54:10 +00002707 AllSame &= Val == ToC;
Owen Andersonc2c79322009-07-28 18:32:17 +00002708 }
Galina Kistanovafc259902012-07-13 01:25:27 +00002709
Pete Cooper5815b1f2015-06-24 18:55:24 +00002710 if (AllSame && ToC->isNullValue())
2711 return ConstantAggregateZero::get(getType());
2712
2713 if (AllSame && isa<UndefValue>(ToC))
2714 return UndefValue::get(getType());
Aaron Ballmane4b91dc2014-08-19 14:59:02 +00002715
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002716 // Check for any other type of constant-folding.
Pete Cooper5815b1f2015-06-24 18:55:24 +00002717 if (Constant *C = getImpl(getType(), Values))
2718 return C;
Aaron Ballmane4b91dc2014-08-19 14:59:02 +00002719
Duncan P. N. Exon Smith909620a2014-08-19 19:13:30 +00002720 // Update to the new value.
Pete Cooper5815b1f2015-06-24 18:55:24 +00002721 return getContext().pImpl->ArrayConstants.replaceOperandsInPlace(
Mehdi Amini8914d292016-02-10 22:47:15 +00002722 Values, this, From, ToC, NumUpdated, OperandNo);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002723}
2724
Mehdi Amini8914d292016-02-10 22:47:15 +00002725Value *ConstantStruct::handleOperandChangeImpl(Value *From, Value *To) {
Owen Anderson45308b52009-07-27 22:29:26 +00002726 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2727 Constant *ToC = cast<Constant>(To);
2728
Pete Cooper74510a42015-06-12 17:48:05 +00002729 Use *OperandList = getOperandList();
Owen Anderson45308b52009-07-27 22:29:26 +00002730
Talin46e9b442012-02-05 20:54:10 +00002731 SmallVector<Constant*, 8> Values;
Owen Anderson45308b52009-07-27 22:29:26 +00002732 Values.reserve(getNumOperands()); // Build replacement struct.
Galina Kistanovafc259902012-07-13 01:25:27 +00002733
2734 // Fill values with the modified operands of the constant struct. Also,
Owen Anderson45308b52009-07-27 22:29:26 +00002735 // compute whether this turns into an all-zeros struct.
Mehdi Amini8914d292016-02-10 22:47:15 +00002736 unsigned NumUpdated = 0;
2737 bool AllSame = true;
2738 unsigned OperandNo = 0;
2739 for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O) {
2740 Constant *Val = cast<Constant>(O->get());
2741 if (Val == From) {
2742 OperandNo = (O - OperandList);
2743 Val = ToC;
2744 ++NumUpdated;
Owen Anderson45308b52009-07-27 22:29:26 +00002745 }
Mehdi Amini8914d292016-02-10 22:47:15 +00002746 Values.push_back(Val);
2747 AllSame &= Val == ToC;
Owen Anderson45308b52009-07-27 22:29:26 +00002748 }
Galina Kistanovafc259902012-07-13 01:25:27 +00002749
Mehdi Amini8914d292016-02-10 22:47:15 +00002750 if (AllSame && ToC->isNullValue())
Pete Cooper5815b1f2015-06-24 18:55:24 +00002751 return ConstantAggregateZero::get(getType());
2752
Mehdi Amini8914d292016-02-10 22:47:15 +00002753 if (AllSame && isa<UndefValue>(ToC))
Pete Cooper5815b1f2015-06-24 18:55:24 +00002754 return UndefValue::get(getType());
Galina Kistanovafc259902012-07-13 01:25:27 +00002755
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002756 // Update to the new value.
Pete Cooper5815b1f2015-06-24 18:55:24 +00002757 return getContext().pImpl->StructConstants.replaceOperandsInPlace(
Mehdi Amini8914d292016-02-10 22:47:15 +00002758 Values, this, From, ToC, NumUpdated, OperandNo);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002759}
2760
Mehdi Amini8914d292016-02-10 22:47:15 +00002761Value *ConstantVector::handleOperandChangeImpl(Value *From, Value *To) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002762 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002763 Constant *ToC = cast<Constant>(To);
Galina Kistanovafc259902012-07-13 01:25:27 +00002764
Chris Lattnera474bb22012-01-26 20:40:56 +00002765 SmallVector<Constant*, 8> Values;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002766 Values.reserve(getNumOperands()); // Build replacement array...
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002767 unsigned NumUpdated = 0;
Mehdi Amini8914d292016-02-10 22:47:15 +00002768 unsigned OperandNo = 0;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002769 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2770 Constant *Val = getOperand(i);
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002771 if (Val == From) {
Mehdi Amini8914d292016-02-10 22:47:15 +00002772 OperandNo = i;
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002773 ++NumUpdated;
2774 Val = ToC;
2775 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002776 Values.push_back(Val);
2777 }
Galina Kistanovafc259902012-07-13 01:25:27 +00002778
Pete Cooper5815b1f2015-06-24 18:55:24 +00002779 if (Constant *C = getImpl(Values))
2780 return C;
Duncan P. N. Exon Smith687744d2014-08-19 02:24:46 +00002781
Duncan P. N. Exon Smith909620a2014-08-19 19:13:30 +00002782 // Update to the new value.
Pete Cooper5815b1f2015-06-24 18:55:24 +00002783 return getContext().pImpl->VectorConstants.replaceOperandsInPlace(
Mehdi Amini8914d292016-02-10 22:47:15 +00002784 Values, this, From, ToC, NumUpdated, OperandNo);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002785}
2786
Mehdi Amini8914d292016-02-10 22:47:15 +00002787Value *ConstantExpr::handleOperandChangeImpl(Value *From, Value *ToV) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002788 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2789 Constant *To = cast<Constant>(ToV);
Galina Kistanovafc259902012-07-13 01:25:27 +00002790
Chris Lattner37e38352012-01-26 20:37:11 +00002791 SmallVector<Constant*, 8> NewOps;
Duncan P. N. Exon Smith33de00c2014-08-19 20:03:35 +00002792 unsigned NumUpdated = 0;
Mehdi Amini8914d292016-02-10 22:47:15 +00002793 unsigned OperandNo = 0;
Chris Lattner37e38352012-01-26 20:37:11 +00002794 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2795 Constant *Op = getOperand(i);
Duncan P. N. Exon Smith33de00c2014-08-19 20:03:35 +00002796 if (Op == From) {
Mehdi Amini8914d292016-02-10 22:47:15 +00002797 OperandNo = i;
Duncan P. N. Exon Smith33de00c2014-08-19 20:03:35 +00002798 ++NumUpdated;
2799 Op = To;
2800 }
2801 NewOps.push_back(Op);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002802 }
Duncan P. N. Exon Smith33de00c2014-08-19 20:03:35 +00002803 assert(NumUpdated && "I didn't contain From!");
Galina Kistanovafc259902012-07-13 01:25:27 +00002804
Pete Cooper5815b1f2015-06-24 18:55:24 +00002805 if (Constant *C = getWithOperands(NewOps, getType(), true))
2806 return C;
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002807
Duncan P. N. Exon Smith33de00c2014-08-19 20:03:35 +00002808 // Update to the new value.
Pete Cooper5815b1f2015-06-24 18:55:24 +00002809 return getContext().pImpl->ExprConstants.replaceOperandsInPlace(
Mehdi Amini8914d292016-02-10 22:47:15 +00002810 NewOps, this, From, To, NumUpdated, OperandNo);
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002811}
2812
James Molloyce545682012-11-17 17:56:30 +00002813Instruction *ConstantExpr::getAsInstruction() {
Benjamin Kramer5fbfe2f2015-02-28 13:20:15 +00002814 SmallVector<Value *, 4> ValueOperands(op_begin(), op_end());
James Molloyce545682012-11-17 17:56:30 +00002815 ArrayRef<Value*> Ops(ValueOperands);
2816
2817 switch (getOpcode()) {
2818 case Instruction::Trunc:
2819 case Instruction::ZExt:
2820 case Instruction::SExt:
2821 case Instruction::FPTrunc:
2822 case Instruction::FPExt:
2823 case Instruction::UIToFP:
2824 case Instruction::SIToFP:
2825 case Instruction::FPToUI:
2826 case Instruction::FPToSI:
2827 case Instruction::PtrToInt:
2828 case Instruction::IntToPtr:
2829 case Instruction::BitCast:
Eli Bendersky157a97a2014-01-18 22:54:33 +00002830 case Instruction::AddrSpaceCast:
James Molloyce545682012-11-17 17:56:30 +00002831 return CastInst::Create((Instruction::CastOps)getOpcode(),
2832 Ops[0], getType());
2833 case Instruction::Select:
2834 return SelectInst::Create(Ops[0], Ops[1], Ops[2]);
2835 case Instruction::InsertElement:
2836 return InsertElementInst::Create(Ops[0], Ops[1], Ops[2]);
2837 case Instruction::ExtractElement:
2838 return ExtractElementInst::Create(Ops[0], Ops[1]);
2839 case Instruction::InsertValue:
2840 return InsertValueInst::Create(Ops[0], Ops[1], getIndices());
2841 case Instruction::ExtractValue:
2842 return ExtractValueInst::Create(Ops[0], getIndices());
2843 case Instruction::ShuffleVector:
2844 return new ShuffleVectorInst(Ops[0], Ops[1], Ops[2]);
2845
David Blaikie741c8f82015-03-14 01:53:18 +00002846 case Instruction::GetElementPtr: {
2847 const auto *GO = cast<GEPOperator>(this);
2848 if (GO->isInBounds())
2849 return GetElementPtrInst::CreateInBounds(GO->getSourceElementType(),
2850 Ops[0], Ops.slice(1));
2851 return GetElementPtrInst::Create(GO->getSourceElementType(), Ops[0],
2852 Ops.slice(1));
2853 }
James Molloyce545682012-11-17 17:56:30 +00002854 case Instruction::ICmp:
2855 case Instruction::FCmp:
2856 return CmpInst::Create((Instruction::OtherOps)getOpcode(),
Craig Topper1c3f2832015-12-15 06:11:33 +00002857 (CmpInst::Predicate)getPredicate(), Ops[0], Ops[1]);
James Molloyce545682012-11-17 17:56:30 +00002858
2859 default:
2860 assert(getNumOperands() == 2 && "Must be binary operator?");
2861 BinaryOperator *BO =
2862 BinaryOperator::Create((Instruction::BinaryOps)getOpcode(),
2863 Ops[0], Ops[1]);
2864 if (isa<OverflowingBinaryOperator>(BO)) {
2865 BO->setHasNoUnsignedWrap(SubclassOptionalData &
2866 OverflowingBinaryOperator::NoUnsignedWrap);
2867 BO->setHasNoSignedWrap(SubclassOptionalData &
2868 OverflowingBinaryOperator::NoSignedWrap);
2869 }
2870 if (isa<PossiblyExactOperator>(BO))
2871 BO->setIsExact(SubclassOptionalData & PossiblyExactOperator::IsExact);
2872 return BO;
2873 }
2874}