diff options
| author | Sanjay Patel <spatel@rotateright.com> | 2019-07-31 16:53:22 +0000 | 
|---|---|---|
| committer | Sanjay Patel <spatel@rotateright.com> | 2019-07-31 16:53:22 +0000 | 
| commit | 435cdecdf721e39ec3b732f0affde970eadac525 (patch) | |
| tree | a6c707ed7c5bb546c63ef69ca37997c4f79ed056 /llvm/lib/Transforms | |
| parent | b9973f87c6e23062a8a921e8617d4625c355338d (diff) | |
| download | bcm5719-llvm-435cdecdf721e39ec3b732f0affde970eadac525.tar.gz bcm5719-llvm-435cdecdf721e39ec3b732f0affde970eadac525.zip | |
[InstCombine] canonicalize fneg before fmul/fdiv
Reverse the canonicalization of fneg relative to fmul/fdiv. That makes it
easier to implement the transforms (and possibly other fneg transforms) in
1 place because we can always start the pattern match from fneg (either the
legacy binop or the new unop).
There's a secondary practical benefit seen in PR21914 and PR42681:
https://bugs.llvm.org/show_bug.cgi?id=21914
https://bugs.llvm.org/show_bug.cgi?id=42681
...hoisting fneg rather than sinking seems to play nicer with LICM in IR
(although this change may expose analysis holes in the other direction).
1. The instcombine test changes show the expected neutral IR diffs from
   reversing the order.
2. The reassociation tests show that we were missing an optimization
   opportunity to fold away fneg-of-fneg. My reading of IEEE-754 says
   that all of these transforms are allowed (regardless of binop/unop
   fneg version) because:
   "For all other operations [besides copy/abs/negate/copysign], this
   standard does not specify the sign bit of a NaN result."
   In all of these transforms, we always have some other binop
   (fadd/fsub/fmul/fdiv), so we are free to flip the sign bit of a
   potential intermediate NaN operand.
   (If that interpretation is wrong, then we must already have a bug in
   the existing transforms?)
3. The clang tests shouldn't exist as-is, but that's effectively a
   revert of rL367149 (the test broke with an extension of the
   pre-existing fneg canonicalization in rL367146).
Differential Revision: https://reviews.llvm.org/D65399
llvm-svn: 367447
Diffstat (limited to 'llvm/lib/Transforms')
| -rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp | 22 | ||||
| -rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp | 20 | 
2 files changed, 22 insertions, 20 deletions
| diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp index 328c5021967..9c1c0c93480 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp @@ -1900,6 +1900,22 @@ static Instruction *foldFNegIntoConstant(Instruction &I) {    return nullptr;  } +static Instruction *hoistFNegAboveFMulFDiv(Instruction &I, +                                           InstCombiner::BuilderTy &Builder) { +  Value *FNeg; +  if (!match(&I, m_FNeg(m_Value(FNeg)))) +    return nullptr; + +  Value *X, *Y; +  if (match(FNeg, m_OneUse(m_FMul(m_Value(X), m_Value(Y))))) +    return BinaryOperator::CreateFMulFMF(Builder.CreateFNegFMF(X, &I), Y, &I); + +  if (match(FNeg, m_OneUse(m_FDiv(m_Value(X), m_Value(Y))))) +    return BinaryOperator::CreateFDivFMF(Builder.CreateFNegFMF(X, &I), Y, &I); + +  return nullptr; +} +  Instruction *InstCombiner::visitFNeg(UnaryOperator &I) {    Value *Op = I.getOperand(0); @@ -1917,6 +1933,9 @@ Instruction *InstCombiner::visitFNeg(UnaryOperator &I) {        match(Op, m_OneUse(m_FSub(m_Value(X), m_Value(Y)))))      return BinaryOperator::CreateFSubFMF(Y, X, &I); +  if (Instruction *R = hoistFNegAboveFMulFDiv(I, Builder)) +    return R; +    return nullptr;  } @@ -1938,6 +1957,9 @@ Instruction *InstCombiner::visitFSub(BinaryOperator &I) {    if (Instruction *X = foldFNegIntoConstant(I))      return X; +  if (Instruction *R = hoistFNegAboveFMulFDiv(I, Builder)) +    return R; +    Value *X, *Y;    Constant *C; diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp index 7939bbf5ca6..9e761c9e538 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp @@ -373,16 +373,6 @@ Instruction *InstCombiner::visitFMul(BinaryOperator &I) {    if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_Constant(C)))      return BinaryOperator::CreateFMulFMF(X, ConstantExpr::getFNeg(C), &I); -  // Sink negation: -X * Y --> -(X * Y) -  // But don't transform constant expressions because there's an inverse fold. -  if (match(Op0, m_OneUse(m_FNeg(m_Value(X)))) && !isa<ConstantExpr>(Op0)) -    return BinaryOperator::CreateFNegFMF(Builder.CreateFMulFMF(X, Op1, &I), &I); - -  // Sink negation: Y * -X --> -(X * Y) -  // But don't transform constant expressions because there's an inverse fold. -  if (match(Op1, m_OneUse(m_FNeg(m_Value(X)))) && !isa<ConstantExpr>(Op1)) -    return BinaryOperator::CreateFNegFMF(Builder.CreateFMulFMF(X, Op0, &I), &I); -    // fabs(X) * fabs(X) -> X * X    if (Op0 == Op1 && match(Op0, m_Intrinsic<Intrinsic::fabs>(m_Value(X))))      return BinaryOperator::CreateFMulFMF(X, X, &I); @@ -1234,16 +1224,6 @@ Instruction *InstCombiner::visitFDiv(BinaryOperator &I) {      return &I;    } -  // Sink negation: -X / Y --> -(X / Y) -  // But don't transform constant expressions because there's an inverse fold. -  if (match(Op0, m_OneUse(m_FNeg(m_Value(X)))) && !isa<ConstantExpr>(Op0)) -    return BinaryOperator::CreateFNegFMF(Builder.CreateFDivFMF(X, Op1, &I), &I); - -  // Sink negation: Y / -X --> -(Y / X) -  // But don't transform constant expressions because there's an inverse fold. -  if (match(Op1, m_OneUse(m_FNeg(m_Value(X)))) && !isa<ConstantExpr>(Op1)) -    return BinaryOperator::CreateFNegFMF(Builder.CreateFDivFMF(Op0, X, &I), &I); -    // X / (X * Y) --> 1.0 / Y    // Reassociate to (X / X -> 1.0) is legal when NaNs are not allowed.    // We can ignore the possibility that X is infinity because INF/INF is NaN. | 

