In SimplifyCFGPass we have transformed specific branches to select instruction and we sometimes want undo this transformation in CGP.
Currently, CGP only transform select i1 %cond to branches if %cond has one use. https://reviews.llvm.org/D24147 relaxes it a bit, but it relies on the branch weight on the select instruction and SimplifyCFGPass looks not maintaining this branch weight when perform the transformation.
Some of our internal workload do some computation like
loop: ... if (cond) { // update max/min values // update indexes of max/min values }
and will be like
loop: select cond ... select cond ... select cond ... ...
after SimplifyCFGPass.
If these select are retained after CGP, we will have redundant computations of select's operands in the loop and hurt the performance(even the compuation of the operand is cheap, but it's in the loop, sill has big impact on the performance). This patch tries to mitigate this situation.
Introduced new flag cgp-sink-select-operand-ratio-against-misprediction to model such situation roughly. Say computing %t's cost is Cost(%t), the probability of taking %t is P(%t), misprediction probability is PM, misprediction penalty is PENALTY. We want Cost(%t) * P(%t) + PM * PENALTY < Cost(%t) aka PENALTY < Cost(%t)*(1-P(%t))/PM when sinking select operand.
This is not sound. Checking for just one use was conservative but it ensured that the instruction can be sinked. If other select instructions use the same operand, the operand can be sinked only if it is always used on the same path (i.e., always true or false operand).
Here is an example:
If converted to branch, %x and %y cannot be sinked since they are needed in both paths:
So, you will need to change the check here to account for such scenarios.