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

Code quality: arraylength loads are not commoned between branches

    Details

      Description

      If you run a simple method like this:

          public int doCur(int flag) {
              if (flag == 0) {
                  return array.length;
              } else {
                  return array.length >> 1;
              }
          }

      ...when the branch profile for both branches is 50% - 50%, then you will see this generated code:

       mov %eax,-0x14000(%rsp)
       push %rbp
       sub $0x10,%rsp
       mov 0xc(%rsi),%r10d ; get field $array
       test %edx,%edx ; test $flag
       je BRANCH_2
       mov 0xc(%r12,%r10,8),%eax ; $array.arraylength
       sar %eax ; >> 1
       jmp RETURN ; jump over
      BRANCH_2:
       mov 0xc(%r12,%r10,8),%eax ; $array.arraylength AGAIN
      RETURN:
       add $0x10,%rsp ; return
       pop %rbp
       test %eax,0x162611d3(%rip)
       retq

      In other words, "arraylength" loads manifest on both branches, while can (probably) be moved before the test.

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              shade Aleksey Shipilev
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated: