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.core.test;
  24 
  25 import org.junit.Test;
  26 
  27 import org.graalvm.compiler.nodes.StructuredGraph;
  28 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  29 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  30 import org.graalvm.compiler.phases.common.DominatorConditionalEliminationPhase;
  31 import org.graalvm.compiler.phases.tiers.HighTierContext;
  32 
  33 public class ConditionalEliminationMulTest extends GraalCompilerTest {
  34 
  35     public static void snippet01(int a) {
  36         if (a == 2) {
  37             if (a * 3 != 6) {
  38                 shouldBeOptimizedAway();
  39             }
  40         }
  41     }
  42 
  43     public static void snippet02(int a) {
  44         if (a == 0) {
  45             if (a * 3 != 0) {
  46                 shouldBeOptimizedAway();
  47             }
  48         }
  49     }
  50 
  51     public static void snippet03(int a) {
  52         if (a * 0 == 6) {
  53             shouldBeOptimizedAway();
  54         }
  55     }
  56 
  57     @Test
  58     public void testConditionalEliminated01() {
  59         assertOptimized("snippet01");
  60     }
  61 
  62     @Test
  63     public void testConditionalEliminated02() {
  64         assertOptimized("snippet02");
  65     }
  66 
  67     @Test
  68     public void testConditionalEliminated03() {
  69         assertOptimized("snippet03");
  70     }
  71 
  72     private void assertOptimized(String snippet) {
  73         assertOptimizedAway(prepareGraph(snippet));
  74     }
  75 
  76     private StructuredGraph prepareGraph(String snippet) {
  77         StructuredGraph graph = parseEager(snippet, AllowAssumptions.NO);
  78         HighTierContext context = getDefaultHighTierContext();
  79         CanonicalizerPhase c = new CanonicalizerPhase();
  80         c.apply(graph, context);
  81         new DominatorConditionalEliminationPhase(false).apply(graph, context);
  82         c.apply(graph, context);
  83         return graph;
  84     }
  85 
  86 }