[llvm-bugs] [Bug 39518] New: [InstCombine] optimize compares of abs/nabs into union/intersection of compares

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Nov 1 07:26:41 PDT 2018


https://bugs.llvm.org/show_bug.cgi?id=39518

            Bug ID: 39518
           Summary: [InstCombine] optimize compares of abs/nabs into
                    union/intersection of compares
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: spatel+llvm at rotateright.com
                CC: llvm-bugs at lists.llvm.org

Adjusting the bug-inducing code from bug 39510 to the more likely (?) pattern
shows an opportunity:

define i1 @abs_test(i32 %a) {
  %cmp = icmp slt i32 %a, 0
  %sub = sub nsw i32 0, %a
  %abs = select i1 %cmp, i32 %sub, i32 %a
  %r = icmp eq i32 %abs, 2
  ret i1 %r
}

This can be reduced to logic-of-icmps:

https://rise4fun.com/Alive/FjLJ


define i1 @abs_test_better(i32 %a) {
  %cmp1 = icmp eq i32 %a, 2
  %cmp2 = icmp eq i32 %a, -2
  %r = or i1 %cmp1, %cmp2
  ret i1 %r
}

----------------------------------------------------------------------------

More generally, we should be forming union/intersection patterns:
https://rise4fun.com/Alive/Rqq0

Name: union
  %cmp = icmp slt i32 %a, 0
  %sub = sub nsw i32 0, %a
  %abs = select i1 %cmp, i32 %sub, i32 %a
  %r = icmp sgt i32 %abs, 42
=>
  %cmp1 = icmp sgt i32 %a, 42
  %cmp2 = icmp slt i32 %a, -42
  %r = or i1 %cmp1, %cmp2

Name: intersection  
  %cmp = icmp slt i32 %a, 0
  %sub = sub nsw i32 0, %a
  %abs = select i1 %cmp, i32 %sub, i32 %a
  %r = icmp slt i32 %abs, 42
=>
  %cmp1 = icmp slt i32 %a, 42
  %cmp2 = icmp sgt i32 %a, -42
  %r = and i1 %cmp1, %cmp2

----------------------------------------------------------------------------

I don't have an immediate plan to work on this, but it probably needs a codegen
reversal before the IR canonicalization based on whether a target has a legal
ISD::ABS node.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20181101/95b9c0c7/attachment-0001.html>


More information about the llvm-bugs mailing list