Skip to content

Closes #23681: Unnecessary reference duplicate in backend #23685

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions compiler/src/dotty/tools/backend/jvm/BCodeBodyBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1713,26 +1713,23 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
genCZJUMP(success, failure, Primitives.NE, BOOL, targetIfNoJump)
} else {
// l == r -> if (l eq null) r eq null else l.equals(r)
val eqEqTempLocal = locals.makeLocal(ObjectRef, nme.EQEQ_LOCAL_VAR.mangledString, defn.ObjectType, r.span)
val lNull = new asm.Label
val lNonNull = new asm.Label

genLoad(l, ObjectRef)
genLoad(r, ObjectRef) // load rhs --> (r)
stack.push(ObjectRef)
genLoad(r, ObjectRef)
genLoad(l, ObjectRef) // load lhs --> (l,r)
stack.pop()
locals.store(eqEqTempLocal)
bc dup ObjectRef
genCZJUMP(lNull, lNonNull, Primitives.EQ, ObjectRef, targetIfNoJump = lNull)
bc dup ObjectRef // duplicate top stack variable --> (l,l,r)
genCZJUMP(lNull, lNonNull, Primitives.EQ, ObjectRef, targetIfNoJump = lNull) // compare lhs with NULL --> (l,r)

markProgramPoint(lNull)
bc drop ObjectRef
locals.load(eqEqTempLocal)
genCZJUMP(success, failure, Primitives.EQ, ObjectRef, targetIfNoJump = lNonNull)
bc drop ObjectRef // drop top stack variable --> (r)
genCZJUMP(success, failure, Primitives.EQ, ObjectRef, targetIfNoJump = lNonNull) // --> (-)

markProgramPoint(lNonNull)
locals.load(eqEqTempLocal)
genCallMethod(defn.Any_equals, InvokeStyle.Virtual)
emit(asm.Opcodes.SWAP) //swap l and r for correct .equals ordering --> (r,l)
genCallMethod(defn.Any_equals, InvokeStyle.Virtual) // --> (-)
genCZJUMP(success, failure, Primitives.NE, BOOL, targetIfNoJump)
}
}
Expand Down
24 changes: 11 additions & 13 deletions compiler/test/dotty/tools/backend/jvm/DottyBytecodeTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,7 @@ class DottyBytecodeTests extends DottyBytecodeTest {

val m1Meth = getMethod(fooClass, "m1")

assertSameCode(m1Meth, List(
assertSameCode(m1Meth,List(
VarOp(ALOAD, 1),
VarOp(ASTORE, 2),
VarOp(ALOAD, 2),
Expand All @@ -1189,29 +1189,27 @@ class DottyBytecodeTests extends DottyBytecodeTest {
VarOp(ILOAD, 5),
Op(IRETURN),
Label(19),
VarOp(ALOAD, 2),
Field(GETSTATIC, "scala/package$", "MODULE$", "Lscala/package$;"),
Invoke(INVOKEVIRTUAL, "scala/package$", "Nil", "()Lscala/collection/immutable/Nil$;", false),
VarOp(ALOAD, 2),
VarOp(ASTORE, 7),
Op(DUP),
Jump(IFNONNULL, Label(31)),
Jump(IFNONNULL, Label(29)),
Op(POP),
VarOp(ALOAD, 7),
Jump(IFNULL, Label(36)),
Jump(GOTO, Label(40)),
Label(31),
VarOp(ALOAD, 7),
Jump(IFNULL, Label(34)),
Jump(GOTO, Label(38)),
Label(29),
Op(SWAP),
Invoke(INVOKEVIRTUAL, "java/lang/Object", "equals", "(Ljava/lang/Object;)Z", false),
Jump(IFEQ, Label(40)),
Label(36),
Jump(IFEQ, Label(38)),
Label(34),
IntOp(BIPUSH, 20),
Op(IRETURN),
Label(40),
Label(38),
TypeOp(NEW, "scala/MatchError"),
Op(DUP),
VarOp(ALOAD, 2),
Invoke(INVOKESPECIAL, "scala/MatchError", "<init>", "(Ljava/lang/Object;)V", false),
Op(ATHROW),
Op(ATHROW)
))

// ---------------
Expand Down
Loading