|
| 1 | +/* |
| 2 | + * Copyright Amazon.com Inc. 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 8333791 |
| 27 | + * @requires os.arch=="aarch64" | os.arch=="riscv64" | os.arch=="x86_64" | os.arch=="amd64" |
| 28 | + * @requires vm.gc.Parallel |
| 29 | + * @requires vm.compiler2.enabled |
| 30 | + * @summary Check stable field folding and barriers |
| 31 | + * @modules java.base/jdk.internal.vm.annotation |
| 32 | + * @library /test/lib / |
| 33 | + * @run driver compiler.c2.irTests.stable.StablePrimArrayTest |
| 34 | + */ |
| 35 | + |
| 36 | +package compiler.c2.irTests.stable; |
| 37 | + |
| 38 | +import compiler.lib.ir_framework.*; |
| 39 | +import jdk.test.lib.Asserts; |
| 40 | + |
| 41 | +import jdk.internal.vm.annotation.Stable; |
| 42 | + |
| 43 | +public class StablePrimArrayTest { |
| 44 | + |
| 45 | + public static void main(String[] args) { |
| 46 | + TestFramework tf = new TestFramework(); |
| 47 | + tf.addTestClassesToBootClassPath(); |
| 48 | + tf.addFlags( |
| 49 | + "-XX:+UnlockExperimentalVMOptions", |
| 50 | + "-XX:CompileThreshold=100", |
| 51 | + "-XX:-TieredCompilation", |
| 52 | + "-XX:+UseParallelGC" |
| 53 | + ); |
| 54 | + tf.start(); |
| 55 | + } |
| 56 | + |
| 57 | + static final int[] EMPTY_INTEGER = new int[] { 0 }; |
| 58 | + static final int[] FULL_INTEGER = new int[] { 42 }; |
| 59 | + |
| 60 | + static class Carrier { |
| 61 | + @Stable |
| 62 | + int[] field; |
| 63 | + |
| 64 | + @ForceInline |
| 65 | + public Carrier(int initLevel) { |
| 66 | + switch (initLevel) { |
| 67 | + case 0: |
| 68 | + // Do nothing. |
| 69 | + break; |
| 70 | + case 1: |
| 71 | + field = EMPTY_INTEGER; |
| 72 | + break; |
| 73 | + case 2: |
| 74 | + field = FULL_INTEGER; |
| 75 | + break; |
| 76 | + default: |
| 77 | + throw new IllegalStateException("Unknown level"); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @ForceInline |
| 82 | + public void initEmpty() { |
| 83 | + field = EMPTY_INTEGER; |
| 84 | + } |
| 85 | + |
| 86 | + @ForceInline |
| 87 | + public void initFull() { |
| 88 | + field = FULL_INTEGER; |
| 89 | + } |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + static final Carrier BLANK_CARRIER = new Carrier(0); |
| 94 | + static final Carrier INIT_EMPTY_CARRIER = new Carrier(1); |
| 95 | + static final Carrier INIT_FULL_CARRIER = new Carrier(2); |
| 96 | + |
| 97 | + @Test |
| 98 | + @IR(counts = { IRNode.LOAD, ">0" }) |
| 99 | + @IR(failOn = { IRNode.MEMBAR }) |
| 100 | + static int testNoFold() { |
| 101 | + // Access should not be folded. |
| 102 | + // No barriers expected for plain fields. |
| 103 | + int[] is = BLANK_CARRIER.field; |
| 104 | + if (is != null) { |
| 105 | + return is[0]; |
| 106 | + } |
| 107 | + return 0; |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + @IR(counts = { IRNode.LOAD, ">0" }) |
| 112 | + @IR(failOn = { IRNode.MEMBAR }) |
| 113 | + static int testPartialFold() { |
| 114 | + // Access should not be folded. |
| 115 | + // No barriers expected for plain fields. |
| 116 | + int[] is = INIT_EMPTY_CARRIER.field; |
| 117 | + if (is != null) { |
| 118 | + return is[0]; |
| 119 | + } |
| 120 | + return 0; |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + @Test |
| 125 | + @IR(failOn = { IRNode.LOAD, IRNode.MEMBAR }) |
| 126 | + static int testFold() { |
| 127 | + // Access should be completely folded. |
| 128 | + int[] is = INIT_FULL_CARRIER.field; |
| 129 | + if (is != null) { |
| 130 | + return is[0]; |
| 131 | + } |
| 132 | + return 0; |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + @IR(counts = { IRNode.MEMBAR_STORESTORE, "1" }) |
| 137 | + static Carrier testConstructorBlankInit() { |
| 138 | + // Only the header barrier. |
| 139 | + return new Carrier(0); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + @IR(counts = { IRNode.MEMBAR_STORESTORE, "1" }) |
| 144 | + static Carrier testConstructorEmptyInit() { |
| 145 | + // Only the header barrier. |
| 146 | + return new Carrier(1); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + @IR(counts = { IRNode.MEMBAR_STORESTORE, "1" }) |
| 151 | + static Carrier testConstructorFullInit() { |
| 152 | + // Only the header barrier. |
| 153 | + return new Carrier(2); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + @IR(failOn = { IRNode.MEMBAR }) |
| 158 | + static void testMethodEmptyInit() { |
| 159 | + // Reference inits do not have membars. |
| 160 | + INIT_EMPTY_CARRIER.initEmpty(); |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + @IR(failOn = { IRNode.MEMBAR }) |
| 165 | + static void testMethodFullInit() { |
| 166 | + // Reference inits do not have membars. |
| 167 | + INIT_FULL_CARRIER.initFull(); |
| 168 | + } |
| 169 | + |
| 170 | +} |
0 commit comments