1// Copyright 2014 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_CONTEXT_SPECIALIZATION_H_
6#define V8_COMPILER_JS_CONTEXT_SPECIALIZATION_H_
7
8#include "src/compiler/graph-reducer.h"
9#include "src/maybe-handles.h"
10
11namespace v8 {
12namespace internal {
13namespace compiler {
14
15// Forward declarations.
16class JSGraph;
17class JSOperatorBuilder;
18
19// Pair of a context and its distance from some point of reference.
20struct OuterContext {
21 OuterContext() = default;
22 OuterContext(Handle<Context> context_, size_t distance_)
23 : 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// Additionally, constant-folds the function parameter if {closure} is given.
32//
33// The context can be the incoming function context or any outer context
34// thereof, as indicated by {outer}'s {distance}.
35class V8_EXPORT_PRIVATE JSContextSpecialization final : public AdvancedReducer {
36 public:
37 JSContextSpecialization(Editor* editor, JSGraph* jsgraph,
38 JSHeapBroker* broker, Maybe<OuterContext> outer,
39 MaybeHandle<JSFunction> closure)
40 : AdvancedReducer(editor),
41 jsgraph_(jsgraph),
42 outer_(outer),
43 closure_(closure),
44 broker_(broker) {}
45
46 const char* reducer_name() const override {
47 return "JSContextSpecialization";
48 }
49
50 Reduction Reduce(Node* node) final;
51
52 private:
53 Reduction ReduceParameter(Node* node);
54 Reduction ReduceJSLoadContext(Node* node);
55 Reduction ReduceJSStoreContext(Node* node);
56
57 Reduction SimplifyJSStoreContext(Node* node, Node* new_context,
58 size_t new_depth);
59 Reduction SimplifyJSLoadContext(Node* node, Node* new_context,
60 size_t new_depth);
61
62 Isolate* isolate() const;
63 JSGraph* jsgraph() const { return jsgraph_; }
64 Maybe<OuterContext> outer() const { return outer_; }
65 MaybeHandle<JSFunction> closure() const { return closure_; }
66 JSHeapBroker* broker() const { return broker_; }
67
68 JSGraph* const jsgraph_;
69 Maybe<OuterContext> outer_;
70 MaybeHandle<JSFunction> closure_;
71 JSHeapBroker* const broker_;
72
73 DISALLOW_COPY_AND_ASSIGN(JSContextSpecialization);
74};
75
76} // namespace compiler
77} // namespace internal
78} // namespace v8
79
80#endif // V8_COMPILER_JS_CONTEXT_SPECIALIZATION_H_
81