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 package org.graalvm.compiler.core.test;
  24 
  25 import org.graalvm.compiler.debug.Debug;
  26 import org.graalvm.compiler.nodes.ProxyNode;
  27 import org.graalvm.compiler.nodes.StructuredGraph;
  28 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  29 import org.graalvm.compiler.nodes.spi.LoweringTool;
  30 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  31 import org.graalvm.compiler.phases.common.ConvertDeoptimizeToGuardPhase;
  32 import org.graalvm.compiler.phases.common.DominatorConditionalEliminationPhase;
  33 import org.graalvm.compiler.phases.common.IterativeConditionalEliminationPhase;
  34 import org.graalvm.compiler.phases.common.LoweringPhase;
  35 import org.graalvm.compiler.phases.schedule.SchedulePhase;
  36 import org.graalvm.compiler.phases.tiers.PhaseContext;
  37 import org.junit.Assert;
  38 
  39 /**
  40  * Collection of tests for
  41  * {@link org.graalvm.compiler.phases.common.DominatorConditionalEliminationPhase} including those
  42  * that triggered bugs in this phase.
  43  */
  44 public class ConditionalEliminationTestBase extends GraalCompilerTest {
  45     private final boolean disableSimplification;
  46 
  47     protected ConditionalEliminationTestBase() {
  48         this(true);
  49     }
  50 
  51     protected ConditionalEliminationTestBase(boolean disableSimplification) {
  52         this.disableSimplification = disableSimplification;
  53     }
  54 
  55     protected void testConditionalElimination(String snippet, String referenceSnippet) {
  56         testConditionalElimination(snippet, referenceSnippet, false, false);
  57     }
  58 
  59     @SuppressWarnings("try")
  60     protected void testConditionalElimination(String snippet, String referenceSnippet, boolean applyConditionalEliminationOnReference, boolean applyLowering) {
  61         StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES);
  62         Debug.dump(Debug.BASIC_LOG_LEVEL, graph, "Graph");
  63         PhaseContext context = new PhaseContext(getProviders());
  64         CanonicalizerPhase canonicalizer1 = new CanonicalizerPhase();
  65         if (disableSimplification) {
  66             /**
  67              * Some tests break if simplification is done so only do it when needed.
  68              */
  69             canonicalizer1.disableSimplification();
  70         }
  71         CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
  72         try (Debug.Scope scope = Debug.scope("ConditionalEliminationTest", graph)) {
  73             prepareGraph(graph, canonicalizer1, context, applyLowering);
  74             new IterativeConditionalEliminationPhase(canonicalizer, true).apply(graph, context);
  75             canonicalizer.apply(graph, context);
  76             canonicalizer.apply(graph, context);
  77             new ConvertDeoptimizeToGuardPhase().apply(graph, context);
  78         } catch (Throwable t) {
  79             Debug.handle(t);
  80         }
  81         StructuredGraph referenceGraph = parseEager(referenceSnippet, AllowAssumptions.YES);
  82         try (Debug.Scope scope = Debug.scope("ConditionalEliminationTest.ReferenceGraph", referenceGraph)) {
  83             prepareGraph(referenceGraph, canonicalizer, context, applyLowering);
  84             if (applyConditionalEliminationOnReference) {
  85                 DominatorConditionalEliminationPhase.create(true).apply(referenceGraph, context);
  86             }
  87             canonicalizer.apply(referenceGraph, context);
  88             canonicalizer.apply(referenceGraph, context);
  89             new ConvertDeoptimizeToGuardPhase().apply(graph, context);
  90         } catch (Throwable t) {
  91             Debug.handle(t);
  92         }
  93         assertEquals(referenceGraph, graph);
  94     }
  95 
  96     protected void prepareGraph(StructuredGraph graph, CanonicalizerPhase canonicalizer, PhaseContext context, boolean applyLowering) {
  97         if (applyLowering) {
  98             new ConvertDeoptimizeToGuardPhase().apply(graph, context);
  99             new LoweringPhase(canonicalizer, LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
 100             canonicalizer.apply(graph, context);
 101         }
 102         canonicalizer.apply(graph, context);
 103         new ConvertDeoptimizeToGuardPhase().apply(graph, context);
 104     }
 105 
 106     public void testProxies(String snippet, int expectedProxiesCreated) {
 107         StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES);
 108         PhaseContext context = new PhaseContext(getProviders());
 109         CanonicalizerPhase canonicalizer1 = new CanonicalizerPhase();
 110         canonicalizer1.disableSimplification();
 111         canonicalizer1.apply(graph, context);
 112         CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
 113         new LoweringPhase(canonicalizer, LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
 114         canonicalizer.apply(graph, context);
 115 
 116         int baseProxyCount = graph.getNodes().filter(ProxyNode.class).count();
 117         DominatorConditionalEliminationPhase.create(true).apply(graph, context);
 118         canonicalizer.apply(graph, context);
 119         new SchedulePhase(graph.getOptions()).apply(graph, context);
 120         int actualProxiesCreated = graph.getNodes().filter(ProxyNode.class).count() - baseProxyCount;
 121         Assert.assertEquals(expectedProxiesCreated, actualProxiesCreated);
 122     }
 123 }