-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dart2js] Reduce redundant phis with refinements
Redundant phi elimination removes phis that join the same value. However, this does not work when one or more of the inputs has a refinement (HTypeKnown). This change adds redundant phi elimination when the phi inputs have refinements. The need for this optimization shows up when static js_interop needs a dispatch on type for conversion: ``` final JSAny? jsValue; if (value is String) { jsValue = value.toJS; } else if (value is bool) { jsValue = value.toJS; ... ``` (The `.toJS` calls become no-ops since, for dart2js, we are already in JavaScript, and so leave an otherwise pointless if-then-else chain). Change-Id: If1a94856592163a81ac36c686cee04232c16d197 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/403950 Reviewed-by: Nate Biggs <[email protected]> Commit-Queue: Stephen Adams <[email protected]>
- Loading branch information
Showing
2 changed files
with
98 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
pkg/compiler/test/codegen/data/partial_phi_redundancy.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
@pragma('dart2js:never-inline') | ||
/*member: foo1:function(x) { | ||
if (Date.now() > 0) { | ||
if (typeof x != "string") | ||
return "bad1"; | ||
} else if (typeof x != "string") | ||
return "bad2"; | ||
return x; | ||
}*/ | ||
String foo1(Object x) { | ||
final Object y; | ||
if (DateTime.now().millisecondsSinceEpoch > 0) { | ||
if (x is! String) return 'bad1'; | ||
y = x; | ||
} else { | ||
if (x is! String) return 'bad2'; | ||
y = x; | ||
} | ||
// The phi for y has refinements to String on both branches, so the return | ||
// should not need stringification. | ||
return '$y'; | ||
} | ||
|
||
/*member: main:ignore*/ | ||
main() { | ||
print(foo1('a')); | ||
print(foo1('b')); | ||
print(foo1(123)); | ||
} |