1 /*
   2  * Copyright (c) 2016, 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  */
  23 package org.graalvm.compiler.api.directives.test;
  24 
  25 import org.junit.Test;
  26 
  27 import org.graalvm.compiler.api.directives.GraalDirectives;
  28 import org.graalvm.compiler.core.common.GraalOptions;
  29 import org.graalvm.compiler.core.test.GraalCompilerTest;
  30 import org.graalvm.compiler.options.OptionValue;
  31 import org.graalvm.compiler.options.OptionValue.OverrideScope;
  32 
  33 import jdk.vm.ci.code.InstalledCode;
  34 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
  35 import jdk.vm.ci.meta.ResolvedJavaMethod;
  36 
  37 public class IsMethodInlineDirectiveTest extends GraalCompilerTest {
  38 
  39     public IsMethodInlineDirectiveTest() {
  40         HotSpotResolvedJavaMethod calleeSnippet = (HotSpotResolvedJavaMethod) getResolvedJavaMethod(IsMethodInlineDirectiveTest.class, "calleeSnippet");
  41         calleeSnippet.shouldBeInlined();
  42 
  43         HotSpotResolvedJavaMethod calleeWithInstrumentationSnippet = (HotSpotResolvedJavaMethod) getResolvedJavaMethod(IsMethodInlineDirectiveTest.class, "calleeWithInstrumentationSnippet");
  44         calleeWithInstrumentationSnippet.shouldBeInlined();
  45     }
  46 
  47     public static boolean rootMethodSnippet() {
  48         return GraalDirectives.isMethodInlined();
  49     }
  50 
  51     @SuppressWarnings("try")
  52     @Test
  53     public void testRootMethod() {
  54         ResolvedJavaMethod method = getResolvedJavaMethod("rootMethodSnippet");
  55         executeExpected(method, null); // ensure the method is fully resolved
  56         // The target method is not inlined. We expect the result to be false.
  57         InstalledCode code = getCode(method);
  58         try {
  59             Result result = new Result(code.executeVarargs(), null);
  60             assertEquals(new Result(false, null), result);
  61         } catch (Throwable e) {
  62             throw new AssertionError(e);
  63         }
  64     }
  65 
  66     public static boolean calleeSnippet() {
  67         return GraalDirectives.isMethodInlined();
  68     }
  69 
  70     public static boolean callerSnippet() {
  71         return calleeSnippet();
  72     }
  73 
  74     @Test
  75     public void testInlinedCallee() {
  76         ResolvedJavaMethod method = getResolvedJavaMethod("callerSnippet");
  77         executeExpected(method, null); // ensure the method is fully resolved
  78         // calleeSnippet will be inlined. We expect the result to be true.
  79         InstalledCode code = getCode(method);
  80         try {
  81             Result result = new Result(code.executeVarargs(), null);
  82             assertEquals(new Result(true, null), result);
  83         } catch (Throwable e) {
  84             throw new AssertionError(e);
  85         }
  86     }
  87 
  88     static boolean isCalleeInlined;
  89     static boolean isCallerInlined;
  90 
  91     public static void calleeWithInstrumentationSnippet() {
  92         GraalDirectives.instrumentationBegin();
  93         isCalleeInlined = GraalDirectives.isMethodInlined();
  94         GraalDirectives.instrumentationEnd();
  95     }
  96 
  97     public static void callerSnippet1() {
  98         calleeWithInstrumentationSnippet();
  99 
 100         GraalDirectives.instrumentationBegin();
 101         isCallerInlined = GraalDirectives.isMethodInlined();
 102         GraalDirectives.instrumentationEnd();
 103     }
 104 
 105     @SuppressWarnings("try")
 106     @Test
 107     public void testInlinedCalleeWithInstrumentation() {
 108         try (OverrideScope s = OptionValue.override(GraalOptions.UseGraalInstrumentation, true)) {
 109             ResolvedJavaMethod method = getResolvedJavaMethod("callerSnippet1");
 110             executeExpected(method, null); // ensure the method is fully resolved
 111             isCalleeInlined = false;
 112             isCallerInlined = false;
 113             // calleeWithInstrumentationSnippet will be inlined. We expect the flag1 set in
 114             // calleeWithInstrumentationSnippet to be true, and the flag2 set in callerSnippet1 to
 115             // be false.
 116             InstalledCode code = getCode(method);
 117             code.executeVarargs();
 118             assertTrue("calleWithInstrumentationSnippet should be inlined", isCalleeInlined);
 119             assertFalse("callerSnippet1 should not be inlined", isCallerInlined);
 120         } catch (Throwable e) {
 121             throw new AssertionError(e);
 122         }
 123     }
 124 
 125 }