tail-call-optimization.cc 3.05 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/compiler/tail-call-optimization.h"

#include "src/compiler/common-operator.h"
#include "src/compiler/graph.h"
#include "src/compiler/linkage.h"
#include "src/compiler/node-properties.h"

namespace v8 {
namespace internal {
namespace compiler {

Reduction TailCallOptimization::Reduce(Node* node) {
  if (node->opcode() != IrOpcode::kReturn) return NoChange();
  // The value which is returned must be the result of a potential tail call,
  // there must be no try/catch/finally around the Call, and there must be no
  // other effect between the Call and the Return nodes.
  Node* const call = NodeProperties::GetValueInput(node, 0);
  if (call->opcode() == IrOpcode::kCall &&
      OpParameter<CallDescriptor const*>(call)->SupportsTailCalls() &&
      NodeProperties::GetEffectInput(node) == call &&
      !NodeProperties::IsExceptionalCall(call)) {
    Node* const control = NodeProperties::GetControlInput(node);
    if (control->opcode() == IrOpcode::kIfSuccess &&
        call->OwnedBy(node, control) && control->OwnedBy(node)) {
      // Furthermore, control has to flow via an IfSuccess from the Call, so
      // the Return node value and effect depends directly on the Call node,
      // and indirectly control depends on the Call via an IfSuccess.

      // Value1 ... ValueN Effect Control
      //   ^          ^      ^       ^
      //   |          |      |       |
      //   |          +--+ +-+       |
      //   +----------+  | |  +------+
      //               \ | | /
      //             Call[Descriptor]
      //                ^ ^ ^
      //                | | |
      //              +-+ | |
      //              |   | |
      //              | +-+ |
      //              | | IfSuccess
      //              | |  ^
      //              | |  |
      //              Return
      //                ^
      //                |

      // The resulting graph looks like this:

      // Value1 ... ValueN Effect Control
      //   ^          ^      ^       ^
      //   |          |      |       |
      //   |          +--+ +-+       |
      //   +----------+  | |  +------+
      //               \ | | /
      //           TailCall[Descriptor]
      //                 ^
      //                 |

      DCHECK_EQ(call, NodeProperties::GetControlInput(control, 0));
      DCHECK_EQ(3, node->InputCount());
      node->ReplaceInput(0, NodeProperties::GetEffectInput(call));
      node->ReplaceInput(1, NodeProperties::GetControlInput(call));
      node->RemoveInput(2);
      for (int index = 0; index < call->op()->ValueInputCount(); ++index) {
        node->InsertInput(graph()->zone(), index,
                          NodeProperties::GetValueInput(call, index));
      }
73 74
      NodeProperties::ChangeOp(
          node, common()->TailCall(OpParameter<CallDescriptor const*>(call)));
75 76 77 78 79 80 81 82 83
      return Changed(node);
    }
  }
  return NoChange();
}

}  // namespace compiler
}  // namespace internal
}  // namespace v8