TestEscapeThroughInvoke.java revision 11707:ad7af1afda7a
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 8073956
27 * @summary Tests C2 EA with allocated object escaping through a call.
28 *
29 * @run main/othervm
30 *      -XX:CompileCommand=dontinline,compiler.escapeAnalysis.TestEscapeThroughInvoke::create
31 *      compiler.escapeAnalysis.TestEscapeThroughInvoke
32 */
33
34package compiler.escapeAnalysis;
35
36public class TestEscapeThroughInvoke {
37    private A a;
38
39    public static void main(String[] args) {
40        TestEscapeThroughInvoke test = new TestEscapeThroughInvoke();
41        test.a = new A(42);
42        // Make sure run gets compiled by C2
43        for (int i = 0; i < 100_000; ++i) {
44            test.run();
45        }
46    }
47
48    private void run() {
49        // Allocate something to trigger EA
50        new Object();
51        // Create a new escaping instance of A and
52        // verify that it is always equal to 'a.saved'.
53        A escapingA = create(42);
54        a.check(escapingA);
55    }
56
57    // Create and return a new instance of A that escaped through 'A::saveInto'.
58    // The 'dummy' parameters are needed to avoid EA skipping the methods.
59    private A create(Integer dummy) {
60        A result = new A(dummy);
61        result.saveInto(a, dummy); // result escapes into 'a' here
62        return result;
63    }
64
65    static class A {
66        private A saved;
67
68        public A(Integer dummy) {
69        }
70
71        public void saveInto(A other, Integer dummy) {
72            other.saved = this;
73        }
74
75        public void check(A other) {
76            if (this.saved != other) {
77                throw new RuntimeException("TEST FAILED: Objects not equal.");
78            }
79        }
80    }
81}