Issue 83417
Summary [SimplifyCFG] Missed optimization: simplify if-else BBs that share common destinations
Labels new issue
Assignees
Reporter XChy
    Alive2 proof: https://alive2.llvm.org/ce/z/Q3dd5y

### Motivating example 

```llvm
define void @src(i1 %c1, i1 %c2) {
entry:
  br i1 %c1, label %then, label %else

then:
  br i1 %c2, label %bb1, label %bb2
  
else:
  br i1 %c2, label %bb2, label %bb1

bb1:
 call void @dummy1()
  ret void
bb2:
  call void @dummy2()
  ret void
}
```
can be folded to
```llvm
define void @tgt(i1 %c1, i1 %c2) {
entry:
  %xor = xor i1 %c1, %c2
  br i1 %xor, label %bb2, label %bb1

bb1:
  call void @dummy1()
  ret void
bb2:
 call void @dummy2()
  ret void
}
```
Here both `then` and `else` are trivial basicblocks with only a conditional branch inst, but I think it's possible to generalize it, 
like replacing the condition of one of branch inst with `%c3`: https://alive2.llvm.org/ce/z/Xw9Ms2.

### Real-world motivation

This snippet of IR is derived from [z3/src/sat/tactic/goal2sat.cpp@convert_iff](https://github.com/Z3Prover/z3/blob/2880ea39719971226e616d1077788288d6107632/src/sat/tactic/goal2sat.cpp#L626) (after O3 pipeline).
The example above is a reduced version. Original IR comes from https://github.com/dtcxzyw/llvm-opt-benchmark/blob/main/bench/z3/original/goal2sat.cpp.ll, if you want optimal one, please email me please.

**Let me know if you can confirm that it's an optimization opportunity, thanks.**
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to