js-inlining-heuristic.h 4.21 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
  JSInliningHeuristic(Editor* editor, Zone* local_zone,
17
                      OptimizedCompilationInfo* info, JSGraph* jsgraph,
18
                      JSHeapBroker* broker,
19 20
                      SourcePositionTable* source_positions)
      : AdvancedReducer(editor),
21
        inliner_(editor, local_zone, info, jsgraph, broker, source_positions),
22 23
        candidates_(local_zone),
        seen_(local_zone),
24
        source_positions_(source_positions),
25 26
        jsgraph_(jsgraph),
        broker_(broker) {}
27

28 29
  const char* reducer_name() const override { return "JSInliningHeuristic"; }

30 31
  Reduction Reduce(Node* node) final;

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

36 37 38 39
  int total_inlined_bytecode_size() const {
    return total_inlined_bytecode_size_;
  }

40
 private:
41
  // This limit currently matches what the old compiler did. We may want to
42 43 44
  // re-evaluate and come up with a proper limit for TurboFan.
  static const int kMaxCallPolymorphism = 4;

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

64 65 66 67 68 69
  // Comparator for candidates.
  struct CandidateCompare {
    bool operator()(const Candidate& left, const Candidate& right) const;
  };

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

  // Dumps candidates to console.
73
  void PrintCandidates();
74
  Reduction InlineCandidate(Candidate const& candidate, bool small_function);
75 76 77
  void CreateOrReuseDispatch(Node* node, Node* callee,
                             Candidate const& candidate, Node** if_successes,
                             Node** calls, Node** inputs, int input_count);
78 79
  bool TryReuseDispatch(Node* node, Node* callee, Node** if_successes,
                        Node** calls, Node** inputs, int input_count);
80 81 82 83 84
  enum StateCloneMode { kCloneState, kChangeInPlace };
  Node* DuplicateFrameStateAndRename(Node* frame_state, Node* from, Node* to,
                                     StateCloneMode mode);
  Node* DuplicateStateValuesAndRename(Node* state_values, Node* from, Node* to,
                                      StateCloneMode mode);
85
  Candidate CollectFunctions(Node* node, int functions_size);
86 87 88 89

  CommonOperatorBuilder* common() const;
  Graph* graph() const;
  JSGraph* jsgraph() const { return jsgraph_; }
90 91
  // TODO(neis): Make heap broker a component of JSGraph?
  JSHeapBroker* broker() const { return broker_; }
92
  Isolate* isolate() const { return jsgraph_->isolate(); }
93
  SimplifiedOperatorBuilder* simplified() const;
94

95
  JSInliner inliner_;
96
  Candidates candidates_;
97
  ZoneSet<NodeId> seen_;
98
  SourcePositionTable* source_positions_;
99
  JSGraph* const jsgraph_;
100
  JSHeapBroker* const broker_;
101
  int total_inlined_bytecode_size_ = 0;
102 103 104 105 106 107 108
};

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

#endif  // V8_COMPILER_JS_INLINING_HEURISTIC_H_