Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8078629

VM should constant fold Unsafe.get*() loads from final fields

    XMLWordPrintable

Details

    • Enhancement
    • Resolution: Fixed
    • P3
    • 9
    • 9
    • hotspot
    • None
    • b76

    Backports

      Description

        VM doesn't constant fold loads from final fields when field MethodHandles are used.

        public class FieldAccess {
            static final int s1 = 123;

            static final MethodHandle f1;
            static {
                try {
                    Class<?> THIS_CLASS = FieldAccess.class;
                    MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
                    f1 = LOOKUP.findStaticGetter(THIS_CLASS, "s1", int.class);
                } catch (Exception e) {
                    throw new Error(e);
                }
            }

            static int testMH() throws Throwable {
                return (int)f1.invokeExact();
            }
            static int testDirect() throws Throwable {
                return s1;
            }

            public static void main(String[] args) throws Throwable {
                while (true) {
                    testDirect();
                    testMH();
                }
            }
        }

        $ java -XX:CompileCommand=dontinline,*::test* -XX:CompileCommand=print,*::test* FieldAccess
        ...
          # {method} {0x0000000118aca538} 'testDirect' '()I' in 'jsr292/FieldAccess'
        ...
          0x000000010be1716c: mov $0x7b,%eax
        ...
          # {method} {0x0000000118aca498} 'testMH' '()I' in 'jsr292/FieldAccess'
        ...
          0x000000010be1f3ec: movabs $0x76abb2f48,%r10 ; {oop(a 'java/lang/Class' = 'jsr292/FieldAccess')}
          0x000000010be1f3f6: mov 0x6c(%r10),%eax ;*invokevirtual getInt
                                                        ; - java.lang.invoke.LambdaForm$MH015/708049632::getIntStatic_001@23
                                                        ; - java.lang.invoke.LambdaForm$MH013/991505714::invokeExact_000_MT@15
                                                        ; - jsr292.FieldAccess::testMH@3 (line 24)
        ...

        Attachments

          Issue Links

            Activity

              People

                vlivanov Vladimir Ivanov
                vlivanov Vladimir Ivanov
                Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved: