Path: blob/master/test/hotspot/jtreg/compiler/escapeAnalysis/TestEliminateAllocation.java
41152 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 823141226* @summary The enhancement eliminates all allocations in the loop body of test() due to an improved field zeroing elimination dominance check.27* @run main/othervm -XX:-TieredCompilation -XX:CompileCommand=compileonly,compiler.escapeAnalysis.TestEliminateAllocation::test28* compiler.escapeAnalysis.TestEliminateAllocation29*/3031package compiler.escapeAnalysis;3233public class TestEliminateAllocation {3435public static int a = 20;36public static int b = 30;37public static int c = 40;3839public void test() {40int i = 0;4142/*43* The resulting IR for the loop body contains 2 allocations, one Wrapper and an int array44* The array field store in the Wrapper object 'wrapper.arr = arr' cannot be capturued due to an early bail out.45* Therefore, the initial value of wrapper.arr is null.46* As a result, the escape analysis marks the array allocation as not scalar replaceable:47* 'wrapper.arr' which is null is merged with the int array object in the assignment 'wrapper.arr = arr'.48* Both null and the int array are treated as different objects. As a result the array allocation cannot be eliminated.49*50* The new enhancement does not bail out early anymore and therefore escape analysis does not mark it as51* not scalar replaceable. This results in elimination of all allocations in this method.52*/53do {54int[] arr = new int[] { a / b / c };55Wrapper wrapper = new Wrapper();56wrapper.setArr(arr);57i++;58}59while (i < 10);60}6162public static void main(String[] strArr) {63TestEliminateAllocation _instance = new TestEliminateAllocation();64for (int i = 0; i < 10_000; i++ ) {65_instance.test();66}67}68}6970class Wrapper {71int[] arr;72void setArr(int... many) { arr = many; }73}747576