1 // 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/base/compiler-specific.h"
9 #include "src/compiler/common-node-cache.h"
10 #include "src/compiler/common-operator.h"
11 #include "src/compiler/graph.h"
12 #include "src/compiler/js-operator.h"
13 #include "src/compiler/machine-operator.h"
14 #include "src/compiler/node-properties.h"
15 #include "src/globals.h"
16 #include "src/isolate.h"
17
18 namespace v8 {
19 namespace internal {
20 namespace compiler {
21
22 class SimplifiedOperatorBuilder;
23 class Typer;
24
25 // Implements a facade on a Graph, enhancing the graph with JS-specific
26 // notions, including various builders for operators, canonicalized global
27 // constants, and various helper methods.
NON_EXPORTED_BASE(ZoneObject)28 class V8_EXPORT_PRIVATE JSGraph : public NON_EXPORTED_BASE(ZoneObject) {
29 public:
30 JSGraph(Isolate* isolate, Graph* graph, CommonOperatorBuilder* common,
31 JSOperatorBuilder* javascript, SimplifiedOperatorBuilder* simplified,
32 MachineOperatorBuilder* machine)
33 : isolate_(isolate),
34 graph_(graph),
35 common_(common),
36 javascript_(javascript),
37 simplified_(simplified),
38 machine_(machine),
39 cache_(zone()) {
40 for (int i = 0; i < kNumCachedNodes; i++) cached_nodes_[i] = nullptr;
41 }
42
43 // Canonicalized global constants.
44 Node* AllocateInNewSpaceStubConstant();
45 Node* AllocateInOldSpaceStubConstant();
46 Node* ToNumberBuiltinConstant();
47 Node* CEntryStubConstant(int result_size,
48 SaveFPRegsMode save_doubles = kDontSaveFPRegs,
49 ArgvMode argv_mode = kArgvOnStack,
50 bool builtin_exit_frame = false);
51 Node* EmptyFixedArrayConstant();
52 Node* EmptyStringConstant();
53 Node* FixedArrayMapConstant();
54 Node* FixedDoubleArrayMapConstant();
55 Node* HeapNumberMapConstant();
56 Node* OptimizedOutConstant();
57 Node* StaleRegisterConstant();
58 Node* UndefinedConstant();
59 Node* TheHoleConstant();
60 Node* TrueConstant();
61 Node* FalseConstant();
62 Node* NullConstant();
63 Node* ZeroConstant();
64 Node* OneConstant();
65 Node* NaNConstant();
66
67 // Creates a HeapConstant node, possibly canonicalized, and may access the
68 // heap to inspect the object.
69 Node* HeapConstant(Handle<HeapObject> value);
70
71 // Creates a Constant node of the appropriate type for the given object.
72 // Accesses the heap to inspect the object and determine whether one of the
73 // canonicalized globals or a number constant should be returned.
74 Node* Constant(Handle<Object> value);
75
76 // Creates a NumberConstant node, usually canonicalized.
77 Node* Constant(double value);
78
79 // Creates a NumberConstant node, usually canonicalized.
80 Node* Constant(int32_t value);
81
82 // Creates a NumberConstant node, usually canonicalized.
83 Node* Constant(uint32_t value);
84
85 // Creates a Int32Constant node, usually canonicalized.
86 Node* Int32Constant(int32_t value);
87 Node* Uint32Constant(uint32_t value) {
88 return Int32Constant(bit_cast<int32_t>(value));
89 }
90
91 // Creates a HeapConstant node for either true or false.
92 Node* BooleanConstant(bool is_true) {
93 return is_true ? TrueConstant() : FalseConstant();
94 }
95
96 // Creates a Int64Constant node, usually canonicalized.
97 Node* Int64Constant(int64_t value);
98 Node* Uint64Constant(uint64_t value) {
99 return Int64Constant(bit_cast<int64_t>(value));
100 }
101
102 // Creates a Int32Constant/Int64Constant node, depending on the word size of
103 // the target machine.
104 // TODO(turbofan): Code using Int32Constant/Int64Constant to store pointer
105 // constants is probably not serializable.
106 Node* IntPtrConstant(intptr_t value) {
107 return machine()->Is32() ? Int32Constant(static_cast<int32_t>(value))
108 : Int64Constant(static_cast<int64_t>(value));
109 }
110
111 Node* RelocatableInt32Constant(int32_t value, RelocInfo::Mode rmode);
112 Node* RelocatableInt64Constant(int64_t value, RelocInfo::Mode rmode);
113 Node* RelocatableIntPtrConstant(intptr_t value, RelocInfo::Mode rmode);
114
115 // Creates a Float32Constant node, usually canonicalized.
116 Node* Float32Constant(float value);
117
118 // Creates a Float64Constant node, usually canonicalized.
119 Node* Float64Constant(double value);
120
121 // Creates a PointerConstant node (asm.js only).
122 Node* PointerConstant(intptr_t value);
123 template <typename T>
124 Node* PointerConstant(T* value) {
125 return PointerConstant(bit_cast<intptr_t>(value));
126 }
127
128 // Creates an ExternalConstant node, usually canonicalized.
129 Node* ExternalConstant(ExternalReference ref);
130 Node* ExternalConstant(Runtime::FunctionId function_id);
131
132 Node* SmiConstant(int32_t immediate) {
133 DCHECK(Smi::IsValid(immediate));
134 return Constant(immediate);
135 }
136
137 // Creates a dummy Constant node, used to satisfy calling conventions of
138 // stubs and runtime functions that do not require a context.
139 Node* NoContextConstant() { return ZeroConstant(); }
140
141 // Creates an empty StateValues node, used when we don't have any concrete
142 // values for a certain part of the frame state.
143 Node* EmptyStateValues();
144
145 // Create a control node that serves as dependency for dead nodes.
146 Node* Dead();
147
148 CommonOperatorBuilder* common() const { return common_; }
149 JSOperatorBuilder* javascript() const { return javascript_; }
150 SimplifiedOperatorBuilder* simplified() const { return simplified_; }
151 MachineOperatorBuilder* machine() const { return machine_; }
152 Graph* graph() const { return graph_; }
153 Zone* zone() const { return graph()->zone(); }
154 Isolate* isolate() const { return isolate_; }
155 Factory* factory() const { return isolate()->factory(); }
156
157 void GetCachedNodes(NodeVector* nodes);
158
159 private:
160 enum CachedNode {
161 kAllocateInNewSpaceStubConstant,
162 kAllocateInOldSpaceStubConstant,
163 kToNumberBuiltinConstant,
164 kCEntryStub1Constant,
165 kCEntryStub2Constant,
166 kCEntryStub3Constant,
167 kCEntryStub1WithBuiltinExitFrameConstant,
168 kEmptyFixedArrayConstant,
169 kEmptyStringConstant,
170 kFixedArrayMapConstant,
171 kFixedDoubleArrayMapConstant,
172 kHeapNumberMapConstant,
173 kOptimizedOutConstant,
174 kStaleRegisterConstant,
175 kUndefinedConstant,
176 kTheHoleConstant,
177 kTrueConstant,
178 kFalseConstant,
179 kNullConstant,
180 kZeroConstant,
181 kOneConstant,
182 kNaNConstant,
183 kEmptyStateValues,
184 kDead,
185 kNumCachedNodes // Must remain last.
186 };
187
188 Isolate* isolate_;
189 Graph* graph_;
190 CommonOperatorBuilder* common_;
191 JSOperatorBuilder* javascript_;
192 SimplifiedOperatorBuilder* simplified_;
193 MachineOperatorBuilder* machine_;
194 CommonNodeCache cache_;
195 Node* cached_nodes_[kNumCachedNodes];
196
197 Node* NumberConstant(double value);
198
199 DISALLOW_COPY_AND_ASSIGN(JSGraph);
200 };
201
202 } // namespace compiler
203 } // namespace internal
204 } // namespace v8
205
206 #endif
207