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_NATIVE_CONTEXT_SPECIALIZATION_H_
6 #define V8_COMPILER_JS_NATIVE_CONTEXT_SPECIALIZATION_H_
7 
8 #include "src/base/flags.h"
9 #include "src/compiler/graph-reducer.h"
10 
11 namespace v8 {
12 namespace internal {
13 
14 // Forward declarations.
15 class CompilationDependencies;
16 class Factory;
17 class FeedbackNexus;
18 class TypeCache;
19 
20 
21 namespace compiler {
22 
23 // Forward declarations.
24 enum class AccessMode;
25 class CommonOperatorBuilder;
26 class JSGraph;
27 class JSOperatorBuilder;
28 class MachineOperatorBuilder;
29 class SimplifiedOperatorBuilder;
30 
31 
32 // Specializes a given JSGraph to a given native context, potentially constant
33 // folding some {LoadGlobal} nodes or strength reducing some {StoreGlobal}
34 // nodes.  And also specializes {LoadNamed} and {StoreNamed} nodes according
35 // to type feedback (if available).
36 class JSNativeContextSpecialization final : public AdvancedReducer {
37  public:
38   // Flags that control the mode of operation.
39   enum Flag {
40     kNoFlags = 0u,
41     kDeoptimizationEnabled = 1u << 0,
42   };
43   typedef base::Flags<Flag> Flags;
44 
45   JSNativeContextSpecialization(Editor* editor, JSGraph* jsgraph, Flags flags,
46                                 MaybeHandle<Context> native_context,
47                                 CompilationDependencies* dependencies,
48                                 Zone* zone);
49 
50   Reduction Reduce(Node* node) final;
51 
52  private:
53   Reduction ReduceJSLoadNamed(Node* node);
54   Reduction ReduceJSStoreNamed(Node* node);
55   Reduction ReduceJSLoadProperty(Node* node);
56   Reduction ReduceJSStoreProperty(Node* node);
57 
58   Reduction ReduceElementAccess(Node* node, Node* index, Node* value,
59                                 MapHandleList const& receiver_maps,
60                                 AccessMode access_mode,
61                                 LanguageMode language_mode,
62                                 KeyedAccessStoreMode store_mode);
63   Reduction ReduceKeyedAccess(Node* node, Node* index, Node* value,
64                               FeedbackNexus const& nexus,
65                               AccessMode access_mode,
66                               LanguageMode language_mode,
67                               KeyedAccessStoreMode store_mode);
68   Reduction ReduceNamedAccess(Node* node, Node* value,
69                               MapHandleList const& receiver_maps,
70                               Handle<Name> name, AccessMode access_mode,
71                               LanguageMode language_mode,
72                               Node* index = nullptr);
73 
74   // Adds stability dependencies on all prototypes of every class in
75   // {receiver_type} up to (and including) the {holder}.
76   void AssumePrototypesStable(Type* receiver_type,
77                               Handle<Context> native_context,
78                               Handle<JSObject> holder);
79 
80   // Assuming that {if_projection} is either IfTrue or IfFalse, adds a hint on
81   // the dominating Branch that {if_projection} is the unlikely (deferred) case.
82   void MarkAsDeferred(Node* if_projection);
83 
84   // Retrieve the native context from the given {node} if known.
85   MaybeHandle<Context> GetNativeContext(Node* node);
86 
87   Graph* graph() const;
jsgraph()88   JSGraph* jsgraph() const { return jsgraph_; }
89   Isolate* isolate() const;
90   Factory* factory() const;
91   CommonOperatorBuilder* common() const;
92   JSOperatorBuilder* javascript() const;
93   SimplifiedOperatorBuilder* simplified() const;
94   MachineOperatorBuilder* machine() const;
flags()95   Flags flags() const { return flags_; }
native_context()96   MaybeHandle<Context> native_context() const { return native_context_; }
dependencies()97   CompilationDependencies* dependencies() const { return dependencies_; }
zone()98   Zone* zone() const { return zone_; }
99 
100   JSGraph* const jsgraph_;
101   Flags const flags_;
102   MaybeHandle<Context> native_context_;
103   CompilationDependencies* const dependencies_;
104   Zone* const zone_;
105   TypeCache const& type_cache_;
106 
107   DISALLOW_COPY_AND_ASSIGN(JSNativeContextSpecialization);
108 };
109 
110 DEFINE_OPERATORS_FOR_FLAGS(JSNativeContextSpecialization::Flags)
111 
112 }  // namespace compiler
113 }  // namespace internal
114 }  // namespace v8
115 
116 #endif  // V8_COMPILER_JS_NATIVE_CONTEXT_SPECIALIZATION_H_
117