Skip to content

Commit d9211be

Browse files
committed
[SimplifyCFG] cleanup code for converting switch to select (NFC)
This renames functions for more general usage (and current capitalization style) before a proposed logic change in D122485. Differential Revision: https://reviews.llvm.org/D123614
1 parent 747a490 commit d9211be

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5531,7 +5531,7 @@ ConstantFold(Instruction *I, const DataLayout &DL,
55315531
/// destionations CaseDest corresponding to value CaseVal (0 for the default
55325532
/// case), of a switch instruction SI.
55335533
static bool
5534-
GetCaseResults(SwitchInst *SI, ConstantInt *CaseVal, BasicBlock *CaseDest,
5534+
getCaseResults(SwitchInst *SI, ConstantInt *CaseVal, BasicBlock *CaseDest,
55355535
BasicBlock **CommonDest,
55365536
SmallVectorImpl<std::pair<PHINode *, Constant *>> &Res,
55375537
const DataLayout &DL, const TargetTransformInfo &TTI) {
@@ -5602,7 +5602,7 @@ GetCaseResults(SwitchInst *SI, ConstantInt *CaseVal, BasicBlock *CaseDest,
56025602

56035603
// Helper function used to add CaseVal to the list of cases that generate
56045604
// Result. Returns the updated number of cases that generate this result.
5605-
static uintptr_t MapCaseToResult(ConstantInt *CaseVal,
5605+
static uintptr_t mapCaseToResult(ConstantInt *CaseVal,
56065606
SwitchCaseResultVectorTy &UniqueResults,
56075607
Constant *Result) {
56085608
for (auto &I : UniqueResults) {
@@ -5621,7 +5621,7 @@ static uintptr_t MapCaseToResult(ConstantInt *CaseVal,
56215621
// instruction. Returns false if multiple PHI nodes have been found or if
56225622
// there is not a common destination block for the switch.
56235623
static bool
5624-
InitializeUniqueCases(SwitchInst *SI, PHINode *&PHI, BasicBlock *&CommonDest,
5624+
initializeUniqueCases(SwitchInst *SI, PHINode *&PHI, BasicBlock *&CommonDest,
56255625
SwitchCaseResultVectorTy &UniqueResults,
56265626
Constant *&DefaultResult, const DataLayout &DL,
56275627
const TargetTransformInfo &TTI,
@@ -5631,7 +5631,7 @@ InitializeUniqueCases(SwitchInst *SI, PHINode *&PHI, BasicBlock *&CommonDest,
56315631

56325632
// Resulting value at phi nodes for this case value.
56335633
SwitchCaseResultsTy Results;
5634-
if (!GetCaseResults(SI, CaseVal, I.getCaseSuccessor(), &CommonDest, Results,
5634+
if (!getCaseResults(SI, CaseVal, I.getCaseSuccessor(), &CommonDest, Results,
56355635
DL, TTI))
56365636
return false;
56375637

@@ -5641,7 +5641,7 @@ InitializeUniqueCases(SwitchInst *SI, PHINode *&PHI, BasicBlock *&CommonDest,
56415641

56425642
// Add the case->result mapping to UniqueResults.
56435643
const uintptr_t NumCasesForResult =
5644-
MapCaseToResult(CaseVal, UniqueResults, Results.begin()->second);
5644+
mapCaseToResult(CaseVal, UniqueResults, Results.begin()->second);
56455645

56465646
// Early out if there are too many cases for this result.
56475647
if (NumCasesForResult > MaxCasesPerResult)
@@ -5660,7 +5660,7 @@ InitializeUniqueCases(SwitchInst *SI, PHINode *&PHI, BasicBlock *&CommonDest,
56605660
// Find the default result value.
56615661
SmallVector<std::pair<PHINode *, Constant *>, 1> DefaultResults;
56625662
BasicBlock *DefaultDest = SI->getDefaultDest();
5663-
GetCaseResults(SI, nullptr, SI->getDefaultDest(), &CommonDest, DefaultResults,
5663+
getCaseResults(SI, nullptr, SI->getDefaultDest(), &CommonDest, DefaultResults,
56645664
DL, TTI);
56655665
// If the default value is not found abort unless the default destination
56665666
// is unreachable.
@@ -5684,9 +5684,10 @@ InitializeUniqueCases(SwitchInst *SI, PHINode *&PHI, BasicBlock *&CommonDest,
56845684
// default:
56855685
// return 4;
56865686
// }
5687-
static Value *ConvertTwoCaseSwitch(const SwitchCaseResultVectorTy &ResultVector,
5688-
Constant *DefaultResult, Value *Condition,
5689-
IRBuilder<> &Builder) {
5687+
// TODO: Handle switches with more than 2 cases that map to the same result.
5688+
static Value *foldSwitchToSelect(const SwitchCaseResultVectorTy &ResultVector,
5689+
Constant *DefaultResult, Value *Condition,
5690+
IRBuilder<> &Builder) {
56905691
// If we are selecting between only two cases transform into a simple
56915692
// select or a two-way select if default is possible.
56925693
if (ResultVector.size() == 2 && ResultVector[0].second.size() == 1 &&
@@ -5724,10 +5725,10 @@ static Value *ConvertTwoCaseSwitch(const SwitchCaseResultVectorTy &ResultVector,
57245725

57255726
// Helper function to cleanup a switch instruction that has been converted into
57265727
// a select, fixing up PHI nodes and basic blocks.
5727-
static void RemoveSwitchAfterSelectConversion(SwitchInst *SI, PHINode *PHI,
5728-
Value *SelectValue,
5729-
IRBuilder<> &Builder,
5730-
DomTreeUpdater *DTU) {
5728+
static void removeSwitchAfterSelectFold(SwitchInst *SI, PHINode *PHI,
5729+
Value *SelectValue,
5730+
IRBuilder<> &Builder,
5731+
DomTreeUpdater *DTU) {
57315732
std::vector<DominatorTree::UpdateType> Updates;
57325733

57335734
BasicBlock *SelectBB = SI->getParent();
@@ -5758,33 +5759,32 @@ static void RemoveSwitchAfterSelectConversion(SwitchInst *SI, PHINode *PHI,
57585759
DTU->applyUpdates(Updates);
57595760
}
57605761

5761-
/// If the switch is only used to initialize one or more
5762-
/// phi nodes in a common successor block with only two different
5763-
/// constant values, replace the switch with select.
5764-
static bool switchToSelect(SwitchInst *SI, IRBuilder<> &Builder,
5765-
DomTreeUpdater *DTU, const DataLayout &DL,
5766-
const TargetTransformInfo &TTI) {
5762+
/// If a switch is only used to initialize one or more phi nodes in a common
5763+
/// successor block with only two different constant values, try to replace the
5764+
/// switch with a select. Returns true if the fold was made.
5765+
static bool trySwitchToSelect(SwitchInst *SI, IRBuilder<> &Builder,
5766+
DomTreeUpdater *DTU, const DataLayout &DL,
5767+
const TargetTransformInfo &TTI) {
57675768
Value *const Cond = SI->getCondition();
57685769
PHINode *PHI = nullptr;
57695770
BasicBlock *CommonDest = nullptr;
57705771
Constant *DefaultResult;
57715772
SwitchCaseResultVectorTy UniqueResults;
57725773
// Collect all the cases that will deliver the same value from the switch.
5773-
if (!InitializeUniqueCases(SI, PHI, CommonDest, UniqueResults, DefaultResult,
5774-
DL, TTI, /*MaxUniqueResults*/2,
5775-
/*MaxCasesPerResult*/2))
5774+
if (!initializeUniqueCases(SI, PHI, CommonDest, UniqueResults, DefaultResult,
5775+
DL, TTI, /*MaxUniqueResults*/ 2,
5776+
/*MaxCasesPerResult*/ 2))
57765777
return false;
5777-
assert(PHI != nullptr && "PHI for value select not found");
57785778

5779+
assert(PHI != nullptr && "PHI for value select not found");
57795780
Builder.SetInsertPoint(SI);
57805781
Value *SelectValue =
5781-
ConvertTwoCaseSwitch(UniqueResults, DefaultResult, Cond, Builder);
5782-
if (SelectValue) {
5783-
RemoveSwitchAfterSelectConversion(SI, PHI, SelectValue, Builder, DTU);
5784-
return true;
5785-
}
5786-
// The switch couldn't be converted into a select.
5787-
return false;
5782+
foldSwitchToSelect(UniqueResults, DefaultResult, Cond, Builder);
5783+
if (!SelectValue)
5784+
return false;
5785+
5786+
removeSwitchAfterSelectFold(SI, PHI, SelectValue, Builder, DTU);
5787+
return true;
57885788
}
57895789

57905790
namespace {
@@ -6237,7 +6237,7 @@ static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
62376237
// Resulting value at phi nodes for this case value.
62386238
using ResultsTy = SmallVector<std::pair<PHINode *, Constant *>, 4>;
62396239
ResultsTy Results;
6240-
if (!GetCaseResults(SI, CaseVal, CI->getCaseSuccessor(), &CommonDest,
6240+
if (!getCaseResults(SI, CaseVal, CI->getCaseSuccessor(), &CommonDest,
62416241
Results, DL, TTI))
62426242
return false;
62436243

@@ -6265,7 +6265,7 @@ static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
62656265
// or a bitmask that fits in a register.
62666266
SmallVector<std::pair<PHINode *, Constant *>, 4> DefaultResultsList;
62676267
bool HasDefaultResults =
6268-
GetCaseResults(SI, nullptr, SI->getDefaultDest(), &CommonDest,
6268+
getCaseResults(SI, nullptr, SI->getDefaultDest(), &CommonDest,
62696269
DefaultResultsList, DL, TTI);
62706270

62716271
bool NeedMask = (TableHasHoles && !HasDefaultResults);
@@ -6569,7 +6569,7 @@ bool SimplifyCFGOpt::simplifySwitch(SwitchInst *SI, IRBuilder<> &Builder) {
65696569
if (eliminateDeadSwitchCases(SI, DTU, Options.AC, DL))
65706570
return requestResimplify();
65716571

6572-
if (switchToSelect(SI, Builder, DTU, DL, TTI))
6572+
if (trySwitchToSelect(SI, Builder, DTU, DL, TTI))
65736573
return requestResimplify();
65746574

65756575
if (Options.ForwardSwitchCondToPhi && ForwardSwitchConditionToPHI(SI))

0 commit comments

Comments
 (0)