1 /*
   2  * Copyright (c) 2015, 2015, 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.api.directives.GraalDirectives;
  28 import org.graalvm.compiler.nodes.GuardNode;
  29 import org.graalvm.compiler.nodes.StructuredGraph;
  30 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  31 import org.graalvm.compiler.nodes.spi.LoweringTool;
  32 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  33 import org.graalvm.compiler.phases.common.LoweringPhase;
  34 import org.graalvm.compiler.phases.common.ConditionalEliminationPhase;
  35 import org.graalvm.compiler.phases.tiers.PhaseContext;
  36 import org.junit.Assert;
  37 import org.junit.Test;
  38 
  39 /**
  40  * This test checks the combined action of
  41  * {@link org.graalvm.compiler.phases.common.ConditionalEliminationPhase} and
  42  * {@link org.graalvm.compiler.phases.common.LoweringPhase}. The lowering phase needs to introduce
  43  * the null checks at the correct places for the dominator conditional elimination phase to pick
  44  * them up.
  45  */
  46 public class ConditionalEliminationTest10 extends ConditionalEliminationTestBase {
  47 
  48     private static boolean condition1;
  49     private static boolean condition2;
  50 
  51     private static class TestClass {
  52         int x;
  53     }
  54 
  55     @SuppressWarnings("all")
  56     public static int testSnippet1(TestClass t) {
  57         int result = 0;
  58         if (condition1) {
  59             GraalDirectives.controlFlowAnchor();
  60             result = t.x;
  61         }
  62         GraalDirectives.controlFlowAnchor();
  63         return result + t.x;
  64     }
  65 
  66     @Test
  67     public void test1() {
  68         test("testSnippet1", 1);
  69     }
  70 
  71     @SuppressWarnings("all")
  72     public static int testSnippet2(TestClass t) {
  73         int result = 0;
  74         if (condition1) {
  75             GraalDirectives.controlFlowAnchor();
  76             result = t.x;
  77         } else {
  78             GraalDirectives.controlFlowAnchor();
  79             result = t.x;
  80         }
  81 
  82         if (condition2) {
  83             result = t.x;
  84             GraalDirectives.controlFlowAnchor();
  85         }
  86 
  87         return result;
  88     }
  89 
  90     @Test
  91     public void test2() {
  92         test("testSnippet2", 1);
  93     }
  94 
  95     private void test(String snippet, int guardCount) {
  96         StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES);
  97         PhaseContext context = new PhaseContext(getProviders());
  98         new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
  99         new ConditionalEliminationPhase(true).apply(graph, context);
 100         Assert.assertEquals(guardCount, graph.getNodes().filter(GuardNode.class).count());
 101     }
 102 }