jdk
1/*
2* Copyright (c) 2022, 2023, 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* @test
26* @bug 8286972
27* @summary Test vectorization of loop induction variable usage in the loop
28* @requires vm.compiler2.enabled
29* @requires (os.simpleArch == "x64" & vm.cpu.features ~= ".*avx2.*") |
30* (os.simpleArch == "aarch64" & vm.cpu.features ~= ".*sve.*")
31* @library /test/lib /
32* @run driver compiler.vectorization.TestPopulateIndex
33*/
34
35package compiler.vectorization;36import compiler.lib.ir_framework.*;37import java.util.Random;38
39public class TestPopulateIndex {40private static final int count = 10000;41
42private int[] idx;43private int[] src;44private int[] dst;45private float[] f;46
47public static void main(String args[]) {48TestFramework.run(TestPopulateIndex.class);49}50
51public TestPopulateIndex() {52idx = new int[count];53src = new int[count];54dst = new int[count];55f = new float[count];56Random ran = new Random(0);57for (int i = 0; i < count; i++) {58src[i] = ran.nextInt();59}60}61
62@Test63@IR(counts = {IRNode.POPULATE_INDEX, "> 0"})64public void indexArrayFill() {65for (int i = 0; i < count; i++) {66idx[i] = i;67}68checkResultIndexArrayFill();69}70
71public void checkResultIndexArrayFill() {72for (int i = 0; i < count; i++) {73int expected = i;74if (idx[i] != expected) {75throw new RuntimeException("Invalid result: idx[" + i + "] = " + idx[i] + " != " + expected);76}77}78}79
80@Test81@IR(counts = {IRNode.POPULATE_INDEX, "> 0"})82public void exprWithIndex1() {83for (int i = 0; i < count; i++) {84dst[i] = src[i] * (i & 7);85}86checkResultExprWithIndex1();87}88
89public void checkResultExprWithIndex1() {90for (int i = 0; i < count; i++) {91int expected = src[i] * (i & 7);92if (dst[i] != expected) {93throw new RuntimeException("Invalid result: dst[" + i + "] = " + dst[i] + " != " + expected);94}95}96}97
98@Test99@IR(counts = {IRNode.POPULATE_INDEX, "> 0"})100public void exprWithIndex2() {101for (int i = 0; i < count; i++) {102f[i] = i * i + 100;103}104checkResultExprWithIndex2();105}106
107public void checkResultExprWithIndex2() {108for (int i = 0; i < count; i++) {109float expected = i * i + 100;110if (f[i] != expected) {111throw new RuntimeException("Invalid result: f[" + i + "] = " + f[i] + " != " + expected);112}113}114}115}
116