@@ -429,6 +429,25 @@ struct Reducer : public WalkerPass<PostWalker<Reducer, UnifiedExpressionVisitor<
429429 handleCondition (select->condition );
430430 } else if (auto * sw = curr->dynCast <Switch>()) {
431431 handleCondition (sw->condition );
432+ // Try to replace switch targets with the default
433+ for (auto & target : sw->targets ) {
434+ if (target != sw->default_ ) {
435+ auto old = target;
436+ target = sw->default_ ;
437+ if (!tryToReplaceCurrent (curr)) {
438+ target = old;
439+ }
440+ }
441+ }
442+ // Try to shorten the list of targets.
443+ while (sw->targets .size () > 1 ) {
444+ auto last = sw->targets .back ();
445+ sw->targets .pop_back ();
446+ if (!tryToReplaceCurrent (curr)) {
447+ sw->targets .push_back (last);
448+ break ;
449+ }
450+ }
432451 } else if (auto * block = curr->dynCast <Block>()) {
433452 if (!shouldTryToReduce ()) return ;
434453 // replace a singleton
@@ -445,13 +464,13 @@ struct Reducer : public WalkerPass<PostWalker<Reducer, UnifiedExpressionVisitor<
445464 for (Index j = i; j < list.size () - 1 ; j++) {
446465 list[j] = list[j + 1 ];
447466 }
448- list.resize (list. size () - 1 );
467+ list.pop_back ( );
449468 if (writeAndTestReduction ()) {
450469 std::cerr << " | block-nop removed\n " ;
451470 noteReduction ();
452471 return ;
453472 }
454- list.resize (list. size () + 1 );
473+ list.push_back ( nullptr );
455474 // we failed; undo
456475 for (Index j = list.size () - 1 ; j > i; j--) {
457476 list[j] = list[j - 1 ];
0 commit comments