[PATCH] D17731: [InstCombine] Optimize (+0.0 - A) + B

Andres Nötzli via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 29 13:26:08 PST 2016


4tXJ7f created this revision.
4tXJ7f added a reviewer: majnemer.
4tXJ7f added a subscriber: llvm-commits.

Before, InstCombiner::visitFAdd() was only optimizing (-0.0 - A) + B and
not (+0.0 - A) + B because InstCombiner::dyn_castFNegVal() by default
only matches negative floating-point zeroes even though +/- 0.0 should
behave the same in this situation. This patch changes the check to
accept positive zeros and fixes a discrepancy between the names of the
optional parameter in the header and the source file for
InstCombiner::dyn_castFNegVal().

http://reviews.llvm.org/D17731

Files:
  lib/Transforms/InstCombine/InstCombineAddSub.cpp
  lib/Transforms/InstCombine/InstCombineInternal.h
  test/Transforms/InstCombine/fadd.ll

Index: test/Transforms/InstCombine/fadd.ll
===================================================================
--- /dev/null
+++ test/Transforms/InstCombine/fadd.ll
@@ -0,0 +1,23 @@
+; RUN: opt < %s -instcombine -S | FileCheck %s
+
+; -A + B --> B - A
+define float @test1(float %x, float %y) {
+  %t0 = fsub float -0.000000e+00, %x
+  %t1 = fadd float %t0, %y
+  ret float %t1
+
+; CHECK-LABEL: @test1
+; CHECK-NEXT:  [[R:%[a-z0-9]*]] = fsub float %y, %x
+; CHECK-NEXT:  ret float  [[R]]
+}
+
+; -A + B --> B - A
+define float @test2(float %x, float %y) {
+  %t0 = fsub float +0.000000e+00, %x
+  %t1 = fadd float %t0, %y
+  ret float %t1
+
+; CHECK-LABEL: @test2
+; CHECK-NEXT:  [[R:%[a-z0-9]*]] = fsub float %y, %x
+; CHECK-NEXT:  ret float [[R]]
+}
Index: lib/Transforms/InstCombine/InstCombineInternal.h
===================================================================
--- lib/Transforms/InstCombine/InstCombineInternal.h
+++ lib/Transforms/InstCombine/InstCombineInternal.h
@@ -345,7 +345,7 @@
   bool ShouldChangeType(unsigned FromBitWidth, unsigned ToBitWidth) const;
   bool ShouldChangeType(Type *From, Type *To) const;
   Value *dyn_castNegVal(Value *V) const;
-  Value *dyn_castFNegVal(Value *V, bool NoSignedZero = false) const;
+  Value *dyn_castFNegVal(Value *V, bool IgnoreZeroSign = false) const;
   Type *FindElementAtOffset(PointerType *PtrTy, int64_t Offset,
                             SmallVectorImpl<Value *> &NewIndices);
   Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI);
Index: lib/Transforms/InstCombine/InstCombineAddSub.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -1340,7 +1340,7 @@
 
   // -A + B  -->  B - A
   // -A + -B  -->  -(A + B)
-  if (Value *LHSV = dyn_castFNegVal(LHS)) {
+  if (Value *LHSV = dyn_castFNegVal(LHS, true)) {
     Instruction *RI = BinaryOperator::CreateFSub(RHS, LHSV);
     RI->copyFastMathFlags(&I);
     return RI;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D17731.49410.patch
Type: text/x-patch
Size: 2031 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160229/0e7273c7/attachment.bin>


More information about the llvm-commits mailing list