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