@@ -56,19 +56,11 @@ class PhaseOptimizer implements CompilerPass {
56
56
private NamedPass currentPass ;
57
57
// For each pass, remember the time at the end of the pass's last run.
58
58
private Map <NamedPass , Integer > lastRuns ;
59
- private Node currentScope ;
60
- // Starts at 0, increases as "interesting" things happen.
61
- // Nothing happens at time START_TIME, the first pass starts at time 1.
62
- // The correctness of scope-change tracking relies on Node/getIntProp
63
- // returning 0 if the custom attribute on a node hasn't been set.
64
- private int timestamp ;
65
59
// The time of the last change made to the program by any pass.
66
60
private int lastChange ;
67
61
private static final int START_TIME = 0 ;
68
62
private final Node jsRoot ;
69
- // Compiler/reportChangeToScope must call reportCodeChange to update all
70
- // change handlers. This flag prevents double update in ScopedChangeHandler.
71
- private boolean crossScopeReporting ;
63
+
72
64
private final boolean useSizeHeuristicToStopOptimizationLoop ;
73
65
74
66
// Used for sanity checks between loopable passes
@@ -130,8 +122,7 @@ enum State {
130
122
this .passes = new ArrayList <>();
131
123
this .progressRange = range ;
132
124
this .inLoop = false ;
133
- this .crossScopeReporting = false ;
134
- this .timestamp = this .lastChange = START_TIME ;
125
+ this .lastChange = START_TIME ;
135
126
this .useSizeHeuristicToStopOptimizationLoop =
136
127
comp .getOptions ().useSizeHeuristicToStopOptimizationLoop ;
137
128
}
@@ -198,6 +189,7 @@ void setSanityCheck(PassFactory sanityCheck) {
198
189
}
199
190
200
191
private void setSanityCheckState () {
192
+ // TODO(johnlenz): change this to always validate. See b/37164291
201
193
if (inLoop ) {
202
194
lastAst = jsRoot .cloneTree ();
203
195
mtoc = NodeUtil .mapMainToClone (jsRoot , lastAst );
@@ -242,8 +234,8 @@ public void process(Node externs, Node root) {
242
234
243
235
private void maybePrintAstHashcodes (String passName , Node root ) {
244
236
if (printAstHashcodes ) {
245
- String hashCodeMsg = "AST hashCode after " + passName + ": " +
246
- compiler .toSource (root ).hashCode ();
237
+ String hashCodeMsg = "AST hashCode after " + passName + ": "
238
+ + compiler .toSource (root ).hashCode ();
247
239
System .err .println (hashCodeMsg );
248
240
compiler .addToDebugLog (hashCodeMsg );
249
241
}
@@ -252,7 +244,7 @@ private void maybePrintAstHashcodes(String passName, Node root) {
252
244
/**
253
245
* Runs the sanity check if it is available.
254
246
*/
255
- private void maybeSanityCheck (Node externs , Node root ) {
247
+ private void maybeSanityCheck (String passName , Node externs , Node root ) {
256
248
if (sanityCheck != null ) {
257
249
sanityCheck .create (compiler ).process (externs , root );
258
250
// The cross-module passes are loopable and ran together, but do not
@@ -261,7 +253,7 @@ private void maybeSanityCheck(Node externs, Node root) {
261
253
if (inLoop
262
254
&& !currentPass .name .equals (Compiler .CROSS_MODULE_CODE_MOTION_NAME )
263
255
&& !currentPass .name .equals (Compiler .CROSS_MODULE_METHOD_MOTION_NAME )) {
264
- NodeUtil .verifyScopeChanges (mtoc , jsRoot , true );
256
+ NodeUtil .verifyScopeChanges (passName , mtoc , jsRoot );
265
257
}
266
258
}
267
259
}
@@ -320,7 +312,7 @@ public void process(Node externs, Node root) {
320
312
tracker .recordPassStop (name , traceRuntime );
321
313
}
322
314
maybePrintAstHashcodes (name , root );
323
- maybeSanityCheck (externs , root );
315
+ maybeSanityCheck (name , externs , root );
324
316
} catch (IllegalStateException e ) {
325
317
// TODO(johnlenz): Remove this once the normalization checks report
326
318
// errors instead of exceptions.
@@ -334,14 +326,6 @@ public String toString() {
334
326
}
335
327
}
336
328
337
- void setScope (Node n ) {
338
- // NodeTraversal causes setScope calls outside loops; ignore them.
339
- if (inLoop ) {
340
- // Find the top-level node in the scope.
341
- currentScope = n .isFunction () ? n : getEnclosingScope (n );
342
- }
343
- }
344
-
345
329
boolean hasScopeChanged (Node n ) {
346
330
// Outside loops we don't track changed scopes, so we visit them all.
347
331
if (!inLoop ) {
@@ -353,65 +337,26 @@ boolean hasScopeChanged(Node n) {
353
337
|| n .getChangeTime () > timeOfLastRun ;
354
338
}
355
339
356
- private Node getEnclosingScope (Node n ) {
357
- while (n .getParent () != null ) {
358
- n = n .getParent ();
359
- if (n .isFunction () || n .isScript ()) {
360
- return n ;
361
- }
362
- }
363
- return n ;
364
- }
365
-
366
- void reportChangeToEnclosingScope (Node n ) {
367
- lastChange = timestamp ;
368
- getEnclosingScope (n ).setChangeTime (timestamp );
369
- // Every code change happens at a different time
370
- timestamp ++;
371
- }
372
-
373
- /**
374
- * Records that the currently-running pass may report cross-scope changes.
375
- * When this happens, we don't want to falsely report the current scope as
376
- * changed when reportChangeToScope is called from Compiler.
377
- */
378
- void startCrossScopeReporting () {
379
- crossScopeReporting = true ;
380
- }
381
-
382
- /** The currently-running pass won't report cross-scope changes. */
383
- void endCrossScopeReporting () {
384
- crossScopeReporting = false ;
385
- }
386
-
387
340
/**
388
341
* A change handler that marks scopes as changed when reportChange is called.
389
342
*/
390
343
private class ScopedChangeHandler implements CodeChangeHandler {
391
344
private int lastCodeChangeQuery ;
392
345
393
346
ScopedChangeHandler () {
394
- this .lastCodeChangeQuery = timestamp ;
347
+ this .lastCodeChangeQuery = compiler . getChangeStamp () ;
395
348
}
396
349
397
350
@ Override
398
351
public void reportChange () {
399
- if (crossScopeReporting ) {
400
- // This call was caused by Compiler/reportChangeToEnclosingScope,
401
- // do nothing.
402
- return ;
403
- }
404
- lastChange = timestamp ;
405
- currentScope .setChangeTime (timestamp );
406
- // Every code change happens at a different time
407
- timestamp ++;
352
+ lastChange = compiler .getChangeStamp ();
408
353
}
409
354
410
355
private boolean hasCodeChangedSinceLastCall () {
411
356
boolean result = lastChange > lastCodeChangeQuery ;
412
- lastCodeChangeQuery = timestamp ;
357
+ lastCodeChangeQuery = compiler . getChangeStamp () ;
413
358
// The next call to the method will happen at a different time
414
- timestamp ++ ;
359
+ compiler . incrementChangeStamp () ;
415
360
return result ;
416
361
}
417
362
}
@@ -449,7 +394,11 @@ public void process(Node externs, Node root) {
449
394
// Set up function-change tracking
450
395
scopeHandler = new ScopedChangeHandler ();
451
396
compiler .addChangeHandler (scopeHandler );
452
- setScope (root );
397
+
398
+ // TODO(johnlenz): It is unclear why "setScope" is called here. Try to remove
399
+ // this.
400
+ compiler .setScope (root );
401
+
453
402
// lastRuns is initialized before each loop. This way, when a pass is run
454
403
// in the 2nd loop for the 1st time, it looks at all scopes.
455
404
lastRuns = new HashMap <>();
@@ -487,11 +436,11 @@ public void process(Node externs, Node root) {
487
436
&& !runInPrevIter .contains (pass ))
488
437
|| (state == State .RUN_PASSES_THAT_CHANGED_STH_IN_PREV_ITER
489
438
&& madeChanges .contains (pass ))) {
490
- timestamp ++ ;
439
+ compiler . incrementChangeStamp () ;
491
440
currentPass = pass ;
492
441
pass .process (externs , root );
493
442
runInPrevIter .add (pass );
494
- lastRuns .put (pass , timestamp );
443
+ lastRuns .put (pass , compiler . getChangeStamp () );
495
444
if (hasHaltingErrors ()) {
496
445
return ;
497
446
} else if (scopeHandler .hasCodeChangedSinceLastCall ()) {
0 commit comments