C2: Missed optimization opportunity for AddI

XMLWordPrintable

    • Type: Enhancement
    • Resolution: Unresolved
    • Priority: P4
    • tbd
    • Affects Version/s: 26
    • Component/s: hotspot

      Int and long addition can suffer similar issues to JDK-8373134, but the situation arises from a combination of add and subtract. E.g.

      ```
          // int c = a * i; is going to optimize to c = a;
          // Then (a + (b - c)) will become (a + (b - a)),
          // which should optimize to b
          private static int test(int b, int a)
          {
              int i;
              for (i = -10; i < 1; i++)
              {
              }
              int c = a * i;
              return a + (b - c);
          }
      ```

      This has already been noted as seen in `PhaseIterGVN::verify_Identity_for`:

      ```
         // AddINode::Identity
          // Converts (x-y)+y to x
          // Could be issue with notification
          //
          // Turns out AddL does the same.
          //
          // Found with:
          // compiler/c2/Test6792161.java
          // -ea -esa -XX:CompileThreshold=100 -XX:+UnlockExperimentalVMOptions -server -XX:-TieredCompilation -XX:+IgnoreUnrecognizedVMOptions -XX:VerifyIterativeGVN=1110
          case Op_AddI:
          case Op_AddL:
            return false;
      ```

      If you remove the above and run the test it fails like:

      ```
      Missed Identity optimization:
      Old node:
      dist dump
      ---------------------------------------------
         1 114 SubI === _ 10 11 [[ 115 ]] !jvms: TestIntAdd::test @ bci:21 (line 34)
         1 11 Parm === 3 [[ 114 115 71 82 38 49 60 ]] Parm1: int !orig=[113] !jvms: TestIntAdd::test @ bci:-1 (line 30)
         0 115 AddI === _ 11 114 [[ 116 ]] !jvms: TestIntAdd::test @ bci:22 (line 34)
      New node:
      dist dump
      ---------------------------------------------
         1 3 Start === 3 0 [[ 3 5 6 7 8 9 10 11 ]] #{0:control, 1:abIO, 2:memory, 3:rawptr:BotPTR, 4:return_address, 5:int, 6:int}
         0 10 Parm === 3 [[ 114 60 82 71 38 49 ]] Parm0: int !jvms: TestIntAdd::test @ bci:-1 (line 30)
      ```

            Assignee:
            Unassigned
            Reporter:
            Galder ZamarreƱo
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: