Skip to content

Commit 3a998c0

Browse files
committed
Revert "Recommit "Revert "[CVP] processSwitch: Remove default case when switch cover all possible values."""
This reverts commit 8ba2adc.
1 parent e2eb651 commit 3a998c0

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

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

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

llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp

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

344-
APInt Low =
345-
APInt::getSignedMaxValue(Cond->getType()->getScalarSizeInBits());
346-
APInt High =
347-
APInt::getSignedMinValue(Cond->getType()->getScalarSizeInBits());
348-
349-
SwitchInst::CaseIt CI = SI->case_begin();
350-
for (auto CE = SI->case_end(); CI != CE;) {
344+
for (auto CI = SI->case_begin(), CE = SI->case_end(); CI != CE;) {
351345
ConstantInt *Case = CI->getCaseValue();
352346
LazyValueInfo::Tristate State =
353347
LVI->getPredicateAt(CmpInst::ICMP_EQ, Cond, Case, I,
@@ -380,28 +374,9 @@ static bool processSwitch(SwitchInst *I, LazyValueInfo *LVI,
380374
break;
381375
}
382376

383-
// Get Lower/Upper bound from switch cases.
384-
Low = APIntOps::smin(Case->getValue(), Low);
385-
High = APIntOps::smax(Case->getValue(), High);
386-
387377
// Increment the case iterator since we didn't delete it.
388378
++CI;
389379
}
390-
391-
// Try to simplify default case as unreachable
392-
if (CI == SI->case_end() && SI->getNumCases() != 0 &&
393-
!isa<UnreachableInst>(SI->getDefaultDest()->getFirstNonPHIOrDbg())) {
394-
const ConstantRange SIRange =
395-
LVI->getConstantRange(SI->getCondition(), SI);
396-
397-
// If the numbered switch cases cover the entire range of the condition,
398-
// then the default case is not reachable.
399-
if (SIRange.getSignedMin() == Low && SIRange.getSignedMax() == High &&
400-
SI->getNumCases() == High - Low + 1) {
401-
createUnreachableSwitchDefault(SI, &DTU);
402-
Changed = true;
403-
}
404-
}
405380
}
406381

407382
if (Changed)

llvm/lib/Transforms/Utils/Local.cpp

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

2185-
void llvm::createUnreachableSwitchDefault(SwitchInst *Switch,
2186-
DomTreeUpdater *DTU) {
2187-
LLVM_DEBUG(dbgs() << "SimplifyCFG: switch default is dead.\n");
2188-
auto *BB = Switch->getParent();
2189-
auto *OrigDefaultBlock = Switch->getDefaultDest();
2190-
OrigDefaultBlock->removePredecessor(BB);
2191-
BasicBlock *NewDefaultBlock = BasicBlock::Create(
2192-
BB->getContext(), BB->getName() + ".unreachabledefault", BB->getParent(),
2193-
OrigDefaultBlock);
2194-
new UnreachableInst(Switch->getContext(), NewDefaultBlock);
2195-
Switch->setDefaultDest(&*NewDefaultBlock);
2196-
if (DTU) {
2197-
SmallVector<DominatorTree::UpdateType, 2> Updates;
2198-
Updates.push_back({DominatorTree::Insert, BB, &*NewDefaultBlock});
2199-
if (!is_contained(successors(BB), OrigDefaultBlock))
2200-
Updates.push_back({DominatorTree::Delete, BB, &*OrigDefaultBlock});
2201-
DTU->applyUpdates(Updates);
2202-
}
2203-
}
2204-
22052185
BasicBlock *llvm::changeToInvokeAndSplitBasicBlock(CallInst *CI,
22062186
BasicBlock *UnwindEdge,
22072187
DomTreeUpdater *DTU) {

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

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

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