[IR][NFC] Use `SwitchInst::defaultDestUnreachable` (#134199)
diff --git a/llvm/include/llvm/IR/Instructions.h b/llvm/include/llvm/IR/Instructions.h
index 9a4fb29..95f0ef8 100644
--- a/llvm/include/llvm/IR/Instructions.h
+++ b/llvm/include/llvm/IR/Instructions.h
@@ -3366,7 +3366,7 @@
/// Returns true if the default branch must result in immediate undefined
/// behavior, false otherwise.
- bool defaultDestUndefined() const {
+ bool defaultDestUnreachable() const {
return isa<UnreachableInst>(getDefaultDest()->getFirstNonPHIOrDbg());
}
diff --git a/llvm/include/llvm/SandboxIR/Instruction.h b/llvm/include/llvm/SandboxIR/Instruction.h
index 49ea670..ce5a2cb 100644
--- a/llvm/include/llvm/SandboxIR/Instruction.h
+++ b/llvm/include/llvm/SandboxIR/Instruction.h
@@ -1865,8 +1865,8 @@
Value *getCondition() const;
void setCondition(Value *V);
BasicBlock *getDefaultDest() const;
- bool defaultDestUndefined() const {
- return cast<llvm::SwitchInst>(Val)->defaultDestUndefined();
+ bool defaultDestUnreachable() const {
+ return cast<llvm::SwitchInst>(Val)->defaultDestUnreachable();
}
void setDefaultDest(BasicBlock *DefaultCase);
unsigned getNumCases() const {
diff --git a/llvm/lib/Analysis/InlineCost.cpp b/llvm/lib/Analysis/InlineCost.cpp
index 9f193b6..30e1af6 100644
--- a/llvm/lib/Analysis/InlineCost.cpp
+++ b/llvm/lib/Analysis/InlineCost.cpp
@@ -344,7 +344,7 @@
/// Called at the end of processing a switch instruction, with the given
/// number of case clusters.
virtual void onFinalizeSwitch(unsigned JumpTableSize, unsigned NumCaseCluster,
- bool DefaultDestUndefined) {}
+ bool DefaultDestUnreachable) {}
/// Called to account for any other instruction not specifically accounted
/// for.
@@ -722,14 +722,14 @@
}
void onFinalizeSwitch(unsigned JumpTableSize, unsigned NumCaseCluster,
- bool DefaultDestUndefined) override {
+ bool DefaultDestUnreachable) override {
// If suitable for a jump table, consider the cost for the table size and
// branch to destination.
// Maximum valid cost increased in this function.
if (JumpTableSize) {
// Suppose a default branch includes one compare and one conditional
// branch if it's reachable.
- if (!DefaultDestUndefined)
+ if (!DefaultDestUnreachable)
addCost(2 * InstrCost);
// Suppose a jump table requires one load and one jump instruction.
int64_t JTCost =
@@ -742,7 +742,7 @@
// Suppose a comparison includes one compare and one conditional branch.
// We can reduce a set of instructions if the default branch is
// undefined.
- addCost((NumCaseCluster - DefaultDestUndefined) * 2 * InstrCost);
+ addCost((NumCaseCluster - DefaultDestUnreachable) * 2 * InstrCost);
return;
}
@@ -1268,9 +1268,9 @@
}
void onFinalizeSwitch(unsigned JumpTableSize, unsigned NumCaseCluster,
- bool DefaultDestUndefined) override {
+ bool DefaultDestUnreachable) override {
if (JumpTableSize) {
- if (!DefaultDestUndefined)
+ if (!DefaultDestUnreachable)
increment(InlineCostFeatureIndex::switch_default_dest_penalty,
SwitchDefaultDestCostMultiplier * InstrCost);
int64_t JTCost = static_cast<int64_t>(JumpTableSize) * InstrCost +
@@ -1281,7 +1281,7 @@
if (NumCaseCluster <= 3) {
increment(InlineCostFeatureIndex::case_cluster_penalty,
- (NumCaseCluster - DefaultDestUndefined) *
+ (NumCaseCluster - DefaultDestUnreachable) *
CaseClusterCostMultiplier * InstrCost);
return;
}
@@ -2508,7 +2508,7 @@
unsigned NumCaseCluster =
TTI.getEstimatedNumberOfCaseClusters(SI, JumpTableSize, PSI, BFI);
- onFinalizeSwitch(JumpTableSize, NumCaseCluster, SI.defaultDestUndefined());
+ onFinalizeSwitch(JumpTableSize, NumCaseCluster, SI.defaultDestUnreachable());
return false;
}
diff --git a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
index 314a5d1..5226aeb 100644
--- a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
+++ b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
@@ -410,9 +410,8 @@
++ReachableCaseCount;
}
- BasicBlock *DefaultDest = SI->getDefaultDest();
- if (ReachableCaseCount > 1 &&
- !isa<UnreachableInst>(DefaultDest->getFirstNonPHIOrDbg())) {
+ if (ReachableCaseCount > 1 && !SI->defaultDestUnreachable()) {
+ BasicBlock *DefaultDest = SI->getDefaultDest();
ConstantRange CR = LVI->getConstantRangeAtUse(I->getOperandUse(0),
/*UndefAllowed*/ false);
// The default dest is unreachable if all cases are covered.
diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp
index edec0e7..2f3ea22 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -203,10 +203,8 @@
BasicBlock *TheOnlyDest = DefaultDest;
// If the default is unreachable, ignore it when searching for TheOnlyDest.
- if (isa<UnreachableInst>(DefaultDest->getFirstNonPHIOrDbg()) &&
- SI->getNumCases() > 0) {
+ if (SI->defaultDestUnreachable() && SI->getNumCases() > 0)
TheOnlyDest = SI->case_begin()->getCaseSuccessor();
- }
bool Changed = false;
diff --git a/llvm/lib/Transforms/Utils/LowerSwitch.cpp b/llvm/lib/Transforms/Utils/LowerSwitch.cpp
index c1999fe..b70310b 100644
--- a/llvm/lib/Transforms/Utils/LowerSwitch.cpp
+++ b/llvm/lib/Transforms/Utils/LowerSwitch.cpp
@@ -388,7 +388,7 @@
ConstantInt *UpperBound = nullptr;
bool DefaultIsUnreachableFromSwitch = false;
- if (isa<UnreachableInst>(Default->getFirstNonPHIOrDbg())) {
+ if (SI->defaultDestUnreachable()) {
// Make the bounds tightly fitted around the case value range, because we
// know that the value passed to the switch must be exactly one of the case
// values.
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 998677a..e7c550b 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -5715,8 +5715,7 @@
IRBuilder<> &Builder) {
assert(SI->getNumCases() > 1 && "Degenerate switch?");
- bool HasDefault =
- !isa<UnreachableInst>(SI->getDefaultDest()->getFirstNonPHIOrDbg());
+ bool HasDefault = !SI->defaultDestUnreachable();
auto *BB = SI->getParent();
@@ -5879,8 +5878,7 @@
// default destination becomes dead and we can remove it. If we know some
// of the bits in the value, we can use that to more precisely compute the
// number of possible unique case values.
- bool HasDefault =
- !isa<UnreachableInst>(SI->getDefaultDest()->getFirstNonPHIOrDbg());
+ bool HasDefault = !SI->defaultDestUnreachable();
const unsigned NumUnknownBits =
Known.getBitWidth() - (Known.Zero | Known.One).popcount();
assert(NumUnknownBits <= Known.getBitWidth());
@@ -6237,11 +6235,8 @@
// is unreachable.
DefaultResult =
DefaultResults.size() == 1 ? DefaultResults.begin()->second : nullptr;
- if ((!DefaultResult &&
- !isa<UnreachableInst>(DefaultDest->getFirstNonPHIOrDbg())))
- return false;
- return true;
+ return DefaultResult || SI->defaultDestUnreachable();
}
// Helper function that checks if it is possible to transform a switch with only
@@ -6948,7 +6943,7 @@
// If the default destination is unreachable, or if the lookup table covers
// all values of the conditional variable, branch directly to the lookup table
// BB. Otherwise, check that the condition is within the case range.
- bool DefaultIsReachable = !SI->defaultDestUndefined();
+ bool DefaultIsReachable = !SI->defaultDestUnreachable();
bool TableHasHoles = (NumResults < TableSize);
@@ -7281,7 +7276,7 @@
// We perform this optimization only for switches with
// unreachable default case.
// This assumtion will save us from checking if `Condition` is a power of two.
- if (!isa<UnreachableInst>(SI->getDefaultDest()->getFirstNonPHIOrDbg()))
+ if (!SI->defaultDestUnreachable())
return false;
// Check that switch cases are powers of two.
@@ -7363,7 +7358,7 @@
assert(Missing.size() == 1 && "Should have one case left");
Res = *Missing.begin();
- } else if (SI->getNumCases() == 3 && SI->defaultDestUndefined()) {
+ } else if (SI->getNumCases() == 3 && SI->defaultDestUnreachable()) {
// Normalize so that Succ is taken once and OtherSucc twice.
Unreachable = SI->getDefaultDest();
Succ = OtherSucc = nullptr;
diff --git a/llvm/unittests/SandboxIR/SandboxIRTest.cpp b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
index bdc9c2c..bac2e888 100644
--- a/llvm/unittests/SandboxIR/SandboxIRTest.cpp
+++ b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
@@ -4222,8 +4222,9 @@
EXPECT_EQ(Switch->getDefaultDest(),
Ctx.getValue(LLVMSwitch->getDefaultDest()));
EXPECT_EQ(Switch->getDefaultDest(), Default);
- // Check defaultDestUndefined().
- EXPECT_EQ(Switch->defaultDestUndefined(), LLVMSwitch->defaultDestUndefined());
+ // Check defaultDestUnreachable().
+ EXPECT_EQ(Switch->defaultDestUnreachable(),
+ LLVMSwitch->defaultDestUnreachable());
// Check setDefaultDest().
auto *OrigDefaultDest = Switch->getDefaultDest();
auto *NewDefaultDest = Entry;