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