1 // Copyright 2015 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_COMPILER_JS_INLINING_HEURISTIC_H_
6 #define V8_COMPILER_JS_INLINING_HEURISTIC_H_
7 
8 #include "src/compiler/js-inlining.h"
9 
10 namespace v8 {
11 namespace internal {
12 namespace compiler {
13 
14 class JSInliningHeuristic final : public AdvancedReducer {
15  public:
16   enum Mode { kGeneralInlining, kRestrictedInlining, kStressInlining };
JSInliningHeuristic(Editor * editor,Mode mode,Zone * local_zone,CompilationInfo * info,JSGraph * jsgraph,SourcePositionTable * source_positions)17   JSInliningHeuristic(Editor* editor, Mode mode, Zone* local_zone,
18                       CompilationInfo* info, JSGraph* jsgraph,
19                       SourcePositionTable* source_positions)
20       : AdvancedReducer(editor),
21         mode_(mode),
22         inliner_(editor, local_zone, info, jsgraph, source_positions),
23         candidates_(local_zone),
24         seen_(local_zone),
25         jsgraph_(jsgraph) {}
26 
27   Reduction Reduce(Node* node) final;
28 
29   // Processes the list of candidates gathered while the reducer was running,
30   // and inlines call sites that the heuristic determines to be important.
31   void Finalize() final;
32 
33  private:
34   // This limit currently matches what Crankshaft does. We may want to
35   // re-evaluate and come up with a proper limit for TurboFan.
36   static const int kMaxCallPolymorphism = 4;
37 
38   struct Candidate {
39     Handle<JSFunction> functions[kMaxCallPolymorphism];
40     int num_functions;
41     Node* node = nullptr;    // The call site at which to inline.
42     float frequency = 0.0f;  // Relative frequency of this call site.
43   };
44 
45   // Comparator for candidates.
46   struct CandidateCompare {
47     bool operator()(const Candidate& left, const Candidate& right) const;
48   };
49 
50   // Candidates are kept in a sorted set of unique candidates.
51   typedef ZoneSet<Candidate, CandidateCompare> Candidates;
52 
53   // Dumps candidates to console.
54   void PrintCandidates();
55   Reduction InlineCandidate(Candidate const& candidate);
56 
57   CommonOperatorBuilder* common() const;
58   Graph* graph() const;
jsgraph()59   JSGraph* jsgraph() const { return jsgraph_; }
60   SimplifiedOperatorBuilder* simplified() const;
61 
62   Mode const mode_;
63   JSInliner inliner_;
64   Candidates candidates_;
65   ZoneSet<NodeId> seen_;
66   JSGraph* const jsgraph_;
67   int cumulative_count_ = 0;
68 };
69 
70 }  // namespace compiler
71 }  // namespace internal
72 }  // namespace v8
73 
74 #endif  // V8_COMPILER_JS_INLINING_HEURISTIC_H_
75