| Issue |
115493
|
| Summary |
[InstCombine] folding add and compare can negatively effect codegen for loops.
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
SpencerAbson
|
The following transform,
`icmp sgt/slt (add nsw X, C2), C --> icmp sgt/slt X, (C - C2)`
which was introduced in https://github.com/llvm/llvm-project/commit/45b7e69fefae98ad3c747fd090e6aafcf2cba94f, can have a negative effect on codegen where the icmp serves as the exiting condition of a loop.
Please see https://godbolt.org/z/9hMWo6b58
For an ARM target, we would expect:
```
.test
cmp w2, #1
b.lt .LBB0_2
.LBB0_1:
ldrb w8, [x0], #1
subs w2, w2, #1
strb w8, [x1], #1
b.hi .LBB0_1
.LBB0_2:
ret
```
But instead, we have:
```
.test
cmp w2, #1
b.lt .LBB0_3
add w8, w2, #1
.LBB0_2:
ldrb w9, [x0], #1
sub w8, w8, #1
cmp w8, #1
strb w9, [x1], #1
b.hi .LBB0_2
.LBB0_3:
ret
```
In this case, two things have poorly effected codegen
- The the possibility for a target to combine the add and compare with zero into a single CC-writing operation is sacrificed in favor of removing a use of the add, even if this add has other users.
- The post-inc exiting condition is transformed into a pre-inc exiting condition. LSR has the ability to rectify this, but it's use of SCEVExpander can only do so by inserting an add into the pre-header.
A similar issue (https://github.com/llvm/llvm-project/issues/54558) was fixed by https://github.com/llvm/llvm-project/commit/5f8c2b884d4288617138114ebd2fdb235452c8ce.
I'm considering a few options to fix this, and I'd really appreciate some feedback on what would be preferred.
- Implementing a similar restriction as in https://github.com/llvm/llvm-project/commit/5f8c2b884d4288617138114ebd2fdb235452c8ce.
- Allowing IndVarSimplify to perform LFTR on loops with a stride of -1, which would convert the terminating condition into an equality and allow LSR to treat this as an `ICmpZero` (see line 3554 of LoopStrengthReduce.cpp).
Thanks
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs