Skip to content

Merge T2 = BOOL T1 and T3 = IS_IDENTICAL T2 bool(true) into T3 = BOOL T1 #18411

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
TimWolla opened this issue Apr 23, 2025 · 0 comments · May be fixed by #18423
Open

Merge T2 = BOOL T1 and T3 = IS_IDENTICAL T2 bool(true) into T3 = BOOL T1 #18411

TimWolla opened this issue Apr 23, 2025 · 0 comments · May be fixed by #18423

Comments

@TimWolla
Copy link
Member

Description

This is related to php/doc-en#4564 (review):

<?php

$text = 'Bienvenue chez nous';

$result = match (true) {
    !!str_contains($text, 'Welcome'), !!str_contains($text, 'Hello') => 'en',
    !!str_contains($text, 'Bienvenue'), !!str_contains($text, 'Bonjour') => 'fr',
    // ...
};

currently results in:


$_main:
     ; (lines=23, args=0, vars=2, tmps=3)
     ; (after optimizer)
     ; test3.php:1-10
0000 ASSIGN CV0($text) string("Bienvenue chez nous")
0001 T2 = FRAMELESS_ICALL_2(str_contains) CV0($text) string("Welcome")
0002 T3 = BOOL T2
0003 T2 = IS_IDENTICAL T3 bool(true)
0004 JMPNZ T2 0018
0005 T4 = FRAMELESS_ICALL_2(str_contains) CV0($text) string("Hello")
0006 T3 = BOOL T4
0007 T2 = IS_IDENTICAL T3 bool(true)
0008 JMPNZ T2 0018
0009 T4 = FRAMELESS_ICALL_2(str_contains) CV0($text) string("Bienvenue")
0010 T3 = BOOL T4
0011 T2 = IS_IDENTICAL T3 bool(true)
0012 JMPNZ T2 0020
0013 T4 = FRAMELESS_ICALL_2(str_contains) CV0($text) string("Bonjour")
0014 T3 = BOOL T4
0015 T2 = IS_IDENTICAL T3 bool(true)
0016 JMPNZ T2 0020
0017 MATCH_ERROR bool(true)
0018 T2 = QM_ASSIGN string("en")
0019 JMP 0021
0020 T2 = QM_ASSIGN string("fr")
0021 ASSIGN CV1($result) T2
0022 RETURN int(1)

and is from a OPcode count PoV worse than:

<?php

$text = 'Bienvenue chez nous';

$result = match (true) {
    str_contains($text, 'Welcome') || str_contains($text, 'Hello') => 'en',
    str_contains($text, 'Bienvenue') || str_contains($text, 'Bonjour') => 'fr',
    // ...
};

which results in:


$_main:
     ; (lines=19, args=0, vars=2, tmps=3)
     ; (after optimizer)
     ; test2.php:1-10
0000 ASSIGN CV0($text) string("Bienvenue chez nous")
0001 T3 = FRAMELESS_ICALL_2(str_contains) CV0($text) string("Welcome")
0002 T3 = JMPNZ_EX T3 0005
0003 T2 = FRAMELESS_ICALL_2(str_contains) CV0($text) string("Hello")
0004 T3 = BOOL T2
0005 T2 = IS_IDENTICAL T3 bool(true)
0006 JMPNZ T2 0014
0007 T3 = FRAMELESS_ICALL_2(str_contains) CV0($text) string("Bienvenue")
0008 T3 = JMPNZ_EX T3 0011
0009 T4 = FRAMELESS_ICALL_2(str_contains) CV0($text) string("Bonjour")
0010 T3 = BOOL T4
0011 T2 = IS_IDENTICAL T3 bool(true)
0012 JMPNZ T2 0016
0013 MATCH_ERROR bool(true)
0014 T2 = QM_ASSIGN string("en")
0015 JMP 0017
0016 T2 = QM_ASSIGN string("fr")
0017 ASSIGN CV1($result) T2
0018 RETURN int(1)

Without a semantic change it should be possible to optimize

0001 T2 = FRAMELESS_ICALL_2(str_contains) CV0($text) string("Welcome")
0002 T3 = BOOL T2
0003 T2 = IS_IDENTICAL T3 bool(true)
0004 JMPNZ T2 0018

into

0001 T2 = FRAMELESS_ICALL_2(str_contains) CV0($text) string("Welcome")
0002 T3 = BOOL T2
0004 JMPNZ T3 0018

and then even further into:

0001 T2 = FRAMELESS_ICALL_2(str_contains) CV0($text) string("Welcome")
0004 JMPNZ T2 0018
@TimWolla TimWolla self-assigned this Apr 24, 2025
TimWolla added a commit to TimWolla/php-src that referenced this issue Apr 24, 2025
@TimWolla TimWolla linked a pull request Apr 24, 2025 that will close this issue
TimWolla added a commit to TimWolla/php-src that referenced this issue Apr 24, 2025
TimWolla added a commit to TimWolla/php-src that referenced this issue Apr 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant