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 = _ => ()

0 commit comments

Comments
 (0)