<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/62430>62430</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Miscompilation in LoopStrengthReduce
</td>
</tr>
<tr>
<th>Labels</th>
<td>
miscompilation
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
d-makogon
</td>
</tr>
</table>
<pre>
Reproducer: https://godbolt.org/z/54r6nnccM
Alive2: https://alive2.llvm.org/ce/z/AmPCvh
LSR inserts a lot of IVs during code expansion for some SCEVs.
In the test loop `bb4` never takes its backedge, so effectively `%phi5` and `%shl` values are zeroes.
Then `@foo` is called with the argument equal to the value of `%phi` to the power of 6. On the first iteration it is zero, on the second iteration it is `4^6 = 4096`.
LSR replaces the `@foo` argument with the newly inserted `%lsr.iv21`. It is an expansion of some large SCEV expression with many addrecs. However it is inserted in the `bb4` loop, so effectively `%lsr.iv21` value is always zero. So in the version after LSR we always call `@foo` with argument equal to zero while in the initial test its value changes (on 2nd iter it's 4096).
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx0VE1v4zYQ_TXUZRBBpj4sH3RwkzUaYBctkmLvFDWS2FCkyqHsen99QUr2Jg32YgMz8968mSeOIFKDQWxY-RsrnxKx-NG6pnuYxJsdrEla212bF5yd7RaJjuVHGL2fieVHxk-MnwbbtVb71LqB8dMPxk9l4SpjpPzGsieWHY9anZF_BooYT7U-TxtY4sZwnP58PI8rfP39-voCyhA6TyBAWw-2h-fvBN3ilBlA2g4B_52FIWUN9NYB2Qnh9fHLd0pXjmcDfkTwSB60tTOwKmvbglUZGDyjAy_ekEB5glbIN-wGZPwRyAL2PUqvzqivAcR4OY-qDEBhui1Cow6Bs9ALEgiH8AOdxVvzv0Y0sbLIemtDpSKQQmvs4KL8GKUJNywTGg_4zyI0eBujkTLMe28d4Ftythd0IVml8Mc6YK8ceVAenfBhGcqHXkFNGMeuRYTSmu5TFauygpVfKmD5ExTZoWJVlv60wOGshUSKFB-muUu_D2Pwoq-baXjbkiaXqjPfBVp4ji2FeWec7VfftHDD6l5IOqSYjdyTMFcQXedQUgq_20v0bpV_76bMTeLmcDD8l26-U7VtO-jSF3Fd95bCq71RntFFLaL36CAs5YK32uDnx7VExZ9tDaRwGZXGG60yyquQxOgdbTrkKMyABIzX1gDfHAPlGd_TahA_pO9fStI1eXfIDyLBZlfVvKrrst4lYyPzftcX1f5Q7Dlmect3_JC3WVWJrq9K2Saq4RnPs4LXu5wfil3a1fUey_rQ5X2d7yWyIsNJKH1_s4kiWrCpeJFniRYtaoqHhPNJkbTTrHT8uBjn4bi4JgAf2mUgVmRakaefVF55jc23D7iwnK_Wzq_eoRn8-ILhCCWL083_jpDy49Km0k6MnwLl9vcwO_s3Ss_4KUolxk9R7X8BAAD__39OnRE">