7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24
25 package org.graalvm.compiler.phases.common;
26
27 import org.graalvm.compiler.core.common.type.FloatStamp;
28 import org.graalvm.compiler.core.common.type.Stamp;
29 import org.graalvm.compiler.debug.DebugCloseable;
30 import org.graalvm.compiler.debug.GraalError;
31 import org.graalvm.compiler.graph.Graph;
32 import org.graalvm.compiler.graph.Node;
33 import org.graalvm.compiler.nodes.AbstractBeginNode;
34 import org.graalvm.compiler.nodes.AbstractMergeNode;
35 import org.graalvm.compiler.nodes.BeginNode;
36 import org.graalvm.compiler.nodes.ConstantNode;
37 import org.graalvm.compiler.nodes.EndNode;
38 import org.graalvm.compiler.nodes.IfNode;
39 import org.graalvm.compiler.nodes.LogicNode;
40 import org.graalvm.compiler.nodes.MergeNode;
41 import org.graalvm.compiler.nodes.NodeView;
42 import org.graalvm.compiler.nodes.ShortCircuitOrNode;
43 import org.graalvm.compiler.nodes.StructuredGraph;
44 import org.graalvm.compiler.nodes.ValueNode;
45 import org.graalvm.compiler.nodes.calc.ConditionalNode;
46 import org.graalvm.compiler.nodes.calc.FloatEqualsNode;
47 import org.graalvm.compiler.nodes.calc.FloatLessThanNode;
48 import org.graalvm.compiler.nodes.calc.IntegerEqualsNode;
49 import org.graalvm.compiler.nodes.calc.IntegerLessThanNode;
50 import org.graalvm.compiler.nodes.calc.NormalizeCompareNode;
51 import org.graalvm.compiler.phases.Phase;
52
53 public class ExpandLogicPhase extends Phase {
54 private static final double EPSILON = 1E-6;
55
56 @Override
57 @SuppressWarnings("try")
58 protected void run(StructuredGraph graph) {
59 for (ShortCircuitOrNode logic : graph.getNodes(ShortCircuitOrNode.TYPE)) {
60 processBinary(logic);
61 }
62 assert graph.getNodes(ShortCircuitOrNode.TYPE).isEmpty();
63
64 for (NormalizeCompareNode logic : graph.getNodes(NormalizeCompareNode.TYPE)) {
65 try (DebugCloseable context = logic.withNodeSourcePosition()) {
66 processNormalizeCompareNode(logic);
67 }
68 }
69 graph.setAfterExpandLogic();
70 }
71
72 private static void processNormalizeCompareNode(NormalizeCompareNode normalize) {
73 LogicNode equalComp;
74 LogicNode lessComp;
75 StructuredGraph graph = normalize.graph();
76 ValueNode x = normalize.getX();
77 ValueNode y = normalize.getY();
78 if (x.stamp(NodeView.DEFAULT) instanceof FloatStamp) {
79 equalComp = graph.addOrUniqueWithInputs(FloatEqualsNode.create(x, y, NodeView.DEFAULT));
80 lessComp = graph.addOrUniqueWithInputs(FloatLessThanNode.create(x, y, normalize.isUnorderedLess(), NodeView.DEFAULT));
81 } else {
82 equalComp = graph.addOrUniqueWithInputs(IntegerEqualsNode.create(x, y, NodeView.DEFAULT));
83 lessComp = graph.addOrUniqueWithInputs(IntegerLessThanNode.create(x, y, NodeView.DEFAULT));
84 }
85
86 Stamp stamp = normalize.stamp(NodeView.DEFAULT);
87 ConditionalNode equalValue = graph.unique(
88 new ConditionalNode(equalComp, ConstantNode.forIntegerStamp(stamp, 0, graph), ConstantNode.forIntegerStamp(stamp, 1, graph)));
89 ConditionalNode value = graph.unique(new ConditionalNode(lessComp, ConstantNode.forIntegerStamp(stamp, -1, graph), equalValue));
90 normalize.replaceAtUsagesAndDelete(value);
91 }
92
93 @SuppressWarnings("try")
94 private static void processBinary(ShortCircuitOrNode binary) {
95 while (binary.usages().isNotEmpty()) {
96 Node usage = binary.usages().first();
97 try (DebugCloseable nsp = usage.withNodeSourcePosition()) {
98 if (usage instanceof ShortCircuitOrNode) {
99 processBinary((ShortCircuitOrNode) usage);
100 } else if (usage instanceof IfNode) {
101 processIf(binary.getX(), binary.isXNegated(), binary.getY(), binary.isYNegated(), (IfNode) usage, binary.getShortCircuitProbability());
102 } else if (usage instanceof ConditionalNode) {
103 processConditional(binary.getX(), binary.isXNegated(), binary.getY(), binary.isYNegated(), (ConditionalNode) usage);
104 } else {
105 throw GraalError.shouldNotReachHere();
106 }
107 }
108 }
|
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24
25 package org.graalvm.compiler.phases.common;
26
27 import org.graalvm.compiler.core.common.type.Stamp;
28 import org.graalvm.compiler.debug.DebugCloseable;
29 import org.graalvm.compiler.debug.GraalError;
30 import org.graalvm.compiler.graph.Graph;
31 import org.graalvm.compiler.graph.Node;
32 import org.graalvm.compiler.nodes.AbstractBeginNode;
33 import org.graalvm.compiler.nodes.AbstractMergeNode;
34 import org.graalvm.compiler.nodes.BeginNode;
35 import org.graalvm.compiler.nodes.ConstantNode;
36 import org.graalvm.compiler.nodes.EndNode;
37 import org.graalvm.compiler.nodes.IfNode;
38 import org.graalvm.compiler.nodes.LogicNode;
39 import org.graalvm.compiler.nodes.MergeNode;
40 import org.graalvm.compiler.nodes.NodeView;
41 import org.graalvm.compiler.nodes.ShortCircuitOrNode;
42 import org.graalvm.compiler.nodes.StructuredGraph;
43 import org.graalvm.compiler.nodes.ValueNode;
44 import org.graalvm.compiler.nodes.calc.AbstractNormalizeCompareNode;
45 import org.graalvm.compiler.nodes.calc.ConditionalNode;
46 import org.graalvm.compiler.phases.Phase;
47
48 public class ExpandLogicPhase extends Phase {
49 private static final double EPSILON = 1E-6;
50
51 @Override
52 @SuppressWarnings("try")
53 protected void run(StructuredGraph graph) {
54 for (ShortCircuitOrNode logic : graph.getNodes(ShortCircuitOrNode.TYPE)) {
55 processBinary(logic);
56 }
57 assert graph.getNodes(ShortCircuitOrNode.TYPE).isEmpty();
58
59 for (AbstractNormalizeCompareNode logic : graph.getNodes(AbstractNormalizeCompareNode.TYPE)) {
60 try (DebugCloseable context = logic.withNodeSourcePosition()) {
61 processNormalizeCompareNode(logic);
62 }
63 }
64 graph.setAfterExpandLogic();
65 }
66
67 private static void processNormalizeCompareNode(AbstractNormalizeCompareNode normalize) {
68 StructuredGraph graph = normalize.graph();
69 LogicNode equalComp = graph.addOrUniqueWithInputs(normalize.createEqualComparison());
70 LogicNode lessComp = graph.addOrUniqueWithInputs(normalize.createLowerComparison());
71 Stamp stamp = normalize.stamp(NodeView.DEFAULT);
72 ConditionalNode equalValue = graph.unique(new ConditionalNode(equalComp, ConstantNode.forIntegerStamp(stamp, 0, graph), ConstantNode.forIntegerStamp(stamp, 1, graph)));
73 ConditionalNode value = graph.unique(new ConditionalNode(lessComp, ConstantNode.forIntegerStamp(stamp, -1, graph), equalValue));
74 normalize.replaceAtUsagesAndDelete(value);
75 }
76
77 @SuppressWarnings("try")
78 private static void processBinary(ShortCircuitOrNode binary) {
79 while (binary.usages().isNotEmpty()) {
80 Node usage = binary.usages().first();
81 try (DebugCloseable nsp = usage.withNodeSourcePosition()) {
82 if (usage instanceof ShortCircuitOrNode) {
83 processBinary((ShortCircuitOrNode) usage);
84 } else if (usage instanceof IfNode) {
85 processIf(binary.getX(), binary.isXNegated(), binary.getY(), binary.isYNegated(), (IfNode) usage, binary.getShortCircuitProbability());
86 } else if (usage instanceof ConditionalNode) {
87 processConditional(binary.getX(), binary.isXNegated(), binary.getY(), binary.isYNegated(), (ConditionalNode) usage);
88 } else {
89 throw GraalError.shouldNotReachHere();
90 }
91 }
92 }
|