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