1 /*
   2  * Copyright (c) 2015, 2019, 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 java.util.List;
  28 
  29 import org.graalvm.compiler.core.common.cfg.BlockMap;
  30 import org.graalvm.compiler.debug.DebugContext;
  31 import org.graalvm.compiler.graph.Node;
  32 import org.graalvm.compiler.graph.NodeMap;
  33 import org.graalvm.compiler.nodes.BeginNode;
  34 import org.graalvm.compiler.nodes.DeoptimizingNode.DeoptDuring;
  35 import org.graalvm.compiler.nodes.FrameState;
  36 import org.graalvm.compiler.nodes.ReturnNode;
  37 import org.graalvm.compiler.nodes.StateSplit;
  38 import org.graalvm.compiler.nodes.StructuredGraph;
  39 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  40 import org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult;
  41 import org.graalvm.compiler.nodes.calc.AddNode;
  42 import org.graalvm.compiler.nodes.calc.BinaryArithmeticNode;
  43 import org.graalvm.compiler.nodes.cfg.Block;
  44 import org.graalvm.compiler.nodes.spi.CoreProviders;
  45 import org.graalvm.compiler.nodes.spi.LoweringTool;
  46 import org.graalvm.compiler.phases.OptimisticOptimizations;
  47 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  48 import org.graalvm.compiler.phases.common.FrameStateAssignmentPhase;
  49 import org.graalvm.compiler.phases.common.GuardLoweringPhase;
  50 import org.graalvm.compiler.phases.common.LoweringPhase;
  51 import org.graalvm.compiler.phases.schedule.SchedulePhase;
  52 import org.graalvm.compiler.phases.schedule.SchedulePhase.SchedulingStrategy;
  53 import org.graalvm.compiler.phases.tiers.MidTierContext;
  54 import org.junit.Test;
  55 
  56 public class SchedulingTest2 extends GraphScheduleTest {
  57 
  58     public static int testSnippet() {
  59         return test() + 2;
  60     }
  61 
  62     public static int test() {
  63         return 40;
  64     }
  65 
  66     @Test
  67     public void testValueProxyInputs() {
  68         StructuredGraph graph = parseEager("testSnippet", AllowAssumptions.YES);
  69         DebugContext debug = graph.getDebug();
  70         ReturnNode returnNode = graph.getNodes(ReturnNode.TYPE).first();
  71         BeginNode beginNode = graph.add(new BeginNode());
  72         returnNode.replaceAtPredecessor(beginNode);
  73         beginNode.setNext(returnNode);
  74         debug.dump(DebugContext.BASIC_LEVEL, graph, "Graph");
  75         SchedulePhase schedulePhase = new SchedulePhase(SchedulingStrategy.EARLIEST_WITH_GUARD_ORDER);
  76         schedulePhase.apply(graph);
  77         ScheduleResult schedule = graph.getLastSchedule();
  78         BlockMap<List<Node>> blockToNodesMap = schedule.getBlockToNodesMap();
  79         NodeMap<Block> nodeToBlock = schedule.getNodeToBlockMap();
  80         assertDeepEquals(2, schedule.getCFG().getBlocks().length);
  81         for (BinaryArithmeticNode<?> node : graph.getNodes().filter(BinaryArithmeticNode.class)) {
  82             if (node instanceof AddNode) {
  83                 assertTrue(node.toString() + " expected: " + nodeToBlock.get(beginNode) + " but was: " + nodeToBlock.get(node), nodeToBlock.get(node) != nodeToBlock.get(beginNode));
  84             }
  85         }
  86 
  87         for (FrameState fs : graph.getNodes(FrameState.TYPE)) {
  88             Block block = nodeToBlock.get(fs);
  89             assertTrue(fs.toString(), block == schedule.getCFG().getStartBlock());
  90             for (Node usage : fs.usages()) {
  91                 if (usage instanceof StateSplit && ((StateSplit) usage).stateAfter() == fs) {
  92                     assertTrue(usage.toString(), nodeToBlock.get(usage) == block);
  93                     if (usage != block.getBeginNode()) {
  94                         List<Node> map = blockToNodesMap.get(block);
  95                         assertTrue(map.indexOf(fs) + " < " + map.indexOf(usage), map.indexOf(fs) < map.indexOf(usage));
  96                     }
  97                 }
  98             }
  99         }
 100 
 101         CoreProviders context = getProviders();
 102         new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
 103         new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.MID_TIER).apply(graph, context);
 104         MidTierContext midContext = new MidTierContext(getProviders(), getTargetProvider(), OptimisticOptimizations.ALL, graph.getProfilingInfo());
 105 
 106         new GuardLoweringPhase().apply(graph, midContext);
 107         FrameStateAssignmentPhase phase = new FrameStateAssignmentPhase();
 108         phase.apply(graph);
 109 
 110         schedulePhase.apply(graph);
 111         schedule = graph.getLastSchedule();
 112         blockToNodesMap = schedule.getBlockToNodesMap();
 113         nodeToBlock = schedule.getNodeToBlockMap();
 114         for (FrameState fs : graph.getNodes(FrameState.TYPE)) {
 115             Block block = nodeToBlock.get(fs);
 116             assertTrue(fs.toString(), block == schedule.getCFG().getStartBlock());
 117             for (Node usage : fs.usages()) {
 118                 if ((usage instanceof StateSplit && ((StateSplit) usage).stateAfter() == fs) || (usage instanceof DeoptDuring && ((DeoptDuring) usage).stateDuring() == fs)) {
 119                     assertTrue(usage.toString(), nodeToBlock.get(usage) == block);
 120                     if (usage != block.getBeginNode()) {
 121                         List<Node> map = blockToNodesMap.get(block);
 122                         assertTrue(map.indexOf(fs) + " < " + map.indexOf(usage), map.indexOf(fs) < map.indexOf(usage));
 123                     }
 124                 }
 125             }
 126         }
 127     }
 128 }