blob: f56fe7089807be2651bbf5cbe047757a4268193e [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))
Craig Topper0b4b4e32017-07-15 22:06:19 +000047 if (CV->getElementType()->isFloatingPointTy() && CV->isSplat())
48 if (CV->getElementAsAPFloat(0).isNegZero())
David Tweed5493fee2013-03-18 11:54:44 +000049 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))
Craig Topper0b4b4e32017-07-15 22:06:19 +000073 if (CV->getElementType()->isFloatingPointTy() && CV->isSplat())
74 if (CV->getElementAsAPFloat(0).isZero())
Owen Anderson630077e2015-11-20 08:16:13 +000075 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.
Craig Topper0b4b4e32017-07-15 22:06:19 +0000116 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this)) {
117 if (CV->isSplat()) {
118 if (CV->getElementType()->isFloatingPointTy())
119 return CV->getElementAsAPFloat(0).bitcastToAPInt().isAllOnesValue();
120 return CV->getElementAsAPInt(0).isAllOnesValue();
121 }
122 }
Chris Lattnerf14a67f2012-01-26 02:31:22 +0000123
Nadav Rotem365af6f2011-08-24 20:18:38 +0000124 return false;
125}
Benjamin Kramer42d098e2011-11-14 19:12:20 +0000126
David Majnemer1a0bbc82014-08-16 09:23:42 +0000127bool Constant::isOneValue() const {
128 // Check for 1 integers
129 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
130 return CI->isOne();
131
132 // Check for FP which are bitcasted from 1 integers
133 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
Craig Topper93ac6e12017-06-07 00:58:02 +0000134 return CFP->getValueAPF().bitcastToAPInt().isOneValue();
David Majnemer1a0bbc82014-08-16 09:23:42 +0000135
136 // Check for constant vectors which are splats of 1 values.
137 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
138 if (Constant *Splat = CV->getSplatValue())
139 return Splat->isOneValue();
140
141 // Check for constant vectors which are splats of 1 values.
Craig Topper0b4b4e32017-07-15 22:06:19 +0000142 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this)) {
143 if (CV->isSplat()) {
144 if (CV->getElementType()->isFloatingPointTy())
145 return CV->getElementAsAPFloat(0).bitcastToAPInt().isOneValue();
146 return CV->getElementAsAPInt(0).isOneValue();
147 }
148 }
David Majnemer1a0bbc82014-08-16 09:23:42 +0000149
150 return false;
151}
152
David Majnemerbdeef602014-07-02 06:07:09 +0000153bool Constant::isMinSignedValue() const {
154 // Check for INT_MIN integers
155 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
156 return CI->isMinValue(/*isSigned=*/true);
157
158 // Check for FP which are bitcasted from INT_MIN integers
159 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
160 return CFP->getValueAPF().bitcastToAPInt().isMinSignedValue();
161
162 // Check for constant vectors which are splats of INT_MIN values.
163 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
164 if (Constant *Splat = CV->getSplatValue())
165 return Splat->isMinSignedValue();
166
167 // Check for constant vectors which are splats of INT_MIN values.
Craig Topper0b4b4e32017-07-15 22:06:19 +0000168 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this)) {
169 if (CV->isSplat()) {
170 if (CV->getElementType()->isFloatingPointTy())
171 return CV->getElementAsAPFloat(0).bitcastToAPInt().isMinSignedValue();
172 return CV->getElementAsAPInt(0).isMinSignedValue();
173 }
174 }
David Majnemerbdeef602014-07-02 06:07:09 +0000175
176 return false;
177}
178
David Majnemer0e6c9862014-08-22 16:41:23 +0000179bool Constant::isNotMinSignedValue() const {
180 // Check for INT_MIN integers
181 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
182 return !CI->isMinValue(/*isSigned=*/true);
183
184 // Check for FP which are bitcasted from INT_MIN integers
185 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
186 return !CFP->getValueAPF().bitcastToAPInt().isMinSignedValue();
187
188 // Check for constant vectors which are splats of INT_MIN values.
189 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
190 if (Constant *Splat = CV->getSplatValue())
191 return Splat->isNotMinSignedValue();
192
193 // Check for constant vectors which are splats of INT_MIN values.
Craig Topper0b4b4e32017-07-15 22:06:19 +0000194 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this)) {
195 if (CV->isSplat()) {
196 if (CV->getElementType()->isFloatingPointTy())
197 return !CV->getElementAsAPFloat(0).bitcastToAPInt().isMinSignedValue();
198 return !CV->getElementAsAPInt(0).isMinSignedValue();
199 }
200 }
David Majnemer0e6c9862014-08-22 16:41:23 +0000201
202 // It *may* contain INT_MIN, we can't tell.
203 return false;
204}
205
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +0000206/// Constructor to create a '0' constant of arbitrary type.
Chris Lattner229907c2011-07-18 04:54:35 +0000207Constant *Constant::getNullValue(Type *Ty) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000208 switch (Ty->getTypeID()) {
209 case Type::IntegerTyID:
210 return ConstantInt::get(Ty, 0);
Dan Gohman518cda42011-12-17 00:04:22 +0000211 case Type::HalfTyID:
212 return ConstantFP::get(Ty->getContext(),
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000213 APFloat::getZero(APFloat::IEEEhalf()));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000214 case Type::FloatTyID:
Benjamin Kramer8ceebfa2010-12-04 14:22:24 +0000215 return ConstantFP::get(Ty->getContext(),
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000216 APFloat::getZero(APFloat::IEEEsingle()));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000217 case Type::DoubleTyID:
Benjamin Kramer8ceebfa2010-12-04 14:22:24 +0000218 return ConstantFP::get(Ty->getContext(),
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000219 APFloat::getZero(APFloat::IEEEdouble()));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000220 case Type::X86_FP80TyID:
Benjamin Kramer8ceebfa2010-12-04 14:22:24 +0000221 return ConstantFP::get(Ty->getContext(),
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000222 APFloat::getZero(APFloat::x87DoubleExtended()));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000223 case Type::FP128TyID:
224 return ConstantFP::get(Ty->getContext(),
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000225 APFloat::getZero(APFloat::IEEEquad()));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000226 case Type::PPC_FP128TyID:
Benjamin Kramer8ceebfa2010-12-04 14:22:24 +0000227 return ConstantFP::get(Ty->getContext(),
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000228 APFloat(APFloat::PPCDoubleDouble(),
Tim Northover29178a32013-01-22 09:46:31 +0000229 APInt::getNullValue(128)));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000230 case Type::PointerTyID:
231 return ConstantPointerNull::get(cast<PointerType>(Ty));
232 case Type::StructTyID:
233 case Type::ArrayTyID:
234 case Type::VectorTyID:
235 return ConstantAggregateZero::get(Ty);
David Majnemerf0f224d2015-11-11 21:57:16 +0000236 case Type::TokenTyID:
237 return ConstantTokenNone::get(Ty->getContext());
Owen Anderson5a1acd92009-07-31 20:28:14 +0000238 default:
239 // Function, Label, or Opaque type?
Craig Topperc514b542012-02-05 22:14:15 +0000240 llvm_unreachable("Cannot create a null constant of that type!");
Owen Anderson5a1acd92009-07-31 20:28:14 +0000241 }
242}
243
Chris Lattner229907c2011-07-18 04:54:35 +0000244Constant *Constant::getIntegerValue(Type *Ty, const APInt &V) {
245 Type *ScalarTy = Ty->getScalarType();
Dan Gohmanf011f5a2009-08-03 22:07:33 +0000246
247 // Create the base integer constant.
248 Constant *C = ConstantInt::get(Ty->getContext(), V);
249
250 // Convert an integer to a pointer, if necessary.
Chris Lattner229907c2011-07-18 04:54:35 +0000251 if (PointerType *PTy = dyn_cast<PointerType>(ScalarTy))
Dan Gohmanf011f5a2009-08-03 22:07:33 +0000252 C = ConstantExpr::getIntToPtr(C, PTy);
253
254 // Broadcast a scalar to a vector, if necessary.
Chris Lattner229907c2011-07-18 04:54:35 +0000255 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
Chris Lattnere9eed292012-01-25 05:19:54 +0000256 C = ConstantVector::getSplat(VTy->getNumElements(), C);
Dan Gohmanf011f5a2009-08-03 22:07:33 +0000257
258 return C;
259}
260
Chris Lattner229907c2011-07-18 04:54:35 +0000261Constant *Constant::getAllOnesValue(Type *Ty) {
262 if (IntegerType *ITy = dyn_cast<IntegerType>(Ty))
Owen Anderson5a1acd92009-07-31 20:28:14 +0000263 return ConstantInt::get(Ty->getContext(),
264 APInt::getAllOnesValue(ITy->getBitWidth()));
Nadav Rotem7cc6d122011-02-17 21:22:27 +0000265
266 if (Ty->isFloatingPointTy()) {
267 APFloat FL = APFloat::getAllOnesValue(Ty->getPrimitiveSizeInBits(),
268 !Ty->isPPC_FP128Ty());
269 return ConstantFP::get(Ty->getContext(), FL);
270 }
271
Chris Lattner229907c2011-07-18 04:54:35 +0000272 VectorType *VTy = cast<VectorType>(Ty);
Chris Lattnere9eed292012-01-25 05:19:54 +0000273 return ConstantVector::getSplat(VTy->getNumElements(),
274 getAllOnesValue(VTy->getElementType()));
Owen Anderson5a1acd92009-07-31 20:28:14 +0000275}
276
Chris Lattner7e683d12012-01-25 06:16:32 +0000277Constant *Constant::getAggregateElement(unsigned Elt) const {
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +0000278 if (const ConstantAggregate *CC = dyn_cast<ConstantAggregate>(this))
279 return Elt < CC->getNumOperands() ? CC->getOperand(Elt) : nullptr;
Galina Kistanovafc259902012-07-13 01:25:27 +0000280
David Majnemer9b529a72015-02-16 04:02:09 +0000281 if (const ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(this))
282 return Elt < CAZ->getNumElements() ? CAZ->getElementValue(Elt) : nullptr;
Galina Kistanovafc259902012-07-13 01:25:27 +0000283
Chris Lattner7e683d12012-01-25 06:16:32 +0000284 if (const UndefValue *UV = dyn_cast<UndefValue>(this))
David Majnemer9b529a72015-02-16 04:02:09 +0000285 return Elt < UV->getNumElements() ? UV->getElementValue(Elt) : nullptr;
Galina Kistanovafc259902012-07-13 01:25:27 +0000286
Chris Lattner8326bd82012-01-26 00:42:34 +0000287 if (const ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(this))
Craig Topperc6207612014-04-09 06:08:46 +0000288 return Elt < CDS->getNumElements() ? CDS->getElementAsConstant(Elt)
289 : nullptr;
290 return nullptr;
Chris Lattner7e683d12012-01-25 06:16:32 +0000291}
292
293Constant *Constant::getAggregateElement(Constant *Elt) const {
294 assert(isa<IntegerType>(Elt->getType()) && "Index must be an integer");
295 if (ConstantInt *CI = dyn_cast<ConstantInt>(Elt))
296 return getAggregateElement(CI->getZExtValue());
Craig Topperc6207612014-04-09 06:08:46 +0000297 return nullptr;
Chris Lattner7e683d12012-01-25 06:16:32 +0000298}
299
Pete Cooper86dd4cf2015-06-23 21:55:11 +0000300void Constant::destroyConstant() {
301 /// First call destroyConstantImpl on the subclass. This gives the subclass
302 /// a chance to remove the constant from any maps/pools it's contained in.
303 switch (getValueID()) {
304 default:
305 llvm_unreachable("Not a constant!");
306#define HANDLE_CONSTANT(Name) \
307 case Value::Name##Val: \
308 cast<Name>(this)->destroyConstantImpl(); \
309 break;
310#include "llvm/IR/Value.def"
311 }
Chris Lattner7e683d12012-01-25 06:16:32 +0000312
Chris Lattner3462ae32001-12-03 22:26:30 +0000313 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +0000314 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +0000315 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +0000316 // but they don't know that. Because we only find out when the CPV is
317 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +0000318 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +0000319 //
320 while (!use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000321 Value *V = user_back();
Pete Cooper86dd4cf2015-06-23 21:55:11 +0000322#ifndef NDEBUG // Only in -g mode...
Chris Lattner78683a72009-08-23 04:02:03 +0000323 if (!isa<Constant>(V)) {
David Greene1e27a132010-01-05 01:29:19 +0000324 dbgs() << "While deleting: " << *this
Pete Cooper86dd4cf2015-06-23 21:55:11 +0000325 << "\n\nUse still stuck around after Def is destroyed: " << *V
326 << "\n\n";
Chris Lattner78683a72009-08-23 04:02:03 +0000327 }
Chris Lattnerd7a73302001-10-13 06:57:33 +0000328#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000329 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Chris Lattner8326bd82012-01-26 00:42:34 +0000330 cast<Constant>(V)->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +0000331
332 // The constant should remove itself from our use list...
Chandler Carruthcdf47882014-03-09 03:16:01 +0000333 assert((use_empty() || user_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +0000334 }
335
336 // Value has no outstanding references it is safe to delete it now...
337 delete this;
Chris Lattner38569342001-10-01 20:11:19 +0000338}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000339
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000340static bool canTrapImpl(const Constant *C,
Craig Topper71b7b682014-08-21 05:55:13 +0000341 SmallPtrSetImpl<const ConstantExpr *> &NonTrappingOps) {
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000342 assert(C->getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
Chris Lattner23dd1f62006-10-20 00:27:06 +0000343 // The only thing that could possibly trap are constant exprs.
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000344 const ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
345 if (!CE)
346 return false;
Galina Kistanovafc259902012-07-13 01:25:27 +0000347
348 // ConstantExpr traps if any operands can trap.
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000349 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
350 if (ConstantExpr *Op = dyn_cast<ConstantExpr>(CE->getOperand(i))) {
David Blaikie70573dc2014-11-19 07:49:26 +0000351 if (NonTrappingOps.insert(Op).second && canTrapImpl(Op, NonTrappingOps))
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000352 return true;
353 }
354 }
Chris Lattner23dd1f62006-10-20 00:27:06 +0000355
356 // Otherwise, only specific operations can trap.
357 switch (CE->getOpcode()) {
358 default:
359 return false;
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000360 case Instruction::UDiv:
361 case Instruction::SDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +0000362 case Instruction::URem:
363 case Instruction::SRem:
Chris Lattner23dd1f62006-10-20 00:27:06 +0000364 // Div and rem can trap if the RHS is not known to be non-zero.
Chris Lattnera91a5632009-10-28 05:14:34 +0000365 if (!isa<ConstantInt>(CE->getOperand(1)) ||CE->getOperand(1)->isNullValue())
Chris Lattner23dd1f62006-10-20 00:27:06 +0000366 return true;
367 return false;
368 }
369}
370
Benjamin Kramer89ca4bc2013-04-13 12:53:18 +0000371bool Constant::canTrap() const {
372 SmallPtrSet<const ConstantExpr *, 4> NonTrappingOps;
373 return canTrapImpl(this, NonTrappingOps);
374}
375
Hans Wennborg4dc89512014-06-20 00:38:12 +0000376/// Check if C contains a GlobalValue for which Predicate is true.
377static bool
378ConstHasGlobalValuePredicate(const Constant *C,
379 bool (*Predicate)(const GlobalValue *)) {
380 SmallPtrSet<const Constant *, 8> Visited;
381 SmallVector<const Constant *, 8> WorkList;
382 WorkList.push_back(C);
383 Visited.insert(C);
Hans Wennborg709e0152012-11-15 11:40:00 +0000384
385 while (!WorkList.empty()) {
Hans Wennborg4dc89512014-06-20 00:38:12 +0000386 const Constant *WorkItem = WorkList.pop_back_val();
387 if (const auto *GV = dyn_cast<GlobalValue>(WorkItem))
388 if (Predicate(GV))
Hans Wennborg709e0152012-11-15 11:40:00 +0000389 return true;
Hans Wennborg4dc89512014-06-20 00:38:12 +0000390 for (const Value *Op : WorkItem->operands()) {
391 const Constant *ConstOp = dyn_cast<Constant>(Op);
392 if (!ConstOp)
Hans Wennborg18aa12402012-11-16 10:33:25 +0000393 continue;
David Blaikie70573dc2014-11-19 07:49:26 +0000394 if (Visited.insert(ConstOp).second)
Hans Wennborg4dc89512014-06-20 00:38:12 +0000395 WorkList.push_back(ConstOp);
Hans Wennborg709e0152012-11-15 11:40:00 +0000396 }
397 }
Hans Wennborg709e0152012-11-15 11:40:00 +0000398 return false;
399}
400
Hans Wennborg4dc89512014-06-20 00:38:12 +0000401bool Constant::isThreadDependent() const {
402 auto DLLImportPredicate = [](const GlobalValue *GV) {
403 return GV->isThreadLocal();
404 };
405 return ConstHasGlobalValuePredicate(this, DLLImportPredicate);
406}
407
408bool Constant::isDLLImportDependent() const {
409 auto DLLImportPredicate = [](const GlobalValue *GV) {
410 return GV->hasDLLImportStorageClass();
411 };
412 return ConstHasGlobalValuePredicate(this, DLLImportPredicate);
413}
414
Chris Lattner253bc772009-11-01 18:11:50 +0000415bool Constant::isConstantUsed() const {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000416 for (const User *U : users()) {
417 const Constant *UC = dyn_cast<Constant>(U);
Craig Topperc6207612014-04-09 06:08:46 +0000418 if (!UC || isa<GlobalValue>(UC))
Chris Lattner253bc772009-11-01 18:11:50 +0000419 return true;
Galina Kistanovafc259902012-07-13 01:25:27 +0000420
Chris Lattner253bc772009-11-01 18:11:50 +0000421 if (UC->isConstantUsed())
422 return true;
423 }
424 return false;
425}
426
Rafael Espindola65e49022015-11-17 00:51:23 +0000427bool Constant::needsRelocation() const {
428 if (isa<GlobalValue>(this))
429 return true; // Global reference.
430
Chris Lattner2cb85b42009-10-28 04:12:16 +0000431 if (const BlockAddress *BA = dyn_cast<BlockAddress>(this))
Rafael Espindola65e49022015-11-17 00:51:23 +0000432 return BA->getFunction()->needsRelocation();
433
Chris Lattnera7cfc432010-01-03 18:09:40 +0000434 // While raw uses of blockaddress need to be relocated, differences between
435 // two of them don't when they are for labels in the same function. This is a
436 // common idiom when creating a table for the indirect goto extension, so we
437 // handle it efficiently here.
438 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(this))
439 if (CE->getOpcode() == Instruction::Sub) {
440 ConstantExpr *LHS = dyn_cast<ConstantExpr>(CE->getOperand(0));
441 ConstantExpr *RHS = dyn_cast<ConstantExpr>(CE->getOperand(1));
Rafael Espindola65e49022015-11-17 00:51:23 +0000442 if (LHS && RHS && LHS->getOpcode() == Instruction::PtrToInt &&
Chris Lattnera7cfc432010-01-03 18:09:40 +0000443 RHS->getOpcode() == Instruction::PtrToInt &&
444 isa<BlockAddress>(LHS->getOperand(0)) &&
445 isa<BlockAddress>(RHS->getOperand(0)) &&
446 cast<BlockAddress>(LHS->getOperand(0))->getFunction() ==
Rafael Espindola65e49022015-11-17 00:51:23 +0000447 cast<BlockAddress>(RHS->getOperand(0))->getFunction())
448 return false;
Chris Lattnera7cfc432010-01-03 18:09:40 +0000449 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000450
Rafael Espindola65e49022015-11-17 00:51:23 +0000451 bool Result = false;
Evan Chengf9e003b2007-03-08 00:59:12 +0000452 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Rafael Espindola65e49022015-11-17 00:51:23 +0000453 Result |= cast<Constant>(getOperand(i))->needsRelocation();
Galina Kistanovafc259902012-07-13 01:25:27 +0000454
Chris Lattner4565ef52009-07-22 00:05:44 +0000455 return Result;
Evan Chengf9e003b2007-03-08 00:59:12 +0000456}
457
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +0000458/// If the specified constantexpr is dead, remove it. This involves recursively
459/// eliminating any dead users of the constantexpr.
Chris Lattner84886402011-02-18 04:41:42 +0000460static bool removeDeadUsersOfConstant(const Constant *C) {
461 if (isa<GlobalValue>(C)) return false; // Cannot remove this
Galina Kistanovafc259902012-07-13 01:25:27 +0000462
Chris Lattner84886402011-02-18 04:41:42 +0000463 while (!C->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000464 const Constant *User = dyn_cast<Constant>(C->user_back());
Chris Lattner84886402011-02-18 04:41:42 +0000465 if (!User) return false; // Non-constant usage;
466 if (!removeDeadUsersOfConstant(User))
467 return false; // Constant wasn't dead
468 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000469
Chris Lattner84886402011-02-18 04:41:42 +0000470 const_cast<Constant*>(C)->destroyConstant();
471 return true;
472}
473
474
Chris Lattner84886402011-02-18 04:41:42 +0000475void Constant::removeDeadConstantUsers() const {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000476 Value::const_user_iterator I = user_begin(), E = user_end();
477 Value::const_user_iterator LastNonDeadUser = E;
Chris Lattner84886402011-02-18 04:41:42 +0000478 while (I != E) {
479 const Constant *User = dyn_cast<Constant>(*I);
Craig Topperc6207612014-04-09 06:08:46 +0000480 if (!User) {
Chris Lattner84886402011-02-18 04:41:42 +0000481 LastNonDeadUser = I;
482 ++I;
483 continue;
484 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000485
Chris Lattner84886402011-02-18 04:41:42 +0000486 if (!removeDeadUsersOfConstant(User)) {
487 // If the constant wasn't dead, remember that this was the last live use
488 // and move on to the next constant.
489 LastNonDeadUser = I;
490 ++I;
491 continue;
492 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000493
Chris Lattner84886402011-02-18 04:41:42 +0000494 // If the constant was dead, then the iterator is invalidated.
495 if (LastNonDeadUser == E) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000496 I = user_begin();
Chris Lattner84886402011-02-18 04:41:42 +0000497 if (I == E) break;
498 } else {
499 I = LastNonDeadUser;
500 ++I;
501 }
502 }
503}
504
505
Chris Lattner2105d662008-07-10 00:28:11 +0000506
Chris Lattner2f7c9632001-06-06 20:29:01 +0000507//===----------------------------------------------------------------------===//
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000508// ConstantInt
Chris Lattner2f7c9632001-06-06 20:29:01 +0000509//===----------------------------------------------------------------------===//
510
Duncan P. N. Exon Smithb6452792016-02-21 02:39:49 +0000511ConstantInt::ConstantInt(IntegerType *Ty, const APInt &V)
512 : ConstantData(Ty, ConstantIntVal), Val(V) {
Reid Spencerb31bffe2007-02-26 23:54:03 +0000513 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000514}
515
Nick Lewycky92db8e82011-03-06 03:36:19 +0000516ConstantInt *ConstantInt::getTrue(LLVMContext &Context) {
Owen Anderson23a204d2009-07-31 17:39:07 +0000517 LLVMContextImpl *pImpl = Context.pImpl;
Benjamin Kramerddd1b7b2010-11-20 18:43:35 +0000518 if (!pImpl->TheTrueVal)
519 pImpl->TheTrueVal = ConstantInt::get(Type::getInt1Ty(Context), 1);
520 return pImpl->TheTrueVal;
Owen Anderson23a204d2009-07-31 17:39:07 +0000521}
522
Nick Lewycky92db8e82011-03-06 03:36:19 +0000523ConstantInt *ConstantInt::getFalse(LLVMContext &Context) {
Owen Anderson23a204d2009-07-31 17:39:07 +0000524 LLVMContextImpl *pImpl = Context.pImpl;
Benjamin Kramerddd1b7b2010-11-20 18:43:35 +0000525 if (!pImpl->TheFalseVal)
526 pImpl->TheFalseVal = ConstantInt::get(Type::getInt1Ty(Context), 0);
527 return pImpl->TheFalseVal;
Owen Anderson23a204d2009-07-31 17:39:07 +0000528}
529
Chris Lattner229907c2011-07-18 04:54:35 +0000530Constant *ConstantInt::getTrue(Type *Ty) {
Craig Topperfde47232017-07-09 07:04:03 +0000531 assert(Ty->isIntOrIntVectorTy(1) && "Type not i1 or vector of i1.");
Sanjay Patel70a575a2017-04-16 17:00:21 +0000532 ConstantInt *TrueC = ConstantInt::getTrue(Ty->getContext());
533 if (auto *VTy = dyn_cast<VectorType>(Ty))
534 return ConstantVector::getSplat(VTy->getNumElements(), TrueC);
535 return TrueC;
Nick Lewycky92db8e82011-03-06 03:36:19 +0000536}
537
Chris Lattner229907c2011-07-18 04:54:35 +0000538Constant *ConstantInt::getFalse(Type *Ty) {
Craig Topperfde47232017-07-09 07:04:03 +0000539 assert(Ty->isIntOrIntVectorTy(1) && "Type not i1 or vector of i1.");
Sanjay Patel70a575a2017-04-16 17:00:21 +0000540 ConstantInt *FalseC = ConstantInt::getFalse(Ty->getContext());
541 if (auto *VTy = dyn_cast<VectorType>(Ty))
542 return ConstantVector::getSplat(VTy->getNumElements(), FalseC);
543 return FalseC;
Nick Lewycky92db8e82011-03-06 03:36:19 +0000544}
545
Benjamin Kramer8e5dc532014-12-06 13:12:56 +0000546// Get a ConstantInt from an APInt.
Nick Lewycky92db8e82011-03-06 03:36:19 +0000547ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt &V) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000548 // get an existing value or the insertion position
Benjamin Kramer320682f2013-06-01 17:51:03 +0000549 LLVMContextImpl *pImpl = Context.pImpl;
Justin Lebar611c5c22016-10-10 16:26:13 +0000550 std::unique_ptr<ConstantInt> &Slot = pImpl->IntConstants[V];
Benjamin Kramer8e5dc532014-12-06 13:12:56 +0000551 if (!Slot) {
552 // Get the corresponding integer type for the bit width of the value.
553 IntegerType *ITy = IntegerType::get(Context, V.getBitWidth());
Justin Lebar611c5c22016-10-10 16:26:13 +0000554 Slot.reset(new ConstantInt(ITy, V));
Benjamin Kramer8e5dc532014-12-06 13:12:56 +0000555 }
556 assert(Slot->getType() == IntegerType::get(Context, V.getBitWidth()));
Justin Lebar611c5c22016-10-10 16:26:13 +0000557 return Slot.get();
Owen Andersonedb4a702009-07-24 23:12:02 +0000558}
559
Chris Lattner229907c2011-07-18 04:54:35 +0000560Constant *ConstantInt::get(Type *Ty, uint64_t V, bool isSigned) {
Nick Lewycky92db8e82011-03-06 03:36:19 +0000561 Constant *C = get(cast<IntegerType>(Ty->getScalarType()), V, isSigned);
Owen Andersonedb4a702009-07-24 23:12:02 +0000562
563 // For vectors, broadcast the value.
Chris Lattner229907c2011-07-18 04:54:35 +0000564 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
Chris Lattnere9eed292012-01-25 05:19:54 +0000565 return ConstantVector::getSplat(VTy->getNumElements(), C);
Owen Andersonedb4a702009-07-24 23:12:02 +0000566
567 return C;
568}
569
Sanjay Patelf3587ec2016-05-18 22:05:28 +0000570ConstantInt *ConstantInt::get(IntegerType *Ty, uint64_t V, bool isSigned) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000571 return get(Ty->getContext(), APInt(Ty->getBitWidth(), V, isSigned));
572}
573
Chris Lattner0256be92012-01-27 03:08:05 +0000574ConstantInt *ConstantInt::getSigned(IntegerType *Ty, int64_t V) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000575 return get(Ty, V, true);
576}
577
Chris Lattner229907c2011-07-18 04:54:35 +0000578Constant *ConstantInt::getSigned(Type *Ty, int64_t V) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000579 return get(Ty, V, true);
580}
581
Chris Lattner0256be92012-01-27 03:08:05 +0000582Constant *ConstantInt::get(Type *Ty, const APInt& V) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000583 ConstantInt *C = get(Ty->getContext(), V);
584 assert(C->getType() == Ty->getScalarType() &&
585 "ConstantInt type doesn't match the type implied by its value!");
586
587 // For vectors, broadcast the value.
Chris Lattner229907c2011-07-18 04:54:35 +0000588 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
Chris Lattnere9eed292012-01-25 05:19:54 +0000589 return ConstantVector::getSplat(VTy->getNumElements(), C);
Owen Andersonedb4a702009-07-24 23:12:02 +0000590
591 return C;
592}
593
Sanjay Patelf3587ec2016-05-18 22:05:28 +0000594ConstantInt *ConstantInt::get(IntegerType* Ty, StringRef Str, uint8_t radix) {
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000595 return get(Ty->getContext(), APInt(Ty->getBitWidth(), Str, radix));
596}
597
Pete Cooper86dd4cf2015-06-23 21:55:11 +0000598/// Remove the constant from the constant table.
599void ConstantInt::destroyConstantImpl() {
600 llvm_unreachable("You can't ConstantInt->destroyConstantImpl()!");
601}
602
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000603//===----------------------------------------------------------------------===//
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000604// ConstantFP
Chris Lattnera80bf0b2007-02-20 06:39:57 +0000605//===----------------------------------------------------------------------===//
606
Chris Lattner229907c2011-07-18 04:54:35 +0000607static const fltSemantics *TypeToFloatSemantics(Type *Ty) {
Dan Gohman518cda42011-12-17 00:04:22 +0000608 if (Ty->isHalfTy())
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000609 return &APFloat::IEEEhalf();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000610 if (Ty->isFloatTy())
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000611 return &APFloat::IEEEsingle();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000612 if (Ty->isDoubleTy())
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000613 return &APFloat::IEEEdouble();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000614 if (Ty->isX86_FP80Ty())
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000615 return &APFloat::x87DoubleExtended();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000616 else if (Ty->isFP128Ty())
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000617 return &APFloat::IEEEquad();
Galina Kistanovafc259902012-07-13 01:25:27 +0000618
Chris Lattnerfdd87902009-10-05 05:54:46 +0000619 assert(Ty->isPPC_FP128Ty() && "Unknown FP format");
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000620 return &APFloat::PPCDoubleDouble();
Rafael Espindolaf5d53d42009-07-15 17:40:42 +0000621}
622
Chris Lattner0256be92012-01-27 03:08:05 +0000623Constant *ConstantFP::get(Type *Ty, double V) {
Owen Anderson69c464d2009-07-27 20:59:43 +0000624 LLVMContext &Context = Ty->getContext();
Galina Kistanovafc259902012-07-13 01:25:27 +0000625
Owen Anderson69c464d2009-07-27 20:59:43 +0000626 APFloat FV(V);
627 bool ignored;
628 FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
629 APFloat::rmNearestTiesToEven, &ignored);
630 Constant *C = get(Context, FV);
631
632 // For vectors, broadcast the value.
Chris Lattner229907c2011-07-18 04:54:35 +0000633 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
Chris Lattnere9eed292012-01-25 05:19:54 +0000634 return ConstantVector::getSplat(VTy->getNumElements(), C);
Owen Anderson69c464d2009-07-27 20:59:43 +0000635
636 return C;
637}
638
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000639
Chris Lattner0256be92012-01-27 03:08:05 +0000640Constant *ConstantFP::get(Type *Ty, StringRef Str) {
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000641 LLVMContext &Context = Ty->getContext();
642
643 APFloat FV(*TypeToFloatSemantics(Ty->getScalarType()), Str);
644 Constant *C = get(Context, FV);
645
646 // For vectors, broadcast the value.
Chris Lattner229907c2011-07-18 04:54:35 +0000647 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
Chris Lattnere9eed292012-01-25 05:19:54 +0000648 return ConstantVector::getSplat(VTy->getNumElements(), C);
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000649
650 return C;
651}
652
Tom Stellard67246d12015-04-20 19:38:24 +0000653Constant *ConstantFP::getNaN(Type *Ty, bool Negative, unsigned Type) {
654 const fltSemantics &Semantics = *TypeToFloatSemantics(Ty->getScalarType());
655 APFloat NaN = APFloat::getNaN(Semantics, Negative, Type);
656 Constant *C = get(Ty->getContext(), NaN);
657
658 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
659 return ConstantVector::getSplat(VTy->getNumElements(), C);
660
661 return C;
662}
663
Benjamin Kramer5d2ff222014-01-18 16:43:06 +0000664Constant *ConstantFP::getNegativeZero(Type *Ty) {
665 const fltSemantics &Semantics = *TypeToFloatSemantics(Ty->getScalarType());
666 APFloat NegZero = APFloat::getZero(Semantics, /*Negative=*/true);
667 Constant *C = get(Ty->getContext(), NegZero);
Erick Tryzelaarfc2280d2009-08-16 23:36:33 +0000668
Benjamin Kramer5d2ff222014-01-18 16:43:06 +0000669 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
670 return ConstantVector::getSplat(VTy->getNumElements(), C);
671
672 return C;
Owen Anderson69c464d2009-07-27 20:59:43 +0000673}
674
675
Chris Lattnere9eed292012-01-25 05:19:54 +0000676Constant *ConstantFP::getZeroValueForNegation(Type *Ty) {
Benjamin Kramer5d2ff222014-01-18 16:43:06 +0000677 if (Ty->isFPOrFPVectorTy())
678 return getNegativeZero(Ty);
Owen Anderson69c464d2009-07-27 20:59:43 +0000679
Owen Anderson5a1acd92009-07-31 20:28:14 +0000680 return Constant::getNullValue(Ty);
Owen Anderson69c464d2009-07-27 20:59:43 +0000681}
682
683
684// ConstantFP accessors.
685ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) {
Owen Anderson69c464d2009-07-27 20:59:43 +0000686 LLVMContextImpl* pImpl = Context.pImpl;
Galina Kistanovafc259902012-07-13 01:25:27 +0000687
Justin Lebar611c5c22016-10-10 16:26:13 +0000688 std::unique_ptr<ConstantFP> &Slot = pImpl->FPConstants[V];
Galina Kistanovafc259902012-07-13 01:25:27 +0000689
Owen Anderson69c464d2009-07-27 20:59:43 +0000690 if (!Slot) {
Chris Lattner229907c2011-07-18 04:54:35 +0000691 Type *Ty;
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000692 if (&V.getSemantics() == &APFloat::IEEEhalf())
Dan Gohman518cda42011-12-17 00:04:22 +0000693 Ty = Type::getHalfTy(Context);
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000694 else if (&V.getSemantics() == &APFloat::IEEEsingle())
Owen Anderson5dab84c2009-10-19 20:11:52 +0000695 Ty = Type::getFloatTy(Context);
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000696 else if (&V.getSemantics() == &APFloat::IEEEdouble())
Owen Anderson5dab84c2009-10-19 20:11:52 +0000697 Ty = Type::getDoubleTy(Context);
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000698 else if (&V.getSemantics() == &APFloat::x87DoubleExtended())
Owen Anderson5dab84c2009-10-19 20:11:52 +0000699 Ty = Type::getX86_FP80Ty(Context);
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000700 else if (&V.getSemantics() == &APFloat::IEEEquad())
Owen Anderson5dab84c2009-10-19 20:11:52 +0000701 Ty = Type::getFP128Ty(Context);
702 else {
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000703 assert(&V.getSemantics() == &APFloat::PPCDoubleDouble() &&
Owen Anderson5dab84c2009-10-19 20:11:52 +0000704 "Unknown FP format");
705 Ty = Type::getPPC_FP128Ty(Context);
Owen Anderson69c464d2009-07-27 20:59:43 +0000706 }
Justin Lebar611c5c22016-10-10 16:26:13 +0000707 Slot.reset(new ConstantFP(Ty, V));
Owen Anderson69c464d2009-07-27 20:59:43 +0000708 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000709
Justin Lebar611c5c22016-10-10 16:26:13 +0000710 return Slot.get();
Owen Anderson69c464d2009-07-27 20:59:43 +0000711}
712
Benjamin Kramer5d2ff222014-01-18 16:43:06 +0000713Constant *ConstantFP::getInfinity(Type *Ty, bool Negative) {
714 const fltSemantics &Semantics = *TypeToFloatSemantics(Ty->getScalarType());
715 Constant *C = get(Ty->getContext(), APFloat::getInf(Semantics, Negative));
716
717 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
718 return ConstantVector::getSplat(VTy->getNumElements(), C);
719
720 return C;
Dan Gohmanfeb50212009-09-25 23:00:48 +0000721}
722
Duncan P. N. Exon Smithb6452792016-02-21 02:39:49 +0000723ConstantFP::ConstantFP(Type *Ty, const APFloat &V)
724 : ConstantData(Ty, ConstantFPVal), Val(V) {
Chris Lattner98bd9392008-04-09 06:38:30 +0000725 assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
726 "FP type Mismatch");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000727}
728
Chris Lattnerbe6610c2011-07-15 06:14:08 +0000729bool ConstantFP::isExactlyValue(const APFloat &V) const {
Dale Johannesend246b2c2007-08-30 00:23:21 +0000730 return Val.bitwiseIsEqual(V);
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000731}
732
Pete Cooper86dd4cf2015-06-23 21:55:11 +0000733/// Remove the constant from the constant table.
734void ConstantFP::destroyConstantImpl() {
Craig Topperccbb8102017-06-27 19:57:51 +0000735 llvm_unreachable("You can't ConstantFP->destroyConstantImpl()!");
Pete Cooper86dd4cf2015-06-23 21:55:11 +0000736}
737
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000738//===----------------------------------------------------------------------===//
Chris Lattner030af792012-01-24 05:42:11 +0000739// ConstantAggregateZero Implementation
740//===----------------------------------------------------------------------===//
741
Chris Lattner7e683d12012-01-25 06:16:32 +0000742Constant *ConstantAggregateZero::getSequentialElement() const {
Chris Lattner8326bd82012-01-26 00:42:34 +0000743 return Constant::getNullValue(getType()->getSequentialElementType());
Chris Lattner030af792012-01-24 05:42:11 +0000744}
745
Chris Lattner7e683d12012-01-25 06:16:32 +0000746Constant *ConstantAggregateZero::getStructElement(unsigned Elt) const {
Chris Lattner8326bd82012-01-26 00:42:34 +0000747 return Constant::getNullValue(getType()->getStructElementType(Elt));
Chris Lattner030af792012-01-24 05:42:11 +0000748}
749
Chris Lattner7e683d12012-01-25 06:16:32 +0000750Constant *ConstantAggregateZero::getElementValue(Constant *C) const {
Chris Lattner030af792012-01-24 05:42:11 +0000751 if (isa<SequentialType>(getType()))
752 return getSequentialElement();
753 return getStructElement(cast<ConstantInt>(C)->getZExtValue());
754}
755
Chris Lattner7e683d12012-01-25 06:16:32 +0000756Constant *ConstantAggregateZero::getElementValue(unsigned Idx) const {
Chris Lattnerf7eb5432012-01-24 07:54:10 +0000757 if (isa<SequentialType>(getType()))
758 return getSequentialElement();
759 return getStructElement(Idx);
760}
761
David Majnemer9b529a72015-02-16 04:02:09 +0000762unsigned ConstantAggregateZero::getNumElements() const {
Craig Toppere3dcce92015-08-01 22:20:21 +0000763 Type *Ty = getType();
764 if (auto *AT = dyn_cast<ArrayType>(Ty))
David Majnemer9b529a72015-02-16 04:02:09 +0000765 return AT->getNumElements();
Craig Toppere3dcce92015-08-01 22:20:21 +0000766 if (auto *VT = dyn_cast<VectorType>(Ty))
David Majnemer9b529a72015-02-16 04:02:09 +0000767 return VT->getNumElements();
768 return Ty->getStructNumElements();
769}
Chris Lattnerf7eb5432012-01-24 07:54:10 +0000770
Chris Lattner030af792012-01-24 05:42:11 +0000771//===----------------------------------------------------------------------===//
772// UndefValue Implementation
773//===----------------------------------------------------------------------===//
774
Chris Lattner7e683d12012-01-25 06:16:32 +0000775UndefValue *UndefValue::getSequentialElement() const {
Chris Lattner8326bd82012-01-26 00:42:34 +0000776 return UndefValue::get(getType()->getSequentialElementType());
Chris Lattner030af792012-01-24 05:42:11 +0000777}
778
Chris Lattner7e683d12012-01-25 06:16:32 +0000779UndefValue *UndefValue::getStructElement(unsigned Elt) const {
Chris Lattner8326bd82012-01-26 00:42:34 +0000780 return UndefValue::get(getType()->getStructElementType(Elt));
Chris Lattner030af792012-01-24 05:42:11 +0000781}
782
Chris Lattner7e683d12012-01-25 06:16:32 +0000783UndefValue *UndefValue::getElementValue(Constant *C) const {
Chris Lattner030af792012-01-24 05:42:11 +0000784 if (isa<SequentialType>(getType()))
785 return getSequentialElement();
786 return getStructElement(cast<ConstantInt>(C)->getZExtValue());
787}
788
Chris Lattner7e683d12012-01-25 06:16:32 +0000789UndefValue *UndefValue::getElementValue(unsigned Idx) const {
Chris Lattnerf7eb5432012-01-24 07:54:10 +0000790 if (isa<SequentialType>(getType()))
791 return getSequentialElement();
792 return getStructElement(Idx);
793}
794
David Majnemer9b529a72015-02-16 04:02:09 +0000795unsigned UndefValue::getNumElements() const {
Craig Toppere3dcce92015-08-01 22:20:21 +0000796 Type *Ty = getType();
Peter Collingbournebc070522016-12-02 03:20:58 +0000797 if (auto *ST = dyn_cast<SequentialType>(Ty))
798 return ST->getNumElements();
David Majnemer9b529a72015-02-16 04:02:09 +0000799 return Ty->getStructNumElements();
800}
Chris Lattner030af792012-01-24 05:42:11 +0000801
802//===----------------------------------------------------------------------===//
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000803// ConstantXXX Classes
804//===----------------------------------------------------------------------===//
805
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000806template <typename ItTy, typename EltTy>
807static bool rangeOnlyContains(ItTy Start, ItTy End, EltTy Elt) {
808 for (; Start != End; ++Start)
809 if (*Start != Elt)
810 return false;
811 return true;
812}
Chris Lattnerc6ee77d2007-02-20 07:17:17 +0000813
Justin Bogner909e1c02015-12-01 20:20:49 +0000814template <typename SequentialTy, typename ElementTy>
815static Constant *getIntSequenceIfElementsMatch(ArrayRef<Constant *> V) {
816 assert(!V.empty() && "Cannot get empty int sequence.");
817
818 SmallVector<ElementTy, 16> Elts;
819 for (Constant *C : V)
820 if (auto *CI = dyn_cast<ConstantInt>(C))
821 Elts.push_back(CI->getZExtValue());
822 else
823 return nullptr;
824 return SequentialTy::get(V[0]->getContext(), Elts);
825}
826
827template <typename SequentialTy, typename ElementTy>
828static Constant *getFPSequenceIfElementsMatch(ArrayRef<Constant *> V) {
829 assert(!V.empty() && "Cannot get empty FP sequence.");
830
831 SmallVector<ElementTy, 16> Elts;
832 for (Constant *C : V)
833 if (auto *CFP = dyn_cast<ConstantFP>(C))
834 Elts.push_back(CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
835 else
836 return nullptr;
837 return SequentialTy::getFP(V[0]->getContext(), Elts);
838}
839
840template <typename SequenceTy>
841static Constant *getSequenceIfElementsMatch(Constant *C,
842 ArrayRef<Constant *> V) {
843 // We speculatively build the elements here even if it turns out that there is
844 // a constantexpr or something else weird, since it is so uncommon for that to
845 // happen.
846 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
847 if (CI->getType()->isIntegerTy(8))
848 return getIntSequenceIfElementsMatch<SequenceTy, uint8_t>(V);
849 else if (CI->getType()->isIntegerTy(16))
850 return getIntSequenceIfElementsMatch<SequenceTy, uint16_t>(V);
851 else if (CI->getType()->isIntegerTy(32))
852 return getIntSequenceIfElementsMatch<SequenceTy, uint32_t>(V);
853 else if (CI->getType()->isIntegerTy(64))
854 return getIntSequenceIfElementsMatch<SequenceTy, uint64_t>(V);
855 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
Justin Bogner0ebc8602015-12-08 03:01:16 +0000856 if (CFP->getType()->isHalfTy())
857 return getFPSequenceIfElementsMatch<SequenceTy, uint16_t>(V);
858 else if (CFP->getType()->isFloatTy())
Justin Bogner909e1c02015-12-01 20:20:49 +0000859 return getFPSequenceIfElementsMatch<SequenceTy, uint32_t>(V);
860 else if (CFP->getType()->isDoubleTy())
861 return getFPSequenceIfElementsMatch<SequenceTy, uint64_t>(V);
862 }
863
864 return nullptr;
865}
866
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +0000867ConstantAggregate::ConstantAggregate(CompositeType *T, ValueTy VT,
868 ArrayRef<Constant *> V)
869 : Constant(T, VT, OperandTraits<ConstantAggregate>::op_end(this) - V.size(),
870 V.size()) {
Jay Foad89d9b812011-07-25 10:14:44 +0000871 std::copy(V.begin(), V.end(), op_begin());
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +0000872
873 // Check that types match, unless this is an opaque struct.
874 if (auto *ST = dyn_cast<StructType>(T))
875 if (ST->isOpaque())
876 return;
877 for (unsigned I = 0, E = V.size(); I != E; ++I)
878 assert(V[I]->getType() == T->getTypeAtIndex(I) &&
879 "Initializer for composite element doesn't match!");
880}
881
882ConstantArray::ConstantArray(ArrayType *T, ArrayRef<Constant *> V)
883 : ConstantAggregate(T, ConstantArrayVal, V) {
884 assert(V.size() == T->getNumElements() &&
885 "Invalid initializer for constant array");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000886}
887
Chris Lattner229907c2011-07-18 04:54:35 +0000888Constant *ConstantArray::get(ArrayType *Ty, ArrayRef<Constant*> V) {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000889 if (Constant *C = getImpl(Ty, V))
890 return C;
891 return Ty->getContext().pImpl->ArrayConstants.getOrCreate(Ty, V);
892}
Justin Bogner909e1c02015-12-01 20:20:49 +0000893
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000894Constant *ConstantArray::getImpl(ArrayType *Ty, ArrayRef<Constant*> V) {
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000895 // Empty arrays are canonicalized to ConstantAggregateZero.
896 if (V.empty())
897 return ConstantAggregateZero::get(Ty);
898
Jeffrey Yasskin8ce67f82009-09-30 21:08:08 +0000899 for (unsigned i = 0, e = V.size(); i != e; ++i) {
900 assert(V[i]->getType() == Ty->getElementType() &&
901 "Wrong type in array element initializer");
902 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000903
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000904 // If this is an all-zero array, return a ConstantAggregateZero object. If
905 // all undef, return an UndefValue, if "all simple", then return a
906 // ConstantDataArray.
907 Constant *C = V[0];
908 if (isa<UndefValue>(C) && rangeOnlyContains(V.begin(), V.end(), C))
909 return UndefValue::get(Ty);
Chris Lattnerf14a67f2012-01-26 02:31:22 +0000910
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000911 if (C->isNullValue() && rangeOnlyContains(V.begin(), V.end(), C))
912 return ConstantAggregateZero::get(Ty);
913
914 // Check to see if all of the elements are ConstantFP or ConstantInt and if
915 // the element type is compatible with ConstantDataVector. If so, use it.
Justin Bogner909e1c02015-12-01 20:20:49 +0000916 if (ConstantDataSequential::isElementTypeCompatible(C->getType()))
917 return getSequenceIfElementsMatch<ConstantDataArray>(C, V);
Chris Lattnerf14a67f2012-01-26 02:31:22 +0000918
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000919 // Otherwise, we really do want to create a ConstantArray.
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000920 return nullptr;
Owen Andersonc2c79322009-07-28 18:32:17 +0000921}
922
Chris Lattnercc19efa2011-06-20 04:01:31 +0000923StructType *ConstantStruct::getTypeForElements(LLVMContext &Context,
924 ArrayRef<Constant*> V,
925 bool Packed) {
Bill Wendling3ae7dd32012-02-07 01:27:51 +0000926 unsigned VecSize = V.size();
927 SmallVector<Type*, 16> EltTypes(VecSize);
928 for (unsigned i = 0; i != VecSize; ++i)
929 EltTypes[i] = V[i]->getType();
Galina Kistanovafc259902012-07-13 01:25:27 +0000930
Chris Lattnercc19efa2011-06-20 04:01:31 +0000931 return StructType::get(Context, EltTypes, Packed);
932}
933
934
935StructType *ConstantStruct::getTypeForElements(ArrayRef<Constant*> V,
936 bool Packed) {
937 assert(!V.empty() &&
938 "ConstantStruct::getTypeForElements cannot be called on empty list");
939 return getTypeForElements(V[0]->getContext(), V, Packed);
940}
941
Jay Foad89d9b812011-07-25 10:14:44 +0000942ConstantStruct::ConstantStruct(StructType *T, ArrayRef<Constant *> V)
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +0000943 : ConstantAggregate(T, ConstantStructVal, V) {
944 assert((T->isOpaque() || V.size() == T->getNumElements()) &&
945 "Invalid initializer for constant struct");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000946}
947
Owen Anderson45308b52009-07-27 22:29:26 +0000948// ConstantStruct accessors.
Chris Lattner229907c2011-07-18 04:54:35 +0000949Constant *ConstantStruct::get(StructType *ST, ArrayRef<Constant*> V) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000950 assert((ST->isOpaque() || ST->getNumElements() == V.size()) &&
951 "Incorrect # elements specified to ConstantStruct::get");
Chris Lattnerf14a67f2012-01-26 02:31:22 +0000952
953 // Create a ConstantAggregateZero value if all elements are zeros.
954 bool isZero = true;
955 bool isUndef = false;
956
957 if (!V.empty()) {
958 isUndef = isa<UndefValue>(V[0]);
959 isZero = V[0]->isNullValue();
960 if (isUndef || isZero) {
961 for (unsigned i = 0, e = V.size(); i != e; ++i) {
962 if (!V[i]->isNullValue())
963 isZero = false;
964 if (!isa<UndefValue>(V[i]))
965 isUndef = false;
966 }
967 }
Galina Kistanovafc259902012-07-13 01:25:27 +0000968 }
Chris Lattnerf14a67f2012-01-26 02:31:22 +0000969 if (isZero)
970 return ConstantAggregateZero::get(ST);
971 if (isUndef)
972 return UndefValue::get(ST);
Galina Kistanovafc259902012-07-13 01:25:27 +0000973
Chris Lattnerf14a67f2012-01-26 02:31:22 +0000974 return ST->getContext().pImpl->StructConstants.getOrCreate(ST, V);
Owen Anderson45308b52009-07-27 22:29:26 +0000975}
976
Jay Foad89d9b812011-07-25 10:14:44 +0000977ConstantVector::ConstantVector(VectorType *T, ArrayRef<Constant *> V)
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +0000978 : ConstantAggregate(T, ConstantVectorVal, V) {
Duncan P. N. Exon Smithdb63bda2016-04-05 20:53:47 +0000979 assert(V.size() == T->getNumElements() &&
Duncan P. N. Exon Smith1de3c7e2016-04-05 21:10:45 +0000980 "Invalid initializer for constant vector");
Brian Gaeke02209042004-08-20 06:00:58 +0000981}
982
Owen Anderson4aa32952009-07-28 21:19:26 +0000983// ConstantVector accessors.
Jay Foadb8a8bed32011-06-22 09:10:19 +0000984Constant *ConstantVector::get(ArrayRef<Constant*> V) {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000985 if (Constant *C = getImpl(V))
986 return C;
987 VectorType *Ty = VectorType::get(V.front()->getType(), V.size());
988 return Ty->getContext().pImpl->VectorConstants.getOrCreate(Ty, V);
989}
Justin Bogner909e1c02015-12-01 20:20:49 +0000990
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000991Constant *ConstantVector::getImpl(ArrayRef<Constant*> V) {
Jay Foad9f32cfd2011-01-27 14:44:55 +0000992 assert(!V.empty() && "Vectors can't be empty");
Chris Lattner229907c2011-07-18 04:54:35 +0000993 VectorType *T = VectorType::get(V.front()->getType(), V.size());
Jay Foad9f32cfd2011-01-27 14:44:55 +0000994
Chris Lattner69229312011-02-15 00:14:00 +0000995 // If this is an all-undef or all-zero vector, return a
Owen Anderson4aa32952009-07-28 21:19:26 +0000996 // ConstantAggregateZero or UndefValue.
997 Constant *C = V[0];
998 bool isZero = C->isNullValue();
999 bool isUndef = isa<UndefValue>(C);
1000
1001 if (isZero || isUndef) {
1002 for (unsigned i = 1, e = V.size(); i != e; ++i)
1003 if (V[i] != C) {
1004 isZero = isUndef = false;
1005 break;
1006 }
1007 }
Galina Kistanovafc259902012-07-13 01:25:27 +00001008
Owen Anderson4aa32952009-07-28 21:19:26 +00001009 if (isZero)
Owen Andersonb292b8c2009-07-30 23:03:37 +00001010 return ConstantAggregateZero::get(T);
Owen Anderson4aa32952009-07-28 21:19:26 +00001011 if (isUndef)
Owen Andersonb292b8c2009-07-30 23:03:37 +00001012 return UndefValue::get(T);
Galina Kistanovafc259902012-07-13 01:25:27 +00001013
Chris Lattner978fe0c2012-01-30 06:21:21 +00001014 // Check to see if all of the elements are ConstantFP or ConstantInt and if
1015 // the element type is compatible with ConstantDataVector. If so, use it.
Justin Bogner909e1c02015-12-01 20:20:49 +00001016 if (ConstantDataSequential::isElementTypeCompatible(C->getType()))
1017 return getSequenceIfElementsMatch<ConstantDataVector>(C, V);
Galina Kistanovafc259902012-07-13 01:25:27 +00001018
Chris Lattner978fe0c2012-01-30 06:21:21 +00001019 // Otherwise, the element type isn't compatible with ConstantDataVector, or
Craig Topperdf907262017-04-11 06:41:55 +00001020 // the operand list contains a ConstantExpr or something else strange.
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001021 return nullptr;
Owen Anderson4aa32952009-07-28 21:19:26 +00001022}
1023
Chris Lattnere9eed292012-01-25 05:19:54 +00001024Constant *ConstantVector::getSplat(unsigned NumElts, Constant *V) {
Chris Lattner978fe0c2012-01-30 06:21:21 +00001025 // If this splat is compatible with ConstantDataVector, use it instead of
1026 // ConstantVector.
1027 if ((isa<ConstantFP>(V) || isa<ConstantInt>(V)) &&
1028 ConstantDataSequential::isElementTypeCompatible(V->getType()))
1029 return ConstantDataVector::getSplat(NumElts, V);
Galina Kistanovafc259902012-07-13 01:25:27 +00001030
Chris Lattnere9eed292012-01-25 05:19:54 +00001031 SmallVector<Constant*, 32> Elts(NumElts, V);
1032 return get(Elts);
1033}
1034
David Majnemerf0f224d2015-11-11 21:57:16 +00001035ConstantTokenNone *ConstantTokenNone::get(LLVMContext &Context) {
1036 LLVMContextImpl *pImpl = Context.pImpl;
1037 if (!pImpl->TheNoneToken)
David Majnemer2dd41c52015-11-16 20:55:57 +00001038 pImpl->TheNoneToken.reset(new ConstantTokenNone(Context));
1039 return pImpl->TheNoneToken.get();
David Majnemerf0f224d2015-11-11 21:57:16 +00001040}
1041
1042/// Remove the constant from the constant table.
1043void ConstantTokenNone::destroyConstantImpl() {
1044 llvm_unreachable("You can't ConstantTokenNone->destroyConstantImpl()!");
1045}
Chris Lattnere9eed292012-01-25 05:19:54 +00001046
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001047// Utility function for determining if a ConstantExpr is a CastOp or not. This
1048// can't be inline because we don't want to #include Instruction.h into
1049// Constant.h
1050bool ConstantExpr::isCast() const {
1051 return Instruction::isCast(getOpcode());
1052}
1053
Reid Spenceree3c9912006-12-04 05:19:50 +00001054bool ConstantExpr::isCompare() const {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001055 return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
Reid Spenceree3c9912006-12-04 05:19:50 +00001056}
1057
Dan Gohman7190d482009-09-10 23:37:55 +00001058bool ConstantExpr::isGEPWithNoNotionalOverIndexing() const {
1059 if (getOpcode() != Instruction::GetElementPtr) return false;
1060
1061 gep_type_iterator GEPI = gep_type_begin(this), E = gep_type_end(this);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001062 User::const_op_iterator OI = std::next(this->op_begin());
Dan Gohman7190d482009-09-10 23:37:55 +00001063
Sanjay Patel81ed3492016-12-11 20:07:02 +00001064 // The remaining indices may be compile-time known integers within the bounds
1065 // of the corresponding notional static array types.
Dan Gohman7190d482009-09-10 23:37:55 +00001066 for (; GEPI != E; ++GEPI, ++OI) {
Sanjay Patel81ed3492016-12-11 20:07:02 +00001067 if (isa<UndefValue>(*OI))
1068 continue;
1069 auto *CI = dyn_cast<ConstantInt>(*OI);
1070 if (!CI || (GEPI.isBoundedSequential() &&
1071 (CI->getValue().getActiveBits() > 64 ||
1072 CI->getZExtValue() >= GEPI.getSequentialNumElements())))
Peter Collingbourneab85225b2016-12-02 02:24:42 +00001073 return false;
Dan Gohman7190d482009-09-10 23:37:55 +00001074 }
1075
1076 // All the indices checked out.
1077 return true;
1078}
1079
Dan Gohman1ecaf452008-05-31 00:58:22 +00001080bool ConstantExpr::hasIndices() const {
1081 return getOpcode() == Instruction::ExtractValue ||
1082 getOpcode() == Instruction::InsertValue;
1083}
1084
Jay Foad0091fe82011-04-13 15:22:40 +00001085ArrayRef<unsigned> ConstantExpr::getIndices() const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001086 if (const ExtractValueConstantExpr *EVCE =
1087 dyn_cast<ExtractValueConstantExpr>(this))
1088 return EVCE->Indices;
Dan Gohmana469bdb2008-06-23 16:39:44 +00001089
1090 return cast<InsertValueConstantExpr>(this)->Indices;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001091}
1092
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001093unsigned ConstantExpr::getPredicate() const {
Craig Toppercc03b492015-12-15 06:11:36 +00001094 return cast<CompareConstantExpr>(this)->predicate;
Reid Spencer10fbf0e2006-12-03 05:48:19 +00001095}
Chris Lattner60e0dd72001-10-03 06:12:09 +00001096
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001097Constant *
1098ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
Chris Lattner7c1018a2006-07-14 19:37:40 +00001099 assert(Op->getType() == getOperand(OpNo)->getType() &&
1100 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +00001101 if (getOperand(OpNo) == Op)
1102 return const_cast<ConstantExpr*>(this);
Chris Lattner37e38352012-01-26 20:37:11 +00001103
1104 SmallVector<Constant*, 8> NewOps;
1105 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1106 NewOps.push_back(i == OpNo ? Op : getOperand(i));
Galina Kistanovafc259902012-07-13 01:25:27 +00001107
Chris Lattner37e38352012-01-26 20:37:11 +00001108 return getWithOperands(NewOps);
Chris Lattner227816342006-07-14 22:20:01 +00001109}
1110
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001111Constant *ConstantExpr::getWithOperands(ArrayRef<Constant *> Ops, Type *Ty,
David Blaikie88208842015-08-21 20:16:51 +00001112 bool OnlyIfReduced, Type *SrcTy) const {
Jay Foad5c984e562011-04-13 13:46:01 +00001113 assert(Ops.size() == getNumOperands() && "Operand count mismatch!");
Galina Kistanovafc259902012-07-13 01:25:27 +00001114
Benjamin Kramer8008e9f2015-03-02 11:57:04 +00001115 // If no operands changed return self.
1116 if (Ty == getType() && std::equal(Ops.begin(), Ops.end(), op_begin()))
Chris Lattner227816342006-07-14 22:20:01 +00001117 return const_cast<ConstantExpr*>(this);
1118
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001119 Type *OnlyIfReducedTy = OnlyIfReduced ? Ty : nullptr;
Chris Lattner227816342006-07-14 22:20:01 +00001120 switch (getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001121 case Instruction::Trunc:
1122 case Instruction::ZExt:
1123 case Instruction::SExt:
1124 case Instruction::FPTrunc:
1125 case Instruction::FPExt:
1126 case Instruction::UIToFP:
1127 case Instruction::SIToFP:
1128 case Instruction::FPToUI:
1129 case Instruction::FPToSI:
1130 case Instruction::PtrToInt:
1131 case Instruction::IntToPtr:
1132 case Instruction::BitCast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001133 case Instruction::AddrSpaceCast:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001134 return ConstantExpr::getCast(getOpcode(), Ops[0], Ty, OnlyIfReduced);
Chris Lattner227816342006-07-14 22:20:01 +00001135 case Instruction::Select:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001136 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2], OnlyIfReducedTy);
Chris Lattner227816342006-07-14 22:20:01 +00001137 case Instruction::InsertElement:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001138 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2],
1139 OnlyIfReducedTy);
Chris Lattner227816342006-07-14 22:20:01 +00001140 case Instruction::ExtractElement:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001141 return ConstantExpr::getExtractElement(Ops[0], Ops[1], OnlyIfReducedTy);
Chris Lattner37e38352012-01-26 20:37:11 +00001142 case Instruction::InsertValue:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001143 return ConstantExpr::getInsertValue(Ops[0], Ops[1], getIndices(),
1144 OnlyIfReducedTy);
Chris Lattner37e38352012-01-26 20:37:11 +00001145 case Instruction::ExtractValue:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001146 return ConstantExpr::getExtractValue(Ops[0], getIndices(), OnlyIfReducedTy);
Chris Lattner227816342006-07-14 22:20:01 +00001147 case Instruction::ShuffleVector:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001148 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2],
1149 OnlyIfReducedTy);
David Blaikie88208842015-08-21 20:16:51 +00001150 case Instruction::GetElementPtr: {
1151 auto *GEPO = cast<GEPOperator>(this);
1152 assert(SrcTy || (Ops[0]->getType() == getOperand(0)->getType()));
1153 return ConstantExpr::getGetElementPtr(
1154 SrcTy ? SrcTy : GEPO->getSourceElementType(), Ops[0], Ops.slice(1),
Peter Collingbourned93620b2016-11-10 22:34:55 +00001155 GEPO->isInBounds(), GEPO->getInRangeIndex(), OnlyIfReducedTy);
David Blaikie88208842015-08-21 20:16:51 +00001156 }
Reid Spencer266e42b2006-12-23 06:05:41 +00001157 case Instruction::ICmp:
1158 case Instruction::FCmp:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001159 return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1],
1160 OnlyIfReducedTy);
Chris Lattner227816342006-07-14 22:20:01 +00001161 default:
1162 assert(getNumOperands() == 2 && "Must be binary operator?");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001163 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1], SubclassOptionalData,
1164 OnlyIfReducedTy);
Chris Lattner7c1018a2006-07-14 19:37:40 +00001165 }
1166}
1167
Chris Lattner2f7c9632001-06-06 20:29:01 +00001168
1169//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00001170// isValueValidForType implementations
1171
Chris Lattner229907c2011-07-18 04:54:35 +00001172bool ConstantInt::isValueValidForType(Type *Ty, uint64_t Val) {
Chris Lattner8326bd82012-01-26 00:42:34 +00001173 unsigned NumBits = Ty->getIntegerBitWidth(); // assert okay
1174 if (Ty->isIntegerTy(1))
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001175 return Val == 0 || Val == 1;
Craig Topper50b1e512017-06-07 00:58:05 +00001176 return isUIntN(NumBits, Val);
Reid Spencere7334722006-12-19 01:28:19 +00001177}
1178
Chris Lattner229907c2011-07-18 04:54:35 +00001179bool ConstantInt::isValueValidForType(Type *Ty, int64_t Val) {
Chris Lattner8326bd82012-01-26 00:42:34 +00001180 unsigned NumBits = Ty->getIntegerBitWidth();
1181 if (Ty->isIntegerTy(1))
Reid Spencera94d3942007-01-19 21:13:56 +00001182 return Val == 0 || Val == 1 || Val == -1;
Craig Topper50b1e512017-06-07 00:58:05 +00001183 return isIntN(NumBits, Val);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001184}
1185
Chris Lattner229907c2011-07-18 04:54:35 +00001186bool ConstantFP::isValueValidForType(Type *Ty, const APFloat& Val) {
Dale Johannesend246b2c2007-08-30 00:23:21 +00001187 // convert modifies in place, so make a copy.
1188 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001189 bool losesInfo;
Chris Lattner6b727592004-06-17 18:19:28 +00001190 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001191 default:
1192 return false; // These can't be represented as floating point!
1193
Dale Johannesend246b2c2007-08-30 00:23:21 +00001194 // FIXME rounding mode needs to be more flexible
Dan Gohman518cda42011-12-17 00:04:22 +00001195 case Type::HalfTyID: {
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001196 if (&Val2.getSemantics() == &APFloat::IEEEhalf())
Dan Gohman518cda42011-12-17 00:04:22 +00001197 return true;
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001198 Val2.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &losesInfo);
Dan Gohman518cda42011-12-17 00:04:22 +00001199 return !losesInfo;
1200 }
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001201 case Type::FloatTyID: {
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001202 if (&Val2.getSemantics() == &APFloat::IEEEsingle())
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001203 return true;
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001204 Val2.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &losesInfo);
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001205 return !losesInfo;
1206 }
1207 case Type::DoubleTyID: {
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001208 if (&Val2.getSemantics() == &APFloat::IEEEhalf() ||
1209 &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1210 &Val2.getSemantics() == &APFloat::IEEEdouble())
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001211 return true;
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001212 Val2.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &losesInfo);
Dale Johannesen4f0bd682008-10-09 23:00:39 +00001213 return !losesInfo;
1214 }
Dale Johannesenbdad8092007-08-09 22:51:36 +00001215 case Type::X86_FP80TyID:
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001216 return &Val2.getSemantics() == &APFloat::IEEEhalf() ||
1217 &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1218 &Val2.getSemantics() == &APFloat::IEEEdouble() ||
1219 &Val2.getSemantics() == &APFloat::x87DoubleExtended();
Dale Johannesenbdad8092007-08-09 22:51:36 +00001220 case Type::FP128TyID:
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001221 return &Val2.getSemantics() == &APFloat::IEEEhalf() ||
1222 &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1223 &Val2.getSemantics() == &APFloat::IEEEdouble() ||
1224 &Val2.getSemantics() == &APFloat::IEEEquad();
Dale Johannesen007aa372007-10-11 18:07:22 +00001225 case Type::PPC_FP128TyID:
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001226 return &Val2.getSemantics() == &APFloat::IEEEhalf() ||
1227 &Val2.getSemantics() == &APFloat::IEEEsingle() ||
1228 &Val2.getSemantics() == &APFloat::IEEEdouble() ||
1229 &Val2.getSemantics() == &APFloat::PPCDoubleDouble();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001230 }
Chris Lattneraa2372562006-05-24 17:04:05 +00001231}
Chris Lattner9655e542001-07-20 19:16:02 +00001232
Chris Lattner030af792012-01-24 05:42:11 +00001233
Chris Lattner49d855c2001-09-07 16:46:31 +00001234//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +00001235// Factory Function Implementation
1236
Chris Lattnerc7f9fd42012-01-23 15:20:12 +00001237ConstantAggregateZero *ConstantAggregateZero::get(Type *Ty) {
Chris Lattner13ee7952010-08-28 04:09:24 +00001238 assert((Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy()) &&
Owen Andersonb292b8c2009-07-30 23:03:37 +00001239 "Cannot create an aggregate zero of non-aggregate type!");
David Blaikie899b85a2014-11-25 02:13:54 +00001240
Justin Lebar611c5c22016-10-10 16:26:13 +00001241 std::unique_ptr<ConstantAggregateZero> &Entry =
1242 Ty->getContext().pImpl->CAZConstants[Ty];
1243 if (!Entry)
1244 Entry.reset(new ConstantAggregateZero(Ty));
1245
1246 return Entry.get();
Owen Andersonb292b8c2009-07-30 23:03:37 +00001247}
1248
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001249/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001250void ConstantAggregateZero::destroyConstantImpl() {
Chris Lattnerc7f9fd42012-01-23 15:20:12 +00001251 getContext().pImpl->CAZConstants.erase(getType());
Chris Lattner9fba3da2004-02-15 05:53:04 +00001252}
1253
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001254/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001255void ConstantArray::destroyConstantImpl() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001256 getType()->getContext().pImpl->ArrayConstants.remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001257}
1258
Chris Lattner81fabb02002-08-26 17:53:56 +00001259
Chris Lattner3462ae32001-12-03 22:26:30 +00001260//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001261//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001262
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001263/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001264void ConstantStruct::destroyConstantImpl() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001265 getType()->getContext().pImpl->StructConstants.remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001266}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001267
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001268/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001269void ConstantVector::destroyConstantImpl() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001270 getType()->getContext().pImpl->VectorConstants.remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001271}
1272
Duncan Sandse6beec62012-11-13 12:59:33 +00001273Constant *Constant::getSplatValue() const {
1274 assert(this->getType()->isVectorTy() && "Only valid for vectors!");
1275 if (isa<ConstantAggregateZero>(this))
1276 return getNullValue(this->getType()->getVectorElementType());
1277 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this))
1278 return CV->getSplatValue();
1279 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
1280 return CV->getSplatValue();
Craig Topperc6207612014-04-09 06:08:46 +00001281 return nullptr;
Duncan Sandse6beec62012-11-13 12:59:33 +00001282}
1283
Duncan Sandscf0ff032011-02-01 08:39:12 +00001284Constant *ConstantVector::getSplatValue() const {
Dan Gohman07159202007-10-17 17:51:30 +00001285 // Check out first element.
1286 Constant *Elt = getOperand(0);
1287 // Then make sure all remaining elements point to the same value.
1288 for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
Chris Lattnerd04e32d2011-07-17 06:01:30 +00001289 if (getOperand(I) != Elt)
Craig Topperc6207612014-04-09 06:08:46 +00001290 return nullptr;
Dan Gohman07159202007-10-17 17:51:30 +00001291 return Elt;
1292}
1293
Duncan Sandse6beec62012-11-13 12:59:33 +00001294const APInt &Constant::getUniqueInteger() const {
1295 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
1296 return CI->getValue();
1297 assert(this->getSplatValue() && "Doesn't contain a unique integer!");
1298 const Constant *C = this->getAggregateElement(0U);
1299 assert(C && isa<ConstantInt>(C) && "Not a vector of numbers!");
1300 return cast<ConstantInt>(C)->getValue();
1301}
1302
Chris Lattner31b132c2009-10-28 00:01:44 +00001303//---- ConstantPointerNull::get() implementation.
Chris Lattnerd7a73302001-10-13 06:57:33 +00001304//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001305
Chris Lattner229907c2011-07-18 04:54:35 +00001306ConstantPointerNull *ConstantPointerNull::get(PointerType *Ty) {
Justin Lebar611c5c22016-10-10 16:26:13 +00001307 std::unique_ptr<ConstantPointerNull> &Entry =
1308 Ty->getContext().pImpl->CPNConstants[Ty];
Craig Topperc6207612014-04-09 06:08:46 +00001309 if (!Entry)
Justin Lebar611c5c22016-10-10 16:26:13 +00001310 Entry.reset(new ConstantPointerNull(Ty));
Galina Kistanovafc259902012-07-13 01:25:27 +00001311
Justin Lebar611c5c22016-10-10 16:26:13 +00001312 return Entry.get();
Chris Lattner883ad0b2001-10-03 15:39:36 +00001313}
1314
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001315/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001316void ConstantPointerNull::destroyConstantImpl() {
Chris Lattnerc7f9fd42012-01-23 15:20:12 +00001317 getContext().pImpl->CPNConstants.erase(getType());
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001318}
1319
Chris Lattner229907c2011-07-18 04:54:35 +00001320UndefValue *UndefValue::get(Type *Ty) {
Justin Lebar611c5c22016-10-10 16:26:13 +00001321 std::unique_ptr<UndefValue> &Entry = Ty->getContext().pImpl->UVConstants[Ty];
Craig Topperc6207612014-04-09 06:08:46 +00001322 if (!Entry)
Justin Lebar611c5c22016-10-10 16:26:13 +00001323 Entry.reset(new UndefValue(Ty));
Galina Kistanovafc259902012-07-13 01:25:27 +00001324
Justin Lebar611c5c22016-10-10 16:26:13 +00001325 return Entry.get();
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001326}
1327
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001328/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001329void UndefValue::destroyConstantImpl() {
Chris Lattnerc7f9fd42012-01-23 15:20:12 +00001330 // Free the constant and any dangling references to it.
1331 getContext().pImpl->UVConstants.erase(getType());
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001332}
1333
Chris Lattner31b132c2009-10-28 00:01:44 +00001334BlockAddress *BlockAddress::get(BasicBlock *BB) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001335 assert(BB->getParent() && "Block must have a parent");
Chris Lattner31b132c2009-10-28 00:01:44 +00001336 return get(BB->getParent(), BB);
1337}
1338
1339BlockAddress *BlockAddress::get(Function *F, BasicBlock *BB) {
1340 BlockAddress *&BA =
1341 F->getContext().pImpl->BlockAddresses[std::make_pair(F, BB)];
Craig Topperc6207612014-04-09 06:08:46 +00001342 if (!BA)
Chris Lattner31b132c2009-10-28 00:01:44 +00001343 BA = new BlockAddress(F, BB);
Galina Kistanovafc259902012-07-13 01:25:27 +00001344
Chris Lattner31b132c2009-10-28 00:01:44 +00001345 assert(BA->getFunction() == F && "Basic block moved between functions");
1346 return BA;
1347}
1348
1349BlockAddress::BlockAddress(Function *F, BasicBlock *BB)
1350: Constant(Type::getInt8PtrTy(F->getContext()), Value::BlockAddressVal,
1351 &Op<0>(), 2) {
Chris Lattnerc559a9f2009-11-01 03:03:03 +00001352 setOperand(0, F);
1353 setOperand(1, BB);
Chris Lattneraa99c942009-11-01 01:27:45 +00001354 BB->AdjustBlockAddressRefCount(1);
Chris Lattner31b132c2009-10-28 00:01:44 +00001355}
1356
Chandler Carruth6a936922014-01-19 02:13:50 +00001357BlockAddress *BlockAddress::lookup(const BasicBlock *BB) {
1358 if (!BB->hasAddressTaken())
Craig Topperc6207612014-04-09 06:08:46 +00001359 return nullptr;
Chandler Carruth6a936922014-01-19 02:13:50 +00001360
1361 const Function *F = BB->getParent();
Craig Topper2617dcc2014-04-15 06:32:26 +00001362 assert(F && "Block must have a parent");
Chandler Carruth6a936922014-01-19 02:13:50 +00001363 BlockAddress *BA =
1364 F->getContext().pImpl->BlockAddresses.lookup(std::make_pair(F, BB));
1365 assert(BA && "Refcount and block address map disagree!");
1366 return BA;
1367}
Chris Lattner31b132c2009-10-28 00:01:44 +00001368
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00001369/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00001370void BlockAddress::destroyConstantImpl() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001371 getFunction()->getType()->getContext().pImpl
Chris Lattner31b132c2009-10-28 00:01:44 +00001372 ->BlockAddresses.erase(std::make_pair(getFunction(), getBasicBlock()));
Chris Lattneraa99c942009-11-01 01:27:45 +00001373 getBasicBlock()->AdjustBlockAddressRefCount(-1);
Chris Lattner31b132c2009-10-28 00:01:44 +00001374}
1375
Mehdi Amini8914d292016-02-10 22:47:15 +00001376Value *BlockAddress::handleOperandChangeImpl(Value *From, Value *To) {
Chris Lattner31b132c2009-10-28 00:01:44 +00001377 // This could be replacing either the Basic Block or the Function. In either
1378 // case, we have to remove the map entry.
1379 Function *NewF = getFunction();
1380 BasicBlock *NewBB = getBasicBlock();
Galina Kistanovafc259902012-07-13 01:25:27 +00001381
Mehdi Amini8914d292016-02-10 22:47:15 +00001382 if (From == NewF)
Derek Schuffec9dc012013-06-13 19:51:17 +00001383 NewF = cast<Function>(To->stripPointerCasts());
Mehdi Amini8914d292016-02-10 22:47:15 +00001384 else {
1385 assert(From == NewBB && "From does not match any operand");
Chris Lattner31b132c2009-10-28 00:01:44 +00001386 NewBB = cast<BasicBlock>(To);
Mehdi Amini8914d292016-02-10 22:47:15 +00001387 }
Galina Kistanovafc259902012-07-13 01:25:27 +00001388
Chris Lattner31b132c2009-10-28 00:01:44 +00001389 // See if the 'new' entry already exists, if not, just update this in place
1390 // and return early.
1391 BlockAddress *&NewBA =
1392 getContext().pImpl->BlockAddresses[std::make_pair(NewF, NewBB)];
Pete Cooper5815b1f2015-06-24 18:55:24 +00001393 if (NewBA)
1394 return NewBA;
Chris Lattner31b132c2009-10-28 00:01:44 +00001395
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001396 getBasicBlock()->AdjustBlockAddressRefCount(-1);
Galina Kistanovafc259902012-07-13 01:25:27 +00001397
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001398 // Remove the old entry, this can't cause the map to rehash (just a
1399 // tombstone will get added).
1400 getContext().pImpl->BlockAddresses.erase(std::make_pair(getFunction(),
1401 getBasicBlock()));
1402 NewBA = this;
1403 setOperand(0, NewF);
1404 setOperand(1, NewBB);
1405 getBasicBlock()->AdjustBlockAddressRefCount(1);
Pete Cooper5815b1f2015-06-24 18:55:24 +00001406
1407 // If we just want to keep the existing value, then return null.
1408 // Callers know that this means we shouldn't delete this value.
1409 return nullptr;
Chris Lattner31b132c2009-10-28 00:01:44 +00001410}
1411
1412//---- ConstantExpr::get() implementations.
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001413//
Reid Spencer8d9336d2006-12-31 05:26:44 +00001414
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001415/// This is a utility function to handle folding of casts and lookup of the
Duncan Sands7d6c8ae2008-03-30 19:38:55 +00001416/// cast in the ExprConstants map. It is used by the various get* methods below.
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001417static Constant *getFoldedCast(Instruction::CastOps opc, Constant *C, Type *Ty,
1418 bool OnlyIfReduced = false) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001419 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001420 // Fold a few common cases
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001421 if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001422 return FC;
Chris Lattneracdbe712003-04-17 19:24:48 +00001423
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001424 if (OnlyIfReduced)
1425 return nullptr;
1426
Owen Anderson1584a292009-08-04 20:25:11 +00001427 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
1428
Nadav Rotem88330432013-03-07 01:30:40 +00001429 // Look up the constant in the table first to ensure uniqueness.
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001430 ConstantExprKeyType Key(opc, C);
Galina Kistanovafc259902012-07-13 01:25:27 +00001431
Owen Anderson1584a292009-08-04 20:25:11 +00001432 return pImpl->ExprConstants.getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001433}
Galina Kistanovafc259902012-07-13 01:25:27 +00001434
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001435Constant *ConstantExpr::getCast(unsigned oc, Constant *C, Type *Ty,
1436 bool OnlyIfReduced) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001437 Instruction::CastOps opc = Instruction::CastOps(oc);
1438 assert(Instruction::isCast(opc) && "opcode out of range");
1439 assert(C && Ty && "Null arguments to getCast");
Chris Lattner37bc78a2010-01-26 21:51:43 +00001440 assert(CastInst::castIsValid(opc, C, Ty) && "Invalid constantexpr cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001441
1442 switch (opc) {
Chris Lattner37bc78a2010-01-26 21:51:43 +00001443 default:
1444 llvm_unreachable("Invalid cast opcode");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001445 case Instruction::Trunc:
1446 return getTrunc(C, Ty, OnlyIfReduced);
1447 case Instruction::ZExt:
1448 return getZExt(C, Ty, OnlyIfReduced);
1449 case Instruction::SExt:
1450 return getSExt(C, Ty, OnlyIfReduced);
1451 case Instruction::FPTrunc:
1452 return getFPTrunc(C, Ty, OnlyIfReduced);
1453 case Instruction::FPExt:
1454 return getFPExtend(C, Ty, OnlyIfReduced);
1455 case Instruction::UIToFP:
1456 return getUIToFP(C, Ty, OnlyIfReduced);
1457 case Instruction::SIToFP:
1458 return getSIToFP(C, Ty, OnlyIfReduced);
1459 case Instruction::FPToUI:
1460 return getFPToUI(C, Ty, OnlyIfReduced);
1461 case Instruction::FPToSI:
1462 return getFPToSI(C, Ty, OnlyIfReduced);
1463 case Instruction::PtrToInt:
1464 return getPtrToInt(C, Ty, OnlyIfReduced);
1465 case Instruction::IntToPtr:
1466 return getIntToPtr(C, Ty, OnlyIfReduced);
1467 case Instruction::BitCast:
1468 return getBitCast(C, Ty, OnlyIfReduced);
1469 case Instruction::AddrSpaceCast:
1470 return getAddrSpaceCast(C, Ty, OnlyIfReduced);
Chris Lattner1ece6f82005-01-01 15:59:57 +00001471 }
Galina Kistanovafc259902012-07-13 01:25:27 +00001472}
Reid Spencerf37dc652006-12-05 19:14:13 +00001473
Chris Lattner229907c2011-07-18 04:54:35 +00001474Constant *ConstantExpr::getZExtOrBitCast(Constant *C, Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001475 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001476 return getBitCast(C, Ty);
1477 return getZExt(C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001478}
1479
Chris Lattner229907c2011-07-18 04:54:35 +00001480Constant *ConstantExpr::getSExtOrBitCast(Constant *C, Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001481 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001482 return getBitCast(C, Ty);
1483 return getSExt(C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001484}
1485
Chris Lattner229907c2011-07-18 04:54:35 +00001486Constant *ConstantExpr::getTruncOrBitCast(Constant *C, Type *Ty) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001487 if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001488 return getBitCast(C, Ty);
1489 return getTrunc(C, Ty);
Reid Spencer5c140882006-12-04 20:17:56 +00001490}
1491
Chris Lattner229907c2011-07-18 04:54:35 +00001492Constant *ConstantExpr::getPointerCast(Constant *S, Type *Ty) {
Evgeniy Stepanov23382642013-01-16 14:41:46 +00001493 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
1494 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
1495 "Invalid cast");
Reid Spencerbc245a02006-12-05 03:25:26 +00001496
Evgeniy Stepanov23382642013-01-16 14:41:46 +00001497 if (Ty->isIntOrIntVectorTy())
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001498 return getPtrToInt(S, Ty);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001499
1500 unsigned SrcAS = S->getType()->getPointerAddressSpace();
1501 if (Ty->isPtrOrPtrVectorTy() && SrcAS != Ty->getPointerAddressSpace())
1502 return getAddrSpaceCast(S, Ty);
1503
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001504 return getBitCast(S, Ty);
Reid Spencerbc245a02006-12-05 03:25:26 +00001505}
1506
Matt Arsenault21f38f42013-12-07 02:58:41 +00001507Constant *ConstantExpr::getPointerBitCastOrAddrSpaceCast(Constant *S,
1508 Type *Ty) {
1509 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
1510 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
1511
1512 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
1513 return getAddrSpaceCast(S, Ty);
1514
1515 return getBitCast(S, Ty);
1516}
1517
Sanjay Patelf3587ec2016-05-18 22:05:28 +00001518Constant *ConstantExpr::getIntegerCast(Constant *C, Type *Ty, bool isSigned) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001519 assert(C->getType()->isIntOrIntVectorTy() &&
1520 Ty->isIntOrIntVectorTy() && "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001521 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1522 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer56521c42006-12-12 00:51:07 +00001523 Instruction::CastOps opcode =
1524 (SrcBits == DstBits ? Instruction::BitCast :
1525 (SrcBits > DstBits ? Instruction::Trunc :
1526 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1527 return getCast(opcode, C, Ty);
1528}
1529
Chris Lattner229907c2011-07-18 04:54:35 +00001530Constant *ConstantExpr::getFPCast(Constant *C, Type *Ty) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001531 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer56521c42006-12-12 00:51:07 +00001532 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001533 unsigned SrcBits = C->getType()->getScalarSizeInBits();
1534 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencerca104e82006-12-12 05:38:50 +00001535 if (SrcBits == DstBits)
1536 return C; // Avoid a useless cast
Reid Spencer56521c42006-12-12 00:51:07 +00001537 Instruction::CastOps opcode =
Jay Foad9f32cfd2011-01-27 14:44:55 +00001538 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
Reid Spencer56521c42006-12-12 00:51:07 +00001539 return getCast(opcode, C, Ty);
1540}
1541
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001542Constant *ConstantExpr::getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001543#ifndef NDEBUG
1544 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1545 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1546#endif
1547 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001548 assert(C->getType()->isIntOrIntVectorTy() && "Trunc operand must be integer");
1549 assert(Ty->isIntOrIntVectorTy() && "Trunc produces only integral");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001550 assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001551 "SrcTy must be larger than DestTy for Trunc!");
1552
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001553 return getFoldedCast(Instruction::Trunc, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001554}
1555
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001556Constant *ConstantExpr::getSExt(Constant *C, Type *Ty, bool OnlyIfReduced) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001557#ifndef NDEBUG
1558 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1559 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1560#endif
1561 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001562 assert(C->getType()->isIntOrIntVectorTy() && "SExt operand must be integral");
1563 assert(Ty->isIntOrIntVectorTy() && "SExt produces only integer");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001564 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001565 "SrcTy must be smaller than DestTy for SExt!");
1566
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001567 return getFoldedCast(Instruction::SExt, C, Ty, OnlyIfReduced);
Chris Lattnerdd284742004-04-04 23:20:30 +00001568}
1569
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001570Constant *ConstantExpr::getZExt(Constant *C, Type *Ty, bool OnlyIfReduced) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001571#ifndef NDEBUG
1572 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1573 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1574#endif
1575 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001576 assert(C->getType()->isIntOrIntVectorTy() && "ZEXt operand must be integral");
1577 assert(Ty->isIntOrIntVectorTy() && "ZExt produces only integer");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001578 assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001579 "SrcTy must be smaller than DestTy for ZExt!");
1580
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001581 return getFoldedCast(Instruction::ZExt, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001582}
1583
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001584Constant *ConstantExpr::getFPTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001585#ifndef NDEBUG
1586 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1587 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1588#endif
1589 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001590 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001591 C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001592 "This is an illegal floating point truncation!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001593 return getFoldedCast(Instruction::FPTrunc, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001594}
1595
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001596Constant *ConstantExpr::getFPExtend(Constant *C, Type *Ty, bool OnlyIfReduced) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001597#ifndef NDEBUG
1598 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1599 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1600#endif
1601 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001602 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001603 C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001604 "This is an illegal floating point extension!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001605 return getFoldedCast(Instruction::FPExt, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001606}
1607
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001608Constant *ConstantExpr::getUIToFP(Constant *C, Type *Ty, bool OnlyIfReduced) {
Devang Pateld26344d2008-11-03 23:20:04 +00001609#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001610 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1611 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001612#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001613 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001614 assert(C->getType()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00001615 "This is an illegal uint to floating point cast!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001616 return getFoldedCast(Instruction::UIToFP, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001617}
1618
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001619Constant *ConstantExpr::getSIToFP(Constant *C, Type *Ty, bool OnlyIfReduced) {
Devang Pateld26344d2008-11-03 23:20:04 +00001620#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001621 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1622 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001623#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001624 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001625 assert(C->getType()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001626 "This is an illegal sint to floating point cast!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001627 return getFoldedCast(Instruction::SIToFP, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001628}
1629
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001630Constant *ConstantExpr::getFPToUI(Constant *C, Type *Ty, bool OnlyIfReduced) {
Devang Pateld26344d2008-11-03 23:20:04 +00001631#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001632 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1633 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001634#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001635 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001636 assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00001637 "This is an illegal floating point to uint cast!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001638 return getFoldedCast(Instruction::FPToUI, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001639}
1640
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001641Constant *ConstantExpr::getFPToSI(Constant *C, Type *Ty, bool OnlyIfReduced) {
Devang Pateld26344d2008-11-03 23:20:04 +00001642#ifndef NDEBUG
Nate Begemand4d45c22007-11-17 03:58:34 +00001643 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1644 bool toVec = Ty->getTypeID() == Type::VectorTyID;
Devang Pateld26344d2008-11-03 23:20:04 +00001645#endif
Nate Begemand4d45c22007-11-17 03:58:34 +00001646 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001647 assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00001648 "This is an illegal floating point to sint cast!");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001649 return getFoldedCast(Instruction::FPToSI, C, Ty, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001650}
1651
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001652Constant *ConstantExpr::getPtrToInt(Constant *C, Type *DstTy,
1653 bool OnlyIfReduced) {
Craig Topper95d23472017-07-09 07:04:00 +00001654 assert(C->getType()->isPtrOrPtrVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00001655 "PtrToInt source must be pointer or pointer vector");
Craig Topper95d23472017-07-09 07:04:00 +00001656 assert(DstTy->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00001657 "PtrToInt destination must be integer or integer vector");
Chris Lattner8a3df542012-01-25 01:32:59 +00001658 assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy));
Nick Lewyckyff509622012-01-25 03:20:12 +00001659 if (isa<VectorType>(C->getType()))
Chris Lattner8326bd82012-01-26 00:42:34 +00001660 assert(C->getType()->getVectorNumElements()==DstTy->getVectorNumElements()&&
Chris Lattner8a3df542012-01-25 01:32:59 +00001661 "Invalid cast between a different number of vector elements");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001662 return getFoldedCast(Instruction::PtrToInt, C, DstTy, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001663}
1664
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001665Constant *ConstantExpr::getIntToPtr(Constant *C, Type *DstTy,
1666 bool OnlyIfReduced) {
Craig Topper95d23472017-07-09 07:04:00 +00001667 assert(C->getType()->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00001668 "IntToPtr source must be integer or integer vector");
Craig Topper95d23472017-07-09 07:04:00 +00001669 assert(DstTy->isPtrOrPtrVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00001670 "IntToPtr destination must be a pointer or pointer vector");
Chris Lattner8a3df542012-01-25 01:32:59 +00001671 assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy));
Nick Lewyckyff509622012-01-25 03:20:12 +00001672 if (isa<VectorType>(C->getType()))
Chris Lattner8326bd82012-01-26 00:42:34 +00001673 assert(C->getType()->getVectorNumElements()==DstTy->getVectorNumElements()&&
Chris Lattner8a3df542012-01-25 01:32:59 +00001674 "Invalid cast between a different number of vector elements");
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001675 return getFoldedCast(Instruction::IntToPtr, C, DstTy, OnlyIfReduced);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001676}
1677
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001678Constant *ConstantExpr::getBitCast(Constant *C, Type *DstTy,
1679 bool OnlyIfReduced) {
Chris Lattner37bc78a2010-01-26 21:51:43 +00001680 assert(CastInst::castIsValid(Instruction::BitCast, C, DstTy) &&
1681 "Invalid constantexpr bitcast!");
Galina Kistanovafc259902012-07-13 01:25:27 +00001682
Chris Lattnercbeda872009-03-21 06:55:54 +00001683 // It is common to ask for a bitcast of a value to its own type, handle this
1684 // speedily.
1685 if (C->getType() == DstTy) return C;
Galina Kistanovafc259902012-07-13 01:25:27 +00001686
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001687 return getFoldedCast(Instruction::BitCast, C, DstTy, OnlyIfReduced);
Chris Lattnerdd284742004-04-04 23:20:30 +00001688}
1689
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001690Constant *ConstantExpr::getAddrSpaceCast(Constant *C, Type *DstTy,
1691 bool OnlyIfReduced) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001692 assert(CastInst::castIsValid(Instruction::AddrSpaceCast, C, DstTy) &&
1693 "Invalid constantexpr addrspacecast!");
1694
Jingyue Wubaabe502014-06-15 21:40:57 +00001695 // Canonicalize addrspacecasts between different pointer types by first
1696 // bitcasting the pointer type and then converting the address space.
1697 PointerType *SrcScalarTy = cast<PointerType>(C->getType()->getScalarType());
1698 PointerType *DstScalarTy = cast<PointerType>(DstTy->getScalarType());
1699 Type *DstElemTy = DstScalarTy->getElementType();
1700 if (SrcScalarTy->getElementType() != DstElemTy) {
1701 Type *MidTy = PointerType::get(DstElemTy, SrcScalarTy->getAddressSpace());
1702 if (VectorType *VT = dyn_cast<VectorType>(DstTy)) {
1703 // Handle vectors of pointers.
1704 MidTy = VectorType::get(MidTy, VT->getNumElements());
1705 }
1706 C = getBitCast(C, MidTy);
1707 }
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001708 return getFoldedCast(Instruction::AddrSpaceCast, C, DstTy, OnlyIfReduced);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001709}
1710
Chris Lattner887ecac2011-07-09 18:23:52 +00001711Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2,
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001712 unsigned Flags, Type *OnlyIfReducedTy) {
Chris Lattner887ecac2011-07-09 18:23:52 +00001713 // Check the operands for consistency first.
Reid Spencer7eb55b32006-11-02 01:53:59 +00001714 assert(Opcode >= Instruction::BinaryOpsBegin &&
1715 Opcode < Instruction::BinaryOpsEnd &&
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001716 "Invalid opcode in binary constant expression");
1717 assert(C1->getType() == C2->getType() &&
1718 "Operand types in binary constant expression should match");
Galina Kistanovafc259902012-07-13 01:25:27 +00001719
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001720#ifndef NDEBUG
1721 switch (Opcode) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001722 case Instruction::Add:
Reid Spencer7eb55b32006-11-02 01:53:59 +00001723 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001724 case Instruction::Mul:
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001725 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001726 assert(C1->getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001727 "Tried to create an integer operation on a non-integer type!");
1728 break;
1729 case Instruction::FAdd:
1730 case Instruction::FSub:
1731 case Instruction::FMul:
1732 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001733 assert(C1->getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001734 "Tried to create a floating-point operation on a "
1735 "non-floating-point type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001736 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001737 case Instruction::UDiv:
1738 case Instruction::SDiv:
1739 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001740 assert(C1->getType()->isIntOrIntVectorTy() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001741 "Tried to create an arithmetic operation on a non-arithmetic type!");
1742 break;
1743 case Instruction::FDiv:
1744 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001745 assert(C1->getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001746 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001747 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001748 case Instruction::URem:
1749 case Instruction::SRem:
1750 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001751 assert(C1->getType()->isIntOrIntVectorTy() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001752 "Tried to create an arithmetic operation on a non-arithmetic type!");
1753 break;
1754 case Instruction::FRem:
1755 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001756 assert(C1->getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001757 "Tried to create an arithmetic operation on a non-arithmetic type!");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001758 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001759 case Instruction::And:
1760 case Instruction::Or:
1761 case Instruction::Xor:
1762 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001763 assert(C1->getType()->isIntOrIntVectorTy() &&
Misha Brukman3852f652005-01-27 06:46:38 +00001764 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001765 break;
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001766 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001767 case Instruction::LShr:
1768 case Instruction::AShr:
Reid Spencer2341c222007-02-02 02:16:23 +00001769 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001770 assert(C1->getType()->isIntOrIntVectorTy() &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001771 "Tried to create a shift operation on a non-integer type!");
1772 break;
1773 default:
1774 break;
1775 }
1776#endif
1777
Chris Lattner887ecac2011-07-09 18:23:52 +00001778 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1779 return FC; // Fold a few common cases.
Galina Kistanovafc259902012-07-13 01:25:27 +00001780
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001781 if (OnlyIfReducedTy == C1->getType())
1782 return nullptr;
1783
Benjamin Kramer324322b2013-03-07 20:53:34 +00001784 Constant *ArgVec[] = { C1, C2 };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001785 ConstantExprKeyType Key(Opcode, ArgVec, 0, Flags);
Galina Kistanovafc259902012-07-13 01:25:27 +00001786
Chris Lattner887ecac2011-07-09 18:23:52 +00001787 LLVMContextImpl *pImpl = C1->getContext().pImpl;
1788 return pImpl->ExprConstants.getOrCreate(C1->getType(), Key);
Reid Spencera009d0d2006-12-04 21:35:24 +00001789}
1790
Chris Lattner229907c2011-07-18 04:54:35 +00001791Constant *ConstantExpr::getSizeOf(Type* Ty) {
Owen Anderson487375e2009-07-29 18:55:55 +00001792 // sizeof is implemented as: (i64) gep (Ty*)null, 1
1793 // Note that a non-inbounds gep is used, as null isn't within any object.
Owen Anderson55f1c092009-08-13 21:58:54 +00001794 Constant *GEPIdx = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
Owen Anderson487375e2009-07-29 18:55:55 +00001795 Constant *GEP = getGetElementPtr(
David Blaikie4a2e73b2015-04-02 18:55:32 +00001796 Ty, Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx);
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001797 return getPtrToInt(GEP,
1798 Type::getInt64Ty(Ty->getContext()));
Owen Anderson487375e2009-07-29 18:55:55 +00001799}
1800
Chris Lattner229907c2011-07-18 04:54:35 +00001801Constant *ConstantExpr::getAlignOf(Type* Ty) {
Dan Gohmancf913832010-01-28 02:15:55 +00001802 // alignof is implemented as: (i64) gep ({i1,Ty}*)null, 0, 1
Dan Gohman50c09d02009-08-11 17:57:01 +00001803 // Note that a non-inbounds gep is used, as null isn't within any object.
Serge Gueltone38003f2017-05-09 19:31:13 +00001804 Type *AligningTy = StructType::get(Type::getInt1Ty(Ty->getContext()), Ty);
Matt Arsenaultbe558882014-04-23 20:58:57 +00001805 Constant *NullPtr = Constant::getNullValue(AligningTy->getPointerTo(0));
Dan Gohmana9be7392010-01-28 02:43:22 +00001806 Constant *Zero = ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0);
Owen Anderson55f1c092009-08-13 21:58:54 +00001807 Constant *One = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
Owen Anderson487375e2009-07-29 18:55:55 +00001808 Constant *Indices[2] = { Zero, One };
David Blaikie4a2e73b2015-04-02 18:55:32 +00001809 Constant *GEP = getGetElementPtr(AligningTy, NullPtr, Indices);
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001810 return getPtrToInt(GEP,
1811 Type::getInt64Ty(Ty->getContext()));
Owen Anderson487375e2009-07-29 18:55:55 +00001812}
1813
Chris Lattner229907c2011-07-18 04:54:35 +00001814Constant *ConstantExpr::getOffsetOf(StructType* STy, unsigned FieldNo) {
Dan Gohmanede94e62010-02-01 16:37:38 +00001815 return getOffsetOf(STy, ConstantInt::get(Type::getInt32Ty(STy->getContext()),
1816 FieldNo));
1817}
1818
Chris Lattner229907c2011-07-18 04:54:35 +00001819Constant *ConstantExpr::getOffsetOf(Type* Ty, Constant *FieldNo) {
Dan Gohmanff3af7252009-08-16 21:26:11 +00001820 // offsetof is implemented as: (i64) gep (Ty*)null, 0, FieldNo
1821 // Note that a non-inbounds gep is used, as null isn't within any object.
1822 Constant *GEPIdx[] = {
Dan Gohmanede94e62010-02-01 16:37:38 +00001823 ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0),
1824 FieldNo
Dan Gohmanff3af7252009-08-16 21:26:11 +00001825 };
1826 Constant *GEP = getGetElementPtr(
David Blaikie4a2e73b2015-04-02 18:55:32 +00001827 Ty, Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx);
Dan Gohman3cdcc3f2010-04-12 22:12:29 +00001828 return getPtrToInt(GEP,
1829 Type::getInt64Ty(Ty->getContext()));
Dan Gohmanff3af7252009-08-16 21:26:11 +00001830}
Owen Anderson487375e2009-07-29 18:55:55 +00001831
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001832Constant *ConstantExpr::getCompare(unsigned short Predicate, Constant *C1,
1833 Constant *C2, bool OnlyIfReduced) {
Reid Spencera009d0d2006-12-04 21:35:24 +00001834 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Galina Kistanovafc259902012-07-13 01:25:27 +00001835
Chris Lattner887ecac2011-07-09 18:23:52 +00001836 switch (Predicate) {
1837 default: llvm_unreachable("Invalid CmpInst predicate");
1838 case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
1839 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
1840 case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
1841 case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
1842 case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
1843 case CmpInst::FCMP_TRUE:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001844 return getFCmp(Predicate, C1, C2, OnlyIfReduced);
Galina Kistanovafc259902012-07-13 01:25:27 +00001845
Chris Lattner887ecac2011-07-09 18:23:52 +00001846 case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT:
1847 case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
1848 case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
1849 case CmpInst::ICMP_SLE:
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001850 return getICmp(Predicate, C1, C2, OnlyIfReduced);
Chris Lattner887ecac2011-07-09 18:23:52 +00001851 }
Chris Lattner29ca2c62004-08-04 18:50:09 +00001852}
1853
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001854Constant *ConstantExpr::getSelect(Constant *C, Constant *V1, Constant *V2,
1855 Type *OnlyIfReducedTy) {
Chris Lattner41632132008-12-29 00:16:12 +00001856 assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
Chris Lattner6e415c02004-03-12 05:54:04 +00001857
Chris Lattner887ecac2011-07-09 18:23:52 +00001858 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1859 return SC; // Fold common cases
Chris Lattner6e415c02004-03-12 05:54:04 +00001860
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001861 if (OnlyIfReducedTy == V1->getType())
1862 return nullptr;
1863
Benjamin Kramer324322b2013-03-07 20:53:34 +00001864 Constant *ArgVec[] = { C, V1, V2 };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001865 ConstantExprKeyType Key(Instruction::Select, ArgVec);
Galina Kistanovafc259902012-07-13 01:25:27 +00001866
Chris Lattner887ecac2011-07-09 18:23:52 +00001867 LLVMContextImpl *pImpl = C->getContext().pImpl;
1868 return pImpl->ExprConstants.getOrCreate(V1->getType(), Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00001869}
1870
David Blaikie4a2e73b2015-04-02 18:55:32 +00001871Constant *ConstantExpr::getGetElementPtr(Type *Ty, Constant *C,
1872 ArrayRef<Value *> Idxs, bool InBounds,
Peter Collingbourned93620b2016-11-10 22:34:55 +00001873 Optional<unsigned> InRangeIndex,
David Blaikie4a2e73b2015-04-02 18:55:32 +00001874 Type *OnlyIfReducedTy) {
David Blaikie4a2e73b2015-04-02 18:55:32 +00001875 if (!Ty)
1876 Ty = cast<PointerType>(C->getType()->getScalarType())->getElementType();
1877 else
David Blaikied9d900c2015-05-07 17:28:58 +00001878 assert(
1879 Ty ==
1880 cast<PointerType>(C->getType()->getScalarType())->getContainedType(0u));
1881
Peter Collingbourned93620b2016-11-10 22:34:55 +00001882 if (Constant *FC =
1883 ConstantFoldGetElementPtr(Ty, C, InBounds, InRangeIndex, Idxs))
David Blaikied9d900c2015-05-07 17:28:58 +00001884 return FC; // Fold a few common cases.
1885
Chris Lattner887ecac2011-07-09 18:23:52 +00001886 // Get the result type of the getelementptr!
David Blaikie4a2e73b2015-04-02 18:55:32 +00001887 Type *DestTy = GetElementPtrInst::getIndexedType(Ty, Idxs);
1888 assert(DestTy && "GEP indices invalid!");
Chris Lattner8326bd82012-01-26 00:42:34 +00001889 unsigned AS = C->getType()->getPointerAddressSpace();
David Blaikie4a2e73b2015-04-02 18:55:32 +00001890 Type *ReqTy = DestTy->getPointerTo(AS);
Elena Demikhovskyee004bc2016-05-15 12:30:25 +00001891
1892 unsigned NumVecElts = 0;
1893 if (C->getType()->isVectorTy())
1894 NumVecElts = C->getType()->getVectorNumElements();
1895 else for (auto Idx : Idxs)
1896 if (Idx->getType()->isVectorTy())
1897 NumVecElts = Idx->getType()->getVectorNumElements();
1898
1899 if (NumVecElts)
1900 ReqTy = VectorType::get(ReqTy, NumVecElts);
Galina Kistanovafc259902012-07-13 01:25:27 +00001901
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001902 if (OnlyIfReducedTy == ReqTy)
1903 return nullptr;
1904
Dan Gohman1b849082009-09-07 23:54:19 +00001905 // Look up the constant in the table first to ensure uniqueness
1906 std::vector<Constant*> ArgVec;
Jay Foaded8db7d2011-07-21 14:31:17 +00001907 ArgVec.reserve(1 + Idxs.size());
Dan Gohman1b849082009-09-07 23:54:19 +00001908 ArgVec.push_back(C);
Duncan Sandse6beec62012-11-13 12:59:33 +00001909 for (unsigned i = 0, e = Idxs.size(); i != e; ++i) {
Duncan Sandse6beec62012-11-13 12:59:33 +00001910 assert((!Idxs[i]->getType()->isVectorTy() ||
Elena Demikhovskyee004bc2016-05-15 12:30:25 +00001911 Idxs[i]->getType()->getVectorNumElements() == NumVecElts) &&
Duncan Sandse6beec62012-11-13 12:59:33 +00001912 "getelementptr index type missmatch");
Elena Demikhovskyee004bc2016-05-15 12:30:25 +00001913
1914 Constant *Idx = cast<Constant>(Idxs[i]);
1915 if (NumVecElts && !Idxs[i]->getType()->isVectorTy())
1916 Idx = ConstantVector::getSplat(NumVecElts, Idx);
1917 ArgVec.push_back(Idx);
Duncan Sandse6beec62012-11-13 12:59:33 +00001918 }
Peter Collingbourned93620b2016-11-10 22:34:55 +00001919
1920 unsigned SubClassOptionalData = InBounds ? GEPOperator::IsInBounds : 0;
1921 if (InRangeIndex && *InRangeIndex < 63)
1922 SubClassOptionalData |= (*InRangeIndex + 1) << 1;
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001923 const ConstantExprKeyType Key(Instruction::GetElementPtr, ArgVec, 0,
Peter Collingbourned93620b2016-11-10 22:34:55 +00001924 SubClassOptionalData, None, Ty);
Galina Kistanovafc259902012-07-13 01:25:27 +00001925
Chris Lattner887ecac2011-07-09 18:23:52 +00001926 LLVMContextImpl *pImpl = C->getContext().pImpl;
Dan Gohman1b849082009-09-07 23:54:19 +00001927 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
1928}
1929
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001930Constant *ConstantExpr::getICmp(unsigned short pred, Constant *LHS,
1931 Constant *RHS, bool OnlyIfReduced) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001932 assert(LHS->getType() == RHS->getType());
Craig Topper45844762017-07-05 23:35:46 +00001933 assert(CmpInst::isIntPredicate((CmpInst::Predicate)pred) &&
1934 "Invalid ICmp Predicate");
Reid Spenceree3c9912006-12-04 05:19:50 +00001935
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001936 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001937 return FC; // Fold a few common cases...
1938
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001939 if (OnlyIfReduced)
1940 return nullptr;
1941
Reid Spenceree3c9912006-12-04 05:19:50 +00001942 // Look up the constant in the table first to ensure uniqueness
Benjamin Kramer324322b2013-03-07 20:53:34 +00001943 Constant *ArgVec[] = { LHS, RHS };
Reid Spencerb1537492006-12-24 18:42:29 +00001944 // Get the key type with both the opcode and predicate
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001945 const ConstantExprKeyType Key(Instruction::ICmp, ArgVec, pred);
Owen Anderson61794042009-06-17 20:10:08 +00001946
Chris Lattner229907c2011-07-18 04:54:35 +00001947 Type *ResultTy = Type::getInt1Ty(LHS->getContext());
1948 if (VectorType *VT = dyn_cast<VectorType>(LHS->getType()))
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00001949 ResultTy = VectorType::get(ResultTy, VT->getNumElements());
1950
Owen Anderson1584a292009-08-04 20:25:11 +00001951 LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl;
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00001952 return pImpl->ExprConstants.getOrCreate(ResultTy, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001953}
1954
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001955Constant *ConstantExpr::getFCmp(unsigned short pred, Constant *LHS,
1956 Constant *RHS, bool OnlyIfReduced) {
Reid Spenceree3c9912006-12-04 05:19:50 +00001957 assert(LHS->getType() == RHS->getType());
Craig Topper45844762017-07-05 23:35:46 +00001958 assert(CmpInst::isFPPredicate((CmpInst::Predicate)pred) &&
1959 "Invalid FCmp Predicate");
Reid Spenceree3c9912006-12-04 05:19:50 +00001960
Chris Lattnerf5edeeb2010-02-01 20:48:08 +00001961 if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
Reid Spenceree3c9912006-12-04 05:19:50 +00001962 return FC; // Fold a few common cases...
1963
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001964 if (OnlyIfReduced)
1965 return nullptr;
1966
Reid Spenceree3c9912006-12-04 05:19:50 +00001967 // Look up the constant in the table first to ensure uniqueness
Benjamin Kramer324322b2013-03-07 20:53:34 +00001968 Constant *ArgVec[] = { LHS, RHS };
Reid Spencerb1537492006-12-24 18:42:29 +00001969 // Get the key type with both the opcode and predicate
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001970 const ConstantExprKeyType Key(Instruction::FCmp, ArgVec, pred);
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00001971
Chris Lattner229907c2011-07-18 04:54:35 +00001972 Type *ResultTy = Type::getInt1Ty(LHS->getContext());
1973 if (VectorType *VT = dyn_cast<VectorType>(LHS->getType()))
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00001974 ResultTy = VectorType::get(ResultTy, VT->getNumElements());
1975
Owen Anderson1584a292009-08-04 20:25:11 +00001976 LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl;
Nick Lewycky9e26c1c2010-01-21 07:03:21 +00001977 return pImpl->ExprConstants.getOrCreate(ResultTy, Key);
Reid Spenceree3c9912006-12-04 05:19:50 +00001978}
1979
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001980Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx,
1981 Type *OnlyIfReducedTy) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001982 assert(Val->getType()->isVectorTy() &&
Reid Spencer09575ba2007-02-15 03:39:18 +00001983 "Tried to create extractelement operation on non-vector type!");
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001984 assert(Idx->getType()->isIntegerTy() &&
1985 "Extractelement index must be an integer type!");
Galina Kistanovafc259902012-07-13 01:25:27 +00001986
Chris Lattner887ecac2011-07-09 18:23:52 +00001987 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
Chris Lattner09660c92009-12-30 20:25:09 +00001988 return FC; // Fold a few common cases.
Galina Kistanovafc259902012-07-13 01:25:27 +00001989
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00001990 Type *ReqTy = Val->getType()->getVectorElementType();
1991 if (OnlyIfReducedTy == ReqTy)
1992 return nullptr;
1993
Robert Bocchinoca27f032006-01-17 20:07:22 +00001994 // Look up the constant in the table first to ensure uniqueness
Benjamin Kramer324322b2013-03-07 20:53:34 +00001995 Constant *ArgVec[] = { Val, Idx };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00001996 const ConstantExprKeyType Key(Instruction::ExtractElement, ArgVec);
Galina Kistanovafc259902012-07-13 01:25:27 +00001997
Chris Lattner887ecac2011-07-09 18:23:52 +00001998 LLVMContextImpl *pImpl = Val->getContext().pImpl;
Owen Anderson1584a292009-08-04 20:25:11 +00001999 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00002000}
2001
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002002Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
2003 Constant *Idx, Type *OnlyIfReducedTy) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002004 assert(Val->getType()->isVectorTy() &&
Reid Spencer09575ba2007-02-15 03:39:18 +00002005 "Tried to create insertelement operation on non-vector type!");
Chris Lattner8326bd82012-01-26 00:42:34 +00002006 assert(Elt->getType() == Val->getType()->getVectorElementType() &&
2007 "Insertelement types must match!");
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00002008 assert(Idx->getType()->isIntegerTy() &&
Reid Spencer2546b762007-01-26 07:37:34 +00002009 "Insertelement index must be i32 type!");
Robert Bocchinoca27f032006-01-17 20:07:22 +00002010
Chris Lattner887ecac2011-07-09 18:23:52 +00002011 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
2012 return FC; // Fold a few common cases.
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002013
2014 if (OnlyIfReducedTy == Val->getType())
2015 return nullptr;
2016
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002017 // Look up the constant in the table first to ensure uniqueness
Benjamin Kramer324322b2013-03-07 20:53:34 +00002018 Constant *ArgVec[] = { Val, Elt, Idx };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002019 const ConstantExprKeyType Key(Instruction::InsertElement, ArgVec);
Galina Kistanovafc259902012-07-13 01:25:27 +00002020
Chris Lattner887ecac2011-07-09 18:23:52 +00002021 LLVMContextImpl *pImpl = Val->getContext().pImpl;
2022 return pImpl->ExprConstants.getOrCreate(Val->getType(), Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002023}
2024
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002025Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
2026 Constant *Mask, Type *OnlyIfReducedTy) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002027 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
2028 "Invalid shuffle vector constant expr operands!");
Nate Begeman94aa38d2009-02-12 21:28:33 +00002029
Chris Lattner887ecac2011-07-09 18:23:52 +00002030 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
2031 return FC; // Fold a few common cases.
2032
Chris Lattner8326bd82012-01-26 00:42:34 +00002033 unsigned NElts = Mask->getType()->getVectorNumElements();
2034 Type *EltTy = V1->getType()->getVectorElementType();
Chris Lattner229907c2011-07-18 04:54:35 +00002035 Type *ShufTy = VectorType::get(EltTy, NElts);
Chris Lattner887ecac2011-07-09 18:23:52 +00002036
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002037 if (OnlyIfReducedTy == ShufTy)
2038 return nullptr;
2039
Chris Lattner887ecac2011-07-09 18:23:52 +00002040 // Look up the constant in the table first to ensure uniqueness
Benjamin Kramer324322b2013-03-07 20:53:34 +00002041 Constant *ArgVec[] = { V1, V2, Mask };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002042 const ConstantExprKeyType Key(Instruction::ShuffleVector, ArgVec);
Galina Kistanovafc259902012-07-13 01:25:27 +00002043
Chris Lattner887ecac2011-07-09 18:23:52 +00002044 LLVMContextImpl *pImpl = ShufTy->getContext().pImpl;
2045 return pImpl->ExprConstants.getOrCreate(ShufTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002046}
2047
Chris Lattner887ecac2011-07-09 18:23:52 +00002048Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002049 ArrayRef<unsigned> Idxs,
2050 Type *OnlyIfReducedTy) {
Hal Finkelb31366d2013-07-10 22:51:01 +00002051 assert(Agg->getType()->isFirstClassType() &&
2052 "Non-first-class type for constant insertvalue expression");
2053
Jay Foad57aa6362011-07-13 10:26:04 +00002054 assert(ExtractValueInst::getIndexedType(Agg->getType(),
2055 Idxs) == Val->getType() &&
Dan Gohman12fce772008-05-15 19:50:34 +00002056 "insertvalue indices invalid!");
Hal Finkelb31366d2013-07-10 22:51:01 +00002057 Type *ReqTy = Val->getType();
2058
2059 if (Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs))
2060 return FC;
2061
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002062 if (OnlyIfReducedTy == ReqTy)
2063 return nullptr;
2064
Hal Finkelb31366d2013-07-10 22:51:01 +00002065 Constant *ArgVec[] = { Agg, Val };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002066 const ConstantExprKeyType Key(Instruction::InsertValue, ArgVec, 0, 0, Idxs);
Hal Finkelb31366d2013-07-10 22:51:01 +00002067
2068 LLVMContextImpl *pImpl = Agg->getContext().pImpl;
2069 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Dan Gohman12fce772008-05-15 19:50:34 +00002070}
2071
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002072Constant *ConstantExpr::getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs,
2073 Type *OnlyIfReducedTy) {
Dan Gohman0752bff2008-05-23 00:36:11 +00002074 assert(Agg->getType()->isFirstClassType() &&
Chris Lattner887ecac2011-07-09 18:23:52 +00002075 "Tried to create extractelement operation on non-first-class type!");
Dan Gohman12fce772008-05-15 19:50:34 +00002076
Chris Lattner229907c2011-07-18 04:54:35 +00002077 Type *ReqTy = ExtractValueInst::getIndexedType(Agg->getType(), Idxs);
Chandler Carruth9db56b82011-07-10 09:45:35 +00002078 (void)ReqTy;
Chris Lattner887ecac2011-07-09 18:23:52 +00002079 assert(ReqTy && "extractvalue indices invalid!");
Galina Kistanovafc259902012-07-13 01:25:27 +00002080
Dan Gohman0752bff2008-05-23 00:36:11 +00002081 assert(Agg->getType()->isFirstClassType() &&
2082 "Non-first-class type for constant extractvalue expression");
Hal Finkelb31366d2013-07-10 22:51:01 +00002083 if (Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs))
2084 return FC;
2085
Duncan P. N. Exon Smith170eb982014-08-19 19:45:37 +00002086 if (OnlyIfReducedTy == ReqTy)
2087 return nullptr;
2088
Hal Finkelb31366d2013-07-10 22:51:01 +00002089 Constant *ArgVec[] = { Agg };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002090 const ConstantExprKeyType Key(Instruction::ExtractValue, ArgVec, 0, 0, Idxs);
Hal Finkelb31366d2013-07-10 22:51:01 +00002091
2092 LLVMContextImpl *pImpl = Agg->getContext().pImpl;
2093 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
Dan Gohman12fce772008-05-15 19:50:34 +00002094}
2095
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002096Constant *ConstantExpr::getNeg(Constant *C, bool HasNUW, bool HasNSW) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002097 assert(C->getType()->isIntOrIntVectorTy() &&
Owen Anderson487375e2009-07-29 18:55:55 +00002098 "Cannot NEG a nonintegral value!");
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002099 return getSub(ConstantFP::getZeroValueForNegation(C->getType()),
2100 C, HasNUW, HasNSW);
Owen Anderson487375e2009-07-29 18:55:55 +00002101}
2102
Chris Lattnera676c0f2011-02-07 16:40:21 +00002103Constant *ConstantExpr::getFNeg(Constant *C) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002104 assert(C->getType()->isFPOrFPVectorTy() &&
Owen Anderson487375e2009-07-29 18:55:55 +00002105 "Cannot FNEG a non-floating-point value!");
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002106 return getFSub(ConstantFP::getZeroValueForNegation(C->getType()), C);
Owen Anderson487375e2009-07-29 18:55:55 +00002107}
2108
Chris Lattnera676c0f2011-02-07 16:40:21 +00002109Constant *ConstantExpr::getNot(Constant *C) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002110 assert(C->getType()->isIntOrIntVectorTy() &&
Owen Anderson487375e2009-07-29 18:55:55 +00002111 "Cannot NOT a nonintegral value!");
Owen Anderson5a1acd92009-07-31 20:28:14 +00002112 return get(Instruction::Xor, C, Constant::getAllOnesValue(C->getType()));
Owen Anderson487375e2009-07-29 18:55:55 +00002113}
2114
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002115Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2,
2116 bool HasNUW, bool HasNSW) {
2117 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2118 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
2119 return get(Instruction::Add, C1, C2, Flags);
Owen Anderson487375e2009-07-29 18:55:55 +00002120}
2121
Chris Lattnera676c0f2011-02-07 16:40:21 +00002122Constant *ConstantExpr::getFAdd(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002123 return get(Instruction::FAdd, C1, C2);
2124}
2125
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002126Constant *ConstantExpr::getSub(Constant *C1, Constant *C2,
2127 bool HasNUW, bool HasNSW) {
2128 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2129 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
2130 return get(Instruction::Sub, C1, C2, Flags);
Owen Anderson487375e2009-07-29 18:55:55 +00002131}
2132
Chris Lattnera676c0f2011-02-07 16:40:21 +00002133Constant *ConstantExpr::getFSub(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002134 return get(Instruction::FSub, C1, C2);
2135}
2136
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002137Constant *ConstantExpr::getMul(Constant *C1, Constant *C2,
2138 bool HasNUW, bool HasNSW) {
2139 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2140 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
2141 return get(Instruction::Mul, C1, C2, Flags);
Owen Anderson487375e2009-07-29 18:55:55 +00002142}
2143
Chris Lattnera676c0f2011-02-07 16:40:21 +00002144Constant *ConstantExpr::getFMul(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002145 return get(Instruction::FMul, C1, C2);
2146}
2147
Chris Lattner0d75eac2011-02-09 16:43:07 +00002148Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2, bool isExact) {
2149 return get(Instruction::UDiv, C1, C2,
2150 isExact ? PossiblyExactOperator::IsExact : 0);
Owen Anderson487375e2009-07-29 18:55:55 +00002151}
2152
Chris Lattner0d75eac2011-02-09 16:43:07 +00002153Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2, bool isExact) {
2154 return get(Instruction::SDiv, C1, C2,
2155 isExact ? PossiblyExactOperator::IsExact : 0);
Owen Anderson487375e2009-07-29 18:55:55 +00002156}
2157
Chris Lattnera676c0f2011-02-07 16:40:21 +00002158Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002159 return get(Instruction::FDiv, C1, C2);
2160}
2161
Chris Lattnera676c0f2011-02-07 16:40:21 +00002162Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002163 return get(Instruction::URem, C1, C2);
2164}
2165
Chris Lattnera676c0f2011-02-07 16:40:21 +00002166Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002167 return get(Instruction::SRem, C1, C2);
2168}
2169
Chris Lattnera676c0f2011-02-07 16:40:21 +00002170Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002171 return get(Instruction::FRem, C1, C2);
2172}
2173
Chris Lattnera676c0f2011-02-07 16:40:21 +00002174Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002175 return get(Instruction::And, C1, C2);
2176}
2177
Chris Lattnera676c0f2011-02-07 16:40:21 +00002178Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002179 return get(Instruction::Or, C1, C2);
2180}
2181
Chris Lattnera676c0f2011-02-07 16:40:21 +00002182Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
Owen Anderson487375e2009-07-29 18:55:55 +00002183 return get(Instruction::Xor, C1, C2);
2184}
2185
Chris Lattnere9b4ad72011-02-10 07:01:55 +00002186Constant *ConstantExpr::getShl(Constant *C1, Constant *C2,
2187 bool HasNUW, bool HasNSW) {
2188 unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2189 (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
2190 return get(Instruction::Shl, C1, C2, Flags);
Owen Anderson487375e2009-07-29 18:55:55 +00002191}
2192
Chris Lattner0d75eac2011-02-09 16:43:07 +00002193Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2, bool isExact) {
2194 return get(Instruction::LShr, C1, C2,
2195 isExact ? PossiblyExactOperator::IsExact : 0);
Owen Anderson487375e2009-07-29 18:55:55 +00002196}
2197
Chris Lattner0d75eac2011-02-09 16:43:07 +00002198Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2, bool isExact) {
2199 return get(Instruction::AShr, C1, C2,
2200 isExact ? PossiblyExactOperator::IsExact : 0);
Owen Anderson487375e2009-07-29 18:55:55 +00002201}
2202
Duncan Sandsd7aeefe2012-06-12 14:33:56 +00002203Constant *ConstantExpr::getBinOpIdentity(unsigned Opcode, Type *Ty) {
2204 switch (Opcode) {
2205 default:
Duncan Sands318a89d2012-06-13 09:42:13 +00002206 // Doesn't have an identity.
Craig Topperc6207612014-04-09 06:08:46 +00002207 return nullptr;
Duncan Sands318a89d2012-06-13 09:42:13 +00002208
Duncan Sandsd7aeefe2012-06-12 14:33:56 +00002209 case Instruction::Add:
2210 case Instruction::Or:
2211 case Instruction::Xor:
2212 return Constant::getNullValue(Ty);
2213
2214 case Instruction::Mul:
2215 return ConstantInt::get(Ty, 1);
2216
2217 case Instruction::And:
2218 return Constant::getAllOnesValue(Ty);
2219 }
2220}
2221
Duncan Sands318a89d2012-06-13 09:42:13 +00002222Constant *ConstantExpr::getBinOpAbsorber(unsigned Opcode, Type *Ty) {
2223 switch (Opcode) {
2224 default:
2225 // Doesn't have an absorber.
Craig Topperc6207612014-04-09 06:08:46 +00002226 return nullptr;
Duncan Sands318a89d2012-06-13 09:42:13 +00002227
2228 case Instruction::Or:
2229 return Constant::getAllOnesValue(Ty);
2230
2231 case Instruction::And:
2232 case Instruction::Mul:
2233 return Constant::getNullValue(Ty);
2234 }
2235}
2236
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00002237/// Remove the constant from the constant table.
Pete Cooper86dd4cf2015-06-23 21:55:11 +00002238void ConstantExpr::destroyConstantImpl() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002239 getType()->getContext().pImpl->ExprConstants.remove(this);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002240}
2241
Chris Lattner3cd8c562002-07-30 18:54:25 +00002242const char *ConstantExpr::getOpcodeName() const {
2243 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00002244}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00002245
David Blaikie60310f22015-05-08 00:42:26 +00002246GetElementPtrConstantExpr::GetElementPtrConstantExpr(
2247 Type *SrcElementTy, Constant *C, ArrayRef<Constant *> IdxList, Type *DestTy)
2248 : ConstantExpr(DestTy, Instruction::GetElementPtr,
2249 OperandTraits<GetElementPtrConstantExpr>::op_end(this) -
2250 (IdxList.size() + 1),
2251 IdxList.size() + 1),
Eduard Burtescu19eb0312016-01-19 17:28:00 +00002252 SrcElementTy(SrcElementTy),
2253 ResElementTy(GetElementPtrInst::getIndexedType(SrcElementTy, IdxList)) {
Pete Coopereb31b682015-05-21 22:48:54 +00002254 Op<0>() = C;
Pete Cooper74510a42015-06-12 17:48:05 +00002255 Use *OperandList = getOperandList();
Chris Lattnera3b94ba2010-03-30 20:48:48 +00002256 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
2257 OperandList[i+1] = IdxList[i];
2258}
2259
David Blaikie60310f22015-05-08 00:42:26 +00002260Type *GetElementPtrConstantExpr::getSourceElementType() const {
2261 return SrcElementTy;
2262}
2263
Eduard Burtescu19eb0312016-01-19 17:28:00 +00002264Type *GetElementPtrConstantExpr::getResultElementType() const {
2265 return ResElementTy;
2266}
2267
Chris Lattner3756b912012-01-23 22:57:10 +00002268//===----------------------------------------------------------------------===//
2269// ConstantData* implementations
2270
Chris Lattnere4f3f102012-01-24 04:43:41 +00002271Type *ConstantDataSequential::getElementType() const {
2272 return getType()->getElementType();
2273}
2274
Chris Lattner5d4497b2012-01-24 09:31:43 +00002275StringRef ConstantDataSequential::getRawDataValues() const {
Chris Lattner00245f42012-01-24 13:41:11 +00002276 return StringRef(DataElements, getNumElements()*getElementByteSize());
Chris Lattner5d4497b2012-01-24 09:31:43 +00002277}
2278
Craig Toppere3dcce92015-08-01 22:20:21 +00002279bool ConstantDataSequential::isElementTypeCompatible(Type *Ty) {
Justin Bogner0ebc8602015-12-08 03:01:16 +00002280 if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) return true;
Craig Toppere3dcce92015-08-01 22:20:21 +00002281 if (auto *IT = dyn_cast<IntegerType>(Ty)) {
Chris Lattnere4f3f102012-01-24 04:43:41 +00002282 switch (IT->getBitWidth()) {
2283 case 8:
2284 case 16:
2285 case 32:
2286 case 64:
2287 return true;
2288 default: break;
2289 }
2290 }
2291 return false;
2292}
2293
Chris Lattner00245f42012-01-24 13:41:11 +00002294unsigned ConstantDataSequential::getNumElements() const {
Chris Lattner8a3df542012-01-25 01:32:59 +00002295 if (ArrayType *AT = dyn_cast<ArrayType>(getType()))
2296 return AT->getNumElements();
Chris Lattner8326bd82012-01-26 00:42:34 +00002297 return getType()->getVectorNumElements();
Chris Lattner00245f42012-01-24 13:41:11 +00002298}
2299
2300
Chris Lattnere4f3f102012-01-24 04:43:41 +00002301uint64_t ConstantDataSequential::getElementByteSize() const {
2302 return getElementType()->getPrimitiveSizeInBits()/8;
2303}
2304
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00002305/// Return the start of the specified element.
Chris Lattnere4f3f102012-01-24 04:43:41 +00002306const char *ConstantDataSequential::getElementPointer(unsigned Elt) const {
Chris Lattner00245f42012-01-24 13:41:11 +00002307 assert(Elt < getNumElements() && "Invalid Elt");
Chris Lattnere4f3f102012-01-24 04:43:41 +00002308 return DataElements+Elt*getElementByteSize();
2309}
2310
2311
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00002312/// Return true if the array is empty or all zeros.
Chris Lattner3756b912012-01-23 22:57:10 +00002313static bool isAllZeros(StringRef Arr) {
Benjamin Krameraf28e7d2016-06-26 14:10:56 +00002314 for (char I : Arr)
2315 if (I != 0)
Chris Lattner3756b912012-01-23 22:57:10 +00002316 return false;
2317 return true;
2318}
Chris Lattner030af792012-01-24 05:42:11 +00002319
Sanjay Patel1d0ac7c2016-04-29 22:03:27 +00002320/// This is the underlying implementation of all of the
Chris Lattner3756b912012-01-23 22:57:10 +00002321/// ConstantDataSequential::get methods. They all thunk down to here, providing
Chris Lattnerf06039b2012-01-30 18:19:30 +00002322/// the correct element type. We take the bytes in as a StringRef because
Chris Lattner3756b912012-01-23 22:57:10 +00002323/// we *want* an underlying "char*" to avoid TBAA type punning violations.
2324Constant *ConstantDataSequential::getImpl(StringRef Elements, Type *Ty) {
Chris Lattner8326bd82012-01-26 00:42:34 +00002325 assert(isElementTypeCompatible(Ty->getSequentialElementType()));
Chris Lattner139822f2012-01-24 14:17:05 +00002326 // If the elements are all zero or there are no elements, return a CAZ, which
2327 // is more dense and canonical.
Chris Lattner3756b912012-01-23 22:57:10 +00002328 if (isAllZeros(Elements))
2329 return ConstantAggregateZero::get(Ty);
2330
2331 // Do a lookup to see if we have already formed one of these.
David Blaikie5106ce72014-11-19 05:49:42 +00002332 auto &Slot =
2333 *Ty->getContext()
2334 .pImpl->CDSConstants.insert(std::make_pair(Elements, nullptr))
2335 .first;
Galina Kistanovafc259902012-07-13 01:25:27 +00002336
Chris Lattner3756b912012-01-23 22:57:10 +00002337 // The bucket can point to a linked list of different CDS's that have the same
2338 // body but different types. For example, 0,0,0,1 could be a 4 element array
2339 // of i8, or a 1-element array of i32. They'll both end up in the same
2340 /// StringMap bucket, linked up by their Next pointers. Walk the list.
David Blaikie5106ce72014-11-19 05:49:42 +00002341 ConstantDataSequential **Entry = &Slot.second;
Craig Topperc6207612014-04-09 06:08:46 +00002342 for (ConstantDataSequential *Node = *Entry; Node;
Chris Lattner3756b912012-01-23 22:57:10 +00002343 Entry = &Node->Next, Node = *Entry)
2344 if (Node->getType() == Ty)
2345 return Node;
Galina Kistanovafc259902012-07-13 01:25:27 +00002346
Chris Lattner3756b912012-01-23 22:57:10 +00002347 // Okay, we didn't get a hit. Create a node of the right class, link it in,
2348 // and return it.
2349 if (isa<ArrayType>(Ty))
David Blaikie5106ce72014-11-19 05:49:42 +00002350 return *Entry = new ConstantDataArray(Ty, Slot.first().data());
Chris Lattner3756b912012-01-23 22:57:10 +00002351
2352 assert(isa<VectorType>(Ty));
David Blaikie5106ce72014-11-19 05:49:42 +00002353 return *Entry = new ConstantDataVector(Ty, Slot.first().data());
Chris Lattner3756b912012-01-23 22:57:10 +00002354}
2355
Pete Cooper86dd4cf2015-06-23 21:55:11 +00002356void ConstantDataSequential::destroyConstantImpl() {
Chris Lattner3756b912012-01-23 22:57:10 +00002357 // Remove the constant from the StringMap.
2358 StringMap<ConstantDataSequential*> &CDSConstants =
2359 getType()->getContext().pImpl->CDSConstants;
Galina Kistanovafc259902012-07-13 01:25:27 +00002360
Chris Lattner3756b912012-01-23 22:57:10 +00002361 StringMap<ConstantDataSequential*>::iterator Slot =
Chris Lattner5d4497b2012-01-24 09:31:43 +00002362 CDSConstants.find(getRawDataValues());
Chris Lattner3756b912012-01-23 22:57:10 +00002363
2364 assert(Slot != CDSConstants.end() && "CDS not found in uniquing table");
2365
2366 ConstantDataSequential **Entry = &Slot->getValue();
2367
2368 // Remove the entry from the hash table.
Craig Topperc6207612014-04-09 06:08:46 +00002369 if (!(*Entry)->Next) {
Chris Lattner3756b912012-01-23 22:57:10 +00002370 // If there is only one value in the bucket (common case) it must be this
2371 // entry, and removing the entry should remove the bucket completely.
2372 assert((*Entry) == this && "Hash mismatch in ConstantDataSequential");
2373 getContext().pImpl->CDSConstants.erase(Slot);
2374 } else {
2375 // Otherwise, there are multiple entries linked off the bucket, unlink the
2376 // node we care about but keep the bucket around.
2377 for (ConstantDataSequential *Node = *Entry; ;
2378 Entry = &Node->Next, Node = *Entry) {
2379 assert(Node && "Didn't find entry in its uniquing hash table!");
2380 // If we found our entry, unlink it from the list and we're done.
2381 if (Node == this) {
2382 *Entry = Node->Next;
2383 break;
2384 }
2385 }
2386 }
Galina Kistanovafc259902012-07-13 01:25:27 +00002387
Chris Lattner3756b912012-01-23 22:57:10 +00002388 // If we were part of a list, make sure that we don't delete the list that is
2389 // still owned by the uniquing map.
Craig Topperc6207612014-04-09 06:08:46 +00002390 Next = nullptr;
Chris Lattner3756b912012-01-23 22:57:10 +00002391}
2392
2393/// get() constructors - Return a constant with array type with an element
2394/// count and element type matching the ArrayRef passed in. Note that this
2395/// can return a ConstantAggregateZero object.
Chris Lattner20683932012-01-24 14:04:40 +00002396Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint8_t> Elts) {
Chris Lattner3756b912012-01-23 22:57:10 +00002397 Type *Ty = ArrayType::get(Type::getInt8Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002398 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002399 return getImpl(StringRef(Data, Elts.size() * 1), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002400}
Chris Lattner20683932012-01-24 14:04:40 +00002401Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint16_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002402 Type *Ty = ArrayType::get(Type::getInt16Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002403 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002404 return getImpl(StringRef(Data, Elts.size() * 2), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002405}
Chris Lattner20683932012-01-24 14:04:40 +00002406Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint32_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002407 Type *Ty = ArrayType::get(Type::getInt32Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002408 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002409 return getImpl(StringRef(Data, Elts.size() * 4), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002410}
Chris Lattner20683932012-01-24 14:04:40 +00002411Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint64_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002412 Type *Ty = ArrayType::get(Type::getInt64Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002413 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002414 return getImpl(StringRef(Data, Elts.size() * 8), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002415}
Chris Lattner20683932012-01-24 14:04:40 +00002416Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<float> Elts) {
Chris Lattner3756b912012-01-23 22:57:10 +00002417 Type *Ty = ArrayType::get(Type::getFloatTy(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002418 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002419 return getImpl(StringRef(Data, Elts.size() * 4), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002420}
Chris Lattner20683932012-01-24 14:04:40 +00002421Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<double> Elts) {
Chris Lattner3756b912012-01-23 22:57:10 +00002422 Type *Ty = ArrayType::get(Type::getDoubleTy(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002423 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002424 return getImpl(StringRef(Data, Elts.size() * 8), Ty);
Rafael Espindola8c97e192015-02-19 16:08:20 +00002425}
2426
2427/// getFP() constructors - Return a constant with array type with an element
2428/// count and element type of float with precision matching the number of
2429/// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits,
2430/// double for 64bits) Note that this can return a ConstantAggregateZero
2431/// object.
2432Constant *ConstantDataArray::getFP(LLVMContext &Context,
2433 ArrayRef<uint16_t> Elts) {
Justin Bognerb7389d6712015-12-09 21:21:07 +00002434 Type *Ty = ArrayType::get(Type::getHalfTy(Context), Elts.size());
Rafael Espindola8c97e192015-02-19 16:08:20 +00002435 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002436 return getImpl(StringRef(Data, Elts.size() * 2), Ty);
Rafael Espindola8c97e192015-02-19 16:08:20 +00002437}
2438Constant *ConstantDataArray::getFP(LLVMContext &Context,
2439 ArrayRef<uint32_t> Elts) {
2440 Type *Ty = ArrayType::get(Type::getFloatTy(Context), Elts.size());
2441 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002442 return getImpl(StringRef(Data, Elts.size() * 4), Ty);
Rafael Espindola8c97e192015-02-19 16:08:20 +00002443}
2444Constant *ConstantDataArray::getFP(LLVMContext &Context,
2445 ArrayRef<uint64_t> Elts) {
2446 Type *Ty = ArrayType::get(Type::getDoubleTy(Context), Elts.size());
2447 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002448 return getImpl(StringRef(Data, Elts.size() * 8), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002449}
2450
Chris Lattner20683932012-01-24 14:04:40 +00002451Constant *ConstantDataArray::getString(LLVMContext &Context,
2452 StringRef Str, bool AddNull) {
Galina Kistanovafc259902012-07-13 01:25:27 +00002453 if (!AddNull) {
2454 const uint8_t *Data = reinterpret_cast<const uint8_t *>(Str.data());
Craig Topper393ce692017-07-11 15:52:21 +00002455 return get(Context, makeArrayRef(Data, Str.size()));
Galina Kistanovafc259902012-07-13 01:25:27 +00002456 }
2457
Chris Lattner20683932012-01-24 14:04:40 +00002458 SmallVector<uint8_t, 64> ElementVals;
2459 ElementVals.append(Str.begin(), Str.end());
2460 ElementVals.push_back(0);
2461 return get(Context, ElementVals);
2462}
Chris Lattner3756b912012-01-23 22:57:10 +00002463
2464/// get() constructors - Return a constant with vector type with an element
2465/// count and element type matching the ArrayRef passed in. Note that this
2466/// can return a ConstantAggregateZero object.
Chris Lattner20683932012-01-24 14:04:40 +00002467Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint8_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002468 Type *Ty = VectorType::get(Type::getInt8Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002469 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002470 return getImpl(StringRef(Data, Elts.size() * 1), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002471}
Chris Lattner20683932012-01-24 14:04:40 +00002472Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint16_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002473 Type *Ty = VectorType::get(Type::getInt16Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002474 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002475 return getImpl(StringRef(Data, Elts.size() * 2), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002476}
Chris Lattner20683932012-01-24 14:04:40 +00002477Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint32_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002478 Type *Ty = VectorType::get(Type::getInt32Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002479 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002480 return getImpl(StringRef(Data, Elts.size() * 4), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002481}
Chris Lattner20683932012-01-24 14:04:40 +00002482Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint64_t> Elts){
Chris Lattner3756b912012-01-23 22:57:10 +00002483 Type *Ty = VectorType::get(Type::getInt64Ty(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002484 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002485 return getImpl(StringRef(Data, Elts.size() * 8), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002486}
Chris Lattner20683932012-01-24 14:04:40 +00002487Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<float> Elts) {
Chris Lattner3756b912012-01-23 22:57:10 +00002488 Type *Ty = VectorType::get(Type::getFloatTy(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002489 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002490 return getImpl(StringRef(Data, Elts.size() * 4), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002491}
Chris Lattner20683932012-01-24 14:04:40 +00002492Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<double> Elts) {
Chris Lattner3756b912012-01-23 22:57:10 +00002493 Type *Ty = VectorType::get(Type::getDoubleTy(Context), Elts.size());
Galina Kistanovafc259902012-07-13 01:25:27 +00002494 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002495 return getImpl(StringRef(Data, Elts.size() * 8), Ty);
Rafael Espindola8c97e192015-02-19 16:08:20 +00002496}
2497
2498/// getFP() constructors - Return a constant with vector type with an element
2499/// count and element type of float with the precision matching the number of
2500/// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits,
2501/// double for 64bits) Note that this can return a ConstantAggregateZero
2502/// object.
2503Constant *ConstantDataVector::getFP(LLVMContext &Context,
2504 ArrayRef<uint16_t> Elts) {
2505 Type *Ty = VectorType::get(Type::getHalfTy(Context), Elts.size());
2506 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002507 return getImpl(StringRef(Data, Elts.size() * 2), Ty);
Rafael Espindola8c97e192015-02-19 16:08:20 +00002508}
2509Constant *ConstantDataVector::getFP(LLVMContext &Context,
2510 ArrayRef<uint32_t> Elts) {
2511 Type *Ty = VectorType::get(Type::getFloatTy(Context), Elts.size());
2512 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002513 return getImpl(StringRef(Data, Elts.size() * 4), Ty);
Rafael Espindola8c97e192015-02-19 16:08:20 +00002514}
2515Constant *ConstantDataVector::getFP(LLVMContext &Context,
2516 ArrayRef<uint64_t> Elts) {
2517 Type *Ty = VectorType::get(Type::getDoubleTy(Context), Elts.size());
2518 const char *Data = reinterpret_cast<const char *>(Elts.data());
Craig Topper393ce692017-07-11 15:52:21 +00002519 return getImpl(StringRef(Data, Elts.size() * 8), Ty);
Chris Lattner3756b912012-01-23 22:57:10 +00002520}
2521
Chris Lattnere9eed292012-01-25 05:19:54 +00002522Constant *ConstantDataVector::getSplat(unsigned NumElts, Constant *V) {
2523 assert(isElementTypeCompatible(V->getType()) &&
2524 "Element type not compatible with ConstantData");
2525 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
2526 if (CI->getType()->isIntegerTy(8)) {
2527 SmallVector<uint8_t, 16> Elts(NumElts, CI->getZExtValue());
2528 return get(V->getContext(), Elts);
2529 }
2530 if (CI->getType()->isIntegerTy(16)) {
2531 SmallVector<uint16_t, 16> Elts(NumElts, CI->getZExtValue());
2532 return get(V->getContext(), Elts);
2533 }
2534 if (CI->getType()->isIntegerTy(32)) {
2535 SmallVector<uint32_t, 16> Elts(NumElts, CI->getZExtValue());
2536 return get(V->getContext(), Elts);
2537 }
2538 assert(CI->getType()->isIntegerTy(64) && "Unsupported ConstantData type");
2539 SmallVector<uint64_t, 16> Elts(NumElts, CI->getZExtValue());
2540 return get(V->getContext(), Elts);
2541 }
2542
Chris Lattner978fe0c2012-01-30 06:21:21 +00002543 if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
Justin Bogner0ebc8602015-12-08 03:01:16 +00002544 if (CFP->getType()->isHalfTy()) {
2545 SmallVector<uint16_t, 16> Elts(
2546 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
2547 return getFP(V->getContext(), Elts);
2548 }
Chris Lattner978fe0c2012-01-30 06:21:21 +00002549 if (CFP->getType()->isFloatTy()) {
Rafael Espindola8c97e192015-02-19 16:08:20 +00002550 SmallVector<uint32_t, 16> Elts(
2551 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
2552 return getFP(V->getContext(), Elts);
Chris Lattner978fe0c2012-01-30 06:21:21 +00002553 }
2554 if (CFP->getType()->isDoubleTy()) {
Rafael Espindola8c97e192015-02-19 16:08:20 +00002555 SmallVector<uint64_t, 16> Elts(
2556 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
2557 return getFP(V->getContext(), Elts);
Chris Lattner978fe0c2012-01-30 06:21:21 +00002558 }
Chris Lattnere9eed292012-01-25 05:19:54 +00002559 }
Chris Lattner978fe0c2012-01-30 06:21:21 +00002560 return ConstantVector::getSplat(NumElts, V);
Chris Lattnere9eed292012-01-25 05:19:54 +00002561}
2562
2563
Chris Lattnere4f3f102012-01-24 04:43:41 +00002564uint64_t ConstantDataSequential::getElementAsInteger(unsigned Elt) const {
2565 assert(isa<IntegerType>(getElementType()) &&
2566 "Accessor can only be used when element is an integer");
2567 const char *EltPtr = getElementPointer(Elt);
Galina Kistanovafc259902012-07-13 01:25:27 +00002568
Chris Lattnere4f3f102012-01-24 04:43:41 +00002569 // The data is stored in host byte order, make sure to cast back to the right
2570 // type to load with the right endianness.
Chris Lattner8326bd82012-01-26 00:42:34 +00002571 switch (getElementType()->getIntegerBitWidth()) {
Craig Topperc514b542012-02-05 22:14:15 +00002572 default: llvm_unreachable("Invalid bitwidth for CDS");
Galina Kistanovafc259902012-07-13 01:25:27 +00002573 case 8:
Craig Topper393ce692017-07-11 15:52:21 +00002574 return *reinterpret_cast<const uint8_t *>(EltPtr);
Galina Kistanovafc259902012-07-13 01:25:27 +00002575 case 16:
Craig Topper393ce692017-07-11 15:52:21 +00002576 return *reinterpret_cast<const uint16_t *>(EltPtr);
Galina Kistanovafc259902012-07-13 01:25:27 +00002577 case 32:
Craig Topper393ce692017-07-11 15:52:21 +00002578 return *reinterpret_cast<const uint32_t *>(EltPtr);
Galina Kistanovafc259902012-07-13 01:25:27 +00002579 case 64:
Craig Topper393ce692017-07-11 15:52:21 +00002580 return *reinterpret_cast<const uint64_t *>(EltPtr);
Chris Lattnere4f3f102012-01-24 04:43:41 +00002581 }
2582}
2583
Craig Topper0b4b4e32017-07-15 22:06:19 +00002584APInt ConstantDataSequential::getElementAsAPInt(unsigned Elt) const {
2585 assert(isa<IntegerType>(getElementType()) &&
2586 "Accessor can only be used when element is an integer");
2587 const char *EltPtr = getElementPointer(Elt);
2588
2589 // The data is stored in host byte order, make sure to cast back to the right
2590 // type to load with the right endianness.
2591 switch (getElementType()->getIntegerBitWidth()) {
2592 default: llvm_unreachable("Invalid bitwidth for CDS");
2593 case 8: {
2594 auto EltVal = *reinterpret_cast<const uint8_t *>(EltPtr);
2595 return APInt(8, EltVal);
2596 }
2597 case 16: {
2598 auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr);
2599 return APInt(16, EltVal);
2600 }
2601 case 32: {
2602 auto EltVal = *reinterpret_cast<const uint32_t *>(EltPtr);
2603 return APInt(32, EltVal);
2604 }
2605 case 64: {
2606 auto EltVal = *reinterpret_cast<const uint64_t *>(EltPtr);
2607 return APInt(64, EltVal);
2608 }
2609 }
2610}
2611
Chris Lattnere4f3f102012-01-24 04:43:41 +00002612APFloat ConstantDataSequential::getElementAsAPFloat(unsigned Elt) const {
2613 const char *EltPtr = getElementPointer(Elt);
2614
2615 switch (getElementType()->getTypeID()) {
Nick Lewyckyff509622012-01-25 03:20:12 +00002616 default:
Craig Topperc514b542012-02-05 22:14:15 +00002617 llvm_unreachable("Accessor can only be used when element is float/double!");
Justin Bogner0ebc8602015-12-08 03:01:16 +00002618 case Type::HalfTyID: {
2619 auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr);
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002620 return APFloat(APFloat::IEEEhalf(), APInt(16, EltVal));
Justin Bogner0ebc8602015-12-08 03:01:16 +00002621 }
Benjamin Kramer7af984b2015-02-20 15:11:55 +00002622 case Type::FloatTyID: {
2623 auto EltVal = *reinterpret_cast<const uint32_t *>(EltPtr);
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002624 return APFloat(APFloat::IEEEsingle(), APInt(32, EltVal));
Benjamin Kramer7af984b2015-02-20 15:11:55 +00002625 }
2626 case Type::DoubleTyID: {
2627 auto EltVal = *reinterpret_cast<const uint64_t *>(EltPtr);
Stephan Bergmann17c7f702016-12-14 11:57:17 +00002628 return APFloat(APFloat::IEEEdouble(), APInt(64, EltVal));
Chris Lattnere4f3f102012-01-24 04:43:41 +00002629 }
Benjamin Kramer7af984b2015-02-20 15:11:55 +00002630 }
Chris Lattnere4f3f102012-01-24 04:43:41 +00002631}
2632
Chris Lattnere4f3f102012-01-24 04:43:41 +00002633float ConstantDataSequential::getElementAsFloat(unsigned Elt) const {
2634 assert(getElementType()->isFloatTy() &&
2635 "Accessor can only be used when element is a 'float'");
Craig Topper393ce692017-07-11 15:52:21 +00002636 return *reinterpret_cast<const float *>(getElementPointer(Elt));
Chris Lattnere4f3f102012-01-24 04:43:41 +00002637}
2638
Chris Lattnere4f3f102012-01-24 04:43:41 +00002639double ConstantDataSequential::getElementAsDouble(unsigned Elt) const {
2640 assert(getElementType()->isDoubleTy() &&
2641 "Accessor can only be used when element is a 'float'");
Craig Topper393ce692017-07-11 15:52:21 +00002642 return *reinterpret_cast<const double *>(getElementPointer(Elt));
Chris Lattnere4f3f102012-01-24 04:43:41 +00002643}
2644
Chris Lattnere4f3f102012-01-24 04:43:41 +00002645Constant *ConstantDataSequential::getElementAsConstant(unsigned Elt) const {
Justin Bogner0ebc8602015-12-08 03:01:16 +00002646 if (getElementType()->isHalfTy() || getElementType()->isFloatTy() ||
2647 getElementType()->isDoubleTy())
Chris Lattnere4f3f102012-01-24 04:43:41 +00002648 return ConstantFP::get(getContext(), getElementAsAPFloat(Elt));
Galina Kistanovafc259902012-07-13 01:25:27 +00002649
Chris Lattnere4f3f102012-01-24 04:43:41 +00002650 return ConstantInt::get(getElementType(), getElementAsInteger(Elt));
2651}
2652
Matthias Braun50ec0b52017-05-19 22:37:09 +00002653bool ConstantDataSequential::isString(unsigned CharSize) const {
2654 return isa<ArrayType>(getType()) && getElementType()->isIntegerTy(CharSize);
Chris Lattner5dd4d872012-01-24 09:01:07 +00002655}
Chris Lattner3756b912012-01-23 22:57:10 +00002656
Chris Lattner5dd4d872012-01-24 09:01:07 +00002657bool ConstantDataSequential::isCString() const {
2658 if (!isString())
2659 return false;
Galina Kistanovafc259902012-07-13 01:25:27 +00002660
Chris Lattner5dd4d872012-01-24 09:01:07 +00002661 StringRef Str = getAsString();
Galina Kistanovafc259902012-07-13 01:25:27 +00002662
Chris Lattner5dd4d872012-01-24 09:01:07 +00002663 // The last value must be nul.
2664 if (Str.back() != 0) return false;
Galina Kistanovafc259902012-07-13 01:25:27 +00002665
Chris Lattner5dd4d872012-01-24 09:01:07 +00002666 // Other elements must be non-nul.
2667 return Str.drop_back().find(0) == StringRef::npos;
2668}
Chris Lattner3756b912012-01-23 22:57:10 +00002669
Craig Topper0b4b4e32017-07-15 22:06:19 +00002670bool ConstantDataVector::isSplat() const {
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002671 const char *Base = getRawDataValues().data();
Galina Kistanovafc259902012-07-13 01:25:27 +00002672
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002673 // Compare elements 1+ to the 0'th element.
2674 unsigned EltSize = getElementByteSize();
2675 for (unsigned i = 1, e = getNumElements(); i != e; ++i)
2676 if (memcmp(Base, Base+i*EltSize, EltSize))
Craig Topper0b4b4e32017-07-15 22:06:19 +00002677 return false;
Galina Kistanovafc259902012-07-13 01:25:27 +00002678
Craig Topper0b4b4e32017-07-15 22:06:19 +00002679 return true;
2680}
2681
2682Constant *ConstantDataVector::getSplatValue() const {
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002683 // If they're all the same, return the 0th one as a representative.
Craig Topper0b4b4e32017-07-15 22:06:19 +00002684 return isSplat() ? getElementAsConstant(0) : nullptr;
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002685}
Chris Lattnera3b94ba2010-03-30 20:48:48 +00002686
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002687//===----------------------------------------------------------------------===//
Pete Cooper5815b1f2015-06-24 18:55:24 +00002688// handleOperandChange implementations
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002689
Pete Cooper5815b1f2015-06-24 18:55:24 +00002690/// Update this constant array to change uses of
Chris Lattner913849b2007-08-21 00:55:23 +00002691/// 'From' to be uses of 'To'. This must update the uniquing data structures
2692/// etc.
2693///
2694/// Note that we intentionally replace all uses of From with To here. Consider
2695/// a large array that uses 'From' 1000 times. By handling this case all here,
Pete Cooper5815b1f2015-06-24 18:55:24 +00002696/// ConstantArray::handleOperandChange is only invoked once, and that
Chris Lattner913849b2007-08-21 00:55:23 +00002697/// single invocation handles all 1000 uses. Handling them one at a time would
2698/// work, but would be really slow because it would have to unique each updated
2699/// array instance.
Chris Lattner31b132c2009-10-28 00:01:44 +00002700///
Mehdi Amini8914d292016-02-10 22:47:15 +00002701void Constant::handleOperandChange(Value *From, Value *To) {
Pete Cooper5815b1f2015-06-24 18:55:24 +00002702 Value *Replacement = nullptr;
2703 switch (getValueID()) {
2704 default:
2705 llvm_unreachable("Not a constant!");
2706#define HANDLE_CONSTANT(Name) \
2707 case Value::Name##Val: \
Mehdi Amini8914d292016-02-10 22:47:15 +00002708 Replacement = cast<Name>(this)->handleOperandChangeImpl(From, To); \
Pete Cooper5815b1f2015-06-24 18:55:24 +00002709 break;
2710#include "llvm/IR/Value.def"
2711 }
2712
2713 // If handleOperandChangeImpl returned nullptr, then it handled
2714 // replacing itself and we don't want to delete or replace anything else here.
2715 if (!Replacement)
2716 return;
2717
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002718 // I do need to replace this with an existing value.
2719 assert(Replacement != this && "I didn't contain From!");
2720
2721 // Everyone using this now uses the replacement.
2722 replaceAllUsesWith(Replacement);
2723
2724 // Delete the old constant!
2725 destroyConstant();
2726}
2727
Mehdi Amini8914d292016-02-10 22:47:15 +00002728Value *ConstantArray::handleOperandChangeImpl(Value *From, Value *To) {
Owen Andersonc2c79322009-07-28 18:32:17 +00002729 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2730 Constant *ToC = cast<Constant>(To);
2731
Talin46e9b442012-02-05 20:54:10 +00002732 SmallVector<Constant*, 8> Values;
Owen Andersonc2c79322009-07-28 18:32:17 +00002733 Values.reserve(getNumOperands()); // Build replacement array.
2734
Galina Kistanovafc259902012-07-13 01:25:27 +00002735 // Fill values with the modified operands of the constant array. Also,
Owen Andersonc2c79322009-07-28 18:32:17 +00002736 // compute whether this turns into an all-zeros array.
Owen Andersonc2c79322009-07-28 18:32:17 +00002737 unsigned NumUpdated = 0;
Galina Kistanovafc259902012-07-13 01:25:27 +00002738
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002739 // Keep track of whether all the values in the array are "ToC".
2740 bool AllSame = true;
Pete Cooper74510a42015-06-12 17:48:05 +00002741 Use *OperandList = getOperandList();
Mehdi Amini8914d292016-02-10 22:47:15 +00002742 unsigned OperandNo = 0;
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002743 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2744 Constant *Val = cast<Constant>(O->get());
2745 if (Val == From) {
Mehdi Amini8914d292016-02-10 22:47:15 +00002746 OperandNo = (O - OperandList);
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002747 Val = ToC;
2748 ++NumUpdated;
Owen Andersonc2c79322009-07-28 18:32:17 +00002749 }
Chris Lattnerf14a67f2012-01-26 02:31:22 +00002750 Values.push_back(Val);
Talin46e9b442012-02-05 20:54:10 +00002751 AllSame &= Val == ToC;
Owen Andersonc2c79322009-07-28 18:32:17 +00002752 }
Galina Kistanovafc259902012-07-13 01:25:27 +00002753
Pete Cooper5815b1f2015-06-24 18:55:24 +00002754 if (AllSame && ToC->isNullValue())
2755 return ConstantAggregateZero::get(getType());
2756
2757 if (AllSame && isa<UndefValue>(ToC))
2758 return UndefValue::get(getType());
Aaron Ballmane4b91dc2014-08-19 14:59:02 +00002759
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002760 // Check for any other type of constant-folding.
Pete Cooper5815b1f2015-06-24 18:55:24 +00002761 if (Constant *C = getImpl(getType(), Values))
2762 return C;
Aaron Ballmane4b91dc2014-08-19 14:59:02 +00002763
Duncan P. N. Exon Smith909620a2014-08-19 19:13:30 +00002764 // Update to the new value.
Pete Cooper5815b1f2015-06-24 18:55:24 +00002765 return getContext().pImpl->ArrayConstants.replaceOperandsInPlace(
Mehdi Amini8914d292016-02-10 22:47:15 +00002766 Values, this, From, ToC, NumUpdated, OperandNo);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002767}
2768
Mehdi Amini8914d292016-02-10 22:47:15 +00002769Value *ConstantStruct::handleOperandChangeImpl(Value *From, Value *To) {
Owen Anderson45308b52009-07-27 22:29:26 +00002770 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2771 Constant *ToC = cast<Constant>(To);
2772
Pete Cooper74510a42015-06-12 17:48:05 +00002773 Use *OperandList = getOperandList();
Owen Anderson45308b52009-07-27 22:29:26 +00002774
Talin46e9b442012-02-05 20:54:10 +00002775 SmallVector<Constant*, 8> Values;
Owen Anderson45308b52009-07-27 22:29:26 +00002776 Values.reserve(getNumOperands()); // Build replacement struct.
Galina Kistanovafc259902012-07-13 01:25:27 +00002777
2778 // Fill values with the modified operands of the constant struct. Also,
Owen Anderson45308b52009-07-27 22:29:26 +00002779 // compute whether this turns into an all-zeros struct.
Mehdi Amini8914d292016-02-10 22:47:15 +00002780 unsigned NumUpdated = 0;
2781 bool AllSame = true;
2782 unsigned OperandNo = 0;
2783 for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O) {
2784 Constant *Val = cast<Constant>(O->get());
2785 if (Val == From) {
2786 OperandNo = (O - OperandList);
2787 Val = ToC;
2788 ++NumUpdated;
Owen Anderson45308b52009-07-27 22:29:26 +00002789 }
Mehdi Amini8914d292016-02-10 22:47:15 +00002790 Values.push_back(Val);
2791 AllSame &= Val == ToC;
Owen Anderson45308b52009-07-27 22:29:26 +00002792 }
Galina Kistanovafc259902012-07-13 01:25:27 +00002793
Mehdi Amini8914d292016-02-10 22:47:15 +00002794 if (AllSame && ToC->isNullValue())
Pete Cooper5815b1f2015-06-24 18:55:24 +00002795 return ConstantAggregateZero::get(getType());
2796
Mehdi Amini8914d292016-02-10 22:47:15 +00002797 if (AllSame && isa<UndefValue>(ToC))
Pete Cooper5815b1f2015-06-24 18:55:24 +00002798 return UndefValue::get(getType());
Galina Kistanovafc259902012-07-13 01:25:27 +00002799
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002800 // Update to the new value.
Pete Cooper5815b1f2015-06-24 18:55:24 +00002801 return getContext().pImpl->StructConstants.replaceOperandsInPlace(
Mehdi Amini8914d292016-02-10 22:47:15 +00002802 Values, this, From, ToC, NumUpdated, OperandNo);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002803}
2804
Mehdi Amini8914d292016-02-10 22:47:15 +00002805Value *ConstantVector::handleOperandChangeImpl(Value *From, Value *To) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002806 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002807 Constant *ToC = cast<Constant>(To);
Galina Kistanovafc259902012-07-13 01:25:27 +00002808
Chris Lattnera474bb22012-01-26 20:40:56 +00002809 SmallVector<Constant*, 8> Values;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002810 Values.reserve(getNumOperands()); // Build replacement array...
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002811 unsigned NumUpdated = 0;
Mehdi Amini8914d292016-02-10 22:47:15 +00002812 unsigned OperandNo = 0;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002813 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2814 Constant *Val = getOperand(i);
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002815 if (Val == From) {
Mehdi Amini8914d292016-02-10 22:47:15 +00002816 OperandNo = i;
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002817 ++NumUpdated;
2818 Val = ToC;
2819 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002820 Values.push_back(Val);
2821 }
Galina Kistanovafc259902012-07-13 01:25:27 +00002822
Pete Cooper5815b1f2015-06-24 18:55:24 +00002823 if (Constant *C = getImpl(Values))
2824 return C;
Duncan P. N. Exon Smith687744d2014-08-19 02:24:46 +00002825
Duncan P. N. Exon Smith909620a2014-08-19 19:13:30 +00002826 // Update to the new value.
Pete Cooper5815b1f2015-06-24 18:55:24 +00002827 return getContext().pImpl->VectorConstants.replaceOperandsInPlace(
Mehdi Amini8914d292016-02-10 22:47:15 +00002828 Values, this, From, ToC, NumUpdated, OperandNo);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002829}
2830
Mehdi Amini8914d292016-02-10 22:47:15 +00002831Value *ConstantExpr::handleOperandChangeImpl(Value *From, Value *ToV) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002832 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2833 Constant *To = cast<Constant>(ToV);
Galina Kistanovafc259902012-07-13 01:25:27 +00002834
Chris Lattner37e38352012-01-26 20:37:11 +00002835 SmallVector<Constant*, 8> NewOps;
Duncan P. N. Exon Smith33de00c2014-08-19 20:03:35 +00002836 unsigned NumUpdated = 0;
Mehdi Amini8914d292016-02-10 22:47:15 +00002837 unsigned OperandNo = 0;
Chris Lattner37e38352012-01-26 20:37:11 +00002838 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2839 Constant *Op = getOperand(i);
Duncan P. N. Exon Smith33de00c2014-08-19 20:03:35 +00002840 if (Op == From) {
Mehdi Amini8914d292016-02-10 22:47:15 +00002841 OperandNo = i;
Duncan P. N. Exon Smith33de00c2014-08-19 20:03:35 +00002842 ++NumUpdated;
2843 Op = To;
2844 }
2845 NewOps.push_back(Op);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00002846 }
Duncan P. N. Exon Smith33de00c2014-08-19 20:03:35 +00002847 assert(NumUpdated && "I didn't contain From!");
Galina Kistanovafc259902012-07-13 01:25:27 +00002848
Pete Cooper5815b1f2015-06-24 18:55:24 +00002849 if (Constant *C = getWithOperands(NewOps, getType(), true))
2850 return C;
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002851
Duncan P. N. Exon Smith33de00c2014-08-19 20:03:35 +00002852 // Update to the new value.
Pete Cooper5815b1f2015-06-24 18:55:24 +00002853 return getContext().pImpl->ExprConstants.replaceOperandsInPlace(
Mehdi Amini8914d292016-02-10 22:47:15 +00002854 NewOps, this, From, To, NumUpdated, OperandNo);
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +00002855}
2856
James Molloyce545682012-11-17 17:56:30 +00002857Instruction *ConstantExpr::getAsInstruction() {
Benjamin Kramer5fbfe2f2015-02-28 13:20:15 +00002858 SmallVector<Value *, 4> ValueOperands(op_begin(), op_end());
James Molloyce545682012-11-17 17:56:30 +00002859 ArrayRef<Value*> Ops(ValueOperands);
2860
2861 switch (getOpcode()) {
2862 case Instruction::Trunc:
2863 case Instruction::ZExt:
2864 case Instruction::SExt:
2865 case Instruction::FPTrunc:
2866 case Instruction::FPExt:
2867 case Instruction::UIToFP:
2868 case Instruction::SIToFP:
2869 case Instruction::FPToUI:
2870 case Instruction::FPToSI:
2871 case Instruction::PtrToInt:
2872 case Instruction::IntToPtr:
2873 case Instruction::BitCast:
Eli Bendersky157a97a2014-01-18 22:54:33 +00002874 case Instruction::AddrSpaceCast:
James Molloyce545682012-11-17 17:56:30 +00002875 return CastInst::Create((Instruction::CastOps)getOpcode(),
2876 Ops[0], getType());
2877 case Instruction::Select:
2878 return SelectInst::Create(Ops[0], Ops[1], Ops[2]);
2879 case Instruction::InsertElement:
2880 return InsertElementInst::Create(Ops[0], Ops[1], Ops[2]);
2881 case Instruction::ExtractElement:
2882 return ExtractElementInst::Create(Ops[0], Ops[1]);
2883 case Instruction::InsertValue:
2884 return InsertValueInst::Create(Ops[0], Ops[1], getIndices());
2885 case Instruction::ExtractValue:
2886 return ExtractValueInst::Create(Ops[0], getIndices());
2887 case Instruction::ShuffleVector:
2888 return new ShuffleVectorInst(Ops[0], Ops[1], Ops[2]);
2889
David Blaikie741c8f82015-03-14 01:53:18 +00002890 case Instruction::GetElementPtr: {
2891 const auto *GO = cast<GEPOperator>(this);
2892 if (GO->isInBounds())
2893 return GetElementPtrInst::CreateInBounds(GO->getSourceElementType(),
2894 Ops[0], Ops.slice(1));
2895 return GetElementPtrInst::Create(GO->getSourceElementType(), Ops[0],
2896 Ops.slice(1));
2897 }
James Molloyce545682012-11-17 17:56:30 +00002898 case Instruction::ICmp:
2899 case Instruction::FCmp:
2900 return CmpInst::Create((Instruction::OtherOps)getOpcode(),
Craig Topper1c3f2832015-12-15 06:11:33 +00002901 (CmpInst::Predicate)getPredicate(), Ops[0], Ops[1]);
James Molloyce545682012-11-17 17:56:30 +00002902
2903 default:
2904 assert(getNumOperands() == 2 && "Must be binary operator?");
2905 BinaryOperator *BO =
2906 BinaryOperator::Create((Instruction::BinaryOps)getOpcode(),
2907 Ops[0], Ops[1]);
2908 if (isa<OverflowingBinaryOperator>(BO)) {
2909 BO->setHasNoUnsignedWrap(SubclassOptionalData &
2910 OverflowingBinaryOperator::NoUnsignedWrap);
2911 BO->setHasNoSignedWrap(SubclassOptionalData &
2912 OverflowingBinaryOperator::NoSignedWrap);
2913 }
2914 if (isa<PossiblyExactOperator>(BO))
2915 BO->setIsExact(SubclassOptionalData & PossiblyExactOperator::IsExact);
2916 return BO;
2917 }
2918}