Skip to content

Commit d25a221

Browse files
committed
Simplify: remove duplicate null tests.
1 parent 1b6074c commit d25a221

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

src/dotty/tools/dotc/transform/linker/Simplify.scala

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ class Simplify extends MiniPhaseTransform with IdentityDenotTransformer {
387387
hasPerfectRHS(symbol) = true
388388
case Apply(fun, _) if fun.symbol.is(Flags.Label) && (fun.symbol ne symbol) =>
389389
checkGood.put(symbol, checkGood.getOrElse(symbol, Set.empty) + fun.symbol)
390-
assert(forwarderWritesTo.getOrElse(t.symbol, symbol) == symbol)
390+
//assert(forwarderWritesTo.getOrElse(t.symbol, symbol) == symbol)
391391
forwarderWritesTo(t.symbol) = symbol
392392
case t: Ident if !t.symbol.owner.isClass && (t.symbol ne symbol) =>
393393
checkGood.put(symbol, checkGood.getOrElse(symbol, Set.empty) + t.symbol)
@@ -536,13 +536,28 @@ class Simplify extends MiniPhaseTransform with IdentityDenotTransformer {
536536
recur(t)
537537
}
538538

539+
private def collectNullTests(t: Tree)(implicit ctx: Context): List[Symbol] = {
540+
def recur(t: Tree): List[Symbol] =
541+
t match {
542+
case Apply(x, _) if (x.symbol == defn.Boolean_! || x.symbol == defn.Boolean_||) => List.empty
543+
case Apply(fun @ Select(x, _), y) if (fun.symbol == defn.Boolean_&&) => recur(x) ++ recur(y.head)
544+
case Apply(fun @Select(x, _), List(tp)) if fun.symbol eq defn.Object_ne =>
545+
if (x.symbol.exists && !x.symbol.owner.isClass && !x.symbol.is(Flags.Method|Flags.Mutable))
546+
x.symbol :: Nil
547+
else Nil
548+
case _ => List.empty
549+
}
550+
recur(t)
551+
}
552+
539553
val dropGoodCasts: Optimization = { (ctx0: Context) => {
540554
implicit val ctx: Context = ctx0
541555

542556
val transformer: Transformer = () => localCtx => {
543557
case t @ If(cond, thenp, elsep) =>
544-
val newTested = collectTypeTests(cond)
545-
val testedMap = newTested.foldRight[Map[Symbol, List[Type]]](Map.empty)((x, y) =>
558+
val newTypeTested = collectTypeTests(cond)
559+
val nullTested = collectNullTests(cond).toSet
560+
val testedMap = newTypeTested.foldRight[Map[Symbol, List[Type]]](Map.empty)((x, y) =>
546561
y + ((x._1, x._2 :: y.getOrElse(x._1, Nil)))
547562
)
548563
val dropGoodCastsInStats = new TreeMap() {
@@ -555,6 +570,14 @@ class Simplify extends MiniPhaseTransform with IdentityDenotTransformer {
555570
})
556571
if (nstats eq t.stats) t
557572
else Block(nstats, t.expr)
573+
case Apply(fun @ Select(lhs, _), List(Literal(const)))
574+
if const.tag == Constants.NullTag && (fun.symbol == defn.Object_eq || fun.symbol == defn.Object_ne) && nullTested.contains(lhs.symbol) =>
575+
if (fun.symbol == defn.Object_eq) Literal(Constant(false))
576+
else Literal(Constant(true))
577+
case Apply(fun @ Select(Literal(const), _), List(rhs))
578+
if const.tag == Constants.NullTag && (fun.symbol == defn.Object_eq || fun.symbol == defn.Object_ne) && nullTested.contains(rhs.symbol) =>
579+
if (fun.symbol == defn.Object_eq) Literal(Constant(false))
580+
else Literal(Constant(true))
558581
case t => t
559582
}
560583
}

0 commit comments

Comments
 (0)