[llvm] r329362 - [InstCombine] FP: Z - (X - Y) --> Z + (Y - X)
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 5 16:21:15 PDT 2018
Author: spatel
Date: Thu Apr 5 16:21:15 2018
New Revision: 329362
URL: http://llvm.org/viewvc/llvm-project?rev=329362&view=rev
Log:
[InstCombine] FP: Z - (X - Y) --> Z + (Y - X)
This restores what was lost with rL73243 but without
re-introducing the bug that was present in the old code.
Note that we already have these transforms if the ops are
marked 'fast' (and I assume that's happening somewhere in
the code added with rL170471), but we clearly don't need
all of 'fast' for these transforms.
Modified:
llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp
llvm/trunk/test/Transforms/InstCombine/fsub.ll
Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp?rev=329362&r1=329361&r2=329362&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp Thu Apr 5 16:21:15 2018
@@ -1698,6 +1698,7 @@ Instruction *InstCombiner::visitFSub(Bin
SQ.getWithInstruction(&I)))
return replaceInstUsesWith(I, V);
+ Value *X, *Y;
if (I.hasNoSignedZeros()) {
// Subtraction from -0.0 is the canonical form of fneg.
// fsub nsz 0, X ==> fsub nsz -0.0, X
@@ -1705,11 +1706,20 @@ Instruction *InstCombiner::visitFSub(Bin
return BinaryOperator::CreateFNegFMF(Op1, &I);
// With no-signed-zeros: -(X - Y) --> Y - X
- Value *X, *Y;
if (match(Op0, m_NegZeroFP()) && match(Op1, m_FSub(m_Value(X), m_Value(Y))))
return BinaryOperator::CreateFSubFMF(Y, X, &I);
}
+ // More generally than above, if Op0 is not -0.0: Z - (X - Y) --> Z + (Y - X)
+ // Canonicalize to fadd to make analysis easier.
+ // This can also help codegen because fadd is commutative.
+ if (I.hasNoSignedZeros() || CannotBeNegativeZero(Op0, SQ.TLI)) {
+ if (match(Op1, m_OneUse(m_FSub(m_Value(X), m_Value(Y))))) {
+ Value *NewSub = Builder.CreateFSubFMF(Y, X, &I);
+ return BinaryOperator::CreateFAddFMF(Op0, NewSub, &I);
+ }
+ }
+
if (isa<Constant>(Op0))
if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
if (Instruction *NV = FoldOpIntoSelect(I, SI))
@@ -1721,7 +1731,6 @@ Instruction *InstCombiner::visitFSub(Bin
return BinaryOperator::CreateFAddFMF(Op0, ConstantExpr::getFNeg(C), &I);
// X - (-Y) --> X + Y
- Value *Y;
if (match(Op1, m_FNeg(m_Value(Y))))
return BinaryOperator::CreateFAddFMF(Op0, Y, &I);
Modified: llvm/trunk/test/Transforms/InstCombine/fsub.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/fsub.ll?rev=329362&r1=329361&r2=329362&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/fsub.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/fsub.ll Thu Apr 5 16:21:15 2018
@@ -27,12 +27,12 @@ define float @neg_sub_nsz(float %x, floa
ret float %t2
}
-; FIXME: With nsz: Z - (X - Y) --> Z + (Y - X)
+; With nsz: Z - (X - Y) --> Z + (Y - X)
define float @sub_sub_nsz(float %x, float %y, float %z) {
; CHECK-LABEL: @sub_sub_nsz(
-; CHECK-NEXT: [[T1:%.*]] = fsub float [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT: [[T2:%.*]] = fsub nsz float [[Z:%.*]], [[T1]]
+; CHECK-NEXT: [[TMP1:%.*]] = fsub nsz float [[Y:%.*]], [[X:%.*]]
+; CHECK-NEXT: [[T2:%.*]] = fadd nsz float [[TMP1]], [[Z:%.*]]
; CHECK-NEXT: ret float [[T2]]
;
%t1 = fsub float %x, %y
@@ -40,12 +40,12 @@ define float @sub_sub_nsz(float %x, floa
ret float %t2
}
-; FIXME: Same as above: if 'Z' is not -0.0, swap fsub operands and convert to fadd.
+; Same as above: if 'Z' is not -0.0, swap fsub operands and convert to fadd.
define float @sub_sub_known_not_negzero(float %x, float %y) {
; CHECK-LABEL: @sub_sub_known_not_negzero(
-; CHECK-NEXT: [[T1:%.*]] = fsub float [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT: [[T2:%.*]] = fsub float 4.200000e+01, [[T1]]
+; CHECK-NEXT: [[TMP1:%.*]] = fsub float [[Y:%.*]], [[X:%.*]]
+; CHECK-NEXT: [[T2:%.*]] = fadd float [[TMP1]], 4.200000e+01
; CHECK-NEXT: ret float [[T2]]
;
%t1 = fsub float %x, %y
More information about the llvm-commits
mailing list