This is an archive of the discontinued LLVM Phabricator instance.

[InstSimplify] fold icmp based on range of abs/nabs
ClosedPublic

Authored by spatel on Oct 29 2018, 3:45 PM.

Details

Summary

This is a fix for PR39475:
https://bugs.llvm.org/show_bug.cgi?id=39475

We managed to get some of these patterns using the computeKnownBits logic in D47041, but I don't think that can be used for nabs().
Instead, put in some range-based logic, so we can fold both abs/nabs with icmp with a constant value.

Alive proofs:
https://rise4fun.com/Alive/21r

Name: abs_nsw_is_positive
  %cmp = icmp slt i32 %x, 0
  %negx = sub nsw i32 0, %x
  %abs = select i1 %cmp, i32 %negx, i32 %x
  %r = icmp sgt i32 %abs, -1
    =>
  %r = i1 true
 
Name: abs_nsw_is_not_negative
  %cmp = icmp slt i32 %x, 0
  %negx = sub nsw i32 0, %x
  %abs = select i1 %cmp, i32 %negx, i32 %x
  %r = icmp slt i32 %abs, 0
    =>
  %r = i1 false
 
Name: nabs_is_negative_or_0
  %cmp = icmp slt i32 %x, 0
  %negx = sub i32 0, %x
  %nabs = select i1 %cmp, i32 %x, i32 %negx
  %r = icmp slt i32 %nabs, 1
    =>
  %r = i1 true

Name: nabs_is_not_over_0
  %cmp = icmp slt i32 %x, 0
  %negx = sub i32 0, %x
  %nabs = select i1 %cmp, i32 %x, i32 %negx
  %r = icmp sgt i32 %nabs, 0
    =>
  %r = i1 false

Diff Detail

Repository
rL LLVM

Event Timeline

spatel created this revision.Oct 29 2018, 3:45 PM

I forgot to add it to PR39475 but what about float cases?

I forgot to add it to PR39475 but what about float cases?

That'll be an independent patch (but probably simpler since it's just a signbit test if we're ignoring -0.0/Nan).

RKSimon accepted this revision.Oct 30 2018, 6:52 AM

LGTM

This revision is now accepted and ready to land.Oct 30 2018, 6:52 AM
This revision was automatically updated to reflect the committed changes.
sbc100 added a subscriber: sbc100.Oct 31 2018, 2:06 PM

I think this change was responsible for breaking test on the wasm waterfall: https://bugs.llvm.org/show_bug.cgi?id=39510

spatel reopened this revision.Oct 31 2018, 2:42 PM

I think this change was responsible for breaking test on the wasm waterfall: https://bugs.llvm.org/show_bug.cgi?id=39510

Yes, sorry about that. Reverted at rL345780. Taking a look now.

This revision is now accepted and ready to land.Oct 31 2018, 2:42 PM
This revision was automatically updated to reflect the committed changes.