1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8140574
  27  * @summary Verify proper re-execution of checks after merging of uncommon traps
  28  * @run main/othervm -Xcomp -XX:-TieredCompilation -XX:CompileCommand=compileonly,TestUncommonTrapMerging::test* TestUncommonTrapMerging Test1
  29  * @run main/othervm -XX:CompileCommand=compileonly,TestUncommonTrapMerging::test* TestUncommonTrapMerging Test2
  30  */
  31 public class TestUncommonTrapMerging {
  32 
  33     public static void main(String[] args) throws Throwable {
  34         if (args.length < 1) {
  35             throw new RuntimeException("Not enough arguments!");
  36         }
  37         TestUncommonTrapMerging mytest = new TestUncommonTrapMerging();
  38         String testcase = args[0];
  39         if (testcase.equals("Test1")) {
  40             try {
  41                 // '42' should hit the 'arg > 0' check
  42                 mytest.test(42);
  43 
  44             } catch (OutOfMemoryError e) {
  45                 // expected
  46             }
  47         } else if (testcase.equals("Test2")) {
  48             // Compile test2 with uncommon traps at path 1 and path 2
  49             for (int i = 0; i < 100_000; i++) {
  50                 mytest.test2(-1, 0);
  51             }
  52 
  53             // Compile test3 which inlines test2 with uncommon traps at
  54             // path 1 and path 2. Because test3 always passes 'value = 1',
  55             // C2 will remove the 'value > 0' check and then merge the two
  56             // uncommon traps.
  57             for (int i = 0; i < 100_000; i++) {
  58                 mytest.test3(0);
  59             }
  60 
  61             // This should return through path 2
  62             if (!mytest.test3(42)) {
  63                 throw new RuntimeException("test2 returned through wrong path!");
  64             }
  65         }
  66     }
  67 
  68     public void test(int arg) throws Throwable {
  69         // The following two checks should not be merged if the
  70         // uncommon trap of the dominating if has 'Reason_unloaded'
  71         // because we need to re-execute both checks after deopt.
  72         if (arg < 0) {
  73             throw new RuntimeException("Should not reach here");
  74         } else if (arg > 0) {
  75             throw new OutOfMemoryError();
  76         }
  77         throw new RuntimeException("Should not reach here");
  78     }
  79 
  80     public boolean test2(int arg, int value) {
  81         if (arg < 0) {
  82             if (value > 0) {
  83                 // path 1
  84                 return false;
  85             }
  86         } else if (arg > 0) {
  87             // path 2
  88             return true;
  89         }
  90         // path 3
  91         return false;
  92     }
  93 
  94     public boolean test3(int arg) {
  95         int i;
  96         for (i = 0; i < 1; ++i) { }
  97         // i == 1
  98         return test2(arg, i);
  99     }
 100 }