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

    <tr>
        <th>Summary</th>
        <td>
            Use three-operand LEA for select of constants on some x86 architectures
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            backend:X86,
            missed-optimization
      </td>
    </tr>

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

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

<pre>
    Compile:

```
// clang -O2 -march=znver3
unsigned select_unsigned_lt_10_8_13(unsigned X) {
  return X < 10 ? 8 : 13;
}
```

I get:

```
  31 c0                      xor    %eax,%eax
  83 ff 0a                   cmp $0xa,%edi
  0f 93 c0                   setae  %al
  8d 04 80 lea    (%rax,%rax,4),%eax
  83 c8 08                   or $0x8,%eax
```

We could generate:

```
  31 c0 xor    %eax,%eax
  83 ff 0a                   cmp    $0xa,%edi
 0f 93 c0                   setae  %al
  8d 44 80 08                lea 8(%rax,%rax,4),%eax
```

saving one instruction and 2 bytes on those x86 architectures where three-operand LEAs are not discouraged.

I thought the x86 backend might be intentionally avoiding the three-operand LEA for `-march=znver3`, but that doesn't seem to be the case.  For `return X < 10 ? 9 : 12`, I get:

```
  31 c0                      xor %eax,%eax
  83 ff 0a                   cmp    $0xa,%edi
  0f 93 c0 setae  %al
  8d 44 40 09                lea 0x9(%rax,%rax,2),%eax
```

Even on those x86 architectures where three-operand LEAs are discouraged, this optimization might be useful for size optimization purposes.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVcuO4zYQ_Brq0rBBkbIsH3SYHa-BAAFyCjI3o022JGYk0iApxzNfH1CyZ1_aZJMdw9DD7uoqFqtBDMG0lqhmmw9ss89wjJ3z9TO-jhGfsTMeI2Ynp1_qRzecTU9MPjC-Z_x-LfntO7-KAxMHUD3aFla_CVgN6FXH5P7VXsjLuWq0E6uGQD2peLy_H_t4zPmxOuaSieqt6omJHbDthxkM4CmO3sITMPkIOQcmD1ABkw-QSyZvZWy7X1Y4XX-BluI_LwVA5qA4LH6uzqcbExvCKxOPt4cbsJLQNMBxAaiGMzBR8CveUNrcUbyBnVxmDBSRJjrs30g08AIqDj3hrKViYuPvcuaHgondkjxVAa8WiJyf1VVfgRZN_INAubHX0JIlj_FfsnE39Ke8m4BL9v1394rJvW9tSH5WP-rmojEBL8a24CyBsSH6UUXjLKDVIOD0EimAsxA7FwiuVQlpREwkFUdPAf7qyBPEzhOt3Jl8gv368SEAegLrImgTlBs9tqTXX4Y6dm5suwixmxufUD2T1TCY9OspyYlkkxjs-xfAizM6KU313xBCk9JQ8q9HuORMPMJpTDQYQTsKlolthEA0QHSJJzVUGGgNcJi7LA3tbh5acWv5HkP5zqn6FKvvh6jgwHdLIeLX3WKMxA_G6OOF7P8OymchSd7GzgRw52gG84pTGt8yMQZqxn7a7WBe6cuq8-jPLlBYZ7qWeid3mFGdl9uq3JZ8W2ZdTTwXokGteXNC1fBCbXKlEZXCqtJYZKYWXEguc8nFppDluuAaN5Q3JCjfcalYwWlA06_7_jKsnW8zE8JIdZnLcpP1eKI-TAeUELdAM_nwVJVMiMlGMZgQSK8-F57-2-wzX6eeq9PYBlbw3oQYPrFEE3uqfw_fy_58OoFrQDkbIto4zW1ww8JuZKPv6y7Gc0gJno7B1sRuPK2VG5g4JNbbbXX27k9SkYnDtNDAxGFa698BAAD__10oGx0">