This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] reassociate min/max intrinsics with constant operands
ClosedPublic

Authored by spatel on Feb 14 2022, 11:46 AM.

Details

Summary

Integer min/max operations are associative:
max (max X, C0), C1 --> max X, (max C0, C1) --> max X, NewC

https://alive2.llvm.org/ce/z/wW5HVM

This would avoid a regression when we canonicalize to min/max intrinsics (see D98152 ).

Diff Detail

Event Timeline

spatel created this revision.Feb 14 2022, 11:46 AM
spatel requested review of this revision.Feb 14 2022, 11:46 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 14 2022, 11:46 AM
spatel edited the summary of this revision. (Show Details)Feb 14 2022, 11:46 AM

For this to work in practice, we also need: https://alive2.llvm.org/ce/z/XC3JqP

For this to work in practice, we also need: https://alive2.llvm.org/ce/z/XC3JqP

You mean to enable more general reassociation to cascade?

If the reassociate pass handled intrinsics, it would go the other direction (pull the constant higher up in the instruction sequence). I suspect it wouldn't be a small patch to generalize that pass to handle intrinsics instead of just true binop instructions.

I didn't think instcombine inverted that, but it does do that for 'or' instructions at least:
https://github.com/llvm/llvm-project/blob/cccef321096c20825fe8738045c1d91d3b9fd57d/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp#L2938

For this to work in practice, we also need: https://alive2.llvm.org/ce/z/XC3JqP

You mean to enable more general reassociation to cascade?

That was the idea, yes.

If the reassociate pass handled intrinsics, it would go the other direction (pull the constant higher up in the instruction sequence). I suspect it wouldn't be a small patch to generalize that pass to handle intrinsics instead of just true binop instructions.

I didn't think instcombine inverted that, but it does do that for 'or' instructions at least:
https://github.com/llvm/llvm-project/blob/cccef321096c20825fe8738045c1d91d3b9fd57d/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp#L2938

nikic accepted this revision.Feb 15 2022, 2:23 AM

LGTM

I think additional reassociation is not really a requirement here, and probably InstCombine is not the right place to reassociate large chains.

This revision is now accepted and ready to land.Feb 15 2022, 2:23 AM

LGTM

I think additional reassociation is not really a requirement here, and probably InstCombine is not the right place to reassociate large chains.

I think it's good to make that an incremental improvement. It's not a lot of work to implement a variation of the suggested generalization -- I already drafted it. :)

This revision was landed with ongoing or failed builds.Feb 15 2022, 5:31 AM
This revision was automatically updated to reflect the committed changes.