@@ -6020,6 +6020,8 @@ static bool eliminateDeadSwitchCases(SwitchInst *SI, DomTreeUpdater *DTU,
60206020 const DataLayout &DL) {
60216021 Value *Cond = SI->getCondition ();
60226022 KnownBits Known = computeKnownBits (Cond, DL, AC, SI);
6023+ SmallPtrSet<const Constant *, 4 > KnownValues;
6024+ bool IsKnownValuesValid = collectPossibleValues (Cond, KnownValues, 4 );
60236025
60246026 // We can also eliminate cases by determining that their values are outside of
60256027 // the limited range of the condition based on how many significant (non-sign)
@@ -6039,15 +6041,18 @@ static bool eliminateDeadSwitchCases(SwitchInst *SI, DomTreeUpdater *DTU,
60396041 UniqueSuccessors.push_back (Successor);
60406042 ++It->second ;
60416043 }
6042- const APInt &CaseVal = Case.getCaseValue ()->getValue ();
6044+ ConstantInt *CaseC = Case.getCaseValue ();
6045+ const APInt &CaseVal = CaseC->getValue ();
60436046 if (Known.Zero .intersects (CaseVal) || !Known.One .isSubsetOf (CaseVal) ||
6044- (CaseVal.getSignificantBits () > MaxSignificantBitsInCond)) {
6045- DeadCases.push_back (Case.getCaseValue ());
6047+ (CaseVal.getSignificantBits () > MaxSignificantBitsInCond) ||
6048+ (IsKnownValuesValid && !KnownValues.contains (CaseC))) {
6049+ DeadCases.push_back (CaseC);
60466050 if (DTU)
60476051 --NumPerSuccessorCases[Successor];
60486052 LLVM_DEBUG (dbgs () << " SimplifyCFG: switch case " << CaseVal
60496053 << " is dead.\n " );
6050- }
6054+ } else if (IsKnownValuesValid)
6055+ KnownValues.erase (CaseC);
60516056 }
60526057
60536058 // If we can prove that the cases must cover all possible values, the
@@ -6058,33 +6063,41 @@ static bool eliminateDeadSwitchCases(SwitchInst *SI, DomTreeUpdater *DTU,
60586063 const unsigned NumUnknownBits =
60596064 Known.getBitWidth () - (Known.Zero | Known.One ).popcount ();
60606065 assert (NumUnknownBits <= Known.getBitWidth ());
6061- if (HasDefault && DeadCases.empty () &&
6062- NumUnknownBits < 64 /* avoid overflow */ ) {
6063- uint64_t AllNumCases = 1ULL << NumUnknownBits;
6064- if (SI->getNumCases () == AllNumCases) {
6066+ if (HasDefault && DeadCases.empty ()) {
6067+ if (IsKnownValuesValid && all_of (KnownValues, IsaPred<UndefValue>)) {
60656068 createUnreachableSwitchDefault (SI, DTU);
60666069 return true ;
60676070 }
6068- // When only one case value is missing, replace default with that case.
6069- // Eliminating the default branch will provide more opportunities for
6070- // optimization, such as lookup tables.
6071- if (SI->getNumCases () == AllNumCases - 1 ) {
6072- assert (NumUnknownBits > 1 && " Should be canonicalized to a branch" );
6073- IntegerType *CondTy = cast<IntegerType>(Cond->getType ());
6074- if (CondTy->getIntegerBitWidth () > 64 ||
6075- !DL.fitsInLegalInteger (CondTy->getIntegerBitWidth ()))
6076- return false ;
60776071
6078- uint64_t MissingCaseVal = 0 ;
6079- for (const auto &Case : SI->cases ())
6080- MissingCaseVal ^= Case.getCaseValue ()->getValue ().getLimitedValue ();
6081- auto *MissingCase =
6082- cast<ConstantInt>(ConstantInt::get (Cond->getType (), MissingCaseVal));
6083- SwitchInstProfUpdateWrapper SIW (*SI);
6084- SIW.addCase (MissingCase, SI->getDefaultDest (), SIW.getSuccessorWeight (0 ));
6085- createUnreachableSwitchDefault (SI, DTU, /* RemoveOrigDefaultBlock*/ false );
6086- SIW.setSuccessorWeight (0 , 0 );
6087- return true ;
6072+ if (NumUnknownBits < 64 /* avoid overflow */ ) {
6073+ uint64_t AllNumCases = 1ULL << NumUnknownBits;
6074+ if (SI->getNumCases () == AllNumCases) {
6075+ createUnreachableSwitchDefault (SI, DTU);
6076+ return true ;
6077+ }
6078+ // When only one case value is missing, replace default with that case.
6079+ // Eliminating the default branch will provide more opportunities for
6080+ // optimization, such as lookup tables.
6081+ if (SI->getNumCases () == AllNumCases - 1 ) {
6082+ assert (NumUnknownBits > 1 && " Should be canonicalized to a branch" );
6083+ IntegerType *CondTy = cast<IntegerType>(Cond->getType ());
6084+ if (CondTy->getIntegerBitWidth () > 64 ||
6085+ !DL.fitsInLegalInteger (CondTy->getIntegerBitWidth ()))
6086+ return false ;
6087+
6088+ uint64_t MissingCaseVal = 0 ;
6089+ for (const auto &Case : SI->cases ())
6090+ MissingCaseVal ^= Case.getCaseValue ()->getValue ().getLimitedValue ();
6091+ auto *MissingCase = cast<ConstantInt>(
6092+ ConstantInt::get (Cond->getType (), MissingCaseVal));
6093+ SwitchInstProfUpdateWrapper SIW (*SI);
6094+ SIW.addCase (MissingCase, SI->getDefaultDest (),
6095+ SIW.getSuccessorWeight (0 ));
6096+ createUnreachableSwitchDefault (SI, DTU,
6097+ /* RemoveOrigDefaultBlock*/ false );
6098+ SIW.setSuccessorWeight (0 , 0 );
6099+ return true ;
6100+ }
60886101 }
60896102 }
60906103
0 commit comments