1 /*
   2  * Copyright (c) 2012, 2012, 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 static org.graalvm.compiler.core.common.CompilationIdentifier.INVALID_COMPILATION_ID;
  26 
  27 import org.junit.Assert;
  28 import org.junit.Test;
  29 
  30 import org.graalvm.compiler.debug.Debug;
  31 import org.graalvm.compiler.nodes.AbstractBeginNode;
  32 import org.graalvm.compiler.nodes.AbstractMergeNode;
  33 import org.graalvm.compiler.nodes.BeginNode;
  34 import org.graalvm.compiler.nodes.EndNode;
  35 import org.graalvm.compiler.nodes.IfNode;
  36 import org.graalvm.compiler.nodes.MergeNode;
  37 import org.graalvm.compiler.nodes.ReturnNode;
  38 import org.graalvm.compiler.nodes.StructuredGraph;
  39 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  40 import org.graalvm.compiler.nodes.cfg.Block;
  41 import org.graalvm.compiler.nodes.cfg.ControlFlowGraph;
  42 
  43 public class SimpleCFGTest extends GraalCompilerTest {
  44 
  45     private static void dumpGraph(final StructuredGraph graph) {
  46         Debug.dump(Debug.BASIC_LOG_LEVEL, graph, "Graph");
  47     }
  48 
  49     @Test
  50     public void testImplies() {
  51         StructuredGraph graph = new StructuredGraph(AllowAssumptions.YES, INVALID_COMPILATION_ID);
  52 
  53         EndNode trueEnd = graph.add(new EndNode());
  54         EndNode falseEnd = graph.add(new EndNode());
  55 
  56         AbstractBeginNode trueBegin = graph.add(new BeginNode());
  57         trueBegin.setNext(trueEnd);
  58         AbstractBeginNode falseBegin = graph.add(new BeginNode());
  59         falseBegin.setNext(falseEnd);
  60 
  61         IfNode ifNode = graph.add(new IfNode(null, trueBegin, falseBegin, 0.5));
  62         graph.start().setNext(ifNode);
  63 
  64         AbstractMergeNode merge = graph.add(new MergeNode());
  65         merge.addForwardEnd(trueEnd);
  66         merge.addForwardEnd(falseEnd);
  67         ReturnNode returnNode = graph.add(new ReturnNode(null));
  68         merge.setNext(returnNode);
  69 
  70         dumpGraph(graph);
  71 
  72         ControlFlowGraph cfg = ControlFlowGraph.compute(graph, true, true, true, true);
  73 
  74         Block[] blocks = cfg.getBlocks();
  75         // check number of blocks
  76         assertDeepEquals(4, blocks.length);
  77 
  78         // check block - node assignment
  79         assertDeepEquals(blocks[0], cfg.blockFor(graph.start()));
  80         assertDeepEquals(blocks[0], cfg.blockFor(ifNode));
  81         assertDeepEquals(blocks[1], cfg.blockFor(trueBegin));
  82         assertDeepEquals(blocks[1], cfg.blockFor(trueEnd));
  83         assertDeepEquals(blocks[2], cfg.blockFor(falseBegin));
  84         assertDeepEquals(blocks[2], cfg.blockFor(falseEnd));
  85         assertDeepEquals(blocks[3], cfg.blockFor(merge));
  86         assertDeepEquals(blocks[3], cfg.blockFor(returnNode));
  87 
  88         // check dominators
  89         assertDominator(blocks[0], null);
  90         assertDominator(blocks[1], blocks[0]);
  91         assertDominator(blocks[2], blocks[0]);
  92         assertDominator(blocks[3], blocks[0]);
  93 
  94         // check dominated
  95         assertDominatedSize(blocks[0], 3);
  96         assertDominatedSize(blocks[1], 0);
  97         assertDominatedSize(blocks[2], 0);
  98         assertDominatedSize(blocks[3], 0);
  99 
 100         // check postdominators
 101         assertPostdominator(blocks[0], blocks[3]);
 102         assertPostdominator(blocks[1], blocks[3]);
 103         assertPostdominator(blocks[2], blocks[3]);
 104         assertPostdominator(blocks[3], null);
 105     }
 106 
 107     public static void assertDominator(Block block, Block expectedDominator) {
 108         Assert.assertEquals("dominator of " + block, expectedDominator, block.getDominator());
 109     }
 110 
 111     public static void assertDominatedSize(Block block, int size) {
 112         Assert.assertEquals("number of dominated blocks of " + block, size, block.getDominated().size());
 113     }
 114 
 115     public static void assertPostdominator(Block block, Block expectedPostdominator) {
 116         Assert.assertEquals("postdominator of " + block, expectedPostdominator, block.getPostdominator());
 117     }
 118 
 119 }