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