Skip to content

Commit 852f3f0

Browse files
fixup! respond to review
1 parent 3430a66 commit 852f3f0

File tree

2 files changed

+13
-62
lines changed

2 files changed

+13
-62
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7450,7 +7450,7 @@ struct SwitchSuccWrapper {
74507450
// be important to equality though.
74517451
unsigned SuccNum;
74527452
BasicBlock *Dest;
7453-
DenseMap<PHINode *, DenseMap<BasicBlock *, Value *>> *PhiPredIVs;
7453+
DenseMap<PHINode *, SmallDenseMap<BasicBlock *, Value *, 8>> *PhiPredIVs;
74547454
};
74557455

74567456
namespace llvm {
@@ -7473,14 +7473,13 @@ template <> struct DenseMapInfo<const SwitchSuccWrapper *> {
74737473
assert(Succ->size() == 1 && "Expected just a single branch in the BB");
74747474

74757475
// Since we assume the BB is just a single BranchInst with a single
7476-
// succsessor, we hash as the BB and the incoming Values of its successor
7476+
// successor, we hash as the BB and the incoming Values of its successor
74777477
// PHIs. Initially, we tried to just use the successor BB as the hash, but
7478-
// this had poor performance. We find that the extra computation of getting
7479-
// the incoming PHI values here leads to better performance on overall Set
7480-
// performance. We also tried to build a map from BB -> Succs.IncomingValues
7481-
// ahead of time and passing it in SwitchSuccWrapper, but this slowed down
7482-
// the average compile time without having any impact on the worst case
7483-
// compile time.
7478+
// including the incoming PHI values leads to better performance.
7479+
// We also tried to build a map from BB -> Succs.IncomingValues ahead of
7480+
// time and passing it in SwitchSuccWrapper, but this slowed down the
7481+
// average compile time without having any impact on the worst case compile
7482+
// time.
74847483
BasicBlock *BB = BI->getSuccessor(0);
74857484
SmallVector<Value *> PhiValsForBB;
74867485
for (PHINode &Phi : BB->phis())
@@ -7535,7 +7534,7 @@ bool SimplifyCFGOpt::simplifyDuplicateSwitchArms(SwitchInst *SI,
75357534
// getIncomingValueForBlock is O(|Preds|).
75367535
SmallPtrSet<PHINode *, 8> Phis;
75377536
SmallPtrSet<BasicBlock *, 8> Seen;
7538-
DenseMap<PHINode *, DenseMap<BasicBlock *, Value *>> PhiPredIVs;
7537+
DenseMap<PHINode *, SmallDenseMap<BasicBlock *, Value *, 8>> PhiPredIVs;
75397538
SmallVector<SwitchSuccWrapper> Cases;
75407539
Cases.reserve(SI->getNumSuccessors());
75417540

@@ -7547,10 +7546,6 @@ bool SimplifyCFGOpt::simplifyDuplicateSwitchArms(SwitchInst *SI,
75477546
if (BB->size() != 1)
75487547
continue;
75497548

7550-
Instruction *T = BB->getTerminator();
7551-
if (!T)
7552-
continue;
7553-
75547549
// FIXME: This case needs some extra care because the terminators other than
75557550
// SI need to be updated.
75567551
if (BB->hasNPredecessorsOrMore(2))
@@ -7559,7 +7554,7 @@ bool SimplifyCFGOpt::simplifyDuplicateSwitchArms(SwitchInst *SI,
75597554
// FIXME: Relax that the terminator is a BranchInst by checking for equality
75607555
// on other kinds of terminators. We decide to only support unconditional
75617556
// branches for now for compile time reasons.
7562-
auto *BI = dyn_cast<BranchInst>(T);
7557+
auto *BI = dyn_cast<BranchInst>(BB->getTerminator());
75637558
if (!BI || BI->isConditional())
75647559
continue;
75657560

@@ -7577,7 +7572,7 @@ bool SimplifyCFGOpt::simplifyDuplicateSwitchArms(SwitchInst *SI,
75777572
PhiPredIVs.reserve(Phis.size());
75787573
for (PHINode *Phi : Phis) {
75797574
PhiPredIVs[Phi] =
7580-
DenseMap<BasicBlock *, Value *>(Phi->getNumIncomingValues());
7575+
SmallDenseMap<BasicBlock *, Value *, 8>(Phi->getNumIncomingValues());
75817576
for (auto &IV : Phi->incoming_values())
75827577
PhiPredIVs[Phi].insert({Phi->getIncomingBlock(IV), IV.get()});
75837578
}
@@ -7590,8 +7585,7 @@ bool SimplifyCFGOpt::simplifyDuplicateSwitchArms(SwitchInst *SI,
75907585
// SwitchSuccWrapper instead of just BasicBlock because we'd like to pass
75917586
// around information to isEquality, getHashValue, and when doing the
75927587
// replacement with better performance.
7593-
DenseSet<const SwitchSuccWrapper *, DenseMapInfo<const SwitchSuccWrapper *>>
7594-
ReplaceWith;
7588+
DenseSet<const SwitchSuccWrapper *> ReplaceWith;
75957589
ReplaceWith.reserve(Cases.size());
75967590

75977591
SmallVector<DominatorTree::UpdateType> Updates;

llvm/test/Transforms/SimplifyCFG/switch-dup-bbs.ll

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2-
; RUN: opt < %s -passes=simplifycfg -S | FileCheck %s -check-prefix=SIMPLIFY-CFG
3-
; RUN: opt < %s -O3 -S | FileCheck %s -check-prefix=O3
2+
; RUN: opt < %s -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S \
3+
; RUN: | FileCheck %s -check-prefix=SIMPLIFY-CFG
44

55
define i32 @switch_all_duplicate_arms(i32 %0, i32 %1, i32 %2, i32 %3) {
66
; SIMPLIFY-CFG-LABEL: define i32 @switch_all_duplicate_arms(
@@ -14,12 +14,6 @@ define i32 @switch_all_duplicate_arms(i32 %0, i32 %1, i32 %2, i32 %3) {
1414
; SIMPLIFY-CFG: [[BB6]]:
1515
; SIMPLIFY-CFG-NEXT: [[TMP8:%.*]] = phi i32 [ [[TMP3]], [[TMP4:%.*]] ], [ [[TMP2]], %[[BB5]] ]
1616
; SIMPLIFY-CFG-NEXT: ret i32 [[TMP8]]
17-
;
18-
; O3-LABEL: define i32 @switch_all_duplicate_arms(
19-
; O3-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]], i32 [[TMP2:%.*]], i32 [[TMP3:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] {
20-
; O3-NEXT: [[SWITCH:%.*]] = icmp ult i32 [[TMP1]], 2
21-
; O3-NEXT: [[TMP8:%.*]] = select i1 [[SWITCH]], i32 [[TMP2]], i32 [[TMP3]]
22-
; O3-NEXT: ret i32 [[TMP8]]
2317
;
2418
switch i32 %1, label %7 [
2519
i32 0, label %5
@@ -52,21 +46,6 @@ define i32 @switch_some_duplicate_arms(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4) {
5246
; SIMPLIFY-CFG: [[BB8]]:
5347
; SIMPLIFY-CFG-NEXT: [[TMP10:%.*]] = phi i32 [ [[TMP3]], [[TMP5:%.*]] ], [ [[TMP4]], %[[BB7]] ], [ [[TMP2]], %[[BB6]] ]
5448
; SIMPLIFY-CFG-NEXT: ret i32 [[TMP10]]
55-
;
56-
; O3-LABEL: define i32 @switch_some_duplicate_arms(
57-
; O3-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]], i32 [[TMP2:%.*]], i32 [[TMP3:%.*]], i32 [[TMP4:%.*]]) local_unnamed_addr #[[ATTR0]] {
58-
; O3-NEXT: switch i32 [[TMP1]], label %[[BB8:.*]] [
59-
; O3-NEXT: i32 0, label %[[BB6:.*]]
60-
; O3-NEXT: i32 1, label %[[BB6]]
61-
; O3-NEXT: i32 2, label %[[BB7:.*]]
62-
; O3-NEXT: ]
63-
; O3: [[BB6]]:
64-
; O3-NEXT: br label %[[BB8]]
65-
; O3: [[BB7]]:
66-
; O3-NEXT: br label %[[BB8]]
67-
; O3: [[BB8]]:
68-
; O3-NEXT: [[TMP10:%.*]] = phi i32 [ [[TMP3]], [[TMP5:%.*]] ], [ [[TMP4]], %[[BB7]] ], [ [[TMP2]], %[[BB6]] ]
69-
; O3-NEXT: ret i32 [[TMP10]]
7049
;
7150
switch i32 %1, label %9 [
7251
i32 0, label %6
@@ -104,22 +83,6 @@ define i32 @switch_duplicate_arms_multipred(i1 %0, i32 %1, i32 %2, i32 %3, i32 %
10483
; SIMPLIFY-CFG: [[BB9]]:
10584
; SIMPLIFY-CFG-NEXT: [[TMP10:%.*]] = phi i32 [ [[TMP4]], %[[BB6]] ], [ [[TMP3]], %[[BB8]] ], [ [[TMP3]], %[[BB7]] ]
10685
; SIMPLIFY-CFG-NEXT: ret i32 [[TMP10]]
107-
;
108-
; O3-LABEL: define i32 @switch_duplicate_arms_multipred(
109-
; O3-SAME: i1 [[TMP0:%.*]], i32 [[TMP1:%.*]], i32 [[TMP2:%.*]], i32 [[TMP3:%.*]], i32 [[TMP4:%.*]]) local_unnamed_addr #[[ATTR0]] {
110-
; O3-NEXT: br i1 [[TMP0]], label %[[BB6:.*]], label %[[BB7:.*]]
111-
; O3: [[BB6]]:
112-
; O3-NEXT: switch i32 [[TMP2]], label %[[BB9:.*]] [
113-
; O3-NEXT: i32 0, label %[[BB7]]
114-
; O3-NEXT: i32 1, label %[[BB8:.*]]
115-
; O3-NEXT: ]
116-
; O3: [[BB7]]:
117-
; O3-NEXT: br label %[[BB9]]
118-
; O3: [[BB8]]:
119-
; O3-NEXT: br label %[[BB9]]
120-
; O3: [[BB9]]:
121-
; O3-NEXT: [[TMP10:%.*]] = phi i32 [ [[TMP4]], %[[BB6]] ], [ [[TMP3]], %[[BB8]] ], [ [[TMP3]], %[[BB7]] ]
122-
; O3-NEXT: ret i32 [[TMP10]]
12386
;
12487
br i1 %0, label %6, label %7
12588
6:
@@ -145,12 +108,6 @@ define i32 @switch_dup_default(i32 %0, i32 %1, i32 %2, i32 %3) {
145108
; SIMPLIFY-CFG-NEXT: [[COND:%.*]] = icmp eq i32 [[TMP1]], 0
146109
; SIMPLIFY-CFG-NEXT: [[TMP8:%.*]] = select i1 [[COND]], i32 [[TMP3]], i32 [[TMP2]]
147110
; SIMPLIFY-CFG-NEXT: ret i32 [[TMP8]]
148-
;
149-
; O3-LABEL: define i32 @switch_dup_default(
150-
; O3-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]], i32 [[TMP2:%.*]], i32 [[TMP3:%.*]]) local_unnamed_addr #[[ATTR0]] {
151-
; O3-NEXT: [[COND:%.*]] = icmp eq i32 [[TMP1]], 0
152-
; O3-NEXT: [[TMP8:%.*]] = select i1 [[COND]], i32 [[TMP3]], i32 [[TMP2]]
153-
; O3-NEXT: ret i32 [[TMP8]]
154111
;
155112
switch i32 %1, label %7 [
156113
i32 0, label %5

0 commit comments

Comments
 (0)