node-properties.h 9.71 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2013 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_COMPILER_NODE_PROPERTIES_H_
#define V8_COMPILER_NODE_PROPERTIES_H_

8
#include "src/common/globals.h"
9
#include "src/compiler/node.h"
10
#include "src/compiler/operator-properties.h"
11
#include "src/compiler/types.h"
12
#include "src/objects/map.h"
13
#include "src/zone/zone-handle-set.h"
14 15 16 17 18

namespace v8 {
namespace internal {
namespace compiler {

19
class Graph;
20
class Operator;
21
class CommonOperatorBuilder;
22 23

// A facade that simplifies access to the different kinds of inputs to a node.
24
class V8_EXPORT_PRIVATE NodeProperties final {
25
 public:
26 27 28 29 30 31 32 33 34 35 36
  // ---------------------------------------------------------------------------
  // Input layout.
  // Inputs are always arranged in order as follows:
  //     0 [ values, context, frame state, effects, control ] node->InputCount()

  static int FirstValueIndex(Node* node) { return 0; }
  static int FirstContextIndex(Node* node) { return PastValueIndex(node); }
  static int FirstFrameStateIndex(Node* node) { return PastContextIndex(node); }
  static int FirstEffectIndex(Node* node) { return PastFrameStateIndex(node); }
  static int FirstControlIndex(Node* node) { return PastEffectIndex(node); }

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
  static int PastValueIndex(Node* node) {
    return FirstValueIndex(node) + node->op()->ValueInputCount();
  }

  static int PastContextIndex(Node* node) {
    return FirstContextIndex(node) +
           OperatorProperties::GetContextInputCount(node->op());
  }

  static int PastFrameStateIndex(Node* node) {
    return FirstFrameStateIndex(node) +
           OperatorProperties::GetFrameStateInputCount(node->op());
  }

  static int PastEffectIndex(Node* node) {
    return FirstEffectIndex(node) + node->op()->EffectInputCount();
  }

  static int PastControlIndex(Node* node) {
    return FirstControlIndex(node) + node->op()->ControlInputCount();
  }
58 59 60 61

  // ---------------------------------------------------------------------------
  // Input accessors.

62 63 64 65 66
  static Node* GetValueInput(Node* node, int index) {
    CHECK_LE(0, index);
    CHECK_LT(index, node->op()->ValueInputCount());
    return node->InputAt(FirstValueIndex(node) + index);
  }
67

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
  static Node* GetContextInput(Node* node) {
    CHECK(OperatorProperties::HasContextInput(node->op()));
    return node->InputAt(FirstContextIndex(node));
  }

  static Node* GetFrameStateInput(Node* node) {
    CHECK(OperatorProperties::HasFrameStateInput(node->op()));
    return node->InputAt(FirstFrameStateIndex(node));
  }

  static Node* GetEffectInput(Node* node, int index = 0) {
    CHECK_LE(0, index);
    CHECK_LT(index, node->op()->EffectInputCount());
    return node->InputAt(FirstEffectIndex(node) + index);
  }

  static Node* GetControlInput(Node* node, int index = 0) {
    CHECK_LE(0, index);
    CHECK_LT(index, node->op()->ControlInputCount());
    return node->InputAt(FirstControlIndex(node) + index);
  }
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

  // ---------------------------------------------------------------------------
  // Edge kinds.

  static bool IsValueEdge(Edge edge);
  static bool IsContextEdge(Edge edge);
  static bool IsFrameStateEdge(Edge edge);
  static bool IsEffectEdge(Edge edge);
  static bool IsControlEdge(Edge edge);

  // ---------------------------------------------------------------------------
  // Miscellaneous predicates.

  static bool IsCommon(Node* node) {
    return IrOpcode::IsCommonOpcode(node->opcode());
  }
  static bool IsControl(Node* node) {
    return IrOpcode::IsControlOpcode(node->opcode());
  }
  static bool IsConstant(Node* node) {
    return IrOpcode::IsConstantOpcode(node->opcode());
  }
  static bool IsPhi(Node* node) {
    return IrOpcode::IsPhiOpcode(node->opcode());
  }

115
  // Determines whether exceptions thrown by the given node are handled locally
116 117 118
  // within the graph (i.e. an IfException projection is present). Optionally
  // the present IfException projection is returned via {out_exception}.
  static bool IsExceptionalCall(Node* node, Node** out_exception = nullptr);
119

120 121 122 123
  // Returns the node producing the successful control output of {node}. This is
  // the IfSuccess projection of {node} if present and {node} itself otherwise.
  static Node* FindSuccessfulControlProjection(Node* node);

124 125 126
  // ---------------------------------------------------------------------------
  // Miscellaneous mutators.

127
  static void ReplaceValueInput(Node* node, Node* value, int index);
128
  static void ReplaceContextInput(Node* node, Node* context);
129
  static void ReplaceControlInput(Node* node, Node* control, int index = 0);
130
  static void ReplaceEffectInput(Node* node, Node* effect, int index = 0);
131
  static void ReplaceFrameStateInput(Node* node, Node* frame_state);
132
  static void RemoveNonValueInputs(Node* node);
133
  static void RemoveValueInputs(Node* node);
134

135 136 137
  // Replaces all value inputs of {node} with the single input {value}.
  static void ReplaceValueInputs(Node* node, Node* value);

138 139 140 141 142
  // Merge the control node {node} into the end of the graph, introducing a
  // merge node or expanding an existing merge node if necessary.
  static void MergeControlToEnd(Graph* graph, CommonOperatorBuilder* common,
                                Node* node);

143
  // Replace all uses of {node} with the given replacement nodes. All occurring
144
  // use kinds need to be replaced, {nullptr} is only valid if a use kind is
145 146 147
  // guaranteed not to exist.
  static void ReplaceUses(Node* node, Node* value, Node* effect = nullptr,
                          Node* success = nullptr, Node* exception = nullptr);
148

149 150 151 152
  // Safe wrapper to mutate the operator of a node. Checks that the node is
  // currently in a state that satisfies constraints of the new operator.
  static void ChangeOp(Node* node, const Operator* new_op);

153 154
  // ---------------------------------------------------------------------------
  // Miscellaneous utilities.
155

156 157
  // Find the last frame state that is effect-wise before the given node. This
  // assumes a linear effect-chain up to a {CheckPoint} node in the graph.
158 159
  // Returns {unreachable_sentinel} if {node} is determined to be unreachable.
  static Node* FindFrameStateBefore(Node* node, Node* unreachable_sentinel);
160 161

