js-context-specialization.h 2.76 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2014 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_CONTEXT_SPECIALIZATION_H_
#define V8_COMPILER_JS_CONTEXT_SPECIALIZATION_H_

#include "src/compiler/graph-reducer.h"
9
#include "src/handles/maybe-handles.h"
10 11 12 13 14

namespace v8 {
namespace internal {
namespace compiler {

15 16 17 18
// Forward declarations.
class JSGraph;
class JSOperatorBuilder;

19 20
// Pair of a context and its distance from some point of reference.
struct OuterContext {
21
  OuterContext() = default;
22 23
  OuterContext(Handle<Context> context_, size_t distance_)
      : context(context_), distance(distance_) {}
24

25
  Handle<Context> context;
26
  size_t distance = 0;
27
};
28

29
// Specializes a given JSGraph to a given context, potentially constant folding
30
// some {LoadContext} nodes or strength reducing some {StoreContext} nodes.
31 32 33
// Additionally, constant-folds the function parameter if {closure} is given,
// and constant-folds import.meta loads if the corresponding object already
// exists.
34 35 36
//
// The context can be the incoming function context or any outer context
// thereof, as indicated by {outer}'s {distance}.
37
class V8_EXPORT_PRIVATE JSContextSpecialization final : public AdvancedReducer {
38
 public:
39
  JSContextSpecialization(Editor* editor, JSGraph* jsgraph,
40
                          JSHeapBroker* broker, Maybe<OuterContext> outer,
41 42 43
                          MaybeHandle<JSFunction> closure)
      : AdvancedReducer(editor),
        jsgraph_(jsgraph),
44
        outer_(outer),
45
        closure_(closure),
46
        broker_(broker) {}
47 48
  JSContextSpecialization(const JSContextSpecialization&) = delete;
  JSContextSpecialization& operator=(const JSContextSpecialization&) = delete;
49

50 51 52 53
  const char* reducer_name() const override {
    return "JSContextSpecialization";
  }

54
  Reduction Reduce(Node* node) final;
55

56
 private:
57
  Reduction ReduceParameter(Node* node);
58
  Reduction ReduceJSLoadContext(Node* node);
59
  Reduction ReduceJSStoreContext(Node* node);
60
  Reduction ReduceJSGetImportMeta(Node* node);
61

62 63 64 65
  Reduction SimplifyJSStoreContext(Node* node, Node* new_context,
                                   size_t new_depth);
  Reduction SimplifyJSLoadContext(Node* node, Node* new_context,
                                  size_t new_depth);
66

67 68
  Isolate* isolate() const;
  JSGraph* jsgraph() const { return jsgraph_; }
69
  Maybe<OuterContext> outer() const { return outer_; }
70
  MaybeHandle<JSFunction> closure() const { return closure_; }
71
  JSHeapBroker* broker() const { return broker_; }
72 73

  JSGraph* const jsgraph_;
74
  Maybe<OuterContext> outer_;
75
  MaybeHandle<JSFunction> closure_;
76
  JSHeapBroker* const broker_;
77
};
78 79 80 81

}  // namespace compiler
}  // namespace internal
}  // namespace v8
82 83

#endif  // V8_COMPILER_JS_CONTEXT_SPECIALIZATION_H_