1 /* 2 * Copyright (c) 2016, 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 package org.graalvm.compiler.nodes.test; 24 25 import java.util.function.Function; 26 27 import org.junit.Test; 28 29 import org.graalvm.compiler.core.test.GraalCompilerTest; 30 import org.graalvm.compiler.nodes.ConstantNode; 31 import org.graalvm.compiler.nodes.LogicNode; 32 import org.graalvm.compiler.nodes.ShortCircuitOrNode; 33 import org.graalvm.compiler.nodes.ValueNode; 34 import org.graalvm.compiler.nodes.calc.ConditionalNode; 35 import org.graalvm.compiler.nodes.calc.IntegerEqualsNode; 36 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins; 37 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext; 38 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin; 39 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration; 40 41 import jdk.vm.ci.meta.JavaKind; 42 import jdk.vm.ci.meta.ResolvedJavaMethod; 43 44 public class ShortCircuitOrNodeTest extends GraalCompilerTest { 45 static boolean shortCircuitOr(boolean b1, boolean b2) { 46 return b1 || b2; 47 } 48 49 @Override 50 protected Plugins getDefaultGraphBuilderPlugins() { 51 Plugins plugins = super.getDefaultGraphBuilderPlugins(); 52 Registration r = new Registration(plugins.getInvocationPlugins(), ShortCircuitOrNodeTest.class); 53 r.register2("shortCircuitOr", boolean.class, boolean.class, new InvocationPlugin() { 54 @Override 55 public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode b1, ValueNode b2) { 56 LogicNode x = b.add(new IntegerEqualsNode(b1, b.add(ConstantNode.forInt(1)))); 57 LogicNode y = b.add(new IntegerEqualsNode(b2, b.add(ConstantNode.forInt(1)))); 58 ShortCircuitOrNode compare = b.add(new ShortCircuitOrNode(x, false, y, false, 0.5)); 59 b.addPush(JavaKind.Boolean, new ConditionalNode(compare, b.add(ConstantNode.forBoolean(true)), b.add(ConstantNode.forBoolean(false)))); 60 return true; 61 } 62 }); 63 64 return plugins; 65 } 66 67 public static int testSharedConditionSnippet(Object o) { 68 boolean b2 = o != null; 69 boolean b1 = o instanceof Function; 70 if (b1) { 71 if (shortCircuitOr(b1, b2)) { 72 return 4; 73 } else { 74 return 3; 75 } 76 } 77 return 1; 78 } 79 80 @Test 81 public void testSharedCondition() { 82 test("testSharedConditionSnippet", "String"); 83 } 84 85 }