<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - [InstCombine] ashr disguised as lshr+signmask"
   href="https://bugs.llvm.org/show_bug.cgi?id=43705">43705</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[InstCombine] ashr disguised as lshr+signmask
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Scalar Optimizations
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>lebedev.ri@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>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.)

<a href="https://godbolt.org/z/XPn2Dw">https://godbolt.org/z/XPn2Dw</a>

#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:

<a href="https://rise4fun.com/Alive/USf">https://rise4fun.com/Alive/USf</a>

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</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>