84
84
#include < cstdint>
85
85
#include < iterator>
86
86
#include < map>
87
- #include < numeric>
88
87
#include < optional>
89
88
#include < set>
90
89
#include < tuple>
@@ -6356,33 +6355,33 @@ static Value *foldSwitchToSelect(const SwitchCaseResultVectorTy &ResultVector,
6356
6355
if (DefaultResult) {
6357
6356
Value *ValueCompare =
6358
6357
Builder.CreateICmpEQ (Condition, SecondCase, " switch.selectcmp" );
6359
- SelectInst *SelectValueInst = cast<SelectInst>( Builder.CreateSelect (
6360
- ValueCompare, ResultVector[ 1 ]. first , DefaultResult, " switch.select" ) );
6361
- SelectValue = SelectValueInst ;
6362
- if ( HasBranchWeights) {
6358
+ SelectValue = Builder.CreateSelect (ValueCompare, ResultVector[ 1 ]. first ,
6359
+ DefaultResult, " switch.select" );
6360
+ if ( auto *SI = dyn_cast<SelectInst>(SelectValue) ;
6361
+ SI && HasBranchWeights) {
6363
6362
// We start with 3 probabilities, where the numerator is the
6364
6363
// corresponding BranchWeights[i], and the denominator is the sum over
6365
6364
// BranchWeights. We want the probability and negative probability of
6366
6365
// Condition == SecondCase.
6367
6366
assert (BranchWeights.size () == 3 );
6368
- setBranchWeights (SelectValueInst , BranchWeights[2 ],
6367
+ setBranchWeights (SI , BranchWeights[2 ],
6369
6368
BranchWeights[0 ] + BranchWeights[1 ],
6370
6369
/* IsExpected=*/ false );
6371
6370
}
6372
6371
}
6373
6372
Value *ValueCompare =
6374
6373
Builder.CreateICmpEQ (Condition, FirstCase, " switch.selectcmp" );
6375
- SelectInst *Ret = cast<SelectInst>( Builder.CreateSelect (
6376
- ValueCompare, ResultVector[ 0 ]. first , SelectValue, " switch.select" ) );
6377
- if (HasBranchWeights) {
6374
+ Value *Ret = Builder.CreateSelect (ValueCompare, ResultVector[ 0 ]. first ,
6375
+ SelectValue, " switch.select" );
6376
+ if (auto *SI = dyn_cast<SelectInst>(Ret); SI && HasBranchWeights) {
6378
6377
// We may have had a DefaultResult. Base the position of the first and
6379
6378
// second's branch weights accordingly. Also the proability that Condition
6380
6379
// != FirstCase needs to take that into account.
6381
6380
assert (BranchWeights.size () >= 2 );
6382
6381
size_t FirstCasePos = (Condition != nullptr );
6383
6382
size_t SecondCasePos = FirstCasePos + 1 ;
6384
6383
uint32_t DefaultCase = (Condition != nullptr ) ? BranchWeights[0 ] : 0 ;
6385
- setBranchWeights (Ret , BranchWeights[FirstCasePos],
6384
+ setBranchWeights (SI , BranchWeights[FirstCasePos],
6386
6385
DefaultCase + BranchWeights[SecondCasePos],
6387
6386
/* IsExpected=*/ false );
6388
6387
}
@@ -6422,13 +6421,13 @@ static Value *foldSwitchToSelect(const SwitchCaseResultVectorTy &ResultVector,
6422
6421
Value *And = Builder.CreateAnd (Condition, AndMask);
6423
6422
Value *Cmp = Builder.CreateICmpEQ (
6424
6423
And, Constant::getIntegerValue (And->getType (), AndMask));
6425
- SelectInst *Ret = cast<SelectInst>(
6426
- Builder.CreateSelect (Cmp, ResultVector[0 ].first , DefaultResult)) ;
6427
- if (HasBranchWeights) {
6424
+ Value *Ret =
6425
+ Builder.CreateSelect (Cmp, ResultVector[0 ].first , DefaultResult);
6426
+ if (auto *SI = dyn_cast<SelectInst>(Ret); SI && HasBranchWeights) {
6428
6427
// We know there's a Default case. We base the resulting branch
6429
6428
// weights off its probability.
6430
6429
assert (BranchWeights.size () >= 2 );
6431
- setBranchWeights (Ret , accumulate (drop_begin (BranchWeights), 0 ),
6430
+ setBranchWeights (SI , accumulate (drop_begin (BranchWeights), 0 ),
6432
6431
BranchWeights[0 ], /* IsExpected=*/ false );
6433
6432
}
6434
6433
return Ret;
@@ -6448,11 +6447,11 @@ static Value *foldSwitchToSelect(const SwitchCaseResultVectorTy &ResultVector,
6448
6447
Value *And = Builder.CreateAnd (Condition, ~BitMask, " switch.and" );
6449
6448
Value *Cmp = Builder.CreateICmpEQ (
6450
6449
And, Constant::getNullValue (And->getType ()), " switch.selectcmp" );
6451
- SelectInst *Ret = cast<SelectInst>(
6452
- Builder.CreateSelect (Cmp, ResultVector[0 ].first , DefaultResult)) ;
6453
- if (HasBranchWeights) {
6450
+ Value *Ret =
6451
+ Builder.CreateSelect (Cmp, ResultVector[0 ].first , DefaultResult);
6452
+ if (auto *SI = dyn_cast<SelectInst>(Ret); SI && HasBranchWeights) {
6454
6453
assert (BranchWeights.size () >= 2 );
6455
- setBranchWeights (Ret , accumulate (drop_begin (BranchWeights), 0 ),
6454
+ setBranchWeights (SI , accumulate (drop_begin (BranchWeights), 0 ),
6456
6455
BranchWeights[0 ], /* IsExpected=*/ false );
6457
6456
}
6458
6457
return Ret;
@@ -6466,11 +6465,11 @@ static Value *foldSwitchToSelect(const SwitchCaseResultVectorTy &ResultVector,
6466
6465
Value *Cmp2 = Builder.CreateICmpEQ (Condition, CaseValues[1 ],
6467
6466
" switch.selectcmp.case2" );
6468
6467
Value *Cmp = Builder.CreateOr (Cmp1, Cmp2, " switch.selectcmp" );
6469
- SelectInst *Ret = cast<SelectInst>(
6470
- Builder.CreateSelect (Cmp, ResultVector[0 ].first , DefaultResult)) ;
6471
- if (HasBranchWeights) {
6468
+ Value *Ret =
6469
+ Builder.CreateSelect (Cmp, ResultVector[0 ].first , DefaultResult);
6470
+ if (auto *SI = dyn_cast<SelectInst>(Ret); SI && HasBranchWeights) {
6472
6471
assert (BranchWeights.size () >= 2 );
6473
- setBranchWeights (Ret , accumulate (drop_begin (BranchWeights), 0 ),
6472
+ setBranchWeights (SI , accumulate (drop_begin (BranchWeights), 0 ),
6474
6473
BranchWeights[0 ], /* IsExpected=*/ false );
6475
6474
}
6476
6475
return Ret;
0 commit comments