From 2ad42c2653ce31ca93dd2c9fdbd561d1d846d983 Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Sun, 21 Jun 2020 08:50:29 -0400 Subject: [PATCH] [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! --- llvm/lib/Analysis/ValueTracking.cpp | 5 +++-- llvm/test/Transforms/InstSimplify/floating-point-compare.ll | 4 +--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp index 545dab7..d01889e 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(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 e5d5ffa..7de0664 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 -- 2.7.4