[llvm-bugs] [Bug 51014] New: Failure to merge div(x, y) with sub(x, mul(div(x, y), y)) to divrem(x, y)

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Jul 7 09:26:47 PDT 2021


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

            Bug ID: 51014
           Summary: Failure to merge div(x,y) with sub(x,mul(div(x,y),y))
                    to divrem(x,y)
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Common Code Generator Code
          Assignee: unassignedbugs at nondot.org
          Reporter: llvm-dev at redking.me.uk
                CC: lebedev.ri at gmail.com, llvm-bugs at lists.llvm.org,
                    spatel+llvm at rotateright.com

(There's a number of similar divrem bugs but nothing covering exactly the same
issues....)

https://c.godbolt.org/z/a3YMExMqh

https://alive2.llvm.org/ce/z/2GgRX4

We fail to convert this to srem:

// only need a srem - should we do this in instcombine?
int sdivrem(int x, int y) {
    int d = x / y;
    int r = d - (d * y);
    return r;
}

or make use of X86's divide instructions (or many divrem libcalls) also
providing the remainder:

#include <utility>

auto sdivrem_both(int x, int y) {
    int d = x / y;
    int r = d - (d * y);
    return std::make_pair(d, r);
}

----------------------------------------
define i8 @src(i8 %a0, i8 %a1) {
%0:
  %sd = sdiv i8 %a0, %a1
  %mul = mul i8 %sd, %a1
  %res = sub i8 %a0, %mul
  ret i8 %res
}
=>
define i8 @tgt(i8 %a0, i8 %a1) {
%0:
  %sd = srem i8 %a0, %a1
  ret i8 %sd
}
Transformation seems to be correct!

-- 
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/20210707/83a0b951/attachment-0001.html>


More information about the llvm-bugs mailing list