<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/70651>70651</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[JumpThreading] Incorrect elimination of condition
</td>
</tr>
<tr>
<th>Labels</th>
<td>
miscompilation
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
nikic
</td>
</tr>
</table>
<pre>
The following test case is miscomiled by JumpThreading (https://llvm.godbolt.org/z/o9W6efoTf):
```llvm
define i64 @test(i64 noundef %v) {
entry:
%v.nonneg = icmp sgt i64 %v, -1
br label %for.body
for.body:
%sum = phi i64 [ 0, %entry ], [ %sum.next, %for.body ]
%sum.next = add i64 %sum, %v
%overflow = icmp ult i64 %sum.next, %sum
%cmp.i5.i = xor i1 %v.nonneg, %overflow
br i1 %cmp.i5.i, label %for.body, label %cleanup1.loopexit
cleanup1.loopexit:
%cmp.not.lcssa.ph = phi i64 [ %sum, %for.body ]
ret i64 %cmp.not.lcssa.ph
}
```
The `%overflow` condition is folded to `false` as part of `processBranchOnXOR()`.
I believe the root cause is that `computeValueKnownInPredecessorsImpl()` performs phi translation of one icmp operand (the phi node), while leaving the other alone, and then trying to simplify it. This means that we are working on icmp operands from two different loop iterations.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJxsVEGP4jgT_TXmUuooMSQNhxyGQUj9fYdZrVq7e3XsMvGO44psB5r99Ss7QIfplRDIznuvKo-qJ0IwJ4fYsnrP6sNKTLEn3zrz08hVR-ravvcImqyli3EniBgiSBEQTIDBBEmDsaigu8L_pmF87z0KlYCMb_sYx8DW3xg_Mn609jwUJ1Id2ViQPzF-_IfxI-3-bFDTu2Z8l7DlgZXfWFPOn0SarxRq4xBMswG2KVMbjG_TydHkFGpgvD4zvgP2up8Z6KK_PiQhAwpHzuEJ2PoARg4jhFOcNTP7O7xUd3jnwYoObXqkyRfJjFt3-ftx91QhTEMWH3sz69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