js-graph.h 5.86 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// Copyright 2014 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_JS_GRAPH_H_
#define V8_COMPILER_JS_GRAPH_H_

#include "src/compiler/common-node-cache.h"
#include "src/compiler/common-operator.h"
#include "src/compiler/graph.h"
#include "src/compiler/js-operator.h"
sigurds@chromium.org's avatar
sigurds@chromium.org committed
12
#include "src/compiler/machine-operator.h"
13
#include "src/compiler/node-properties.h"
14
#include "src/isolate.h"
15 16 17 18 19

namespace v8 {
namespace internal {
namespace compiler {

20
class SimplifiedOperatorBuilder;
21 22 23
class Typer;

// Implements a facade on a Graph, enhancing the graph with JS-specific
24
// notions, including various builders for operators, canonicalized global
25 26 27
// constants, and various helper methods.
class JSGraph : public ZoneObject {
 public:
28
  JSGraph(Isolate* isolate, Graph* graph, CommonOperatorBuilder* common,
29 30
          JSOperatorBuilder* javascript, SimplifiedOperatorBuilder* simplified,
          MachineOperatorBuilder* machine)
31 32
      : isolate_(isolate),
        graph_(graph),
33
        common_(common),
sigurds@chromium.org's avatar
sigurds@chromium.org committed
34
        javascript_(javascript),
35
        simplified_(simplified),
sigurds@chromium.org's avatar
sigurds@chromium.org committed
36
        machine_(machine),
37 38 39
        cache_(zone()) {
    for (int i = 0; i < kNumCachedNodes; i++) cached_nodes_[i] = nullptr;
  }
40 41

  // Canonicalized global constants.
42 43
  Node* AllocateInNewSpaceStubConstant();
  Node* AllocateInOldSpaceStubConstant();
44
  Node* CEntryStubConstant(int result_size);
45
  Node* EmptyFixedArrayConstant();
46
  Node* HeapNumberMapConstant();
47
  Node* OptimizedOutConstant();
48
  Node* StaleRegisterConstant();
49 50 51 52 53 54 55 56 57 58 59
  Node* UndefinedConstant();
  Node* TheHoleConstant();
  Node* TrueConstant();
  Node* FalseConstant();
  Node* NullConstant();
  Node* ZeroConstant();
  Node* OneConstant();
  Node* NaNConstant();

  // Creates a HeapConstant node, possibly canonicalized, and may access the
  // heap to inspect the object.
60
  Node* HeapConstant(Handle<HeapObject> value);
61 62 63 64 65 66 67 68 69 70 71 72 73 74

  // Creates a Constant node of the appropriate type for the given object.
  // Accesses the heap to inspect the object and determine whether one of the
  // canonicalized globals or a number constant should be returned.
  Node* Constant(Handle<Object> value);

  // Creates a NumberConstant node, usually canonicalized.
  Node* Constant(double value);

  // Creates a NumberConstant node, usually canonicalized.
  Node* Constant(int32_t value);

  // Creates a Int32Constant node, usually canonicalized.
  Node* Int32Constant(int32_t value);
75 76 77
  Node* Uint32Constant(uint32_t value) {
    return Int32Constant(bit_cast<int32_t>(value));
  }
78

79 80 81 82 83
  // Creates a HeapConstant node for either true or false.
  Node* BooleanConstant(bool is_true) {
    return is_true ? TrueConstant() : FalseConstant();
  }

84 85 86 87 88 89 90 91 92 93 94 95 96 97
  // Creates a Int64Constant node, usually canonicalized.
  Node* Int64Constant(int64_t value);
  Node* Uint64Constant(uint64_t value) {
    return Int64Constant(bit_cast<int64_t>(value));
  }

  // Creates a Int32Constant/Int64Constant node, depending on the word size of
  // the target machine.
  // TODO(turbofan): Code using Int32Constant/Int64Constant to store pointer
  // constants is probably not serializable.
  Node* IntPtrConstant(intptr_t value) {
    return machine()->Is32() ? Int32Constant(static_cast<int32_t>(value))
                             : Int64Constant(static_cast<int64_t>(value));
  }
98 99 100 101
  template <typename T>
  Node* PointerConstant(T* value) {
    return IntPtrConstant(bit_cast<intptr_t>(value));
  }
102

103 104 105 106
  Node* RelocatableInt32Constant(int32_t value, RelocInfo::Mode rmode);
  Node* RelocatableInt64Constant(int64_t value, RelocInfo::Mode rmode);
  Node* RelocatableIntPtrConstant(intptr_t value, RelocInfo::Mode rmode);

107 108 109
  // Creates a Float32Constant node, usually canonicalized.
  Node* Float32Constant(float value);

110 111 112 113 114
  // Creates a Float64Constant node, usually canonicalized.
  Node* Float64Constant(double value);

  // Creates an ExternalConstant node, usually canonicalized.
  Node* ExternalConstant(ExternalReference ref);
115
  Node* ExternalConstant(Runtime::FunctionId function_id);
116 117

  Node* SmiConstant(int32_t immediate) {
118
    DCHECK(Smi::IsValid(immediate));
119 120 121
    return Constant(immediate);
  }

122 123 124 125
  // Creates a dummy Constant node, used to satisfy calling conventions of
  // stubs and runtime functions that do not require a context.
  Node* NoContextConstant() { return ZeroConstant(); }

126 127 128 129
  // Creates an empty frame states for cases where we know that a function
  // cannot deopt.
  Node* EmptyFrameState();

130 131
  // Create a control node that serves as dependency for dead nodes.
  Node* Dead();
132

133
  CommonOperatorBuilder* common() const { return common_; }
134 135
  JSOperatorBuilder* javascript() const { return javascript_; }
  SimplifiedOperatorBuilder* simplified() const { return simplified_; }
136 137 138 139 140
  MachineOperatorBuilder* machine() const { return machine_; }
  Graph* graph() const { return graph_; }
  Zone* zone() const { return graph()->zone(); }
  Isolate* isolate() const { return isolate_; }
  Factory* factory() const { return isolate()->factory(); }
141

142 143
  void GetCachedNodes(NodeVector* nodes);

144
 private:
145
  enum CachedNode {
146 147
    kAllocateInNewSpaceStubConstant,
    kAllocateInOldSpaceStubConstant,
148
    kCEntryStubConstant,
149
    kEmptyFixedArrayConstant,
150
    kHeapNumberMapConstant,
151
    kOptimizedOutConstant,
152
    kStaleRegisterConstant,
153 154 155 156 157 158 159 160 161
    kUndefinedConstant,
    kTheHoleConstant,
    kTrueConstant,
    kFalseConstant,
    kNullConstant,
    kZeroConstant,
    kOneConstant,
    kNaNConstant,
    kEmptyFrameState,
162
    kDead,
163 164 165
    kNumCachedNodes  // Must remain last.
  };

166
  Isolate* isolate_;
167 168
  Graph* graph_;
  CommonOperatorBuilder* common_;
sigurds@chromium.org's avatar
sigurds@chromium.org committed
169
  JSOperatorBuilder* javascript_;
170
  SimplifiedOperatorBuilder* simplified_;
sigurds@chromium.org's avatar
sigurds@chromium.org committed
171
  MachineOperatorBuilder* machine_;
172
  CommonNodeCache cache_;
173
  Node* cached_nodes_[kNumCachedNodes];
174 175 176

  Node* NumberConstant(double value);

177
  DISALLOW_COPY_AND_ASSIGN(JSGraph);
178
};
179

180 181 182 183 184
}  // namespace compiler
}  // namespace internal
}  // namespace v8

#endif