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 /testlibrary /test/lib /
  29  *
  30  * @build compiler.intrinsics.IntrinsicAvailableTest
  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     protected String VMName;
  56 
  57     public IntrinsicAvailableTest(IntrinsicAvailableTestTestCase testCase) {
  58         super(testCase);
  59         VMName = System.getProperty("java.vm.name");
  60     }
  61 
  62     public static class IntrinsicAvailableTestTestCase implements TestCase {
  63 
  64         public String name() {
  65             return "IntrinsicAvailableTestTestCase";
  66         }
  67 
  68         public Executable getExecutable() {
  69             // Using a single method to test the
  70             // WhiteBox.isIntrinsicAvailable(Executable method, int compLevel)
  71             // call for the compilation level corresponding to both the C1 and C2
  72             // compiler keeps the current test simple.
  73             //
  74             // The tested method is java.util.zip.CRC32.update(int, int) because
  75             // both C1 and C2 define an intrinsic for the method and
  76             // the UseCRC32Intrinsics flag can be used to enable/disable
  77             // intrinsification of the method in both product and fastdebug
  78             // builds.
  79             try {
  80                 return Class.forName("java.util.zip.CRC32").getDeclaredMethod("update", int.class, int.class);
  81             } catch (NoSuchMethodException e) {
  82                 throw new RuntimeException("Test bug, method unavailable. " + e);
  83             } catch (ClassNotFoundException e) {
  84                 throw new RuntimeException("Test bug, class unavailable. " + e);
  85             }
  86         }
  87 
  88         public Callable<Integer> getCallable() {
  89             return null;
  90         }
  91 
  92         public boolean isOsr() {
  93             return false;
  94         }
  95 
  96     }
  97 
  98     protected void checkIntrinsicForCompilationLevel(Executable method, int compLevel) throws Exception {
  99         boolean intrinsicEnabled = Boolean.valueOf(getVMOption("UseCRC32Intrinsics"));
 100         boolean intrinsicAvailable = WHITE_BOX.isIntrinsicAvailable(method,
 101                                                                     compLevel);
 102 
 103         String intrinsicEnabledMessage = intrinsicEnabled ? "enabled" : "disabled";
 104         String intrinsicAvailableMessage = intrinsicAvailable ? "available" : "not available";
 105 
 106         if (intrinsicEnabled == intrinsicAvailable) {
 107             System.out.println("Expected result: intrinsic for java.util.zip.CRC32.update() is " +
 108                                intrinsicEnabledMessage + " and intrinsic is " + intrinsicAvailableMessage +
 109                                " at compilation level " + compLevel);
 110         } else {
 111             throw new RuntimeException("Unexpected result: intrinsic for java.util.zip.CRC32.update() is " +
 112                                        intrinsicEnabledMessage + " but intrinsic is " + intrinsicAvailableMessage +
 113                                        " at compilation level " + compLevel);
 114         }
 115     }
 116 
 117     public void test() throws Exception {
 118         Executable intrinsicMethod = testCase.getExecutable();
 119         if (Platform.isServer()) {
 120             if (TIERED_COMPILATION) {
 121                 checkIntrinsicForCompilationLevel(intrinsicMethod, COMP_LEVEL_SIMPLE);
 122             }
 123             // Dont bother check JVMCI compiler - returns false on all intrinsics.
 124             if (!Boolean.valueOf(getVMOption("UseJVMCICompiler"))) {
 125                 checkIntrinsicForCompilationLevel(intrinsicMethod, COMP_LEVEL_FULL_OPTIMIZATION);
 126             }
 127         } else {
 128             checkIntrinsicForCompilationLevel(intrinsicMethod, COMP_LEVEL_SIMPLE);
 129         }
 130     }
 131 
 132     public static void main(String args[]) throws Exception {
 133         new IntrinsicAvailableTest(new IntrinsicAvailableTestTestCase()).test();
 134     }
 135 }