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 (!isIntrinsicSupported()) {
  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     protected abstract boolean isIntrinsicSupported();
 118 
 119     protected abstract String getIntrinsicId();
 120 
 121     protected boolean isServerVM() {
 122         return javaVmName.toLowerCase().contains("server");
 123     }
 124 
 125     static class IntTest extends IntrinsicBase {
 126         protected IntTest(MathIntrinsic.IntIntrinsic testCase) {
 127             super(testCase);
 128         }
 129 
 130         @Override
 131         protected boolean isIntrinsicSupported() {
 132             return isServerVM() && Boolean.valueOf(useMathExactIntrinsics)
 133                 && (Platform.isX86() || Platform.isX64() || Platform.isAArch64());
 134         }
 135 
 136         @Override
 137         protected String getIntrinsicId() {
 138             return "_" + testCase.name().toLowerCase() + "ExactI";
 139         }
 140     }
 141 
 142     static class LongTest extends IntrinsicBase {
 143         protected LongTest(MathIntrinsic.LongIntrinsic testCase) {
 144             super(testCase);
 145         }
 146 
 147         @Override
 148         protected boolean isIntrinsicSupported() {
 149             return isServerVM() && Boolean.valueOf(useMathExactIntrinsics) &&
 150                 (Platform.isX64() || Platform.isPPC() || Platform.isAArch64());
 151         }
 152 
 153         @Override
 154         protected String getIntrinsicId() {
 155             return "_" + testCase.name().toLowerCase() + "ExactL";
 156         }
 157     }
 158 }