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