1 /*
   2  * Copyright (c) 2013, 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 import jdk.test.lib.Platform;
  25 import intrinsics.Verifier;
  26 
  27 import java.io.FileOutputStream;
  28 import java.lang.reflect.Executable;
  29 import java.util.Properties;
  30 
  31 public abstract class IntrinsicBase extends CompilerWhiteBoxTest {
  32     protected String javaVmName;
  33     protected String useMathExactIntrinsics;
  34 
  35     protected IntrinsicBase(TestCase testCase) {
  36         super(testCase);
  37         javaVmName = System.getProperty("java.vm.name");
  38         useMathExactIntrinsics = getVMOption("UseMathExactIntrinsics");
  39     }
  40 
  41     @Override
  42     protected void test() throws Exception {
  43         //java.lang.Math should be loaded to allow a compilation of the methods that use Math's method
  44         System.out.println("class java.lang.Math should be loaded. Proof: " + Math.class);
  45         printEnvironmentInfo();
  46 
  47         int expectedIntrinsicCount = 0;
  48 
  49         switch (MODE) {
  50             case "compiled mode":
  51             case "mixed mode":
  52                 if (isServerVM()) {
  53                     if (TIERED_COMPILATION) {
  54                         int max_level = TIERED_STOP_AT_LEVEL;
  55                         expectedIntrinsicCount = (max_level == COMP_LEVEL_MAX) ? 1 : 0;
  56                         for (int i = CompilerWhiteBoxTest.COMP_LEVEL_SIMPLE; i <= max_level; ++i) {
  57                             deoptimize();
  58                             compileAtLevel(i);
  59                         }
  60                     } else {
  61                         expectedIntrinsicCount = 1;
  62                         deoptimize();
  63                         compileAtLevel(CompilerWhiteBoxTest.COMP_LEVEL_MAX);
  64                     }
  65                 } else {
  66                     deoptimize();
  67                     compileAtLevel(CompilerWhiteBoxTest.COMP_LEVEL_SIMPLE);
  68                 }
  69 
  70                 if (!isIntrinsicAvailable()) {
  71                     expectedIntrinsicCount = 0;
  72                 }
  73                 break;
  74             case "interpreted mode": //test is not applicable in this mode;
  75                 System.err.println("Warning: This test is not applicable in mode: " + MODE);
  76                 break;
  77             default:
  78                 throw new RuntimeException("Test bug, unknown VM mode: " + MODE);
  79         }
  80 
  81         System.out.println("Expected intrinsic count is " + expectedIntrinsicCount + " name " + getIntrinsicId());
  82 
  83         final FileOutputStream out = new FileOutputStream(getVMOption("LogFile") + Verifier.PROPERTY_FILE_SUFFIX);
  84         Properties expectedProps = new Properties();
  85         expectedProps.setProperty(Verifier.INTRINSIC_NAME_PROPERTY, getIntrinsicId());
  86         expectedProps.setProperty(Verifier.INTRINSIC_EXPECTED_COUNT_PROPERTY, String.valueOf(expectedIntrinsicCount));
  87         expectedProps.store(out, null);
  88 
  89         out.close();
  90     }
  91 
  92     protected void printEnvironmentInfo() {
  93         System.out.println("java.vm.name=" + javaVmName);
  94         System.out.println("os.arch=" + Platform.getOsArch());
  95         System.out.println("java.vm.info=" + MODE);
  96         System.out.println("useMathExactIntrinsics=" + useMathExactIntrinsics);
  97     }
  98 
  99     protected void compileAtLevel(int level) {
 100         WHITE_BOX.enqueueMethodForCompilation(method, level);
 101         waitBackgroundCompilation();
 102         checkCompilation(method, level);
 103     }
 104 
 105     protected void checkCompilation(Executable executable, int level) {
 106         if (!WHITE_BOX.isMethodCompiled(executable)) {
 107             throw new RuntimeException("Test bug, expected compilation (level): " + level + ", but not compiled");
 108         }
 109         final int compilationLevel = WHITE_BOX.getMethodCompilationLevel(executable);
 110         if (compilationLevel != level) {
 111             if (!(TIERED_COMPILATION && level == COMP_LEVEL_FULL_PROFILE && compilationLevel == COMP_LEVEL_LIMITED_PROFILE)) { //possible case
 112                 throw new RuntimeException("Test bug, expected compilation (level): " + level + ", but level: " + compilationLevel);
 113             }
 114         }
 115     }
 116 
 117     // An intrinsic is available if:
 118     // - the intrinsic is enabled (by using the appropriate command-line flag) and
 119     // - the intrinsic is supported by the VM (i.e., the platform on which the VM is
 120     //   running provides the instructions necessary for the VM to generate the intrinsic).
 121     protected abstract boolean isIntrinsicAvailable();
 122 
 123     protected abstract String getIntrinsicId();
 124 
 125     protected boolean isServerVM() {
 126         return javaVmName.toLowerCase().contains("server");
 127     }
 128 
 129     static class IntTest extends IntrinsicBase {
 130 
 131         protected boolean isIntrinsicAvailable; // The tested intrinsic is available on the current platform.
 132 
 133         protected IntTest(MathIntrinsic.IntIntrinsic testCase) {
 134             super(testCase);
 135             // Only the C2 compiler intrinsifies exact math methods
 136             // so check if the intrinsics are available with C2.
 137             isIntrinsicAvailable = WHITE_BOX.isIntrinsicAvailableForMethod(testCase.getTestMethod(),
 138                                                                            COMP_LEVEL_FULL_OPTIMIZATION);
 139         }
 140 
 141         @Override
 142         protected boolean isIntrinsicAvailable() {
 143             return isIntrinsicAvailable;
 144         }
 145 
 146         @Override
 147         protected String getIntrinsicId() {
 148             return "_" + testCase.name().toLowerCase() + "ExactI";
 149         }
 150     }
 151 
 152     static class LongTest extends IntrinsicBase {
 153 
 154         protected boolean isIntrinsicAvailable; // The tested intrinsic is available on the current platform.
 155 
 156         protected LongTest(MathIntrinsic.LongIntrinsic testCase) {
 157             super(testCase);
 158             // Only the C2 compiler intrinsifies exact math methods
 159             // so check if the intrinsics are available with C2.
 160             isIntrinsicAvailable = WHITE_BOX.isIntrinsicAvailableForMethod(testCase.getTestMethod(),
 161                                                                            COMP_LEVEL_FULL_OPTIMIZATION);
 162         }
 163 
 164         @Override
 165         protected boolean isIntrinsicAvailable() {
 166             return isIntrinsicAvailable;
 167         }
 168 
 169         @Override
 170         protected String getIntrinsicId() {
 171             return "_" + testCase.name().toLowerCase() + "ExactL";
 172         }
 173     }
 174 }