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