SimplifyGlobals: Ignore irrelevant effects in read-only-to-write #4363
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Previously this pass would see something like this and fail:
The call to
foo()has side effects, so we did not optimize. However, in sucha case the side effects are safe: they happen anyhow, regardless of the global
that we are optimizing. That is,
"global"is read only to be written, even thoughother things also influence the decision to write it. But
"global"is not used in away that is observable: we can remove it, and nothing will notice (except for
things getting smaller/faster).
In other words, this PR will let us optimize the above example, while it also
needs to avoid optimizing the dangerous cases, like this:
Here
"global"flows into a place that notices its value and may use it asidefrom deciding to write that global.
A common case where we want to optimize is combined ifs,
which the optimizer turns into
With this PR we can handle those things too.
This lets us optimize out some important globals in j2wasm like the initializer
boolean for the Math object, reducing some total 0.5% of code size.