Sameer Sahasrabuddhe 5f6172f068 [Transforms] Refactor CreateControlFlowHub (#103013)
CreateControlFlowHub is a method that redirects control flow edges from a set of
incoming blocks to a set of outgoing blocks through a new set of "guard" blocks.
This is now refactored into a separate file with one enhancement: The input to
the method is now a set of branches rather than two sets of blocks.

The original implementation reroutes every edge from incoming blocks to outgoing
blocks. But it is possible that for some incoming block InBB, some successor S
might be in the set of outgoing blocks, but that particular edge should not be
rerouted. The new implementation makes this possible by allowing the user to
specify the targets of each branch that need to be rerouted.

This is needed when improving the implementation of FixIrreducible #101386.
Current use in FixIrreducible does not demonstrate this finer control over the
edges being rerouted. But in UnifyLoopExits, when only one successor of an
exiting block is an exit block, this refinement now reroutes only the relevant
control-flow through the edge; the non-exit successor is not rerouted. This
results in fewer branches and PHI nodes in the hub.
2024-08-22 12:18:01 +05:30

81 lines
2.3 KiB
LLVM

; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -passes='lower-switch,unify-loop-exits' -S | FileCheck %s
define void @nested(i1 %PredB3, i1 %PredB4, i1 %PredA4, i1 %PredA3, i32 %X, i32 %Y, i32 %Z) {
; CHECK-LABEL: @nested(
; CHECK-NEXT: entry:
; CHECK-NEXT: br label [[A1:%.*]]
; CHECK: A1:
; CHECK-NEXT: br label [[B1:%.*]]
; CHECK: B1:
; CHECK-NEXT: br label [[B2:%.*]]
; CHECK: B2:
; CHECK-NEXT: [[X_INC:%.*]] = add i32 [[X:%.*]], 1
; CHECK-NEXT: br label [[B3:%.*]]
; CHECK: B3:
; CHECK-NEXT: br i1 [[PREDB3:%.*]], label [[B4:%.*]], label [[LOOP_EXIT_GUARD1:%.*]]
; CHECK: B4:
; CHECK-NEXT: br i1 [[PREDB4:%.*]], label [[B1]], label [[LOOP_EXIT_GUARD1]]
; CHECK: A2:
; CHECK-NEXT: br label [[A4:%.*]]
; CHECK: A3:
; CHECK-NEXT: br label [[A4]]
; CHECK: A4:
; CHECK-NEXT: [[A4_PHI:%.*]] = phi i32 [ [[Y:%.*]], [[A3:%.*]] ], [ [[X_INC_MOVED:%.*]], [[A2:%.*]] ]
; CHECK-NEXT: br i1 [[PREDA4:%.*]], label [[LOOP_EXIT_GUARD:%.*]], label [[A5:%.*]]
; CHECK: A5:
; CHECK-NEXT: br i1 [[PREDA3:%.*]], label [[LOOP_EXIT_GUARD]], label [[A1]]
; CHECK: C:
; CHECK-NEXT: br label [[EXIT:%.*]]
; CHECK: exit:
; CHECK-NEXT: [[EXIT_PHI:%.*]] = phi i32 [ [[Z:%.*]], [[C:%.*]] ], [ [[EXIT_PHI_MOVED:%.*]], [[LOOP_EXIT_GUARD]] ]
; CHECK-NEXT: ret void
; CHECK: loop.exit.guard:
; CHECK-NEXT: [[EXIT_PHI_MOVED]] = phi i32 [ poison, [[A4]] ], [ [[A4_PHI]], [[A5]] ]
; CHECK-NEXT: [[GUARD_C:%.*]] = phi i1 [ true, [[A4]] ], [ false, [[A5]] ]
; CHECK-NEXT: br i1 [[GUARD_C]], label [[C]], label [[EXIT]]
; CHECK: loop.exit.guard1:
; CHECK-NEXT: [[X_INC_MOVED]] = phi i32 [ [[X_INC]], [[B3]] ], [ [[X_INC]], [[B4]] ]
; CHECK-NEXT: [[GUARD_A3:%.*]] = phi i1 [ true, [[B3]] ], [ false, [[B4]] ]
; CHECK-NEXT: br i1 [[GUARD_A3]], label [[A3]], label [[A2]]
;
entry:
br label %A1
A1:
br label %B1
B1:
br label %B2
B2:
%X.inc = add i32 %X, 1
br label %B3
B3:
br i1 %PredB3, label %B4, label %A3
B4:
br i1 %PredB4, label %B1, label %A2
A2:
br label %A4
A3:
br label %A4
A4:
%A4.phi = phi i32 [%Y, %A3], [%X.inc, %A2]
br i1 %PredA4, label %C, label %A5
A5:
br i1 %PredA3, label %exit, label %A1
C:
br label %exit
exit:
%exit.phi = phi i32 [%A4.phi, %A5], [%Z, %C]
ret void
}