{"record":{"id":"429852a1ddf12ec1","repo":"oracle/graal","slug":"wrong-usage-of-branch-probability-injection-thi","errorCode":null,"errorMessage":"Wrong usage of branch probability injection  + this","messagePattern":"Wrong usage of branch probability injection  \\+ this","errorType":"exception","errorClass":"GraalError","httpStatus":null,"severity":"error","filePath":"compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/nodes/extended/BranchProbabilityNode.java","lineNumber":239,"sourceCode":"                                    ((ConditionalNode) currentCondition).trueValue().isConstant() && ((ConditionalNode) currentCondition).falseValue().isConstant()) {\n                        for (IntegerEqualsNode eq : currentCondition.usages().filter(IntegerEqualsNode.class).snapshot()) {\n                            if (eq.getY().isConstant() || eq.getX().isConstant()) {\n                                ValueNode canonical = eq.canonical(tool);\n                                if (canonical != eq && canonical != null) {\n                                    tool.addToWorkList(eq.usages());\n                                    eq.replaceAtUsages(graph().addOrUnique(canonical));\n                                    GraphUtil.killWithUnusedFloatingInputs(eq);\n                                }\n                            }\n                        }\n                    }\n                    if (currentCondition.hasUsages()) {\n                        tool.addToWorkList(currentCondition.usages());\n                    }\n                }\n            } else {\n                if (!isSubstitutionGraph()) {\n                    throw new GraalError(\"Wrong usage of branch probability injection \" + this);\n                }\n            }\n        }\n    }\n\n    private boolean isSubstitutionGraph() {\n        return hasExactlyOneUsage() && usages().first() instanceof ReturnNode;\n    }\n\n    /**\n     * Normally a branch probability should be consumed directly as a condition, but in some cases\n     * it can be used as a value itself. For example:\n     *\n     * <pre>\n     * boolean helper() {\n     *     if (probability(a, ...) || probability(b, ...) || probability(c, condition)) {\n     *         return true;\n     *     } else {","sourceCodeStart":221,"sourceCodeEnd":257,"githubUrl":"https://github.com/oracle/graal/blob/a66e9ccd1d7bf2552883939aa0788dfd0e294aab/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/nodes/extended/BranchProbabilityNode.java#L221-L257","documentation":"After simplification, if the BranchProbabilityNode's condition was consumed but some usage remains that is not the recognized pattern, simplify() throws 'Wrong usage of branch probability injection' — unless isSubstitutionGraph() (exactly one usage which is a ReturnNode) holds, which supports returning the probability value itself from snippets. The error means the intrinsic result was consumed in a way that never feeds an if-condition, so the probability can never be injected.","triggerScenarios":"Using probability(p, cond)'s boolean result in non-condition positions: storing it in a variable, passing it to another method, combining it with && / || in ways the simplifier cannot rewrite, or using it as a value — while not matching the single-ReturnNode substitution exception.","commonSituations":"Refactoring a snippet so the intrinsic result flows through a phi or helper before reaching an if; new snippet authors assuming probability() works like a plain boolean; using it inside complex boolean expressions.","solutions":["Use the intrinsic result directly as the if condition: if (probability(p, cond)) { ... }","If you need the value returned (substitution graph), keep exactly one usage that is the ReturnNode — any other shape throws","Remove the intrinsic if you only need the boolean, not the probability"],"exampleFix":"// before\nboolean b = BranchProbabilityNode.probability(p, cond);\nmap.put(k, b);          // non-condition usage -> error\nif (map.get(k)) { ... }\n\n// after\nif (BranchProbabilityNode.probability(p, cond)) { ... }","handlingStrategy":"validation","validationCode":"// Structural check: the intrinsic result must feed an if-condition (or a single return)\n// lint rule / review check: forbid assignments or map/list insertions of probability(...) results\nboolean ok = usage instanceof IfNode condition || (singleUsage && usage instanceof ReturnNode);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always write if (probability(p, cond)) directly, with no intermediates","Do not store, pass around, or combine the intrinsic's boolean result","In snippets, the only non-condition usage allowed is returning the value itself"],"tags":["graalvm","branch-probability","intrinsic-misuse","snippets"],"backgroundTag":null,"analyzedSha":"a66e9ccd1d7bf2552883939aa0788dfd0e294aab","analyzedAt":"2026-08-14T13:58:47.161Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}