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: unassignedb...@nondot.org
          Reporter: rif...@purdue.edu
                CC: llvm-bugs@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.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to