Skip to content

Instantly share code, notes, and snippets.

@rurban
Created July 15, 2010 20:50
Show Gist options
  • Save rurban/477517 to your computer and use it in GitHub Desktop.
Save rurban/477517 to your computer and use it in GitHub Desktop.
#! perl
package
optimizer::and_not;
=head1 DESCRIPTION
[perl #76438] peephole optimiser could prune more dead code
optimize (AND CONST->NO) to null if no GVSV/PADSV, else (DOR $x) or do some SvGETMAGIC.
(AND CONST->NO) is always false, but all SVs must call their mg_get for all SVs before NO
See also the p5p thread at
http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2010-07/msg00460.html
=head1 SYNOPSIS
perl -Moptimizer::and_not -e'if ($a and "x" eq "y") { print $s;}'
=head1 EXAMPLE1 gvsv
$ perl -MO=Concise,-exec -e'if ($a and "x" eq "y") { print $s;}'
1 <0> enter
2 <;> nextstate(main 3 -e:1) v:{
3 <$> gvsv(*a) s
4 <|> and(other->5) sK/1
5 <$> const(SPECIAL sv_no) s
6 <|> and(other->7) vK/1
7 <0> pushmark s
8 <$> gvsv(*s) s
9 <@> print vK
a <@> leave[1 ref] vKP/REFC
can be optimized to
1 <0> enter
2 <;> nextstate(main 3 -e:1) v:{
3 <$> gvsv(*a) s
4 <1> dor vK/1
a <@> leave[1 ref] vKP/REFC
=head1 EXAMPLE2 padsv
$ perl -MO=Concise,-exec -e'my $a; if ($a and "x" eq "y") { print $s;}'
1 <0> enter
2 <;> nextstate(main 1 -e:1) v:{
3 <0> padsv[$a:1,4] vM/LVINTRO
...
4 <;> nextstate(main 4 -e:1) v:{
5 <0> padsv[$a:1,4] s
6 <|> and(other->7) sK/1
7 <$> const[SPECIAL sv_no] s
8 <|> and(other->9) vK/1
9 <0> pushmark s
a <#> gvsv[*s] s
b <@> print vK
c <@> leave[1 ref] vKP/REFC
can be optimized to
1 <0> enter
2 <;> nextstate(main 1 -e:1) v:{
3 <0> padsv[$a:1,3] vM/LVINTRO
...
4 <;> nextstate(main 2 -e:1) v:{
5 <$> padsv([$a:1,3) s
6 <1> dor vK/1
7 <@> leave[1 ref] vKP/REFC
=head1 EXAMPLE3 ok
$ perl -MO=Concise,-exec -e'if ("x" eq "y" and $a) { print $s;}'
is already optimized to
1 <0> enter
2 <;> nextstate(main 3 -e:1) v:{
3 <@> leave[1 ref] vKP/REFC
=cut
use optimizer 'extend-c' => sub {
my $o = shift or return;
warn " ..".$o->name."\n";
if ($o->isa("B::OP")
and ($o->name eq 'gvsv' or $o->name eq 'padsv')
and ${$o->next} and $o->next->name eq 'and'
and ${$o->next->other} and $o->next->other->name eq 'const'
and ${$o->next->other->sv} == 3 # B::sv_no = SPECIAL 0x3
)
{
# TODO change o->next to dor and nullify the rest
warn " .. optimize ".uc $o->name." AND CONST 3 pushmark... to DOR\n";
}
};
1;
@rurban
Copy link
Author

rurban commented Jul 15, 2010

[perl #76438] peephole optimiser could prune more dead code

See also the p5p thread at http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2010-07/msg00460.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment