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

    <tr>
        <th>Summary</th>
        <td>
            Poor code when a pointer is a global var, but good code for static or local var
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            llvm:core
      </td>
    </tr>

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

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

<pre>
    If you add "static" before "ptr" below, or make it a local var in function main() it generates 3 store instruction storing constant integer data. If you leave it as-is it generates pretty poor code. Every element in each struct is written to and there is no padding in any struct so I don't see why it would behave differently for a global.

#include "stdlib.h"
#include "stdint.h"

typedef union reg1_t {
  uint32_t val;
  struct {
    uint32_t name1 :13;
    uint32_t name2 :6;
    uint32_t name3 :9;
    uint32_t name4 :4;
  } field;
} reg1_t; 

typedef union reg2_t {
  uint32_t val;
  struct {
    uint32_t name1 :7;
    uint32_t name2 :13;
    uint32_t name3 :12;
  } field;
} reg2_t; 

typedef union reg3_t {
  uint32_t val;
  struct{
    uint32_t name1 :13;
    uint32_t name2 :6;
    uint32_t name3 :9;
    uint32_t name4 :4;
  } field;
} reg3_t; 

typedef struct hw_reg {
  reg1_t reg1;
  reg2_t reg2;
  reg3_t reg3;
} hw_reg;

hw_reg *ptr = (hw_reg*) 0x90000000;

int main() {
  ptr->reg1.field.name1=3;
  ptr->reg1.field.name2=3;
  ptr->reg1.field.name3=3;
  ptr->reg1.field.name4=3;

  ptr->reg2.field.name1=3;
  ptr->reg2.field.name2=3;
  ptr->reg2.field.name3=3;

  ptr->reg3.field.name1=3;
  ptr->reg3.field.name2=3;
  ptr->reg3.field.name3=3;
  ptr->reg3.field.name4=3;
  return 0;
}
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzVVU2PmzAQ_TVwGS0Cm3wdOGS7u9Le-g8qgwdw69iRbZLm33cMyZZtm5ZKvTSKsJl5npk3Nn61lZfqtYWLHUBICQljPoigGppAja11GG3H4CaDtueEfQDr4CC-IKgAArRthIaTcKAMtINpgrKG_MokbJuwXUR1aNCJgB44-BCjKuODGyZstCjTQWPJKEwgZ8AOHUgRRAbX-jSK05TSPyj_PurRYQgXOFqqrLESM3g-obsAajzgGBBQND1MOYGWn50KAQ0EC8JICD3GojwYC0fqRCyHFglzua3xFl5BWiK1oRdEOPeXWMTZDlpSa_pYnVRtS4FM0Beg5lF3Om1robMkf0ry_fXJuDKNHiRO_ZZa1VlP0ztu6sbcPT7D5YgSWxhMbKDDrvgUINk8Tl6AgRZxRraT0Al_M1-5zIAzqBEHLCDh-4LPlvwAYBGwvu_n0b-77y-jv5z5k80TtAq1fLNFy8SITPBb1uwfst78gfTvujKyLtgSWmwBLf4XtP6LveT3SV83pz9_Ityc9fVQx2GW4rrpcXhv5ZOVv0s9Rf1uGp-3VGxP9xpReKLp9opk-3hj5V93-fT7YSmxn99ss2op1EPCn2O12diCbNwCij7fgF-j2CIUX4Qq36F-xrJF1bFF1bG71f2M5Yvy8kV5-aKu8LtdieclDM5APj8sKVbFer1d8y0vylRWXO74TqRBBY3Vx5uy0LVPqiFIaqJIuSgZt0s-amBUx3ogabJWTvgoA5OmRtl8E8t0cLrqQzh6-oYS9kL_ToV-qLPGHuhF69NteDg6-xnpS2cvyvsBPU1Wu7zkaV-1a7ZiuEFW7LDJV_lqW67rbbMpil1Zrtgm1YJU21fJ6pEUZAzK9w0JcNST1VOqKpYzVhTFmjEiXmbbUuTbpmnrUsrVpi6SMkc68TqLazPrutRVY1H10HlyauWD_-4U3qvOII4JKb4YQm9ddRDui296dTigTkcS1cjgGzJLgFE">