3487 lines · cpp
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-exception6//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 35using namespace llvm;36using namespace PatternMatch;37 38// As set of temporary options to help migrate how splats are represented.39static cl::opt<bool> UseConstantIntForFixedLengthSplat(40 "use-constant-int-for-fixed-length-splat", cl::init(false), cl::Hidden,41 cl::desc("Use ConstantInt's native fixed-length vector splat support."));42static cl::opt<bool> UseConstantFPForFixedLengthSplat(43 "use-constant-fp-for-fixed-length-splat", cl::init(false), cl::Hidden,44 cl::desc("Use ConstantFP's native fixed-length vector splat support."));45static cl::opt<bool> UseConstantIntForScalableSplat(46 "use-constant-int-for-scalable-splat", cl::init(false), cl::Hidden,47 cl::desc("Use ConstantInt's native scalable vector splat support."));48static cl::opt<bool> UseConstantFPForScalableSplat(49 "use-constant-fp-for-scalable-splat", cl::init(false), cl::Hidden,50 cl::desc("Use ConstantFP's native scalable vector splat support."));51 52//===----------------------------------------------------------------------===//53// Constant Class54//===----------------------------------------------------------------------===//55 56bool Constant::isNegativeZeroValue() const {57 // Floating point values have an explicit -0.0 value.58 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(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>(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), negative75// zero (floating point), or a null value.76bool Constant::isZeroValue() const {77 // Floating point values have an explicit -0.0 value.78 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(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>(getSplatValue()))84 return SplatCFP->isZero();85 86 // Otherwise, just use +0.0.87 return isNullValue();88}89 90bool Constant::isNullValue() const {91 // 0 is null.92 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))93 return CI->isZero();94 95 // +0.0 is null.96 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))97 // ppc_fp128 determine isZero using high order double only98 // Should check the bitwise value to make sure all bits are zero.99 return CFP->isExactlyValue(+0.0);100 101 // constant zero is zero for aggregates, cpnull is null for pointers, none for102 // tokens.103 return isa<ConstantAggregateZero>(this) || isa<ConstantPointerNull>(this) ||104 isa<ConstantTokenNone>(this) || isa<ConstantTargetNone>(this);105}106 107bool Constant::isAllOnesValue() const {108 // Check for -1 integers109 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))110 return CI->isMinusOne();111 112 // Check for FP which are bitcasted from -1 integers113 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(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 124bool Constant::isOneValue() const {125 // Check for 1 integers126 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))127 return CI->isOne();128 129 // Check for FP which are bitcasted from 1 integers130 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(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 141bool Constant::isNotOneValue() const {142 // Check for 1 integers143 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))144 return !CI->isOneValue();145 146 // Check for FP which are bitcasted from 1 integers147 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))148 return !CFP->getValueAPF().bitcastToAPInt().isOne();149 150 // Check that vectors don't contain 1151 if (auto *VTy = dyn_cast<FixedVectorType>(getType())) {152 for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {153 Constant *Elt = getAggregateElement(I);154 if (!Elt || !Elt->isNotOneValue())155 return false;156 }157 return true;158 }159 160 // Check for splats that don't contain 1161 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 169bool Constant::isMinSignedValue() const {170 // Check for INT_MIN integers171 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))172 return CI->isMinValue(/*isSigned=*/true);173 174 // Check for FP which are bitcasted from INT_MIN integers175 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(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 186bool Constant::isMaxSignedValue() const {187 // Check for INT_MAX integers188 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))189 return CI->isMaxValue(/*isSigned=*/true);190 191 // Check for FP which are bitcasted from INT_MAX integers192 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(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 203bool Constant::isNotMinSignedValue() const {204 // Check for INT_MIN integers205 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))206 return !CI->isMinValue(/*isSigned=*/true);207 208 // Check for FP which are bitcasted from INT_MIN integers209 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))210 return !CFP->getValueAPF().bitcastToAPInt().isMinSignedValue();211 212 // Check that vectors don't contain INT_MIN213 if (auto *VTy = dyn_cast<FixedVectorType>(getType())) {214 for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {215 Constant *Elt = getAggregateElement(I);216 if (!Elt || !Elt->isNotMinSignedValue())217 return false;218 }219 return true;220 }221 222 // Check for splats that aren't INT_MIN223 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 231bool Constant::isFiniteNonZeroFP() const {232 if (auto *CFP = dyn_cast<ConstantFP>(this))233 return CFP->getValueAPF().isFiniteNonZero();234 235 if (auto *VTy = dyn_cast<FixedVectorType>(getType())) {236 for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {237 auto *CFP = dyn_cast_or_null<ConstantFP>(getAggregateElement(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>(getSplatValue()))246 return SplatCFP->isFiniteNonZeroFP();247 248 // It *may* contain finite non-zero, we can't tell.249 return false;250}251 252bool Constant::isNormalFP() const {253 if (auto *CFP = dyn_cast<ConstantFP>(this))254 return CFP->getValueAPF().isNormal();255 256 if (auto *VTy = dyn_cast<FixedVectorType>(getType())) {257 for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {258 auto *CFP = dyn_cast_or_null<ConstantFP>(getAggregateElement(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>(getSplatValue()))267 return SplatCFP->isNormalFP();268 269 // It *may* contain a normal fp value, we can't tell.270 return false;271}272 273bool Constant::hasExactInverseFP() const {274 if (auto *CFP = dyn_cast<ConstantFP>(this))275 return CFP->getValueAPF().getExactInverse(nullptr);276 277 if (auto *VTy = dyn_cast<FixedVectorType>(getType())) {278 for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {279 auto *CFP = dyn_cast_or_null<ConstantFP>(getAggregateElement(I));280 if (!CFP || !CFP->getValueAPF().getExactInverse(nullptr))281 return false;282 }283 return true;284 }285 286 if (getType()->isVectorTy())287 if (const auto *SplatCFP = dyn_cast_or_null<ConstantFP>(getSplatValue()))288 return SplatCFP->hasExactInverseFP();289 290 // It *may* have an exact inverse fp value, we can't tell.291 return false;292}293 294bool Constant::isNaN() const {295 if (auto *CFP = dyn_cast<ConstantFP>(this))296 return CFP->isNaN();297 298 if (auto *VTy = dyn_cast<FixedVectorType>(getType())) {299 for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {300 auto *CFP = dyn_cast_or_null<ConstantFP>(getAggregateElement(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>(getSplatValue()))309 return SplatCFP->isNaN();310 311 // It *may* be NaN, we can't tell.312 return false;313}314 315bool 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>(getType());322 if (!isa<Constant>(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(const_cast<Constant *>(this), IntTy);334 Constant *C1 = ConstantExpr::getBitCast(cast<Constant>(Y), IntTy);335 Constant *CmpEq = ConstantFoldCompareInstruction(ICmpInst::ICMP_EQ, C0, C1);336 return CmpEq && (isa<PoisonValue>(CmpEq) || match(CmpEq, m_One()));337}338 339static bool340containsUndefinedElement(const Constant *C,341 function_ref<bool(const Constant *)> HasFn) {342 if (auto *VTy = dyn_cast<VectorType>(C->getType())) {343 if (HasFn(C))344 return true;345 if (isa<ConstantAggregateZero>(C))346 return false;347 if (isa<ScalableVectorType>(C->getType()))348 return false;349 350 for (unsigned i = 0, e = cast<FixedVectorType>(VTy)->getNumElements();351 i != e; ++i) {352 if (Constant *Elem = C->getAggregateElement(i))353 if (HasFn(Elem))354 return true;355 }356 }357 358 return false;359}360 361bool Constant::containsUndefOrPoisonElement() const {362 return containsUndefinedElement(363 this, [&](const auto *C) { return isa<UndefValue>(C); });364}365 366bool Constant::containsPoisonElement() const {367 return containsUndefinedElement(368 this, [&](const auto *C) { return isa<PoisonValue>(C); });369}370 371bool Constant::containsUndefElement() const {372 return containsUndefinedElement(this, [&](const auto *C) {373 return isa<UndefValue>(C) && !isa<PoisonValue>(C);374 });375}376 377bool Constant::containsConstantExpression() const {378 if (isa<ConstantInt>(this) || isa<ConstantFP>(this))379 return false;380 381 if (auto *VTy = dyn_cast<FixedVectorType>(getType())) {382 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i)383 if (isa<ConstantExpr>(getAggregateElement(i)))384 return true;385 }386 return false;387}388 389/// Constructor to create a '0' constant of arbitrary type.390Constant *Constant::getNullValue(Type *Ty) {391 switch (Ty->getTypeID()) {392 case Type::IntegerTyID:393 return ConstantInt::get(Ty, 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(Ty->getContext(),402 APFloat::getZero(Ty->getFltSemantics()));403 case Type::PointerTyID:404 return ConstantPointerNull::get(cast<PointerType>(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(Ty->getContext());412 case Type::TargetExtTyID:413 return ConstantTargetNone::get(cast<TargetExtType>(Ty));414 default:415 // Function, Label, or Opaque type?416 llvm_unreachable("Cannot create a null constant of that type!");417 }418}419 420Constant *Constant::getIntegerValue(Type *Ty, const APInt &V) {421 Type *ScalarTy = Ty->getScalarType();422 423 // Create the base integer constant.424 Constant *C = ConstantInt::get(Ty->getContext(), V);425 426 // Convert an integer to a pointer, if necessary.427 if (PointerType *PTy = dyn_cast<PointerType>(ScalarTy))428 C = ConstantExpr::getIntToPtr(C, PTy);429 430 // Broadcast a scalar to a vector, if necessary.431 if (VectorType *VTy = dyn_cast<VectorType>(Ty))432 C = ConstantVector::getSplat(VTy->getElementCount(), C);433 434 return C;435}436 437Constant *Constant::getAllOnesValue(Type *Ty) {438 if (IntegerType *ITy = dyn_cast<IntegerType>(Ty))439 return ConstantInt::get(Ty->getContext(),440 APInt::getAllOnes(ITy->getBitWidth()));441 442 if (Ty->isFloatingPointTy()) {443 APFloat FL = APFloat::getAllOnesValue(Ty->getFltSemantics());444 return ConstantFP::get(Ty->getContext(), FL);445 }446 447 VectorType *VTy = cast<VectorType>(Ty);448 return ConstantVector::getSplat(VTy->getElementCount(),449 getAllOnesValue(VTy->getElementType()));450}451 452Constant *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>(this))457 return Elt < CC->getNumOperands() ? CC->getOperand(Elt) : nullptr;458 459 if (const auto *CAZ = dyn_cast<ConstantAggregateZero>(this))460 return Elt < CAZ->getElementCount().getKnownMinValue()461 ? CAZ->getElementValue(Elt)462 : nullptr;463 464 if (const auto *CI = dyn_cast<ConstantInt>(this))465 return Elt < cast<VectorType>(getType())466 ->getElementCount()467 .getKnownMinValue()468 ? ConstantInt::get(getContext(), CI->getValue())469 : nullptr;470 471 if (const auto *CFP = dyn_cast<ConstantFP>(this))472 return Elt < cast<VectorType>(getType())473 ->getElementCount()474 .getKnownMinValue()475 ? ConstantFP::get(getContext(), CFP->getValue())476 : nullptr;477 478 // FIXME: getNumElements() will fail for non-fixed vector types.479 if (isa<ScalableVectorType>(getType()))480 return nullptr;481 482 if (const auto *PV = dyn_cast<PoisonValue>(this))483 return Elt < PV->getNumElements() ? PV->getElementValue(Elt) : nullptr;484 485 if (const auto *UV = dyn_cast<UndefValue>(this))486 return Elt < UV->getNumElements() ? UV->getElementValue(Elt) : nullptr;487 488 if (const auto *CDS = dyn_cast<ConstantDataSequential>(this))489 return Elt < CDS->getNumElements() ? CDS->getElementAsConstant(Elt)490 : nullptr;491 492 return nullptr;493}494 495Constant *Constant::getAggregateElement(Constant *Elt) const {496 assert(isa<IntegerType>(Elt->getType()) && "Index must be an integer");497 if (ConstantInt *CI = dyn_cast<ConstantInt>(Elt)) {498 // Check if the constant fits into an uint64_t.499 if (CI->getValue().getActiveBits() > 64)500 return nullptr;501 return getAggregateElement(CI->getZExtValue());502 }503 return nullptr;504}505 506void Constant::destroyConstant() {507 /// First call destroyConstantImpl on the subclass. This gives the subclass508 /// 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 lingering520 // references to the constant by other constants in the constant pool. These521 // 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 is523 // deleted, we must now notify all of our users (that should only be524 // 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: " << *this531 << "\n\nUse still stuck around after Def is destroyed: " << *V532 << "\n\n";533 }534#endif535 assert(isa<Constant>(V) && "References remain to Constant being destroyed");536 cast<Constant>(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(this);544}545 546void 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>(C))598 delete static_cast<CastConstantExpr *>(C);599 else if (isa<BinaryConstantExpr>(C))600 delete static_cast<BinaryConstantExpr *>(C);601 else if (isa<ExtractElementConstantExpr>(C))602 delete static_cast<ExtractElementConstantExpr *>(C);603 else if (isa<InsertElementConstantExpr>(C))604 delete static_cast<InsertElementConstantExpr *>(C);605 else if (isa<ShuffleVectorConstantExpr>(C))606 delete static_cast<ShuffleVectorConstantExpr *>(C);607 else if (isa<GetElementPtrConstantExpr>(C))608 delete static_cast<GetElementPtrConstantExpr *>(C);609 else610 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.618static bool619ConstHasGlobalValuePredicate(const Constant *C,620 bool (*Predicate)(const GlobalValue *)) {621 SmallPtrSet<const Constant *, 8> Visited;622 SmallVector<const Constant *, 8> WorkList;623 WorkList.push_back(C);624 Visited.insert(C);625 626 while (!WorkList.empty()) {627 const Constant *WorkItem = WorkList.pop_back_val();628 if (const auto *GV = dyn_cast<GlobalValue>(WorkItem))629 if (Predicate(GV))630 return true;631 for (const Value *Op : WorkItem->operands()) {632 const Constant *ConstOp = dyn_cast<Constant>(Op);633 if (!ConstOp)634 continue;635 if (Visited.insert(ConstOp).second)636 WorkList.push_back(ConstOp);637 }638 }639 return false;640}641 642bool Constant::isThreadDependent() const {643 auto DLLImportPredicate = [](const GlobalValue *GV) {644 return GV->isThreadLocal();645 };646 return ConstHasGlobalValuePredicate(this, DLLImportPredicate);647}648 649bool Constant::isDLLImportDependent() const {650 auto DLLImportPredicate = [](const GlobalValue *GV) {651 return GV->hasDLLImportStorageClass();652 };653 return ConstHasGlobalValuePredicate(this, DLLImportPredicate);654}655 656bool Constant::isConstantUsed() const {657 for (const User *U : users()) {658 const Constant *UC = dyn_cast<Constant>(U);659 if (!UC || isa<GlobalValue>(UC))660 return true;661 662 if (UC->isConstantUsed())663 return true;664 }665 return false;666}667 668bool Constant::needsDynamicRelocation() const {669 return getRelocationInfo() == GlobalRelocation;670}671 672bool Constant::needsRelocation() const {673 return getRelocationInfo() != NoRelocation;674}675 676Constant::PossibleRelocationsTy Constant::getRelocationInfo() const {677 if (isa<GlobalValue>(this))678 return GlobalRelocation; // Global reference.679 680 if (const BlockAddress *BA = dyn_cast<BlockAddress>(this))681 return BA->getFunction()->getRelocationInfo();682 683 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(this)) {684 if (CE->getOpcode() == Instruction::Sub) {685 ConstantExpr *LHS = dyn_cast<ConstantExpr>(CE->getOperand(0));686 ConstantExpr *RHS = dyn_cast<ConstantExpr>(CE->getOperand(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(0);693 Constant *RHSOp0 = RHS->getOperand(0);694 695 // While raw uses of blockaddress need to be relocated, differences696 // between two of them don't when they are for labels in the same697 // function. This is a common idiom when creating a table for the698 // indirect goto extension, so we handle it efficiently here.699 if (isa<BlockAddress>(LHSOp0) && isa<BlockAddress>(RHSOp0) &&700 cast<BlockAddress>(LHSOp0)->getFunction() ==701 cast<BlockAddress>(RHSOp0)->getFunction())702 return NoRelocation;703 704 // Relative pointers do not need to be dynamically relocated.705 if (auto *RHSGV =706 dyn_cast<GlobalValue>(RHSOp0->stripInBoundsConstantOffsets())) {707 auto *LHS = LHSOp0->stripInBoundsConstantOffsets();708 if (auto *LHSGV = dyn_cast<GlobalValue>(LHS)) {709 if (LHSGV->isDSOLocal() && RHSGV->isDSOLocal())710 return LocalRelocation;711 } else if (isa<DSOLocalEquivalent>(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(cast<Constant>(Op)->getRelocationInfo(), Result);723 724 return Result;725}726 727/// Return true if the specified constantexpr is dead. This involves728/// recursively traversing users of the constantexpr.729/// If RemoveDeadUsers is true, also remove dead users at the same time.730static bool constantIsDead(const Constant *C, bool RemoveDeadUsers) {731 if (isa<GlobalValue>(C)) return false; // Cannot remove this732 733 Value::const_user_iterator I = C->user_begin(), E = C->user_end();734 while (I != E) {735 const Constant *User = dyn_cast<Constant>(*I);736 if (!User) return false; // Non-constant usage;737 if (!constantIsDead(User, RemoveDeadUsers))738 return false; // Constant wasn't dead739 740 // Just removed User, so the iterator was invalidated.741 // Since we return immediately upon finding a live user, we can always742 // restart from user_begin().743 if (RemoveDeadUsers)744 I = C->user_begin();745 else746 ++I;747 }748 749 if (RemoveDeadUsers) {750 // If C is only used by metadata, it should not be preserved but should751 // have its uses replaced.752 ReplaceableMetadataImpl::SalvageDebugInfo(*C);753 const_cast<Constant *>(C)->destroyConstant();754 }755 756 return true;757}758 759void 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>(*I);764 if (!User) {765 LastNonDeadUser = I;766 ++I;767 continue;768 }769 770 if (!constantIsDead(User, /* RemoveDeadUsers= */ true)) {771 // If the constant wasn't dead, remember that this was the last live use772 // 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 else782 I = std::next(LastNonDeadUser);783 }784}785 786bool Constant::hasOneLiveUse() const { return hasNLiveUses(1); }787 788bool Constant::hasZeroLiveUses() const { return hasNLiveUses(0); }789 790bool Constant::hasNLiveUses(unsigned N) const {791 unsigned NumUses = 0;792 for (const Use &U : uses()) {793 const Constant *User = dyn_cast<Constant>(U.getUser());794 if (!User || !constantIsDead(User, /* RemoveDeadUsers= */ false)) {795 ++NumUses;796 797 if (NumUses > N)798 return false;799 }800 }801 return NumUses == N;802}803 804Constant *Constant::replaceUndefsWith(Constant *C, Constant *Replacement) {805 assert(C && Replacement && "Expected non-nullptr constant arguments");806 Type *Ty = C->getType();807 if (match(C, 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>(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(i);821 assert((!EltC || EltC->getType() == Replacement->getType()) &&822 "Expected matching types");823 NewC[i] = EltC && match(EltC, m_Undef()) ? Replacement : EltC;824 }825 return ConstantVector::get(NewC);826}827 828Constant *Constant::mergeUndefsWith(Constant *C, Constant *Other) {829 assert(C && Other && "Expected non-nullptr constant arguments");830 if (match(C, m_Undef()))831 return C;832 833 Type *Ty = C->getType();834 if (match(Other, m_Undef()))835 return UndefValue::get(Ty);836 837 auto *VTy = dyn_cast<FixedVectorType>(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 FoundExtraUndef = false;848 SmallVector<Constant *, 32> NewC(NumElts);849 for (unsigned I = 0; I != NumElts; ++I) {850 NewC[I] = C->getAggregateElement(I);851 Constant *OtherEltC = Other->getAggregateElement(I);852 assert(NewC[I] && OtherEltC && "Unknown vector element");853 if (!match(NewC[I], m_Undef()) && match(OtherEltC, m_Undef())) {854 NewC[I] = UndefValue::get(EltTy);855 FoundExtraUndef = true;856 }857 }858 if (FoundExtraUndef)859 return ConstantVector::get(NewC);860 return C;861}862 863bool Constant::isManifestConstant() const {864 if (isa<UndefValue>(this))865 return false;866 if (isa<ConstantData>(this))867 return true;868 if (isa<ConstantAggregate>(this) || isa<ConstantExpr>(this)) {869 for (const Value *Op : operand_values())870 if (!cast<Constant>(Op)->isManifestConstant())871 return false;872 return true;873 }874 return false;875}876 877//===----------------------------------------------------------------------===//878// ConstantInt879//===----------------------------------------------------------------------===//880 881ConstantInt::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 888ConstantInt *ConstantInt::getTrue(LLVMContext &Context) {889 LLVMContextImpl *pImpl = Context.pImpl;890 if (!pImpl->TheTrueVal)891 pImpl->TheTrueVal = ConstantInt::get(Type::getInt1Ty(Context), 1);892 return pImpl->TheTrueVal;893}894 895ConstantInt *ConstantInt::getFalse(LLVMContext &Context) {896 LLVMContextImpl *pImpl = Context.pImpl;897 if (!pImpl->TheFalseVal)898 pImpl->TheFalseVal = ConstantInt::get(Type::getInt1Ty(Context), 0);899 return pImpl->TheFalseVal;900}901 902ConstantInt *ConstantInt::getBool(LLVMContext &Context, bool V) {903 return V ? getTrue(Context) : getFalse(Context);904}905 906Constant *ConstantInt::getTrue(Type *Ty) {907 assert(Ty->isIntOrIntVectorTy(1) && "Type not i1 or vector of i1.");908 ConstantInt *TrueC = ConstantInt::getTrue(Ty->getContext());909 if (auto *VTy = dyn_cast<VectorType>(Ty))910 return ConstantVector::getSplat(VTy->getElementCount(), TrueC);911 return TrueC;912}913 914Constant *ConstantInt::getFalse(Type *Ty) {915 assert(Ty->isIntOrIntVectorTy(1) && "Type not i1 or vector of i1.");916 ConstantInt *FalseC = ConstantInt::getFalse(Ty->getContext());917 if (auto *VTy = dyn_cast<VectorType>(Ty))918 return ConstantVector::getSplat(VTy->getElementCount(), FalseC);919 return FalseC;920}921 922Constant *ConstantInt::getBool(Type *Ty, bool V) {923 return V ? getTrue(Ty) : getFalse(Ty);924}925 926// Get a ConstantInt from an APInt.927ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt &V) {928 // get an existing value or the insertion position929 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(Context, V.getBitWidth());937 Slot.reset(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.944ConstantInt *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(EC, V)];949 if (!Slot) {950 IntegerType *ITy = IntegerType::get(Context, V.getBitWidth());951 VectorType *VTy = VectorType::get(ITy, EC);952 Slot.reset(new ConstantInt(VTy, V));953 }954 955#ifndef NDEBUG956 IntegerType *ITy = IntegerType::get(Context, V.getBitWidth());957 VectorType *VTy = VectorType::get(ITy, EC);958 assert(Slot->getType() == VTy);959#endif960 return Slot.get();961}962 963Constant *ConstantInt::get(Type *Ty, uint64_t V, bool isSigned) {964 Constant *C = get(cast<IntegerType>(Ty->getScalarType()), V, isSigned);965 966 // For vectors, broadcast the value.967 if (VectorType *VTy = dyn_cast<VectorType>(Ty))968 return ConstantVector::getSplat(VTy->getElementCount(), C);969 970 return C;971}972 973ConstantInt *ConstantInt::get(IntegerType *Ty, uint64_t V, bool isSigned) {974 // TODO: Avoid implicit trunc?975 // See https://github.com/llvm/llvm-project/issues/112510.976 return get(Ty->getContext(),977 APInt(Ty->getBitWidth(), V, isSigned, /*implicitTrunc=*/true));978}979 980Constant *ConstantInt::get(Type *Ty, const APInt& V) {981 ConstantInt *C = get(Ty->getContext(), V);982 assert(C->getType() == Ty->getScalarType() &&983 "ConstantInt type doesn't match the type implied by its value!");984 985 // For vectors, broadcast the value.986 if (VectorType *VTy = dyn_cast<VectorType>(Ty))987 return ConstantVector::getSplat(VTy->getElementCount(), C);988 989 return C;990}991 992ConstantInt *ConstantInt::get(IntegerType* Ty, StringRef Str, uint8_t radix) {993 return get(Ty->getContext(), APInt(Ty->getBitWidth(), Str, radix));994}995 996/// Remove the constant from the constant table.997void ConstantInt::destroyConstantImpl() {998 llvm_unreachable("You can't ConstantInt->destroyConstantImpl()!");999}1000 1001//===----------------------------------------------------------------------===//1002// ConstantFP1003//===----------------------------------------------------------------------===//1004 1005Constant *ConstantFP::get(Type *Ty, double V) {1006 LLVMContext &Context = Ty->getContext();1007 1008 APFloat FV(V);1009 bool ignored;1010 FV.convert(Ty->getScalarType()->getFltSemantics(),1011 APFloat::rmNearestTiesToEven, &ignored);1012 Constant *C = get(Context, FV);1013 1014 // For vectors, broadcast the value.1015 if (VectorType *VTy = dyn_cast<VectorType>(Ty))1016 return ConstantVector::getSplat(VTy->getElementCount(), C);1017 1018 return C;1019}1020 1021Constant *ConstantFP::get(Type *Ty, const APFloat &V) {1022 ConstantFP *C = get(Ty->getContext(), V);1023 assert(C->getType() == Ty->getScalarType() &&1024 "ConstantFP type doesn't match the type implied by its value!");1025 1026 // For vectors, broadcast the value.1027 if (auto *VTy = dyn_cast<VectorType>(Ty))1028 return ConstantVector::getSplat(VTy->getElementCount(), C);1029 1030 return C;1031}1032 1033Constant *ConstantFP::get(Type *Ty, StringRef Str) {1034 LLVMContext &Context = Ty->getContext();1035 1036 APFloat FV(Ty->getScalarType()->getFltSemantics(), Str);1037 Constant *C = get(Context, FV);1038 1039 // For vectors, broadcast the value.1040 if (VectorType *VTy = dyn_cast<VectorType>(Ty))1041 return ConstantVector::getSplat(VTy->getElementCount(), C);1042 1043 return C;1044}1045 1046Constant *ConstantFP::getNaN(Type *Ty, bool Negative, uint64_t Payload) {1047 const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics();1048 APFloat NaN = APFloat::getNaN(Semantics, Negative, Payload);1049 Constant *C = get(Ty->getContext(), NaN);1050 1051 if (VectorType *VTy = dyn_cast<VectorType>(Ty))1052 return ConstantVector::getSplat(VTy->getElementCount(), C);1053 1054 return C;1055}1056 1057Constant *ConstantFP::getQNaN(Type *Ty, bool Negative, APInt *Payload) {1058 const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics();1059 APFloat NaN = APFloat::getQNaN(Semantics, Negative, Payload);1060 Constant *C = get(Ty->getContext(), NaN);1061 1062 if (VectorType *VTy = dyn_cast<VectorType>(Ty))1063 return ConstantVector::getSplat(VTy->getElementCount(), C);1064 1065 return C;1066}1067 1068Constant *ConstantFP::getSNaN(Type *Ty, bool Negative, APInt *Payload) {1069 const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics();1070 APFloat NaN = APFloat::getSNaN(Semantics, Negative, Payload);1071 Constant *C = get(Ty->getContext(), NaN);1072 1073 if (VectorType *VTy = dyn_cast<VectorType>(Ty))1074 return ConstantVector::getSplat(VTy->getElementCount(), C);1075 1076 return C;1077}1078 1079Constant *ConstantFP::getZero(Type *Ty, bool Negative) {1080 const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics();1081 APFloat NegZero = APFloat::getZero(Semantics, Negative);1082 Constant *C = get(Ty->getContext(), NegZero);1083 1084 if (VectorType *VTy = dyn_cast<VectorType>(Ty))1085 return ConstantVector::getSplat(VTy->getElementCount(), C);1086 1087 return C;1088}1089 1090 1091// ConstantFP accessors.1092ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) {1093 LLVMContextImpl* pImpl = Context.pImpl;1094 1095 std::unique_ptr<ConstantFP> &Slot = pImpl->FPConstants[V];1096 1097 if (!Slot) {1098 Type *Ty = Type::getFloatingPointTy(Context, V.getSemantics());1099 Slot.reset(new ConstantFP(Ty, V));1100 }1101 1102 return Slot.get();1103}1104 1105// Get a ConstantFP vector with each lane set to the same APFloat.1106ConstantFP *ConstantFP::get(LLVMContext &Context, ElementCount EC,1107 const APFloat &V) {1108 // Get an existing value or the insertion position.1109 std::unique_ptr<ConstantFP> &Slot =1110 Context.pImpl->FPSplatConstants[std::make_pair(EC, V)];1111 if (!Slot) {1112 Type *EltTy = Type::getFloatingPointTy(Context, V.getSemantics());1113 VectorType *VTy = VectorType::get(EltTy, EC);1114 Slot.reset(new ConstantFP(VTy, V));1115 }1116 1117#ifndef NDEBUG1118 Type *EltTy = Type::getFloatingPointTy(Context, V.getSemantics());1119 VectorType *VTy = VectorType::get(EltTy, EC);1120 assert(Slot->getType() == VTy);1121#endif1122 return Slot.get();1123}1124 1125Constant *ConstantFP::getInfinity(Type *Ty, bool Negative) {1126 const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics();1127 Constant *C = get(Ty->getContext(), APFloat::getInf(Semantics, Negative));1128 1129 if (VectorType *VTy = dyn_cast<VectorType>(Ty))1130 return ConstantVector::getSplat(VTy->getElementCount(), C);1131 1132 return C;1133}1134 1135ConstantFP::ConstantFP(Type *Ty, const APFloat &V)1136 : ConstantData(Ty, ConstantFPVal), Val(V) {1137 assert(&V.getSemantics() == &Ty->getScalarType()->getFltSemantics() &&1138 "FP type Mismatch");1139}1140 1141bool ConstantFP::isExactlyValue(const APFloat &V) const {1142 return Val.bitwiseIsEqual(V);1143}1144 1145/// Remove the constant from the constant table.1146void ConstantFP::destroyConstantImpl() {1147 llvm_unreachable("You can't ConstantFP->destroyConstantImpl()!");1148}1149 1150//===----------------------------------------------------------------------===//1151// ConstantAggregateZero Implementation1152//===----------------------------------------------------------------------===//1153 1154Constant *ConstantAggregateZero::getSequentialElement() const {1155 if (auto *AT = dyn_cast<ArrayType>(getType()))1156 return Constant::getNullValue(AT->getElementType());1157 return Constant::getNullValue(cast<VectorType>(getType())->getElementType());1158}1159 1160Constant *ConstantAggregateZero::getStructElement(unsigned Elt) const {1161 return Constant::getNullValue(getType()->getStructElementType(Elt));1162}1163 1164Constant *ConstantAggregateZero::getElementValue(Constant *C) const {1165 if (isa<ArrayType>(getType()) || isa<VectorType>(getType()))1166 return getSequentialElement();1167 return getStructElement(cast<ConstantInt>(C)->getZExtValue());1168}1169 1170Constant *ConstantAggregateZero::getElementValue(unsigned Idx) const {1171 if (isa<ArrayType>(getType()) || isa<VectorType>(getType()))1172 return getSequentialElement();1173 return getStructElement(Idx);1174}1175 1176ElementCount ConstantAggregateZero::getElementCount() const {1177 Type *Ty = getType();1178 if (auto *AT = dyn_cast<ArrayType>(Ty))1179 return ElementCount::getFixed(AT->getNumElements());1180 if (auto *VT = dyn_cast<VectorType>(Ty))1181 return VT->getElementCount();1182 return ElementCount::getFixed(Ty->getStructNumElements());1183}1184 1185//===----------------------------------------------------------------------===//1186// UndefValue Implementation1187//===----------------------------------------------------------------------===//1188 1189UndefValue *UndefValue::getSequentialElement() const {1190 if (ArrayType *ATy = dyn_cast<ArrayType>(getType()))1191 return UndefValue::get(ATy->getElementType());1192 return UndefValue::get(cast<VectorType>(getType())->getElementType());1193}1194 1195UndefValue *UndefValue::getStructElement(unsigned Elt) const {1196 return UndefValue::get(getType()->getStructElementType(Elt));1197}1198 1199UndefValue *UndefValue::getElementValue(Constant *C) const {1200 if (isa<ArrayType>(getType()) || isa<VectorType>(getType()))1201 return getSequentialElement();1202 return getStructElement(cast<ConstantInt>(C)->getZExtValue());1203}1204 1205UndefValue *UndefValue::getElementValue(unsigned Idx) const {1206 if (isa<ArrayType>(getType()) || isa<VectorType>(getType()))1207 return getSequentialElement();1208 return getStructElement(Idx);1209}1210 1211unsigned UndefValue::getNumElements() const {1212 Type *Ty = getType();1213 if (auto *AT = dyn_cast<ArrayType>(Ty))1214 return AT->getNumElements();1215 if (auto *VT = dyn_cast<VectorType>(Ty))1216 return cast<FixedVectorType>(VT)->getNumElements();1217 return Ty->getStructNumElements();1218}1219 1220//===----------------------------------------------------------------------===//1221// PoisonValue Implementation1222//===----------------------------------------------------------------------===//1223 1224PoisonValue *PoisonValue::getSequentialElement() const {1225 if (ArrayType *ATy = dyn_cast<ArrayType>(getType()))1226 return PoisonValue::get(ATy->getElementType());1227 return PoisonValue::get(cast<VectorType>(getType())->getElementType());1228}1229 1230PoisonValue *PoisonValue::getStructElement(unsigned Elt) const {1231 return PoisonValue::get(getType()->getStructElementType(Elt));1232}1233 1234PoisonValue *PoisonValue::getElementValue(Constant *C) const {1235 if (isa<ArrayType>(getType()) || isa<VectorType>(getType()))1236 return getSequentialElement();1237 return getStructElement(cast<ConstantInt>(C)->getZExtValue());1238}1239 1240PoisonValue *PoisonValue::getElementValue(unsigned Idx) const {1241 if (isa<ArrayType>(getType()) || isa<VectorType>(getType()))1242 return getSequentialElement();1243 return getStructElement(Idx);1244}1245 1246//===----------------------------------------------------------------------===//1247// ConstantXXX Classes1248//===----------------------------------------------------------------------===//1249 1250template <typename ItTy, typename EltTy>1251static bool rangeOnlyContains(ItTy Start, ItTy End, EltTy Elt) {1252 for (; Start != End; ++Start)1253 if (*Start != Elt)1254 return false;1255 return true;1256}1257 1258template <typename SequentialTy, typename ElementTy>1259static Constant *getIntSequenceIfElementsMatch(ArrayRef<Constant *> V) {1260 assert(!V.empty() && "Cannot get empty int sequence.");1261 1262 SmallVector<ElementTy, 16> Elts;1263 for (Constant *C : V)1264 if (auto *CI = dyn_cast<ConstantInt>(C))1265 Elts.push_back(CI->getZExtValue());1266 else1267 return nullptr;1268 return SequentialTy::get(V[0]->getContext(), Elts);1269}1270 1271template <typename SequentialTy, typename ElementTy>1272static Constant *getFPSequenceIfElementsMatch(ArrayRef<Constant *> V) {1273 assert(!V.empty() && "Cannot get empty FP sequence.");1274 1275 SmallVector<ElementTy, 16> Elts;1276 for (Constant *C : V)1277 if (auto *CFP = dyn_cast<ConstantFP>(C))1278 Elts.push_back(CFP->getValueAPF().bitcastToAPInt().getLimitedValue());1279 else1280 return nullptr;1281 return SequentialTy::getFP(V[0]->getType(), Elts);1282}1283 1284template <typename SequenceTy>1285static Constant *getSequenceIfElementsMatch(Constant *C,1286 ArrayRef<Constant *> V) {1287 // We speculatively build the elements here even if it turns out that there is1288 // a constantexpr or something else weird, since it is so uncommon for that to1289 // happen.1290 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {1291 if (CI->getType()->isIntegerTy(8))1292 return getIntSequenceIfElementsMatch<SequenceTy, uint8_t>(V);1293 else if (CI->getType()->isIntegerTy(16))1294 return getIntSequenceIfElementsMatch<SequenceTy, uint16_t>(V);1295 else if (CI->getType()->isIntegerTy(32))1296 return getIntSequenceIfElementsMatch<SequenceTy, uint32_t>(V);1297 else if (CI->getType()->isIntegerTy(64))1298 return getIntSequenceIfElementsMatch<SequenceTy, uint64_t>(V);1299 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {1300 if (CFP->getType()->isHalfTy() || CFP->getType()->isBFloatTy())1301 return getFPSequenceIfElementsMatch<SequenceTy, uint16_t>(V);1302 else if (CFP->getType()->isFloatTy())1303 return getFPSequenceIfElementsMatch<SequenceTy, uint32_t>(V);1304 else if (CFP->getType()->isDoubleTy())1305 return getFPSequenceIfElementsMatch<SequenceTy, uint64_t>(V);1306 }1307 1308 return nullptr;1309}1310 1311ConstantAggregate::ConstantAggregate(Type *T, ValueTy VT,1312 ArrayRef<Constant *> V,1313 AllocInfo AllocInfo)1314 : Constant(T, VT, AllocInfo) {1315 llvm::copy(V, op_begin());1316 1317 // Check that types match, unless this is an opaque struct.1318 if (auto *ST = dyn_cast<StructType>(T)) {1319 if (ST->isOpaque())1320 return;1321 for (unsigned I = 0, E = V.size(); I != E; ++I)1322 assert(V[I]->getType() == ST->getTypeAtIndex(I) &&1323 "Initializer for struct element doesn't match!");1324 }1325}1326 1327ConstantArray::ConstantArray(ArrayType *T, ArrayRef<Constant *> V,1328 AllocInfo AllocInfo)1329 : ConstantAggregate(T, ConstantArrayVal, V, AllocInfo) {1330 assert(V.size() == T->getNumElements() &&1331 "Invalid initializer for constant array");1332}1333 1334Constant *ConstantArray::get(ArrayType *Ty, ArrayRef<Constant*> V) {1335 if (Constant *C = getImpl(Ty, V))1336 return C;1337 return Ty->getContext().pImpl->ArrayConstants.getOrCreate(Ty, V);1338}1339 1340Constant *ConstantArray::getImpl(ArrayType *Ty, ArrayRef<Constant*> V) {1341 // Empty arrays are canonicalized to ConstantAggregateZero.1342 if (V.empty())1343 return ConstantAggregateZero::get(Ty);1344 1345 for (Constant *C : V) {1346 assert(C->getType() == Ty->getElementType() &&1347 "Wrong type in array element initializer");1348 (void)C;1349 }1350 1351 // If this is an all-zero array, return a ConstantAggregateZero object. If1352 // all undef, return an UndefValue, if "all simple", then return a1353 // ConstantDataArray.1354 Constant *C = V[0];1355 if (isa<PoisonValue>(C) && rangeOnlyContains(V.begin(), V.end(), C))1356 return PoisonValue::get(Ty);1357 1358 if (isa<UndefValue>(C) && rangeOnlyContains(V.begin(), V.end(), C))1359 return UndefValue::get(Ty);1360 1361 if (C->isNullValue() && rangeOnlyContains(V.begin(), V.end(), C))1362 return ConstantAggregateZero::get(Ty);1363 1364 // Check to see if all of the elements are ConstantFP or ConstantInt and if1365 // the element type is compatible with ConstantDataVector. If so, use it.1366 if (ConstantDataSequential::isElementTypeCompatible(C->getType()))1367 return getSequenceIfElementsMatch<ConstantDataArray>(C, V);1368 1369 // Otherwise, we really do want to create a ConstantArray.1370 return nullptr;1371}1372 1373StructType *ConstantStruct::getTypeForElements(LLVMContext &Context,1374 ArrayRef<Constant*> V,1375 bool Packed) {1376 unsigned VecSize = V.size();1377 SmallVector<Type*, 16> EltTypes(VecSize);1378 for (unsigned i = 0; i != VecSize; ++i)1379 EltTypes[i] = V[i]->getType();1380 1381 return StructType::get(Context, EltTypes, Packed);1382}1383 1384 1385StructType *ConstantStruct::getTypeForElements(ArrayRef<Constant*> V,1386 bool Packed) {1387 assert(!V.empty() &&1388 "ConstantStruct::getTypeForElements cannot be called on empty list");1389 return getTypeForElements(V[0]->getContext(), V, Packed);1390}1391 1392ConstantStruct::ConstantStruct(StructType *T, ArrayRef<Constant *> V,1393 AllocInfo AllocInfo)1394 : ConstantAggregate(T, ConstantStructVal, V, AllocInfo) {1395 assert((T->isOpaque() || V.size() == T->getNumElements()) &&1396 "Invalid initializer for constant struct");1397}1398 1399// ConstantStruct accessors.1400Constant *ConstantStruct::get(StructType *ST, ArrayRef<Constant*> V) {1401 assert((ST->isOpaque() || ST->getNumElements() == V.size()) &&1402 "Incorrect # elements specified to ConstantStruct::get");1403 1404 // Create a ConstantAggregateZero value if all elements are zeros.1405 bool isZero = true;1406 bool isUndef = false;1407 bool isPoison = false;1408 1409 if (!V.empty()) {1410 isUndef = isa<UndefValue>(V[0]);1411 isPoison = isa<PoisonValue>(V[0]);1412 isZero = V[0]->isNullValue();1413 // PoisonValue inherits UndefValue, so its check is not necessary.1414 if (isUndef || isZero) {1415 for (Constant *C : V) {1416 if (!C->isNullValue())1417 isZero = false;1418 if (!isa<PoisonValue>(C))1419 isPoison = false;1420 if (isa<PoisonValue>(C) || !isa<UndefValue>(C))1421 isUndef = false;1422 }1423 }1424 }1425 if (isZero)1426 return ConstantAggregateZero::get(ST);1427 if (isPoison)1428 return PoisonValue::get(ST);1429 if (isUndef)1430 return UndefValue::get(ST);1431 1432 return ST->getContext().pImpl->StructConstants.getOrCreate(ST, V);1433}1434 1435ConstantVector::ConstantVector(VectorType *T, ArrayRef<Constant *> V,1436 AllocInfo AllocInfo)1437 : ConstantAggregate(T, ConstantVectorVal, V, AllocInfo) {1438 assert(V.size() == cast<FixedVectorType>(T)->getNumElements() &&1439 "Invalid initializer for constant vector");1440}1441 1442// ConstantVector accessors.1443Constant *ConstantVector::get(ArrayRef<Constant*> V) {1444 if (Constant *C = getImpl(V))1445 return C;1446 auto *Ty = FixedVectorType::get(V.front()->getType(), V.size());1447 return Ty->getContext().pImpl->VectorConstants.getOrCreate(Ty, V);1448}1449 1450Constant *ConstantVector::getImpl(ArrayRef<Constant*> V) {1451 assert(!V.empty() && "Vectors can't be empty");1452 auto *T = FixedVectorType::get(V.front()->getType(), V.size());1453 1454 // If this is an all-undef or all-zero vector, return a1455 // ConstantAggregateZero or UndefValue.1456 Constant *C = V[0];1457 bool isZero = C->isNullValue();1458 bool isUndef = isa<UndefValue>(C);1459 bool isPoison = isa<PoisonValue>(C);1460 bool isSplatFP = UseConstantFPForFixedLengthSplat && isa<ConstantFP>(C);1461 bool isSplatInt = UseConstantIntForFixedLengthSplat && isa<ConstantInt>(C);1462 1463 if (isZero || isUndef || isSplatFP || isSplatInt) {1464 for (unsigned i = 1, e = V.size(); i != e; ++i)1465 if (V[i] != C) {1466 isZero = isUndef = isPoison = isSplatFP = isSplatInt = false;1467 break;1468 }1469 }1470 1471 if (isZero)1472 return ConstantAggregateZero::get(T);1473 if (isPoison)1474 return PoisonValue::get(T);1475 if (isUndef)1476 return UndefValue::get(T);1477 if (isSplatFP)1478 return ConstantFP::get(C->getContext(), T->getElementCount(),1479 cast<ConstantFP>(C)->getValue());1480 if (isSplatInt)1481 return ConstantInt::get(C->getContext(), T->getElementCount(),1482 cast<ConstantInt>(C)->getValue());1483 1484 // Check to see if all of the elements are ConstantFP or ConstantInt and if1485 // the element type is compatible with ConstantDataVector. If so, use it.1486 if (ConstantDataSequential::isElementTypeCompatible(C->getType()))1487 return getSequenceIfElementsMatch<ConstantDataVector>(C, V);1488 1489 // Otherwise, the element type isn't compatible with ConstantDataVector, or1490 // the operand list contains a ConstantExpr or something else strange.1491 return nullptr;1492}1493 1494Constant *ConstantVector::getSplat(ElementCount EC, Constant *V) {1495 if (!EC.isScalable()) {1496 // Maintain special handling of zero.1497 if (!V->isNullValue()) {1498 if (UseConstantIntForFixedLengthSplat && isa<ConstantInt>(V))1499 return ConstantInt::get(V->getContext(), EC,1500 cast<ConstantInt>(V)->getValue());1501 if (UseConstantFPForFixedLengthSplat && isa<ConstantFP>(V))1502 return ConstantFP::get(V->getContext(), EC,1503 cast<ConstantFP>(V)->getValue());1504 }1505 1506 // If this splat is compatible with ConstantDataVector, use it instead of1507 // ConstantVector.1508 if ((isa<ConstantFP>(V) || isa<ConstantInt>(V)) &&1509 ConstantDataSequential::isElementTypeCompatible(V->getType()))1510 return ConstantDataVector::getSplat(EC.getKnownMinValue(), V);1511 1512 SmallVector<Constant *, 32> Elts(EC.getKnownMinValue(), V);1513 return get(Elts);1514 }1515 1516 // Maintain special handling of zero.1517 if (!V->isNullValue()) {1518 if (UseConstantIntForScalableSplat && isa<ConstantInt>(V))1519 return ConstantInt::get(V->getContext(), EC,1520 cast<ConstantInt>(V)->getValue());1521 if (UseConstantFPForScalableSplat && isa<ConstantFP>(V))1522 return ConstantFP::get(V->getContext(), EC,1523 cast<ConstantFP>(V)->getValue());1524 }1525 1526 Type *VTy = VectorType::get(V->getType(), EC);1527 1528 if (V->isNullValue())1529 return ConstantAggregateZero::get(VTy);1530 if (isa<PoisonValue>(V))1531 return PoisonValue::get(VTy);1532 if (isa<UndefValue>(V))1533 return UndefValue::get(VTy);1534 1535 Type *IdxTy = Type::getInt64Ty(VTy->getContext());1536 1537 // Move scalar into vector.1538 Constant *PoisonV = PoisonValue::get(VTy);1539 V = ConstantExpr::getInsertElement(PoisonV, V, ConstantInt::get(IdxTy, 0));1540 // Build shuffle mask to perform the splat.1541 SmallVector<int, 8> Zeros(EC.getKnownMinValue(), 0);1542 // Splat.1543 return ConstantExpr::getShuffleVector(V, PoisonV, Zeros);1544}1545 1546ConstantTokenNone *ConstantTokenNone::get(LLVMContext &Context) {1547 LLVMContextImpl *pImpl = Context.pImpl;1548 if (!pImpl->TheNoneToken)1549 pImpl->TheNoneToken.reset(new ConstantTokenNone(Context));1550 return pImpl->TheNoneToken.get();1551}1552 1553/// Remove the constant from the constant table.1554void ConstantTokenNone::destroyConstantImpl() {1555 llvm_unreachable("You can't ConstantTokenNone->destroyConstantImpl()!");1556}1557 1558// Utility function for determining if a ConstantExpr is a CastOp or not. This1559// can't be inline because we don't want to #include Instruction.h into1560// Constant.h1561bool ConstantExpr::isCast() const { return Instruction::isCast(getOpcode()); }1562 1563ArrayRef<int> ConstantExpr::getShuffleMask() const {1564 return cast<ShuffleVectorConstantExpr>(this)->ShuffleMask;1565}1566 1567Constant *ConstantExpr::getShuffleMaskForBitcode() const {1568 return cast<ShuffleVectorConstantExpr>(this)->ShuffleMaskForBitcode;1569}1570 1571Constant *ConstantExpr::getWithOperands(ArrayRef<Constant *> Ops, Type *Ty,1572 bool OnlyIfReduced, Type *SrcTy) const {1573 assert(Ops.size() == getNumOperands() && "Operand count mismatch!");1574 1575 // If no operands changed return self.1576 if (Ty == getType() && std::equal(Ops.begin(), Ops.end(), op_begin()))1577 return const_cast<ConstantExpr*>(this);1578 1579 Type *OnlyIfReducedTy = OnlyIfReduced ? Ty : nullptr;1580 switch (getOpcode()) {1581 case Instruction::Trunc:1582 case Instruction::ZExt:1583 case Instruction::SExt:1584 case Instruction::FPTrunc:1585 case Instruction::FPExt:1586 case Instruction::UIToFP:1587 case Instruction::SIToFP:1588 case Instruction::FPToUI:1589 case Instruction::FPToSI:1590 case Instruction::PtrToAddr:1591 case Instruction::PtrToInt:1592 case Instruction::IntToPtr:1593 case Instruction::BitCast:1594 case Instruction::AddrSpaceCast:1595 return ConstantExpr::getCast(getOpcode(), Ops[0], Ty, OnlyIfReduced);1596 case Instruction::InsertElement:1597 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2],1598 OnlyIfReducedTy);1599 case Instruction::ExtractElement:1600 return ConstantExpr::getExtractElement(Ops[0], Ops[1], OnlyIfReducedTy);1601 case Instruction::ShuffleVector:1602 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], getShuffleMask(),1603 OnlyIfReducedTy);1604 case Instruction::GetElementPtr: {1605 auto *GEPO = cast<GEPOperator>(this);1606 assert(SrcTy || (Ops[0]->getType() == getOperand(0)->getType()));1607 return ConstantExpr::getGetElementPtr(1608 SrcTy ? SrcTy : GEPO->getSourceElementType(), Ops[0], Ops.slice(1),1609 GEPO->getNoWrapFlags(), GEPO->getInRange(), OnlyIfReducedTy);1610 }1611 default:1612 assert(getNumOperands() == 2 && "Must be binary operator?");1613 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1], SubclassOptionalData,1614 OnlyIfReducedTy);1615 }1616}1617 1618 1619//===----------------------------------------------------------------------===//1620// isValueValidForType implementations1621 1622bool ConstantInt::isValueValidForType(Type *Ty, uint64_t Val) {1623 unsigned NumBits = Ty->getIntegerBitWidth(); // assert okay1624 if (Ty->isIntegerTy(1))1625 return Val == 0 || Val == 1;1626 return isUIntN(NumBits, Val);1627}1628 1629bool ConstantInt::isValueValidForType(Type *Ty, int64_t Val) {1630 unsigned NumBits = Ty->getIntegerBitWidth();1631 if (Ty->isIntegerTy(1))1632 return Val == 0 || Val == 1 || Val == -1;1633 return isIntN(NumBits, Val);1634}1635 1636bool ConstantFP::isValueValidForType(Type *Ty, const APFloat& Val) {1637 // convert modifies in place, so make a copy.1638 APFloat Val2 = APFloat(Val);1639 bool losesInfo;1640 switch (Ty->getTypeID()) {1641 default:1642 return false; // These can't be represented as floating point!1643 1644 // FIXME rounding mode needs to be more flexible1645 case Type::HalfTyID: {1646 if (&Val2.getSemantics() == &APFloat::IEEEhalf())1647 return true;1648 Val2.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &losesInfo);1649 return !losesInfo;1650 }1651 case Type::BFloatTyID: {1652 if (&Val2.getSemantics() == &APFloat::BFloat())1653 return true;1654 Val2.convert(APFloat::BFloat(), APFloat::rmNearestTiesToEven, &losesInfo);1655 return !losesInfo;1656 }1657 case Type::FloatTyID: {1658 if (&Val2.getSemantics() == &APFloat::IEEEsingle())1659 return true;1660 Val2.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &losesInfo);1661 return !losesInfo;1662 }1663 case Type::DoubleTyID: {1664 if (&Val2.getSemantics() == &APFloat::IEEEhalf() ||1665 &Val2.getSemantics() == &APFloat::BFloat() ||1666 &Val2.getSemantics() == &APFloat::IEEEsingle() ||1667 &Val2.getSemantics() == &APFloat::IEEEdouble())1668 return true;1669 Val2.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &losesInfo);1670 return !losesInfo;1671 }1672 case Type::X86_FP80TyID:1673 return &Val2.getSemantics() == &APFloat::IEEEhalf() ||1674 &Val2.getSemantics() == &APFloat::BFloat() ||1675 &Val2.getSemantics() == &APFloat::IEEEsingle() ||1676 &Val2.getSemantics() == &APFloat::IEEEdouble() ||1677 &Val2.getSemantics() == &APFloat::x87DoubleExtended();1678 case Type::FP128TyID:1679 return &Val2.getSemantics() == &APFloat::IEEEhalf() ||1680 &Val2.getSemantics() == &APFloat::BFloat() ||1681 &Val2.getSemantics() == &APFloat::IEEEsingle() ||1682 &Val2.getSemantics() == &APFloat::IEEEdouble() ||1683 &Val2.getSemantics() == &APFloat::IEEEquad();1684 case Type::PPC_FP128TyID:1685 return &Val2.getSemantics() == &APFloat::IEEEhalf() ||1686 &Val2.getSemantics() == &APFloat::BFloat() ||1687 &Val2.getSemantics() == &APFloat::IEEEsingle() ||1688 &Val2.getSemantics() == &APFloat::IEEEdouble() ||1689 &Val2.getSemantics() == &APFloat::PPCDoubleDouble();1690 }1691}1692 1693 1694//===----------------------------------------------------------------------===//1695// Factory Function Implementation1696 1697ConstantAggregateZero *ConstantAggregateZero::get(Type *Ty) {1698 assert((Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy()) &&1699 "Cannot create an aggregate zero of non-aggregate type!");1700 1701 std::unique_ptr<ConstantAggregateZero> &Entry =1702 Ty->getContext().pImpl->CAZConstants[Ty];1703 if (!Entry)1704 Entry.reset(new ConstantAggregateZero(Ty));1705 1706 return Entry.get();1707}1708 1709/// Remove the constant from the constant table.1710void ConstantAggregateZero::destroyConstantImpl() {1711 getContext().pImpl->CAZConstants.erase(getType());1712}1713 1714/// Remove the constant from the constant table.1715void ConstantArray::destroyConstantImpl() {1716 getType()->getContext().pImpl->ArrayConstants.remove(this);1717}1718 1719 1720//---- ConstantStruct::get() implementation...1721//1722 1723/// Remove the constant from the constant table.1724void ConstantStruct::destroyConstantImpl() {1725 getType()->getContext().pImpl->StructConstants.remove(this);1726}1727 1728/// Remove the constant from the constant table.1729void ConstantVector::destroyConstantImpl() {1730 getType()->getContext().pImpl->VectorConstants.remove(this);1731}1732 1733Constant *Constant::getSplatValue(bool AllowPoison) const {1734 assert(this->getType()->isVectorTy() && "Only valid for vectors!");1735 if (isa<PoisonValue>(this))1736 return PoisonValue::get(cast<VectorType>(getType())->getElementType());1737 if (isa<ConstantAggregateZero>(this))1738 return getNullValue(cast<VectorType>(getType())->getElementType());1739 if (auto *CI = dyn_cast<ConstantInt>(this))1740 return ConstantInt::get(getContext(), CI->getValue());1741 if (auto *CFP = dyn_cast<ConstantFP>(this))1742 return ConstantFP::get(getContext(), CFP->getValue());1743 if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this))1744 return CV->getSplatValue();1745 if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))1746 return CV->getSplatValue(AllowPoison);1747 1748 // Check if this is a constant expression splat of the form returned by1749 // ConstantVector::getSplat()1750 const auto *Shuf = dyn_cast<ConstantExpr>(this);1751 if (Shuf && Shuf->getOpcode() == Instruction::ShuffleVector &&1752 isa<UndefValue>(Shuf->getOperand(1))) {1753 1754 const auto *IElt = dyn_cast<ConstantExpr>(Shuf->getOperand(0));1755 if (IElt && IElt->getOpcode() == Instruction::InsertElement &&1756 isa<UndefValue>(IElt->getOperand(0))) {1757 1758 ArrayRef<int> Mask = Shuf->getShuffleMask();1759 Constant *SplatVal = IElt->getOperand(1);1760 ConstantInt *Index = dyn_cast<ConstantInt>(IElt->getOperand(2));1761 1762 if (Index && Index->getValue() == 0 &&1763 llvm::all_of(Mask, [](int I) { return I == 0; }))1764 return SplatVal;1765 }1766 }1767 1768 return nullptr;1769}1770 1771Constant *ConstantVector::getSplatValue(bool AllowPoison) const {1772 // Check out first element.1773 Constant *Elt = getOperand(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);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>(OpC))1786 continue;1787 1788 // If we do not have a defined element yet, use the current operand.1789 if (isa<PoisonValue>(Elt))1790 Elt = OpC;1791 1792 if (OpC != Elt)1793 return nullptr;1794 }1795 return Elt;1796}1797 1798const APInt &Constant::getUniqueInteger() const {1799 if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))1800 return CI->getValue();1801 // Scalable vectors can use a ConstantExpr to build a splat.1802 if (isa<ConstantExpr>(this))1803 return cast<ConstantInt>(this->getSplatValue())->getValue();1804 // For non-ConstantExpr we use getAggregateElement as a fast path to avoid1805 // calling getSplatValue in release builds.1806 assert(this->getSplatValue() && "Doesn't contain a unique integer!");1807 const Constant *C = this->getAggregateElement(0U);1808 assert(C && isa<ConstantInt>(C) && "Not a vector of numbers!");1809 return cast<ConstantInt>(C)->getValue();1810}1811 1812ConstantRange Constant::toConstantRange() const {1813 if (auto *CI = dyn_cast<ConstantInt>(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 getSplatValue(/*AllowPoison=*/true)))1822 return ConstantRange(CI->getValue());1823 1824 if (auto *CDV = dyn_cast<ConstantDataVector>(this)) {1825 ConstantRange CR = ConstantRange::getEmpty(BitWidth);1826 for (unsigned I = 0, E = CDV->getNumElements(); I < E; ++I)1827 CR = CR.unionWith(CDV->getElementAsAPInt(I));1828 return CR;1829 }1830 1831 if (auto *CV = dyn_cast<ConstantVector>(this)) {1832 ConstantRange CR = ConstantRange::getEmpty(BitWidth);1833 for (unsigned I = 0, E = CV->getNumOperands(); I < E; ++I) {1834 Constant *Elem = CV->getOperand(I);1835 if (!Elem)1836 return ConstantRange::getFull(BitWidth);1837 if (isa<PoisonValue>(Elem))1838 continue;1839 auto *CI = dyn_cast<ConstantInt>(Elem);1840 if (!CI)1841 return ConstantRange::getFull(BitWidth);1842 CR = CR.unionWith(CI->getValue());1843 }1844 return CR;1845 }1846 1847 return ConstantRange::getFull(BitWidth);1848}1849 1850//---- ConstantPointerNull::get() implementation.1851//1852 1853ConstantPointerNull *ConstantPointerNull::get(PointerType *Ty) {1854 std::unique_ptr<ConstantPointerNull> &Entry =1855 Ty->getContext().pImpl->CPNConstants[Ty];1856 if (!Entry)1857 Entry.reset(new ConstantPointerNull(Ty));1858 1859 return Entry.get();1860}1861 1862/// Remove the constant from the constant table.1863void ConstantPointerNull::destroyConstantImpl() {1864 getContext().pImpl->CPNConstants.erase(getType());1865}1866 1867//---- ConstantTargetNone::get() implementation.1868//1869 1870ConstantTargetNone *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(new ConstantTargetNone(Ty));1877 1878 return Entry.get();1879}1880 1881/// Remove the constant from the constant table.1882void ConstantTargetNone::destroyConstantImpl() {1883 getContext().pImpl->CTNConstants.erase(getType());1884}1885 1886UndefValue *UndefValue::get(Type *Ty) {1887 std::unique_ptr<UndefValue> &Entry = Ty->getContext().pImpl->UVConstants[Ty];1888 if (!Entry)1889 Entry.reset(new UndefValue(Ty));1890 1891 return Entry.get();1892}1893 1894/// Remove the constant from the constant table.1895void UndefValue::destroyConstantImpl() {1896 // Free the constant and any dangling references to it.1897 if (getValueID() == UndefValueVal) {1898 getContext().pImpl->UVConstants.erase(getType());1899 } else if (getValueID() == PoisonValueVal) {1900 getContext().pImpl->PVConstants.erase(getType());1901 }1902 llvm_unreachable("Not a undef or a poison!");1903}1904 1905PoisonValue *PoisonValue::get(Type *Ty) {1906 std::unique_ptr<PoisonValue> &Entry = Ty->getContext().pImpl->PVConstants[Ty];1907 if (!Entry)1908 Entry.reset(new PoisonValue(Ty));1909 1910 return Entry.get();1911}1912 1913/// Remove the constant from the constant table.1914void PoisonValue::destroyConstantImpl() {1915 // Free the constant and any dangling references to it.1916 getContext().pImpl->PVConstants.erase(getType());1917}1918 1919BlockAddress *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 1926BlockAddress *BlockAddress::get(BasicBlock *BB) {1927 assert(BB->getParent() && "Block must have a parent");1928 return get(BB->getParent()->getType(), BB);1929}1930 1931BlockAddress *BlockAddress::get(Function *F, BasicBlock *BB) {1932 assert(BB->getParent() == F && "Block not part of specified function");1933 return get(BB->getParent()->getType(), BB);1934}1935 1936BlockAddress::BlockAddress(Type *Ty, BasicBlock *BB)1937 : Constant(Ty, Value::BlockAddressVal, AllocMarker) {1938 setOperand(0, BB);1939 BB->setHasAddressTaken(true);1940}1941 1942BlockAddress *BlockAddress::lookup(const BasicBlock *BB) {1943 if (!BB->hasAddressTaken())1944 return nullptr;1945 1946 BlockAddress *BA = BB->getContext().pImpl->BlockAddresses.lookup(BB);1947 assert(BA && "Refcount and block address map disagree!");1948 return BA;1949}1950 1951/// Remove the constant from the constant table.1952void BlockAddress::destroyConstantImpl() {1953 getType()->getContext().pImpl->BlockAddresses.erase(getBasicBlock());1954 getBasicBlock()->setHasAddressTaken(false);1955}1956 1957Value *BlockAddress::handleOperandChangeImpl(Value *From, Value *To) {1958 assert(From == getBasicBlock());1959 BasicBlock *NewBB = cast<BasicBlock>(To);1960 1961 // See if the 'new' entry already exists, if not, just update this in place1962 // 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 a1970 // tombstone will get added).1971 getContext().pImpl->BlockAddresses.erase(getBasicBlock());1972 NewBA = this;1973 setOperand(0, 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 1981DSOLocalEquivalent *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 1991DSOLocalEquivalent::DSOLocalEquivalent(GlobalValue *GV)1992 : Constant(GV->getType(), Value::DSOLocalEquivalentVal, AllocMarker) {1993 setOperand(0, GV);1994}1995 1996/// Remove the constant from the constant table.1997void DSOLocalEquivalent::destroyConstantImpl() {1998 const GlobalValue *GV = getGlobalValue();1999 GV->getContext().pImpl->DSOLocalEquivalents.erase(GV);2000}2001 2002Value *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>(To)) {2008 DSOLocalEquivalent *&NewEquiv =2009 getContext().pImpl->DSOLocalEquivalents[ToObj];2010 if (NewEquiv)2011 return llvm::ConstantExpr::getBitCast(NewEquiv, getType());2012 }2013 2014 // If the argument is replaced with a null value, just replace this constant2015 // with a null value.2016 if (cast<Constant>(To)->isNullValue())2017 return To;2018 2019 // The replacement could be a bitcast or an alias to another function. We can2020 // replace it with a bitcast to the dso_local_equivalent of that function.2021 auto *Func = cast<Function>(To->stripPointerCastsAndAliases());2022 DSOLocalEquivalent *&NewEquiv = getContext().pImpl->DSOLocalEquivalents[Func];2023 if (NewEquiv)2024 return llvm::ConstantExpr::getBitCast(NewEquiv, getType());2025 2026 // Replace this with the new one.2027 getContext().pImpl->DSOLocalEquivalents.erase(getGlobalValue());2028 NewEquiv = this;2029 setOperand(0, Func);2030 2031 if (Func->getType() != getType()) {2032 // It is ok to mutate the type here because this constant should always2033 // reflect the type of the function it's holding.2034 mutateType(Func->getType());2035 }2036 return nullptr;2037}2038 2039NoCFIValue *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 2049NoCFIValue::NoCFIValue(GlobalValue *GV)2050 : Constant(GV->getType(), Value::NoCFIValueVal, AllocMarker) {2051 setOperand(0, GV);2052}2053 2054/// Remove the constant from the constant table.2055void NoCFIValue::destroyConstantImpl() {2056 const GlobalValue *GV = getGlobalValue();2057 GV->getContext().pImpl->NoCFIValues.erase(GV);2058}2059 2060Value *NoCFIValue::handleOperandChangeImpl(Value *From, Value *To) {2061 assert(From == getGlobalValue() && "Changing value does not match operand.");2062 2063 GlobalValue *GV = dyn_cast<GlobalValue>(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(NewNC, getType());2069 2070 getContext().pImpl->NoCFIValues.erase(getGlobalValue());2071 NewNC = this;2072 setOperand(0, GV);2073 2074 if (GV->getType() != getType())2075 mutateType(GV->getType());2076 2077 return nullptr;2078}2079 2080//---- ConstantPtrAuth::get() implementations.2081//2082 2083ConstantPtrAuth *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(Ptr->getType(), MapKey);2090}2091 2092ConstantPtrAuth *ConstantPtrAuth::getWithSameSchema(Constant *Pointer) const {2093 return get(Pointer, getKey(), getDiscriminator(), getAddrDiscriminator(),2094 getDeactivationSymbol());2095}2096 2097ConstantPtrAuth::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(0, Ptr);2107 setOperand(1, Key);2108 setOperand(2, Disc);2109 setOperand(3, AddrDisc);2110 setOperand(4, DeactivationSymbol);2111}2112 2113/// Remove the constant from the constant table.2114void ConstantPtrAuth::destroyConstantImpl() {2115 getType()->getContext().pImpl->ConstantPtrAuths.remove(this);2116}2117 2118Value *ConstantPtrAuth::handleOperandChangeImpl(Value *From, Value *ToV) {2119 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");2120 Constant *To = cast<Constant>(ToV);2121 2122 SmallVector<Constant *, 4> Values;2123 Values.reserve(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>(O->get());2131 if (Val == From) {2132 OperandNo = (O - OperandList);2133 Val = To;2134 ++NumUpdated;2135 }2136 Values.push_back(Val);2137 }2138 2139 return getContext().pImpl->ConstantPtrAuths.replaceOperandsInPlace(2140 Values, this, From, To, NumUpdated, OperandNo);2141}2142 2143bool ConstantPtrAuth::hasSpecialAddressDiscriminator(uint64_t Value) const {2144 const auto *CastV = dyn_cast<ConstantExpr>(getAddrDiscriminator());2145 if (!CastV || CastV->getOpcode() != Instruction::IntToPtr)2146 return false;2147 2148 const auto *IntVal = dyn_cast<ConstantInt>(CastV->getOperand(0));2149 if (!IntVal)2150 return false;2151 2152 return IntVal->getValue() == Value;2153}2154 2155bool 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 operation2159 // 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 simple2174 // 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 blend2184 // intrinsic in the provided full discriminator.2185 if (!match(Discriminator,2186 m_Intrinsic<Intrinsic::ptrauth_blend>(2187 m_Value(AddrDiscriminator), m_Specific(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>(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(getAddrDiscriminator()->getType()), 0);2210 auto *Base1 = getAddrDiscriminator()->stripAndAccumulateConstantOffsets(2211 DL, Off1, /*AllowNonInbounds=*/true);2212 2213 APInt Off2(DL.getIndexTypeSizeInBits(AddrDiscriminator->getType()), 0);2214 auto *Base2 = AddrDiscriminator->stripAndAccumulateConstantOffsets(2215 DL, 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 the2224/// cast in the ExprConstants map. It is used by the various get* methods below.2225static 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 cases2229 if (Constant *FC = ConstantFoldCastInstruction(opc, C, 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, Key);2241}2242 2243Constant *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 2270Constant *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 2276Constant *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(S, Ty);2283 2284 unsigned SrcAS = S->getType()->getPointerAddressSpace();2285 if (Ty->isPtrOrPtrVectorTy() && SrcAS != Ty->getPointerAddressSpace())2286 return getAddrSpaceCast(S, Ty);2287 2288 return getBitCast(S, Ty);2289}2290 2291Constant *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(S, Ty);2298 2299 return getBitCast(S, Ty);2300}2301 2302Constant *ConstantExpr::getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) {2303#ifndef NDEBUG2304 bool fromVec = isa<VectorType>(C->getType());2305 bool toVec = isa<VectorType>(Ty);2306#endif2307 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(Instruction::Trunc, C, Ty, OnlyIfReduced);2314}2315 2316Constant *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>(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(Instruction::PtrToAddr, C, DstTy, OnlyIfReduced);2328}2329 2330Constant *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>(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(Instruction::PtrToInt, C, DstTy, OnlyIfReduced);2342}2343 2344Constant *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>(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(Instruction::IntToPtr, C, DstTy, OnlyIfReduced);2356}2357 2358Constant *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 this2364 // speedily.2365 if (C->getType() == DstTy) return C;2366 2367 return getFoldedCast(Instruction::BitCast, C, DstTy, OnlyIfReduced);2368}2369 2370Constant *ConstantExpr::getAddrSpaceCast(Constant *C, Type *DstTy,2371 bool OnlyIfReduced) {2372 assert(CastInst::castIsValid(Instruction::AddrSpaceCast, C, DstTy) &&2373 "Invalid constantexpr addrspacecast!");2374 return getFoldedCast(Instruction::AddrSpaceCast, C, DstTy, OnlyIfReduced);2375}2376 2377Constant *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 NDEBUG2388 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#endif2405 2406 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, 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(C1->getType(), Key);2417}2418 2419bool 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 2446bool 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 2473bool 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 2496bool 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 2519Constant *ConstantExpr::getSizeOf(Type* Ty) {2520 // sizeof is implemented as: (i64) gep (Ty*)null, 12521 // Note that a non-inbounds gep is used, as null isn't within any object.2522 Constant *GEPIdx = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);2523 Constant *GEP = getGetElementPtr(2524 Ty, Constant::getNullValue(PointerType::getUnqual(Ty->getContext())),2525 GEPIdx);2526 return getPtrToInt(GEP,2527 Type::getInt64Ty(Ty->getContext()));2528}2529 2530Constant *ConstantExpr::getAlignOf(Type* Ty) {2531 // alignof is implemented as: (i64) gep ({i1,Ty}*)null, 0, 12532 // Note that a non-inbounds gep is used, as null isn't within any object.2533 Type *AligningTy = StructType::get(Type::getInt1Ty(Ty->getContext()), Ty);2534 Constant *NullPtr =2535 Constant::getNullValue(PointerType::getUnqual(AligningTy->getContext()));2536 Constant *Zero = ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0);2537 Constant *One = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);2538 Constant *Indices[2] = {Zero, One};2539 Constant *GEP = getGetElementPtr(AligningTy, NullPtr, Indices);2540 return getPtrToInt(GEP, Type::getInt64Ty(Ty->getContext()));2541}2542 2543Constant *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(C, Idxs);2559 if (OnlyIfReducedTy == ReqTy)2560 return nullptr;2561 2562 auto EltCount = ElementCount::getFixed(0);2563 if (VectorType *VecTy = dyn_cast<VectorType>(ReqTy))2564 EltCount = VecTy->getElementCount();2565 2566 // Look up the constant in the table first to ensure uniqueness2567 std::vector<Constant*> ArgVec;2568 ArgVec.reserve(1 + Idxs.size());2569 ArgVec.push_back(C);2570 auto GTI = gep_type_begin(Ty, Idxs), GTE = gep_type_end(Ty, Idxs);2571 for (; GTI != GTE; ++GTI) {2572 auto *Idx = cast<Constant>(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(EltCount, Idx);2583 }2584 ArgVec.push_back(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(ReqTy, Key);2592}2593 2594Constant *ConstantExpr::getExtractElement(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->getType())->getElementType();2605 if (OnlyIfReducedTy == ReqTy)2606 return nullptr;2607 2608 // Look up the constant in the table first to ensure uniqueness2609 Constant *ArgVec[] = { Val, Idx };2610 const ConstantExprKeyType Key(Instruction::ExtractElement, ArgVec);2611 2612 LLVMContextImpl *pImpl = Val->getContext().pImpl;2613 return pImpl->ExprConstants.getOrCreate(ReqTy, Key);2614}2615 2616Constant *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 uniqueness2632 Constant *ArgVec[] = { Val, Elt, Idx };2633 const ConstantExprKeyType Key(Instruction::InsertElement, ArgVec);2634 2635 LLVMContextImpl *pImpl = Val->getContext().pImpl;2636 return pImpl->ExprConstants.getOrCreate(Val->getType(), Key);2637}2638 2639Constant *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>(V1->getType());2650 Type *EltTy = V1VTy->getElementType();2651 bool TypeIsScalable = isa<ScalableVectorType>(V1VTy);2652 Type *ShufTy = VectorType::get(EltTy, NElts, TypeIsScalable);2653 2654 if (OnlyIfReducedTy == ShufTy)2655 return nullptr;2656 2657 // Look up the constant in the table first to ensure uniqueness2658 Constant *ArgVec[] = {V1, V2};2659 ConstantExprKeyType Key(Instruction::ShuffleVector, ArgVec, 0, Mask);2660 2661 LLVMContextImpl *pImpl = ShufTy->getContext().pImpl;2662 return pImpl->ExprConstants.getOrCreate(ShufTy, Key);2663}2664 2665Constant *ConstantExpr::getNeg(Constant *C, bool HasNSW) {2666 assert(C->getType()->isIntOrIntVectorTy() &&2667 "Cannot NEG a nonintegral value!");2668 return getSub(ConstantInt::get(C->getType(), 0), C, /*HasNUW=*/false, HasNSW);2669}2670 2671Constant *ConstantExpr::getNot(Constant *C) {2672 assert(C->getType()->isIntOrIntVectorTy() &&2673 "Cannot NOT a nonintegral value!");2674 return get(Instruction::Xor, C, Constant::getAllOnesValue(C->getType()));2675}2676 2677Constant *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(Instruction::Add, C1, C2, Flags);2682}2683 2684Constant *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(Instruction::Sub, C1, C2, Flags);2689}2690 2691Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {2692 return get(Instruction::Xor, C1, C2);2693}2694 2695Constant *ConstantExpr::getExactLogBase2(Constant *C) {2696 Type *Ty = C->getType();2697 const APInt *IVal;2698 if (match(C, m_APInt(IVal)) && IVal->isPowerOf2())2699 return ConstantInt::get(Ty, IVal->logBase2());2700 2701 // FIXME: We can extract pow of 2 of splat constant for scalable vectors.2702 auto *VecTy = dyn_cast<FixedVectorType>(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(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>(Elt)) {2713 Elts.push_back(Constant::getNullValue(Ty->getScalarType()));2714 continue;2715 }2716 if (!match(Elt, m_APInt(IVal)) || !IVal->isPowerOf2())2717 return nullptr;2718 Elts.push_back(ConstantInt::get(Ty->getScalarType(), IVal->logBase2()));2719 }2720 2721 return ConstantVector::get(Elts);2722}2723 2724Constant *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 = X2732 case Instruction::Or: // X | 0 = X2733 case Instruction::Xor: // X ^ 0 = X2734 return Constant::getNullValue(Ty);2735 case Instruction::Mul: // X * 1 = X2736 return ConstantInt::get(Ty, 1);2737 case Instruction::And: // X & -1 = X2738 return Constant::getAllOnesValue(Ty);2739 case Instruction::FAdd: // X + -0.0 = X2740 return ConstantFP::getZero(Ty, !NSZ);2741 case Instruction::FMul: // X * 1.0 = X2742 return ConstantFP::get(Ty, 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 = X2754 case Instruction::Shl: // X << 0 = X2755 case Instruction::LShr: // X >>u 0 = X2756 case Instruction::AShr: // X >> 0 = X2757 case Instruction::FSub: // X - 0.0 = X2758 return Constant::getNullValue(Ty);2759 case Instruction::SDiv: // X / 1 = X2760 case Instruction::UDiv: // X /u 1 = X2761 return ConstantInt::get(Ty, 1);2762 case Instruction::FDiv: // X / 1.0 = X2763 return ConstantFP::get(Ty, 1.0);2764 default:2765 return nullptr;2766 }2767}2768 2769Constant *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, APInt::getSignedMinValue(Ty->getIntegerBitWidth()));2778 case Intrinsic::smin:2779 return Constant::getIntegerValue(2780 Ty, APInt::getSignedMaxValue(Ty->getIntegerBitWidth()));2781 default:2782 return nullptr;2783 }2784}2785 2786Constant *ConstantExpr::getIdentity(Instruction *I, Type *Ty,2787 bool AllowRHSConstant, bool NSZ) {2788 if (I->isBinaryOp())2789 return getBinOpIdentity(I->getOpcode(), Ty, AllowRHSConstant, NSZ);2790 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))2791 return getIntrinsicIdentity(II->getIntrinsicID(), Ty);2792 return nullptr;2793}2794 2795Constant *ConstantExpr::getBinOpAbsorber(unsigned Opcode, Type *Ty,2796 bool AllowLHSConstant) {2797 switch (Opcode) {2798 default:2799 break;2800 2801 case Instruction::Or: // -1 | X = -12802 return Constant::getAllOnesValue(Ty);2803 2804 case Instruction::And: // 0 & X = 02805 case Instruction::Mul: // 0 * X = 02806 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 = 02817 case Instruction::LShr: // 0 >>l X = 02818 case Instruction::AShr: // 0 >>a X = 02819 case Instruction::SDiv: // 0 /s X = 02820 case Instruction::UDiv: // 0 /u X = 02821 case Instruction::URem: // 0 %u X = 02822 case Instruction::SRem: // 0 %s X = 02823 return Constant::getNullValue(Ty);2824 }2825}2826 2827/// Remove the constant from the constant table.2828void ConstantExpr::destroyConstantImpl() {2829 getType()->getContext().pImpl->ExprConstants.remove(this);2830}2831 2832const char *ConstantExpr::getOpcodeName() const {2833 return Instruction::getOpcodeName(getOpcode());2834}2835 2836GetElementPtrConstantExpr::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(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 2849Type *GetElementPtrConstantExpr::getSourceElementType() const {2850 return SrcElementTy;2851}2852 2853Type *GetElementPtrConstantExpr::getResultElementType() const {2854 return ResElementTy;2855}2856 2857std::optional<ConstantRange> GetElementPtrConstantExpr::getInRange() const {2858 return InRange;2859}2860 2861//===----------------------------------------------------------------------===//2862// ConstantData* implementations2863 2864Type *ConstantDataSequential::getElementType() const {2865 if (ArrayType *ATy = dyn_cast<ArrayType>(getType()))2866 return ATy->getElementType();2867 return cast<VectorType>(getType())->getElementType();2868}2869 2870StringRef ConstantDataSequential::getRawDataValues() const {2871 return StringRef(DataElements, getNumElements()*getElementByteSize());2872}2873 2874bool ConstantDataSequential::isElementTypeCompatible(Type *Ty) {2875 if (Ty->isHalfTy() || Ty->isBFloatTy() || Ty->isFloatTy() || Ty->isDoubleTy())2876 return true;2877 if (auto *IT = dyn_cast<IntegerType>(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 2890uint64_t ConstantDataSequential::getNumElements() const {2891 if (ArrayType *AT = dyn_cast<ArrayType>(getType()))2892 return AT->getNumElements();2893 return cast<FixedVectorType>(getType())->getNumElements();2894}2895 2896uint64_t ConstantDataSequential::getElementByteSize() const {2897 return getElementType()->getPrimitiveSizeInBits().getFixedValue() / 8;2898}2899 2900/// Return the start of the specified element.2901const 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.2907static 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 the2915/// ConstantDataSequential::get methods. They all thunk down to here, providing2916/// the correct element type. We take the bytes in as a StringRef because2917/// we *want* an underlying "char*" to avoid TBAA type punning violations.2918Constant *ConstantDataSequential::getImpl(StringRef Elements, Type *Ty) {2919#ifndef NDEBUG2920 if (ArrayType *ATy = dyn_cast<ArrayType>(Ty))2921 assert(isElementTypeCompatible(ATy->getElementType()));2922 else2923 assert(isElementTypeCompatible(cast<VectorType>(Ty)->getElementType()));2924#endif2925 // If the elements are all zero or there are no elements, return a CAZ, which2926 // is more dense and canonical.2927 if (isAllZeros(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(Elements).first;2933 2934 // The bucket can point to a linked list of different CDS's that have the same2935 // body but different types. For example, 0,0,0,1 could be a 4 element array2936 // of i8, or a 1-element array of i32. They'll both end up in the same2937 /// 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>(Ty)) {2946 // Use reset because std::make_unique can't access the constructor.2947 Entry->reset(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(new ConstantDataVector(Ty, Slot.first().data()));2954 return Entry->get();2955}2956 2957void 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(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 this2971 // entry, and removing the entry should remove the bucket completely.2972 assert(Entry->get() == this && "Hash mismatch in ConstantDataSequential");2973 getContext().pImpl->CDSConstants.erase(Slot);2974 return;2975 }2976 2977 // Otherwise, there are multiple entries linked off the bucket, unlink the2978 // 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 float2993/// element type taken from argument `ElementType', and count taken from2994/// argument `Elts'. The amount of bits of the contained type must match the2995/// 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) Note2997/// that this can return a ConstantAggregateZero object.2998Constant *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, Elts.size());3002 const char *Data = reinterpret_cast<const char *>(Elts.data());3003 return getImpl(StringRef(Data, Elts.size() * 2), Ty);3004}3005Constant *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, Elts.size());3008 const char *Data = reinterpret_cast<const char *>(Elts.data());3009 return getImpl(StringRef(Data, Elts.size() * 4), Ty);3010}3011Constant *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, Elts.size());3015 const char *Data = reinterpret_cast<const char *>(Elts.data());3016 return getImpl(StringRef(Data, Elts.size() * 8), Ty);3017}3018 3019Constant *ConstantDataArray::getString(LLVMContext &Context,3020 StringRef Str, bool AddNull) {3021 if (!AddNull) {3022 const uint8_t *Data = Str.bytes_begin();3023 return get(Context, ArrayRef(Data, Str.size()));3024 }3025 3026 SmallVector<uint8_t, 64> ElementVals;3027 ElementVals.append(Str.begin(), Str.end());3028 ElementVals.push_back(0);3029 return get(Context, ElementVals);3030}3031 3032/// get() constructors - Return a constant with vector type with an element3033/// count and element type matching the ArrayRef passed in. Note that this3034/// can return a ConstantAggregateZero object.3035Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint8_t> Elts){3036 auto *Ty = FixedVectorType::get(Type::getInt8Ty(Context), Elts.size());3037 const char *Data = reinterpret_cast<const char *>(Elts.data());3038 return getImpl(StringRef(Data, Elts.size() * 1), Ty);3039}3040Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint16_t> Elts){3041 auto *Ty = FixedVectorType::get(Type::getInt16Ty(Context), Elts.size());3042 const char *Data = reinterpret_cast<const char *>(Elts.data());3043 return getImpl(StringRef(Data, Elts.size() * 2), Ty);3044}3045Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint32_t> Elts){3046 auto *Ty = FixedVectorType::get(Type::getInt32Ty(Context), Elts.size());3047 const char *Data = reinterpret_cast<const char *>(Elts.data());3048 return getImpl(StringRef(Data, Elts.size() * 4), Ty);3049}3050Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<uint64_t> Elts){3051 auto *Ty = FixedVectorType::get(Type::getInt64Ty(Context), Elts.size());3052 const char *Data = reinterpret_cast<const char *>(Elts.data());3053 return getImpl(StringRef(Data, Elts.size() * 8), Ty);3054}3055Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<float> Elts) {3056 auto *Ty = FixedVectorType::get(Type::getFloatTy(Context), Elts.size());3057 const char *Data = reinterpret_cast<const char *>(Elts.data());3058 return getImpl(StringRef(Data, Elts.size() * 4), Ty);3059}3060Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef<double> Elts) {3061 auto *Ty = FixedVectorType::get(Type::getDoubleTy(Context), Elts.size());3062 const char *Data = reinterpret_cast<const char *>(Elts.data());3063 return getImpl(StringRef(Data, Elts.size() * 8), Ty);3064}3065 3066/// getFP() constructors - Return a constant of vector type with a float3067/// element type taken from argument `ElementType', and count taken from3068/// argument `Elts'. The amount of bits of the contained type must match the3069/// 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) Note3071/// that this can return a ConstantAggregateZero object.3072Constant *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, Elts.size());3077 const char *Data = reinterpret_cast<const char *>(Elts.data());3078 return getImpl(StringRef(Data, Elts.size() * 2), Ty);3079}3080Constant *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, Elts.size());3084 const char *Data = reinterpret_cast<const char *>(Elts.data());3085 return getImpl(StringRef(Data, Elts.size() * 4), Ty);3086}3087Constant *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, Elts.size());3092 const char *Data = reinterpret_cast<const char *>(Elts.data());3093 return getImpl(StringRef(Data, Elts.size() * 8), Ty);3094}3095 3096Constant *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>(V)) {3100 if (CI->getType()->isIntegerTy(8)) {3101 SmallVector<uint8_t, 16> Elts(NumElts, CI->getZExtValue());3102 return get(V->getContext(), Elts);3103 }3104 if (CI->getType()->isIntegerTy(16)) {3105 SmallVector<uint16_t, 16> Elts(NumElts, CI->getZExtValue());3106 return get(V->getContext(), Elts);3107 }3108 if (CI->getType()->isIntegerTy(32)) {3109 SmallVector<uint32_t, 16> Elts(NumElts, CI->getZExtValue());3110 return get(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(V->getContext(), Elts);3115 }3116 3117 if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {3118 if (CFP->getType()->isHalfTy()) {3119 SmallVector<uint16_t, 16> Elts(3120 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());3121 return getFP(V->getType(), Elts);3122 }3123 if (CFP->getType()->isBFloatTy()) {3124 SmallVector<uint16_t, 16> Elts(3125 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());3126 return getFP(V->getType(), Elts);3127 }3128 if (CFP->getType()->isFloatTy()) {3129 SmallVector<uint32_t, 16> Elts(3130 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());3131 return getFP(V->getType(), Elts);3132 }3133 if (CFP->getType()->isDoubleTy()) {3134 SmallVector<uint64_t, 16> Elts(3135 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());3136 return getFP(V->getType(), Elts);3137 }3138 }3139 return ConstantVector::getSplat(ElementCount::getFixed(NumElts), V);3140}3141 3142uint64_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 right3148 // 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 3162APInt 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 right3168 // 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 3190APFloat 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 3215float 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 3221double 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 3227Constant *ConstantDataSequential::getElementAsConstant(uint64_t Elt) const {3228 if (getElementType()->isHalfTy() || getElementType()->isBFloatTy() ||3229 getElementType()->isFloatTy() || getElementType()->isDoubleTy())3230 return ConstantFP::get(getContext(), getElementAsAPFloat(Elt));3231 3232 return ConstantInt::get(getElementType(), getElementAsInteger(Elt));3233}3234 3235bool ConstantDataSequential::isString(unsigned CharSize) const {3236 return isa<ArrayType>(getType()) && getElementType()->isIntegerTy(CharSize);3237}3238 3239bool 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(0);3250}3251 3252bool 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(Base, Base+i*EltSize, EltSize))3259 return false;3260 3261 return true;3262}3263 3264bool ConstantDataVector::isSplat() const {3265 if (!IsSplatSet) {3266 IsSplatSet = true;3267 IsSplat = isSplatData();3268 }3269 return IsSplat;3270}3271 3272Constant *ConstantDataVector::getSplatValue() const {3273 // If they're all the same, return the 0th one as a representative.3274 return isSplat() ? getElementAsConstant(0) : nullptr;3275}3276 3277//===----------------------------------------------------------------------===//3278// handleOperandChange implementations3279 3280/// Update this constant array to change uses of3281/// 'From' to be uses of 'To'. This must update the uniquing data structures3282/// etc.3283///3284/// Note that we intentionally replace all uses of From with To here. Consider3285/// a large array that uses 'From' 1000 times. By handling this case all here,3286/// ConstantArray::handleOperandChange is only invoked once, and that3287/// single invocation handles all 1000 uses. Handling them one at a time would3288/// work, but would be really slow because it would have to unique each updated3289/// array instance.3290///3291void 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 handled3304 // 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(Replacement);3313 3314 // Delete the old constant!3315 destroyConstant();3316}3317 3318Value *ConstantArray::handleOperandChangeImpl(Value *From, Value *To) {3319 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");3320 Constant *ToC = cast<Constant>(To);3321 3322 SmallVector<Constant*, 8> Values;3323 Values.reserve(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>(O->get());3335 if (Val == From) {3336 OperandNo = (O - OperandList);3337 Val = ToC;3338 ++NumUpdated;3339 }3340 Values.push_back(Val);3341 AllSame &= Val == ToC;3342 }3343 3344 if (AllSame && ToC->isNullValue())3345 return ConstantAggregateZero::get(getType());3346 3347 if (AllSame && isa<UndefValue>(ToC))3348 return UndefValue::get(getType());3349 3350 // Check for any other type of constant-folding.3351 if (Constant *C = getImpl(getType(), Values))3352 return C;3353 3354 // Update to the new value.3355 return getContext().pImpl->ArrayConstants.replaceOperandsInPlace(3356 Values, this, From, ToC, NumUpdated, OperandNo);3357}3358 3359Value *ConstantStruct::handleOperandChangeImpl(Value *From, Value *To) {3360 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");3361 Constant *ToC = cast<Constant>(To);3362 3363 Use *OperandList = getOperandList();3364 3365 SmallVector<Constant*, 8> Values;3366 Values.reserve(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>(O->get());3375 if (Val == From) {3376 OperandNo = (O - OperandList);3377 Val = ToC;3378 ++NumUpdated;3379 }3380 Values.push_back(Val);3381 AllSame &= Val == ToC;3382 }3383 3384 if (AllSame && ToC->isNullValue())3385 return ConstantAggregateZero::get(getType());3386 3387 if (AllSame && isa<UndefValue>(ToC))3388 return UndefValue::get(getType());3389 3390 // Update to the new value.3391 return getContext().pImpl->StructConstants.replaceOperandsInPlace(3392 Values, this, From, ToC, NumUpdated, OperandNo);3393}3394 3395Value *ConstantVector::handleOperandChangeImpl(Value *From, Value *To) {3396 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");3397 Constant *ToC = cast<Constant>(To);3398 3399 SmallVector<Constant*, 8> Values;3400 Values.reserve(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);3405 if (Val == From) {3406 OperandNo = i;3407 ++NumUpdated;3408 Val = ToC;3409 }3410 Values.push_back(Val);3411 }3412 3413 if (Constant *C = getImpl(Values))3414 return C;3415 3416 // Update to the new value.3417 return getContext().pImpl->VectorConstants.replaceOperandsInPlace(3418 Values, this, From, ToC, NumUpdated, OperandNo);3419}3420 3421Value *ConstantExpr::handleOperandChangeImpl(Value *From, Value *ToV) {3422 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");3423 Constant *To = cast<Constant>(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);3430 if (Op == From) {3431 OperandNo = i;3432 ++NumUpdated;3433 Op = To;3434 }3435 NewOps.push_back(Op);3436 }3437 assert(NumUpdated && "I didn't contain From!");3438 3439 if (Constant *C = getWithOperands(NewOps, getType(), true))3440 return C;3441 3442 // Update to the new value.3443 return getContext().pImpl->ExprConstants.replaceOperandsInPlace(3444 NewOps, this, From, To, NumUpdated, OperandNo);3445}3446 3447Instruction *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(), Ops[0],3459 getType(), "");3460 case Instruction::InsertElement:3461 return InsertElementInst::Create(Ops[0], Ops[1], Ops[2], "");3462 case Instruction::ExtractElement:3463 return ExtractElementInst::Create(Ops[0], Ops[1], "");3464 case Instruction::ShuffleVector:3465 return new ShuffleVectorInst(Ops[0], Ops[1], getShuffleMask(), "");3466 3467 case Instruction::GetElementPtr: {3468 const auto *GO = cast<GEPOperator>(this);3469 return GetElementPtrInst::Create(GO->getSourceElementType(), Ops[0],3470 Ops.slice(1), GO->getNoWrapFlags(), "");3471 }3472 default:3473 assert(getNumOperands() == 2 && "Must be binary operator?");3474 BinaryOperator *BO = BinaryOperator::Create(3475 (Instruction::BinaryOps)getOpcode(), Ops[0], Ops[1], "");3476 if (isa<OverflowingBinaryOperator>(BO)) {3477 BO->setHasNoUnsignedWrap(SubclassOptionalData &3478 OverflowingBinaryOperator::NoUnsignedWrap);3479 BO->setHasNoSignedWrap(SubclassOptionalData &3480 OverflowingBinaryOperator::NoSignedWrap);3481 }3482 if (isa<PossiblyExactOperator>(BO))3483 BO->setIsExact(SubclassOptionalData & PossiblyExactOperator::IsExact);3484 return BO;3485 }3486}3487