Skip to content

Local optimisation: Inline local objects #2813

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

Merged
merged 6 commits into from
Jul 10, 2017
Merged
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
5 changes: 2 additions & 3 deletions compiler/src/dotty/tools/dotc/core/NameKinds.scala
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,8 @@ object NameKinds {
val PatMatCaseName = new UniqueNameKind("case")
val PatMatMatchFailName = new UniqueNameKind("matchFail")
val PatMatSelectorName = new UniqueNameKind("selector")
val LocalOptFact = new UniqueNameKind("fact")
val LocalOptSelector = new UniqueNameKind("selector")
val LocalOptFallback = new UniqueNameKind("fallback")

val LocalOptInlineLocalObj = new UniqueNameKind("ilo")

/** The kind of names of default argument getters */
val DefaultGetterName = new NumberedNameKind(DEFAULTGETTER, "DefaultGetter") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class BubbleUpNothing extends Optimisation {
import ast.tpd._

def visitor(implicit ctx: Context) = NoVisitor
def clear(): Unit = ()

def transformer(implicit ctx: Context): Tree => Tree = {
case t @ Apply(Select(Notathing(qual), _), args) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import Simplify.desugarIdent
import ast.tpd._

def visitor(implicit ctx: Context) = NoVisitor
def clear(): Unit = ()

def transformer(implicit ctx: Context): Tree => Tree = { x => preEval(x) match {
// TODO: include handling of isInstanceOf similar to one in IsInstanceOfEvaluator
Expand Down
105 changes: 42 additions & 63 deletions compiler/src/dotty/tools/dotc/transform/localopt/Devalify.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ package transform.localopt
import core.Constants.Constant
import core.Contexts.Context
import core.Flags._
import core.NameOps._
import core.Symbols._
import core.Types._
import ast.Trees._
import scala.collection.mutable
import config.Printers.simplify
import Simplify.{desugarIdent, isEffectivelyMutable}
import Simplify._
import transform.SymUtils._

/** Inline vals and remove vals that are aliases to other vals
Expand All @@ -32,6 +31,14 @@ class Devalify extends Optimisation {
// Either a duplicate or a read through series of immutable fields
val copies = mutable.HashMap[Symbol, Tree]()

def clear(): Unit = {
timesUsed.clear()
timesUsedAsType.clear()
defined.clear()
usedInInnerClass.clear()
copies.clear()
}

def visitType(tp: Type)(implicit ctx: Context): Unit = {
tp.foreachPart(x => x match {
case TermRef(NoPrefix, _) =>
Expand Down Expand Up @@ -164,66 +171,38 @@ class Devalify extends Optimisation {
case _ => t
}

def readingOnlyVals(t: Tree)(implicit ctx: Context): Boolean = {
def isGetterOfAImmutableField = t.symbol.isGetter && !t.symbol.is(Mutable)
def isCaseClassWithVar = t.symbol.info.decls.exists(_.is(Mutable))
def isAccessingProductField = t.symbol.exists &&
t.symbol.owner.derivesFrom(defn.ProductClass) &&
t.symbol.owner.is(CaseClass) &&
t.symbol.name.isSelectorName &&
!isCaseClassWithVar // Conservatively covers case class A(var x: Int)
def isImmutableCaseAccessor = t.symbol.is(CaseAccessor) && !t.symbol.is(Mutable)

dropCasts(t) match {
case Typed(exp, _) => readingOnlyVals(exp)

case TypeApply(fun @ Select(rec, _), List(tp)) =>
if ((fun.symbol eq defn.Any_asInstanceOf) && rec.tpe.derivesFrom(tp.tpe.classSymbol))
readingOnlyVals(rec)
else false

case Apply(Select(rec, _), Nil) =>
if (isGetterOfAImmutableField || isAccessingProductField || isImmutableCaseAccessor)
readingOnlyVals(rec)
else false

case Select(rec, _) if t.symbol.is(Method) =>
if (isGetterOfAImmutableField)
readingOnlyVals(rec) // Getter of an immutable field
else if (isAccessingProductField) {
def isImmutableField = {
val fieldId = t.symbol.name.toString.drop(1).toInt - 1
!t.symbol.owner.caseAccessors(ctx)(fieldId).is(Mutable)
}
if (isImmutableField) readingOnlyVals(rec) // Accessing a field of a product
else false
} else if (isImmutableCaseAccessor)
readingOnlyVals(rec)
else false

case t @ Select(qual, _) if !isEffectivelyMutable(t) =>
readingOnlyVals(qual)

case t: Ident if !t.symbol.is(Mutable | Method) && !t.symbol.info.dealias.isInstanceOf[ExprType] =>
desugarIdent(t) match {
case Some(t) => readingOnlyVals(t)
case None => true
}

case t: This => true
// null => false, or the following fails devalify:
// trait I {
// def foo: Any = null
// }
// object Main {
// def main = {
// val s: I = null
// s.foo
// }
// }
case Literal(Constant(null)) => false
case t: Literal => true
case _ => false
}
def readingOnlyVals(t: Tree)(implicit ctx: Context): Boolean = dropCasts(t) match {
case Typed(exp, _) => readingOnlyVals(exp)

case TypeApply(fun @ Select(rec, _), List(tp)) =>
val isAsInstanceOf = fun.symbol == defn.Any_asInstanceOf && rec.tpe.derivesFrom(tp.tpe.classSymbol)
isAsInstanceOf && readingOnlyVals(rec)

case t @ Apply(Select(rec, _), Nil) =>
isImmutableAccessor(t) && readingOnlyVals(rec)

case t @ Select(rec, _) if t.symbol.is(Method) =>
isImmutableAccessor(t) && readingOnlyVals(rec)

case t @ Select(qual, _) if !isEffectivelyMutable(t) =>
readingOnlyVals(qual)

case t: Ident if !t.symbol.is(Mutable | Method) && !t.symbol.info.dealias.isInstanceOf[ExprType] =>
desugarIdent(t).forall(readingOnlyVals)

case t: This => true
// null => false, or the following fails devalify:
// trait I {
// def foo: Any = null
// }
// object Main {
// def main = {
// val s: I = null
// s.foo
// }
// }
case Literal(Constant(null)) => false
case t: Literal => true
case _ => false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import Simplify.isEffectivelyMutable
import ast.tpd._

def visitor(implicit ctx: Context) = NoVisitor
def clear(): Unit = ()

def transformer(implicit ctx: Context): Tree => Tree = {
case t @ If(cond, thenp, elsep) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package transform.localopt

import core.TypeErasure
import core.Contexts.Context
import core.NameOps._
import core.Symbols._
import core.Types._
import core.Flags._
import ast.Trees._
import Simplify.desugarIdent
import Simplify._

/** Removes side effect free statements in blocks and Defdef.
* Flattens blocks (except Closure-blocks)
Expand All @@ -20,6 +19,7 @@ class DropNoEffects(val simplifyPhase: Simplify) extends Optimisation {
import ast.tpd._

def visitor(implicit ctx: Context) = NoVisitor
def clear(): Unit = ()

def transformer(implicit ctx: Context): Tree => Tree = {
// Remove empty blocks
Expand Down Expand Up @@ -79,11 +79,7 @@ class DropNoEffects(val simplifyPhase: Simplify) extends Optimisation {
elsep = nelsep.orElse(if (elsep.isInstanceOf[Literal]) elsep else unitLiteral))

// Accessing a field of a product
case t @ Select(rec, _)
if (t.symbol.isGetter && !t.symbol.is(Mutable | Lazy)) ||
(t.symbol.owner.derivesFrom(defn.ProductClass) && t.symbol.owner.is(CaseClass) && t.symbol.name.isSelectorName) ||
(t.symbol.is(CaseAccessor) && !t.symbol.is(Mutable)) =>

case t @ Select(rec, _) if isImmutableAccessor(t) =>
keepOnlySideEffects(rec)

// !name.eq(nme.TYPE_) && // Keep the .TYPE added by ClassOf, would be needed for AfterErasure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class InlineCaseIntrinsics(val simplifyPhase: Simplify) extends Optimisation {
import ast.tpd._

def visitor(implicit ctx: Context): Tree => Unit = NoVisitor
def clear(): Unit = ()

def transformer(implicit ctx: Context): Tree => Tree = {
// For synthetic applies on case classes (both dotty/scalac)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class InlineLabelsCalledOnce extends Optimisation {
val timesUsed = mutable.HashMap[Symbol, Int]()
val defined = mutable.HashMap[Symbol, DefDef]()

def clear(): Unit = {
timesUsed.clear()
defined.clear()
}

def visitor(implicit ctx: Context): Tree => Unit = {
case d: DefDef if d.symbol.is(Label) =>
var isRecursive = false
Expand Down
Loading