llvm / llvm/llvm-project

[LSR] Unnecessary phi introduced

Open
#187,728 1 comment 0 reactions 1 assignee Claimed by @aleks-tmb View on GitHub
loopoptim
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

The LSR pass introduces a redundant loop phi in cases where an existing phi could be reused: https://godbolt.org/z/Thx44K4rj

### reproducer
```llvm
define void @main() {
entry:
br label %loop

loop:
%iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
%iv.next = add i64 %iv, 8
%iv.inc = add i64 %iv, 1
%use = add i64 %iv.next, %iv.inc
%check = icmp eq i64 %iv.next, 100
br i1 %check, label %exit, label %loop

exit:
ret void
}
```
```bash
opt -passes=loop-reduce lsr.ll -S
```

### what happens
The loop has one phi `%iv` (`addrec {0,+,8}<%loop>`). Both `%use` and `%check` use `%iv.next` (`addrec {8,+,8}<%loop>`).
LSR replaces them with a new phi for {8,+,8}, but can't eliminate `%iv` since it is still needed for `%iv.inc`.
The result has two phis where the input had one:

```llvm
loop:
%lsr.iv = phi i64 [ %lsr.iv.next, %loop ], [ 8, %entry ]
%iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
%iv.next = add i64 %iv, 8
%iv.inc = add i64 %iv, 1
%use = add i64 %lsr.iv, %iv.inc
%lsr.iv.next = add i64 %lsr.iv, 8
%check = icmp eq i64 %lsr.iv.next, 108
br i1 %check, label %exit, label %loop
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.