[all-commits] [llvm/llvm-project] e8a116: [SimplifyCFG] Use range check in simplifyBranchOnI...

Yingwei Zheng via All-commits all-commits at lists.llvm.org
Tue Oct 28 19:52:50 PDT 2025


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: e8a1162b7be6cf11041260886097843ce9f5e4a3
      https://github.com/llvm/llvm-project/commit/e8a1162b7be6cf11041260886097843ce9f5e4a3
  Author: Yingwei Zheng <dtcxzyw2333 at gmail.com>
  Date:   2025-10-29 (Wed, 29 Oct 2025)

  Changed paths:
    M llvm/lib/Transforms/Utils/SimplifyCFG.cpp
    A llvm/test/Transforms/SimplifyCFG/pr165088.ll

  Log Message:
  -----------
  [SimplifyCFG] Use range check in simplifyBranchOnICmpChain if possible (#165105)

In `simplifyBranchOnICmpChain`, if we can merge the comparisons into a
range check, use a conditional branch instead. This change also breaks
the cycle found in https://github.com/llvm/llvm-project/issues/165088.

Closes https://github.com/llvm/llvm-project/issues/165088.

Detailed description of the cycle:

```
define void @pr165088_cycle_1(i8 %x) {
entry:
  %switch = icmp uge i8 %x, 2
  %cond1 = icmp ugt i8 %x, 1
  %or.cond = and i1 %switch, %cond1
  br i1 %or.cond, label %block3, label %block2

block1:
  %cond2 = icmp ugt i8 %x, 1
  br i1 %cond2, label %block3, label %block2

block2:
  br label %block3

block3:
  %cond3 = icmp eq i8 %x, 0
  br i1 %cond3, label %exit, label %block1

exit:
  ret void
}
```

`simplifyBranchOnICmpChain` folds the branch in `entry` to a switch.
Then we get:
```
entry:
  switch i8 %x, label %block3 [
  i8 1, label %block2
  i8 0, label %block2
  ]

...
```

`performValueComparisonIntoPredecessorFolding` redirects the default
target `block3` into `block1` because `%x` is never zero.
```
entry:
  switch i8 %x, label %block1 [
  i8 1, label %block2
  i8 0, label %block2
  ]
...
```

Then `turnSwitchRangeIntoICmp` will convert the switch back into a
branch on icmp.
```
entry:
  %switch = icmp ult i8 %x, 2
  br i1 %switch, label %block2, label %block1
...
```

Since `block1` and `block2` share the same successor `block3`,
`performBranchToCommonDestFolding` merges the conditions of `entry` and
`block1`, resulting in the original pattern again.



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list