Skip to content

Commit b6c0ce0

Browse files
authored
[IR][NFC] Use SwitchInst::defaultDestUnreachable (#134199)
1 parent 3295970 commit b6c0ce0

File tree

8 files changed

+24
-31
lines changed

8 files changed

+24
-31
lines changed

llvm/include/llvm/IR/Instructions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3366,7 +3366,7 @@ class SwitchInst : public Instruction {
33663366

33673367
/// Returns true if the default branch must result in immediate undefined
33683368
/// behavior, false otherwise.
3369-
bool defaultDestUndefined() const {
3369+
bool defaultDestUnreachable() const {
33703370
return isa<UnreachableInst>(getDefaultDest()->getFirstNonPHIOrDbg());
33713371
}
33723372

llvm/include/llvm/SandboxIR/Instruction.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,8 +1865,8 @@ class SwitchInst : public SingleLLVMInstructionImpl<llvm::SwitchInst> {
18651865
Value *getCondition() const;
18661866
void setCondition(Value *V);
18671867
BasicBlock *getDefaultDest() const;
1868-
bool defaultDestUndefined() const {
1869-
return cast<llvm::SwitchInst>(Val)->defaultDestUndefined();
1868+
bool defaultDestUnreachable() const {
1869+
return cast<llvm::SwitchInst>(Val)->defaultDestUnreachable();
18701870
}
18711871
void setDefaultDest(BasicBlock *DefaultCase);
18721872
unsigned getNumCases() const {

llvm/lib/Analysis/InlineCost.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ class CallAnalyzer : public InstVisitor<CallAnalyzer, bool> {
344344
/// Called at the end of processing a switch instruction, with the given
345345
/// number of case clusters.
346346
virtual void onFinalizeSwitch(unsigned JumpTableSize, unsigned NumCaseCluster,
347-
bool DefaultDestUndefined) {}
347+
bool DefaultDestUnreachable) {}
348348

349349
/// Called to account for any other instruction not specifically accounted
350350
/// for.
@@ -722,14 +722,14 @@ class InlineCostCallAnalyzer final : public CallAnalyzer {
722722
}
723723

724724
void onFinalizeSwitch(unsigned JumpTableSize, unsigned NumCaseCluster,
725-
bool DefaultDestUndefined) override {
725+
bool DefaultDestUnreachable) override {
726726
// If suitable for a jump table, consider the cost for the table size and
727727
// branch to destination.
728728
// Maximum valid cost increased in this function.
729729
if (JumpTableSize) {
730730
// Suppose a default branch includes one compare and one conditional
731731
// branch if it's reachable.
732-
if (!DefaultDestUndefined)
732+
if (!DefaultDestUnreachable)
733733
addCost(2 * InstrCost);
734734
// Suppose a jump table requires one load and one jump instruction.
735735
int64_t JTCost =
@@ -742,7 +742,7 @@ class InlineCostCallAnalyzer final : public CallAnalyzer {
742742
// Suppose a comparison includes one compare and one conditional branch.
743743
// We can reduce a set of instructions if the default branch is
744744
// undefined.
745-
addCost((NumCaseCluster - DefaultDestUndefined) * 2 * InstrCost);
745+
addCost((NumCaseCluster - DefaultDestUnreachable) * 2 * InstrCost);
746746
return;
747747
}
748748

@@ -1268,9 +1268,9 @@ class InlineCostFeaturesAnalyzer final : public CallAnalyzer {
12681268
}
12691269

12701270
void onFinalizeSwitch(unsigned JumpTableSize, unsigned NumCaseCluster,
1271-
bool DefaultDestUndefined) override {
1271+
bool DefaultDestUnreachable) override {
12721272
if (JumpTableSize) {
1273-
if (!DefaultDestUndefined)
1273+
if (!DefaultDestUnreachable)
12741274
increment(InlineCostFeatureIndex::switch_default_dest_penalty,
12751275
SwitchDefaultDestCostMultiplier * InstrCost);
12761276
int64_t JTCost = static_cast<int64_t>(JumpTableSize) * InstrCost +
@@ -1281,7 +1281,7 @@ class InlineCostFeaturesAnalyzer final : public CallAnalyzer {
12811281

12821282
if (NumCaseCluster <= 3) {
12831283
increment(InlineCostFeatureIndex::case_cluster_penalty,
1284-
(NumCaseCluster - DefaultDestUndefined) *
1284+
(NumCaseCluster - DefaultDestUnreachable) *
12851285
CaseClusterCostMultiplier * InstrCost);
12861286
return;
12871287
}
@@ -2508,7 +2508,7 @@ bool CallAnalyzer::visitSwitchInst(SwitchInst &SI) {
25082508
unsigned NumCaseCluster =
25092509
TTI.getEstimatedNumberOfCaseClusters(SI, JumpTableSize, PSI, BFI);
25102510

2511-
onFinalizeSwitch(JumpTableSize, NumCaseCluster, SI.defaultDestUndefined());
2511+
onFinalizeSwitch(JumpTableSize, NumCaseCluster, SI.defaultDestUnreachable());
25122512
return false;
25132513
}
25142514

llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,8 @@ static bool processSwitch(SwitchInst *I, LazyValueInfo *LVI,
410410
++ReachableCaseCount;
411411
}
412412

413-
BasicBlock *DefaultDest = SI->getDefaultDest();
414-
if (ReachableCaseCount > 1 &&
415-
!isa<UnreachableInst>(DefaultDest->getFirstNonPHIOrDbg())) {
413+
if (ReachableCaseCount > 1 && !SI->defaultDestUnreachable()) {
414+
BasicBlock *DefaultDest = SI->getDefaultDest();
416415
ConstantRange CR = LVI->getConstantRangeAtUse(I->getOperandUse(0),
417416
/*UndefAllowed*/ false);
418417
// The default dest is unreachable if all cases are covered.

llvm/lib/Transforms/Utils/Local.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,8 @@ bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions,
203203
BasicBlock *TheOnlyDest = DefaultDest;
204204

205205
// If the default is unreachable, ignore it when searching for TheOnlyDest.
206-
if (isa<UnreachableInst>(DefaultDest->getFirstNonPHIOrDbg()) &&
207-
SI->getNumCases() > 0) {
206+
if (SI->defaultDestUnreachable() && SI->getNumCases() > 0)
208207
TheOnlyDest = SI->case_begin()->getCaseSuccessor();
209-
}
210208

211209
bool Changed = false;
212210

llvm/lib/Transforms/Utils/LowerSwitch.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ void ProcessSwitchInst(SwitchInst *SI,
388388
ConstantInt *UpperBound = nullptr;
389389
bool DefaultIsUnreachableFromSwitch = false;
390390

391-
if (isa<UnreachableInst>(Default->getFirstNonPHIOrDbg())) {
391+
if (SI->defaultDestUnreachable()) {
392392
// Make the bounds tightly fitted around the case value range, because we
393393
// know that the value passed to the switch must be exactly one of the case
394394
// values.

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5715,8 +5715,7 @@ bool SimplifyCFGOpt::turnSwitchRangeIntoICmp(SwitchInst *SI,
57155715
IRBuilder<> &Builder) {
57165716
assert(SI->getNumCases() > 1 && "Degenerate switch?");
57175717

5718-
bool HasDefault =
5719-
!isa<UnreachableInst>(SI->getDefaultDest()->getFirstNonPHIOrDbg());
5718+
bool HasDefault = !SI->defaultDestUnreachable();
57205719

57215720
auto *BB = SI->getParent();
57225721

@@ -5879,8 +5878,7 @@ static bool eliminateDeadSwitchCases(SwitchInst *SI, DomTreeUpdater *DTU,
58795878
// default destination becomes dead and we can remove it. If we know some
58805879
// of the bits in the value, we can use that to more precisely compute the
58815880
// number of possible unique case values.
5882-
bool HasDefault =
5883-
!isa<UnreachableInst>(SI->getDefaultDest()->getFirstNonPHIOrDbg());
5881+
bool HasDefault = !SI->defaultDestUnreachable();
58845882
const unsigned NumUnknownBits =
58855883
Known.getBitWidth() - (Known.Zero | Known.One).popcount();
58865884
assert(NumUnknownBits <= Known.getBitWidth());
@@ -6237,11 +6235,8 @@ static bool initializeUniqueCases(SwitchInst *SI, PHINode *&PHI,
62376235
// is unreachable.
62386236
DefaultResult =
62396237
DefaultResults.size() == 1 ? DefaultResults.begin()->second : nullptr;
6240-
if ((!DefaultResult &&
6241-
!isa<UnreachableInst>(DefaultDest->getFirstNonPHIOrDbg())))
6242-
return false;
62436238

6244-
return true;
6239+
return DefaultResult || SI->defaultDestUnreachable();
62456240
}
62466241

62476242
// Helper function that checks if it is possible to transform a switch with only
@@ -6948,7 +6943,7 @@ static bool switchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
69486943
// If the default destination is unreachable, or if the lookup table covers
69496944
// all values of the conditional variable, branch directly to the lookup table
69506945
// BB. Otherwise, check that the condition is within the case range.
6951-
bool DefaultIsReachable = !SI->defaultDestUndefined();
6946+
bool DefaultIsReachable = !SI->defaultDestUnreachable();
69526947

69536948
bool TableHasHoles = (NumResults < TableSize);
69546949

@@ -7281,7 +7276,7 @@ static bool simplifySwitchOfPowersOfTwo(SwitchInst *SI, IRBuilder<> &Builder,
72817276
// We perform this optimization only for switches with
72827277
// unreachable default case.
72837278
// This assumtion will save us from checking if `Condition` is a power of two.
7284-
if (!isa<UnreachableInst>(SI->getDefaultDest()->getFirstNonPHIOrDbg()))
7279+
if (!SI->defaultDestUnreachable())
72857280
return false;
72867281

72877282
// Check that switch cases are powers of two.
@@ -7363,7 +7358,7 @@ static bool simplifySwitchOfCmpIntrinsic(SwitchInst *SI, IRBuilderBase &Builder,
73637358

73647359
assert(Missing.size() == 1 && "Should have one case left");
73657360
Res = *Missing.begin();
7366-
} else if (SI->getNumCases() == 3 && SI->defaultDestUndefined()) {
7361+
} else if (SI->getNumCases() == 3 && SI->defaultDestUnreachable()) {
73677362
// Normalize so that Succ is taken once and OtherSucc twice.
73687363
Unreachable = SI->getDefaultDest();
73697364
Succ = OtherSucc = nullptr;

llvm/unittests/SandboxIR/SandboxIRTest.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4222,8 +4222,9 @@ define void @foo(i32 %cond0, i32 %cond1) {
42224222
EXPECT_EQ(Switch->getDefaultDest(),
42234223
Ctx.getValue(LLVMSwitch->getDefaultDest()));
42244224
EXPECT_EQ(Switch->getDefaultDest(), Default);
4225-
// Check defaultDestUndefined().
4226-
EXPECT_EQ(Switch->defaultDestUndefined(), LLVMSwitch->defaultDestUndefined());
4225+
// Check defaultDestUnreachable().
4226+
EXPECT_EQ(Switch->defaultDestUnreachable(),
4227+
LLVMSwitch->defaultDestUnreachable());
42274228
// Check setDefaultDest().
42284229
auto *OrigDefaultDest = Switch->getDefaultDest();
42294230
auto *NewDefaultDest = Entry;

0 commit comments

Comments
 (0)