{"record":{"id":"d506e5334e4b6a09","repo":"oracle/graal","slug":"a-negative-probability-of-probabilityvalue-i","errorCode":null,"errorMessage":"A negative probability of  + probabilityValue +  is not allowed!","messagePattern":"A negative probability of  \\+ probabilityValue \\+  is not allowed!","errorType":"exception","errorClass":"GraalError","httpStatus":null,"severity":"error","filePath":"compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/nodes/extended/BranchProbabilityNode.java","lineNumber":154,"sourceCode":"\n    @Override\n    public Node canonical(CanonicalizerTool tool) {\n        if (condition.isConstant()) {\n            // fold constant conditions early during PE\n            return condition;\n        }\n        return this;\n    }\n\n    @Override\n    public void simplify(SimplifierTool tool) {\n        if (!hasUsages()) {\n            return;\n        }\n        if (probability.isConstant()) {\n            double probabilityValue = probability.asJavaConstant().asDouble();\n            if (probabilityValue < 0.0) {\n                throw new GraalError(\"A negative probability of \" + probabilityValue + \" is not allowed!\");\n            } else if (probabilityValue > 1.0) {\n                throw new GraalError(\"A probability of more than 1.0 (\" + probabilityValue + \") is not allowed!\");\n            } else if (Double.isNaN(probabilityValue)) {\n                /*\n                 * We allow NaN if the node is in unreachable code that will eventually fall away,\n                 * or else an error will be thrown during lowering since we keep the node around.\n                 */\n                return;\n            }\n            boolean usageFound = false;\n            for (IntegerEqualsNode node : this.usages().filter(IntegerEqualsNode.class)) {\n                assert node.condition() == CanonicalCondition.EQ : Assertions.errorMessage(node, node.condition());\n                ValueNode other = node.getX();\n                if (node.getX() == this) {\n                    other = node.getY();\n                }\n                if (other.isConstant()) {\n                    double probabilityToSet = probabilityValue;","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/oracle/graal/blob/a66e9ccd1d7bf2552883939aa0788dfd0e294aab/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/nodes/extended/BranchProbabilityNode.java#L136-L172","documentation":"BranchProbabilityNode.simplify validates the injected probability constant: values must be within [0.0, 1.0]. A negative constant throws GraalError with the offending value inline. The node comes from the branch probability intrinsic (com.oracle.graal.nodes.extended.BranchProbabilityNode.probability(double, boolean) / ProbabilityNode style intrinsics), so the error pinpoints an invalid literal in source or snippet code.","triggerScenarios":"Calling the branch-probability intrinsic with a negative constant that survives to a constant node: probability(-0.1, cond), or a snippet/substitution parameter constant-folding to a negative value.","commonSituations":"Copy-pasted probability values; passing NOT_FREQUENT_PROBABILITY-style constants through arithmetic that can go negative; refactoring prob constants into computations with a sign bug.","solutions":["Fix the probability value to be within [0.0, 1.0]","Use the provided constants (BranchProbabilityNode.DELTA_ON_TAKEN / LIKELY_PROBABILITY / NOT_FREQUENT_PROBABILITY, etc.) instead of hand-written numbers","If the value flows from configuration, validate/clamp it before it reaches the intrinsic"],"exampleFix":"// before\nBranchProbabilityNode.probability(-0.1, cond)\n\n// after\nBranchProbabilityNode.probability(BranchProbabilityNode.NOT_FREQUENT_PROBABILITY, cond)","handlingStrategy":"validation","validationCode":"static double checkProbability(double p) {\n    if (p < 0.0 || p > 1.0 || Double.isNaN(p)) throw new IllegalArgumentException(\"probability out of range: \" + p);\n    return p;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use the library's probability constants instead of literals","Validate probability inputs where they are configurable","Remember semantics: probability in [0,1], not a weight"],"tags":["graalvm","branch-probability","intrinsic-misuse","validation"],"backgroundTag":null,"analyzedSha":"a66e9ccd1d7bf2552883939aa0788dfd0e294aab","analyzedAt":"2026-08-14T13:58:47.161Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}