Skip to content

Missed opportunities for transformation of value type array helpers by Value Propagation #14402

Closed

Description

@hzongaro

Consider the following test Java program.

public class TestVPArrayStore2 {
    public static class MyClass {}

    public static final void sub4(Integer[] arr, Integer val) {
        arr[1] = val;
    }

    public static final void sub6(MyClass[] arr) {
        arr[1] = arr[2];
    }

    public static final void sub7(Integer val, int i) {
        Object oarr[] = new Object[5];
        oarr[i] = val;
        oarr[3] = val;
    }

    public static final void main(String[] args) {
        Integer[] iarr = new Integer[3];
        Integer ival = Integer.valueOf(3);
        MyClass[] myarr = new MyClass[3];

         for (int i = 0; i < 2; i++) {
            sub4(iarr, ival);
            sub6(myarr);
            sub7(ival, 1);
        }
    }
}

If the test is run as follows:

java -Xjit:count=1,disableAsyncCompilation,limit={TestVPArrayStore2.sub*},{TestVPArrayStore2.sub*}\(log=vparrstore.log,traceGlobalVP,traceLocalVP\) -XX:+EnableValhalla TestVPArrayStore2

some potential opportunities are missed in Value Propagation with value types support enabled.

In sub4, when storing an Integer value to an array with component type Integer, an ArrayStoreCHK is not needed, but Value Propagation misses that opportunity in transforming the call to the jitStoreFlattenableArrayElement helper to inline IL:

n7n       NULLCHK on n3n [#32]
n6n         PassThrough
n3n           aload  <parm 0 [Ljava/lang/Integer;>[#355  Parm] [flags 0x40000107 0x0 ]
n18n      BNDCHK [#1]
n17n        arraylength (stride 4)
n3n           ==>aload
n4n         iconst 1 (X!=0 X>=0 )
n19n      ArrayStoreCHK [#43] (sharedMemory )   // <<<< UNNECESSARY
n8n         awrtbari  <array-shadow>[#232  Shadow] [flags 0x80000607 0x0 ] (sharedMemory )
n16n          aladd (internalPtr sharedMemory )
n3n             ==>aload
n15n            ladd (X>=0 )
n13n              lshl
n11n                i2l
n4n                   ==>iconst 1
n12n                iconst 2
n14n              lconst 16
n5n           aload  <parm 1 Ljava/lang/Integer;>[#356  Parm] [flags 0x40000107 0x0 ]
n3n           ==>aload
n21n      compressedRefs
n8n         ==>awrtbari
n20n        lconst 0

In sub6, when copying a value from an element of MyClass[] to another element of the same array, an ArrayStoreException can never occur, but an unneededArrayStoreCHK is generated in transforming the call to the jitStoreFlattenableArrayElement helper to inline IL:

n8n       NULLCHK on n5n [#32]
n7n         PassThrough
n5n           aload  <parm 0 [LTestVPArrayStore2$MyClass;>[#355  Parm] [flags 0x40000107 0x0 ]
n35n      BNDCHK [#1]
n34n        arraylength (stride 4)
n5n           ==>aload
n6n         iconst 2 (X!=0 X>=0 )
n37n      compressedRefs
n9n         aloadi  <array-shadow>[#232  Shadow] [flags 0x80000607 0x0 ]
n33n          aladd (internalPtr sharedMemory )
n5n             ==>aload
n32n            ladd (X>=0 )
n30n              lshl
n28n                i2l
n6n                   ==>iconst 2
n29n                iconst 2
n31n              lconst 16
n36n        lconst 0
n10n      treetop
n9n         ==>aloadi
n12n      treetop
n5n         ==>aload
n24n      BNDCHK [#1]
n23n        arraylength (stride 4)
n5n           ==>aload
n4n         iconst 1 (X!=0 X>=0 )
n25n      ArrayStoreCHK [#43] (sharedMemory )   // <<<< UNNECESSARY
n13n        awrtbari  <array-shadow>[#232  Shadow] [flags 0x80000607 0x0 ] (sharedMemory )
n22n          aladd (internalPtr sharedMemory )
n5n             ==>aload
n21n            ladd (X>=0 )
n19n              lshl
n17n                i2l
n4n                   ==>iconst 1
n18n                iconst 2
n20n              lconst 16
n9n           ==>aloadi
n5n           ==>aload
n27n      compressedRefs
n13n        ==>awrtbari
n26n        lconst 0

Finally, in sub7, opportunities to avoid unneeded arraylength and BNDCHK operations are missed when information about the size of the array involved is available:

n71n      BNDCHK [#1]
n70n        arraylength (stride 4)    // <<<< arraylength is known to be 5
n5n           ==>anewarray
n9n         iload  <parm 1 I>[#356  Parm] [flags 0x40000103 0x0 ] (cannotOverflow )
n72n      ArrayStoreCHK [#43] (sharedMemory )
n13n        awrtbari  <array-shadow>[#232  Shadow] [flags 0x80000607 0x0 ] (sharedMemory )
n69n          aladd (internalPtr sharedMemory )
n5n             ==>anewarray
n68n            ladd (X>=0 )
n66n              lshl
n64n                i2l
n9n                   ==>iload
n65n                iconst 2
n67n              lconst 16
n10n          aload  <parm 0 Ljava/lang/Integer;>[#355  Parm] [flags 0x40000107 0x0 ]
n5n           ==>anewarray
n60n      BNDCHK [#1]   // <<< arraylength is known to be 5 and index is 3, so BNDCHK is redundant
n59n        arraylength (stride 4)
n5n           ==>anewarray
n16n        iconst 3 (X!=0 X>=0 )
n61n      ArrayStoreCHK [#43] (sharedMemory )
n20n        awrtbari  <array-shadow>[#232  Shadow] [flags 0x80000607 0x0 ] (sharedMemory )
n58n          aladd (internalPtr sharedMemory )
n5n             ==>anewarray
n57n            ladd (X>=0 )
n55n              lshl
n53n                i2l
n16n                  ==>iconst 3
n54n                iconst 2
n56n              lconst 16
n10n          ==>aload
n5n           ==>anewarray

Activity

  1. self-assigned this
    on Jan 31, 2022
  2. hzongaro commented on Mar 31, 2022

    @hzongaro
    MemberAuthor

    Updates:

    Those changes complete the work needed for this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions