Commit Graph

239 Commits

Author SHA1 Message Date
Sanjay Patel e989b12044 [InstCombine] add nnan requirement for sqrt(x) * sqrt(y) -> sqrt(x*y)
This is similar to D43765.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@327797 91177308-0d34-0410-b5e6-96231b3b80d8
2018-03-18 14:32:54 +00:00
Sanjay Patel 68ba6f10e9 [InstCombine] fix fmul reassociation to avoid creating an extra fdiv
This was supposed to be an NFC refactoring that will eventually allow
eliminating the isFast() predicate, but there's a rare possibility
that we would pessimize the code as shown in the test case because
we failed to check 'hasOneUse()' properly. This version also removes
an inefficiency of the old code; we would look for: 
(X * C) * C1 --> X * (C * C1)
...but that pattern is always handled by 
SimplifyAssociativeOrCommutative().


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@327404 91177308-0d34-0410-b5e6-96231b3b80d8
2018-03-13 14:46:32 +00:00
Sanjay Patel 027535e24a [InstCombine] rearrange visitFMul; NFCI
Put the simplest non-FMF folds first, so it's easier to
see what's left to fix/group/add with the FMF folds.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@326632 91177308-0d34-0410-b5e6-96231b3b80d8
2018-03-02 23:06:45 +00:00
Sanjay Patel a1ef0f7b4b [InstCombine] partly fix FMF for fmul+log2 fold
The code was checking that all of the instructions in the 
sequence are 'fast', but that's not necessary. The final 
multiply is all that we need to check (tests adjusted). 
The fmul doesn't need to be fully 'fast' either, but that 
can be another patch.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@326608 91177308-0d34-0410-b5e6-96231b3b80d8
2018-03-02 20:32:46 +00:00
Sanjay Patel 49894f8747 [InstCombine] allow fmul fold with less than 'fast'
This is a retry of r326502 with updates to the reassociate 
test file that I missed the first time.

@test15_reassoc in the supposed -reassociate test file 
(except that it tests 2 other passes too...) shows that
there's no clear responsiblity for reassociation transforms.

Instcombine now gets that case, but only because the
constant values are identical. Otherwise, it would still
miss that pattern. 

Reassociate doesn't get that case because it hasn't been 
updated to use less than 'fast' FMF.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@326513 91177308-0d34-0410-b5e6-96231b3b80d8
2018-03-02 00:14:51 +00:00
Sanjay Patel 76263322b9 revert r326502: [InstCombine] allow fmul fold with less than 'fast'
I forgot that I added tests for 'reassoc' to -reassociate, but
suprisingly that file calls -instcombine too, so it is affected.
I'll update that file and try again.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@326510 91177308-0d34-0410-b5e6-96231b3b80d8
2018-03-01 23:39:24 +00:00
Sanjay Patel 5bd1ce0c5d [InstCombine] allow fmul fold with less than 'fast'
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@326502 91177308-0d34-0410-b5e6-96231b3b80d8
2018-03-01 22:53:47 +00:00
Sanjay Patel 2648a57b6f [InstCombine] simplify code for (X*Y) * X => (X*X) * Y ; NFCI
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@326444 91177308-0d34-0410-b5e6-96231b3b80d8
2018-03-01 15:50:26 +00:00
Sanjay Patel 3190427825 [InstCombine] simplify code for X * -1.0 --> -X; NFC
I've added random FMF to one of the tests to show those are propagated.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@326377 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-28 22:30:04 +00:00
Sanjay Patel 8053b777d6 [InstCombine] move invariant call out of loop; NFC
We really shouldn't need a 2-loop here at all, but that's another cleanup.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@326330 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-28 16:50:51 +00:00
Sanjay Patel 2f11a75319 [InstCombine] move constant check into foldBinOpIntoSelectOrPhi; NFCI
Also, rename 'foldOpWithConstantIntoOperand' because that's annoyingly 
vague. The constant check is redundant in some cases, but it allows 
removing duplication for most of the calls.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@326329 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-28 16:36:24 +00:00
Sanjay Patel 1516d0af24 [InstCombine] allow fdiv folds with less than fully 'fast' ops
Note: gcc appears to allow this fold with -freciprocal-math alone, 
but clang/llvm require more than that with this patch. The wording
in the definitions seems fuzzy enough that it could go either way,
but we'll err on the conservative side of FMF interpretation.

This patch also changes the newly created fmul to have FMF propagated
by the last fdiv rather than intersecting the FMF of the fdivs. This
matches the behavior of other folds near here. The new fmul is only 
used to produce an intermediate op for the final fdiv result, so it
shouldn't be any stricter than that result. The previous behavior
could result in dropping FMF via other folds in instcombine or CSE.

