<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/129141>129141</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Miscompile with GVN load PRE
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
annamthomas
</td>
</tr>
</table>
<pre>
Given this example: https://godbolt.org/z/Mrd6jn344,
we see that GVN miscompiles by PRE'ing the load out of the loop. However, the load address is rewritten after some iterations in the loop by the store following it, so PRE'ing the load is incorrect.
If we use MemorySSA with GVN, the load is not PRE'd. Also, with LICM, we do not hoist the load since we state that the store invalidates the loaded pointer.
IMO, it looks like https://github.com/llvm/llvm-project/blob/63ecb0135d1c6457f82fc0e717d4fa8cdf0ee8e0/llvm/lib/Analysis/MemoryDependenceAnalysis.cpp#L339 is the issue.`canSkipClobberingStore` does not consider the fact that the store maybe overlapping with the Load's pointer (i.e. the MemLoc passed into `canSkipClobberingStore`). Circumventing this function also avoids the miscompile.
This is the source code (I've simplified the IR to just the 1st loop within that function):
https://github.com/andikleen/snappy-c/blob/master/snappy.c#L421
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyUVE1v4zYQ_TX0ZbCCRCmWddDBza63AeJ2kRS9U-TImoQiBQ5l1_31BWXH3i6KBQoYsIj54Js371Ex08EhtuLhF_HweaXmOPjQKufUGAc_Kl513pzbr3REB3EgBvxLjZNFUW5hiHFiUW6F3Am5O3jTeRszHw5C7v4WcrcPZv3myqoS8lHk2xMCI0IcVISvf_4GI7H240QWGbozfHv5ImRN7gBxQLBeGfBzBN9fz37K4Fd_wiMGIR_vScqYgMxADAFPgWJEB6qPGID9iEARg4rkHQO5W690Y_rm6ANC7631p3Q3xdSc_X_BodRB-xBQxwxEvr38nno4IcDMCHscfTi_vm7hRHFIU_4LKjE4H6-tTQZbyz4lLMnPT4_75YBg_JI3eOJ4r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