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
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
- relates to
-
JDK-8137309 Long/Integer.compareTo code generation could be improved
-
- Open
-
-
JDK-8211518 C2 should optimize if diamonds that feed into Phis merging the same value
-
- In Progress
-
-
JDK-8212553 [TESTBUG] TestTrichotomyExpressions.java times out with Graal as JIT
-
- Resolved
-