[llvm] [SimplifyCFG] Probabilities associated with same condition are constant (PR #155734)

David Li via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 3 09:47:49 PDT 2025


================
@@ -4808,23 +4808,12 @@ static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI,
       SelectInst *NV = cast<SelectInst>(
           Builder.CreateSelect(PBICond, PBIV, BIV, PBIV->getName() + ".mux"));
       PN.setIncomingValue(PBBIdx, NV);
-      // Although the select has the same condition as PBI, the original branch
-      // weights for PBI do not apply to the new select because the select's
-      // 'logical' edges are incoming edges of the phi that is eliminated, not
-      // the outgoing edges of PBI.
+      // The select has the same condition as PBI. The probabilities don't
----------------
david-xl wrote:

I looked at the original change that introduced this. I am not convinced it is  completely wrong though. The reason is that the branch probability is control flow dependent.  For instance :

if (a) {
     if (a) {
        ...
      }
}

These two branches have the same condition, but the second branch is 100% biased.

The select case is tricky -- the 'select cmpa, <v1>, <v2>' is result is intended to be used under the condition if (cmpa || cmpb), so the branch probability should change logically, however the select statement is no longer control dependent, so it should not use the control dependent branch prob. The dillema is that 1) the current impl is wrong if select is lowered to branch (but not the 'or statement', 2) however if some cfg lowering that pushes the select under the guarding control flow, then we will lose information (control dependent branch prob).

I am leaning toward making it consistent (i.e. the way your patch does). 

https://github.com/llvm/llvm-project/pull/155734


More information about the llvm-commits mailing list