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