From 05281d95f2385fd7e266c1b439e5045f0187b149 Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Sun, 10 Oct 2021 11:26:03 -0400 Subject: [PATCH] [InstCombine] move fold for "(X-Y) == 0"; NFC This consolidates related folds that all have a similar use restriction that may not be necessary. --- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp index 63be48f..d47e616 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -2602,9 +2602,16 @@ Instruction *InstCombinerImpl::foldICmpSubConstant(ICmpInst &Cmp, // The following transforms are only worth it if the only user of the subtract // is the icmp. + // TODO: This is an artificial restriction for all of the transforms below + // that only need a single replacement icmp. if (!Sub->hasOneUse()) return nullptr; + // X - Y == 0 --> X == Y. + // X - Y != 0 --> X != Y. + if (Cmp.isEquality() && C.isZero()) + return new ICmpInst(Pred, X, Y); + if (Sub->hasNoSignedWrap()) { // (icmp sgt (sub nsw X, Y), -1) -> (icmp sge X, Y) if (Pred == ICmpInst::ICMP_SGT && C.isAllOnes()) @@ -3130,14 +3137,6 @@ Instruction *InstCombinerImpl::foldICmpBinOpEqualityWithConstant( } } break; - case Instruction::Sub: - if (BO->hasOneUse()) { - if (C.isZero()) { - // Replace ((sub A, B) != 0) with (A != B). - return new ICmpInst(Pred, BOp0, BOp1); - } - } - break; case Instruction::Or: { const APInt *BOC; if (match(BOp1, m_APInt(BOC)) && BO->hasOneUse() && RHS->isAllOnesValue()) { -- 2.7.4