1 /* 2 * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 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.nodes.extended; 26 27 import static org.graalvm.compiler.nodeinfo.InputType.Guard; 28 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_0; 29 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_0; 30 31 import org.graalvm.compiler.core.common.type.StampFactory; 32 import org.graalvm.compiler.graph.Node; 33 import org.graalvm.compiler.graph.NodeClass; 34 import org.graalvm.compiler.graph.NodeInputList; 35 import org.graalvm.compiler.graph.spi.Canonicalizable; 36 import org.graalvm.compiler.graph.spi.CanonicalizerTool; 37 import org.graalvm.compiler.graph.spi.Simplifiable; 38 import org.graalvm.compiler.graph.spi.SimplifierTool; 39 import org.graalvm.compiler.nodeinfo.NodeInfo; 40 import org.graalvm.compiler.nodes.StructuredGraph; 41 import org.graalvm.compiler.nodes.ValueNode; 42 import org.graalvm.compiler.nodes.calc.FloatingNode; 43 import org.graalvm.compiler.nodes.spi.LIRLowerable; 44 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool; 45 import org.graalvm.compiler.nodes.util.GraphUtil; 46 47 @NodeInfo(allowedUsageTypes = Guard, cycles = CYCLES_0, size = SIZE_0) 48 public final class MultiGuardNode extends FloatingNode implements GuardingNode, LIRLowerable, Simplifiable, Canonicalizable, Node.ValueNumberable { 49 public static final NodeClass<MultiGuardNode> TYPE = NodeClass.create(MultiGuardNode.class); 50 51 @OptionalInput(Guard) NodeInputList<ValueNode> guards; 52 53 public MultiGuardNode(ValueNode... guards) { 54 super(TYPE, StampFactory.forVoid()); 55 this.guards = new NodeInputList<>(this, guards); 56 } 57 58 @Override 59 public void generate(NodeLIRBuilderTool generator) { 60 } 61 62 @Override 63 public Node canonical(CanonicalizerTool tool) { 64 // Make sure there are no nulls remaining in the set of guards references. 65 guards.trim(); 66 if (guards.size() == 0) { 67 // No guards left => can delete the multi-guard. 68 return null; 69 } else if (guards.size() == 1) { 70 // Only a single guard left => replace multi-guard with that single guard. 71 return guards.get(0); 72 } 73 return this; 74 } 75 76 @Override 77 public void simplify(SimplifierTool tool) { 78 if (usages().filter(node -> node instanceof ValueAnchorNode).isNotEmpty()) { 79 /* 80 * For ValueAnchorNode usages, we can optimize MultiGuardNodes away if they depend on 81 * zero or one floating nodes (as opposed to fixed nodes). 82 */ 83 Node singleFloatingGuard = null; 84 for (ValueNode guard : guards) { 85 if (GraphUtil.isFloatingNode(guard)) { 86 if (singleFloatingGuard == null) { 87 singleFloatingGuard = guard; 88 } else if (singleFloatingGuard != guard) { 89 return; 90 } 91 } 92 } 93 for (Node usage : usages().snapshot()) { 94 if (usage instanceof ValueAnchorNode) { 95 usage.replaceFirstInput(this, singleFloatingGuard); 96 tool.addToWorkList(usage); 97 } 98 } 99 if (usages().isEmpty()) { 100 GraphUtil.killWithUnusedFloatingInputs(this); 101 } 102 } 103 } 104 105 public void addGuard(GuardingNode g) { 106 this.guards.add(g.asNode()); 107 } 108 109 public static GuardingNode combine(GuardingNode first, GuardingNode second) { 110 if (first == null) { 111 return second; 112 } else if (second == null) { 113 return first; 114 } else { 115 StructuredGraph graph = first.asNode().graph(); 116 return graph.unique(new MultiGuardNode(first.asNode(), second.asNode())); 117 } 118 } 119 120 public static GuardingNode addGuard(GuardingNode first, GuardingNode second) { 121 if (first instanceof MultiGuardNode && second != null) { 122 MultiGuardNode multi = (MultiGuardNode) first; 123 multi.addGuard(second); 124 return multi; 125 } else { 126 return combine(first, second); 127 } 128 } 129 }