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 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() : context(), distance() {} 22 OuterContext(Handle<Context> context_, size_t distance_) 23 : context(context_), distance(distance_) {} 24 Handle<Context> context; 25 size_t distance; 26 }; 27 28 // Specializes a given JSGraph to a given context, potentially constant folding 29 // some {LoadContext} nodes or strength reducing some {StoreContext} nodes. 30 // Additionally, constant-folds the function parameter if {closure} is given. 31 // 32 // The context can be the incoming function context or any outer context 33 // thereof, as indicated by {outer}'s {distance}. 34 class JSContextSpecialization final : public AdvancedReducer { 35 public: 36 JSContextSpecialization(Editor* editor, JSGraph* jsgraph, 37 JSHeapBroker* js_heap_broker, 38 Maybe<OuterContext> outer, 39 MaybeHandle<JSFunction> closure) 40 : AdvancedReducer(editor), 41 jsgraph_(jsgraph), 42 outer_(outer), 43 closure_(closure), 44 js_heap_broker_(js_heap_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* js_heap_broker() const { return js_heap_broker_; } 67 68 JSGraph* const jsgraph_; 69 Maybe<OuterContext> outer_; 70 MaybeHandle<JSFunction> closure_; 71 JSHeapBroker* const js_heap_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