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

    <tr>
        <th>Summary</th>
        <td>
            miscompile related to coalescing stores in AArch64 SDAG backend
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            backend:AArch64,
            llvm:codegen,
            miscompilation,
            llvm:SelectionDAG
      </td>
    </tr>

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

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

<pre>
    here's a function:
```llvm
define void @f(i16 %0, ptr %1) {
  %3 = trunc i16 %0 to i8
  %4 = trunc i16 %0 to i14
  %new0 = lshr i14 %4, 8
 store i8 %3, ptr %1, align 1
  %5 = getelementptr i8, ptr %1, i64 1
 %6 = trunc i14 %new0 to i8
  store i8 %6, ptr %5, align 1
  ret void
}
```

during codegen, the two 8-bit stores are getting merged:
```
_f:    
        strh    w0, [x1]
        ret
```

but the backend seems to miss that the trunc changed the value, it dropped those 2 high bits. 
I also have versions of this that improperly merge 16-bit stores and 32-bit stores.

we can demonstrate using this driver:
```c
#include <stdio.h>

void f(unsigned short, unsigned char *);

int main(void) {
  unsigned char mem[8];

  for (int i=0; i<8; ++i)
    mem[i] = 0xee;

  f(0xFFFF, mem);

  for (int i=0; i<8; ++i)
    printf("%x\n", mem[i]);
}
```

```
Johns-MacBook-Pro:tmp regehr$ ~/llvm-project/for-alive/bin/llc foo.ll
Johns-MacBook-Pro:tmp regehr$ clang driver.c foo.s && ./a.out 
ff
ff
ee
ee
ee
ee
ee
ee
Johns-MacBook-Pro:tmp regehr$ 
```

in contrast, global isel properly notices that those high bits were chopped off:
```
Johns-MacBook-Pro:tmp regehr$ ~/llvm-project/for-alive/bin/llc foo.ll -global-isel
Johns-MacBook-Pro:tmp regehr$ clang driver.c foo.s && ./a.out 
ff
3f
ee
ee
ee
ee
ee
ee
```

cc @hatsunespica

</pre>
<img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