compiler: improve boundary analysis and optimizations #110
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.
This PR implements better boundary analysis to optimize memory accesses.
In current implementation, boundary analysis was only performed for iterations, to do this the semantic analyzer associated each iteration variable with the iteration expression and had some strict conditions: for example optimization was mostly applied when the index value was immutable, but it could not optimize the cost of boundary checking where it was mutable and could be used safely before being mutated.
In addition, compiler optimizes array indexing with constant expressions. However, that's a simple optimization. Just check whether expression is constant and fits size of array.
New Boundary Analysis Implementation
The new boundary analysis tracks constant values and expressions that can be predicted at runtime. If the expressions are safe until they are mutated or being risky, it optimizes boundary access checking and provides faster access to memory at runtime without compromising safety.
For example:
In the code example above, the compiler knows at compile time that the length of
x
is3
, so it skips the runtime boundary checking analysis of subsequent accesses.