blob: 5a25ed0697c8c335998920d82b7430d52ca950a2 [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 Murdochb8a8cc12014-11-26 15:28:44 +000044 Node* UndefinedConstant();
45 Node* TheHoleConstant();
46 Node* TrueConstant();
47 Node* FalseConstant();
48 Node* NullConstant();
49 Node* ZeroConstant();
50 Node* OneConstant();
51 Node* NaNConstant();
52
Ben Murdochb8a8cc12014-11-26 15:28:44 +000053 // Creates a HeapConstant node, possibly canonicalized, and may access the
54 // heap to inspect the object.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040055 Node* HeapConstant(Handle<HeapObject> value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000056
57 // Creates a Constant node of the appropriate type for the given object.
58 // Accesses the heap to inspect the object and determine whether one of the
59 // canonicalized globals or a number constant should be returned.
60 Node* Constant(Handle<Object> value);
61
62 // Creates a NumberConstant node, usually canonicalized.
63 Node* Constant(double value);
64
65 // Creates a NumberConstant node, usually canonicalized.
66 Node* Constant(int32_t value);
67
68 // Creates a Int32Constant node, usually canonicalized.
69 Node* Int32Constant(int32_t value);
70 Node* Uint32Constant(uint32_t value) {
71 return Int32Constant(bit_cast<int32_t>(value));
72 }
73
Emily Bernierd0a1eb72015-03-24 16:35:39 -040074 // Creates a HeapConstant node for either true or false.
75 Node* BooleanConstant(bool is_true) {
76 return is_true ? TrueConstant() : FalseConstant();
77 }
78
79 // Creates a Int64Constant node, usually canonicalized.
80 Node* Int64Constant(int64_t value);
81 Node* Uint64Constant(uint64_t value) {
82 return Int64Constant(bit_cast<int64_t>(value));
83 }
84
85 // Creates a Int32Constant/Int64Constant node, depending on the word size of
86 // the target machine.
87 // TODO(turbofan): Code using Int32Constant/Int64Constant to store pointer
88 // constants is probably not serializable.
89 Node* IntPtrConstant(intptr_t value) {
90 return machine()->Is32() ? Int32Constant(static_cast<int32_t>(value))
91 : Int64Constant(static_cast<int64_t>(value));
92 }
93 template <typename T>
94 Node* PointerConstant(T* value) {
95 return IntPtrConstant(bit_cast<intptr_t>(value));
96 }
97
98 // Creates a Float32Constant node, usually canonicalized.
99 Node* Float32Constant(float value);
100
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000101 // Creates a Float64Constant node, usually canonicalized.
102 Node* Float64Constant(double value);
103
104 // Creates an ExternalConstant node, usually canonicalized.
105 Node* ExternalConstant(ExternalReference ref);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000106 Node* ExternalConstant(Runtime::FunctionId function_id);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000107
108 Node* SmiConstant(int32_t immediate) {
109 DCHECK(Smi::IsValid(immediate));
110 return Constant(immediate);
111 }
112
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400113 // Creates a dummy Constant node, used to satisfy calling conventions of
114 // stubs and runtime functions that do not require a context.
115 Node* NoContextConstant() { return ZeroConstant(); }
116
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000117 // Creates an empty frame states for cases where we know that a function
118 // cannot deopt.
119 Node* EmptyFrameState();
120
121 // Create a control node that serves as dependency for dead nodes.
122 Node* Dead();
123
124 CommonOperatorBuilder* common() const { return common_; }
125 JSOperatorBuilder* javascript() const { return javascript_; }
126 SimplifiedOperatorBuilder* simplified() const { return simplified_; }
127 MachineOperatorBuilder* machine() const { return machine_; }
128 Graph* graph() const { return graph_; }
129 Zone* zone() const { return graph()->zone(); }
130 Isolate* isolate() const { return isolate_; }
131 Factory* factory() const { return isolate()->factory(); }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400132
133 void GetCachedNodes(NodeVector* nodes);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000134
135 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000136 enum CachedNode {
137 kCEntryStubConstant,
138 kEmptyFixedArrayConstant,
139 kUndefinedConstant,
140 kTheHoleConstant,
141 kTrueConstant,
142 kFalseConstant,
143 kNullConstant,
144 kZeroConstant,
145 kOneConstant,
146 kNaNConstant,
147 kEmptyFrameState,
148 kDead,
149 kNumCachedNodes // Must remain last.
150 };
151
152 Isolate* isolate_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000153 Graph* graph_;
154 CommonOperatorBuilder* common_;
155 JSOperatorBuilder* javascript_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000156 SimplifiedOperatorBuilder* simplified_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000157 MachineOperatorBuilder* machine_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000158 CommonNodeCache cache_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000159 Node* cached_nodes_[kNumCachedNodes];
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000160
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000161 Node* NumberConstant(double value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000162
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400163 DISALLOW_COPY_AND_ASSIGN(JSGraph);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000164};
165
166} // namespace compiler
167} // namespace internal
168} // namespace v8
169
170#endif