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