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

Optimize double negation elimination

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P4 P4
    • 17
    • 17
    • hotspot
    • b16
    • generic
    • generic

      public static int neg(int i) {
             return -(-i);
      }

      For this simple test case above, c2 could reduce the redundant 'sub' through SubNode::Ideal. But every time when 'sub' be removed, there would generate two useless nodes.

      The Ideal first transform '0-(0-i)' into 'i - 0', which generated a new SubNode[1]. Then the generated 'i-0' continues to be replaced with 'i+(-0)', a new generated AddNode[2].

       

      Those generated and useless nodes would be removed by later phases, but perhaps it's better to optimize 0-(0-i) to i itself, without new node was generated. 

      There already have code to remove double negation[3], but never been used since they have been transformed to others in Ideal(), which happens before Identity.

      [1] https://github.com/openjdk/jdk/blob/master/src/hotspot/share/opto/subnode.cpp#L238
      [2] https://github.com/openjdk/jdk/blob/master/src/hotspot/share/opto/subnode.cpp#L181
      [3] https://github.com/openjdk/jdk/blob/master/src/hotspot/share/opto/subnode.cpp#L55

       
       

            eliu Eric Liu
            eliu Eric Liu
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: