src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.test/src/org/graalvm/compiler/core/test/IfCanonicalizerTest.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File hotspot Sdiff src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.test/src/org/graalvm/compiler/core/test

src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.test/src/org/graalvm/compiler/core/test/IfCanonicalizerTest.java

Print this page




   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.core.test;
  24 
  25 import org.junit.Test;
  26 
  27 import org.graalvm.compiler.debug.Debug;
  28 import org.graalvm.compiler.graph.Node;
  29 import org.graalvm.compiler.nodes.ConstantNode;
  30 import org.graalvm.compiler.nodes.FrameState;
  31 import org.graalvm.compiler.nodes.IfNode;
  32 import org.graalvm.compiler.nodes.ParameterNode;
  33 import org.graalvm.compiler.nodes.StructuredGraph;
  34 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  35 import org.graalvm.compiler.nodes.spi.LoweringTool;
  36 import org.graalvm.compiler.phases.OptimisticOptimizations;
  37 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  38 import org.graalvm.compiler.phases.common.FloatingReadPhase;
  39 import org.graalvm.compiler.phases.common.GuardLoweringPhase;
  40 import org.graalvm.compiler.phases.common.LoweringPhase;
  41 import org.graalvm.compiler.phases.tiers.MidTierContext;
  42 import org.graalvm.compiler.phases.tiers.PhaseContext;

  43 
  44 /**
  45  * In the following tests, the usages of local variable "a" are replaced with the integer constant
  46  * 0. Then canonicalization is applied and it is verified that the resulting graph is equal to the
  47  * graph of the method that just has a "return 1" statement in it.
  48  */
  49 public class IfCanonicalizerTest extends GraalCompilerTest {
  50 
  51     private static final String REFERENCE_SNIPPET = "referenceSnippet";
  52 
  53     @SuppressWarnings("all")
  54     public static int referenceSnippet(int a) {
  55         return 1;
  56     }
  57 
  58     @Test
  59     public void test1() {
  60         test("test1Snippet");
  61     }
  62 


 220     }
 221 
 222     private static Long normalizeCompareLong(int x, int y) {
 223         return (x < y) ? -1L : ((x == y) ? 0L : 1L);
 224     }
 225 
 226     private void testCombinedIf(String snippet, int count) {
 227         StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES);
 228         PhaseContext context = new PhaseContext(getProviders());
 229         new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
 230         new FloatingReadPhase().apply(graph);
 231         MidTierContext midContext = new MidTierContext(getProviders(), getTargetProvider(), OptimisticOptimizations.ALL, graph.getProfilingInfo());
 232         new GuardLoweringPhase().apply(graph, midContext);
 233         new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.MID_TIER).apply(graph, midContext);
 234         new CanonicalizerPhase().apply(graph, context);
 235         assertDeepEquals(count, graph.getNodes().filter(IfNode.class).count());
 236     }
 237 
 238     private void test(String snippet) {
 239         StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES);

 240         ParameterNode param = graph.getNodes(ParameterNode.TYPE).iterator().next();
 241         ConstantNode constant = ConstantNode.forInt(0, graph);
 242         for (Node n : param.usages().snapshot()) {
 243             if (!(n instanceof FrameState)) {
 244                 n.replaceFirstInput(param, constant);
 245             }
 246         }
 247         Debug.dump(Debug.BASIC_LEVEL, graph, "Graph");
 248         new CanonicalizerPhase().apply(graph, new PhaseContext(getProviders()));
 249         for (FrameState fs : param.usages().filter(FrameState.class).snapshot()) {
 250             fs.replaceFirstInput(param, null);
 251             param.safeDelete();
 252         }
 253         StructuredGraph referenceGraph = parseEager(REFERENCE_SNIPPET, AllowAssumptions.YES);
 254         assertEquals(referenceGraph, graph);
 255     }
 256 }


   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.core.test;
  24 
  25 import org.graalvm.compiler.debug.DebugContext;


  26 import org.graalvm.compiler.graph.Node;
  27 import org.graalvm.compiler.nodes.ConstantNode;
  28 import org.graalvm.compiler.nodes.FrameState;
  29 import org.graalvm.compiler.nodes.IfNode;
  30 import org.graalvm.compiler.nodes.ParameterNode;
  31 import org.graalvm.compiler.nodes.StructuredGraph;
  32 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  33 import org.graalvm.compiler.nodes.spi.LoweringTool;
  34 import org.graalvm.compiler.phases.OptimisticOptimizations;
  35 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  36 import org.graalvm.compiler.phases.common.FloatingReadPhase;
  37 import org.graalvm.compiler.phases.common.GuardLoweringPhase;
  38 import org.graalvm.compiler.phases.common.LoweringPhase;
  39 import org.graalvm.compiler.phases.tiers.MidTierContext;
  40 import org.graalvm.compiler.phases.tiers.PhaseContext;
  41 import org.junit.Test;
  42 
  43 /**
  44  * In the following tests, the usages of local variable "a" are replaced with the integer constant
  45  * 0. Then canonicalization is applied and it is verified that the resulting graph is equal to the
  46  * graph of the method that just has a "return 1" statement in it.
  47  */
  48 public class IfCanonicalizerTest extends GraalCompilerTest {
  49 
  50     private static final String REFERENCE_SNIPPET = "referenceSnippet";
  51 
  52     @SuppressWarnings("all")
  53     public static int referenceSnippet(int a) {
  54         return 1;
  55     }
  56 
  57     @Test
  58     public void test1() {
  59         test("test1Snippet");
  60     }
  61 


 219     }
 220 
 221     private static Long normalizeCompareLong(int x, int y) {
 222         return (x < y) ? -1L : ((x == y) ? 0L : 1L);
 223     }
 224 
 225     private void testCombinedIf(String snippet, int count) {
 226         StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES);
 227         PhaseContext context = new PhaseContext(getProviders());
 228         new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
 229         new FloatingReadPhase().apply(graph);
 230         MidTierContext midContext = new MidTierContext(getProviders(), getTargetProvider(), OptimisticOptimizations.ALL, graph.getProfilingInfo());
 231         new GuardLoweringPhase().apply(graph, midContext);
 232         new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.MID_TIER).apply(graph, midContext);
 233         new CanonicalizerPhase().apply(graph, context);
 234         assertDeepEquals(count, graph.getNodes().filter(IfNode.class).count());
 235     }
 236 
 237     private void test(String snippet) {
 238         StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES);
 239         DebugContext debug = graph.getDebug();
 240         ParameterNode param = graph.getNodes(ParameterNode.TYPE).iterator().next();
 241         ConstantNode constant = ConstantNode.forInt(0, graph);
 242         for (Node n : param.usages().snapshot()) {
 243             if (!(n instanceof FrameState)) {
 244                 n.replaceFirstInput(param, constant);
 245             }
 246         }
 247         debug.dump(DebugContext.BASIC_LEVEL, graph, "Graph");
 248         new CanonicalizerPhase().apply(graph, new PhaseContext(getProviders()));
 249         for (FrameState fs : param.usages().filter(FrameState.class).snapshot()) {
 250             fs.replaceFirstInput(param, null);
 251             param.safeDelete();
 252         }
 253         StructuredGraph referenceGraph = parseEager(REFERENCE_SNIPPET, AllowAssumptions.YES);
 254         assertEquals(referenceGraph, graph);
 255     }
 256 }
src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.test/src/org/graalvm/compiler/core/test/IfCanonicalizerTest.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File