1 /*
   2  * Copyright (c) 2015, 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  * @test
  26  * @bug 8130832
  27  * @modules java.base/jdk.internal.misc
  28  * @library /test/lib /
  29  *
  30  * @build sun.hotspot.WhiteBox
  31  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  32  *                                sun.hotspot.WhiteBox$WhiteBoxPermission
  33  * @run main/othervm -Xbootclasspath/a:.
  34  *                   -XX:+UnlockDiagnosticVMOptions
  35  *                   -XX:+WhiteBoxAPI
  36  *                   -XX:+UseCRC32Intrinsics
  37  *                   compiler.intrinsics.IntrinsicAvailableTest
  38  * @run main/othervm -Xbootclasspath/a:.
  39  *                   -XX:+UnlockDiagnosticVMOptions
  40  *                   -XX:+WhiteBoxAPI
  41  *                   -XX:-UseCRC32Intrinsics
  42  *                   compiler.intrinsics.IntrinsicAvailableTest
  43  */
  44 
  45 
  46 package compiler.intrinsics;
  47 
  48 import compiler.whitebox.CompilerWhiteBoxTest;
  49 import jdk.test.lib.Platform;
  50 
  51 import java.lang.reflect.Executable;
  52 import java.util.concurrent.Callable;
  53 
  54 public class IntrinsicAvailableTest extends CompilerWhiteBoxTest {
  55 
  56     public IntrinsicAvailableTest(IntrinsicAvailableTestTestCase testCase) {
  57         super(testCase);
  58     }
  59 
  60     public static class IntrinsicAvailableTestTestCase implements TestCase {
  61 
  62         public String name() {
  63             return "IntrinsicAvailableTestTestCase";
  64         }
  65 
  66         public Executable getExecutable() {
  67             // Using a single method to test the
  68             // WhiteBox.isIntrinsicAvailable(Executable method, int compLevel)
  69             // call for the compilation level corresponding to both the C1 and C2
  70             // compiler keeps the current test simple.
  71             //
  72             // The tested method is java.util.zip.CRC32.update(int, int) because
  73             // both C1 and C2 define an intrinsic for the method and
  74             // the UseCRC32Intrinsics flag can be used to enable/disable
  75             // intrinsification of the method in both product and fastdebug
  76             // builds.
  77             try {
  78                 return Class.forName("java.util.zip.CRC32").getDeclaredMethod("update", int.class, int.class);
  79             } catch (NoSuchMethodException e) {
  80                 throw new RuntimeException("Test bug, method unavailable. " + e);
  81             } catch (ClassNotFoundException e) {
  82                 throw new RuntimeException("Test bug, class unavailable. " + e);
  83             }
  84         }
  85 
  86         public Callable<Integer> getCallable() {
  87             return null;
  88         }
  89 
  90         public boolean isOsr() {
  91             return false;
  92         }
  93 
  94     }
  95 
  96     protected void checkIntrinsicForCompilationLevel(Executable method, int compLevel) throws Exception {
  97         boolean intrinsicEnabled = Boolean.valueOf(getVMOption("UseCRC32Intrinsics"));
  98         boolean intrinsicAvailable = WHITE_BOX.isIntrinsicAvailable(method,
  99                                                                     compLevel);
 100 
 101         String intrinsicEnabledMessage = intrinsicEnabled ? "enabled" : "disabled";
 102         String intrinsicAvailableMessage = intrinsicAvailable ? "available" : "not available";
 103 
 104         if (intrinsicEnabled == intrinsicAvailable) {
 105             System.out.println("Expected result: intrinsic for java.util.zip.CRC32.update() is " +
 106                                intrinsicEnabledMessage + " and intrinsic is " + intrinsicAvailableMessage +
 107                                " at compilation level " + compLevel);
 108         } else {
 109             throw new RuntimeException("Unexpected result: intrinsic for java.util.zip.CRC32.update() is " +
 110                                        intrinsicEnabledMessage + " but intrinsic is " + intrinsicAvailableMessage +
 111                                        " at compilation level " + compLevel);
 112         }
 113     }
 114 
 115     public void test() throws Exception {
 116         Executable intrinsicMethod = testCase.getExecutable();
 117         if (Platform.isServer()) {
 118             if (TIERED_COMPILATION) {
 119                 checkIntrinsicForCompilationLevel(intrinsicMethod, COMP_LEVEL_SIMPLE);
 120             }
 121             // Dont bother check JVMCI compiler - returns false on all intrinsics.
 122             if (!Boolean.valueOf(getVMOption("UseJVMCICompiler"))) {
 123                 checkIntrinsicForCompilationLevel(intrinsicMethod, COMP_LEVEL_FULL_OPTIMIZATION);
 124             }
 125         } else {
 126             checkIntrinsicForCompilationLevel(intrinsicMethod, COMP_LEVEL_SIMPLE);
 127         }
 128     }
 129 
 130     public static void main(String args[]) throws Exception {
 131         new IntrinsicAvailableTest(new IntrinsicAvailableTestTestCase()).test();
 132     }
 133 }