<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/117509>117509</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Const loop not folded
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
ZuseZ4
</td>
</tr>
</table>
<pre>
Originally encountered on the Julia Slack, then discussed a bit on the Rust Discord.
The following loop has no side effects and should be simple enough to fold, yet both clang and rustc end up with a phi node.
https://godbolt.org/z/16rG5bjz9
```c++
unsigned long foo() {
unsigned long max = 1000000000;
unsigned long total = 0;
for (unsigned long i = 0; i < max; i++) {
total = i;
}
return total;
}
```
under -O3 and -S -emit-llvm gives
```llvm
define dso_local noundef i64 @foo()() local_unnamed_addr #0 !dbg !10 {
entry:
#dbg_value(i64 1000000000, !16, !DIExpression(), !20)
#dbg_value(i64 0, !17, !DIExpression(), !20)
#dbg_value(i64 0, !18, !DIExpression(), !21)
br label %vector.body, !dbg !22
vector.body:
%index = phi i64 [ 0, %entry ], [ %index.next.9, %vector.body ], !dbg !23
%0 = phi i64 [ 15, %entry ], [ %1, %vector.body ], !dbg !23
%index.next.9 = add nuw nsw i64 %index, 320, !dbg !23
%1 = add nuw nsw i64 %0, 320, !dbg !23
%2 = icmp eq i64 %index.next.9, 1000000000, !dbg !23
br i1 %2, label %for.cond, label %vector.body, !dbg !23
for.cond:
%vector.recur.extract = add nuw i64 %0, 304
#dbg_value(i64 1000000000, !18, !DIExpression(), !21)
#dbg_value(i64 1000000000, !17, !DIExpression(), !20)
#dbg_value(i64 1000000001, !18, !DIExpression(), !21)
ret i64 %vector.recur.extract, !dbg !30
}
attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(none) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
```
The resulting asm is:
```asm
foo():
mov rax, -1
.LBB0_1:
add rax, 320
cmp rax, 999999999
jne .LBB0_1
ret
```
compared to GCC, which just has a
```
foo():
mov eax, 999999999
ret
```
Here the pass that looks suspicious (thanks to @scottamain) https://rust.godbolt.org/z/dG9far1dW
Tbh. it looks like a fun issue, so if someone gives me a hint on where to fix it, I could start trying to come up with a fix next week.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysV9uOozwSfhrnppQIDEnIRS6mk-3ZWa000s5KK81Ny-AC3G1s1odOep5-ZUNImD7Mzq9BUSjwVweXXeUPZq1oFOKerO_I-rhg3rXa7L97i9_zRan5y_6rEY1QTMoXQFVprxwa5KAVuBbhH14KBt8kq54IPYRXCriwlbcWOTAohbtA_-Wtg6OwlTZ8RZIjST79u0WotZT6JFQDUuseWmZBabCCI2BdY-UsMMXBttpLDiWCFV0vEVBp37TgdLDAg_MXdFBq10IlmWqilvHWVYCKg-_hJFwLDPpWgNIcxxha53pLsk-E3hN632heaulW2jSE3v8g9D7dmM_r8vHHboCP_5tk-FWE3oVffOtVzCYHqVUDtdaEFoTugGxHAADAHNSxM5DsCGlyuUj2Lthpx2SEz1FXqdYGCC3mamJSieIhOI0PY-yzCK8-xNzH9nh9MOi8UQN2Qk2IKTmXrHA0sPyaxRVZfoMldsItpXzuoBHPaH_SCgPDK461UAjc6gepKyZB6WCsBrHJgeTJlOExzxH04JViHfIHxnlIRpYAoSkvm3BLk-tUUTnzEhb-Okea8bJ5eGbSI6FF8HKzLvQQLWxG4fjlb-feoLVCq0sUcYAmQf7I6GRr-wdtFb-0ld7YKg1IVqIEQtfPWDltVqHaR-iYLUpv9_wt7Jo1QtdCcRx2cSituDbru0tg65hmIOtjfF7fTRorhWe3GqO7DWICXwPJbtwlr1yl6w98pb_t4Da66ItxDsqfQNnT4HPEBBsZTT4wlb6nn_xalw5VWHU94H9nfm8y92p_vrJUGhBptBcQ06LX2qwq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