<html>
  <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <p>I've found a bug report that likely describes the same problem
      you mentioned in your example:
      <meta http-equiv="content-type" content="text/html; charset=utf-8">
      <a class="issue-link" data-issue-key="JDK-8171828"
        href="https://bugs.openjdk.java.net/browse/JDK-8171828"
        id="key-val" rel="4910162" style="color: rgb(59, 115, 175);
        text-decoration: none;">JDK-8171828</a></p>
    Another interesting thing is if we replace new Object() with a
    creation of a primitive box object from a constant value, it will be
    eliminated, even without a local variable. <br>
    <br>
        private static void singleBoxedConstant() {<br>
            Object[] array = new Object[]{new Double(123.456)};<br>
            Object a = array[0];<br>
        }<br>
    <br>
    It also works with multiple box objects:<br>
    <br>
        private static void multiBoxedConstant() {<br>
            Object[] array = new Object[]{new Double(21), new
    Double(1.2)};<br>
            Object a = array[0];<br>
            Object b = array[1];<br>
        }<br>
    <br>
    But if we replace the box constructor argument with a non-constant
    value, it will again not work. <br>
    <br>
        private static volatile double x = 43;<br>
    <br>
        private static void multiBoxedNonconstant() {<br>
            Object[] array = new Object[]{new Double(x), new
    Double(1.2)};<br>
            Object a = array[0];<br>
            Object b = array[1];<br>
        }<br>
    <br>
    <br>
    <div class="moz-cite-prefix">2017-05-26 14:18 keltezéssel, Vitaly
      Davidovich írta:<br>
    </div>
    <blockquote
