@@ -32,7 +32,7 @@ class Simplify extends MiniPhaseTransform with IdentityDenotTransformer {
32
32
* The order of optimizations is tuned to converge faster.
33
33
* Reordering them may require quadratically more rounds to finish.
34
34
*/
35
- private val beforeErasure : List [Optimisation ] =
35
+ private def beforeErasure : List [Optimisation ] =
36
36
new InlineCaseIntrinsics ::
37
37
new RemoveUnnecessaryNullChecks ::
38
38
new InlineOptions ::
@@ -49,7 +49,7 @@ class Simplify extends MiniPhaseTransform with IdentityDenotTransformer {
49
49
Nil
50
50
51
51
/** See comment on beforeErasure */
52
- private val afterErasure : List [Optimisation ] =
52
+ private def afterErasure : List [Optimisation ] =
53
53
new Valify (this ) ::
54
54
new Devalify ::
55
55
new Jumpjump ::
@@ -67,26 +67,10 @@ class Simplify extends MiniPhaseTransform with IdentityDenotTransformer {
67
67
*/
68
68
var fuel : Int = - 1
69
69
70
-
71
- /** Using fuel stops any inlining and prevents optimizations from triggering.
72
- * on my tests it gives 20% slowdown, so it is going to be disabled in public builds.
73
- */
74
- private final val useFuel = false
75
-
76
- private var optimisations : List [Optimisation ] = _
77
-
78
70
override def prepareForUnit (tree : Tree )(implicit ctx : Context ) = {
79
71
val maxFuel = ctx.settings.YoptFuel .value
80
- if (! useFuel && maxFuel != ctx.settings.YoptFuel .default)
81
- ctx.error(" Optimization fuel-debugging requires enabling in source, see Simplify.scala::useFuel" )
82
72
if (fuel < 0 && maxFuel > 0 ) // Both defaults are at -1
83
73
fuel = maxFuel
84
-
85
- optimisations = {
86
- val o = if (ctx.erasedTypes) afterErasure else beforeErasure
87
- val p = ctx.settings.YoptPhases .value
88
- if (p.isEmpty) o else o.filter(x => p.contains(x.name))
89
- }
90
74
this
91
75
}
92
76
@@ -95,12 +79,17 @@ class Simplify extends MiniPhaseTransform with IdentityDenotTransformer {
95
79
val ctx0 = ctx
96
80
if (ctx.settings.optimise.value && ! tree.symbol.is(Label )) {
97
81
implicit val ctx : Context = ctx0.withOwner(tree.symbol(ctx0))
82
+ val optimisations = {
83
+ val o = if (ctx.erasedTypes) afterErasure else beforeErasure
84
+ val p = ctx.settings.YoptPhases .value
85
+ if (p.isEmpty) o else o.filter(x => p.contains(x.name))
86
+ }
98
87
99
88
var rhs0 = tree.rhs
100
89
var rhs1 : Tree = null
101
90
while (rhs1 ne rhs0) {
102
91
rhs1 = rhs0
103
- // val context = ctx.withOwner(tree.symbol)
92
+ val context = ctx.withOwner(tree.symbol)
104
93
optimisations.foreach { optimisation => // TODO: fuse for performance
105
94
// Visit
106
95
rhs0.foreachSubTree(optimisation.visitor)
@@ -110,21 +99,7 @@ class Simplify extends MiniPhaseTransform with IdentityDenotTransformer {
110
99
override def transform (tree : Tree )(implicit ctx : Context ): Tree = {
111
100
val innerCtx = if (tree.isDef && tree.symbol.exists) ctx.withOwner(tree.symbol) else ctx
112
101
val childOptimizedTree = super .transform(tree)(innerCtx)
113
-
114
- if (useFuel && fuel == 0 )
115
- childOptimizedTree
116
- else {
117
- val fullyOptimizedTree = optimisation.transformer(ctx)(childOptimizedTree)
118
-
119
- if (useFuel && (tree ne fullyOptimizedTree)) {
120
- if (fuel > 0 ) fuel -= 1
121
- if (fuel != - 1 && fuel < 10 ) {
122
- println(s " ${tree.symbol} was simplified by ${optimisation.name} (fuel= $fuel): ${tree.show}" )
123
- println(s " became after ${optimisation.name}: (fuel= $fuel) ${fullyOptimizedTree.show}" )
124
- }
125
- }
126
- fullyOptimizedTree
127
- }
102
+ printIfDifferent(childOptimizedTree, optimisation.transformer(ctx)(childOptimizedTree), optimisation)
128
103
}
129
104
}.transform(rhs0)
130
105
}
@@ -133,6 +108,23 @@ class Simplify extends MiniPhaseTransform with IdentityDenotTransformer {
133
108
else tree
134
109
} else tree
135
110
}
111
+
112
+ private def printIfDifferent (tree1 : Tree , tree2 : => Tree , opt : Optimisation )(implicit ctx : Context ): Tree = {
113
+ if (fuel == - 1 )
114
+ tree2 // Does nothing when fuel is disabled.
115
+ else if (fuel == 0 )
116
+ tree1 // No more fuel? No more transformations for you!
117
+ else { // Print the trees if different and consume fuel accordingly.
118
+ if (tree1 ne tree2) {
119
+ if (fuel > 0 ) fuel -= 1
120
+ if (fuel != - 1 ) {
121
+ println(s " ${tree1.symbol} was simplified by ${opt.name} (fuel= $fuel): ${tree1.show}" )
122
+ println(s " became after ${opt.name}: (fuel= $fuel) ${tree2.show}" )
123
+ }
124
+ }
125
+ tree2
126
+ }
127
+ }
136
128
}
137
129
138
130
object Simplify {
0 commit comments