v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
node-properties.h
Go to the documentation of this file.
1// Copyright 2013 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_NODE_PROPERTIES_H_
6#define V8_COMPILER_NODE_PROPERTIES_H_
7
11#include "src/compiler/node.h"
14
15namespace v8 {
16namespace internal {
17namespace compiler {
18
19class TFGraph;
20class Operator;
21class CommonOperatorBuilder;
22
23// A facade that simplifies access to the different kinds of inputs to a node.
25 public:
26 // ---------------------------------------------------------------------------
27 // Input layout.
28 // Inputs are always arranged in order as follows:
29 // 0 [ values, context, frame state, effects, control ] node->InputCount()
30
31 static int FirstValueIndex(const Node* node) { return 0; }
32 static int FirstContextIndex(Node* node) { return PastValueIndex(node); }
33 static int FirstFrameStateIndex(Node* node) { return PastContextIndex(node); }
34 static int FirstEffectIndex(Node* node) { return PastFrameStateIndex(node); }
35 static int FirstControlIndex(Node* node) { return PastEffectIndex(node); }
36
37 static int PastValueIndex(Node* node) {
38 return FirstValueIndex(node) + node->op()->ValueInputCount();
39 }
40
41 static int PastContextIndex(Node* node) {
42 return FirstContextIndex(node) +
43 OperatorProperties::GetContextInputCount(node->op());
44 }
45
46 static int PastFrameStateIndex(Node* node) {
47 return FirstFrameStateIndex(node) +
48 OperatorProperties::GetFrameStateInputCount(node->op());
49 }
50
51 static int PastEffectIndex(Node* node) {
52 return FirstEffectIndex(node) + node->op()->EffectInputCount();
53 }
54
55 static int PastControlIndex(Node* node) {
56 return FirstControlIndex(node) + node->op()->ControlInputCount();
57 }
58
59 // ---------------------------------------------------------------------------
60 // Input accessors.
61
62 static Node* GetValueInput(Node* node, int index) {
63 CHECK_LE(0, index);
64 CHECK_LT(index, node->op()->ValueInputCount());
65 return node->InputAt(FirstValueIndex(node) + index);
66 }
67
68 static const Node* GetValueInput(const Node* node, int index) {
69 CHECK_LE(0, index);
70 CHECK_LT(index, node->op()->ValueInputCount());
71 return node->InputAt(FirstValueIndex(node) + index);
72 }
73
74 static Node* GetContextInput(Node* node) {
75 CHECK(OperatorProperties::HasContextInput(node->op()));
76 return node->InputAt(FirstContextIndex(node));
77 }
78
79 static Node* GetFrameStateInput(Node* node) {
80 CHECK(OperatorProperties::HasFrameStateInput(node->op()));
81 return node->InputAt(FirstFrameStateIndex(node));
82 }
83
84 static Node* GetEffectInput(Node* node, int index = 0) {
85 CHECK_LE(0, index);
86 CHECK_LT(index, node->op()->EffectInputCount());
87 return node->InputAt(FirstEffectIndex(node) + index);
88 }
89
90 static Node* GetControlInput(Node* node, int index = 0) {
91 CHECK_LE(0, index);
92 CHECK_LT(index, node->op()->ControlInputCount());
93 return node->InputAt(FirstControlIndex(node) + index);
94 }
95
96 // ---------------------------------------------------------------------------
97 // Edge kinds.
98
99 static bool IsValueEdge(Edge edge);
100 static bool IsContextEdge(Edge edge);
101 static bool IsFrameStateEdge(Edge edge);
102 static bool IsEffectEdge(Edge edge);
103 static bool IsControlEdge(Edge edge);
104
105 // ---------------------------------------------------------------------------
106 // Miscellaneous predicates.
107
108 static bool IsCommon(Node* node) {
109 return IrOpcode::IsCommonOpcode(node->opcode());
110 }
111 static bool IsControl(Node* node) {
112 return IrOpcode::IsControlOpcode(node->opcode());
113 }
114 static bool IsConstant(Node* node) {
115 return IrOpcode::IsConstantOpcode(node->opcode());
116 }
117 static bool IsPhi(Node* node) {
118 return IrOpcode::IsPhiOpcode(node->opcode());
119 }
120#if V8_ENABLE_WEBASSEMBLY
121 static bool IsSimd128Operation(Node* node) {
122 return IrOpcode::IsSimd128Opcode(node->opcode());
123 }
124#endif // V8_ENABLE_WEBASSEMBLY
125
126 // Determines whether exceptions thrown by the given node are handled locally
127 // within the graph (i.e. an IfException projection is present). Optionally
128 // the present IfException projection is returned via {out_exception}.
129 static bool IsExceptionalCall(Node* node, Node** out_exception = nullptr);
130
131 // Returns the node producing the successful control output of {node}. This is
132 // the IfSuccess projection of {node} if present and {node} itself otherwise.
133 static Node* FindSuccessfulControlProjection(Node* node);
134
135 // Returns whether the node acts as the identity function on a value
136 // input. The input that is passed through is returned via {out_value}.
137 static bool IsValueIdentity(Node* node, Node** out_value) {
138 switch (node->opcode()) {
139 case IrOpcode::kTypeGuard:
140 *out_value = GetValueInput(node, 0);
141 return true;
142 default:
143 return false;
144 }
145 }
146
147 // ---------------------------------------------------------------------------
148 // Miscellaneous mutators.
149
150 static void ReplaceValueInput(Node* node, Node* value, int index);
151 static void ReplaceContextInput(Node* node, Node* context);
152 static void ReplaceControlInput(Node* node, Node* control, int index = 0);
153 static void ReplaceEffectInput(Node* node, Node* effect, int index = 0);
154 static void ReplaceFrameStateInput(Node* node, Node* frame_state);
155 static void RemoveNonValueInputs(Node* node);
156 static void RemoveValueInputs(Node* node);
157
158 // Replaces all value inputs of {node} with the single input {value}.
159 static void ReplaceValueInputs(Node* node, Node* value);
160
161 // Merge the control node {node} into the end of the graph, introducing a
162 // merge node or expanding an existing merge node if necessary.
163 static void MergeControlToEnd(TFGraph* graph, CommonOperatorBuilder* common,
164 Node* node);
165
166 // Removes the control node {node} from the end of the graph, reducing the
167 // existing merge node's input count.
168 static void RemoveControlFromEnd(TFGraph* graph,
169 CommonOperatorBuilder* common, Node* node);
170
171 // Replace all uses of {node} with the given replacement nodes. All occurring
172 // use kinds need to be replaced, {nullptr} is only valid if a use kind is
173 // guaranteed not to exist.
174 static void ReplaceUses(Node* node, Node* value, Node* effect = nullptr,
175 Node* success = nullptr, Node* exception = nullptr);
176
177 // Safe wrapper to mutate the operator of a node. Checks that the node is
178 // currently in a state that satisfies constraints of the new operator.
179 static void ChangeOp(Node* node, const Operator* new_op);
180 // Like `ChangeOp`, but without checking constraints.
181 static void ChangeOpUnchecked(Node* node, const Operator* new_op);
182
183 // ---------------------------------------------------------------------------
184 // Miscellaneous utilities.
185
186 // Find the last frame state that is effect-wise before the given node. This
187 // assumes a linear effect-chain up to a {CheckPoint} node in the graph.
188 // Returns {unreachable_sentinel} if {node} is determined to be unreachable.
189 static Node* FindFrameStateBefore(Node* node, Node* unreachable_sentinel);
190
191 // Collect the output-value projection for the given output index.
192 static Node* FindProjection(Node* node, size_t projection_index);
193
194 // Collect the value projections from a node.
195 static void CollectValueProjections(Node* node, Node** proj, size_t count);
196
197 // Collect the branch-related projections from a node, such as IfTrue,
198 // IfFalse, IfSuccess, IfException, IfValue and IfDefault.
199 // - Branch: [ IfTrue, IfFalse ]
200 // - Call : [ IfSuccess, IfException ]
201 // - Switch: [ IfValue, ..., IfDefault ]
202 static void CollectControlProjections(Node* node, Node** proj, size_t count);
203
204 // Return the MachineRepresentation of a Projection based on its input.
205 static MachineRepresentation GetProjectionType(Node const* projection);
206
207 // Checks if two nodes are the same, looking past {CheckHeapObject}.
208 static bool IsSame(Node* a, Node* b);
209
210 // Check if two nodes have equal operators and reference-equal inputs. Used
211 // for value numbering/hash-consing.
212 static bool Equals(Node* a, Node* b);
213 // A corresponding hash function.
214 static size_t HashCode(Node* node);
215
216 // Walks up the {effect} chain to find a witness that provides map
217 // information about the {receiver}. Can look through potentially
218 // side effecting nodes.
220 kNoMaps, // No maps inferred.
221 kReliableMaps, // Maps can be trusted.
222 kUnreliableMaps // Maps might have changed (side-effect).
223 };
224 // DO NOT USE InferMapsUnsafe IN NEW CODE. Use MapInference instead.
225 static InferMapsResult InferMapsUnsafe(JSHeapBroker* broker, Node* receiver,
226 Effect effect,
227 ZoneRefSet<Map>* maps_out);
228
229 // Return the initial map of the new-target if the allocation can be inlined.
230 static OptionalMapRef GetJSCreateMap(JSHeapBroker* broker, Node* receiver);
231
232 // Walks up the {effect} chain to check that there's no observable side-effect
233 // between the {effect} and it's {dominator}. Aborts the walk if there's join
234 // in the effect chain.
235 static bool NoObservableSideEffectBetween(Node* effect, Node* dominator);
236
237 // Returns true if the {receiver} can be a primitive value (i.e. is not
238 // definitely a JavaScript object); might walk up the {effect} chain to
239 // find map checks on {receiver}.
240 static bool CanBePrimitive(JSHeapBroker* broker, Node* receiver,
241 Effect effect);
242
243 // Returns true if the {receiver} can be null or undefined. Might walk
244 // up the {effect} chain to find map checks for {receiver}.
245 static bool CanBeNullOrUndefined(JSHeapBroker* broker, Node* receiver,
246 Effect effect);
247
248 // ---------------------------------------------------------------------------
249 // Context.
250
251 // Walk up the context chain from the given {node} until we reduce the {depth}
252 // to 0 or hit a node that does not extend the context chain ({depth} will be
253 // updated accordingly).
254 static Node* GetOuterContext(Node* node, size_t* depth);
255
256 // ---------------------------------------------------------------------------
257 // Type.
258
259 static bool IsTyped(const Node* node) { return !node->type().IsInvalid(); }
260 static Type GetType(const Node* node) {
261 DCHECK(IsTyped(node));
262 return node->type();
263 }
264 static Type GetTypeOrAny(const Node* node);
265 static void SetType(Node* node, Type type) {
266 DCHECK(!type.IsInvalid());
267 node->set_type(type);
268 }
269 static void RemoveType(Node* node) { node->set_type(Type::Invalid()); }
270 static bool AllValueInputsAreTyped(Node* node);
271
272 private:
273 static inline bool IsInputRange(Edge edge, int first, int count);
274};
275
276} // namespace compiler
277} // namespace internal
278} // namespace v8
279
280#endif // V8_COMPILER_NODE_PROPERTIES_H_
static bool IsValueIdentity(Node *node, Node **out_value)
static Type GetType(const Node *node)
static bool IsTyped(const Node *node)
static Node * GetEffectInput(Node *node, int index=0)
static int FirstFrameStateIndex(Node *node)
static Node * GetContextInput(Node *node)
static Node * GetFrameStateInput(Node *node)
static Node * GetValueInput(Node *node, int index)
static void SetType(Node *node, Type type)
static const Node * GetValueInput(const Node *node, int index)
static int FirstValueIndex(const Node *node)
static Node * GetControlInput(Node *node, int index=0)
static int PastFrameStateIndex(Node *node)
JSHeapBroker * broker
TNode< Object > receiver
#define CHECK(condition)
Definition logging.h:124
#define CHECK_LT(lhs, rhs)
#define CHECK_LE(lhs, rhs)
#define DCHECK(condition)
Definition logging.h:482
#define V8_EXPORT_PRIVATE
Definition macros.h:460