Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[dotnet] Quick and dirty experimental optimization that demotes lexic…
…al to locals in a few places (so their storage slot goes on the stack and is instantly to hand). Again, optimizer not enabled by default.
  • Loading branch information
jnthn committed Nov 21, 2010
1 parent 361ec9a commit a7baae0
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion dotnet/compiler/NQPOptimizer.pm
Expand Up @@ -33,6 +33,11 @@ our multi optimizer(PAST::Block $block) {
$block.control('');
}

# If there's no inner blocks, we can demote lexicals to constants.
unless get_annotation($block, 'has_nested_blocks') {
demote_lexicals_to_locals($block);
}

# Remove this block from the block stack.
@*BLOCK_STACK.shift();
}
Expand Down Expand Up @@ -63,10 +68,13 @@ our multi optimizer(PAST::Op $op) {
}

our multi optimizer(PAST::Var $var) {
# May need to visit the viviself.
# May need to visit the viviself as well as children.
if $var.viviself ~~ PAST::Node {
optimizer($var.viviself);
}
for @($var) {
optimizer($_);
}
}

our multi optimizer(PAST::Val $stmts) {
Expand All @@ -91,3 +99,49 @@ sub annotate($node, $key, $value) {
sub get_annotation($node, $key) {
$node{'nqp_opt_' ~ $key}
}

# Demotes lexicals to local variables.
sub demote_lexicals_to_locals($block) {
my %*demoted;
my $*counter := 0;
for @($block) {
demote_lexicals_worker($_);
}
}
our multi sub demote_lexicals_worker(PAST::Stmts $stmts) {
for @($stmts) {
demote_lexicals_worker($_);
}
}
our multi sub demote_lexicals_worker(PAST::Var $var) {
if %*demoted{$var.name} {
$var.scope('register');
$var.name(%*demoted{$var.name});
}
elsif $var.isdecl && $var.scope eq 'lexical' {
# Make sure it's not a context var.
my $twigil := pir::substr($var.name, 1, 1);
if $twigil ne '*' && $twigil ne '?' {
# Good demotion candidate.
my $new_name := 'demoted_lexical_' ~ $*counter;
$*counter := $*counter + 1;
%*demoted{$var.name} := $new_name;
$var.name($new_name);
$var.scope('register');
}
}
if $var.viviself {
demote_lexicals_worker($var.viviself);
}
for @($var) {
demote_lexicals_worker($_);
}
}
our multi sub demote_lexicals_worker(PAST::Op $op) {
for @($op) {
demote_lexicals_worker($_);
}
}
our multi sub demote_lexicals_worker($any) {
# Nothing to do for this node type
}

0 comments on commit a7baae0

Please sign in to comment.