[llvm-bugs] [Bug 43432] New: Division by a constant could be optimized for known variables value range

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Sep 24 08:01:21 PDT 2019


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

            Bug ID: 43432
           Summary: Division by a constant could be optimized for known
                    variables value range
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: LLVM Codegen
          Assignee: unassignedclangbugs at nondot.org
          Reporter: zamazan4ik at tut.by
                CC: llvm-bugs at lists.llvm.org, neeilans at live.com,
                    richard-llvm at metafoo.co.uk

Consider the example:

unsigned long long kBorder = (1ull<<62);

unsigned long long sample(unsigned long long m) {
    if (m >= kBorder) __builtin_unreachable();
    return m / 10;
}

It produces the following assembly:

sample(unsigned long long):
  movabs rdx, -3689348814741910323
  mov rax, rdi
  mul rdx
  mov rax, rdx
  shr rax, 3
  ret

However, knowing that the higher bits are always 0, the constant could be
adjusted to avoid the `shr rax, 3`:

sample(unsigned long long):
  movabs rax, 1844674407370955162
  mul rdi
  mov rax, rdx
  ret

Godbolt playground: https://godbolt.org/z/YU2yAC

This issue is probably related to https://bugs.llvm.org/show_bug.cgi?id=43431

Same issue for GCC: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91883

P.S.: that optimization is important for std::to_chars(..., double) like
functions, where a significant of a double is extracted into an unsigned long
long variable, so its upper bits are always zero.

-- 
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/20190924/4c7d325c/attachment-0001.html>


More information about the llvm-bugs mailing list