1//===- ConstantFold.cpp - LLVM constant folder ----------------------------===// 3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4// See https://llvm.org/LICENSE.txt for license information. 5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7//===----------------------------------------------------------------------===// 9// This file implements folding of constants for LLVM. This implements the 10// (internal) ConstantFold.h interface, which is used by the 11// ConstantExpr::get* methods to automatically fold constants when possible. 13// The current constant folding implementation is implemented in two pieces: the 14// pieces that don't need DataLayout, and the pieces that do. This is to avoid 15// a dependence in IR on Target. 17//===----------------------------------------------------------------------===// 35//===----------------------------------------------------------------------===// 36// ConstantFold*Instruction Implementations 37//===----------------------------------------------------------------------===// 39/// This function determines which opcode to use to fold two constant cast 40/// expressions together. It uses CastInst::isEliminableCastPair to determine 41/// the opcode. Consequently its just a wrapper around that function. 42/// Determine if it is valid to fold a cast of a cast 45unsigned opc,
///< opcode of the second cast constant expression 47Type *DstTy
///< destination type of the first cast 49assert(
Op &&
Op->isCast() &&
"Can't fold cast of cast without a cast!");
51assert(CastInst::isCast(opc) &&
"Invalid cast opcode");
53// The types and opcodes for the two Cast constant expressions 54Type *SrcTy =
Op->getOperand(0)->getType();
59// Assume that pointers are never more than 64 bits wide, and only use this 60// for the middle type. Otherwise we could end up folding away illegal 61// bitcasts between address spaces with different sizes. 64// Let CastInst::isEliminableCastPair do the heavy lifting. 66nullptr, FakeIntPtrTy,
nullptr);
70Type *SrcTy = V->getType();
74if (V->isAllOnesValue())
77// Handle ConstantInt -> ConstantFP 79// Canonicalize scalar-to-vector bitcasts into vector-to-vector bitcasts 80// This allows for other simplifications (although some of them 81// can only be handled by Analysis/ConstantFolding.cpp). 82if (isa<VectorType>(DestTy) && !isa<VectorType>(SrcTy))
85// Make sure dest type is compatible with the folded fp constant. 86// See note below regarding the PPC_FP128 restriction. 91return ConstantFP::get(
96// Handle ConstantFP -> ConstantInt 98// Canonicalize scalar-to-vector bitcasts into vector-to-vector bitcasts 99// This allows for other simplifications (although some of them 100// can only be handled by Analysis/ConstantFolding.cpp). 101if (isa<VectorType>(DestTy) && !isa<VectorType>(SrcTy))
104// PPC_FP128 is really the sum of two consecutive doubles, where the first 105// double is always stored first in memory, regardless of the target 106// endianness. The memory layout of i128, however, depends on the target 107// endianness, and so we can't fold this without target endianness 108// information. This should instead be handled by 109// Analysis/ConstantFolding.cpp 113// Make sure dest type is compatible with the folded integer constant. 118return ConstantInt::get(DestTy,
FP->getValueAPF().bitcastToAPInt());
133if (isa<PoisonValue>(V))
136if (isa<UndefValue>(V)) {
137// zext(undef) = 0, because the top bits will be zero. 138// sext(undef) = 0, because the top bits will all be the same. 139// [us]itofp(undef) = 0, because the result value is bounded. 140if (opc == Instruction::ZExt || opc == Instruction::SExt ||
141 opc == Instruction::UIToFP || opc == Instruction::SIToFP)
147 opc != Instruction::AddrSpaceCast)
150// If the cast operand is a constant expression, there's a few things we can 151// do to try to simplify it. 154// Try hard to fold cast of cast because they are often eliminable. 160// If the cast operand is a constant vector, perform the cast by 161// operating on each element. In the cast of bitcasts, the element 162// count may be mismatched; don't attempt to handle that here. 163if ((isa<ConstantVector>(V) || isa<ConstantDataVector>(V)) &&
165 cast<FixedVectorType>(DestTy)->getNumElements() ==
166 cast<FixedVectorType>(V->getType())->getNumElements()) {
167VectorType *DestVecTy = cast<VectorType>(DestTy);
169// Fast path for splatted constants. 175 cast<VectorType>(DestTy)->getElementCount(), Res);
180 e = cast<FixedVectorType>(V->getType())->getNumElements();
191// We actually have to do a cast now. Perform the cast according to the 196case Instruction::FPTrunc:
197case Instruction::FPExt:
198if (
ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
200APFloat Val = FPC->getValueAPF();
202 APFloat::rmNearestTiesToEven, &
ignored);
203return ConstantFP::get(DestTy, Val);
205returnnullptr;
// Can't fold. 206case Instruction::FPToUI:
207case Instruction::FPToSI:
208if (
ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
209constAPFloat &V = FPC->getValueAPF();
212if (APFloat::opInvalidOp ==
213 V.convertToInteger(IntVal, APFloat::rmTowardZero, &
ignored)) {
214// Undefined behavior invoked - the destination type can't represent 215// the input constant. 218return ConstantInt::get(DestTy, IntVal);
220returnnullptr;
// Can't fold. 221case Instruction::UIToFP:
222case Instruction::SIToFP:
224constAPInt &api = CI->getValue();
228 APFloat::rmNearestTiesToEven);
229return ConstantFP::get(DestTy, apf);
232case Instruction::ZExt:
235return ConstantInt::get(DestTy, CI->getValue().zext(
BitWidth));
238case Instruction::SExt:
241return ConstantInt::get(DestTy, CI->getValue().sext(
BitWidth));
244case Instruction::Trunc: {
247return ConstantInt::get(DestTy, CI->getValue().trunc(
BitWidth));
252case Instruction::BitCast:
254case Instruction::AddrSpaceCast:
255case Instruction::IntToPtr:
256case Instruction::PtrToInt:
263// Check for i1 and vector true/false conditions. 264if (
Cond->isNullValue())
return V2;
265if (
Cond->isAllOnesValue())
return V1;
267// If the condition is a vector constant, fold the result elementwise. 272for (
unsigned i = 0, e = V1VTy->getNumElements(); i != e; ++i) {
275 ConstantInt::get(Ty, i));
277 ConstantInt::get(Ty, i));
278auto *
Cond = cast<Constant>(CondV->getOperand(i));
279if (isa<PoisonValue>(
Cond)) {
281 }
elseif (V1Element == V2Element) {
283 }
elseif (isa<UndefValue>(
Cond)) {
284 V = isa<UndefValue>(V1Element) ? V1Element : V2Element;
286if (!isa<ConstantInt>(
Cond))
break;
287 V =
Cond->isNullValue() ? V2Element : V1Element;
292// If we were able to build the vector, return it. 293if (Result.size() == V1VTy->getNumElements())
297if (isa<PoisonValue>(
Cond))
300if (isa<UndefValue>(
Cond)) {
301if (isa<UndefValue>(V1))
return V1;
305if (V1 == V2)
return V1;
307if (isa<PoisonValue>(V1))
309if (isa<PoisonValue>(V2))
312// If the true or false value is undef, we can fold to the other value as 313// long as the other value isn't poison. 315if (isa<PoisonValue>(
C))
318// TODO: We can analyze ConstExpr by opcode to determine if there is any 319// possibility of poison. 320if (isa<ConstantExpr>(
C))
323if (isa<ConstantInt>(
C) || isa<GlobalVariable>(
C) || isa<ConstantFP>(
C) ||
324 isa<ConstantPointerNull>(
C) || isa<Function>(
C))
327if (
C->getType()->isVectorTy())
328return !
C->containsPoisonElement() && !
C->containsConstantExpression();
330// TODO: Recursively analyze aggregates or other constants. 333if (isa<UndefValue>(V1) && NotPoison(V2))
return V2;
334if (isa<UndefValue>(V2) && NotPoison(V1))
return V1;
341auto *ValVTy = cast<VectorType>(Val->
getType());
343// extractelt poison, C -> poison 344// extractelt C, undef -> poison 345if (isa<PoisonValue>(Val) || isa<UndefValue>(
Idx))
348// extractelt undef, C -> undef 349if (isa<UndefValue>(Val))
352auto *CIdx = dyn_cast<ConstantInt>(
Idx);
356if (
auto *ValFVTy = dyn_cast<FixedVectorType>(Val->
getType())) {
357// ee({w,x,y,z}, wrong_value) -> poison 358if (CIdx->uge(ValFVTy->getNumElements()))
362// ee (gep (ptr, idx0, ...), idx) -> gep (ee (ptr, idx), ee (idx0, idx), ...) 363if (
auto *CE = dyn_cast<ConstantExpr>(Val)) {
364if (
auto *
GEP = dyn_cast<GEPOperator>(CE)) {
366 Ops.
reserve(CE->getNumOperands());
367for (
unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
369if (
Op->getType()->isVectorTy()) {
377return CE->getWithOperands(Ops, ValVTy->getElementType(),
false,
378GEP->getSourceElementType());
379 }
elseif (CE->getOpcode() == Instruction::InsertElement) {
380if (
constauto *IEIdx = dyn_cast<ConstantInt>(CE->getOperand(2))) {
382APSInt(CIdx->getValue()))) {
383return CE->getOperand(1);
394// Lane < Splat minimum vector width => extractelt Splat(x), Lane -> x 395if (CIdx->getValue().ult(ValVTy->getElementCount().getKnownMinValue())) {
406if (isa<UndefValue>(
Idx))
409// Inserting null into all zeros is still all zeros. 410// TODO: This is true for undef and poison splats too. 411if (isa<ConstantAggregateZero>(Val) && Elt->
isNullValue())
415if (!CIdx)
returnnullptr;
417// Do not iterate on scalable vector. The num of elements is unknown at 419if (isa<ScalableVectorType>(Val->
getType()))
422auto *ValTy = cast<FixedVectorType>(Val->
getType());
424unsigned NumElts = ValTy->getNumElements();
425if (CIdx->
uge(NumElts))
429 Result.reserve(NumElts);
432for (
unsigned i = 0; i != NumElts; ++i) {
434 Result.push_back(Elt);
447auto *V1VTy = cast<VectorType>(V1->
getType());
448unsigned MaskNumElts = Mask.size();
451Type *EltTy = V1VTy->getElementType();
453// Poison shuffle mask -> poison value. 458// If the mask is all zeros this is a splat, no need to go through all 460if (
all_of(Mask, [](
int Elt) {
return Elt == 0; })) {
466auto *VTy = VectorType::get(EltTy, MaskEltCount);
468 }
elseif (!MaskEltCount.isScalable())
472// Do not iterate on scalable vector. The num of elements is unknown at 474if (isa<ScalableVectorType>(V1VTy))
477unsigned SrcNumElts = V1VTy->getElementCount().getKnownMinValue();
479// Loop over the shuffle mask, evaluating each element. 481for (
unsigned i = 0; i != MaskNumElts; ++i) {
488if (
unsigned(Elt) >= SrcNumElts*2)
490elseif (
unsigned(Elt) >= SrcNumElts) {
494 ConstantInt::get(Ty, Elt - SrcNumElts));
499 Result.push_back(InElt);
507// Base case: no indices, so return the entire value. 520// Base case: no indices, so replace the entire value. 526 NumElts = ST->getNumElements();
528 NumElts = cast<ArrayType>(Agg->
getType())->getNumElements();
531for (
unsigned i = 0; i != NumElts; ++i) {
549// Handle scalar UndefValue and scalable vector UndefValue. Fixed-length 550// vectors are always evaluated per element. 551bool IsScalableVector = isa<ScalableVectorType>(
C->getType());
552bool HasScalarUndefOrScalableVectorUndef =
553 (!
C->getType()->isVectorTy() || IsScalableVector) && isa<UndefValue>(
C);
555if (HasScalarUndefOrScalableVectorUndef) {
557case Instruction::FNeg:
558returnC;
// -undef -> undef 559case Instruction::UnaryOpsEnd:
564// Constant should not be UndefValue, unless these are vector constants. 565assert(!HasScalarUndefOrScalableVectorUndef &&
"Unexpected UndefValue");
566// We only have FP UnaryOps right now. 567assert(!isa<ConstantInt>(
C) &&
"Unexpected Integer UnaryOp");
570constAPFloat &CV = CFP->getValueAPF();
574case Instruction::FNeg:
575return ConstantFP::get(
C->getType(),
neg(CV));
577 }
elseif (
auto *VTy = dyn_cast<VectorType>(
C->getType())) {
578// Fast path for splatted constants. 583if (
auto *FVTy = dyn_cast<FixedVectorType>(VTy)) {
584// Fold each element and create a vector constant from those constants. 587for (
unsigned i = 0, e = FVTy->getNumElements(); i != e; ++i) {
588Constant *ExtractIdx = ConstantInt::get(Ty, i);
593 Result.push_back(Res);
600// We don't know how to fold this. 608// Simplify BinOps with their identity values first. They are no-ops and we 609// can always return the other value, including undef or poison values. 611 Opcode, C1->
getType(),
/*AllowRHSIdentity*/false)) {
617 Opcode, C1->
getType(),
/*AllowRHSIdentity*/true)) {
622// Binary operations propagate poison. 623if (isa<PoisonValue>(C1) || isa<PoisonValue>(C2))
626// Handle scalar UndefValue and scalable vector UndefValue. Fixed-length 627// vectors are always evaluated per element. 628bool IsScalableVector = isa<ScalableVectorType>(C1->
getType());
629bool HasScalarUndefOrScalableVectorUndef =
631 (isa<UndefValue>(C1) || isa<UndefValue>(C2));
632if (HasScalarUndefOrScalableVectorUndef) {
634case Instruction::Xor:
635if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
636// Handle undef ^ undef -> 0 special case. This is a common 640case Instruction::Add:
641case Instruction::Sub:
643case Instruction::And:
644if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
// undef & undef -> undef 647case Instruction::Mul: {
648// undef * undef -> undef 649if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
652// X * undef -> undef if X is odd 657// X * undef -> 0 otherwise 660case Instruction::SDiv:
661case Instruction::UDiv:
662// X / undef -> poison 666// undef / X -> 0 otherwise 668case Instruction::URem:
669case Instruction::SRem:
670// X % undef -> poison 674// undef % X -> 0 otherwise 676case Instruction::Or:
// X | undef -> -1 677if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
// undef | undef -> undef 680case Instruction::LShr:
681// X >>l undef -> poison 682if (isa<UndefValue>(C2))
686case Instruction::AShr:
687// X >>a undef -> poison 688if (isa<UndefValue>(C2))
690// TODO: undef >>a X -> poison if the shift is exact 693case Instruction::Shl:
694// X << undef -> undef 695if (isa<UndefValue>(C2))
699case Instruction::FSub:
700// -0.0 - undef --> undef (consistent with "fneg undef") 704case Instruction::FAdd:
705case Instruction::FMul:
706case Instruction::FDiv:
707case Instruction::FRem:
708// [any flop] undef, undef -> undef 709if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
711// [any flop] C, undef -> NaN 712// [any flop] undef, C -> NaN 713// We could potentially specialize NaN/Inf constants vs. 'normal' 714// constants (possibly differently depending on opcode and operand). This 715// would allow returning undef sometimes. But it is always safe to fold to 716// NaN because we can choose the undef operand as NaN, and any FP opcode 717// with a NaN operand will propagate NaN. 719case Instruction::BinaryOpsEnd:
724// Neither constant should be UndefValue, unless these are vector constants. 725assert((!HasScalarUndefOrScalableVectorUndef) &&
"Unexpected UndefValue");
727// Handle simplifications when the RHS is a constant int. 730/*AllowLHSConstant*/false))
734case Instruction::UDiv:
735case Instruction::SDiv:
739case Instruction::URem:
740case Instruction::SRem:
746case Instruction::And:
747assert(!CI2->isZero() &&
"And zero handled above");
749// If and'ing the address of a global with a constant, fold it. 750if (CE1->getOpcode() == Instruction::PtrToInt &&
751 isa<GlobalValue>(CE1->getOperand(0))) {
752GlobalValue *GV = cast<GlobalValue>(CE1->getOperand(0));
754Align GVAlign;
// defaults to 1 760// If the function alignment is not specified then assume that it 762// This is dangerous; on x86, the alignment of the pointer 763// corresponds to the alignment of the function, but might be less 764// than 4 if it isn't explicitly specified. 765// However, a fix for this behaviour was reverted because it 766// increased code size (see https://reviews.llvm.org/D55115) 767// FIXME: This code should be deleted once existing targets have 768// appropriate defaults 769if (isa<Function>(GV) && !
DL.getFunctionPtrAlign())
771 }
elseif (isa<GlobalVariable>(GV)) {
772 GVAlign = cast<GlobalVariable>(GV)->getAlign().valueOrOne();
776unsigned DstWidth = CI2->getBitWidth();
777unsigned SrcWidth = std::min(DstWidth,
Log2(GVAlign));
780// If checking bits we know are clear, return zero. 781if ((CI2->getValue() & BitsNotSet) == CI2->getValue())
788 }
elseif (isa<ConstantInt>(C1)) {
789// If C1 is a ConstantInt and C2 is not, swap the operands. 798constAPInt &C1V = CI1->getValue();
799constAPInt &C2V = CI2->getValue();
803case Instruction::Add:
804return ConstantInt::get(C1->
getType(), C1V + C2V);
805case Instruction::Sub:
806return ConstantInt::get(C1->
getType(), C1V - C2V);
807case Instruction::Mul:
808return ConstantInt::get(C1->
getType(), C1V * C2V);
809case Instruction::UDiv:
810assert(!CI2->isZero() &&
"Div by zero handled above");
811return ConstantInt::get(CI1->getType(), C1V.
udiv(C2V));
812case Instruction::SDiv:
813assert(!CI2->isZero() &&
"Div by zero handled above");
816return ConstantInt::get(CI1->getType(), C1V.
sdiv(C2V));
817case Instruction::URem:
818assert(!CI2->isZero() &&
"Div by zero handled above");
819return ConstantInt::get(C1->
getType(), C1V.
urem(C2V));
820case Instruction::SRem:
821assert(!CI2->isZero() &&
"Div by zero handled above");
824return ConstantInt::get(C1->
getType(), C1V.
srem(C2V));
825case Instruction::And:
826return ConstantInt::get(C1->
getType(), C1V & C2V);
828return ConstantInt::get(C1->
getType(), C1V | C2V);
829case Instruction::Xor:
830return ConstantInt::get(C1->
getType(), C1V ^ C2V);
831case Instruction::Shl:
833return ConstantInt::get(C1->
getType(), C1V.
shl(C2V));
835case Instruction::LShr:
837return ConstantInt::get(C1->
getType(), C1V.
lshr(C2V));
839case Instruction::AShr:
841return ConstantInt::get(C1->
getType(), C1V.
ashr(C2V));
847/*AllowLHSConstant*/true))
849 }
elseif (
ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
850if (
ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) {
851constAPFloat &C1V = CFP1->getValueAPF();
852constAPFloat &C2V = CFP2->getValueAPF();
853APFloat C3V = C1V;
// copy for modification 857case Instruction::FAdd:
858 (void)C3V.
add(C2V, APFloat::rmNearestTiesToEven);
859return ConstantFP::get(C1->
getType(), C3V);
860case Instruction::FSub:
861 (void)C3V.
subtract(C2V, APFloat::rmNearestTiesToEven);
862return ConstantFP::get(C1->
getType(), C3V);
863case Instruction::FMul:
864 (void)C3V.
multiply(C2V, APFloat::rmNearestTiesToEven);
865return ConstantFP::get(C1->
getType(), C3V);
866case Instruction::FDiv:
867 (void)C3V.
divide(C2V, APFloat::rmNearestTiesToEven);
868return ConstantFP::get(C1->
getType(), C3V);
869case Instruction::FRem:
871return ConstantFP::get(C1->
getType(), C3V);
876if (
auto *VTy = dyn_cast<VectorType>(C1->
getType())) {
877// Fast path for splatted constants. 892if (
auto *FVTy = dyn_cast<FixedVectorType>(VTy)) {
893// Fold each element and create a vector constant from those constants. 896for (
unsigned i = 0, e = FVTy->getNumElements(); i != e; ++i) {
897Constant *ExtractIdx = ConstantInt::get(Ty, i);
905 Result.push_back(Res);
913// There are many possible foldings we could do here. We should probably 914// at least fold add of a pointer with an integer into the appropriate 915// getelementptr. This will improve alias analysis a bit. 917// Given ((a + b) + c), if (b + c) folds to something interesting, return 921if (!isa<ConstantExpr>(
T) || cast<ConstantExpr>(
T)->
getOpcode() != Opcode)
924 }
elseif (isa<ConstantExpr>(C2)) {
925// If C2 is a constant expr and C1 isn't, flop them around and fold the 926// other way if possible. 931// i1 can be simplified in many cases. 934case Instruction::Add:
935case Instruction::Sub:
937case Instruction::Shl:
938case Instruction::LShr:
939case Instruction::AShr:
940// We can assume that C2 == 0. If it were one the result would be 941// undefined because the shift value is as large as the bitwidth. 943case Instruction::SDiv:
944case Instruction::UDiv:
945// We can assume that C2 == 1. If it were zero the result would be 946// undefined through division by zero. 948case Instruction::URem:
949case Instruction::SRem:
950// We can assume that C2 == 1. If it were zero the result would be 951// undefined through division by zero. 958// We don't know how to fold this. 964auto isGlobalUnsafeForEquality = [](
constGlobalValue *GV) {
965if (GV->isInterposable() || GV->hasGlobalUnnamedAddr())
967if (
constauto *GVar = dyn_cast<GlobalVariable>(GV)) {
968Type *Ty = GVar->getValueType();
969// A global with opaque type might end up being zero sized. 972// A global with an empty type might lie at the address of any other 979// Don't try to decide equality of aliases. 980if (!isa<GlobalAlias>(GV1) && !isa<GlobalAlias>(GV2))
981if (!isGlobalUnsafeForEquality(GV1) && !isGlobalUnsafeForEquality(GV2))
982return ICmpInst::ICMP_NE;
983return ICmpInst::BAD_ICMP_PREDICATE;
986/// This function determines if there is anything we can decide about the two 987/// constants provided. This doesn't need to handle simple things like integer 988/// comparisons, but should instead handle ConstantExprs and GlobalValues. 989/// If we can determine that the two constants have a particular relation to 990/// each other, we should return the corresponding ICmp predicate, otherwise 991/// return ICmpInst::BAD_ICMP_PREDICATE. 994"Cannot compare different types of values!");
995if (V1 == V2)
return ICmpInst::ICMP_EQ;
997// The following folds only apply to pointers. 999return ICmpInst::BAD_ICMP_PREDICATE;
1001// To simplify this code we canonicalize the relation so that the first 1002// operand is always the most "complex" of the two. We consider simple 1003// constants (like ConstantPointerNull) to be the simplest, followed by 1004// BlockAddress, GlobalValues, and ConstantExpr's (the most complex). 1005auto GetComplexity = [](
Constant *V) {
1006if (isa<ConstantExpr>(V))
1008if (isa<GlobalValue>(V))
1010if (isa<BlockAddress>(V))
1014if (GetComplexity(V1) < GetComplexity(V2)) {
1016if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
1017return ICmpInst::getSwappedPredicate(SwappedRelation);
1018return ICmpInst::BAD_ICMP_PREDICATE;
1021if (
constBlockAddress *BA = dyn_cast<BlockAddress>(V1)) {
1022// Now we know that the RHS is a BlockAddress or simple constant. 1023if (
constBlockAddress *BA2 = dyn_cast<BlockAddress>(V2)) {
1024// Block address in another function can't equal this one, but block 1025// addresses in the current function might be the same if blocks are 1027if (BA2->getFunction() != BA->getFunction())
1028return ICmpInst::ICMP_NE;
1029 }
elseif (isa<ConstantPointerNull>(V2)) {
1030return ICmpInst::ICMP_NE;
1032 }
elseif (
constGlobalValue *GV = dyn_cast<GlobalValue>(V1)) {
1033// Now we know that the RHS is a GlobalValue, BlockAddress or simple 1035if (
constGlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
1037 }
elseif (isa<BlockAddress>(V2)) {
1038return ICmpInst::ICMP_NE;
// Globals never equal labels. 1039 }
elseif (isa<ConstantPointerNull>(V2)) {
1040// GlobalVals can never be null unless they have external weak linkage. 1041// We don't try to evaluate aliases here. 1042// NOTE: We should not be doing this constant folding if null pointer 1043// is considered valid for the function. But currently there is no way to 1044// query it from the Constant type. 1045if (!GV->hasExternalWeakLinkage() && !isa<GlobalAlias>(GV) &&
1047 GV->getType()->getAddressSpace()))
1048return ICmpInst::ICMP_UGT;
1050 }
elseif (
auto *CE1 = dyn_cast<ConstantExpr>(V1)) {
1051// Ok, the LHS is known to be a constantexpr. The RHS can be any of a 1052// constantexpr, a global, block address, or a simple constant. 1055switch (CE1->getOpcode()) {
1056case Instruction::GetElementPtr: {
1058// Ok, since this is a getelementptr, we know that the constant has a 1059// pointer type. Check the various cases. 1060if (isa<ConstantPointerNull>(V2)) {
1061// If we are comparing a GEP to a null pointer, check to see if the base 1062// of the GEP equals the null pointer. 1063if (
constGlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
1064// If its not weak linkage, the GVal must have a non-zero address 1065// so the result is greater-than 1066if (!GV->hasExternalWeakLinkage() && CE1GEP->
isInBounds())
1067return ICmpInst::ICMP_UGT;
1069 }
elseif (
constGlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
1070if (
constGlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
1074return ICmpInst::BAD_ICMP_PREDICATE;
1077 }
elseif (
constauto *CE2GEP = dyn_cast<GEPOperator>(V2)) {
1078// By far the most common case to handle is when the base pointers are 1079// obviously to the same global. 1080constConstant *CE2Op0 = cast<Constant>(CE2GEP->getPointerOperand());
1081if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
1082// Don't know relative ordering, but check for inequality. 1083if (CE1Op0 != CE2Op0) {
1086 cast<GlobalValue>(CE2Op0));
1087return ICmpInst::BAD_ICMP_PREDICATE;
1098return ICmpInst::BAD_ICMP_PREDICATE;
1106 VT->getElementCount());
1110// Fold FCMP_FALSE/FCMP_TRUE unconditionally. 1111if (Predicate == FCmpInst::FCMP_FALSE)
1114if (Predicate == FCmpInst::FCMP_TRUE)
1117// Handle some degenerate cases first 1118if (isa<PoisonValue>(C1) || isa<PoisonValue>(C2))
1121if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
1122bool isIntegerPredicate = ICmpInst::isIntPredicate(Predicate);
1123// For EQ and NE, we can always pick a value for the undef to make the 1124// predicate pass or fail, so we can return undef. 1125// Also, if both operands are undef, we can return undef for int comparison. 1129// Otherwise, for integer compare, pick the same value as the non-undef 1130// operand, and fold it to true or false. 1131if (isIntegerPredicate)
1134// Choosing NaN for the undef will always make unordered comparison succeed 1135// and ordered comparison fails. 1140// The caller is expected to commute the operands if the constant expression 1143if (Predicate == ICmpInst::ICMP_UGE)
1146if (Predicate == ICmpInst::ICMP_ULT)
1150// If the comparison is a comparison between two i1's, simplify it. 1153case ICmpInst::ICMP_EQ:
1154if (isa<ConstantInt>(C2))
1157case ICmpInst::ICMP_NE:
1164if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) {
1165constAPInt &V1 = cast<ConstantInt>(C1)->getValue();
1166constAPInt &V2 = cast<ConstantInt>(C2)->getValue();
1168 }
elseif (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) {
1169constAPFloat &C1V = cast<ConstantFP>(C1)->getValueAPF();
1170constAPFloat &C2V = cast<ConstantFP>(C2)->getValueAPF();
1172 }
elseif (
auto *C1VTy = dyn_cast<VectorType>(C1->
getType())) {
1174// Fast path for splatted constants. 1181// Do not iterate on scalable vector. The number of elements is unknown at 1183if (isa<ScalableVectorType>(C1VTy))
1186// If we can constant fold the comparison of each element, constant fold 1187// the whole vector comparison. 1190// Compare the elements, producing an i1 result or constant expr. 1191for (
unsignedI = 0, E = C1VTy->getElementCount().getKnownMinValue();
1209// We know that C1 == C2 || isUnordered(C1, C2). 1210if (Predicate == FCmpInst::FCMP_ONE)
1212elseif (Predicate == FCmpInst::FCMP_UEQ)
1216// Evaluate the relation between the two constants, per the predicate. 1217int Result = -1;
// -1 = unknown, 0 = known false, 1 = known true. 1220case ICmpInst::BAD_ICMP_PREDICATE:
1221break;
// Couldn't determine anything about these constants. 1222case ICmpInst::ICMP_EQ:
// We know the constants are equal! 1223// If we know the constants are equal, we can decide the result of this 1224// computation precisely. 1225 Result = ICmpInst::isTrueWhenEqual(Predicate);
1227case ICmpInst::ICMP_ULT:
1229case ICmpInst::ICMP_ULT:
case ICmpInst::ICMP_NE:
case ICmpInst::ICMP_ULE:
1231case ICmpInst::ICMP_UGT:
case ICmpInst::ICMP_EQ:
case ICmpInst::ICMP_UGE:
1237case ICmpInst::ICMP_SLT:
1239case ICmpInst::ICMP_SLT:
case ICmpInst::ICMP_NE:
case ICmpInst::ICMP_SLE:
1241case ICmpInst::ICMP_SGT:
case ICmpInst::ICMP_EQ:
case ICmpInst::ICMP_SGE:
1247case ICmpInst::ICMP_UGT:
1249case ICmpInst::ICMP_UGT:
case ICmpInst::ICMP_NE:
case ICmpInst::ICMP_UGE:
1251case ICmpInst::ICMP_ULT:
case ICmpInst::ICMP_EQ:
case ICmpInst::ICMP_ULE:
1257case ICmpInst::ICMP_SGT:
1259case ICmpInst::ICMP_SGT:
case ICmpInst::ICMP_NE:
case ICmpInst::ICMP_SGE:
1261case ICmpInst::ICMP_SLT:
case ICmpInst::ICMP_EQ:
case ICmpInst::ICMP_SLE:
1267case ICmpInst::ICMP_ULE:
1268if (Predicate == ICmpInst::ICMP_UGT)
1270if (Predicate == ICmpInst::ICMP_ULT || Predicate == ICmpInst::ICMP_ULE)
1273case ICmpInst::ICMP_SLE:
1274if (Predicate == ICmpInst::ICMP_SGT)
1276if (Predicate == ICmpInst::ICMP_SLT || Predicate == ICmpInst::ICMP_SLE)
1279case ICmpInst::ICMP_UGE:
1280if (Predicate == ICmpInst::ICMP_ULT)
1282if (Predicate == ICmpInst::ICMP_UGT || Predicate == ICmpInst::ICMP_UGE)
1285case ICmpInst::ICMP_SGE:
1286if (Predicate == ICmpInst::ICMP_SLT)
1288if (Predicate == ICmpInst::ICMP_SGT || Predicate == ICmpInst::ICMP_SGE)
1291case ICmpInst::ICMP_NE:
1292if (Predicate == ICmpInst::ICMP_EQ)
1294if (Predicate == ICmpInst::ICMP_NE)
1299// If we evaluated the result, return it now. 1301return ConstantInt::get(ResultTy, Result);
1303if ((!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) ||
1305// If C2 is a constant expr and C1 isn't, flip them around and fold the 1306// other way if possible. 1307// Also, if C1 is null and C2 isn't, flip them around. 1308 Predicate = ICmpInst::getSwappedPredicate(Predicate);
1316 std::optional<ConstantRange>
InRange,
1323if (isa<PoisonValue>(
C))
1326if (isa<UndefValue>(
C))
1329auto IsNoOp = [&]() {
1330// Avoid losing inrange information. 1336return IdxC->
isNullValue() || isa<UndefValue>(IdxC);
1340return GEPTy->
isVectorTy() && !
C->getType()->isVectorTy()
1342 cast<VectorType>(GEPTy)->getElementCount(),
C)
This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static unsigned foldConstantCastPair(unsigned opc, ConstantExpr *Op, Type *DstTy)
This function determines which opcode to use to fold two constant cast expressions together.
static Constant * foldMaybeUndesirableCast(unsigned opc, Constant *V, Type *DestTy)
static ICmpInst::Predicate areGlobalsPotentiallyEqual(const GlobalValue *GV1, const GlobalValue *GV2)
static Constant * FoldBitCast(Constant *V, Type *DestTy)
static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2)
This function determines if there is anything we can decide about the two constants provided.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
Looks at all the uses of the given value Returns the Liveness deduced from the uses of this value Adds all uses that cause the result to be MaybeLive to MaybeLiveRetUses If the result is MaybeLiveUses might be modified but its content should be ignored(since it might not be complete). DeadArgumentEliminationPass
Module.h This file contains the declarations for the Module class.
static bool InRange(int64_t Value, unsigned short Shift, int LBound, int HBound)
const SmallVectorImpl< MachineOperand > & Cond
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
opStatus divide(const APFloat &RHS, roundingMode RM)
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
opStatus subtract(const APFloat &RHS, roundingMode RM)
opStatus add(const APFloat &RHS, roundingMode RM)
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
opStatus multiply(const APFloat &RHS, roundingMode RM)
opStatus mod(const APFloat &RHS)
Class for arbitrary precision integers.
APInt udiv(const APInt &RHS) const
Unsigned division operation.
bool isMinSignedValue() const
Determine if this is the smallest signed value.
APInt urem(const APInt &RHS) const
Unsigned remainder operation.
unsigned getBitWidth() const
Return the number of bits in the APInt.
APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
APInt srem(const APInt &RHS) const
Function for signed remainder operation.
APInt shl(unsigned shiftAmt) const
Left-shift function.
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
An arbitrary precision integer that knows its signedness.
static bool isSameValue(const APSInt &I1, const APSInt &I2)
Determine if two APSInts have the same value, zero- or sign-extending as needed.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
size - Get the array size.
bool empty() const
empty - Check if the array is empty.
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
The address of a basic block.
static unsigned isEliminableCastPair(Instruction::CastOps firstOpcode, Instruction::CastOps secondOpcode, Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy, Type *DstIntPtrTy)
Determine how a pair of casts can be eliminated, if they can be at all.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
bool isTrueWhenEqual() const
This is just a convenience.
static bool isUnordered(Predicate predicate)
Determine if the predicate is an unordered operation.
static ConstantAggregateZero * get(Type *Ty)
static Constant * get(ArrayType *T, ArrayRef< Constant * > V)
A constant value that is initialized with an expression using other constant values.
static Constant * getExtractElement(Constant *Vec, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
static bool isDesirableCastOp(unsigned Opcode)
Whether creating a constant expression for this cast is desirable.
static Constant * getBinOpAbsorber(unsigned Opcode, Type *Ty, bool AllowLHSConstant=false)
Return the absorbing element for the given binary operation, i.e.
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
static Constant * getNot(Constant *C)
static Constant * getXor(Constant *C1, Constant *C2)
static Constant * get(unsigned Opcode, Constant *C1, Constant *C2, unsigned Flags=0, Type *OnlyIfReducedTy=nullptr)
get - Return a binary or shift operator constant expression, folding if possible.
static bool isDesirableBinOp(unsigned Opcode)
Whether creating a constant expression for this binary operator is desirable.
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static Constant * getBinOpIdentity(unsigned Opcode, Type *Ty, bool AllowRHSConstant=false, bool NSZ=false)
Return the identity constant for a binary opcode.
ConstantFP - Floating Point Values [float, double].
static Constant * getNaN(Type *Ty, bool Negative=false, uint64_t Payload=0)
This is the shared class of boolean and integer constants.
static ConstantInt * getTrue(LLVMContext &Context)
static ConstantInt * getFalse(LLVMContext &Context)
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
bool uge(uint64_t Num) const
This function will return true iff this constant represents a value with active bits bigger than 64 b...
static Constant * get(StructType *T, ArrayRef< Constant * > V)
Constant Vector Declarations.
static Constant * getSplat(ElementCount EC, Constant *Elt)
Return a ConstantVector with the specified constant in each element.
static Constant * get(ArrayRef< Constant * > V)
This is an important base class in LLVM.
Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
static Constant * getAllOnesValue(Type *Ty)
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
static constexpr ElementCount get(ScalarTy MinVal, bool Scalable)
static bool compare(const APFloat &LHS, const APFloat &RHS, FCmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
bool isInBounds() const
Test whether this is an inbounds GEP, as defined by LangRef.html.
bool hasAllZeroIndices() const
Return true if all of the indices of this GEP are zeros.
static Type * getGEPReturnType(Value *Ptr, ArrayRef< Value * > IdxList)
Returns the pointer type returned by the GEP instruction, which may be a vector of pointers.
Module * getParent()
Get the module that this global value is contained inside of...
static bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
bool isEquality() const
Return true if this predicate is either EQ or NE.
bool isAssociative() const LLVM_READONLY
Return true if the instruction is associative:
bool isCommutative() const LLVM_READONLY
Return true if the instruction is commutative:
Class to represent integer types.
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
A Module instance is used to store all the information related to an LLVM module.
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
void reserve(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Class to represent struct types.
The instances of the Type class are immutable: once they are created, they are never changed.
const fltSemantics & getFltSemantics() const
bool isVectorTy() const
True if this is an instance of VectorType.
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
bool isPointerTy() const
True if this is an instance of PointerType.
static IntegerType * getInt1Ty(LLVMContext &C)
bool isEmptyTy() const
Return true if this type is empty, that is, it has no elements or all of its elements are empty.
bool isPPC_FP128Ty() const
Return true if this is powerpc long double.
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
bool isFirstClassType() const
Return true if the type is "first class", meaning it is a valid type for a Value.
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
bool isX86_AMXTy() const
Return true if this is X86 AMX.
static IntegerType * getInt32Ty(LLVMContext &C)
static IntegerType * getInt64Ty(LLVMContext &C)
bool isIntegerTy() const
True if this is an instance of IntegerType.
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Value * getOperand(unsigned i) const
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
Align getPointerAlignment(const DataLayout &DL) const
Returns an alignment of the pointer value.
LLVMContext & getContext() const
All values hold a context through their type.
Base class of all SIMD vector types.
Type * getElementType() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
bool match(Val *V, const Pattern &P)
cstfp_pred_ty< is_neg_zero_fp > m_NegZeroFP()
Match a floating-point negative zero.
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
auto m_Undef()
Match an arbitrary undef constant.
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
This is an optimization pass for GlobalISel generic memory operations.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Constant * ConstantFoldSelectInstruction(Constant *Cond, Constant *V1, Constant *V2)
Attempt to constant fold a select instruction with the specified operands.
Constant * ConstantFoldCompareInstruction(CmpInst::Predicate Predicate, Constant *C1, Constant *C2)
Constant * ConstantFoldUnaryInstruction(unsigned Opcode, Constant *V)
Constant * ConstantFoldGetElementPtr(Type *Ty, Constant *C, std::optional< ConstantRange > InRange, ArrayRef< Value * > Idxs)
Constant * ConstantFoldExtractValueInstruction(Constant *Agg, ArrayRef< unsigned > Idxs)
Attempt to constant fold an extractvalue instruction with the specified operands and indices.
bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
Constant * ConstantFoldInsertElementInstruction(Constant *Val, Constant *Elt, Constant *Idx)
Attempt to constant fold an insertelement instruction with the specified operands and indices.
constexpr int PoisonMaskElem
Constant * ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx)
Attempt to constant fold an extractelement instruction with the specified operands and indices.
constexpr unsigned BitWidth
APFloat neg(APFloat X)
Returns the negated value of the argument.
Constant * ConstantFoldCastInstruction(unsigned opcode, Constant *V, Type *DestTy)
Constant * ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val, ArrayRef< unsigned > Idxs)
ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue instruction with the spe...
unsigned Log2(Align A)
Returns the log2 of the alignment.
Constant * ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2, ArrayRef< int > Mask)
Attempt to constant fold a shufflevector instruction with the specified operands and mask.
Constant * ConstantFoldBinaryInstruction(unsigned Opcode, Constant *V1, Constant *V2)
This struct is a compact representation of a valid (non-zero power of two) alignment.