Chris Lattner | 2b383d2e | 2003-05-13 21:37:02 +0000 | [diff] [blame] | 1 | //===-- Constants.cpp - Implement Constant nodes --------------------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | e17322b | 2011-02-07 20:03:14 +0000 | [diff] [blame] | 10 | // This file implements the Constant* classes. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 14 | #include "llvm/IR/Constants.h" |
Chris Lattner | 33e93b8 | 2007-02-27 03:05:06 +0000 | [diff] [blame] | 15 | #include "ConstantFold.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "LLVMContextImpl.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
| 18 | #include "llvm/ADT/SmallVector.h" |
| 19 | #include "llvm/ADT/StringExtras.h" |
| 20 | #include "llvm/ADT/StringMap.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/DerivedTypes.h" |
Chandler Carruth | 03eb0de | 2014-03-04 10:40:04 +0000 | [diff] [blame] | 22 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/GlobalValue.h" |
| 24 | #include "llvm/IR/Instructions.h" |
| 25 | #include "llvm/IR/Module.h" |
| 26 | #include "llvm/IR/Operator.h" |
Bill Wendling | 6a462f1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Debug.h" |
Torok Edwin | ccb29cd | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 69edc98 | 2006-09-28 00:35:06 +0000 | [diff] [blame] | 29 | #include "llvm/Support/ManagedStatic.h" |
Bill Wendling | 6a462f1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 30 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | 78683a7 | 2009-08-23 04:02:03 +0000 | [diff] [blame] | 31 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 32 | #include <algorithm> |
Serge Guelton | e38003f | 2017-05-09 19:31:13 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 34 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 35 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 36 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 37 | // Constant Class |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 38 | //===----------------------------------------------------------------------===// |
| 39 | |
Chris Lattner | ac5fb56 | 2011-07-15 05:58:04 +0000 | [diff] [blame] | 40 | bool 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 Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 44 | |
David Tweed | 5493fee | 2013-03-18 11:54:44 +0000 | [diff] [blame] | 45 | // Equivalent for a vector of -0.0's. |
| 46 | if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this)) |
| 47 | if (ConstantFP *SplatCFP = dyn_cast_or_null<ConstantFP>(CV->getSplatValue())) |
| 48 | if (SplatCFP && SplatCFP->isZero() && SplatCFP->isNegative()) |
| 49 | return true; |
| 50 | |
Owen Anderson | 8e85130 | 2015-11-20 22:34:48 +0000 | [diff] [blame] | 51 | 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 Tweed | 298e419 | 2013-03-19 10:16:40 +0000 | [diff] [blame] | 56 | // We've already handled true FP case; any other FP vectors can't represent -0.0. |
| 57 | if (getType()->isFPOrFPVectorTy()) |
| 58 | return false; |
David Tweed | 5493fee | 2013-03-18 11:54:44 +0000 | [diff] [blame] | 59 | |
Chris Lattner | ac5fb56 | 2011-07-15 05:58:04 +0000 | [diff] [blame] | 60 | // Otherwise, just use +0.0. |
| 61 | return isNullValue(); |
| 62 | } |
| 63 | |
Shuxin Yang | 9ca562e | 2013-01-09 00:53:25 +0000 | [diff] [blame] | 64 | // Return true iff this constant is positive zero (floating point), negative |
| 65 | // zero (floating point), or a null value. |
Shuxin Yang | f0537ab | 2013-01-09 00:13:41 +0000 | [diff] [blame] | 66 | bool 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 Anderson | 630077e | 2015-11-20 08:16:13 +0000 | [diff] [blame] | 71 | // Equivalent for a vector of -0.0's. |
| 72 | if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this)) |
| 73 | if (ConstantFP *SplatCFP = dyn_cast_or_null<ConstantFP>(CV->getSplatValue())) |
| 74 | if (SplatCFP && SplatCFP->isZero()) |
| 75 | return true; |
| 76 | |
Owen Anderson | 8e85130 | 2015-11-20 22:34:48 +0000 | [diff] [blame] | 77 | 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 Yang | f0537ab | 2013-01-09 00:13:41 +0000 | [diff] [blame] | 82 | // Otherwise, just use +0.0. |
| 83 | return isNullValue(); |
| 84 | } |
| 85 | |
Chris Lattner | be6610c | 2011-07-15 06:14:08 +0000 | [diff] [blame] | 86 | bool Constant::isNullValue() const { |
| 87 | // 0 is null. |
| 88 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(this)) |
| 89 | return CI->isZero(); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 90 | |
Chris Lattner | be6610c | 2011-07-15 06:14:08 +0000 | [diff] [blame] | 91 | // +0.0 is null. |
| 92 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this)) |
| 93 | return CFP->isZero() && !CFP->isNegative(); |
| 94 | |
David Majnemer | f0f224d | 2015-11-11 21:57:16 +0000 | [diff] [blame] | 95 | // 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 Lattner | be6610c | 2011-07-15 06:14:08 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Nadav Rotem | 365af6f | 2011-08-24 20:18:38 +0000 | [diff] [blame] | 101 | bool 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 Kramer | 42d098e | 2011-11-14 19:12:20 +0000 | [diff] [blame] | 110 | // Check for constant vectors which are splats of -1 values. |
Nadav Rotem | 365af6f | 2011-08-24 20:18:38 +0000 | [diff] [blame] | 111 | if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) |
Benjamin Kramer | 42d098e | 2011-11-14 19:12:20 +0000 | [diff] [blame] | 112 | if (Constant *Splat = CV->getSplatValue()) |
| 113 | return Splat->isAllOnesValue(); |
Nadav Rotem | 365af6f | 2011-08-24 20:18:38 +0000 | [diff] [blame] | 114 | |
Chris Lattner | f14a67f | 2012-01-26 02:31:22 +0000 | [diff] [blame] | 115 | // Check for constant vectors which are splats of -1 values. |
| 116 | if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this)) |
| 117 | if (Constant *Splat = CV->getSplatValue()) |
| 118 | return Splat->isAllOnesValue(); |
| 119 | |
Nadav Rotem | 365af6f | 2011-08-24 20:18:38 +0000 | [diff] [blame] | 120 | return false; |
| 121 | } |
Benjamin Kramer | 42d098e | 2011-11-14 19:12:20 +0000 | [diff] [blame] | 122 | |
David Majnemer | 1a0bbc8 | 2014-08-16 09:23:42 +0000 | [diff] [blame] | 123 | bool Constant::isOneValue() const { |
| 124 | // Check for 1 integers |
| 125 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(this)) |
| 126 | return CI->isOne(); |
| 127 | |
| 128 | // Check for FP which are bitcasted from 1 integers |
| 129 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this)) |
Craig Topper | 93ac6e1 | 2017-06-07 00:58:02 +0000 | [diff] [blame] | 130 | return CFP->getValueAPF().bitcastToAPInt().isOneValue(); |
David Majnemer | 1a0bbc8 | 2014-08-16 09:23:42 +0000 | [diff] [blame] | 131 | |
| 132 | // Check for constant vectors which are splats of 1 values. |
| 133 | if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) |
| 134 | if (Constant *Splat = CV->getSplatValue()) |
| 135 | return Splat->isOneValue(); |
| 136 | |
| 137 | // Check for constant vectors which are splats of 1 values. |
| 138 | if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this)) |
| 139 | if (Constant *Splat = CV->getSplatValue()) |
| 140 | return Splat->isOneValue(); |
| 141 | |
| 142 | return false; |
| 143 | } |
| 144 | |
David Majnemer | bdeef60 | 2014-07-02 06:07:09 +0000 | [diff] [blame] | 145 | bool Constant::isMinSignedValue() const { |
| 146 | // Check for INT_MIN integers |
| 147 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(this)) |
| 148 | return CI->isMinValue(/*isSigned=*/true); |
| 149 | |
| 150 | // Check for FP which are bitcasted from INT_MIN integers |
| 151 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this)) |
| 152 | return CFP->getValueAPF().bitcastToAPInt().isMinSignedValue(); |
| 153 | |
| 154 | // Check for constant vectors which are splats of INT_MIN values. |
| 155 | if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) |
| 156 | if (Constant *Splat = CV->getSplatValue()) |
| 157 | return Splat->isMinSignedValue(); |
| 158 | |
| 159 | // Check for constant vectors which are splats of INT_MIN values. |
| 160 | if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this)) |
| 161 | if (Constant *Splat = CV->getSplatValue()) |
| 162 | return Splat->isMinSignedValue(); |
| 163 | |
| 164 | return false; |
| 165 | } |
| 166 | |
David Majnemer | 0e6c986 | 2014-08-22 16:41:23 +0000 | [diff] [blame] | 167 | bool Constant::isNotMinSignedValue() const { |
| 168 | // Check for INT_MIN integers |
| 169 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(this)) |
| 170 | return !CI->isMinValue(/*isSigned=*/true); |
| 171 | |
| 172 | // Check for FP which are bitcasted from INT_MIN integers |
| 173 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this)) |
| 174 | return !CFP->getValueAPF().bitcastToAPInt().isMinSignedValue(); |
| 175 | |
| 176 | // Check for constant vectors which are splats of INT_MIN values. |
| 177 | if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) |
| 178 | if (Constant *Splat = CV->getSplatValue()) |
| 179 | return Splat->isNotMinSignedValue(); |
| 180 | |
| 181 | // Check for constant vectors which are splats of INT_MIN values. |
| 182 | if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this)) |
| 183 | if (Constant *Splat = CV->getSplatValue()) |
| 184 | return Splat->isNotMinSignedValue(); |
| 185 | |
| 186 | // It *may* contain INT_MIN, we can't tell. |
| 187 | return false; |
| 188 | } |
| 189 | |
Sanjay Patel | 1d0ac7c | 2016-04-29 22:03:27 +0000 | [diff] [blame] | 190 | /// Constructor to create a '0' constant of arbitrary type. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 191 | Constant *Constant::getNullValue(Type *Ty) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 192 | switch (Ty->getTypeID()) { |
| 193 | case Type::IntegerTyID: |
| 194 | return ConstantInt::get(Ty, 0); |
Dan Gohman | 518cda4 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 195 | case Type::HalfTyID: |
| 196 | return ConstantFP::get(Ty->getContext(), |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 197 | APFloat::getZero(APFloat::IEEEhalf())); |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 198 | case Type::FloatTyID: |
Benjamin Kramer | 8ceebfa | 2010-12-04 14:22:24 +0000 | [diff] [blame] | 199 | return ConstantFP::get(Ty->getContext(), |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 200 | APFloat::getZero(APFloat::IEEEsingle())); |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 201 | case Type::DoubleTyID: |
Benjamin Kramer | 8ceebfa | 2010-12-04 14:22:24 +0000 | [diff] [blame] | 202 | return ConstantFP::get(Ty->getContext(), |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 203 | APFloat::getZero(APFloat::IEEEdouble())); |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 204 | case Type::X86_FP80TyID: |
Benjamin Kramer | 8ceebfa | 2010-12-04 14:22:24 +0000 | [diff] [blame] | 205 | return ConstantFP::get(Ty->getContext(), |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 206 | APFloat::getZero(APFloat::x87DoubleExtended())); |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 207 | case Type::FP128TyID: |
| 208 | return ConstantFP::get(Ty->getContext(), |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 209 | APFloat::getZero(APFloat::IEEEquad())); |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 210 | case Type::PPC_FP128TyID: |
Benjamin Kramer | 8ceebfa | 2010-12-04 14:22:24 +0000 | [diff] [blame] | 211 | return ConstantFP::get(Ty->getContext(), |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 212 | APFloat(APFloat::PPCDoubleDouble(), |
Tim Northover | 29178a3 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 213 | APInt::getNullValue(128))); |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 214 | case Type::PointerTyID: |
| 215 | return ConstantPointerNull::get(cast<PointerType>(Ty)); |
| 216 | case Type::StructTyID: |
| 217 | case Type::ArrayTyID: |
| 218 | case Type::VectorTyID: |
| 219 | return ConstantAggregateZero::get(Ty); |
David Majnemer | f0f224d | 2015-11-11 21:57:16 +0000 | [diff] [blame] | 220 | case Type::TokenTyID: |
| 221 | return ConstantTokenNone::get(Ty->getContext()); |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 222 | default: |
| 223 | // Function, Label, or Opaque type? |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 224 | llvm_unreachable("Cannot create a null constant of that type!"); |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 228 | Constant *Constant::getIntegerValue(Type *Ty, const APInt &V) { |
| 229 | Type *ScalarTy = Ty->getScalarType(); |
Dan Gohman | f011f5a | 2009-08-03 22:07:33 +0000 | [diff] [blame] | 230 | |
| 231 | // Create the base integer constant. |
| 232 | Constant *C = ConstantInt::get(Ty->getContext(), V); |
| 233 | |
| 234 | // Convert an integer to a pointer, if necessary. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 235 | if (PointerType *PTy = dyn_cast<PointerType>(ScalarTy)) |
Dan Gohman | f011f5a | 2009-08-03 22:07:33 +0000 | [diff] [blame] | 236 | C = ConstantExpr::getIntToPtr(C, PTy); |
| 237 | |
| 238 | // Broadcast a scalar to a vector, if necessary. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 239 | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) |
Chris Lattner | e9eed29 | 2012-01-25 05:19:54 +0000 | [diff] [blame] | 240 | C = ConstantVector::getSplat(VTy->getNumElements(), C); |
Dan Gohman | f011f5a | 2009-08-03 22:07:33 +0000 | [diff] [blame] | 241 | |
| 242 | return C; |
| 243 | } |
| 244 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 245 | Constant *Constant::getAllOnesValue(Type *Ty) { |
| 246 | if (IntegerType *ITy = dyn_cast<IntegerType>(Ty)) |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 247 | return ConstantInt::get(Ty->getContext(), |
| 248 | APInt::getAllOnesValue(ITy->getBitWidth())); |
Nadav Rotem | 7cc6d12 | 2011-02-17 21:22:27 +0000 | [diff] [blame] | 249 | |
| 250 | if (Ty->isFloatingPointTy()) { |
| 251 | APFloat FL = APFloat::getAllOnesValue(Ty->getPrimitiveSizeInBits(), |
| 252 | !Ty->isPPC_FP128Ty()); |
| 253 | return ConstantFP::get(Ty->getContext(), FL); |
| 254 | } |
| 255 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 256 | VectorType *VTy = cast<VectorType>(Ty); |
Chris Lattner | e9eed29 | 2012-01-25 05:19:54 +0000 | [diff] [blame] | 257 | return ConstantVector::getSplat(VTy->getNumElements(), |
| 258 | getAllOnesValue(VTy->getElementType())); |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Chris Lattner | 7e683d1 | 2012-01-25 06:16:32 +0000 | [diff] [blame] | 261 | Constant *Constant::getAggregateElement(unsigned Elt) const { |
Duncan P. N. Exon Smith | 1de3c7e | 2016-04-05 21:10:45 +0000 | [diff] [blame] | 262 | if (const ConstantAggregate *CC = dyn_cast<ConstantAggregate>(this)) |
| 263 | return Elt < CC->getNumOperands() ? CC->getOperand(Elt) : nullptr; |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 264 | |
David Majnemer | 9b529a7 | 2015-02-16 04:02:09 +0000 | [diff] [blame] | 265 | if (const ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(this)) |
| 266 | return Elt < CAZ->getNumElements() ? CAZ->getElementValue(Elt) : nullptr; |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 267 | |
Chris Lattner | 7e683d1 | 2012-01-25 06:16:32 +0000 | [diff] [blame] | 268 | if (const UndefValue *UV = dyn_cast<UndefValue>(this)) |
David Majnemer | 9b529a7 | 2015-02-16 04:02:09 +0000 | [diff] [blame] | 269 | return Elt < UV->getNumElements() ? UV->getElementValue(Elt) : nullptr; |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 270 | |
Chris Lattner | 8326bd8 | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 271 | if (const ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(this)) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 272 | return Elt < CDS->getNumElements() ? CDS->getElementAsConstant(Elt) |
| 273 | : nullptr; |
| 274 | return nullptr; |
Chris Lattner | 7e683d1 | 2012-01-25 06:16:32 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | Constant *Constant::getAggregateElement(Constant *Elt) const { |
| 278 | assert(isa<IntegerType>(Elt->getType()) && "Index must be an integer"); |
| 279 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Elt)) |
| 280 | return getAggregateElement(CI->getZExtValue()); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 281 | return nullptr; |
Chris Lattner | 7e683d1 | 2012-01-25 06:16:32 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Pete Cooper | 86dd4cf | 2015-06-23 21:55:11 +0000 | [diff] [blame] | 284 | void Constant::destroyConstant() { |
| 285 | /// First call destroyConstantImpl on the subclass. This gives the subclass |
| 286 | /// a chance to remove the constant from any maps/pools it's contained in. |
| 287 | switch (getValueID()) { |
| 288 | default: |
| 289 | llvm_unreachable("Not a constant!"); |
| 290 | #define HANDLE_CONSTANT(Name) \ |
| 291 | case Value::Name##Val: \ |
| 292 | cast<Name>(this)->destroyConstantImpl(); \ |
| 293 | break; |
| 294 | #include "llvm/IR/Value.def" |
| 295 | } |
Chris Lattner | 7e683d1 | 2012-01-25 06:16:32 +0000 | [diff] [blame] | 296 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 297 | // When a Constant is destroyed, there may be lingering |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 298 | // references to the constant by other constants in the constant pool. These |
Misha Brukman | be372b9 | 2003-08-21 22:14:26 +0000 | [diff] [blame] | 299 | // constants are implicitly dependent on the module that is being deleted, |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 300 | // but they don't know that. Because we only find out when the CPV is |
| 301 | // deleted, we must now notify all of our users (that should only be |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 302 | // Constants) that they are, in fact, invalid now and should be deleted. |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 303 | // |
| 304 | while (!use_empty()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 305 | Value *V = user_back(); |
Pete Cooper | 86dd4cf | 2015-06-23 21:55:11 +0000 | [diff] [blame] | 306 | #ifndef NDEBUG // Only in -g mode... |
Chris Lattner | 78683a7 | 2009-08-23 04:02:03 +0000 | [diff] [blame] | 307 | if (!isa<Constant>(V)) { |
David Greene | 1e27a13 | 2010-01-05 01:29:19 +0000 | [diff] [blame] | 308 | dbgs() << "While deleting: " << *this |
Pete Cooper | 86dd4cf | 2015-06-23 21:55:11 +0000 | [diff] [blame] | 309 | << "\n\nUse still stuck around after Def is destroyed: " << *V |
| 310 | << "\n\n"; |
Chris Lattner | 78683a7 | 2009-08-23 04:02:03 +0000 | [diff] [blame] | 311 | } |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 312 | #endif |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 313 | assert(isa<Constant>(V) && "References remain to Constant being destroyed"); |
Chris Lattner | 8326bd8 | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 314 | cast<Constant>(V)->destroyConstant(); |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 315 | |
| 316 | // The constant should remove itself from our use list... |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 317 | assert((use_empty() || user_back() != V) && "Constant not removed!"); |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | // Value has no outstanding references it is safe to delete it now... |
| 321 | delete this; |
Chris Lattner | 3856934 | 2001-10-01 20:11:19 +0000 | [diff] [blame] | 322 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 323 | |
Benjamin Kramer | 89ca4bc | 2013-04-13 12:53:18 +0000 | [diff] [blame] | 324 | static bool canTrapImpl(const Constant *C, |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 325 | SmallPtrSetImpl<const ConstantExpr *> &NonTrappingOps) { |
Benjamin Kramer | 89ca4bc | 2013-04-13 12:53:18 +0000 | [diff] [blame] | 326 | assert(C->getType()->isFirstClassType() && "Cannot evaluate aggregate vals!"); |
Chris Lattner | 23dd1f6 | 2006-10-20 00:27:06 +0000 | [diff] [blame] | 327 | // The only thing that could possibly trap are constant exprs. |
Benjamin Kramer | 89ca4bc | 2013-04-13 12:53:18 +0000 | [diff] [blame] | 328 | const ConstantExpr *CE = dyn_cast<ConstantExpr>(C); |
| 329 | if (!CE) |
| 330 | return false; |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 331 | |
| 332 | // ConstantExpr traps if any operands can trap. |
Benjamin Kramer | 89ca4bc | 2013-04-13 12:53:18 +0000 | [diff] [blame] | 333 | for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) { |
| 334 | if (ConstantExpr *Op = dyn_cast<ConstantExpr>(CE->getOperand(i))) { |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 335 | if (NonTrappingOps.insert(Op).second && canTrapImpl(Op, NonTrappingOps)) |
Benjamin Kramer | 89ca4bc | 2013-04-13 12:53:18 +0000 | [diff] [blame] | 336 | return true; |
| 337 | } |
| 338 | } |
Chris Lattner | 23dd1f6 | 2006-10-20 00:27:06 +0000 | [diff] [blame] | 339 | |
| 340 | // Otherwise, only specific operations can trap. |
| 341 | switch (CE->getOpcode()) { |
| 342 | default: |
| 343 | return false; |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 344 | case Instruction::UDiv: |
| 345 | case Instruction::SDiv: |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 346 | case Instruction::URem: |
| 347 | case Instruction::SRem: |
Chris Lattner | 23dd1f6 | 2006-10-20 00:27:06 +0000 | [diff] [blame] | 348 | // Div and rem can trap if the RHS is not known to be non-zero. |
Chris Lattner | a91a563 | 2009-10-28 05:14:34 +0000 | [diff] [blame] | 349 | if (!isa<ConstantInt>(CE->getOperand(1)) ||CE->getOperand(1)->isNullValue()) |
Chris Lattner | 23dd1f6 | 2006-10-20 00:27:06 +0000 | [diff] [blame] | 350 | return true; |
| 351 | return false; |
| 352 | } |
| 353 | } |
| 354 | |
Benjamin Kramer | 89ca4bc | 2013-04-13 12:53:18 +0000 | [diff] [blame] | 355 | bool Constant::canTrap() const { |
| 356 | SmallPtrSet<const ConstantExpr *, 4> NonTrappingOps; |
| 357 | return canTrapImpl(this, NonTrappingOps); |
| 358 | } |
| 359 | |
Hans Wennborg | 4dc8951 | 2014-06-20 00:38:12 +0000 | [diff] [blame] | 360 | /// Check if C contains a GlobalValue for which Predicate is true. |
| 361 | static bool |
| 362 | ConstHasGlobalValuePredicate(const Constant *C, |
| 363 | bool (*Predicate)(const GlobalValue *)) { |
| 364 | SmallPtrSet<const Constant *, 8> Visited; |
| 365 | SmallVector<const Constant *, 8> WorkList; |
| 366 | WorkList.push_back(C); |
| 367 | Visited.insert(C); |
Hans Wennborg | 709e015 | 2012-11-15 11:40:00 +0000 | [diff] [blame] | 368 | |
| 369 | while (!WorkList.empty()) { |
Hans Wennborg | 4dc8951 | 2014-06-20 00:38:12 +0000 | [diff] [blame] | 370 | const Constant *WorkItem = WorkList.pop_back_val(); |
| 371 | if (const auto *GV = dyn_cast<GlobalValue>(WorkItem)) |
| 372 | if (Predicate(GV)) |
Hans Wennborg | 709e015 | 2012-11-15 11:40:00 +0000 | [diff] [blame] | 373 | return true; |
Hans Wennborg | 4dc8951 | 2014-06-20 00:38:12 +0000 | [diff] [blame] | 374 | for (const Value *Op : WorkItem->operands()) { |
| 375 | const Constant *ConstOp = dyn_cast<Constant>(Op); |
| 376 | if (!ConstOp) |
Hans Wennborg | 18aa1240 | 2012-11-16 10:33:25 +0000 | [diff] [blame] | 377 | continue; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 378 | if (Visited.insert(ConstOp).second) |
Hans Wennborg | 4dc8951 | 2014-06-20 00:38:12 +0000 | [diff] [blame] | 379 | WorkList.push_back(ConstOp); |
Hans Wennborg | 709e015 | 2012-11-15 11:40:00 +0000 | [diff] [blame] | 380 | } |
| 381 | } |
Hans Wennborg | 709e015 | 2012-11-15 11:40:00 +0000 | [diff] [blame] | 382 | return false; |
| 383 | } |
| 384 | |
Hans Wennborg | 4dc8951 | 2014-06-20 00:38:12 +0000 | [diff] [blame] | 385 | bool Constant::isThreadDependent() const { |
| 386 | auto DLLImportPredicate = [](const GlobalValue *GV) { |
| 387 | return GV->isThreadLocal(); |
| 388 | }; |
| 389 | return ConstHasGlobalValuePredicate(this, DLLImportPredicate); |
| 390 | } |
| 391 | |
| 392 | bool Constant::isDLLImportDependent() const { |
| 393 | auto DLLImportPredicate = [](const GlobalValue *GV) { |
| 394 | return GV->hasDLLImportStorageClass(); |
| 395 | }; |
| 396 | return ConstHasGlobalValuePredicate(this, DLLImportPredicate); |
| 397 | } |
| 398 | |
Chris Lattner | 253bc77 | 2009-11-01 18:11:50 +0000 | [diff] [blame] | 399 | bool Constant::isConstantUsed() const { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 400 | for (const User *U : users()) { |
| 401 | const Constant *UC = dyn_cast<Constant>(U); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 402 | if (!UC || isa<GlobalValue>(UC)) |
Chris Lattner | 253bc77 | 2009-11-01 18:11:50 +0000 | [diff] [blame] | 403 | return true; |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 404 | |
Chris Lattner | 253bc77 | 2009-11-01 18:11:50 +0000 | [diff] [blame] | 405 | if (UC->isConstantUsed()) |
| 406 | return true; |
| 407 | } |
| 408 | return false; |
| 409 | } |
| 410 | |
Rafael Espindola | 65e4902 | 2015-11-17 00:51:23 +0000 | [diff] [blame] | 411 | bool Constant::needsRelocation() const { |
| 412 | if (isa<GlobalValue>(this)) |
| 413 | return true; // Global reference. |
| 414 | |
Chris Lattner | 2cb85b4 | 2009-10-28 04:12:16 +0000 | [diff] [blame] | 415 | if (const BlockAddress *BA = dyn_cast<BlockAddress>(this)) |
Rafael Espindola | 65e4902 | 2015-11-17 00:51:23 +0000 | [diff] [blame] | 416 | return BA->getFunction()->needsRelocation(); |
| 417 | |
Chris Lattner | a7cfc43 | 2010-01-03 18:09:40 +0000 | [diff] [blame] | 418 | // While raw uses of blockaddress need to be relocated, differences between |
| 419 | // two of them don't when they are for labels in the same function. This is a |
| 420 | // common idiom when creating a table for the indirect goto extension, so we |
| 421 | // handle it efficiently here. |
| 422 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(this)) |
| 423 | if (CE->getOpcode() == Instruction::Sub) { |
| 424 | ConstantExpr *LHS = dyn_cast<ConstantExpr>(CE->getOperand(0)); |
| 425 | ConstantExpr *RHS = dyn_cast<ConstantExpr>(CE->getOperand(1)); |
Rafael Espindola | 65e4902 | 2015-11-17 00:51:23 +0000 | [diff] [blame] | 426 | if (LHS && RHS && LHS->getOpcode() == Instruction::PtrToInt && |
Chris Lattner | a7cfc43 | 2010-01-03 18:09:40 +0000 | [diff] [blame] | 427 | RHS->getOpcode() == Instruction::PtrToInt && |
| 428 | isa<BlockAddress>(LHS->getOperand(0)) && |
| 429 | isa<BlockAddress>(RHS->getOperand(0)) && |
| 430 | cast<BlockAddress>(LHS->getOperand(0))->getFunction() == |
Rafael Espindola | 65e4902 | 2015-11-17 00:51:23 +0000 | [diff] [blame] | 431 | cast<BlockAddress>(RHS->getOperand(0))->getFunction()) |
| 432 | return false; |
Chris Lattner | a7cfc43 | 2010-01-03 18:09:40 +0000 | [diff] [blame] | 433 | } |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 434 | |
Rafael Espindola | 65e4902 | 2015-11-17 00:51:23 +0000 | [diff] [blame] | 435 | bool Result = false; |
Evan Cheng | f9e003b | 2007-03-08 00:59:12 +0000 | [diff] [blame] | 436 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
Rafael Espindola | 65e4902 | 2015-11-17 00:51:23 +0000 | [diff] [blame] | 437 | Result |= cast<Constant>(getOperand(i))->needsRelocation(); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 438 | |
Chris Lattner | 4565ef5 | 2009-07-22 00:05:44 +0000 | [diff] [blame] | 439 | return Result; |
Evan Cheng | f9e003b | 2007-03-08 00:59:12 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Sanjay Patel | 1d0ac7c | 2016-04-29 22:03:27 +0000 | [diff] [blame] | 442 | /// If the specified constantexpr is dead, remove it. This involves recursively |
| 443 | /// eliminating any dead users of the constantexpr. |
Chris Lattner | 8488640 | 2011-02-18 04:41:42 +0000 | [diff] [blame] | 444 | static bool removeDeadUsersOfConstant(const Constant *C) { |
| 445 | if (isa<GlobalValue>(C)) return false; // Cannot remove this |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 446 | |
Chris Lattner | 8488640 | 2011-02-18 04:41:42 +0000 | [diff] [blame] | 447 | while (!C->use_empty()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 448 | const Constant *User = dyn_cast<Constant>(C->user_back()); |
Chris Lattner | 8488640 | 2011-02-18 04:41:42 +0000 | [diff] [blame] | 449 | if (!User) return false; // Non-constant usage; |
| 450 | if (!removeDeadUsersOfConstant(User)) |
| 451 | return false; // Constant wasn't dead |
| 452 | } |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 453 | |
Chris Lattner | 8488640 | 2011-02-18 04:41:42 +0000 | [diff] [blame] | 454 | const_cast<Constant*>(C)->destroyConstant(); |
| 455 | return true; |
| 456 | } |
| 457 | |
| 458 | |
Chris Lattner | 8488640 | 2011-02-18 04:41:42 +0000 | [diff] [blame] | 459 | void Constant::removeDeadConstantUsers() const { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 460 | Value::const_user_iterator I = user_begin(), E = user_end(); |
| 461 | Value::const_user_iterator LastNonDeadUser = E; |
Chris Lattner | 8488640 | 2011-02-18 04:41:42 +0000 | [diff] [blame] | 462 | while (I != E) { |
| 463 | const Constant *User = dyn_cast<Constant>(*I); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 464 | if (!User) { |
Chris Lattner | 8488640 | 2011-02-18 04:41:42 +0000 | [diff] [blame] | 465 | LastNonDeadUser = I; |
| 466 | ++I; |
| 467 | continue; |
| 468 | } |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 469 | |
Chris Lattner | 8488640 | 2011-02-18 04:41:42 +0000 | [diff] [blame] | 470 | if (!removeDeadUsersOfConstant(User)) { |
| 471 | // If the constant wasn't dead, remember that this was the last live use |
| 472 | // and move on to the next constant. |
| 473 | LastNonDeadUser = I; |
| 474 | ++I; |
| 475 | continue; |
| 476 | } |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 477 | |
Chris Lattner | 8488640 | 2011-02-18 04:41:42 +0000 | [diff] [blame] | 478 | // If the constant was dead, then the iterator is invalidated. |
| 479 | if (LastNonDeadUser == E) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 480 | I = user_begin(); |
Chris Lattner | 8488640 | 2011-02-18 04:41:42 +0000 | [diff] [blame] | 481 | if (I == E) break; |
| 482 | } else { |
| 483 | I = LastNonDeadUser; |
| 484 | ++I; |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | |
Chris Lattner | 2105d66 | 2008-07-10 00:28:11 +0000 | [diff] [blame] | 490 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 491 | //===----------------------------------------------------------------------===// |
Chris Lattner | a80bf0b | 2007-02-20 06:39:57 +0000 | [diff] [blame] | 492 | // ConstantInt |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 493 | //===----------------------------------------------------------------------===// |
| 494 | |
Duncan P. N. Exon Smith | b645279 | 2016-02-21 02:39:49 +0000 | [diff] [blame] | 495 | ConstantInt::ConstantInt(IntegerType *Ty, const APInt &V) |
| 496 | : ConstantData(Ty, ConstantIntVal), Val(V) { |
Reid Spencer | b31bffe | 2007-02-26 23:54:03 +0000 | [diff] [blame] | 497 | assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Nick Lewycky | 92db8e8 | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 500 | ConstantInt *ConstantInt::getTrue(LLVMContext &Context) { |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 501 | LLVMContextImpl *pImpl = Context.pImpl; |
Benjamin Kramer | ddd1b7b | 2010-11-20 18:43:35 +0000 | [diff] [blame] | 502 | if (!pImpl->TheTrueVal) |
| 503 | pImpl->TheTrueVal = ConstantInt::get(Type::getInt1Ty(Context), 1); |
| 504 | return pImpl->TheTrueVal; |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Nick Lewycky | 92db8e8 | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 507 | ConstantInt *ConstantInt::getFalse(LLVMContext &Context) { |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 508 | LLVMContextImpl *pImpl = Context.pImpl; |
Benjamin Kramer | ddd1b7b | 2010-11-20 18:43:35 +0000 | [diff] [blame] | 509 | if (!pImpl->TheFalseVal) |
| 510 | pImpl->TheFalseVal = ConstantInt::get(Type::getInt1Ty(Context), 0); |
| 511 | return pImpl->TheFalseVal; |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 514 | Constant *ConstantInt::getTrue(Type *Ty) { |
Craig Topper | fde4723 | 2017-07-09 07:04:03 +0000 | [diff] [blame^] | 515 | assert(Ty->isIntOrIntVectorTy(1) && "Type not i1 or vector of i1."); |
Sanjay Patel | 70a575a | 2017-04-16 17:00:21 +0000 | [diff] [blame] | 516 | ConstantInt *TrueC = ConstantInt::getTrue(Ty->getContext()); |
| 517 | if (auto *VTy = dyn_cast<VectorType>(Ty)) |
| 518 | return ConstantVector::getSplat(VTy->getNumElements(), TrueC); |
| 519 | return TrueC; |
Nick Lewycky | 92db8e8 | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 522 | Constant *ConstantInt::getFalse(Type *Ty) { |
Craig Topper | fde4723 | 2017-07-09 07:04:03 +0000 | [diff] [blame^] | 523 | assert(Ty->isIntOrIntVectorTy(1) && "Type not i1 or vector of i1."); |
Sanjay Patel | 70a575a | 2017-04-16 17:00:21 +0000 | [diff] [blame] | 524 | ConstantInt *FalseC = ConstantInt::getFalse(Ty->getContext()); |
| 525 | if (auto *VTy = dyn_cast<VectorType>(Ty)) |
| 526 | return ConstantVector::getSplat(VTy->getNumElements(), FalseC); |
| 527 | return FalseC; |
Nick Lewycky | 92db8e8 | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Benjamin Kramer | 8e5dc53 | 2014-12-06 13:12:56 +0000 | [diff] [blame] | 530 | // Get a ConstantInt from an APInt. |
Nick Lewycky | 92db8e8 | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 531 | ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt &V) { |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 532 | // get an existing value or the insertion position |
Benjamin Kramer | 320682f | 2013-06-01 17:51:03 +0000 | [diff] [blame] | 533 | LLVMContextImpl *pImpl = Context.pImpl; |
Justin Lebar | 611c5c2 | 2016-10-10 16:26:13 +0000 | [diff] [blame] | 534 | std::unique_ptr<ConstantInt> &Slot = pImpl->IntConstants[V]; |
Benjamin Kramer | 8e5dc53 | 2014-12-06 13:12:56 +0000 | [diff] [blame] | 535 | if (!Slot) { |
| 536 | // Get the corresponding integer type for the bit width of the value. |
| 537 | IntegerType *ITy = IntegerType::get(Context, V.getBitWidth()); |
Justin Lebar | 611c5c2 | 2016-10-10 16:26:13 +0000 | [diff] [blame] | 538 | Slot.reset(new ConstantInt(ITy, V)); |
Benjamin Kramer | 8e5dc53 | 2014-12-06 13:12:56 +0000 | [diff] [blame] | 539 | } |
| 540 | assert(Slot->getType() == IntegerType::get(Context, V.getBitWidth())); |
Justin Lebar | 611c5c2 | 2016-10-10 16:26:13 +0000 | [diff] [blame] | 541 | return Slot.get(); |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 544 | Constant *ConstantInt::get(Type *Ty, uint64_t V, bool isSigned) { |
Nick Lewycky | 92db8e8 | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 545 | Constant *C = get(cast<IntegerType>(Ty->getScalarType()), V, isSigned); |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 546 | |
| 547 | // For vectors, broadcast the value. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 548 | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) |
Chris Lattner | e9eed29 | 2012-01-25 05:19:54 +0000 | [diff] [blame] | 549 | return ConstantVector::getSplat(VTy->getNumElements(), C); |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 550 | |
| 551 | return C; |
| 552 | } |
| 553 | |
Sanjay Patel | f3587ec | 2016-05-18 22:05:28 +0000 | [diff] [blame] | 554 | ConstantInt *ConstantInt::get(IntegerType *Ty, uint64_t V, bool isSigned) { |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 555 | return get(Ty->getContext(), APInt(Ty->getBitWidth(), V, isSigned)); |
| 556 | } |
| 557 | |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 558 | ConstantInt *ConstantInt::getSigned(IntegerType *Ty, int64_t V) { |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 559 | return get(Ty, V, true); |
| 560 | } |
| 561 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 562 | Constant *ConstantInt::getSigned(Type *Ty, int64_t V) { |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 563 | return get(Ty, V, true); |
| 564 | } |
| 565 | |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 566 | Constant *ConstantInt::get(Type *Ty, const APInt& V) { |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 567 | ConstantInt *C = get(Ty->getContext(), V); |
| 568 | assert(C->getType() == Ty->getScalarType() && |
| 569 | "ConstantInt type doesn't match the type implied by its value!"); |
| 570 | |
| 571 | // For vectors, broadcast the value. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 572 | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) |
Chris Lattner | e9eed29 | 2012-01-25 05:19:54 +0000 | [diff] [blame] | 573 | return ConstantVector::getSplat(VTy->getNumElements(), C); |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 574 | |
| 575 | return C; |
| 576 | } |
| 577 | |
Sanjay Patel | f3587ec | 2016-05-18 22:05:28 +0000 | [diff] [blame] | 578 | ConstantInt *ConstantInt::get(IntegerType* Ty, StringRef Str, uint8_t radix) { |
Erick Tryzelaar | fc2280d | 2009-08-16 23:36:33 +0000 | [diff] [blame] | 579 | return get(Ty->getContext(), APInt(Ty->getBitWidth(), Str, radix)); |
| 580 | } |
| 581 | |
Pete Cooper | 86dd4cf | 2015-06-23 21:55:11 +0000 | [diff] [blame] | 582 | /// Remove the constant from the constant table. |
| 583 | void ConstantInt::destroyConstantImpl() { |
| 584 | llvm_unreachable("You can't ConstantInt->destroyConstantImpl()!"); |
| 585 | } |
| 586 | |
Chris Lattner | a80bf0b | 2007-02-20 06:39:57 +0000 | [diff] [blame] | 587 | //===----------------------------------------------------------------------===// |
Chris Lattner | c6ee77d | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 588 | // ConstantFP |
Chris Lattner | a80bf0b | 2007-02-20 06:39:57 +0000 | [diff] [blame] | 589 | //===----------------------------------------------------------------------===// |
| 590 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 591 | static const fltSemantics *TypeToFloatSemantics(Type *Ty) { |
Dan Gohman | 518cda4 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 592 | if (Ty->isHalfTy()) |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 593 | return &APFloat::IEEEhalf(); |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 594 | if (Ty->isFloatTy()) |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 595 | return &APFloat::IEEEsingle(); |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 596 | if (Ty->isDoubleTy()) |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 597 | return &APFloat::IEEEdouble(); |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 598 | if (Ty->isX86_FP80Ty()) |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 599 | return &APFloat::x87DoubleExtended(); |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 600 | else if (Ty->isFP128Ty()) |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 601 | return &APFloat::IEEEquad(); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 602 | |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 603 | assert(Ty->isPPC_FP128Ty() && "Unknown FP format"); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 604 | return &APFloat::PPCDoubleDouble(); |
Rafael Espindola | f5d53d4 | 2009-07-15 17:40:42 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 607 | Constant *ConstantFP::get(Type *Ty, double V) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 608 | LLVMContext &Context = Ty->getContext(); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 609 | |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 610 | APFloat FV(V); |
| 611 | bool ignored; |
| 612 | FV.convert(*TypeToFloatSemantics(Ty->getScalarType()), |
| 613 | APFloat::rmNearestTiesToEven, &ignored); |
| 614 | Constant *C = get(Context, FV); |
| 615 | |
| 616 | // For vectors, broadcast the value. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 617 | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) |
Chris Lattner | e9eed29 | 2012-01-25 05:19:54 +0000 | [diff] [blame] | 618 | return ConstantVector::getSplat(VTy->getNumElements(), C); |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 619 | |
| 620 | return C; |
| 621 | } |
| 622 | |
Erick Tryzelaar | fc2280d | 2009-08-16 23:36:33 +0000 | [diff] [blame] | 623 | |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 624 | Constant *ConstantFP::get(Type *Ty, StringRef Str) { |
Erick Tryzelaar | fc2280d | 2009-08-16 23:36:33 +0000 | [diff] [blame] | 625 | LLVMContext &Context = Ty->getContext(); |
| 626 | |
| 627 | APFloat FV(*TypeToFloatSemantics(Ty->getScalarType()), Str); |
| 628 | Constant *C = get(Context, FV); |
| 629 | |
| 630 | // For vectors, broadcast the value. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 631 | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) |
Chris Lattner | e9eed29 | 2012-01-25 05:19:54 +0000 | [diff] [blame] | 632 | return ConstantVector::getSplat(VTy->getNumElements(), C); |
Erick Tryzelaar | fc2280d | 2009-08-16 23:36:33 +0000 | [diff] [blame] | 633 | |
| 634 | return C; |
| 635 | } |
| 636 | |
Tom Stellard | 67246d1 | 2015-04-20 19:38:24 +0000 | [diff] [blame] | 637 | Constant *ConstantFP::getNaN(Type *Ty, bool Negative, unsigned Type) { |
| 638 | const fltSemantics &Semantics = *TypeToFloatSemantics(Ty->getScalarType()); |
| 639 | APFloat NaN = APFloat::getNaN(Semantics, Negative, Type); |
| 640 | Constant *C = get(Ty->getContext(), NaN); |
| 641 | |
| 642 | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) |
| 643 | return ConstantVector::getSplat(VTy->getNumElements(), C); |
| 644 | |
| 645 | return C; |
| 646 | } |
| 647 | |
Benjamin Kramer | 5d2ff22 | 2014-01-18 16:43:06 +0000 | [diff] [blame] | 648 | Constant *ConstantFP::getNegativeZero(Type *Ty) { |
| 649 | const fltSemantics &Semantics = *TypeToFloatSemantics(Ty->getScalarType()); |
| 650 | APFloat NegZero = APFloat::getZero(Semantics, /*Negative=*/true); |
| 651 | Constant *C = get(Ty->getContext(), NegZero); |
Erick Tryzelaar | fc2280d | 2009-08-16 23:36:33 +0000 | [diff] [blame] | 652 | |
Benjamin Kramer | 5d2ff22 | 2014-01-18 16:43:06 +0000 | [diff] [blame] | 653 | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) |
| 654 | return ConstantVector::getSplat(VTy->getNumElements(), C); |
| 655 | |
| 656 | return C; |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | |
Chris Lattner | e9eed29 | 2012-01-25 05:19:54 +0000 | [diff] [blame] | 660 | Constant *ConstantFP::getZeroValueForNegation(Type *Ty) { |
Benjamin Kramer | 5d2ff22 | 2014-01-18 16:43:06 +0000 | [diff] [blame] | 661 | if (Ty->isFPOrFPVectorTy()) |
| 662 | return getNegativeZero(Ty); |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 663 | |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 664 | return Constant::getNullValue(Ty); |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | |
| 668 | // ConstantFP accessors. |
| 669 | ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 670 | LLVMContextImpl* pImpl = Context.pImpl; |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 671 | |
Justin Lebar | 611c5c2 | 2016-10-10 16:26:13 +0000 | [diff] [blame] | 672 | std::unique_ptr<ConstantFP> &Slot = pImpl->FPConstants[V]; |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 673 | |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 674 | if (!Slot) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 675 | Type *Ty; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 676 | if (&V.getSemantics() == &APFloat::IEEEhalf()) |
Dan Gohman | 518cda4 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 677 | Ty = Type::getHalfTy(Context); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 678 | else if (&V.getSemantics() == &APFloat::IEEEsingle()) |
Owen Anderson | 5dab84c | 2009-10-19 20:11:52 +0000 | [diff] [blame] | 679 | Ty = Type::getFloatTy(Context); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 680 | else if (&V.getSemantics() == &APFloat::IEEEdouble()) |
Owen Anderson | 5dab84c | 2009-10-19 20:11:52 +0000 | [diff] [blame] | 681 | Ty = Type::getDoubleTy(Context); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 682 | else if (&V.getSemantics() == &APFloat::x87DoubleExtended()) |
Owen Anderson | 5dab84c | 2009-10-19 20:11:52 +0000 | [diff] [blame] | 683 | Ty = Type::getX86_FP80Ty(Context); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 684 | else if (&V.getSemantics() == &APFloat::IEEEquad()) |
Owen Anderson | 5dab84c | 2009-10-19 20:11:52 +0000 | [diff] [blame] | 685 | Ty = Type::getFP128Ty(Context); |
| 686 | else { |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 687 | assert(&V.getSemantics() == &APFloat::PPCDoubleDouble() && |
Owen Anderson | 5dab84c | 2009-10-19 20:11:52 +0000 | [diff] [blame] | 688 | "Unknown FP format"); |
| 689 | Ty = Type::getPPC_FP128Ty(Context); |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 690 | } |
Justin Lebar | 611c5c2 | 2016-10-10 16:26:13 +0000 | [diff] [blame] | 691 | Slot.reset(new ConstantFP(Ty, V)); |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 692 | } |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 693 | |
Justin Lebar | 611c5c2 | 2016-10-10 16:26:13 +0000 | [diff] [blame] | 694 | return Slot.get(); |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 695 | } |
| 696 | |
Benjamin Kramer | 5d2ff22 | 2014-01-18 16:43:06 +0000 | [diff] [blame] | 697 | Constant *ConstantFP::getInfinity(Type *Ty, bool Negative) { |
| 698 | const fltSemantics &Semantics = *TypeToFloatSemantics(Ty->getScalarType()); |
| 699 | Constant *C = get(Ty->getContext(), APFloat::getInf(Semantics, Negative)); |
| 700 | |
| 701 | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) |
| 702 | return ConstantVector::getSplat(VTy->getNumElements(), C); |
| 703 | |
| 704 | return C; |
Dan Gohman | feb5021 | 2009-09-25 23:00:48 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Duncan P. N. Exon Smith | b645279 | 2016-02-21 02:39:49 +0000 | [diff] [blame] | 707 | ConstantFP::ConstantFP(Type *Ty, const APFloat &V) |
| 708 | : ConstantData(Ty, ConstantFPVal), Val(V) { |
Chris Lattner | 98bd939 | 2008-04-09 06:38:30 +0000 | [diff] [blame] | 709 | assert(&V.getSemantics() == TypeToFloatSemantics(Ty) && |
| 710 | "FP type Mismatch"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Chris Lattner | be6610c | 2011-07-15 06:14:08 +0000 | [diff] [blame] | 713 | bool ConstantFP::isExactlyValue(const APFloat &V) const { |
Dale Johannesen | d246b2c | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 714 | return Val.bitwiseIsEqual(V); |
Chris Lattner | c6ee77d | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Pete Cooper | 86dd4cf | 2015-06-23 21:55:11 +0000 | [diff] [blame] | 717 | /// Remove the constant from the constant table. |
| 718 | void ConstantFP::destroyConstantImpl() { |
Craig Topper | ccbb810 | 2017-06-27 19:57:51 +0000 | [diff] [blame] | 719 | llvm_unreachable("You can't ConstantFP->destroyConstantImpl()!"); |
Pete Cooper | 86dd4cf | 2015-06-23 21:55:11 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Chris Lattner | c6ee77d | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 722 | //===----------------------------------------------------------------------===// |
Chris Lattner | 030af79 | 2012-01-24 05:42:11 +0000 | [diff] [blame] | 723 | // ConstantAggregateZero Implementation |
| 724 | //===----------------------------------------------------------------------===// |
| 725 | |
Chris Lattner | 7e683d1 | 2012-01-25 06:16:32 +0000 | [diff] [blame] | 726 | Constant *ConstantAggregateZero::getSequentialElement() const { |
Chris Lattner | 8326bd8 | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 727 | return Constant::getNullValue(getType()->getSequentialElementType()); |
Chris Lattner | 030af79 | 2012-01-24 05:42:11 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Chris Lattner | 7e683d1 | 2012-01-25 06:16:32 +0000 | [diff] [blame] | 730 | Constant *ConstantAggregateZero::getStructElement(unsigned Elt) const { |
Chris Lattner | 8326bd8 | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 731 | return Constant::getNullValue(getType()->getStructElementType(Elt)); |
Chris Lattner | 030af79 | 2012-01-24 05:42:11 +0000 | [diff] [blame] | 732 | } |
| 733 | |
Chris Lattner | 7e683d1 | 2012-01-25 06:16:32 +0000 | [diff] [blame] | 734 | Constant *ConstantAggregateZero::getElementValue(Constant *C) const { |
Chris Lattner | 030af79 | 2012-01-24 05:42:11 +0000 | [diff] [blame] | 735 | if (isa<SequentialType>(getType())) |
| 736 | return getSequentialElement(); |
| 737 | return getStructElement(cast<ConstantInt>(C)->getZExtValue()); |
| 738 | } |
| 739 | |
Chris Lattner | 7e683d1 | 2012-01-25 06:16:32 +0000 | [diff] [blame] | 740 | Constant *ConstantAggregateZero::getElementValue(unsigned Idx) const { |
Chris Lattner | f7eb543 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 741 | if (isa<SequentialType>(getType())) |
| 742 | return getSequentialElement(); |
| 743 | return getStructElement(Idx); |
| 744 | } |
| 745 | |
David Majnemer | 9b529a7 | 2015-02-16 04:02:09 +0000 | [diff] [blame] | 746 | unsigned ConstantAggregateZero::getNumElements() const { |
Craig Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 747 | Type *Ty = getType(); |
| 748 | if (auto *AT = dyn_cast<ArrayType>(Ty)) |
David Majnemer | 9b529a7 | 2015-02-16 04:02:09 +0000 | [diff] [blame] | 749 | return AT->getNumElements(); |
Craig Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 750 | if (auto *VT = dyn_cast<VectorType>(Ty)) |
David Majnemer | 9b529a7 | 2015-02-16 04:02:09 +0000 | [diff] [blame] | 751 | return VT->getNumElements(); |
| 752 | return Ty->getStructNumElements(); |
| 753 | } |
Chris Lattner | f7eb543 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 754 | |
Chris Lattner | 030af79 | 2012-01-24 05:42:11 +0000 | [diff] [blame] | 755 | //===----------------------------------------------------------------------===// |
| 756 | // UndefValue Implementation |
| 757 | //===----------------------------------------------------------------------===// |
| 758 | |
Chris Lattner | 7e683d1 | 2012-01-25 06:16:32 +0000 | [diff] [blame] | 759 | UndefValue *UndefValue::getSequentialElement() const { |
Chris Lattner | 8326bd8 | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 760 | return UndefValue::get(getType()->getSequentialElementType()); |
Chris Lattner | 030af79 | 2012-01-24 05:42:11 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Chris Lattner | 7e683d1 | 2012-01-25 06:16:32 +0000 | [diff] [blame] | 763 | UndefValue *UndefValue::getStructElement(unsigned Elt) const { |
Chris Lattner | 8326bd8 | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 764 | return UndefValue::get(getType()->getStructElementType(Elt)); |
Chris Lattner | 030af79 | 2012-01-24 05:42:11 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Chris Lattner | 7e683d1 | 2012-01-25 06:16:32 +0000 | [diff] [blame] | 767 | UndefValue *UndefValue::getElementValue(Constant *C) const { |
Chris Lattner | 030af79 | 2012-01-24 05:42:11 +0000 | [diff] [blame] | 768 | if (isa<SequentialType>(getType())) |
| 769 | return getSequentialElement(); |
| 770 | return getStructElement(cast<ConstantInt>(C)->getZExtValue()); |
| 771 | } |
| 772 | |
Chris Lattner | 7e683d1 | 2012-01-25 06:16:32 +0000 | [diff] [blame] | 773 | UndefValue *UndefValue::getElementValue(unsigned Idx) const { |
Chris Lattner | f7eb543 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 774 | if (isa<SequentialType>(getType())) |
| 775 | return getSequentialElement(); |
| 776 | return getStructElement(Idx); |
| 777 | } |
| 778 | |
David Majnemer | 9b529a7 | 2015-02-16 04:02:09 +0000 | [diff] [blame] | 779 | unsigned UndefValue::getNumElements() const { |
Craig Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 780 | Type *Ty = getType(); |
Peter Collingbourne | bc07052 | 2016-12-02 03:20:58 +0000 | [diff] [blame] | 781 | if (auto *ST = dyn_cast<SequentialType>(Ty)) |
| 782 | return ST->getNumElements(); |
David Majnemer | 9b529a7 | 2015-02-16 04:02:09 +0000 | [diff] [blame] | 783 | return Ty->getStructNumElements(); |
| 784 | } |
Chris Lattner | 030af79 | 2012-01-24 05:42:11 +0000 | [diff] [blame] | 785 | |
| 786 | //===----------------------------------------------------------------------===// |
Chris Lattner | c6ee77d | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 787 | // ConstantXXX Classes |
| 788 | //===----------------------------------------------------------------------===// |
| 789 | |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 790 | template <typename ItTy, typename EltTy> |
| 791 | static bool rangeOnlyContains(ItTy Start, ItTy End, EltTy Elt) { |
| 792 | for (; Start != End; ++Start) |
| 793 | if (*Start != Elt) |
| 794 | return false; |
| 795 | return true; |
| 796 | } |
Chris Lattner | c6ee77d | 2007-02-20 07:17:17 +0000 | [diff] [blame] | 797 | |
Justin Bogner | 909e1c0 | 2015-12-01 20:20:49 +0000 | [diff] [blame] | 798 | template <typename SequentialTy, typename ElementTy> |
| 799 | static Constant *getIntSequenceIfElementsMatch(ArrayRef<Constant *> V) { |
| 800 | assert(!V.empty() && "Cannot get empty int sequence."); |
| 801 | |
| 802 | SmallVector<ElementTy, 16> Elts; |
| 803 | for (Constant *C : V) |
| 804 | if (auto *CI = dyn_cast<ConstantInt>(C)) |
| 805 | Elts.push_back(CI->getZExtValue()); |
| 806 | else |
| 807 | return nullptr; |
| 808 | return SequentialTy::get(V[0]->getContext(), Elts); |
| 809 | } |
| 810 | |
| 811 | template <typename SequentialTy, typename ElementTy> |
| 812 | static Constant *getFPSequenceIfElementsMatch(ArrayRef<Constant *> V) { |
| 813 | assert(!V.empty() && "Cannot get empty FP sequence."); |
| 814 | |
| 815 | SmallVector<ElementTy, 16> Elts; |
| 816 | for (Constant *C : V) |
| 817 | if (auto *CFP = dyn_cast<ConstantFP>(C)) |
| 818 | Elts.push_back(CFP->getValueAPF().bitcastToAPInt().getLimitedValue()); |
| 819 | else |
| 820 | return nullptr; |
| 821 | return SequentialTy::getFP(V[0]->getContext(), Elts); |
| 822 | } |
| 823 | |
| 824 | template <typename SequenceTy> |
| 825 | static Constant *getSequenceIfElementsMatch(Constant *C, |
| 826 | ArrayRef<Constant *> V) { |
| 827 | // We speculatively build the elements here even if it turns out that there is |
| 828 | // a constantexpr or something else weird, since it is so uncommon for that to |
| 829 | // happen. |
| 830 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) { |
| 831 | if (CI->getType()->isIntegerTy(8)) |
| 832 | return getIntSequenceIfElementsMatch<SequenceTy, uint8_t>(V); |
| 833 | else if (CI->getType()->isIntegerTy(16)) |
| 834 | return getIntSequenceIfElementsMatch<SequenceTy, uint16_t>(V); |
| 835 | else if (CI->getType()->isIntegerTy(32)) |
| 836 | return getIntSequenceIfElementsMatch<SequenceTy, uint32_t>(V); |
| 837 | else if (CI->getType()->isIntegerTy(64)) |
| 838 | return getIntSequenceIfElementsMatch<SequenceTy, uint64_t>(V); |
| 839 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
Justin Bogner | 0ebc860 | 2015-12-08 03:01:16 +0000 | [diff] [blame] | 840 | if (CFP->getType()->isHalfTy()) |
| 841 | return getFPSequenceIfElementsMatch<SequenceTy, uint16_t>(V); |
| 842 | else if (CFP->getType()->isFloatTy()) |
Justin Bogner | 909e1c0 | 2015-12-01 20:20:49 +0000 | [diff] [blame] | 843 | return getFPSequenceIfElementsMatch<SequenceTy, uint32_t>(V); |
| 844 | else if (CFP->getType()->isDoubleTy()) |
| 845 | return getFPSequenceIfElementsMatch<SequenceTy, uint64_t>(V); |
| 846 | } |
| 847 | |
| 848 | return nullptr; |
| 849 | } |
| 850 | |
Duncan P. N. Exon Smith | 1de3c7e | 2016-04-05 21:10:45 +0000 | [diff] [blame] | 851 | ConstantAggregate::ConstantAggregate(CompositeType *T, ValueTy VT, |
| 852 | ArrayRef<Constant *> V) |
| 853 | : Constant(T, VT, OperandTraits<ConstantAggregate>::op_end(this) - V.size(), |
| 854 | V.size()) { |
Jay Foad | 89d9b81 | 2011-07-25 10:14:44 +0000 | [diff] [blame] | 855 | std::copy(V.begin(), V.end(), op_begin()); |
Duncan P. N. Exon Smith | 1de3c7e | 2016-04-05 21:10:45 +0000 | [diff] [blame] | 856 | |
| 857 | // Check that types match, unless this is an opaque struct. |
| 858 | if (auto *ST = dyn_cast<StructType>(T)) |
| 859 | if (ST->isOpaque()) |
| 860 | return; |
| 861 | for (unsigned I = 0, E = V.size(); I != E; ++I) |
| 862 | assert(V[I]->getType() == T->getTypeAtIndex(I) && |
| 863 | "Initializer for composite element doesn't match!"); |
| 864 | } |
| 865 | |
| 866 | ConstantArray::ConstantArray(ArrayType *T, ArrayRef<Constant *> V) |
| 867 | : ConstantAggregate(T, ConstantArrayVal, V) { |
| 868 | assert(V.size() == T->getNumElements() && |
| 869 | "Invalid initializer for constant array"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 870 | } |
| 871 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 872 | Constant *ConstantArray::get(ArrayType *Ty, ArrayRef<Constant*> V) { |
Duncan P. N. Exon Smith | 317c139 | 2014-08-19 16:39:58 +0000 | [diff] [blame] | 873 | if (Constant *C = getImpl(Ty, V)) |
| 874 | return C; |
| 875 | return Ty->getContext().pImpl->ArrayConstants.getOrCreate(Ty, V); |
| 876 | } |
Justin Bogner | 909e1c0 | 2015-12-01 20:20:49 +0000 | [diff] [blame] | 877 | |
Duncan P. N. Exon Smith | 317c139 | 2014-08-19 16:39:58 +0000 | [diff] [blame] | 878 | Constant *ConstantArray::getImpl(ArrayType *Ty, ArrayRef<Constant*> V) { |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 879 | // Empty arrays are canonicalized to ConstantAggregateZero. |
| 880 | if (V.empty()) |
| 881 | return ConstantAggregateZero::get(Ty); |
| 882 | |
Jeffrey Yasskin | 8ce67f8 | 2009-09-30 21:08:08 +0000 | [diff] [blame] | 883 | for (unsigned i = 0, e = V.size(); i != e; ++i) { |
| 884 | assert(V[i]->getType() == Ty->getElementType() && |
| 885 | "Wrong type in array element initializer"); |
| 886 | } |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 887 | |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 888 | // If this is an all-zero array, return a ConstantAggregateZero object. If |
| 889 | // all undef, return an UndefValue, if "all simple", then return a |
| 890 | // ConstantDataArray. |
| 891 | Constant *C = V[0]; |
| 892 | if (isa<UndefValue>(C) && rangeOnlyContains(V.begin(), V.end(), C)) |
| 893 | return UndefValue::get(Ty); |
Chris Lattner | f14a67f | 2012-01-26 02:31:22 +0000 | [diff] [blame] | 894 | |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 895 | if (C->isNullValue() && rangeOnlyContains(V.begin(), V.end(), C)) |
| 896 | return ConstantAggregateZero::get(Ty); |
| 897 | |
| 898 | // Check to see if all of the elements are ConstantFP or ConstantInt and if |
| 899 | // the element type is compatible with ConstantDataVector. If so, use it. |
Justin Bogner | 909e1c0 | 2015-12-01 20:20:49 +0000 | [diff] [blame] | 900 | if (ConstantDataSequential::isElementTypeCompatible(C->getType())) |
| 901 | return getSequenceIfElementsMatch<ConstantDataArray>(C, V); |
Chris Lattner | f14a67f | 2012-01-26 02:31:22 +0000 | [diff] [blame] | 902 | |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 903 | // Otherwise, we really do want to create a ConstantArray. |
Duncan P. N. Exon Smith | 317c139 | 2014-08-19 16:39:58 +0000 | [diff] [blame] | 904 | return nullptr; |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 905 | } |
| 906 | |
Chris Lattner | cc19efa | 2011-06-20 04:01:31 +0000 | [diff] [blame] | 907 | StructType *ConstantStruct::getTypeForElements(LLVMContext &Context, |
| 908 | ArrayRef<Constant*> V, |
| 909 | bool Packed) { |
Bill Wendling | 3ae7dd3 | 2012-02-07 01:27:51 +0000 | [diff] [blame] | 910 | unsigned VecSize = V.size(); |
| 911 | SmallVector<Type*, 16> EltTypes(VecSize); |
| 912 | for (unsigned i = 0; i != VecSize; ++i) |
| 913 | EltTypes[i] = V[i]->getType(); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 914 | |
Chris Lattner | cc19efa | 2011-06-20 04:01:31 +0000 | [diff] [blame] | 915 | return StructType::get(Context, EltTypes, Packed); |
| 916 | } |
| 917 | |
| 918 | |
| 919 | StructType *ConstantStruct::getTypeForElements(ArrayRef<Constant*> V, |
| 920 | bool Packed) { |
| 921 | assert(!V.empty() && |
| 922 | "ConstantStruct::getTypeForElements cannot be called on empty list"); |
| 923 | return getTypeForElements(V[0]->getContext(), V, Packed); |
| 924 | } |
| 925 | |
Jay Foad | 89d9b81 | 2011-07-25 10:14:44 +0000 | [diff] [blame] | 926 | ConstantStruct::ConstantStruct(StructType *T, ArrayRef<Constant *> V) |
Duncan P. N. Exon Smith | 1de3c7e | 2016-04-05 21:10:45 +0000 | [diff] [blame] | 927 | : ConstantAggregate(T, ConstantStructVal, V) { |
| 928 | assert((T->isOpaque() || V.size() == T->getNumElements()) && |
| 929 | "Invalid initializer for constant struct"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 932 | // ConstantStruct accessors. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 933 | Constant *ConstantStruct::get(StructType *ST, ArrayRef<Constant*> V) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 934 | assert((ST->isOpaque() || ST->getNumElements() == V.size()) && |
| 935 | "Incorrect # elements specified to ConstantStruct::get"); |
Chris Lattner | f14a67f | 2012-01-26 02:31:22 +0000 | [diff] [blame] | 936 | |
| 937 | // Create a ConstantAggregateZero value if all elements are zeros. |
| 938 | bool isZero = true; |
| 939 | bool isUndef = false; |
| 940 | |
| 941 | if (!V.empty()) { |
| 942 | isUndef = isa<UndefValue>(V[0]); |
| 943 | isZero = V[0]->isNullValue(); |
| 944 | if (isUndef || isZero) { |
| 945 | for (unsigned i = 0, e = V.size(); i != e; ++i) { |
| 946 | if (!V[i]->isNullValue()) |
| 947 | isZero = false; |
| 948 | if (!isa<UndefValue>(V[i])) |
| 949 | isUndef = false; |
| 950 | } |
| 951 | } |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 952 | } |
Chris Lattner | f14a67f | 2012-01-26 02:31:22 +0000 | [diff] [blame] | 953 | if (isZero) |
| 954 | return ConstantAggregateZero::get(ST); |
| 955 | if (isUndef) |
| 956 | return UndefValue::get(ST); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 957 | |
Chris Lattner | f14a67f | 2012-01-26 02:31:22 +0000 | [diff] [blame] | 958 | return ST->getContext().pImpl->StructConstants.getOrCreate(ST, V); |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 959 | } |
| 960 | |
Jay Foad | 89d9b81 | 2011-07-25 10:14:44 +0000 | [diff] [blame] | 961 | ConstantVector::ConstantVector(VectorType *T, ArrayRef<Constant *> V) |
Duncan P. N. Exon Smith | 1de3c7e | 2016-04-05 21:10:45 +0000 | [diff] [blame] | 962 | : ConstantAggregate(T, ConstantVectorVal, V) { |
Duncan P. N. Exon Smith | db63bda | 2016-04-05 20:53:47 +0000 | [diff] [blame] | 963 | assert(V.size() == T->getNumElements() && |
Duncan P. N. Exon Smith | 1de3c7e | 2016-04-05 21:10:45 +0000 | [diff] [blame] | 964 | "Invalid initializer for constant vector"); |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 965 | } |
| 966 | |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 967 | // ConstantVector accessors. |
Jay Foad | b8a8bed3 | 2011-06-22 09:10:19 +0000 | [diff] [blame] | 968 | Constant *ConstantVector::get(ArrayRef<Constant*> V) { |
Duncan P. N. Exon Smith | 317c139 | 2014-08-19 16:39:58 +0000 | [diff] [blame] | 969 | if (Constant *C = getImpl(V)) |
| 970 | return C; |
| 971 | VectorType *Ty = VectorType::get(V.front()->getType(), V.size()); |
| 972 | return Ty->getContext().pImpl->VectorConstants.getOrCreate(Ty, V); |
| 973 | } |
Justin Bogner | 909e1c0 | 2015-12-01 20:20:49 +0000 | [diff] [blame] | 974 | |
Duncan P. N. Exon Smith | 317c139 | 2014-08-19 16:39:58 +0000 | [diff] [blame] | 975 | Constant *ConstantVector::getImpl(ArrayRef<Constant*> V) { |
Jay Foad | 9f32cfd | 2011-01-27 14:44:55 +0000 | [diff] [blame] | 976 | assert(!V.empty() && "Vectors can't be empty"); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 977 | VectorType *T = VectorType::get(V.front()->getType(), V.size()); |
Jay Foad | 9f32cfd | 2011-01-27 14:44:55 +0000 | [diff] [blame] | 978 | |
Chris Lattner | 6922931 | 2011-02-15 00:14:00 +0000 | [diff] [blame] | 979 | // If this is an all-undef or all-zero vector, return a |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 980 | // ConstantAggregateZero or UndefValue. |
| 981 | Constant *C = V[0]; |
| 982 | bool isZero = C->isNullValue(); |
| 983 | bool isUndef = isa<UndefValue>(C); |
| 984 | |
| 985 | if (isZero || isUndef) { |
| 986 | for (unsigned i = 1, e = V.size(); i != e; ++i) |
| 987 | if (V[i] != C) { |
| 988 | isZero = isUndef = false; |
| 989 | break; |
| 990 | } |
| 991 | } |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 992 | |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 993 | if (isZero) |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 994 | return ConstantAggregateZero::get(T); |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 995 | if (isUndef) |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 996 | return UndefValue::get(T); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 997 | |
Chris Lattner | 978fe0c | 2012-01-30 06:21:21 +0000 | [diff] [blame] | 998 | // Check to see if all of the elements are ConstantFP or ConstantInt and if |
| 999 | // the element type is compatible with ConstantDataVector. If so, use it. |
Justin Bogner | 909e1c0 | 2015-12-01 20:20:49 +0000 | [diff] [blame] | 1000 | if (ConstantDataSequential::isElementTypeCompatible(C->getType())) |
| 1001 | return getSequenceIfElementsMatch<ConstantDataVector>(C, V); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1002 | |
Chris Lattner | 978fe0c | 2012-01-30 06:21:21 +0000 | [diff] [blame] | 1003 | // Otherwise, the element type isn't compatible with ConstantDataVector, or |
Craig Topper | df90726 | 2017-04-11 06:41:55 +0000 | [diff] [blame] | 1004 | // the operand list contains a ConstantExpr or something else strange. |
Duncan P. N. Exon Smith | 317c139 | 2014-08-19 16:39:58 +0000 | [diff] [blame] | 1005 | return nullptr; |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
Chris Lattner | e9eed29 | 2012-01-25 05:19:54 +0000 | [diff] [blame] | 1008 | Constant *ConstantVector::getSplat(unsigned NumElts, Constant *V) { |
Chris Lattner | 978fe0c | 2012-01-30 06:21:21 +0000 | [diff] [blame] | 1009 | // If this splat is compatible with ConstantDataVector, use it instead of |
| 1010 | // ConstantVector. |
| 1011 | if ((isa<ConstantFP>(V) || isa<ConstantInt>(V)) && |
| 1012 | ConstantDataSequential::isElementTypeCompatible(V->getType())) |
| 1013 | return ConstantDataVector::getSplat(NumElts, V); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1014 | |
Chris Lattner | e9eed29 | 2012-01-25 05:19:54 +0000 | [diff] [blame] | 1015 | SmallVector<Constant*, 32> Elts(NumElts, V); |
| 1016 | return get(Elts); |
| 1017 | } |
| 1018 | |
David Majnemer | f0f224d | 2015-11-11 21:57:16 +0000 | [diff] [blame] | 1019 | ConstantTokenNone *ConstantTokenNone::get(LLVMContext &Context) { |
| 1020 | LLVMContextImpl *pImpl = Context.pImpl; |
| 1021 | if (!pImpl->TheNoneToken) |
David Majnemer | 2dd41c5 | 2015-11-16 20:55:57 +0000 | [diff] [blame] | 1022 | pImpl->TheNoneToken.reset(new ConstantTokenNone(Context)); |
| 1023 | return pImpl->TheNoneToken.get(); |
David Majnemer | f0f224d | 2015-11-11 21:57:16 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | /// Remove the constant from the constant table. |
| 1027 | void ConstantTokenNone::destroyConstantImpl() { |
| 1028 | llvm_unreachable("You can't ConstantTokenNone->destroyConstantImpl()!"); |
| 1029 | } |
Chris Lattner | e9eed29 | 2012-01-25 05:19:54 +0000 | [diff] [blame] | 1030 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1031 | // Utility function for determining if a ConstantExpr is a CastOp or not. This |
| 1032 | // can't be inline because we don't want to #include Instruction.h into |
| 1033 | // Constant.h |
| 1034 | bool ConstantExpr::isCast() const { |
| 1035 | return Instruction::isCast(getOpcode()); |
| 1036 | } |
| 1037 | |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1038 | bool ConstantExpr::isCompare() const { |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 1039 | return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp; |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
Dan Gohman | 7190d48 | 2009-09-10 23:37:55 +0000 | [diff] [blame] | 1042 | bool ConstantExpr::isGEPWithNoNotionalOverIndexing() const { |
| 1043 | if (getOpcode() != Instruction::GetElementPtr) return false; |
| 1044 | |
| 1045 | gep_type_iterator GEPI = gep_type_begin(this), E = gep_type_end(this); |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1046 | User::const_op_iterator OI = std::next(this->op_begin()); |
Dan Gohman | 7190d48 | 2009-09-10 23:37:55 +0000 | [diff] [blame] | 1047 | |
Sanjay Patel | 81ed349 | 2016-12-11 20:07:02 +0000 | [diff] [blame] | 1048 | // The remaining indices may be compile-time known integers within the bounds |
| 1049 | // of the corresponding notional static array types. |
Dan Gohman | 7190d48 | 2009-09-10 23:37:55 +0000 | [diff] [blame] | 1050 | for (; GEPI != E; ++GEPI, ++OI) { |
Sanjay Patel | 81ed349 | 2016-12-11 20:07:02 +0000 | [diff] [blame] | 1051 | if (isa<UndefValue>(*OI)) |
| 1052 | continue; |
| 1053 | auto *CI = dyn_cast<ConstantInt>(*OI); |
| 1054 | if (!CI || (GEPI.isBoundedSequential() && |
| 1055 | (CI->getValue().getActiveBits() > 64 || |
| 1056 | CI->getZExtValue() >= GEPI.getSequentialNumElements()))) |
Peter Collingbourne | ab85225b | 2016-12-02 02:24:42 +0000 | [diff] [blame] | 1057 | return false; |
Dan Gohman | 7190d48 | 2009-09-10 23:37:55 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | // All the indices checked out. |
| 1061 | return true; |
| 1062 | } |
| 1063 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1064 | bool ConstantExpr::hasIndices() const { |
| 1065 | return getOpcode() == Instruction::ExtractValue || |
| 1066 | getOpcode() == Instruction::InsertValue; |
| 1067 | } |
| 1068 | |
Jay Foad | 0091fe8 | 2011-04-13 15:22:40 +0000 | [diff] [blame] | 1069 | ArrayRef<unsigned> ConstantExpr::getIndices() const { |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1070 | if (const ExtractValueConstantExpr *EVCE = |
| 1071 | dyn_cast<ExtractValueConstantExpr>(this)) |
| 1072 | return EVCE->Indices; |
Dan Gohman | a469bdb | 2008-06-23 16:39:44 +0000 | [diff] [blame] | 1073 | |
| 1074 | return cast<InsertValueConstantExpr>(this)->Indices; |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
Reid Spencer | 10fbf0e | 2006-12-03 05:48:19 +0000 | [diff] [blame] | 1077 | unsigned ConstantExpr::getPredicate() const { |
Craig Topper | cc03b49 | 2015-12-15 06:11:36 +0000 | [diff] [blame] | 1078 | return cast<CompareConstantExpr>(this)->predicate; |
Reid Spencer | 10fbf0e | 2006-12-03 05:48:19 +0000 | [diff] [blame] | 1079 | } |
Chris Lattner | 60e0dd7 | 2001-10-03 06:12:09 +0000 | [diff] [blame] | 1080 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1081 | Constant * |
| 1082 | ConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const { |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 1083 | assert(Op->getType() == getOperand(OpNo)->getType() && |
| 1084 | "Replacing operand with value of different type!"); |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 1085 | if (getOperand(OpNo) == Op) |
| 1086 | return const_cast<ConstantExpr*>(this); |
Chris Lattner | 37e3835 | 2012-01-26 20:37:11 +0000 | [diff] [blame] | 1087 | |
| 1088 | SmallVector<Constant*, 8> NewOps; |
| 1089 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 1090 | NewOps.push_back(i == OpNo ? Op : getOperand(i)); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1091 | |
Chris Lattner | 37e3835 | 2012-01-26 20:37:11 +0000 | [diff] [blame] | 1092 | return getWithOperands(NewOps); |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1095 | Constant *ConstantExpr::getWithOperands(ArrayRef<Constant *> Ops, Type *Ty, |
David Blaikie | 8820884 | 2015-08-21 20:16:51 +0000 | [diff] [blame] | 1096 | bool OnlyIfReduced, Type *SrcTy) const { |
Jay Foad | 5c984e56 | 2011-04-13 13:46:01 +0000 | [diff] [blame] | 1097 | assert(Ops.size() == getNumOperands() && "Operand count mismatch!"); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1098 | |
Benjamin Kramer | 8008e9f | 2015-03-02 11:57:04 +0000 | [diff] [blame] | 1099 | // If no operands changed return self. |
| 1100 | if (Ty == getType() && std::equal(Ops.begin(), Ops.end(), op_begin())) |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 1101 | return const_cast<ConstantExpr*>(this); |
| 1102 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1103 | Type *OnlyIfReducedTy = OnlyIfReduced ? Ty : nullptr; |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 1104 | switch (getOpcode()) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1105 | case Instruction::Trunc: |
| 1106 | case Instruction::ZExt: |
| 1107 | case Instruction::SExt: |
| 1108 | case Instruction::FPTrunc: |
| 1109 | case Instruction::FPExt: |
| 1110 | case Instruction::UIToFP: |
| 1111 | case Instruction::SIToFP: |
| 1112 | case Instruction::FPToUI: |
| 1113 | case Instruction::FPToSI: |
| 1114 | case Instruction::PtrToInt: |
| 1115 | case Instruction::IntToPtr: |
| 1116 | case Instruction::BitCast: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 1117 | case Instruction::AddrSpaceCast: |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1118 | return ConstantExpr::getCast(getOpcode(), Ops[0], Ty, OnlyIfReduced); |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 1119 | case Instruction::Select: |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1120 | return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2], OnlyIfReducedTy); |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 1121 | case Instruction::InsertElement: |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1122 | return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2], |
| 1123 | OnlyIfReducedTy); |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 1124 | case Instruction::ExtractElement: |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1125 | return ConstantExpr::getExtractElement(Ops[0], Ops[1], OnlyIfReducedTy); |
Chris Lattner | 37e3835 | 2012-01-26 20:37:11 +0000 | [diff] [blame] | 1126 | case Instruction::InsertValue: |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1127 | return ConstantExpr::getInsertValue(Ops[0], Ops[1], getIndices(), |
| 1128 | OnlyIfReducedTy); |
Chris Lattner | 37e3835 | 2012-01-26 20:37:11 +0000 | [diff] [blame] | 1129 | case Instruction::ExtractValue: |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1130 | return ConstantExpr::getExtractValue(Ops[0], getIndices(), OnlyIfReducedTy); |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 1131 | case Instruction::ShuffleVector: |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1132 | return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2], |
| 1133 | OnlyIfReducedTy); |
David Blaikie | 8820884 | 2015-08-21 20:16:51 +0000 | [diff] [blame] | 1134 | case Instruction::GetElementPtr: { |
| 1135 | auto *GEPO = cast<GEPOperator>(this); |
| 1136 | assert(SrcTy || (Ops[0]->getType() == getOperand(0)->getType())); |
| 1137 | return ConstantExpr::getGetElementPtr( |
| 1138 | SrcTy ? SrcTy : GEPO->getSourceElementType(), Ops[0], Ops.slice(1), |
Peter Collingbourne | d93620b | 2016-11-10 22:34:55 +0000 | [diff] [blame] | 1139 | GEPO->isInBounds(), GEPO->getInRangeIndex(), OnlyIfReducedTy); |
David Blaikie | 8820884 | 2015-08-21 20:16:51 +0000 | [diff] [blame] | 1140 | } |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1141 | case Instruction::ICmp: |
| 1142 | case Instruction::FCmp: |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1143 | return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1], |
| 1144 | OnlyIfReducedTy); |
Chris Lattner | 22781634 | 2006-07-14 22:20:01 +0000 | [diff] [blame] | 1145 | default: |
| 1146 | assert(getNumOperands() == 2 && "Must be binary operator?"); |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1147 | return ConstantExpr::get(getOpcode(), Ops[0], Ops[1], SubclassOptionalData, |
| 1148 | OnlyIfReducedTy); |
Chris Lattner | 7c1018a | 2006-07-14 19:37:40 +0000 | [diff] [blame] | 1149 | } |
| 1150 | } |
| 1151 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1152 | |
| 1153 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1154 | // isValueValidForType implementations |
| 1155 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1156 | bool ConstantInt::isValueValidForType(Type *Ty, uint64_t Val) { |
Chris Lattner | 8326bd8 | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 1157 | unsigned NumBits = Ty->getIntegerBitWidth(); // assert okay |
| 1158 | if (Ty->isIntegerTy(1)) |
Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1159 | return Val == 0 || Val == 1; |
Craig Topper | 50b1e51 | 2017-06-07 00:58:05 +0000 | [diff] [blame] | 1160 | return isUIntN(NumBits, Val); |
Reid Spencer | e733472 | 2006-12-19 01:28:19 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1163 | bool ConstantInt::isValueValidForType(Type *Ty, int64_t Val) { |
Chris Lattner | 8326bd8 | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 1164 | unsigned NumBits = Ty->getIntegerBitWidth(); |
| 1165 | if (Ty->isIntegerTy(1)) |
Reid Spencer | a94d394 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 1166 | return Val == 0 || Val == 1 || Val == -1; |
Craig Topper | 50b1e51 | 2017-06-07 00:58:05 +0000 | [diff] [blame] | 1167 | return isIntN(NumBits, Val); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1170 | bool ConstantFP::isValueValidForType(Type *Ty, const APFloat& Val) { |
Dale Johannesen | d246b2c | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 1171 | // convert modifies in place, so make a copy. |
| 1172 | APFloat Val2 = APFloat(Val); |
Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 1173 | bool losesInfo; |
Chris Lattner | 6b72759 | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 1174 | switch (Ty->getTypeID()) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1175 | default: |
| 1176 | return false; // These can't be represented as floating point! |
| 1177 | |
Dale Johannesen | d246b2c | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 1178 | // FIXME rounding mode needs to be more flexible |
Dan Gohman | 518cda4 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 1179 | case Type::HalfTyID: { |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 1180 | if (&Val2.getSemantics() == &APFloat::IEEEhalf()) |
Dan Gohman | 518cda4 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 1181 | return true; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 1182 | Val2.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &losesInfo); |
Dan Gohman | 518cda4 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 1183 | return !losesInfo; |
| 1184 | } |
Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 1185 | case Type::FloatTyID: { |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 1186 | if (&Val2.getSemantics() == &APFloat::IEEEsingle()) |
Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 1187 | return true; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 1188 | Val2.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &losesInfo); |
Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 1189 | return !losesInfo; |
| 1190 | } |
| 1191 | case Type::DoubleTyID: { |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 1192 | if (&Val2.getSemantics() == &APFloat::IEEEhalf() || |
| 1193 | &Val2.getSemantics() == &APFloat::IEEEsingle() || |
| 1194 | &Val2.getSemantics() == &APFloat::IEEEdouble()) |
Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 1195 | return true; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 1196 | Val2.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &losesInfo); |
Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 1197 | return !losesInfo; |
| 1198 | } |
Dale Johannesen | bdad809 | 2007-08-09 22:51:36 +0000 | [diff] [blame] | 1199 | case Type::X86_FP80TyID: |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 1200 | return &Val2.getSemantics() == &APFloat::IEEEhalf() || |
| 1201 | &Val2.getSemantics() == &APFloat::IEEEsingle() || |
| 1202 | &Val2.getSemantics() == &APFloat::IEEEdouble() || |
| 1203 | &Val2.getSemantics() == &APFloat::x87DoubleExtended(); |
Dale Johannesen | bdad809 | 2007-08-09 22:51:36 +0000 | [diff] [blame] | 1204 | case Type::FP128TyID: |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 1205 | return &Val2.getSemantics() == &APFloat::IEEEhalf() || |
| 1206 | &Val2.getSemantics() == &APFloat::IEEEsingle() || |
| 1207 | &Val2.getSemantics() == &APFloat::IEEEdouble() || |
| 1208 | &Val2.getSemantics() == &APFloat::IEEEquad(); |
Dale Johannesen | 007aa37 | 2007-10-11 18:07:22 +0000 | [diff] [blame] | 1209 | case Type::PPC_FP128TyID: |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 1210 | return &Val2.getSemantics() == &APFloat::IEEEhalf() || |
| 1211 | &Val2.getSemantics() == &APFloat::IEEEsingle() || |
| 1212 | &Val2.getSemantics() == &APFloat::IEEEdouble() || |
| 1213 | &Val2.getSemantics() == &APFloat::PPCDoubleDouble(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1214 | } |
Chris Lattner | aa237256 | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 1215 | } |
Chris Lattner | 9655e54 | 2001-07-20 19:16:02 +0000 | [diff] [blame] | 1216 | |
Chris Lattner | 030af79 | 2012-01-24 05:42:11 +0000 | [diff] [blame] | 1217 | |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 1218 | //===----------------------------------------------------------------------===// |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 1219 | // Factory Function Implementation |
| 1220 | |
Chris Lattner | c7f9fd4 | 2012-01-23 15:20:12 +0000 | [diff] [blame] | 1221 | ConstantAggregateZero *ConstantAggregateZero::get(Type *Ty) { |
Chris Lattner | 13ee795 | 2010-08-28 04:09:24 +0000 | [diff] [blame] | 1222 | assert((Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy()) && |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 1223 | "Cannot create an aggregate zero of non-aggregate type!"); |
David Blaikie | 899b85a | 2014-11-25 02:13:54 +0000 | [diff] [blame] | 1224 | |
Justin Lebar | 611c5c2 | 2016-10-10 16:26:13 +0000 | [diff] [blame] | 1225 | std::unique_ptr<ConstantAggregateZero> &Entry = |
| 1226 | Ty->getContext().pImpl->CAZConstants[Ty]; |
| 1227 | if (!Entry) |
| 1228 | Entry.reset(new ConstantAggregateZero(Ty)); |
| 1229 | |
| 1230 | return Entry.get(); |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 1231 | } |
| 1232 | |
Sanjay Patel | 1d0ac7c | 2016-04-29 22:03:27 +0000 | [diff] [blame] | 1233 | /// Remove the constant from the constant table. |
Pete Cooper | 86dd4cf | 2015-06-23 21:55:11 +0000 | [diff] [blame] | 1234 | void ConstantAggregateZero::destroyConstantImpl() { |
Chris Lattner | c7f9fd4 | 2012-01-23 15:20:12 +0000 | [diff] [blame] | 1235 | getContext().pImpl->CAZConstants.erase(getType()); |
Chris Lattner | 9fba3da | 2004-02-15 05:53:04 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
Sanjay Patel | 1d0ac7c | 2016-04-29 22:03:27 +0000 | [diff] [blame] | 1238 | /// Remove the constant from the constant table. |
Pete Cooper | 86dd4cf | 2015-06-23 21:55:11 +0000 | [diff] [blame] | 1239 | void ConstantArray::destroyConstantImpl() { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1240 | getType()->getContext().pImpl->ArrayConstants.remove(this); |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
Chris Lattner | 81fabb0 | 2002-08-26 17:53:56 +0000 | [diff] [blame] | 1243 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 1244 | //---- ConstantStruct::get() implementation... |
Chris Lattner | 49d855c | 2001-09-07 16:46:31 +0000 | [diff] [blame] | 1245 | // |
Chris Lattner | b50d135 | 2003-10-05 00:17:43 +0000 | [diff] [blame] | 1246 | |
Sanjay Patel | 1d0ac7c | 2016-04-29 22:03:27 +0000 | [diff] [blame] | 1247 | /// Remove the constant from the constant table. |
Pete Cooper | 86dd4cf | 2015-06-23 21:55:11 +0000 | [diff] [blame] | 1248 | void ConstantStruct::destroyConstantImpl() { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1249 | getType()->getContext().pImpl->StructConstants.remove(this); |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 1250 | } |
Chris Lattner | 883ad0b | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 1251 | |
Sanjay Patel | 1d0ac7c | 2016-04-29 22:03:27 +0000 | [diff] [blame] | 1252 | /// Remove the constant from the constant table. |
Pete Cooper | 86dd4cf | 2015-06-23 21:55:11 +0000 | [diff] [blame] | 1253 | void ConstantVector::destroyConstantImpl() { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1254 | getType()->getContext().pImpl->VectorConstants.remove(this); |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
Duncan Sands | e6beec6 | 2012-11-13 12:59:33 +0000 | [diff] [blame] | 1257 | Constant *Constant::getSplatValue() const { |
| 1258 | assert(this->getType()->isVectorTy() && "Only valid for vectors!"); |
| 1259 | if (isa<ConstantAggregateZero>(this)) |
| 1260 | return getNullValue(this->getType()->getVectorElementType()); |
| 1261 | if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this)) |
| 1262 | return CV->getSplatValue(); |
| 1263 | if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) |
| 1264 | return CV->getSplatValue(); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1265 | return nullptr; |
Duncan Sands | e6beec6 | 2012-11-13 12:59:33 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
Duncan Sands | cf0ff03 | 2011-02-01 08:39:12 +0000 | [diff] [blame] | 1268 | Constant *ConstantVector::getSplatValue() const { |
Dan Gohman | 0715920 | 2007-10-17 17:51:30 +0000 | [diff] [blame] | 1269 | // Check out first element. |
| 1270 | Constant *Elt = getOperand(0); |
| 1271 | // Then make sure all remaining elements point to the same value. |
| 1272 | for (unsigned I = 1, E = getNumOperands(); I < E; ++I) |
Chris Lattner | d04e32d | 2011-07-17 06:01:30 +0000 | [diff] [blame] | 1273 | if (getOperand(I) != Elt) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1274 | return nullptr; |
Dan Gohman | 0715920 | 2007-10-17 17:51:30 +0000 | [diff] [blame] | 1275 | return Elt; |
| 1276 | } |
| 1277 | |
Duncan Sands | e6beec6 | 2012-11-13 12:59:33 +0000 | [diff] [blame] | 1278 | const APInt &Constant::getUniqueInteger() const { |
| 1279 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(this)) |
| 1280 | return CI->getValue(); |
| 1281 | assert(this->getSplatValue() && "Doesn't contain a unique integer!"); |
| 1282 | const Constant *C = this->getAggregateElement(0U); |
| 1283 | assert(C && isa<ConstantInt>(C) && "Not a vector of numbers!"); |
| 1284 | return cast<ConstantInt>(C)->getValue(); |
| 1285 | } |
| 1286 | |
Chris Lattner | 31b132c | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1287 | //---- ConstantPointerNull::get() implementation. |
Chris Lattner | d7a7330 | 2001-10-13 06:57:33 +0000 | [diff] [blame] | 1288 | // |
Chris Lattner | 98fa07b | 2003-05-23 20:03:32 +0000 | [diff] [blame] | 1289 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1290 | ConstantPointerNull *ConstantPointerNull::get(PointerType *Ty) { |
Justin Lebar | 611c5c2 | 2016-10-10 16:26:13 +0000 | [diff] [blame] | 1291 | std::unique_ptr<ConstantPointerNull> &Entry = |
| 1292 | Ty->getContext().pImpl->CPNConstants[Ty]; |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1293 | if (!Entry) |
Justin Lebar | 611c5c2 | 2016-10-10 16:26:13 +0000 | [diff] [blame] | 1294 | Entry.reset(new ConstantPointerNull(Ty)); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1295 | |
Justin Lebar | 611c5c2 | 2016-10-10 16:26:13 +0000 | [diff] [blame] | 1296 | return Entry.get(); |
Chris Lattner | 883ad0b | 2001-10-03 15:39:36 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
Sanjay Patel | 1d0ac7c | 2016-04-29 22:03:27 +0000 | [diff] [blame] | 1299 | /// Remove the constant from the constant table. |
Pete Cooper | 86dd4cf | 2015-06-23 21:55:11 +0000 | [diff] [blame] | 1300 | void ConstantPointerNull::destroyConstantImpl() { |
Chris Lattner | c7f9fd4 | 2012-01-23 15:20:12 +0000 | [diff] [blame] | 1301 | getContext().pImpl->CPNConstants.erase(getType()); |
Chris Lattner | 0c6e0b9 | 2002-08-18 00:40:04 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1304 | UndefValue *UndefValue::get(Type *Ty) { |
Justin Lebar | 611c5c2 | 2016-10-10 16:26:13 +0000 | [diff] [blame] | 1305 | std::unique_ptr<UndefValue> &Entry = Ty->getContext().pImpl->UVConstants[Ty]; |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1306 | if (!Entry) |
Justin Lebar | 611c5c2 | 2016-10-10 16:26:13 +0000 | [diff] [blame] | 1307 | Entry.reset(new UndefValue(Ty)); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1308 | |
Justin Lebar | 611c5c2 | 2016-10-10 16:26:13 +0000 | [diff] [blame] | 1309 | return Entry.get(); |
Chris Lattner | d5f67d8 | 2004-10-16 18:07:16 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
Sanjay Patel | 1d0ac7c | 2016-04-29 22:03:27 +0000 | [diff] [blame] | 1312 | /// Remove the constant from the constant table. |
Pete Cooper | 86dd4cf | 2015-06-23 21:55:11 +0000 | [diff] [blame] | 1313 | void UndefValue::destroyConstantImpl() { |
Chris Lattner | c7f9fd4 | 2012-01-23 15:20:12 +0000 | [diff] [blame] | 1314 | // Free the constant and any dangling references to it. |
| 1315 | getContext().pImpl->UVConstants.erase(getType()); |
Chris Lattner | d5f67d8 | 2004-10-16 18:07:16 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
Chris Lattner | 31b132c | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1318 | BlockAddress *BlockAddress::get(BasicBlock *BB) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1319 | assert(BB->getParent() && "Block must have a parent"); |
Chris Lattner | 31b132c | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1320 | return get(BB->getParent(), BB); |
| 1321 | } |
| 1322 | |
| 1323 | BlockAddress *BlockAddress::get(Function *F, BasicBlock *BB) { |
| 1324 | BlockAddress *&BA = |
| 1325 | F->getContext().pImpl->BlockAddresses[std::make_pair(F, BB)]; |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1326 | if (!BA) |
Chris Lattner | 31b132c | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1327 | BA = new BlockAddress(F, BB); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1328 | |
Chris Lattner | 31b132c | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1329 | assert(BA->getFunction() == F && "Basic block moved between functions"); |
| 1330 | return BA; |
| 1331 | } |
| 1332 | |
| 1333 | BlockAddress::BlockAddress(Function *F, BasicBlock *BB) |
| 1334 | : Constant(Type::getInt8PtrTy(F->getContext()), Value::BlockAddressVal, |
| 1335 | &Op<0>(), 2) { |
Chris Lattner | c559a9f | 2009-11-01 03:03:03 +0000 | [diff] [blame] | 1336 | setOperand(0, F); |
| 1337 | setOperand(1, BB); |
Chris Lattner | aa99c94 | 2009-11-01 01:27:45 +0000 | [diff] [blame] | 1338 | BB->AdjustBlockAddressRefCount(1); |
Chris Lattner | 31b132c | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
Chandler Carruth | 6a93692 | 2014-01-19 02:13:50 +0000 | [diff] [blame] | 1341 | BlockAddress *BlockAddress::lookup(const BasicBlock *BB) { |
| 1342 | if (!BB->hasAddressTaken()) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1343 | return nullptr; |
Chandler Carruth | 6a93692 | 2014-01-19 02:13:50 +0000 | [diff] [blame] | 1344 | |
| 1345 | const Function *F = BB->getParent(); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1346 | assert(F && "Block must have a parent"); |
Chandler Carruth | 6a93692 | 2014-01-19 02:13:50 +0000 | [diff] [blame] | 1347 | BlockAddress *BA = |
| 1348 | F->getContext().pImpl->BlockAddresses.lookup(std::make_pair(F, BB)); |
| 1349 | assert(BA && "Refcount and block address map disagree!"); |
| 1350 | return BA; |
| 1351 | } |
Chris Lattner | 31b132c | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1352 | |
Sanjay Patel | 1d0ac7c | 2016-04-29 22:03:27 +0000 | [diff] [blame] | 1353 | /// Remove the constant from the constant table. |
Pete Cooper | 86dd4cf | 2015-06-23 21:55:11 +0000 | [diff] [blame] | 1354 | void BlockAddress::destroyConstantImpl() { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1355 | getFunction()->getType()->getContext().pImpl |
Chris Lattner | 31b132c | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1356 | ->BlockAddresses.erase(std::make_pair(getFunction(), getBasicBlock())); |
Chris Lattner | aa99c94 | 2009-11-01 01:27:45 +0000 | [diff] [blame] | 1357 | getBasicBlock()->AdjustBlockAddressRefCount(-1); |
Chris Lattner | 31b132c | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
Mehdi Amini | 8914d29 | 2016-02-10 22:47:15 +0000 | [diff] [blame] | 1360 | Value *BlockAddress::handleOperandChangeImpl(Value *From, Value *To) { |
Chris Lattner | 31b132c | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1361 | // This could be replacing either the Basic Block or the Function. In either |
| 1362 | // case, we have to remove the map entry. |
| 1363 | Function *NewF = getFunction(); |
| 1364 | BasicBlock *NewBB = getBasicBlock(); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1365 | |
Mehdi Amini | 8914d29 | 2016-02-10 22:47:15 +0000 | [diff] [blame] | 1366 | if (From == NewF) |
Derek Schuff | ec9dc01 | 2013-06-13 19:51:17 +0000 | [diff] [blame] | 1367 | NewF = cast<Function>(To->stripPointerCasts()); |
Mehdi Amini | 8914d29 | 2016-02-10 22:47:15 +0000 | [diff] [blame] | 1368 | else { |
| 1369 | assert(From == NewBB && "From does not match any operand"); |
Chris Lattner | 31b132c | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1370 | NewBB = cast<BasicBlock>(To); |
Mehdi Amini | 8914d29 | 2016-02-10 22:47:15 +0000 | [diff] [blame] | 1371 | } |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1372 | |
Chris Lattner | 31b132c | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1373 | // See if the 'new' entry already exists, if not, just update this in place |
| 1374 | // and return early. |
| 1375 | BlockAddress *&NewBA = |
| 1376 | getContext().pImpl->BlockAddresses[std::make_pair(NewF, NewBB)]; |
Pete Cooper | 5815b1f | 2015-06-24 18:55:24 +0000 | [diff] [blame] | 1377 | if (NewBA) |
| 1378 | return NewBA; |
Chris Lattner | 31b132c | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1379 | |
Duncan P. N. Exon Smith | 317c139 | 2014-08-19 16:39:58 +0000 | [diff] [blame] | 1380 | getBasicBlock()->AdjustBlockAddressRefCount(-1); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1381 | |
Duncan P. N. Exon Smith | 317c139 | 2014-08-19 16:39:58 +0000 | [diff] [blame] | 1382 | // Remove the old entry, this can't cause the map to rehash (just a |
| 1383 | // tombstone will get added). |
| 1384 | getContext().pImpl->BlockAddresses.erase(std::make_pair(getFunction(), |
| 1385 | getBasicBlock())); |
| 1386 | NewBA = this; |
| 1387 | setOperand(0, NewF); |
| 1388 | setOperand(1, NewBB); |
| 1389 | getBasicBlock()->AdjustBlockAddressRefCount(1); |
Pete Cooper | 5815b1f | 2015-06-24 18:55:24 +0000 | [diff] [blame] | 1390 | |
| 1391 | // If we just want to keep the existing value, then return null. |
| 1392 | // Callers know that this means we shouldn't delete this value. |
| 1393 | return nullptr; |
Chris Lattner | 31b132c | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
| 1396 | //---- ConstantExpr::get() implementations. |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1397 | // |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1398 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1399 | /// This is a utility function to handle folding of casts and lookup of the |
Duncan Sands | 7d6c8ae | 2008-03-30 19:38:55 +0000 | [diff] [blame] | 1400 | /// cast in the ExprConstants map. It is used by the various get* methods below. |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1401 | static Constant *getFoldedCast(Instruction::CastOps opc, Constant *C, Type *Ty, |
| 1402 | bool OnlyIfReduced = false) { |
Chris Lattner | 815ae2b | 2003-10-07 22:19:19 +0000 | [diff] [blame] | 1403 | assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1404 | // Fold a few common cases |
Chris Lattner | f5edeeb | 2010-02-01 20:48:08 +0000 | [diff] [blame] | 1405 | if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty)) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1406 | return FC; |
Chris Lattner | acdbe71 | 2003-04-17 19:24:48 +0000 | [diff] [blame] | 1407 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1408 | if (OnlyIfReduced) |
| 1409 | return nullptr; |
| 1410 | |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1411 | LLVMContextImpl *pImpl = Ty->getContext().pImpl; |
| 1412 | |
Nadav Rotem | 8833043 | 2013-03-07 01:30:40 +0000 | [diff] [blame] | 1413 | // Look up the constant in the table first to ensure uniqueness. |
Duncan P. N. Exon Smith | 317c139 | 2014-08-19 16:39:58 +0000 | [diff] [blame] | 1414 | ConstantExprKeyType Key(opc, C); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1415 | |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1416 | return pImpl->ExprConstants.getOrCreate(Ty, Key); |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 1417 | } |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1418 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1419 | Constant *ConstantExpr::getCast(unsigned oc, Constant *C, Type *Ty, |
| 1420 | bool OnlyIfReduced) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1421 | Instruction::CastOps opc = Instruction::CastOps(oc); |
| 1422 | assert(Instruction::isCast(opc) && "opcode out of range"); |
| 1423 | assert(C && Ty && "Null arguments to getCast"); |
Chris Lattner | 37bc78a | 2010-01-26 21:51:43 +0000 | [diff] [blame] | 1424 | assert(CastInst::castIsValid(opc, C, Ty) && "Invalid constantexpr cast!"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1425 | |
| 1426 | switch (opc) { |
Chris Lattner | 37bc78a | 2010-01-26 21:51:43 +0000 | [diff] [blame] | 1427 | default: |
| 1428 | llvm_unreachable("Invalid cast opcode"); |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1429 | case Instruction::Trunc: |
| 1430 | return getTrunc(C, Ty, OnlyIfReduced); |
| 1431 | case Instruction::ZExt: |
| 1432 | return getZExt(C, Ty, OnlyIfReduced); |
| 1433 | case Instruction::SExt: |
| 1434 | return getSExt(C, Ty, OnlyIfReduced); |
| 1435 | case Instruction::FPTrunc: |
| 1436 | return getFPTrunc(C, Ty, OnlyIfReduced); |
| 1437 | case Instruction::FPExt: |
| 1438 | return getFPExtend(C, Ty, OnlyIfReduced); |
| 1439 | case Instruction::UIToFP: |
| 1440 | return getUIToFP(C, Ty, OnlyIfReduced); |
| 1441 | case Instruction::SIToFP: |
| 1442 | return getSIToFP(C, Ty, OnlyIfReduced); |
| 1443 | case Instruction::FPToUI: |
| 1444 | return getFPToUI(C, Ty, OnlyIfReduced); |
| 1445 | case Instruction::FPToSI: |
| 1446 | return getFPToSI(C, Ty, OnlyIfReduced); |
| 1447 | case Instruction::PtrToInt: |
| 1448 | return getPtrToInt(C, Ty, OnlyIfReduced); |
| 1449 | case Instruction::IntToPtr: |
| 1450 | return getIntToPtr(C, Ty, OnlyIfReduced); |
| 1451 | case Instruction::BitCast: |
| 1452 | return getBitCast(C, Ty, OnlyIfReduced); |
| 1453 | case Instruction::AddrSpaceCast: |
| 1454 | return getAddrSpaceCast(C, Ty, OnlyIfReduced); |
Chris Lattner | 1ece6f8 | 2005-01-01 15:59:57 +0000 | [diff] [blame] | 1455 | } |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1456 | } |
Reid Spencer | f37dc65 | 2006-12-05 19:14:13 +0000 | [diff] [blame] | 1457 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1458 | Constant *ConstantExpr::getZExtOrBitCast(Constant *C, Type *Ty) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1459 | if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Dan Gohman | 3cdcc3f | 2010-04-12 22:12:29 +0000 | [diff] [blame] | 1460 | return getBitCast(C, Ty); |
| 1461 | return getZExt(C, Ty); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1464 | Constant *ConstantExpr::getSExtOrBitCast(Constant *C, Type *Ty) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1465 | if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Dan Gohman | 3cdcc3f | 2010-04-12 22:12:29 +0000 | [diff] [blame] | 1466 | return getBitCast(C, Ty); |
| 1467 | return getSExt(C, Ty); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1470 | Constant *ConstantExpr::getTruncOrBitCast(Constant *C, Type *Ty) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1471 | if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Dan Gohman | 3cdcc3f | 2010-04-12 22:12:29 +0000 | [diff] [blame] | 1472 | return getBitCast(C, Ty); |
| 1473 | return getTrunc(C, Ty); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1476 | Constant *ConstantExpr::getPointerCast(Constant *S, Type *Ty) { |
Evgeniy Stepanov | 2338264 | 2013-01-16 14:41:46 +0000 | [diff] [blame] | 1477 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 1478 | assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) && |
| 1479 | "Invalid cast"); |
Reid Spencer | bc245a0 | 2006-12-05 03:25:26 +0000 | [diff] [blame] | 1480 | |
Evgeniy Stepanov | 2338264 | 2013-01-16 14:41:46 +0000 | [diff] [blame] | 1481 | if (Ty->isIntOrIntVectorTy()) |
Dan Gohman | 3cdcc3f | 2010-04-12 22:12:29 +0000 | [diff] [blame] | 1482 | return getPtrToInt(S, Ty); |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 1483 | |
| 1484 | unsigned SrcAS = S->getType()->getPointerAddressSpace(); |
| 1485 | if (Ty->isPtrOrPtrVectorTy() && SrcAS != Ty->getPointerAddressSpace()) |
| 1486 | return getAddrSpaceCast(S, Ty); |
| 1487 | |
Dan Gohman | 3cdcc3f | 2010-04-12 22:12:29 +0000 | [diff] [blame] | 1488 | return getBitCast(S, Ty); |
Reid Spencer | bc245a0 | 2006-12-05 03:25:26 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
Matt Arsenault | 21f38f4 | 2013-12-07 02:58:41 +0000 | [diff] [blame] | 1491 | Constant *ConstantExpr::getPointerBitCastOrAddrSpaceCast(Constant *S, |
| 1492 | Type *Ty) { |
| 1493 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 1494 | assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 1495 | |
| 1496 | if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace()) |
| 1497 | return getAddrSpaceCast(S, Ty); |
| 1498 | |
| 1499 | return getBitCast(S, Ty); |
| 1500 | } |
| 1501 | |
Sanjay Patel | f3587ec | 2016-05-18 22:05:28 +0000 | [diff] [blame] | 1502 | Constant *ConstantExpr::getIntegerCast(Constant *C, Type *Ty, bool isSigned) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1503 | assert(C->getType()->isIntOrIntVectorTy() && |
| 1504 | Ty->isIntOrIntVectorTy() && "Invalid cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1505 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 1506 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 56521c4 | 2006-12-12 00:51:07 +0000 | [diff] [blame] | 1507 | Instruction::CastOps opcode = |
| 1508 | (SrcBits == DstBits ? Instruction::BitCast : |
| 1509 | (SrcBits > DstBits ? Instruction::Trunc : |
| 1510 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
| 1511 | return getCast(opcode, C, Ty); |
| 1512 | } |
| 1513 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1514 | Constant *ConstantExpr::getFPCast(Constant *C, Type *Ty) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1515 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && |
Reid Spencer | 56521c4 | 2006-12-12 00:51:07 +0000 | [diff] [blame] | 1516 | "Invalid cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1517 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 1518 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | ca104e8 | 2006-12-12 05:38:50 +0000 | [diff] [blame] | 1519 | if (SrcBits == DstBits) |
| 1520 | return C; // Avoid a useless cast |
Reid Spencer | 56521c4 | 2006-12-12 00:51:07 +0000 | [diff] [blame] | 1521 | Instruction::CastOps opcode = |
Jay Foad | 9f32cfd | 2011-01-27 14:44:55 +0000 | [diff] [blame] | 1522 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt); |
Reid Spencer | 56521c4 | 2006-12-12 00:51:07 +0000 | [diff] [blame] | 1523 | return getCast(opcode, C, Ty); |
| 1524 | } |
| 1525 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1526 | Constant *ConstantExpr::getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1527 | #ifndef NDEBUG |
| 1528 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1529 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
| 1530 | #endif |
| 1531 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1532 | assert(C->getType()->isIntOrIntVectorTy() && "Trunc operand must be integer"); |
| 1533 | assert(Ty->isIntOrIntVectorTy() && "Trunc produces only integral"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1534 | assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&& |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1535 | "SrcTy must be larger than DestTy for Trunc!"); |
| 1536 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1537 | return getFoldedCast(Instruction::Trunc, C, Ty, OnlyIfReduced); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1538 | } |
| 1539 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1540 | Constant *ConstantExpr::getSExt(Constant *C, Type *Ty, bool OnlyIfReduced) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1541 | #ifndef NDEBUG |
| 1542 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1543 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
| 1544 | #endif |
| 1545 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1546 | assert(C->getType()->isIntOrIntVectorTy() && "SExt operand must be integral"); |
| 1547 | assert(Ty->isIntOrIntVectorTy() && "SExt produces only integer"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1548 | assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&& |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1549 | "SrcTy must be smaller than DestTy for SExt!"); |
| 1550 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1551 | return getFoldedCast(Instruction::SExt, C, Ty, OnlyIfReduced); |
Chris Lattner | dd28474 | 2004-04-04 23:20:30 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1554 | Constant *ConstantExpr::getZExt(Constant *C, Type *Ty, bool OnlyIfReduced) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1555 | #ifndef NDEBUG |
| 1556 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1557 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
| 1558 | #endif |
| 1559 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1560 | assert(C->getType()->isIntOrIntVectorTy() && "ZEXt operand must be integral"); |
| 1561 | assert(Ty->isIntOrIntVectorTy() && "ZExt produces only integer"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1562 | assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&& |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1563 | "SrcTy must be smaller than DestTy for ZExt!"); |
| 1564 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1565 | return getFoldedCast(Instruction::ZExt, C, Ty, OnlyIfReduced); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1568 | Constant *ConstantExpr::getFPTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1569 | #ifndef NDEBUG |
| 1570 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1571 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
| 1572 | #endif |
| 1573 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1574 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1575 | C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&& |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1576 | "This is an illegal floating point truncation!"); |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1577 | return getFoldedCast(Instruction::FPTrunc, C, Ty, OnlyIfReduced); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1580 | Constant *ConstantExpr::getFPExtend(Constant *C, Type *Ty, bool OnlyIfReduced) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1581 | #ifndef NDEBUG |
| 1582 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1583 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
| 1584 | #endif |
| 1585 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1586 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1587 | C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&& |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1588 | "This is an illegal floating point extension!"); |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1589 | return getFoldedCast(Instruction::FPExt, C, Ty, OnlyIfReduced); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1592 | Constant *ConstantExpr::getUIToFP(Constant *C, Type *Ty, bool OnlyIfReduced) { |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1593 | #ifndef NDEBUG |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1594 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1595 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1596 | #endif |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1597 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1598 | assert(C->getType()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() && |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1599 | "This is an illegal uint to floating point cast!"); |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1600 | return getFoldedCast(Instruction::UIToFP, C, Ty, OnlyIfReduced); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1601 | } |
| 1602 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1603 | Constant *ConstantExpr::getSIToFP(Constant *C, Type *Ty, bool OnlyIfReduced) { |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1604 | #ifndef NDEBUG |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1605 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1606 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1607 | #endif |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1608 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1609 | assert(C->getType()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() && |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1610 | "This is an illegal sint to floating point cast!"); |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1611 | return getFoldedCast(Instruction::SIToFP, C, Ty, OnlyIfReduced); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1614 | Constant *ConstantExpr::getFPToUI(Constant *C, Type *Ty, bool OnlyIfReduced) { |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1615 | #ifndef NDEBUG |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1616 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1617 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1618 | #endif |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1619 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1620 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() && |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1621 | "This is an illegal floating point to uint cast!"); |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1622 | return getFoldedCast(Instruction::FPToUI, C, Ty, OnlyIfReduced); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1623 | } |
| 1624 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1625 | Constant *ConstantExpr::getFPToSI(Constant *C, Type *Ty, bool OnlyIfReduced) { |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1626 | #ifndef NDEBUG |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1627 | bool fromVec = C->getType()->getTypeID() == Type::VectorTyID; |
| 1628 | bool toVec = Ty->getTypeID() == Type::VectorTyID; |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 1629 | #endif |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1630 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1631 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() && |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1632 | "This is an illegal floating point to sint cast!"); |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1633 | return getFoldedCast(Instruction::FPToSI, C, Ty, OnlyIfReduced); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1634 | } |
| 1635 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1636 | Constant *ConstantExpr::getPtrToInt(Constant *C, Type *DstTy, |
| 1637 | bool OnlyIfReduced) { |
Craig Topper | 95d2347 | 2017-07-09 07:04:00 +0000 | [diff] [blame] | 1638 | assert(C->getType()->isPtrOrPtrVectorTy() && |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 1639 | "PtrToInt source must be pointer or pointer vector"); |
Craig Topper | 95d2347 | 2017-07-09 07:04:00 +0000 | [diff] [blame] | 1640 | assert(DstTy->isIntOrIntVectorTy() && |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 1641 | "PtrToInt destination must be integer or integer vector"); |
Chris Lattner | 8a3df54 | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 1642 | assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy)); |
Nick Lewycky | ff50962 | 2012-01-25 03:20:12 +0000 | [diff] [blame] | 1643 | if (isa<VectorType>(C->getType())) |
Chris Lattner | 8326bd8 | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 1644 | assert(C->getType()->getVectorNumElements()==DstTy->getVectorNumElements()&& |
Chris Lattner | 8a3df54 | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 1645 | "Invalid cast between a different number of vector elements"); |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1646 | return getFoldedCast(Instruction::PtrToInt, C, DstTy, OnlyIfReduced); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1649 | Constant *ConstantExpr::getIntToPtr(Constant *C, Type *DstTy, |
| 1650 | bool OnlyIfReduced) { |
Craig Topper | 95d2347 | 2017-07-09 07:04:00 +0000 | [diff] [blame] | 1651 | assert(C->getType()->isIntOrIntVectorTy() && |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 1652 | "IntToPtr source must be integer or integer vector"); |
Craig Topper | 95d2347 | 2017-07-09 07:04:00 +0000 | [diff] [blame] | 1653 | assert(DstTy->isPtrOrPtrVectorTy() && |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 1654 | "IntToPtr destination must be a pointer or pointer vector"); |
Chris Lattner | 8a3df54 | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 1655 | assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy)); |
Nick Lewycky | ff50962 | 2012-01-25 03:20:12 +0000 | [diff] [blame] | 1656 | if (isa<VectorType>(C->getType())) |
Chris Lattner | 8326bd8 | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 1657 | assert(C->getType()->getVectorNumElements()==DstTy->getVectorNumElements()&& |
Chris Lattner | 8a3df54 | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 1658 | "Invalid cast between a different number of vector elements"); |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1659 | return getFoldedCast(Instruction::IntToPtr, C, DstTy, OnlyIfReduced); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1662 | Constant *ConstantExpr::getBitCast(Constant *C, Type *DstTy, |
| 1663 | bool OnlyIfReduced) { |
Chris Lattner | 37bc78a | 2010-01-26 21:51:43 +0000 | [diff] [blame] | 1664 | assert(CastInst::castIsValid(Instruction::BitCast, C, DstTy) && |
| 1665 | "Invalid constantexpr bitcast!"); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1666 | |
Chris Lattner | cbeda87 | 2009-03-21 06:55:54 +0000 | [diff] [blame] | 1667 | // It is common to ask for a bitcast of a value to its own type, handle this |
| 1668 | // speedily. |
| 1669 | if (C->getType() == DstTy) return C; |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1670 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1671 | return getFoldedCast(Instruction::BitCast, C, DstTy, OnlyIfReduced); |
Chris Lattner | dd28474 | 2004-04-04 23:20:30 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1674 | Constant *ConstantExpr::getAddrSpaceCast(Constant *C, Type *DstTy, |
| 1675 | bool OnlyIfReduced) { |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 1676 | assert(CastInst::castIsValid(Instruction::AddrSpaceCast, C, DstTy) && |
| 1677 | "Invalid constantexpr addrspacecast!"); |
| 1678 | |
Jingyue Wu | baabe50 | 2014-06-15 21:40:57 +0000 | [diff] [blame] | 1679 | // Canonicalize addrspacecasts between different pointer types by first |
| 1680 | // bitcasting the pointer type and then converting the address space. |
| 1681 | PointerType *SrcScalarTy = cast<PointerType>(C->getType()->getScalarType()); |
| 1682 | PointerType *DstScalarTy = cast<PointerType>(DstTy->getScalarType()); |
| 1683 | Type *DstElemTy = DstScalarTy->getElementType(); |
| 1684 | if (SrcScalarTy->getElementType() != DstElemTy) { |
| 1685 | Type *MidTy = PointerType::get(DstElemTy, SrcScalarTy->getAddressSpace()); |
| 1686 | if (VectorType *VT = dyn_cast<VectorType>(DstTy)) { |
| 1687 | // Handle vectors of pointers. |
| 1688 | MidTy = VectorType::get(MidTy, VT->getNumElements()); |
| 1689 | } |
| 1690 | C = getBitCast(C, MidTy); |
| 1691 | } |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1692 | return getFoldedCast(Instruction::AddrSpaceCast, C, DstTy, OnlyIfReduced); |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
Chris Lattner | 887ecac | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1695 | Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2, |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1696 | unsigned Flags, Type *OnlyIfReducedTy) { |
Chris Lattner | 887ecac | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1697 | // Check the operands for consistency first. |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1698 | assert(Opcode >= Instruction::BinaryOpsBegin && |
| 1699 | Opcode < Instruction::BinaryOpsEnd && |
Chris Lattner | 38a9bcd | 2003-05-21 17:49:25 +0000 | [diff] [blame] | 1700 | "Invalid opcode in binary constant expression"); |
| 1701 | assert(C1->getType() == C2->getType() && |
| 1702 | "Operand types in binary constant expression should match"); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1703 | |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1704 | #ifndef NDEBUG |
| 1705 | switch (Opcode) { |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1706 | case Instruction::Add: |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1707 | case Instruction::Sub: |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1708 | case Instruction::Mul: |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1709 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1710 | assert(C1->getType()->isIntOrIntVectorTy() && |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1711 | "Tried to create an integer operation on a non-integer type!"); |
| 1712 | break; |
| 1713 | case Instruction::FAdd: |
| 1714 | case Instruction::FSub: |
| 1715 | case Instruction::FMul: |
| 1716 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1717 | assert(C1->getType()->isFPOrFPVectorTy() && |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1718 | "Tried to create a floating-point operation on a " |
| 1719 | "non-floating-point type!"); |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1720 | break; |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1721 | case Instruction::UDiv: |
| 1722 | case Instruction::SDiv: |
| 1723 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1724 | assert(C1->getType()->isIntOrIntVectorTy() && |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1725 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
| 1726 | break; |
| 1727 | case Instruction::FDiv: |
| 1728 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1729 | assert(C1->getType()->isFPOrFPVectorTy() && |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 1730 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1731 | break; |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1732 | case Instruction::URem: |
| 1733 | case Instruction::SRem: |
| 1734 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1735 | assert(C1->getType()->isIntOrIntVectorTy() && |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1736 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
| 1737 | break; |
| 1738 | case Instruction::FRem: |
| 1739 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1740 | assert(C1->getType()->isFPOrFPVectorTy() && |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 1741 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1742 | break; |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1743 | case Instruction::And: |
| 1744 | case Instruction::Or: |
| 1745 | case Instruction::Xor: |
| 1746 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1747 | assert(C1->getType()->isIntOrIntVectorTy() && |
Misha Brukman | 3852f65 | 2005-01-27 06:46:38 +0000 | [diff] [blame] | 1748 | "Tried to create a logical operation on a non-integral type!"); |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1749 | break; |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1750 | case Instruction::Shl: |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1751 | case Instruction::LShr: |
| 1752 | case Instruction::AShr: |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1753 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1754 | assert(C1->getType()->isIntOrIntVectorTy() && |
Chris Lattner | caf3f3e | 2004-08-17 17:28:46 +0000 | [diff] [blame] | 1755 | "Tried to create a shift operation on a non-integer type!"); |
| 1756 | break; |
| 1757 | default: |
| 1758 | break; |
| 1759 | } |
| 1760 | #endif |
| 1761 | |
Chris Lattner | 887ecac | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1762 | if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2)) |
| 1763 | return FC; // Fold a few common cases. |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1764 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1765 | if (OnlyIfReducedTy == C1->getType()) |
| 1766 | return nullptr; |
| 1767 | |
Benjamin Kramer | 324322b | 2013-03-07 20:53:34 +0000 | [diff] [blame] | 1768 | Constant *ArgVec[] = { C1, C2 }; |
Duncan P. N. Exon Smith | 317c139 | 2014-08-19 16:39:58 +0000 | [diff] [blame] | 1769 | ConstantExprKeyType Key(Opcode, ArgVec, 0, Flags); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1770 | |
Chris Lattner | 887ecac | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1771 | LLVMContextImpl *pImpl = C1->getContext().pImpl; |
| 1772 | return pImpl->ExprConstants.getOrCreate(C1->getType(), Key); |
Reid Spencer | a009d0d | 2006-12-04 21:35:24 +0000 | [diff] [blame] | 1773 | } |
| 1774 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1775 | Constant *ConstantExpr::getSizeOf(Type* Ty) { |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1776 | // sizeof is implemented as: (i64) gep (Ty*)null, 1 |
| 1777 | // Note that a non-inbounds gep is used, as null isn't within any object. |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1778 | Constant *GEPIdx = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1779 | Constant *GEP = getGetElementPtr( |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 1780 | Ty, Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx); |
Dan Gohman | 3cdcc3f | 2010-04-12 22:12:29 +0000 | [diff] [blame] | 1781 | return getPtrToInt(GEP, |
| 1782 | Type::getInt64Ty(Ty->getContext())); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1785 | Constant *ConstantExpr::getAlignOf(Type* Ty) { |
Dan Gohman | cf91383 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 1786 | // alignof is implemented as: (i64) gep ({i1,Ty}*)null, 0, 1 |
Dan Gohman | 50c09d0 | 2009-08-11 17:57:01 +0000 | [diff] [blame] | 1787 | // Note that a non-inbounds gep is used, as null isn't within any object. |
Serge Guelton | e38003f | 2017-05-09 19:31:13 +0000 | [diff] [blame] | 1788 | Type *AligningTy = StructType::get(Type::getInt1Ty(Ty->getContext()), Ty); |
Matt Arsenault | be55888 | 2014-04-23 20:58:57 +0000 | [diff] [blame] | 1789 | Constant *NullPtr = Constant::getNullValue(AligningTy->getPointerTo(0)); |
Dan Gohman | a9be739 | 2010-01-28 02:43:22 +0000 | [diff] [blame] | 1790 | Constant *Zero = ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0); |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1791 | Constant *One = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1792 | Constant *Indices[2] = { Zero, One }; |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 1793 | Constant *GEP = getGetElementPtr(AligningTy, NullPtr, Indices); |
Dan Gohman | 3cdcc3f | 2010-04-12 22:12:29 +0000 | [diff] [blame] | 1794 | return getPtrToInt(GEP, |
| 1795 | Type::getInt64Ty(Ty->getContext())); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1796 | } |
| 1797 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1798 | Constant *ConstantExpr::getOffsetOf(StructType* STy, unsigned FieldNo) { |
Dan Gohman | ede94e6 | 2010-02-01 16:37:38 +0000 | [diff] [blame] | 1799 | return getOffsetOf(STy, ConstantInt::get(Type::getInt32Ty(STy->getContext()), |
| 1800 | FieldNo)); |
| 1801 | } |
| 1802 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1803 | Constant *ConstantExpr::getOffsetOf(Type* Ty, Constant *FieldNo) { |
Dan Gohman | ff3af725 | 2009-08-16 21:26:11 +0000 | [diff] [blame] | 1804 | // offsetof is implemented as: (i64) gep (Ty*)null, 0, FieldNo |
| 1805 | // Note that a non-inbounds gep is used, as null isn't within any object. |
| 1806 | Constant *GEPIdx[] = { |
Dan Gohman | ede94e6 | 2010-02-01 16:37:38 +0000 | [diff] [blame] | 1807 | ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0), |
| 1808 | FieldNo |
Dan Gohman | ff3af725 | 2009-08-16 21:26:11 +0000 | [diff] [blame] | 1809 | }; |
| 1810 | Constant *GEP = getGetElementPtr( |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 1811 | Ty, Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx); |
Dan Gohman | 3cdcc3f | 2010-04-12 22:12:29 +0000 | [diff] [blame] | 1812 | return getPtrToInt(GEP, |
| 1813 | Type::getInt64Ty(Ty->getContext())); |
Dan Gohman | ff3af725 | 2009-08-16 21:26:11 +0000 | [diff] [blame] | 1814 | } |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1815 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1816 | Constant *ConstantExpr::getCompare(unsigned short Predicate, Constant *C1, |
| 1817 | Constant *C2, bool OnlyIfReduced) { |
Reid Spencer | a009d0d | 2006-12-04 21:35:24 +0000 | [diff] [blame] | 1818 | assert(C1->getType() == C2->getType() && "Op types should be identical!"); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1819 | |
Chris Lattner | 887ecac | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1820 | switch (Predicate) { |
| 1821 | default: llvm_unreachable("Invalid CmpInst predicate"); |
| 1822 | case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT: |
| 1823 | case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE: |
| 1824 | case CmpInst::FCMP_ONE: case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO: |
| 1825 | case CmpInst::FCMP_UEQ: case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE: |
| 1826 | case CmpInst::FCMP_ULT: case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE: |
| 1827 | case CmpInst::FCMP_TRUE: |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1828 | return getFCmp(Predicate, C1, C2, OnlyIfReduced); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1829 | |
Chris Lattner | 887ecac | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1830 | case CmpInst::ICMP_EQ: case CmpInst::ICMP_NE: case CmpInst::ICMP_UGT: |
| 1831 | case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE: |
| 1832 | case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT: |
| 1833 | case CmpInst::ICMP_SLE: |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1834 | return getICmp(Predicate, C1, C2, OnlyIfReduced); |
Chris Lattner | 887ecac | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1835 | } |
Chris Lattner | 29ca2c6 | 2004-08-04 18:50:09 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1838 | Constant *ConstantExpr::getSelect(Constant *C, Constant *V1, Constant *V2, |
| 1839 | Type *OnlyIfReducedTy) { |
Chris Lattner | 4163213 | 2008-12-29 00:16:12 +0000 | [diff] [blame] | 1840 | assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands"); |
Chris Lattner | 6e415c0 | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 1841 | |
Chris Lattner | 887ecac | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1842 | if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2)) |
| 1843 | return SC; // Fold common cases |
Chris Lattner | 6e415c0 | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 1844 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1845 | if (OnlyIfReducedTy == V1->getType()) |
| 1846 | return nullptr; |
| 1847 | |
Benjamin Kramer | 324322b | 2013-03-07 20:53:34 +0000 | [diff] [blame] | 1848 | Constant *ArgVec[] = { C, V1, V2 }; |
Duncan P. N. Exon Smith | 317c139 | 2014-08-19 16:39:58 +0000 | [diff] [blame] | 1849 | ConstantExprKeyType Key(Instruction::Select, ArgVec); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1850 | |
Chris Lattner | 887ecac | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1851 | LLVMContextImpl *pImpl = C->getContext().pImpl; |
| 1852 | return pImpl->ExprConstants.getOrCreate(V1->getType(), Key); |
Chris Lattner | 6e415c0 | 2004-03-12 05:54:04 +0000 | [diff] [blame] | 1853 | } |
| 1854 | |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 1855 | Constant *ConstantExpr::getGetElementPtr(Type *Ty, Constant *C, |
| 1856 | ArrayRef<Value *> Idxs, bool InBounds, |
Peter Collingbourne | d93620b | 2016-11-10 22:34:55 +0000 | [diff] [blame] | 1857 | Optional<unsigned> InRangeIndex, |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 1858 | Type *OnlyIfReducedTy) { |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 1859 | if (!Ty) |
| 1860 | Ty = cast<PointerType>(C->getType()->getScalarType())->getElementType(); |
| 1861 | else |
David Blaikie | d9d900c | 2015-05-07 17:28:58 +0000 | [diff] [blame] | 1862 | assert( |
| 1863 | Ty == |
| 1864 | cast<PointerType>(C->getType()->getScalarType())->getContainedType(0u)); |
| 1865 | |
Peter Collingbourne | d93620b | 2016-11-10 22:34:55 +0000 | [diff] [blame] | 1866 | if (Constant *FC = |
| 1867 | ConstantFoldGetElementPtr(Ty, C, InBounds, InRangeIndex, Idxs)) |
David Blaikie | d9d900c | 2015-05-07 17:28:58 +0000 | [diff] [blame] | 1868 | return FC; // Fold a few common cases. |
| 1869 | |
Chris Lattner | 887ecac | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1870 | // Get the result type of the getelementptr! |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 1871 | Type *DestTy = GetElementPtrInst::getIndexedType(Ty, Idxs); |
| 1872 | assert(DestTy && "GEP indices invalid!"); |
Chris Lattner | 8326bd8 | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 1873 | unsigned AS = C->getType()->getPointerAddressSpace(); |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 1874 | Type *ReqTy = DestTy->getPointerTo(AS); |
Elena Demikhovsky | ee004bc | 2016-05-15 12:30:25 +0000 | [diff] [blame] | 1875 | |
| 1876 | unsigned NumVecElts = 0; |
| 1877 | if (C->getType()->isVectorTy()) |
| 1878 | NumVecElts = C->getType()->getVectorNumElements(); |
| 1879 | else for (auto Idx : Idxs) |
| 1880 | if (Idx->getType()->isVectorTy()) |
| 1881 | NumVecElts = Idx->getType()->getVectorNumElements(); |
| 1882 | |
| 1883 | if (NumVecElts) |
| 1884 | ReqTy = VectorType::get(ReqTy, NumVecElts); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1885 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1886 | if (OnlyIfReducedTy == ReqTy) |
| 1887 | return nullptr; |
| 1888 | |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 1889 | // Look up the constant in the table first to ensure uniqueness |
| 1890 | std::vector<Constant*> ArgVec; |
Jay Foad | ed8db7d | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 1891 | ArgVec.reserve(1 + Idxs.size()); |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 1892 | ArgVec.push_back(C); |
Duncan Sands | e6beec6 | 2012-11-13 12:59:33 +0000 | [diff] [blame] | 1893 | for (unsigned i = 0, e = Idxs.size(); i != e; ++i) { |
Duncan Sands | e6beec6 | 2012-11-13 12:59:33 +0000 | [diff] [blame] | 1894 | assert((!Idxs[i]->getType()->isVectorTy() || |
Elena Demikhovsky | ee004bc | 2016-05-15 12:30:25 +0000 | [diff] [blame] | 1895 | Idxs[i]->getType()->getVectorNumElements() == NumVecElts) && |
Duncan Sands | e6beec6 | 2012-11-13 12:59:33 +0000 | [diff] [blame] | 1896 | "getelementptr index type missmatch"); |
Elena Demikhovsky | ee004bc | 2016-05-15 12:30:25 +0000 | [diff] [blame] | 1897 | |
| 1898 | Constant *Idx = cast<Constant>(Idxs[i]); |
| 1899 | if (NumVecElts && !Idxs[i]->getType()->isVectorTy()) |
| 1900 | Idx = ConstantVector::getSplat(NumVecElts, Idx); |
| 1901 | ArgVec.push_back(Idx); |
Duncan Sands | e6beec6 | 2012-11-13 12:59:33 +0000 | [diff] [blame] | 1902 | } |
Peter Collingbourne | d93620b | 2016-11-10 22:34:55 +0000 | [diff] [blame] | 1903 | |
| 1904 | unsigned SubClassOptionalData = InBounds ? GEPOperator::IsInBounds : 0; |
| 1905 | if (InRangeIndex && *InRangeIndex < 63) |
| 1906 | SubClassOptionalData |= (*InRangeIndex + 1) << 1; |
Duncan P. N. Exon Smith | 317c139 | 2014-08-19 16:39:58 +0000 | [diff] [blame] | 1907 | const ConstantExprKeyType Key(Instruction::GetElementPtr, ArgVec, 0, |
Peter Collingbourne | d93620b | 2016-11-10 22:34:55 +0000 | [diff] [blame] | 1908 | SubClassOptionalData, None, Ty); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1909 | |
Chris Lattner | 887ecac | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1910 | LLVMContextImpl *pImpl = C->getContext().pImpl; |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 1911 | return pImpl->ExprConstants.getOrCreate(ReqTy, Key); |
| 1912 | } |
| 1913 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1914 | Constant *ConstantExpr::getICmp(unsigned short pred, Constant *LHS, |
| 1915 | Constant *RHS, bool OnlyIfReduced) { |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1916 | assert(LHS->getType() == RHS->getType()); |
Craig Topper | 4584476 | 2017-07-05 23:35:46 +0000 | [diff] [blame] | 1917 | assert(CmpInst::isIntPredicate((CmpInst::Predicate)pred) && |
| 1918 | "Invalid ICmp Predicate"); |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1919 | |
Chris Lattner | f5edeeb | 2010-02-01 20:48:08 +0000 | [diff] [blame] | 1920 | if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS)) |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1921 | return FC; // Fold a few common cases... |
| 1922 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1923 | if (OnlyIfReduced) |
| 1924 | return nullptr; |
| 1925 | |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1926 | // Look up the constant in the table first to ensure uniqueness |
Benjamin Kramer | 324322b | 2013-03-07 20:53:34 +0000 | [diff] [blame] | 1927 | Constant *ArgVec[] = { LHS, RHS }; |
Reid Spencer | b153749 | 2006-12-24 18:42:29 +0000 | [diff] [blame] | 1928 | // Get the key type with both the opcode and predicate |
Duncan P. N. Exon Smith | 317c139 | 2014-08-19 16:39:58 +0000 | [diff] [blame] | 1929 | const ConstantExprKeyType Key(Instruction::ICmp, ArgVec, pred); |
Owen Anderson | 6179404 | 2009-06-17 20:10:08 +0000 | [diff] [blame] | 1930 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1931 | Type *ResultTy = Type::getInt1Ty(LHS->getContext()); |
| 1932 | if (VectorType *VT = dyn_cast<VectorType>(LHS->getType())) |
Nick Lewycky | 9e26c1c | 2010-01-21 07:03:21 +0000 | [diff] [blame] | 1933 | ResultTy = VectorType::get(ResultTy, VT->getNumElements()); |
| 1934 | |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1935 | LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl; |
Nick Lewycky | 9e26c1c | 2010-01-21 07:03:21 +0000 | [diff] [blame] | 1936 | return pImpl->ExprConstants.getOrCreate(ResultTy, Key); |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1937 | } |
| 1938 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1939 | Constant *ConstantExpr::getFCmp(unsigned short pred, Constant *LHS, |
| 1940 | Constant *RHS, bool OnlyIfReduced) { |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1941 | assert(LHS->getType() == RHS->getType()); |
Craig Topper | 4584476 | 2017-07-05 23:35:46 +0000 | [diff] [blame] | 1942 | assert(CmpInst::isFPPredicate((CmpInst::Predicate)pred) && |
| 1943 | "Invalid FCmp Predicate"); |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1944 | |
Chris Lattner | f5edeeb | 2010-02-01 20:48:08 +0000 | [diff] [blame] | 1945 | if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS)) |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1946 | return FC; // Fold a few common cases... |
| 1947 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1948 | if (OnlyIfReduced) |
| 1949 | return nullptr; |
| 1950 | |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1951 | // Look up the constant in the table first to ensure uniqueness |
Benjamin Kramer | 324322b | 2013-03-07 20:53:34 +0000 | [diff] [blame] | 1952 | Constant *ArgVec[] = { LHS, RHS }; |
Reid Spencer | b153749 | 2006-12-24 18:42:29 +0000 | [diff] [blame] | 1953 | // Get the key type with both the opcode and predicate |
Duncan P. N. Exon Smith | 317c139 | 2014-08-19 16:39:58 +0000 | [diff] [blame] | 1954 | const ConstantExprKeyType Key(Instruction::FCmp, ArgVec, pred); |
Nick Lewycky | 9e26c1c | 2010-01-21 07:03:21 +0000 | [diff] [blame] | 1955 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1956 | Type *ResultTy = Type::getInt1Ty(LHS->getContext()); |
| 1957 | if (VectorType *VT = dyn_cast<VectorType>(LHS->getType())) |
Nick Lewycky | 9e26c1c | 2010-01-21 07:03:21 +0000 | [diff] [blame] | 1958 | ResultTy = VectorType::get(ResultTy, VT->getNumElements()); |
| 1959 | |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1960 | LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl; |
Nick Lewycky | 9e26c1c | 2010-01-21 07:03:21 +0000 | [diff] [blame] | 1961 | return pImpl->ExprConstants.getOrCreate(ResultTy, Key); |
Reid Spencer | ee3c991 | 2006-12-04 05:19:50 +0000 | [diff] [blame] | 1962 | } |
| 1963 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1964 | Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx, |
| 1965 | Type *OnlyIfReducedTy) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1966 | assert(Val->getType()->isVectorTy() && |
Reid Spencer | 09575ba | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1967 | "Tried to create extractelement operation on non-vector type!"); |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 1968 | assert(Idx->getType()->isIntegerTy() && |
| 1969 | "Extractelement index must be an integer type!"); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1970 | |
Chris Lattner | 887ecac | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1971 | if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx)) |
Chris Lattner | 09660c9 | 2009-12-30 20:25:09 +0000 | [diff] [blame] | 1972 | return FC; // Fold a few common cases. |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1973 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1974 | Type *ReqTy = Val->getType()->getVectorElementType(); |
| 1975 | if (OnlyIfReducedTy == ReqTy) |
| 1976 | return nullptr; |
| 1977 | |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1978 | // Look up the constant in the table first to ensure uniqueness |
Benjamin Kramer | 324322b | 2013-03-07 20:53:34 +0000 | [diff] [blame] | 1979 | Constant *ArgVec[] = { Val, Idx }; |
Duncan P. N. Exon Smith | 317c139 | 2014-08-19 16:39:58 +0000 | [diff] [blame] | 1980 | const ConstantExprKeyType Key(Instruction::ExtractElement, ArgVec); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 1981 | |
Chris Lattner | 887ecac | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1982 | LLVMContextImpl *pImpl = Val->getContext().pImpl; |
Owen Anderson | 1584a29 | 2009-08-04 20:25:11 +0000 | [diff] [blame] | 1983 | return pImpl->ExprConstants.getOrCreate(ReqTy, Key); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1984 | } |
| 1985 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1986 | Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt, |
| 1987 | Constant *Idx, Type *OnlyIfReducedTy) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1988 | assert(Val->getType()->isVectorTy() && |
Reid Spencer | 09575ba | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1989 | "Tried to create insertelement operation on non-vector type!"); |
Chris Lattner | 8326bd8 | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 1990 | assert(Elt->getType() == Val->getType()->getVectorElementType() && |
| 1991 | "Insertelement types must match!"); |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 1992 | assert(Idx->getType()->isIntegerTy() && |
Reid Spencer | 2546b76 | 2007-01-26 07:37:34 +0000 | [diff] [blame] | 1993 | "Insertelement index must be i32 type!"); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1994 | |
Chris Lattner | 887ecac | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 1995 | if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx)) |
| 1996 | return FC; // Fold a few common cases. |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 1997 | |
| 1998 | if (OnlyIfReducedTy == Val->getType()) |
| 1999 | return nullptr; |
| 2000 | |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 2001 | // Look up the constant in the table first to ensure uniqueness |
Benjamin Kramer | 324322b | 2013-03-07 20:53:34 +0000 | [diff] [blame] | 2002 | Constant *ArgVec[] = { Val, Elt, Idx }; |
Duncan P. N. Exon Smith | 317c139 | 2014-08-19 16:39:58 +0000 | [diff] [blame] | 2003 | const ConstantExprKeyType Key(Instruction::InsertElement, ArgVec); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2004 | |
Chris Lattner | 887ecac | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 2005 | LLVMContextImpl *pImpl = Val->getContext().pImpl; |
| 2006 | return pImpl->ExprConstants.getOrCreate(Val->getType(), Key); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 2007 | } |
| 2008 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 2009 | Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2, |
| 2010 | Constant *Mask, Type *OnlyIfReducedTy) { |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 2011 | assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) && |
| 2012 | "Invalid shuffle vector constant expr operands!"); |
Nate Begeman | 94aa38d | 2009-02-12 21:28:33 +0000 | [diff] [blame] | 2013 | |
Chris Lattner | 887ecac | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 2014 | if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask)) |
| 2015 | return FC; // Fold a few common cases. |
| 2016 | |
Chris Lattner | 8326bd8 | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 2017 | unsigned NElts = Mask->getType()->getVectorNumElements(); |
| 2018 | Type *EltTy = V1->getType()->getVectorElementType(); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2019 | Type *ShufTy = VectorType::get(EltTy, NElts); |
Chris Lattner | 887ecac | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 2020 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 2021 | if (OnlyIfReducedTy == ShufTy) |
| 2022 | return nullptr; |
| 2023 | |
Chris Lattner | 887ecac | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 2024 | // Look up the constant in the table first to ensure uniqueness |
Benjamin Kramer | 324322b | 2013-03-07 20:53:34 +0000 | [diff] [blame] | 2025 | Constant *ArgVec[] = { V1, V2, Mask }; |
Duncan P. N. Exon Smith | 317c139 | 2014-08-19 16:39:58 +0000 | [diff] [blame] | 2026 | const ConstantExprKeyType Key(Instruction::ShuffleVector, ArgVec); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2027 | |
Chris Lattner | 887ecac | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 2028 | LLVMContextImpl *pImpl = ShufTy->getContext().pImpl; |
| 2029 | return pImpl->ExprConstants.getOrCreate(ShufTy, Key); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
Chris Lattner | 887ecac | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 2032 | Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val, |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 2033 | ArrayRef<unsigned> Idxs, |
| 2034 | Type *OnlyIfReducedTy) { |
Hal Finkel | b31366d | 2013-07-10 22:51:01 +0000 | [diff] [blame] | 2035 | assert(Agg->getType()->isFirstClassType() && |
| 2036 | "Non-first-class type for constant insertvalue expression"); |
| 2037 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 2038 | assert(ExtractValueInst::getIndexedType(Agg->getType(), |
| 2039 | Idxs) == Val->getType() && |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2040 | "insertvalue indices invalid!"); |
Hal Finkel | b31366d | 2013-07-10 22:51:01 +0000 | [diff] [blame] | 2041 | Type *ReqTy = Val->getType(); |
| 2042 | |
| 2043 | if (Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs)) |
| 2044 | return FC; |
| 2045 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 2046 | if (OnlyIfReducedTy == ReqTy) |
| 2047 | return nullptr; |
| 2048 | |
Hal Finkel | b31366d | 2013-07-10 22:51:01 +0000 | [diff] [blame] | 2049 | Constant *ArgVec[] = { Agg, Val }; |
Duncan P. N. Exon Smith | 317c139 | 2014-08-19 16:39:58 +0000 | [diff] [blame] | 2050 | const ConstantExprKeyType Key(Instruction::InsertValue, ArgVec, 0, 0, Idxs); |
Hal Finkel | b31366d | 2013-07-10 22:51:01 +0000 | [diff] [blame] | 2051 | |
| 2052 | LLVMContextImpl *pImpl = Agg->getContext().pImpl; |
| 2053 | return pImpl->ExprConstants.getOrCreate(ReqTy, Key); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2054 | } |
| 2055 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 2056 | Constant *ConstantExpr::getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs, |
| 2057 | Type *OnlyIfReducedTy) { |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2058 | assert(Agg->getType()->isFirstClassType() && |
Chris Lattner | 887ecac | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 2059 | "Tried to create extractelement operation on non-first-class type!"); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2060 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2061 | Type *ReqTy = ExtractValueInst::getIndexedType(Agg->getType(), Idxs); |
Chandler Carruth | 9db56b8 | 2011-07-10 09:45:35 +0000 | [diff] [blame] | 2062 | (void)ReqTy; |
Chris Lattner | 887ecac | 2011-07-09 18:23:52 +0000 | [diff] [blame] | 2063 | assert(ReqTy && "extractvalue indices invalid!"); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2064 | |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2065 | assert(Agg->getType()->isFirstClassType() && |
| 2066 | "Non-first-class type for constant extractvalue expression"); |
Hal Finkel | b31366d | 2013-07-10 22:51:01 +0000 | [diff] [blame] | 2067 | if (Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs)) |
| 2068 | return FC; |
| 2069 | |
Duncan P. N. Exon Smith | 170eb98 | 2014-08-19 19:45:37 +0000 | [diff] [blame] | 2070 | if (OnlyIfReducedTy == ReqTy) |
| 2071 | return nullptr; |
| 2072 | |
Hal Finkel | b31366d | 2013-07-10 22:51:01 +0000 | [diff] [blame] | 2073 | Constant *ArgVec[] = { Agg }; |
Duncan P. N. Exon Smith | 317c139 | 2014-08-19 16:39:58 +0000 | [diff] [blame] | 2074 | const ConstantExprKeyType Key(Instruction::ExtractValue, ArgVec, 0, 0, Idxs); |
Hal Finkel | b31366d | 2013-07-10 22:51:01 +0000 | [diff] [blame] | 2075 | |
| 2076 | LLVMContextImpl *pImpl = Agg->getContext().pImpl; |
| 2077 | return pImpl->ExprConstants.getOrCreate(ReqTy, Key); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 2078 | } |
| 2079 | |
Chris Lattner | e9b4ad7 | 2011-02-10 07:01:55 +0000 | [diff] [blame] | 2080 | Constant *ConstantExpr::getNeg(Constant *C, bool HasNUW, bool HasNSW) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2081 | assert(C->getType()->isIntOrIntVectorTy() && |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2082 | "Cannot NEG a nonintegral value!"); |
Chris Lattner | e9b4ad7 | 2011-02-10 07:01:55 +0000 | [diff] [blame] | 2083 | return getSub(ConstantFP::getZeroValueForNegation(C->getType()), |
| 2084 | C, HasNUW, HasNSW); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2085 | } |
| 2086 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2087 | Constant *ConstantExpr::getFNeg(Constant *C) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2088 | assert(C->getType()->isFPOrFPVectorTy() && |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2089 | "Cannot FNEG a non-floating-point value!"); |
Chris Lattner | e9b4ad7 | 2011-02-10 07:01:55 +0000 | [diff] [blame] | 2090 | return getFSub(ConstantFP::getZeroValueForNegation(C->getType()), C); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2091 | } |
| 2092 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2093 | Constant *ConstantExpr::getNot(Constant *C) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2094 | assert(C->getType()->isIntOrIntVectorTy() && |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2095 | "Cannot NOT a nonintegral value!"); |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 2096 | return get(Instruction::Xor, C, Constant::getAllOnesValue(C->getType())); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2097 | } |
| 2098 | |
Chris Lattner | e9b4ad7 | 2011-02-10 07:01:55 +0000 | [diff] [blame] | 2099 | Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2, |
| 2100 | bool HasNUW, bool HasNSW) { |
| 2101 | unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) | |
| 2102 | (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0); |
| 2103 | return get(Instruction::Add, C1, C2, Flags); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2104 | } |
| 2105 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2106 | Constant *ConstantExpr::getFAdd(Constant *C1, Constant *C2) { |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2107 | return get(Instruction::FAdd, C1, C2); |
| 2108 | } |
| 2109 | |
Chris Lattner | e9b4ad7 | 2011-02-10 07:01:55 +0000 | [diff] [blame] | 2110 | Constant *ConstantExpr::getSub(Constant *C1, Constant *C2, |
| 2111 | bool HasNUW, bool HasNSW) { |
| 2112 | unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) | |
| 2113 | (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0); |
| 2114 | return get(Instruction::Sub, C1, C2, Flags); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2115 | } |
| 2116 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2117 | Constant *ConstantExpr::getFSub(Constant *C1, Constant *C2) { |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2118 | return get(Instruction::FSub, C1, C2); |
| 2119 | } |
| 2120 | |
Chris Lattner | e9b4ad7 | 2011-02-10 07:01:55 +0000 | [diff] [blame] | 2121 | Constant *ConstantExpr::getMul(Constant *C1, Constant *C2, |
| 2122 | bool HasNUW, bool HasNSW) { |
| 2123 | unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) | |
| 2124 | (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0); |
| 2125 | return get(Instruction::Mul, C1, C2, Flags); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2126 | } |
| 2127 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2128 | Constant *ConstantExpr::getFMul(Constant *C1, Constant *C2) { |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2129 | return get(Instruction::FMul, C1, C2); |
| 2130 | } |
| 2131 | |
Chris Lattner | 0d75eac | 2011-02-09 16:43:07 +0000 | [diff] [blame] | 2132 | Constant *ConstantExpr::getUDiv(Constant *C1, Constant *C2, bool isExact) { |
| 2133 | return get(Instruction::UDiv, C1, C2, |
| 2134 | isExact ? PossiblyExactOperator::IsExact : 0); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2135 | } |
| 2136 | |
Chris Lattner | 0d75eac | 2011-02-09 16:43:07 +0000 | [diff] [blame] | 2137 | Constant *ConstantExpr::getSDiv(Constant *C1, Constant *C2, bool isExact) { |
| 2138 | return get(Instruction::SDiv, C1, C2, |
| 2139 | isExact ? PossiblyExactOperator::IsExact : 0); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2140 | } |
| 2141 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2142 | Constant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) { |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2143 | return get(Instruction::FDiv, C1, C2); |
| 2144 | } |
| 2145 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2146 | Constant *ConstantExpr::getURem(Constant *C1, Constant *C2) { |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2147 | return get(Instruction::URem, C1, C2); |
| 2148 | } |
| 2149 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2150 | Constant *ConstantExpr::getSRem(Constant *C1, Constant *C2) { |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2151 | return get(Instruction::SRem, C1, C2); |
| 2152 | } |
| 2153 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2154 | Constant *ConstantExpr::getFRem(Constant *C1, Constant *C2) { |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2155 | return get(Instruction::FRem, C1, C2); |
| 2156 | } |
| 2157 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2158 | Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) { |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2159 | return get(Instruction::And, C1, C2); |
| 2160 | } |
| 2161 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2162 | Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) { |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2163 | return get(Instruction::Or, C1, C2); |
| 2164 | } |
| 2165 | |
Chris Lattner | a676c0f | 2011-02-07 16:40:21 +0000 | [diff] [blame] | 2166 | Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) { |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2167 | return get(Instruction::Xor, C1, C2); |
| 2168 | } |
| 2169 | |
Chris Lattner | e9b4ad7 | 2011-02-10 07:01:55 +0000 | [diff] [blame] | 2170 | Constant *ConstantExpr::getShl(Constant *C1, Constant *C2, |
| 2171 | bool HasNUW, bool HasNSW) { |
| 2172 | unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) | |
| 2173 | (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0); |
| 2174 | return get(Instruction::Shl, C1, C2, Flags); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2175 | } |
| 2176 | |
Chris Lattner | 0d75eac | 2011-02-09 16:43:07 +0000 | [diff] [blame] | 2177 | Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2, bool isExact) { |
| 2178 | return get(Instruction::LShr, C1, C2, |
| 2179 | isExact ? PossiblyExactOperator::IsExact : 0); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2180 | } |
| 2181 | |
Chris Lattner | 0d75eac | 2011-02-09 16:43:07 +0000 | [diff] [blame] | 2182 | Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2, bool isExact) { |
| 2183 | return get(Instruction::AShr, C1, C2, |
| 2184 | isExact ? PossiblyExactOperator::IsExact : 0); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2185 | } |
| 2186 | |
Duncan Sands | d7aeefe | 2012-06-12 14:33:56 +0000 | [diff] [blame] | 2187 | Constant *ConstantExpr::getBinOpIdentity(unsigned Opcode, Type *Ty) { |
| 2188 | switch (Opcode) { |
| 2189 | default: |
Duncan Sands | 318a89d | 2012-06-13 09:42:13 +0000 | [diff] [blame] | 2190 | // Doesn't have an identity. |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 2191 | return nullptr; |
Duncan Sands | 318a89d | 2012-06-13 09:42:13 +0000 | [diff] [blame] | 2192 | |
Duncan Sands | d7aeefe | 2012-06-12 14:33:56 +0000 | [diff] [blame] | 2193 | case Instruction::Add: |
| 2194 | case Instruction::Or: |
| 2195 | case Instruction::Xor: |
| 2196 | return Constant::getNullValue(Ty); |
| 2197 | |
| 2198 | case Instruction::Mul: |
| 2199 | return ConstantInt::get(Ty, 1); |
| 2200 | |
| 2201 | case Instruction::And: |
| 2202 | return Constant::getAllOnesValue(Ty); |
| 2203 | } |
| 2204 | } |
| 2205 | |
Duncan Sands | 318a89d | 2012-06-13 09:42:13 +0000 | [diff] [blame] | 2206 | Constant *ConstantExpr::getBinOpAbsorber(unsigned Opcode, Type *Ty) { |
| 2207 | switch (Opcode) { |
| 2208 | default: |
| 2209 | // Doesn't have an absorber. |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 2210 | return nullptr; |
Duncan Sands | 318a89d | 2012-06-13 09:42:13 +0000 | [diff] [blame] | 2211 | |
| 2212 | case Instruction::Or: |
| 2213 | return Constant::getAllOnesValue(Ty); |
| 2214 | |
| 2215 | case Instruction::And: |
| 2216 | case Instruction::Mul: |
| 2217 | return Constant::getNullValue(Ty); |
| 2218 | } |
| 2219 | } |
| 2220 | |
Sanjay Patel | 1d0ac7c | 2016-04-29 22:03:27 +0000 | [diff] [blame] | 2221 | /// Remove the constant from the constant table. |
Pete Cooper | 86dd4cf | 2015-06-23 21:55:11 +0000 | [diff] [blame] | 2222 | void ConstantExpr::destroyConstantImpl() { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2223 | getType()->getContext().pImpl->ExprConstants.remove(this); |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 2224 | } |
| 2225 | |
Chris Lattner | 3cd8c56 | 2002-07-30 18:54:25 +0000 | [diff] [blame] | 2226 | const char *ConstantExpr::getOpcodeName() const { |
| 2227 | return Instruction::getOpcodeName(getOpcode()); |
Vikram S. Adve | 4e537b2 | 2002-07-14 23:13:17 +0000 | [diff] [blame] | 2228 | } |
Reid Spencer | 1ebe1ab | 2004-07-17 23:48:33 +0000 | [diff] [blame] | 2229 | |
David Blaikie | 60310f2 | 2015-05-08 00:42:26 +0000 | [diff] [blame] | 2230 | GetElementPtrConstantExpr::GetElementPtrConstantExpr( |
| 2231 | Type *SrcElementTy, Constant *C, ArrayRef<Constant *> IdxList, Type *DestTy) |
| 2232 | : ConstantExpr(DestTy, Instruction::GetElementPtr, |
| 2233 | OperandTraits<GetElementPtrConstantExpr>::op_end(this) - |
| 2234 | (IdxList.size() + 1), |
| 2235 | IdxList.size() + 1), |
Eduard Burtescu | 19eb031 | 2016-01-19 17:28:00 +0000 | [diff] [blame] | 2236 | SrcElementTy(SrcElementTy), |
| 2237 | ResElementTy(GetElementPtrInst::getIndexedType(SrcElementTy, IdxList)) { |
Pete Cooper | eb31b68 | 2015-05-21 22:48:54 +0000 | [diff] [blame] | 2238 | Op<0>() = C; |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 2239 | Use *OperandList = getOperandList(); |
Chris Lattner | a3b94ba | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 2240 | for (unsigned i = 0, E = IdxList.size(); i != E; ++i) |
| 2241 | OperandList[i+1] = IdxList[i]; |
| 2242 | } |
| 2243 | |
David Blaikie | 60310f2 | 2015-05-08 00:42:26 +0000 | [diff] [blame] | 2244 | Type *GetElementPtrConstantExpr::getSourceElementType() const { |
| 2245 | return SrcElementTy; |
| 2246 | } |
| 2247 | |
Eduard Burtescu | 19eb031 | 2016-01-19 17:28:00 +0000 | [diff] [blame] | 2248 | Type *GetElementPtrConstantExpr::getResultElementType() const { |
| 2249 | return ResElementTy; |
| 2250 | } |
| 2251 | |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2252 | //===----------------------------------------------------------------------===// |
| 2253 | // ConstantData* implementations |
| 2254 | |
Chris Lattner | e4f3f10 | 2012-01-24 04:43:41 +0000 | [diff] [blame] | 2255 | Type *ConstantDataSequential::getElementType() const { |
| 2256 | return getType()->getElementType(); |
| 2257 | } |
| 2258 | |
Chris Lattner | 5d4497b | 2012-01-24 09:31:43 +0000 | [diff] [blame] | 2259 | StringRef ConstantDataSequential::getRawDataValues() const { |
Chris Lattner | 00245f4 | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 2260 | return StringRef(DataElements, getNumElements()*getElementByteSize()); |
Chris Lattner | 5d4497b | 2012-01-24 09:31:43 +0000 | [diff] [blame] | 2261 | } |
| 2262 | |
Craig Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 2263 | bool ConstantDataSequential::isElementTypeCompatible(Type *Ty) { |
Justin Bogner | 0ebc860 | 2015-12-08 03:01:16 +0000 | [diff] [blame] | 2264 | if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) return true; |
Craig Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 2265 | if (auto *IT = dyn_cast<IntegerType>(Ty)) { |
Chris Lattner | e4f3f10 | 2012-01-24 04:43:41 +0000 | [diff] [blame] | 2266 | switch (IT->getBitWidth()) { |
| 2267 | case 8: |
| 2268 | case 16: |
| 2269 | case 32: |
| 2270 | case 64: |
| 2271 | return true; |
| 2272 | default: break; |
| 2273 | } |
| 2274 | } |
| 2275 | return false; |
| 2276 | } |
| 2277 | |
Chris Lattner | 00245f4 | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 2278 | unsigned ConstantDataSequential::getNumElements() const { |
Chris Lattner | 8a3df54 | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 2279 | if (ArrayType *AT = dyn_cast<ArrayType>(getType())) |
| 2280 | return AT->getNumElements(); |
Chris Lattner | 8326bd8 | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 2281 | return getType()->getVectorNumElements(); |
Chris Lattner | 00245f4 | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 2282 | } |
| 2283 | |
| 2284 | |
Chris Lattner | e4f3f10 | 2012-01-24 04:43:41 +0000 | [diff] [blame] | 2285 | uint64_t ConstantDataSequential::getElementByteSize() const { |
| 2286 | return getElementType()->getPrimitiveSizeInBits()/8; |
| 2287 | } |
| 2288 | |
Sanjay Patel | 1d0ac7c | 2016-04-29 22:03:27 +0000 | [diff] [blame] | 2289 | /// Return the start of the specified element. |
Chris Lattner | e4f3f10 | 2012-01-24 04:43:41 +0000 | [diff] [blame] | 2290 | const char *ConstantDataSequential::getElementPointer(unsigned Elt) const { |
Chris Lattner | 00245f4 | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 2291 | assert(Elt < getNumElements() && "Invalid Elt"); |
Chris Lattner | e4f3f10 | 2012-01-24 04:43:41 +0000 | [diff] [blame] | 2292 | return DataElements+Elt*getElementByteSize(); |
| 2293 | } |
| 2294 | |
| 2295 | |
Sanjay Patel | 1d0ac7c | 2016-04-29 22:03:27 +0000 | [diff] [blame] | 2296 | /// Return true if the array is empty or all zeros. |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2297 | static bool isAllZeros(StringRef Arr) { |
Benjamin Kramer | af28e7d | 2016-06-26 14:10:56 +0000 | [diff] [blame] | 2298 | for (char I : Arr) |
| 2299 | if (I != 0) |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2300 | return false; |
| 2301 | return true; |
| 2302 | } |
Chris Lattner | 030af79 | 2012-01-24 05:42:11 +0000 | [diff] [blame] | 2303 | |
Sanjay Patel | 1d0ac7c | 2016-04-29 22:03:27 +0000 | [diff] [blame] | 2304 | /// This is the underlying implementation of all of the |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2305 | /// ConstantDataSequential::get methods. They all thunk down to here, providing |
Chris Lattner | f06039b | 2012-01-30 18:19:30 +0000 | [diff] [blame] | 2306 | /// the correct element type. We take the bytes in as a StringRef because |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2307 | /// we *want* an underlying "char*" to avoid TBAA type punning violations. |
| 2308 | Constant *ConstantDataSequential::getImpl(StringRef Elements, Type *Ty) { |
Chris Lattner | 8326bd8 | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 2309 | assert(isElementTypeCompatible(Ty->getSequentialElementType())); |
Chris Lattner | 139822f | 2012-01-24 14:17:05 +0000 | [diff] [blame] | 2310 | // If the elements are all zero or there are no elements, return a CAZ, which |
| 2311 | // is more dense and canonical. |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2312 | if (isAllZeros(Elements)) |
| 2313 | return ConstantAggregateZero::get(Ty); |
| 2314 | |
| 2315 | // Do a lookup to see if we have already formed one of these. |
David Blaikie | 5106ce7 | 2014-11-19 05:49:42 +0000 | [diff] [blame] | 2316 | auto &Slot = |
| 2317 | *Ty->getContext() |
| 2318 | .pImpl->CDSConstants.insert(std::make_pair(Elements, nullptr)) |
| 2319 | .first; |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2320 | |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2321 | // The bucket can point to a linked list of different CDS's that have the same |
| 2322 | // body but different types. For example, 0,0,0,1 could be a 4 element array |
| 2323 | // of i8, or a 1-element array of i32. They'll both end up in the same |
| 2324 | /// StringMap bucket, linked up by their Next pointers. Walk the list. |
David Blaikie | 5106ce7 | 2014-11-19 05:49:42 +0000 | [diff] [blame] | 2325 | ConstantDataSequential **Entry = &Slot.second; |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 2326 | for (ConstantDataSequential *Node = *Entry; Node; |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2327 | Entry = &Node->Next, Node = *Entry) |
| 2328 | if (Node->getType() == Ty) |
| 2329 | return Node; |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2330 | |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2331 | // Okay, we didn't get a hit. Create a node of the right class, link it in, |
| 2332 | // and return it. |
| 2333 | if (isa<ArrayType>(Ty)) |
David Blaikie | 5106ce7 | 2014-11-19 05:49:42 +0000 | [diff] [blame] | 2334 | return *Entry = new ConstantDataArray(Ty, Slot.first().data()); |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2335 | |
| 2336 | assert(isa<VectorType>(Ty)); |
David Blaikie | 5106ce7 | 2014-11-19 05:49:42 +0000 | [diff] [blame] | 2337 | return *Entry = new ConstantDataVector(Ty, Slot.first().data()); |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2338 | } |
| 2339 | |
Pete Cooper | 86dd4cf | 2015-06-23 21:55:11 +0000 | [diff] [blame] | 2340 | void ConstantDataSequential::destroyConstantImpl() { |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2341 | // Remove the constant from the StringMap. |
| 2342 | StringMap<ConstantDataSequential*> &CDSConstants = |
| 2343 | getType()->getContext().pImpl->CDSConstants; |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2344 | |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2345 | StringMap<ConstantDataSequential*>::iterator Slot = |
Chris Lattner | 5d4497b | 2012-01-24 09:31:43 +0000 | [diff] [blame] | 2346 | CDSConstants.find(getRawDataValues()); |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2347 | |
| 2348 | assert(Slot != CDSConstants.end() && "CDS not found in uniquing table"); |
| 2349 | |
| 2350 | ConstantDataSequential **Entry = &Slot->getValue(); |
| 2351 | |
| 2352 | // Remove the entry from the hash table. |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 2353 | if (!(*Entry)->Next) { |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2354 | // If there is only one value in the bucket (common case) it must be this |
| 2355 | // entry, and removing the entry should remove the bucket completely. |
| 2356 | assert((*Entry) == this && "Hash mismatch in ConstantDataSequential"); |
| 2357 | getContext().pImpl->CDSConstants.erase(Slot); |
| 2358 | } else { |
| 2359 | // Otherwise, there are multiple entries linked off the bucket, unlink the |
| 2360 | // node we care about but keep the bucket around. |
| 2361 | for (ConstantDataSequential *Node = *Entry; ; |
| 2362 | Entry = &Node->Next, Node = *Entry) { |
| 2363 | assert(Node && "Didn't find entry in its uniquing hash table!"); |
| 2364 | // If we found our entry, unlink it from the list and we're done. |
| 2365 | if (Node == this) { |
| 2366 | *Entry = Node->Next; |
| 2367 | break; |
| 2368 | } |
| 2369 | } |
| 2370 | } |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2371 | |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2372 | // If we were part of a list, make sure that we don't delete the list that is |
| 2373 | // still owned by the uniquing map. |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 2374 | Next = nullptr; |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2375 | } |
| 2376 | |
| 2377 | /// get() constructors - Return a constant with array type with an element |
| 2378 | /// count and element type matching the ArrayRef passed in. Note that this |
| 2379 | /// can return a ConstantAggregateZero object. |
Chris Lattner | 2068393 | 2012-01-24 14:04:40 +0000 | [diff] [blame] | 2380 | Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint8_t> Elts) { |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2381 | Type *Ty = ArrayType::get(Type::getInt8Ty(Context), Elts.size()); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2382 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 2383 | return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*1), Ty); |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2384 | } |
Chris Lattner | 2068393 | 2012-01-24 14:04:40 +0000 | [diff] [blame] | 2385 | Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint16_t> Elts){ |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2386 | Type *Ty = ArrayType::get(Type::getInt16Ty(Context), Elts.size()); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2387 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 2388 | return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*2), Ty); |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2389 | } |
Chris Lattner | 2068393 | 2012-01-24 14:04:40 +0000 | [diff] [blame] | 2390 | Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint32_t> Elts){ |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2391 | Type *Ty = ArrayType::get(Type::getInt32Ty(Context), Elts.size()); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2392 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 2393 | return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*4), Ty); |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2394 | } |
Chris Lattner | 2068393 | 2012-01-24 14:04:40 +0000 | [diff] [blame] | 2395 | Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<uint64_t> Elts){ |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2396 | Type *Ty = ArrayType::get(Type::getInt64Ty(Context), Elts.size()); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2397 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 2398 | return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*8), Ty); |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2399 | } |
Chris Lattner | 2068393 | 2012-01-24 14:04:40 +0000 | [diff] [blame] | 2400 | Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<float> Elts) { |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2401 | Type *Ty = ArrayType::get(Type::getFloatTy(Context), Elts.size()); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2402 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 2403 | return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*4), Ty); |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2404 | } |
Chris Lattner | 2068393 | 2012-01-24 14:04:40 +0000 | [diff] [blame] | 2405 | Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef<double> Elts) { |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2406 | Type *Ty = ArrayType::get(Type::getDoubleTy(Context), Elts.size()); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2407 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
Rafael Espindola | 8c97e19 | 2015-02-19 16:08:20 +0000 | [diff] [blame] | 2408 | return getImpl(StringRef(const_cast<char *>(Data), Elts.size() * 8), Ty); |
| 2409 | } |
| 2410 | |
| 2411 | /// getFP() constructors - Return a constant with array type with an element |
| 2412 | /// count and element type of float with precision matching the number of |
| 2413 | /// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits, |
| 2414 | /// double for 64bits) Note that this can return a ConstantAggregateZero |
| 2415 | /// object. |
| 2416 | Constant *ConstantDataArray::getFP(LLVMContext &Context, |
| 2417 | ArrayRef<uint16_t> Elts) { |
Justin Bogner | b7389d671 | 2015-12-09 21:21:07 +0000 | [diff] [blame] | 2418 | Type *Ty = ArrayType::get(Type::getHalfTy(Context), Elts.size()); |
Rafael Espindola | 8c97e19 | 2015-02-19 16:08:20 +0000 | [diff] [blame] | 2419 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 2420 | return getImpl(StringRef(const_cast<char *>(Data), Elts.size() * 2), Ty); |
| 2421 | } |
| 2422 | Constant *ConstantDataArray::getFP(LLVMContext &Context, |
| 2423 | ArrayRef<uint32_t> Elts) { |
| 2424 | Type *Ty = ArrayType::get(Type::getFloatTy(Context), Elts.size()); |
| 2425 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 2426 | return getImpl(StringRef(const_cast<char *>(Data), Elts.size() * 4), Ty); |
| 2427 | } |
| 2428 | Constant *ConstantDataArray::getFP(LLVMContext &Context, |
| 2429 | ArrayRef<uint64_t> Elts) { |
| 2430 | Type *Ty = ArrayType::get(Type::getDoubleTy(Context), Elts.size()); |
| 2431 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 2432 | return getImpl(StringRef(const_cast<char *>(Data), Elts.size() * 8), Ty); |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2433 | } |
| 2434 | |
Chris Lattner | 2068393 | 2012-01-24 14:04:40 +0000 | [diff] [blame] | 2435 | Constant *ConstantDataArray::getString(LLVMContext &Context, |
| 2436 | StringRef Str, bool AddNull) { |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2437 | if (!AddNull) { |
| 2438 | const uint8_t *Data = reinterpret_cast<const uint8_t *>(Str.data()); |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 2439 | return get(Context, makeArrayRef(const_cast<uint8_t *>(Data), |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2440 | Str.size())); |
| 2441 | } |
| 2442 | |
Chris Lattner | 2068393 | 2012-01-24 14:04:40 +0000 | [diff] [blame] | 2443 | SmallVector<uint8_t, 64> ElementVals; |
| 2444 | ElementVals.append(Str.begin(), Str.end()); |
| 2445 | ElementVals.push_back(0); |
| 2446 | return get(Context, ElementVals); |
| 2447 | } |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2448 | |
| 2449 | /// get() constructors - Return a constant with vector type with an element |
| 2450 | /// count and element type matching the ArrayRef passed in. Note that this |
| 2451 | /// can return a ConstantAggregateZero object. |
Chris Lattner | 2068393 | 2012-01-24 14:04:40 +0000 | [diff] [blame] | 2452 | Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint8_t> Elts){ |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2453 | Type *Ty = VectorType::get(Type::getInt8Ty(Context), Elts.size()); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2454 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 2455 | return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*1), Ty); |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2456 | } |
Chris Lattner | 2068393 | 2012-01-24 14:04:40 +0000 | [diff] [blame] | 2457 | Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint16_t> Elts){ |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2458 | Type *Ty = VectorType::get(Type::getInt16Ty(Context), Elts.size()); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2459 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 2460 | return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*2), Ty); |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2461 | } |
Chris Lattner | 2068393 | 2012-01-24 14:04:40 +0000 | [diff] [blame] | 2462 | Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint32_t> Elts){ |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2463 | Type *Ty = VectorType::get(Type::getInt32Ty(Context), Elts.size()); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2464 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 2465 | return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*4), Ty); |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2466 | } |
Chris Lattner | 2068393 | 2012-01-24 14:04:40 +0000 | [diff] [blame] | 2467 | Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint64_t> Elts){ |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2468 | Type *Ty = VectorType::get(Type::getInt64Ty(Context), Elts.size()); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2469 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 2470 | return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*8), Ty); |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2471 | } |
Chris Lattner | 2068393 | 2012-01-24 14:04:40 +0000 | [diff] [blame] | 2472 | Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<float> Elts) { |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2473 | Type *Ty = VectorType::get(Type::getFloatTy(Context), Elts.size()); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2474 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 2475 | return getImpl(StringRef(const_cast<char *>(Data), Elts.size()*4), Ty); |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2476 | } |
Chris Lattner | 2068393 | 2012-01-24 14:04:40 +0000 | [diff] [blame] | 2477 | Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<double> Elts) { |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2478 | Type *Ty = VectorType::get(Type::getDoubleTy(Context), Elts.size()); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2479 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
Rafael Espindola | 8c97e19 | 2015-02-19 16:08:20 +0000 | [diff] [blame] | 2480 | return getImpl(StringRef(const_cast<char *>(Data), Elts.size() * 8), Ty); |
| 2481 | } |
| 2482 | |
| 2483 | /// getFP() constructors - Return a constant with vector type with an element |
| 2484 | /// count and element type of float with the precision matching the number of |
| 2485 | /// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits, |
| 2486 | /// double for 64bits) Note that this can return a ConstantAggregateZero |
| 2487 | /// object. |
| 2488 | Constant *ConstantDataVector::getFP(LLVMContext &Context, |
| 2489 | ArrayRef<uint16_t> Elts) { |
| 2490 | Type *Ty = VectorType::get(Type::getHalfTy(Context), Elts.size()); |
| 2491 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 2492 | return getImpl(StringRef(const_cast<char *>(Data), Elts.size() * 2), Ty); |
| 2493 | } |
| 2494 | Constant *ConstantDataVector::getFP(LLVMContext &Context, |
| 2495 | ArrayRef<uint32_t> Elts) { |
| 2496 | Type *Ty = VectorType::get(Type::getFloatTy(Context), Elts.size()); |
| 2497 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 2498 | return getImpl(StringRef(const_cast<char *>(Data), Elts.size() * 4), Ty); |
| 2499 | } |
| 2500 | Constant *ConstantDataVector::getFP(LLVMContext &Context, |
| 2501 | ArrayRef<uint64_t> Elts) { |
| 2502 | Type *Ty = VectorType::get(Type::getDoubleTy(Context), Elts.size()); |
| 2503 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 2504 | return getImpl(StringRef(const_cast<char *>(Data), Elts.size() * 8), Ty); |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2505 | } |
| 2506 | |
Chris Lattner | e9eed29 | 2012-01-25 05:19:54 +0000 | [diff] [blame] | 2507 | Constant *ConstantDataVector::getSplat(unsigned NumElts, Constant *V) { |
| 2508 | assert(isElementTypeCompatible(V->getType()) && |
| 2509 | "Element type not compatible with ConstantData"); |
| 2510 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 2511 | if (CI->getType()->isIntegerTy(8)) { |
| 2512 | SmallVector<uint8_t, 16> Elts(NumElts, CI->getZExtValue()); |
| 2513 | return get(V->getContext(), Elts); |
| 2514 | } |
| 2515 | if (CI->getType()->isIntegerTy(16)) { |
| 2516 | SmallVector<uint16_t, 16> Elts(NumElts, CI->getZExtValue()); |
| 2517 | return get(V->getContext(), Elts); |
| 2518 | } |
| 2519 | if (CI->getType()->isIntegerTy(32)) { |
| 2520 | SmallVector<uint32_t, 16> Elts(NumElts, CI->getZExtValue()); |
| 2521 | return get(V->getContext(), Elts); |
| 2522 | } |
| 2523 | assert(CI->getType()->isIntegerTy(64) && "Unsupported ConstantData type"); |
| 2524 | SmallVector<uint64_t, 16> Elts(NumElts, CI->getZExtValue()); |
| 2525 | return get(V->getContext(), Elts); |
| 2526 | } |
| 2527 | |
Chris Lattner | 978fe0c | 2012-01-30 06:21:21 +0000 | [diff] [blame] | 2528 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) { |
Justin Bogner | 0ebc860 | 2015-12-08 03:01:16 +0000 | [diff] [blame] | 2529 | if (CFP->getType()->isHalfTy()) { |
| 2530 | SmallVector<uint16_t, 16> Elts( |
| 2531 | NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue()); |
| 2532 | return getFP(V->getContext(), Elts); |
| 2533 | } |
Chris Lattner | 978fe0c | 2012-01-30 06:21:21 +0000 | [diff] [blame] | 2534 | if (CFP->getType()->isFloatTy()) { |
Rafael Espindola | 8c97e19 | 2015-02-19 16:08:20 +0000 | [diff] [blame] | 2535 | SmallVector<uint32_t, 16> Elts( |
| 2536 | NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue()); |
| 2537 | return getFP(V->getContext(), Elts); |
Chris Lattner | 978fe0c | 2012-01-30 06:21:21 +0000 | [diff] [blame] | 2538 | } |
| 2539 | if (CFP->getType()->isDoubleTy()) { |
Rafael Espindola | 8c97e19 | 2015-02-19 16:08:20 +0000 | [diff] [blame] | 2540 | SmallVector<uint64_t, 16> Elts( |
| 2541 | NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue()); |
| 2542 | return getFP(V->getContext(), Elts); |
Chris Lattner | 978fe0c | 2012-01-30 06:21:21 +0000 | [diff] [blame] | 2543 | } |
Chris Lattner | e9eed29 | 2012-01-25 05:19:54 +0000 | [diff] [blame] | 2544 | } |
Chris Lattner | 978fe0c | 2012-01-30 06:21:21 +0000 | [diff] [blame] | 2545 | return ConstantVector::getSplat(NumElts, V); |
Chris Lattner | e9eed29 | 2012-01-25 05:19:54 +0000 | [diff] [blame] | 2546 | } |
| 2547 | |
| 2548 | |
Chris Lattner | e4f3f10 | 2012-01-24 04:43:41 +0000 | [diff] [blame] | 2549 | uint64_t ConstantDataSequential::getElementAsInteger(unsigned Elt) const { |
| 2550 | assert(isa<IntegerType>(getElementType()) && |
| 2551 | "Accessor can only be used when element is an integer"); |
| 2552 | const char *EltPtr = getElementPointer(Elt); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2553 | |
Chris Lattner | e4f3f10 | 2012-01-24 04:43:41 +0000 | [diff] [blame] | 2554 | // The data is stored in host byte order, make sure to cast back to the right |
| 2555 | // type to load with the right endianness. |
Chris Lattner | 8326bd8 | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 2556 | switch (getElementType()->getIntegerBitWidth()) { |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2557 | default: llvm_unreachable("Invalid bitwidth for CDS"); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2558 | case 8: |
| 2559 | return *const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(EltPtr)); |
| 2560 | case 16: |
| 2561 | return *const_cast<uint16_t *>(reinterpret_cast<const uint16_t *>(EltPtr)); |
| 2562 | case 32: |
| 2563 | return *const_cast<uint32_t *>(reinterpret_cast<const uint32_t *>(EltPtr)); |
| 2564 | case 64: |
| 2565 | return *const_cast<uint64_t *>(reinterpret_cast<const uint64_t *>(EltPtr)); |
Chris Lattner | e4f3f10 | 2012-01-24 04:43:41 +0000 | [diff] [blame] | 2566 | } |
| 2567 | } |
| 2568 | |
Chris Lattner | e4f3f10 | 2012-01-24 04:43:41 +0000 | [diff] [blame] | 2569 | APFloat ConstantDataSequential::getElementAsAPFloat(unsigned Elt) const { |
| 2570 | const char *EltPtr = getElementPointer(Elt); |
| 2571 | |
| 2572 | switch (getElementType()->getTypeID()) { |
Nick Lewycky | ff50962 | 2012-01-25 03:20:12 +0000 | [diff] [blame] | 2573 | default: |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2574 | llvm_unreachable("Accessor can only be used when element is float/double!"); |
Justin Bogner | 0ebc860 | 2015-12-08 03:01:16 +0000 | [diff] [blame] | 2575 | case Type::HalfTyID: { |
| 2576 | auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2577 | return APFloat(APFloat::IEEEhalf(), APInt(16, EltVal)); |
Justin Bogner | 0ebc860 | 2015-12-08 03:01:16 +0000 | [diff] [blame] | 2578 | } |
Benjamin Kramer | 7af984b | 2015-02-20 15:11:55 +0000 | [diff] [blame] | 2579 | case Type::FloatTyID: { |
| 2580 | auto EltVal = *reinterpret_cast<const uint32_t *>(EltPtr); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2581 | return APFloat(APFloat::IEEEsingle(), APInt(32, EltVal)); |
Benjamin Kramer | 7af984b | 2015-02-20 15:11:55 +0000 | [diff] [blame] | 2582 | } |
| 2583 | case Type::DoubleTyID: { |
| 2584 | auto EltVal = *reinterpret_cast<const uint64_t *>(EltPtr); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2585 | return APFloat(APFloat::IEEEdouble(), APInt(64, EltVal)); |
Chris Lattner | e4f3f10 | 2012-01-24 04:43:41 +0000 | [diff] [blame] | 2586 | } |
Benjamin Kramer | 7af984b | 2015-02-20 15:11:55 +0000 | [diff] [blame] | 2587 | } |
Chris Lattner | e4f3f10 | 2012-01-24 04:43:41 +0000 | [diff] [blame] | 2588 | } |
| 2589 | |
Chris Lattner | e4f3f10 | 2012-01-24 04:43:41 +0000 | [diff] [blame] | 2590 | float ConstantDataSequential::getElementAsFloat(unsigned Elt) const { |
| 2591 | assert(getElementType()->isFloatTy() && |
| 2592 | "Accessor can only be used when element is a 'float'"); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2593 | const float *EltPtr = reinterpret_cast<const float *>(getElementPointer(Elt)); |
| 2594 | return *const_cast<float *>(EltPtr); |
Chris Lattner | e4f3f10 | 2012-01-24 04:43:41 +0000 | [diff] [blame] | 2595 | } |
| 2596 | |
Chris Lattner | e4f3f10 | 2012-01-24 04:43:41 +0000 | [diff] [blame] | 2597 | double ConstantDataSequential::getElementAsDouble(unsigned Elt) const { |
| 2598 | assert(getElementType()->isDoubleTy() && |
| 2599 | "Accessor can only be used when element is a 'float'"); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2600 | const double *EltPtr = |
| 2601 | reinterpret_cast<const double *>(getElementPointer(Elt)); |
| 2602 | return *const_cast<double *>(EltPtr); |
Chris Lattner | e4f3f10 | 2012-01-24 04:43:41 +0000 | [diff] [blame] | 2603 | } |
| 2604 | |
Chris Lattner | e4f3f10 | 2012-01-24 04:43:41 +0000 | [diff] [blame] | 2605 | Constant *ConstantDataSequential::getElementAsConstant(unsigned Elt) const { |
Justin Bogner | 0ebc860 | 2015-12-08 03:01:16 +0000 | [diff] [blame] | 2606 | if (getElementType()->isHalfTy() || getElementType()->isFloatTy() || |
| 2607 | getElementType()->isDoubleTy()) |
Chris Lattner | e4f3f10 | 2012-01-24 04:43:41 +0000 | [diff] [blame] | 2608 | return ConstantFP::get(getContext(), getElementAsAPFloat(Elt)); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2609 | |
Chris Lattner | e4f3f10 | 2012-01-24 04:43:41 +0000 | [diff] [blame] | 2610 | return ConstantInt::get(getElementType(), getElementAsInteger(Elt)); |
| 2611 | } |
| 2612 | |
Matthias Braun | 50ec0b5 | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 2613 | bool ConstantDataSequential::isString(unsigned CharSize) const { |
| 2614 | return isa<ArrayType>(getType()) && getElementType()->isIntegerTy(CharSize); |
Chris Lattner | 5dd4d87 | 2012-01-24 09:01:07 +0000 | [diff] [blame] | 2615 | } |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2616 | |
Chris Lattner | 5dd4d87 | 2012-01-24 09:01:07 +0000 | [diff] [blame] | 2617 | bool ConstantDataSequential::isCString() const { |
| 2618 | if (!isString()) |
| 2619 | return false; |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2620 | |
Chris Lattner | 5dd4d87 | 2012-01-24 09:01:07 +0000 | [diff] [blame] | 2621 | StringRef Str = getAsString(); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2622 | |
Chris Lattner | 5dd4d87 | 2012-01-24 09:01:07 +0000 | [diff] [blame] | 2623 | // The last value must be nul. |
| 2624 | if (Str.back() != 0) return false; |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2625 | |
Chris Lattner | 5dd4d87 | 2012-01-24 09:01:07 +0000 | [diff] [blame] | 2626 | // Other elements must be non-nul. |
| 2627 | return Str.drop_back().find(0) == StringRef::npos; |
| 2628 | } |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 2629 | |
Chris Lattner | f14a67f | 2012-01-26 02:31:22 +0000 | [diff] [blame] | 2630 | Constant *ConstantDataVector::getSplatValue() const { |
| 2631 | const char *Base = getRawDataValues().data(); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2632 | |
Chris Lattner | f14a67f | 2012-01-26 02:31:22 +0000 | [diff] [blame] | 2633 | // Compare elements 1+ to the 0'th element. |
| 2634 | unsigned EltSize = getElementByteSize(); |
| 2635 | for (unsigned i = 1, e = getNumElements(); i != e; ++i) |
| 2636 | if (memcmp(Base, Base+i*EltSize, EltSize)) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 2637 | return nullptr; |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2638 | |
Chris Lattner | f14a67f | 2012-01-26 02:31:22 +0000 | [diff] [blame] | 2639 | // If they're all the same, return the 0th one as a representative. |
| 2640 | return getElementAsConstant(0); |
| 2641 | } |
Chris Lattner | a3b94ba | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 2642 | |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2643 | //===----------------------------------------------------------------------===// |
Pete Cooper | 5815b1f | 2015-06-24 18:55:24 +0000 | [diff] [blame] | 2644 | // handleOperandChange implementations |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2645 | |
Pete Cooper | 5815b1f | 2015-06-24 18:55:24 +0000 | [diff] [blame] | 2646 | /// Update this constant array to change uses of |
Chris Lattner | 913849b | 2007-08-21 00:55:23 +0000 | [diff] [blame] | 2647 | /// 'From' to be uses of 'To'. This must update the uniquing data structures |
| 2648 | /// etc. |
| 2649 | /// |
| 2650 | /// Note that we intentionally replace all uses of From with To here. Consider |
| 2651 | /// a large array that uses 'From' 1000 times. By handling this case all here, |
Pete Cooper | 5815b1f | 2015-06-24 18:55:24 +0000 | [diff] [blame] | 2652 | /// ConstantArray::handleOperandChange is only invoked once, and that |
Chris Lattner | 913849b | 2007-08-21 00:55:23 +0000 | [diff] [blame] | 2653 | /// single invocation handles all 1000 uses. Handling them one at a time would |
| 2654 | /// work, but would be really slow because it would have to unique each updated |
| 2655 | /// array instance. |
Chris Lattner | 31b132c | 2009-10-28 00:01:44 +0000 | [diff] [blame] | 2656 | /// |
Mehdi Amini | 8914d29 | 2016-02-10 22:47:15 +0000 | [diff] [blame] | 2657 | void Constant::handleOperandChange(Value *From, Value *To) { |
Pete Cooper | 5815b1f | 2015-06-24 18:55:24 +0000 | [diff] [blame] | 2658 | Value *Replacement = nullptr; |
| 2659 | switch (getValueID()) { |
| 2660 | default: |
| 2661 | llvm_unreachable("Not a constant!"); |
| 2662 | #define HANDLE_CONSTANT(Name) \ |
| 2663 | case Value::Name##Val: \ |
Mehdi Amini | 8914d29 | 2016-02-10 22:47:15 +0000 | [diff] [blame] | 2664 | Replacement = cast<Name>(this)->handleOperandChangeImpl(From, To); \ |
Pete Cooper | 5815b1f | 2015-06-24 18:55:24 +0000 | [diff] [blame] | 2665 | break; |
| 2666 | #include "llvm/IR/Value.def" |
| 2667 | } |
| 2668 | |
| 2669 | // If handleOperandChangeImpl returned nullptr, then it handled |
| 2670 | // replacing itself and we don't want to delete or replace anything else here. |
| 2671 | if (!Replacement) |
| 2672 | return; |
| 2673 | |
Duncan P. N. Exon Smith | 317c139 | 2014-08-19 16:39:58 +0000 | [diff] [blame] | 2674 | // I do need to replace this with an existing value. |
| 2675 | assert(Replacement != this && "I didn't contain From!"); |
| 2676 | |
| 2677 | // Everyone using this now uses the replacement. |
| 2678 | replaceAllUsesWith(Replacement); |
| 2679 | |
| 2680 | // Delete the old constant! |
| 2681 | destroyConstant(); |
| 2682 | } |
| 2683 | |
Mehdi Amini | 8914d29 | 2016-02-10 22:47:15 +0000 | [diff] [blame] | 2684 | Value *ConstantArray::handleOperandChangeImpl(Value *From, Value *To) { |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 2685 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); |
| 2686 | Constant *ToC = cast<Constant>(To); |
| 2687 | |
Talin | 46e9b44 | 2012-02-05 20:54:10 +0000 | [diff] [blame] | 2688 | SmallVector<Constant*, 8> Values; |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 2689 | Values.reserve(getNumOperands()); // Build replacement array. |
| 2690 | |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2691 | // Fill values with the modified operands of the constant array. Also, |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 2692 | // compute whether this turns into an all-zeros array. |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 2693 | unsigned NumUpdated = 0; |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2694 | |
Chris Lattner | f14a67f | 2012-01-26 02:31:22 +0000 | [diff] [blame] | 2695 | // Keep track of whether all the values in the array are "ToC". |
| 2696 | bool AllSame = true; |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 2697 | Use *OperandList = getOperandList(); |
Mehdi Amini | 8914d29 | 2016-02-10 22:47:15 +0000 | [diff] [blame] | 2698 | unsigned OperandNo = 0; |
Chris Lattner | f14a67f | 2012-01-26 02:31:22 +0000 | [diff] [blame] | 2699 | for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { |
| 2700 | Constant *Val = cast<Constant>(O->get()); |
| 2701 | if (Val == From) { |
Mehdi Amini | 8914d29 | 2016-02-10 22:47:15 +0000 | [diff] [blame] | 2702 | OperandNo = (O - OperandList); |
Chris Lattner | f14a67f | 2012-01-26 02:31:22 +0000 | [diff] [blame] | 2703 | Val = ToC; |
| 2704 | ++NumUpdated; |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 2705 | } |
Chris Lattner | f14a67f | 2012-01-26 02:31:22 +0000 | [diff] [blame] | 2706 | Values.push_back(Val); |
Talin | 46e9b44 | 2012-02-05 20:54:10 +0000 | [diff] [blame] | 2707 | AllSame &= Val == ToC; |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 2708 | } |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2709 | |
Pete Cooper | 5815b1f | 2015-06-24 18:55:24 +0000 | [diff] [blame] | 2710 | if (AllSame && ToC->isNullValue()) |
| 2711 | return ConstantAggregateZero::get(getType()); |
| 2712 | |
| 2713 | if (AllSame && isa<UndefValue>(ToC)) |
| 2714 | return UndefValue::get(getType()); |
Aaron Ballman | e4b91dc | 2014-08-19 14:59:02 +0000 | [diff] [blame] | 2715 | |
Duncan P. N. Exon Smith | 317c139 | 2014-08-19 16:39:58 +0000 | [diff] [blame] | 2716 | // Check for any other type of constant-folding. |
Pete Cooper | 5815b1f | 2015-06-24 18:55:24 +0000 | [diff] [blame] | 2717 | if (Constant *C = getImpl(getType(), Values)) |
| 2718 | return C; |
Aaron Ballman | e4b91dc | 2014-08-19 14:59:02 +0000 | [diff] [blame] | 2719 | |
Duncan P. N. Exon Smith | 909620a | 2014-08-19 19:13:30 +0000 | [diff] [blame] | 2720 | // Update to the new value. |
Pete Cooper | 5815b1f | 2015-06-24 18:55:24 +0000 | [diff] [blame] | 2721 | return getContext().pImpl->ArrayConstants.replaceOperandsInPlace( |
Mehdi Amini | 8914d29 | 2016-02-10 22:47:15 +0000 | [diff] [blame] | 2722 | Values, this, From, ToC, NumUpdated, OperandNo); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2723 | } |
| 2724 | |
Mehdi Amini | 8914d29 | 2016-02-10 22:47:15 +0000 | [diff] [blame] | 2725 | Value *ConstantStruct::handleOperandChangeImpl(Value *From, Value *To) { |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 2726 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); |
| 2727 | Constant *ToC = cast<Constant>(To); |
| 2728 | |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 2729 | Use *OperandList = getOperandList(); |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 2730 | |
Talin | 46e9b44 | 2012-02-05 20:54:10 +0000 | [diff] [blame] | 2731 | SmallVector<Constant*, 8> Values; |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 2732 | Values.reserve(getNumOperands()); // Build replacement struct. |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2733 | |
| 2734 | // Fill values with the modified operands of the constant struct. Also, |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 2735 | // compute whether this turns into an all-zeros struct. |
Mehdi Amini | 8914d29 | 2016-02-10 22:47:15 +0000 | [diff] [blame] | 2736 | unsigned NumUpdated = 0; |
| 2737 | bool AllSame = true; |
| 2738 | unsigned OperandNo = 0; |
| 2739 | for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O) { |
| 2740 | Constant *Val = cast<Constant>(O->get()); |
| 2741 | if (Val == From) { |
| 2742 | OperandNo = (O - OperandList); |
| 2743 | Val = ToC; |
| 2744 | ++NumUpdated; |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 2745 | } |
Mehdi Amini | 8914d29 | 2016-02-10 22:47:15 +0000 | [diff] [blame] | 2746 | Values.push_back(Val); |
| 2747 | AllSame &= Val == ToC; |
Owen Anderson | 45308b5 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 2748 | } |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2749 | |
Mehdi Amini | 8914d29 | 2016-02-10 22:47:15 +0000 | [diff] [blame] | 2750 | if (AllSame && ToC->isNullValue()) |
Pete Cooper | 5815b1f | 2015-06-24 18:55:24 +0000 | [diff] [blame] | 2751 | return ConstantAggregateZero::get(getType()); |
| 2752 | |
Mehdi Amini | 8914d29 | 2016-02-10 22:47:15 +0000 | [diff] [blame] | 2753 | if (AllSame && isa<UndefValue>(ToC)) |
Pete Cooper | 5815b1f | 2015-06-24 18:55:24 +0000 | [diff] [blame] | 2754 | return UndefValue::get(getType()); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2755 | |
Duncan P. N. Exon Smith | 317c139 | 2014-08-19 16:39:58 +0000 | [diff] [blame] | 2756 | // Update to the new value. |
Pete Cooper | 5815b1f | 2015-06-24 18:55:24 +0000 | [diff] [blame] | 2757 | return getContext().pImpl->StructConstants.replaceOperandsInPlace( |
Mehdi Amini | 8914d29 | 2016-02-10 22:47:15 +0000 | [diff] [blame] | 2758 | Values, this, From, ToC, NumUpdated, OperandNo); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2759 | } |
| 2760 | |
Mehdi Amini | 8914d29 | 2016-02-10 22:47:15 +0000 | [diff] [blame] | 2761 | Value *ConstantVector::handleOperandChangeImpl(Value *From, Value *To) { |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2762 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); |
Duncan P. N. Exon Smith | 317c139 | 2014-08-19 16:39:58 +0000 | [diff] [blame] | 2763 | Constant *ToC = cast<Constant>(To); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2764 | |
Chris Lattner | a474bb2 | 2012-01-26 20:40:56 +0000 | [diff] [blame] | 2765 | SmallVector<Constant*, 8> Values; |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2766 | Values.reserve(getNumOperands()); // Build replacement array... |
Duncan P. N. Exon Smith | 317c139 | 2014-08-19 16:39:58 +0000 | [diff] [blame] | 2767 | unsigned NumUpdated = 0; |
Mehdi Amini | 8914d29 | 2016-02-10 22:47:15 +0000 | [diff] [blame] | 2768 | unsigned OperandNo = 0; |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2769 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 2770 | Constant *Val = getOperand(i); |
Duncan P. N. Exon Smith | 317c139 | 2014-08-19 16:39:58 +0000 | [diff] [blame] | 2771 | if (Val == From) { |
Mehdi Amini | 8914d29 | 2016-02-10 22:47:15 +0000 | [diff] [blame] | 2772 | OperandNo = i; |
Duncan P. N. Exon Smith | 317c139 | 2014-08-19 16:39:58 +0000 | [diff] [blame] | 2773 | ++NumUpdated; |
| 2774 | Val = ToC; |
| 2775 | } |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2776 | Values.push_back(Val); |
| 2777 | } |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2778 | |
Pete Cooper | 5815b1f | 2015-06-24 18:55:24 +0000 | [diff] [blame] | 2779 | if (Constant *C = getImpl(Values)) |
| 2780 | return C; |
Duncan P. N. Exon Smith | 687744d | 2014-08-19 02:24:46 +0000 | [diff] [blame] | 2781 | |
Duncan P. N. Exon Smith | 909620a | 2014-08-19 19:13:30 +0000 | [diff] [blame] | 2782 | // Update to the new value. |
Pete Cooper | 5815b1f | 2015-06-24 18:55:24 +0000 | [diff] [blame] | 2783 | return getContext().pImpl->VectorConstants.replaceOperandsInPlace( |
Mehdi Amini | 8914d29 | 2016-02-10 22:47:15 +0000 | [diff] [blame] | 2784 | Values, this, From, ToC, NumUpdated, OperandNo); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2785 | } |
| 2786 | |
Mehdi Amini | 8914d29 | 2016-02-10 22:47:15 +0000 | [diff] [blame] | 2787 | Value *ConstantExpr::handleOperandChangeImpl(Value *From, Value *ToV) { |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2788 | assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!"); |
| 2789 | Constant *To = cast<Constant>(ToV); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2790 | |
Chris Lattner | 37e3835 | 2012-01-26 20:37:11 +0000 | [diff] [blame] | 2791 | SmallVector<Constant*, 8> NewOps; |
Duncan P. N. Exon Smith | 33de00c | 2014-08-19 20:03:35 +0000 | [diff] [blame] | 2792 | unsigned NumUpdated = 0; |
Mehdi Amini | 8914d29 | 2016-02-10 22:47:15 +0000 | [diff] [blame] | 2793 | unsigned OperandNo = 0; |
Chris Lattner | 37e3835 | 2012-01-26 20:37:11 +0000 | [diff] [blame] | 2794 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 2795 | Constant *Op = getOperand(i); |
Duncan P. N. Exon Smith | 33de00c | 2014-08-19 20:03:35 +0000 | [diff] [blame] | 2796 | if (Op == From) { |
Mehdi Amini | 8914d29 | 2016-02-10 22:47:15 +0000 | [diff] [blame] | 2797 | OperandNo = i; |
Duncan P. N. Exon Smith | 33de00c | 2014-08-19 20:03:35 +0000 | [diff] [blame] | 2798 | ++NumUpdated; |
| 2799 | Op = To; |
| 2800 | } |
| 2801 | NewOps.push_back(Op); |
Chris Lattner | c4062ba | 2005-10-03 21:58:36 +0000 | [diff] [blame] | 2802 | } |
Duncan P. N. Exon Smith | 33de00c | 2014-08-19 20:03:35 +0000 | [diff] [blame] | 2803 | assert(NumUpdated && "I didn't contain From!"); |
Galina Kistanova | fc25990 | 2012-07-13 01:25:27 +0000 | [diff] [blame] | 2804 | |
Pete Cooper | 5815b1f | 2015-06-24 18:55:24 +0000 | [diff] [blame] | 2805 | if (Constant *C = getWithOperands(NewOps, getType(), true)) |
| 2806 | return C; |
Duncan P. N. Exon Smith | 317c139 | 2014-08-19 16:39:58 +0000 | [diff] [blame] | 2807 | |
Duncan P. N. Exon Smith | 33de00c | 2014-08-19 20:03:35 +0000 | [diff] [blame] | 2808 | // Update to the new value. |
Pete Cooper | 5815b1f | 2015-06-24 18:55:24 +0000 | [diff] [blame] | 2809 | return getContext().pImpl->ExprConstants.replaceOperandsInPlace( |
Mehdi Amini | 8914d29 | 2016-02-10 22:47:15 +0000 | [diff] [blame] | 2810 | NewOps, this, From, To, NumUpdated, OperandNo); |
Duncan P. N. Exon Smith | 317c139 | 2014-08-19 16:39:58 +0000 | [diff] [blame] | 2811 | } |
| 2812 | |
James Molloy | ce54568 | 2012-11-17 17:56:30 +0000 | [diff] [blame] | 2813 | Instruction *ConstantExpr::getAsInstruction() { |
Benjamin Kramer | 5fbfe2f | 2015-02-28 13:20:15 +0000 | [diff] [blame] | 2814 | SmallVector<Value *, 4> ValueOperands(op_begin(), op_end()); |
James Molloy | ce54568 | 2012-11-17 17:56:30 +0000 | [diff] [blame] | 2815 | ArrayRef<Value*> Ops(ValueOperands); |
| 2816 | |
| 2817 | switch (getOpcode()) { |
| 2818 | case Instruction::Trunc: |
| 2819 | case Instruction::ZExt: |
| 2820 | case Instruction::SExt: |
| 2821 | case Instruction::FPTrunc: |
| 2822 | case Instruction::FPExt: |
| 2823 | case Instruction::UIToFP: |
| 2824 | case Instruction::SIToFP: |
| 2825 | case Instruction::FPToUI: |
| 2826 | case Instruction::FPToSI: |
| 2827 | case Instruction::PtrToInt: |
| 2828 | case Instruction::IntToPtr: |
| 2829 | case Instruction::BitCast: |
Eli Bendersky | 157a97a | 2014-01-18 22:54:33 +0000 | [diff] [blame] | 2830 | case Instruction::AddrSpaceCast: |
James Molloy | ce54568 | 2012-11-17 17:56:30 +0000 | [diff] [blame] | 2831 | return CastInst::Create((Instruction::CastOps)getOpcode(), |
| 2832 | Ops[0], getType()); |
| 2833 | case Instruction::Select: |
| 2834 | return SelectInst::Create(Ops[0], Ops[1], Ops[2]); |
| 2835 | case Instruction::InsertElement: |
| 2836 | return InsertElementInst::Create(Ops[0], Ops[1], Ops[2]); |
| 2837 | case Instruction::ExtractElement: |
| 2838 | return ExtractElementInst::Create(Ops[0], Ops[1]); |
| 2839 | case Instruction::InsertValue: |
| 2840 | return InsertValueInst::Create(Ops[0], Ops[1], getIndices()); |
| 2841 | case Instruction::ExtractValue: |
| 2842 | return ExtractValueInst::Create(Ops[0], getIndices()); |
| 2843 | case Instruction::ShuffleVector: |
| 2844 | return new ShuffleVectorInst(Ops[0], Ops[1], Ops[2]); |
| 2845 | |
David Blaikie | 741c8f8 | 2015-03-14 01:53:18 +0000 | [diff] [blame] | 2846 | case Instruction::GetElementPtr: { |
| 2847 | const auto *GO = cast<GEPOperator>(this); |
| 2848 | if (GO->isInBounds()) |
| 2849 | return GetElementPtrInst::CreateInBounds(GO->getSourceElementType(), |
| 2850 | Ops[0], Ops.slice(1)); |
| 2851 | return GetElementPtrInst::Create(GO->getSourceElementType(), Ops[0], |
| 2852 | Ops.slice(1)); |
| 2853 | } |
James Molloy | ce54568 | 2012-11-17 17:56:30 +0000 | [diff] [blame] | 2854 | case Instruction::ICmp: |
| 2855 | case Instruction::FCmp: |
| 2856 | return CmpInst::Create((Instruction::OtherOps)getOpcode(), |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 2857 | (CmpInst::Predicate)getPredicate(), Ops[0], Ops[1]); |
James Molloy | ce54568 | 2012-11-17 17:56:30 +0000 | [diff] [blame] | 2858 | |
| 2859 | default: |
| 2860 | assert(getNumOperands() == 2 && "Must be binary operator?"); |
| 2861 | BinaryOperator *BO = |
| 2862 | BinaryOperator::Create((Instruction::BinaryOps)getOpcode(), |
| 2863 | Ops[0], Ops[1]); |
| 2864 | if (isa<OverflowingBinaryOperator>(BO)) { |
| 2865 | BO->setHasNoUnsignedWrap(SubclassOptionalData & |
| 2866 | OverflowingBinaryOperator::NoUnsignedWrap); |
| 2867 | BO->setHasNoSignedWrap(SubclassOptionalData & |
| 2868 | OverflowingBinaryOperator::NoSignedWrap); |
| 2869 | } |
| 2870 | if (isa<PossiblyExactOperator>(BO)) |
| 2871 | BO->setIsExact(SubclassOptionalData & PossiblyExactOperator::IsExact); |
| 2872 | return BO; |
| 2873 | } |
| 2874 | } |