Well, when complete boolean evaluation is OFF {$B-} and given a capable programmer Pascal already will do that implicitly.
No, booleval is not used to control branch predictions (and avoid misspredictions). This is something completely different.
But it does not hurt to add attributes as reminder to the programmer that the most likely executed path should be the first part in the comparison.
This is not entirely true because it only applies to complex conditional statements with many
else if's. Moreover, in many cases checks are organized differently to increase the readability of the code.
Adding such as - basically empty - attributes [likely]/[unlikely] is already possible for classes enums and records.
Do they have to do with code optimization?
What you are suggesting is that based on such keywords can explicitly reverse the order of evaluation and I think that is not necessarily a good idea.
No, they are intended to inform the compiler about the probability of the conditional statement being true or false, so that the optimizer can produce faster code. This is especially important when we first need to check some variables (e.g. whether the pointer is not
nil), and then execute further code in which more similar checks may exist. You cannot change the order of conditions, but the compiler can optimize such code and gain run time, which is available in compilers for C++ and commonly used (e.g. in the Linux kernel, where
likely is used over 3,000 times, and
unlikely over a 14,000 times —
source).
The reason is because, at least in my opinion, it is the programmer's responsibility to organize the code to best fit the problem including taking into account the most likely case.
I wrote above why this is only a partial solution.
IOW, IMO, it isn't the compiler's job to rearrange instructions based on statistical concerns which may have unintended/unforeseen consequences.
The compiler has to do what the developer tells it to do, because it is just a stupid tool. If I know that some conditional statement needs to be pre-executed and that the condition is 99.9% likely to be met, I want to be able to tell the optimizer that this will be the case. As a developer, I am supposed to have control over what code will be emitted, not the compiler over me.
If I predict branching incorrectly, it will be my fault, I can change it at any time. Currently, I can't do anything except watch the CPU waste time on mispredictions. In the case of performance-critical software (like the game engine I work on), branch misprediction can be expensive. It would be very good for me and the compiler if we could support the optimizer with additional information and get better machine code.