Differential Revision: https://reviews.llvm.org/D43398


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@326098 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-26 16:02:45 +00:00
Sanjay Patel f021d3eee1 [InstCombine] simplify code for fabs(X) * fabs(X) -> X * X; NFC
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325968 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-23 22:38:10 +00:00
Sanjay Patel 4b957aaea8 [InstSimplify] sqrt(X) * sqrt(X) --> X
This was misplaced in InstCombine. We can loosen the FMF as a follow-up step.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325965 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-23 22:20:13 +00:00
Sanjay Patel 9d9603f80e [InstCombine] allow fmul-sqrt folds with less than full -ffast-math
Also, add a Builder method for intrinsics to reduce code duplication for clients.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325960 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-23 21:16:12 +00:00
Sanjay Patel 02d9ce7461 [InstCombine] refactor fmul with negated op folds; NFCI
The existing code was inefficiently looking for 'nsz' variants.
That's unnecessary because we canonicalize those to the expected
form with -0.0.

We may also want to adjust or remove the fold that sinks negation.
We don't do that for fdiv (or integer ops?). That should be uniform?
It may also lead to missed optimization as in PR21914:
https://bugs.llvm.org/show_bug.cgi?id=21914
...or we just have to fix other passes to avoid that problem.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325924 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-23 17:14:28 +00:00
Sanjay Patel 8ff0fbbcf6 [InstCombine] add and use Create*FMF functions; NFC
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325730 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-21 22:18:55 +00:00
Sanjay Patel 2fb7f37b94 [InstCombine] C / -X --> -C / X
We already do this in DAGCombiner, but it should
also be good to eliminate the fsub use in IR.

This is similar to rL325648.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325649 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-21 00:01:45 +00:00
Sanjay Patel ba989e1b97 [InstCombine] -X / C --> X / -C for FP
We already do this in DAGCombiner, but it should 
also be good to eliminate the fsub use in IR.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325648 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-20 23:51:16 +00:00
Sanjay Patel 968ac28c66 [InstCombine] remove unneeded operand swap: NFCI
FMul is commutative, so complexity-based canonicalization should always 
take care of the swap via SimplifyAssociativeOrCommutative(). 


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325628 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-20 21:52:46 +00:00
Sanjay Patel 928e1d73a4 [InstCombine] remove unneeded dyn_cast to prevent unused variable warning
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325597 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-20 17:14:53 +00:00
Sanjay Patel 162d2b4f9c [InstCombine] remove compound fdiv pattern folds
These are fdiv-with-constant-divisor, so they already become
reciprocal multiplies. The last gap for vector ops should be
closed with rL325590.

It's possible that we're missing folds for some edge cases 
with denormal intermediate constants after deleting these,
but there are no tests for those patterns, and it would be 
better to handle denormals more consistently (and less 
conservatively) as noted in TODO comments.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325595 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-20 16:52:17 +00:00
Sanjay Patel c1de3c6452 [InstCombine] fold fdiv with non-splat divisor to fmul: X/C --> X * (1/C)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325590 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-20 16:08:15 +00:00
Sanjay Patel 645e59085a [InstCombine] use CreateWithCopiedFlags to reduce code; NFCI
Also, move the folds with constants closer to make it easier to follow. 


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325541 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-19 23:09:03 +00:00
Sanjay Patel c6e8ed569c [InstCombine] allow fdiv with constant dividend folds with less than full -ffast-math
It's possible that we could allow this either 'arcp' or 'reassoc' alone, but this
should be conservatively better than what we have right now. GCC allows this with
only -freciprocal-math.

