class DeadAssignmentsElimination extends NodeTraversal.AbstractPostOrderCallback implements CompilerPass, NodeTraversal.ScopedCallback
LiveVariablesAnalysis
. If there is an assignment to variable
x
and x
is dead after this assignment, we know that the
current content of x
will not be read and this assignment is useless.Modifier and Type | Class and Description |
---|---|
private static class |
DeadAssignmentsElimination.VariableLiveness |
Modifier and Type | Field and Description |
---|---|
private AbstractCompiler |
compiler |
private LiveVariablesAnalysis |
liveness |
private static com.google.common.base.Predicate<Node> |
matchRemovableAssigns |
Constructor and Description |
---|
DeadAssignmentsElimination(AbstractCompiler compiler) |
Modifier and Type | Method and Description |
---|---|
private DeadAssignmentsElimination.VariableLiveness |
checkHookBranchReadBeforeKill(Node trueCase,
Node falseCase,
java.lang.String variable) |
void |
enterScope(NodeTraversal t)
Called immediately after entering a new scope.
|
void |
exitScope(NodeTraversal t)
Called immediately before exiting a scope.
|
private DeadAssignmentsElimination.VariableLiveness |
isVariableReadBeforeKill(Node n,
java.lang.String variable)
Give an expression and a variable.
|
private boolean |
isVariableStillLiveWithinExpression(Node n,
Node exprRoot,
java.lang.String variable)
Given a variable, node n in the tree and a sub-tree denoted by exprRoot as
the root, this function returns true if there exists a read of that
variable before a write to that variable that is on the right side of n.
|
void |
process(Node externs,
Node root)
Process the JS with root node root.
|
private void |
tryRemoveAssignment(NodeTraversal t,
Node n,
DataFlowAnalysis.FlowState<LiveVariablesAnalysis.LiveVariableLattice> state) |
private void |
tryRemoveAssignment(NodeTraversal t,
Node n,
Node exprRoot,
DataFlowAnalysis.FlowState<LiveVariablesAnalysis.LiveVariableLattice> state)
Determines if any local variables are dead after the instruction
n
and are assigned within the subtree of n . |
private void |
tryRemoveDeadAssignments(NodeTraversal t,
ControlFlowGraph<Node> cfg)
Try to remove useless assignments from a control flow graph that has been
annotated with liveness information.
|
void |
visit(NodeTraversal t,
Node n,
Node parent)
Visits a node in postorder (after its children have been visited).
|
shouldTraverse
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
shouldTraverse
private final AbstractCompiler compiler
private LiveVariablesAnalysis liveness
private static final com.google.common.base.Predicate<Node> matchRemovableAssigns
public DeadAssignmentsElimination(AbstractCompiler compiler)
public void process(Node externs, Node root)
CompilerPass
process
in interface CompilerPass
externs
- Top of external JS treeroot
- Top of JS treepublic void enterScope(NodeTraversal t)
NodeTraversal.ScopedCallback
enterScope
in interface NodeTraversal.ScopedCallback
public void exitScope(NodeTraversal t)
NodeTraversal.ScopedCallback
exitScope
in interface NodeTraversal.ScopedCallback
public void visit(NodeTraversal t, Node n, Node parent)
NodeTraversal.Callback
Visits a node in postorder (after its children have been visited).
A node is visited only if all its parents should be traversed
(NodeTraversal.Callback.shouldTraverse(NodeTraversal, Node, Node)
).
Implementations can have side effects (e.g. modifying the parse tree).
visit
in interface NodeTraversal.Callback
private void tryRemoveDeadAssignments(NodeTraversal t, ControlFlowGraph<Node> cfg)
t
- The node traversal.cfg
- The control flow graph of the program annotated with liveness
information.private void tryRemoveAssignment(NodeTraversal t, Node n, DataFlowAnalysis.FlowState<LiveVariablesAnalysis.LiveVariableLattice> state)
private void tryRemoveAssignment(NodeTraversal t, Node n, Node exprRoot, DataFlowAnalysis.FlowState<LiveVariablesAnalysis.LiveVariableLattice> state)
n
and are assigned within the subtree of n
. Removes those assignments
if there are any.n
- Target instruction.exprRoot
- The CFG node where the liveness information in state is
still correct.state
- The liveness information at n
.private boolean isVariableStillLiveWithinExpression(Node n, Node exprRoot, java.lang.String variable)
n
- The current node we should look at.exprRoot
- The nodeprivate DeadAssignmentsElimination.VariableLiveness isVariableReadBeforeKill(Node n, java.lang.String variable)
private DeadAssignmentsElimination.VariableLiveness checkHookBranchReadBeforeKill(Node trueCase, Node falseCase, java.lang.String variable)