1 /*
   2  * Copyright (c) 2011, 2018, 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.graph.Node;
  30 import org.graalvm.compiler.graph.NodeMap;
  31 import org.graalvm.compiler.nodes.StructuredGraph;
  32 import org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult;
  33 import org.graalvm.compiler.nodes.cfg.Block;
  34 import org.graalvm.compiler.phases.schedule.SchedulePhase;
  35 import org.junit.Assert;
  36 
  37 import jdk.vm.ci.meta.SpeculationLog;
  38 
  39 public class GraphScheduleTest extends GraalCompilerTest {
  40 
  41     protected void assertOrderedAfterSchedule(StructuredGraph graph, Node a, Node b) {
  42         assertOrderedAfterSchedule(graph, SchedulePhase.SchedulingStrategy.LATEST, a, b);
  43     }
  44 
  45     protected void assertOrderedAfterSchedule(StructuredGraph graph, SchedulePhase.SchedulingStrategy strategy, Node a, Node b) {
  46         SchedulePhase ibp = new SchedulePhase(strategy);
  47         ibp.apply(graph);
  48         assertOrderedAfterLastSchedule(graph, a, b);
  49     }
  50 
  51     protected void assertOrderedAfterLastSchedule(StructuredGraph graph, Node a, Node b) {
  52         assertOrderedAfterSchedule(graph.getLastSchedule(), a, b);
  53     }
  54 
  55     protected void assertOrderedAfterSchedule(ScheduleResult ibp, Node a, Node b) {
  56         NodeMap<Block> nodeToBlock = ibp.getCFG().getNodeToBlock();
  57         Block bBlock = nodeToBlock.get(b);
  58         Block aBlock = nodeToBlock.get(a);
  59 
  60         if (bBlock == aBlock) {
  61             List<Node> instructions = ibp.nodesFor(bBlock);
  62             Assert.assertTrue(a + " should be before " + b, instructions.indexOf(b) > instructions.indexOf(a));
  63         } else {
  64             Block block = bBlock;
  65             while (block != null) {
  66                 if (block == aBlock) {
  67                     return;
  68                 }
  69                 block = block.getDominator();
  70             }
  71             Assert.fail("block of A doesn't dominate the block of B");
  72         }
  73     }
  74 
  75     @Override
  76     protected SpeculationLog getSpeculationLog() {
  77         return getCodeCache().createSpeculationLog();
  78     }
  79 }