@@ -486,14 +486,16 @@ struct OnceReduction : public Pass {
486
486
ExpressionManipulator::nop (body);
487
487
continue ;
488
488
}
489
- if (list.size () != 3 ) {
490
- // Something non-trivial; too many items for us to consider.
491
- continue ;
492
- }
493
- auto * payload = list[2 ];
494
- if (auto * call = payload->dynCast <Call>()) {
489
+ // The early-exit logic is the first item, followed by the global setting,
490
+ // and we've ruled out the case of there being nothing else after those,
491
+ // so there are at least 3 items.
492
+ assert (list.size () >= 3 );
493
+ // We consider the first item in the payload. Anything further would be
494
+ // much more difficult to analyze.
495
+ auto * payloadStart = list[2 ];
496
+ if (auto * call = payloadStart->dynCast <Call>()) {
495
497
if (optInfo.onceFuncs .at (call->target ).is ()) {
496
- // All this "once" function does is call another. We do not need the
498
+ // This "once" function immediately calls another. We do not need the
497
499
// early-exit logic in this one, then, because of the following
498
500
// reasoning. We are comparing these forms:
499
501
//
@@ -502,27 +504,29 @@ struct OnceReduction : public Pass {
502
504
// if (!foo$once) return; // two lines of
503
505
// foo$once = 1; // early-exit code
504
506
// bar();
507
+ // ..
505
508
// }
506
509
//
507
510
// to
508
511
//
509
512
// // AFTER
510
513
// function foo() {
511
514
// bar();
515
+ // ..
512
516
// }
513
517
//
514
518
// The question is whether different behavior can be observed between
515
519
// those two. There are two cases, when we enter foo:
516
520
//
517
- // 1. foo has been called before. Then we early-exit in BEFORE, and
521
+ // 1. foo has been entered before. Then we early-exit in BEFORE, and
518
522
// in AFTER we call bar which will early-exit (since foo was
519
523
// called, which means bar was at least entered, which set its
520
524
// global; bar might be on the stack, if it called foo, so it has
521
525
// not necessarily fully executed - this is a tricky situation to
522
526
// handle in general, like recursive imports of modules in various
523
527
// languages - but we do know bar has been *entered*, which means
524
528
// the global was set).
525
- // 2. foo has never been called before. In this case in BEFORE we set
529
+ // 2. foo has never been entered before. In this case in BEFORE we set
526
530
// the global and call bar, and in AFTER we also call bar.
527
531
//
528
532
// Thus, the behavior is the same, and we can remove the early-exit
0 commit comments