cite="mid:CAHjP37HDnbJSqwrQyzmncW8sYMBWgkyFjHOCqyCqkoEAsDbSsA@mail.gmail.com"
      type="cite">
      <div dir="ltr">Indeed, you're right - I see the same
        (unsurprisingly) on 8u121.  I wasn't looking at the loop
        compilation before, but just the single/multi methods standalone
        (analogous to them not being inlined).
        <div><br>
        </div>
        <div>What's interesting also is that even single(), with just a
          slight re-arrangement, doesn't get eliminated.  For instance:
          <div><br>
          </div>
          <div>static void single() {<br>
            <div>
              <div>    final Object[] array = new Object[] {new
                Object()};</div>
              <div>    final Object a = array[0];</div>
            </div>
            <div>}</div>
            <div><br>
            </div>
          </div>
        </div>
      </div>
      <div class="gmail_extra"><br>
        <div class="gmail_quote">On Fri, May 26, 2017 at 7:34 AM,
          Hontvári Attila <span dir="ltr"><<a moz-do-not-send="true"
              href="mailto:attila@hontvari.net" target="_blank">attila@hontvari.net</a>></span>
          wrote:<br>
          <blockquote class="gmail_quote" style="margin:0 0 0
            .8ex;border-left:1px #ccc solid;padding-left:1ex">
            <div bgcolor="#FFFFFF" text="#000000">
              <p>They are eliminated only if multi() is not inlined into
                an enclosing loop. (8u101) If I disable inlining it with
                CompileCommand, C2 compiles it to an noop method. But
                when inlining is enabled, the wrapper loop compiles into
                a long code: <a moz-do-not-send="true"
                  class="m_-6681420737192240477moz-txt-link-freetext"
                  href="http://pastebin.com/KAq5v2XE" target="_blank">http://pastebin.com/KAq5v2XE</a></p>
              <p>Maybe this issue is related to <a
                  moz-do-not-send="true"
                  class="m_-6681420737192240477issue-link"
                  href="https://bugs.openjdk.java.net/browse/JDK-8155769"
                  id="m_-6681420737192240477key-val" rel="4880407"
                  style="color:rgb(59,115,175);text-decoration:none"
                  target="_blank">JDK-8155769</a>. <br>
              </p>
              <div>
                <div class="h5">
                  <div class="m_-6681420737192240477moz-cite-prefix">2017-05-25
                    21:50 keltezéssel, Vitaly Davidovich írta:<br>
                  </div>
                  <blockquote type="cite">
                    <div dir="ltr">Looking at C2 generated assembly
                      (8u121), both cases are eliminated - the single()
                      and multi() are dead code.  To alleviate the truly
                      dead code scenario here, I had these methods
                      return something from the allocated object (I
                      created a dummy class that holds an int) and
                      accessed those objects via array indexing - no
                      allocations are generated (not the array, and not
                      the objects).</div>
                    <div class="gmail_extra"><br>
                      <div class="gmail_quote">On Thu, May 25, 2017 at
                        3:12 PM, Kirk Pepperdine <span dir="ltr"><<a
                            moz-do-not-send="true"
                            href="mailto:kirk.pepperdine@gmail.com"
                            target="_blank">kirk.pepperdine@gmail.com</a>></span>
                        wrote:<br>
                        <blockquote class="gmail_quote" style="margin:0
                          0 0 .8ex;border-left:1px #ccc
                          solid;padding-left:1ex">I can see the arrays
                          being DVE’ed.. in 8_121. EA seems to be doing
                          it’s job.<br>
                          <br>
                          Kind regards,<br>
                          Kirk<br>
                          <div class="m_-6681420737192240477HOEnZb">
                            <div class="m_-6681420737192240477h5"><br>
                              > On May 25, 2017, at 9:03 PM, Vladimir
                              Kozlov <<a moz-do-not-send="true"
                                href="mailto:vladimir.kozlov@oracle.com"
                                target="_blank">vladimir.kozlov@oracle.com</a>>
                              wrote:<br>
                              ><br>
                              > Hi Attila,<br>
                              ><br>
                              > What Java version you are using?<br>
                              ><br>
                              > I tried jdk 7, 8, 9.<br>
                              ><br>
                              > What I see is that in both cases only
                              arrays are eliminated
                              (-XX:+PrintEliminateAllocation<wbr>s with
                              debug version of VM):<br>
                              ><br>
                              > BEGIN single<br>
                              > Scalar  73    AllocateArray   === 
                              51  46  47  8  1 ( 71  59  21  58  41  1 1
                              ) [[ 74  75  76  83  84  85 ]] 
                              rawptr:NotNull ( int:>=0,
                              java/lang/Object:NotNull *, bool, int )
                              NewMain::single @ bci:9 !jvms:
                              NewMain::single @ bci:9<br>
                              > ++++ Eliminated: 73 AllocateArray<br>
                              > END single<br>
                              > BEGIN multi<br>
                              > Scalar  116   AllocateArray   === 
                              91  86  87  8  1 ( 114  103  21  102  41
                              76  1  1  1 ) [[ 117  118  119  126  127 
                              128 ]]  rawptr:NotNull ( int:>=0,
                              java/lang/Object:NotNull *, bool, int )
                              NewMain::multi @ bci:17 !jvms:
                              NewMain::multi @ bci:17<br>
                              > ++++ Eliminated: 116 AllocateArray<br>
                              > DONE<br>
                              ><br>
                              > But based on code non of objects
                              should be allocated - both methods should
                              have only 'return' generated.<br>
                              ><br>
                              > Looks like EA missing such case when
                              objects are stored into small array.<br>
                              ><br>
                              > Thanks,<br>
                              > Vladimir<br>
                              ><br>
                              > On 5/25/17 9:16 AM, Hontvári Attila
                              wrote:<br>
                              >> When creating a non-escaping
                              array and putting a newly created,
                              non-escaping object in it, the EA works,
                              there are no heap allocations.<br>
                              >>     private static void single()
                              {<br>
                              >>         Object x = new Object();<br>
                              >>         Object[] array = new
                              Object[]{x};<br>
                              >>         Object a = array[0];<br>
                              >>     }<br>
                              >> But if we do the same with two or
                              more objects, the array will be allocated
                              on the heap, and not eliminated.<br>
                              >>     private static void multi() {<br>
                              >>         Object x = new Object();<br>
                              >>         Object y = new Object();<br>
                              >>         Object[] array = new
                              Object[]{x, y};<br>
                              >>         Object a = array[0];<br>
                              >>         Object b = array[1];<br>
                              >>     }<br>
                              >> Is there a reason why it is only
                              working in the first case?<br>
                              >> This would be useful for example,
                              MethodHandle::invokeWithArgume<wbr>nts,
                              when the primitive types are boxed, and
                              put into a varargs array, see my older
                              email [1].<br>
                              >> A complete test source code is in
                              [2], if we run it with -verbose:gc, we can
                              see there are many GCs in the second case,
                              but there are no GCs in the first case.<br>
                              >> [1] <a moz-do-not-send="true"
href="http://mail.openjdk.java.net/pipermail/jigsaw-dev/2017-January/010933.html"
                                rel="noreferrer" target="_blank">http://mail.openjdk.java.net/p<wbr>ipermail/jigsaw-dev/2017-Janua<wbr>ry/010933.html</a><br>
                              >> [2] <a moz-do-not-send="true"
href="https://gist.github.com/anonymous/bd46075ef1ebd858dae49fe6cfe39da8"
                                rel="noreferrer" target="_blank">https://gist.github.com/anonym<wbr>ous/bd46075ef1ebd858dae49fe6cf<wbr>e39da8</a><br>
                              <br>
                            </div>
                          </div>
                        </blockquote>
                      </div>
                      <br>
                    </div>
                  </blockquote>
                  <br>
                </div>
              </div>
            </div>
          </blockquote>
        </div>
        <br>
      </div>
    </blockquote>
    <br>
  </body>
</html>