The last test is changed to show a case that is expected to fold, but we need D43398.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325533 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-19 21:46:52 +00:00
Sanjay Patel 17348b5856 [InstCombine] refactor fdiv with constant dividend folds; NFC
The last fold that used to be here was not necessary. That's a 
combination of 2 folds (and there's a regression test to show that).

The transforms are guarded by isFast(), but that should be loosened.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325531 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-19 21:17:58 +00:00
Sanjay Patel 8afcfc323d [Constant] add floating-point helpers for normal/finite-nz; NFC
...and delete the equivalent local functiona from InstCombine.

These might be useful to other InstCombine files or other passes
and makes FP queries more similar to integer constant queries.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325398 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-16 22:32:54 +00:00
Sanjay Patel e150103e1c [InstCombine] clean up fdiv-with-fdiv folds; NFCI
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325366 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-16 17:52:32 +00:00
Sanjay Patel 76c06b1097 [InstCombine] remove redundant debug info setting; NFC
The IRBuilder sets debuginfo in Insert(), so this was duplicating what already happened.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325358 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-16 16:42:04 +00:00
Sanjay Patel a60b694525 [InstCombine] reduce code duplication; NFC
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325353 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-16 16:13:20 +00:00
Sanjay Patel 04ab83e17b [InstCombine] use m_OneUse to reduce code; NFC
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325263 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-15 16:30:10 +00:00
Sanjay Patel b1c2195313 [InstCombine] allow sin/cos transforms with 'reassoc'
The variable name 'AllowReassociate' is a lie at this point because
it's set to 'isFast()' which is more than the 'reassoc' FMF after
rL317488.

In D41286, we showed that this transform may be valid even with strict
math by brute force checking every 32-bit float result.

There's a potential problem here because we're replacing with a tan()
libcall rather than a hypothetical LLVM tan intrinsic. So we might
set errno when we should be guaranteed not to do that. But that's
independent of this change.




git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325247 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-15 15:07:12 +00:00
Sanjay Patel 0e9e744bdd [InstCombine] allow X / C -> X * (1.0/C) for vector splat FP constants
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325237 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-15 13:55:52 +00:00
Sanjay Patel 4ba06d7558 [InstCombine] clean up fold for X / C -> X * (1.0/C); NFCI
This should work with vector constants too, but it's currently limited to scalar.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325187 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-14 23:04:17 +00:00
Sanjay Patel a7c2acc8f1 [InstCombine] simplify isFMulOrFDivWithConstant(); NFCI
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325142 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-14 17:16:33 +00:00
Sanjay Patel e735f34e8b [InstCombine] replace isa/cast with dyn_cast; NFC
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325141 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-14 16:56:44 +00:00
Sanjay Patel 8be336f7ed [InstCombine] refactor folds for mul with negated operands; NFCI
This keeps with our current usage of 'match' and is easier to see that
the optional NSW only applies in the non-constant operand case. 


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325140 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-14 16:50:55 +00:00
Sanjay Patel 96c99810d8 [InstCombine] (lshr X, 31) * Y --> (ashr X, 31) & Y
This replaces the bit-tracking based fold that did the same thing,
but it only worked for scalars and not directly. 

There is no evidence in existing regression tests that the greater 
power of bit-tracking was needed here, but we should be aware of 
this potential loss of optimization.



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325062 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-13 22:24:37 +00:00
Sanjay Patel b0e47911f9 [InstCombine] (bool X) * Y --> X ? Y : 0
This is both a functional improvement for vectors and an
efficiency improvement for scalars. The existing code below
the new folds does the same thing for scalars, but in an 
indirect and expensive way.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325048 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-13 20:41:22 +00:00
Simon Pilgrim 913da000b5 [InstCombine] Simplify getLogBase2 case for scalar/splats. NFCI.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325003 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-13 13:16:26 +00:00
Sanjay Patel 4ac9cb02bc [InstCombine] X / (X * Y) --> 1.0 / Y
This is similar to the instsimplify fold added with D42385 
( rL323716 )
...but this can't be in instsimplify because we're creating/morphing
a different instruction.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@324927 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-12 19:39:21 +00:00
Sanjay Patel dd467c1e78 [InstCombine] various clean-ups for div transforms; NFC
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@324922 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-12 18:38:35 +00:00
Sanjay Patel 0a090c80ad [InstCombine] various clean-ups for commonIDivTransforms; NFC
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@324891 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-12 14:14:56 +00:00
Sanjay Patel fab575cbcb [InstCombine] X / (X * Y) -> 1 / Y if the multiplication does not overflow
The related cases for (X * Y) / X were handled in rL124487.

https://rise4fun.com/Alive/6k9

The division in these tests is subsequently eliminated by existing instcombines
for 1/X.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@324843 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-11 17:20:32 +00:00
Simon Pilgrim be64cee9b5 [InstCombine] Add constant vector support for X udiv C, where C >= signbit
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@324728 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-09 10:43:59 +00:00
Simon Pilgrim 7adf52c27e [InstCombine] visitSRem - use m_Negative(APInt) helper. NFCI.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@324636 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-08 19:00:45 +00:00
Simon Pilgrim 28a96ee071 [InstCombine] Add m_Negative pattern matching
Allows us to add non-uniform constant vector support for "X urem C -> X < C ? X : X - C, where C >= signbit."

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@324631 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-08 18:36:01 +00:00
Simon Pilgrim 0a5886c1e0 [InstCombine] Fix issue with X udiv (POW2_C1 << N) for non-splat constant vectors
foldUDivShl was assuming that the input was a scalar or a splat constant

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@324613 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-08 15:19:38 +00:00
Simon Pilgrim a53fc2e9d8 [InstCombine] Fix issue with X udiv 2^C -> X >> C for non-splat constant vectors
foldUDivPow2Cst was assuming that the input was a scalar or a splat constant

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@324608 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-08 14:46:10 +00:00
Simon Pilgrim 78c0953f5c Fix unused variable warning.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@324605 91177308-0d34-0410-b5e6-96231b3b80d8
2018-02-08 14:24:26 +00:00