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