Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "op_scope/newCONDOP: Optimise away empty else blocks in OP_COND_EXPR #22870

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 7 additions & 16 deletions lib/B/Deparse.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3966,22 +3966,13 @@ sub pp_cond_expr {
my $true = $cond->sibling;
my $false = $true->sibling;
my $cuddle = $self->{'cuddle'};

if (class($false) eq "NULL") { # Empty else {} block was optimised away
unless ($cx < 1 and (is_scope($true) and $true->name ne "null")) {
$cond = $self->deparse($cond, 8);
$true = $self->deparse($true, 6);
return $self->maybe_parens("$cond ? $true : ()", $cx, 8);
}
} else { # Both true and false branches are present
unless ($cx < 1 and (is_scope($true) and $true->name ne "null")
and (is_scope($false) || is_ifelse_cont($false))
and $self->{'expand'} < 7) {
$cond = $self->deparse($cond, 8);
$true = $self->deparse($true, 6);
$false = $self->deparse($false, 8);
return $self->maybe_parens("$cond ? $true : $false", $cx, 8);
}
unless ($cx < 1 and (is_scope($true) and $true->name ne "null") and
(is_scope($false) || is_ifelse_cont($false))
and $self->{'expand'} < 7) {
$cond = $self->deparse($cond, 8);
$true = $self->deparse($true, 6);
$false = $self->deparse($false, 8);
return $self->maybe_parens("$cond ? $true : $false", $cx, 8);
}

$cond = $self->deparse($cond, 1);
Expand Down
4 changes: 0 additions & 4 deletions lib/B/Deparse.t
Original file line number Diff line number Diff line change
Expand Up @@ -3378,7 +3378,3 @@ $_ = (!$p) isa 'Some::Class';
$_ = (!$p) =~ tr/1//;
$_ = (!$p) =~ /1/;
$_ = (!$p) =~ s/1//r;
####
# Else block of a ternary is optimised away
my $x;
my(@y) = $x ? [1, 2] : ();
23 changes: 4 additions & 19 deletions op.c
Original file line number Diff line number Diff line change
Expand Up @@ -4517,12 +4517,6 @@ Perl_op_scope(pTHX_ OP *o)
{
if (o) {
if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || TAINTING_get) {

/* There's no need to wrap an empty stub in an enter/leave.
* This also makes eliding empty if/else blocks simpler. */
if (OP_TYPE_IS(o, OP_STUB) && (o->op_flags & OPf_PARENS))
return o;

o = op_prepend_elem(OP_LINESEQ,
newOP(OP_ENTER, (o->op_flags & OPf_WANT)), o);
OpTYPE_set(o, OP_LEAVE);
Expand Down Expand Up @@ -9274,6 +9268,7 @@ Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
logop = alloc_LOGOP(OP_COND_EXPR, first, LINKLIST(trueop));
logop->op_flags |= (U8)flags;
logop->op_private = (U8)(1 | (flags >> 8));
logop->op_next = LINKLIST(falseop);

CHECKOP(OP_COND_EXPR, /* that's logop->op_type */
logop);
Expand All @@ -9282,23 +9277,13 @@ Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
start = LINKLIST(first);
first->op_next = (OP*)logop;

/* make first & trueop siblings */
/* falseop may also be a sibling, if not optimised away */
/* make first, trueop, falseop siblings */
op_sibling_splice((OP*)logop, first, 0, trueop);
op_sibling_splice((OP*)logop, trueop, 0, falseop);

o = newUNOP(OP_NULL, 0, (OP*)logop);

if (OP_TYPE_IS(falseop, OP_STUB) && (falseop->op_flags & OPf_PARENS)) {
/* The `else` block is empty. Optimise it away. */
logop->op_next = o;
op_free(falseop);
} else {
op_sibling_splice((OP*)logop, trueop, 0, falseop);
logop->op_next = LINKLIST(falseop);
falseop->op_next = o;
}

trueop->op_next = o;
trueop->op_next = falseop->op_next = o;

o->op_next = start;
return o;
Expand Down
Loading