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

    <tr>
        <th>Summary</th>
        <td>
            x86-64 optimization: (x <= 0xFFFFF) in '-Os' mode can convert to ((x >> 20) == 0)
        </td>
    </tr>

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

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

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

<pre>
    Test code

```c
#include <stdbool.h>
#include <stdint.h>

bool func1(uint32_t x) {
    return x < 0x100000U;
}

bool func2(uint32_t x) {
    return ((x >> 20) == 0);
}

bool func3(uint32_t x) {
    return ((x / 0x100000U) == 0);
}
```

The three functions are equivalent. Specifically the threshold constant to be compared with is a power-of-two. If there is no shortage of temporary registers, then the right shift version (that is, `func2`) will likely make the smallest code.

My expected result is like this (assuming `-Os` optimization, not `-O2`)

```x86asm
        shrl    $20, %edi
 sete    %al
        retq
```

Instead I got this (x86-64 clang 20.1.0 with `-Os` option, tested in Compiler Explorer):

```x86asm
        cmpl $1048576, %edi
        setb    %al
        retq
```

(Clang can recognize the three functions are equivalent. It just made suboptimal code among the possible choices.)

Note that in RISC-V targets of Clang, it does use the right shift to reduce code size

```text
        srliw   a0, a0, 20
        seqz    a0, a0
        ret
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyUVEFz4zYP_TX0BWMNRVmSfdAh68QzOXxfZ7rbXjsUBUvcpUgtAcVOfn2HspMm2XbTcjSWNQAe8IBHaCLbe8RGlJ9EebvSMw8hNnfnyYWIUe5Wbegemy9IDCZ0KORNeip5eUz6UIX1xs0dgij2xF0bgssGUdz9ndF6frHJm-QKx9mbXKjtbD0X6g-Gs1A7EPUnIW8AACLyHD2cEwLIcy7T-U0UyS7q23dI6mMkobZCbRPenSjuQMnFq7gVxS2k__8EXfwHaHV4VepP4J87eU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