package compiler.c2;

import compiler.lib.ir_framework.DontInline;
import compiler.lib.ir_framework.Run;
import compiler.lib.ir_framework.Test;
import compiler.lib.ir_framework.TestFramework;
import jdk.internal.misc.Unsafe;
import jdk.test.lib.Asserts;

/*
 * @test
 * @bug 8372374
 * @summary Test the compiler memory analysis
 * @modules java.base/jdk.internal.misc
 * @library /test/lib /
 * @run compiler.c2.TestArrayOfBottoms
 */
public class TestArrayOfBottoms {
    public static void main(String[] args) {
        TestFramework framework = new TestFramework(TestArrayOfBottoms.class);
        framework.addFlags("--add-exports", "java.base/jdk.internal.misc=ALL-UNNAMED");
        framework.start();
    }

    @DontInline
    public void consume(byte[] a1, int[] a2) {}

    @DontInline
    public void consume(int s) {
        Asserts.assertEQ(0, s);
    }

    @Test
    public void test(boolean b1, boolean b2, int val) {
        byte[] a1 = new byte[4];
        int[] a2 = new int[1];
        consume(a1, a2);

        Object a;
        long offset;
        if (b1) {
            a = a1;
            offset = Unsafe.ARRAY_BYTE_BASE_OFFSET;
        } else {
            a = a2;
            offset = Unsafe.ARRAY_INT_BASE_OFFSET;
        }

        int s1 = a1[0];
        int s2 = a2[0];
        Unsafe.getUnsafe().putIntUnaligned(a, offset, val);
        if (b2) {
            consume(s1);
        } else {
            consume(s2);
        }
    }

    @Run(test = "test")
    public void run() {
        test(false, false, 1);
        test(false, true, 1);
        test(true, false, 1);
        test(true, true, 1);
    }
}
