1 /*
   2  * Copyright (c) 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 java.util.List;
  28 
  29 import org.junit.Test;
  30 
  31 import org.graalvm.compiler.graph.Node;
  32 import org.graalvm.compiler.graph.NodeMap;
  33 import org.graalvm.compiler.nodes.FrameState;
  34 import org.graalvm.compiler.nodes.LoopExitNode;
  35 import org.graalvm.compiler.nodes.StructuredGraph;
  36 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  37 import org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult;
  38 import org.graalvm.compiler.nodes.calc.AddNode;
  39 import org.graalvm.compiler.nodes.calc.BinaryArithmeticNode;
  40 import org.graalvm.compiler.nodes.cfg.Block;
  41 import org.graalvm.compiler.nodes.util.GraphUtil;
  42 import org.graalvm.compiler.phases.schedule.SchedulePhase;
  43 import org.graalvm.compiler.phases.schedule.SchedulePhase.SchedulingStrategy;
  44 
  45 public class SchedulingTest extends GraphScheduleTest {
  46 
  47     public static int testValueProxyInputsSnippet(int s) {
  48         int i = 0;
  49         while (true) {
  50             i++;
  51             int v = i - s * 2;
  52             if (i == s) {
  53                 return v;
  54             }
  55         }
  56     }
  57 
  58     @Test
  59     public void testValueProxyInputs() {
  60         StructuredGraph graph = parseEager("testValueProxyInputsSnippet", AllowAssumptions.YES);
  61         for (FrameState fs : graph.getNodes().filter(FrameState.class).snapshot()) {
  62             fs.replaceAtUsages(null);
  63             GraphUtil.killWithUnusedFloatingInputs(fs);
  64         }
  65         SchedulePhase schedulePhase = new SchedulePhase(SchedulingStrategy.LATEST);
  66         schedulePhase.apply(graph);
  67         ScheduleResult schedule = graph.getLastSchedule();
  68         NodeMap<Block> nodeToBlock = schedule.getCFG().getNodeToBlock();
  69         assertTrue(graph.getNodes().filter(LoopExitNode.class).count() == 1);
  70         LoopExitNode loopExit = graph.getNodes().filter(LoopExitNode.class).first();
  71         List<Node> list = schedule.nodesFor(nodeToBlock.get(loopExit));
  72         for (BinaryArithmeticNode<?> node : graph.getNodes().filter(BinaryArithmeticNode.class)) {
  73             if (!(node instanceof AddNode)) {
  74                 assertTrue(node.toString(), nodeToBlock.get(node) == nodeToBlock.get(loopExit));
  75                 assertTrue(list.indexOf(node) + " < " + list.indexOf(loopExit) + ", " + node + ", " + loopExit, list.indexOf(node) < list.indexOf(loopExit));
  76             }
  77         }
  78     }
  79 }