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/handles/maybe-handles.h" 10 11 namespace v8 { 12 namespace internal { 13 namespace compiler { 14 15 // Forward declarations. 16 class JSGraph; 17 class JSOperatorBuilder; 18 19 // Pair of a context and its distance from some point of reference. 20 struct OuterContext { 21 OuterContext() = default; OuterContextOuterContext22 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 // and constant-folds import.meta loads if the corresponding object already 33 // exists. 34 // 35 // The context can be the incoming function context or any outer context 36 // thereof, as indicated by {outer}'s {distance}. 37 class V8_EXPORT_PRIVATE JSContextSpecialization final : public AdvancedReducer { 38 public: JSContextSpecialization(Editor * editor,JSGraph * jsgraph,JSHeapBroker * broker,Maybe<OuterContext> outer,MaybeHandle<JSFunction> closure)39 JSContextSpecialization(Editor* editor, JSGraph* jsgraph, 40 JSHeapBroker* broker, Maybe<OuterContext> outer, 41 MaybeHandle<JSFunction> closure) 42 : AdvancedReducer(editor), 43 jsgraph_(jsgraph), 44 outer_(outer), 45 closure_(closure), 46 broker_(broker) {} 47 JSContextSpecialization(const JSContextSpecialization&) = delete; 48 JSContextSpecialization& operator=(const JSContextSpecialization&) = delete; 49 reducer_name()50 const char* reducer_name() const override { 51 return "JSContextSpecialization"; 52 } 53 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 Reduction SimplifyJSStoreContext(Node* node, Node* new_context, 63 size_t new_depth); 64 Reduction SimplifyJSLoadContext(Node* node, Node* new_context, 65 size_t new_depth); 66 67 Isolate* isolate() const; jsgraph()68 JSGraph* jsgraph() const { return jsgraph_; } outer()69 Maybe<OuterContext> outer() const { return outer_; } closure()70 MaybeHandle<JSFunction> closure() const { return closure_; } broker()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 } // namespace compiler 80 } // namespace internal 81 } // namespace v8 82 83 #endif // V8_COMPILER_JS_CONTEXT_SPECIALIZATION_H_ 84