@@ -5990,6 +5990,8 @@ static bool eliminateDeadSwitchCases(SwitchInst *SI, DomTreeUpdater *DTU,
59905990 const DataLayout &DL) {
59915991 Value *Cond = SI->getCondition ();
59925992 KnownBits Known = computeKnownBits (Cond, DL, AC, SI);
5993+ SmallPtrSet<const Constant *, 4 > KnownValues;
5994+ bool IsKnownValuesValid = collectPossibleValues (Cond, KnownValues, 4 );
59935995
59945996 // We can also eliminate cases by determining that their values are outside of
59955997 // the limited range of the condition based on how many significant (non-sign)
@@ -6009,15 +6011,18 @@ static bool eliminateDeadSwitchCases(SwitchInst *SI, DomTreeUpdater *DTU,
60096011 UniqueSuccessors.push_back (Successor);
60106012 ++It->second ;
60116013 }
6012- const APInt &CaseVal = Case.getCaseValue ()->getValue ();
6014+ ConstantInt *CaseC = Case.getCaseValue ();
6015+ const APInt &CaseVal = CaseC->getValue ();
60136016 if (Known.Zero .intersects (CaseVal) || !Known.One .isSubsetOf (CaseVal) ||
6014- (CaseVal.getSignificantBits () > MaxSignificantBitsInCond)) {
6015- DeadCases.push_back (Case.getCaseValue ());
6017+ (CaseVal.getSignificantBits () > MaxSignificantBitsInCond) ||
6018+ (IsKnownValuesValid && !KnownValues.contains (CaseC))) {
6019+ DeadCases.push_back (CaseC);
60166020 if (DTU)
60176021 --NumPerSuccessorCases[Successor];
60186022 LLVM_DEBUG (dbgs () << " SimplifyCFG: switch case " << CaseVal
60196023 << " is dead.\n " );
6020- }
6024+ } else if (IsKnownValuesValid)
6025+ KnownValues.erase (CaseC);
60216026 }
60226027
60236028 // If we can prove that the cases must cover all possible values, the
@@ -6028,33 +6033,41 @@ static bool eliminateDeadSwitchCases(SwitchInst *SI, DomTreeUpdater *DTU,
60286033 const unsigned NumUnknownBits =
60296034 Known.getBitWidth () - (Known.Zero | Known.One ).popcount ();
60306035 assert (NumUnknownBits <= Known.getBitWidth ());
6031- if (HasDefault && DeadCases.empty () &&
6032- NumUnknownBits < 64 /* avoid overflow */ ) {
6033- uint64_t AllNumCases = 1ULL << NumUnknownBits;
6034- if (SI->getNumCases () == AllNumCases) {
6036+ if (HasDefault && DeadCases.empty ()) {
6037+ if (IsKnownValuesValid && all_of (KnownValues, IsaPred<UndefValue>)) {
60356038 createUnreachableSwitchDefault (SI, DTU);
60366039 return true ;
60376040 }
6038- // When only one case value is missing, replace default with that case.
6039- // Eliminating the default branch will provide more opportunities for
6040- // optimization, such as lookup tables.
6041- if (SI->getNumCases () == AllNumCases - 1 ) {
6042- assert (NumUnknownBits > 1 && " Should be canonicalized to a branch" );
6043- IntegerType *CondTy = cast<IntegerType>(Cond->getType ());
6044- if (CondTy->getIntegerBitWidth () > 64 ||
6045- !DL.fitsInLegalInteger (CondTy->getIntegerBitWidth ()))
6046- return false ;
60476041
6048- uint64_t MissingCaseVal = 0 ;
6049- for (const auto &Case : SI->cases ())
6050- MissingCaseVal ^= Case.getCaseValue ()->getValue ().getLimitedValue ();
6051- auto *MissingCase =
6052- cast<ConstantInt>(ConstantInt::get (Cond->getType (), MissingCaseVal));
6053- SwitchInstProfUpdateWrapper SIW (*SI);
6054- SIW.addCase (MissingCase, SI->getDefaultDest (), SIW.getSuccessorWeight (0 ));
6055- createUnreachableSwitchDefault (SI, DTU, /* RemoveOrigDefaultBlock*/ false );
6056- SIW.setSuccessorWeight (0 , 0 );
6057- return true ;
6042+ if (NumUnknownBits < 64 /* avoid overflow */ ) {
6043+ uint64_t AllNumCases = 1ULL << NumUnknownBits;
6044+ if (SI->getNumCases () == AllNumCases) {
6045+ createUnreachableSwitchDefault (SI, DTU);
6046+ return true ;
6047+ }
6048+ // When only one case value is missing, replace default with that case.
6049+ // Eliminating the default branch will provide more opportunities for
6050+ // optimization, such as lookup tables.
6051+ if (SI->getNumCases () == AllNumCases - 1 ) {
6052+ assert (NumUnknownBits > 1 && " Should be canonicalized to a branch" );
6053+ IntegerType *CondTy = cast<IntegerType>(Cond->getType ());
6054+ if (CondTy->getIntegerBitWidth () > 64 ||
6055+ !DL.fitsInLegalInteger (CondTy->getIntegerBitWidth ()))
6056+ return false ;
6057+
6058+ uint64_t MissingCaseVal = 0 ;
6059+ for (const auto &Case : SI->cases ())
6060+ MissingCaseVal ^= Case.getCaseValue ()->getValue ().getLimitedValue ();
6061+ auto *MissingCase = cast<ConstantInt>(
6062+ ConstantInt::get (Cond->getType (), MissingCaseVal));
6063+ SwitchInstProfUpdateWrapper SIW (*SI);
6064+ SIW.addCase (MissingCase, SI->getDefaultDest (),
6065+ SIW.getSuccessorWeight (0 ));
6066+ createUnreachableSwitchDefault (SI, DTU,
6067+ /* RemoveOrigDefaultBlock*/ false );
6068+ SIW.setSuccessorWeight (0 , 0 );
6069+ return true ;
6070+ }
60586071 }
60596072 }
60606073
0 commit comments