1 /*
   2  * Copyright (c) 2017, 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 
  26 package org.graalvm.compiler.core.test;
  27 
  28 import org.graalvm.compiler.nodes.StructuredGraph;
  29 import org.graalvm.compiler.nodes.memory.WriteNode;
  30 import org.graalvm.compiler.nodes.spi.LoweringTool;
  31 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  32 import org.graalvm.compiler.phases.common.FloatingReadPhase;
  33 import org.graalvm.compiler.phases.common.IncrementalCanonicalizerPhase;
  34 import org.graalvm.compiler.phases.common.LoweringPhase;
  35 import org.graalvm.compiler.phases.tiers.HighTierContext;
  36 import org.junit.Test;
  37 
  38 public class MemoryGraphCanonicalizeTest extends GraalCompilerTest {
  39     static class TestObject {
  40         Object object;
  41         Integer integer;
  42         int value;
  43         volatile boolean written;
  44     }
  45 
  46     public static void simpleElimination(TestObject object) {
  47         object.object = object;
  48         object.value = object.integer;
  49         object.value = object.integer + 2;
  50         object.value = object.integer + 3;
  51     }
  52 
  53     @Test
  54     public void testSimpleElimination() {
  55         testGraph("simpleElimination", 2);
  56     }
  57 
  58     public static void complexElimination(TestObject object) {
  59         object.object = object;
  60         object.value = object.integer;
  61         object.value = object.integer + 2;
  62         if (object.object == null) {
  63             object.value = object.integer + 3;
  64         } else {
  65             object.object = new Object();
  66         }
  67         object.written = true;
  68         object.value = 5;
  69     }
  70 
  71     @Test
  72     public void testComplexElimination() {
  73         testGraph("complexElimination", 6);
  74     }
  75 
  76     public void testGraph(String name, int expectedWrites) {
  77         StructuredGraph graph = parseEager(name, StructuredGraph.AllowAssumptions.YES);
  78         HighTierContext context = getDefaultHighTierContext();
  79         CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
  80         new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
  81         new IncrementalCanonicalizerPhase<>(canonicalizer, new FloatingReadPhase()).apply(graph, context);
  82         new CanonicalizerPhase().apply(graph, context);
  83         int writes = graph.getNodes().filter(WriteNode.class).count();
  84         assertTrue(writes == expectedWrites, "Expected %d writes, found %d", expectedWrites, writes);
  85     }
  86 }