Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logical or expressions are not always optimal. #17027

Open
floitschG opened this issue Feb 21, 2014 · 2 comments
Open

logical or expressions are not always optimal. #17027

floitschG opened this issue Feb 21, 2014 · 2 comments
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. dart2js-optimization P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug web-dart2js

Comments

@floitschG
Copy link
Contributor

floitschG commented Feb 21, 2014

After a fix to issue #16996 some logical or expressions are not optimal anymore.

See tests/compiler/dart2js/logical_expression_test.dart test moved here.

This happens when the first part of a logical-or is shared between several other logical or expressions:

var cond = foo();
if (cond || bar()) ...
if (cond || gee()) ...

This now generates:
var cond = !foo();
if (!cond || bar()) ...
if (!cond || gee()) ...

@floitschG
Copy link
Contributor Author

Removed Type-Defect label.
Added Type-Enhancement label.

@floitschG floitschG added Type-Enhancement P3 A lower priority bug or feature request web-dart2js dart2js-optimization labels Feb 22, 2014
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed triaged labels Mar 1, 2016
@vsmenon vsmenon added the area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. label Jul 20, 2019
@rakudrama
Copy link
Member

logical_expression_test.dart is still failing.

The mystery 'not' comes from here:
During CFG constructions a || b is is converted into CFG blocks equivalent to !a ? b : true.

In CFG codegen, diamond control flow for x ? y : true is generated as the expression !x || y (provided x and y can be generated as expressions).
If the HNot is generated-at-use, the two negations cancel out in generateNot

When the HNot is a common subexpression the 'nots' no longer cancel out, leading to the observed strange code.

How to fix this?

Converting a || b to a ? true : b and later recognizing x ? true : y as x || y would avoid the negations.
However, the code is worse when y cannot be emitted as an expression, since then it becomes an if statement where the code is in the else-part. Perhaps that can be fixed by adding the negation only when and if statement is needed (if (e) ; else S; --> if (!e) S;).
dart2js currently gets confused for nested && and || patterns. Perhaps using a ? true : b would help tell the pattern for || apart from that for && (which is a ? b : false, with the constant then on the other arm). Or perhaps, with more conditional control flow available in modern JavaScript (??, ??=, a?.b), dart2js should remember which control-flow operator matches the CFG.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. dart2js-optimization P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug web-dart2js
Projects
None yet
Development

No branches or pull requests

4 participants