js-inlining-heuristic.cc 11.5 KB
Newer Older
1 2 3 4 5 6
// 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/js-inlining-heuristic.h"

7
#include "src/compilation-info.h"
8
#include "src/compiler/common-operator.h"
9
#include "src/compiler/node-matchers.h"
10
#include "src/compiler/simplified-operator.h"
11 12 13 14 15 16
#include "src/objects-inl.h"

namespace v8 {
namespace internal {
namespace compiler {

17 18 19 20
#define TRACE(...)                                      \
  do {                                                  \
    if (FLAG_trace_turbo_inlining) PrintF(__VA_ARGS__); \
  } while (false)
21

22
namespace {
23

24
int CollectFunctions(Node* node, Handle<JSFunction>* functions,
25
                     int functions_size, Handle<SharedFunctionInfo>& shared) {
26
  DCHECK_NE(0, functions_size);
27 28 29 30
  HeapObjectMatcher m(node);
  if (m.HasValue() && m.Value()->IsJSFunction()) {
    functions[0] = Handle<JSFunction>::cast(m.Value());
    return 1;
31
  }
32 33 34 35 36 37 38 39 40
  if (m.IsPhi()) {
    int const value_input_count = m.node()->op()->ValueInputCount();
    if (value_input_count > functions_size) return 0;
    for (int n = 0; n < value_input_count; ++n) {
      HeapObjectMatcher m(node->InputAt(n));
      if (!m.HasValue() || !m.Value()->IsJSFunction()) return 0;
      functions[n] = Handle<JSFunction>::cast(m.Value());
    }
    return value_input_count;
41
  }
42 43 44 45 46 47
  if (m.IsJSCreateClosure()) {
    CreateClosureParameters const& p = CreateClosureParametersOf(m.op());
    functions[0] = Handle<JSFunction>::null();
    shared = p.shared_info();
    return 1;
  }
48 49
  return 0;
}
50

51
bool CanInlineFunction(Handle<SharedFunctionInfo> shared) {
52
  // Built-in functions are handled by the JSBuiltinReducer.
53
  if (shared->HasBuiltinFunctionId()) return false;
54

55
  // Only choose user code for inlining.
56
  if (!shared->IsUserJavaScript()) return false;
57

58
  // Quick check on the size of the AST to avoid parsing large candidate.
59
  if (shared->ast_node_count() > FLAG_max_inlined_nodes) {
60 61 62 63
    return false;
  }

  // Avoid inlining across the boundary of asm.js code.
64
  if (shared->asm_function()) return false;
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  return true;
}

}  // namespace

Reduction JSInliningHeuristic::Reduce(Node* node) {
  if (!IrOpcode::IsInlineeOpcode(node->opcode())) return NoChange();

  // Check if we already saw that {node} before, and if so, just skip it.
  if (seen_.find(node->id()) != seen_.end()) return NoChange();
  seen_.insert(node->id());

  // Check if the {node} is an appropriate candidate for inlining.
  Node* callee = node->InputAt(0);
  Candidate candidate;
  candidate.node = node;
81 82
  candidate.num_functions = CollectFunctions(
      callee, candidate.functions, kMaxCallPolymorphism, candidate.shared_info);
83 84 85 86 87 88 89
  if (candidate.num_functions == 0) {
    return NoChange();
  } else if (candidate.num_functions > 1 && !FLAG_polymorphic_inlining) {
    TRACE(
        "Not considering call site #%d:%s, because polymorphic inlining "
        "is disabled\n",
        node->id(), node->op()->mnemonic());
90 91 92
    return NoChange();
  }

93 94 95
  // Functions marked with %SetForceInlineFlag are immediately inlined.
  bool can_inline = false, force_inline = true;
  for (int i = 0; i < candidate.num_functions; ++i) {
96 97 98 99 100
    Handle<SharedFunctionInfo> shared =
        candidate.functions[i].is_null()
            ? candidate.shared_info
            : handle(candidate.functions[i]->shared());
    if (!shared->force_inline()) {
101 102
      force_inline = false;
    }
103
    if (CanInlineFunction(shared)) {
104 105 106 107 108
      can_inline = true;
    }
  }
  if (force_inline) return InlineCandidate(candidate);
  if (!can_inline) return NoChange();
109

110
  // Stop inlining once the maximum allowed level is reached.
111
  int level = 0;
112
  for (Node* frame_state = NodeProperties::GetFrameStateInput(node);
113
       frame_state->opcode() == IrOpcode::kFrameState;
114
       frame_state = NodeProperties::GetFrameStateInput(frame_state)) {
115
    FrameStateInfo const& frame_info = OpParameter<FrameStateInfo>(frame_state);
116
    if (FrameStateFunctionInfo::IsJSFunctionType(frame_info.type())) {
117 118 119 120 121 122 123 124 125
      if (++level > FLAG_max_inlining_levels) {
        TRACE(
            "Not considering call site #%d:%s, because inlining depth "
            "%d exceeds maximum allowed level %d\n",
            node->id(), node->op()->mnemonic(), level,
            FLAG_max_inlining_levels);
        return NoChange();
      }
    }
126 127
  }

128
  // Gather feedback on how often this call site has been hit before.
129 130
  if (node->opcode() == IrOpcode::kJSCall) {
    CallParameters const p = CallParametersOf(node->op());
131
    candidate.frequency = p.frequency();
132
  } else {
133
    ConstructParameters const p = ConstructParametersOf(node->op());
134
    candidate.frequency = p.frequency();
135
  }
136

137 138 139 140 141 142 143 144 145 146 147
  // Handling of special inlining modes right away:
  //  - For restricted inlining: stop all handling at this point.
  //  - For stressing inlining: immediately handle all functions.
  switch (mode_) {
    case kRestrictedInlining:
      return NoChange();
    case kStressInlining:
      return InlineCandidate(candidate);
    case kGeneralInlining:
      break;
  }
148 149

  // In the general case we remember the candidate for later.
150
  candidates_.insert(candidate);
151 152 153
  return NoChange();
}

154
void JSInliningHeuristic::Finalize() {
155
  if (candidates_.empty()) return;  // Nothing to do without candidates.
156 157
  if (FLAG_trace_turbo_inlining) PrintCandidates();

158 159 160
  // We inline at most one candidate in every iteration of the fixpoint.
  // This is to ensure that we don't consume the full inlining budget
  // on things that aren't called very often.
161 162 163 164 165 166
  // TODO(bmeurer): Use std::priority_queue instead of std::set here.
  while (!candidates_.empty()) {
    if (cumulative_count_ > FLAG_max_inlined_nodes_cumulative) return;
    auto i = candidates_.begin();
    Candidate candidate = *i;
    candidates_.erase(i);
167 168 169 170
    // Only include candidates that we've successfully called before.
    // The candidate list is sorted, so we can exit at the first occurance of
    // frequency 0 in the list.
    if (candidate.frequency <= 0.0) return;
171 172
    // Make sure we don't try to inline dead candidate nodes.
    if (!candidate.node->IsDead()) {
173 174
      Reduction const reduction = InlineCandidate(candidate);
      if (reduction.Changed()) return;
175 176
    }
  }
177 178
}

179 180 181 182
Reduction JSInliningHeuristic::InlineCandidate(Candidate const& candidate) {
  int const num_calls = candidate.num_functions;
  Node* const node = candidate.node;
  if (num_calls == 1) {
183 184 185 186 187
    Handle<SharedFunctionInfo> shared =
        candidate.functions[0].is_null()
            ? candidate.shared_info
            : handle(candidate.functions[0]->shared());
    Reduction const reduction = inliner_.ReduceJSCall(node);
188
    if (reduction.Changed()) {
189
      cumulative_count_ += shared->ast_node_count();
190 191 192 193
    }
    return reduction;
  }

194
  // Expand the JSCall/JSConstruct node to a subgraph first if
195 196 197 198 199
  // we have multiple known target functions.
  DCHECK_LT(1, num_calls);
  Node* calls[kMaxCallPolymorphism + 1];
  Node* if_successes[kMaxCallPolymorphism];
  Node* callee = NodeProperties::GetValueInput(node, 0);
200
  Node* fallthrough_control = NodeProperties::GetControlInput(node);
201 202 203 204 205 206 207 208 209 210

  // Setup the inputs for the cloned call nodes.
  int const input_count = node->InputCount();
  Node** inputs = graph()->zone()->NewArray<Node*>(input_count);
  for (int i = 0; i < input_count; ++i) {
    inputs[i] = node->InputAt(i);
  }

  // Create the appropriate control flow to dispatch to the cloned calls.
  for (int i = 0; i < num_calls; ++i) {
211 212
    // TODO(2206): Make comparison be based on underlying SharedFunctionInfo
    // instead of the target JSFunction reference directly.
213 214 215 216
    Node* target = jsgraph()->HeapConstant(candidate.functions[i]);
    if (i != (num_calls - 1)) {
      Node* check =
          graph()->NewNode(simplified()->ReferenceEqual(), callee, target);
217 218 219
      Node* branch =
          graph()->NewNode(common()->Branch(), check, fallthrough_control);
      fallthrough_control = graph()->NewNode(common()->IfFalse(), branch);
220 221
      if_successes[i] = graph()->NewNode(common()->IfTrue(), branch);
    } else {
222
      if_successes[i] = fallthrough_control;
223 224 225 226 227 228
    }

    // The first input to the call is the actual target (which we specialize
    // to the known {target}); the last input is the control dependency.
    inputs[0] = target;
    inputs[input_count - 1] = if_successes[i];
229 230
    calls[i] = if_successes[i] =
        graph()->NewNode(node->op(), input_count, inputs);
231 232 233 234
  }

  // Check if we have an exception projection for the call {node}.
  Node* if_exception = nullptr;
235
  if (NodeProperties::IsExceptionalCall(node, &if_exception)) {
236 237
    Node* if_exceptions[kMaxCallPolymorphism + 1];
    for (int i = 0; i < num_calls; ++i) {
238
      if_successes[i] = graph()->NewNode(common()->IfSuccess(), calls[i]);
239 240 241
      if_exceptions[i] =
          graph()->NewNode(common()->IfException(), calls[i], calls[i]);
    }
242 243

    // Morph the {if_exception} projection into a join.
244
    Node* exception_control =
245
        graph()->NewNode(common()->Merge(num_calls), num_calls, if_exceptions);
246 247 248 249
    if_exceptions[num_calls] = exception_control;
    Node* exception_effect = graph()->NewNode(common()->EffectPhi(num_calls),
                                              num_calls + 1, if_exceptions);
    Node* exception_value = graph()->NewNode(
250 251
        common()->Phi(MachineRepresentation::kTagged, num_calls), num_calls + 1,
        if_exceptions);
252 253
    ReplaceWithValue(if_exception, exception_value, exception_effect,
                     exception_control);
254 255
  }

256
  // Morph the original call site into a join of the dispatched call sites.
257
  Node* control =
258 259 260 261 262 263 264 265 266 267 268 269 270
      graph()->NewNode(common()->Merge(num_calls), num_calls, if_successes);
  calls[num_calls] = control;
  Node* effect =
      graph()->NewNode(common()->EffectPhi(num_calls), num_calls + 1, calls);
  Node* value =
      graph()->NewNode(common()->Phi(MachineRepresentation::kTagged, num_calls),
                       num_calls + 1, calls);
  ReplaceWithValue(node, value, effect, control);

  // Inline the individual, cloned call sites.
  for (int i = 0; i < num_calls; ++i) {
    Handle<JSFunction> function = candidate.functions[i];
    Node* node = calls[i];
271
    Reduction const reduction = inliner_.ReduceJSCall(node);
272 273 274 275 276 277 278
    if (reduction.Changed()) {
      cumulative_count_ += function->shared()->ast_node_count();
    }
  }

  return Replace(value);
}
279

280 281
bool JSInliningHeuristic::CandidateCompare::operator()(
    const Candidate& left, const Candidate& right) const {
282 283 284 285 286 287
  if (left.frequency > right.frequency) {
    return true;
  } else if (left.frequency < right.frequency) {
    return false;
  } else {
    return left.node->id() > right.node->id();
288
  }
289 290 291 292 293
}

void JSInliningHeuristic::PrintCandidates() {
  PrintF("Candidates for inlining (size=%zu):\n", candidates_.size());
  for (const Candidate& candidate : candidates_) {
294 295
    PrintF("  #%d:%s, frequency:%g\n", candidate.node->id(),
           candidate.node->op()->mnemonic(), candidate.frequency);
296
    for (int i = 0; i < candidate.num_functions; ++i) {
297 298 299 300 301 302
      Handle<SharedFunctionInfo> shared =
          candidate.functions[i].is_null()
              ? candidate.shared_info
              : handle(candidate.functions[i]->shared());
      PrintF("  - size:%d, name: %s\n", shared->ast_node_count(),
             shared->DebugName()->ToCString().get());
303
    }
304
  }
305 306
}

307 308 309 310 311 312 313 314 315 316
Graph* JSInliningHeuristic::graph() const { return jsgraph()->graph(); }

CommonOperatorBuilder* JSInliningHeuristic::common() const {
  return jsgraph()->common();
}

SimplifiedOperatorBuilder* JSInliningHeuristic::simplified() const {
  return jsgraph()->simplified();
}

317 318 319
}  // namespace compiler
}  // namespace internal
}  // namespace v8