This is an archive of the discontinued LLVM Phabricator instance.

Eliminate a bitwise 'not' op of 'not' min/max by inverting the min/max.
ClosedPublic

Authored by ArturGainullin on Apr 5 2018, 5:54 AM.

Details

Summary

Bitwise 'not' of the min/max could be eliminated in the pattern:

%notx = xor i32 %x, -1
%cmp1 = icmp sgt[slt/ugt/ult] i32 %notx, %y
%smax = select i1 %cmp1, i32 %notx, i32 %y
%res = xor i32 %smax, -1

https://rise4fun.com/Alive/lCN

Diff Detail

Repository
rL LLVM

Event Timeline

ArturGainullin created this revision.Apr 5 2018, 5:54 AM

For reference, this pattern came up somewhere in the discussion leading up to D44266, but I can't remember exactly where.

We can generalize this - you don't need a constant operand to the min/max for it to be a win:
https://rise4fun.com/Alive/bftvin

spatel added a comment.Apr 5 2018, 6:09 AM

For reference, this pattern came up somewhere in the discussion leading up to D44266, but I can't remember exactly where.

We can generalize this - you don't need a constant operand to the min/max for it to be a win:
https://rise4fun.com/Alive/bftvin

I think I botched making the permalink:
https://rise4fun.com/Alive/nJp

Name: not_max_with_not_operand
%notx = xor i32 %x, -1
%cmp = icmp sgt i32 %notx, %y
%max = select i1 %cmp, i32 %notx, i32 %y
%res = xor i32 %max, -1

=>

%noty = xor i32 %y, -1
%cmp2 = icmp slt %x, %noty
%res = select i1 %cmp2, i32 %x, i32 %noty

spatel added inline comments.Apr 5 2018, 6:17 AM
lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
2716 ↗(On Diff #141138)

Use m_Not() to reduce the code.

  • Addressed review comments.
ArturGainullin edited the summary of this revision. (Show Details)Apr 9 2018, 4:10 AM
spatel added inline comments.Apr 9 2018, 6:24 AM
lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
2699 ↗(On Diff #141612)

A better description would be something like:
Eliminate a bitwise 'not' op of 'not' min/max by inverting the min/max.

2721 ↗(On Diff #141612)

Use CreateNot() to simplify code.

test/Transforms/InstCombine/xor.ll
580 ↗(On Diff #141612)

Please remove the Alive link. If you want to include the text of the proof as a comment that's fine AFAIK.
See discussion here: https://reviews.llvm.org/D45108?id=141224#inline-396638

651–663 ↗(On Diff #141612)

This is identical to test43? You may need to inhibit complexity-based operand canonicalization to test the pattern where the 'not' op is actually operand 1 of the compare; grep for 'thwart' in test/Transforms/InstCombine to see examples of that.

spatel added inline comments.Apr 9 2018, 6:31 AM
lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
2714 ↗(On Diff #141612)

We need to check (and add regression test) if the min/max hasOneUse().

  • Fixed according to comments.
ArturGainullin retitled this revision from Canonicalization of the min/max patterns. to Eliminate a bitwise 'not' op of 'not' min/max by inverting the min/max..Apr 10 2018, 1:59 AM
ArturGainullin edited the summary of this revision. (Show Details)
spatel accepted this revision.Apr 10 2018, 8:34 AM

LGTM - see inline for a nit.

lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
2717–2719 ↗(On Diff #141808)

Can remove the braces here based on coding style guidelines.

This revision is now accepted and ready to land.Apr 10 2018, 8:34 AM
This revision was automatically updated to reflect the committed changes.