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