/** @return Whether inlining will lower cost. */
  private boolean doesLowerCost(
      Node fnNode,
      int callCost,
      int directInlines,
      int costDeltaDirect,
      int blockInlines,
      int costDeltaBlock,
      boolean removable) {

    // Determine the threshold value for this inequality:
    //     inline_cost < call_cost
    // But solve it for the function declaration size so the size of it
    // is only calculated once and terminated early if possible.

    int fnInstanceCount = directInlines + blockInlines - (removable ? 1 : 0);
    // Prevent division by zero.
    if (fnInstanceCount == 0) {
      // Special case single reference function that are being block inlined:
      // If the cost of the inline is greater than the function definition size,
      // don't inline.
      if (blockInlines > 0 && costDeltaBlock > 0) {
        return false;
      }
      return true;
    }

    int costDelta = (directInlines * costDeltaDirect) + (blockInlines * costDeltaBlock);
    int threshold = (callCost - costDelta) / fnInstanceCount;

    return InlineCostEstimator.getCost(fnNode, threshold + 1) <= threshold;
  }
Esempio n. 2
0
 /** Calculates the minimum number of occurrences of throw needed to alias throw. */
 static int estimateMinOccurrencesRequriedToAlias() {
   // Assuming that the alias function name is two bytes in length, two bytes
   // will be saved per occurrence of throw:
   //   <code>throw e;</code>, compared to
   //   <code>TT(e);</code>.
   // However, the alias definition is some length, N, e.g.,
   //   <code>function TT(t){throw t;}</code>
   // Hence there must be more than N/2 occurrences of throw to reduce
   // the code size.
   Node alias = createAliasFunctionNode("TT");
   return InlineCostEstimator.getCost(alias) / 2 + 1;
 }