TestVectorizationWithInvariant.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/**
26 * @test
27 * @bug 8078497
28 * @summary Tests correct alignment of vectors with loop invariant offset.
29 * @modules java.base/jdk.internal.misc
30 * @library /testlibrary
31 * @run main compiler.loopopts.superword.TestVectorizationWithInvariant
32 */
33
34package compiler.loopopts.superword;
35
36import jdk.internal.misc.Unsafe;
37import jdk.test.lib.Utils;
38
39public class TestVectorizationWithInvariant {
40
41    private static Unsafe unsafe;
42    private static final long BYTE_ARRAY_OFFSET;
43    private static final long CHAR_ARRAY_OFFSET;
44
45    static {
46        unsafe = Utils.getUnsafe();
47        BYTE_ARRAY_OFFSET = unsafe.arrayBaseOffset(byte[].class);
48        CHAR_ARRAY_OFFSET = unsafe.arrayBaseOffset(char[].class);
49    }
50
51    public static void main(String[] args) throws Exception {
52        byte[] byte_array1 = new byte[1000];
53        byte[] byte_array2 = new byte[1000];
54        char[] char_array = new char[1000];
55
56        for (int i = 0; i < 20_000; ++i) {
57            copyByteToChar(byte_array1, byte_array2, char_array, 1);
58            copyCharToByte(char_array, byte_array1, 1);
59            copyCharToByteAligned(char_array, byte_array1);
60            copyCharToByteUnaligned(char_array, byte_array1);
61        }
62    }
63
64    /*
65     * Copy multiple consecutive chars from a byte array to a given offset in a char array
66     * to trigger C2's superword optimization. The offset in the byte array is independent
67     * of the loop induction variable and can be set to an arbitrary value. It may then not
68     * be possible to both align the LoadUS and the StoreC operations. Therefore, vectorization
69     * should only be done in this case if unaligned memory accesses are allowed.
70     */
71    public static void copyByteToChar(byte[] src1, byte[] src2, char[] dst, int off) {
72        off = (int) BYTE_ARRAY_OFFSET + (off << 1);
73        byte[] src = src1;
74        for (int i = (int) CHAR_ARRAY_OFFSET; i < 100; i = i + 8) {
75            // Copy 8 chars from src to dst
76            unsafe.putChar(dst, i + 0, unsafe.getChar(src, off + 0));
77            unsafe.putChar(dst, i + 2, unsafe.getChar(src, off + 2));
78            unsafe.putChar(dst, i + 4, unsafe.getChar(src, off + 4));
79            unsafe.putChar(dst, i + 6, unsafe.getChar(src, off + 6));
80            unsafe.putChar(dst, i + 8, unsafe.getChar(src, off + 8));
81            unsafe.putChar(dst, i + 10, unsafe.getChar(src, off + 10));
82            unsafe.putChar(dst, i + 12, unsafe.getChar(src, off + 12));
83            unsafe.putChar(dst, i + 14, unsafe.getChar(src, off + 14));
84
85            // Prevent loop invariant code motion of char read.
86            src = (src == src1) ? src2 : src1;
87        }
88    }
89
90    /*
91     * Copy multiple consecutive chars from a char array to a given offset in a byte array
92     * to trigger C2's superword optimization. Checks for similar problems as 'copyByteToChar'.
93     */
94    public static void copyCharToByte(char[] src, byte[] dst, int off) {
95        off = (int) BYTE_ARRAY_OFFSET + (off << 1);
96        for (int i = 0; i < 100; i = i + 8) {
97            // Copy 8 chars from src to dst
98            unsafe.putChar(dst, off + 0, src[i + 0]);
99            unsafe.putChar(dst, off + 2, src[i + 1]);
100            unsafe.putChar(dst, off + 4, src[i + 2]);
101            unsafe.putChar(dst, off + 6, src[i + 3]);
102            unsafe.putChar(dst, off + 8, src[i + 4]);
103            unsafe.putChar(dst, off + 10, src[i + 5]);
104            unsafe.putChar(dst, off + 12, src[i + 6]);
105            unsafe.putChar(dst, off + 14, src[i + 7]);
106        }
107    }
108
109    /*
110     * Variant of copyCharToByte with a constant destination array offset.
111     * The loop should always be vectorized because both the LoadUS and StoreC
112     * operations can be aligned.
113     */
114    public static void copyCharToByteAligned(char[] src, byte[] dst) {
115        final int off = (int) BYTE_ARRAY_OFFSET;
116        for (int i = 8; i < 100; i = i + 8) {
117            // Copy 8 chars from src to dst
118            unsafe.putChar(dst, off + 0, src[i + 0]);
119            unsafe.putChar(dst, off + 2, src[i + 1]);
120            unsafe.putChar(dst, off + 4, src[i + 2]);
121            unsafe.putChar(dst, off + 6, src[i + 3]);
122            unsafe.putChar(dst, off + 8, src[i + 4]);
123            unsafe.putChar(dst, off + 10, src[i + 5]);
124            unsafe.putChar(dst, off + 12, src[i + 6]);
125            unsafe.putChar(dst, off + 14, src[i + 7]);
126        }
127    }
128
129    /*
130     * Variant of copyCharToByte with a constant destination array offset. The
131     * loop should only be vectorized if unaligned memory operations are allowed
132     * because not both the LoadUS and the StoreC can be aligned.
133     */
134    public static void copyCharToByteUnaligned(char[] src, byte[] dst) {
135        final int off = (int) BYTE_ARRAY_OFFSET + 2;
136        for (int i = 0; i < 100; i = i + 8) {
137            // Copy 8 chars from src to dst
138            unsafe.putChar(dst, off + 0, src[i + 0]);
139            unsafe.putChar(dst, off + 2, src[i + 1]);
140            unsafe.putChar(dst, off + 4, src[i + 2]);
141            unsafe.putChar(dst, off + 6, src[i + 3]);
142            unsafe.putChar(dst, off + 8, src[i + 4]);
143            unsafe.putChar(dst, off + 10, src[i + 5]);
144            unsafe.putChar(dst, off + 12, src[i + 6]);
145            unsafe.putChar(dst, off + 14, src[i + 7]);
146        }
147    }
148}
149