1 /*
2  * Copyright (c) 2015, 2019, 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.replacements.test;
26 
27 import static org.graalvm.compiler.nodes.graphbuilderconf.InlineInvokePlugin.InlineInfo.createStandardInlineInfo;
28 
29 import jdk.internal.vm.compiler.collections.EconomicMap;
30 import org.graalvm.compiler.core.common.type.StampFactory;
31 import org.graalvm.compiler.core.test.GraalCompilerTest;
32 import org.graalvm.compiler.debug.DebugContext;
33 import org.graalvm.compiler.nodes.AbstractBeginNode;
34 import org.graalvm.compiler.nodes.StructuredGraph;
35 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
36 import org.graalvm.compiler.nodes.ValueNode;
37 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
38 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
39 import org.graalvm.compiler.nodes.graphbuilderconf.InlineInvokePlugin;
40 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin;
41 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
42 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration;
43 import org.graalvm.compiler.nodes.memory.HeapAccess.BarrierType;
44 import org.graalvm.compiler.nodes.memory.ReadNode;
45 import org.graalvm.compiler.nodes.memory.address.AddressNode;
46 import org.graalvm.compiler.nodes.memory.address.OffsetAddressNode;
47 import org.graalvm.compiler.nodes.spi.CoreProviders;
48 import org.graalvm.compiler.phases.OptimisticOptimizations;
49 import org.graalvm.compiler.replacements.CachingPEGraphDecoder;
50 import jdk.internal.vm.compiler.word.LocationIdentity;
51 import org.junit.Test;
52 
53 import jdk.vm.ci.meta.JavaKind;
54 import jdk.vm.ci.meta.ResolvedJavaMethod;
55 
56 public class PEGraphDecoderTest extends GraalCompilerTest {
57 
58     /**
59      * This method is intrinsified to a node with a guard dependency on the block it is in. The
60      * various tests ensure that this guard is correctly updated when blocks are merged during
61      * inlining.
62      */
readInt(Object obj, long offset)63     private static native int readInt(Object obj, long offset);
64 
65     private static boolean flag;
66     private static int value;
67 
invokeSimple()68     private static void invokeSimple() {
69         value = 111;
70     }
71 
invokeComplicated()72     private static void invokeComplicated() {
73         if (flag) {
74             value = 0;
75         } else {
76             value = 42;
77         }
78     }
79 
readInt1(Object obj)80     private static int readInt1(Object obj) {
81         return readInt(obj, 16);
82     }
83 
readInt2(Object obj)84     private static int readInt2(Object obj) {
85         invokeSimple();
86         return readInt(obj, 16);
87     }
88 
readInt3(Object obj)89     private static int readInt3(Object obj) {
90         invokeComplicated();
91         return readInt(obj, 16);
92     }
93 
readInt4(Object obj, int n)94     private static int readInt4(Object obj, int n) {
95         if (n > 0) {
96             invokeComplicated();
97         }
98         return readInt(obj, 16);
99     }
100 
doTest(Object obj)101     public static int doTest(Object obj) {
102         int result = 0;
103         result += readInt1(obj);
104         result += readInt2(obj);
105         result += readInt3(obj);
106         result += readInt4(obj, 2);
107         return result;
108     }
109 
registerPlugins(InvocationPlugins plugins)110     private static void registerPlugins(InvocationPlugins plugins) {
111         Registration r = new Registration(plugins, PEGraphDecoderTest.class);
112         r.register2("readInt", Object.class, long.class, new InvocationPlugin() {
113             @Override
114             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unused, ValueNode obj, ValueNode offset) {
115                 AddressNode address = b.add(new OffsetAddressNode(obj, offset));
116                 ReadNode read = b.addPush(JavaKind.Int, new ReadNode(address, LocationIdentity.any(), StampFactory.forKind(JavaKind.Int), BarrierType.NONE));
117                 read.setGuard(AbstractBeginNode.prevBegin(read));
118                 return true;
119             }
120         });
121     }
122 
123     class InlineAll implements InlineInvokePlugin {
124         @Override
shouldInlineInvoke(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode[] args)125         public InlineInfo shouldInlineInvoke(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode[] args) {
126             return createStandardInlineInfo(method);
127         }
128     }
129 
130     @Test
131     @SuppressWarnings("try")
test()132     public void test() {
133         ResolvedJavaMethod testMethod = getResolvedJavaMethod(PEGraphDecoderTest.class, "doTest", Object.class);
134         StructuredGraph targetGraph = null;
135         DebugContext debug = getDebugContext();
136         try (DebugContext.Scope scope = debug.scope("GraphPETest", testMethod)) {
137             GraphBuilderConfiguration graphBuilderConfig = GraphBuilderConfiguration.getDefault(getDefaultGraphBuilderPlugins()).withEagerResolving(true).withUnresolvedIsError(true);
138             registerPlugins(graphBuilderConfig.getPlugins().getInvocationPlugins());
139             targetGraph = new StructuredGraph.Builder(getInitialOptions(), debug, AllowAssumptions.YES).method(testMethod).build();
140             CachingPEGraphDecoder decoder = new CachingPEGraphDecoder(getTarget().arch, targetGraph, getProviders(), graphBuilderConfig, OptimisticOptimizations.NONE, AllowAssumptions.YES,
141                             null, null, new InlineInvokePlugin[]{new InlineAll()}, null, null, null, null, null, null, EconomicMap.create());
142 
143             decoder.decode(testMethod, false, false);
144             debug.dump(DebugContext.BASIC_LEVEL, targetGraph, "Target Graph");
145             targetGraph.verify();
146 
147             CoreProviders context = getProviders();
148             createCanonicalizerPhase().apply(targetGraph, context);
149             targetGraph.verify();
150 
151         } catch (Throwable ex) {
152             if (targetGraph != null) {
153                 debug.dump(DebugContext.BASIC_LEVEL, targetGraph, ex.toString());
154             }
155             debug.handle(ex);
156         }
157     }
158 }
159