blob: e772da8c30f8873552332c64907e52413cef1b40 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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 Murdoch4a90d5f2016-03-22 12:00:34 +000014#include "src/isolate.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015
16namespace v8 {
17namespace internal {
18namespace compiler {
19
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000020class SimplifiedOperatorBuilder;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000021class Typer;
22
23// Implements a facade on a Graph, enhancing the graph with JS-specific
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000024// notions, including various builders for operators, canonicalized global
Ben Murdochb8a8cc12014-11-26 15:28:44 +000025// constants, and various helper methods.
26class JSGraph : public ZoneObject {
27 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000028 JSGraph(Isolate* isolate, Graph* graph, CommonOperatorBuilder* common,
29 JSOperatorBuilder* javascript, SimplifiedOperatorBuilder* simplified,
30 MachineOperatorBuilder* machine)
31 : isolate_(isolate),
32 graph_(graph),
Ben Murdochb8a8cc12014-11-26 15:28:44 +000033 common_(common),
34 javascript_(javascript),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000035 simplified_(simplified),
Ben Murdochb8a8cc12014-11-26 15:28:44 +000036 machine_(machine),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000037 cache_(zone()) {
38 for (int i = 0; i < kNumCachedNodes; i++) cached_nodes_[i] = nullptr;
39 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000040
41 // Canonicalized global constants.
Ben Murdochc5610432016-08-08 18:44:38 +010042 Node* AllocateInNewSpaceStubConstant();
43 Node* AllocateInOldSpaceStubConstant();
Emily Bernierd0a1eb72015-03-24 16:35:39 -040044 Node* CEntryStubConstant(int result_size);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000045 Node* EmptyFixedArrayConstant();
Ben Murdochc5610432016-08-08 18:44:38 +010046 Node* HeapNumberMapConstant();
Ben Murdochda12d292016-06-02 14:46:10 +010047 Node* OptimizedOutConstant();
Ben Murdochc5610432016-08-08 18:44:38 +010048 Node* StaleRegisterConstant();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000049 Node* UndefinedConstant();
50 Node* TheHoleConstant();
51 Node* TrueConstant();
52 Node* FalseConstant();
53 Node* NullConstant();
54 Node* ZeroConstant();
55 Node* OneConstant();
56 Node* NaNConstant();
57
Ben Murdochb8a8cc12014-11-26 15:28:44 +000058 // Creates a HeapConstant node, possibly canonicalized, and may access the
59 // heap to inspect the object.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040060 Node* HeapConstant(Handle<HeapObject> value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000061
62 // Creates a Constant node of the appropriate type for the given object.
63 // Accesses the heap to inspect the object and determine whether one of the
64 // canonicalized globals or a number constant should be returned.
65 Node* Constant(Handle<Object> value);
66
67 // Creates a NumberConstant node, usually canonicalized.
68 Node* Constant(double value);
69
70 // Creates a NumberConstant node, usually canonicalized.
71 Node* Constant(int32_t value);
72
73 // Creates a Int32Constant node, usually canonicalized.
74 Node* Int32Constant(int32_t value);
75 Node* Uint32Constant(uint32_t value) {
76 return Int32Constant(bit_cast<int32_t>(value));
77 }
78
Emily Bernierd0a1eb72015-03-24 16:35:39 -040079 // Creates a HeapConstant node for either true or false.
80 Node* BooleanConstant(bool is_true) {
81 return is_true ? TrueConstant() : FalseConstant();
82 }
83
84 // Creates a Int64Constant node, usually canonicalized.
85 Node* Int64Constant(int64_t value);
86 Node* Uint64Constant(uint64_t value) {
87 return Int64Constant(bit_cast<int64_t>(value));
88 }
89
90 // Creates a Int32Constant/Int64Constant node, depending on the word size of
91 // the target machine.
92 // TODO(turbofan): Code using Int32Constant/Int64Constant to store pointer
93 // constants is probably not serializable.
94 Node* IntPtrConstant(intptr_t value) {
95 return machine()->Is32() ? Int32Constant(static_cast<int32_t>(value))
96 : Int64Constant(static_cast<int64_t>(value));
97 }
98 template <typename T>
99 Node* PointerConstant(T* value) {
100 return IntPtrConstant(bit_cast<intptr_t>(value));
101 }
102
Ben Murdochc5610432016-08-08 18:44:38 +0100103 Node* RelocatableInt32Constant(int32_t value, RelocInfo::Mode rmode);
104 Node* RelocatableInt64Constant(int64_t value, RelocInfo::Mode rmode);
105 Node* RelocatableIntPtrConstant(intptr_t value, RelocInfo::Mode rmode);
106
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400107 // Creates a Float32Constant node, usually canonicalized.
108 Node* Float32Constant(float value);
109
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000110 // Creates a Float64Constant node, usually canonicalized.
111 Node* Float64Constant(double value);
112
113 // Creates an ExternalConstant node, usually canonicalized.
114 Node* ExternalConstant(ExternalReference ref);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000115 Node* ExternalConstant(Runtime::FunctionId function_id);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000116
117 Node* SmiConstant(int32_t immediate) {
118 DCHECK(Smi::IsValid(immediate));
119 return Constant(immediate);
120 }
121
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400122 // Creates a dummy Constant node, used to satisfy calling conventions of
123 // stubs and runtime functions that do not require a context.
124 Node* NoContextConstant() { return ZeroConstant(); }
125
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000126 // Creates an empty frame states for cases where we know that a function
127 // cannot deopt.
128 Node* EmptyFrameState();
129
130 // Create a control node that serves as dependency for dead nodes.
131 Node* Dead();
132
133 CommonOperatorBuilder* common() const { return common_; }
134 JSOperatorBuilder* javascript() const { return javascript_; }
135 SimplifiedOperatorBuilder* simplified() const { return simplified_; }
136 MachineOperatorBuilder* machine() const { return machine_; }
137 Graph* graph() const { return graph_; }
138 Zone* zone() const { return graph()->zone(); }
139 Isolate* isolate() const { return isolate_; }
140 Factory* factory() const { return isolate()->factory(); }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400141
142 void GetCachedNodes(NodeVector* nodes);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000143
144 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000145 enum CachedNode {
Ben Murdochc5610432016-08-08 18:44:38 +0100146 kAllocateInNewSpaceStubConstant,
147 kAllocateInOldSpaceStubConstant,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000148 kCEntryStubConstant,
149 kEmptyFixedArrayConstant,
Ben Murdochc5610432016-08-08 18:44:38 +0100150 kHeapNumberMapConstant,
Ben Murdochda12d292016-06-02 14:46:10 +0100151 kOptimizedOutConstant,
Ben Murdochc5610432016-08-08 18:44:38 +0100152 kStaleRegisterConstant,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000153 kUndefinedConstant,
154 kTheHoleConstant,
155 kTrueConstant,
156 kFalseConstant,
157 kNullConstant,
158 kZeroConstant,
159 kOneConstant,
160 kNaNConstant,
161 kEmptyFrameState,
162 kDead,
163 kNumCachedNodes // Must remain last.
164 };
165
166 Isolate* isolate_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000167 Graph* graph_;
168 CommonOperatorBuilder* common_;
169 JSOperatorBuilder* javascript_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000170 SimplifiedOperatorBuilder* simplified_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000171 MachineOperatorBuilder* machine_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000172 CommonNodeCache cache_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000173 Node* cached_nodes_[kNumCachedNodes];
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000174
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000175 Node* NumberConstant(double value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000176
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400177 DISALLOW_COPY_AND_ASSIGN(JSGraph);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000178};
179
180} // namespace compiler
181} // namespace internal
182} // namespace v8
183
184#endif