Skip to content

Make OP_DEFINED eligible for constant folding #23349

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

Open
wants to merge 1 commit into
base: blead
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions op.c
Original file line number Diff line number Diff line change
Expand Up @@ -5011,6 +5011,12 @@ S_fold_constants(pTHX_ OP *const o)
if (cUNOPx(cUNOPo->op_first)->op_first->op_type != OP_CONST
|| SvPADTMP(cSVOPx_sv(cUNOPx(cUNOPo->op_first)->op_first)))
goto nope;
break;
case OP_DEFINED:
if ( (cUNOPo->op_first->op_type != OP_CONST)
|| cUNOPo->op_first->op_moresib )
goto nope;
break;
}

if (PL_parser && PL_parser->error_count)
Expand Down
2 changes: 1 addition & 1 deletion opcode.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion regen/opcodes
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ chop chop ck_spair mts% L
schop scalar chop ck_null stu% S?
chomp chomp ck_spair mTs% L
schomp scalar chomp ck_null sTu% S?
defined defined operator ck_defined isu% S?
defined defined operator ck_defined ifsu% S?
undef undef operator ck_fun s% R?
study study ck_fun su% S?
pos match position ck_fun stu% R?
Expand Down
8 changes: 8 additions & 0 deletions t/perf/opcount.t
Original file line number Diff line number Diff line change
Expand Up @@ -1106,4 +1106,12 @@ test_opcount(0, "substr with const zero offset (gv)",
sassign => 1
});

# defined(SOMECONST) gets constant folded
test_opcount(0, "defined(ABC) gets constant folded",
sub { use constant ABC => 1; my $x = (defined(ABC)) ? 1 : 0 },
{
cond_expr => 0,
defined => 0,
});

done_testing();
Loading