Dotty Documentation

0.6.0-bin-SNAPSHOT

class ConstantFold
extends Object with Optimisation

Various constant folding.

  • Starts/ends with the constant folding implemented in typer (ConstFold).

  • Join branches if they are "similar"

  • regularize arithmetic and boolean expressions to have constants on the left, ie. 6 * 2 * a * 5 => 60 * a

  • (if) specific optimisation that propagate booleans, negation, and factor out (nested) if with equivalent branches wrt to isSimilar. For example:
    • if (b) exp else exp → b; exp
    • if (b1) e1 else if (b2) e1 else e2 → if (b1 || b2) e1 else e2
    • if(!b) e1 else e2 → if(b) e2 else e1
  • Constant propagation over pattern matching.

[-] Constructors

ConstantFold ( simplifyPhase: Simplify )

[-] Members

[+] val simplifyPhase : Simplify
[+] def asConst ( tpe: Type ) ( implicit ctx: Context ) : ConstantType
[+] def clear ( ) : Unit

Clears all the state of this optimisation, to be run last.

[+] def isBool ( tpe: Type ) ( implicit ctx: Context ) : Boolean
[+] def isConst ( tpe: Type ) ( implicit ctx: Context ) : Boolean
[+] def isSimilar ( t1: Tree , t2: Tree ) ( implicit ctx: Context ) : Boolean
[+] def transformer ( implicit ctx: Context ) : Tree => Tree

Does the actual Tree => Tree transformation.

[+] def visitor ( implicit ctx: Context ) : Tree => Unit

Gathers information on trees (using mutation), to be run first.