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

    <tr>
        <th>Summary</th>
        <td>
            [DAGCombiner] WRONG code (visitSTORE)
        </td>
    </tr>

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

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

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

<pre>
    [testcase.tar.gz](https://github.com/llvm/llvm-project/files/9966027/testcase.tar.gz)

This program is supposed to print some '0's:

```
struct a {
  short b;
  unsigned c;
} e = {9, 0}, *f = &e;

unsigned d = 0;
void h(struct a i) {
  *f = i;
  e.b = 0;
  d = i.c;
}

int main() {
  h(e);
  for (int g = 0; g < 10; g++)
    printf("%d\n", e.b);
}
```

It instead prints some '9's with these options:
clang -march=arch13 -O3 wrong0.i -o a.out -mllvm -pre-RA-sched=list-ilp -mllvm -disable-sched-live-uses=false

(The extra options are for the scheduler which are needed to expose an earlier incorrect transformation by DAGCombiner.)

The initial DAG is created with the storing of the '0' chained after the previous two stores to the 'e' struct:

  ```
t13: ch = store<(store (s32) into %ir.1)> t12, t8, t11, undef:i64
  t27: i64 = SystemZISD::PCREL_WRAPPER TargetGlobalAddress:i64<ptr @printf> 0
            t15: i64 = add nuw t11, Constant:i64<4>
          t16: ch = store<(store (s32) into %ir.i.sroa.2.0..sroa_idx.i)> t13, t9, t15, undef:i64
        t18: ch = store<(store (s16) into @e, align 8, !tbaa !14)> t16, Constant:i16<0>, GlobalAddress:i64<ptr @e> 0, undef:i64
        t55: ch = store<(store (s32) into @d, !tbaa !11)> t13, t9, GlobalAddress:i64<ptr @d> 0, undef:i64
```
Here t18 does not alias t16 so it is effectively removed from the ordering deps of t18, which is correct. This is done by DAGCombiner::FindBetterChain() / GatherAllAliases() which returns BetterChain: t13. After this t18 is chained only after t13, and not t16.

Then t16 is visited by DAGCombiner::visitSTORE() : mergeTruncStores(ST)

Combining: t16: ch = store<(store (s32) into %ir.i.sroa.2.0..sroa_idx.i)> t13, t9, t15, undef:i64
 ... into: t60: ch = store<(store (s64) into %ir.1, align 4)> t12, t4, t11, undef:i64

This is problematic as the first optimization of t18 was based on t16 not aliasing (as long as it was only writing 32 bits), which now t60 does (it writes its original 32 bits and now also the same bits as the constant '0' is supposed to end up in)!

 ```
 t13: ch = store<(store (s32) into %ir.1)> t12, t8, t11, undef:i64
  t27: i64 = SystemZISD::PCREL_WRAPPER TargetGlobalAddress:i64<ptr @printf> 0
          t9: i32 = truncate t4
        t55: ch = store<(store (s32) into @d, !tbaa !11)> t13, t9, GlobalAddress:i64<ptr @d> 0, undef:i64
        t60: ch = store<(store (s64) into %ir.1, align 4)> t12, t4, t11, undef:i64
        t57: ch = store<(store (s16) into @e, align 8, !tbaa !14)> t13, Constant:i16<0>, GlobalAddress:i64<ptr @e> 0, undef:i64
```

The end result is that the constant 0 stored by t18/t57 which must come last ends up going before t60 in the final schedule, giving an incorrect result.


</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzdV01v2zgQ_TXyhbCgD8uWDj44dpLtYrENkgAFeikokZK4kEmDpOKmv35nSFm21TZFD91dbOBIFMmZeRy-mSFLxV7XQXZjubEVNTy0VIfNlyDbBUneWnswQboJkjv4NcK2fRlWag8fXfdyes0PWv3FKwuftei4gXdRLJdRsoLWVG9SBNEuiDb--dwKQ0C80XRPoGn6w0EZzohV0C2kJUbtOQmSVQT_DsqFdLCMhp_7NFb3lSWUBKsb30OIaZW2pAzSsaeXRjQSTFRjZ7DaETCS7lCyCJItiaAL30Gyqf1AsuTn-e45KmJuRjQOvyjBSAvuGwEJWPYlqlGtuADGw3KiiAyqRXiF9RIEumhPhQRrExsIgKO7z9pqpcF0jjLNaMo1tyT27SC5cb_iJEP8PtTOQBIkGQuyrXTNLUK-tHDGdr0x_vnOEiGN5ZR5lWbc2wL3lhyB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