1//===- CmpInstAnalysis.cpp - Utils to help fold compares ---------------===// 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 holds routines to help analyse compare instructions 10// and fold them into constants or other compare instructions 12//===----------------------------------------------------------------------===// 24case ICmpInst::ICMP_UGT:
return 1;
// 001 25case ICmpInst::ICMP_SGT:
return 1;
// 001 26case ICmpInst::ICMP_EQ:
return 2;
// 010 27case ICmpInst::ICMP_UGE:
return 3;
// 011 28case ICmpInst::ICMP_SGE:
return 3;
// 011 29case ICmpInst::ICMP_ULT:
return 4;
// 100 30case ICmpInst::ICMP_SLT:
return 4;
// 100 31case ICmpInst::ICMP_NE:
return 5;
// 101 32case ICmpInst::ICMP_ULE:
return 6;
// 110 33case ICmpInst::ICMP_SLE:
return 6;
// 110 46case 1: Pred = Sign ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
break;
47case 2: Pred = ICmpInst::ICMP_EQ;
break;
48case 3: Pred = Sign ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE;
break;
49case 4: Pred = Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
break;
50case 5: Pred = ICmpInst::ICMP_NE;
break;
51case 6: Pred = Sign ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE;
break;
67assert(FCmpInst::FCMP_FALSE <= Pred && Pred <= FCmpInst::FCMP_TRUE &&
68"Unexpected FCmp predicate!");
69if (Pred == FCmpInst::FCMP_FALSE)
71if (Pred == FCmpInst::FCMP_TRUE)
76std::optional<DecomposedBitTest>
78bool LookThruTrunc,
bool AllowNonZeroC) {
79using namespacePatternMatch;
88 Pred = ICmpInst::getInversePredicate(Pred);
93if (ICmpInst::isSigned(Pred) ?
C.isMaxSignedValue() :
C.isMaxValue())
96 Pred = ICmpInst::getStrictPredicate(Pred);
103case ICmpInst::ICMP_SLT: {
104// X < 0 is equivalent to (X & SignMask) != 0. 108 Result.Pred = ICmpInst::ICMP_NE;
114// X s< 10000100 is equivalent to (X & 11111100 == 10000000) 115 Result.Mask = -FlippedSign;
117 Result.Pred = ICmpInst::ICMP_EQ;
122// X s< 01111100 is equivalent to (X & 11111100 != 01111100) 123 Result.Mask = FlippedSign;
125 Result.Pred = ICmpInst::ICMP_NE;
131case ICmpInst::ICMP_ULT:
132// X <u 2^n is equivalent to (X & ~(2^n-1)) == 0. 136 Result.Pred = ICmpInst::ICMP_EQ;
140// X u< 11111100 is equivalent to (X & 11111100 != 11111100) 141if (
C.isNegatedPowerOf2()) {
144 Result.Pred = ICmpInst::ICMP_NE;
151if (!AllowNonZeroC && !Result.C.isZero())
155 Result.Pred = ICmpInst::getInversePredicate(Result.Pred);
160 Result.Mask = Result.Mask.zext(
X->getType()->getScalarSizeInBits());
161 Result.C = Result.C.zext(
X->getType()->getScalarSizeInBits());
169std::optional<DecomposedBitTest>
171if (
auto *ICmp = dyn_cast<ICmpInst>(
Cond)) {
172// Don't allow pointers. Splat vectors are fine. 173if (!ICmp->getOperand(0)->getType()->isIntOrIntVectorTy())
176 ICmp->getPredicate(), LookThruTrunc,
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
const SmallVectorImpl< MachineOperand > & Cond
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Class for arbitrary precision integers.
bool isNegatedPowerOf2() const
Check if this APInt's negated value is a power of two greater than zero.
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
static Type * makeCmpResultType(Type *opnd_type)
Create a result type for fcmp/icmp.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
This is an important base class in LLVM.
static bool isGE(Predicate P)
Return true if the predicate is SGE or UGE.
static bool isGT(Predicate P)
Return true if the predicate is SGT or UGT.
bool isEquality() const
Return true if this predicate is either EQ or NE.
bool isRelational() const
Return true if the predicate is relational (not EQ or NE).
static bool isLE(Predicate P)
Return true if the predicate is SLE or ULE.
The instances of the Type class are immutable: once they are created, they are never changed.
LLVM Value Representation.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
bool match(Val *V, const Pattern &P)
apint_match m_APIntAllowPoison(const APInt *&Res)
Match APInt while allowing poison in splat vector constants.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
This is an optimization pass for GlobalISel generic memory operations.
Constant * getPredForFCmpCode(unsigned Code, Type *OpTy, CmpInst::Predicate &Pred)
This is the complement of getFCmpCode.
bool predicatesFoldable(CmpInst::Predicate P1, CmpInst::Predicate P2)
Return true if both predicates match sign or if at least one of them is an equality comparison (which...
std::optional< DecomposedBitTest > decomposeBitTest(Value *Cond, bool LookThroughTrunc=true, bool AllowNonZeroC=false)
Decompose an icmp into the form ((X & Mask) pred C) if possible.
std::optional< DecomposedBitTest > decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate Pred, bool LookThroughTrunc=true, bool AllowNonZeroC=false)
Decompose an icmp into the form ((X & Mask) pred C) if possible.
unsigned getICmpCode(CmpInst::Predicate Pred)
Encode a icmp predicate into a three bit mask.
Constant * getPredForICmpCode(unsigned Code, bool Sign, Type *OpTy, CmpInst::Predicate &Pred)
This is the complement of getICmpCode.
Represents the operation icmp (X & Mask) pred C, where pred can only be eq or ne.