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

    <tr>
        <th>Summary</th>
        <td>
            [Clang] Inconsistent optimization around constant 1.0f
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

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

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

<pre>
    Given a simple switch which uses the constant 1.0f in case 0, the optimization is different than with any another constant, e.g. 0.0f.

```
#include <utility>

auto Switch(long i, float f)
{
    switch(i)
 {
    case 0:
        return CASE_O * f;
    case 1:
 return 2.0f * f;
    case 2:
        return 3.0f * f;
    case 3:
        return 5.0f * f;
    case 4:
        return 7.0f * f;
 case 5:
        return 11.0f * f;
    case 6:
        return 13.0f * f;
    case 7:
        return 17.0f * f;
    }

 std::unreachable();
}
```
Compiled with `-std=c++23 -O3 -DCASE_O=1.0f` and `-std=c++23 -O3 -DCASE_O=0.0f` respectively. The former leads to a jump for each of the 8 cases, while the latter is able to optimize to a single multiplication with a constant defined in a table. The latter appears to be more efficient, especially if it wasn't 8 different cases but 256 or more.

It seems to me that the 1.0f * f is optimized to f before the switch could optimized to table[i] * f. This also happens with -1.0f.

See https://godbolt.org/z/K4EdfzW4q

Side note: The example above is a simplified reproducer of what I observed in a more complex scenario, so optimizing it myself to table[i] * f is not a solution I can use.


</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMVV1v6jgQ_TXmZQRKnISQBx5oKatqH-5DV9rHlRNPyNx17KztwKW_fmUnLaUq1UWID2fOmTnzZeEcHTXilhUPrNgvxOg7Y7f_CupbK2hRG3nZ_kEn1CDAUT8oBHcm33Rw7qjpYHTowHcIjdHOC-0hXSUtkIZGOISE8cf42AyeenoVnowGciCpbdGi9uA7oeFMvgOhLyC08R3ad7qAx9VxBckqaVcs2bNkN3-uk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