1 /*
  2  * Copyright (c) 2024, Red Hat, Inc. All rights reserved.
  3  * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  */
 24 
 25 package compiler.c2;
 26 
 27 import compiler.lib.ir_framework.*;
 28 import jdk.internal.misc.Unsafe;
 29 
 30 /*
 31  * @test
 32  * @bug 8343068
 33  * @summary C2: CastX2P Ideal transformation not always applied
 34  * @modules java.base/jdk.internal.misc
 35  * @library /test/lib /
 36  * @run driver compiler.c2.TestCastX2NotProcessedIGVN
 37  */
 38 
 39 public class TestCastX2NotProcessedIGVN {
 40     private static final Unsafe UNSAFE = Unsafe.getUnsafe();
 41     static int size = 1024;
 42     static long base = UNSAFE.allocateMemory(4 * size);
 43 
 44 
 45     public static void main(String[] args) {
 46         // Cross-product: +-AlignVector and +-UseCompactObjectHeaders
 47         TestFramework.runWithFlags("--add-modules", "java.base", "--add-exports", "java.base/jdk.internal.misc=ALL-UNNAMED",
 48                                    "-XX:+UnlockExperimentalVMOptions", "-XX:-UseCompactObjectHeaders",
 49                                    "-XX:-AlignVector");
 50         TestFramework.runWithFlags("--add-modules", "java.base", "--add-exports", "java.base/jdk.internal.misc=ALL-UNNAMED",
 51                                    "-XX:+UnlockExperimentalVMOptions", "-XX:-UseCompactObjectHeaders",
 52                                    "-XX:+AlignVector");
 53         TestFramework.runWithFlags("--add-modules", "java.base", "--add-exports", "java.base/jdk.internal.misc=ALL-UNNAMED",
 54                                    "-XX:+UnlockExperimentalVMOptions", "-XX:+UseCompactObjectHeaders",
 55                                    "-XX:-AlignVector");
 56         TestFramework.runWithFlags("--add-modules", "java.base", "--add-exports", "java.base/jdk.internal.misc=ALL-UNNAMED",
 57                                    "-XX:+UnlockExperimentalVMOptions", "-XX:+UseCompactObjectHeaders",
 58                                    "-XX:+AlignVector");
 59     }
 60 
 61     @Test
 62     @IR(failOn = IRNode.ADD_L, counts = {IRNode.ADD_P, "1"})
 63     public static byte test1(long base) {
 64         int offset = 0;
 65         do {
 66             offset++;
 67         } while (offset < 100);
 68         long longOffset = ((long) offset) * 2;
 69         return UNSAFE.getByte(null, base + longOffset);
 70     }
 71 
 72     @Run(test = "test1")
 73     public static void test1Runner() {
 74         test1(base);
 75     }
 76 
 77     @Test
 78     @IR(counts = {IRNode.LOAD_VECTOR_I, "> 1"},
 79         applyIfOr = {"UseCompactObjectHeaders", "false", "AlignVector", "false"},
 80         applyIfPlatformOr = {"x64", "true", "aarch64", "true"})
 81     public static int test2(int stop, int[] array) {
 82         int v = 0;
 83         stop = Math.min(stop, Integer.MAX_VALUE / 4);
 84         for (int i = 0; i < stop; i++) {
 85             long offset = ((long)i) * 4;
 86             array[i] = UNSAFE.getInt(null, offset + base);
 87             // With AlignVector, we need 8-byte alignment of vector loads/stores.
 88             // UseCompactObjectHeaders=false                  UseCompactObjectHeaders=true
 89             // I_adr = base + 16 + 4*i  ->  i % 2 = 0         B_adr = base + 12 + 4*i  ->  i % 2 = 1
 90             // N_adr = base      + 4*i  ->  i % 2 = 0         N_adr = base      + 4*i  ->  i % 2 = 0
 91             // -> vectorize                                   -> no vectorization
 92         }
 93         return v;
 94     }
 95 
 96     @Run(test = "test2")
 97     public static void test2Runner() {
 98         test2(size, new int[size]);
 99     }
100 }