|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, Red Hat, Inc. 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 | +package compiler.c2.irTests; |
| 25 | + |
| 26 | +import compiler.lib.ir_framework.*; |
| 27 | +import jdk.test.lib.Utils; |
| 28 | +import jdk.internal.misc.Unsafe; |
| 29 | +import java.util.Objects; |
| 30 | +import java.util.Random; |
| 31 | + |
| 32 | +/* |
| 33 | + * @test |
| 34 | + * @bug 8286197 |
| 35 | + * @key randomness |
| 36 | + * @summary C2: Optimize MemorySegment shape in int loop |
| 37 | + * @modules java.base/jdk.internal.misc |
| 38 | + * @library /test/lib / |
| 39 | + * @run driver compiler.c2.irTests.TestConvI2LCastLongLoop |
| 40 | + */ |
| 41 | + |
| 42 | +public class TestConvI2LCastLongLoop { |
| 43 | + private static final Unsafe UNSAFE = Unsafe.getUnsafe(); |
| 44 | + private static final Random RANDOM = Utils.getRandomInstance(); |
| 45 | + |
| 46 | + public static void main(String[] args) { |
| 47 | + TestFramework.runWithFlags("--add-modules", "java.base", "--add-exports", "java.base/jdk.internal.misc=ALL-UNNAMED", "-XX:LoopMaxUnroll=0", "-XX:-UseCountedLoopSafepoints"); |
| 48 | + } |
| 49 | + |
| 50 | + static int size = 1024; |
| 51 | + static long base = UNSAFE.allocateMemory(size * 4); |
| 52 | + |
| 53 | + @Test |
| 54 | + @IR(failOn = { IRNode.CAST_LL }) |
| 55 | + public static int test1() { |
| 56 | + // Make sure enough round of loop opts are executed |
| 57 | + for (int i = 0; i < 10; i++) { |
| 58 | + for (int j = 0; j < 10; j++) { |
| 59 | + for (int k = 0; k < 10; k++) { |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + int v = 0; |
| 64 | + // In order to optimize the range check the loop is |
| 65 | + // transformed to: |
| 66 | + // |
| 67 | + // for (int i1;;) { |
| 68 | + // for (int i2;;) { |
| 69 | + // long j = (i1 + i2) * UNSAFE.ARRAY_INT_INDEX_SCALE; // (i1 + i2) << 2 |
| 70 | + // v += UNSAFE.getInt(base + j); |
| 71 | + // } |
| 72 | + // } |
| 73 | + // |
| 74 | + // (i1 + i2) << 2 is transformed to (i1 << 2) + (i2 << 2) |
| 75 | + // because i1 is loop invariant in the inner loop. |
| 76 | + // |
| 77 | + // long j = ... really is (CastLL (Convi2L ...)) |
| 78 | + // |
| 79 | + // With that transformed into (ConvI2L (CastII ...)), The AddL |
| 80 | + // (i1 << 2) + (i2 << 2) can be pushed through the CastII and |
| 81 | + // ConvI2L. |
| 82 | + // The address of the getInt is then: |
| 83 | + // (AddP base (AddL I V)) with I, loop invariant and V loop invariant |
| 84 | + // which can be transformed into: |
| 85 | + // (AddP (AddP base I) V) |
| 86 | + // The inner AddP is computed out of loop |
| 87 | + for (int i = 0; i < size; i++) { |
| 88 | + long j = i * UNSAFE.ARRAY_INT_INDEX_SCALE; |
| 89 | + |
| 90 | + j = Objects.checkIndex(j, size * 4); |
| 91 | + |
| 92 | + if (((base + j) & 3) != 0) { |
| 93 | + throw new RuntimeException(); |
| 94 | + } |
| 95 | + |
| 96 | + v += UNSAFE.getInt(base + j); |
| 97 | + } |
| 98 | + return v; |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + @IR(counts = { IRNode.CAST_II, ">=1", IRNode.CONV_I2L, ">=1" }) |
| 103 | + @IR(failOn = { IRNode.CAST_LL }) |
| 104 | + public static long test2(int i) { |
| 105 | + // Convert (CastLL (ConvI2L ...)) into (ConvI2L (CastII ...)) |
| 106 | + long j = i * UNSAFE.ARRAY_INT_INDEX_SCALE; |
| 107 | + j = Objects.checkIndex(j, size * 4); |
| 108 | + return j; |
| 109 | + } |
| 110 | + |
| 111 | + @Run(test = "test2") |
| 112 | + public static void test2_runner() { |
| 113 | + int i = RANDOM.nextInt(size); |
| 114 | + long res = test2(i); |
| 115 | + if (res != i * UNSAFE.ARRAY_INT_INDEX_SCALE) { |
| 116 | + throw new RuntimeException("incorrect result: " + res); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + @IR(counts = { IRNode.PHI, "2" }) |
| 122 | + public static int test3() { |
| 123 | + int v = 0; |
| 124 | + // splif if should not push LshiftI through the iv Phi |
| 125 | + for (int i = 0; i < 1024; i++) { |
| 126 | + v += i * UNSAFE.ARRAY_INT_INDEX_SCALE; |
| 127 | + } |
| 128 | + return v; |
| 129 | + } |
| 130 | +} |
0 commit comments