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 /*
  25  * @test MakeMethodNotCompilableTest
  26  * @bug 8012322 8006683 8007288 8022832
  27  * @library /testlibrary /test/lib
  28  * @modules java.management
  29  * @build MakeMethodNotCompilableTest
  30  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  31  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  32  * @run main/othervm/timeout=2400 -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:CompileCommand=compileonly,SimpleTestCase$Helper::* MakeMethodNotCompilableTest
  33  * @summary testing of WB::makeMethodNotCompilable()
  34  * @author igor.ignatyev@oracle.com
  35  */
  36 public class MakeMethodNotCompilableTest extends CompilerWhiteBoxTest {
  37     private int bci;
  38     public static void main(String[] args) throws Exception {
  39         CompilerWhiteBoxTest.main(MakeMethodNotCompilableTest::new, args);
  40     }
  41 
  42     private MakeMethodNotCompilableTest(TestCase testCase) {
  43         super(testCase);
  44         // to prevent inlining of #method
  45         WHITE_BOX.testSetDontInlineMethod(method, true);
  46     }
  47 
  48     /**
  49      * Tests {@code WB::makeMethodNotCompilable()} by calling it before
  50      * compilation and checking that method isn't compiled. Also
  51      * checks that WB::clearMethodState() clears no-compilable flags. For
  52      * tiered, additional checks for all available levels are conducted.
  53      *
  54      * @throws Exception if one of the checks fails.
  55      */
  56     @Override
  57     protected void test() throws Exception {
  58         if (skipXcompOSR()) {
  59             return;
  60         }
  61         checkNotCompiled();
  62         if (!isCompilable()) {
  63             throw new RuntimeException(method + " must be compilable");
  64         }
  65 
  66         bci = getBci();
  67 
  68         if (TIERED_COMPILATION) {
  69             final int tierLimit = TIERED_STOP_AT_LEVEL + 1;
  70             for (int testedTier = 1; testedTier < tierLimit; ++testedTier) {
  71                 testTier(testedTier);
  72             }
  73             for (int testedTier = 1; testedTier < tierLimit; ++testedTier) {
  74                 makeNotCompilable(testedTier);
  75                 if (isCompilable(testedTier)) {
  76                     throw new RuntimeException(method
  77                             + " must be not compilable at level" + testedTier);
  78                 }
  79                 WHITE_BOX.enqueueMethodForCompilation(method, testedTier, bci);
  80                 checkNotCompiled();
  81 
  82                 if (!isCompilable()) {
  83                     System.out.println(method
  84                             + " is not compilable after level " + testedTier);
  85                 }
  86             }
  87         } else {
  88             compile();
  89             checkCompiled();
  90             int compLevel = getCompLevel();
  91             deoptimize();
  92             makeNotCompilable(compLevel);
  93             if (isCompilable(COMP_LEVEL_ANY)) {
  94                 throw new RuntimeException(method
  95                         + " must be not compilable at CompLevel::CompLevel_any,"
  96                         + " after it is not compilable at " + compLevel);
  97             }
  98 
  99             WHITE_BOX.clearMethodState(method);
 100             if (!isCompilable()) {
 101                 throw new RuntimeException(method
 102                         + " is not compilable after clearMethodState()");
 103             }
 104 
 105             // nocompilable at opposite level must make no sense
 106             int oppositeLevel;
 107             if (isC1Compile(compLevel)) {
 108               oppositeLevel = COMP_LEVEL_FULL_OPTIMIZATION;
 109             } else {
 110               oppositeLevel = COMP_LEVEL_SIMPLE;
 111             }
 112             makeNotCompilable(oppositeLevel);
 113 
 114             if (!isCompilable(COMP_LEVEL_ANY)) {
 115                   throw new RuntimeException(method
 116                         + " must be compilable at CompLevel::CompLevel_any,"
 117                         + " even it is not compilable at opposite level ["
 118                         + compLevel + "]");
 119             }
 120 
 121             if (!isCompilable(compLevel)) {
 122                   throw new RuntimeException(method
 123                         + " must be compilable at level " + compLevel
 124                         + ", even it is not compilable at opposite level ["
 125                         + compLevel + "]");
 126             }
 127         }
 128 
 129         // clearing after tiered/non-tiered tests
 130         // WB.clearMethodState() must reset no-compilable flags
 131         WHITE_BOX.clearMethodState(method);
 132         if (!isCompilable()) {
 133             throw new RuntimeException(method
 134                     + " is not compilable after clearMethodState()");
 135         }
 136         // Make method not (OSR-)compilable (depending on testCase.isOsr())
 137         makeNotCompilable();
 138         if (isCompilable()) {
 139             throw new RuntimeException(method + " must be not compilable");
 140         }
 141         // Try to (OSR-)compile method
 142         compile();
 143         // Method should not be (OSR-)compiled
 144         checkNotCompiled(testCase.isOsr());
 145         if (isCompilable()) {
 146             throw new RuntimeException(method + " must be not compilable");
 147         }
 148         // WB.clearMethodState() must reset no-compilable flags
 149         WHITE_BOX.clearMethodState(method);
 150         if (!isCompilable()) {
 151             throw new RuntimeException(method
 152                     + " is not compilable after clearMethodState()");
 153         }
 154         compile();
 155         checkCompiled();
 156     }
 157 
 158     // separately tests each tier
 159     private void testTier(int testedTier) {
 160         if (!isCompilable(testedTier)) {
 161             throw new RuntimeException(method
 162                     + " is not compilable on start");
 163         }
 164         makeNotCompilable(testedTier);
 165 
 166         // tests for all other tiers
 167         for (int anotherTier = 1, tierLimit = TIERED_STOP_AT_LEVEL + 1;
 168                     anotherTier < tierLimit; ++anotherTier) {
 169             boolean isCompilable = isCompilable(anotherTier);
 170             if (sameCompile(testedTier, anotherTier)) {
 171                 if (isCompilable) {
 172                     throw new RuntimeException(method
 173                             + " must be not compilable at level " + anotherTier
 174                             + ", if it is not compilable at " + testedTier);
 175                 }
 176                 WHITE_BOX.enqueueMethodForCompilation(method, anotherTier, bci);
 177                 checkNotCompiled();
 178             } else {
 179                 if (!isCompilable) {
 180                     throw new RuntimeException(method
 181                             + " must be compilable at level " + anotherTier
 182                             + ", even if it is not compilable at "
 183                             + testedTier);
 184                 }
 185                 WHITE_BOX.enqueueMethodForCompilation(method, anotherTier, bci);
 186                 checkCompiled();
 187                 deoptimize();
 188             }
 189 
 190             if (!isCompilable(COMP_LEVEL_ANY)) {
 191                 throw new RuntimeException(method
 192                         + " must be compilable at 'CompLevel::CompLevel_any'"
 193                         + ", if it is not compilable only at " + testedTier);
 194             }
 195         }
 196 
 197         // clear state after test
 198         WHITE_BOX.clearMethodState(method);
 199         if (!isCompilable(testedTier)) {
 200             throw new RuntimeException(method
 201                     + " is not compilable after clearMethodState()");
 202         }
 203     }
 204 
 205     private boolean sameCompile(int level1, int level2) {
 206         if (level1 == level2) {
 207             return true;
 208         }
 209         if (isC1Compile(level1) && isC1Compile(level2)) {
 210             return true;
 211         }
 212         if (isC2Compile(level1) && isC2Compile(level2)) {
 213             return true;
 214         }
 215         return false;
 216     }
 217 
 218     private int getBci() {
 219         compile();
 220         checkCompiled();
 221         int result = WHITE_BOX.getMethodEntryBci(method);
 222         deoptimize();
 223         WHITE_BOX.clearMethodState(method);
 224         return result;
 225     }
 226 }