Before converting PHP source code to opcode, is there any optimization process that eliminates dead conditionals?
Example:
<?php
if (false) {
echo 'false';
}
echo 'true';
Is it transformed to just echo 'true';?
If the answer is Yes, which of the following situations can PHP handle?
if(false); //Explicit boolean
$true = true; if($true); //Variable that was assigned a constant boolean
if(ClassName::Constant); //Class constant
if(1>2); //Constant value expression
If there are version specific differences, please be generous.
Yes, unreachable blocks will be eliminated by the "block pass" part of the opcache optimizer. For your particular examples:
if(false); // 1. Will be optimized
$true = true; if($true); // 2. Will NOT be optimized
if(ClassName::Constant); // 3. Will MAYBE be optimized
if(1>2); // 4. Will be optimized
Example 2 will not be optimized, because we do not currently perform constant propagation on "real" variables. Currently the optimizer does not use SSA form and as such we do not have the confidence to perform this type of optimization. Once we have that, we can cover this using an SCP / SCCP pass.
Example 3 may be optimized, depending on where ClassName was defined. Generally, if it's either self in a non-rebindable scope or a class defined in the same file, it will be optimized. Furthermore the value of the constant must also be statically evaluable constant expression.
The unreachable code elimination is implemented as part of block_pass.c.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With