| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.common.base.Preconditions; |
| 20 |
import com.google.common.base.Predicate; |
| 21 |
import com.google.common.base.Predicates; |
| 22 |
import com.google.javascript.jscomp.ControlFlowGraph.Branch; |
| 23 |
import com.google.javascript.jscomp.DataFlowAnalysis.FlowState; |
| 24 |
import com.google.javascript.jscomp.LiveVariablesAnalysis.LiveVariableLattice; |
| 25 |
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; |
| 26 |
import com.google.javascript.jscomp.NodeTraversal.ScopedCallback; |
| 27 |
import com.google.javascript.jscomp.Scope.Var; |
| 28 |
import com.google.javascript.jscomp.graph.DiGraph.DiGraphNode; |
| 29 |
import com.google.javascript.rhino.IR; |
| 30 |
import com.google.javascript.rhino.Node; |
| 31 |
import com.google.javascript.rhino.Token; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
class DeadAssignmentsElimination extends AbstractPostOrderCallback implements |
| 41 |
CompilerPass, ScopedCallback { |
| 42 |
|
| 43 |
private final AbstractCompiler compiler; |
| 44 |
private LiveVariablesAnalysis liveness; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
private static final Predicate<Node> matchRemovableAssigns = |
| 50 |
new Predicate<Node>() { |
| 51 |
@Override |
| 52 |
public boolean apply(Node n) { |
| 53 |
return (NodeUtil.isAssignmentOp(n) && |
| 54 |
n.getFirstChild().isName()) || |
| 55 |
n.isInc() || n.isDec(); |
| 56 |
} |
| 57 |
}; |
| 58 |
|
| 59 |
public DeadAssignmentsElimination(AbstractCompiler compiler) { |
| 60 |
this.compiler = compiler; |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public void process(Node externs, Node root) { |
| 65 |
Preconditions.checkNotNull(externs); |
| 66 |
Preconditions.checkNotNull(root); |
| 67 |
NodeTraversal.traverse(compiler, root, this); |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public void enterScope(NodeTraversal t) { |
| 72 |
Scope scope = t.getScope(); |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
if (scope.isGlobal()) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
if (LiveVariablesAnalysis.MAX_VARIABLES_TO_ANALYZE < |
| 81 |
t.getScope().getVarCount()) { |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
Node fnBlock = t.getScopeRoot().getLastChild(); |
| 89 |
if (NodeUtil.containsFunction(fnBlock)) { |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
if (!NodeUtil.has(fnBlock, matchRemovableAssigns, |
| 96 |
Predicates.<Node>alwaysTrue())) { |
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
ControlFlowGraph<Node> cfg = t.getControlFlowGraph(); |
| 102 |
liveness = new LiveVariablesAnalysis(cfg, scope, compiler); |
| 103 |
liveness.analyze(); |
| 104 |
tryRemoveDeadAssignments(t, cfg); |
| 105 |
} |
| 106 |
|
| 107 |
@Override |
| 108 |
public void exitScope(NodeTraversal t) { |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
private void tryRemoveDeadAssignments(NodeTraversal t, |
| 124 |
ControlFlowGraph<Node> cfg) { |
| 125 |
Iterable<DiGraphNode<Node, Branch>> nodes = cfg.getDirectedGraphNodes(); |
| 126 |
|
| 127 |
for (DiGraphNode<Node, Branch> cfgNode : nodes) { |
| 128 |
FlowState<LiveVariableLattice> state = |
| 129 |
cfgNode.getAnnotation(); |
| 130 |
Node n = cfgNode.getValue(); |
| 131 |
if (n == null) { |
| 132 |
continue; |
| 133 |
} |
| 134 |
switch (n.getType()) { |
| 135 |
case Token.IF: |
| 136 |
case Token.WHILE: |
| 137 |
case Token.DO: |
| 138 |
tryRemoveAssignment(t, NodeUtil.getConditionExpression(n), state); |
| 139 |
continue; |
| 140 |
case Token.FOR: |
| 141 |
if (!NodeUtil.isForIn(n)) { |
| 142 |
tryRemoveAssignment( |
| 143 |
t, NodeUtil.getConditionExpression(n), state); |
| 144 |
} |
| 145 |
continue; |
| 146 |
case Token.SWITCH: |
| 147 |
case Token.CASE: |
| 148 |
case Token.RETURN: |
| 149 |
if (n.hasChildren()) { |
| 150 |
tryRemoveAssignment(t, n.getFirstChild(), state); |
| 151 |
} |
| 152 |
continue; |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
tryRemoveAssignment(t, n, state); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
private void tryRemoveAssignment(NodeTraversal t, Node n, |
| 161 |
FlowState<LiveVariableLattice> state) { |
| 162 |
tryRemoveAssignment(t, n, n, state); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
private void tryRemoveAssignment(NodeTraversal t, Node n, Node exprRoot, |
| 176 |
FlowState<LiveVariableLattice> state) { |
| 177 |
|
| 178 |
Node parent = n.getParent(); |
| 179 |
|
| 180 |
if (NodeUtil.isAssignmentOp(n) || |
| 181 |
n.isInc() || n.isDec()) { |
| 182 |
|
| 183 |
Node lhs = n.getFirstChild(); |
| 184 |
Node rhs = lhs.getNext(); |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
if (rhs != null) { |
| 189 |
tryRemoveAssignment(t, rhs, exprRoot, state); |
| 190 |
rhs = lhs.getNext(); |
| 191 |
} |
| 192 |
|
| 193 |
Scope scope = t.getScope(); |
| 194 |
if (!lhs.isName()) { |
| 195 |
return; |
| 196 |
} |
| 197 |
String name = lhs.getString(); |
| 198 |
if (!scope.isDeclared(name, false)) { |
| 199 |
return; |
| 200 |
} |
| 201 |
Var var = scope.getVar(name); |
| 202 |
|
| 203 |
if (liveness.getEscapedLocals().contains(var)) { |
| 204 |
return; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
if (rhs != null && |
| 211 |
rhs.isName() && |
| 212 |
rhs.getString().equals(var.name) && |
| 213 |
n.isAssign()) { |
| 214 |
n.removeChild(rhs); |
| 215 |
n.getParent().replaceChild(n, rhs); |
| 216 |
compiler.reportCodeChange(); |
| 217 |
return; |
| 218 |
} |
| 219 |
|
| 220 |
if (state.getOut().isLive(var)) { |
| 221 |
return; |
| 222 |
} |
| 223 |
|
| 224 |
if (state.getIn().isLive(var) && |
| 225 |
isVariableStillLiveWithinExpression(n, exprRoot, var.name)) { |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
return; |
| 237 |
} |
| 238 |
|
| 239 |
if (n.isAssign()) { |
| 240 |
n.removeChild(rhs); |
| 241 |
n.getParent().replaceChild(n, rhs); |
| 242 |
} else if (NodeUtil.isAssignmentOp(n)) { |
| 243 |
n.removeChild(rhs); |
| 244 |
n.removeChild(lhs); |
| 245 |
Node op = new Node(NodeUtil.getOpFromAssignmentOp(n), lhs, rhs); |
| 246 |
parent.replaceChild(n, op); |
| 247 |
} else if (n.isInc() || n.isDec()) { |
| 248 |
if (parent.isExprResult()) { |
| 249 |
parent.replaceChild(n, |
| 250 |
IR.voidNode(IR.number(0).srcref(n))); |
| 251 |
} else if(n.isComma() && n != parent.getLastChild()) { |
| 252 |
parent.removeChild(n); |
| 253 |
} else if (parent.isFor() && !NodeUtil.isForIn(parent) && |
| 254 |
NodeUtil.getConditionExpression(parent) != n) { |
| 255 |
parent.replaceChild(n, IR.empty()); |
| 256 |
} else { |
| 257 |
|
| 258 |
|
| 259 |
return; |
| 260 |
} |
| 261 |
} else { |
| 262 |
|
| 263 |
Preconditions.checkState(false, "Unknown statement"); |
| 264 |
} |
| 265 |
|
| 266 |
compiler.reportCodeChange(); |
| 267 |
return; |
| 268 |
|
| 269 |
} else { |
| 270 |
for (Node c = n.getFirstChild(); c != null;) { |
| 271 |
Node next = c.getNext(); |
| 272 |
if (!ControlFlowGraph.isEnteringNewCfgNode(c)) { |
| 273 |
tryRemoveAssignment(t, c, exprRoot, state); |
| 274 |
} |
| 275 |
c = next; |
| 276 |
} |
| 277 |
return; |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
private boolean isVariableStillLiveWithinExpression( |
| 297 |
Node n, Node exprRoot, String variable) { |
| 298 |
while (n != exprRoot) { |
| 299 |
VariableLiveness state = VariableLiveness.MAYBE_LIVE; |
| 300 |
switch (n.getParent().getType()) { |
| 301 |
case Token.OR: |
| 302 |
case Token.AND: |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
if (n.getNext() != null) { |
| 307 |
state = isVariableReadBeforeKill( |
| 308 |
n.getNext(), variable); |
| 309 |
if (state == VariableLiveness.KILL) { |
| 310 |
state = VariableLiveness.MAYBE_LIVE; |
| 311 |
} |
| 312 |
} |
| 313 |
break; |
| 314 |
|
| 315 |
case Token.HOOK: |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
if (n.getNext() != null && n.getNext().getNext() != null) { |
| 320 |
state = checkHookBranchReadBeforeKill( |
| 321 |
n.getNext(), n.getNext().getNext(), variable); |
| 322 |
} |
| 323 |
break; |
| 324 |
|
| 325 |
default: |
| 326 |
for(Node sibling = n.getNext(); sibling != null; |
| 327 |
sibling = sibling.getNext()) { |
| 328 |
state = isVariableReadBeforeKill(sibling, variable); |
| 329 |
if (state != VariableLiveness.MAYBE_LIVE) { |
| 330 |
break; |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
if (state == VariableLiveness.READ) { |
| 337 |
return true; |
| 338 |
} else if (state == VariableLiveness.KILL) { |
| 339 |
return false; |
| 340 |
} |
| 341 |
n = n.getParent(); |
| 342 |
} |
| 343 |
return false; |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
private enum VariableLiveness { |
| 348 |
MAYBE_LIVE, |
| 349 |
READ, |
| 350 |
KILL, |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
private VariableLiveness isVariableReadBeforeKill( |
| 359 |
Node n, String variable) { |
| 360 |
if (ControlFlowGraph.isEnteringNewCfgNode(n)) { |
| 361 |
return VariableLiveness.MAYBE_LIVE; |
| 362 |
} |
| 363 |
|
| 364 |
if (n.isName() && variable.equals(n.getString())) { |
| 365 |
if (NodeUtil.isVarOrSimpleAssignLhs(n, n.getParent())) { |
| 366 |
Preconditions.checkState(n.getParent().isAssign()); |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
Node rhs = n.getNext(); |
| 371 |
VariableLiveness state = isVariableReadBeforeKill(rhs, variable); |
| 372 |
if (state == VariableLiveness.READ) { |
| 373 |
return state; |
| 374 |
} |
| 375 |
return VariableLiveness.KILL; |
| 376 |
} else { |
| 377 |
return VariableLiveness.READ; |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
switch (n.getType()) { |
| 382 |
|
| 383 |
case Token.OR: |
| 384 |
case Token.AND: |
| 385 |
VariableLiveness v1 = isVariableReadBeforeKill( |
| 386 |
n.getFirstChild(), variable); |
| 387 |
VariableLiveness v2 = isVariableReadBeforeKill( |
| 388 |
n.getLastChild(), variable); |
| 389 |
|
| 390 |
|
| 391 |
if (v1 != VariableLiveness.MAYBE_LIVE) { |
| 392 |
return v1; |
| 393 |
} else if (v2 == VariableLiveness.READ) { |
| 394 |
return VariableLiveness.READ; |
| 395 |
} else { |
| 396 |
return VariableLiveness.MAYBE_LIVE; |
| 397 |
} |
| 398 |
case Token.HOOK: |
| 399 |
VariableLiveness first = isVariableReadBeforeKill( |
| 400 |
n.getFirstChild(), variable); |
| 401 |
if (first != VariableLiveness.MAYBE_LIVE) { |
| 402 |
return first; |
| 403 |
} |
| 404 |
return checkHookBranchReadBeforeKill( |
| 405 |
n.getFirstChild().getNext(), n.getLastChild(), variable); |
| 406 |
|
| 407 |
default: |
| 408 |
|
| 409 |
for (Node child = n.getFirstChild(); |
| 410 |
child != null; child = child.getNext()) { |
| 411 |
VariableLiveness state = isVariableReadBeforeKill(child, variable); |
| 412 |
if (state != VariableLiveness.MAYBE_LIVE) { |
| 413 |
return state; |
| 414 |
} |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
return VariableLiveness.MAYBE_LIVE; |
| 419 |
} |
| 420 |
|
| 421 |
private VariableLiveness checkHookBranchReadBeforeKill( |
| 422 |
Node trueCase, Node falseCase, String variable) { |
| 423 |
VariableLiveness v1 = isVariableReadBeforeKill( |
| 424 |
trueCase, variable); |
| 425 |
VariableLiveness v2 = isVariableReadBeforeKill( |
| 426 |
falseCase, variable); |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
if (v1 == VariableLiveness.READ || v2 == VariableLiveness.READ) { |
| 431 |
return VariableLiveness.READ; |
| 432 |
} else if (v1 == VariableLiveness.KILL && v2 == VariableLiveness.KILL) { |
| 433 |
return VariableLiveness.KILL; |
| 434 |
} else { |
| 435 |
return VariableLiveness.MAYBE_LIVE; |
| 436 |
} |
| 437 |
} |
| 438 |
} |