[llvm-bugs] [Bug 43705] New: [InstCombine] ashr disguised as lshr+signmask

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Oct 17 12:47:29 PDT 2019


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

            Bug ID: 43705
           Summary: [InstCombine] ashr disguised as lshr+signmask
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: lebedev.ri at gmail.com
                CC: llvm-bugs at lists.llvm.org

I don't really personally need this, but i just noticed that we don't do this
fold,
so documenting. One might write such a monstrosity because technically,
arithmetic right shift is implementation defined until C++20 (don't know about
C.)

https://godbolt.org/z/XPn2Dw

#include <cstdint>
int t0(unsigned x, unsigned y) {
    uint32_t bottom = x >> y;
    uint32_t top = (-(x >> 31)) << (32 - y);
    return top | bottom;
}
int t1(unsigned x, unsigned y) {
    uint32_t bottom = x >> y;
    uint32_t top = (-(x >> 31)) & (-1U << (32 - y));
    return top | bottom;
}
int t2(unsigned x) {
    uint32_t bottom = x >> 4;
    uint32_t top = (-(x >> 31)) << (32 - 4);
    return top | bottom;
}
int t3(unsigned x) {
    uint32_t bottom = x >> 4;
    uint32_t top = (-(x >> 31)) & (-1UL << (32 - 4));
    return top | bottom;
}

These are all should be just arithmetic right-shift:

https://rise4fun.com/Alive/USf

Name: ashr disguised
  %3 = lshr i32 %x, %1
  %4 = ashr i32 %x, 31
  %5 = sub i32 32, %1
  %6 = shl i32 %4, %5
  %r = or i32 %6, %3
=>
  %r = ashr i32 %x, %1

Name: ashr disguised, with mask
  %3 = lshr i32 %x, %1
  %4 = ashr i32 %x, 31
  %5 = sub i32 32, %1
  %6 = shl i32 -1, %5
  %7 = and i32 %6, %4
  %r = or i32 %7, %3
=>
  %r = ashr i32 %x, %1

Name: ashr disguised, constants
Pre: C2 u>= C1 && C3 == C2-C1
  %2 = lshr i32 %x, C1
  %3 = ashr i32 %x, C2
  %4 = shl i32 %3, C3
  %r = or i32 %4, %2
=>
  %r = ashr i32 %x, C1

Name: ashr disguised, constants, with mask
Pre: C2 u>= C1 && countLeadingOnes(C3) == C1 && countTrailingZeros(C3) == 32-C1
  %2 = lshr i32 %x, C1
  %3 = ashr i32 %x, C2
  %4 = and i32 %3, C3
  %r = or i32 %4, %2
=>
  %r = ashr i32 %x, C1

-- 
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/20191017/8db6e1fe/attachment.html>


More information about the llvm-bugs mailing list