Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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_GRAPH_H_ |
| 6 | #define V8_COMPILER_JS_GRAPH_H_ |
| 7 | |
| 8 | #include "src/compiler/common-node-cache.h" |
| 9 | #include "src/compiler/common-operator.h" |
| 10 | #include "src/compiler/graph.h" |
| 11 | #include "src/compiler/js-operator.h" |
| 12 | #include "src/compiler/machine-operator.h" |
| 13 | #include "src/compiler/node-properties.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14 | #include "src/isolate.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15 | |
| 16 | namespace v8 { |
| 17 | namespace internal { |
| 18 | namespace compiler { |
| 19 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 20 | class SimplifiedOperatorBuilder; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 21 | class Typer; |
| 22 | |
| 23 | // Implements a facade on a Graph, enhancing the graph with JS-specific |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 24 | // notions, including various builders for operators, canonicalized global |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 25 | // constants, and various helper methods. |
| 26 | class JSGraph : public ZoneObject { |
| 27 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 28 | JSGraph(Isolate* isolate, Graph* graph, CommonOperatorBuilder* common, |
| 29 | JSOperatorBuilder* javascript, SimplifiedOperatorBuilder* simplified, |
| 30 | MachineOperatorBuilder* machine) |
| 31 | : isolate_(isolate), |
| 32 | graph_(graph), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 33 | common_(common), |
| 34 | javascript_(javascript), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 35 | simplified_(simplified), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 36 | machine_(machine), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 37 | cache_(zone()) { |
| 38 | for (int i = 0; i < kNumCachedNodes; i++) cached_nodes_[i] = nullptr; |
| 39 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 40 | |
| 41 | // Canonicalized global constants. |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 42 | Node* AllocateInNewSpaceStubConstant(); |
| 43 | Node* AllocateInOldSpaceStubConstant(); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 44 | Node* ToNumberBuiltinConstant(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 45 | Node* CEntryStubConstant(int result_size); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 46 | Node* EmptyFixedArrayConstant(); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 47 | Node* EmptyLiteralsArrayConstant(); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 48 | Node* HeapNumberMapConstant(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 49 | Node* OptimizedOutConstant(); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 50 | Node* StaleRegisterConstant(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 51 | Node* UndefinedConstant(); |
| 52 | Node* TheHoleConstant(); |
| 53 | Node* TrueConstant(); |
| 54 | Node* FalseConstant(); |
| 55 | Node* NullConstant(); |
| 56 | Node* ZeroConstant(); |
| 57 | Node* OneConstant(); |
| 58 | Node* NaNConstant(); |
| 59 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 60 | // Creates a HeapConstant node, possibly canonicalized, and may access the |
| 61 | // heap to inspect the object. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 62 | Node* HeapConstant(Handle<HeapObject> value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 63 | |
| 64 | // Creates a Constant node of the appropriate type for the given object. |
| 65 | // Accesses the heap to inspect the object and determine whether one of the |
| 66 | // canonicalized globals or a number constant should be returned. |
| 67 | Node* Constant(Handle<Object> value); |
| 68 | |
| 69 | // Creates a NumberConstant node, usually canonicalized. |
| 70 | Node* Constant(double value); |
| 71 | |
| 72 | // Creates a NumberConstant node, usually canonicalized. |
| 73 | Node* Constant(int32_t value); |
| 74 | |
| 75 | // Creates a Int32Constant node, usually canonicalized. |
| 76 | Node* Int32Constant(int32_t value); |
| 77 | Node* Uint32Constant(uint32_t value) { |
| 78 | return Int32Constant(bit_cast<int32_t>(value)); |
| 79 | } |
| 80 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 81 | // Creates a HeapConstant node for either true or false. |
| 82 | Node* BooleanConstant(bool is_true) { |
| 83 | return is_true ? TrueConstant() : FalseConstant(); |
| 84 | } |
| 85 | |
| 86 | // Creates a Int64Constant node, usually canonicalized. |
| 87 | Node* Int64Constant(int64_t value); |
| 88 | Node* Uint64Constant(uint64_t value) { |
| 89 | return Int64Constant(bit_cast<int64_t>(value)); |
| 90 | } |
| 91 | |
| 92 | // Creates a Int32Constant/Int64Constant node, depending on the word size of |
| 93 | // the target machine. |
| 94 | // TODO(turbofan): Code using Int32Constant/Int64Constant to store pointer |
| 95 | // constants is probably not serializable. |
| 96 | Node* IntPtrConstant(intptr_t value) { |
| 97 | return machine()->Is32() ? Int32Constant(static_cast<int32_t>(value)) |
| 98 | : Int64Constant(static_cast<int64_t>(value)); |
| 99 | } |
| 100 | template <typename T> |
| 101 | Node* PointerConstant(T* value) { |
| 102 | return IntPtrConstant(bit_cast<intptr_t>(value)); |
| 103 | } |
| 104 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 105 | Node* RelocatableInt32Constant(int32_t value, RelocInfo::Mode rmode); |
| 106 | Node* RelocatableInt64Constant(int64_t value, RelocInfo::Mode rmode); |
| 107 | Node* RelocatableIntPtrConstant(intptr_t value, RelocInfo::Mode rmode); |
| 108 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 109 | // Creates a Float32Constant node, usually canonicalized. |
| 110 | Node* Float32Constant(float value); |
| 111 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 112 | // Creates a Float64Constant node, usually canonicalized. |
| 113 | Node* Float64Constant(double value); |
| 114 | |
| 115 | // Creates an ExternalConstant node, usually canonicalized. |
| 116 | Node* ExternalConstant(ExternalReference ref); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 117 | Node* ExternalConstant(Runtime::FunctionId function_id); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 118 | |
| 119 | Node* SmiConstant(int32_t immediate) { |
| 120 | DCHECK(Smi::IsValid(immediate)); |
| 121 | return Constant(immediate); |
| 122 | } |
| 123 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 124 | // Creates a dummy Constant node, used to satisfy calling conventions of |
| 125 | // stubs and runtime functions that do not require a context. |
| 126 | Node* NoContextConstant() { return ZeroConstant(); } |
| 127 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 128 | // Creates an empty StateValues node, used when we don't have any concrete |
| 129 | // values for a certain part of the frame state. |
| 130 | Node* EmptyStateValues(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 131 | |
| 132 | // Create a control node that serves as dependency for dead nodes. |
| 133 | Node* Dead(); |
| 134 | |
| 135 | CommonOperatorBuilder* common() const { return common_; } |
| 136 | JSOperatorBuilder* javascript() const { return javascript_; } |
| 137 | SimplifiedOperatorBuilder* simplified() const { return simplified_; } |
| 138 | MachineOperatorBuilder* machine() const { return machine_; } |
| 139 | Graph* graph() const { return graph_; } |
| 140 | Zone* zone() const { return graph()->zone(); } |
| 141 | Isolate* isolate() const { return isolate_; } |
| 142 | Factory* factory() const { return isolate()->factory(); } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 143 | |
| 144 | void GetCachedNodes(NodeVector* nodes); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 145 | |
| 146 | private: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 147 | enum CachedNode { |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 148 | kAllocateInNewSpaceStubConstant, |
| 149 | kAllocateInOldSpaceStubConstant, |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 150 | kToNumberBuiltinConstant, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 151 | kCEntryStubConstant, |
| 152 | kEmptyFixedArrayConstant, |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 153 | kEmptyLiteralsArrayConstant, |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 154 | kHeapNumberMapConstant, |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 155 | kOptimizedOutConstant, |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 156 | kStaleRegisterConstant, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 157 | kUndefinedConstant, |
| 158 | kTheHoleConstant, |
| 159 | kTrueConstant, |
| 160 | kFalseConstant, |
| 161 | kNullConstant, |
| 162 | kZeroConstant, |
| 163 | kOneConstant, |
| 164 | kNaNConstant, |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 165 | kEmptyStateValues, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 166 | kDead, |
| 167 | kNumCachedNodes // Must remain last. |
| 168 | }; |
| 169 | |
| 170 | Isolate* isolate_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 171 | Graph* graph_; |
| 172 | CommonOperatorBuilder* common_; |
| 173 | JSOperatorBuilder* javascript_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 174 | SimplifiedOperatorBuilder* simplified_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 175 | MachineOperatorBuilder* machine_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 176 | CommonNodeCache cache_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 177 | Node* cached_nodes_[kNumCachedNodes]; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 178 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 179 | Node* NumberConstant(double value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 180 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 181 | DISALLOW_COPY_AND_ASSIGN(JSGraph); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | } // namespace compiler |
| 185 | } // namespace internal |
| 186 | } // namespace v8 |
| 187 | |
| 188 | #endif |