Skip to content

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

Closed
@TimWolla

Description

@TimWolla

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions