1 /*
   2  * Copyright (c) 2017, 2019, 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.valhalla.valuetypes;
  25 
  26 import compiler.whitebox.CompilerWhiteBoxTest;
  27 import jdk.test.lib.Asserts;
  28 import jdk.test.lib.management.InputArguments;
  29 import jdk.test.lib.Platform;
  30 import jdk.test.lib.process.ProcessTools;
  31 import jdk.test.lib.process.OutputAnalyzer;
  32 import jdk.test.lib.Utils;
  33 import sun.hotspot.WhiteBox;
  34 
  35 import java.lang.annotation.Retention;
  36 import java.lang.annotation.RetentionPolicy;
  37 import java.lang.annotation.Repeatable;
  38 import java.lang.invoke.*;
  39 import java.lang.reflect.Method;
  40 import java.util.ArrayList;
  41 import java.util.Arrays;
  42 import java.util.Hashtable;
  43 import java.util.LinkedHashMap;
  44 import java.util.List;
  45 import java.util.Map;
  46 import java.util.regex.Matcher;
  47 import java.util.regex.Pattern;
  48 import java.util.TreeMap;
  49 
  50 // Mark method as test
  51 @Retention(RetentionPolicy.RUNTIME)
  52 @Repeatable(Tests.class)
  53 @interface Test {
  54     // Regular expression used to match forbidden IR nodes
  55     // in the C2 IR emitted for this test.
  56     String failOn() default "";
  57     // Regular expressions used to match and count IR nodes.
  58     String[] match() default { };
  59     int[] matchCount() default { };
  60     int compLevel() default ValueTypeTest.COMP_LEVEL_ANY;
  61     int valid() default ValueTypeTest.AllFlags;
  62 }
  63 
  64 @Retention(RetentionPolicy.RUNTIME)
  65 @interface Tests {
  66     Test[] value();
  67 }
  68 
  69 // Force method inlining during compilation
  70 @Retention(RetentionPolicy.RUNTIME)
  71 @interface ForceInline { }
  72 
  73 // Prevent method inlining during compilation
  74 @Retention(RetentionPolicy.RUNTIME)
  75 @interface DontInline { }
  76 
  77 // Prevent method compilation
  78 @Retention(RetentionPolicy.RUNTIME)
  79 @interface DontCompile { }
  80 
  81 // Force method compilation
  82 @Retention(RetentionPolicy.RUNTIME)
  83 @interface ForceCompile {
  84     int compLevel() default ValueTypeTest.COMP_LEVEL_ANY;
  85 }
  86 
  87 // Number of warmup iterations
  88 @Retention(RetentionPolicy.RUNTIME)
  89 @interface Warmup {
  90     int value();
  91 }
  92 
  93 // Do not enqueue the test method for compilation immediately after warmup loops have finished. Instead
  94 // let the test method be compiled with on-stack-replacement.
  95 @Retention(RetentionPolicy.RUNTIME)
  96 @interface OSRCompileOnly {}
  97 
  98 // Skip this test temporarily for C1 testing
  99 @Retention(RetentionPolicy.RUNTIME)
 100 @interface TempSkipForC1 {
 101     String reason() default "";
 102 }
 103 
 104 public abstract class ValueTypeTest {
 105     protected static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
 106 
 107     // Currently C1 is disabled by default. To test C1, run "jtreg -vmoptions:-XX:+EnableValhallaC1 -vmoptions:-XX:TieredStopAtLevel=1".
 108     // This forces C1 to be use for all methods that are compiled, @Test(compLevel=?) setting.
 109     static final boolean TEST_C1 = (Boolean)WHITE_BOX.getVMFlag("EnableValhallaC1");
 110 
 111     // Should we execute tests that assume (ValueType[] <: Object[])?
 112     static final boolean ENABLE_VALUE_ARRAY_COVARIANCE = Boolean.getBoolean("ValueArrayCovariance");
 113 
 114     // Random test values
 115     public static final int  rI = Utils.getRandomInstance().nextInt() % 1000;
 116     public static final long rL = Utils.getRandomInstance().nextLong() % 1000;
 117 
 118     // User defined settings
 119     protected static final boolean XCOMP = Platform.isComp();
 120     private static final boolean PRINT_GRAPH = true;
 121     protected static final boolean VERBOSE = Boolean.parseBoolean(System.getProperty("Verbose", "false"));
 122     private static final boolean PRINT_TIMES = Boolean.parseBoolean(System.getProperty("PrintTimes", "false"));
 123     private static       boolean VERIFY_IR = Boolean.parseBoolean(System.getProperty("VerifyIR", "true")) && !TEST_C1 && !XCOMP;
 124     private static final boolean VERIFY_VM = Boolean.parseBoolean(System.getProperty("VerifyVM", "false"));
 125     private static final String SCENARIOS = System.getProperty("Scenarios", "");
 126     private static final String TESTLIST = System.getProperty("Testlist", "");
 127     private static final String EXCLUDELIST = System.getProperty("Exclude", "");
 128     private static final int WARMUP = Integer.parseInt(System.getProperty("Warmup", "251"));
 129     private static final boolean DUMP_REPLAY = Boolean.parseBoolean(System.getProperty("DumpReplay", "false"));
 130     protected static final boolean FLIP_C1_C2 = Boolean.parseBoolean(System.getProperty("FlipC1C2", "false"));
 131     protected static final boolean GCAfter = Boolean.parseBoolean(System.getProperty("GCAfter", "false"));
 132     private static final int OSRTestTimeOut = Integer.parseInt(System.getProperty("OSRTestTimeOut", "-1"));
 133 
 134     // "jtreg -DXcomp=true" runs all the scenarios with -Xcomp. This is faster than "jtreg -javaoptions:-Xcomp".
 135     protected static final boolean RUN_SCENARIOS_WITH_XCOMP = Boolean.parseBoolean(System.getProperty("Xcomp", "false"));
 136 
 137     // Pre-defined settings
 138     private static final String[] defaultFlags = {
 139         "-XX:-BackgroundCompilation",
 140         "-XX:CompileCommand=quiet",
 141         "-XX:CompileCommand=compileonly,java.lang.invoke.*::*",
 142         "-XX:CompileCommand=compileonly,java.lang.Long::sum",
 143         "-XX:CompileCommand=compileonly,java.lang.Object::<init>",
 144         "-XX:CompileCommand=inline,compiler.valhalla.valuetypes.MyValue*::<init>",
 145         "-XX:CompileCommand=compileonly,compiler.valhalla.valuetypes.*::*"};
 146     private static final String[] printFlags = {
 147         "-XX:+PrintCompilation", "-XX:+PrintIdeal", "-XX:+UnlockDiagnosticVMOptions", "-XX:+PrintOptoAssembly"};
 148     private static final String[] verifyFlags = {
 149         "-XX:+VerifyOops", "-XX:+VerifyStack", "-XX:+VerifyLastFrame", "-XX:+VerifyBeforeGC", "-XX:+VerifyAfterGC",
 150         "-XX:+VerifyDuringGC", "-XX:+VerifyAdapterSharing"};
 151 
 152     protected static final int ValueTypePassFieldsAsArgsOn = 0x1;
 153     protected static final int ValueTypePassFieldsAsArgsOff = 0x2;
 154     protected static final int ValueTypeArrayFlattenOn = 0x4;
 155     protected static final int ValueTypeArrayFlattenOff = 0x8;
 156     protected static final int ValueTypeReturnedAsFieldsOn = 0x10;
 157     protected static final int ValueTypeReturnedAsFieldsOff = 0x20;
 158     protected static final int AlwaysIncrementalInlineOn = 0x40;
 159     protected static final int AlwaysIncrementalInlineOff = 0x80;
 160     protected static final int G1GCOn = 0x100;
 161     protected static final int G1GCOff = 0x200;
 162     static final int AllFlags = ValueTypePassFieldsAsArgsOn | ValueTypePassFieldsAsArgsOff | ValueTypeArrayFlattenOn | ValueTypeArrayFlattenOff | ValueTypeReturnedAsFieldsOn;
 163     protected static final boolean ValueTypePassFieldsAsArgs = (Boolean)WHITE_BOX.getVMFlag("ValueTypePassFieldsAsArgs");
 164     protected static final boolean ValueTypeArrayFlatten = (WHITE_BOX.getIntxVMFlag("ValueArrayElemMaxFlatSize") == -1); // FIXME - fix this if default of ValueArrayElemMaxFlatSize is changed
 165     protected static final boolean ValueTypeReturnedAsFields = (Boolean)WHITE_BOX.getVMFlag("ValueTypeReturnedAsFields");
 166     protected static final boolean AlwaysIncrementalInline = (Boolean)WHITE_BOX.getVMFlag("AlwaysIncrementalInline");
 167     protected static final boolean G1GC = (Boolean)WHITE_BOX.getVMFlag("UseG1GC");
 168     protected static final long TieredStopAtLevel = (Long)WHITE_BOX.getVMFlag("TieredStopAtLevel");
 169     protected static final boolean VerifyOops = (Boolean)WHITE_BOX.getVMFlag("VerifyOops");
 170     protected static final int COMP_LEVEL_ANY               = -2;
 171     protected static final int COMP_LEVEL_ALL               = -2;
 172     protected static final int COMP_LEVEL_AOT               = -1;
 173     protected static final int COMP_LEVEL_NONE              =  0;
 174     protected static final int COMP_LEVEL_SIMPLE            =  1;     // C1
 175     protected static final int COMP_LEVEL_LIMITED_PROFILE   =  2;     // C1, invocation & backedge counters
 176     protected static final int COMP_LEVEL_FULL_PROFILE      =  3;     // C1, invocation & backedge counters + mdo
 177     protected static final int COMP_LEVEL_FULL_OPTIMIZATION =  4;     // C2 or JVMCI
 178 
 179     protected static final Hashtable<String, Method> tests = new Hashtable<String, Method>();
 180     protected static final boolean USE_COMPILER = WHITE_BOX.getBooleanVMFlag("UseCompiler");
 181     protected static final boolean PRINT_IDEAL  = WHITE_BOX.getBooleanVMFlag("PrintIdeal");
 182 
 183     // Regular expressions used to match nodes in the PrintIdeal output
 184     protected static final String START = "(\\d+\\t(.*";
 185     protected static final String MID = ".*)+\\t===.*";
 186     protected static final String END = ")|";
 187     // Generic allocation
 188     protected static final String ALLOC_G  = "(.*call,static  wrapper for: _new_instance_Java" + END;
 189     protected static final String ALLOCA_G = "(.*call,static  wrapper for: _new_array_Java" + END;
 190     // Value type allocation
 191     protected static final String ALLOC  = "(.*precise klass compiler/valhalla/valuetypes/MyValue.*\\R(.*(nop|spill).*\\R)*.*_new_instance_Java" + END;
 192     protected static final String ALLOCA = "(.*precise klass \\[Lcompiler/valhalla/valuetypes/MyValue.*\\R(.*(nop|spill).*\\R)*.*_new_array_Java" + END;
 193     protected static final String LOAD   = START + "Load(B|S|I|L|F|D|P|N)" + MID + "@compiler/valhalla/valuetypes/MyValue.*" + END;
 194     protected static final String LOADK  = START + "LoadK" + MID + END;
 195     protected static final String STORE  = START + "Store(B|C|S|I|L|F|D|P|N)" + MID + "@compiler/valhalla/valuetypes/MyValue.*" + END;
 196     protected static final String LOOP   = START + "Loop" + MID + "" + END;
 197     protected static final String COUNTEDLOOP = START + "CountedLoop\\b" + MID + "" + END;
 198     protected static final String TRAP   = START + "CallStaticJava" + MID + "uncommon_trap.*(unstable_if|predicate)" + END;
 199     protected static final String RETURN = START + "Return" + MID + "returns" + END;
 200     protected static final String LINKTOSTATIC = START + "CallStaticJava" + MID + "linkToStatic" + END;
 201     protected static final String NPE = START + "CallStaticJava" + MID + "null_check" + END;
 202     protected static final String CALL = START + "CallStaticJava" + MID + END;
 203     protected static final String STOREVALUETYPEFIELDS = START + "CallStaticJava" + MID + "store_value_type_fields" + END;
 204     protected static final String SCOBJ = "(.*# ScObj.*" + END;
 205     protected static final String LOAD_UNKNOWN_VALUE = "(.*call_leaf,runtime  load_unknown_value.*" + END;
 206     protected static final String STORE_UNKNOWN_VALUE = "(.*call_leaf,runtime  store_unknown_value.*" + END;
 207     protected static final String VALUE_ARRAY_NULL_GUARD = "(.*call,static  wrapper for: uncommon_trap.*reason='null_check' action='none'.*" + END;
 208     protected static final String STORAGE_PROPERTY_CLEARING = "(.*((int:536870911)|(salq.*3\\R.*sarq.*3)).*" + END;
 209 
 210     public static String[] concat(String prefix[], String... extra) {
 211         ArrayList<String> list = new ArrayList<String>();
 212         if (prefix != null) {
 213             for (String s : prefix) {
 214                 list.add(s);
 215             }
 216         }
 217         if (extra != null) {
 218             for (String s : extra) {
 219                 list.add(s);
 220             }
 221         }
 222 
 223         return list.toArray(new String[list.size()]);
 224     }
 225 
 226     /**
 227      * Override getNumScenarios and getVMParameters if you want to run with more than
 228      * the 6 built-in scenarios
 229      */
 230     public int getNumScenarios() {
 231         return 6;
 232     }
 233 
 234     /**
 235      * VM paramaters for the 5 built-in test scenarios. If your test needs to append
 236      * extra parameters for (some of) these scenarios, override getExtraVMParameters().
 237      */
 238     public String[] getVMParameters(int scenario) {
 239         switch (scenario) {
 240         case 0: return new String[] {
 241                 "-XX:+AlwaysIncrementalInline",
 242                 "-XX:ValueArrayElemMaxFlatOops=5",
 243                 "-XX:ValueArrayElemMaxFlatSize=-1",
 244                 "-XX:ValueFieldMaxFlatSize=-1",
 245                 "-XX:+ValueTypePassFieldsAsArgs",
 246                 "-XX:+ValueTypeReturnedAsFields"};
 247         case 1: return new String[] {
 248                 "-XX:-UseCompressedOops",
 249                 "-XX:ValueArrayElemMaxFlatOops=5",
 250                 "-XX:ValueArrayElemMaxFlatSize=-1",
 251                 "-XX:ValueFieldMaxFlatSize=-1",
 252                 "-XX:-ValueTypePassFieldsAsArgs",
 253                 "-XX:-ValueTypeReturnedAsFields"};
 254         case 2: return new String[] {
 255                 "-XX:-UseCompressedOops",
 256                 "-XX:ValueArrayElemMaxFlatOops=0",
 257                 "-XX:ValueArrayElemMaxFlatSize=0",
 258                 "-XX:ValueFieldMaxFlatSize=-1",
 259                 "-XX:+ValueTypePassFieldsAsArgs",
 260                 "-XX:+ValueTypeReturnedAsFields",
 261                 "-XX:+StressValueTypeReturnedAsFields"};
 262         case 3: return new String[] {
 263                 "-DVerifyIR=false",
 264                 "-XX:+AlwaysIncrementalInline",
 265                 "-XX:ValueArrayElemMaxFlatOops=0",
 266                 "-XX:ValueArrayElemMaxFlatSize=0",
 267                 "-XX:ValueFieldMaxFlatSize=0",
 268                 "-XX:+ValueTypePassFieldsAsArgs",
 269                 "-XX:+ValueTypeReturnedAsFields"};
 270         case 4: return new String[] {
 271                 "-DVerifyIR=false",
 272                 "-XX:ValueArrayElemMaxFlatOops=-1",
 273                 "-XX:ValueArrayElemMaxFlatSize=-1",
 274                 "-XX:ValueFieldMaxFlatSize=0",
 275                 "-XX:+ValueTypePassFieldsAsArgs",
 276                 "-XX:-ValueTypeReturnedAsFields",
 277                 "-XX:-ReduceInitialCardMarks"};
 278         case 5: return new String[] {
 279                 "-XX:+AlwaysIncrementalInline",
 280                 "-XX:ValueArrayElemMaxFlatOops=5",
 281                 "-XX:ValueArrayElemMaxFlatSize=-1",
 282                 "-XX:ValueFieldMaxFlatSize=-1",
 283                 "-XX:-ValueTypePassFieldsAsArgs",
 284                 "-XX:-ValueTypeReturnedAsFields"};
 285         }
 286         return null;
 287     }
 288 
 289     /**
 290      * Override this method and return a non-null reason if the given scenario should be
 291      * ignored (due to an existing bug, etc).
 292      */
 293     String isScenarioIgnored(int scenario) {
 294         return null;
 295     }
 296 
 297     /**
 298      * Override this method to provide extra parameters for selected scenarios
 299      */
 300     public String[] getExtraVMParameters(int scenario) {
 301         return null;
 302     }
 303 
 304     public static void main(String[] args) throws Throwable {
 305         if (args.length != 1) {
 306             throw new RuntimeException("Usage: @run main/othervm/timeout=120 -Xbootclasspath/a:." +
 307                                        " -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions" +
 308                                        " -XX:+UnlockExperimentalVMOptions -XX:+WhiteBoxAPI" +
 309                                        " compiler.valhalla.valuetypes.ValueTypeTest <YourTestMainClass>");
 310         }
 311         String testMainClassName = args[0];
 312         Class testMainClass = Class.forName(testMainClassName);
 313         ValueTypeTest test = (ValueTypeTest)testMainClass.newInstance();
 314         List<String> scenarios = null;
 315         if (!SCENARIOS.isEmpty()) {
 316            scenarios = Arrays.asList(SCENARIOS.split(","));
 317         }
 318         for (int i=0; i<test.getNumScenarios(); i++) {
 319             String reason;
 320             if ((reason = test.isScenarioIgnored(i)) != null) {
 321                 System.out.println("Scenario #" + i + " is ignored: " + reason);
 322             } else if (scenarios != null && !scenarios.contains(Integer.toString(i))) {
 323                 System.out.println("Scenario #" + i + " is skipped due to -Dscenarios=" + SCENARIOS);
 324             } else {
 325                 System.out.println("Scenario #" + i + " -------- ");
 326                 String[] cmds = InputArguments.getVmInputArgs();
 327                 if (RUN_SCENARIOS_WITH_XCOMP) {
 328                     cmds = concat(cmds, "-Xcomp");
 329                 }
 330                 cmds = concat(cmds, test.getVMParameters(i));
 331                 cmds = concat(cmds, test.getExtraVMParameters(i));
 332                 cmds = concat(cmds, testMainClassName);
 333 
 334                 OutputAnalyzer oa = ProcessTools.executeTestJvm(cmds);
 335                 String output = oa.getOutput();
 336                 oa.shouldHaveExitValue(0);
 337                 System.out.println(output);
 338             }
 339         }
 340     }
 341 
 342     // To exclude test cases, use -DExclude=<case1>,<case2>,...
 343     // Each case can be just the method name, or can be <class>.<method>. The latter form is useful
 344     // when you are running several tests at the same time.
 345     //
 346     // jtreg -DExclude=test12 TestArrays.java
 347     // jtreg -DExclude=test34 TestLWorld.java
 348     // -- or --
 349     // jtreg -DExclude=TestArrays.test12,TestLWorld.test34 TestArrays.java TestLWorld.java
 350     //
 351     private List<String> buildExcludeList() {
 352         List<String> exclude = null;
 353         String classPrefix = getClass().getSimpleName() + ".";
 354         if (!EXCLUDELIST.isEmpty()) {
 355             exclude = new ArrayList(Arrays.asList(EXCLUDELIST.split(",")));
 356             for (int i = exclude.size() - 1; i >= 0; i--) {
 357                 String ex = exclude.get(i);
 358                 if (ex.indexOf(".") > 0) {
 359                     if (ex.startsWith(classPrefix)) {
 360                         ex = ex.substring(classPrefix.length());
 361                         exclude.set(i, ex);
 362                     } else {
 363                         exclude.remove(i);
 364                     }
 365                 }
 366             }
 367         }
 368         return exclude;
 369     }
 370 
 371     protected ValueTypeTest() {
 372         List<String> list = null;
 373         if (!TESTLIST.isEmpty()) {
 374            list = Arrays.asList(TESTLIST.split(","));
 375         }
 376         List<String> exclude = buildExcludeList();
 377 
 378         // Gather all test methods and put them in Hashtable
 379         for (Method m : getClass().getDeclaredMethods()) {
 380             Test[] annos = m.getAnnotationsByType(Test.class);
 381             if (annos.length != 0 &&
 382                 ((list == null || list.contains(m.getName())) && (exclude == null || !exclude.contains(m.getName())))) {
 383                 tests.put(getClass().getSimpleName() + "::" + m.getName(), m);
 384             }
 385         }
 386     }
 387 
 388     protected void run(String[] args, Class<?>... classes) throws Throwable {
 389         if (args.length == 0) {
 390             // Spawn a new VM instance
 391             execute_vm();
 392         } else {
 393             // Execute tests in the VM spawned by the above code.
 394             Asserts.assertTrue(args.length == 1 && args[0].equals("run"), "must be");
 395             run(classes);
 396         }
 397     }
 398 
 399     private void execute_vm() throws Throwable {
 400         Asserts.assertFalse(tests.isEmpty(), "no tests to execute");
 401         String[] vmInputArgs = InputArguments.getVmInputArgs();
 402         for (String arg : vmInputArgs) {
 403             if (arg.startsWith("-XX:CompileThreshold")) {
 404                 // Disable IR verification if non-default CompileThreshold is set
 405                 VERIFY_IR = false;
 406             }
 407             if (arg.startsWith("-XX:+EnableValhallaC1")) {
 408                 // Disable IR verification if C1 is used (FIXME!)
 409                 VERIFY_IR = false;
 410             }
 411         }
 412         // Each VM is launched with flags in this order, so the later ones can override the earlier one:
 413         //     VERIFY_IR/VERIFY_VM flags specified below
 414         //     vmInputArgs, which consists of:
 415         //        @run options
 416         //        getVMParameters()
 417         //        getExtraVMParameters()
 418         //     defaultFlags
 419         String cmds[] = null;
 420         if (VERIFY_IR) {
 421             // Add print flags for IR verification
 422             cmds = concat(cmds, printFlags);
 423             // Always trap for exception throwing to not confuse IR verification
 424             cmds = concat(cmds, "-XX:-OmitStackTraceInFastThrow");
 425         }
 426         if (VERIFY_VM) {
 427             cmds = concat(cmds, verifyFlags);
 428         }
 429         cmds = concat(cmds, vmInputArgs);
 430         cmds = concat(cmds, defaultFlags);
 431 
 432         // Run tests in own process and verify output
 433         cmds = concat(cmds, getClass().getName(), "run");
 434         OutputAnalyzer oa = ProcessTools.executeTestJvm(cmds);
 435         // If ideal graph printing is enabled/supported, verify output
 436         String output = oa.getOutput();
 437         oa.shouldHaveExitValue(0);
 438         if (VERIFY_IR) {
 439             if (output.contains("PrintIdeal enabled")) {
 440                 parseOutput(output);
 441             } else {
 442                 System.out.println(output);
 443                 System.out.println("WARNING: IR verification failed! Running with -Xint, -Xcomp or release build?");
 444             }
 445         }
 446     }
 447 
 448     private void parseOutput(String output) throws Exception {
 449         Pattern comp_re = Pattern.compile("\\n\\s+\\d+\\s+\\d+\\s+(%| )(s| )(!| )b(n| )\\s+\\S+\\.(?<name>[^.]+::\\S+)\\s+(?<osr>@ \\d+\\s+)?[(]\\d+ bytes[)]\\n");
 450         Matcher m = comp_re.matcher(output);
 451         Map<String,String> compilations = new LinkedHashMap<>();
 452         int prev = 0;
 453         String methodName = null;
 454         while (m.find()) {
 455             if (prev == 0) {
 456                 // Print header
 457                 System.out.print(output.substring(0, m.start()+1));
 458             } else if (methodName != null) {
 459                 compilations.put(methodName, output.substring(prev, m.start()+1));
 460             }
 461             if (m.group("osr") != null) {
 462                 methodName = null;
 463             } else {
 464                 methodName = m.group("name");
 465             }
 466             prev = m.end();
 467         }
 468         if (prev == 0) {
 469             // Print header
 470             System.out.print(output);
 471         } else if (methodName != null) {
 472             compilations.put(methodName, output.substring(prev));
 473         }
 474         // Iterate over compilation output
 475         for (String testName : compilations.keySet()) {
 476             Method test = tests.get(testName);
 477             if (test == null) {
 478                 // Skip helper methods
 479                 continue;
 480             }
 481             String graph = compilations.get(testName);
 482             if (PRINT_GRAPH) {
 483                 System.out.println("\nGraph for " + testName + "\n" + graph);
 484             }
 485             // Parse graph using regular expressions to determine if it contains forbidden nodes
 486             Test[] annos = test.getAnnotationsByType(Test.class);
 487             Test anno = null;
 488             for (Test a : annos) {
 489                 if ((a.valid() & ValueTypePassFieldsAsArgsOn) != 0 && ValueTypePassFieldsAsArgs) {
 490                     assert anno == null;
 491                     anno = a;
 492                 } else if ((a.valid() & ValueTypePassFieldsAsArgsOff) != 0 && !ValueTypePassFieldsAsArgs) {
 493                     assert anno == null;
 494                     anno = a;
 495                 } else if ((a.valid() & ValueTypeArrayFlattenOn) != 0 && ValueTypeArrayFlatten) {
 496                     assert anno == null;
 497                     anno = a;
 498                 } else if ((a.valid() & ValueTypeArrayFlattenOff) != 0 && !ValueTypeArrayFlatten) {
 499                     assert anno == null;
 500                     anno = a;
 501                 } else if ((a.valid() & ValueTypeReturnedAsFieldsOn) != 0 && ValueTypeReturnedAsFields) {
 502                     assert anno == null;
 503                     anno = a;
 504                 } else if ((a.valid() & ValueTypeReturnedAsFieldsOff) != 0 && !ValueTypeReturnedAsFields) {
 505                     assert anno == null;
 506                     anno = a;
 507                 } else if ((a.valid() & AlwaysIncrementalInlineOn) != 0 && AlwaysIncrementalInline) {
 508                     assert anno == null;
 509                     anno = a;
 510                 } else if ((a.valid() & AlwaysIncrementalInlineOff) != 0 && !AlwaysIncrementalInline) {
 511                     assert anno == null;
 512                     anno = a;
 513                 } else if ((a.valid() & G1GCOn) != 0 && G1GC) {
 514                     assert anno == null;
 515                     anno = a;
 516                 } else if ((a.valid() & G1GCOff) != 0 && !G1GC) {
 517                     assert anno == null;
 518                     anno = a;
 519                 }
 520             }
 521             assert anno != null;
 522             String regexFail = anno.failOn();
 523             if (!regexFail.isEmpty()) {
 524                 Pattern pattern = Pattern.compile(regexFail.substring(0, regexFail.length()-1));
 525                 Matcher matcher = pattern.matcher(graph);
 526                 boolean found = matcher.find();
 527                 Asserts.assertFalse(found, "Graph for '" + testName + "' contains forbidden node:\n" + (found ? matcher.group() : ""));
 528             }
 529             String[] regexMatch = anno.match();
 530             int[] matchCount = anno.matchCount();
 531             for (int i = 0; i < regexMatch.length; ++i) {
 532                 Pattern pattern = Pattern.compile(regexMatch[i].substring(0, regexMatch[i].length()-1));
 533                 Matcher matcher = pattern.matcher(graph);
 534                 int count = 0;
 535                 String nodes = "";
 536                 while (matcher.find()) {
 537                     count++;
 538                     nodes += matcher.group() + "\n";
 539                 }
 540                 if (matchCount[i] < 0) {
 541                     Asserts.assertLTE(Math.abs(matchCount[i]), count, "Graph for '" + testName + "' contains different number of match nodes:\n" + nodes);
 542                 } else {
 543                     Asserts.assertEQ(matchCount[i], count, "Graph for '" + testName + "' contains different number of match nodes:\n" + nodes);
 544                 }
 545             }
 546             tests.remove(testName);
 547             System.out.println(testName + " passed");
 548         }
 549         // Check if all tests were compiled
 550         if (tests.size() != 0) {
 551             for (String name : tests.keySet()) {
 552                 System.out.println("Test '" + name + "' not compiled!");
 553             }
 554             throw new RuntimeException("Not all tests were compiled");
 555         }
 556     }
 557 
 558     private void setup(Class<?> clazz) {
 559         if (XCOMP) {
 560             // Don't control compilation if -Xcomp is enabled
 561             return;
 562         }
 563         if (DUMP_REPLAY) {
 564             // Generate replay compilation files
 565             String directive = "[{ match: \"*.*\", DumpReplay: true }]";
 566             if (WHITE_BOX.addCompilerDirective(directive) != 1) {
 567                 throw new RuntimeException("Failed to add compiler directive");
 568             }
 569         }
 570 
 571         Method[] methods = clazz.getDeclaredMethods();
 572         for (Method m : methods) {
 573             if (m.isAnnotationPresent(Test.class)) {
 574                 // Don't inline tests
 575                 WHITE_BOX.testSetDontInlineMethod(m, true);
 576             }
 577             if (m.isAnnotationPresent(DontCompile.class)) {
 578                 WHITE_BOX.makeMethodNotCompilable(m, COMP_LEVEL_ANY, true);
 579                 WHITE_BOX.makeMethodNotCompilable(m, COMP_LEVEL_ANY, false);
 580                 WHITE_BOX.testSetDontInlineMethod(m, true);
 581             } else if (m.isAnnotationPresent(ForceCompile.class)) {
 582                 int compLevel = getCompLevel(m.getAnnotation(ForceCompile.class));
 583                 enqueueMethodForCompilation(m, compLevel);
 584             }
 585             if (m.isAnnotationPresent(ForceInline.class)) {
 586                 WHITE_BOX.testSetForceInlineMethod(m, true);
 587             } else if (m.isAnnotationPresent(DontInline.class)) {
 588                 WHITE_BOX.testSetDontInlineMethod(m, true);
 589             }
 590         }
 591 
 592         // Compile class initializers
 593         int compLevel = getCompLevel(null);
 594         WHITE_BOX.enqueueInitializerForCompilation(clazz, compLevel);
 595     }
 596 
 597     private void run(Class<?>... classes) throws Exception {
 598         if (USE_COMPILER && PRINT_IDEAL && !XCOMP) {
 599             System.out.println("PrintIdeal enabled");
 600         }
 601         System.out.format("rI = %d, rL = %d\n", rI, rL);
 602 
 603         setup(getClass());
 604         for (Class<?> clazz : classes) {
 605             setup(clazz);
 606         }
 607 
 608         // Execute tests
 609         TreeMap<Long, String> durations = (PRINT_TIMES || VERBOSE) ? new TreeMap<Long, String>() : null;
 610         for (Method test : tests.values()) {
 611             if (VERBOSE) {
 612                 System.out.println("Starting " + test.getName());
 613             }
 614             TempSkipForC1 c1skip = test.getAnnotation(TempSkipForC1.class);
 615             if (TEST_C1 && c1skip != null) {
 616                 System.out.println("Skipped " + test.getName() + " for C1 testing: " + c1skip.reason());
 617                 continue;
 618             }
 619             long startTime = System.nanoTime();
 620             Method verifier = getClass().getMethod(test.getName() + "_verifier", boolean.class);
 621             // Warmup using verifier method
 622             Warmup anno = test.getAnnotation(Warmup.class);
 623             int warmup = anno == null ? WARMUP : anno.value();
 624             for (int i = 0; i < warmup; ++i) {
 625                 verifier.invoke(this, true);
 626             }
 627             boolean osrOnly = (test.getAnnotation(OSRCompileOnly.class) != null);
 628 
 629             // C1 generates a lot of code when VerifyOops is enabled and may run out of space (for a small
 630             // number of test cases).
 631             boolean maybeCodeBufferOverflow = (TEST_C1 && VerifyOops);
 632 
 633             if (osrOnly) {
 634                 long started = System.currentTimeMillis();
 635                 boolean stateCleared = false;
 636                 for (;;)  {
 637                     long elapsed = System.currentTimeMillis() - started;
 638                     if (maybeCodeBufferOverflow && elapsed > 5000 && !WHITE_BOX.isMethodCompiled(test, false)) {
 639                         System.out.println("Temporarily disabling VerifyOops");
 640                         try {
 641                             WHITE_BOX.setBooleanVMFlag("VerifyOops", false);
 642                             if (!stateCleared) {
 643                                 WHITE_BOX.clearMethodState(test);
 644                                 stateCleared = true;
 645                             }
 646                             verifier.invoke(this, false);
 647                         } finally {
 648                             WHITE_BOX.setBooleanVMFlag("VerifyOops", true);
 649                             System.out.println("Re-enabled VerifyOops");
 650                         }
 651                     } else {
 652                         verifier.invoke(this, false);
 653                     }
 654 
 655                     boolean b = WHITE_BOX.isMethodCompiled(test, false);
 656                     if (VERBOSE) {
 657                         System.out.println("Is " + test.getName() + " compiled? " + b);
 658                     }
 659                     if (b || XCOMP || !USE_COMPILER) {
 660                         // Don't control compilation if -Xcomp is enabled, or if compiler is disabled
 661                         break;
 662                     }
 663                     if (OSRTestTimeOut > 0 && elapsed > OSRTestTimeOut) {
 664                         break;
 665                     }
 666                 }
 667                 if (!XCOMP) {
 668                     Asserts.assertTrue(!USE_COMPILER || WHITE_BOX.isMethodCompiled(test, false), test + " not compiled");
 669                 }
 670             } else {
 671                 int compLevel = getCompLevel(test.getAnnotation(Test.class));
 672                 // Trigger compilation
 673                 enqueueMethodForCompilation(test, compLevel);
 674                 if (maybeCodeBufferOverflow && !WHITE_BOX.isMethodCompiled(test, false)) {
 675                     // Let's disable VerifyOops temporarily and retry.
 676                     WHITE_BOX.setBooleanVMFlag("VerifyOops", false);
 677                     WHITE_BOX.clearMethodState(test);
 678                     enqueueMethodForCompilation(test, compLevel);
 679                     WHITE_BOX.setBooleanVMFlag("VerifyOops", true);
 680                 }
 681                 Asserts.assertTrue(!USE_COMPILER || WHITE_BOX.isMethodCompiled(test, false), test + " not compiled");
 682                 // Check result
 683                 verifier.invoke(this, false);
 684             }
 685             if (PRINT_TIMES || VERBOSE) {
 686                 long endTime = System.nanoTime();
 687                 long duration = (endTime - startTime);
 688                 durations.put(duration, test.getName());
 689                 if (VERBOSE) {
 690                     System.out.println("Done " + test.getName() + ": " + duration + " ns = " + (duration / 1000000) + " ms");
 691                 }
 692             }
 693             if (GCAfter) {
 694                 System.out.println("doing GC");
 695                 System.gc();
 696             }
 697         }
 698 
 699         // Print execution times
 700         if (PRINT_TIMES) {
 701           System.out.println("\n\nTest execution times:");
 702           for (Map.Entry<Long, String> entry : durations.entrySet()) {
 703               System.out.format("%-10s%15d ns\n", entry.getValue() + ":", entry.getKey());
 704           }
 705         }
 706     }
 707 
 708     // Get the appropriate compilation level for a method, according to the
 709     // given annotation, as well as the current test scenario and VM options.
 710     //
 711     private int getCompLevel(Object annotation) {
 712         int compLevel;
 713         if (annotation == null) {
 714             compLevel = COMP_LEVEL_ANY;
 715         } else if (annotation instanceof Test) {
 716             compLevel = ((Test)annotation).compLevel();
 717         } else {
 718             compLevel = ((ForceCompile)annotation).compLevel();
 719         }
 720 
 721         return restrictCompLevel(compLevel);
 722     }
 723 
 724     // Get the appropriate level as permitted by the test scenario and VM options.
 725     private static int restrictCompLevel(int compLevel) {
 726         if (compLevel == COMP_LEVEL_ANY) {
 727             compLevel = COMP_LEVEL_FULL_OPTIMIZATION;
 728         }
 729         if (FLIP_C1_C2) {
 730             // Effectively treat all (compLevel = C1) as (compLevel = C2), and
 731             //                       (compLevel = C2) as (compLevel = C1).
 732             if (compLevel == COMP_LEVEL_SIMPLE) {
 733                 compLevel = COMP_LEVEL_FULL_OPTIMIZATION;
 734             } else if (compLevel == COMP_LEVEL_FULL_OPTIMIZATION) {
 735                 compLevel = COMP_LEVEL_SIMPLE;
 736             }
 737         }
 738         if (!TEST_C1 && compLevel < COMP_LEVEL_FULL_OPTIMIZATION) {
 739             compLevel = COMP_LEVEL_FULL_OPTIMIZATION;
 740         }
 741         if (compLevel > (int)TieredStopAtLevel) {
 742             compLevel = (int)TieredStopAtLevel;
 743         }
 744         return compLevel;
 745     }
 746 
 747     public static void enqueueMethodForCompilation(Method m, int level) {
 748         level = restrictCompLevel(level);
 749         if (VERBOSE) {
 750             System.out.println("enqueueMethodForCompilation " + m + ", level = " + level);
 751         }
 752         WHITE_BOX.enqueueMethodForCompilation(m, level);
 753     }
 754 }