11cb0ef41Sopenharmony_ci// Copyright 2014 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#ifndef V8_COMPILER_JS_CONTEXT_SPECIALIZATION_H_
61cb0ef41Sopenharmony_ci#define V8_COMPILER_JS_CONTEXT_SPECIALIZATION_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include "src/compiler/graph-reducer.h"
91cb0ef41Sopenharmony_ci#include "src/handles/maybe-handles.h"
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_cinamespace v8 {
121cb0ef41Sopenharmony_cinamespace internal {
131cb0ef41Sopenharmony_cinamespace compiler {
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci// Forward declarations.
161cb0ef41Sopenharmony_ciclass JSGraph;
171cb0ef41Sopenharmony_ciclass JSOperatorBuilder;
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci// Pair of a context and its distance from some point of reference.
201cb0ef41Sopenharmony_cistruct OuterContext {
211cb0ef41Sopenharmony_ci  OuterContext() = default;
221cb0ef41Sopenharmony_ci  OuterContext(Handle<Context> context_, size_t distance_)
231cb0ef41Sopenharmony_ci      : context(context_), distance(distance_) {}
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  Handle<Context> context;
261cb0ef41Sopenharmony_ci  size_t distance = 0;
271cb0ef41Sopenharmony_ci};
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci// Specializes a given JSGraph to a given context, potentially constant folding
301cb0ef41Sopenharmony_ci// some {LoadContext} nodes or strength reducing some {StoreContext} nodes.
311cb0ef41Sopenharmony_ci// Additionally, constant-folds the function parameter if {closure} is given,
321cb0ef41Sopenharmony_ci// and constant-folds import.meta loads if the corresponding object already
331cb0ef41Sopenharmony_ci// exists.
341cb0ef41Sopenharmony_ci//
351cb0ef41Sopenharmony_ci// The context can be the incoming function context or any outer context
361cb0ef41Sopenharmony_ci// thereof, as indicated by {outer}'s {distance}.
371cb0ef41Sopenharmony_ciclass V8_EXPORT_PRIVATE JSContextSpecialization final : public AdvancedReducer {
381cb0ef41Sopenharmony_ci public:
391cb0ef41Sopenharmony_ci  JSContextSpecialization(Editor* editor, JSGraph* jsgraph,
401cb0ef41Sopenharmony_ci                          JSHeapBroker* broker, Maybe<OuterContext> outer,
411cb0ef41Sopenharmony_ci                          MaybeHandle<JSFunction> closure)
421cb0ef41Sopenharmony_ci      : AdvancedReducer(editor),
431cb0ef41Sopenharmony_ci        jsgraph_(jsgraph),
441cb0ef41Sopenharmony_ci        outer_(outer),
451cb0ef41Sopenharmony_ci        closure_(closure),
461cb0ef41Sopenharmony_ci        broker_(broker) {}
471cb0ef41Sopenharmony_ci  JSContextSpecialization(const JSContextSpecialization&) = delete;
481cb0ef41Sopenharmony_ci  JSContextSpecialization& operator=(const JSContextSpecialization&) = delete;
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  const char* reducer_name() const override {
511cb0ef41Sopenharmony_ci    return "JSContextSpecialization";
521cb0ef41Sopenharmony_ci  }
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  Reduction Reduce(Node* node) final;
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci private:
571cb0ef41Sopenharmony_ci  Reduction ReduceParameter(Node* node);
581cb0ef41Sopenharmony_ci  Reduction ReduceJSLoadContext(Node* node);
591cb0ef41Sopenharmony_ci  Reduction ReduceJSStoreContext(Node* node);
601cb0ef41Sopenharmony_ci  Reduction ReduceJSGetImportMeta(Node* node);
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  Reduction SimplifyJSStoreContext(Node* node, Node* new_context,
631cb0ef41Sopenharmony_ci                                   size_t new_depth);
641cb0ef41Sopenharmony_ci  Reduction SimplifyJSLoadContext(Node* node, Node* new_context,
651cb0ef41Sopenharmony_ci                                  size_t new_depth);
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci  Isolate* isolate() const;
681cb0ef41Sopenharmony_ci  JSGraph* jsgraph() const { return jsgraph_; }
691cb0ef41Sopenharmony_ci  Maybe<OuterContext> outer() const { return outer_; }
701cb0ef41Sopenharmony_ci  MaybeHandle<JSFunction> closure() const { return closure_; }
711cb0ef41Sopenharmony_ci  JSHeapBroker* broker() const { return broker_; }
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci  JSGraph* const jsgraph_;
741cb0ef41Sopenharmony_ci  Maybe<OuterContext> outer_;
751cb0ef41Sopenharmony_ci  MaybeHandle<JSFunction> closure_;
761cb0ef41Sopenharmony_ci  JSHeapBroker* const broker_;
771cb0ef41Sopenharmony_ci};
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci}  // namespace compiler
801cb0ef41Sopenharmony_ci}  // namespace internal
811cb0ef41Sopenharmony_ci}  // namespace v8
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci#endif  // V8_COMPILER_JS_CONTEXT_SPECIALIZATION_H_
84