<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/62852>62852</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[LSR] Bug in Loop Strength Reduce
</td>
</tr>
<tr>
<th>Labels</th>
<td>
loopoptim
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
PeixinQiao
</td>
</tr>
</table>
<pre>
```
target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
target triple = "aarch64-unknown-linux-gnu"
@l = global i64 0
@i = global i64 0
@.str = private constant [4 x i8] c"%d\0A\00"
define i32 @main() {
for.body8.1.preheader:
br label %for.body8.1
for.body8.1: ; preds = %for.body8.1, %for.body8.1.preheader
%dec.137 = phi i32 [ %dec.1, %for.body8.1 ], [ 1, %for.body8.1.preheader ]
%inc1821.1 = phi i64 [ %inc.1, %for.body8.1 ], [ 0, %for.body8.1.preheader ]
%inc.1 = add nsw i64 %inc1821.1, 1
%dec.1 = add nsw i32 %dec.137, -1
%cmp6.1 = icmp sgt i32 %dec.137, 0
br i1 %cmp6.1, label %for.body8.1, label %for.inc10.1
for.inc10.1: ; preds = %for.body8.1
br label %for.end15
for.end15: ; preds = %for.inc10.1
%conv9.le.le.le = zext i32 %dec.137 to i64
store i64 %inc1821.1, ptr @l, align 8
store i64 %conv9.le.le.le, ptr @i, align 8
%rem = urem i32 %dec.1, 53
%call = tail call i32 (ptr, ...) @printf(ptr @.str, i32 %rem)
ret i32 0
}
declare i32 @printf(ptr, ...)
```
```
$ clang reduced.ll -mllvm -opt-bisect-limit=-1 && ./a.out
BISECT: running pass (1) Two-Address instruction pass on function (main)
BISECT: running pass (2) Fixup Statepoint Caller Saved on function (main)
41
$ opt -loop-reduce test.ll -S -o out.ll
$ cat out.ll
$ clang out.ll -mllvm -opt-bisect-limit=-1 && ./a.out
BISECT: running pass (1) Two-Address instruction pass on function (main)
BISECT: running pass (2) Fixup Statepoint Caller Saved on function (main)
14
; ModuleID = 'test.ll'
source_filename = "test.ll"
target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
target triple = "aarch64-unknown-linux-gnu"
@l = global i64 0
@i = global i64 0
@.str = private constant [4 x i8] c"%d\0A\00"
define i32 @main() {
for.body8.1.preheader:
br label %for.body8.1
for.body8.1: ; preds = %for.body8.1, %for.body8.1.preheader
%lsr.iv = phi i64 [ %lsr.iv.next, %for.body8.1 ], [ 1, %for.body8.1.preheader ]
%lsr.iv.next = add nsw i64 %lsr.iv, -1
%0 = add i64 %lsr.iv.next, 1
%tmp = trunc i64 %0 to i32
%cmp6.1 = icmp sgt i32 %tmp, 0
br i1 %cmp6.1, label %for.body8.1, label %for.inc10.1
for.inc10.1: ; preds = %for.body8.1
%1 = mul i64 %lsr.iv.next, -1
br label %for.end15
for.end15: ; preds = %for.inc10.1
%2 = add i64 %lsr.iv.next, 1
store i64 %1, ptr @l, align 8
store i64 %2, ptr @i, align 8
%3 = udiv i64 %lsr.iv.next, 53
%4 = mul i64 %3, 4294967243
%5 = add i64 %4, %lsr.iv.next
%tmp1 = trunc i64 %5 to i32
%call = tail call i32 (ptr, ...) @printf(ptr @.str, i32 %tmp1)
ret i32 0
}
declare i32 @printf(ptr, ...)
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsV1-P4rYX_TTm5SqRY8chPPAAww9ppf1J7c6-VyYxwa1jR7bDsv30lePABIbZZatpX7qIYcj1uX98cnN94M7JRguxRGyN2GbGe38wdvmLkCepf5XczHam_rpEBR7feIPwynPbCA8191zxr6b3gOgGECEiaRFdiUSWiK7CHyWJzApEV8NHuCpyRFdFnsiMBEBGykRTEm3PwUbIVRJvZafEOQHntjoUedLrP7T5ohMldX9KGt1f3MbPHKvBp1FmxxXIIgd8WZJvL6XO22G1s_LIvYDKaOe59oDYOocTyBKxDVQhH2E1Yk94FT7wTQG12EstQFICKMctlxqFvS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