<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/78051>78051</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[IndVarSimplify] WRONG code
</td>
</tr>
<tr>
<th>Labels</th>
<td>
llvm:transforms
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
nikic,
preames
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
JonPsson1
</td>
</tr>
</table>
<pre>
IndVarSimplify seems to make an incorrect transformation, as seen below. Given that the code to the left is correct, it does not seem right to substitute %dec in left to %1 in right. %dec is after first iteration %b.promoted - 1, while %1 is %b.promoted - 1 - %umin..?
opt -mtriple=s390x-unknown-linux -mcpu=z15 wrong0_indvar.bc -O3 -o - -S -scev-cheap-expansion-budget=8
[wrong0_indvar.tar.gz](https://github.com/llvm/llvm-project/files/13928920/wrong0_indvar.tar.gz)
` opt -O3 <=> opt -O3 -scev-cheap-expansion-budget=8
`
```
; *** IR Dump After IndVarSimplifyPass on for ; *** IR Dump After IndVarSimplifyPass on for
; Preheader: ; Preheader:
entry: entry:
%b.promoted = load i128, ptr @b, align 1, ! %b.promoted = load i128, ptr @b, align 1, !
%0 = add i128 %b.promoted, -1 %0 = add i128 %b.promoted, -1
%umin = call i128 @llvm.umin.i128(i128 %0, %umin = call i128 @llvm.umin.i128(i128 %0,
%1 = trunc i128 %umin to i64 | %1 = sub i128 %0, %umin
%2 = add nuw nsw i64 %1, 1 | %2 = trunc i128 %umin to i64
> %3 = add nuw nsw i64 %2, 1
br label %for.body br label %for.body
; Loop: ; Loop:
for.body: for.body:
%storemerge3 = phi i64 [ 0, %entry ], [ %in %storemerge3 = phi i64 [ 0, %entry ], [ %in
%dec12 = phi i128 [ %b.promoted, %entry ], %dec12 = phi i128 [ %b.promoted, %entry ],
%dec = add nsw i128 %dec12, -1 %dec = add nsw i128 %dec12, -1
%tobool.not = icmp eq i128 %dec12, 1 %tobool.not = icmp eq i128 %dec12, 1
br i1 %tobool.not, label %return, label %fo br i1 %tobool.not, label %return, label %fo
for.inc: for.inc:
%inc = add nuw nsw i64 %storemerge3, 1 %inc = add nuw nsw i64 %storemerge3, 1
%exitcond = icmp ne i64 %inc, 4 %exitcond = icmp ne i64 %inc, 4
br i1 %exitcond, label %for.body, label %re br i1 %exitcond, label %for.body, label %re
; Exit blocks ; Exit blocks
return: return:
%dec.lcssa = phi i128 [ %dec, %for.inc ], [ | %dec.lcssa = phi i128 [ %1, %for.inc ], [ %
%storemerge.lcssa = phi i64 [ %2, %for.inc | %storemerge.lcssa = phi i64 [ %3, %for.inc
%retval.0 = phi i16 [ 0, %for.inc ], [ 6, % %retval.0 = phi i16 [ 0, %for.inc ], [ 6, %
store i128 %dec.lcssa, ptr @b, align 8, !tb store i128 %dec.lcssa, ptr @b, align 8, !tb
store i64 %storemerge.lcssa, ptr @a, align store i64 %storemerge.lcssa, ptr @a, align
ret i16 %retval.0 ret i16 %retval.0
return: return:
%dec.lcssa = phi i128 [ %dec, %for.inc ], [ | %dec.lcssa = phi i128 [ %1, %for.inc ], [ %
%storemerge.lcssa = phi i64 [ %2, %for.inc | %storemerge.lcssa = phi i64 [ %3, %for.inc
%retval.0 = phi i16 [ 0, %for.inc ], [ 6, % %retval.0 = phi i16 [ 0, %for.inc ], [ 6, %
store i128 %dec.lcssa, ptr @b, align 8, !tb store i128 %dec.lcssa, ptr @b, align 8, !tb
store i64 %storemerge.lcssa, ptr @a, align store i64 %storemerge.lcssa, ptr @a, align
ret i16 %retval.0 ret i16 %retval.0
; *** IR Dump After LoopDeletionPass on for.b ; *** IR Dump After LoopDeletionPass on for.b
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWMFu4zYTfhr6MpBAkZZjH3xw4vVif_zoLnaB9lhQ0thmI5EqSSXZffqClGRLjpPa3ksLVAgciZzvm-FwZqShsFbuFOKSpPeEMSUfZU4YI-yBMFYbFBVa_5yuJ6Jxe22W_9Pqi7VaJZNMF9-Xn1Tx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