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 org.graalvm.compiler.api.directives.GraalDirectives;
28 import org.graalvm.compiler.api.replacements.ClassSubstitution;
29 import org.graalvm.compiler.api.replacements.Fold;
30 import org.graalvm.compiler.api.replacements.MethodSubstitution;
31 import org.graalvm.compiler.bytecode.BytecodeProvider;
32 import org.graalvm.compiler.nodes.ReturnNode;
33 import org.graalvm.compiler.nodes.StartNode;
34 import org.graalvm.compiler.nodes.StructuredGraph;
35 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
36 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration;
37 import org.graalvm.compiler.nodes.graphbuilderconf.NodeIntrinsicPluginFactory.InjectionProvider;
38 import org.graalvm.compiler.replacements.NodeIntrinsificationProvider;
39 import org.junit.Test;
40 
41 public class FoldTest extends ReplacementsTest {
42 
43     private static class TestMethod {
44 
test()45         public static int test() {
46             return 42;
47         }
48     }
49 
50     static class FoldUtils {
51 
52         private int number;
53 
FoldUtils(int number)54         FoldUtils(int number) {
55             this.number = number;
56         }
57 
58         @Fold
multiply(int a, int b)59         static int multiply(int a, int b) {
60             // we want to test whether @Fold works, so prevent automatic constant folding
61             return a * GraalDirectives.opaque(b);
62         }
63 
64         @Fold
getNumber()65         int getNumber() {
66             // we want to test whether @Fold works, so prevent automatic constant folding
67             return GraalDirectives.opaque(number);
68         }
69     }
70 
71     @ClassSubstitution(TestMethod.class)
72     private static class TestMethodSubstitution {
73 
74         private static final FoldUtils utils = new FoldUtils(21);
75 
76         @MethodSubstitution
test()77         public static int test() {
78             return FoldUtils.multiply(utils.getNumber(), 2);
79         }
80     }
81 
82     @Override
registerInvocationPlugins(InvocationPlugins invocationPlugins)83     protected void registerInvocationPlugins(InvocationPlugins invocationPlugins) {
84         InjectionProvider injection = new NodeIntrinsificationProvider(getMetaAccess(), getSnippetReflection(), getProviders().getForeignCalls(), null, getTarget());
85         new PluginFactory_FoldTest().registerPlugins(invocationPlugins, injection);
86         BytecodeProvider replacementBytecodeProvider = getSystemClassLoaderBytecodeProvider();
87         Registration r = new Registration(invocationPlugins, TestMethod.class, getReplacements(), replacementBytecodeProvider);
88         r.registerMethodSubstitution(TestMethodSubstitution.class, "test");
89         super.registerInvocationPlugins(invocationPlugins);
90     }
91 
callTest()92     public static int callTest() {
93         return TestMethod.test();
94     }
95 
96     @Override
checkHighTierGraph(StructuredGraph graph)97     protected void checkHighTierGraph(StructuredGraph graph) {
98         // check that folding happened correctly
99         StartNode start = graph.start();
100         assert start.next() instanceof ReturnNode : "expected ReturnNode, got " + start.next();
101 
102         ReturnNode ret = (ReturnNode) start.next();
103         assert ret.result().isConstant() : "expected ConstantNode, got " + ret.result();
104     }
105 
106     @Test
snippetTest()107     public void snippetTest() {
108         test("callTest");
109     }
110 }
111