Skip to content

the ICode optimizer doesn't DCE-away trivial branches #7560

@scabug

Description

@scabug

The discussion at scala/scala#2620
uncovered that the ICode-based optimizer's DCE doesn't eliminate a redundant conditional jump,
where both branches lead to one and the same instruction.

The example showing the above is test/files/jvm/bytecode-test-example/Foo_1.scala reproduced below:

class Foo_1 {
  def foo(x: AnyRef): Int = {
    val bool = x == null
    if (x != null)
      1
    else
      0
  }
}

With GenASM and -optimise we get the two conditional jumps that bytecode-test-example.check expects:

  public foo(Ljava/lang/Object;)I
   L0
    ALOAD 1
    IFNONNULL L1
   L1
    ALOAD 1
    IFNONNULL L2
   L3
    ICONST_0
    GOTO L4
   L2
    ICONST_1
   L4
    IRETURN
   L5
    LOCALVARIABLE this LFoo_1; L0 L5 0
    LOCALVARIABLE x Ljava/lang/Object; L0 L5 1
    MAXSTACK = 1
    MAXLOCALS = 2

including a useless

    ALOAD 1
    IFNONNULL L1
   L1

(useless because, whether the conditional jump is taken or not, L1 is the target anyway)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions