[llvm] GVN: generalize impliesEquivalence (NFC) (PR #111416)

Ramkumar Ramachandra via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 8 07:58:08 PDT 2024


https://github.com/artagnon updated https://github.com/llvm/llvm-project/pull/111416

>From 6b9f0449037ac6b3e6761ebd0164b43e48bcf7a0 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <ramkumar.ramachandra at codasip.com>
Date: Mon, 7 Oct 2024 12:20:57 +0100
Subject: [PATCH] GVN: generalize impliesEquivalence (NFC)

impliesEquivalenceIfTrue and impliesEquivalenceIfFalse can be extended
to aggregates easily, although as the added tests show, the codepath
cannot be exercised: GVN does not propogate values through a
vector-select. Generalize the code anyway, as it opens up opportunities
for optimizing GVN. While at it, note that the optimization is invalid
for the no-signed-zeros case, and strip the bad FIXME.

Alive2 proof: https://alive2.llvm.org/ce/z/vEaK8M
---
 llvm/lib/Transforms/Scalar/GVN.cpp | 44 ++++++++++++++----------------
 llvm/test/Transforms/GVN/edge.ll   | 28 +++++++++++++++++++
 2 files changed, 48 insertions(+), 24 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/GVN.cpp b/llvm/lib/Transforms/Scalar/GVN.cpp
index 2ba600497e00d3..f31ad82f60051b 100644
--- a/llvm/lib/Transforms/Scalar/GVN.cpp
+++ b/llvm/lib/Transforms/Scalar/GVN.cpp
@@ -1998,19 +1998,17 @@ static bool impliesEquivalanceIfTrue(CmpInst* Cmp) {
   // +0.0 vs 0.0 for all operators
   if (Cmp->getPredicate() == CmpInst::Predicate::FCMP_OEQ ||
       (Cmp->getPredicate() == CmpInst::Predicate::FCMP_UEQ &&
-       Cmp->getFastMathFlags().noNaNs())) {
-      Value *LHS = Cmp->getOperand(0);
-      Value *RHS = Cmp->getOperand(1);
-      // If we can prove either side non-zero, then equality must imply
-      // equivalence.
-      // FIXME: We should do this optimization if 'no signed zeros' is
-      // applicable via an instruction-level fast-math-flag or some other
-      // indicator that relaxed FP semantics are being used.
-      if (isa<ConstantFP>(LHS) && !cast<ConstantFP>(LHS)->isZero())
+       Cmp->hasNoNaNs())) {
+    Value *LHS = Cmp->getOperand(0);
+    Value *RHS = Cmp->getOperand(1);
+    // If we can prove either side non-zero, then equality must imply
+    // equivalence.
+    auto *ConstLHS = dyn_cast<Constant>(LHS),
+         *ConstRHS = dyn_cast<Constant>(RHS);
+    if (auto *Const = ConstLHS ? ConstLHS : ConstRHS) {
+      if (!Const->isZeroValue())
         return true;
-      if (isa<ConstantFP>(RHS) && !cast<ConstantFP>(RHS)->isZero())
-        return true;
-      // TODO: Handle vector floating point constants
+    }
   }
   return false;
 }
@@ -2023,20 +2021,18 @@ static bool impliesEquivalanceIfFalse(CmpInst* Cmp) {
   // NaNs for unordered operators
   // +0.0 vs 0.0 for all operators
   if ((Cmp->getPredicate() == CmpInst::Predicate::FCMP_ONE &&
-       Cmp->getFastMathFlags().noNaNs()) ||
+       Cmp->hasNoNaNs()) ||
       Cmp->getPredicate() == CmpInst::Predicate::FCMP_UNE) {
-      Value *LHS = Cmp->getOperand(0);
-      Value *RHS = Cmp->getOperand(1);
-      // If we can prove either side non-zero, then equality must imply
-      // equivalence.
-      // FIXME: We should do this optimization if 'no signed zeros' is
-      // applicable via an instruction-level fast-math-flag or some other
-      // indicator that relaxed FP semantics are being used.
-      if (isa<ConstantFP>(LHS) && !cast<ConstantFP>(LHS)->isZero())
+    Value *LHS = Cmp->getOperand(0);
+    Value *RHS = Cmp->getOperand(1);
+    // If we can prove either side non-zero, then equality must imply
+    // equivalence.
+    auto *ConstLHS = dyn_cast<Constant>(LHS),
+         *ConstRHS = dyn_cast<Constant>(RHS);
+    if (auto *Const = ConstLHS ? ConstLHS : ConstRHS) {
+      if (!Const->isZeroValue())
         return true;
-      if (isa<ConstantFP>(RHS) && !cast<ConstantFP>(RHS)->isZero())
-        return true;
-      // TODO: Handle vector floating point constants
+    }
   }
   return false;
 }
