The current JumpThreading pass does not jump thread loops since it can
result in irreducible control flow that harms other optimizations. This
prevents switch statements inside a loop from being optimized to use
unconditional branches.
This code pattern occurs in the core_state_transition function of
Coremark. The state machine can be implemented manually with goto
statements resulting in a large runtime improvement, and this transform
makes the switch implementation match the goto version in performance.
This patch specifically targets switch statements inside a loop that
have the opportunity to be threaded. Once it identifies an opportunity,
it creates new paths that branch directly to the correct code block.
For example, the left CFG could be transformed to the right CFG:
```
sw.bb sw.bb
/ | \ / | \
case1 case2 case3 case1 case2 case3
\ | / / | \
latch.bb latch.2 latch.3 latch.1
br sw.bb / | \
sw.bb.2 sw.bb.3 sw.bb.1
br case2 br case3 br case1
```
Co-author: Justin Kreiner @jkreiner
Co-author: Ehsan Amiri @amehsan
Reviewed By: SjoerdMeijer
Differential Revision: https://reviews.llvm.org/D99205
33 lines
750 B
LLVM
33 lines
750 B
LLVM
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
|
|
; RUN: opt -S -dfa-jump-threading -sccp -simplifycfg %s | FileCheck %s
|
|
|
|
; This test checks that a constant propagation is applied for a basic loop.
|
|
; Related to bug 44679.
|
|
define i32 @test(i32 %a) {
|
|
; CHECK-LABEL: @test(
|
|
; CHECK-NEXT: entry:
|
|
; CHECK-NEXT: ret i32 3
|
|
;
|
|
entry:
|
|
br label %while.cond
|
|
|
|
while.cond:
|
|
%num = phi i32 [ 0, %entry ], [ %add, %case1 ]
|
|
%state = phi i32 [ 1, %entry ], [ %state.next, %case1 ]
|
|
switch i32 %state, label %end [
|
|
i32 1, label %case1
|
|
i32 2, label %case2
|
|
]
|
|
|
|
case1:
|
|
%state.next = phi i32 [ 3, %case2 ], [ 2, %while.cond ]
|
|
%add = add nsw i32 %num, %state
|
|
br label %while.cond
|
|
|
|
case2:
|
|
br label %case1
|
|
|
|
end:
|
|
ret i32 %num
|
|
}
|