Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 350209 - [compiler] Compiler produces unnecessary goto statement blocks for ternary operator
Summary: [compiler] Compiler produces unnecessary goto statement blocks for ternary op...
Status: CLOSED WONTFIX
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.7   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Manoj N Palat CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks:
 
Reported: 2011-06-23 23:26 EDT by shalaku_dx CLA
Modified: 2020-01-31 12:12 EST (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description shalaku_dx CLA 2011-06-23 23:26:51 EDT
Build Identifier: 20110609-2236

return (a < b) ? 1 : (a > b) -1 : 0;

  public final test2(int,int) : int
    ILOAD 1
    ILOAD 2
    IF_ICMPGE L0
    ICONST_1
    GOTO L1
   L0
   FRAME SAME
    ILOAD 1
    ILOAD 2
    IF_ICMPLE L2
    ICONST_M1
    GOTO L1
   L2
   FRAME SAME
    ICONST_0
   L1
   FRAME SAME1 int
    IRETURN
    MAXSTACK = 2
    MAXLOCALS = 3

should at least return the same bytecode as below (if not better, ternary operator is known to be faster in some languages/platforms)

if (a < b) return 1;
if (a > b) return -1;
return 0;

  public final test(int,int) : int
    ILOAD 1
    ILOAD 2
    IF_ICMPGE L0
    ICONST_1
    IRETURN
   L0
   FRAME SAME
    ILOAD 1
    ILOAD 2
    IF_ICMPLE L1
    ICONST_M1
    IRETURN
   L1
   FRAME SAME
    ICONST_0
    IRETURN
    MAXSTACK = 2
    MAXLOCALS = 3

Note how there is an extra L2 label and the goto's.

Reproducible: Always

Steps to Reproduce:
1. Write the Java code in an IF statement.
2. Write the Java code using the ternary operator.
3. Compare the bytecode.
Comment 1 Srikanth Sankaran CLA 2012-10-16 04:12:56 EDT
Manoj, Please take a look.
Comment 2 Eclipse Genie CLA 2020-01-31 12:12:58 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. As such, we're closing this bug.

If you have further information on the current state of the bug, please add it and reopen this bug. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.