[PATCH] D137253: [CVP] Simplify comparisons without constant operand

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 2 07:06:02 PDT 2022


nikic created this revision.
nikic added a reviewer: spatel.
Herald added a subscriber: hiraditya.
Herald added a project: All.
nikic requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

CVP currently only tries to simplify comparisons if there is a constant operand. However, even if both are non-constant, we may be able to determine the result of the comparison based on range information.

IPSCCP is already capable of doing this, but because it runs very early, it may miss some cases.

This has some compile-time cost, but not particularly large: http://llvm-compile-time-tracker.com/compare.php?from=5fe9273c73969791795e5302933abadc9f33f09a&to=4c49e6b0e7d1834dd9e1f29144e915c7279740fb&stat=instructions:u


https://reviews.llvm.org/D137253

Files:
  llvm/lib/Analysis/LazyValueInfo.cpp
  llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
  llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll
  llvm/test/Transforms/CorrelatedValuePropagation/mul.ll
  llvm/test/Transforms/CorrelatedValuePropagation/shl.ll


Index: llvm/test/Transforms/CorrelatedValuePropagation/shl.ll
===================================================================
--- llvm/test/Transforms/CorrelatedValuePropagation/shl.ll
+++ llvm/test/Transforms/CorrelatedValuePropagation/shl.ll
@@ -412,8 +412,7 @@
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[C:%.*]] = add nuw nsw i8 [[B:%.*]], -3
 ; CHECK-NEXT:    [[SHL:%.*]] = shl nsw i8 [[C]], 2
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ult i8 [[C]], [[SHL]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 false
 ;
 entry:
   %c = add nuw nsw i8 %b, -3
Index: llvm/test/Transforms/CorrelatedValuePropagation/mul.ll
===================================================================
--- llvm/test/Transforms/CorrelatedValuePropagation/mul.ll
+++ llvm/test/Transforms/CorrelatedValuePropagation/mul.ll
@@ -209,8 +209,7 @@
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[C:%.*]] = add nuw nsw i8 [[B:%.*]], -3
 ; CHECK-NEXT:    [[MUL:%.*]] = mul nsw i8 [[C]], 4
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ult i8 [[C]], [[MUL]]
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 false
 ;
 entry:
   %c = add nuw nsw i8 %b, -3
Index: llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll
===================================================================
--- llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll
+++ llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll
@@ -1180,10 +1180,8 @@
 ; CHECK-NEXT:    br i1 [[AND]], label [[IF:%.*]], label [[ELSE:%.*]]
 ; CHECK:       if:
 ; CHECK-NEXT:    [[A_100:%.*]] = add nuw nsw i32 [[A]], 100
-; CHECK-NEXT:    [[CMP3:%.*]] = icmp ne i32 [[A_100]], [[B]]
-; CHECK-NEXT:    call void @check1(i1 [[CMP3]])
-; CHECK-NEXT:    [[CMP4:%.*]] = icmp eq i32 [[A_100]], [[B]]
-; CHECK-NEXT:    call void @check1(i1 [[CMP4]])
+; CHECK-NEXT:    call void @check1(i1 true)
+; CHECK-NEXT:    call void @check1(i1 false)
 ; CHECK-NEXT:    [[A_10:%.*]] = add nuw nsw i32 [[A]], 10
 ; CHECK-NEXT:    [[CMP5:%.*]] = icmp ne i32 [[A_10]], [[B]]
 ; CHECK-NEXT:    call void @check1(i1 [[CMP5]])
Index: llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
+++ llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
@@ -340,12 +340,9 @@
 /// exploiting range information.
 static bool constantFoldCmp(CmpInst *Cmp, LazyValueInfo *LVI) {
   Value *Op0 = Cmp->getOperand(0);
-  auto *C = dyn_cast<Constant>(Cmp->getOperand(1));
-  if (!C)
-    return false;
-
+  Value *Op1 = Cmp->getOperand(1);
   LazyValueInfo::Tristate Result =
-      LVI->getPredicateAt(Cmp->getPredicate(), Op0, C, Cmp,
+      LVI->getPredicateAt(Cmp->getPredicate(), Op0, Op1, Cmp,
                           /*UseBlockValue=*/true);
   if (Result == LazyValueInfo::Unknown)
     return false;
Index: llvm/lib/Analysis/LazyValueInfo.cpp
===================================================================
--- llvm/lib/Analysis/LazyValueInfo.cpp
+++ llvm/lib/Analysis/LazyValueInfo.cpp
@@ -1859,9 +1859,27 @@
     return getPredicateAt(CmpInst::getSwappedPredicate(Pred), RHS, C, CxtI,
                           UseBlockValue);
 
-  // Got two non-Constant values. While we could handle them somewhat,
-  // by getting their constant ranges, and applying ConstantRange::icmp(),
-  // so far it did not appear to be profitable.
+  // Got two non-Constant values. Try to determine the comparison results based
+  // on the block values of the two operands, e.g. because they have
+  // non-overlapping ranges.
+  if (UseBlockValue) {
+    Module *M = CxtI->getModule();
+    ValueLatticeElement L =
+        getImpl(PImpl, AC, M).getValueInBlock(LHS, CxtI->getParent(), CxtI);
+    if (L.isOverdefined())
+      return LazyValueInfo::Unknown;
+
+    ValueLatticeElement R =
+        getImpl(PImpl, AC, M).getValueInBlock(RHS, CxtI->getParent(), CxtI);
+    Type *Ty = Type::getInt1Ty(CxtI->getContext());
+    if (Constant *Res = L.getCompare((CmpInst::Predicate)P, Ty, R,
+                                     M->getDataLayout())) {
+      if (Res->isNullValue())
+        return LazyValueInfo::False;
+      if (Res->isOneValue())
+        return LazyValueInfo::True;
+    }
+  }
   return LazyValueInfo::Unknown;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D137253.472602.patch
Type: text/x-patch
Size: 4271 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221102/1376f2fc/attachment.bin>


More information about the llvm-commits mailing list