diff --git a/llvm/test/Transforms/GVN/edge.ll b/llvm/test/Transforms/GVN/edge.ll
index 9703195d3b642e..e761d238db75c6 100644
--- a/llvm/test/Transforms/GVN/edge.ll
+++ b/llvm/test/Transforms/GVN/edge.ll
@@ -117,6 +117,20 @@ return:
   ret double %retval
 }
 
+define <2 x double> @fcmp_oeq_not_zero_vec(<2 x double> %x, <2 x double> %y) {
+; CHECK-LABEL: define <2 x double> @fcmp_oeq_not_zero_vec(
+; CHECK-SAME: <2 x double> [[X:%.*]], <2 x double> [[Y:%.*]]) {
+; CHECK-NEXT:    [[FCMP:%.*]] = fcmp oeq <2 x double> [[Y]], <double 2.000000e+00, double 2.000000e+00>
+; CHECK-NEXT:    [[DIV:%.*]] = fdiv <2 x double> [[X]], [[Y]]
+; CHECK-NEXT:    [[RETVAL:%.*]] = select <2 x i1> [[FCMP]], <2 x double> [[DIV]], <2 x double> [[X]]
+; CHECK-NEXT:    ret <2 x double> [[RETVAL]]
+;
+  %fcmp = fcmp oeq <2 x double> %y, <double 2.0, double 2.0>
+  %div = fdiv <2 x double> %x, %y
+  %retval = select <2 x i1> %fcmp, <2 x double> %div, <2 x double> %x
+  ret <2 x double> %retval
+}
+
 define double @fcmp_une_not_zero(double %x, double %y) {
 ; CHECK-LABEL: define double @fcmp_une_not_zero(
 ; CHECK-SAME: double [[X:%.*]], double [[Y:%.*]]) {
@@ -143,6 +157,20 @@ return:
   ret double %retval
 }
 
+define <2 x double> @fcmp_une_not_zero_vec(<2 x double> %x, <2 x double> %y) {
+; CHECK-LABEL: define <2 x double> @fcmp_une_not_zero_vec(
+; CHECK-SAME: <2 x double> [[X:%.*]], <2 x double> [[Y:%.*]]) {
+; CHECK-NEXT:    [[FCMP:%.*]] = fcmp une <2 x double> [[Y]], <double 2.000000e+00, double 2.000000e+00>
+; CHECK-NEXT:    [[DIV:%.*]] = fdiv <2 x double> [[X]], [[Y]]
+; CHECK-NEXT:    [[RETVAL:%.*]] = select <2 x i1> [[FCMP]], <2 x double> [[X]], <2 x double> [[DIV]]
+; CHECK-NEXT:    ret <2 x double> [[RETVAL]]
+;
+  %fcmp = fcmp une <2 x double> %y, <double 2.0, double 2.0>
+  %div = fdiv <2 x double> %x, %y
+  %retval = select <2 x i1> %fcmp, <2 x double> %x, <2 x double> %div
+  ret <2 x double> %retval
+}
+
 define double @fcmp_one_possibly_nan(double %x, double %y) {
 ; CHECK-LABEL: define double @fcmp_one_possibly_nan(
 ; CHECK-SAME: double [[X:%.*]], double [[Y:%.*]]) {



More information about the llvm-commits mailing list