Skip to content

Conversation

jserv
Copy link
Collaborator

@jserv jserv commented Sep 1, 2025

Virtual Register (VReg) abstraction layer is added to improve register allocation efficiency.

@jserv jserv changed the title Introduce Virtual Register (VReg) abstraction layer to improve register allocation efficiency Introduce Virtual Register abstraction to improve register allocation Sep 1, 2025
return n;
}

int calculate_spill_cost(var_t *var, basic_block_t *bb, int current_idx)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although the commit message has explained "cost", how about also adding some comments here?

This commit introduces a Virtual Register (VReg) abstraction layer to
improve register allocation efficiency:
- Add phys_reg field to var_t for direct physical register mapping
- Optimize prepare_operand() and prepare_dest() with O(1) lookups
- Eliminate redundant linear register searches
Building on the VReg abstraction, this commit adds intelligent spill
decisions based on variable usage patterns and context.
- Track variable lifetime with first_use, last_use, and use_count
- Add loop_depth field for identifying loop-resident variables
- Transform spill cost from simple distance to multi-factor heuristic:
  * Loop variables: +200 cost per nesting level
  * Usage frequency: +5 cost per use
  * Live range length: +20 for long ranges
  * Constants: -50 (prefer spilling for rematerialization)
@jserv jserv requested a review from DrXiao September 3, 2025 09:13
@jserv jserv merged commit dd9c3aa into master Sep 3, 2025
12 checks passed
@jserv jserv deleted the improve-regalloc branch September 3, 2025 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants