<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 - incorrect conversion of sqrdmulh+sqsub to sqrdmlsh"
   href="https://bugs.llvm.org/show_bug.cgi?id=51563">51563</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>incorrect conversion of sqrdmulh+sqsub to sqrdmlsh
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </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>normal
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>LLVM Codegen
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>jzern@google.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk, sam.parker@arm.com, sjoerd.meijer@arm.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=25180" name="attach_25180" title="repro">attachment 25180</a> <a href="attachment.cgi?id=25180&action=edit" title="repro">[details]</a></span>
repro

The sequence of sqrdmulh followed by sqsub is being converted to sqrdmlsh which
shifts the rounding from the first multiply to the result causing an off by one
in some cases. gcc (tested with aarch64-linux-gnu-gcc (Debian 10.2.1-6+build2)
10.2.1 20210110) does not perform this conversion.

Reproduced with llvmorg-14-init-1836-ge4888be74e34 and releases going back to
3.9.1.

# repros with -O2/-O3, other levels aside from 0 untested.
$ clang -march=armv8.1-a -target aarch64-pc-linux-gnu -O2 \
  sqrdmulh_sqsub-sqrdmlsh-repro.c

See also:
<a href="https://godbolt.org/z/zhKnMT15s">https://godbolt.org/z/zhKnMT15s</a>
<a href="https://chromium.googlesource.com/codecs/libgav1/+/65bb0dae17596e88ec33cba5d53d1ce54e2fcfcf">https://chromium.googlesource.com/codecs/libgav1/+/65bb0dae17596e88ec33cba5d53d1ce54e2fcfcf</a>

non_opt:                                // @non_opt
        sub     sp, sp, #64                     // =64
        movi    v0.8h, #1, lsl #8
        str     q0, [sp, #48]
        movi    v0.8h, #1
        str     q0, [sp, #32]
        ldr     q0, [sp, #48]
        mov     w8, #42944
        dup     v1.8h, w8
        sqrdmulh        v0.8h, v0.8h, v1.8h
        str     q0, [sp, #16]
        ldr     q0, [sp, #32]
        ldr     q1, [sp, #16]
        sqsub   v0.8h, v0.8h, v1.8h
        str     q0, [sp]
        ldr     q0, [sp]
        add     sp, sp, #64                     // =64
        ret
opt:                                    // @opt
        mov     w8, #42944
        movi    v1.8h, #1, lsl #8
        dup     v2.8h, w8
        movi    v0.8h, #1
        sqrdmlsh        v0.8h, v1.8h, v2.8h
        ret</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>