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