public class DeterministicCodeOptimizer extends ClassDeclarationFinder
Modifier and Type | Field and Description |
---|---|
protected Map<Expression,Boolean> |
constants
The map contains known to be effectively-final expression.
|
protected Map<Expression,ParameterExpression> |
dedup
The map that de-duplicates expressions, so the same expressions may reuse
the same final static fields.
|
protected Map<String,ParameterExpression> |
fieldsByName
The map of all the added final static fields.
|
addedDeclarations, parent
Constructor and Description |
---|
DeterministicCodeOptimizer(ClassDeclarationFinder parent)
Creates a child optimizer.
|
Modifier and Type | Method and Description |
---|---|
protected boolean |
allMethodsDeterministic(Class klass)
Checks if all the methods in given class are deterministic (i.e.
|
protected Expression |
createField(Expression expression)
Creates final static field to hold the given expression.
|
protected ParameterExpression |
findDeclaredExpression(Expression expression)
Finds if there exists ready for reuse declaration for given expression.
|
protected DeterministicCodeOptimizer |
goDeeper()
Creates child visitor.
|
protected boolean |
hasField(String name)
Verifies if the variable name is already in use.
|
protected String |
inventFieldName(Expression expression)
Generates field name to store given expression.
|
protected boolean |
isConstant(Expression expression)
Verifies if the expression is effectively constant.
|
protected boolean |
isConstructorDeterministic(NewExpression newExpression)
Checks if new instance creation can be reused.
|
protected boolean |
isMethodDeterministic(Method method)
Checks if given method is deterministic (i.e.
|
protected void |
learnFinalStaticDeclarations(List<MemberDeclaration> memberDeclarations)
Processes the list of declarations and learns final static ones as
effectively constant.
|
protected Expression |
tryOptimizeMethodCall(MethodCallExpression methodCallExpression)
Optimized method call, possibly converting it to final static field.
|
protected Expression |
tryOptimizeNewInstance(NewExpression newExpression)
Optimizes
new Type() constructs, |
Expression |
visit(BinaryExpression binaryExpression,
Expression expression0,
Expression expression1) |
MemberDeclaration |
visit(FieldDeclaration fieldDeclaration,
Expression initializer) |
Expression |
visit(MemberExpression memberExpression,
Expression expression) |
Expression |
visit(MethodCallExpression methodCallExpression,
Expression targetExpression,
List<Expression> expressions) |
Expression |
visit(TernaryExpression ternaryExpression,
Expression expression0,
Expression expression1,
Expression expression2) |
Expression |
visit(TypeBinaryExpression typeBinaryExpression,
Expression expression) |
Expression |
visit(UnaryExpression unaryExpression,
Expression expression) |
create, create, create, isConstant, optimizeDeclarations, preVisit, preVisit, visit, visit
preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit
protected final Map<Expression,Boolean> constants
ParameterExpression
, however there might be
non-factored to final field expression that is known to be constant.
For instance, cast expression will not be factored to a field,
but we still need to track its constant status.protected final Map<Expression,ParameterExpression> dedup
protected final Map<String,ParameterExpression> fieldsByName
public DeterministicCodeOptimizer(ClassDeclarationFinder parent)
parent
- parent optimizerprotected Expression tryOptimizeNewInstance(NewExpression newExpression)
new Type()
constructs,tryOptimizeNewInstance
in class ClassDeclarationFinder
newExpression
- expression to optimizepublic Expression visit(BinaryExpression binaryExpression, Expression expression0, Expression expression1)
public Expression visit(TernaryExpression ternaryExpression, Expression expression0, Expression expression1, Expression expression2)
public Expression visit(UnaryExpression unaryExpression, Expression expression)
public Expression visit(TypeBinaryExpression typeBinaryExpression, Expression expression)
protected Expression tryOptimizeMethodCall(MethodCallExpression methodCallExpression)
methodCallExpression
- method call to optimizepublic Expression visit(MethodCallExpression methodCallExpression, Expression targetExpression, List<Expression> expressions)
public Expression visit(MemberExpression memberExpression, Expression expression)
public MemberDeclaration visit(FieldDeclaration fieldDeclaration, Expression initializer)
protected void learnFinalStaticDeclarations(List<MemberDeclaration> memberDeclarations)
learnFinalStaticDeclarations
in class ClassDeclarationFinder
memberDeclarations
- list of declarations to search finals fromprotected ParameterExpression findDeclaredExpression(Expression expression)
findDeclaredExpression
in class ClassDeclarationFinder
expression
- input expressionprotected Expression createField(Expression expression)
expression
- expression to store in final fieldprotected String inventFieldName(Expression expression)
"_$L4J$C$"
suffix is
added to avoid conflicts with other variables.
When multiple variables are mangled to the same name,
counter is used to avoid conflicts.expression
- input expressionprotected boolean isConstant(Expression expression)
ConstantExpression
or
ParameterExpression
).
The method verifies parent chain since the expression might be defined
in enclosing class.isConstant
in class ClassDeclarationFinder
expression
- expression to testprotected boolean isMethodDeterministic(Method method)
method
- method to testprotected boolean isConstructorDeterministic(NewExpression newExpression)
new
BigInteger("42")
is effectively final and can be reused.newExpression
- method to testprotected boolean allMethodsDeterministic(Class klass)
klass
- class to testprotected boolean hasField(String name)
fieldsByName
are
verified. The method verifies parent chain.hasField
in class ClassDeclarationFinder
name
- name of the variable to testprotected DeterministicCodeOptimizer goDeeper()
goDeeper
in class ClassDeclarationFinder
Copyright © 2012-2014 Julian Hyde. All Rights Reserved.