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

    <tr>
        <th>Summary</th>
        <td>
            Load unnecessarily happens before malloc
        </td>
    </tr>

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

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

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

<pre>
    The following code will be compiled with the load of `next` happening before the call to `malloc`. This generates worse code than if the load is delayed until after the call to `malloc`.

```c
struct Foo {
    Foo* next;
};
void ctx_push(Foo* f) {
    Foo tmp = { f->next };
    Foo *n = (Foo*)malloc(sizeof(Foo));
    *n = tmp;
    f->next = n;
}
```

Manually moving the load in this example improves the generated code:
```c
struct Foo {
    Foo* next;
};

void ctx_push(Foo* f) {
    Foo *n = (Foo*)malloc(sizeof(Foo));
    Foo tmp = { f->next };
    *n = tmp;
    f->next = n;
}
```

https://gcc.godbolt.org/z/jKvWEfvdc
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJytU02PmzAQ_TXmYi0CQ_g4cNhVdi9tbyv1WBl7AEfGRtiQZH99B0g22VaVqnaRgfHM-D37Pbm28ly9dkAbq7U9KtNSYSXQo9Ka1oCTflAaJCZ8Rz02assltQ0lWWTg5PFHOz4MYJa1NTR2hLVPcETwdunrMbQCg5C-dsrRFgyM3IOjRzs62Bh9xw1VzY0DGyVofkbyyXilKW88jH_GJtGeRI-XbxZtQ2xz58dJePpicU3-tOUoPpgg7JGuB0kueZLv3-PZKkmFP_0YJtcRVlz6G8LK34Co7wdKkv1SoM0DSZ4XWHoPd-1EDLO1XiER8HISVjj1Bra5lspl3AO8L0a-D4U7TqyaDwf6qMq9VN-4mZD5THs7Lxbe9DcYowlw4v2ggap-GO2Mpi0dVwvlah5JPln3f1D_vzT9e_s-Wf3O-8Et8rEXHK0QYWtlbbUP7dhi5g3fw5f5-3MzSxFAFWdZWrA0ZXEgq0SWSckDr7yG6uvi2WQMCHCOjwod3e6lu97KTYxgGnX1Cy3e7akO8a7jROv5-ntAww8gPE6VcxM4DHZFzIqgq9Jc1rmIs4JDljGRprtMsjJnSZkwUeZZoHkN2lVk90QYM3CkKwTGZLcPVMUixuIoSuIyTtI4jPM6ihlPymhXR5mQJI2g50qHyz4WLYKxWrdUT63DolbOu1uRO6daA7DSIT6ffGfH6jD2Exqvg5W7Wvf-E51LcG0">