[llvm] [SimplifyCFG] remove misleading arrow operator (PR #182435)
Congcong Cai via llvm-commits
llvm-commits at lists.llvm.org
Fri Feb 20 16:48:04 PST 2026
https://github.com/HerrCai0907 updated https://github.com/llvm/llvm-project/pull/182435
>From 1e5824b267e2843e6548f372577f1037be62c534 Mon Sep 17 00:00:00 2001
From: Cai Congcong <congcong.cai at bmw.com>
Date: Fri, 20 Feb 2026 12:40:17 +0800
Subject: [PATCH 1/2] [SimplifyCFG] remove misleading arrow operator
arrow operator SwitchInstProfUpdateWrapper is misleading when adding or removing case.
this PR removes this operator
---
llvm/include/llvm/IR/Instructions.h | 2 +-
.../Scalar/CorrelatedValuePropagation.cpp | 20 ++++++++--------
.../Transforms/Scalar/SimpleLoopUnswitch.cpp | 6 ++---
llvm/lib/Transforms/Utils/SCCPSolver.cpp | 10 ++++----
llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 23 ++++++++++---------
5 files changed, 31 insertions(+), 30 deletions(-)
diff --git a/llvm/include/llvm/IR/Instructions.h b/llvm/include/llvm/IR/Instructions.h
index d4ea74c05ae62..95412698a81c2 100644
--- a/llvm/include/llvm/IR/Instructions.h
+++ b/llvm/include/llvm/IR/Instructions.h
@@ -3550,7 +3550,7 @@ class SwitchInstProfUpdateWrapper {
public:
using CaseWeightOpt = std::optional<uint32_t>;
- SwitchInst *operator->() { return &SI; }
+ SwitchInst *raw() { return &SI; }
SwitchInst &operator*() { return SI; }
operator SwitchInst *() { return &SI; }
diff --git a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
index 4f8eba4b2d12b..988ff5a0fa297 100644
--- a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
+++ b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
@@ -369,12 +369,12 @@ static bool processSwitch(SwitchInst *I, LazyValueInfo *LVI,
{ // Scope for SwitchInstProfUpdateWrapper. It must not live during
// ConstantFoldTerminator() as the underlying SwitchInst can be changed.
- SwitchInstProfUpdateWrapper SI(*I);
+ SwitchInstProfUpdateWrapper SIW(*I);
ConstantRange CR =
LVI->getConstantRangeAtUse(I->getOperandUse(0), /*UndefAllowed=*/false);
unsigned ReachableCaseCount = 0;
- for (auto CI = SI->case_begin(), CE = SI->case_end(); CI != CE;) {
+ for (auto CI = (*SIW).case_begin(), CE = (*SIW).case_end(); CI != CE;) {
ConstantInt *Case = CI->getCaseValue();
std::optional<bool> Predicate = std::nullopt;
if (!CR.contains(Case->getValue()))
@@ -397,12 +397,12 @@ static bool processSwitch(SwitchInst *I, LazyValueInfo *LVI,
// This case never fires - remove it.
BasicBlock *Succ = CI->getCaseSuccessor();
Succ->removePredecessor(BB);
- CI = SI.removeCase(CI);
- CE = SI->case_end();
+ CI = SIW.removeCase(CI);
+ CE = (*SIW).case_end();
// The condition can be modified by removePredecessor's PHI simplification
// logic.
- Cond = SI->getCondition();
+ Cond = (*SIW).getCondition();
++NumDeadCases;
Changed = true;
@@ -414,8 +414,8 @@ static bool processSwitch(SwitchInst *I, LazyValueInfo *LVI,
// This case always fires. Arrange for the switch to be turned into an
// unconditional branch by replacing the switch condition with the case
// value.
- SI->setCondition(Case);
- NumDeadCases += SI->getNumCases();
+ (*SIW).setCondition(Case);
+ NumDeadCases += (*SIW).getNumCases();
Changed = true;
break;
}
@@ -426,9 +426,9 @@ static bool processSwitch(SwitchInst *I, LazyValueInfo *LVI,
}
// The default dest is unreachable if all cases are covered.
- if (!SI->defaultDestUnreachable() &&
+ if (!(*SIW).defaultDestUnreachable() &&
!CR.isSizeLargerThan(ReachableCaseCount)) {
- BasicBlock *DefaultDest = SI->getDefaultDest();
+ BasicBlock *DefaultDest = (*SIW).getDefaultDest();
BasicBlock *NewUnreachableBB =
BasicBlock::Create(BB->getContext(), "default.unreachable",
BB->getParent(), DefaultDest);
@@ -436,7 +436,7 @@ static bool processSwitch(SwitchInst *I, LazyValueInfo *LVI,
UI->setDebugLoc(DebugLoc::getTemporary());
DefaultDest->removePredecessor(BB);
- SI->setDefaultDest(NewUnreachableBB);
+ (*SIW).setDefaultDest(NewUnreachableBB);
if (SuccessorsCount[DefaultDest] == 1)
DTU.applyUpdates({{DominatorTree::Delete, BB, DefaultDest}});
diff --git a/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp b/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
index 531922babc50a..8874aee059988 100644
--- a/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
+++ b/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
@@ -927,7 +927,7 @@ static bool unswitchTrivialSwitch(Loop &L, SwitchInst &SI, DominatorTree &DT,
// debug location of the old switch, because it semantically replace the old
// one.
auto *NewSI = SwitchInst::Create(LoopCond, NewPH, ExitCases.size(), OldPH);
- NewSI->setDebugLoc(SIW->getDebugLoc());
+ NewSI->setDebugLoc((*SIW).getDebugLoc());
SwitchInstProfUpdateWrapper NewSIW(*NewSI);
// Rewrite the IR for the unswitched basic blocks. This requires two steps.
@@ -995,7 +995,7 @@ static bool unswitchTrivialSwitch(Loop &L, SwitchInst &SI, DominatorTree &DT,
// If the default was unswitched, re-point it and add explicit cases for
// entering the loop.
if (DefaultExitBB) {
- NewSIW->setDefaultDest(DefaultExitBB);
+ (*NewSIW).setDefaultDest(DefaultExitBB);
NewSIW.setSuccessorWeight(0, DefaultCaseWeight);
// We removed all the exit cases, so we just copy the cases to the
@@ -1038,7 +1038,7 @@ static bool unswitchTrivialSwitch(Loop &L, SwitchInst &SI, DominatorTree &DT,
}
// Now nuke the switch and replace it with a direct branch.
Instruction *NewBI = BranchInst::Create(CommonSuccBB, BB);
- NewBI->setDebugLoc(SIW->getDebugLoc());
+ NewBI->setDebugLoc((*SIW).getDebugLoc());
SIW.eraseFromParent();
} else if (DefaultExitBB) {
assert(SI.getNumCases() > 0 &&
diff --git a/llvm/lib/Transforms/Utils/SCCPSolver.cpp b/llvm/lib/Transforms/Utils/SCCPSolver.cpp
index 0ac99413a2cf8..23e8c850b2326 100644
--- a/llvm/lib/Transforms/Utils/SCCPSolver.cpp
+++ b/llvm/lib/Transforms/Utils/SCCPSolver.cpp
@@ -431,12 +431,12 @@ bool SCCPSolver::removeNonFeasibleEdges(BasicBlock *BB, DomTreeUpdater &DTU,
TI->eraseFromParent();
DTU.applyUpdatesPermissive(Updates);
} else if (FeasibleSuccessors.size() > 1) {
- SwitchInstProfUpdateWrapper SI(*cast<SwitchInst>(TI));
+ SwitchInstProfUpdateWrapper SIW(*cast<SwitchInst>(TI));
SmallVector<DominatorTree::UpdateType, 8> Updates;
// If the default destination is unfeasible it will never be taken. Replace
// it with a new block with a single Unreachable instruction.
- BasicBlock *DefaultDest = SI->getDefaultDest();
+ BasicBlock *DefaultDest = (*SIW).getDefaultDest();
if (!FeasibleSuccessors.contains(DefaultDest)) {
if (!NewUnreachableBB) {
NewUnreachableBB =
@@ -448,12 +448,12 @@ bool SCCPSolver::removeNonFeasibleEdges(BasicBlock *BB, DomTreeUpdater &DTU,
}
DefaultDest->removePredecessor(BB);
- SI->setDefaultDest(NewUnreachableBB);
+ (*SIW).setDefaultDest(NewUnreachableBB);
Updates.push_back({DominatorTree::Delete, BB, DefaultDest});
Updates.push_back({DominatorTree::Insert, BB, NewUnreachableBB});
}
- for (auto CI = SI->case_begin(); CI != SI->case_end();) {
+ for (auto CI = (*SIW).case_begin(); CI != (*SIW).case_end();) {
if (FeasibleSuccessors.contains(CI->getCaseSuccessor())) {
++CI;
continue;
@@ -462,7 +462,7 @@ bool SCCPSolver::removeNonFeasibleEdges(BasicBlock *BB, DomTreeUpdater &DTU,
BasicBlock *Succ = CI->getCaseSuccessor();
Succ->removePredecessor(BB);
Updates.push_back({DominatorTree::Delete, BB, Succ});
- SI.removeCase(CI);
+ SIW.removeCase(CI);
// Don't increment CI, as we removed a case.
}
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index b912baef6188d..80c75806dbffa 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -1028,7 +1028,7 @@ bool SimplifyCFGOpt::simplifyEqualityComparisonWithOnlyPredecessor(
return true;
}
- SwitchInstProfUpdateWrapper SI = *cast<SwitchInst>(TI);
+ SwitchInstProfUpdateWrapper SIW = *cast<SwitchInst>(TI);
// Okay, TI has cases that are statically dead, prune them away.
SmallPtrSet<Constant *, 16> DeadCases;
for (const ValueEqualityComparisonCase &Case : PredCases)
@@ -1038,14 +1038,15 @@ bool SimplifyCFGOpt::simplifyEqualityComparisonWithOnlyPredecessor(
<< "Through successor TI: " << *TI);
SmallDenseMap<BasicBlock *, int, 8> NumPerSuccessorCases;
- for (SwitchInst::CaseIt i = SI->case_end(), e = SI->case_begin(); i != e;) {
+ for (SwitchInst::CaseIt i = (*SIW).case_end(), e = (*SIW).case_begin();
+ i != e;) {
--i;
auto *Successor = i->getCaseSuccessor();
if (DTU)
++NumPerSuccessorCases[Successor];
if (DeadCases.count(i->getCaseValue())) {
Successor->removePredecessor(PredDef);
- SI.removeCase(i);
+ SIW.removeCase(i);
if (DTU)
--NumPerSuccessorCases[Successor];
}
@@ -5762,15 +5763,15 @@ bool SimplifyCFGOpt::simplifyUnreachable(UnreachableInst *UI) {
if (DTU)
Updates.push_back({DominatorTree::Delete, Predecessor, BB});
} else if (auto *SI = dyn_cast<SwitchInst>(TI)) {
- SwitchInstProfUpdateWrapper SU(*SI);
- for (auto i = SU->case_begin(), e = SU->case_end(); i != e;) {
+ SwitchInstProfUpdateWrapper SIW(*SI);
+ for (auto i = (*SIW).case_begin(), e = (*SIW).case_end(); i != e;) {
if (i->getCaseSuccessor() != BB) {
++i;
continue;
}
- BB->removePredecessor(SU->getParent());
- i = SU.removeCase(i);
- e = SU->case_end();
+ BB->removePredecessor((*SIW).getParent());
+ i = SIW.removeCase(i);
+ e = (*SIW).case_end();
Changed = true;
}
// Note that the default destination can't be removed!
@@ -7712,7 +7713,7 @@ static bool simplifySwitchWhenUMin(SwitchInst *SI, DomTreeUpdater *DTU) {
SmallVector<DominatorTree::UpdateType> Updates;
SwitchInstProfUpdateWrapper SIW(*SI);
- BasicBlock *BB = SIW->getParent();
+ BasicBlock *BB = (*SIW).getParent();
// Dead cases are removed even when the simplification fails.
// A case is dead when its value is higher than the Constant.
@@ -7725,7 +7726,7 @@ static bool simplifySwitchWhenUMin(SwitchInst *SI, DomTreeUpdater *DTU) {
DeadCaseBB->removePredecessor(BB);
Updates.push_back({DominatorTree::Delete, BB, DeadCaseBB});
I = SIW.removeCase(I);
- E = SIW->case_end();
+ E = (*SIW).case_end();
}
auto Case = SI->findCaseValue(Constant);
@@ -7742,7 +7743,7 @@ static bool simplifySwitchWhenUMin(SwitchInst *SI, DomTreeUpdater *DTU) {
BasicBlock *Unreachable = SI->getDefaultDest();
SIW.replaceDefaultDest(Case);
SIW.removeCase(Case);
- SIW->setCondition(A);
+ (*SIW).setCondition(A);
Updates.push_back({DominatorTree::Delete, BB, Unreachable});
>From e795da622f85516d73af1440002735a8861dab30 Mon Sep 17 00:00:00 2001
From: Cai Congcong <congcong.cai at bmw.com>
Date: Sat, 21 Feb 2026 08:47:20 +0800
Subject: [PATCH 2/2] clean
---
llvm/include/llvm/IR/Instructions.h | 1 -
1 file changed, 1 deletion(-)
diff --git a/llvm/include/llvm/IR/Instructions.h b/llvm/include/llvm/IR/Instructions.h
index 95412698a81c2..437a5f1b5c53b 100644
--- a/llvm/include/llvm/IR/Instructions.h
+++ b/llvm/include/llvm/IR/Instructions.h
@@ -3550,7 +3550,6 @@ class SwitchInstProfUpdateWrapper {
public:
using CaseWeightOpt = std::optional<uint32_t>;
- SwitchInst *raw() { return &SI; }
SwitchInst &operator*() { return SI; }
operator SwitchInst *() { return &SI; }
More information about the llvm-commits
mailing list