Skip to content

Commit 0f8745b

Browse files
committed
Revert "[SimplifyCFG] When only one case value is missing, replace default with that case (#76669)"
This reverts commit 7d81e07, which introduces a compiler memory usage regression. See #76669 (comment)
1 parent af9be74 commit 0f8745b

File tree

3 files changed

+10
-142
lines changed

3 files changed

+10
-142
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

+7-28
Original file line numberDiff line numberDiff line change
@@ -5414,13 +5414,11 @@ static bool CasesAreContiguous(SmallVectorImpl<ConstantInt *> &Cases) {
54145414
}
54155415

54165416
static void createUnreachableSwitchDefault(SwitchInst *Switch,
5417-
DomTreeUpdater *DTU,
5418-
bool RemoveOrigDefaultBlock = true) {
5417+
DomTreeUpdater *DTU) {
54195418
LLVM_DEBUG(dbgs() << "SimplifyCFG: switch default is dead.\n");
54205419
auto *BB = Switch->getParent();
54215420
auto *OrigDefaultBlock = Switch->getDefaultDest();
5422-
if (RemoveOrigDefaultBlock)
5423-
OrigDefaultBlock->removePredecessor(BB);
5421+
OrigDefaultBlock->removePredecessor(BB);
54245422
BasicBlock *NewDefaultBlock = BasicBlock::Create(
54255423
BB->getContext(), BB->getName() + ".unreachabledefault", BB->getParent(),
54265424
OrigDefaultBlock);
@@ -5429,8 +5427,7 @@ static void createUnreachableSwitchDefault(SwitchInst *Switch,
54295427
if (DTU) {
54305428
SmallVector<DominatorTree::UpdateType, 2> Updates;
54315429
Updates.push_back({DominatorTree::Insert, BB, &*NewDefaultBlock});
5432-
if (RemoveOrigDefaultBlock &&
5433-
!is_contained(successors(BB), OrigDefaultBlock))
5430+
if (!is_contained(successors(BB), OrigDefaultBlock))
54345431
Updates.push_back({DominatorTree::Delete, BB, &*OrigDefaultBlock});
54355432
DTU->applyUpdates(Updates);
54365433
}
@@ -5612,28 +5609,10 @@ static bool eliminateDeadSwitchCases(SwitchInst *SI, DomTreeUpdater *DTU,
56125609
Known.getBitWidth() - (Known.Zero | Known.One).popcount();
56135610
assert(NumUnknownBits <= Known.getBitWidth());
56145611
if (HasDefault && DeadCases.empty() &&
5615-
NumUnknownBits < 64 /* avoid overflow */) {
5616-
uint64_t AllNumCases = 1ULL << NumUnknownBits;
5617-
if (SI->getNumCases() == AllNumCases) {
5618-
createUnreachableSwitchDefault(SI, DTU);
5619-
return true;
5620-
}
5621-
// When only one case value is missing, replace default with that case.
5622-
// Eliminating the default branch will provide more opportunities for
5623-
// optimization, such as lookup tables.
5624-
if (SI->getNumCases() == AllNumCases - 1) {
5625-
assert(NumUnknownBits > 1 && "Should be canonicalized to a branch");
5626-
uint64_t MissingCaseVal = 0;
5627-
for (const auto &Case : SI->cases())
5628-
MissingCaseVal ^= Case.getCaseValue()->getValue().getLimitedValue();
5629-
auto *MissingCase =
5630-
cast<ConstantInt>(ConstantInt::get(Cond->getType(), MissingCaseVal));
5631-
SwitchInstProfUpdateWrapper SIW(*SI);
5632-
SIW.addCase(MissingCase, SI->getDefaultDest(), SIW.getSuccessorWeight(0));
5633-
createUnreachableSwitchDefault(SI, DTU, /*RemoveOrigDefaultBlock*/ false);
5634-
SIW.setSuccessorWeight(0, 0);
5635-
return true;
5636-
}
5612+
NumUnknownBits < 64 /* avoid overflow */ &&
5613+
SI->getNumCases() == (1ULL << NumUnknownBits)) {
5614+
createUnreachableSwitchDefault(SI, DTU);
5615+
return true;
56375616
}
56385617

56395618
if (DeadCases.empty())

llvm/test/Transforms/SimplifyCFG/switch-dead-default-lookup-table.ll

-61
This file was deleted.

llvm/test/Transforms/SimplifyCFG/switch-dead-default.ll

+3-53
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ default:
7979
ret void
8080
}
8181

82-
; We can replace the default branch with case 3 since it is the only case that is missing.
82+
; This one is a negative test - we know the value of the default,
83+
; but that's about it
8384
define void @test3(i2 %a) {
8485
; CHECK-LABEL: define void @test3(
8586
; CHECK-SAME: i2 [[A:%.*]]) {
86-
; CHECK-NEXT: switch i2 [[A]], label [[DOTUNREACHABLEDEFAULT:%.*]] [
87+
; CHECK-NEXT: switch i2 [[A]], label [[DEFAULT:%.*]] [
8788
; CHECK-NEXT: i2 0, label [[CASE0:%.*]]
8889
; CHECK-NEXT: i2 1, label [[CASE1:%.*]]
8990
; CHECK-NEXT: i2 -2, label [[CASE2:%.*]]
90-
; CHECK-NEXT: i2 -1, label [[DEFAULT:%.*]]
9191
; CHECK-NEXT: ]
9292
; CHECK: common.ret:
9393
; CHECK-NEXT: ret void
@@ -100,8 +100,6 @@ define void @test3(i2 %a) {
100100
; CHECK: case2:
101101
; CHECK-NEXT: call void @foo(i32 2)
102102
; CHECK-NEXT: br label [[COMMON_RET]]
103-
; CHECK: .unreachabledefault:
104-
; CHECK-NEXT: unreachable
105103
; CHECK: default:
106104
; CHECK-NEXT: call void @foo(i32 3)
107105
; CHECK-NEXT: br label [[COMMON_RET]]
@@ -124,50 +122,6 @@ default:
124122
ret void
125123
}
126124

127-
define void @test3_prof(i2 %a) {
128-
; CHECK-LABEL: define void @test3_prof(
129-
; CHECK-SAME: i2 [[A:%.*]]) {
130-
; CHECK-NEXT: switch i2 [[A]], label [[DOTUNREACHABLEDEFAULT:%.*]] [
131-
; CHECK-NEXT: i2 0, label [[CASE0:%.*]]
132-
; CHECK-NEXT: i2 1, label [[CASE1:%.*]]
133-
; CHECK-NEXT: i2 -2, label [[CASE2:%.*]]
134-
; CHECK-NEXT: i2 -1, label [[DEFAULT:%.*]]
135-
; CHECK-NEXT: ], !prof [[PROF0:![0-9]+]]
136-
; CHECK: common.ret:
137-
; CHECK-NEXT: ret void
138-
; CHECK: case0:
139-
; CHECK-NEXT: call void @foo(i32 0)
140-
; CHECK-NEXT: br label [[COMMON_RET:%.*]]
141-
; CHECK: case1:
142-
; CHECK-NEXT: call void @foo(i32 1)
143-
; CHECK-NEXT: br label [[COMMON_RET]]
144-
; CHECK: case2:
145-
; CHECK-NEXT: call void @foo(i32 2)
146-
; CHECK-NEXT: br label [[COMMON_RET]]
147-
; CHECK: .unreachabledefault:
148-
; CHECK-NEXT: unreachable
149-
; CHECK: default:
150-
; CHECK-NEXT: call void @foo(i32 3)
151-
; CHECK-NEXT: br label [[COMMON_RET]]
152-
;
153-
switch i2 %a, label %default [i2 0, label %case0
154-
i2 1, label %case1
155-
i2 2, label %case2], !prof !0
156-
157-
case0:
158-
call void @foo(i32 0)
159-
ret void
160-
case1:
161-
call void @foo(i32 1)
162-
ret void
163-
case2:
164-
call void @foo(i32 2)
165-
ret void
166-
default:
167-
call void @foo(i32 3)
168-
ret void
169-
}
170-
171125
; Negative test - check for possible overflow when computing
172126
; number of possible cases.
173127
define void @test4(i128 %a) {
@@ -313,7 +267,3 @@ default:
313267

314268
declare void @llvm.assume(i1)
315269

316-
!0 = !{!"branch_weights", i32 8, i32 4, i32 2, i32 1}
317-
;.
318-
; CHECK: [[PROF0]] = !{!"branch_weights", i32 0, i32 4, i32 2, i32 1, i32 8}
319-
;.

0 commit comments

Comments
 (0)