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