diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp --- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -168,6 +168,10 @@ "simplifycfg-merge-compatible-invokes", cl::Hidden, cl::init(true), cl::desc("Allow SimplifyCFG to merge invokes together when appropriate")); +static cl::opt MaxSwitchCasesPerResult( + "max-switch-cases-per-result", cl::Hidden, cl::init(2), + cl::desc("Limit cases to analyze when converting a switch to select")); + STATISTIC(NumBitMaps, "Number of switch instructions turned into bitmaps"); STATISTIC(NumLinearMaps, "Number of switch instructions turned into linear mapping"); @@ -5602,9 +5606,9 @@ // Helper function used to add CaseVal to the list of cases that generate // Result. Returns the updated number of cases that generate this result. -static uintptr_t mapCaseToResult(ConstantInt *CaseVal, - SwitchCaseResultVectorTy &UniqueResults, - Constant *Result) { +static size_t mapCaseToResult(ConstantInt *CaseVal, + SwitchCaseResultVectorTy &UniqueResults, + Constant *Result) { for (auto &I : UniqueResults) { if (I.first == Result) { I.second.push_back(CaseVal); @@ -5620,12 +5624,13 @@ // results for the PHI node of the common destination block for a switch // instruction. Returns false if multiple PHI nodes have been found or if // there is not a common destination block for the switch. -static bool -initializeUniqueCases(SwitchInst *SI, PHINode *&PHI, BasicBlock *&CommonDest, - SwitchCaseResultVectorTy &UniqueResults, - Constant *&DefaultResult, const DataLayout &DL, - const TargetTransformInfo &TTI, - uintptr_t MaxUniqueResults, uintptr_t MaxCasesPerResult) { +static bool initializeUniqueCases(SwitchInst *SI, PHINode *&PHI, + BasicBlock *&CommonDest, + SwitchCaseResultVectorTy &UniqueResults, + Constant *&DefaultResult, + const DataLayout &DL, + const TargetTransformInfo &TTI, + uintptr_t MaxUniqueResults) { for (auto &I : SI->cases()) { ConstantInt *CaseVal = I.getCaseValue(); @@ -5640,11 +5645,11 @@ return false; // Add the case->result mapping to UniqueResults. - const uintptr_t NumCasesForResult = + const size_t NumCasesForResult = mapCaseToResult(CaseVal, UniqueResults, Results.begin()->second); // Early out if there are too many cases for this result. - if (NumCasesForResult > MaxCasesPerResult) + if (NumCasesForResult > MaxSwitchCasesPerResult) return false; // Early out if there are too many unique results. @@ -5772,8 +5777,7 @@ SwitchCaseResultVectorTy UniqueResults; // Collect all the cases that will deliver the same value from the switch. if (!initializeUniqueCases(SI, PHI, CommonDest, UniqueResults, DefaultResult, - DL, TTI, /*MaxUniqueResults*/ 2, - /*MaxCasesPerResult*/ 2)) + DL, TTI, /*MaxUniqueResults*/ 2)) return false; assert(PHI != nullptr && "PHI for value select not found");