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