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 *
29 * @run main/othervm -Xcomp -XX:-TieredCompilation
30 *                   -XX:CompileCommand=compileonly,compiler.rangechecks.TestUncommonTrapMerging::test*
31 *                   compiler.rangechecks.TestUncommonTrapMerging Test1
32 * @run main/othervm -XX:CompileCommand=compileonly,compiler.rangechecks.TestUncommonTrapMerging::test*
33 *                   compiler.rangechecks.TestUncommonTrapMerging Test2
34 */
35
36package compiler.rangechecks;
37
38public class TestUncommonTrapMerging {
39
40    public static void main(String[] args) throws Throwable {
41        if (args.length < 1) {
42            throw new RuntimeException("Not enough arguments!");
43        }
44        TestUncommonTrapMerging mytest = new TestUncommonTrapMerging();
45        String testcase = args[0];
46        if (testcase.equals("Test1")) {
47            try {
48                // '42' should hit the 'arg > 0' check
49                mytest.test(42);
50
51            } catch (OutOfMemoryError e) {
52                // expected
53            }
54        } else if (testcase.equals("Test2")) {
55            // Compile test2 with uncommon traps at path 1 and path 2
56            for (int i = 0; i < 100_000; i++) {
57                mytest.test2(-1, 0);
58            }
59
60            // Compile test3 which inlines test2 with uncommon traps at
61            // path 1 and path 2. Because test3 always passes 'value = 1',
62            // C2 will remove the 'value > 0' check and then merge the two
63            // uncommon traps.
64            for (int i = 0; i < 100_000; i++) {
65                mytest.test3(0);
66            }
67
68            // This should return through path 2
69            if (!mytest.test3(42)) {
70                throw new RuntimeException("test2 returned through wrong path!");
71            }
72        }
73    }
74
75    public void test(int arg) throws Throwable {
76        // The following two checks should not be merged if the
77        // uncommon trap of the dominating if has 'Reason_unloaded'
78        // because we need to re-execute both checks after deopt.
79        if (arg < 0) {
80            throw new RuntimeException("Should not reach here");
81        } else if (arg > 0) {
82            throw new OutOfMemoryError();
83        }
84        throw new RuntimeException("Should not reach here");
85    }
86
87    public boolean test2(int arg, int value) {
88        if (arg < 0) {
89            if (value > 0) {
90                // path 1
91                return false;
92            }
93        } else if (arg > 0) {
94            // path 2
95            return true;
96        }
97        // path 3
98        return false;
99    }
100
101    public boolean test3(int arg) {
102        int i;
103        for (i = 0; i < 1; ++i) { }
104        // i == 1
105        return test2(arg, i);
106    }
107}
108