js-inlining-heuristic.h 4.95 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 17
  enum Mode { kJSOnly, kWasmOnly };

18
  JSInliningHeuristic(Editor* editor, Zone* local_zone,
19
                      OptimizedCompilationInfo* info, JSGraph* jsgraph,
20
                      JSHeapBroker* broker,
21
                      SourcePositionTable* source_positions, Mode mode)
22
      : AdvancedReducer(editor),
23
        inliner_(editor, local_zone, info, jsgraph, broker, source_positions),
24 25
        candidates_(local_zone),
        seen_(local_zone),
26
        source_positions_(source_positions),
27
        jsgraph_(jsgraph),
28
        broker_(broker),
29 30 31 32 33 34 35
        mode_(mode),
        max_inlined_bytecode_size_(
            ScaleInliningSize(FLAG_max_inlined_bytecode_size, broker)),
        max_inlined_bytecode_size_cumulative_(ScaleInliningSize(
            FLAG_max_inlined_bytecode_size_cumulative, broker)),
        max_inlined_bytecode_size_absolute_(ScaleInliningSize(
            FLAG_max_inlined_bytecode_size_absolute, broker)) {}
36

37 38
  const char* reducer_name() const override { return "JSInliningHeuristic"; }

39 40
  Reduction Reduce(Node* node) final;

41 42
  // Processes the list of candidates gathered while the reducer was running,
  // and inlines call sites that the heuristic determines to be important.
43
  void Finalize() final;
44

45 46 47 48
  int total_inlined_bytecode_size() const {
    return total_inlined_bytecode_size_;
  }

49
 private:
50
  // This limit currently matches what the old compiler did. We may want to
51 52 53
  // re-evaluate and come up with a proper limit for TurboFan.
  static const int kMaxCallPolymorphism = 4;

54
  struct Candidate {
55
    base::Optional<JSFunctionRef> functions[kMaxCallPolymorphism];
56 57 58
    // In the case of polymorphic inlining, this tells if each of the
    // functions could be inlined.
    bool can_inline_function[kMaxCallPolymorphism];
59 60
    // Strong references to bytecode to ensure it is not flushed from SFI
    // while choosing inlining candidates.
61
    base::Optional<BytecodeArrayRef> bytecode[kMaxCallPolymorphism];
62 63 64 65
    // 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.
66
    base::Optional<SharedFunctionInfoRef> shared_info;
67
    int num_functions;
68 69
    Node* node = nullptr;     // The call site at which to inline.
    CallFrequency frequency;  // Relative frequency of this call site.
70
    int total_size = 0;
71 72
  };

73 74 75 76 77 78
  // Comparator for candidates.
  struct CandidateCompare {
    bool operator()(const Candidate& left, const Candidate& right) const;
  };

  // Candidates are kept in a sorted set of unique candidates.
79
  using Candidates = ZoneSet<Candidate, CandidateCompare>;
80

81 82
  static int ScaleInliningSize(int value, JSHeapBroker* broker);

83
  // Dumps candidates to console.
84
  void PrintCandidates();
85
  Reduction InlineCandidate(Candidate const& candidate, bool small_function);
86 87 88
  void CreateOrReuseDispatch(Node* node, Node* callee,
                             Candidate const& candidate, Node** if_successes,
                             Node** calls, Node** inputs, int input_count);
89 90
  bool TryReuseDispatch(Node* node, Node* callee, Node** if_successes,
                        Node** calls, Node** inputs, int input_count);
91
  enum StateCloneMode { kCloneState, kChangeInPlace };
92 93
  FrameState DuplicateFrameStateAndRename(FrameState frame_state, Node* from,
                                          Node* to, StateCloneMode mode);
94 95
  Node* DuplicateStateValuesAndRename(Node* state_values, Node* from, Node* to,
                                      StateCloneMode mode);
96
  Candidate CollectFunctions(Node* node, int functions_size);
97 98 99 100

  CommonOperatorBuilder* common() const;
  Graph* graph() const;
  JSGraph* jsgraph() const { return jsgraph_; }
101 102
  // TODO(neis): Make heap broker a component of JSGraph?
  JSHeapBroker* broker() const { return broker_; }
103
  CompilationDependencies* dependencies() const;
104
  Isolate* isolate() const { return jsgraph_->isolate(); }
105
  SimplifiedOperatorBuilder* simplified() const;
106
  Mode mode() const { return mode_; }
107

108
  JSInliner inliner_;
109
  Candidates candidates_;
110
  ZoneSet<NodeId> seen_;
111
  SourcePositionTable* source_positions_;
112
  JSGraph* const jsgraph_;
113
  JSHeapBroker* const broker_;
114
  int total_inlined_bytecode_size_ = 0;
115
  const Mode mode_;
116 117 118
  const int max_inlined_bytecode_size_;
  const int max_inlined_bytecode_size_cumulative_;
  const int max_inlined_bytecode_size_absolute_;
119 120 121 122 123 124 125
};

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

#endif  // V8_COMPILER_JS_INLINING_HEURISTIC_H_