<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/82169>82169</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Missed optimization: `divq` never gets emitted directly for 128-bit division, even when safe
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          Eisenwave
      </td>
    </tr>
</table>

<pre>
    ## Code to reproduce (https://godbolt.org/z/rM53K6crE)
```cpp
using u128 = unsigned __int128;
using u64 = unsigned long long;

u64 div(u128 x, u64 y) {
    __builtin_assume(x <= (u128{1} << 80));
    __builtin_assume(y >= (u64{1} << 40));
    return x / y;
}
```

## Actual output

```asm
div(unsigned __int128, unsigned long long): 
 push    rax
        xor     ecx, ecx
        call __udivti3@PLT
        pop     rcx
        ret
```

## Expected output

```asm
div(unsigned __int128, unsigned long long):
 mov rcx, rdx
        mov rdx, rdi
        mov rax, rsi
        div rcx
        ret
```

## Explanation

If the divisor is 2<sup>40</sup> or more, and the dividend is 2<sup>80</sup> or less, the result can be at most 2<sup>40</sup>, which always fits into 64 bits.

In this special case, it is safe to use `div` directly without going through `__udivti3` because `div` will never raise an exception (except for division by zero).

## Recommended solution

Add a special case to x86_64 instruction selection. Don't always call `__udivti3` but check whether it would be safe to use `div` directly.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0VV2P6jYQ_TXmZXRRcEISHvKwFxapaq9UVX1Hjj0Qt46d2mM-9tdXNuzuhd3bSpWKECaeOeNzzsS2CEEfLGLHll_ZcjMTkQbnu2cd0J7EEWe9U5eO8ZLxEtZOIZADj5N3KkoExtuBaAqsfGJ8y_j24FTvDM2dPzC-fWF8678ty59r6Z8ZX7Fiw4onVhfXr5ym60wM2h4gLngLrNxAtJmUgt1OW1rwlpVf7xLr6j7POHvIP2-Jt_S6AqWPjLe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