From b6ea2df851d9b34511a1b80d2abbbc91e1fef50f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerg=C3=B6=20Barany?= Date: Mon, 13 Jan 2025 10:22:13 +0100 Subject: [PATCH] Don't allow IntegerExactOverflowNode as condition in ConditionalNode --- .../src/jdk/graal/compiler/nodes/IfNode.java | 10 +++++++++- .../arithmetic/IntegerExactOverflowNode.java | 20 ++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/nodes/IfNode.java b/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/nodes/IfNode.java index 5e4130821823..cc3eab7ed2b0 100644 --- a/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/nodes/IfNode.java +++ b/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/nodes/IfNode.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2009, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -84,6 +84,7 @@ import jdk.graal.compiler.nodes.spi.SimplifierTool; import jdk.graal.compiler.nodes.spi.SwitchFoldable; import jdk.graal.compiler.nodes.util.GraphUtil; +import jdk.graal.compiler.replacements.nodes.arithmetic.IntegerExactOverflowNode; import jdk.vm.ci.code.CodeUtil; import jdk.vm.ci.meta.Constant; import jdk.vm.ci.meta.JavaConstant; @@ -1706,6 +1707,13 @@ private ValueNode canonicalizeConditionalCascade(SimplifierTool tool, ValueNode if (trueValue.getStackKind() != JavaKind.Int && trueValue.getStackKind() != JavaKind.Long) { return null; } + if (condition() instanceof IntegerExactOverflowNode) { + /* + * An exact overflow node is tightly coupled to the if node that uses it. It must not be + * used as the condition in a conditional node. + */ + return null; + } if (isSafeConditionalInput(trueValue, trueSuccessor) && isSafeConditionalInput(falseValue, falseSuccessor)) { return graph().unique(new ConditionalNode(condition(), trueValue, falseValue)); } diff --git a/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/replacements/nodes/arithmetic/IntegerExactOverflowNode.java b/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/replacements/nodes/arithmetic/IntegerExactOverflowNode.java index d94a8a51121c..0b8201d77f94 100644 --- a/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/replacements/nodes/arithmetic/IntegerExactOverflowNode.java +++ b/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/replacements/nodes/arithmetic/IntegerExactOverflowNode.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -31,9 +31,12 @@ import java.util.List; import jdk.graal.compiler.core.common.type.Stamp; +import jdk.graal.compiler.graph.Node; import jdk.graal.compiler.graph.NodeClass; import jdk.graal.compiler.nodeinfo.NodeInfo; import jdk.graal.compiler.nodes.AbstractBeginNode; +import jdk.graal.compiler.nodes.FixedGuardNode; +import jdk.graal.compiler.nodes.GuardNode; import jdk.graal.compiler.nodes.IfNode; import jdk.graal.compiler.nodes.LogicNode; import jdk.graal.compiler.nodes.NodeView; @@ -65,6 +68,14 @@ public ValueNode getY() { return y; } + @Override + protected boolean verifyNode() { + for (Node usage : usages()) { + assertTrue(usage instanceof GuardNode || usage instanceof FixedGuardNode || usage instanceof IfNode, "exact overflow node must only be used by guards or ifs, found: %s", usage); + } + return super.verifyNode(); + } + /** * Make sure the overflow detection nodes have the same order of inputs as the exact arithmetic * nodes. @@ -119,5 +130,12 @@ public void simplify(SimplifierTool tool) { coupledNodes.forEach(n -> n.replaceAndDelete(split)); } + if (hasNoUsages()) { + /* + * We don't want GVN during canonicalization to find this node and give it usages again, + * since the canonicalizer would not call this simplify method again. + */ + safeDelete(); + } } }