@@ -3302,8 +3302,8 @@ SwitchLookupTable::SwitchLookupTable(Module &M,
3302
3302
const SmallVector<std::pair<ConstantInt*, Constant*>, 4 >& Values,
3303
3303
Constant *DefaultValue,
3304
3304
const TargetData *TD) {
3305
- assert (Values.size () && " Can't build lookup table without values. " );
3306
- assert (TableSize >= Values.size () && " Can't fit values in table. " );
3305
+ assert (Values.size () && " Can't build lookup table without values! " );
3306
+ assert (TableSize >= Values.size () && " Can't fit values in table! " );
3307
3307
3308
3308
// If all values in the table are equal, this is that value.
3309
3309
SingleValue = Values.begin ()->second ;
@@ -3431,6 +3431,8 @@ static bool ShouldBuildLookupTable(SwitchInst *SI,
3431
3431
// The table density should be at least 40%. This is the same criterion as for
3432
3432
// jump tables, see SelectionDAGBuilder::handleJTSwitchCase.
3433
3433
// FIXME: Find the best cut-off.
3434
+ if (SI->getNumCases () > TableSize || TableSize >= UINT64_MAX / 10 )
3435
+ return false ; // TableSize overflowed, or mul below might overflow.
3434
3436
if (SI->getNumCases () * 10 >= TableSize * 4 )
3435
3437
return true ;
3436
3438
@@ -3513,10 +3515,6 @@ static bool SwitchToLookupTable(SwitchInst *SI,
3513
3515
}
3514
3516
3515
3517
APInt RangeSpread = MaxCaseVal->getValue () - MinCaseVal->getValue ();
3516
- // Be careful to avoid overflow when TableSize is used in
3517
- // ShouldBuildLookupTable.
3518
- if (RangeSpread.zextOrSelf (64 ).ugt (UINT64_MAX / 4 - 1 ))
3519
- return false ;
3520
3518
uint64_t TableSize = RangeSpread.getLimitedValue () + 1 ;
3521
3519
if (!ShouldBuildLookupTable (SI, TableSize, TD, ResultTypes))
3522
3520
return false ;
0 commit comments