Skip to content

Commit ec804ad

Browse files
Add clear() method to share instances of optimisations
1 parent df617b3 commit ec804ad

14 files changed

+61
-9
lines changed

compiler/src/dotty/tools/dotc/transform/localopt/BubbleUpNothing.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class BubbleUpNothing extends Optimisation {
2323
import ast.tpd._
2424

2525
def visitor(implicit ctx: Context) = NoVisitor
26+
def clear(): Unit = ()
2627

2728
def transformer(implicit ctx: Context): Tree => Tree = {
2829
case t @ Apply(Select(Notathing(qual), _), args) =>

compiler/src/dotty/tools/dotc/transform/localopt/ConstantFold.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import Simplify.desugarIdent
3030
import ast.tpd._
3131

3232
def visitor(implicit ctx: Context) = NoVisitor
33+
def clear(): Unit = ()
3334

3435
def transformer(implicit ctx: Context): Tree => Tree = { x => preEval(x) match {
3536
// TODO: include handling of isInstanceOf similar to one in IsInstanceOfEvaluator

compiler/src/dotty/tools/dotc/transform/localopt/Devalify.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ class Devalify extends Optimisation {
3131
// Either a duplicate or a read through series of immutable fields
3232
val copies = mutable.HashMap[Symbol, Tree]()
3333

34+
def clear(): Unit = {
35+
timesUsed.clear()
36+
timesUsedAsType.clear()
37+
defined.clear()
38+
usedInInnerClass.clear()
39+
copies.clear()
40+
}
41+
3442
def visitType(tp: Type)(implicit ctx: Context): Unit = {
3543
tp.foreachPart(x => x match {
3644
case TermRef(NoPrefix, _) =>

compiler/src/dotty/tools/dotc/transform/localopt/DropGoodCasts.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import Simplify.isEffectivelyMutable
2424
import ast.tpd._
2525

2626
def visitor(implicit ctx: Context) = NoVisitor
27+
def clear(): Unit = ()
2728

2829
def transformer(implicit ctx: Context): Tree => Tree = {
2930
case t @ If(cond, thenp, elsep) =>

compiler/src/dotty/tools/dotc/transform/localopt/DropNoEffects.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class DropNoEffects(val simplifyPhase: Simplify) extends Optimisation {
1919
import ast.tpd._
2020

2121
def visitor(implicit ctx: Context) = NoVisitor
22+
def clear(): Unit = ()
2223

2324
def transformer(implicit ctx: Context): Tree => Tree = {
2425
// Remove empty blocks

compiler/src/dotty/tools/dotc/transform/localopt/InlineCaseIntrinsics.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class InlineCaseIntrinsics(val simplifyPhase: Simplify) extends Optimisation {
2424
import ast.tpd._
2525

2626
def visitor(implicit ctx: Context): Tree => Unit = NoVisitor
27+
def clear(): Unit = ()
2728

2829
def transformer(implicit ctx: Context): Tree => Tree = {
2930
// For synthetic applies on case classes (both dotty/scalac)

compiler/src/dotty/tools/dotc/transform/localopt/InlineLabelsCalledOnce.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ class InlineLabelsCalledOnce extends Optimisation {
1818
val timesUsed = mutable.HashMap[Symbol, Int]()
1919
val defined = mutable.HashMap[Symbol, DefDef]()
2020

21+
def clear(): Unit = {
22+
timesUsed.clear()
23+
defined.clear()
24+
}
25+
2126
def visitor(implicit ctx: Context): Tree => Unit = {
2227
case d: DefDef if d.symbol.is(Label) =>
2328
var isRecursive = false

compiler/src/dotty/tools/dotc/transform/localopt/InlineOptions.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@ import scala.collection.mutable
1717
class InlineOptions extends Optimisation {
1818
import ast.tpd._
1919

20-
private val somes = mutable.HashMap[Symbol, Tree]()
21-
private val nones = mutable.HashSet[Symbol]()
20+
val somes = mutable.HashMap[Symbol, Tree]()
21+
val nones = mutable.HashSet[Symbol]()
22+
23+
def clear(): Unit = {
24+
somes.clear()
25+
nones.clear()
26+
}
2227

2328
def visitor(implicit ctx: Context): Tree => Unit = {
2429
case valdef: ValDef if !valdef.symbol.is(Mutable) &&

compiler/src/dotty/tools/dotc/transform/localopt/Jumpjump.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class Jumpjump extends Optimisation {
2020

2121
val defined = mutable.HashMap[Symbol, Symbol]()
2222

23+
def clear(): Unit = defined.clear()
24+
2325
def visitor(implicit ctx: Context): Tree => Unit = {
2426
case defdef: DefDef if defdef.symbol.is(Label) =>
2527
defdef.rhs match {

compiler/src/dotty/tools/dotc/transform/localopt/Optimisation.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ import ast.tpd.Tree
66

77
trait Optimisation {
88

9-
/** Run first to gather information on Trees (using mutation) */
9+
/** Gathers information on trees (using mutation), to be run first. */
1010
def visitor(implicit ctx: Context): Tree => Unit
1111

1212
/** Does the actual Tree => Tree transformation. */
1313
def transformer(implicit ctx: Context): Tree => Tree
1414

15+
/** Clears all the state of this optimisation, to be run last. */
16+
def clear(): Unit
17+
1518
def name: String = this.getClass.getSimpleName
1619

1720
val NoVisitor: Tree => Unit = _ => ()

compiler/src/dotty/tools/dotc/transform/localopt/RemoveUnnecessaryNullChecks.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ import scala.collection.mutable
2626

2727
val checkGood = mutable.HashMap[Symbol, Set[Symbol]]()
2828

29+
def clear(): Unit = {
30+
initializedVals.clear()
31+
checkGood.clear()
32+
}
33+
2934
def isGood(t: Symbol)(implicit ctx: Context): Boolean = {
3035
t.exists && initializedVals.contains(t) && {
3136
var changed = true

compiler/src/dotty/tools/dotc/transform/localopt/Simplify.scala

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ class Simplify extends MiniPhaseTransform with IdentityDenotTransformer {
6363
new ConstantFold(this) ::
6464
Nil
6565

66+
var optimisations: List[Optimisation] = Nil
67+
6668
/** Optimisation fuel, for debugging. Decremented every time Simplify
6769
* applies an optimisation until fuel == 0. Original idea from Automatic
6870
* Isolation of Compiler Errors by David Whalley. Unable with -Yopt-fuel.
@@ -76,9 +78,17 @@ class Simplify extends MiniPhaseTransform with IdentityDenotTransformer {
7678
override def prepareForUnit(tree: Tree)(implicit ctx: Context) = {
7779
SeqFactoryClass = ctx.requiredClass("scala.collection.generic.SeqFactory")
7880
CommutativePrimitiveOperations = Set(defn.Boolean_&&, defn.Boolean_||, defn.Int_+, defn.Int_*, defn.Long_+, defn.Long_*)
81+
7982
val maxFuel = ctx.settings.YoptFuel.value
8083
if (fuel < 0 && maxFuel > 0) // Both defaults are at -1
8184
fuel = maxFuel
85+
86+
optimisations = {
87+
val o = if (ctx.erasedTypes) afterErasure else beforeErasure
88+
val p = ctx.settings.YoptPhases.value
89+
if (p.isEmpty) o else o.filter(x => p.contains(x.name))
90+
}
91+
8292
this
8393
}
8494

@@ -87,12 +97,6 @@ class Simplify extends MiniPhaseTransform with IdentityDenotTransformer {
8797
val ctx0 = ctx
8898
if (ctx.settings.optimise.value && !tree.symbol.is(Label)) {
8999
implicit val ctx: Context = ctx0.withOwner(tree.symbol(ctx0))
90-
val optimisations = {
91-
val o = if (ctx.erasedTypes) afterErasure else beforeErasure
92-
val p = ctx.settings.YoptPhases.value
93-
if (p.isEmpty) o else o.filter(x => p.contains(x.name))
94-
}
95-
96100
var rhs0 = tree.rhs
97101
var rhs1: Tree = null
98102
while (rhs1 ne rhs0) {
@@ -110,6 +114,9 @@ class Simplify extends MiniPhaseTransform with IdentityDenotTransformer {
110114
printIfDifferent(childOptimizedTree, optimisation.transformer(ctx)(childOptimizedTree), optimisation)
111115
}
112116
}.transform(rhs0)
117+
118+
// Clean
119+
optimisation.clear()
113120
}
114121
}
115122
if (rhs0 ne tree.rhs) tpd.cpy.DefDef(tree)(rhs = rhs0)

compiler/src/dotty/tools/dotc/transform/localopt/Valify.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ class Valify(val simplifyPhase: Simplify) extends Optimisation {
2424

2525
val secondWrite: mutable.Map[Symbol, Assign] = mutable.Map()
2626

27+
def clear(): Unit = {
28+
defined.clear()
29+
firstRead.clear()
30+
firstWrite.clear()
31+
secondWrite.clear()
32+
}
33+
2734
def visitor(implicit ctx: Context): Tree => Unit = {
2835
case t: ValDef if t.symbol.is(Mutable, Lazy) && !t.symbol.is(Method) && !t.symbol.owner.isClass =>
2936
if (isPureExpr(t.rhs))

compiler/src/dotty/tools/dotc/transform/localopt/Varify.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ import scala.collection.mutable
3131

3232
val possibleRenames = mutable.HashMap[Symbol, Set[Symbol]]()
3333

34+
def clear(): Unit = {
35+
paramsTimesUsed.clear()
36+
possibleRenames.clear()
37+
}
38+
3439
def visitor(implicit ctx: Context): Tree => Unit = {
3540
case t: ValDef if t.symbol.is(Param) =>
3641
paramsTimesUsed += (t.symbol -> 0)

0 commit comments

Comments
 (0)