This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Add support for maximum(a,b) + minimum(a,b) => a + b
ClosedPublic

Authored by skatkov on Mar 30 2023, 10:59 PM.

Details

Summary

Unfortunately alive2 cannot prove the correctness due to fails by timeout even for
float type half.

However it should be correct. If a and b are not NaN, maximum and minimum will just
return different values (a and b) and take into account a + b == b + a this is the same.
If a or b is NaN, than maximum and minimum are equal to NaN and NaN + NaN is NaN.
a + b is also a NaN.

In terms of preserving fast flags, we cannot preserve ninf due to
minimum(NaN, Infinity) == maximum(NaN, Infinity) == NaN,
minimum(NaN, Infinity) +ninf maximum(NaN, Infinity) == NaN +ninf NaN = NaN
However transformation will change
minimum(NaN, Infinity) + maximum(NaN, Infinity) to NaN +ninf Infinity == poison.

But if fadd is marked as nnan, we can preserve because NaN +ninf/nnan NaN = poison as well.

The same optimization for

maximum(a,b) * minimum(a,b) => a * b

is added.
All said above for fadd is correct for fmul.

Diff Detail

Event Timeline

skatkov created this revision.Mar 30 2023, 10:59 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 30 2023, 10:59 PM
skatkov requested review of this revision.Mar 30 2023, 10:59 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 30 2023, 10:59 PM

maximum/minimum propagate nan if present in either argument - I think that's OK here but could do with a second opinion

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
1814

minumin -> minimum

1820

Its it worth adding a m_c_Intrinsic matcher?

llvm/test/Transforms/InstCombine/fmul-maximum-minimum.ll
18

vector tests? more commute tests?

skatkov updated this revision to Diff 510742.Apr 4 2023, 4:08 AM

Added new tests and pre-land them.
Fix fast math flags copying.
Introduced m_c_Intrinsic.

skatkov marked 3 inline comments as done.Apr 4 2023, 4:08 AM
skatkov edited the summary of this revision. (Show Details)Apr 4 2023, 7:48 AM
skatkov added inline comments.Apr 4 2023, 7:05 PM
llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
775

Hrrrr, minimum again. Will update before landing or next iteration.

mkazantsev added inline comments.Apr 5 2023, 11:51 PM
llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
1818

We could also match min(a, b) + max(b, a)

skatkov added inline comments.Apr 5 2023, 11:54 PM
llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
1818

we do, see the test @test_comm2.
Actually it was m_c versus m_ does.

mkazantsev added inline comments.Apr 5 2023, 11:54 PM
llvm/test/Transforms/InstCombine/fadd-maximum-minimum.ll
14

Add test on (MAX + MIN)?

mkazantsev added inline comments.Apr 5 2023, 11:56 PM
llvm/test/Transforms/InstCombine/fadd-maximum-minimum.ll
14

Ah ok, its there already.

mkazantsev accepted this revision.Apr 5 2023, 11:58 PM

Maybe it makes sense to add alive2 links into test or code comments to demonstrate correctness.

This revision is now accepted and ready to land.Apr 5 2023, 11:58 PM

Maybe it makes sense to add alive2 links into test or code comments to demonstrate correctness.

As I wrote in description, alive2 ends with timeout on these tests.