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

    <tr>
        <th>Summary</th>
        <td>
            Regression missed optimization with constant signed division going from version 9 to 10
        </td>
    </tr>

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

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

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

<pre>
    clang 10 and newer produce sub-optimal code when doing a signed floor division for the same llvm IR

I have 3 versions of floor_div that preform division with rounding towards -inf instead of 0
https://godbolt.org/z/dbfWhEdGY

```
long long xfloor_div_16(long long a)
{
     return (a + 0x8000000000000000ULL) / 16 - 0x8000000000000000 / 16;
}

long long yfloor_div_16(long long a)

    return (long long)((unsigned long long)(a) / 16 * 16) / 16;
}

long long zfloor_div_16(long long a)
{
    unsigned long long q = a; return (long long)(q - q % 16) / 16;
}
```

current versions of clang/llvm compile the first version to a single arithmetic shift right for power of 2 constants, whereas version 9 and older compile all three to optimal code even though both old and new versions produce the same llvm IR
current versions of gcc compile the the last 2 optimally, but not the first :-(

the first version gets optimized directly to optimal llvm IR 
```
%2 = ashr i64 %0, 4, !dbg !17
```
versions 2 & 3 both produce:
```
%2 = and i64 %0, -16, !dbg !23
%3 = sdiv i64 %2, 16, !dbg !24
```

which gets turned into a single x86 sar instruction in version <= 9 
```
sar     rax, 4
```
while it becomes an adjustment + conditional move before the sar in versions >= 10

```
and     rdi, -16
lea     rax, [rdi + 15]
test    rdi, rdi
cmovns  rax, rdi
sar     rax, 4
```



</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyVVVtvozgU_jXwcpQKDKTwwEMzna5GmqeRVqt9GhlswJXBGdskbX_9HptLSJu5bERMHJ_L58_fOa4Uey1rSYcW4gjowGDgZ67hqBUbaw5mrHbqaEVPJdSKcTh3fACmBDpQMKIdOINGKqWBiZMw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