js-inlining-heuristic.h 2.66 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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.

#ifndef V8_COMPILER_JS_INLINING_HEURISTIC_H_
#define V8_COMPILER_JS_INLINING_HEURISTIC_H_

#include "src/compiler/js-inlining.h"

namespace v8 {
namespace internal {
namespace compiler {

class JSInliningHeuristic final : public AdvancedReducer {
 public:
16
  enum Mode { kGeneralInlining, kRestrictedInlining, kStressInlining };
17
  JSInliningHeuristic(Editor* editor, Mode mode, Zone* local_zone,
18 19
                      CompilationInfo* info, JSGraph* jsgraph,
                      SourcePositionTable* source_positions)
20 21
      : AdvancedReducer(editor),
        mode_(mode),
22
        inliner_(editor, local_zone, info, jsgraph, source_positions),
23
        candidates_(local_zone),
24
        seen_(local_zone),
25
        jsgraph_(jsgraph) {}
26 27 28

  Reduction Reduce(Node* node) final;

29 30
  // Processes the list of candidates gathered while the reducer was running,
  // and inlines call sites that the heuristic determines to be important.
31
  void Finalize() final;
32

33
 private:
34 35 36 37
  // This limit currently matches what Crankshaft does. We may want to
  // re-evaluate and come up with a proper limit for TurboFan.
  static const int kMaxCallPolymorphism = 4;

38
  struct Candidate {
39
    Handle<JSFunction> functions[kMaxCallPolymorphism];
40 41 42 43 44
    // TODO(2206): For now polymorphic inlining is treated orthogonally to
    // inlining based on SharedFunctionInfo. This should be unified and the
    // above array should be switched to SharedFunctionInfo instead. Currently
    // we use {num_functions == 1 && functions[0].is_null()} as an indicator.
    Handle<SharedFunctionInfo> shared_info;
45
    int num_functions;
46 47
    Node* node = nullptr;    // The call site at which to inline.
    float frequency = 0.0f;  // Relative frequency of this call site.
48 49
  };

50 51 52 53 54 55 56 57 58
  // Comparator for candidates.
  struct CandidateCompare {
    bool operator()(const Candidate& left, const Candidate& right) const;
  };

  // Candidates are kept in a sorted set of unique candidates.
  typedef ZoneSet<Candidate, CandidateCompare> Candidates;

  // Dumps candidates to console.
59
  void PrintCandidates();
60 61 62 63 64 65
  Reduction InlineCandidate(Candidate const& candidate);

  CommonOperatorBuilder* common() const;
  Graph* graph() const;
  JSGraph* jsgraph() const { return jsgraph_; }
  SimplifiedOperatorBuilder* simplified() const;
66

67 68
  Mode const mode_;
  JSInliner inliner_;
69
  Candidates candidates_;
70
  ZoneSet<NodeId> seen_;
71
  JSGraph* const jsgraph_;
72
  int cumulative_count_ = 0;
73 74 75 76 77 78 79
};

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

#endif  // V8_COMPILER_JS_INLINING_HEURISTIC_H_