[llvm] 460faa0 - [InstSimplify] Check common operand with constant earlier

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 1 03:23:48 PST 2023


Author: Nikita Popov
Date: 2023-12-01T12:18:59+01:00
New Revision: 460faa0c87f0a9496cdaf6c856aff1886e29afe3

URL: https://github.com/llvm/llvm-project/commit/460faa0c87f0a9496cdaf6c856aff1886e29afe3
DIFF: https://github.com/llvm/llvm-project/commit/460faa0c87f0a9496cdaf6c856aff1886e29afe3.diff

LOG: [InstSimplify] Check common operand with constant earlier

If both icmps have the same operands and the RHS is constant, we
would currently go into the isImpliedCondMatchingOperands() code
path, instead of the isImpliedCondCommonOperandWithConstants()
path. Both are correct, but the latter can produce more accurate
results if the implication is dependent on the sign.

Added: 
    

Modified: 
    llvm/lib/Analysis/ValueTracking.cpp
    llvm/test/Transforms/InstSimplify/implies.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 9cfe7315a7a4dcc..d8a72c9f7b989d2 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -8352,17 +8352,17 @@ static std::optional<bool> isImpliedCondICmps(const ICmpInst *LHS,
   CmpInst::Predicate LPred =
       LHSIsTrue ? LHS->getPredicate() : LHS->getInversePredicate();
 
-  // Can we infer anything when the two compares have matching operands?
-  bool AreSwappedOps;
-  if (areMatchingOperands(L0, L1, R0, R1, AreSwappedOps))
-    return isImpliedCondMatchingOperands(LPred, RPred, AreSwappedOps);
-
   // Can we infer anything when the 0-operands match and the 1-operands are
   // constants (not necessarily matching)?
   const APInt *LC, *RC;
   if (L0 == R0 && match(L1, m_APInt(LC)) && match(R1, m_APInt(RC)))
     return isImpliedCondCommonOperandWithConstants(LPred, *LC, RPred, *RC);
 
+  // Can we infer anything when the two compares have matching operands?
+  bool AreSwappedOps;
+  if (areMatchingOperands(L0, L1, R0, R1, AreSwappedOps))
+    return isImpliedCondMatchingOperands(LPred, RPred, AreSwappedOps);
+
   // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1
   if (ICmpInst::isUnsigned(LPred) && ICmpInst::isUnsigned(RPred)) {
     if (L0 == R1) {

diff  --git a/llvm/test/Transforms/InstSimplify/implies.ll b/llvm/test/Transforms/InstSimplify/implies.ll
index 75044f4d9a356fa..d72dad95bfbd09b 100644
--- a/llvm/test/Transforms/InstSimplify/implies.ll
+++ b/llvm/test/Transforms/InstSimplify/implies.ll
@@ -501,10 +501,7 @@ define i1 @lshr_value(i32 %length.i, i32 %i, i32 %v) {
 
 define i1 @same_ops_with_constant(i8 %x) {
 ; CHECK-LABEL: @same_ops_with_constant(
-; CHECK-NEXT:    [[CMP1:%.*]] = icmp sgt i8 [[X:%.*]], 5
-; CHECK-NEXT:    [[CMP2:%.*]] = icmp ugt i8 [[X]], 5
-; CHECK-NEXT:    [[RES:%.*]] = icmp ule i1 [[CMP1]], [[CMP2]]
-; CHECK-NEXT:    ret i1 [[RES]]
+; CHECK-NEXT:    ret i1 true
 ;
   %cmp1 = icmp sgt i8 %x, 5
   %cmp2 = icmp ugt i8 %x, 5


        


More information about the llvm-commits mailing list