This is an archive of the discontinued LLVM Phabricator instance.

[x86] avoid flipping sign bits for vector icmp by using known bits
ClosedPublic

Authored by spatel on Jun 3 2017, 7:02 AM.

Details

Summary

The change itself is hopefully straightforward: if we know that both operands of an unsigned integer vector comparison are non-negative, then it's safe to directly use a signed-compare-greater-than instruction (the only integer vector compare predicate provided by SSE/AVX).

This should solve PR33276:
https://bugs.llvm.org/show_bug.cgi?id=33276

Some questions and potential follow-ups are raised here:

  1. Are we better off using pcmpgt + min/max/psubus instructions instead of inverting a pcmp?
  2. Since we're computing known bits, should we try harder to eliminate other compares like (known negative < known positive)?
  3. I can't tell what the intent of the AVX512 tests was supposed to be; (X u< 0) is always false. Those tests were added with rL276648.

Diff Detail

Repository
rL LLVM

Event Timeline

spatel created this revision.Jun 3 2017, 7:02 AM
RKSimon edited edge metadata.Jun 4 2017, 7:47 AM

Are we better off using pcmpgt + min/max/psubus instructions instead of inverting a pcmp?

TBH, I think the min/max/psubus cases are preferable.

Since we're computing known bits, should we try harder to eliminate other compares like (known negative < known positive)?

There's definitely scope in InstCombine/DAGCombine to use KnownBits to fold more comparisons.

spatel added a comment.Jun 4 2017, 8:02 AM

Are we better off using pcmpgt + min/max/psubus instructions instead of inverting a pcmp?

TBH, I think the min/max/psubus cases are preferable.

OK - I had an earlier draft that would leave more of those as-is. Let me review that and upload an updated patch.

Since we're computing known bits, should we try harder to eliminate other compares like (known negative < known positive)?

There's definitely scope in InstCombine/DAGCombine to use KnownBits to fold more comparisons.

We should certainly try harder in IR, but I wasn't sure if this was a real problem in the DAG. I'll add a TODO comment about this for a follow-up patch.

spatel updated this revision to Diff 101358.Jun 4 2017, 9:27 AM

Patch updated:

  1. Instead of changing the predicate from unsigned to signed, just remove the FlipSigns requirement when both operands are known non-negative. This lets the min/max/subus logic work as-is, so any case where we would have used those instructions should be unchanged.
  2. Add a TODO comment to see if we can do more with the knownbits knowledge.
RKSimon accepted this revision.Jun 7 2017, 6:03 AM

LGTM

This revision is now accepted and ready to land.Jun 7 2017, 6:03 AM
This revision was automatically updated to reflect the committed changes.