Issue 120539
Summary [SimplifyCFG] Convert conditional branch into unconditional branch if the incoming values of phi node can be represented by the condition
Labels missed-optimization, llvm:transforms
Assignees dtcxzyw
Reporter dtcxzyw
    Alive2: https://alive2.llvm.org/ce/z/K8WVKU
```
define i1 @src1(i32 noundef %0, i32 noundef %1) {
start:
  %2 = icmp eq i32 %0, 0
  br i1 %2, label %bb6, label %bb5

bb6:
  %3 = icmp eq i32 %1, 0
  br i1 %3, label %bb2, label %bb3

bb5:
  %.not = icmp ult i32 %0, %1
  br i1 %.not, label %bb3, label %bb2

bb2:
  br label %bb3

bb3:
  %_0.sroa.0.0 = phi i1 [ true, %bb2 ], [ false, %bb6 ], [ false, %bb5 ]
  ret i1 %_0.sroa.0.0
}

define i1 @tgt1(i32 %0, i32 %1) {
start:
  %2 = icmp eq i32 %0, 0
  %3 = icmp eq i32 %1, 0
  %.not = icmp uge i32 %0, %1
  %cond = select i1 %2, i1 %3, i1 %.not
  ret i1 %cond
}
```
We can convert conditional branches into unconditional branches and adjust incoming values of the phi node:
```
define i1 @src2(i32 noundef %0, i32 noundef %1) {
start:
  %2 = icmp eq i32 %0, 0
  br i1 %2, label %bb6, label %bb5

bb6:
  %3 = icmp eq i32 %1, 0
  br i1 %3, label %bb2, label %bb3

bb5:
  %.not = icmp ult i32 %0, %1
  br i1 %.not, label %bb3, label %bb2

bb2:
  br label %bb3

bb3:
  %_0.sroa.0.0 = phi i1 [ true, %bb2 ], [ false, %bb6 ], [ false, %bb5 ]
  ret i1 %_0.sroa.0.0
}


define i1 @tgt2(i32 noundef %0, i32 noundef %1) {
start:
  %2 = icmp eq i32 %0, 0
  br i1 %2, label %bb6, label %bb5

bb6:
  %3 = icmp eq i32 %1, 0
  br i1 %3, label %bb2, label %bb3

bb5:
  %.not = icmp uge i32 %0, %1
  br label %bb3

bb2:
  br label %bb3

bb3:
  %_0.sroa.0.0 = phi i1 [ true, %bb2 ], [ false, %bb6 ], [ %.not, %bb5 ]
  ret i1 %_0.sroa.0.0
}
```
Finally we will get a select instruction with `foldTwoEntryPHINode`.

Inspired by https://github.com/llvm/llvm-project/pull/120177: https://rust.godbolt.org/z/cEjsW56Pq

_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to