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 package org.graalvm.compiler.hotspot.test; 26 27 import org.graalvm.compiler.core.common.GraalOptions; 28 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig; 29 import org.graalvm.compiler.hotspot.meta.HotSpotClassInitializationPlugin; 30 import org.graalvm.compiler.hotspot.nodes.aot.InitializeKlassNode; 31 import org.graalvm.compiler.hotspot.nodes.aot.LoadConstantIndirectlyNode; 32 import org.graalvm.compiler.hotspot.nodes.aot.ResolveConstantNode; 33 import org.graalvm.compiler.hotspot.phases.LoadJavaMirrorWithKlassPhase; 34 import org.graalvm.compiler.hotspot.phases.aot.EliminateRedundantInitializationPhase; 35 import org.graalvm.compiler.hotspot.phases.aot.ReplaceConstantNodesPhase; 36 import org.graalvm.compiler.nodes.StructuredGraph; 37 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions; 38 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins; 39 import org.graalvm.compiler.nodes.spi.LoweringTool; 40 import org.graalvm.compiler.options.OptionValues; 41 import org.graalvm.compiler.phases.common.CanonicalizerPhase; 42 import org.graalvm.compiler.phases.common.LoweringPhase; 43 import org.graalvm.compiler.phases.tiers.HighTierContext; 44 import org.junit.Assert; 45 import org.junit.Test; 46 47 public class ReplaceConstantNodesPhaseTest extends HotSpotGraalCompilerTest { 48 private final GraalHotSpotVMConfig config = runtime().getVMConfig(); 49 50 @Override 51 protected Plugins getDefaultGraphBuilderPlugins() { 52 Plugins plugins = super.getDefaultGraphBuilderPlugins(); 53 plugins.setClassInitializationPlugin(new HotSpotClassInitializationPlugin()); 54 return plugins; 55 } 56 57 public static class X { 58 public static int x; 59 public static int y; 60 public static int z; 61 public static Object o; 62 } 63 64 public static class Y extends X { 65 public static int a; 66 public static int b; 67 } 68 69 public static int a; 70 71 public static void assignFields() { 72 X.x = 1; 73 X.y = 2; 74 X.z = 3; 75 } 76 77 public static void assignFieldsInBranches(boolean x) { 78 if (x) { 79 X.y = 1; 80 } else { 81 X.z = 2; 82 } 83 } 84 85 public static void assignFieldsWithDominatingInit(boolean x) { 86 X.x = 1; 87 if (x) { 88 X.y = 2; 89 } else { 90 X.z = 3; 91 } 92 } 93 94 public static void assignString() { 95 X.o = "foo"; 96 } 97 98 public static void assignToParentAndChild() { 99 Y.a = 1; 100 X.x = 2; 101 } 102 103 public static void assignToThis() { 104 a = 1; 105 } 106 107 public static void assignFieldsWithDominatingInitOfParent(boolean x) { 108 Y.a = 1; 109 if (x) { 110 X.y = 2; 111 } else { 112 X.z = 3; 113 } 114 Y.b = 4; 115 } 116 117 private void test(String name, int expectedInits, int expectedResolves, int expectedLoads) { 118 StructuredGraph graph = parseEager(name, AllowAssumptions.NO, new OptionValues(getInitialOptions(), GraalOptions.GeneratePIC, true)); 119 HighTierContext highTierContext = getDefaultHighTierContext(); 120 CanonicalizerPhase canonicalizer = new CanonicalizerPhase(); 121 new EliminateRedundantInitializationPhase().apply(graph, highTierContext); 122 new LoweringPhase(canonicalizer, LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, highTierContext); 123 new LoadJavaMirrorWithKlassPhase(config).apply(graph, highTierContext); 124 new ReplaceConstantNodesPhase(false).apply(graph, highTierContext); 125 Assert.assertEquals(expectedInits, graph.getNodes().filter(InitializeKlassNode.class).count()); 126 Assert.assertEquals(expectedResolves, graph.getNodes().filter(ResolveConstantNode.class).count()); 127 Assert.assertEquals(expectedLoads, graph.getNodes().filter(LoadConstantIndirectlyNode.class).count()); 128 } 129 130 @Test 131 public void test1() { 132 test("assignFields", 1, 0, 0); 133 } 134 135 @Test 136 public void test2() { 137 test("assignFieldsWithDominatingInit", 1, 0, 0); 138 } 139 140 @Test 141 public void test3() { 142 test("assignString", 1, 1, 0); 143 } 144 145 @Test 146 public void test4() { 147 test("assignToParentAndChild", 1, 1, 0); 148 } 149 150 @Test 151 public void test5() { 152 test("assignToThis", 0, 0, 1); 153 } 154 155 @Test 156 public void test6() { 157 test("assignFieldsWithDominatingInitOfParent", 1, 1, 0); 158 } 159 160 @Test 161 public void test7() { 162 test("assignFieldsInBranches", 2, 1, 0); 163 } 164 }