blob: 06e80301649119ca60b00b057bdc9accc5ff4cf6 [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.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040042 Node* CEntryStubConstant(int result_size);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000043 Node* EmptyFixedArrayConstant();
Ben Murdochda12d292016-06-02 14:46:10 +010044 Node* OptimizedOutConstant();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000045 Node* UndefinedConstant();
46 Node* TheHoleConstant();
47 Node* TrueConstant();
48 Node* FalseConstant();
49 Node* NullConstant();
50 Node* ZeroConstant();
51 Node* OneConstant();
52 Node* NaNConstant();
53
Ben Murdochb8a8cc12014-11-26 15:28:44 +000054 // Creates a HeapConstant node, possibly canonicalized, and may access the
55 // heap to inspect the object.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040056 Node* HeapConstant(Handle<HeapObject> value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000057
58 // Creates a Constant node of the appropriate type for the given object.
59 // Accesses the heap to inspect the object and determine whether one of the
60 // canonicalized globals or a number constant should be returned.
61 Node* Constant(Handle<Object> value);
62
63 // Creates a NumberConstant node, usually canonicalized.
64 Node* Constant(double value);
65
66 // Creates a NumberConstant node, usually canonicalized.
67 Node* Constant(int32_t value);
68
69 // Creates a Int32Constant node, usually canonicalized.
70 Node* Int32Constant(int32_t value);
71 Node* Uint32Constant(uint32_t value) {
72 return Int32Constant(bit_cast<int32_t>(value));
73 }
74
Emily Bernierd0a1eb72015-03-24 16:35:39 -040075 // Creates a HeapConstant node for either true or false.
76 Node* BooleanConstant(bool is_true) {
77 return is_true ? TrueConstant() : FalseConstant();
78 }
79
80 // Creates a Int64Constant node, usually canonicalized.
81 Node* Int64Constant(int64_t value);
82 Node* Uint64Constant(uint64_t value) {
83 return Int64Constant(bit_cast<int64_t>(value));
84 }
85
86 // Creates a Int32Constant/Int64Constant node, depending on the word size of
87 // the target machine.
88 // TODO(turbofan): Code using Int32Constant/Int64Constant to store pointer
89 // constants is probably not serializable.
90 Node* IntPtrConstant(intptr_t value) {
91 return machine()->Is32() ? Int32Constant(static_cast<int32_t>(value))
92 : Int64Constant(static_cast<int64_t>(value));
93 }
94 template <typename T>
95 Node* PointerConstant(T* value) {
96 return IntPtrConstant(bit_cast<intptr_t>(value));
97 }
98
99 // Creates a Float32Constant node, usually canonicalized.
100 Node* Float32Constant(float value);
101
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000102 // Creates a Float64Constant node, usually canonicalized.
103 Node* Float64Constant(double value);
104
105 // Creates an ExternalConstant node, usually canonicalized.
106 Node* ExternalConstant(ExternalReference ref);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000107 Node* ExternalConstant(Runtime::FunctionId function_id);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000108
109 Node* SmiConstant(int32_t immediate) {
110 DCHECK(Smi::IsValid(immediate));
111 return Constant(immediate);
112 }
113
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400114 // Creates a dummy Constant node, used to satisfy calling conventions of
115 // stubs and runtime functions that do not require a context.
116 Node* NoContextConstant() { return ZeroConstant(); }
117
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000118 // Creates an empty frame states for cases where we know that a function
119 // cannot deopt.
120 Node* EmptyFrameState();
121
122 // Create a control node that serves as dependency for dead nodes.
123 Node* Dead();
124
125 CommonOperatorBuilder* common() const { return common_; }
126 JSOperatorBuilder* javascript() const { return javascript_; }
127 SimplifiedOperatorBuilder* simplified() const { return simplified_; }
128 MachineOperatorBuilder* machine() const { return machine_; }
129 Graph* graph() const { return graph_; }
130 Zone* zone() const { return graph()->zone(); }
131 Isolate* isolate() const { return isolate_; }
132 Factory* factory() const { return isolate()->factory(); }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400133
134 void GetCachedNodes(NodeVector* nodes);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000135
136 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000137 enum CachedNode {
138 kCEntryStubConstant,
139 kEmptyFixedArrayConstant,
Ben Murdochda12d292016-06-02 14:46:10 +0100140 kOptimizedOutConstant,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000141 kUndefinedConstant,
142 kTheHoleConstant,
143 kTrueConstant,
144 kFalseConstant,
145 kNullConstant,
146 kZeroConstant,
147 kOneConstant,
148 kNaNConstant,
149 kEmptyFrameState,
150 kDead,
151 kNumCachedNodes // Must remain last.
152 };
153
154 Isolate* isolate_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000155 Graph* graph_;
156 CommonOperatorBuilder* common_;
157 JSOperatorBuilder* javascript_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000158 SimplifiedOperatorBuilder* simplified_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000159 MachineOperatorBuilder* machine_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000160 CommonNodeCache cache_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000161 Node* cached_nodes_[kNumCachedNodes];
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000162
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000163 Node* NumberConstant(double value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000164
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400165 DISALLOW_COPY_AND_ASSIGN(JSGraph);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000166};
167
168} // namespace compiler
169} // namespace internal
170} // namespace v8
171
172#endif