JDK-8210215 : C2 should optimize trichotomy calculations
  • Type: Enhancement
  • Component: hotspot
  • Sub-Component: compiler
  • Affected Version: 9,10,11,12
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2018-08-30
  • Updated: 2022-12-15
  • Resolved: 2018-10-16
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 12
12 b16Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Description
C2 does not optimize expressions like the following:

    public static int compare1(int a, int b) {
        return (a < b) ? -1 : (a == b) ? 0 : 1; 
    }

    public static boolean testGreater1(int a, int b) {
        return compare1(a, b) == 1;
    }

The check in testGreater1 should be replaced by a simple a > b check but currently C2 emits two comparisons:

  # parm0:    rsi       = int
  # parm1:    rdx       = int
  #           [sp+0x20]  (sp of caller)
 ;; N1: #	B1 <- B3  Freq: 1

 ;; B1: #	B4 B2 <- BLOCK HEAD IS JUNK   Freq: 1

  0x00007f56984da0c0: mov    %eax,-0x16000(%rsp)
  0x00007f56984da0c7: push   %rbp
  0x00007f56984da0c8: sub    $0x10,%rsp         ;*synchronization entry
                                                ; - MyTest::testGreater1@-1 (line 228)

  0x00007f56984da0cc: cmp    %edx,%esi
  0x00007f56984da0ce: jge    0x00007f56984da0e2  ;*if_icmpge {reexecute=0 rethrow=0 return_oop=0}
                                                ; - MyTest::compare1@2 (line 19)
                                                ; - MyTest::testGreater1@2 (line 228)

 ;; B2: #	B3 <- B4 B1  Freq: 0,666659

  0x00007f56984da0d0: xor    %eax,%eax          ;*ireturn {reexecute=0 rethrow=0 return_oop=0}
                                                ; - MyTest::testGreater1@14 (line 228)

 ;; B3: #	N1 <- B2 B5  Freq: 1

  0x00007f56984da0d2: add    $0x10,%rsp
  0x00007f56984da0d6: pop    %rbp
  0x00007f56984da0d7: mov    0x120(%r15),%r10
  0x00007f56984da0de: test   %eax,(%r10)        ;   {poll_return}
  0x00007f56984da0e1: retq   
 ;; B4: #	B2 B5 <- B1  Freq: 0,66667

  0x00007f56984da0e2: cmp    %edx,%esi
  0x00007f56984da0e4: je     0x00007f56984da0d0  ;*if_icmpne {reexecute=0 rethrow=0 return_oop=0}
                                                ; - MyTest::compare1@11 (line 19)
                                                ; - MyTest::testGreater1@2 (line 228)

 ;; B5: #	B3 <- B4  Freq: 0,333341

  0x00007f56984da0e6: mov    $0x1,%eax
  0x00007f56984da0eb: jmp    0x00007f56984da0d2

See discussion at:
http://mail.openjdk.java.net/pipermail/valhalla-dev/2018-August/004883.html
Comments
http://cr.openjdk.java.net/~thartmann/8210215/webrev.05/
16-10-2018

URL: http://hg.openjdk.java.net/jdk/jdk/rev/ad6384355aa3 User: thartmann Date: 2018-10-16 12:17:37 +0000
16-10-2018