Commit d7604f2c9c for perl

commit d7604f2c9cf00b9673955b717b57325017e95c19
Author: Richard Leach <rich+perl@hyphen-dash-hyphen.info>
Date:   Tue Nov 24 10:00:46 2026 +0000

    S_ssc_finalize now tries to optimize an ANYOF stclass

    Prior to this, an ANYOF synthetic start class (SSC) would not be optimized
    into an ANYOFM, even when that transformation was applied to the regnode
    from which the start class originated.

    For example, before:

        Compiling REx "[01]*[23]"
        synthetic stclass "ANYOF[0-3]".
        Final program:
           1: STAR (4)
           2:   ANYOFM[01] (0)
           4: ANYOFM[23] (6)
           6: END (0)
        stclass ANYOF[0-3] minlen 1

    Now:

        Compiling REx "[01]*[23]"
        synthetic stclass "ANYOFM[0-3]".
        Final program:
           1: STAR (4)
           2:   ANYOFM[01] (0)
           4: ANYOFM[23] (6)
           6: END (0)
        stclass ANYOFM[0-3] minlen 1

diff --git a/regcomp.c b/regcomp.c
index 9044cc8c92..0aac1d625e 100644
--- a/regcomp.c
+++ b/regcomp.c
@@ -1282,6 +1282,9 @@ S_ssc_finalize(pTHX_ RExC_state_t *pRExC_state, regnode_ssc *ssc)

     assert(is_ANYOF_SYNTHETIC(ssc));

+    /* Make sure is clone-safe */
+    ssc->invlist = NULL;
+
     /* The code in this file assumes that all but these flags aren't relevant
      * to the SSC, except SSC_MATCHES_EMPTY_STRING, which should be cleared
      * by the time we reach here */
@@ -1290,14 +1293,20 @@ S_ssc_finalize(pTHX_ RExC_state_t *pRExC_state, regnode_ssc *ssc)
             |ANYOFD_NON_UTF8_MATCHES_ALL_NON_ASCII__shared
             |ANYOF_HAS_EXTRA_RUNTIME_MATCHES)));

+    /* Check if a stclass more efficient than ANYOF, such as an ANYOFM,
+     * can be built */
+    if (!ANYOF_FLAGS(ssc) && !RExC_contains_locale
+        && optimize_invariant_class(invlist, (regnode *) ssc) != END)
+    {
+        SvREFCNT_dec_NN(invlist);
+        return;
+    }
+
     populate_anyof_bitmap_from_invlist( (regnode *) ssc, &invlist);

     set_ANYOF_arg(pRExC_state, (regnode *) ssc, invlist, NULL, NULL);
     SvREFCNT_dec(invlist);

-    /* Make sure is clone-safe */
-    ssc->invlist = NULL;
-
     if (ANYOF_POSIXL_SSC_TEST_ANY_SET(ssc)) {
         ANYOF_FLAGS(ssc) |= ANYOF_MATCHES_POSIXL;
         OP(ssc) = ANYOFPOSIXL;
diff --git a/t/re/opt.t b/t/re/opt.t
index 0b035c33ab..7c828dd75b 100644
--- a/t/re/opt.t
+++ b/t/re/opt.t
@@ -294,3 +294,8 @@ abc(*ACCEPT)xyz	3	0+abc	-	-
 \R+foo	4	-	1+foo	skip,stclass=LNBREAK	\R+ start class with PLUS and floating substr

 \A(?=a*)[0-9]a	2	1+a	-	noscan,anchor SBOL,stclass=~^ANYOF\[0-9\]	nullable lookahead does not restrict the start class
+
+# Synthetic start classes can be optimized
+[01]*[23]	1	-	-	stclass=ANYOFM[0-3]	ANYOF can become an ANYOFM
+[01]*[24]	1	-	-	stclass=ANYOF[0124]	ANYOF remains when ANYOFM unviable
+(?l)[[:digit:]]?x	1	-	0:1+x	stclass=ANYOFPOSIXL[x\d]	No ANYOFM when locale matters