js-native-context-specialization.h 11.3 KB
Newer Older
1 2 3 4
// 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.

5 6
#ifndef V8_COMPILER_JS_NATIVE_CONTEXT_SPECIALIZATION_H_
#define V8_COMPILER_JS_NATIVE_CONTEXT_SPECIALIZATION_H_
7 8 9

#include "src/base/flags.h"
#include "src/compiler/graph-reducer.h"
10
#include "src/deoptimize-reason.h"
11
#include "src/objects/map.h"
12 13 14 15 16

namespace v8 {
namespace internal {

// Forward declarations.
17
class Factory;
18
class FeedbackNexus;
19 20
class JSGlobalObject;
class JSGlobalProxy;
21
class StringConstantBase;
22 23 24 25

namespace compiler {

// Forward declarations.
26
enum class AccessMode;
27
class CommonOperatorBuilder;
28
class CompilationDependencies;
29
class ElementAccessInfo;
30
class JSGraph;
31
class JSHeapBroker;
32
class JSOperatorBuilder;
33
class MachineOperatorBuilder;
34
class PropertyAccessInfo;
35
class SimplifiedOperatorBuilder;
36
class TypeCache;
37

38
// Specializes a given JSGraph to a given native context, potentially constant
39
// folding some {LoadGlobal} nodes or strength reducing some {StoreGlobal}
40 41
// nodes.  And also specializes {LoadNamed} and {StoreNamed} nodes according
// to type feedback (if available).
42 43
class V8_EXPORT_PRIVATE JSNativeContextSpecialization final
    : public AdvancedReducer {
44 45 46 47
 public:
  // Flags that control the mode of operation.
  enum Flag {
    kNoFlags = 0u,
48
    kAccessorInliningEnabled = 1u << 0,
49
    kBailoutOnUninitialized = 1u << 1
50 51 52
  };
  typedef base::Flags<Flag> Flags;

53
  JSNativeContextSpecialization(Editor* editor, JSGraph* jsgraph,
54
                                JSHeapBroker* js_heap_broker, Flags flags,
55
                                Handle<Context> native_context,
56
                                CompilationDependencies* dependencies,
57
                                Zone* zone, Zone* shared_zone);
58

59 60 61 62
  const char* reducer_name() const override {
    return "JSNativeContextSpecialization";
  }

63 64
  Reduction Reduce(Node* node) final;

65 66 67 68 69
  // Utility for folding string constant concatenation.
  // Supports JSAdd nodes and nodes typed as string or number.
  // Public for the sake of unit testing.
  static base::Optional<size_t> GetMaxStringLength(Node* node);

70
 private:
71
  Reduction ReduceJSAdd(Node* node);
72
  Reduction ReduceJSGetSuperConstructor(Node* node);
73
  Reduction ReduceJSInstanceOf(Node* node);
74
  Reduction ReduceJSHasInPrototypeChain(Node* node);
75
  Reduction ReduceJSOrdinaryHasInstance(Node* node);
76 77
  Reduction ReduceJSPromiseResolve(Node* node);
  Reduction ReduceJSResolvePromise(Node* node);
78
  Reduction ReduceJSLoadContext(Node* node);
79 80
  Reduction ReduceJSLoadGlobal(Node* node);
  Reduction ReduceJSStoreGlobal(Node* node);
81
  Reduction ReduceJSLoadNamed(Node* node);
82
  Reduction ReduceJSStoreNamed(Node* node);
83 84
  Reduction ReduceJSLoadProperty(Node* node);
  Reduction ReduceJSStoreProperty(Node* node);
85
  Reduction ReduceJSStoreNamedOwn(Node* node);
86
  Reduction ReduceJSStoreDataPropertyInLiteral(Node* node);
87
  Reduction ReduceJSStoreInArrayLiteral(Node* node);
88
  Reduction ReduceJSToObject(Node* node);
89

90
  Reduction ReduceElementAccess(Node* node, Node* index, Node* value,
91
                                MapHandles const& receiver_maps,
92
                                AccessMode access_mode,
93
                                KeyedAccessLoadMode load_mode,
94
                                KeyedAccessStoreMode store_mode);
95
  Reduction ReduceKeyedAccess(Node* node, Node* index, Node* value,
96 97
                              FeedbackNexus const& nexus,
                              AccessMode access_mode,
98
                              KeyedAccessLoadMode load_mode,
99
                              KeyedAccessStoreMode store_mode);
100 101 102
  Reduction ReduceNamedAccessFromNexus(Node* node, Node* value,
                                       FeedbackNexus const& nexus,
                                       Handle<Name> name,
103
                                       AccessMode access_mode);
104
  Reduction ReduceNamedAccess(Node* node, Node* value,
105
                              MapHandles const& receiver_maps,
106
                              Handle<Name> name, AccessMode access_mode,
107
                              Node* index = nullptr);
108
  Reduction ReduceGlobalAccess(Node* node, Node* receiver, Node* value,
109 110
                               Handle<Name> name, AccessMode access_mode,
                               Node* index = nullptr);
111

112
  Reduction ReduceSoftDeoptimize(Node* node, DeoptimizeReason reason);
113
  Reduction ReduceJSToString(Node* node);
114

115 116
  const StringConstantBase* CreateDelayedStringConstant(Node* node);

117 118 119
  // A triple of nodes that represents a continuation.
  class ValueEffectControl final {
   public:
120 121
    ValueEffectControl()
        : value_(nullptr), effect_(nullptr), control_(nullptr) {}
122 123 124 125 126 127 128 129
    ValueEffectControl(Node* value, Node* effect, Node* control)
        : value_(value), effect_(effect), control_(control) {}

    Node* value() const { return value_; }
    Node* effect() const { return effect_; }
    Node* control() const { return control_; }

   private:
130 131 132
    Node* value_;
    Node* effect_;
    Node* control_;
133 134 135
  };

  // Construct the appropriate subgraph for property access.
136 137 138 139 140 141 142
  ValueEffectControl BuildPropertyAccess(Node* receiver, Node* value,
                                         Node* context, Node* frame_state,
                                         Node* effect, Node* control,
                                         Handle<Name> name,
                                         ZoneVector<Node*>* if_exceptions,
                                         PropertyAccessInfo const& access_info,
                                         AccessMode access_mode);
143 144 145 146
  ValueEffectControl BuildPropertyLoad(Node* receiver, Node* context,
                                       Node* frame_state, Node* effect,
                                       Node* control, Handle<Name> name,
                                       ZoneVector<Node*>* if_exceptions,
147 148 149 150 151 152 153 154 155
                                       PropertyAccessInfo const& access_info);

  ValueEffectControl BuildPropertyStore(Node* receiver, Node* value,
                                        Node* context, Node* frame_state,
                                        Node* effect, Node* control,
                                        Handle<Name> name,
                                        ZoneVector<Node*>* if_exceptions,
                                        PropertyAccessInfo const& access_info,
                                        AccessMode access_mode);
156 157 158 159 160 161 162

  // Helpers for accessor inlining.
  Node* InlinePropertyGetterCall(Node* receiver, Node* context,
                                 Node* frame_state, Node** effect,
                                 Node** control,
                                 ZoneVector<Node*>* if_exceptions,
                                 PropertyAccessInfo const& access_info);
163 164 165 166 167
  void InlinePropertySetterCall(Node* receiver, Node* value, Node* context,
                                Node* frame_state, Node** effect,
                                Node** control,
                                ZoneVector<Node*>* if_exceptions,
                                PropertyAccessInfo const& access_info);
168 169 170
  Node* InlineApiCall(Node* receiver, Node* holder, Node* frame_state,
                      Node* value, Node** effect, Node** control,
                      Handle<SharedFunctionInfo> shared_info,
171
                      Handle<FunctionTemplateInfo> function_template_info);
172 173

  // Construct the appropriate subgraph for element access.
174 175 176 177
  ValueEffectControl BuildElementAccess(
      Node* receiver, Node* index, Node* value, Node* effect, Node* control,
      ElementAccessInfo const& access_info, AccessMode access_mode,
      KeyedAccessLoadMode load_mode, KeyedAccessStoreMode store_mode);
178

179 180 181 182 183
  // Construct appropriate subgraph to load from a String.
  Node* BuildIndexedStringLoad(Node* receiver, Node* index, Node* length,
                               Node** effect, Node** control,
                               KeyedAccessLoadMode load_mode);

184 185 186 187
  // Construct appropriate subgraph to extend properties backing store.
  Node* BuildExtendPropertiesBackingStore(Handle<Map> map, Node* properties,
                                          Node* effect, Node* control);

188 189 190 191 192
  // Construct appropriate subgraph to check that the {value} matches
  // the previously recorded {name} feedback.
  Node* BuildCheckEqualsName(Handle<Name> name, Node* value, Node* effect,
                             Node* control);

193 194 195
  // Checks if we can turn the hole into undefined when loading an element
  // from an object with one of the {receiver_maps}; sets up appropriate
  // code dependencies and might use the array protector cell.
196
  bool CanTreatHoleAsUndefined(MapHandles const& receiver_maps);
197

198 199 200
  // Extract receiver maps from {nexus} and filter based on {receiver} if
  // possible.
  bool ExtractReceiverMaps(Node* receiver, Node* effect,
201
                           FeedbackNexus const& nexus,
202
                           MapHandles* receiver_maps);
203

204 205 206 207 208
  // Try to infer maps for the given {receiver} at the current {effect}.
  // If maps are returned then you can be sure that the {receiver} definitely
  // has one of the returned maps at this point in the program (identified
  // by {effect}).
  bool InferReceiverMaps(Node* receiver, Node* effect,
209
                         MapHandles* receiver_maps);
210 211 212 213
  // Try to infer a root map for the {receiver} independent of the current
  // program location.
  MaybeHandle<Map> InferReceiverRootMap(Node* receiver);

214 215 216 217 218 219 220 221 222 223 224
  // Checks if we know at compile time that the {receiver} either definitely
  // has the {prototype} in it's prototype chain, or the {receiver} definitely
  // doesn't have the {prototype} in it's prototype chain.
  enum InferHasInPrototypeChainResult {
    kIsInPrototypeChain,
    kIsNotInPrototypeChain,
    kMayBeInPrototypeChain
  };
  InferHasInPrototypeChainResult InferHasInPrototypeChain(
      Node* receiver, Node* effect, Handle<HeapObject> prototype);

225 226 227 228 229
  // Script context lookup logic.
  struct ScriptContextTableLookupResult;
  bool LookupInScriptContextTable(Handle<Name> name,
                                  ScriptContextTableLookupResult* result);

230 231
  Graph* graph() const;
  JSGraph* jsgraph() const { return jsgraph_; }
232

233
  JSHeapBroker* js_heap_broker() const { return js_heap_broker_; }
234
  Isolate* isolate() const;
235
  Factory* factory() const;
236
  CommonOperatorBuilder* common() const;
237
  JSOperatorBuilder* javascript() const;
238
  SimplifiedOperatorBuilder* simplified() const;
239
  Flags flags() const { return flags_; }
240 241
  Handle<JSGlobalObject> global_object() const { return global_object_; }
  Handle<JSGlobalProxy> global_proxy() const { return global_proxy_; }
242
  const NativeContextRef& native_context() const { return native_context_; }
243
  CompilationDependencies* dependencies() const { return dependencies_; }
244
  Zone* zone() const { return zone_; }
245
  Zone* shared_zone() const { return shared_zone_; }
246 247

  JSGraph* const jsgraph_;
248
  JSHeapBroker* const js_heap_broker_;
249
  Flags const flags_;
250 251
  Handle<JSGlobalObject> global_object_;
  Handle<JSGlobalProxy> global_proxy_;
252
  NativeContextRef native_context_;
253
  CompilationDependencies* const dependencies_;
254
  Zone* const zone_;
255
  Zone* const shared_zone_;
256
  TypeCache const& type_cache_;
257

258
  DISALLOW_COPY_AND_ASSIGN(JSNativeContextSpecialization);
259 260
};

261
DEFINE_OPERATORS_FOR_FLAGS(JSNativeContextSpecialization::Flags)
262

263 264 265 266
}  // namespace compiler
}  // namespace internal
}  // namespace v8

267
#endif  // V8_COMPILER_JS_NATIVE_CONTEXT_SPECIALIZATION_H_