Skip to content

Commit 57100f6

Browse files
alexfhampandey-1995
authored andcommitted
Revert "[SimplifyCFG] switch: Do Not Transform the Default Case if the Condition is Too Wide" (llvm#78469)
Reverts llvm#77831, which depends on llvm#76669, which seriously regresses compilation time / memory usage see llvm#76669 (comment).
1 parent e6d6f49 commit 57100f6

File tree

3 files changed

+10
-180
lines changed

3 files changed

+10
-180
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

+7-33
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,33 +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-
IntegerType *CondTy = cast<IntegerType>(Cond->getType());
5627-
if (CondTy->getIntegerBitWidth() > 64 ||
5628-
!DL.fitsInLegalInteger(CondTy->getIntegerBitWidth()))
5629-
return false;
5630-
5631-
uint64_t MissingCaseVal = 0;
5632-
for (const auto &Case : SI->cases())
5633-
MissingCaseVal ^= Case.getCaseValue()->getValue().getLimitedValue();
5634-
auto *MissingCase =
5635-
cast<ConstantInt>(ConstantInt::get(Cond->getType(), MissingCaseVal));
5636-
SwitchInstProfUpdateWrapper SIW(*SI);
5637-
SIW.addCase(MissingCase, SI->getDefaultDest(), SIW.getSuccessorWeight(0));
5638-
createUnreachableSwitchDefault(SI, DTU, /*RemoveOrigDefaultBlock*/ false);
5639-
SIW.setSuccessorWeight(0, 0);
5640-
return true;
5641-
}
5612+
NumUnknownBits < 64 /* avoid overflow */ &&
5613+
SI->getNumCases() == (1ULL << NumUnknownBits)) {
5614+
createUnreachableSwitchDefault(SI, DTU);
5615+
return true;
56425616
}
56435617

56445618
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-86
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,40 +267,3 @@ default:
313267

314268
declare void @llvm.assume(i1)
315269

316-
define zeroext i1 @test8(i128 %a) {
317-
; We should not transform conditions wider than 64 bit.
318-
; CHECK-LABEL: define zeroext i1 @test8(
319-
; CHECK-SAME: i128 [[A:%.*]]) {
320-
; CHECK-NEXT: entry:
321-
; CHECK-NEXT: [[TMP0:%.*]] = and i128 [[A]], 3894222643901120721397872246915072
322-
; CHECK-NEXT: switch i128 [[TMP0]], label [[LOR_RHS:%.*]] [
323-
; CHECK-NEXT: i128 1298074214633706907132624082305024, label [[LOR_END:%.*]]
324-
; CHECK-NEXT: i128 2596148429267413814265248164610048, label [[LOR_END]]
325-
; CHECK-NEXT: i128 3894222643901120721397872246915072, label [[LOR_END]]
326-
; CHECK-NEXT: ]
327-
; CHECK: lor.rhs:
328-
; CHECK-NEXT: br label [[LOR_END]]
329-
; CHECK: lor.end:
330-
; CHECK-NEXT: [[TMP1:%.*]] = phi i1 [ true, [[ENTRY:%.*]] ], [ false, [[LOR_RHS]] ], [ true, [[ENTRY]] ], [ true, [[ENTRY]] ]
331-
; CHECK-NEXT: ret i1 [[TMP1]]
332-
;
333-
entry:
334-
%0 = and i128 %a, 3894222643901120721397872246915072
335-
switch i128 %0, label %lor.rhs [
336-
i128 1298074214633706907132624082305024, label %lor.end
337-
i128 2596148429267413814265248164610048, label %lor.end
338-
i128 3894222643901120721397872246915072, label %lor.end
339-
]
340-
341-
lor.rhs: ; preds = %entry
342-
br label %lor.end
343-
344-
lor.end: ; preds = %entry, %entry, %entry, %lor.rhs
345-
%1 = phi i1 [ true, %entry ], [ false, %lor.rhs ], [ true, %entry ], [ true, %entry ]
346-
ret i1 %1
347-
}
348-
349-
!0 = !{!"branch_weights", i32 8, i32 4, i32 2, i32 1}
350-
;.
351-
; CHECK: [[PROF0]] = !{!"branch_weights", i32 0, i32 4, i32 2, i32 1, i32 8}
352-
;.

0 commit comments

Comments
 (0)