1/*
2 * Copyright (c) 2017, 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 8172751
27 * @summary OSR compilation at unreachable bci causes C1 crash
28 *
29 * @run main/othervm -XX:-BackgroundCompilation compiler.c1.Test8172751
30 */
31
32package compiler.c1;
33
34import java.lang.invoke.MethodHandle;
35import java.lang.invoke.MethodHandles;
36import java.lang.invoke.MutableCallSite;
37
38public class Test8172751 {
39    private static final MethodHandle CONSTANT_TRUE = MethodHandles.constant(boolean.class, true);
40    private static final MethodHandle CONSTANT_FALSE = MethodHandles.constant(boolean.class, false);
41    private static final MutableCallSite CALL_SITE = new MutableCallSite(CONSTANT_FALSE);
42    private static final int LIMIT = 1_000_000;
43    private static volatile int counter;
44
45    private static boolean doSomething() {
46        return counter++ < LIMIT;
47    }
48
49    private static void executeLoop() {
50        /*
51         * Start off with executing the first loop, then change the call site
52         * target so as to switch over to the second loop but continue running
53         * in the first loop. Eventually, an OSR compilation of the first loop
54         * is triggered. Yet C1 will not find the OSR entry, since it will
55         * have optimized out the first loop already during parsing.
56         */
57        if (CALL_SITE.getTarget() == CONSTANT_FALSE) {
58            int count = 0;
59            while (doSomething()) {
60                if (count++ == 1) {
61                    flipSwitch();
62                }
63            }
64        } else {
65            while (doSomething()) {
66            }
67        }
68    }
69
70    private static void flipSwitch() {
71        CALL_SITE.setTarget(CONSTANT_TRUE);
72    }
73
74    public static void main(String[] args) {
75        executeLoop();
76    }
77}
78