| 1 | //===-- Constants.cpp - Implement Constant nodes --------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the Constant* classes. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "llvm/IR/Constants.h" |
| 14 | #include "LLVMContextImpl.h" |
| 15 | #include "llvm/ADT/STLExtras.h" |
| 16 | #include "llvm/ADT/SmallVector.h" |
| 17 | #include "llvm/ADT/StringMap.h" |
| 18 | #include "llvm/IR/BasicBlock.h" |
| 19 | #include "llvm/IR/ConstantFold.h" |
| 20 | #include "llvm/IR/DerivedTypes.h" |
| 21 | #include "llvm/IR/Function.h" |
| 22 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
| 23 | #include "llvm/IR/GlobalAlias.h" |
| 24 | #include "llvm/IR/GlobalIFunc.h" |
| 25 | #include "llvm/IR/GlobalValue.h" |
| 26 | #include "llvm/IR/GlobalVariable.h" |
| 27 | #include "llvm/IR/Instructions.h" |
| 28 | #include "llvm/IR/Operator.h" |
| 29 | #include "llvm/IR/PatternMatch.h" |
| 30 | #include "llvm/Support/ErrorHandling.h" |
| 31 | #include "llvm/Support/MathExtras.h" |
| 32 | #include "llvm/Support/raw_ostream.h" |
| 33 | #include <algorithm> |
| 34 | |
| 35 | using namespace llvm; |
| 36 | using namespace PatternMatch; |
| 37 | |
| 38 | // As set of temporary options to help migrate how splats are represented. |
| 39 | static cl::opt<bool> UseConstantIntForFixedLengthSplat( |
| 40 | "use-constant-int-for-fixed-length-splat" , cl::init(Val: false), cl::Hidden, |
| 41 | cl::desc("Use ConstantInt's native fixed-length vector splat support." )); |
| 42 | static cl::opt<bool> UseConstantFPForFixedLengthSplat( |
| 43 | "use-constant-fp-for-fixed-length-splat" , cl::init(Val: false), cl::Hidden, |
| 44 | cl::desc("Use ConstantFP's native fixed-length vector splat support." )); |
| 45 | static cl::opt<bool> UseConstantIntForScalableSplat( |
| 46 | "use-constant-int-for-scalable-splat" , cl::init(Val: false), cl::Hidden, |
| 47 | cl::desc("Use ConstantInt's native scalable vector splat support." )); |
| 48 | static cl::opt<bool> UseConstantFPForScalableSplat( |
| 49 | "use-constant-fp-for-scalable-splat" , cl::init(Val: false), cl::Hidden, |
| 50 | cl::desc("Use ConstantFP's native scalable vector splat support." )); |
| 51 | |
| 52 | //===----------------------------------------------------------------------===// |
| 53 | // Constant Class |
| 54 | //===----------------------------------------------------------------------===// |
| 55 | |
| 56 | bool Constant::isNegativeZeroValue() const { |
| 57 | // Floating point values have an explicit -0.0 value. |
| 58 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Val: this)) |
| 59 | return CFP->isZero() && CFP->isNegative(); |
| 60 | |
| 61 | // Equivalent for a vector of -0.0's. |
| 62 | if (getType()->isVectorTy()) |
| 63 | if (const auto *SplatCFP = dyn_cast_or_null<ConstantFP>(Val: getSplatValue())) |
| 64 | return SplatCFP->isNegativeZeroValue(); |
| 65 | |
| 66 | // We've already handled true FP case; any other FP vectors can't represent -0.0. |
| 67 | if (getType()->isFPOrFPVectorTy()) |
| 68 | return false; |
| 69 | |
| 70 | // Otherwise, just use +0.0. |
| 71 | return isNullValue(); |
| 72 | } |
| 73 | |
| 74 | // Return true iff this constant is positive zero (floating point), negative |
| 75 | // zero (floating point), or a null value. |
| 76 | bool Constant::isZeroValue() const { |
| 77 | // Floating point values have an explicit -0.0 value. |
| 78 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Val: this)) |
| 79 | return CFP->isZero(); |
| 80 | |
| 81 | // Check for constant splat vectors of 1 values. |
| 82 | if (getType()->isVectorTy()) |
| 83 | if (const auto *SplatCFP = dyn_cast_or_null<ConstantFP>(Val: getSplatValue())) |
| 84 | return SplatCFP->isZero(); |
| 85 | |
| 86 | // Otherwise, just use +0.0. |
| 87 | return isNullValue(); |
| 88 | } |
| 89 | |
| 90 | bool Constant::isNullValue() const { |
| 91 | // 0 is null. |
| 92 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val: this)) |
| 93 | return CI->isZero(); |
| 94 | |
| 95 | // +0.0 is null. |
| 96 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Val: this)) |
| 97 | // ppc_fp128 determine isZero using high order double only |
| 98 | // Should check the bitwise value to make sure all bits are zero. |
| 99 | return CFP->isExactlyValue(V: +0.0); |
| 100 | |
| 101 | // constant zero is zero for aggregates, cpnull is null for pointers, none for |
| 102 | // tokens. |
| 103 | return isa<ConstantAggregateZero>(Val: this) || isa<ConstantPointerNull>(Val: this) || |
| 104 | isa<ConstantTokenNone>(Val: this) || isa<ConstantTargetNone>(Val: this); |
| 105 | } |
| 106 | |
| 107 | bool Constant::isAllOnesValue() const { |
| 108 | // Check for -1 integers |
| 109 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val: this)) |
| 110 | return CI->isMinusOne(); |
| 111 | |
| 112 | // Check for FP which are bitcasted from -1 integers |
| 113 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Val: this)) |
| 114 | return CFP->getValueAPF().bitcastToAPInt().isAllOnes(); |
| 115 | |
| 116 | // Check for constant splat vectors of 1 values. |
| 117 | if (getType()->isVectorTy()) |
| 118 | if (const auto *SplatVal = getSplatValue()) |
| 119 | return SplatVal->isAllOnesValue(); |
| 120 | |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | bool Constant::isOneValue() const { |
| 125 | // Check for 1 integers |
| 126 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val: this)) |
| 127 | return CI->isOne(); |
| 128 | |
| 129 | // Check for FP which are bitcasted from 1 integers |
| 130 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Val: this)) |
| 131 | return CFP->getValueAPF().bitcastToAPInt().isOne(); |
| 132 | |
| 133 | // Check for constant splat vectors of 1 values. |
| 134 | if (getType()->isVectorTy()) |
| 135 | if (const auto *SplatVal = getSplatValue()) |
| 136 | return SplatVal->isOneValue(); |
| 137 | |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | bool Constant::isNotOneValue() const { |
| 142 | // Check for 1 integers |
| 143 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val: this)) |
| 144 | return !CI->isOneValue(); |
| 145 | |
| 146 | // Check for FP which are bitcasted from 1 integers |
| 147 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Val: this)) |
| 148 | return !CFP->getValueAPF().bitcastToAPInt().isOne(); |
| 149 | |
| 150 | // Check that vectors don't contain 1 |
| 151 | if (auto *VTy = dyn_cast<FixedVectorType>(Val: getType())) { |
| 152 | for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) { |
| 153 | Constant *Elt = getAggregateElement(Elt: I); |
| 154 | if (!Elt || !Elt->isNotOneValue()) |
| 155 | return false; |
| 156 | } |
| 157 | return true; |
| 158 | } |
| 159 | |
| 160 | // Check for splats that don't contain 1 |
| 161 | if (getType()->isVectorTy()) |
| 162 | if (const auto *SplatVal = getSplatValue()) |
| 163 | return SplatVal->isNotOneValue(); |
| 164 | |
| 165 | // It *may* contain 1, we can't tell. |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | bool Constant::isMinSignedValue() const { |
| 170 | // Check for INT_MIN integers |
| 171 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val: this)) |
| 172 | return CI->isMinValue(/*isSigned=*/IsSigned: true); |
| 173 | |
| 174 | // Check for FP which are bitcasted from INT_MIN integers |
| 175 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Val: this)) |
| 176 | return CFP->getValueAPF().bitcastToAPInt().isMinSignedValue(); |
| 177 | |
| 178 | // Check for splats of INT_MIN values. |
| 179 | if (getType()->isVectorTy()) |
| 180 | if (const auto *SplatVal = getSplatValue()) |
| 181 | return SplatVal->isMinSignedValue(); |
| 182 | |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | bool Constant::isMaxSignedValue() const { |
| 187 | // Check for INT_MAX integers |
| 188 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val: this)) |
| 189 | return CI->isMaxValue(/*isSigned=*/IsSigned: true); |
| 190 | |
| 191 | // Check for FP which are bitcasted from INT_MAX integers |
| 192 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Val: this)) |
| 193 | return CFP->getValueAPF().bitcastToAPInt().isMaxSignedValue(); |
| 194 | |
| 195 | // Check for splats of INT_MAX values. |
| 196 | if (getType()->isVectorTy()) |
| 197 | if (const auto *SplatVal = getSplatValue()) |
| 198 | return SplatVal->isMaxSignedValue(); |
| 199 | |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | bool Constant::isNotMinSignedValue() const { |
| 204 | // Check for INT_MIN integers |
| 205 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val: this)) |
| 206 | return !CI->isMinValue(/*isSigned=*/IsSigned: true); |
| 207 | |
| 208 | // Check for FP which are bitcasted from INT_MIN integers |
| 209 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Val: this)) |
| 210 | return !CFP->getValueAPF().bitcastToAPInt().isMinSignedValue(); |
| 211 | |
| 212 | // Check that vectors don't contain INT_MIN |
| 213 | if (auto *VTy = dyn_cast<FixedVectorType>(Val: getType())) { |
| 214 | for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) { |
| 215 | Constant *Elt = getAggregateElement(Elt: I); |
| 216 | if (!Elt || !Elt->isNotMinSignedValue()) |
| 217 | return false; |
| 218 | } |
| 219 | return true; |
| 220 | } |
| 221 | |
| 222 | // Check for splats that aren't INT_MIN |
| 223 | if (getType()->isVectorTy()) |
| 224 | if (const auto *SplatVal = getSplatValue()) |
| 225 | return SplatVal->isNotMinSignedValue(); |
| 226 | |
| 227 | // It *may* contain INT_MIN, we can't tell. |
| 228 | return false; |
| 229 | } |
| 230 | |
| 231 | bool Constant::isFiniteNonZeroFP() const { |
| 232 | if (auto *CFP = dyn_cast<ConstantFP>(Val: this)) |
| 233 | return CFP->getValueAPF().isFiniteNonZero(); |
| 234 | |
| 235 | if (auto *VTy = dyn_cast<FixedVectorType>(Val: getType())) { |
| 236 | for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) { |
| 237 | auto *CFP = dyn_cast_or_null<ConstantFP>(Val: getAggregateElement(Elt: I)); |
| 238 | if (!CFP || !CFP->getValueAPF().isFiniteNonZero()) |
| 239 | return false; |
| 240 | } |
| 241 | return true; |
| 242 | } |
| 243 | |
| 244 | if (getType()->isVectorTy()) |
| 245 | if (const auto *SplatCFP = dyn_cast_or_null<ConstantFP>(Val: getSplatValue())) |
| 246 | return SplatCFP->isFiniteNonZeroFP(); |
| 247 | |
| 248 | // It *may* contain finite non-zero, we can't tell. |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | bool Constant::isNormalFP() const { |
| 253 | if (auto *CFP = dyn_cast<ConstantFP>(Val: this)) |
| 254 | return CFP->getValueAPF().isNormal(); |
| 255 | |
| 256 | if (auto *VTy = dyn_cast<FixedVectorType>(Val: getType())) { |
| 257 | for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) { |
| 258 | auto *CFP = dyn_cast_or_null<ConstantFP>(Val: getAggregateElement(Elt: I)); |
| 259 | if (!CFP || !CFP->getValueAPF().isNormal()) |
| 260 | return false; |
| 261 | } |
| 262 | return true; |
| 263 | } |
| 264 | |
| 265 | if (getType()->isVectorTy()) |
| 266 | if (const auto *SplatCFP = dyn_cast_or_null<ConstantFP>(Val: getSplatValue())) |
| 267 | return SplatCFP->isNormalFP(); |
| 268 | |
| 269 | // It *may* contain a normal fp value, we can't tell. |
| 270 | return false; |
| 271 | } |
| 272 | |
| 273 | bool Constant::hasExactInverseFP() const { |
| 274 | if (auto *CFP = dyn_cast<ConstantFP>(Val: this)) |
| 275 | return CFP->getValueAPF().getExactInverse(Inv: nullptr); |
| 276 | |
| 277 | if (auto *VTy = dyn_cast<FixedVectorType>(Val: getType())) { |
| 278 | for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) { |
| 279 | auto *CFP = dyn_cast_or_null<ConstantFP>(Val: getAggregateElement(Elt: I)); |
| 280 | if (!CFP || !CFP->getValueAPF().getExactInverse(Inv: nullptr)) |
| 281 | return false; |
| 282 | } |
| 283 | return true; |
| 284 | } |
| 285 | |
| 286 | if (getType()->isVectorTy()) |
| 287 | if (const auto *SplatCFP = dyn_cast_or_null<ConstantFP>(Val: getSplatValue())) |
| 288 | return SplatCFP->hasExactInverseFP(); |
| 289 | |
| 290 | // It *may* have an exact inverse fp value, we can't tell. |
| 291 | return false; |
| 292 | } |
| 293 | |
| 294 | bool Constant::isNaN() const { |
| 295 | if (auto *CFP = dyn_cast<ConstantFP>(Val: this)) |
| 296 | return CFP->isNaN(); |
| 297 | |
| 298 | if (auto *VTy = dyn_cast<FixedVectorType>(Val: getType())) { |
| 299 | for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) { |
| 300 | auto *CFP = dyn_cast_or_null<ConstantFP>(Val: getAggregateElement(Elt: I)); |
| 301 | if (!CFP || !CFP->isNaN()) |
| 302 | return false; |
| 303 | } |
| 304 | return true; |
| 305 | } |
| 306 | |
| 307 | if (getType()->isVectorTy()) |
| 308 | if (const auto *SplatCFP = dyn_cast_or_null<ConstantFP>(Val: getSplatValue())) |
| 309 | return SplatCFP->isNaN(); |
| 310 | |
| 311 | // It *may* be NaN, we can't tell. |
| 312 | return false; |
| 313 | } |
| 314 | |
| 315 | bool Constant::isElementWiseEqual(Value *Y) const { |
| 316 | // Are they fully identical? |
| 317 | if (this == Y) |
| 318 | return true; |
| 319 | |
| 320 | // The input value must be a vector constant with the same type. |
| 321 | auto *VTy = dyn_cast<VectorType>(Val: getType()); |
| 322 | if (!isa<Constant>(Val: Y) || !VTy || VTy != Y->getType()) |
| 323 | return false; |
| 324 | |
| 325 | // TODO: Compare pointer constants? |
| 326 | if (!(VTy->getElementType()->isIntegerTy() || |
| 327 | VTy->getElementType()->isFloatingPointTy())) |
| 328 | return false; |
| 329 | |
| 330 | // They may still be identical element-wise (if they have `undef`s). |
| 331 | // Bitcast to integer to allow exact bitwise comparison for all types. |
| 332 | Type *IntTy = VectorType::getInteger(VTy); |
| 333 | Constant *C0 = ConstantExpr::getBitCast(C: const_cast<Constant *>(this), Ty: IntTy); |
| 334 | Constant *C1 = ConstantExpr::getBitCast(C: cast<Constant>(Val: Y), Ty: IntTy); |
| 335 | Constant *CmpEq = ConstantFoldCompareInstruction(Predicate: ICmpInst::ICMP_EQ, C1: C0, C2: C1); |
| 336 | return CmpEq && (isa<PoisonValue>(Val: CmpEq) || match(V: CmpEq, P: m_One())); |
| 337 | } |
| 338 | |
| 339 | static bool |
| 340 | containsUndefinedElement(const Constant *C, |
| 341 | function_ref<bool(const Constant *)> HasFn) { |
| 342 | if (auto *VTy = dyn_cast<VectorType>(Val: C->getType())) { |
| 343 | if (HasFn(C)) |
| 344 | return true; |
| 345 | if (isa<ConstantAggregateZero>(Val: C)) |
| 346 | return false; |
| 347 | if (isa<ScalableVectorType>(Val: C->getType())) |
| 348 | return false; |
| 349 | |
| 350 | for (unsigned i = 0, e = cast<FixedVectorType>(Val: VTy)->getNumElements(); |
| 351 | i != e; ++i) { |
| 352 | if (Constant *Elem = C->getAggregateElement(Elt: i)) |
| 353 | if (HasFn(Elem)) |
| 354 | return true; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | return false; |
| 359 | } |
| 360 | |
| 361 | bool Constant::containsUndefOrPoisonElement() const { |
| 362 | return containsUndefinedElement( |
| 363 | C: this, HasFn: [&](const auto *C) { return isa<UndefValue>(C); }); |
| 364 | } |
| 365 | |
| 366 | bool Constant::containsPoisonElement() const { |
| 367 | return containsUndefinedElement( |
| 368 | C: this, HasFn: [&](const auto *C) { return isa<PoisonValue>(C); }); |
| 369 | } |
| 370 | |
| 371 | bool Constant::containsUndefElement() const { |
| 372 | return containsUndefinedElement(C: this, HasFn: [&](const auto *C) { |
| 373 | return isa<UndefValue>(C) && !isa<PoisonValue>(C); |
| 374 | }); |
| 375 | } |
| 376 | |
| 377 | bool Constant::containsConstantExpression() const { |
| 378 | if (isa<ConstantInt>(Val: this) || isa<ConstantFP>(Val: this)) |
| 379 | return false; |
| 380 | |
| 381 | if (auto *VTy = dyn_cast<FixedVectorType>(Val: getType())) { |
| 382 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) |
| 383 | if (isa<ConstantExpr>(Val: getAggregateElement(Elt: i))) |
| 384 | return true; |
| 385 | } |
| 386 | return false; |
| 387 | } |
| 388 | |
| 389 | /// Constructor to create a '0' constant of arbitrary type. |
| 390 | Constant *Constant::getNullValue(Type *Ty) { |
| 391 | switch (Ty->getTypeID()) { |
| 392 | case Type::IntegerTyID: |
| 393 | return ConstantInt::get(Ty, V: 0); |
| 394 | case Type::HalfTyID: |
| 395 | case Type::BFloatTyID: |
| 396 | case Type::FloatTyID: |
| 397 | case Type::DoubleTyID: |
| 398 | case Type::X86_FP80TyID: |
| 399 | case Type::FP128TyID: |
| 400 | case Type::PPC_FP128TyID: |
| 401 | return ConstantFP::get(Context&: Ty->getContext(), |
| 402 | V: APFloat::getZero(Sem: Ty->getFltSemantics())); |
| 403 | case Type::PointerTyID: |
| 404 | return ConstantPointerNull::get(T: cast<PointerType>(Val: Ty)); |
| 405 | case Type::StructTyID: |
| 406 | case Type::ArrayTyID: |
| 407 | case Type::FixedVectorTyID: |
| 408 | case Type::ScalableVectorTyID: |
| 409 | return ConstantAggregateZero::get(Ty); |
| 410 | case Type::TokenTyID: |
| 411 | return ConstantTokenNone::get(Context&: Ty->getContext()); |
| 412 | case Type::TargetExtTyID: |
| 413 | return ConstantTargetNone::get(T: cast<TargetExtType>(Val: Ty)); |
| 414 | default: |
| 415 | // Function, Label, or Opaque type? |
| 416 | llvm_unreachable("Cannot create a null constant of that type!" ); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | Constant *Constant::getIntegerValue(Type *Ty, const APInt &V) { |
| 421 | Type *ScalarTy = Ty->getScalarType(); |
| 422 | |
| 423 | // Create the base integer constant. |
| 424 | Constant *C = ConstantInt::get(Context&: Ty->getContext(), V); |
| 425 | |
| 426 | // Convert an integer to a pointer, if necessary. |
| 427 | if (PointerType *PTy = dyn_cast<PointerType>(Val: ScalarTy)) |
| 428 | C = ConstantExpr::getIntToPtr(C, Ty: PTy); |
| 429 | |
| 430 | // Broadcast a scalar to a vector, if necessary. |
| 431 | if (VectorType *VTy = dyn_cast<VectorType>(Val: Ty)) |
| 432 | C = ConstantVector::getSplat(EC: VTy->getElementCount(), Elt: C); |
| 433 | |
| 434 | return C; |
| 435 | } |
| 436 | |
| 437 | Constant *Constant::getAllOnesValue(Type *Ty) { |
| 438 | if (IntegerType *ITy = dyn_cast<IntegerType>(Val: Ty)) |
| 439 | return ConstantInt::get(Context&: Ty->getContext(), |
| 440 | V: APInt::getAllOnes(numBits: ITy->getBitWidth())); |
| 441 | |
| 442 | if (Ty->isFloatingPointTy()) { |
| 443 | APFloat FL = APFloat::getAllOnesValue(Semantics: Ty->getFltSemantics()); |
| 444 | return ConstantFP::get(Context&: Ty->getContext(), V: FL); |
| 445 | } |
| 446 | |
| 447 | VectorType *VTy = cast<VectorType>(Val: Ty); |
| 448 | return ConstantVector::getSplat(EC: VTy->getElementCount(), |
| 449 | Elt: getAllOnesValue(Ty: VTy->getElementType())); |
| 450 | } |
| 451 | |
| 452 | Constant *Constant::getAggregateElement(unsigned Elt) const { |
| 453 | assert((getType()->isAggregateType() || getType()->isVectorTy()) && |
| 454 | "Must be an aggregate/vector constant" ); |
| 455 | |
| 456 | if (const auto *CC = dyn_cast<ConstantAggregate>(Val: this)) |
| 457 | return Elt < CC->getNumOperands() ? CC->getOperand(i_nocapture: Elt) : nullptr; |
| 458 | |
| 459 | if (const auto *CAZ = dyn_cast<ConstantAggregateZero>(Val: this)) |
| 460 | return Elt < CAZ->getElementCount().getKnownMinValue() |
| 461 | ? CAZ->getElementValue(Idx: Elt) |
| 462 | : nullptr; |
| 463 | |
| 464 | if (const auto *CI = dyn_cast<ConstantInt>(Val: this)) |
| 465 | return Elt < cast<VectorType>(Val: getType()) |
| 466 | ->getElementCount() |
| 467 | .getKnownMinValue() |
| 468 | ? ConstantInt::get(Context&: getContext(), V: CI->getValue()) |
| 469 | : nullptr; |
| 470 | |
| 471 | if (const auto *CFP = dyn_cast<ConstantFP>(Val: this)) |
| 472 | return Elt < cast<VectorType>(Val: getType()) |
| 473 | ->getElementCount() |
| 474 | .getKnownMinValue() |
| 475 | ? ConstantFP::get(Context&: getContext(), V: CFP->getValue()) |
| 476 | : nullptr; |
| 477 | |
| 478 | // FIXME: getNumElements() will fail for non-fixed vector types. |
| 479 | if (isa<ScalableVectorType>(Val: getType())) |
| 480 | return nullptr; |
| 481 | |
| 482 | if (const auto *PV = dyn_cast<PoisonValue>(Val: this)) |
| 483 | return Elt < PV->getNumElements() ? PV->getElementValue(Idx: Elt) : nullptr; |
| 484 | |
| 485 | if (const auto *UV = dyn_cast<UndefValue>(Val: this)) |
| 486 | return Elt < UV->getNumElements() ? UV->getElementValue(Idx: Elt) : nullptr; |
| 487 | |
| 488 | if (const auto *CDS = dyn_cast<ConstantDataSequential>(Val: this)) |
| 489 | return Elt < CDS->getNumElements() ? CDS->getElementAsConstant(i: Elt) |
| 490 | : nullptr; |
| 491 | |
| 492 | return nullptr; |
| 493 | } |
| 494 | |
| 495 | Constant *Constant::getAggregateElement(Constant *Elt) const { |
| 496 | assert(isa<IntegerType>(Elt->getType()) && "Index must be an integer" ); |
| 497 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Val: Elt)) { |
| 498 | // Check if the constant fits into an uint64_t. |
| 499 | if (CI->getValue().getActiveBits() > 64) |
| 500 | return nullptr; |
| 501 | return getAggregateElement(Elt: CI->getZExtValue()); |
| 502 | } |
| 503 | return nullptr; |
| 504 | } |
| 505 | |
| 506 | void Constant::destroyConstant() { |
| 507 | /// First call destroyConstantImpl on the subclass. This gives the subclass |
| 508 | /// a chance to remove the constant from any maps/pools it's contained in. |
| 509 | switch (getValueID()) { |
| 510 | default: |
| 511 | llvm_unreachable("Not a constant!" ); |
| 512 | #define HANDLE_CONSTANT(Name) \ |
| 513 | case Value::Name##Val: \ |
| 514 | cast<Name>(this)->destroyConstantImpl(); \ |
| 515 | break; |
| 516 | #include "llvm/IR/Value.def" |
| 517 | } |
| 518 | |
| 519 | // When a Constant is destroyed, there may be lingering |
| 520 | // references to the constant by other constants in the constant pool. These |
| 521 | // constants are implicitly dependent on the module that is being deleted, |
| 522 | // but they don't know that. Because we only find out when the CPV is |
| 523 | // deleted, we must now notify all of our users (that should only be |
| 524 | // Constants) that they are, in fact, invalid now and should be deleted. |
| 525 | // |
| 526 | while (!use_empty()) { |
| 527 | Value *V = user_back(); |
| 528 | #ifndef NDEBUG // Only in -g mode... |
| 529 | if (!isa<Constant>(V)) { |
| 530 | dbgs() << "While deleting: " << *this |
| 531 | << "\n\nUse still stuck around after Def is destroyed: " << *V |
| 532 | << "\n\n" ; |
| 533 | } |
| 534 | #endif |
| 535 | assert(isa<Constant>(V) && "References remain to Constant being destroyed" ); |
| 536 | cast<Constant>(Val: V)->destroyConstant(); |
| 537 | |
| 538 | // The constant should remove itself from our use list... |
| 539 | assert((use_empty() || user_back() != V) && "Constant not removed!" ); |
| 540 | } |
| 541 | |
| 542 | // Value has no outstanding references it is safe to delete it now... |
| 543 | deleteConstant(C: this); |
| 544 | } |
| 545 | |
| 546 | void llvm::deleteConstant(Constant *C) { |
| 547 | switch (C->getValueID()) { |
| 548 | case Constant::ConstantIntVal: |
| 549 | delete static_cast<ConstantInt *>(C); |
| 550 | break; |
| 551 | case Constant::ConstantFPVal: |
| 552 | delete static_cast<ConstantFP *>(C); |
| 553 | break; |
| 554 | case Constant::ConstantAggregateZeroVal: |
| 555 | delete static_cast<ConstantAggregateZero *>(C); |
| 556 | break; |
| 557 | case Constant::ConstantArrayVal: |
| 558 | delete static_cast<ConstantArray *>(C); |
| 559 | break; |
| 560 | case Constant::ConstantStructVal: |
| 561 | delete static_cast<ConstantStruct *>(C); |
| 562 | break; |
| 563 | case Constant::ConstantVectorVal: |
| 564 | delete static_cast<ConstantVector *>(C); |
| 565 | break; |
| 566 | case Constant::ConstantPointerNullVal: |
| 567 | delete static_cast<ConstantPointerNull *>(C); |
| 568 | break; |
| 569 | case Constant::ConstantDataArrayVal: |
| 570 | delete static_cast<ConstantDataArray *>(C); |
| 571 | break; |
| 572 | case Constant::ConstantDataVectorVal: |
| 573 | delete static_cast<ConstantDataVector *>(C); |
| 574 | break; |
| 575 | case Constant::ConstantTokenNoneVal: |
| 576 | delete static_cast<ConstantTokenNone *>(C); |
| 577 | break; |
| 578 | case Constant::BlockAddressVal: |
| 579 | delete static_cast<BlockAddress *>(C); |
| 580 | break; |
| 581 | case Constant::DSOLocalEquivalentVal: |
| 582 | delete static_cast<DSOLocalEquivalent *>(C); |
| 583 | break; |
| 584 | case Constant::NoCFIValueVal: |
| 585 | delete static_cast<NoCFIValue *>(C); |
| 586 | break; |
| 587 | case Constant::ConstantPtrAuthVal: |
| 588 | delete static_cast<ConstantPtrAuth *>(C); |
| 589 | break; |
| 590 | case Constant::UndefValueVal: |
| 591 | delete static_cast<UndefValue *>(C); |
| 592 | break; |
| 593 | case Constant::PoisonValueVal: |
| 594 | delete static_cast<PoisonValue *>(C); |
| 595 | break; |
| 596 | case Constant::ConstantExprVal: |
| 597 | if (isa<CastConstantExpr>(Val: C)) |
| 598 | delete static_cast<CastConstantExpr *>(C); |
| 599 | else if (isa<BinaryConstantExpr>(Val: C)) |
| 600 | delete static_cast<BinaryConstantExpr *>(C); |
| 601 | else if (isa<ExtractElementConstantExpr>(Val: C)) |
| 602 | delete static_cast<ExtractElementConstantExpr *>(C); |
| 603 | else if (isa<InsertElementConstantExpr>(Val: C)) |
| 604 | delete static_cast<InsertElementConstantExpr *>(C); |
| 605 | else if (isa<ShuffleVectorConstantExpr>(Val: C)) |
| 606 | delete static_cast<ShuffleVectorConstantExpr *>(C); |
| 607 | else if (isa<GetElementPtrConstantExpr>(Val: C)) |
| 608 | delete static_cast<GetElementPtrConstantExpr *>(C); |
| 609 | else |
| 610 | llvm_unreachable("Unexpected constant expr" ); |
| 611 | break; |
| 612 | default: |
| 613 | llvm_unreachable("Unexpected constant" ); |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | /// Check if C contains a GlobalValue for which Predicate is true. |
| 618 | static bool |
| 619 | ConstHasGlobalValuePredicate(const Constant *C, |
| 620 | bool (*Predicate)(const GlobalValue *)) { |
| 621 | SmallPtrSet<const Constant *, 8> Visited; |
| 622 | SmallVector<const Constant *, 8> WorkList; |
| 623 | WorkList.push_back(Elt: C); |
| 624 | Visited.insert(Ptr: C); |
| 625 | |
| 626 | while (!WorkList.empty()) { |
| 627 | const Constant *WorkItem = WorkList.pop_back_val(); |
| 628 | if (const auto *GV = dyn_cast<GlobalValue>(Val: WorkItem)) |
| 629 | if (Predicate(GV)) |
| 630 | return true; |
| 631 | for (const Value *Op : WorkItem->operands()) { |
| 632 | const Constant *ConstOp = dyn_cast<Constant>(Val: Op); |
| 633 | if (!ConstOp) |
| 634 | continue; |
| 635 | if (Visited.insert(Ptr: ConstOp).second) |
| 636 | WorkList.push_back(Elt: ConstOp); |
| 637 | } |
| 638 | } |
| 639 | return false; |
| 640 | } |
| 641 | |
| 642 | bool Constant::isThreadDependent() const { |
| 643 | auto DLLImportPredicate = [](const GlobalValue *GV) { |
| 644 | return GV->isThreadLocal(); |
| 645 | }; |
| 646 | return ConstHasGlobalValuePredicate(C: this, Predicate: DLLImportPredicate); |
| 647 | } |
| 648 | |
| 649 | bool Constant::isDLLImportDependent() const { |
| 650 | auto DLLImportPredicate = [](const GlobalValue *GV) { |
| 651 | return GV->hasDLLImportStorageClass(); |
| 652 | }; |
| 653 | return ConstHasGlobalValuePredicate(C: this, Predicate: DLLImportPredicate); |
| 654 | } |
| 655 | |
| 656 | bool Constant::isConstantUsed() const { |
| 657 | for (const User *U : users()) { |
| 658 | const Constant *UC = dyn_cast<Constant>(Val: U); |
| 659 | if (!UC || isa<GlobalValue>(Val: UC)) |
| 660 | return true; |
| 661 | |
| 662 | if (UC->isConstantUsed()) |
| 663 | return true; |
| 664 | } |
| 665 | return false; |
| 666 | } |
| 667 | |
| 668 | bool Constant::needsDynamicRelocation() const { |
| 669 | return getRelocationInfo() == GlobalRelocation; |
| 670 | } |
| 671 | |
| 672 | bool Constant::needsRelocation() const { |
| 673 | return getRelocationInfo() != NoRelocation; |
| 674 | } |
| 675 | |
| 676 | Constant::PossibleRelocationsTy Constant::getRelocationInfo() const { |
| 677 | if (isa<GlobalValue>(Val: this)) |
| 678 | return GlobalRelocation; // Global reference. |
| 679 | |
| 680 | if (const BlockAddress *BA = dyn_cast<BlockAddress>(Val: this)) |
| 681 | return BA->getFunction()->getRelocationInfo(); |
| 682 | |
| 683 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Val: this)) { |
| 684 | if (CE->getOpcode() == Instruction::Sub) { |
| 685 | ConstantExpr *LHS = dyn_cast<ConstantExpr>(Val: CE->getOperand(i_nocapture: 0)); |
| 686 | ConstantExpr *RHS = dyn_cast<ConstantExpr>(Val: CE->getOperand(i_nocapture: 1)); |
| 687 | if (LHS && RHS && |
| 688 | (LHS->getOpcode() == Instruction::PtrToInt || |
| 689 | LHS->getOpcode() == Instruction::PtrToAddr) && |
| 690 | (RHS->getOpcode() == Instruction::PtrToInt || |
| 691 | RHS->getOpcode() == Instruction::PtrToAddr)) { |
| 692 | Constant *LHSOp0 = LHS->getOperand(i_nocapture: 0); |
| 693 | Constant *RHSOp0 = RHS->getOperand(i_nocapture: 0); |
| 694 | |
| 695 | // While raw uses of blockaddress need to be relocated, differences |
| 696 | // between two of them don't when they are for labels in the same |
| 697 | // function. This is a common idiom when creating a table for the |
| 698 | // indirect goto extension, so we handle it efficiently here. |
| 699 | if (isa<BlockAddress>(Val: LHSOp0) && isa<BlockAddress>(Val: RHSOp0) && |
| 700 | cast<BlockAddress>(Val: LHSOp0)->getFunction() == |
| 701 | cast<BlockAddress>(Val: RHSOp0)->getFunction()) |
| 702 | return NoRelocation; |
| 703 | |
| 704 | // Relative pointers do not need to be dynamically relocated. |
| 705 | if (auto *RHSGV = |
| 706 | dyn_cast<GlobalValue>(Val: RHSOp0->stripInBoundsConstantOffsets())) { |
| 707 | auto *LHS = LHSOp0->stripInBoundsConstantOffsets(); |
| 708 | if (auto *LHSGV = dyn_cast<GlobalValue>(Val: LHS)) { |
| 709 | if (LHSGV->isDSOLocal() && RHSGV->isDSOLocal()) |
| 710 | return LocalRelocation; |
| 711 | } else if (isa<DSOLocalEquivalent>(Val: LHS)) { |
| 712 | if (RHSGV->isDSOLocal()) |
| 713 | return LocalRelocation; |
| 714 | } |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | PossibleRelocationsTy Result = NoRelocation; |
| 721 | for (const Value *Op : operands()) |
| 722 | Result = std::max(a: cast<Constant>(Val: Op)->getRelocationInfo(), b: Result); |
| 723 | |
| 724 | return Result; |
| 725 | } |
| 726 | |
| 727 | /// Return true if the specified constantexpr is dead. This involves |
| 728 | /// recursively traversing users of the constantexpr. |
| 729 | /// If RemoveDeadUsers is true, also remove dead users at the same time. |
| 730 | static bool constantIsDead(const Constant *C, bool RemoveDeadUsers) { |
| 731 | if (isa<GlobalValue>(Val: C)) return false; // Cannot remove this |
| 732 | |
| 733 | Value::const_user_iterator I = C->user_begin(), E = C->user_end(); |
| 734 | while (I != E) { |
| 735 | const Constant *User = dyn_cast<Constant>(Val: *I); |
| 736 | if (!User) return false; // Non-constant usage; |
| 737 | if (!constantIsDead(C: User, RemoveDeadUsers)) |
| 738 | return false; // Constant wasn't dead |
| 739 | |
| 740 | // Just removed User, so the iterator was invalidated. |
| 741 | // Since we return immediately upon finding a live user, we can always |
| 742 | // restart from user_begin(). |
| 743 | if (RemoveDeadUsers) |
| 744 | I = C->user_begin(); |
| 745 | else |
| 746 | ++I; |
| 747 | } |
| 748 | |
| 749 | if (RemoveDeadUsers) { |
| 750 | // If C is only used by metadata, it should not be preserved but should |
| 751 | // have its uses replaced. |
| 752 | ReplaceableMetadataImpl::SalvageDebugInfo(C: *C); |
| 753 | const_cast<Constant *>(C)->destroyConstant(); |
| 754 | } |
| 755 | |
| 756 | return true; |
| 757 | } |
| 758 | |
| 759 | void Constant::removeDeadConstantUsers() const { |
| 760 | Value::const_user_iterator I = user_begin(), E = user_end(); |
| 761 | Value::const_user_iterator LastNonDeadUser = E; |
| 762 | while (I != E) { |
| 763 | const Constant *User = dyn_cast<Constant>(Val: *I); |
| 764 | if (!User) { |
| 765 | LastNonDeadUser = I; |
| 766 | ++I; |
| 767 | continue; |
| 768 | } |
| 769 | |
| 770 | if (!constantIsDead(C: User, /* RemoveDeadUsers= */ true)) { |
| 771 | // If the constant wasn't dead, remember that this was the last live use |
| 772 | // and move on to the next constant. |
| 773 | LastNonDeadUser = I; |
| 774 | ++I; |
| 775 | continue; |
| 776 | } |
| 777 | |
| 778 | // If the constant was dead, then the iterator is invalidated. |
| 779 | if (LastNonDeadUser == E) |
| 780 | I = user_begin(); |
| 781 | else |
| 782 | I = std::next(x: LastNonDeadUser); |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | bool Constant::hasOneLiveUse() const { return hasNLiveUses(N: 1); } |
| 787 | |
| 788 | bool Constant::hasZeroLiveUses() const { return hasNLiveUses(N: 0); } |
| 789 | |
| 790 | bool Constant::hasNLiveUses(unsigned N) const { |
| 791 | unsigned NumUses = 0; |
| 792 | for (const Use &U : uses()) { |
| 793 | const Constant *User = dyn_cast<Constant>(Val: U.getUser()); |
| 794 | if (!User || !constantIsDead(C: User, /* RemoveDeadUsers= */ false)) { |
| 795 | ++NumUses; |
| 796 | |
| 797 | if (NumUses > N) |
| 798 | return false; |
| 799 | } |
| 800 | } |
| 801 | return NumUses == N; |
| 802 | } |
| 803 | |
| 804 | Constant *Constant::replaceUndefsWith(Constant *C, Constant *Replacement) { |
| 805 | assert(C && Replacement && "Expected non-nullptr constant arguments" ); |
| 806 | Type *Ty = C->getType(); |
| 807 | if (match(V: C, P: m_Undef())) { |
| 808 | assert(Ty == Replacement->getType() && "Expected matching types" ); |
| 809 | return Replacement; |
| 810 | } |
| 811 | |
| 812 | // Don't know how to deal with this constant. |
| 813 | auto *VTy = dyn_cast<FixedVectorType>(Val: Ty); |
| 814 | if (!VTy) |
| 815 | return C; |
| 816 | |
| 817 | unsigned NumElts = VTy->getNumElements(); |
| 818 | SmallVector<Constant *, 32> NewC(NumElts); |
| 819 | for (unsigned i = 0; i != NumElts; ++i) { |
| 820 | Constant *EltC = C->getAggregateElement(Elt: i); |
| 821 | assert((!EltC || EltC->getType() == Replacement->getType()) && |
| 822 | "Expected matching types" ); |
| 823 | NewC[i] = EltC && match(V: EltC, P: m_Undef()) ? Replacement : EltC; |
| 824 | } |
| 825 | return ConstantVector::get(V: NewC); |
| 826 | } |
| 827 | |
| 828 | Constant *Constant::mergeUndefsWith(Constant *C, Constant *Other) { |
| 829 | assert(C && Other && "Expected non-nullptr constant arguments" ); |
| 830 | if (match(V: C, P: m_Undef())) |
| 831 | return C; |
| 832 | |
| 833 | Type *Ty = C->getType(); |
| 834 | if (match(V: Other, P: m_Undef())) |
| 835 | return UndefValue::get(T: Ty); |
| 836 | |
| 837 | auto *VTy = dyn_cast<FixedVectorType>(Val: Ty); |
| 838 | if (!VTy) |
| 839 | return C; |
| 840 | |
| 841 | Type *EltTy = VTy->getElementType(); |
| 842 | unsigned NumElts = VTy->getNumElements(); |
| 843 | assert(isa<FixedVectorType>(Other->getType()) && |
| 844 | cast<FixedVectorType>(Other->getType())->getNumElements() == NumElts && |
| 845 | "Type mismatch" ); |
| 846 | |
| 847 | bool = false; |
| 848 | SmallVector<Constant *, 32> NewC(NumElts); |
| 849 | for (unsigned I = 0; I != NumElts; ++I) { |
| 850 | NewC[I] = C->getAggregateElement(Elt: I); |
| 851 | Constant *OtherEltC = Other->getAggregateElement(Elt: I); |
| 852 | assert(NewC[I] && OtherEltC && "Unknown vector element" ); |
| 853 | if (!match(V: NewC[I], P: m_Undef()) && match(V: OtherEltC, P: m_Undef())) { |
| 854 | NewC[I] = UndefValue::get(T: EltTy); |
| 855 | FoundExtraUndef = true; |
| 856 | } |
| 857 | } |
| 858 | if (FoundExtraUndef) |
| 859 | return ConstantVector::get(V: NewC); |
| 860 | return C; |
| 861 | } |
| 862 | |
| 863 | bool Constant::isManifestConstant() const { |
| 864 | if (isa<UndefValue>(Val: this)) |
| 865 | return false; |
| 866 | if (isa<ConstantData>(Val: this)) |
| 867 | return true; |
| 868 | if (isa<ConstantAggregate>(Val: this) || isa<ConstantExpr>(Val: this)) { |
| 869 | for (const Value *Op : operand_values()) |
| 870 | if (!cast<Constant>(Val: Op)->isManifestConstant()) |
| 871 | return false; |
| 872 | return true; |
| 873 | } |
| 874 | return false; |
| 875 | } |
| 876 | |
| 877 | //===----------------------------------------------------------------------===// |
| 878 | // ConstantInt |
| 879 | //===----------------------------------------------------------------------===// |
| 880 | |
| 881 | ConstantInt::ConstantInt(Type *Ty, const APInt &V) |
| 882 | : ConstantData(Ty, ConstantIntVal), Val(V) { |
| 883 | assert(V.getBitWidth() == |
| 884 | cast<IntegerType>(Ty->getScalarType())->getBitWidth() && |
| 885 | "Invalid constant for type" ); |
| 886 | } |
| 887 | |
| 888 | ConstantInt *ConstantInt::getTrue(LLVMContext &Context) { |
| 889 | LLVMContextImpl *pImpl = Context.pImpl; |
| 890 | if (!pImpl->TheTrueVal) |
| 891 | pImpl->TheTrueVal = ConstantInt::get(Ty: Type::getInt1Ty(C&: Context), V: 1); |
| 892 | return pImpl->TheTrueVal; |
| 893 | } |
| 894 | |
| 895 | ConstantInt *ConstantInt::getFalse(LLVMContext &Context) { |
| 896 | LLVMContextImpl *pImpl = Context.pImpl; |
| 897 | if (!pImpl->TheFalseVal) |
| 898 | pImpl->TheFalseVal = ConstantInt::get(Ty: Type::getInt1Ty(C&: Context), V: 0); |
| 899 | return pImpl->TheFalseVal; |
| 900 | } |
| 901 | |
| 902 | ConstantInt *ConstantInt::getBool(LLVMContext &Context, bool V) { |
| 903 | return V ? getTrue(Context) : getFalse(Context); |
| 904 | } |
| 905 | |
| 906 | Constant *ConstantInt::getTrue(Type *Ty) { |
| 907 | assert(Ty->isIntOrIntVectorTy(1) && "Type not i1 or vector of i1." ); |
| 908 | ConstantInt *TrueC = ConstantInt::getTrue(Context&: Ty->getContext()); |
| 909 | if (auto *VTy = dyn_cast<VectorType>(Val: Ty)) |
| 910 | return ConstantVector::getSplat(EC: VTy->getElementCount(), Elt: TrueC); |
| 911 | return TrueC; |
| 912 | } |
| 913 | |
| 914 | Constant *ConstantInt::getFalse(Type *Ty) { |
| 915 | assert(Ty->isIntOrIntVectorTy(1) && "Type not i1 or vector of i1." ); |
| 916 | ConstantInt *FalseC = ConstantInt::getFalse(Context&: Ty->getContext()); |
| 917 | if (auto *VTy = dyn_cast<VectorType>(Val: Ty)) |
| 918 | return ConstantVector::getSplat(EC: VTy->getElementCount(), Elt: FalseC); |
| 919 | return FalseC; |
| 920 | } |
| 921 | |
| 922 | Constant *ConstantInt::getBool(Type *Ty, bool V) { |
| 923 | return V ? getTrue(Ty) : getFalse(Ty); |
| 924 | } |
| 925 | |
| 926 | // Get a ConstantInt from an APInt. |
| 927 | ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt &V) { |
| 928 | // get an existing value or the insertion position |
| 929 | LLVMContextImpl *pImpl = Context.pImpl; |
| 930 | std::unique_ptr<ConstantInt> &Slot = |
| 931 | V.isZero() ? pImpl->IntZeroConstants[V.getBitWidth()] |
| 932 | : V.isOne() ? pImpl->IntOneConstants[V.getBitWidth()] |
| 933 | : pImpl->IntConstants[V]; |
| 934 | if (!Slot) { |
| 935 | // Get the corresponding integer type for the bit width of the value. |
| 936 | IntegerType *ITy = IntegerType::get(C&: Context, NumBits: V.getBitWidth()); |
| 937 | Slot.reset(p: new ConstantInt(ITy, V)); |
| 938 | } |
| 939 | assert(Slot->getType() == IntegerType::get(Context, V.getBitWidth())); |
| 940 | return Slot.get(); |
| 941 | } |
| 942 | |
| 943 | // Get a ConstantInt vector with each lane set to the same APInt. |
| 944 | ConstantInt *ConstantInt::get(LLVMContext &Context, ElementCount EC, |
| 945 | const APInt &V) { |
| 946 | // Get an existing value or the insertion position. |
| 947 | std::unique_ptr<ConstantInt> &Slot = |
| 948 | Context.pImpl->IntSplatConstants[std::make_pair(x&: EC, y: V)]; |
| 949 | if (!Slot) { |
| 950 | IntegerType *ITy = IntegerType::get(C&: Context, NumBits: V.getBitWidth()); |
| 951 | VectorType *VTy = VectorType::get(ElementType: ITy, EC); |
| 952 | Slot.reset(p: new ConstantInt(VTy, V)); |
| 953 | } |
| 954 | |
| 955 | #ifndef NDEBUG |
| 956 | IntegerType *ITy = IntegerType::get(Context, V.getBitWidth()); |
| 957 | VectorType *VTy = VectorType::get(ITy, EC); |
| 958 | assert(Slot->getType() == VTy); |
| 959 | #endif |
| 960 | return Slot.get(); |
| 961 | } |
| 962 | |
| 963 | Constant *ConstantInt::get(Type *Ty, uint64_t V, bool IsSigned, |
| 964 | bool ImplicitTrunc) { |
| 965 | Constant *C = |
| 966 | get(Ty: cast<IntegerType>(Val: Ty->getScalarType()), V, IsSigned, ImplicitTrunc); |
| 967 | |
| 968 | // For vectors, broadcast the value. |
| 969 | if (VectorType *VTy = dyn_cast<VectorType>(Val: Ty)) |
| 970 | return ConstantVector::getSplat(EC: VTy->getElementCount(), Elt: C); |
| 971 | |
| 972 | return C; |
| 973 | } |
| 974 | |
| 975 | ConstantInt *ConstantInt::get(IntegerType *Ty, uint64_t V, bool IsSigned, |
| 976 | bool ImplicitTrunc) { |
| 977 | return get(Context&: Ty->getContext(), |
| 978 | V: APInt(Ty->getBitWidth(), V, IsSigned, ImplicitTrunc)); |
| 979 | } |
| 980 | |
| 981 | Constant *ConstantInt::get(Type *Ty, const APInt& V) { |
| 982 | ConstantInt *C = get(Context&: Ty->getContext(), V); |
| 983 | assert(C->getType() == Ty->getScalarType() && |
| 984 | "ConstantInt type doesn't match the type implied by its value!" ); |
| 985 | |
| 986 | // For vectors, broadcast the value. |
| 987 | if (VectorType *VTy = dyn_cast<VectorType>(Val: Ty)) |
| 988 | return ConstantVector::getSplat(EC: VTy->getElementCount(), Elt: C); |
| 989 | |
| 990 | return C; |
| 991 | } |
| 992 | |
| 993 | ConstantInt *ConstantInt::get(IntegerType* Ty, StringRef Str, uint8_t radix) { |
| 994 | return get(Context&: Ty->getContext(), V: APInt(Ty->getBitWidth(), Str, radix)); |
| 995 | } |
| 996 | |
| 997 | /// Remove the constant from the constant table. |
| 998 | void ConstantInt::destroyConstantImpl() { |
| 999 | llvm_unreachable("You can't ConstantInt->destroyConstantImpl()!" ); |
| 1000 | } |
| 1001 | |
| 1002 | //===----------------------------------------------------------------------===// |
| 1003 | // ConstantFP |
| 1004 | //===----------------------------------------------------------------------===// |
| 1005 | |
| 1006 | Constant *ConstantFP::get(Type *Ty, double V) { |
| 1007 | LLVMContext &Context = Ty->getContext(); |
| 1008 | |
| 1009 | APFloat FV(V); |
| 1010 | bool ignored; |
| 1011 | FV.convert(ToSemantics: Ty->getScalarType()->getFltSemantics(), |
| 1012 | RM: APFloat::rmNearestTiesToEven, losesInfo: &ignored); |
| 1013 | Constant *C = get(Context, V: FV); |
| 1014 | |
| 1015 | // For vectors, broadcast the value. |
| 1016 | if (VectorType *VTy = dyn_cast<VectorType>(Val: Ty)) |
| 1017 | return ConstantVector::getSplat(EC: VTy->getElementCount(), Elt: C); |
| 1018 | |
| 1019 | return C; |
| 1020 | } |
| 1021 | |
| 1022 | Constant *ConstantFP::get(Type *Ty, const APFloat &V) { |
| 1023 | ConstantFP *C = get(Context&: Ty->getContext(), V); |
| 1024 | assert(C->getType() == Ty->getScalarType() && |
| 1025 | "ConstantFP type doesn't match the type implied by its value!" ); |
| 1026 | |
| 1027 | // For vectors, broadcast the value. |
| 1028 | if (auto *VTy = dyn_cast<VectorType>(Val: Ty)) |
| 1029 | return ConstantVector::getSplat(EC: VTy->getElementCount(), Elt: C); |
| 1030 | |
| 1031 | return C; |
| 1032 | } |
| 1033 | |
| 1034 | Constant *ConstantFP::get(Type *Ty, StringRef Str) { |
| 1035 | LLVMContext &Context = Ty->getContext(); |
| 1036 | |
| 1037 | APFloat FV(Ty->getScalarType()->getFltSemantics(), Str); |
| 1038 | Constant *C = get(Context, V: FV); |
| 1039 | |
| 1040 | // For vectors, broadcast the value. |
| 1041 | if (VectorType *VTy = dyn_cast<VectorType>(Val: Ty)) |
| 1042 | return ConstantVector::getSplat(EC: VTy->getElementCount(), Elt: C); |
| 1043 | |
| 1044 | return C; |
| 1045 | } |
| 1046 | |
| 1047 | Constant *ConstantFP::getNaN(Type *Ty, bool Negative, uint64_t Payload) { |
| 1048 | const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics(); |
| 1049 | APFloat NaN = APFloat::getNaN(Sem: Semantics, Negative, payload: Payload); |
| 1050 | Constant *C = get(Context&: Ty->getContext(), V: NaN); |
| 1051 | |
| 1052 | if (VectorType *VTy = dyn_cast<VectorType>(Val: Ty)) |
| 1053 | return ConstantVector::getSplat(EC: VTy->getElementCount(), Elt: C); |
| 1054 | |
| 1055 | return C; |
| 1056 | } |
| 1057 | |
| 1058 | Constant *ConstantFP::getQNaN(Type *Ty, bool Negative, APInt *Payload) { |
| 1059 | const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics(); |
| 1060 | APFloat NaN = APFloat::getQNaN(Sem: Semantics, Negative, payload: Payload); |
| 1061 | Constant *C = get(Context&: Ty->getContext(), V: NaN); |
| 1062 | |
| 1063 | if (VectorType *VTy = dyn_cast<VectorType>(Val: Ty)) |
| 1064 | return ConstantVector::getSplat(EC: VTy->getElementCount(), Elt: C); |
| 1065 | |
| 1066 | return C; |
| 1067 | } |
| 1068 | |
| 1069 | Constant *ConstantFP::getSNaN(Type *Ty, bool Negative, APInt *Payload) { |
| 1070 | const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics(); |
| 1071 | APFloat NaN = APFloat::getSNaN(Sem: Semantics, Negative, payload: Payload); |
| 1072 | Constant *C = get(Context&: Ty->getContext(), V: NaN); |
| 1073 | |
| 1074 | if (VectorType *VTy = dyn_cast<VectorType>(Val: Ty)) |
| 1075 | return ConstantVector::getSplat(EC: VTy->getElementCount(), Elt: C); |
| 1076 | |
| 1077 | return C; |
| 1078 | } |
| 1079 | |
| 1080 | Constant *ConstantFP::getZero(Type *Ty, bool Negative) { |
| 1081 | const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics(); |
| 1082 | APFloat NegZero = APFloat::getZero(Sem: Semantics, Negative); |
| 1083 | Constant *C = get(Context&: Ty->getContext(), V: NegZero); |
| 1084 | |
| 1085 | if (VectorType *VTy = dyn_cast<VectorType>(Val: Ty)) |
| 1086 | return ConstantVector::getSplat(EC: VTy->getElementCount(), Elt: C); |
| 1087 | |
| 1088 | return C; |
| 1089 | } |
| 1090 | |
| 1091 | |
| 1092 | // ConstantFP accessors. |
| 1093 | ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) { |
| 1094 | LLVMContextImpl* pImpl = Context.pImpl; |
| 1095 | |
| 1096 | std::unique_ptr<ConstantFP> &Slot = pImpl->FPConstants[V]; |
| 1097 | |
| 1098 | if (!Slot) { |
| 1099 | Type *Ty = Type::getFloatingPointTy(C&: Context, S: V.getSemantics()); |
| 1100 | Slot.reset(p: new ConstantFP(Ty, V)); |
| 1101 | } |
| 1102 | |
| 1103 | return Slot.get(); |
| 1104 | } |
| 1105 | |
| 1106 | // Get a ConstantFP vector with each lane set to the same APFloat. |
| 1107 | ConstantFP *ConstantFP::get(LLVMContext &Context, ElementCount EC, |
| 1108 | const APFloat &V) { |
| 1109 | // Get an existing value or the insertion position. |
| 1110 | std::unique_ptr<ConstantFP> &Slot = |
| 1111 | Context.pImpl->FPSplatConstants[std::make_pair(x&: EC, y: V)]; |
| 1112 | if (!Slot) { |
| 1113 | Type *EltTy = Type::getFloatingPointTy(C&: Context, S: V.getSemantics()); |
| 1114 | VectorType *VTy = VectorType::get(ElementType: EltTy, EC); |
| 1115 | Slot.reset(p: new ConstantFP(VTy, V)); |
| 1116 | } |
| 1117 | |
| 1118 | #ifndef NDEBUG |
| 1119 | Type *EltTy = Type::getFloatingPointTy(Context, V.getSemantics()); |
| 1120 | VectorType *VTy = VectorType::get(EltTy, EC); |
| 1121 | assert(Slot->getType() == VTy); |
| 1122 | #endif |
| 1123 | return Slot.get(); |
| 1124 | } |
| 1125 | |
| 1126 | Constant *ConstantFP::getInfinity(Type *Ty, bool Negative) { |
| 1127 | const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics(); |
| 1128 | Constant *C = get(Context&: Ty->getContext(), V: APFloat::getInf(Sem: Semantics, Negative)); |
| 1129 | |
| 1130 | if (VectorType *VTy = dyn_cast<VectorType>(Val: Ty)) |
| 1131 | return ConstantVector::getSplat(EC: VTy->getElementCount(), Elt: C); |
| 1132 | |
| 1133 | return C; |
| 1134 | } |
| 1135 | |
| 1136 | ConstantFP::ConstantFP(Type *Ty, const APFloat &V) |
| 1137 | : ConstantData(Ty, ConstantFPVal), Val(V) { |
| 1138 | assert(&V.getSemantics() == &Ty->getScalarType()->getFltSemantics() && |
| 1139 | "FP type Mismatch" ); |
| 1140 | } |
| 1141 | |
| 1142 | bool ConstantFP::isExactlyValue(const APFloat &V) const { |
| 1143 | return Val.bitwiseIsEqual(RHS: V); |
| 1144 | } |
| 1145 | |
| 1146 | /// Remove the constant from the constant table. |
| 1147 | void ConstantFP::destroyConstantImpl() { |
| 1148 | llvm_unreachable("You can't ConstantFP->destroyConstantImpl()!" ); |
| 1149 | } |
| 1150 | |
| 1151 | //===----------------------------------------------------------------------===// |
| 1152 | // ConstantAggregateZero Implementation |
| 1153 | //===----------------------------------------------------------------------===// |
| 1154 | |
| 1155 | Constant *ConstantAggregateZero::getSequentialElement() const { |
| 1156 | if (auto *AT = dyn_cast<ArrayType>(Val: getType())) |
| 1157 | return Constant::getNullValue(Ty: AT->getElementType()); |
| 1158 | return Constant::getNullValue(Ty: cast<VectorType>(Val: getType())->getElementType()); |
| 1159 | } |
| 1160 | |
| 1161 | Constant *ConstantAggregateZero::getStructElement(unsigned Elt) const { |
| 1162 | return Constant::getNullValue(Ty: getType()->getStructElementType(N: Elt)); |
| 1163 | } |
| 1164 | |
| 1165 | Constant *ConstantAggregateZero::getElementValue(Constant *C) const { |
| 1166 | if (isa<ArrayType>(Val: getType()) || isa<VectorType>(Val: getType())) |
| 1167 | return getSequentialElement(); |
| 1168 | return getStructElement(Elt: cast<ConstantInt>(Val: C)->getZExtValue()); |
| 1169 | } |
| 1170 | |
| 1171 | Constant *ConstantAggregateZero::getElementValue(unsigned Idx) const { |
| 1172 | if (isa<ArrayType>(Val: getType()) || isa<VectorType>(Val: getType())) |
| 1173 | return getSequentialElement(); |
| 1174 | return getStructElement(Elt: Idx); |
| 1175 | } |
| 1176 | |
| 1177 | ElementCount ConstantAggregateZero::getElementCount() const { |
| 1178 | Type *Ty = getType(); |
| 1179 | if (auto *AT = dyn_cast<ArrayType>(Val: Ty)) |
| 1180 | return ElementCount::getFixed(MinVal: AT->getNumElements()); |
| 1181 | if (auto *VT = dyn_cast<VectorType>(Val: Ty)) |
| 1182 | return VT->getElementCount(); |
| 1183 | return ElementCount::getFixed(MinVal: Ty->getStructNumElements()); |
| 1184 | } |
| 1185 | |
| 1186 | //===----------------------------------------------------------------------===// |
| 1187 | // UndefValue Implementation |
| 1188 | //===----------------------------------------------------------------------===// |
| 1189 | |
| 1190 | UndefValue *UndefValue::getSequentialElement() const { |
| 1191 | if (ArrayType *ATy = dyn_cast<ArrayType>(Val: getType())) |
| 1192 | return UndefValue::get(T: ATy->getElementType()); |
| 1193 | return UndefValue::get(T: cast<VectorType>(Val: getType())->getElementType()); |
| 1194 | } |
| 1195 | |
| 1196 | UndefValue *UndefValue::getStructElement(unsigned Elt) const { |
| 1197 | return UndefValue::get(T: getType()->getStructElementType(N: Elt)); |
| 1198 | } |
| 1199 | |
| 1200 | UndefValue *UndefValue::getElementValue(Constant *C) const { |
| 1201 | if (isa<ArrayType>(Val: getType()) || isa<VectorType>(Val: getType())) |
| 1202 | return getSequentialElement(); |
| 1203 | return getStructElement(Elt: cast<ConstantInt>(Val: C)->getZExtValue()); |
| 1204 | } |
| 1205 | |
| 1206 | UndefValue *UndefValue::getElementValue(unsigned Idx) const { |
| 1207 | if (isa<ArrayType>(Val: getType()) || isa<VectorType>(Val: getType())) |
| 1208 | return getSequentialElement(); |
| 1209 | return getStructElement(Elt: Idx); |
| 1210 | } |
| 1211 | |
| 1212 | unsigned UndefValue::getNumElements() const { |
| 1213 | Type *Ty = getType(); |
| 1214 | if (auto *AT = dyn_cast<ArrayType>(Val: Ty)) |
| 1215 | return AT->getNumElements(); |
| 1216 | if (auto *VT = dyn_cast<VectorType>(Val: Ty)) |
| 1217 | return cast<FixedVectorType>(Val: VT)->getNumElements(); |
| 1218 | return Ty->getStructNumElements(); |
| 1219 | } |
| 1220 | |
| 1221 | //===----------------------------------------------------------------------===// |
| 1222 | // PoisonValue Implementation |
| 1223 | //===----------------------------------------------------------------------===// |
| 1224 | |
| 1225 | PoisonValue *PoisonValue::getSequentialElement() const { |
| 1226 | if (ArrayType *ATy = dyn_cast<ArrayType>(Val: getType())) |
| 1227 | return PoisonValue::get(T: ATy->getElementType()); |
| 1228 | return PoisonValue::get(T: cast<VectorType>(Val: getType())->getElementType()); |
| 1229 | } |
| 1230 | |
| 1231 | PoisonValue *PoisonValue::getStructElement(unsigned Elt) const { |
| 1232 | return PoisonValue::get(T: getType()->getStructElementType(N: Elt)); |
| 1233 | } |
| 1234 | |
| 1235 | PoisonValue *PoisonValue::getElementValue(Constant *C) const { |
| 1236 | if (isa<ArrayType>(Val: getType()) || isa<VectorType>(Val: getType())) |
| 1237 | return getSequentialElement(); |
| 1238 | return getStructElement(Elt: cast<ConstantInt>(Val: C)->getZExtValue()); |
| 1239 | } |
| 1240 | |
| 1241 | PoisonValue *PoisonValue::getElementValue(unsigned Idx) const { |
| 1242 | if (isa<ArrayType>(Val: getType()) || isa<VectorType>(Val: getType())) |
| 1243 | return getSequentialElement(); |
| 1244 | return getStructElement(Elt: Idx); |
| 1245 | } |
| 1246 | |
| 1247 | //===----------------------------------------------------------------------===// |
| 1248 | // ConstantXXX Classes |
| 1249 | //===----------------------------------------------------------------------===// |
| 1250 | |
| 1251 | template <typename ItTy, typename EltTy> |
| 1252 | static bool rangeOnlyContains(ItTy Start, ItTy End, EltTy Elt) { |
| 1253 | for (; Start != End; ++Start) |
| 1254 | if (*Start != Elt) |
| 1255 | return false; |
| 1256 | return true; |
| 1257 | } |
| 1258 | |
| 1259 | template <typename SequentialTy, typename ElementTy> |
| 1260 | static Constant *getIntSequenceIfElementsMatch(ArrayRef<Constant *> V) { |
| 1261 | assert(!V.empty() && "Cannot get empty int sequence." ); |
| 1262 | |
| 1263 | SmallVector<ElementTy, 16> Elts; |
| 1264 | for (Constant *C : V) |
| 1265 | if (auto *CI = dyn_cast<ConstantInt>(Val: C)) |
| 1266 | Elts.push_back(CI->getZExtValue()); |
| 1267 | else |
| 1268 | return nullptr; |
| 1269 | return SequentialTy::get(V[0]->getContext(), Elts); |
| 1270 | } |
| 1271 | |
| 1272 | template <typename SequentialTy, typename ElementTy> |
| 1273 | static Constant *getFPSequenceIfElementsMatch(ArrayRef<Constant *> V) { |
| 1274 | assert(!V.empty() && "Cannot get empty FP sequence." ); |
| 1275 | |
| 1276 | SmallVector<ElementTy, 16> Elts; |
| 1277 | for (Constant *C : V) |
| 1278 | if (auto *CFP = dyn_cast<ConstantFP>(Val: C)) |
| 1279 | Elts.push_back(CFP->getValueAPF().bitcastToAPInt().getLimitedValue()); |
| 1280 | else |
| 1281 | return nullptr; |
| 1282 | return SequentialTy::getFP(V[0]->getType(), Elts); |
| 1283 | } |
| 1284 | |
| 1285 | template <typename SequenceTy> |
| 1286 | static Constant *getSequenceIfElementsMatch(Constant *C, |
| 1287 | ArrayRef<Constant *> V) { |
| 1288 | // We speculatively build the elements here even if it turns out that there is |
| 1289 | // a constantexpr or something else weird, since it is so uncommon for that to |
| 1290 | // happen. |
| 1291 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Val: C)) { |
| 1292 | if (CI->getType()->isIntegerTy(Bitwidth: 8)) |
| 1293 | return getIntSequenceIfElementsMatch<SequenceTy, uint8_t>(V); |
| 1294 | else if (CI->getType()->isIntegerTy(Bitwidth: 16)) |
| 1295 | return getIntSequenceIfElementsMatch<SequenceTy, uint16_t>(V); |
| 1296 | else if (CI->getType()->isIntegerTy(Bitwidth: 32)) |
| 1297 | return getIntSequenceIfElementsMatch<SequenceTy, uint32_t>(V); |
| 1298 | else if (CI->getType()->isIntegerTy(Bitwidth: 64)) |
| 1299 | return getIntSequenceIfElementsMatch<SequenceTy, uint64_t>(V); |
| 1300 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(Val: C)) { |
| 1301 | if (CFP->getType()->isHalfTy() || CFP->getType()->isBFloatTy()) |
| 1302 | return getFPSequenceIfElementsMatch<SequenceTy, uint16_t>(V); |
| 1303 | else if (CFP->getType()->isFloatTy()) |
| 1304 | return getFPSequenceIfElementsMatch<SequenceTy, uint32_t>(V); |
| 1305 | else if (CFP->getType()->isDoubleTy()) |
| 1306 | return getFPSequenceIfElementsMatch<SequenceTy, uint64_t>(V); |
| 1307 | } |
| 1308 | |
| 1309 | return nullptr; |
| 1310 | } |
| 1311 | |
| 1312 | ConstantAggregate::ConstantAggregate(Type *T, ValueTy VT, |
| 1313 | ArrayRef<Constant *> V, |
| 1314 | AllocInfo AllocInfo) |
| 1315 | : Constant(T, VT, AllocInfo) { |
| 1316 | llvm::copy(Range&: V, Out: op_begin()); |
| 1317 | |
| 1318 | // Check that types match, unless this is an opaque struct. |
| 1319 | if (auto *ST = dyn_cast<StructType>(Val: T)) { |
| 1320 | if (ST->isOpaque()) |
| 1321 | return; |
| 1322 | for (unsigned I = 0, E = V.size(); I != E; ++I) |
| 1323 | assert(V[I]->getType() == ST->getTypeAtIndex(I) && |
| 1324 | "Initializer for struct element doesn't match!" ); |
| 1325 | } |
| 1326 | } |
| 1327 | |
| 1328 | ConstantArray::ConstantArray(ArrayType *T, ArrayRef<Constant *> V, |
| 1329 | AllocInfo AllocInfo) |
| 1330 | : ConstantAggregate(T, ConstantArrayVal, V, AllocInfo) { |
| 1331 | assert(V.size() == T->getNumElements() && |
| 1332 | "Invalid initializer for constant array" ); |
| 1333 | } |
| 1334 | |
| 1335 | Constant *ConstantArray::get(ArrayType *Ty, ArrayRef<Constant*> V) { |
| 1336 | if (Constant *C = getImpl(T: Ty, V)) |
| 1337 | return C; |
| 1338 | return Ty->getContext().pImpl->ArrayConstants.getOrCreate(Ty, V); |
| 1339 | } |
| 1340 | |
| 1341 | Constant *ConstantArray::getImpl(ArrayType *Ty, ArrayRef<Constant*> V) { |
| 1342 | // Empty arrays are canonicalized to ConstantAggregateZero. |
| 1343 | if (V.empty()) |
| 1344 | return ConstantAggregateZero::get(Ty); |
| 1345 | |
| 1346 | for (Constant *C : V) { |
| 1347 | assert(C->getType() == Ty->getElementType() && |
| 1348 | "Wrong type in array element initializer" ); |
| 1349 | (void)C; |
| 1350 | } |
| 1351 | |
| 1352 | // If this is an all-zero array, return a ConstantAggregateZero object. If |
| 1353 | // all undef, return an UndefValue, if "all simple", then return a |
| 1354 | // ConstantDataArray. |
| 1355 | Constant *C = V[0]; |
| 1356 | if (isa<PoisonValue>(Val: C) && rangeOnlyContains(Start: V.begin(), End: V.end(), Elt: C)) |
| 1357 | return PoisonValue::get(T: Ty); |
| 1358 | |
| 1359 | if (isa<UndefValue>(Val: C) && rangeOnlyContains(Start: V.begin(), End: V.end(), Elt: C)) |
| 1360 | return UndefValue::get(T: Ty); |
| 1361 | |
| 1362 | if (C->isNullValue() && rangeOnlyContains(Start: V.begin(), End: V.end(), Elt: C)) |
| 1363 | return ConstantAggregateZero::get(Ty); |
| 1364 | |
| 1365 | // Check to see if all of the elements are ConstantFP or ConstantInt and if |
| 1366 | // the element type is compatible with ConstantDataVector. If so, use it. |
| 1367 | if (ConstantDataSequential::isElementTypeCompatible(Ty: C->getType())) |
| 1368 | return getSequenceIfElementsMatch<ConstantDataArray>(C, V); |
| 1369 | |
| 1370 | // Otherwise, we really do want to create a ConstantArray. |
| 1371 | return nullptr; |
| 1372 | } |
| 1373 | |
| 1374 | StructType *ConstantStruct::getTypeForElements(LLVMContext &Context, |
| 1375 | ArrayRef<Constant*> V, |
| 1376 | bool Packed) { |
| 1377 | unsigned VecSize = V.size(); |
| 1378 | SmallVector<Type*, 16> EltTypes(VecSize); |
| 1379 | for (unsigned i = 0; i != VecSize; ++i) |
| 1380 | EltTypes[i] = V[i]->getType(); |
| 1381 | |
| 1382 | return StructType::get(Context, Elements: EltTypes, isPacked: Packed); |
| 1383 | } |
| 1384 | |
| 1385 | |
| 1386 | StructType *ConstantStruct::getTypeForElements(ArrayRef<Constant*> V, |
| 1387 | bool Packed) { |
| 1388 | assert(!V.empty() && |
| 1389 | "ConstantStruct::getTypeForElements cannot be called on empty list" ); |
| 1390 | return getTypeForElements(Context&: V[0]->getContext(), V, Packed); |
| 1391 | } |
| 1392 | |
| 1393 | ConstantStruct::ConstantStruct(StructType *T, ArrayRef<Constant *> V, |
| 1394 | AllocInfo AllocInfo) |
| 1395 | : ConstantAggregate(T, ConstantStructVal, V, AllocInfo) { |
| 1396 | assert((T->isOpaque() || V.size() == T->getNumElements()) && |
| 1397 | "Invalid initializer for constant struct" ); |
| 1398 | } |
| 1399 | |
| 1400 | // ConstantStruct accessors. |
| 1401 | Constant *ConstantStruct::get(StructType *ST, ArrayRef<Constant*> V) { |
| 1402 | assert((ST->isOpaque() || ST->getNumElements() == V.size()) && |
| 1403 | "Incorrect # elements specified to ConstantStruct::get" ); |
| 1404 | |
| 1405 | // Create a ConstantAggregateZero value if all elements are zeros. |
| 1406 | bool isZero = true; |
| 1407 | bool isUndef = false; |
| 1408 | bool isPoison = false; |
| 1409 | |
| 1410 | if (!V.empty()) { |
| 1411 | isUndef = isa<UndefValue>(Val: V[0]); |
| 1412 | isPoison = isa<PoisonValue>(Val: V[0]); |
| 1413 | isZero = V[0]->isNullValue(); |
| 1414 | // PoisonValue inherits UndefValue, so its check is not necessary. |
| 1415 | if (isUndef || isZero) { |
| 1416 | for (Constant *C : V) { |
| 1417 | if (!C->isNullValue()) |
| 1418 | isZero = false; |
| 1419 | if (!isa<PoisonValue>(Val: C)) |
| 1420 | isPoison = false; |
| 1421 | if (isa<PoisonValue>(Val: C) || !isa<UndefValue>(Val: C)) |
| 1422 | isUndef = false; |
| 1423 | } |
| 1424 | } |
| 1425 | } |
| 1426 | if (isZero) |
| 1427 | return ConstantAggregateZero::get(Ty: ST); |
| 1428 | if (isPoison) |
| 1429 | return PoisonValue::get(T: ST); |
| 1430 | if (isUndef) |
| 1431 | return UndefValue::get(T: ST); |
| 1432 | |
| 1433 | return ST->getContext().pImpl->StructConstants.getOrCreate(Ty: ST, V); |
| 1434 | } |
| 1435 | |
| 1436 | ConstantVector::ConstantVector(VectorType *T, ArrayRef<Constant *> V, |
| 1437 | AllocInfo AllocInfo) |
| 1438 | : ConstantAggregate(T, ConstantVectorVal, V, AllocInfo) { |
| 1439 | assert(V.size() == cast<FixedVectorType>(T)->getNumElements() && |
| 1440 | "Invalid initializer for constant vector" ); |
| 1441 | } |
| 1442 | |
| 1443 | // ConstantVector accessors. |
| 1444 | Constant *ConstantVector::get(ArrayRef<Constant*> V) { |
| 1445 | if (Constant *C = getImpl(V)) |
| 1446 | return C; |
| 1447 | auto *Ty = FixedVectorType::get(ElementType: V.front()->getType(), NumElts: V.size()); |
| 1448 | return Ty->getContext().pImpl->VectorConstants.getOrCreate(Ty, V); |
| 1449 | } |
| 1450 | |
| 1451 | Constant *ConstantVector::getImpl(ArrayRef<Constant*> V) { |
| 1452 | assert(!V.empty() && "Vectors can't be empty" ); |
| 1453 | auto *T = FixedVectorType::get(ElementType: V.front()->getType(), NumElts: V.size()); |
| 1454 | |
| 1455 | // If this is an all-undef or all-zero vector, return a |
| 1456 | // ConstantAggregateZero or UndefValue. |
| 1457 | Constant *C = V[0]; |
| 1458 | bool isZero = C->isNullValue(); |
| 1459 | bool isUndef = isa<UndefValue>(Val: C); |
| 1460 | bool isPoison = isa<PoisonValue>(Val: C); |
| 1461 | bool isSplatFP = UseConstantFPForFixedLengthSplat && isa<ConstantFP>(Val: C); |
| 1462 | bool isSplatInt = UseConstantIntForFixedLengthSplat && isa<ConstantInt>(Val: C); |
| 1463 | |
| 1464 | if (isZero || isUndef || isSplatFP || isSplatInt) { |
| 1465 | for (unsigned i = 1, e = V.size(); i != e; ++i) |
| 1466 | if (V[i] != C) { |
| 1467 | isZero = isUndef = isPoison = isSplatFP = isSplatInt = false; |
| 1468 | break; |
| 1469 | } |
| 1470 | } |
| 1471 | |
| 1472 | if (isZero) |
| 1473 | return ConstantAggregateZero::get(Ty: T); |
| 1474 | if (isPoison) |
| 1475 | return PoisonValue::get(T); |
| 1476 | if (isUndef) |
| 1477 | return UndefValue::get(T); |
| 1478 | if (isSplatFP) |
| 1479 | return ConstantFP::get(Context&: C->getContext(), EC: T->getElementCount(), |
| 1480 | V: cast<ConstantFP>(Val: C)->getValue()); |
| 1481 | if (isSplatInt) |
| 1482 | return ConstantInt::get(Context&: C->getContext(), EC: T->getElementCount(), |
| 1483 | V: cast<ConstantInt>(Val: C)->getValue()); |
| 1484 | |
| 1485 | // Check to see if all of the elements are ConstantFP or ConstantInt and if |
| 1486 | // the element type is compatible with ConstantDataVector. If so, use it. |
| 1487 | if (ConstantDataSequential::isElementTypeCompatible(Ty: C->getType())) |
| 1488 | return getSequenceIfElementsMatch<ConstantDataVector>(C, V); |
| 1489 | |
| 1490 | // Otherwise, the element type isn't compatible with ConstantDataVector, or |
| 1491 | // the operand list contains a ConstantExpr or something else strange. |
| 1492 | return nullptr; |
| 1493 | } |
| 1494 | |
| 1495 | Constant *ConstantVector::getSplat(ElementCount EC, Constant *V) { |
| 1496 | if (!EC.isScalable()) { |
| 1497 | // Maintain special handling of zero. |
| 1498 | if (!V->isNullValue()) { |
| 1499 | if (UseConstantIntForFixedLengthSplat && isa<ConstantInt>(Val: V)) |
| 1500 | return ConstantInt::get(Context&: V->getContext(), EC, |
| 1501 | V: cast<ConstantInt>(Val: V)->getValue()); |
| 1502 | if (UseConstantFPForFixedLengthSplat && isa<ConstantFP>(Val: V)) |
| 1503 | return ConstantFP::get(Context&: V->getContext(), EC, |
| 1504 | V: cast<ConstantFP>(Val: V)->getValue()); |
| 1505 | } |
| 1506 | |
| 1507 | // If this splat is compatible with ConstantDataVector, use it instead of |
| 1508 | // ConstantVector. |
| 1509 | if ((isa<ConstantFP>(Val: V) || isa<ConstantInt>(Val: V)) && |
| 1510 | ConstantDataSequential::isElementTypeCompatible(Ty: V->getType())) |
| 1511 | return ConstantDataVector::getSplat(NumElts: EC.getKnownMinValue(), Elt: V); |
| 1512 | |
| 1513 | SmallVector<Constant *, 32> Elts(EC.getKnownMinValue(), V); |
| 1514 | return get(V: Elts); |
| 1515 | } |
| 1516 | |
| 1517 | // Maintain special handling of zero. |
| 1518 | if (!V->isNullValue()) { |
| 1519 | if (UseConstantIntForScalableSplat && isa<ConstantInt>(Val: V)) |
| 1520 | return ConstantInt::get(Context&: V->getContext(), EC, |
| 1521 | V: cast<ConstantInt>(Val: V)->getValue()); |
| 1522 | if (UseConstantFPForScalableSplat && isa<ConstantFP>(Val: V)) |
| 1523 | return ConstantFP::get(Context&: V->getContext(), EC, |
| 1524 | V: cast<ConstantFP>(Val: V)->getValue()); |
| 1525 | } |
| 1526 | |
| 1527 | Type *VTy = VectorType::get(ElementType: V->getType(), EC); |
| 1528 | |
| 1529 | if (V->isNullValue()) |
| 1530 | return ConstantAggregateZero::get(Ty: VTy); |
| 1531 | if (isa<PoisonValue>(Val: V)) |
| 1532 | return PoisonValue::get(T: VTy); |
| 1533 | if (isa<UndefValue>(Val: V)) |
| 1534 | return UndefValue::get(T: VTy); |
| 1535 | |
| 1536 | Type *IdxTy = Type::getInt64Ty(C&: VTy->getContext()); |
| 1537 | |
| 1538 | // Move scalar into vector. |
| 1539 | Constant *PoisonV = PoisonValue::get(T: VTy); |
| 1540 | V = ConstantExpr::getInsertElement(Vec: PoisonV, Elt: V, Idx: ConstantInt::get(Ty: IdxTy, V: 0)); |
| 1541 | // Build shuffle mask to perform the splat. |
| 1542 | SmallVector<int, 8> Zeros(EC.getKnownMinValue(), 0); |
| 1543 | // Splat. |
| 1544 | return ConstantExpr::getShuffleVector(V1: V, V2: PoisonV, Mask: Zeros); |
| 1545 | } |
| 1546 | |
| 1547 | ConstantTokenNone *ConstantTokenNone::get(LLVMContext &Context) { |
| 1548 | LLVMContextImpl *pImpl = Context.pImpl; |
| 1549 | if (!pImpl->TheNoneToken) |
| 1550 | pImpl->TheNoneToken.reset(p: new ConstantTokenNone(Context)); |
| 1551 | return pImpl->TheNoneToken.get(); |
| 1552 | } |
| 1553 | |
| 1554 | /// Remove the constant from the constant table. |
| 1555 | void ConstantTokenNone::destroyConstantImpl() { |
| 1556 | llvm_unreachable("You can't ConstantTokenNone->destroyConstantImpl()!" ); |
| 1557 | } |
| 1558 | |
| 1559 | // Utility function for determining if a ConstantExpr is a CastOp or not. This |
| 1560 | // can't be inline because we don't want to #include Instruction.h into |
| 1561 | // Constant.h |
| 1562 | bool ConstantExpr::isCast() const { return Instruction::isCast(Opcode: getOpcode()); } |
| 1563 | |
| 1564 | ArrayRef<int> ConstantExpr::getShuffleMask() const { |
| 1565 | return cast<ShuffleVectorConstantExpr>(Val: this)->ShuffleMask; |
| 1566 | } |
| 1567 | |
| 1568 | Constant *ConstantExpr::getShuffleMaskForBitcode() const { |
| 1569 | return cast<ShuffleVectorConstantExpr>(Val: this)->ShuffleMaskForBitcode; |
| 1570 | } |
| 1571 | |
| 1572 | Constant *ConstantExpr::getWithOperands(ArrayRef<Constant *> Ops, Type *Ty, |
| 1573 | bool OnlyIfReduced, Type *SrcTy) const { |
| 1574 | assert(Ops.size() == getNumOperands() && "Operand count mismatch!" ); |
| 1575 | |
| 1576 | // If no operands changed return self. |
| 1577 | if (Ty == getType() && std::equal(first1: Ops.begin(), last1: Ops.end(), first2: op_begin())) |
| 1578 | return const_cast<ConstantExpr*>(this); |
| 1579 | |
| 1580 | Type *OnlyIfReducedTy = OnlyIfReduced ? Ty : nullptr; |
| 1581 | switch (getOpcode()) { |
| 1582 | case Instruction::Trunc: |
| 1583 | case Instruction::ZExt: |
| 1584 | case Instruction::SExt: |
| 1585 | case Instruction::FPTrunc: |
| 1586 | case Instruction::FPExt: |
| 1587 | case Instruction::UIToFP: |
| 1588 | case Instruction::SIToFP: |
| 1589 | case Instruction::FPToUI: |
| 1590 | case Instruction::FPToSI: |
| 1591 | case Instruction::PtrToAddr: |
| 1592 | case Instruction::PtrToInt: |
| 1593 | case Instruction::IntToPtr: |
| 1594 | case Instruction::BitCast: |
| 1595 | case Instruction::AddrSpaceCast: |
| 1596 | return ConstantExpr::getCast(ops: getOpcode(), C: Ops[0], Ty, OnlyIfReduced); |
| 1597 | case Instruction::InsertElement: |
| 1598 | return ConstantExpr::getInsertElement(Vec: Ops[0], Elt: Ops[1], Idx: Ops[2], |
| 1599 | OnlyIfReducedTy); |
| 1600 | case Instruction::ExtractElement: |
| 1601 | return ConstantExpr::getExtractElement(Vec: Ops[0], Idx: Ops[1], OnlyIfReducedTy); |
| 1602 | case Instruction::ShuffleVector: |
| 1603 | return ConstantExpr::getShuffleVector(V1: Ops[0], V2: Ops[1], Mask: getShuffleMask(), |
| 1604 | OnlyIfReducedTy); |
| 1605 | case Instruction::GetElementPtr: { |
| 1606 | auto *GEPO = cast<GEPOperator>(Val: this); |
| 1607 | assert(SrcTy || (Ops[0]->getType() == getOperand(0)->getType())); |
| 1608 | return ConstantExpr::getGetElementPtr( |
| 1609 | Ty: SrcTy ? SrcTy : GEPO->getSourceElementType(), C: Ops[0], IdxList: Ops.slice(N: 1), |
| 1610 | NW: GEPO->getNoWrapFlags(), InRange: GEPO->getInRange(), OnlyIfReducedTy); |
| 1611 | } |
| 1612 | default: |
| 1613 | assert(getNumOperands() == 2 && "Must be binary operator?" ); |
| 1614 | return ConstantExpr::get(Opcode: getOpcode(), C1: Ops[0], C2: Ops[1], Flags: SubclassOptionalData, |
| 1615 | OnlyIfReducedTy); |
| 1616 | } |
| 1617 | } |
| 1618 | |
| 1619 | |
| 1620 | //===----------------------------------------------------------------------===// |
| 1621 | // isValueValidForType implementations |
| 1622 | |
| 1623 | bool ConstantInt::isValueValidForType(Type *Ty, uint64_t Val) { |
| 1624 | unsigned NumBits = Ty->getIntegerBitWidth(); // assert okay |
| 1625 | if (Ty->isIntegerTy(Bitwidth: 1)) |
| 1626 | return Val == 0 || Val == 1; |
| 1627 | return isUIntN(N: NumBits, x: Val); |
| 1628 | } |
| 1629 | |
| 1630 | bool ConstantInt::isValueValidForType(Type *Ty, int64_t Val) { |
| 1631 | unsigned NumBits = Ty->getIntegerBitWidth(); |
| 1632 | if (Ty->isIntegerTy(Bitwidth: 1)) |
| 1633 | return Val == 0 || Val == 1 || Val == -1; |
| 1634 | return isIntN(N: NumBits, x: Val); |
| 1635 | } |
| 1636 | |
| 1637 | bool ConstantFP::isValueValidForType(Type *Ty, const APFloat& Val) { |
| 1638 | // convert modifies in place, so make a copy. |
| 1639 | APFloat Val2 = APFloat(Val); |
| 1640 | bool losesInfo; |
| 1641 | switch (Ty->getTypeID()) { |
| 1642 | default: |
| 1643 | return false; // These can't be represented as floating point! |
| 1644 | |
| 1645 | // FIXME rounding mode needs to be more flexible |
| 1646 | case Type::HalfTyID: { |
| 1647 | if (&Val2.getSemantics() == &APFloat::IEEEhalf()) |
| 1648 | return true; |
| 1649 | Val2.convert(ToSemantics: APFloat::IEEEhalf(), RM: APFloat::rmNearestTiesToEven, losesInfo: &losesInfo); |
| 1650 | return !losesInfo; |
| 1651 | } |
| 1652 | case Type::BFloatTyID: { |
| 1653 | if (&Val2.getSemantics() == &APFloat::BFloat()) |
| 1654 | return true; |
| 1655 | Val2.convert(ToSemantics: APFloat::BFloat(), RM: APFloat::rmNearestTiesToEven, losesInfo: &losesInfo); |
| 1656 | return !losesInfo; |
| 1657 | } |
| 1658 | case Type::FloatTyID: { |
| 1659 | if (&Val2.getSemantics() == &APFloat::IEEEsingle()) |
| 1660 | return true; |
| 1661 | Val2.convert(ToSemantics: APFloat::IEEEsingle(), RM: APFloat::rmNearestTiesToEven, losesInfo: &losesInfo); |
| 1662 | return !losesInfo; |
| 1663 | } |
| 1664 | case Type::DoubleTyID: { |
| 1665 | if (&Val2.getSemantics() == &APFloat::IEEEhalf() || |
| 1666 | &Val2.getSemantics() == &APFloat::BFloat() || |
| 1667 | &Val2.getSemantics() == &APFloat::IEEEsingle() || |
| 1668 | &Val2.getSemantics() == &APFloat::IEEEdouble()) |
| 1669 | return true; |
| 1670 | Val2.convert(ToSemantics: APFloat::IEEEdouble(), RM: APFloat::rmNearestTiesToEven, losesInfo: &losesInfo); |
| 1671 | return !losesInfo; |
| 1672 | } |
| 1673 | case Type::X86_FP80TyID: |
| 1674 | return &Val2.getSemantics() == &APFloat::IEEEhalf() || |
| 1675 | &Val2.getSemantics() == &APFloat::BFloat() || |
| 1676 | &Val2.getSemantics() == &APFloat::IEEEsingle() || |
| 1677 | &Val2.getSemantics() == &APFloat::IEEEdouble() || |
| 1678 | &Val2.getSemantics() == &APFloat::x87DoubleExtended(); |
| 1679 | case Type::FP128TyID: |
| 1680 | return &Val2.getSemantics() == &APFloat::IEEEhalf() || |
| 1681 | &Val2.getSemantics() == &APFloat::BFloat() || |
| 1682 | &Val2.getSemantics() == &APFloat::IEEEsingle() || |
| 1683 | &Val2.getSemantics() == &APFloat::IEEEdouble() || |
| 1684 | &Val2.getSemantics() == &APFloat::IEEEquad(); |
| 1685 | case Type::PPC_FP128TyID: |
| 1686 | return &Val2.getSemantics() == &APFloat::IEEEhalf() || |
| 1687 | &Val2.getSemantics() == &APFloat::BFloat() || |
| 1688 | &Val2.getSemantics() == &APFloat::IEEEsingle() || |
| 1689 | &Val2.getSemantics() == &APFloat::IEEEdouble() || |
| 1690 | &Val2.getSemantics() == &APFloat::PPCDoubleDouble(); |
| 1691 | } |
| 1692 | } |
| 1693 | |
| 1694 | |
| 1695 | //===----------------------------------------------------------------------===// |
| 1696 | // Factory Function Implementation |
| 1697 | |
| 1698 | ConstantAggregateZero *ConstantAggregateZero::get(Type *Ty) { |
| 1699 | assert((Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy()) && |
| 1700 | "Cannot create an aggregate zero of non-aggregate type!" ); |
| 1701 | |
| 1702 | std::unique_ptr<ConstantAggregateZero> &Entry = |
| 1703 | Ty->getContext().pImpl->CAZConstants[Ty]; |
| 1704 | if (!Entry) |
| 1705 | Entry.reset(p: new ConstantAggregateZero(Ty)); |
| 1706 | |
| 1707 | return Entry.get(); |
| 1708 | } |
| 1709 | |
| 1710 | /// Remove the constant from the constant table. |
| 1711 | void ConstantAggregateZero::destroyConstantImpl() { |
| 1712 | getContext().pImpl->CAZConstants.erase(Val: getType()); |
| 1713 | } |
| 1714 | |
| 1715 | /// Remove the constant from the constant table. |
| 1716 | void ConstantArray::destroyConstantImpl() { |
| 1717 | getType()->getContext().pImpl->ArrayConstants.remove(CP: this); |
| 1718 | } |
| 1719 | |
| 1720 | |
| 1721 | //---- ConstantStruct::get() implementation... |
| 1722 | // |
| 1723 | |
| 1724 | /// Remove the constant from the constant table. |
| 1725 | void ConstantStruct::destroyConstantImpl() { |
| 1726 | getType()->getContext().pImpl->StructConstants.remove(CP: this); |
| 1727 | } |
| 1728 | |
| 1729 | /// Remove the constant from the constant table. |
| 1730 | void ConstantVector::destroyConstantImpl() { |
| 1731 | getType()->getContext().pImpl->VectorConstants.remove(CP: this); |
| 1732 | } |
| 1733 | |
| 1734 | Constant *Constant::getSplatValue(bool AllowPoison) const { |
| 1735 | assert(this->getType()->isVectorTy() && "Only valid for vectors!" ); |
| 1736 | if (isa<PoisonValue>(Val: this)) |
| 1737 | return PoisonValue::get(T: cast<VectorType>(Val: getType())->getElementType()); |
| 1738 | if (isa<ConstantAggregateZero>(Val: this)) |
| 1739 | return getNullValue(Ty: cast<VectorType>(Val: getType())->getElementType()); |
| 1740 | if (auto *CI = dyn_cast<ConstantInt>(Val: this)) |
| 1741 | return ConstantInt::get(Context&: getContext(), V: CI->getValue()); |
| 1742 | if (auto *CFP = dyn_cast<ConstantFP>(Val: this)) |
| 1743 | return ConstantFP::get(Context&: getContext(), V: CFP->getValue()); |
| 1744 | if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(Val: this)) |
| 1745 | return CV->getSplatValue(); |
| 1746 | if (const ConstantVector *CV = dyn_cast<ConstantVector>(Val: this)) |
| 1747 | return CV->getSplatValue(AllowPoison); |
| 1748 | |
| 1749 | // Check if this is a constant expression splat of the form returned by |
| 1750 | // ConstantVector::getSplat() |
| 1751 | const auto *Shuf = dyn_cast<ConstantExpr>(Val: this); |
| 1752 | if (Shuf && Shuf->getOpcode() == Instruction::ShuffleVector && |
| 1753 | isa<UndefValue>(Val: Shuf->getOperand(i_nocapture: 1))) { |
| 1754 | |
| 1755 | const auto *IElt = dyn_cast<ConstantExpr>(Val: Shuf->getOperand(i_nocapture: 0)); |
| 1756 | if (IElt && IElt->getOpcode() == Instruction::InsertElement && |
| 1757 | isa<UndefValue>(Val: IElt->getOperand(i_nocapture: 0))) { |
| 1758 | |
| 1759 | ArrayRef<int> Mask = Shuf->getShuffleMask(); |
| 1760 | Constant *SplatVal = IElt->getOperand(i_nocapture: 1); |
| 1761 | ConstantInt *Index = dyn_cast<ConstantInt>(Val: IElt->getOperand(i_nocapture: 2)); |
| 1762 | |
| 1763 | if (Index && Index->getValue() == 0 && llvm::all_of(Range&: Mask, P: equal_to(Arg: 0))) |
| 1764 | return SplatVal; |
| 1765 | } |
| 1766 | } |
| 1767 | |
| 1768 | return nullptr; |
| 1769 | } |
| 1770 | |
| 1771 | Constant *ConstantVector::getSplatValue(bool AllowPoison) const { |
| 1772 | // Check out first element. |
| 1773 | Constant *Elt = getOperand(i_nocapture: 0); |
| 1774 | // Then make sure all remaining elements point to the same value. |
| 1775 | for (unsigned I = 1, E = getNumOperands(); I < E; ++I) { |
| 1776 | Constant *OpC = getOperand(i_nocapture: I); |
| 1777 | if (OpC == Elt) |
| 1778 | continue; |
| 1779 | |
| 1780 | // Strict mode: any mismatch is not a splat. |
| 1781 | if (!AllowPoison) |
| 1782 | return nullptr; |
| 1783 | |
| 1784 | // Allow poison mode: ignore poison elements. |
| 1785 | if (isa<PoisonValue>(Val: OpC)) |
| 1786 | continue; |
| 1787 | |
| 1788 | // If we do not have a defined element yet, use the current operand. |
| 1789 | if (isa<PoisonValue>(Val: Elt)) |
| 1790 | Elt = OpC; |
| 1791 | |
| 1792 | if (OpC != Elt) |
| 1793 | return nullptr; |
| 1794 | } |
| 1795 | return Elt; |
| 1796 | } |
| 1797 | |
| 1798 | const APInt &Constant::getUniqueInteger() const { |
| 1799 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val: this)) |
| 1800 | return CI->getValue(); |
| 1801 | // Scalable vectors can use a ConstantExpr to build a splat. |
| 1802 | if (isa<ConstantExpr>(Val: this)) |
| 1803 | return cast<ConstantInt>(Val: this->getSplatValue())->getValue(); |
| 1804 | // For non-ConstantExpr we use getAggregateElement as a fast path to avoid |
| 1805 | // calling getSplatValue in release builds. |
| 1806 | assert(this->getSplatValue() && "Doesn't contain a unique integer!" ); |
| 1807 | const Constant *C = this->getAggregateElement(Elt: 0U); |
| 1808 | assert(C && isa<ConstantInt>(C) && "Not a vector of numbers!" ); |
| 1809 | return cast<ConstantInt>(Val: C)->getValue(); |
| 1810 | } |
| 1811 | |
| 1812 | ConstantRange Constant::toConstantRange() const { |
| 1813 | if (auto *CI = dyn_cast<ConstantInt>(Val: this)) |
| 1814 | return ConstantRange(CI->getValue()); |
| 1815 | |
| 1816 | unsigned BitWidth = getType()->getScalarSizeInBits(); |
| 1817 | if (!getType()->isVectorTy()) |
| 1818 | return ConstantRange::getFull(BitWidth); |
| 1819 | |
| 1820 | if (auto *CI = dyn_cast_or_null<ConstantInt>( |
| 1821 | Val: getSplatValue(/*AllowPoison=*/true))) |
| 1822 | return ConstantRange(CI->getValue()); |
| 1823 | |
| 1824 | if (auto *CDV = dyn_cast<ConstantDataVector>(Val: this)) { |
| 1825 | ConstantRange CR = ConstantRange::getEmpty(BitWidth); |
| 1826 | for (unsigned I = 0, E = CDV->getNumElements(); I < E; ++I) |
| 1827 | CR = CR.unionWith(CR: CDV->getElementAsAPInt(i: I)); |
| 1828 | return CR; |
| 1829 | } |
| 1830 | |
| 1831 | if (auto *CV = dyn_cast<ConstantVector>(Val: this)) { |
| 1832 | ConstantRange CR = ConstantRange::getEmpty(BitWidth); |
| 1833 | for (unsigned I = 0, E = CV->getNumOperands(); I < E; ++I) { |
| 1834 | Constant *Elem = CV->getOperand(i_nocapture: I); |
| 1835 | if (!Elem) |
| 1836 | return ConstantRange::getFull(BitWidth); |
| 1837 | if (isa<PoisonValue>(Val: Elem)) |
| 1838 | continue; |
| 1839 | auto *CI = dyn_cast<ConstantInt>(Val: Elem); |
| 1840 | if (!CI) |
| 1841 | return ConstantRange::getFull(BitWidth); |
| 1842 | CR = CR.unionWith(CR: CI->getValue()); |
| 1843 | } |
| 1844 | return CR; |
| 1845 | } |
| 1846 | |
| 1847 | return ConstantRange::getFull(BitWidth); |
| 1848 | } |
| 1849 | |
| 1850 | //---- ConstantPointerNull::get() implementation. |
| 1851 | // |
| 1852 | |
| 1853 | ConstantPointerNull *ConstantPointerNull::get(PointerType *Ty) { |
| 1854 | std::unique_ptr<ConstantPointerNull> &Entry = |
| 1855 | Ty->getContext().pImpl->CPNConstants[Ty]; |
| 1856 | if (!Entry) |
| 1857 | Entry.reset(p: new ConstantPointerNull(Ty)); |
| 1858 | |
| 1859 | return Entry.get(); |
| 1860 | } |
| 1861 | |
| 1862 | /// Remove the constant from the constant table. |
| 1863 | void ConstantPointerNull::destroyConstantImpl() { |
| 1864 | getContext().pImpl->CPNConstants.erase(Val: getType()); |
| 1865 | } |
| 1866 | |
| 1867 | //---- ConstantTargetNone::get() implementation. |
| 1868 | // |
| 1869 | |
| 1870 | ConstantTargetNone *ConstantTargetNone::get(TargetExtType *Ty) { |
| 1871 | assert(Ty->hasProperty(TargetExtType::HasZeroInit) && |
| 1872 | "Target extension type not allowed to have a zeroinitializer" ); |
| 1873 | std::unique_ptr<ConstantTargetNone> &Entry = |
| 1874 | Ty->getContext().pImpl->CTNConstants[Ty]; |
| 1875 | if (!Entry) |
| 1876 | Entry.reset(p: new ConstantTargetNone(Ty)); |
| 1877 | |
| 1878 | return Entry.get(); |
| 1879 | } |
| 1880 | |
| 1881 | /// Remove the constant from the constant table. |
| 1882 | void ConstantTargetNone::destroyConstantImpl() { |
| 1883 | getContext().pImpl->CTNConstants.erase(Val: getType()); |
| 1884 | } |
| 1885 | |
| 1886 | UndefValue *UndefValue::get(Type *Ty) { |
| 1887 | std::unique_ptr<UndefValue> &Entry = Ty->getContext().pImpl->UVConstants[Ty]; |
| 1888 | if (!Entry) |
| 1889 | Entry.reset(p: new UndefValue(Ty)); |
| 1890 | |
| 1891 | return Entry.get(); |
| 1892 | } |
| 1893 | |
| 1894 | /// Remove the constant from the constant table. |
| 1895 | void UndefValue::destroyConstantImpl() { |
| 1896 | // Free the constant and any dangling references to it. |
| 1897 | if (getValueID() == UndefValueVal) { |
| 1898 | getContext().pImpl->UVConstants.erase(Val: getType()); |
| 1899 | } else if (getValueID() == PoisonValueVal) { |
| 1900 | getContext().pImpl->PVConstants.erase(Val: getType()); |
| 1901 | } |
| 1902 | llvm_unreachable("Not a undef or a poison!" ); |
| 1903 | } |
| 1904 | |
| 1905 | PoisonValue *PoisonValue::get(Type *Ty) { |
| 1906 | std::unique_ptr<PoisonValue> &Entry = Ty->getContext().pImpl->PVConstants[Ty]; |
| 1907 | if (!Entry) |
| 1908 | Entry.reset(p: new PoisonValue(Ty)); |
| 1909 | |
| 1910 | return Entry.get(); |
| 1911 | } |
| 1912 | |
| 1913 | /// Remove the constant from the constant table. |
| 1914 | void PoisonValue::destroyConstantImpl() { |
| 1915 | // Free the constant and any dangling references to it. |
| 1916 | getContext().pImpl->PVConstants.erase(Val: getType()); |
| 1917 | } |
| 1918 | |
| 1919 | BlockAddress *BlockAddress::get(Type *Ty, BasicBlock *BB) { |
| 1920 | BlockAddress *&BA = BB->getContext().pImpl->BlockAddresses[BB]; |
| 1921 | if (!BA) |
| 1922 | BA = new BlockAddress(Ty, BB); |
| 1923 | return BA; |
| 1924 | } |
| 1925 | |
| 1926 | BlockAddress *BlockAddress::get(BasicBlock *BB) { |
| 1927 | assert(BB->getParent() && "Block must have a parent" ); |
| 1928 | return get(Ty: BB->getParent()->getType(), BB); |
| 1929 | } |
| 1930 | |
| 1931 | BlockAddress *BlockAddress::get(Function *F, BasicBlock *BB) { |
| 1932 | assert(BB->getParent() == F && "Block not part of specified function" ); |
| 1933 | return get(Ty: BB->getParent()->getType(), BB); |
| 1934 | } |
| 1935 | |
| 1936 | BlockAddress::BlockAddress(Type *Ty, BasicBlock *BB) |
| 1937 | : Constant(Ty, Value::BlockAddressVal, AllocMarker) { |
| 1938 | setOperand(i_nocapture: 0, Val_nocapture: BB); |
| 1939 | BB->setHasAddressTaken(true); |
| 1940 | } |
| 1941 | |
| 1942 | BlockAddress *BlockAddress::lookup(const BasicBlock *BB) { |
| 1943 | if (!BB->hasAddressTaken()) |
| 1944 | return nullptr; |
| 1945 | |
| 1946 | BlockAddress *BA = BB->getContext().pImpl->BlockAddresses.lookup(Val: BB); |
| 1947 | assert(BA && "Refcount and block address map disagree!" ); |
| 1948 | return BA; |
| 1949 | } |
| 1950 | |
| 1951 | /// Remove the constant from the constant table. |
| 1952 | void BlockAddress::destroyConstantImpl() { |
| 1953 | getType()->getContext().pImpl->BlockAddresses.erase(Val: getBasicBlock()); |
| 1954 | getBasicBlock()->setHasAddressTaken(false); |
| 1955 | } |
| 1956 | |
| 1957 | Value *BlockAddress::handleOperandChangeImpl(Value *From, Value *To) { |
| 1958 | assert(From == getBasicBlock()); |
| 1959 | BasicBlock *NewBB = cast<BasicBlock>(Val: To); |
| 1960 | |
| 1961 | // See if the 'new' entry already exists, if not, just update this in place |
| 1962 | // and return early. |
| 1963 | BlockAddress *&NewBA = getContext().pImpl->BlockAddresses[NewBB]; |
| 1964 | if (NewBA) |
| 1965 | return NewBA; |
| 1966 | |
| 1967 | getBasicBlock()->setHasAddressTaken(false); |
| 1968 | |
| 1969 | // Remove the old entry, this can't cause the map to rehash (just a |
| 1970 | // tombstone will get added). |
| 1971 | getContext().pImpl->BlockAddresses.erase(Val: getBasicBlock()); |
| 1972 | NewBA = this; |
| 1973 | setOperand(i_nocapture: 0, Val_nocapture: NewBB); |
| 1974 | getBasicBlock()->setHasAddressTaken(true); |
| 1975 | |
| 1976 | // If we just want to keep the existing value, then return null. |
| 1977 | // Callers know that this means we shouldn't delete this value. |
| 1978 | return nullptr; |
| 1979 | } |
| 1980 | |
| 1981 | DSOLocalEquivalent *DSOLocalEquivalent::get(GlobalValue *GV) { |
| 1982 | DSOLocalEquivalent *&Equiv = GV->getContext().pImpl->DSOLocalEquivalents[GV]; |
| 1983 | if (!Equiv) |
| 1984 | Equiv = new DSOLocalEquivalent(GV); |
| 1985 | |
| 1986 | assert(Equiv->getGlobalValue() == GV && |
| 1987 | "DSOLocalFunction does not match the expected global value" ); |
| 1988 | return Equiv; |
| 1989 | } |
| 1990 | |
| 1991 | DSOLocalEquivalent::DSOLocalEquivalent(GlobalValue *GV) |
| 1992 | : Constant(GV->getType(), Value::DSOLocalEquivalentVal, AllocMarker) { |
| 1993 | setOperand(i_nocapture: 0, Val_nocapture: GV); |
| 1994 | } |
| 1995 | |
| 1996 | /// Remove the constant from the constant table. |
| 1997 | void DSOLocalEquivalent::destroyConstantImpl() { |
| 1998 | const GlobalValue *GV = getGlobalValue(); |
| 1999 | GV->getContext().pImpl->DSOLocalEquivalents.erase(Val: GV); |
| 2000 | } |
| 2001 | |
| 2002 | Value *DSOLocalEquivalent::handleOperandChangeImpl(Value *From, Value *To) { |
| 2003 | assert(From == getGlobalValue() && "Changing value does not match operand." ); |
| 2004 | assert(isa<Constant>(To) && "Can only replace the operands with a constant" ); |
| 2005 | |
| 2006 | // The replacement is with another global value. |
| 2007 | if (const auto *ToObj = dyn_cast<GlobalValue>(Val: To)) { |
| 2008 | DSOLocalEquivalent *&NewEquiv = |
| 2009 | getContext().pImpl->DSOLocalEquivalents[ToObj]; |
| 2010 | if (NewEquiv) |
| 2011 | return llvm::ConstantExpr::getBitCast(C: NewEquiv, Ty: getType()); |
| 2012 | } |
| 2013 | |
| 2014 | // If the argument is replaced with a null value, just replace this constant |
| 2015 | // with a null value. |
| 2016 | if (cast<Constant>(Val: To)->isNullValue()) |
| 2017 | return To; |
| 2018 | |
| 2019 | // The replacement could be a bitcast or an alias to another function. We can |
| 2020 | // replace it with a bitcast to the dso_local_equivalent of that function. |
| 2021 | auto *Func = cast<Function>(Val: To->stripPointerCastsAndAliases()); |
| 2022 | DSOLocalEquivalent *&NewEquiv = getContext().pImpl->DSOLocalEquivalents[Func]; |
| 2023 | if (NewEquiv) |
| 2024 | return llvm::ConstantExpr::getBitCast(C: NewEquiv, Ty: getType()); |
| 2025 | |
| 2026 | // Replace this with the new one. |
| 2027 | getContext().pImpl->DSOLocalEquivalents.erase(Val: getGlobalValue()); |
| 2028 | NewEquiv = this; |
| 2029 | setOperand(i_nocapture: 0, Val_nocapture: Func); |
| 2030 | |
| 2031 | if (Func->getType() != getType()) { |
| 2032 | // It is ok to mutate the type here because this constant should always |
| 2033 | // reflect the type of the function it's holding. |
| 2034 | mutateType(Ty: Func->getType()); |
| 2035 | } |
| 2036 | return nullptr; |
| 2037 | } |
| 2038 | |
| 2039 | NoCFIValue *NoCFIValue::get(GlobalValue *GV) { |
| 2040 | NoCFIValue *&NC = GV->getContext().pImpl->NoCFIValues[GV]; |
| 2041 | if (!NC) |
| 2042 | NC = new NoCFIValue(GV); |
| 2043 | |
| 2044 | assert(NC->getGlobalValue() == GV && |
| 2045 | "NoCFIValue does not match the expected global value" ); |
| 2046 | return NC; |
| 2047 | } |
| 2048 | |
| 2049 | NoCFIValue::NoCFIValue(GlobalValue *GV) |
| 2050 | : Constant(GV->getType(), Value::NoCFIValueVal, AllocMarker) { |
| 2051 | setOperand(i_nocapture: 0, Val_nocapture: GV); |
| 2052 | } |
| 2053 | |
| 2054 | /// Remove the constant from the constant table. |
| 2055 | void NoCFIValue::destroyConstantImpl() { |
| 2056 | const GlobalValue *GV = getGlobalValue(); |
| 2057 | GV->getContext().pImpl->NoCFIValues.erase(Val: GV); |
| 2058 | } |
| 2059 | |
| 2060 | Value *NoCFIValue::handleOperandChangeImpl(Value *From, Value *To) { |
| 2061 | assert(From == getGlobalValue() && "Changing value does not match operand." ); |
| 2062 | |
| 2063 | GlobalValue *GV = dyn_cast<GlobalValue>(Val: To->stripPointerCasts()); |
| 2064 | assert(GV && "Can only replace the operands with a global value" ); |
| 2065 | |
| 2066 | NoCFIValue *&NewNC = getContext().pImpl->NoCFIValues[GV]; |
| 2067 | if (NewNC) |
| 2068 | return llvm::ConstantExpr::getBitCast(C: NewNC, Ty: getType()); |
| 2069 | |
| 2070 | getContext().pImpl->NoCFIValues.erase(Val: getGlobalValue()); |
| 2071 | NewNC = this; |
| 2072 | setOperand(i_nocapture: 0, Val_nocapture: GV); |
| 2073 | |
| 2074 | if (GV->getType() != getType()) |
| 2075 | mutateType(Ty: GV->getType()); |
| 2076 | |
| 2077 | return nullptr; |
| 2078 | } |
| 2079 | |
| 2080 | //---- ConstantPtrAuth::get() implementations. |
| 2081 | // |
| 2082 | |
| 2083 | ConstantPtrAuth *ConstantPtrAuth::get(Constant *Ptr, ConstantInt *Key, |
| 2084 | ConstantInt *Disc, Constant *AddrDisc, |
| 2085 | Constant *DeactivationSymbol) { |
| 2086 | Constant *ArgVec[] = {Ptr, Key, Disc, AddrDisc, DeactivationSymbol}; |
| 2087 | ConstantPtrAuthKeyType MapKey(ArgVec); |
| 2088 | LLVMContextImpl *pImpl = Ptr->getContext().pImpl; |
| 2089 | return pImpl->ConstantPtrAuths.getOrCreate(Ty: Ptr->getType(), V: MapKey); |
| 2090 | } |
| 2091 | |
| 2092 | ConstantPtrAuth *ConstantPtrAuth::getWithSameSchema(Constant *Pointer) const { |
| 2093 | return get(Ptr: Pointer, Key: getKey(), Disc: getDiscriminator(), AddrDisc: getAddrDiscriminator(), |
| 2094 | DeactivationSymbol: getDeactivationSymbol()); |
| 2095 | } |
| 2096 | |
| 2097 | ConstantPtrAuth::ConstantPtrAuth(Constant *Ptr, ConstantInt *Key, |
| 2098 | ConstantInt *Disc, Constant *AddrDisc, |
| 2099 | Constant *DeactivationSymbol) |
| 2100 | : Constant(Ptr->getType(), Value::ConstantPtrAuthVal, AllocMarker) { |
| 2101 | assert(Ptr->getType()->isPointerTy()); |
| 2102 | assert(Key->getBitWidth() == 32); |
| 2103 | assert(Disc->getBitWidth() == 64); |
| 2104 | assert(AddrDisc->getType()->isPointerTy()); |
| 2105 | assert(DeactivationSymbol->getType()->isPointerTy()); |
| 2106 | setOperand(i_nocapture: 0, Val_nocapture: Ptr); |
| 2107 | setOperand(i_nocapture: 1, Val_nocapture: Key); |
| 2108 | setOperand(i_nocapture: 2, Val_nocapture: Disc); |
| 2109 | setOperand(i_nocapture: 3, Val_nocapture: AddrDisc); |
| 2110 | setOperand(i_nocapture: 4, Val_nocapture: DeactivationSymbol); |
| 2111 | } |
| 2112 | |
| 2113 | /// Remove the constant from the constant table. |
| 2114 | void ConstantPtrAuth::destroyConstantImpl() { |
| 2115 | getType()->getContext().pImpl->ConstantPtrAuths.remove(CP: this); |
| 2116 | } |
| 2117 | |
| 2118 | Value *ConstantPtrAuth::handleOperandChangeImpl(Value *From, Value *ToV) { |
| 2119 | assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!" ); |
| 2120 | Constant *To = cast<Constant>(Val: ToV); |
| 2121 | |
| 2122 | SmallVector<Constant *, 4> Values; |
| 2123 | Values.reserve(N: getNumOperands()); |
| 2124 | |
| 2125 | unsigned NumUpdated = 0; |
| 2126 | |
| 2127 | Use *OperandList = getOperandList(); |
| 2128 | unsigned OperandNo = 0; |
| 2129 | for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O) { |
| 2130 | Constant *Val = cast<Constant>(Val: O->get()); |
| 2131 | if (Val == From) { |
| 2132 | OperandNo = (O - OperandList); |
| 2133 | Val = To; |
| 2134 | ++NumUpdated; |
| 2135 | } |
| 2136 | Values.push_back(Elt: Val); |
| 2137 | } |
| 2138 | |
| 2139 | return getContext().pImpl->ConstantPtrAuths.replaceOperandsInPlace( |
| 2140 | Operands: Values, CP: this, From, To, NumUpdated, OperandNo); |
| 2141 | } |
| 2142 | |
| 2143 | bool ConstantPtrAuth::hasSpecialAddressDiscriminator(uint64_t Value) const { |
| 2144 | const auto *CastV = dyn_cast<ConstantExpr>(Val: getAddrDiscriminator()); |
| 2145 | if (!CastV || CastV->getOpcode() != Instruction::IntToPtr) |
| 2146 | return false; |
| 2147 | |
| 2148 | const auto *IntVal = dyn_cast<ConstantInt>(Val: CastV->getOperand(i_nocapture: 0)); |
| 2149 | if (!IntVal) |
| 2150 | return false; |
| 2151 | |
| 2152 | return IntVal->getValue() == Value; |
| 2153 | } |
| 2154 | |
| 2155 | bool ConstantPtrAuth::isKnownCompatibleWith(const Value *Key, |
| 2156 | const Value *Discriminator, |
| 2157 | const DataLayout &DL) const { |
| 2158 | // This function may only be validly called to analyze a ptrauth operation |
| 2159 | // with no deactivation symbol, so if we have one it isn't compatible. |
| 2160 | if (!getDeactivationSymbol()->isNullValue()) |
| 2161 | return false; |
| 2162 | |
| 2163 | // If the keys are different, there's no chance for this to be compatible. |
| 2164 | if (getKey() != Key) |
| 2165 | return false; |
| 2166 | |
| 2167 | // We can have 3 kinds of discriminators: |
| 2168 | // - simple, integer-only: `i64 x, ptr null` vs. `i64 x` |
| 2169 | // - address-only: `i64 0, ptr p` vs. `ptr p` |
| 2170 | // - blended address/integer: `i64 x, ptr p` vs. `@llvm.ptrauth.blend(p, x)` |
| 2171 | |
| 2172 | // If this constant has a simple discriminator (integer, no address), easy: |
| 2173 | // it's compatible iff the provided full discriminator is also a simple |
| 2174 | // discriminator, identical to our integer discriminator. |
| 2175 | if (!hasAddressDiscriminator()) |
| 2176 | return getDiscriminator() == Discriminator; |
| 2177 | |
| 2178 | // Otherwise, we can isolate address and integer discriminator components. |
| 2179 | const Value *AddrDiscriminator = nullptr; |
| 2180 | |
| 2181 | // This constant may or may not have an integer discriminator (instead of 0). |
| 2182 | if (!getDiscriminator()->isNullValue()) { |
| 2183 | // If it does, there's an implicit blend. We need to have a matching blend |
| 2184 | // intrinsic in the provided full discriminator. |
| 2185 | if (!match(V: Discriminator, |
| 2186 | P: m_Intrinsic<Intrinsic::ptrauth_blend>( |
| 2187 | Op0: m_Value(V&: AddrDiscriminator), Op1: m_Specific(V: getDiscriminator())))) |
| 2188 | return false; |
| 2189 | } else { |
| 2190 | // Otherwise, interpret the provided full discriminator as address-only. |
| 2191 | AddrDiscriminator = Discriminator; |
| 2192 | } |
| 2193 | |
| 2194 | // Either way, we can now focus on comparing the address discriminators. |
| 2195 | |
| 2196 | // Discriminators are i64, so the provided addr disc may be a ptrtoint. |
| 2197 | if (auto *Cast = dyn_cast<PtrToIntOperator>(Val: AddrDiscriminator)) |
| 2198 | AddrDiscriminator = Cast->getPointerOperand(); |
| 2199 | |
| 2200 | // Beyond that, we're only interested in compatible pointers. |
| 2201 | if (getAddrDiscriminator()->getType() != AddrDiscriminator->getType()) |
| 2202 | return false; |
| 2203 | |
| 2204 | // These are often the same constant GEP, making them trivially equivalent. |
| 2205 | if (getAddrDiscriminator() == AddrDiscriminator) |
| 2206 | return true; |
| 2207 | |
| 2208 | // Finally, they may be equivalent base+offset expressions. |
| 2209 | APInt Off1(DL.getIndexTypeSizeInBits(Ty: getAddrDiscriminator()->getType()), 0); |
| 2210 | auto *Base1 = getAddrDiscriminator()->stripAndAccumulateConstantOffsets( |
| 2211 | DL, Offset&: Off1, /*AllowNonInbounds=*/true); |
| 2212 | |
| 2213 | APInt Off2(DL.getIndexTypeSizeInBits(Ty: AddrDiscriminator->getType()), 0); |
| 2214 | auto *Base2 = AddrDiscriminator->stripAndAccumulateConstantOffsets( |
| 2215 | DL, Offset&: Off2, /*AllowNonInbounds=*/true); |
| 2216 | |
| 2217 | return Base1 == Base2 && Off1 == Off2; |
| 2218 | } |
| 2219 | |
| 2220 | //---- ConstantExpr::get() implementations. |
| 2221 | // |
| 2222 | |
| 2223 | /// This is a utility function to handle folding of casts and lookup of the |
| 2224 | /// cast in the ExprConstants map. It is used by the various get* methods below. |
| 2225 | static Constant *getFoldedCast(Instruction::CastOps opc, Constant *C, Type *Ty, |
| 2226 | bool OnlyIfReduced = false) { |
| 2227 | assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!" ); |
| 2228 | // Fold a few common cases |
| 2229 | if (Constant *FC = ConstantFoldCastInstruction(opcode: opc, V: C, DestTy: Ty)) |
| 2230 | return FC; |
| 2231 | |
| 2232 | if (OnlyIfReduced) |
| 2233 | return nullptr; |
| 2234 | |
| 2235 | LLVMContextImpl *pImpl = Ty->getContext().pImpl; |
| 2236 | |
| 2237 | // Look up the constant in the table first to ensure uniqueness. |
| 2238 | ConstantExprKeyType Key(opc, C); |
| 2239 | |
| 2240 | return pImpl->ExprConstants.getOrCreate(Ty, V: Key); |
| 2241 | } |
| 2242 | |
| 2243 | Constant *ConstantExpr::getCast(unsigned oc, Constant *C, Type *Ty, |
| 2244 | bool OnlyIfReduced) { |
| 2245 | Instruction::CastOps opc = Instruction::CastOps(oc); |
| 2246 | assert(Instruction::isCast(opc) && "opcode out of range" ); |
| 2247 | assert(isSupportedCastOp(opc) && |
| 2248 | "Cast opcode not supported as constant expression" ); |
| 2249 | assert(C && Ty && "Null arguments to getCast" ); |
| 2250 | assert(CastInst::castIsValid(opc, C, Ty) && "Invalid constantexpr cast!" ); |
| 2251 | |
| 2252 | switch (opc) { |
| 2253 | default: |
| 2254 | llvm_unreachable("Invalid cast opcode" ); |
| 2255 | case Instruction::Trunc: |
| 2256 | return getTrunc(C, Ty, OnlyIfReduced); |
| 2257 | case Instruction::PtrToAddr: |
| 2258 | return getPtrToAddr(C, Ty, OnlyIfReduced); |
| 2259 | case Instruction::PtrToInt: |
| 2260 | return getPtrToInt(C, Ty, OnlyIfReduced); |
| 2261 | case Instruction::IntToPtr: |
| 2262 | return getIntToPtr(C, Ty, OnlyIfReduced); |
| 2263 | case Instruction::BitCast: |
| 2264 | return getBitCast(C, Ty, OnlyIfReduced); |
| 2265 | case Instruction::AddrSpaceCast: |
| 2266 | return getAddrSpaceCast(C, Ty, OnlyIfReduced); |
| 2267 | } |
| 2268 | } |
| 2269 | |
| 2270 | Constant *ConstantExpr::getTruncOrBitCast(Constant *C, Type *Ty) { |
| 2271 | if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
| 2272 | return getBitCast(C, Ty); |
| 2273 | return getTrunc(C, Ty); |
| 2274 | } |
| 2275 | |
| 2276 | Constant *ConstantExpr::getPointerCast(Constant *S, Type *Ty) { |
| 2277 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast" ); |
| 2278 | assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) && |
| 2279 | "Invalid cast" ); |
| 2280 | |
| 2281 | if (Ty->isIntOrIntVectorTy()) |
| 2282 | return getPtrToInt(C: S, Ty); |
| 2283 | |
| 2284 | unsigned SrcAS = S->getType()->getPointerAddressSpace(); |
| 2285 | if (Ty->isPtrOrPtrVectorTy() && SrcAS != Ty->getPointerAddressSpace()) |
| 2286 | return getAddrSpaceCast(C: S, Ty); |
| 2287 | |
| 2288 | return getBitCast(C: S, Ty); |
| 2289 | } |
| 2290 | |
| 2291 | Constant *ConstantExpr::getPointerBitCastOrAddrSpaceCast(Constant *S, |
| 2292 | Type *Ty) { |
| 2293 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast" ); |
| 2294 | assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast" ); |
| 2295 | |
| 2296 | if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace()) |
| 2297 | return getAddrSpaceCast(C: S, Ty); |
| 2298 | |
| 2299 | return getBitCast(C: S, Ty); |
| 2300 | } |
| 2301 | |
| 2302 | Constant *ConstantExpr::getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) { |
| 2303 | #ifndef NDEBUG |
| 2304 | bool fromVec = isa<VectorType>(C->getType()); |
| 2305 | bool toVec = isa<VectorType>(Ty); |
| 2306 | #endif |
| 2307 | assert((fromVec == toVec) && "Cannot convert from scalar to/from vector" ); |
| 2308 | assert(C->getType()->isIntOrIntVectorTy() && "Trunc operand must be integer" ); |
| 2309 | assert(Ty->isIntOrIntVectorTy() && "Trunc produces only integral" ); |
| 2310 | assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&& |
| 2311 | "SrcTy must be larger than DestTy for Trunc!" ); |
| 2312 | |
| 2313 | return getFoldedCast(opc: Instruction::Trunc, C, Ty, OnlyIfReduced); |
| 2314 | } |
| 2315 | |
| 2316 | Constant *ConstantExpr::getPtrToAddr(Constant *C, Type *DstTy, |
| 2317 | bool OnlyIfReduced) { |
| 2318 | assert(C->getType()->isPtrOrPtrVectorTy() && |
| 2319 | "PtrToAddr source must be pointer or pointer vector" ); |
| 2320 | assert(DstTy->isIntOrIntVectorTy() && |
| 2321 | "PtrToAddr destination must be integer or integer vector" ); |
| 2322 | assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy)); |
| 2323 | if (isa<VectorType>(Val: C->getType())) |
| 2324 | assert(cast<VectorType>(C->getType())->getElementCount() == |
| 2325 | cast<VectorType>(DstTy)->getElementCount() && |
| 2326 | "Invalid cast between a different number of vector elements" ); |
| 2327 | return getFoldedCast(opc: Instruction::PtrToAddr, C, Ty: DstTy, OnlyIfReduced); |
| 2328 | } |
| 2329 | |
| 2330 | Constant *ConstantExpr::getPtrToInt(Constant *C, Type *DstTy, |
| 2331 | bool OnlyIfReduced) { |
| 2332 | assert(C->getType()->isPtrOrPtrVectorTy() && |
| 2333 | "PtrToInt source must be pointer or pointer vector" ); |
| 2334 | assert(DstTy->isIntOrIntVectorTy() && |
| 2335 | "PtrToInt destination must be integer or integer vector" ); |
| 2336 | assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy)); |
| 2337 | if (isa<VectorType>(Val: C->getType())) |
| 2338 | assert(cast<VectorType>(C->getType())->getElementCount() == |
| 2339 | cast<VectorType>(DstTy)->getElementCount() && |
| 2340 | "Invalid cast between a different number of vector elements" ); |
| 2341 | return getFoldedCast(opc: Instruction::PtrToInt, C, Ty: DstTy, OnlyIfReduced); |
| 2342 | } |
| 2343 | |
| 2344 | Constant *ConstantExpr::getIntToPtr(Constant *C, Type *DstTy, |
| 2345 | bool OnlyIfReduced) { |
| 2346 | assert(C->getType()->isIntOrIntVectorTy() && |
| 2347 | "IntToPtr source must be integer or integer vector" ); |
| 2348 | assert(DstTy->isPtrOrPtrVectorTy() && |
| 2349 | "IntToPtr destination must be a pointer or pointer vector" ); |
| 2350 | assert(isa<VectorType>(C->getType()) == isa<VectorType>(DstTy)); |
| 2351 | if (isa<VectorType>(Val: C->getType())) |
| 2352 | assert(cast<VectorType>(C->getType())->getElementCount() == |
| 2353 | cast<VectorType>(DstTy)->getElementCount() && |
| 2354 | "Invalid cast between a different number of vector elements" ); |
| 2355 | return getFoldedCast(opc: Instruction::IntToPtr, C, Ty: DstTy, OnlyIfReduced); |
| 2356 | } |
| 2357 | |
| 2358 | Constant *ConstantExpr::getBitCast(Constant *C, Type *DstTy, |
| 2359 | bool OnlyIfReduced) { |
| 2360 | assert(CastInst::castIsValid(Instruction::BitCast, C, DstTy) && |
| 2361 | "Invalid constantexpr bitcast!" ); |
| 2362 | |
| 2363 | // It is common to ask for a bitcast of a value to its own type, handle this |
| 2364 | // speedily. |
| 2365 | if (C->getType() == DstTy) return C; |
| 2366 | |
| 2367 | return getFoldedCast(opc: Instruction::BitCast, C, Ty: DstTy, OnlyIfReduced); |
| 2368 | } |
| 2369 | |
| 2370 | Constant *ConstantExpr::getAddrSpaceCast(Constant *C, Type *DstTy, |
| 2371 | bool OnlyIfReduced) { |
| 2372 | assert(CastInst::castIsValid(Instruction::AddrSpaceCast, C, DstTy) && |
| 2373 | "Invalid constantexpr addrspacecast!" ); |
| 2374 | return getFoldedCast(opc: Instruction::AddrSpaceCast, C, Ty: DstTy, OnlyIfReduced); |
| 2375 | } |
| 2376 | |
| 2377 | Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2, |
| 2378 | unsigned Flags, Type *OnlyIfReducedTy) { |
| 2379 | // Check the operands for consistency first. |
| 2380 | assert(Instruction::isBinaryOp(Opcode) && |
| 2381 | "Invalid opcode in binary constant expression" ); |
| 2382 | assert(isSupportedBinOp(Opcode) && |
| 2383 | "Binop not supported as constant expression" ); |
| 2384 | assert(C1->getType() == C2->getType() && |
| 2385 | "Operand types in binary constant expression should match" ); |
| 2386 | |
| 2387 | #ifndef NDEBUG |
| 2388 | switch (Opcode) { |
| 2389 | case Instruction::Add: |
| 2390 | case Instruction::Sub: |
| 2391 | case Instruction::Mul: |
| 2392 | assert(C1->getType()->isIntOrIntVectorTy() && |
| 2393 | "Tried to create an integer operation on a non-integer type!" ); |
| 2394 | break; |
| 2395 | case Instruction::And: |
| 2396 | case Instruction::Or: |
| 2397 | case Instruction::Xor: |
| 2398 | assert(C1->getType()->isIntOrIntVectorTy() && |
| 2399 | "Tried to create a logical operation on a non-integral type!" ); |
| 2400 | break; |
| 2401 | default: |
| 2402 | break; |
| 2403 | } |
| 2404 | #endif |
| 2405 | |
| 2406 | if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, V1: C1, V2: C2)) |
| 2407 | return FC; |
| 2408 | |
| 2409 | if (OnlyIfReducedTy == C1->getType()) |
| 2410 | return nullptr; |
| 2411 | |
| 2412 | Constant *ArgVec[] = {C1, C2}; |
| 2413 | ConstantExprKeyType Key(Opcode, ArgVec, Flags); |
| 2414 | |
| 2415 | LLVMContextImpl *pImpl = C1->getContext().pImpl; |
| 2416 | return pImpl->ExprConstants.getOrCreate(Ty: C1->getType(), V: Key); |
| 2417 | } |
| 2418 | |
| 2419 | bool ConstantExpr::isDesirableBinOp(unsigned Opcode) { |
| 2420 | switch (Opcode) { |
| 2421 | case Instruction::UDiv: |
| 2422 | case Instruction::SDiv: |
| 2423 | case Instruction::URem: |
| 2424 | case Instruction::SRem: |
| 2425 | case Instruction::FAdd: |
| 2426 | case Instruction::FSub: |
| 2427 | case Instruction::FMul: |
| 2428 | case Instruction::FDiv: |
| 2429 | case Instruction::FRem: |
| 2430 | case Instruction::And: |
| 2431 | case Instruction::Or: |
| 2432 | case Instruction::LShr: |
| 2433 | case Instruction::AShr: |
| 2434 | case Instruction::Shl: |
| 2435 | case Instruction::Mul: |
| 2436 | return false; |
| 2437 | case Instruction::Add: |
| 2438 | case Instruction::Sub: |
| 2439 | case Instruction::Xor: |
| 2440 | return true; |
| 2441 | default: |
| 2442 | llvm_unreachable("Argument must be binop opcode" ); |
| 2443 | } |
| 2444 | } |
| 2445 | |
| 2446 | bool ConstantExpr::isSupportedBinOp(unsigned Opcode) { |
| 2447 | switch (Opcode) { |
| 2448 | case Instruction::UDiv: |
| 2449 | case Instruction::SDiv: |
| 2450 | case Instruction::URem: |
| 2451 | case Instruction::SRem: |
| 2452 | case Instruction::FAdd: |
| 2453 | case Instruction::FSub: |
| 2454 | case Instruction::FMul: |
| 2455 | case Instruction::FDiv: |
| 2456 | case Instruction::FRem: |
| 2457 | case Instruction::And: |
| 2458 | case Instruction::Or: |
| 2459 | case Instruction::LShr: |
| 2460 | case Instruction::AShr: |
| 2461 | case Instruction::Shl: |
| 2462 | case Instruction::Mul: |
| 2463 | return false; |
| 2464 | case Instruction::Add: |
| 2465 | case Instruction::Sub: |
| 2466 | case Instruction::Xor: |
| 2467 | return true; |
| 2468 | default: |
| 2469 | llvm_unreachable("Argument must be binop opcode" ); |
| 2470 | } |
| 2471 | } |
| 2472 | |
| 2473 | bool ConstantExpr::isDesirableCastOp(unsigned Opcode) { |
| 2474 | switch (Opcode) { |
| 2475 | case Instruction::ZExt: |
| 2476 | case Instruction::SExt: |
| 2477 | case Instruction::FPTrunc: |
| 2478 | case Instruction::FPExt: |
| 2479 | case Instruction::UIToFP: |
| 2480 | case Instruction::SIToFP: |
| 2481 | case Instruction::FPToUI: |
| 2482 | case Instruction::FPToSI: |
| 2483 | return false; |
| 2484 | case Instruction::Trunc: |
| 2485 | case Instruction::PtrToAddr: |
| 2486 | case Instruction::PtrToInt: |
| 2487 | case Instruction::IntToPtr: |
| 2488 | case Instruction::BitCast: |
| 2489 | case Instruction::AddrSpaceCast: |
| 2490 | return true; |
| 2491 | default: |
| 2492 | llvm_unreachable("Argument must be cast opcode" ); |
| 2493 | } |
| 2494 | } |
| 2495 | |
| 2496 | bool ConstantExpr::isSupportedCastOp(unsigned Opcode) { |
| 2497 | switch (Opcode) { |
| 2498 | case Instruction::ZExt: |
| 2499 | case Instruction::SExt: |
| 2500 | case Instruction::FPTrunc: |
| 2501 | case Instruction::FPExt: |
| 2502 | case Instruction::UIToFP: |
| 2503 | case Instruction::SIToFP: |
| 2504 | case Instruction::FPToUI: |
| 2505 | case Instruction::FPToSI: |
| 2506 | return false; |
| 2507 | case Instruction::Trunc: |
| 2508 | case Instruction::PtrToAddr: |
| 2509 | case Instruction::PtrToInt: |
| 2510 | case Instruction::IntToPtr: |
| 2511 | case Instruction::BitCast: |
| 2512 | case Instruction::AddrSpaceCast: |
| 2513 | return true; |
| 2514 | default: |
| 2515 | llvm_unreachable("Argument must be cast opcode" ); |
| 2516 | } |
| 2517 | } |
| 2518 | |
| 2519 | Constant *ConstantExpr::getSizeOf(Type* Ty) { |
| 2520 | // sizeof is implemented as: (i64) gep (Ty*)null, 1 |
| 2521 | // Note that a non-inbounds gep is used, as null isn't within any object. |
| 2522 | Constant *GEPIdx = ConstantInt::get(Ty: Type::getInt32Ty(C&: Ty->getContext()), V: 1); |
| 2523 | Constant *GEP = getGetElementPtr( |
| 2524 | Ty, C: Constant::getNullValue(Ty: PointerType::getUnqual(C&: Ty->getContext())), |
| 2525 | Idx: GEPIdx); |
| 2526 | return getPtrToInt(C: GEP, |
| 2527 | DstTy: Type::getInt64Ty(C&: Ty->getContext())); |
| 2528 | } |
| 2529 | |
| 2530 | Constant *ConstantExpr::getAlignOf(Type* Ty) { |
| 2531 | // alignof is implemented as: (i64) gep ({i1,Ty}*)null, 0, 1 |
| 2532 | // Note that a non-inbounds gep is used, as null isn't within any object. |
| 2533 | Type *AligningTy = StructType::get(elt1: Type::getInt1Ty(C&: Ty->getContext()), elts: Ty); |
| 2534 | Constant *NullPtr = |
| 2535 | Constant::getNullValue(Ty: PointerType::getUnqual(C&: AligningTy->getContext())); |
| 2536 | Constant *Zero = ConstantInt::get(Ty: Type::getInt64Ty(C&: Ty->getContext()), V: 0); |
| 2537 | Constant *One = ConstantInt::get(Ty: Type::getInt32Ty(C&: Ty->getContext()), V: 1); |
| 2538 | Constant *Indices[2] = {Zero, One}; |
| 2539 | Constant *GEP = getGetElementPtr(Ty: AligningTy, C: NullPtr, IdxList: Indices); |
| 2540 | return getPtrToInt(C: GEP, DstTy: Type::getInt64Ty(C&: Ty->getContext())); |
| 2541 | } |
| 2542 | |
| 2543 | Constant *ConstantExpr::getGetElementPtr(Type *Ty, Constant *C, |
| 2544 | ArrayRef<Value *> Idxs, |
| 2545 | GEPNoWrapFlags NW, |
| 2546 | std::optional<ConstantRange> InRange, |
| 2547 | Type *OnlyIfReducedTy) { |
| 2548 | assert(Ty && "Must specify element type" ); |
| 2549 | assert(isSupportedGetElementPtr(Ty) && "Element type is unsupported!" ); |
| 2550 | |
| 2551 | if (Constant *FC = ConstantFoldGetElementPtr(Ty, C, InRange, Idxs)) |
| 2552 | return FC; // Fold a few common cases. |
| 2553 | |
| 2554 | assert(GetElementPtrInst::getIndexedType(Ty, Idxs) && "GEP indices invalid!" ); |
| 2555 | ; |
| 2556 | |
| 2557 | // Get the result type of the getelementptr! |
| 2558 | Type *ReqTy = GetElementPtrInst::getGEPReturnType(Ptr: C, IdxList: Idxs); |
| 2559 | if (OnlyIfReducedTy == ReqTy) |
| 2560 | return nullptr; |
| 2561 | |
| 2562 | auto EltCount = ElementCount::getFixed(MinVal: 0); |
| 2563 | if (VectorType *VecTy = dyn_cast<VectorType>(Val: ReqTy)) |
| 2564 | EltCount = VecTy->getElementCount(); |
| 2565 | |
| 2566 | // Look up the constant in the table first to ensure uniqueness |
| 2567 | std::vector<Constant*> ArgVec; |
| 2568 | ArgVec.reserve(n: 1 + Idxs.size()); |
| 2569 | ArgVec.push_back(x: C); |
| 2570 | auto GTI = gep_type_begin(Op0: Ty, A: Idxs), GTE = gep_type_end(Ty, A: Idxs); |
| 2571 | for (; GTI != GTE; ++GTI) { |
| 2572 | auto *Idx = cast<Constant>(Val: GTI.getOperand()); |
| 2573 | assert( |
| 2574 | (!isa<VectorType>(Idx->getType()) || |
| 2575 | cast<VectorType>(Idx->getType())->getElementCount() == EltCount) && |
| 2576 | "getelementptr index type missmatch" ); |
| 2577 | |
| 2578 | if (GTI.isStruct() && Idx->getType()->isVectorTy()) { |
| 2579 | Idx = Idx->getSplatValue(); |
| 2580 | } else if (GTI.isSequential() && EltCount.isNonZero() && |
| 2581 | !Idx->getType()->isVectorTy()) { |
| 2582 | Idx = ConstantVector::getSplat(EC: EltCount, V: Idx); |
| 2583 | } |
| 2584 | ArgVec.push_back(x: Idx); |
| 2585 | } |
| 2586 | |
| 2587 | const ConstantExprKeyType Key(Instruction::GetElementPtr, ArgVec, NW.getRaw(), |
| 2588 | {}, Ty, InRange); |
| 2589 | |
| 2590 | LLVMContextImpl *pImpl = C->getContext().pImpl; |
| 2591 | return pImpl->ExprConstants.getOrCreate(Ty: ReqTy, V: Key); |
| 2592 | } |
| 2593 | |
| 2594 | Constant *ConstantExpr::(Constant *Val, Constant *Idx, |
| 2595 | Type *OnlyIfReducedTy) { |
| 2596 | assert(Val->getType()->isVectorTy() && |
| 2597 | "Tried to create extractelement operation on non-vector type!" ); |
| 2598 | assert(Idx->getType()->isIntegerTy() && |
| 2599 | "Extractelement index must be an integer type!" ); |
| 2600 | |
| 2601 | if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx)) |
| 2602 | return FC; // Fold a few common cases. |
| 2603 | |
| 2604 | Type *ReqTy = cast<VectorType>(Val: Val->getType())->getElementType(); |
| 2605 | if (OnlyIfReducedTy == ReqTy) |
| 2606 | return nullptr; |
| 2607 | |
| 2608 | // Look up the constant in the table first to ensure uniqueness |
| 2609 | Constant *ArgVec[] = { Val, Idx }; |
| 2610 | const ConstantExprKeyType Key(Instruction::ExtractElement, ArgVec); |
| 2611 | |
| 2612 | LLVMContextImpl *pImpl = Val->getContext().pImpl; |
| 2613 | return pImpl->ExprConstants.getOrCreate(Ty: ReqTy, V: Key); |
| 2614 | } |
| 2615 | |
| 2616 | Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt, |
| 2617 | Constant *Idx, Type *OnlyIfReducedTy) { |
| 2618 | assert(Val->getType()->isVectorTy() && |
| 2619 | "Tried to create insertelement operation on non-vector type!" ); |
| 2620 | assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType() && |
| 2621 | "Insertelement types must match!" ); |
| 2622 | assert(Idx->getType()->isIntegerTy() && |
| 2623 | "Insertelement index must be i32 type!" ); |
| 2624 | |
| 2625 | if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx)) |
| 2626 | return FC; // Fold a few common cases. |
| 2627 | |
| 2628 | if (OnlyIfReducedTy == Val->getType()) |
| 2629 | return nullptr; |
| 2630 | |
| 2631 | // Look up the constant in the table first to ensure uniqueness |
| 2632 | Constant *ArgVec[] = { Val, Elt, Idx }; |
| 2633 | const ConstantExprKeyType Key(Instruction::InsertElement, ArgVec); |
| 2634 | |
| 2635 | LLVMContextImpl *pImpl = Val->getContext().pImpl; |
| 2636 | return pImpl->ExprConstants.getOrCreate(Ty: Val->getType(), V: Key); |
| 2637 | } |
| 2638 | |
| 2639 | Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2, |
| 2640 | ArrayRef<int> Mask, |
| 2641 | Type *OnlyIfReducedTy) { |
| 2642 | assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) && |
| 2643 | "Invalid shuffle vector constant expr operands!" ); |
| 2644 | |
| 2645 | if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask)) |
| 2646 | return FC; // Fold a few common cases. |
| 2647 | |
| 2648 | unsigned NElts = Mask.size(); |
| 2649 | auto V1VTy = cast<VectorType>(Val: V1->getType()); |
| 2650 | Type *EltTy = V1VTy->getElementType(); |
| 2651 | bool TypeIsScalable = isa<ScalableVectorType>(Val: V1VTy); |
| 2652 | Type *ShufTy = VectorType::get(ElementType: EltTy, NumElements: NElts, Scalable: TypeIsScalable); |
| 2653 | |
| 2654 | if (OnlyIfReducedTy == ShufTy) |
| 2655 | return nullptr; |
| 2656 | |
| 2657 | // Look up the constant in the table first to ensure uniqueness |
| 2658 | Constant *ArgVec[] = {V1, V2}; |
| 2659 | ConstantExprKeyType Key(Instruction::ShuffleVector, ArgVec, 0, Mask); |
| 2660 | |
| 2661 | LLVMContextImpl *pImpl = ShufTy->getContext().pImpl; |
| 2662 | return pImpl->ExprConstants.getOrCreate(Ty: ShufTy, V: Key); |
| 2663 | } |
| 2664 | |
| 2665 | Constant *ConstantExpr::getNeg(Constant *C, bool HasNSW) { |
| 2666 | assert(C->getType()->isIntOrIntVectorTy() && |
| 2667 | "Cannot NEG a nonintegral value!" ); |
| 2668 | return getSub(C1: ConstantInt::get(Ty: C->getType(), V: 0), C2: C, /*HasNUW=*/false, HasNSW); |
| 2669 | } |
| 2670 | |
| 2671 | Constant *ConstantExpr::getNot(Constant *C) { |
| 2672 | assert(C->getType()->isIntOrIntVectorTy() && |
| 2673 | "Cannot NOT a nonintegral value!" ); |
| 2674 | return get(Opcode: Instruction::Xor, C1: C, C2: Constant::getAllOnesValue(Ty: C->getType())); |
| 2675 | } |
| 2676 | |
| 2677 | Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2, |
| 2678 | bool HasNUW, bool HasNSW) { |
| 2679 | unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) | |
| 2680 | (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0); |
| 2681 | return get(Opcode: Instruction::Add, C1, C2, Flags); |
| 2682 | } |
| 2683 | |
| 2684 | Constant *ConstantExpr::getSub(Constant *C1, Constant *C2, |
| 2685 | bool HasNUW, bool HasNSW) { |
| 2686 | unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) | |
| 2687 | (HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0); |
| 2688 | return get(Opcode: Instruction::Sub, C1, C2, Flags); |
| 2689 | } |
| 2690 | |
| 2691 | Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) { |
| 2692 | return get(Opcode: Instruction::Xor, C1, C2); |
| 2693 | } |
| 2694 | |
| 2695 | Constant *ConstantExpr::getExactLogBase2(Constant *C) { |
| 2696 | Type *Ty = C->getType(); |
| 2697 | const APInt *IVal; |
| 2698 | if (match(V: C, P: m_APInt(Res&: IVal)) && IVal->isPowerOf2()) |
| 2699 | return ConstantInt::get(Ty, V: IVal->logBase2()); |
| 2700 | |
| 2701 | // FIXME: We can extract pow of 2 of splat constant for scalable vectors. |
| 2702 | auto *VecTy = dyn_cast<FixedVectorType>(Val: Ty); |
| 2703 | if (!VecTy) |
| 2704 | return nullptr; |
| 2705 | |
| 2706 | SmallVector<Constant *, 4> Elts; |
| 2707 | for (unsigned I = 0, E = VecTy->getNumElements(); I != E; ++I) { |
| 2708 | Constant *Elt = C->getAggregateElement(Elt: I); |
| 2709 | if (!Elt) |
| 2710 | return nullptr; |
| 2711 | // Note that log2(iN undef) is *NOT* iN undef, because log2(iN undef) u< N. |
| 2712 | if (isa<UndefValue>(Val: Elt)) { |
| 2713 | Elts.push_back(Elt: Constant::getNullValue(Ty: Ty->getScalarType())); |
| 2714 | continue; |
| 2715 | } |
| 2716 | if (!match(V: Elt, P: m_APInt(Res&: IVal)) || !IVal->isPowerOf2()) |
| 2717 | return nullptr; |
| 2718 | Elts.push_back(Elt: ConstantInt::get(Ty: Ty->getScalarType(), V: IVal->logBase2())); |
| 2719 | } |
| 2720 | |
| 2721 | return ConstantVector::get(V: Elts); |
| 2722 | } |
| 2723 | |
| 2724 | Constant *ConstantExpr::getBinOpIdentity(unsigned Opcode, Type *Ty, |
| 2725 | bool AllowRHSConstant, bool NSZ) { |
| 2726 | assert(Instruction::isBinaryOp(Opcode) && "Only binops allowed" ); |
| 2727 | |
| 2728 | // Commutative opcodes: it does not matter if AllowRHSConstant is set. |
| 2729 | if (Instruction::isCommutative(Opcode)) { |
| 2730 | switch (Opcode) { |
| 2731 | case Instruction::Add: // X + 0 = X |
| 2732 | case Instruction::Or: // X | 0 = X |
| 2733 | case Instruction::Xor: // X ^ 0 = X |
| 2734 | return Constant::getNullValue(Ty); |
| 2735 | case Instruction::Mul: // X * 1 = X |
| 2736 | return ConstantInt::get(Ty, V: 1); |
| 2737 | case Instruction::And: // X & -1 = X |
| 2738 | return Constant::getAllOnesValue(Ty); |
| 2739 | case Instruction::FAdd: // X + -0.0 = X |
| 2740 | return ConstantFP::getZero(Ty, Negative: !NSZ); |
| 2741 | case Instruction::FMul: // X * 1.0 = X |
| 2742 | return ConstantFP::get(Ty, V: 1.0); |
| 2743 | default: |
| 2744 | llvm_unreachable("Every commutative binop has an identity constant" ); |
| 2745 | } |
| 2746 | } |
| 2747 | |
| 2748 | // Non-commutative opcodes: AllowRHSConstant must be set. |
| 2749 | if (!AllowRHSConstant) |
| 2750 | return nullptr; |
| 2751 | |
| 2752 | switch (Opcode) { |
| 2753 | case Instruction::Sub: // X - 0 = X |
| 2754 | case Instruction::Shl: // X << 0 = X |
| 2755 | case Instruction::LShr: // X >>u 0 = X |
| 2756 | case Instruction::AShr: // X >> 0 = X |
| 2757 | case Instruction::FSub: // X - 0.0 = X |
| 2758 | return Constant::getNullValue(Ty); |
| 2759 | case Instruction::SDiv: // X / 1 = X |
| 2760 | case Instruction::UDiv: // X /u 1 = X |
| 2761 | return ConstantInt::get(Ty, V: 1); |
| 2762 | case Instruction::FDiv: // X / 1.0 = X |
| 2763 | return ConstantFP::get(Ty, V: 1.0); |
| 2764 | default: |
| 2765 | return nullptr; |
| 2766 | } |
| 2767 | } |
| 2768 | |
| 2769 | Constant *ConstantExpr::getIntrinsicIdentity(Intrinsic::ID ID, Type *Ty) { |
| 2770 | switch (ID) { |
| 2771 | case Intrinsic::umax: |
| 2772 | return Constant::getNullValue(Ty); |
| 2773 | case Intrinsic::umin: |
| 2774 | return Constant::getAllOnesValue(Ty); |
| 2775 | case Intrinsic::smax: |
| 2776 | return Constant::getIntegerValue( |
| 2777 | Ty, V: APInt::getSignedMinValue(numBits: Ty->getIntegerBitWidth())); |
| 2778 | case Intrinsic::smin: |
| 2779 | return Constant::getIntegerValue( |
| 2780 | Ty, V: APInt::getSignedMaxValue(numBits: Ty->getIntegerBitWidth())); |
| 2781 | default: |
| 2782 | return nullptr; |
| 2783 | } |
| 2784 | } |
| 2785 | |
| 2786 | Constant *ConstantExpr::getIdentity(Instruction *I, Type *Ty, |
| 2787 | bool AllowRHSConstant, bool NSZ) { |
| 2788 | if (I->isBinaryOp()) |
| 2789 | return getBinOpIdentity(Opcode: I->getOpcode(), Ty, AllowRHSConstant, NSZ); |
| 2790 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Val: I)) |
| 2791 | return getIntrinsicIdentity(ID: II->getIntrinsicID(), Ty); |
| 2792 | return nullptr; |
| 2793 | } |
| 2794 | |
| 2795 | Constant *ConstantExpr::getBinOpAbsorber(unsigned Opcode, Type *Ty, |
| 2796 | bool AllowLHSConstant) { |
| 2797 | switch (Opcode) { |
| 2798 | default: |
| 2799 | break; |
| 2800 | |
| 2801 | case Instruction::Or: // -1 | X = -1 |
| 2802 | return Constant::getAllOnesValue(Ty); |
| 2803 | |
| 2804 | case Instruction::And: // 0 & X = 0 |
| 2805 | case Instruction::Mul: // 0 * X = 0 |
| 2806 | return Constant::getNullValue(Ty); |
| 2807 | } |
| 2808 | |
| 2809 | // AllowLHSConstant must be set. |
| 2810 | if (!AllowLHSConstant) |
| 2811 | return nullptr; |
| 2812 | |
| 2813 | switch (Opcode) { |
| 2814 | default: |
| 2815 | return nullptr; |
| 2816 | case Instruction::Shl: // 0 << X = 0 |
| 2817 | case Instruction::LShr: // 0 >>l X = 0 |
| 2818 | case Instruction::AShr: // 0 >>a X = 0 |
| 2819 | case Instruction::SDiv: // 0 /s X = 0 |
| 2820 | case Instruction::UDiv: // 0 /u X = 0 |
| 2821 | case Instruction::URem: // 0 %u X = 0 |
| 2822 | case Instruction::SRem: // 0 %s X = 0 |
| 2823 | return Constant::getNullValue(Ty); |
| 2824 | } |
| 2825 | } |
| 2826 | |
| 2827 | /// Remove the constant from the constant table. |
| 2828 | void ConstantExpr::destroyConstantImpl() { |
| 2829 | getType()->getContext().pImpl->ExprConstants.remove(CP: this); |
| 2830 | } |
| 2831 | |
| 2832 | const char *ConstantExpr::getOpcodeName() const { |
| 2833 | return Instruction::getOpcodeName(Opcode: getOpcode()); |
| 2834 | } |
| 2835 | |
| 2836 | GetElementPtrConstantExpr::GetElementPtrConstantExpr( |
| 2837 | Type *SrcElementTy, Constant *C, ArrayRef<Constant *> IdxList, Type *DestTy, |
| 2838 | std::optional<ConstantRange> InRange, AllocInfo AllocInfo) |
| 2839 | : ConstantExpr(DestTy, Instruction::GetElementPtr, AllocInfo), |
| 2840 | SrcElementTy(SrcElementTy), |
| 2841 | ResElementTy(GetElementPtrInst::getIndexedType(Ty: SrcElementTy, IdxList)), |
| 2842 | InRange(std::move(InRange)) { |
| 2843 | Op<0>() = C; |
| 2844 | Use *OperandList = getOperandList(); |
| 2845 | for (unsigned i = 0, E = IdxList.size(); i != E; ++i) |
| 2846 | OperandList[i+1] = IdxList[i]; |
| 2847 | } |
| 2848 | |
| 2849 | Type *GetElementPtrConstantExpr::getSourceElementType() const { |
| 2850 | return SrcElementTy; |
| 2851 | } |
| 2852 | |
| 2853 | Type *GetElementPtrConstantExpr::getResultElementType() const { |
| 2854 | return ResElementTy; |
| 2855 | } |
| 2856 | |
| 2857 | std::optional<ConstantRange> GetElementPtrConstantExpr::getInRange() const { |
| 2858 | return InRange; |
| 2859 | } |
| 2860 | |
| 2861 | //===----------------------------------------------------------------------===// |
| 2862 | // ConstantData* implementations |
| 2863 | |
| 2864 | Type *ConstantDataSequential::getElementType() const { |
| 2865 | if (ArrayType *ATy = dyn_cast<ArrayType>(Val: getType())) |
| 2866 | return ATy->getElementType(); |
| 2867 | return cast<VectorType>(Val: getType())->getElementType(); |
| 2868 | } |
| 2869 | |
| 2870 | StringRef ConstantDataSequential::getRawDataValues() const { |
| 2871 | return StringRef(DataElements, getNumElements()*getElementByteSize()); |
| 2872 | } |
| 2873 | |
| 2874 | bool ConstantDataSequential::isElementTypeCompatible(Type *Ty) { |
| 2875 | if (Ty->isHalfTy() || Ty->isBFloatTy() || Ty->isFloatTy() || Ty->isDoubleTy()) |
| 2876 | return true; |
| 2877 | if (auto *IT = dyn_cast<IntegerType>(Val: Ty)) { |
| 2878 | switch (IT->getBitWidth()) { |
| 2879 | case 8: |
| 2880 | case 16: |
| 2881 | case 32: |
| 2882 | case 64: |
| 2883 | return true; |
| 2884 | default: break; |
| 2885 | } |
| 2886 | } |
| 2887 | return false; |
| 2888 | } |
| 2889 | |
| 2890 | uint64_t ConstantDataSequential::getNumElements() const { |
| 2891 | if (ArrayType *AT = dyn_cast<ArrayType>(Val: getType())) |
| 2892 | return AT->getNumElements(); |
| 2893 | return cast<FixedVectorType>(Val: getType())->getNumElements(); |
| 2894 | } |
| 2895 | |
| 2896 | uint64_t ConstantDataSequential::getElementByteSize() const { |
| 2897 | return getElementType()->getPrimitiveSizeInBits().getFixedValue() / 8; |
| 2898 | } |
| 2899 | |
| 2900 | /// Return the start of the specified element. |
| 2901 | const char *ConstantDataSequential::getElementPointer(uint64_t Elt) const { |
| 2902 | assert(Elt < getNumElements() && "Invalid Elt" ); |
| 2903 | return DataElements + Elt * getElementByteSize(); |
| 2904 | } |
| 2905 | |
| 2906 | /// Return true if the array is empty or all zeros. |
| 2907 | static bool isAllZeros(StringRef Arr) { |
| 2908 | for (char I : Arr) |
| 2909 | if (I != 0) |
| 2910 | return false; |
| 2911 | return true; |
| 2912 | } |
| 2913 | |
| 2914 | /// This is the underlying implementation of all of the |
| 2915 | /// ConstantDataSequential::get methods. They all thunk down to here, providing |
| 2916 | /// the correct element type. We take the bytes in as a StringRef because |
| 2917 | /// we *want* an underlying "char*" to avoid TBAA type punning violations. |
| 2918 | Constant *ConstantDataSequential::getImpl(StringRef Elements, Type *Ty) { |
| 2919 | #ifndef NDEBUG |
| 2920 | if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) |
| 2921 | assert(isElementTypeCompatible(ATy->getElementType())); |
| 2922 | else |
| 2923 | assert(isElementTypeCompatible(cast<VectorType>(Ty)->getElementType())); |
| 2924 | #endif |
| 2925 | // If the elements are all zero or there are no elements, return a CAZ, which |
| 2926 | // is more dense and canonical. |
| 2927 | if (isAllZeros(Arr: Elements)) |
| 2928 | return ConstantAggregateZero::get(Ty); |
| 2929 | |
| 2930 | // Do a lookup to see if we have already formed one of these. |
| 2931 | auto &Slot = |
| 2932 | *Ty->getContext().pImpl->CDSConstants.try_emplace(Key: Elements).first; |
| 2933 | |
| 2934 | // The bucket can point to a linked list of different CDS's that have the same |
| 2935 | // body but different types. For example, 0,0,0,1 could be a 4 element array |
| 2936 | // of i8, or a 1-element array of i32. They'll both end up in the same |
| 2937 | /// StringMap bucket, linked up by their Next pointers. Walk the list. |
| 2938 | std::unique_ptr<ConstantDataSequential> *Entry = &Slot.second; |
| 2939 | for (; *Entry; Entry = &(*Entry)->Next) |
| 2940 | if ((*Entry)->getType() == Ty) |
| 2941 | return Entry->get(); |
| 2942 | |
| 2943 | // Okay, we didn't get a hit. Create a node of the right class, link it in, |
| 2944 | // and return it. |
| 2945 | if (isa<ArrayType>(Val: Ty)) { |
| 2946 | // Use reset because std::make_unique can't access the constructor. |
| 2947 | Entry->reset(p: new ConstantDataArray(Ty, Slot.first().data())); |
| 2948 | return Entry->get(); |
| 2949 | } |
| 2950 | |
| 2951 | assert(isa<VectorType>(Ty)); |
| 2952 | // Use reset because std::make_unique can't access the constructor. |
| 2953 | Entry->reset(p: new ConstantDataVector(Ty, Slot.first().data())); |
| 2954 | return Entry->get(); |
| 2955 | } |
| 2956 | |
| 2957 | void ConstantDataSequential::destroyConstantImpl() { |
| 2958 | // Remove the constant from the StringMap. |
| 2959 | StringMap<std::unique_ptr<ConstantDataSequential>> &CDSConstants = |
| 2960 | getType()->getContext().pImpl->CDSConstants; |
| 2961 | |
| 2962 | auto Slot = CDSConstants.find(Key: getRawDataValues()); |
| 2963 | |
| 2964 | assert(Slot != CDSConstants.end() && "CDS not found in uniquing table" ); |
| 2965 | |
| 2966 | std::unique_ptr<ConstantDataSequential> *Entry = &Slot->getValue(); |
| 2967 | |
| 2968 | // Remove the entry from the hash table. |
| 2969 | if (!(*Entry)->Next) { |
| 2970 | // If there is only one value in the bucket (common case) it must be this |
| 2971 | // entry, and removing the entry should remove the bucket completely. |
| 2972 | assert(Entry->get() == this && "Hash mismatch in ConstantDataSequential" ); |
| 2973 | getContext().pImpl->CDSConstants.erase(I: Slot); |
| 2974 | return; |
| 2975 | } |
| 2976 | |
| 2977 | // Otherwise, there are multiple entries linked off the bucket, unlink the |
| 2978 | // node we care about but keep the bucket around. |
| 2979 | while (true) { |
| 2980 | std::unique_ptr<ConstantDataSequential> &Node = *Entry; |
| 2981 | assert(Node && "Didn't find entry in its uniquing hash table!" ); |
| 2982 | // If we found our entry, unlink it from the list and we're done. |
| 2983 | if (Node.get() == this) { |
| 2984 | Node = std::move(Node->Next); |
| 2985 | return; |
| 2986 | } |
| 2987 | |
| 2988 | Entry = &Node->Next; |
| 2989 | } |
| 2990 | } |
| 2991 | |
| 2992 | /// getFP() constructors - Return a constant of array type with a float |
| 2993 | /// element type taken from argument `ElementType', and count taken from |
| 2994 | /// argument `Elts'. The amount of bits of the contained type must match the |
| 2995 | /// number of bits of the type contained in the passed in ArrayRef. |
| 2996 | /// (i.e. half or bfloat for 16bits, float for 32bits, double for 64bits) Note |
| 2997 | /// that this can return a ConstantAggregateZero object. |
| 2998 | Constant *ConstantDataArray::getFP(Type *ElementType, ArrayRef<uint16_t> Elts) { |
| 2999 | assert((ElementType->isHalfTy() || ElementType->isBFloatTy()) && |
| 3000 | "Element type is not a 16-bit float type" ); |
| 3001 | Type *Ty = ArrayType::get(ElementType, NumElements: Elts.size()); |
| 3002 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 3003 | return getImpl(Elements: StringRef(Data, Elts.size() * 2), Ty); |
| 3004 | } |
| 3005 | Constant *ConstantDataArray::getFP(Type *ElementType, ArrayRef<uint32_t> Elts) { |
| 3006 | assert(ElementType->isFloatTy() && "Element type is not a 32-bit float type" ); |
| 3007 | Type *Ty = ArrayType::get(ElementType, NumElements: Elts.size()); |
| 3008 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 3009 | return getImpl(Elements: StringRef(Data, Elts.size() * 4), Ty); |
| 3010 | } |
| 3011 | Constant *ConstantDataArray::getFP(Type *ElementType, ArrayRef<uint64_t> Elts) { |
| 3012 | assert(ElementType->isDoubleTy() && |
| 3013 | "Element type is not a 64-bit float type" ); |
| 3014 | Type *Ty = ArrayType::get(ElementType, NumElements: Elts.size()); |
| 3015 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 3016 | return getImpl(Elements: StringRef(Data, Elts.size() * 8), Ty); |
| 3017 | } |
| 3018 | |
| 3019 | Constant *ConstantDataArray::getString(LLVMContext &Context, |
| 3020 | StringRef Str, bool AddNull) { |
| 3021 | if (!AddNull) { |
| 3022 | const uint8_t *Data = Str.bytes_begin(); |
| 3023 | return get(Context, Elts: ArrayRef(Data, Str.size())); |
| 3024 | } |
| 3025 | |
| 3026 | SmallVector<uint8_t, 64> ElementVals; |
| 3027 | ElementVals.append(in_start: Str.begin(), in_end: Str.end()); |
| 3028 | ElementVals.push_back(Elt: 0); |
| 3029 | return get(Context, Elts&: ElementVals); |
| 3030 | } |
| 3031 | |
| 3032 | /// get() constructors - Return a constant with vector type with an element |
| 3033 | /// count and element type matching the ArrayRef passed in. Note that this |
| 3034 | /// can return a ConstantAggregateZero object. |
| 3035 | Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint8_t> Elts){ |
| 3036 | auto *Ty = FixedVectorType::get(ElementType: Type::getInt8Ty(C&: Context), NumElts: Elts.size()); |
| 3037 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 3038 | return getImpl(Elements: StringRef(Data, Elts.size() * 1), Ty); |
| 3039 | } |
| 3040 | Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint16_t> Elts){ |
| 3041 | auto *Ty = FixedVectorType::get(ElementType: Type::getInt16Ty(C&: Context), NumElts: Elts.size()); |
| 3042 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 3043 | return getImpl(Elements: StringRef(Data, Elts.size() * 2), Ty); |
| 3044 | } |
| 3045 | Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint32_t> Elts){ |
| 3046 | auto *Ty = FixedVectorType::get(ElementType: Type::getInt32Ty(C&: Context), NumElts: Elts.size()); |
| 3047 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 3048 | return getImpl(Elements: StringRef(Data, Elts.size() * 4), Ty); |
| 3049 | } |
| 3050 | Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint64_t> Elts){ |
| 3051 | auto *Ty = FixedVectorType::get(ElementType: Type::getInt64Ty(C&: Context), NumElts: Elts.size()); |
| 3052 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 3053 | return getImpl(Elements: StringRef(Data, Elts.size() * 8), Ty); |
| 3054 | } |
| 3055 | Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<float> Elts) { |
| 3056 | auto *Ty = FixedVectorType::get(ElementType: Type::getFloatTy(C&: Context), NumElts: Elts.size()); |
| 3057 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 3058 | return getImpl(Elements: StringRef(Data, Elts.size() * 4), Ty); |
| 3059 | } |
| 3060 | Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<double> Elts) { |
| 3061 | auto *Ty = FixedVectorType::get(ElementType: Type::getDoubleTy(C&: Context), NumElts: Elts.size()); |
| 3062 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 3063 | return getImpl(Elements: StringRef(Data, Elts.size() * 8), Ty); |
| 3064 | } |
| 3065 | |
| 3066 | /// getFP() constructors - Return a constant of vector type with a float |
| 3067 | /// element type taken from argument `ElementType', and count taken from |
| 3068 | /// argument `Elts'. The amount of bits of the contained type must match the |
| 3069 | /// number of bits of the type contained in the passed in ArrayRef. |
| 3070 | /// (i.e. half or bfloat for 16bits, float for 32bits, double for 64bits) Note |
| 3071 | /// that this can return a ConstantAggregateZero object. |
| 3072 | Constant *ConstantDataVector::getFP(Type *ElementType, |
| 3073 | ArrayRef<uint16_t> Elts) { |
| 3074 | assert((ElementType->isHalfTy() || ElementType->isBFloatTy()) && |
| 3075 | "Element type is not a 16-bit float type" ); |
| 3076 | auto *Ty = FixedVectorType::get(ElementType, NumElts: Elts.size()); |
| 3077 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 3078 | return getImpl(Elements: StringRef(Data, Elts.size() * 2), Ty); |
| 3079 | } |
| 3080 | Constant *ConstantDataVector::getFP(Type *ElementType, |
| 3081 | ArrayRef<uint32_t> Elts) { |
| 3082 | assert(ElementType->isFloatTy() && "Element type is not a 32-bit float type" ); |
| 3083 | auto *Ty = FixedVectorType::get(ElementType, NumElts: Elts.size()); |
| 3084 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 3085 | return getImpl(Elements: StringRef(Data, Elts.size() * 4), Ty); |
| 3086 | } |
| 3087 | Constant *ConstantDataVector::getFP(Type *ElementType, |
| 3088 | ArrayRef<uint64_t> Elts) { |
| 3089 | assert(ElementType->isDoubleTy() && |
| 3090 | "Element type is not a 64-bit float type" ); |
| 3091 | auto *Ty = FixedVectorType::get(ElementType, NumElts: Elts.size()); |
| 3092 | const char *Data = reinterpret_cast<const char *>(Elts.data()); |
| 3093 | return getImpl(Elements: StringRef(Data, Elts.size() * 8), Ty); |
| 3094 | } |
| 3095 | |
| 3096 | Constant *ConstantDataVector::getSplat(unsigned NumElts, Constant *V) { |
| 3097 | assert(isElementTypeCompatible(V->getType()) && |
| 3098 | "Element type not compatible with ConstantData" ); |
| 3099 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Val: V)) { |
| 3100 | if (CI->getType()->isIntegerTy(Bitwidth: 8)) { |
| 3101 | SmallVector<uint8_t, 16> Elts(NumElts, CI->getZExtValue()); |
| 3102 | return get(Context&: V->getContext(), Elts); |
| 3103 | } |
| 3104 | if (CI->getType()->isIntegerTy(Bitwidth: 16)) { |
| 3105 | SmallVector<uint16_t, 16> Elts(NumElts, CI->getZExtValue()); |
| 3106 | return get(Context&: V->getContext(), Elts); |
| 3107 | } |
| 3108 | if (CI->getType()->isIntegerTy(Bitwidth: 32)) { |
| 3109 | SmallVector<uint32_t, 16> Elts(NumElts, CI->getZExtValue()); |
| 3110 | return get(Context&: V->getContext(), Elts); |
| 3111 | } |
| 3112 | assert(CI->getType()->isIntegerTy(64) && "Unsupported ConstantData type" ); |
| 3113 | SmallVector<uint64_t, 16> Elts(NumElts, CI->getZExtValue()); |
| 3114 | return get(Context&: V->getContext(), Elts); |
| 3115 | } |
| 3116 | |
| 3117 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(Val: V)) { |
| 3118 | if (CFP->getType()->isHalfTy()) { |
| 3119 | SmallVector<uint16_t, 16> Elts( |
| 3120 | NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue()); |
| 3121 | return getFP(ElementType: V->getType(), Elts); |
| 3122 | } |
| 3123 | if (CFP->getType()->isBFloatTy()) { |
| 3124 | SmallVector<uint16_t, 16> Elts( |
| 3125 | NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue()); |
| 3126 | return getFP(ElementType: V->getType(), Elts); |
| 3127 | } |
| 3128 | if (CFP->getType()->isFloatTy()) { |
| 3129 | SmallVector<uint32_t, 16> Elts( |
| 3130 | NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue()); |
| 3131 | return getFP(ElementType: V->getType(), Elts); |
| 3132 | } |
| 3133 | if (CFP->getType()->isDoubleTy()) { |
| 3134 | SmallVector<uint64_t, 16> Elts( |
| 3135 | NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue()); |
| 3136 | return getFP(ElementType: V->getType(), Elts); |
| 3137 | } |
| 3138 | } |
| 3139 | return ConstantVector::getSplat(EC: ElementCount::getFixed(MinVal: NumElts), V); |
| 3140 | } |
| 3141 | |
| 3142 | uint64_t ConstantDataSequential::getElementAsInteger(uint64_t Elt) const { |
| 3143 | assert(isa<IntegerType>(getElementType()) && |
| 3144 | "Accessor can only be used when element is an integer" ); |
| 3145 | const char *EltPtr = getElementPointer(Elt); |
| 3146 | |
| 3147 | // The data is stored in host byte order, make sure to cast back to the right |
| 3148 | // type to load with the right endianness. |
| 3149 | switch (getElementType()->getIntegerBitWidth()) { |
| 3150 | default: llvm_unreachable("Invalid bitwidth for CDS" ); |
| 3151 | case 8: |
| 3152 | return *reinterpret_cast<const uint8_t *>(EltPtr); |
| 3153 | case 16: |
| 3154 | return *reinterpret_cast<const uint16_t *>(EltPtr); |
| 3155 | case 32: |
| 3156 | return *reinterpret_cast<const uint32_t *>(EltPtr); |
| 3157 | case 64: |
| 3158 | return *reinterpret_cast<const uint64_t *>(EltPtr); |
| 3159 | } |
| 3160 | } |
| 3161 | |
| 3162 | APInt ConstantDataSequential::getElementAsAPInt(uint64_t Elt) const { |
| 3163 | assert(isa<IntegerType>(getElementType()) && |
| 3164 | "Accessor can only be used when element is an integer" ); |
| 3165 | const char *EltPtr = getElementPointer(Elt); |
| 3166 | |
| 3167 | // The data is stored in host byte order, make sure to cast back to the right |
| 3168 | // type to load with the right endianness. |
| 3169 | switch (getElementType()->getIntegerBitWidth()) { |
| 3170 | default: llvm_unreachable("Invalid bitwidth for CDS" ); |
| 3171 | case 8: { |
| 3172 | auto EltVal = *reinterpret_cast<const uint8_t *>(EltPtr); |
| 3173 | return APInt(8, EltVal); |
| 3174 | } |
| 3175 | case 16: { |
| 3176 | auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr); |
| 3177 | return APInt(16, EltVal); |
| 3178 | } |
| 3179 | case 32: { |
| 3180 | auto EltVal = *reinterpret_cast<const uint32_t *>(EltPtr); |
| 3181 | return APInt(32, EltVal); |
| 3182 | } |
| 3183 | case 64: { |
| 3184 | auto EltVal = *reinterpret_cast<const uint64_t *>(EltPtr); |
| 3185 | return APInt(64, EltVal); |
| 3186 | } |
| 3187 | } |
| 3188 | } |
| 3189 | |
| 3190 | APFloat ConstantDataSequential::getElementAsAPFloat(uint64_t Elt) const { |
| 3191 | const char *EltPtr = getElementPointer(Elt); |
| 3192 | |
| 3193 | switch (getElementType()->getTypeID()) { |
| 3194 | default: |
| 3195 | llvm_unreachable("Accessor can only be used when element is float/double!" ); |
| 3196 | case Type::HalfTyID: { |
| 3197 | auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr); |
| 3198 | return APFloat(APFloat::IEEEhalf(), APInt(16, EltVal)); |
| 3199 | } |
| 3200 | case Type::BFloatTyID: { |
| 3201 | auto EltVal = *reinterpret_cast<const uint16_t *>(EltPtr); |
| 3202 | return APFloat(APFloat::BFloat(), APInt(16, EltVal)); |
| 3203 | } |
| 3204 | case Type::FloatTyID: { |
| 3205 | auto EltVal = *reinterpret_cast<const uint32_t *>(EltPtr); |
| 3206 | return APFloat(APFloat::IEEEsingle(), APInt(32, EltVal)); |
| 3207 | } |
| 3208 | case Type::DoubleTyID: { |
| 3209 | auto EltVal = *reinterpret_cast<const uint64_t *>(EltPtr); |
| 3210 | return APFloat(APFloat::IEEEdouble(), APInt(64, EltVal)); |
| 3211 | } |
| 3212 | } |
| 3213 | } |
| 3214 | |
| 3215 | float ConstantDataSequential::getElementAsFloat(uint64_t Elt) const { |
| 3216 | assert(getElementType()->isFloatTy() && |
| 3217 | "Accessor can only be used when element is a 'float'" ); |
| 3218 | return *reinterpret_cast<const float *>(getElementPointer(Elt)); |
| 3219 | } |
| 3220 | |
| 3221 | double ConstantDataSequential::getElementAsDouble(uint64_t Elt) const { |
| 3222 | assert(getElementType()->isDoubleTy() && |
| 3223 | "Accessor can only be used when element is a 'float'" ); |
| 3224 | return *reinterpret_cast<const double *>(getElementPointer(Elt)); |
| 3225 | } |
| 3226 | |
| 3227 | Constant *ConstantDataSequential::getElementAsConstant(uint64_t Elt) const { |
| 3228 | if (getElementType()->isHalfTy() || getElementType()->isBFloatTy() || |
| 3229 | getElementType()->isFloatTy() || getElementType()->isDoubleTy()) |
| 3230 | return ConstantFP::get(Context&: getContext(), V: getElementAsAPFloat(Elt)); |
| 3231 | |
| 3232 | return ConstantInt::get(Ty: getElementType(), V: getElementAsInteger(Elt)); |
| 3233 | } |
| 3234 | |
| 3235 | bool ConstantDataSequential::isString(unsigned CharSize) const { |
| 3236 | return isa<ArrayType>(Val: getType()) && getElementType()->isIntegerTy(Bitwidth: CharSize); |
| 3237 | } |
| 3238 | |
| 3239 | bool ConstantDataSequential::isCString() const { |
| 3240 | if (!isString()) |
| 3241 | return false; |
| 3242 | |
| 3243 | StringRef Str = getAsString(); |
| 3244 | |
| 3245 | // The last value must be nul. |
| 3246 | if (Str.back() != 0) return false; |
| 3247 | |
| 3248 | // Other elements must be non-nul. |
| 3249 | return !Str.drop_back().contains(C: 0); |
| 3250 | } |
| 3251 | |
| 3252 | bool ConstantDataVector::isSplatData() const { |
| 3253 | const char *Base = getRawDataValues().data(); |
| 3254 | |
| 3255 | // Compare elements 1+ to the 0'th element. |
| 3256 | unsigned EltSize = getElementByteSize(); |
| 3257 | for (unsigned i = 1, e = getNumElements(); i != e; ++i) |
| 3258 | if (memcmp(s1: Base, s2: Base+i*EltSize, n: EltSize)) |
| 3259 | return false; |
| 3260 | |
| 3261 | return true; |
| 3262 | } |
| 3263 | |
| 3264 | bool ConstantDataVector::isSplat() const { |
| 3265 | if (!IsSplatSet) { |
| 3266 | IsSplatSet = true; |
| 3267 | IsSplat = isSplatData(); |
| 3268 | } |
| 3269 | return IsSplat; |
| 3270 | } |
| 3271 | |
| 3272 | Constant *ConstantDataVector::getSplatValue() const { |
| 3273 | // If they're all the same, return the 0th one as a representative. |
| 3274 | return isSplat() ? getElementAsConstant(Elt: 0) : nullptr; |
| 3275 | } |
| 3276 | |
| 3277 | //===----------------------------------------------------------------------===// |
| 3278 | // handleOperandChange implementations |
| 3279 | |
| 3280 | /// Update this constant array to change uses of |
| 3281 | /// 'From' to be uses of 'To'. This must update the uniquing data structures |
| 3282 | /// etc. |
| 3283 | /// |
| 3284 | /// Note that we intentionally replace all uses of From with To here. Consider |
| 3285 | /// a large array that uses 'From' 1000 times. By handling this case all here, |
| 3286 | /// ConstantArray::handleOperandChange is only invoked once, and that |
| 3287 | /// single invocation handles all 1000 uses. Handling them one at a time would |
| 3288 | /// work, but would be really slow because it would have to unique each updated |
| 3289 | /// array instance. |
| 3290 | /// |
| 3291 | void Constant::handleOperandChange(Value *From, Value *To) { |
| 3292 | Value *Replacement = nullptr; |
| 3293 | switch (getValueID()) { |
| 3294 | default: |
| 3295 | llvm_unreachable("Not a constant!" ); |
| 3296 | #define HANDLE_CONSTANT(Name) \ |
| 3297 | case Value::Name##Val: \ |
| 3298 | Replacement = cast<Name>(this)->handleOperandChangeImpl(From, To); \ |
| 3299 | break; |
| 3300 | #include "llvm/IR/Value.def" |
| 3301 | } |
| 3302 | |
| 3303 | // If handleOperandChangeImpl returned nullptr, then it handled |
| 3304 | // replacing itself and we don't want to delete or replace anything else here. |
| 3305 | if (!Replacement) |
| 3306 | return; |
| 3307 | |
| 3308 | // I do need to replace this with an existing value. |
| 3309 | assert(Replacement != this && "I didn't contain From!" ); |
| 3310 | |
| 3311 | // Everyone using this now uses the replacement. |
| 3312 | replaceAllUsesWith(V: Replacement); |
| 3313 | |
| 3314 | // Delete the old constant! |
| 3315 | destroyConstant(); |
| 3316 | } |
| 3317 | |
| 3318 | Value *ConstantArray::handleOperandChangeImpl(Value *From, Value *To) { |
| 3319 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!" ); |
| 3320 | Constant *ToC = cast<Constant>(Val: To); |
| 3321 | |
| 3322 | SmallVector<Constant*, 8> Values; |
| 3323 | Values.reserve(N: getNumOperands()); // Build replacement array. |
| 3324 | |
| 3325 | // Fill values with the modified operands of the constant array. Also, |
| 3326 | // compute whether this turns into an all-zeros array. |
| 3327 | unsigned NumUpdated = 0; |
| 3328 | |
| 3329 | // Keep track of whether all the values in the array are "ToC". |
| 3330 | bool AllSame = true; |
| 3331 | Use *OperandList = getOperandList(); |
| 3332 | unsigned OperandNo = 0; |
| 3333 | for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) { |
| 3334 | Constant *Val = cast<Constant>(Val: O->get()); |
| 3335 | if (Val == From) { |
| 3336 | OperandNo = (O - OperandList); |
| 3337 | Val = ToC; |
| 3338 | ++NumUpdated; |
| 3339 | } |
| 3340 | Values.push_back(Elt: Val); |
| 3341 | AllSame &= Val == ToC; |
| 3342 | } |
| 3343 | |
| 3344 | if (AllSame && ToC->isNullValue()) |
| 3345 | return ConstantAggregateZero::get(Ty: getType()); |
| 3346 | |
| 3347 | if (AllSame && isa<UndefValue>(Val: ToC)) |
| 3348 | return UndefValue::get(Ty: getType()); |
| 3349 | |
| 3350 | // Check for any other type of constant-folding. |
| 3351 | if (Constant *C = getImpl(Ty: getType(), V: Values)) |
| 3352 | return C; |
| 3353 | |
| 3354 | // Update to the new value. |
| 3355 | return getContext().pImpl->ArrayConstants.replaceOperandsInPlace( |
| 3356 | Operands: Values, CP: this, From, To: ToC, NumUpdated, OperandNo); |
| 3357 | } |
| 3358 | |
| 3359 | Value *ConstantStruct::handleOperandChangeImpl(Value *From, Value *To) { |
| 3360 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!" ); |
| 3361 | Constant *ToC = cast<Constant>(Val: To); |
| 3362 | |
| 3363 | Use *OperandList = getOperandList(); |
| 3364 | |
| 3365 | SmallVector<Constant*, 8> Values; |
| 3366 | Values.reserve(N: getNumOperands()); // Build replacement struct. |
| 3367 | |
| 3368 | // Fill values with the modified operands of the constant struct. Also, |
| 3369 | // compute whether this turns into an all-zeros struct. |
| 3370 | unsigned NumUpdated = 0; |
| 3371 | bool AllSame = true; |
| 3372 | unsigned OperandNo = 0; |
| 3373 | for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O) { |
| 3374 | Constant *Val = cast<Constant>(Val: O->get()); |
| 3375 | if (Val == From) { |
| 3376 | OperandNo = (O - OperandList); |
| 3377 | Val = ToC; |
| 3378 | ++NumUpdated; |
| 3379 | } |
| 3380 | Values.push_back(Elt: Val); |
| 3381 | AllSame &= Val == ToC; |
| 3382 | } |
| 3383 | |
| 3384 | if (AllSame && ToC->isNullValue()) |
| 3385 | return ConstantAggregateZero::get(Ty: getType()); |
| 3386 | |
| 3387 | if (AllSame && isa<UndefValue>(Val: ToC)) |
| 3388 | return UndefValue::get(Ty: getType()); |
| 3389 | |
| 3390 | // Update to the new value. |
| 3391 | return getContext().pImpl->StructConstants.replaceOperandsInPlace( |
| 3392 | Operands: Values, CP: this, From, To: ToC, NumUpdated, OperandNo); |
| 3393 | } |
| 3394 | |
| 3395 | Value *ConstantVector::handleOperandChangeImpl(Value *From, Value *To) { |
| 3396 | assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!" ); |
| 3397 | Constant *ToC = cast<Constant>(Val: To); |
| 3398 | |
| 3399 | SmallVector<Constant*, 8> Values; |
| 3400 | Values.reserve(N: getNumOperands()); // Build replacement array... |
| 3401 | unsigned NumUpdated = 0; |
| 3402 | unsigned OperandNo = 0; |
| 3403 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 3404 | Constant *Val = getOperand(i_nocapture: i); |
| 3405 | if (Val == From) { |
| 3406 | OperandNo = i; |
| 3407 | ++NumUpdated; |
| 3408 | Val = ToC; |
| 3409 | } |
| 3410 | Values.push_back(Elt: Val); |
| 3411 | } |
| 3412 | |
| 3413 | if (Constant *C = getImpl(V: Values)) |
| 3414 | return C; |
| 3415 | |
| 3416 | // Update to the new value. |
| 3417 | return getContext().pImpl->VectorConstants.replaceOperandsInPlace( |
| 3418 | Operands: Values, CP: this, From, To: ToC, NumUpdated, OperandNo); |
| 3419 | } |
| 3420 | |
| 3421 | Value *ConstantExpr::handleOperandChangeImpl(Value *From, Value *ToV) { |
| 3422 | assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!" ); |
| 3423 | Constant *To = cast<Constant>(Val: ToV); |
| 3424 | |
| 3425 | SmallVector<Constant*, 8> NewOps; |
| 3426 | unsigned NumUpdated = 0; |
| 3427 | unsigned OperandNo = 0; |
| 3428 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 3429 | Constant *Op = getOperand(i_nocapture: i); |
| 3430 | if (Op == From) { |
| 3431 | OperandNo = i; |
| 3432 | ++NumUpdated; |
| 3433 | Op = To; |
| 3434 | } |
| 3435 | NewOps.push_back(Elt: Op); |
| 3436 | } |
| 3437 | assert(NumUpdated && "I didn't contain From!" ); |
| 3438 | |
| 3439 | if (Constant *C = getWithOperands(Ops: NewOps, Ty: getType(), OnlyIfReduced: true)) |
| 3440 | return C; |
| 3441 | |
| 3442 | // Update to the new value. |
| 3443 | return getContext().pImpl->ExprConstants.replaceOperandsInPlace( |
| 3444 | Operands: NewOps, CP: this, From, To, NumUpdated, OperandNo); |
| 3445 | } |
| 3446 | |
| 3447 | Instruction *ConstantExpr::getAsInstruction() const { |
| 3448 | SmallVector<Value *, 4> ValueOperands(operands()); |
| 3449 | ArrayRef<Value*> Ops(ValueOperands); |
| 3450 | |
| 3451 | switch (getOpcode()) { |
| 3452 | case Instruction::Trunc: |
| 3453 | case Instruction::PtrToAddr: |
| 3454 | case Instruction::PtrToInt: |
| 3455 | case Instruction::IntToPtr: |
| 3456 | case Instruction::BitCast: |
| 3457 | case Instruction::AddrSpaceCast: |
| 3458 | return CastInst::Create((Instruction::CastOps)getOpcode(), S: Ops[0], |
| 3459 | Ty: getType(), Name: "" ); |
| 3460 | case Instruction::InsertElement: |
| 3461 | return InsertElementInst::Create(Vec: Ops[0], NewElt: Ops[1], Idx: Ops[2], NameStr: "" ); |
| 3462 | case Instruction::ExtractElement: |
| 3463 | return ExtractElementInst::Create(Vec: Ops[0], Idx: Ops[1], NameStr: "" ); |
| 3464 | case Instruction::ShuffleVector: |
| 3465 | return new ShuffleVectorInst(Ops[0], Ops[1], getShuffleMask(), "" ); |
| 3466 | |
| 3467 | case Instruction::GetElementPtr: { |
| 3468 | const auto *GO = cast<GEPOperator>(Val: this); |
| 3469 | return GetElementPtrInst::Create(PointeeType: GO->getSourceElementType(), Ptr: Ops[0], |
| 3470 | IdxList: Ops.slice(N: 1), NW: GO->getNoWrapFlags(), NameStr: "" ); |
| 3471 | } |
| 3472 | default: |
| 3473 | assert(getNumOperands() == 2 && "Must be binary operator?" ); |
| 3474 | BinaryOperator *BO = BinaryOperator::Create( |
| 3475 | Op: (Instruction::BinaryOps)getOpcode(), S1: Ops[0], S2: Ops[1], Name: "" ); |
| 3476 | if (isa<OverflowingBinaryOperator>(Val: BO)) { |
| 3477 | BO->setHasNoUnsignedWrap(SubclassOptionalData & |
| 3478 | OverflowingBinaryOperator::NoUnsignedWrap); |
| 3479 | BO->setHasNoSignedWrap(SubclassOptionalData & |
| 3480 | OverflowingBinaryOperator::NoSignedWrap); |
| 3481 | } |
| 3482 | if (isa<PossiblyExactOperator>(Val: BO)) |
| 3483 | BO->setIsExact(SubclassOptionalData & PossiblyExactOperator::IsExact); |
| 3484 | return BO; |
| 3485 | } |
| 3486 | } |
| 3487 | |