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