<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </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 --- - Lowering operations to compiler intrinsics with reference parameters generates bad code"
   href="https://llvm.org/bugs/show_bug.cgi?id=31363">31363</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Lowering operations to compiler intrinsics with reference parameters generates bad code
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>3.9
          </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>Common Code Generator Code
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>simonas+llvm.org@kazlauskas.me
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Consider this snippet of LLVM IR:

```llvm
target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-windows-msvc"

; Function Attrs: nounwind uwtable
define void @test3(i128* noalias nocapture sret dereferenceable(16), i128*
noalias readonly nocapture dereferenceable(16), i128* noalias readonly
nocapture dereferenceable(16)) unnamed_addr #0 {
start:
  %3 = load i128, i128* %1
  %4 = load i128, i128* %2
  %5 = udiv i128 %3, %4
  store i128 %5, i128* %0
  ret void
}

attributes #0 = { nounwind uwtable }
```

This generates following assembly:

```asm
test3:
    push    rsi
    .seh_pushreg 6
    sub    rsp, 64
    .seh_stackalloc 64
    .seh_endprologue
    mov    rsi, rcx
    mov    r9, qword ptr [rdx]
    mov    rcx, qword ptr [rdx + 8]
    mov    rdx, qword ptr [r8]
    mov    rax, qword ptr [r8 + 8]
    mov    qword ptr [rsp + 56], rcx
    mov    qword ptr [rsp + 48], r9
    mov    qword ptr [rsp + 40], rax
    mov    qword ptr [rsp + 32], rdx
    lea    rcx, [rsp + 48]
    lea    rdx, [rsp + 32]
    call    __udivti3
    pshufd    xmm1, xmm0, 78          # xmm1 = xmm0[2,3,0,1]
    movq    qword ptr [rsi], xmm0
    movq    qword ptr [rsi + 8], xmm1
    mov    rax, rsi
    add    rsp, 64
    pop    rsi
    ret
    .seh_handlerdata
    .text
    .seh_endproc
```

noting that LLVM thinks __udivti3 looks like this when targetting windows:

```
declare i128 @__udivti3(i128* readonly nocapture dereferenceable(16), i128*
readonly nocapture dereferenceable(16));
```

its easy to see that there’s way too much stack trashing going on and the
assembly instead could be just this:

```
test3:
    push    rsi
    .seh_pushreg 6
    sub    rsp, 32
    .seh_stackalloc 32
    .seh_endprologue
    mov    rsi, rcx
    mov    rcx, rdx
    mov    rdx, r8
    call    __udivti3
    pshufd    xmm1, xmm0, 78          # xmm1 = xmm0[2,3,0,1]
    movq    qword ptr [rsi], xmm0
    movq    qword ptr [rsi + 8], xmm1
    mov    rax, rsi
    add    rsp, 32
    pop    rsi
    ret
    .seh_handlerdata
    .text
    .seh_endproc
```

which only does write to the sret parameter, as one would expect from a well
optimised code.</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>