[llvm-bugs] [Bug 49763] New: Sub-optimal optimization of abs(x) % n

via llvm-bugs llvm-bugs at lists.llvm.org
Mon Mar 29 13:13:28 PDT 2021


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

            Bug ID: 49763
           Summary: Sub-optimal optimization of abs(x) % n
           Product: tools
           Version: trunk
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: opt
          Assignee: unassignedbugs at nondot.org
          Reporter: rifkin at purdue.edu
                CC: llvm-bugs at lists.llvm.org

LLVM does not find all optimizations related to taking the remainder of the
absolute value of a signed number.

The following equivilent expressions result in sub-optimal code generation:
    x % 2 == 1 || x % 2 == -1
    std::abs(x % 2) == 1
    std::abs(x) % 2 == 1

LLVM may be ignoring poison values for std::abs / @llvm.abs.* in making
optimizations for the remainder operation. Alternatively, LLVM may not be
taking
the absolute value into account when performing algebraic operations on the
remainder instructions.

If x is guaranteed to be positive (e.g. it is unsigned or an induction
variable),
LLVM does produce expected codegen. When std::abs is present, LLVM should be
able
to take advantage of the remainder operation being odd (i.e. -x % n = -(x %
n)).

The expected codegen is that of x % 2 != 0, x & 1, where LLVM generates a
single
and instruction.

Godbolt comparison of these expressions: https://godbolt.org/z/1YPW1z4v7

-- 
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/20210329/7a0b5cbb/attachment.html>


More information about the llvm-bugs mailing list