diff options
author | Marcello Maggioni <hayarms@gmail.com> | 2018-01-11 02:01:16 +0000 |
---|---|---|
committer | Marcello Maggioni <hayarms@gmail.com> | 2018-01-11 02:01:16 +0000 |
commit | 7083423f22852775ba69bb107176b87979a1c17a (patch) | |
tree | d7b81f505e5cbdb4733091e0e6b1917bf1345c59 /llvm/lib/Transforms/Utils/SimplifyCFG.cpp | |
parent | 919264371bac26a0a25f51512f0838cd384e9b5f (diff) | |
download | bcm5719-llvm-7083423f22852775ba69bb107176b87979a1c17a.tar.gz bcm5719-llvm-7083423f22852775ba69bb107176b87979a1c17a.zip |
[SimplifyCFG] Add cut-off for InitializeUniqueCases.
The function can take a significant amount of time on some
complicated test cases, but for the currently only use of
the function we can stop the initialization much earlier
when we find out we are going to discard the result anyway
in the caller of the function.
Adding configurable cut-off points so that we avoid wasting time.
NFCI.
llvm-svn: 322248
Diffstat (limited to 'llvm/lib/Transforms/Utils/SimplifyCFG.cpp')
-rw-r--r-- | llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 38 |
1 files changed, 25 insertions, 13 deletions
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp index 7c195788e41..d11c208c894 100644 --- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -4670,30 +4670,31 @@ GetCaseResults(SwitchInst *SI, ConstantInt *CaseVal, BasicBlock *CaseDest, } // Helper function used to add CaseVal to the list of cases that generate -// Result. -static void MapCaseToResult(ConstantInt *CaseVal, - SwitchCaseResultVectorTy &UniqueResults, - Constant *Result) { +// Result. Returns the updated number of cases that generate this result. +static uintptr_t MapCaseToResult(ConstantInt *CaseVal, + SwitchCaseResultVectorTy &UniqueResults, + Constant *Result) { for (auto &I : UniqueResults) { if (I.first == Result) { I.second.push_back(CaseVal); - return; + return I.second.size(); } } UniqueResults.push_back( std::make_pair(Result, SmallVector<ConstantInt *, 4>(1, CaseVal))); + return 1; } // Helper function that initializes a map containing // 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) { +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) { for (auto &I : SI->cases()) { ConstantInt *CaseVal = I.getCaseValue(); @@ -4706,7 +4707,18 @@ static bool InitializeUniqueCases(SwitchInst *SI, PHINode *&PHI, // Only one value per case is permitted if (Results.size() > 1) return false; - MapCaseToResult(CaseVal, UniqueResults, Results.begin()->second); + + // Add the case->result mapping to UniqueResults. + const uintptr_t NumCasesForResult = + MapCaseToResult(CaseVal, UniqueResults, Results.begin()->second); + + // Early out if there are too many cases for this result. + if (NumCasesForResult > MaxCasesPerResult) + return false; + + // Early out if there are too many unique results. + if (UniqueResults.size() > MaxUniqueResults) + return false; // Check the PHI consistency. if (!PHI) @@ -4806,7 +4818,7 @@ static bool switchToSelect(SwitchInst *SI, IRBuilder<> &Builder, SwitchCaseResultVectorTy UniqueResults; // Collect all the cases that will deliver the same value from the switch. if (!InitializeUniqueCases(SI, PHI, CommonDest, UniqueResults, DefaultResult, - DL, TTI)) + DL, TTI, 2, 1)) return false; // Selects choose between maximum two values. if (UniqueResults.size() != 2) |