Files
Kunqiu Chen fddc2c05b8 [SimplifyCFG] Simplify identical predecessors (#173022)
When >1 predecessors of BB are identical, try to merge them into ONE.


---

Here is a simplified example (`sink` and `bb*`s share the same
predecessor `entry`, hindering the existing uncond br folding to
optimize such a case):
```diff
- entry:
-   switch to %br1, %br2, %br3, %sink
- bb1:
-   br label %sink
- bb2:
-   br label %sink
- bb3:
-   br label %sink
- sink:
-   %ret = phi i8 [ 0, %bb1 ], [ 0, %bb2 ], [ 0, %bb3 ], [ -1, %entry ]
+ entry:
+   switch to %br1, %sink
+ bb1:
+   br label %sink
+ sink:
+   %ret = phi i8 [ 0, %bb1 ], [ -1, %entry ]
```

Actually, `simplifyDuplicateSwitchArms` did similar things in a very
limited scope (only for switch arms), this patch generalizes its logic
to handle any BB with >1 identical predecessors.

---


This PR lands the
[discussion](https://github.com/dtcxzyw/llvm-opt-benchmark/pull/3033#discussion_r2506973226),
i.e., "merge identical predecessor bottom to up", and implements the
suggestion of
https://github.com/llvm/llvm-project/pull/114262#issuecomment-2448140669.

- IR diff: https://github.com/dtcxzyw/llvm-opt-benchmark/pull/3537
- CompTime Impact:
https://github.com/dtcxzyw/llvm-opt-benchmark/pull/3538
2026-03-12 14:58:46 +08:00

109 lines
4.1 KiB
LLVM

; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 -sink-common-insts -S | FileCheck %s -check-prefix=SIMPLIFY-CFG
define i8 @test_duplicate_preds_merging(i8 %v1, i8 %v2) {
; SIMPLIFY-CFG-LABEL: @test_duplicate_preds_merging(
; SIMPLIFY-CFG-NEXT: entry:
; SIMPLIFY-CFG-NEXT: switch i8 [[V1:%.*]], label [[EXIT:%.*]] [
; SIMPLIFY-CFG-NEXT: i8 0, label [[THEN:%.*]]
; SIMPLIFY-CFG-NEXT: i8 1, label [[ELSE:%.*]]
; SIMPLIFY-CFG-NEXT: ]
; SIMPLIFY-CFG: then:
; SIMPLIFY-CFG-NEXT: switch i8 [[V2:%.*]], label [[EXIT]] [
; SIMPLIFY-CFG-NEXT: i8 0, label [[ELSE]]
; SIMPLIFY-CFG-NEXT: i8 1, label [[SWITCH_CASE_1:%.*]]
; SIMPLIFY-CFG-NEXT: i8 2, label [[SWITCH_CASE_1]]
; SIMPLIFY-CFG-NEXT: ]
; SIMPLIFY-CFG: switch.case.1:
; SIMPLIFY-CFG-NEXT: br label [[EXIT]]
; SIMPLIFY-CFG: else:
; SIMPLIFY-CFG-NEXT: br label [[EXIT]]
; SIMPLIFY-CFG: exit:
; SIMPLIFY-CFG-NEXT: [[RET:%.*]] = phi i8 [ 0, [[ELSE]] ], [ 2, [[THEN]] ], [ 1, [[SWITCH_CASE_1]] ], [ 3, [[ENTRY:%.*]] ]
; SIMPLIFY-CFG-NEXT: ret i8 [[RET]]
;
entry:
switch i8 %v1, label %exit [
i8 0, label %then
i8 1, label %else
]
then: ; preds = %entry
switch i8 %v2, label %exit [
i8 0, label %switch.case.0
i8 1, label %switch.case.1
i8 2, label %switch.case.2
]
switch.case.0: ; preds = %then
br label %exit
switch.case.1: ; preds = %then
br label %exit
switch.case.2: ; preds = %then
br label %exit
else: ; preds = %entry
br label %exit
exit: ; preds = %else, %switch.case.2, %switch.case.1, %switch.case.0, %then, %entry
%ret = phi i8 [ 0, %else ], [ 0, %switch.case.0 ], [ 1, %switch.case.1 ], [ 1, %switch.case.2 ], [ 2, %then ], [ 3, %entry ]
ret i8 %ret
}
define i8 @test_no_merge_address_taken(i8 %v1, i8 %v2, ptr %p) {
; SIMPLIFY-CFG-LABEL: @test_no_merge_address_taken(
; SIMPLIFY-CFG-NEXT: entry:
; SIMPLIFY-CFG-NEXT: store ptr blockaddress(@test_no_merge_address_taken, [[SWITCH_CASE_0:%.*]]), ptr [[P:%.*]], align 8
; SIMPLIFY-CFG-NEXT: switch i8 [[V1:%.*]], label [[EXIT:%.*]] [
; SIMPLIFY-CFG-NEXT: i8 0, label [[THEN:%.*]]
; SIMPLIFY-CFG-NEXT: i8 1, label [[ELSE:%.*]]
; SIMPLIFY-CFG-NEXT: ]
; SIMPLIFY-CFG: then:
; SIMPLIFY-CFG-NEXT: switch i8 [[V2:%.*]], label [[EXIT]] [
; SIMPLIFY-CFG-NEXT: i8 0, label [[SWITCH_CASE_0]]
; SIMPLIFY-CFG-NEXT: i8 1, label [[SWITCH_CASE_1:%.*]]
; SIMPLIFY-CFG-NEXT: i8 2, label [[SWITCH_CASE_1]]
; SIMPLIFY-CFG-NEXT: ]
; SIMPLIFY-CFG: switch.case.0:
; SIMPLIFY-CFG-NEXT: br label [[EXIT]]
; SIMPLIFY-CFG: switch.case.1:
; SIMPLIFY-CFG-NEXT: br label [[EXIT]]
; SIMPLIFY-CFG: else:
; SIMPLIFY-CFG-NEXT: br label [[EXIT]]
; SIMPLIFY-CFG: exit:
; SIMPLIFY-CFG-NEXT: [[RET:%.*]] = phi i8 [ 0, [[ELSE]] ], [ 0, [[SWITCH_CASE_0]] ], [ 1, [[SWITCH_CASE_1]] ], [ 3, [[ENTRY:%.*]] ], [ 2, [[THEN]] ]
; SIMPLIFY-CFG-NEXT: ret i8 [[RET]]
;
entry:
store ptr blockaddress(@test_no_merge_address_taken, %switch.case.0), ptr %p
switch i8 %v1, label %exit [
i8 0, label %then
i8 1, label %else
]
then: ; preds = %entry
switch i8 %v2, label %exit [
i8 0, label %switch.case.0
i8 1, label %switch.case.1
i8 2, label %switch.case.2
]
switch.case.0: ; preds = %then
br label %exit
switch.case.1: ; preds = %then
br label %exit
switch.case.2: ; preds = %then
br label %exit
else: ; preds = %entry
br label %exit
exit: ; preds = %else, %switch.case.2, %switch.case.1, %switch.case.0, %then, %entry
%ret = phi i8 [ 0, %else ], [ 0, %switch.case.0 ], [ 1, %switch.case.1 ], [ 1, %switch.case.2 ], [ 2, %then ], [ 3, %entry ]
ret i8 %ret
}