node-properties.h 6.37 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/compiler/node.h"
9 10 11 12 13 14
#include "src/types.h"

namespace v8 {
namespace internal {
namespace compiler {

15
class Graph;
16
class Operator;
17
class CommonOperatorBuilder;
18 19

// A facade that simplifies access to the different kinds of inputs to a node.
20
class NodeProperties final {
21
 public:
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
  // ---------------------------------------------------------------------------
  // 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); }
  static int PastValueIndex(Node* node);
  static int PastContextIndex(Node* node);
  static int PastFrameStateIndex(Node* node);
  static int PastEffectIndex(Node* node);
  static int PastControlIndex(Node* node);


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

  static Node* GetValueInput(Node* node, int index);
  static Node* GetContextInput(Node* node);
44
  static Node* GetFrameStateInput(Node* node, int index);
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  static Node* GetEffectInput(Node* node, int index = 0);
  static Node* GetControlInput(Node* node, int index = 0);


  // ---------------------------------------------------------------------------
  // 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());
  }

75 76
  // Determines whether exceptions thrown by the given node are handled locally
  // within the graph (i.e. an IfException projection is present).
svenpanne's avatar
svenpanne committed
77
  static bool IsExceptionalCall(Node* node);
78 79 80 81

  // ---------------------------------------------------------------------------
  // Miscellaneous mutators.

82
  static void ReplaceValueInput(Node* node, Node* value, int index);
83 84 85
  static void ReplaceContextInput(Node* node, Node* context);
  static void ReplaceControlInput(Node* node, Node* control);
  static void ReplaceEffectInput(Node* node, Node* effect, int index = 0);
86
  static void ReplaceFrameStateInput(Node* node, int index, Node* frame_state);
87
  static void RemoveFrameStateInput(Node* node, int index);
88
  static void RemoveNonValueInputs(Node* node);
89
  static void RemoveValueInputs(Node* node);
90

91 92 93
  // Replaces all value inputs of {node} with the single input {value}.
  static void ReplaceValueInputs(Node* node, Node* value);

94 95 96 97 98
  // 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);

99
  // Replace all uses of {node} with the given replacement nodes. All occurring
100
  // use kinds need to be replaced, {nullptr} is only valid if a use kind is
101 102 103
  // guaranteed not to exist.
  static void ReplaceUses(Node* node, Node* value, Node* effect = nullptr,
                          Node* success = nullptr, Node* exception = nullptr);
104

105 106 107 108
  // 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);

109 110
  // ---------------------------------------------------------------------------
  // Miscellaneous utilities.
111 112

  static Node* FindProjection(Node* node, size_t projection_index);
113

114
  // Collect the branch-related projections from a node, such as IfTrue,
115
  // IfFalse, IfSuccess, IfException, IfValue and IfDefault.
116
  //  - Branch: [ IfTrue, IfFalse ]
117
  //  - Call  : [ IfSuccess, IfException ]
118 119 120
  //  - Switch: [ IfValue, ..., IfDefault ]
  static void CollectControlProjections(Node* node, Node** proj, size_t count);

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
  // ---------------------------------------------------------------------------
  // Context.

  // Try to retrieve the specialization context from the given {node},
  // optionally utilizing the knowledge about the (outermost) function
  // {context}.
  static MaybeHandle<Context> GetSpecializationContext(
      Node* node, MaybeHandle<Context> context = MaybeHandle<Context>());

  // Try to retrieve the specialization native context from the given
  // {node}, optionally utilizing the knowledge about the (outermost)
  // {native_context}.
  static MaybeHandle<Context> GetSpecializationNativeContext(
      Node* node, MaybeHandle<Context> native_context = MaybeHandle<Context>());

  // Try to retrieve the specialization global object from the given
  // {node}, optionally utilizing the knowledge about the (outermost)
  // {native_context}.
  static MaybeHandle<JSGlobalObject> GetSpecializationGlobalObject(
      Node* node, MaybeHandle<Context> native_context = MaybeHandle<Context>());

142
  // ---------------------------------------------------------------------------
143
  // Type.
144

145 146
  static bool IsTyped(Node* node) { return node->type() != nullptr; }
  static Type* GetType(Node* node) {
147
    DCHECK(IsTyped(node));
148
    return node->type();
149
  }
150
  static Type* GetTypeOrAny(Node* node);
151 152 153
  static void SetType(Node* node, Type* type) {
    DCHECK_NOT_NULL(type);
    node->set_type(type);
154
  }
155
  static void RemoveType(Node* node) { node->set_type(nullptr); }
156 157 158
  static bool AllValueInputsAreTyped(Node* node);

 private:
danno's avatar
danno committed
159
  static inline bool IsInputRange(Edge edge, int first, int count);
160
};
161 162 163 164

}  // namespace compiler
}  // namespace internal
}  // namespace v8
165 166

#endif  // V8_COMPILER_NODE_PROPERTIES_H_