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