Skip to content

Commit 2e89288

Browse files
committed
Change heuristic to check the increase in # phi entries
1 parent fa4dd77 commit 2e89288

File tree

2 files changed

+81
-35
lines changed

2 files changed

+81
-35
lines changed

llvm/lib/Transforms/Utils/Local.cpp

+17-16
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,11 @@ static cl::opt<unsigned> PHICSENumPHISmallSize(
112112
"When the basic block contains not more than this number of PHI nodes, "
113113
"perform a (faster!) exhaustive search instead of set-driven one."));
114114

115-
static cl::opt<unsigned> MaxPhiEntriesAfterRemovingEmptyBlock(
116-
"max-phi-entries-after-removing-empty-block", cl::init(100), cl::Hidden,
117-
cl::desc("Stop removing an empty block if removing it will make a PHI have "
118-
"more than this number of incoming entries."));
115+
static cl::opt<unsigned> MaxPhiEntriesIncreaseAfterRemovingEmptyBlock(
116+
"max-phi-entries-increase-after-removing-empty-block", cl::init(1000),
117+
cl::Hidden,
118+
cl::desc("Stop removing an empty block if removing it will introduce more "
119+
"than this number of phi entries in its successor"));
119120

120121
// Max recursion depth for collectBitParts used when detecting bswap and
121122
// bitreverse idioms.
@@ -1048,29 +1049,29 @@ CanRedirectPredsOfEmptyBBToSucc(BasicBlock *BB, BasicBlock *Succ,
10481049
// Check whether removing BB will make the phis in its Succ have too
10491050
// many incoming entries. This function does not check whether BB is foldable
10501051
// or not.
1051-
static bool introduceTooComplexPhi(BasicBlock *BB, BasicBlock *Succ) {
1052+
static bool introduceTooManyPhiEntries(BasicBlock *BB, BasicBlock *Succ) {
10521053
// If BB only has one predecessor, then removing it will not introduce more
10531054
// incoming edges for phis.
10541055
if (BB->hasNPredecessors(1))
10551056
return false;
1056-
int NumPreds = pred_size(BB);
1057+
unsigned NumPreds = pred_size(BB);
1058+
unsigned NumChangedPhi = 0;
10571059
for (auto &Phi : Succ->phis()) {
10581060
// If the incoming value is a phi and the phi is defined in BB,
10591061
// then removing BB will not increase the total phi entries of the ir.
10601062
if (PHINode *IncomingPhi =
10611063
dyn_cast<PHINode>(Phi.getIncomingValueForBlock(BB)))
10621064
if (IncomingPhi->getParent() == BB)
10631065
continue;
1064-
// Otherwise, we need to add (NumPreds - 1) entries to the phi node.
1065-
// If removing BB makes the phi have more than
1066-
// MaxPhiEntriesAfterRemovingEmptyBlock incoming values, then it will be
1067-
// considered as introducing too complex phi to the ir.
1068-
// The default threshold is 100.
1069-
if ((NumPreds - 1) + Phi.getNumIncomingValues() >
1070-
MaxPhiEntriesAfterRemovingEmptyBlock)
1071-
return true;
1066+
// Otherwise, we need to add entries to the phi
1067+
NumChangedPhi++;
10721068
}
1073-
return false;
1069+
// For every phi that needs to be changed, (NumPreds - 1) new entries will be
1070+
// added. If the total increase in phi entries exceeds
1071+
// MaxPhiEntriesIncreaseAfterRemovingEmptyBlock, it will be considered as
1072+
// introducing too many new phi entries.
1073+
return (NumPreds - 1) * NumChangedPhi >
1074+
MaxPhiEntriesIncreaseAfterRemovingEmptyBlock;
10741075
}
10751076

10761077
/// Replace a value flowing from a block to a phi with
@@ -1172,7 +1173,7 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
11721173
BBKillable ||
11731174
CanRedirectPredsOfEmptyBBToSucc(BB, Succ, BBPreds, SuccPreds, CommonPred);
11741175

1175-
if ((!BBKillable && !BBPhisMergeable) || introduceTooComplexPhi(BB, Succ))
1176+
if ((!BBKillable && !BBPhisMergeable) || introduceTooManyPhiEntries(BB, Succ))
11761177
return false;
11771178

11781179
// Check to see if merging these blocks/phis would cause conflicts for any of

llvm/test/Transforms/SimplifyCFG/avoid-complex-phi.ll

+64-19
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,59 @@
1-
; RUN: opt < %s -passes=simplifycfg -S | FileCheck --check-prefixes=CHECK-100 %s
2-
; RUN: opt < %s -max-phi-entries-after-removing-empty-block=101 -passes=simplifycfg -S | FileCheck --check-prefixes=CHECK-101 %s
3-
; CHECK-100: %x = phi i16 {{((\[ [0-2], %BB[0-9]+ \], ){51})\[ [0-2], %BB[0-9]+ \]}}
4-
; CHECK-101: %x = phi i16 {{((\[ [0-2], %BB[0-9]+ \], ){100})\[ [0-2], %BB[0-9]+ \]}}
1+
; RUN: opt < %s -passes=simplifycfg -S | FileCheck --check-prefixes=CHECK-1000 %s
2+
; RUN: opt < %s -max-phi-entries-increase-after-removing-empty-block=989 -passes=simplifycfg -S | FileCheck --check-prefixes=CHECK-989 %s
3+
; RUN: opt < %s -max-phi-entries-increase-after-removing-empty-block=489 -passes=simplifycfg -S | FileCheck --check-prefixes=CHECK-489 %s
54

65
; This test has the following CFG:
76
; 1. entry has a switch to 100 blocks: BB1 - BB100
8-
; 2. For BB1 to BB50, it branch to BB101 and BB103
9-
; 3. For BB51 to BB100, it branch to BB102 and BB103
7+
; 2. For BB1 to BB50, it branches to BB101 and BB103
8+
; 3. For BB51 to BB100, it branches to BB102 and BB103
109
; 4. BB101, BB102, BB103 branch to Merge unconditionally
11-
; 5. Merge returns value %x which is a phi having incoming blocks BB101, BB102, BB103
10+
; 5. Merge has 10 phis(x1 - x10).
1211
;
13-
; In the first test, we are going to check that the simplifycfg will not introduce a phi with more than
14-
; 100 incoming values. So the simplifycfg can remove only one of BB101 and BB102. Otherwise, the
15-
; phi of %x will have at least 50 + 50 + 1 = 101 incoming blocks (BB1 - BB100 and BB103).
16-
;
17-
; In the second test, the threshold is changed to 101. Then both BB101 and BB102 can be removed, and
18-
; %x will have 101 incoming entries.
12+
; If we remove BB103, it will increase the number of phi entries by (100 - 1) * 10 = 990.
13+
; If we remove BB101 / BB102, it will increase the number of phi entries by (50 - 1) * 10 = 490.
1914
;
20-
define i16 @example(i32 %a, ptr %array) {
15+
; By default, in SimplifyCFG, we will not remove a block if it will increase more than 1000 phi entries.
16+
; In the first test, BB103 will be removed, and every phi will have 102(3 + 100 - 1) phi entries.
17+
; In the second test, we set max-phi-entries-increase-after-removing-empty-block to be 989, then BB103 should not be removed,
18+
; but BB101 and BB102 can be removed.
19+
; In the third test, we set max-phi-entries-increase-after-removing-empty-block to be 489, then no BB can be removed.
20+
21+
; CHECK-1000: %x1 = phi i16 {{((\[ [0-9], %BB[0-9]+ \], ){101})\[ [0-9], %BB[0-9]+ \]}}
22+
; CHECK-1000: %x2 = phi i16 {{((\[ [0-9], %BB[0-9]+ \], ){101})\[ [0-9], %BB[0-9]+ \]}}
23+
; CHECK-1000: %x3 = phi i16 {{((\[ [0-9], %BB[0-9]+ \], ){101})\[ [0-9], %BB[0-9]+ \]}}
24+
; CHECK-1000: %x4 = phi i16 {{((\[ [0-9], %BB[0-9]+ \], ){101})\[ [0-9], %BB[0-9]+ \]}}
25+
; CHECK-1000: %x5 = phi i16 {{((\[ [0-9], %BB[0-9]+ \], ){101})\[ [0-9], %BB[0-9]+ \]}}
26+
; CHECK-1000: %x6 = phi i16 {{((\[ [0-9], %BB[0-9]+ \], ){101})\[ [0-9], %BB[0-9]+ \]}}
27+
; CHECK-1000: %x7 = phi i16 {{((\[ [0-9], %BB[0-9]+ \], ){101})\[ [0-9], %BB[0-9]+ \]}}
28+
; CHECK-1000: %x8 = phi i16 {{((\[ [0-9], %BB[0-9]+ \], ){101})\[ [0-9], %BB[0-9]+ \]}}
29+
; CHECK-1000: %x9 = phi i16 {{((\[ [0-9], %BB[0-9]+ \], ){101})\[ [0-9], %BB[0-9]+ \]}}
30+
; CHECK-1000: %x10 = phi i16 {{((\[ [0-9], %BB[0-9]+ \], ){101})\[ [0-9], %BB[0-9]+ \]}}
31+
32+
; CHECK-989: %x1 = phi i16 {{((\[ [0-9], %BB[0-9]+ \], ){100})\[ [0-9], %BB[0-9]+ \]}}
33+
; CHECK-989: %x2 = phi i16 {{((\[ [0-9], %BB[0-9]+ \], ){100})\[ [0-9], %BB[0-9]+ \]}}
34+
; CHECK-989: %x3 = phi i16 {{((\[ [0-9], %BB[0-9]+ \], ){100})\[ [0-9], %BB[0-9]+ \]}}
35+
; CHECK-989: %x4 = phi i16 {{((\[ [0-9], %BB[0-9]+ \], ){100})\[ [0-9], %BB[0-9]+ \]}}
36+
; CHECK-989: %x5 = phi i16 {{((\[ [0-9], %BB[0-9]+ \], ){100})\[ [0-9], %BB[0-9]+ \]}}
37+
; CHECK-989: %x6 = phi i16 {{((\[ [0-9], %BB[0-9]+ \], ){100})\[ [0-9], %BB[0-9]+ \]}}
38+
; CHECK-989: %x7 = phi i16 {{((\[ [0-9], %BB[0-9]+ \], ){100})\[ [0-9], %BB[0-9]+ \]}}
39+
; CHECK-989: %x8 = phi i16 {{((\[ [0-9], %BB[0-9]+ \], ){100})\[ [0-9], %BB[0-9]+ \]}}
40+
; CHECK-989: %x9 = phi i16 {{((\[ [0-9], %BB[0-9]+ \], ){100})\[ [0-9], %BB[0-9]+ \]}}
41+
; CHECK-989: %x10 = phi i16 {{((\[ [0-9], %BB[0-9]+ \], ){100})\[ [0-9], %BB[0-9]+ \]}}
42+
43+
; CHECK-489: %x1 = phi i16 {{((\[ [0-9], %BB[0-9]+ \], ){2})\[ [0-9], %BB[0-9]+ \]}}
44+
; CHECK-489: %x2 = phi i16 {{((\[ [0-9], %BB[0-9]+ \], ){2})\[ [0-9], %BB[0-9]+ \]}}
45+
; CHECK-489: %x3 = phi i16 {{((\[ [0-9], %BB[0-9]+ \], ){2})\[ [0-9], %BB[0-9]+ \]}}
46+
; CHECK-489: %x4 = phi i16 {{((\[ [0-9], %BB[0-9]+ \], ){2})\[ [0-9], %BB[0-9]+ \]}}
47+
; CHECK-489: %x5 = phi i16 {{((\[ [0-9], %BB[0-9]+ \], ){2})\[ [0-9], %BB[0-9]+ \]}}
48+
; CHECK-489: %x6 = phi i16 {{((\[ [0-9], %BB[0-9]+ \], ){2})\[ [0-9], %BB[0-9]+ \]}}
49+
; CHECK-489: %x7 = phi i16 {{((\[ [0-9], %BB[0-9]+ \], ){2})\[ [0-9], %BB[0-9]+ \]}}
50+
; CHECK-489: %x8 = phi i16 {{((\[ [0-9], %BB[0-9]+ \], ){2})\[ [0-9], %BB[0-9]+ \]}}
51+
; CHECK-489: %x9 = phi i16 {{((\[ [0-9], %BB[0-9]+ \], ){2})\[ [0-9], %BB[0-9]+ \]}}
52+
; CHECK-489: %x10 = phi i16 {{((\[ [0-9], %BB[0-9]+ \], ){2})\[ [0-9], %BB[0-9]+ \]}}
53+
54+
55+
;
56+
define void @example(i32 %a, ptr %array) {
2157
entry:
2258
switch i32 %a, label %BB1 [
2359
i32 1, label %BB1
@@ -722,16 +758,25 @@ BB100: ; preds = %entry
722758
%cmp100 = icmp eq i32 %val100, 100
723759
br i1 %cmp100, label %BB102, label %BB103
724760

725-
BB101: ; preds = %BB50, %BB49, %BB48, %BB47, %BB46, %BB45, %BB44, %BB43, %BB42, %BB41, %BB40, %BB39, %BB38, %BB37, %BB36, %BB35, %BB34, %BB33, %BB32, %BB31, %BB30, %BB29, %BB28, %BB27, %BB26, %BB25, %BB24, %BB23, %BB22, %BB21, %BB20, %BB19, %BB18, %BB17, %BB16, %BB15, %BB14, %BB13, %BB12, %BB11, %BB10, %BB9, %BB8, %BB7, %BB6, %BB5, %BB4, %BB3, %BB2, %BB1
761+
BB103: ; preds = %BB100, %BB99, %BB98, %BB97, %BB96, %BB95, %BB94, %BB93, %BB92, %BB91, %BB90, %BB89, %BB88, %BB87, %BB86, %BB85, %BB84, %BB83, %BB82, %BB81, %BB80, %BB79, %BB78, %BB77, %BB76, %BB75, %BB74, %BB73, %BB72, %BB71, %BB70, %BB69, %BB68, %BB67, %BB66, %BB65, %BB64, %BB63, %BB62, %BB61, %BB60, %BB59, %BB58, %BB57, %BB56, %BB55, %BB54, %BB53, %BB52, %BB51, %BB50, %BB49, %BB48, %BB47, %BB46, %BB45, %BB44, %BB43, %BB42, %BB41, %BB40, %BB39, %BB38, %BB37, %BB36, %BB35, %BB34, %BB33, %BB32, %BB31, %BB30, %BB29, %BB28, %BB27, %BB26, %BB25, %BB24, %BB23, %BB22, %BB21, %BB20, %BB19, %BB18, %BB17, %BB16, %BB15, %BB14, %BB13, %BB12, %BB11, %BB10, %BB9, %BB8, %BB7, %BB6, %BB5, %BB4, %BB3, %BB2, %BB1
726762
br label %Merge
727763

728-
BB102: ; preds = %BB100, %BB99, %BB98, %BB97, %BB96, %BB95, %BB94, %BB93, %BB92, %BB91, %BB90, %BB89, %BB88, %BB87, %BB86, %BB85, %BB84, %BB83, %BB82, %BB81, %BB80, %BB79, %BB78, %BB77, %BB76, %BB75, %BB74, %BB73, %BB72, %BB71, %BB70, %BB69, %BB68, %BB67, %BB66, %BB65, %BB64, %BB63, %BB62, %BB61, %BB60, %BB59, %BB58, %BB57, %BB56, %BB55, %BB54, %BB53, %BB52, %BB51
764+
BB101: ; preds = %BB50, %BB49, %BB48, %BB47, %BB46, %BB45, %BB44, %BB43, %BB42, %BB41, %BB40, %BB39, %BB38, %BB37, %BB36, %BB35, %BB34, %BB33, %BB32, %BB31, %BB30, %BB29, %BB28, %BB27, %BB26, %BB25, %BB24, %BB23, %BB22, %BB21, %BB20, %BB19, %BB18, %BB17, %BB16, %BB15, %BB14, %BB13, %BB12, %BB11, %BB10, %BB9, %BB8, %BB7, %BB6, %BB5, %BB4, %BB3, %BB2, %BB1
729765
br label %Merge
730766

731-
BB103: ; preds = %BB100, %BB99, %BB98, %BB97, %BB96, %BB95, %BB94, %BB93, %BB92, %BB91, %BB90, %BB89, %BB88, %BB87, %BB86, %BB85, %BB84, %BB83, %BB82, %BB81, %BB80, %BB79, %BB78, %BB77, %BB76, %BB75, %BB74, %BB73, %BB72, %BB71, %BB70, %BB69, %BB68, %BB67, %BB66, %BB65, %BB64, %BB63, %BB62, %BB61, %BB60, %BB59, %BB58, %BB57, %BB56, %BB55, %BB54, %BB53, %BB52, %BB51, %BB50, %BB49, %BB48, %BB47, %BB46, %BB45, %BB44, %BB43, %BB42, %BB41, %BB40, %BB39, %BB38, %BB37, %BB36, %BB35, %BB34, %BB33, %BB32, %BB31, %BB30, %BB29, %BB28, %BB27, %BB26, %BB25, %BB24, %BB23, %BB22, %BB21, %BB20, %BB19, %BB18, %BB17, %BB16, %BB15, %BB14, %BB13, %BB12, %BB11, %BB10, %BB9, %BB8, %BB7, %BB6, %BB5, %BB4, %BB3, %BB2, %BB1
767+
BB102: ; preds = %BB100, %BB99, %BB98, %BB97, %BB96, %BB95, %BB94, %BB93, %BB92, %BB91, %BB90, %BB89, %BB88, %BB87, %BB86, %BB85, %BB84, %BB83, %BB82, %BB81, %BB80, %BB79, %BB78, %BB77, %BB76, %BB75, %BB74, %BB73, %BB72, %BB71, %BB70, %BB69, %BB68, %BB67, %BB66, %BB65, %BB64, %BB63, %BB62, %BB61, %BB60, %BB59, %BB58, %BB57, %BB56, %BB55, %BB54, %BB53, %BB52, %BB51
732768
br label %Merge
733769

734770
Merge: ; preds = %BB103, %BB102, %BB101
735-
%x = phi i16 [ 0, %BB101 ], [ 1, %BB102 ], [ 2, %BB103 ]
736-
ret i16 %x
771+
%x1 = phi i16 [ 1, %BB103 ], [ 0, %BB101 ], [ 2, %BB102 ]
772+
%x2 = phi i16 [ 2 , %BB103 ], [ 0, %BB101 ], [ 2, %BB102 ]
773+
%x3 = phi i16 [ 3 , %BB103 ], [ 0, %BB101 ], [ 2, %BB102 ]
774+
%x4 = phi i16 [ 4 , %BB103 ], [ 0, %BB101 ], [ 2, %BB102 ]
775+
%x5 = phi i16 [ 5 , %BB103 ], [ 0, %BB101 ], [ 2, %BB102 ]
776+
%x6 = phi i16 [ 6 , %BB103 ], [ 0, %BB101 ], [ 2, %BB102 ]
777+
%x7 = phi i16 [ 7 , %BB103 ], [ 0, %BB101 ], [ 2, %BB102 ]
778+
%x8 = phi i16 [ 8 , %BB103 ], [ 0, %BB101 ], [ 2, %BB102 ]
779+
%x9 = phi i16 [ 9 , %BB103 ], [ 0, %BB101 ], [ 2, %BB102 ]
780+
%x10 = phi i16 [ 0, %BB103 ], [ 0, %BB101 ], [ 2, %BB102 ]
781+
ret void
737782
}

0 commit comments

Comments
 (0)