1 /*
   2  * Copyright (c) 2015, 2018, 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.test;
  26 
  27 import org.junit.Assert;
  28 import org.junit.Ignore;
  29 import org.junit.Test;
  30 
  31 import org.graalvm.compiler.api.directives.GraalDirectives;
  32 import org.graalvm.compiler.core.test.GraalCompilerTest;
  33 import org.graalvm.compiler.graph.Node;
  34 import org.graalvm.compiler.nodes.IfNode;
  35 import org.graalvm.compiler.nodes.LogicNode;
  36 import org.graalvm.compiler.nodes.StructuredGraph;
  37 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  38 import org.graalvm.compiler.nodes.calc.SubNode;
  39 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  40 import org.graalvm.compiler.loop.phases.ConvertDeoptimizeToGuardPhase;
  41 import org.graalvm.compiler.phases.common.IterativeConditionalEliminationPhase;
  42 import org.graalvm.compiler.phases.tiers.PhaseContext;
  43 
  44 /**
  45  * A few tests of expected simplifications by
  46  * {@link IfNode#simplify(org.graalvm.compiler.graph.spi.SimplifierTool)}.
  47  */
  48 public class IfNodeCanonicalizationTest extends GraalCompilerTest {
  49 
  50     static int value;
  51 
  52     static final byte[] testValues = {-128, -1, 0, 1, 127};
  53 
  54     @Test
  55     public void test1() {
  56         /*
  57          * exercise conversion of x - y < 0 into x < y, both by checking expected graph shape and
  58          * that the transformed code produces the right answer.
  59          */
  60         test("testSnippet1", SubNode.class, 0);
  61         byte[] values = new byte[4];
  62         for (byte a : testValues) {
  63             values[0] = a;
  64             for (byte b : testValues) {
  65                 values[1] = b;
  66                 for (byte c : testValues) {
  67                     values[2] = c;
  68                     for (byte d : testValues) {
  69                         values[3] = d;
  70                         value = 2;
  71                         super.test("testSnippet1", values, true);
  72                         super.test("testSnippet1", values, false);
  73                     }
  74                 }
  75             }
  76         }
  77     }
  78 
  79     public int testSnippet1(byte[] values, boolean test) {
  80         int v = values[0] - values[1];
  81         if (test) {
  82             v = values[2] - values[3];
  83         }
  84         if (v < 0) {
  85             value = 1;
  86         }
  87         return value;
  88     }
  89 
  90     @Test
  91     public void test2() {
  92         test("testSnippet2", 1);
  93     }
  94 
  95     public boolean testSnippet2(int a, int[] limit) {
  96         int l = limit.length;
  97         if (!(a >= 0 && a < l)) {
  98             value = a;
  99             return true;
 100         }
 101         return false;
 102     }
 103 
 104     @Ignore("currently not working because swapped case isn't handled")
 105     @Test
 106     public void test3() {
 107         test("testSnippet3", 1);
 108     }
 109 
 110     public boolean testSnippet3(int a, int[] limit) {
 111         int l = limit.length;
 112         if (a < l && a >= 0) {
 113             value = 9;
 114             return true;
 115         }
 116         return false;
 117     }
 118 
 119     @Test
 120     public void test4() {
 121         test("testSnippet4", 1);
 122     }
 123 
 124     public boolean testSnippet4(int a, int[] limit) {
 125         int l = limit.length;
 126         if (a < 0) {
 127             GraalDirectives.deoptimize();
 128         }
 129         if (a >= l) {
 130             GraalDirectives.deoptimize();
 131         }
 132         return true;
 133     }
 134 
 135     @Ignore("Reversed conditions aren't working with guards")
 136     @Test
 137     public void test5() {
 138         test("testSnippet5", 1);
 139     }
 140 
 141     public boolean testSnippet5(int a, int[] limit) {
 142         int l = limit.length;
 143         if (a >= l) {
 144             GraalDirectives.deoptimize();
 145         }
 146         if (a < 0) {
 147             GraalDirectives.deoptimize();
 148         }
 149         return true;
 150     }
 151 
 152     public void test(String name, int logicCount) {
 153         test(name, LogicNode.class, logicCount);
 154     }
 155 
 156     public void test(String name, Class<? extends Node> expectedClass, int expectedCount) {
 157         StructuredGraph graph = parseEager(name, AllowAssumptions.YES);
 158 
 159         PhaseContext context = new PhaseContext(getProviders());
 160         CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
 161         new ConvertDeoptimizeToGuardPhase().apply(graph, context);
 162         graph.clearAllStateAfter();
 163         graph.setGuardsStage(StructuredGraph.GuardsStage.AFTER_FSA);
 164         canonicalizer.apply(graph, context);
 165 
 166         // new DominatorConditionalEliminationPhase(true).apply(graph, context);
 167         new IterativeConditionalEliminationPhase(canonicalizer, true).apply(graph, context);
 168         canonicalizer.apply(graph, context);
 169         canonicalizer.apply(graph, context);
 170 
 171         Assert.assertEquals(expectedCount, graph.getNodes().filter(expectedClass).count());
 172     }
 173 }