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

    <tr>
        <th>Summary</th>
        <td>
            bad AArch64 codegen vs GCC, missed opportunity to coalesce 8bit values into wider words
        </td>
    </tr>

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

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

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

<pre>
    See the function `foo` in: https://godbolt.org/z/fx9jn9x86

The `foo` function is compiled to 31 instructions by Clang, vs only 24 instructions by GCC:

```c
// just a triple of uint8's + 4-byte align
typedef struct S {
  uint8_t a, b, c;
  uint32_t i;  // just to align this struct
} S;

// Naive if-else chain of triple comparisons
int foo(const S* s) {
  if (s->a == 4 && s->b == 3 && s->c == 2) return 12;
  if (s->a == 2 && s->b == 6 && s->c == 1) return 7;
  if (s->a == 5 && s->b == 1 && s->c == 5) return 8;
  return 0;
}
```

GCC uses a 16-bit load instruction to load both `s->a` and `s->b` at once, and immediately starts performing comparisons on that 16-bit value.

Clang loads and compares 8-bit values separately.

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyFVE1zmzAQ_TVw2bEHBObjwMGJm956Se8dSSxGqYwYSThxf31X4MQk9UxnZD7eet--J-0iTHtpnhHB9wjdNEivzABRkXTG0BXUEGV76L0fHT1E7InW0bTCaL819khvf-jXvdUvQ_1WFVFyiJL9cv1JjDeiD27lQJrTqDS24A1kKdVw3k5z1IG4wKPmAzE_wtmBGfQFWP7Pf74_PgY9q3JUZFny-j5rhZfJeeDgrRo1gulgUoOvIlY6iNgD5Btx8Qhcq-Ow5PnLiC12sNSDZ4jKhyUCS-4v4gvqRLjIKPsUzRiFFYEAawFkdC5B20z-F-qrzPIAzx8kn6T_4OqMoLoNaocge66GYOBqJWwit8rRhixZVB3CZrNKEkbKI7YHF7F67UB1pKtymyj7xiHKDrQgJ6igBTMs3uHsEyzfYRYYLfrJDpCylf171Ow-dXGfOl1Rl_9h3t1nTu8z71bM1Yr5CiW3AygPX9ppfTDUdTA5dNRQabERyoM2vF03ZzjpGRPG96H9F9VhAvjQfgBiBjy1t8TQRiGmTidsFfdIHe88t97BiLYz9qSG4_q4IZTpKfuq4cz1hNu1znmCZh1upl6SSXd1S6A2RALnetfkGJu0KMo8SZK8itsma-us5rFXXmMjyNR-b2Vf5MTX4hGHMKBhEMnASTlHA23G0Vg_Dcpfwk5IwzU6iVCtylKfGnhVLVp4NbZ18WR18-ULo3w_iS3Jphetz--3zWjNC9LosCeqR2T0sKvKooz7pi5FWqRClEnZtXWd80pmkhUlrxkXbZnGmgsapCbaPUSMCS5_49BSxasnwoIPui1WNmb06qT-8HCqIbg7xKphCWNpmrA0z2mLtphwLqTkskzzrs5klCd44kpvg9jwfYxtM-sW09FRUCvn3S3InaNvAuKsifj55HtjG_HCpRHx7LCZ7f0FnhirYw">