1 // Copyright 2019 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_MAP_INFERENCE_H_ 6 #define V8_COMPILER_MAP_INFERENCE_H_ 7 8 #include "include/v8config.h" 9 #include "src/compiler/graph-reducer.h" 10 #include "src/objects/instance-type.h" 11 #include "src/objects/map.h" 12 13 namespace v8 { 14 namespace internal { 15 16 17 namespace compiler { 18 19 class CompilationDependencies; 20 struct FeedbackSource; 21 class JSGraph; 22 class JSHeapBroker; 23 class Node; 24 25 // The MapInference class provides access to the "inferred" maps of an 26 // {object}. This information can be either "reliable", meaning that the object 27 // is guaranteed to have one of these maps at runtime, or "unreliable", meaning 28 // that the object is guaranteed to have HAD one of these maps. 29 // 30 // The MapInference class does not expose whether or not the information is 31 // reliable. A client is expected to eventually make the information reliable by 32 // calling one of several methods that will either insert map checks, or record 33 // stability dependencies (or do nothing if the information was already 34 // reliable). 35 class MapInference { 36 public: 37 MapInference(JSHeapBroker* broker, Node* object, Node* effect); 38 39 // The destructor checks that the information has been made reliable (if 40 // necessary) and force-crashes if not. 41 ~MapInference(); 42 43 // Is there any information at all? 44 V8_WARN_UNUSED_RESULT bool HaveMaps() const; 45 46 // These queries don't require a guard. 47 // 48 V8_WARN_UNUSED_RESULT bool AllOfInstanceTypesAreJSReceiver() const; 49 // Here, {type} must not be a String type. 50 V8_WARN_UNUSED_RESULT bool AllOfInstanceTypesAre(InstanceType type) const; 51 V8_WARN_UNUSED_RESULT bool AnyOfInstanceTypesAre(InstanceType type) const; 52 53 // These queries require a guard. (Even instance types are generally not 54 // reliable because of how the representation of a string can change.) 55 V8_WARN_UNUSED_RESULT MapHandles const& GetMaps(); 56 V8_WARN_UNUSED_RESULT bool AllOfInstanceTypes( 57 std::function<bool(InstanceType)> f); 58 V8_WARN_UNUSED_RESULT bool Is(Handle<Map> expected_map); 59 60 // These methods provide a guard. 61 // 62 // Returns true iff maps were already reliable or stability dependencies were 63 // successfully recorded. 64 V8_WARN_UNUSED_RESULT bool RelyOnMapsViaStability( 65 CompilationDependencies* dependencies); 66 // Records stability dependencies if possible, otherwise it inserts map 67 // checks. Does nothing if maps were already reliable. Returns true iff 68 // dependencies were taken. 69 bool RelyOnMapsPreferStability(CompilationDependencies* dependencies, 70 JSGraph* jsgraph, Effect* effect, 71 Control control, 72 const FeedbackSource& feedback); 73 // Inserts map checks even if maps were already reliable. 74 void InsertMapChecks(JSGraph* jsgraph, Effect* effect, Control control, 75 const FeedbackSource& feedback); 76 77 // Internally marks the maps as reliable (thus bypassing the safety check) and 78 // returns the NoChange reduction. USE THIS ONLY WHEN RETURNING, e.g.: 79 // if (foo) return inference.NoChange(); 80 V8_WARN_UNUSED_RESULT Reduction NoChange(); 81 82 private: 83 JSHeapBroker* const broker_; 84 Node* const object_; 85 86 MapHandles maps_; 87 enum { 88 kReliableOrGuarded, 89 kUnreliableDontNeedGuard, 90 kUnreliableNeedGuard 91 } maps_state_; 92 93 bool Safe() const; 94 void SetNeedGuardIfUnreliable(); 95 void SetGuarded(); 96 97 V8_WARN_UNUSED_RESULT bool AllOfInstanceTypesUnsafe( 98 std::function<bool(InstanceType)> f) const; 99 V8_WARN_UNUSED_RESULT bool AnyOfInstanceTypesUnsafe( 100 std::function<bool(InstanceType)> f) const; 101 V8_WARN_UNUSED_RESULT bool RelyOnMapsHelper( 102 CompilationDependencies* dependencies, JSGraph* jsgraph, Effect* effect, 103 Control control, const FeedbackSource& feedback); 104 }; 105 106 } // namespace compiler 107 } // namespace internal 108 } // namespace v8 109 110 #endif // V8_COMPILER_MAP_INFERENCE_H_ 111