Skip to content

Commit 0733381

Browse files
committed
Revert "Revert "Revert "Recommit "Revert "[CVP] processSwitch: Remove default case when switch cover all possible values."""""
This reverts commit c93f93b.
1 parent 8ec0f22 commit 0733381

File tree

5 files changed

+26
-57
lines changed

5 files changed

+26
-57
lines changed

llvm/include/llvm/Transforms/Utils/Local.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ class MDNode;
5555
class MemorySSAUpdater;
5656
class PHINode;
5757
class StoreInst;
58-
class SwitchInst;
5958
class TargetLibraryInfo;
6059
class TargetTransformInfo;
6160

@@ -238,10 +237,6 @@ CallInst *createCallMatchingInvoke(InvokeInst *II);
238237
/// This function converts the specified invoek into a normall call.
239238
void changeToCall(InvokeInst *II, DomTreeUpdater *DTU = nullptr);
240239

241-
/// This function removes the default destination from the specified switch.
242-
void createUnreachableSwitchDefault(SwitchInst *Switch,
243-
DomTreeUpdater *DTU = nullptr);
244-
245240
///===---------------------------------------------------------------------===//
246241
/// Dbg Intrinsic utilities
247242
///

llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -376,13 +376,7 @@ static bool processSwitch(SwitchInst *I, LazyValueInfo *LVI,
376376
// ConstantFoldTerminator() as the underlying SwitchInst can be changed.
377377
SwitchInstProfUpdateWrapper SI(*I);
378378

379-
APInt Low =
380-
APInt::getSignedMaxValue(Cond->getType()->getScalarSizeInBits());
381-
APInt High =
382-
APInt::getSignedMinValue(Cond->getType()->getScalarSizeInBits());
383-
384-
SwitchInst::CaseIt CI = SI->case_begin();
385-
for (auto CE = SI->case_end(); CI != CE;) {
379+
for (auto CI = SI->case_begin(), CE = SI->case_end(); CI != CE;) {
386380
ConstantInt *Case = CI->getCaseValue();
387381
LazyValueInfo::Tristate State =
388382
LVI->getPredicateAt(CmpInst::ICMP_EQ, Cond, Case, I,
@@ -415,28 +409,9 @@ static bool processSwitch(SwitchInst *I, LazyValueInfo *LVI,
415409
break;
416410
}
417411

418-
// Get Lower/Upper bound from switch cases.
419-
Low = APIntOps::smin(Case->getValue(), Low);
420-
High = APIntOps::smax(Case->getValue(), High);
421-
422412
// Increment the case iterator since we didn't delete it.
423413
++CI;
424414
}
425-
426-
// Try to simplify default case as unreachable
427-
if (CI == SI->case_end() && SI->getNumCases() != 0 &&
428-
!isa<UnreachableInst>(SI->getDefaultDest()->getFirstNonPHIOrDbg())) {
429-
const ConstantRange SIRange =
430-
LVI->getConstantRange(SI->getCondition(), SI);
431-
432-
// If the numbered switch cases cover the entire range of the condition,
433-
// then the default case is not reachable.
434-
if (SIRange.getSignedMin() == Low && SIRange.getSignedMax() == High &&
435-
SI->getNumCases() == High - Low + 1) {
436-
createUnreachableSwitchDefault(SI, &DTU);
437-
Changed = true;
438-
}
439-
}
440415
}
441416

442417
if (Changed)

llvm/lib/Transforms/Utils/Local.cpp

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2190,26 +2190,6 @@ void llvm::changeToCall(InvokeInst *II, DomTreeUpdater *DTU) {
21902190
DTU->applyUpdates({{DominatorTree::Delete, BB, UnwindDestBB}});
21912191
}
21922192

2193-
void llvm::createUnreachableSwitchDefault(SwitchInst *Switch,
2194-
DomTreeUpdater *DTU) {
2195-
LLVM_DEBUG(dbgs() << "SimplifyCFG: switch default is dead.\n");
2196-
auto *BB = Switch->getParent();
2197-
auto *OrigDefaultBlock = Switch->getDefaultDest();
2198-
OrigDefaultBlock->removePredecessor(BB);
2199-
BasicBlock *NewDefaultBlock = BasicBlock::Create(
2200-
BB->getContext(), BB->getName() + ".unreachabledefault", BB->getParent(),
2201-
OrigDefaultBlock);
2202-
new UnreachableInst(Switch->getContext(), NewDefaultBlock);
2203-
Switch->setDefaultDest(&*NewDefaultBlock);
2204-
if (DTU) {
2205-
SmallVector<DominatorTree::UpdateType, 2> Updates;
2206-
Updates.push_back({DominatorTree::Insert, BB, &*NewDefaultBlock});
2207-
if (!is_contained(successors(BB), OrigDefaultBlock))
2208-
Updates.push_back({DominatorTree::Delete, BB, &*OrigDefaultBlock});
2209-
DTU->applyUpdates(Updates);
2210-
}
2211-
}
2212-
22132193
BasicBlock *llvm::changeToInvokeAndSplitBasicBlock(CallInst *CI,
22142194
BasicBlock *UnwindEdge,
22152195
DomTreeUpdater *DTU) {

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4782,6 +4782,26 @@ static bool CasesAreContiguous(SmallVectorImpl<ConstantInt *> &Cases) {
47824782
return true;
47834783
}
47844784

4785+
static void createUnreachableSwitchDefault(SwitchInst *Switch,
4786+
DomTreeUpdater *DTU) {
4787+
LLVM_DEBUG(dbgs() << "SimplifyCFG: switch default is dead.\n");
4788+
auto *BB = Switch->getParent();
4789+
auto *OrigDefaultBlock = Switch->getDefaultDest();
4790+
OrigDefaultBlock->removePredecessor(BB);
4791+
BasicBlock *NewDefaultBlock = BasicBlock::Create(
4792+
BB->getContext(), BB->getName() + ".unreachabledefault", BB->getParent(),
4793+
OrigDefaultBlock);
4794+
new UnreachableInst(Switch->getContext(), NewDefaultBlock);
4795+
Switch->setDefaultDest(&*NewDefaultBlock);
4796+
if (DTU) {
4797+
SmallVector<DominatorTree::UpdateType, 2> Updates;
4798+
Updates.push_back({DominatorTree::Insert, BB, &*NewDefaultBlock});
4799+
if (!is_contained(successors(BB), OrigDefaultBlock))
4800+
Updates.push_back({DominatorTree::Delete, BB, &*OrigDefaultBlock});
4801+
DTU->applyUpdates(Updates);
4802+
}
4803+
}
4804+
47854805
/// Turn a switch with two reachable destinations into an integer range
47864806
/// comparison and branch.
47874807
bool SimplifyCFGOpt::TurnSwitchRangeIntoICmp(SwitchInst *SI,

llvm/test/Transforms/CorrelatedValuePropagation/basic.ll

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ define i32 @switch_range(i32 %cond) {
382382
; CHECK-NEXT: entry:
383383
; CHECK-NEXT: [[S:%.*]] = urem i32 [[COND:%.*]], 3
384384
; CHECK-NEXT: [[S1:%.*]] = add nuw nsw i32 [[S]], 1
385-
; CHECK-NEXT: switch i32 [[S1]], label [[ENTRY_UNREACHABLEDEFAULT:%.*]] [
385+
; CHECK-NEXT: switch i32 [[S1]], label [[UNREACHABLE:%.*]] [
386386
; CHECK-NEXT: i32 1, label [[EXIT1:%.*]]
387387
; CHECK-NEXT: i32 2, label [[EXIT2:%.*]]
388388
; CHECK-NEXT: i32 3, label [[EXIT1]]
@@ -391,8 +391,6 @@ define i32 @switch_range(i32 %cond) {
391391
; CHECK-NEXT: ret i32 1
392392
; CHECK: exit2:
393393
; CHECK-NEXT: ret i32 2
394-
; CHECK: entry.unreachabledefault:
395-
; CHECK-NEXT: unreachable
396394
; CHECK: unreachable:
397395
; CHECK-NEXT: ret i32 0
398396
;
@@ -455,9 +453,10 @@ define i8 @switch_defaultdest_multipleuse(i8 %t0) {
455453
; CHECK-NEXT: entry:
456454
; CHECK-NEXT: [[O:%.*]] = or i8 [[T0:%.*]], 1
457455
; CHECK-NEXT: [[R:%.*]] = srem i8 1, [[O]]
458-
; CHECK-NEXT: br label [[EXIT:%.*]]
459-
; CHECK: entry.unreachabledefault:
460-
; CHECK-NEXT: unreachable
456+
; CHECK-NEXT: switch i8 [[R]], label [[EXIT:%.*]] [
457+
; CHECK-NEXT: i8 0, label [[EXIT]]
458+
; CHECK-NEXT: i8 1, label [[EXIT]]
459+
; CHECK-NEXT: ]
461460
; CHECK: exit:
462461
; CHECK-NEXT: ret i8 0
463462
;

0 commit comments

Comments
 (0)