develooper Front page | perl.perl5.porters | Postings from July 2013

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

From:
Niels Thykier
Date:
July 18, 2013 16:23
Subject:
Re: [perl #76438] peephole optimiser could prune more dead code
Message ID:
51E81659.6090608@thykier.net
Hi,

Attached is a prototype branch that enables some branch elimination
while retaining possible side-effects OPs.

It can kill branches in if-else cases.  Examples:

  $ ./perl -Ilib -MO=Deparse \
    -e 'if ($a && "Pie" eq "Good") {print "True\n" }'
    -e ' else { print "False\n" }'
  unless ($a and !1) {
      print "False\n";
  }

  $ ./perl -Ilib -MO=Deparse \
    -e 'if ($a || "Pie" ne "Good") {print "True\n" }'
    -e ' else { print "False\n" }'
  if ($a or 1) {
      print "True\n";
  }


The patch adds two op_private flags for OP_AND and OP_OR, which
determines if the expression will always evaluate to either TRUE or
FALSE.  If newCONDOP detects its condition is one of these two OPs and
they have one of these flags set, it will kill the dead branch.

However, the newLOGOP does not use this flag itself to elimite dead
branches.  Accordingly, the original test case is still:

  $ ./perl -Ilib -MO=Deparse -e 'if ($a && "Pie" ne "Good") {print}'
  if ($a and 1) {
      print $_;
  }

The only problem here is that folding the OP_AND/OP_OR expressions
should probably be deferred to peep optimisation.  Otherwise, we might fold:

  $a && "Pie" ne "Good"  =>  $a

Before newCONP has a change to see it (and thus lose the ability to
eliminate the dead branch).

~Niels




nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About