Skip to content

[GR-77075] Add instruction rewriting support for stack values. - #14333

Merged
graalvmbot merged 4 commits into
masterfrom
mdsouza/GR-77075-stack-value-rewrites
Aug 28, 2026
Merged

[GR-77075] Add instruction rewriting support for stack values.#14333
graalvmbot merged 4 commits into
masterfrom
mdsouza/GR-77075-stack-value-rewrites

Conversation

@graalvmbot

Copy link
Copy Markdown
Collaborator

Adds some new rewrite rules and the rewrite machinery to support them (immediate matching, REPLACE sections).

Also refactors the builder to keep operation stack entries on stack until after instruction emission, so that we can rewrite child bci's stored in the current operation stack entry.

New rewrite rules:

      store.stackvalue(k), pop * k - 1, return -> return

In text: if the result of a block is returned, we can skip the cleanup instructions since the frame will be thrown away.

        load.stackvalue(n - 1) * n, X, pop * n -> X   (void X consumes n values)
        load.stackvalue(n - 1) * n, X, store.stackvalue(n), pop * (n - 1) -> X  (non-void X consumes n == k stack values)
        load.stackvalue(n - 1) * n, X, store.stackvalue(k), pop * (k - 1) -> X, store.stackvalue(k - n), pop * (k - n - 1)  (non-void X consumes n < k stack values)

In text: if X consumes some stack values from the top of stack, and then we pop those values, let X consume them directly.

…uctions when returning from block

Adds rewrite rules of the form:

  store.stackvalue(k), pop * k - 1, return -> return

Essentially, when the result of a block is returned, we can skip the cleanup instructions since the frame will be thrown away.
This is a non-functional change necessary to properly remap childBci fields.
Adds rewrite rules of the form:

    load.stackvalue(n - 1) * n, X, pop * n -> X   (void X consumes n values)
    load.stackvalue(n - 1) * n, X, store.stackvalue(n), pop * (n - 1) -> X  (non-void X consumes n == k stack values)
    load.stackvalue(n - 1) * n, X, store.stackvalue(k), pop * (k - 1) -> X, store.stackvalue(k - n), pop * (k - n - 1)  (non-void X consumes n < k stack values)

This change requires limited support for REPLACE sections that replace 1 instruction with 1 instruction in order to replace the store.stackvalue instruction in the last pattern.
Since BCIs into instructions are disallowed, this limitation makes replacement safe without adding full support to check for ambiguous BCIs.
@oracle-contributor-agreement oracle-contributor-agreement Bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Aug 28, 2026
@graalvmbot
graalvmbot merged commit 739371e into master Aug 28, 2026
3 checks passed
@graalvmbot
graalvmbot deleted the mdsouza/GR-77075-stack-value-rewrites branch August 28, 2026 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

OCA Verified All contributors have signed the Oracle Contributor Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants