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

    <tr>
        <th>Summary</th>
        <td>
            [AggrInstCombine] Consecutive stores not merged from unrolled loop
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            llvm:instcombine,
            missed-optimization
      </td>
    </tr>

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

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

<pre>
    AggressiveInstCombine doesn't merge stores from an unrolled loop. This is a pass ordering problem: Since #147540, AggressiveInstCombine does handle this case, but it only runs before the loop is unrolled and therefore never sees the consecutive stores.

```c
void src(char *d, char a, unsigned short b) {
    *d++ = a;
    for (unsigned i = 0; i < sizeof(b); i++)
        *d++ = b >> (i * 8);
}

void tgt(char *d, char a, unsigned short b) {
    *d++ = a;
 *d++ = b >> (0 * 8);
    *d++ = b >> (1 * 8);
}
```
LLVM-IR ([Godbolt](https://godbolt.org/z/35TrfrdWf)):
```llvm
define dso_local void @src(ptr noundef writeonly captures(none) initializes((0, 3)) %0, i8 noundef signext %1, i16 noundef zeroext %2) local_unnamed_addr #0 {
  store i8 %1, ptr %0, align 1
  %4 = getelementptr inbounds nuw i8, ptr %0, i64 1
  %5 = trunc i16 %2 to i8
  store i8 %5, ptr %4, align 1
  %6 = getelementptr inbounds nuw i8, ptr %0, i64 2
  %7 = lshr i16 %2, 8
  %8 = trunc nuw i16 %7 to i8
  store i8 %8, ptr %6, align 1
  ret void
}

define dso_local void @tgt(ptr noundef writeonly captures(none) initializes((0, 3)) %0, i8 noundef signext %1, i16 noundef zeroext %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds nuw i8, ptr %0, i64 1
  store i8 %1, ptr %0, align 1
  store i16 %2, ptr %4, align 1
  ret void
}
```

</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzUVU2P2zYQ_TWjy2AN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