class LocalOpt[BT <: BTypes] extends AnyRef
Optimizations within a single method. Certain optimizations enable others, for example removing
unreachable code can render a try
block empty and enable removeEmptyExceptionHandlers. The
latter in turn enables more unreachable code to be eliminated (the catch
block), so there is
a cyclic dependency. Optimizations that depend on each other are therefore executed in a loop
until reaching a fixpoint.
The optimizations marked UPSTREAM enable optimizations that were already executed, so they cause another iteration in the fixpoint loop.
nullness optimizations: rewrite null-checking branches to GOTO if nullness is known + enables downstream
- unreachable code (null / non-null branch becomes unreachable)
- box-unbox elimination (may render an escaping consumer of a box unreachable)
- stale stores (aload x is replaced by aconst_null if it's known null)
- simplify jumps (replaces conditional jumps by goto, so may enable goto chains)
unreachable code / DCE (removes instructions of basic blocks to which there is no branch) + enables downstream:
- stale stores (loads may be eliminated, removing consumers of a store)
- empty handlers (try blocks may become empty)
- simplify jumps (goto l; [dead code]; l: ..) => remove goto
- stale local variable descriptors
- (not box-unbox, which is implemented using prod-cons, so it doesn't consider dead code)
note that eliminating empty handlers and stale local variable descriptors is required for
correctness, see the comment in the body of methodOptimizations
.
box-unbox elimination (eliminates box-unbox pairs withing the same method) + enables UPSTREAM:
- nullness optimizations (a box extraction operation (unknown nullness) may be rewritten to a read of a non-null local. example in doc comment of box-unbox implementation)
- further box-unbox elimination (e.g. an Integer stored in a Tuple; eliminating the tuple may enable eliminating the Integer) + enables downstream:
- copy propagation (new locals are introduced, may be aliases of existing)
- stale stores (multi-value boxes where not all values are used)
- redundant casts (
("a", "b")._1
: the generic_1
method returnsObject
, a cast to String is added. The cast is redundant after eliminating the tuple.) - empty local variable descriptors (local variables that were holding the box may become unused)
copy propagation (replaces LOAD n to the LOAD m for the smallest m that is an alias of n) + enables downstrem:
- stale stores (a stored value may not be loaded anymore)
- store-load pairs (a load n may now be right after a store n) + NOTE: copy propagation is only executed once, in the first fixpoint loop iteration. none of the other optimizations enables further copy prop. we still run it as part of the loop because it requires unreachable code to be eliminated.
stale stores (replace STORE by POP) + enables downstream:
- push-pop (the new pop may be the single consumer for an instruction)
redundant casts: eliminates casts that are statically known to succeed (uses type propagation) + enables UPSTREAM:
- box-unbox elimination (a removed checkcast may be a box consumer) + enables downstream:
- push-pop for closure allocation elimination (every indyLambda is followed by a checkcast, see SI-9540)
push-pop (when a POP is the only consumer of a value, remove the POP and its producer) + enables UPSTREAM:
- stale stores (if a LOAD is removed, a corresponding STORE may become stale)
- box-unbox elimination (push-pop may eliminate a closure allocation, rendering a captured box non-escaping) + enables downstream:
- store-load pairs (a variable may become non-live)
- stale handlers (push-pop removes code)
- simplify jumps (push-pop removes code)
store-load pairs (remove STORE x; LOAD x
if x is otherwise not used in the method)
+ enables downstream:
- empty handlers (code is removes, a try block may become empty
- simplify jumps (code is removed, a goto may become redundant for example)
- stale local variable descriptors
empty handlers (removes exception handlers whose try block is empty) + enables UPSTREAM:
- unreachable code (catch block becomes unreachable)
- box-unbox (a box may be escape in an operation in a dead handler) + enables downstream:
- simplify jumps
simplify jumps (various, like GOTO l; l: ...
, see doc comments of individual optimizations)
+ enables UPSTREAM
- unreachable code (
GOTO a; a: GOTO b; b: ...
, the first jump is changed toGOTO b
, the second becomes unreachable) - store-load pairs (a
GOTO l; l: ...
is removed between store and load) - push-pop (
IFNULL l; l: ...
is replaced byPOP
)
The following cleanup optimizations don't enable any upstream optimizations, so they can be executed once at the end, when the above optimizations reach a fixpoint.
empty local variable descriptors (removes unused variables from the local variable table) + enables downstream:
- stale labels (labels that the entry points to, if not otherwise referenced)
empty line numbers (eliminates line number nodes that describe no executable instructions) + enables downstream:
- stale labels (label of the line number node, if not otherwise referenced)
stale labels (eliminate labels that are not referenced, merge sequences of label definitions)
Note on a method's maxLocals / maxStack: the backend only uses those values for running Analyzers. The values can be conservative approximations: if an optimization removes code and the maximal stack size is now smaller, the larger maxStack value will still work fine for running an Analyzer (just that frames allocate more space than required). The correct max values written to the bytecode are re-computed during classfile serialization. To keep things simpler, we don't update the max values in every optimization:
- we do it in
removeUnreachableCodeImpl
, because it's quite straightforward - maxLocals is updated in
compactLocalVariables
, which runs at the end of method optimizations
Note on updating the call graph: whenever an optimization eliminates a callsite or a closure instantiation, we eliminate the corresponding entry from the call graph.
- Source
- LocalOpt.scala
- Alphabetic
- By Inheritance
- LocalOpt
- AnyRef
- Any
- by any2stringadd
- by StringFormat
- by Ensuring
- by ArrowAssoc
- Hide All
- Show All
- Public
- All
Instance Constructors
- new LocalOpt(btypes: BT)
Value Members
-
final
def
!=(arg0: Any): Boolean
Test two objects for inequality.
Test two objects for inequality.
- returns
true
if !(this == that), false otherwise.
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
Equivalent to
x.hashCode
except for boxed numeric types andnull
.Equivalent to
x.hashCode
except for boxed numeric types andnull
. For numerics, it returns a hash value which is consistent with value equality: if two value type instances compare as true, then ## will produce the same hash value for each of them. Fornull
returns a hashcode wherenull.hashCode
throws aNullPointerException
.- returns
a hash value consistent with ==
- Definition Classes
- AnyRef → Any
- def +(other: String): String
- def ->[B](y: B): (LocalOpt[BT], B)
-
final
def
==(arg0: Any): Boolean
The expression
x == that
is equivalent toif (x eq null) that eq null else x.equals(that)
.The expression
x == that
is equivalent toif (x eq null) that eq null else x.equals(that)
.- returns
true
if the receiver object is equivalent to the argument;false
otherwise.
- Definition Classes
- AnyRef → Any
-
final
def
asInstanceOf[T0]: T0
Cast the receiver object to be of type
T0
.Cast the receiver object to be of type
T0
.Note that the success of a cast at runtime is modulo Scala's erasure semantics. Therefore the expression
1.asInstanceOf[String]
will throw aClassCastException
at runtime, while the expressionList(1).asInstanceOf[List[String]]
will not. In the latter example, because the type argument is erased as part of compilation it is not possible to check whether the contents of the list are of the requested type.- returns
the receiver object.
- Definition Classes
- Any
- Exceptions thrown
ClassCastException
if the receiver object is not an instance of the erasure of typeT0
.
- val boxUnbox: BoxUnbox[BT]
- val btypes: BT
-
def
clone(): AnyRef
Create a copy of the receiver object.
Create a copy of the receiver object.
The default implementation of the
clone
method is platform dependent.- returns
a copy of the receiver object.
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
- Note
not specified by SLS as a member of AnyRef
- val copyProp: CopyProp[BT]
-
def
eliminateRedundantCasts(method: MethodNode, owner: InternalName): Boolean
Eliminate
CHECKCAST
instructions that are statically known to succeed.Eliminate
CHECKCAST
instructions that are statically known to succeed. This is safe if the tested object is null:null.asInstanceOf
always succeeds.The type of the tested object is determined using a NonLubbingTypeFlowAnalyzer. Note that this analysis collapses LUBs of non-equal references types to Object for simplicity. Example: given
B <: A <: Object
, the cast in(if (..) new B else new A).asInstanceOf[A]
would not be eliminated.Note: we cannot replace
INSTANCEOF
tests by only looking at the types,null.isInstanceOf
always returns false, so we'd also need nullness information. - def ensuring(cond: (LocalOpt[BT]) ⇒ Boolean, msg: ⇒ Any): LocalOpt[BT]
- def ensuring(cond: (LocalOpt[BT]) ⇒ Boolean): LocalOpt[BT]
- def ensuring(cond: Boolean, msg: ⇒ Any): LocalOpt[BT]
- def ensuring(cond: Boolean): LocalOpt[BT]
-
final
def
eq(arg0: AnyRef): Boolean
Tests whether the argument (
that
) is a reference to the receiver object (this
).Tests whether the argument (
that
) is a reference to the receiver object (this
).The
eq
method implements an equivalence relation on non-null instances ofAnyRef
, and has three additional properties:- It is consistent: for any non-null instances
x
andy
of typeAnyRef
, multiple invocations ofx.eq(y)
consistently returnstrue
or consistently returnsfalse
. - For any non-null instance
x
of typeAnyRef
,x.eq(null)
andnull.eq(x)
returnsfalse
. null.eq(null)
returnstrue
.
When overriding the
equals
orhashCode
methods, it is important to ensure that their behavior is consistent with reference equality. Therefore, if two objects are references to each other (o1 eq o2
), they should be equal to each other (o1 == o2
) and they should hash to the same value (o1.hashCode == o2.hashCode
).- returns
true
if the argument is a reference to the receiver object;false
otherwise.
- Definition Classes
- AnyRef
- It is consistent: for any non-null instances
-
def
equals(arg0: Any): Boolean
The equality method for reference types.
-
def
finalize(): Unit
Called by the garbage collector on the receiver object when there are no more references to the object.
Called by the garbage collector on the receiver object when there are no more references to the object.
The details of when and if the
finalize
method is invoked, as well as the interaction betweenfinalize
and non-local returns and exceptions, are all platform dependent.- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
- Note
not specified by SLS as a member of AnyRef
- def formatted(fmtstr: String): String
-
final
def
getClass(): Class[_]
Returns the runtime class representation of the object.
Returns the runtime class representation of the object.
- returns
a class object corresponding to the runtime type of the receiver.
- Definition Classes
- AnyRef → Any
-
def
hashCode(): Int
The hashCode method for reference types.
-
final
def
isInstanceOf[T0]: Boolean
Test whether the dynamic type of the receiver object is
T0
.Test whether the dynamic type of the receiver object is
T0
.Note that the result of the test is modulo Scala's erasure semantics. Therefore the expression
1.isInstanceOf[String]
will returnfalse
, while the expressionList(1).isInstanceOf[List[String]]
will returntrue
. In the latter example, because the type argument is erased as part of compilation it is not possible to check whether the contents of the list are of the specified type.- returns
true
if the receiver object is an instance of erasure of typeT0
;false
otherwise.
- Definition Classes
- Any
-
def
methodOptimizations(method: MethodNode, ownerClassName: InternalName): Boolean
Run method-level optimizations, see comment on class LocalOpt.
Run method-level optimizations, see comment on class LocalOpt.
Returns
true
if the bytecode ofmethod
was changed. -
def
methodOptimizations(clazz: ClassNode): Boolean
Remove unreachable instructions from all (non-abstract) methods and apply various other cleanups to the bytecode.
Remove unreachable instructions from all (non-abstract) methods and apply various other cleanups to the bytecode.
- clazz
The class whose methods are optimized
- returns
true
if unreachable code was eliminated in some method,false
otherwise.
-
def
minimalRemoveUnreachableCode(method: MethodNode, ownerClassName: InternalName): Boolean
Remove unreachable code from a method.
Remove unreachable code from a method.
This implementation only removes instructions that are unreachable for an ASM analyzer / interpreter. This ensures that future analyses will not produce
null
frames. The inliner depends on this property.- returns
A set containing the eliminated instructions
-
final
def
ne(arg0: AnyRef): Boolean
Equivalent to
!(this eq that)
.Equivalent to
!(this eq that)
.- returns
true
if the argument is not a reference to the receiver object;false
otherwise.
- Definition Classes
- AnyRef
-
final
def
notify(): Unit
Wakes up a single thread that is waiting on the receiver object's monitor.
Wakes up a single thread that is waiting on the receiver object's monitor.
- Definition Classes
- AnyRef
- Note
not specified by SLS as a member of AnyRef
-
final
def
notifyAll(): Unit
Wakes up all threads that are waiting on the receiver object's monitor.
Wakes up all threads that are waiting on the receiver object's monitor.
- Definition Classes
- AnyRef
- Note
not specified by SLS as a member of AnyRef
-
def
nullnessOptimizations(method: MethodNode, ownerClassName: InternalName): Boolean
Apply various optimizations based on nullness analysis information.
Apply various optimizations based on nullness analysis information.
- IFNULL / IFNONNULL are rewritten to GOTO if nullness is known
- IF_ACMPEQ / IF_ACMPNE are rewritten to GOTO if the both references are known null, or if one is known null and the other known not-null
- ALOAD is replaced by ACONST_NULL if the local is known to hold null
- ASTORE of null is removed if the local is known to hold null
- INSTANCEOF of null is replaced by
ICONST_0
- scala.runtime.BoxesRunTime.unboxToX(null) is rewritten to a zero-value load
-
def
removeUnreachableCodeImpl(method: MethodNode, ownerClassName: InternalName): (Boolean, Set[LabelNode])
Removes unreachable basic blocks.
Removes unreachable basic blocks.
- returns
A set containing eliminated instructions, and a set containing all live label nodes.
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toString(): String
Creates a String representation of this object.
Creates a String representation of this object. The default representation is platform dependent. On the java platform it is the concatenation of the class name, "@", and the object's hashcode in hexadecimal.
- returns
a String representation of the object.
- Definition Classes
- AnyRef → Any
-
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
- def →[B](y: B): (LocalOpt[BT], B)
The Scala compiler API.
The following resources are useful for Scala plugin/compiler development: