[llvm] 2ad42c2 - [ValueTracking] improve analysis for fdiv with same operands

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 21 06:08:46 PDT 2020


Author: Sanjay Patel
Date: 2020-06-21T09:07:59-04:00
New Revision: 2ad42c2653ce31ca93dd2c9fdbd561d1d846d983

URL: https://github.com/llvm/llvm-project/commit/2ad42c2653ce31ca93dd2c9fdbd561d1d846d983
DIFF: https://github.com/llvm/llvm-project/commit/2ad42c2653ce31ca93dd2c9fdbd561d1d846d983.diff

LOG: [ValueTracking] improve analysis for fdiv with same operands

(The 'nnan' variant of this pattern is already tested to produce '1.0'.)

https://alive2.llvm.org/ce/z/D4hPBy

define i1 @src(float %x, i32 %y) {
%0:
  %d = fdiv float %x, %x
  %uge = fcmp uge float %d, 0.000000
  ret i1 %uge
}
=>
define i1 @tgt(float %x, i32 %y) {
%0:
  ret i1 1
}
Transformation seems to be correct!

Added: 
    

Modified: 
    llvm/lib/Analysis/ValueTracking.cpp
    llvm/test/Transforms/InstSimplify/floating-point-compare.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 545dab7714df..d01889e37aa5 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -3340,14 +3340,15 @@ static bool cannotBeOrderedLessThanZeroImpl(const Value *V,
   case Instruction::UIToFP:
     return true;
   case Instruction::FMul:
-    // x*x is always non-negative or a NaN.
+  case Instruction::FDiv:
+    // X * X is always non-negative or a NaN.
+    // X / X is always exactly 1.0 or a NaN.
     if (I->getOperand(0) == I->getOperand(1) &&
         (!SignBitOnly || cast<FPMathOperator>(I)->hasNoNaNs()))
       return true;
 
     LLVM_FALLTHROUGH;
   case Instruction::FAdd:
-  case Instruction::FDiv:
   case Instruction::FRem:
     return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly,
                                            Depth + 1) &&

diff  --git a/llvm/test/Transforms/InstSimplify/floating-point-compare.ll b/llvm/test/Transforms/InstSimplify/floating-point-compare.ll
index e5d5ffa0ff2a..7de0664b041b 100644
--- a/llvm/test/Transforms/InstSimplify/floating-point-compare.ll
+++ b/llvm/test/Transforms/InstSimplify/floating-point-compare.ll
@@ -205,9 +205,7 @@ define i1 @orderedLessZeroTree(float,float,float,float) {
 
 define i1 @orderedLessZero_fdiv(float %x) {
 ; CHECK-LABEL: @orderedLessZero_fdiv(
-; CHECK-NEXT:    [[D:%.*]] = fdiv float [[X:%.*]], [[X]]
-; CHECK-NEXT:    [[UGE:%.*]] = fcmp uge float [[D]], 0.000000e+00
-; CHECK-NEXT:    ret i1 [[UGE]]
+; CHECK-NEXT:    ret i1 true
 ;
   %d = fdiv float %x, %x
   %uge = fcmp uge float %d, 0.0


        


More information about the llvm-commits mailing list