PEGraphDecoderTest.java revision 12657:6ef01bd40ce2
1/*
2 * Copyright (c) 2015, 2016, 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 */
23package org.graalvm.compiler.replacements.test;
24
25import static org.graalvm.compiler.core.common.CompilationIdentifier.INVALID_COMPILATION_ID;
26import static org.graalvm.compiler.nodes.graphbuilderconf.InlineInvokePlugin.InlineInfo.createStandardInlineInfo;
27
28import org.junit.Test;
29
30import org.graalvm.compiler.core.common.LocationIdentity;
31import org.graalvm.compiler.core.common.type.StampFactory;
32import org.graalvm.compiler.core.test.GraalCompilerTest;
33import org.graalvm.compiler.debug.Debug;
34import org.graalvm.compiler.nodes.AbstractBeginNode;
35import org.graalvm.compiler.nodes.StructuredGraph;
36import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
37import org.graalvm.compiler.nodes.ValueNode;
38import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
39import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
40import org.graalvm.compiler.nodes.graphbuilderconf.InlineInvokePlugin;
41import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin;
42import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
43import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration;
44import org.graalvm.compiler.nodes.memory.HeapAccess.BarrierType;
45import org.graalvm.compiler.nodes.memory.ReadNode;
46import org.graalvm.compiler.nodes.memory.address.AddressNode;
47import org.graalvm.compiler.nodes.memory.address.OffsetAddressNode;
48import org.graalvm.compiler.phases.OptimisticOptimizations;
49import org.graalvm.compiler.phases.common.CanonicalizerPhase;
50import org.graalvm.compiler.phases.tiers.PhaseContext;
51import org.graalvm.compiler.replacements.CachingPEGraphDecoder;
52
53import jdk.vm.ci.meta.JavaKind;
54import jdk.vm.ci.meta.ResolvedJavaMethod;
55
56public 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     */
63    private static native int readInt(Object obj, long offset);
64
65    private static boolean flag;
66    private static int value;
67
68    private static void invokeSimple() {
69        value = 111;
70    }
71
72    private static void invokeComplicated() {
73        if (flag) {
74            value = 0;
75        } else {
76            value = 42;
77        }
78    }
79
80    private static int readInt1(Object obj) {
81        return readInt(obj, 16);
82    }
83
84    private static int readInt2(Object obj) {
85        invokeSimple();
86        return readInt(obj, 16);
87    }
88
89    private static int readInt3(Object obj) {
90        invokeComplicated();
91        return readInt(obj, 16);
92    }
93
94    private static int readInt4(Object obj, int n) {
95        if (n > 0) {
96            invokeComplicated();
97        }
98        return readInt(obj, 16);
99    }
100
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
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
125        public InlineInfo shouldInlineInvoke(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode[] args) {
126            return createStandardInlineInfo(method);
127        }
128    }
129
130    @Test
131    @SuppressWarnings("try")
132    public void test() {
133        ResolvedJavaMethod testMethod = getResolvedJavaMethod(PEGraphDecoderTest.class, "doTest", Object.class);
134        StructuredGraph targetGraph = null;
135        try (Debug.Scope scope = Debug.scope("GraphPETest", testMethod)) {
136            GraphBuilderConfiguration graphBuilderConfig = GraphBuilderConfiguration.getDefault(getDefaultGraphBuilderPlugins()).withEagerResolving(true);
137            registerPlugins(graphBuilderConfig.getPlugins().getInvocationPlugins());
138            CachingPEGraphDecoder decoder = new CachingPEGraphDecoder(getProviders(), graphBuilderConfig, OptimisticOptimizations.NONE, AllowAssumptions.YES, getTarget().arch);
139
140            targetGraph = new StructuredGraph(testMethod, AllowAssumptions.YES, INVALID_COMPILATION_ID);
141            decoder.decode(targetGraph, testMethod, null, null, new InlineInvokePlugin[]{new InlineAll()}, null);
142            Debug.dump(Debug.BASIC_LOG_LEVEL, targetGraph, "Target Graph");
143            targetGraph.verify();
144
145            PhaseContext context = new PhaseContext(getProviders());
146            new CanonicalizerPhase().apply(targetGraph, context);
147            targetGraph.verify();
148
149        } catch (Throwable ex) {
150            if (targetGraph != null) {
151                Debug.dump(Debug.BASIC_LOG_LEVEL, targetGraph, ex.toString());
152            }
153            Debug.handle(ex);
154        }
155    }
156}
157