diff --git a/op.c b/op.c index f616532c491c..072e683a8b9b 100644 --- a/op.c +++ b/op.c @@ -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) diff --git a/opcode.h b/opcode.h index 8f040e1813c9..bcd9023068e6 100644 --- a/opcode.h +++ b/opcode.h @@ -1915,7 +1915,7 @@ EXTCONST U32 PL_opargs[] INIT({ 0x00009b8c, /* schop */ 0x00002b1d, /* chomp */ 0x00009b9c, /* schomp */ - 0x00009b84, /* defined */ + 0x00009b86, /* defined */ 0x0000fb04, /* undef */ 0x00009b84, /* study */ 0x0000fb8c, /* pos */ diff --git a/regen/opcodes b/regen/opcodes index 46965259731b..f7267a4e3a95 100644 --- a/regen/opcodes +++ b/regen/opcodes @@ -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? diff --git a/t/perf/opcount.t b/t/perf/opcount.t index 8695e162d16e..d3863690323e 100644 --- a/t/perf/opcount.t +++ b/t/perf/opcount.t @@ -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();