// java -Xbatch -XX:CompileCommand=compileonly,Test*::test -XX:CompileCommand=printcompilation,Test*::test -XX:+PrintIdeal TestOptimizeStoreVector.java

import jdk.incubator.vector.VectorSpecies;
import jdk.incubator.vector.IntVector;

public class TestOptimizeStoreVector {

    static final VectorSpecies<Integer> SPECIES =
                IntVector.SPECIES_256;

    static void test(int[] a) {
        // StoreVectorNode::Ideal calls StoreNode::Ideal, which can get rid of previous
        // stores that go to the same position.
        // HOWEVER: if we somehow exit too early in StoreVectorNode::Ideal, we may
        // never reach StoreNode::Ideal and miss the optimization.
        // This happens on aarch64 SVE with 256bits, when we return true for
        // Matcher::vector_needs_partial_operations, but then do nothing when calling
        // VectorNode::try_to_gen_masked_vector. We just return nullptr instantly,
        // rather than trying the other optimizations that StoreNode::Ideal has to
        // offer.
        IntVector v1 = IntVector.fromArray(SPECIES, a, 0 * SPECIES.length());
        IntVector v2 = IntVector.fromArray(SPECIES, a, 1 * SPECIES.length());
        v1.intoArray(a, 3 * SPECIES.length()); // useless store to same position as below.
        v2.intoArray(a, 3 * SPECIES.length());
    }

    public static void main(String[] args) {
        int[] a = new int[1000];
        for (int i = 0; i < 10_000; i++) {
            test(a);
	}
    }
}