  // Collect the output-value projection for the given output index.
162
  static Node* FindProjection(Node* node, size_t projection_index);
163

164 165 166
  // Collect the value projections from a node.
  static void CollectValueProjections(Node* node, Node** proj, size_t count);

167
  // Collect the branch-related projections from a node, such as IfTrue,
168
  // IfFalse, IfSuccess, IfException, IfValue and IfDefault.
169
  //  - Branch: [ IfTrue, IfFalse ]
170
  //  - Call  : [ IfSuccess, IfException ]
171 172 173
  //  - Switch: [ IfValue, ..., IfDefault ]
  static void CollectControlProjections(Node* node, Node** proj, size_t count);

174 175 176
  // Checks if two nodes are the same, looking past {CheckHeapObject}.
  static bool IsSame(Node* a, Node* b);

177 178 179 180 181 182
  // Check if two nodes have equal operators and reference-equal inputs. Used
  // for value numbering/hash-consing.
  static bool Equals(Node* a, Node* b);
  // A corresponding hash function.
  static size_t HashCode(Node* node);

183
  // Walks up the {effect} chain to find a witness that provides map
184
  // information about the {receiver}. Can look through potentially
185
  // side effecting nodes.
186 187 188
  enum InferReceiverMapsResult {
    kNoReceiverMaps,         // No receiver maps inferred.
    kReliableReceiverMaps,   // Receiver maps can be trusted.
Georg Neis's avatar
Georg Neis committed
189
    kUnreliableReceiverMaps  // Receiver maps might have changed (side-effect).
190
  };
191 192
  // DO NOT USE InferReceiverMapsUnsafe IN NEW CODE. Use MapInference instead.
  static InferReceiverMapsResult InferReceiverMapsUnsafe(
193
      JSHeapBroker* broker, Node* receiver, Node* effect,
194
      ZoneHandleSet<Map>* maps_return);
195

196 197 198 199
  // Return the initial map of the new-target if the allocation can be inlined.
  static base::Optional<MapRef> GetJSCreateMap(JSHeapBroker* broker,
                                               Node* receiver);

200 201 202 203 204
  // Walks up the {effect} chain to check that there's no observable side-effect
  // between the {effect} and it's {dominator}. Aborts the walk if there's join
  // in the effect chain.
  static bool NoObservableSideEffectBetween(Node* effect, Node* dominator);

205 206 207
  // Returns true if the {receiver} can be a primitive value (i.e. is not
  // definitely a JavaScript object); might walk up the {effect} chain to
  // find map checks on {receiver}.
208 209
  static bool CanBePrimitive(JSHeapBroker* broker, Node* receiver,
                             Node* effect);
210 211 212

  // Returns true if the {receiver} can be null or undefined. Might walk
  // up the {effect} chain to find map checks for {receiver}.
213
  static bool CanBeNullOrUndefined(JSHeapBroker* broker, Node* receiver,
214
                                   Node* effect);
215

216 217 218
  // ---------------------------------------------------------------------------
  // Context.

219 220 221 222 223
  // Walk up the context chain from the given {node} until we reduce the {depth}
  // to 0 or hit a node that does not extend the context chain ({depth} will be
  // updated accordingly).
  static Node* GetOuterContext(Node* node, size_t* depth);

224
  // ---------------------------------------------------------------------------
225
  // Type.
226

227 228
  static bool IsTyped(Node* node) { return !node->type().IsInvalid(); }
  static Type GetType(Node* node) {
229
    DCHECK(IsTyped(node));
230
    return node->type();
231
  }
232 233 234
  static Type GetTypeOrAny(Node* node);
  static void SetType(Node* node, Type type) {
    DCHECK(!type.IsInvalid());
235
    node->set_type(type);
236
  }
237
  static void RemoveType(Node* node) { node->set_type(Type::Invalid()); }
238 239 240
  static bool AllValueInputsAreTyped(Node* node);

 private:
danno's avatar
danno committed
241
  static inline bool IsInputRange(Edge edge, int first, int count);
242
};
243 244 245 246

}  // namespace compiler
}  // namespace internal
}  // namespace v8
247 248

#endif  // V8_COMPILER_NODE_PROPERTIES_H_