js-context-specialization.h 2.67 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 49 50 51
  const char* reducer_name() const override {
    return "JSContextSpecialization";
  }

52
  Reduction Reduce(Node* node) final;
53

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

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

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

  JSGraph* const jsgraph_;
72
  Maybe<OuterContext> outer_;
73
  MaybeHandle<JSFunction> closure_;
74
  JSHeapBroker* const broker_;
75

76
  DISALLOW_COPY_AND_ASSIGN(JSContextSpecialization);
77
};
78 79 80 81

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

#endif  // V8_COMPILER_JS_CONTEXT_SPECIALIZATION_H_