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/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"
14 #include "src/isolate.h"
15 
16 namespace v8 {
17 namespace internal {
18 namespace compiler {
19 
20 class SimplifiedOperatorBuilder;
21 class Typer;
22 
23 // Implements a facade on a Graph, enhancing the graph with JS-specific
24 // notions, including various builders for operators, canonicalized global
25 // constants, and various helper methods.
26 class JSGraph : public ZoneObject {
27  public:
JSGraph(Isolate * isolate,Graph * graph,CommonOperatorBuilder * common,JSOperatorBuilder * javascript,SimplifiedOperatorBuilder * simplified,MachineOperatorBuilder * machine)28   JSGraph(Isolate* isolate, Graph* graph, CommonOperatorBuilder* common,
29           JSOperatorBuilder* javascript, SimplifiedOperatorBuilder* simplified,
30           MachineOperatorBuilder* machine)
31       : isolate_(isolate),
32         graph_(graph),
33         common_(common),
34         javascript_(javascript),
35         simplified_(simplified),
36         machine_(machine),
37         cache_(zone()) {
38     for (int i = 0; i < kNumCachedNodes; i++) cached_nodes_[i] = nullptr;
39   }
40 
41   // Canonicalized global constants.
42   Node* CEntryStubConstant(int result_size);
43   Node* EmptyFixedArrayConstant();
44   Node* UndefinedConstant();
45   Node* TheHoleConstant();
46   Node* TrueConstant();
47   Node* FalseConstant();
48   Node* NullConstant();
49   Node* ZeroConstant();
50   Node* OneConstant();
51   Node* NaNConstant();
52 
53   // Creates a HeapConstant node, possibly canonicalized, and may access the
54   // heap to inspect the object.
55   Node* HeapConstant(Handle<HeapObject> value);
56 
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);
Uint32Constant(uint32_t value)70   Node* Uint32Constant(uint32_t value) {
71     return Int32Constant(bit_cast<int32_t>(value));
72   }
73 
74   // Creates a HeapConstant node for either true or false.
BooleanConstant(bool is_true)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);
Uint64Constant(uint64_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.
IntPtrConstant(intptr_t value)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>
PointerConstant(T * value)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 
101   // Creates a Float64Constant node, usually canonicalized.
102   Node* Float64Constant(double value);
103 
104   // Creates an ExternalConstant node, usually canonicalized.
105   Node* ExternalConstant(ExternalReference ref);
106   Node* ExternalConstant(Runtime::FunctionId function_id);
107 
SmiConstant(int32_t immediate)108   Node* SmiConstant(int32_t immediate) {
109     DCHECK(Smi::IsValid(immediate));
110     return Constant(immediate);
111   }
112 
113   // Creates a dummy Constant node, used to satisfy calling conventions of
114   // stubs and runtime functions that do not require a context.
NoContextConstant()115   Node* NoContextConstant() { return ZeroConstant(); }
116 
117   // 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 
common()124   CommonOperatorBuilder* common() const { return common_; }
javascript()125   JSOperatorBuilder* javascript() const { return javascript_; }
simplified()126   SimplifiedOperatorBuilder* simplified() const { return simplified_; }
machine()127   MachineOperatorBuilder* machine() const { return machine_; }
graph()128   Graph* graph() const { return graph_; }
zone()129   Zone* zone() const { return graph()->zone(); }
isolate()130   Isolate* isolate() const { return isolate_; }
factory()131   Factory* factory() const { return isolate()->factory(); }
132 
133   void GetCachedNodes(NodeVector* nodes);
134 
135  private:
136   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_;
153   Graph* graph_;
154   CommonOperatorBuilder* common_;
155   JSOperatorBuilder* javascript_;
156   SimplifiedOperatorBuilder* simplified_;
157   MachineOperatorBuilder* machine_;
158   CommonNodeCache cache_;
159   Node* cached_nodes_[kNumCachedNodes];
160 
161   Node* NumberConstant(double value);
162 
163   DISALLOW_COPY_AND_ASSIGN(JSGraph);
164 };
165 
166 }  // namespace compiler
167 }  // namespace internal
168 }  // namespace v8
169 
170 #endif
171