public class TypeTightener extends Object
final List foo = new ArrayList();can be tightened from List to ArrayList because no type other than ArrayList can ever be assigned to foo. The return value of the method bar:
Collection bar() { return new LinkedHashSet; }can be tightened from Collection to LinkedHashSet since it will never return any other type. By working in conjunction with
MethodCallTightener
, Type tightening
can eliminate generating run-time dispatch code for polymorphic methods.
Type flow occurs automatically in most JExpressions. But locals, fields,
parameters, and method return types serve as "way points" where type
information is fixed based on the declared type. Type tightening can be done
by analyzing the types "flowing" into each way point, and then updating the
declared type of the way point to be a more specific type than it had before.
Oddly, it's quite possible to tighten a variable to the Null type, which
means either the variable was never assigned, or it was only ever assigned
null. This is great for two reasons:
1) Once a variable has been tightened to null, it will no longer impact the
variables that depend on it.
2) It creates some very interesting opportunities to optimize later, since we
know statically that the value of the variable is always null.
Open issue: we don't handle recursion where a method passes (some of) its own
args to itself or returns its own call result. With our naive analysis, we
can't figure out that tightening might occur.
Type flow is not supported for primitive types, only reference types.Modifier and Type | Class and Description |
---|---|
class |
TypeTightener.FixDanglingRefsVisitor
Replaces dangling null references with dummy calls.
|
class |
TypeTightener.TightenTypesVisitor
Wherever possible, use the type flow information recorded by RecordVisitor
to change the declared type of a field, local, parameter, or method to a
more specific type.
|
public static OptimizerStats exec(JProgram program)
Copyright © 2016. All rights reserved.