Skip to content

Commit dc866ae

Browse files
authored
[ValueTracking] Move the isSignBitCheck helper into ValueTracking. NFC. (#81704)
This patch moves the `isSignBitCheck` helper into ValueTracking to reuse the logic in ValueTracking/InstSimplify. Addresses the comment #80740 (comment).
1 parent b5d694b commit dc866ae

File tree

6 files changed

+56
-52
lines changed

6 files changed

+56
-52
lines changed

llvm/include/llvm/Analysis/ValueTracking.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,12 @@ unsigned ComputeMaxSignificantBits(const Value *Op, const DataLayout &DL,
197197
Intrinsic::ID getIntrinsicForCallSite(const CallBase &CB,
198198
const TargetLibraryInfo *TLI);
199199

200+
/// Given an exploded icmp instruction, return true if the comparison only
201+
/// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if
202+
/// the result of the comparison is true when the input value is signed.
203+
bool isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS,
204+
bool &TrueIfSigned);
205+
200206
/// Returns a pair of values, which if passed to llvm.is.fpclass, returns the
201207
/// same result as an fcmp with the given operands.
202208
///

llvm/include/llvm/Transforms/InstCombine/InstCombiner.h

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -170,45 +170,6 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
170170
}
171171
}
172172

173-
/// Given an exploded icmp instruction, return true if the comparison only
174-
/// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if
175-
/// the result of the comparison is true when the input value is signed.
176-
static bool isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS,
177-
bool &TrueIfSigned) {
178-
switch (Pred) {
179-
case ICmpInst::ICMP_SLT: // True if LHS s< 0
180-
TrueIfSigned = true;
181-
return RHS.isZero();
182-
case ICmpInst::ICMP_SLE: // True if LHS s<= -1
183-
TrueIfSigned = true;
184-
return RHS.isAllOnes();
185-
case ICmpInst::ICMP_SGT: // True if LHS s> -1
186-
TrueIfSigned = false;
187-
return RHS.isAllOnes();
188-
case ICmpInst::ICMP_SGE: // True if LHS s>= 0
189-
TrueIfSigned = false;
190-
return RHS.isZero();
191-
case ICmpInst::ICMP_UGT:
192-
// True if LHS u> RHS and RHS == sign-bit-mask - 1
193-
TrueIfSigned = true;
194-
return RHS.isMaxSignedValue();
195-
case ICmpInst::ICMP_UGE:
196-
// True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
197-
TrueIfSigned = true;
198-
return RHS.isMinSignedValue();
199-
case ICmpInst::ICMP_ULT:
200-
// True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
201-
TrueIfSigned = false;
202-
return RHS.isMinSignedValue();
203-
case ICmpInst::ICMP_ULE:
204-
// True if LHS u<= RHS and RHS == sign-bit-mask - 1
205-
TrueIfSigned = false;
206-
return RHS.isMaxSignedValue();
207-
default:
208-
return false;
209-
}
210-
}
211-
212173
/// Add one to a Constant
213174
static Constant *AddOne(Constant *C) {
214175
return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1));

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3032,21 +3032,20 @@ static Value *simplifyICmpWithConstant(CmpInst::Predicate Pred, Value *LHS,
30323032
Type *ITy = getCompareTy(RHS); // The return type.
30333033

30343034
Value *X;
3035+
const APInt *C;
3036+
if (!match(RHS, m_APIntAllowUndef(C)))
3037+
return nullptr;
3038+
30353039
// Sign-bit checks can be optimized to true/false after unsigned
30363040
// floating-point casts:
30373041
// icmp slt (bitcast (uitofp X)), 0 --> false
30383042
// icmp sgt (bitcast (uitofp X)), -1 --> true
30393043
if (match(LHS, m_ElementWiseBitCast(m_UIToFP(m_Value(X))))) {
3040-
if (Pred == ICmpInst::ICMP_SLT && match(RHS, m_Zero()))
3041-
return ConstantInt::getFalse(ITy);
3042-
if (Pred == ICmpInst::ICMP_SGT && match(RHS, m_AllOnes()))
3043-
return ConstantInt::getTrue(ITy);
3044+
bool TrueIfSigned;
3045+
if (isSignBitCheck(Pred, *C, TrueIfSigned))
3046+
return ConstantInt::getBool(ITy, !TrueIfSigned);
30443047
}
30453048

3046-
const APInt *C;
3047-
if (!match(RHS, m_APIntAllowUndef(C)))
3048-
return nullptr;
3049-
30503049
// Rule out tautological comparisons (eg., ult 0 or uge 0).
30513050
ConstantRange RHS_CR = ConstantRange::makeExactICmpRegion(Pred, *C);
30523051
if (RHS_CR.isEmptySet())

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3810,6 +3810,45 @@ void KnownFPClass::propagateCanonicalizingSrc(const KnownFPClass &Src,
38103810
propagateNaN(Src, /*PreserveSign=*/true);
38113811
}
38123812

3813+
/// Given an exploded icmp instruction, return true if the comparison only
3814+
/// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if
3815+
/// the result of the comparison is true when the input value is signed.
3816+
bool llvm::isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS,
3817+
bool &TrueIfSigned) {
3818+
switch (Pred) {
3819+
case ICmpInst::ICMP_SLT: // True if LHS s< 0
3820+
TrueIfSigned = true;
3821+
return RHS.isZero();
3822+
case ICmpInst::ICMP_SLE: // True if LHS s<= -1
3823+
TrueIfSigned = true;
3824+
return RHS.isAllOnes();
3825+
case ICmpInst::ICMP_SGT: // True if LHS s> -1
3826+
TrueIfSigned = false;
3827+
return RHS.isAllOnes();
3828+
case ICmpInst::ICMP_SGE: // True if LHS s>= 0
3829+
TrueIfSigned = false;
3830+
return RHS.isZero();
3831+
case ICmpInst::ICMP_UGT:
3832+
// True if LHS u> RHS and RHS == sign-bit-mask - 1
3833+
TrueIfSigned = true;
3834+
return RHS.isMaxSignedValue();
3835+
case ICmpInst::ICMP_UGE:
3836+
// True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
3837+
TrueIfSigned = true;
3838+
return RHS.isMinSignedValue();
3839+
case ICmpInst::ICMP_ULT:
3840+
// True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
3841+
TrueIfSigned = false;
3842+
return RHS.isMinSignedValue();
3843+
case ICmpInst::ICMP_ULE:
3844+
// True if LHS u<= RHS and RHS == sign-bit-mask - 1
3845+
TrueIfSigned = false;
3846+
return RHS.isMaxSignedValue();
3847+
default:
3848+
return false;
3849+
}
3850+
}
3851+
38133852
/// Returns a pair of values, which if passed to llvm.is.fpclass, returns the
38143853
/// same result as an fcmp with the given operands.
38153854
std::pair<Value *, FPClassTest> llvm::fcmpToClassTest(FCmpInst::Predicate Pred,

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6021,7 +6021,7 @@ static APInt getDemandedBitsLHSMask(ICmpInst &I, unsigned BitWidth) {
60216021
// If this is a normal comparison, it demands all bits. If it is a sign bit
60226022
// comparison, it only demands the sign bit.
60236023
bool UnusedBit;
6024-
if (InstCombiner::isSignBitCheck(I.getPredicate(), *RHS, UnusedBit))
6024+
if (isSignBitCheck(I.getPredicate(), *RHS, UnusedBit))
60256025
return APInt::getSignMask(BitWidth);
60266026

60276027
switch (I.getPredicate()) {

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2381,8 +2381,7 @@ static Instruction *foldSelectToCopysign(SelectInst &Sel,
23812381
ICmpInst::Predicate Pred;
23822382
if (!match(Cond, m_OneUse(m_ICmp(Pred, m_ElementWiseBitCast(m_Value(X)),
23832383
m_APInt(C)))) ||
2384-
!InstCombiner::isSignBitCheck(Pred, *C, IsTrueIfSignSet) ||
2385-
X->getType() != SelType)
2384+
!isSignBitCheck(Pred, *C, IsTrueIfSignSet) || X->getType() != SelType)
23862385
return nullptr;
23872386

23882387
// If needed, negate the value that will be the sign argument of the copysign:
@@ -2581,7 +2580,7 @@ static Instruction *foldSelectWithSRem(SelectInst &SI, InstCombinerImpl &IC,
25812580
bool TrueIfSigned = false;
25822581

25832582
if (!(match(CondVal, m_ICmp(Pred, m_Value(RemRes), m_APInt(C))) &&
2584-
IC.isSignBitCheck(Pred, *C, TrueIfSigned)))
2583+
isSignBitCheck(Pred, *C, TrueIfSigned)))
25852584
return nullptr;
25862585

25872586
// If the sign bit is not set, we have a SGE/SGT comparison, and the operands
@@ -2781,7 +2780,7 @@ static Instruction *foldSelectWithFCmpToFabs(SelectInst &SI,
27812780
bool TrueIfSigned;
27822781
if (!match(CondVal,
27832782
m_ICmp(Pred, m_ElementWiseBitCast(m_Specific(X)), m_APInt(C))) ||
2784-
!IC.isSignBitCheck(Pred, *C, TrueIfSigned))
2783+
!isSignBitCheck(Pred, *C, TrueIfSigned))
27852784
continue;
27862785
if (!match(TrueVal, m_FNeg(m_Specific(X))))
27872786
return nullptr;

0 commit comments

Comments
 (0)