| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package com.google.javascript.jscomp; |
| 17 |
|
| 18 |
import com.google.common.base.Preconditions; |
| 19 |
import com.google.common.base.Predicate; |
| 20 |
import com.google.common.base.Predicates; |
| 21 |
import com.google.common.base.Supplier; |
| 22 |
import com.google.common.collect.Sets; |
| 23 |
import com.google.javascript.jscomp.ExpressionDecomposer.DecompositionType; |
| 24 |
import com.google.javascript.rhino.Node; |
| 25 |
import com.google.javascript.rhino.Token; |
| 26 |
|
| 27 |
import java.util.Collection; |
| 28 |
import java.util.Map; |
| 29 |
import java.util.Set; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class FunctionInjector { |
| 39 |
|
| 40 |
private final AbstractCompiler compiler; |
| 41 |
private final Supplier<String> safeNameIdSupplier; |
| 42 |
private final boolean allowDecomposition; |
| 43 |
private Set<String> knownConstants = Sets.newHashSet(); |
| 44 |
private final boolean assumeStrictThis; |
| 45 |
private final boolean assumeMinimumCapture; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public FunctionInjector( |
| 53 |
AbstractCompiler compiler, |
| 54 |
Supplier<String> safeNameIdSupplier, |
| 55 |
boolean allowDecomposition, |
| 56 |
boolean assumeStrictThis, |
| 57 |
boolean assumeMinimumCapture) { |
| 58 |
Preconditions.checkNotNull(compiler); |
| 59 |
Preconditions.checkNotNull(safeNameIdSupplier); |
| 60 |
this.compiler = compiler; |
| 61 |
this.safeNameIdSupplier = safeNameIdSupplier; |
| 62 |
this.allowDecomposition = allowDecomposition; |
| 63 |
this.assumeStrictThis = assumeStrictThis; |
| 64 |
this.assumeMinimumCapture = assumeMinimumCapture; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
enum InliningMode { |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
DIRECT, |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
BLOCK |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
static class Reference { |
| 84 |
final Node callNode; |
| 85 |
final JSModule module; |
| 86 |
final InliningMode mode; |
| 87 |
|
| 88 |
Reference(Node callNode, JSModule module, InliningMode mode){ |
| 89 |
this.callNode = callNode; |
| 90 |
this.module = module; |
| 91 |
this.mode = mode; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
private static final int NAME_COST_ESTIMATE = |
| 102 |
InlineCostEstimator.ESTIMATED_IDENTIFIER_COST; |
| 103 |
|
| 104 |
|
| 105 |
private static final int COMMA_COST = 1; |
| 106 |
|
| 107 |
|
| 108 |
private static final int PAREN_COST = 2; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
boolean doesFunctionMeetMinimumRequirements( |
| 120 |
final String fnName, Node fnNode) { |
| 121 |
Node block = NodeUtil.getFunctionBody(fnNode); |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
if (!compiler.getCodingConvention().isInlinableFunction(fnNode)) { |
| 132 |
return false; |
| 133 |
} |
| 134 |
|
| 135 |
final String fnRecursionName = fnNode.getFirstChild().getString(); |
| 136 |
Preconditions.checkState(fnRecursionName != null); |
| 137 |
|
| 138 |
|
| 139 |
boolean referencesArguments = NodeUtil.isNameReferenced( |
| 140 |
block, "arguments", NodeUtil.MATCH_NOT_FUNCTION); |
| 141 |
|
| 142 |
|
| 143 |
Predicate<Node> p = new Predicate<Node>(){ |
| 144 |
@Override |
| 145 |
public boolean apply(Node n) { |
| 146 |
if (n.isName()) { |
| 147 |
return n.getString().equals("eval") |
| 148 |
|| (!fnName.isEmpty() |
| 149 |
&& n.getString().equals(fnName)) |
| 150 |
|| (!fnRecursionName.isEmpty() |
| 151 |
&& n.getString().equals(fnRecursionName)); |
| 152 |
} |
| 153 |
return false; |
| 154 |
} |
| 155 |
}; |
| 156 |
|
| 157 |
return !referencesArguments |
| 158 |
&& !NodeUtil.has(block, p, Predicates.<Node>alwaysTrue()); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
CanInlineResult canInlineReferenceToFunction(NodeTraversal t, |
| 174 |
Node callNode, Node fnNode, Set<String> needAliases, |
| 175 |
InliningMode mode, boolean referencesThis, boolean containsFunctions) { |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
if (!isSupportedCallType(callNode)) { |
| 181 |
return CanInlineResult.NO; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
if (containsFunctions) { |
| 189 |
if (!assumeMinimumCapture && !t.inGlobalScope()) { |
| 190 |
|
| 191 |
|
| 192 |
return CanInlineResult.NO; |
| 193 |
} else if (NodeUtil.isWithinLoop(callNode)) { |
| 194 |
|
| 195 |
|
| 196 |
return CanInlineResult.NO; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
if (referencesThis && !NodeUtil.isFunctionObjectCall(callNode)) { |
| 202 |
|
| 203 |
|
| 204 |
return CanInlineResult.NO; |
| 205 |
} |
| 206 |
|
| 207 |
if (mode == InliningMode.DIRECT) { |
| 208 |
return canInlineReferenceDirectly(callNode, fnNode); |
| 209 |
} else { |
| 210 |
return canInlineReferenceAsStatementBlock( |
| 211 |
t, callNode, fnNode, needAliases); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
private boolean isSupportedCallType(Node callNode) { |
| 221 |
if (!callNode.getFirstChild().isName()) { |
| 222 |
if (NodeUtil.isFunctionObjectCall(callNode)) { |
| 223 |
if (!assumeStrictThis) { |
| 224 |
Node thisValue = callNode.getFirstChild().getNext(); |
| 225 |
if (thisValue == null || !thisValue.isThis()) { |
| 226 |
return false; |
| 227 |
} |
| 228 |
} |
| 229 |
} else if (NodeUtil.isFunctionObjectApply(callNode)) { |
| 230 |
return false; |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
return true; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
Node inline( |
| 241 |
Node callNode, String fnName, Node fnNode, InliningMode mode) { |
| 242 |
Preconditions.checkState(compiler.getLifeCycleStage().isNormalized()); |
| 243 |
|
| 244 |
if (mode == InliningMode.DIRECT) { |
| 245 |
return inlineReturnValue(callNode, fnNode); |
| 246 |
} else { |
| 247 |
return inlineFunction(callNode, fnNode, fnName); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
private Node inlineReturnValue(Node callNode, Node fnNode) { |
| 257 |
Node block = fnNode.getLastChild(); |
| 258 |
Node callParentNode = callNode.getParent(); |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
Map<String, Node> argMap = |
| 266 |
FunctionArgumentInjector.getFunctionCallParameterMap( |
| 267 |
fnNode, callNode, this.safeNameIdSupplier); |
| 268 |
|
| 269 |
Node newExpression; |
| 270 |
if (!block.hasChildren()) { |
| 271 |
Node srcLocation = block; |
| 272 |
newExpression = NodeUtil.newUndefinedNode(srcLocation); |
| 273 |
} else { |
| 274 |
Node returnNode = block.getFirstChild(); |
| 275 |
Preconditions.checkArgument(returnNode.isReturn()); |
| 276 |
|
| 277 |
|
| 278 |
Node safeReturnNode = returnNode.cloneTree(); |
| 279 |
Node inlineResult = FunctionArgumentInjector.inject( |
| 280 |
null, safeReturnNode, null, argMap); |
| 281 |
Preconditions.checkArgument(safeReturnNode == inlineResult); |
| 282 |
newExpression = safeReturnNode.removeFirstChild(); |
| 283 |
} |
| 284 |
|
| 285 |
callParentNode.replaceChild(callNode, newExpression); |
| 286 |
return newExpression; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
private enum CallSiteType { |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
UNSUPPORTED() { |
| 299 |
@Override |
| 300 |
public void prepare(FunctionInjector injector, Node callNode) { |
| 301 |
throw new IllegalStateException("unexpected"); |
| 302 |
} |
| 303 |
}, |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
SIMPLE_CALL() { |
| 311 |
@Override |
| 312 |
public void prepare(FunctionInjector injector, Node callNode) { |
| 313 |
|
| 314 |
} |
| 315 |
}, |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
SIMPLE_ASSIGNMENT() { |
| 326 |
@Override |
| 327 |
public void prepare(FunctionInjector injector, Node callNode) { |
| 328 |
|
| 329 |
} |
| 330 |
}, |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
VAR_DECL_SIMPLE_ASSIGNMENT() { |
| 341 |
@Override |
| 342 |
public void prepare(FunctionInjector injector, Node callNode) { |
| 343 |
|
| 344 |
} |
| 345 |
}, |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
EXPRESSION() { |
| 360 |
@Override |
| 361 |
public void prepare(FunctionInjector injector, Node callNode) { |
| 362 |
injector.getDecomposer().moveExpression(callNode); |
| 363 |
|
| 364 |
|
| 365 |
CallSiteType callSiteType = injector.classifyCallSite(callNode); |
| 366 |
Preconditions.checkState(this != callSiteType); |
| 367 |
callSiteType.prepare(injector, callNode); |
| 368 |
} |
| 369 |
}, |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
DECOMPOSABLE_EXPRESSION() { |
| 377 |
@Override |
| 378 |
public void prepare(FunctionInjector injector, Node callNode) { |
| 379 |
injector.getDecomposer().maybeExposeExpression(callNode); |
| 380 |
|
| 381 |
|
| 382 |
CallSiteType callSiteType = injector.classifyCallSite(callNode); |
| 383 |
Preconditions.checkState(this != callSiteType); |
| 384 |
callSiteType.prepare(injector, callNode); |
| 385 |
} |
| 386 |
}; |
| 387 |
|
| 388 |
public abstract void prepare(FunctionInjector injector, Node callNode); |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
private CallSiteType classifyCallSite(Node callNode) { |
| 395 |
Node parent = callNode.getParent(); |
| 396 |
Node grandParent = parent.getParent(); |
| 397 |
|
| 398 |
|
| 399 |
if (NodeUtil.isExprCall(parent)) { |
| 400 |
|
| 401 |
return CallSiteType.SIMPLE_CALL; |
| 402 |
} else if (NodeUtil.isExprAssign(grandParent) |
| 403 |
&& !NodeUtil.isVarOrSimpleAssignLhs(callNode, parent) |
| 404 |
&& parent.getFirstChild().isName() |
| 405 |
&& !NodeUtil.isConstantName(parent.getFirstChild())) { |
| 406 |
|
| 407 |
return CallSiteType.SIMPLE_ASSIGNMENT; |
| 408 |
} else if (parent.isName() |
| 409 |
&& !NodeUtil.isConstantName(parent) |
| 410 |
&& grandParent.isVar() |
| 411 |
&& grandParent.hasOneChild()) { |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
return CallSiteType.VAR_DECL_SIMPLE_ASSIGNMENT; |
| 416 |
} else { |
| 417 |
Node expressionRoot = ExpressionDecomposer.findExpressionRoot(callNode); |
| 418 |
if (expressionRoot != null) { |
| 419 |
ExpressionDecomposer decomposer = new ExpressionDecomposer( |
| 420 |
compiler, safeNameIdSupplier, knownConstants); |
| 421 |
DecompositionType type = decomposer.canExposeExpression( |
| 422 |
callNode); |
| 423 |
if (type == DecompositionType.MOVABLE) { |
| 424 |
return CallSiteType.EXPRESSION; |
| 425 |
} else if (type == DecompositionType.DECOMPOSABLE) { |
| 426 |
return CallSiteType.DECOMPOSABLE_EXPRESSION; |
| 427 |
} else { |
| 428 |
Preconditions.checkState(type == DecompositionType.UNDECOMPOSABLE); |
| 429 |
} |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
return CallSiteType.UNSUPPORTED; |
| 434 |
} |
| 435 |
|
| 436 |
private ExpressionDecomposer getDecomposer() { |
| 437 |
return new ExpressionDecomposer( |
| 438 |
compiler, safeNameIdSupplier, knownConstants); |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
void maybePrepareCall(Node callNode) { |
| 446 |
CallSiteType callSiteType = classifyCallSite(callNode); |
| 447 |
callSiteType.prepare(this, callNode); |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
private Node inlineFunction( |
| 456 |
Node callNode, Node fnNode, String fnName) { |
| 457 |
Node parent = callNode.getParent(); |
| 458 |
Node grandParent = parent.getParent(); |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
CallSiteType callSiteType = classifyCallSite(callNode); |
| 463 |
Preconditions.checkArgument(callSiteType != CallSiteType.UNSUPPORTED); |
| 464 |
|
| 465 |
boolean isCallInLoop = NodeUtil.isWithinLoop(callNode); |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
String resultName = null; |
| 471 |
boolean needsDefaultReturnResult = true; |
| 472 |
switch (callSiteType) { |
| 473 |
case SIMPLE_ASSIGNMENT: |
| 474 |
resultName = parent.getFirstChild().getString(); |
| 475 |
break; |
| 476 |
|
| 477 |
case VAR_DECL_SIMPLE_ASSIGNMENT: |
| 478 |
resultName = parent.getString(); |
| 479 |
break; |
| 480 |
|
| 481 |
case SIMPLE_CALL: |
| 482 |
resultName = null; |
| 483 |
needsDefaultReturnResult = false; |
| 484 |
break; |
| 485 |
|
| 486 |
case EXPRESSION: |
| 487 |
throw new IllegalStateException( |
| 488 |
"Movable expressions must be moved before inlining."); |
| 489 |
|
| 490 |
case DECOMPOSABLE_EXPRESSION: |
| 491 |
throw new IllegalStateException( |
| 492 |
"Decomposable expressions must be decomposed before inlining."); |
| 493 |
|
| 494 |
default: |
| 495 |
throw new IllegalStateException("Unexpected call site type."); |
| 496 |
} |
| 497 |
|
| 498 |
FunctionToBlockMutator mutator = new FunctionToBlockMutator( |
| 499 |
compiler, this.safeNameIdSupplier); |
| 500 |
|
| 501 |
Node newBlock = mutator.mutate( |
| 502 |
fnName, fnNode, callNode, resultName, |
| 503 |
needsDefaultReturnResult, isCallInLoop); |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
Node greatGrandParent = grandParent.getParent(); |
| 509 |
switch (callSiteType) { |
| 510 |
case VAR_DECL_SIMPLE_ASSIGNMENT: |
| 511 |
|
| 512 |
parent.removeChild(parent.getFirstChild()); |
| 513 |
Preconditions.checkState(parent.getFirstChild() == null); |
| 514 |
|
| 515 |
greatGrandParent.addChildAfter(newBlock, grandParent); |
| 516 |
break; |
| 517 |
|
| 518 |
case SIMPLE_ASSIGNMENT: |
| 519 |
|
| 520 |
|
| 521 |
Preconditions.checkState(grandParent.isExprResult()); |
| 522 |
greatGrandParent.replaceChild(grandParent, newBlock); |
| 523 |
break; |
| 524 |
|
| 525 |
case SIMPLE_CALL: |
| 526 |
|
| 527 |
Preconditions.checkState(parent.isExprResult()); |
| 528 |
grandParent.replaceChild(parent, newBlock); |
| 529 |
break; |
| 530 |
|
| 531 |
default: |
| 532 |
throw new IllegalStateException("Unexpected call site type."); |
| 533 |
} |
| 534 |
|
| 535 |
return newBlock; |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
boolean isDirectCallNodeReplacementPossible(Node fnNode) { |
| 543 |
|
| 544 |
Node block = NodeUtil.getFunctionBody(fnNode); |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
if (!block.hasChildren()) { |
| 549 |
|
| 550 |
return true; |
| 551 |
} else if (block.hasOneChild()) { |
| 552 |
|
| 553 |
if (block.getFirstChild().isReturn() |
| 554 |
&& block.getFirstChild().getFirstChild() != null) { |
| 555 |
return true; |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
return false; |
| 560 |
} |
| 561 |
|
| 562 |
enum CanInlineResult { |
| 563 |
YES, |
| 564 |
AFTER_PREPARATION, |
| 565 |
NO |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
private CanInlineResult canInlineReferenceAsStatementBlock( |
| 580 |
NodeTraversal t, Node callNode, Node fnNode, Set<String> namesToAlias) { |
| 581 |
CallSiteType callSiteType = classifyCallSite(callNode); |
| 582 |
if (callSiteType == CallSiteType.UNSUPPORTED) { |
| 583 |
return CanInlineResult.NO; |
| 584 |
} |
| 585 |
|
| 586 |
if (!allowDecomposition |
| 587 |
&& (callSiteType == CallSiteType.DECOMPOSABLE_EXPRESSION |
| 588 |
|| callSiteType == CallSiteType.EXPRESSION)) { |
| 589 |
return CanInlineResult.NO; |
| 590 |
} |
| 591 |
|
| 592 |
if (!callMeetsBlockInliningRequirements( |
| 593 |
t, callNode, fnNode, namesToAlias)) { |
| 594 |
return CanInlineResult.NO; |
| 595 |
} |
| 596 |
|
| 597 |
if (callSiteType == CallSiteType.DECOMPOSABLE_EXPRESSION |
| 598 |
|| callSiteType == CallSiteType.EXPRESSION) { |
| 599 |
return CanInlineResult.AFTER_PREPARATION; |
| 600 |
} else { |
| 601 |
return CanInlineResult.YES; |
| 602 |
} |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
private boolean callMeetsBlockInliningRequirements( |
| 611 |
NodeTraversal t, Node callNode, final Node fnNode, |
| 612 |
Set<String> namesToAlias) { |
| 613 |
final boolean assumeMinimumCapture = this.assumeMinimumCapture; |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
boolean fnContainsVars = NodeUtil.has( |
| 626 |
NodeUtil.getFunctionBody(fnNode), |
| 627 |
new NodeUtil.MatchDeclaration(), |
| 628 |
new NodeUtil.MatchShallowStatement()); |
| 629 |
boolean forbidTemps = false; |
| 630 |
if (!t.inGlobalScope()) { |
| 631 |
Node fnCaller = t.getScopeRoot(); |
| 632 |
Node fnCallerBody = fnCaller.getLastChild(); |
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
Predicate<Node> match = new Predicate<Node>(){ |
| 637 |
@Override |
| 638 |
public boolean apply(Node n) { |
| 639 |
if (n.isName()) { |
| 640 |
return n.getString().equals("eval"); |
| 641 |
} |
| 642 |
if (!assumeMinimumCapture && n.isFunction()) { |
| 643 |
return n != fnNode; |
| 644 |
} |
| 645 |
return false; |
| 646 |
} |
| 647 |
}; |
| 648 |
forbidTemps = NodeUtil.has(fnCallerBody, |
| 649 |
match, NodeUtil.MATCH_NOT_FUNCTION); |
| 650 |
} |
| 651 |
|
| 652 |
if (fnContainsVars && forbidTemps) { |
| 653 |
return false; |
| 654 |
} |
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
if (forbidTemps) { |
| 659 |
Map<String, Node> args = |
| 660 |
FunctionArgumentInjector.getFunctionCallParameterMap( |
| 661 |
fnNode, callNode, this.safeNameIdSupplier); |
| 662 |
boolean hasArgs = !args.isEmpty(); |
| 663 |
if (hasArgs) { |
| 664 |
|
| 665 |
Set<String> allNamesToAlias = Sets.newHashSet(namesToAlias); |
| 666 |
FunctionArgumentInjector.maybeAddTempsForCallArguments( |
| 667 |
fnNode, args, allNamesToAlias, compiler.getCodingConvention()); |
| 668 |
if (!allNamesToAlias.isEmpty()) { |
| 669 |
return false; |
| 670 |
} |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
return true; |
| 675 |
} |
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
private CanInlineResult canInlineReferenceDirectly( |
| 690 |
Node callNode, Node fnNode) { |
| 691 |
if (!isDirectCallNodeReplacementPossible(fnNode)) { |
| 692 |
return CanInlineResult.NO; |
| 693 |
} |
| 694 |
|
| 695 |
Node block = fnNode.getLastChild(); |
| 696 |
|
| 697 |
|
| 698 |
Node cArg = callNode.getFirstChild().getNext(); |
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
if (!callNode.getFirstChild().isName()) { |
| 704 |
if (NodeUtil.isFunctionObjectCall(callNode)) { |
| 705 |
|
| 706 |
if (cArg == null || !cArg.isThis()) { |
| 707 |
return CanInlineResult.NO; |
| 708 |
} |
| 709 |
cArg = cArg.getNext(); |
| 710 |
} else { |
| 711 |
|
| 712 |
Preconditions.checkState(!NodeUtil.isFunctionObjectApply(callNode)); |
| 713 |
} |
| 714 |
} |
| 715 |
|
| 716 |
|
| 717 |
Node fnParam = NodeUtil.getFunctionParameters(fnNode).getFirstChild(); |
| 718 |
while (cArg != null || fnParam != null) { |
| 719 |
|
| 720 |
if (fnParam != null) { |
| 721 |
if (cArg != null) { |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
if (NodeUtil.mayEffectMutableState(cArg, compiler) |
| 726 |
&& NodeUtil.getNameReferenceCount( |
| 727 |
block, fnParam.getString()) > 1) { |
| 728 |
return CanInlineResult.NO; |
| 729 |
} |
| 730 |
} |
| 731 |
|
| 732 |
|
| 733 |
fnParam = fnParam.getNext(); |
| 734 |
} |
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
if (cArg != null) { |
| 739 |
if (NodeUtil.mayHaveSideEffects(cArg, compiler)) { |
| 740 |
return CanInlineResult.NO; |
| 741 |
} |
| 742 |
cArg = cArg.getNext(); |
| 743 |
} |
| 744 |
} |
| 745 |
|
| 746 |
return CanInlineResult.YES; |
| 747 |
} |
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
boolean inliningLowersCost( |
| 754 |
JSModule fnModule, Node fnNode, Collection<? extends Reference> refs, |
| 755 |
Set<String> namesToAlias, boolean isRemovable, boolean referencesThis) { |
| 756 |
int referenceCount = refs.size(); |
| 757 |
if (referenceCount == 0) { |
| 758 |
return true; |
| 759 |
} |
| 760 |
|
| 761 |
int referencesUsingBlockInlining = 0; |
| 762 |
|
| 763 |
boolean checkModules = isRemovable && fnModule != null; |
| 764 |
JSModuleGraph moduleGraph = compiler.getModuleGraph(); |
| 765 |
|
| 766 |
for (Reference ref : refs) { |
| 767 |
if (ref.mode == InliningMode.BLOCK) { |
| 768 |
referencesUsingBlockInlining++; |
| 769 |
} |
| 770 |
|
| 771 |
|
| 772 |
if (checkModules && ref.module != null) { |
| 773 |
if (ref.module != fnModule && |
| 774 |
!moduleGraph.dependsOn(ref.module, fnModule)) { |
| 775 |
|
| 776 |
|
| 777 |
isRemovable = false; |
| 778 |
checkModules = false; |
| 779 |
} |
| 780 |
} |
| 781 |
} |
| 782 |
|
| 783 |
int referencesUsingDirectInlining = referenceCount - |
| 784 |
referencesUsingBlockInlining; |
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
if (referenceCount == 1 && isRemovable && |
| 793 |
referencesUsingDirectInlining == 1) { |
| 794 |
return true; |
| 795 |
} |
| 796 |
|
| 797 |
int callCost = estimateCallCost(fnNode, referencesThis); |
| 798 |
int overallCallCost = callCost * referenceCount; |
| 799 |
|
| 800 |
int costDeltaDirect = inlineCostDelta( |
| 801 |
fnNode, namesToAlias, InliningMode.DIRECT); |
| 802 |
int costDeltaBlock = inlineCostDelta( |
| 803 |
fnNode, namesToAlias, InliningMode.BLOCK); |
| 804 |
|
| 805 |
return doesLowerCost(fnNode, overallCallCost, |
| 806 |
referencesUsingDirectInlining, costDeltaDirect, |
| 807 |
referencesUsingBlockInlining, costDeltaBlock, |
| 808 |
isRemovable); |
| 809 |
} |
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
private boolean doesLowerCost( |
| 815 |
Node fnNode, int callCost, |
| 816 |
int directInlines, int costDeltaDirect, |
| 817 |
int blockInlines, int costDeltaBlock, |
| 818 |
boolean removable) { |
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
int fnInstanceCount = directInlines + blockInlines - (removable ? 1 : 0); |
| 826 |
|
| 827 |
if (fnInstanceCount == 0) { |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
if (blockInlines > 0 && costDeltaBlock > 0) { |
| 832 |
return false; |
| 833 |
} |
| 834 |
return true; |
| 835 |
} |
| 836 |
|
| 837 |
int costDelta = (directInlines * costDeltaDirect) + |
| 838 |
(blockInlines * costDeltaBlock); |
| 839 |
int threshold = (callCost - costDelta) / fnInstanceCount; |
| 840 |
|
| 841 |
return InlineCostEstimator.getCost(fnNode, threshold + 1) <= threshold; |
| 842 |
} |
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
private static int estimateCallCost(Node fnNode, boolean referencesThis) { |
| 850 |
Node argsNode = NodeUtil.getFunctionParameters(fnNode); |
| 851 |
int numArgs = argsNode.getChildCount(); |
| 852 |
|
| 853 |
int callCost = NAME_COST_ESTIMATE + PAREN_COST; |
| 854 |
if (numArgs > 0) { |
| 855 |
callCost += (numArgs * NAME_COST_ESTIMATE) + ((numArgs - 1) * COMMA_COST); |
| 856 |
} |
| 857 |
|
| 858 |
if (referencesThis) { |
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
callCost += 5 + 5; |
| 864 |
} |
| 865 |
|
| 866 |
return callCost; |
| 867 |
} |
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
private static int inlineCostDelta( |
| 874 |
Node fnNode, Set<String> namesToAlias, InliningMode mode) { |
| 875 |
|
| 876 |
|
| 877 |
int paramCount = NodeUtil.getFunctionParameters(fnNode).getChildCount(); |
| 878 |
int commaCount = (paramCount > 1) ? paramCount - 1 : 0; |
| 879 |
int costDeltaFunctionOverhead = 15 + commaCount + |
| 880 |
(paramCount * InlineCostEstimator.ESTIMATED_IDENTIFIER_COST); |
| 881 |
|
| 882 |
Node block = fnNode.getLastChild(); |
| 883 |
if (!block.hasChildren()) { |
| 884 |
|
| 885 |
return -costDeltaFunctionOverhead; |
| 886 |
} |
| 887 |
|
| 888 |
if (mode == InliningMode.DIRECT) { |
| 889 |
|
| 890 |
|
| 891 |
return -(costDeltaFunctionOverhead + 7); |
| 892 |
} else { |
| 893 |
int aliasCount = namesToAlias.size(); |
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
final int INLINE_BLOCK_OVERHEAD = 4; |
| 905 |
final int PER_RETURN_OVERHEAD = 2; |
| 906 |
final int PER_RETURN_RESULT_OVERHEAD = 3; |
| 907 |
final int PER_ALIAS_OVERHEAD = 3; |
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
int returnCount = NodeUtil.getNodeTypeReferenceCount( |
| 913 |
block, Token.RETURN, new NodeUtil.MatchShallowStatement()); |
| 914 |
int resultCount = (returnCount > 0) ? returnCount - 1 : 0; |
| 915 |
int baseOverhead = (returnCount > 0) ? INLINE_BLOCK_OVERHEAD : 0; |
| 916 |
|
| 917 |
int overhead = baseOverhead |
| 918 |
+ returnCount * PER_RETURN_OVERHEAD |
| 919 |
+ resultCount * PER_RETURN_RESULT_OVERHEAD |
| 920 |
+ aliasCount * PER_ALIAS_OVERHEAD; |
| 921 |
|
| 922 |
return (overhead - costDeltaFunctionOverhead); |
| 923 |
} |
| 924 |
} |
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
public void setKnownConstants(Set<String> knownConstants) { |
| 931 |
|
| 932 |
|
| 933 |
Preconditions.checkState(this.knownConstants.isEmpty()); |
| 934 |
this.knownConstants = knownConstants; |
| 935 |
} |
| 936 |
} |