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

    <tr>
        <th>Summary</th>
        <td>
            Inefficient code generation for constexpr structs in switch statements
        </td>
    </tr>

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

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

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

<pre>
    In [https://godbolt.org/z/6r1M5PbnW](url) an attempt to generate a few constexpr classes results in two look up tables being generated, when one should be enough
```
struct A{
    int a; int b;
    constexpr A(int A, int B) : a{A}, b{B} {}
};

A z(int a) {
    switch (a) {
        case 0: return A{16, (int)sizeof(uint8_t)};
        case 1: return A{8, (int)sizeof(float)};
        case 2: return A{8, (int)sizeof(uint16_t)};
        case 3: return A{16, (int)sizeof(uint32_t)};
        default:
             return A{1,1}; 
    }
}
```
This happens both on arm and intel backends.
It would be also worthwhile to consider placing the default return value on memory as well (this may depend on the selected immediates) as in
```
A z_optimised(int a) {
   static constexpr A table[5]={ A{16,1}, A{8,4}, A{8,2}, A{16,4},A{1,1}};
   uint32_t A = a;
   if (A >= 4) A = 4;
 return table[A];
}
```
which is of course a reasonable self made optimisation and a work-around.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVUGPszYQ_TXOZbQRmADhwAG-NNIeKvVQqcdPxh6Cu8ZGtlm6--urIckm2W5XbRQl2GO_eTPzZhAh6JNFrFnesvywEXMcnK9nKw2KMG46p97qZwssb4cYp8CyhvEj48eTU50zcev8ifHjO-PHwqe_5r919g-WHxjfz94wXoGwIGLEcYoQHZzQohcRQUCPC0hnQ8S_Jg_SiBAwgMcwmxhAW4iLA-PcC8wTRNEZDNChtqcPEMX4D1gGtOAsQhjcbBR0CGjdfBpYcmBJw4rk8l2XIfpZRmhY2Z43AAC0jSBY1q4PHcvuTDeCDeN7sjfkl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