@@ -5531,7 +5531,7 @@ ConstantFold(Instruction *I, const DataLayout &DL,
5531
5531
// / destionations CaseDest corresponding to value CaseVal (0 for the default
5532
5532
// / case), of a switch instruction SI.
5533
5533
static bool
5534
- GetCaseResults (SwitchInst *SI, ConstantInt *CaseVal, BasicBlock *CaseDest,
5534
+ getCaseResults (SwitchInst *SI, ConstantInt *CaseVal, BasicBlock *CaseDest,
5535
5535
BasicBlock **CommonDest,
5536
5536
SmallVectorImpl<std::pair<PHINode *, Constant *>> &Res,
5537
5537
const DataLayout &DL, const TargetTransformInfo &TTI) {
@@ -5602,7 +5602,7 @@ GetCaseResults(SwitchInst *SI, ConstantInt *CaseVal, BasicBlock *CaseDest,
5602
5602
5603
5603
// Helper function used to add CaseVal to the list of cases that generate
5604
5604
// 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,
5606
5606
SwitchCaseResultVectorTy &UniqueResults,
5607
5607
Constant *Result) {
5608
5608
for (auto &I : UniqueResults) {
@@ -5621,7 +5621,7 @@ static uintptr_t MapCaseToResult(ConstantInt *CaseVal,
5621
5621
// instruction. Returns false if multiple PHI nodes have been found or if
5622
5622
// there is not a common destination block for the switch.
5623
5623
static bool
5624
- InitializeUniqueCases (SwitchInst *SI, PHINode *&PHI, BasicBlock *&CommonDest,
5624
+ initializeUniqueCases (SwitchInst *SI, PHINode *&PHI, BasicBlock *&CommonDest,
5625
5625
SwitchCaseResultVectorTy &UniqueResults,
5626
5626
Constant *&DefaultResult, const DataLayout &DL,
5627
5627
const TargetTransformInfo &TTI,
@@ -5631,7 +5631,7 @@ InitializeUniqueCases(SwitchInst *SI, PHINode *&PHI, BasicBlock *&CommonDest,
5631
5631
5632
5632
// Resulting value at phi nodes for this case value.
5633
5633
SwitchCaseResultsTy Results;
5634
- if (!GetCaseResults (SI, CaseVal, I.getCaseSuccessor (), &CommonDest, Results,
5634
+ if (!getCaseResults (SI, CaseVal, I.getCaseSuccessor (), &CommonDest, Results,
5635
5635
DL, TTI))
5636
5636
return false ;
5637
5637
@@ -5641,7 +5641,7 @@ InitializeUniqueCases(SwitchInst *SI, PHINode *&PHI, BasicBlock *&CommonDest,
5641
5641
5642
5642
// Add the case->result mapping to UniqueResults.
5643
5643
const uintptr_t NumCasesForResult =
5644
- MapCaseToResult (CaseVal, UniqueResults, Results.begin ()->second );
5644
+ mapCaseToResult (CaseVal, UniqueResults, Results.begin ()->second );
5645
5645
5646
5646
// Early out if there are too many cases for this result.
5647
5647
if (NumCasesForResult > MaxCasesPerResult)
@@ -5660,7 +5660,7 @@ InitializeUniqueCases(SwitchInst *SI, PHINode *&PHI, BasicBlock *&CommonDest,
5660
5660
// Find the default result value.
5661
5661
SmallVector<std::pair<PHINode *, Constant *>, 1 > DefaultResults;
5662
5662
BasicBlock *DefaultDest = SI->getDefaultDest ();
5663
- GetCaseResults (SI, nullptr , SI->getDefaultDest (), &CommonDest, DefaultResults,
5663
+ getCaseResults (SI, nullptr , SI->getDefaultDest (), &CommonDest, DefaultResults,
5664
5664
DL, TTI);
5665
5665
// If the default value is not found abort unless the default destination
5666
5666
// is unreachable.
@@ -5684,9 +5684,10 @@ InitializeUniqueCases(SwitchInst *SI, PHINode *&PHI, BasicBlock *&CommonDest,
5684
5684
// default:
5685
5685
// return 4;
5686
5686
// }
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) {
5690
5691
// If we are selecting between only two cases transform into a simple
5691
5692
// select or a two-way select if default is possible.
5692
5693
if (ResultVector.size () == 2 && ResultVector[0 ].second .size () == 1 &&
@@ -5724,10 +5725,10 @@ static Value *ConvertTwoCaseSwitch(const SwitchCaseResultVectorTy &ResultVector,
5724
5725
5725
5726
// Helper function to cleanup a switch instruction that has been converted into
5726
5727
// 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) {
5731
5732
std::vector<DominatorTree::UpdateType> Updates;
5732
5733
5733
5734
BasicBlock *SelectBB = SI->getParent ();
@@ -5758,33 +5759,32 @@ static void RemoveSwitchAfterSelectConversion(SwitchInst *SI, PHINode *PHI,
5758
5759
DTU->applyUpdates (Updates);
5759
5760
}
5760
5761
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) {
5767
5768
Value *const Cond = SI->getCondition ();
5768
5769
PHINode *PHI = nullptr ;
5769
5770
BasicBlock *CommonDest = nullptr ;
5770
5771
Constant *DefaultResult;
5771
5772
SwitchCaseResultVectorTy UniqueResults;
5772
5773
// 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 ))
5776
5777
return false ;
5777
- assert (PHI != nullptr && " PHI for value select not found" );
5778
5778
5779
+ assert (PHI != nullptr && " PHI for value select not found" );
5779
5780
Builder.SetInsertPoint (SI);
5780
5781
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 ;
5788
5788
}
5789
5789
5790
5790
namespace {
@@ -6237,7 +6237,7 @@ static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
6237
6237
// Resulting value at phi nodes for this case value.
6238
6238
using ResultsTy = SmallVector<std::pair<PHINode *, Constant *>, 4 >;
6239
6239
ResultsTy Results;
6240
- if (!GetCaseResults (SI, CaseVal, CI->getCaseSuccessor (), &CommonDest,
6240
+ if (!getCaseResults (SI, CaseVal, CI->getCaseSuccessor (), &CommonDest,
6241
6241
Results, DL, TTI))
6242
6242
return false ;
6243
6243
@@ -6265,7 +6265,7 @@ static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
6265
6265
// or a bitmask that fits in a register.
6266
6266
SmallVector<std::pair<PHINode *, Constant *>, 4 > DefaultResultsList;
6267
6267
bool HasDefaultResults =
6268
- GetCaseResults (SI, nullptr , SI->getDefaultDest (), &CommonDest,
6268
+ getCaseResults (SI, nullptr , SI->getDefaultDest (), &CommonDest,
6269
6269
DefaultResultsList, DL, TTI);
6270
6270
6271
6271
bool NeedMask = (TableHasHoles && !HasDefaultResults);
@@ -6569,7 +6569,7 @@ bool SimplifyCFGOpt::simplifySwitch(SwitchInst *SI, IRBuilder<> &Builder) {
6569
6569
if (eliminateDeadSwitchCases (SI, DTU, Options.AC , DL))
6570
6570
return requestResimplify ();
6571
6571
6572
- if (switchToSelect (SI, Builder, DTU, DL, TTI))
6572
+ if (trySwitchToSelect (SI, Builder, DTU, DL, TTI))
6573
6573
return requestResimplify ();
6574
6574
6575
6575
if (Options.ForwardSwitchCondToPhi && ForwardSwitchConditionToPHI (SI))
0 commit comments