OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/compiler/change-lowering.h" | 5 #include "src/compiler/change-lowering.h" |
6 | 6 |
7 #include "src/compiler/common-node-cache.h" | 7 #include "src/compiler/js-graph.h" |
8 #include "src/compiler/graph.h" | |
9 | 8 |
10 namespace v8 { | 9 namespace v8 { |
11 namespace internal { | 10 namespace internal { |
12 namespace compiler { | 11 namespace compiler { |
13 | 12 |
14 ChangeLoweringBase::ChangeLoweringBase(Graph* graph, Linkage* linkage, | 13 ChangeLowering::~ChangeLowering() {} |
15 CommonNodeCache* cache) | |
16 : graph_(graph), | |
17 isolate_(graph->zone()->isolate()), | |
18 linkage_(linkage), | |
19 cache_(cache), | |
20 common_(graph->zone()), | |
21 machine_(graph->zone()) {} | |
22 | 14 |
23 | 15 |
24 ChangeLoweringBase::~ChangeLoweringBase() {} | 16 Reduction ChangeLowering::Reduce(Node* node) { |
25 | 17 Node* control = graph()->start(); |
26 | 18 Node* effect = control; |
27 Node* ChangeLoweringBase::ExternalConstant(ExternalReference reference) { | 19 switch (node->opcode()) { |
28 Node** loc = cache()->FindExternalConstant(reference); | 20 case IrOpcode::kChangeBitToBool: |
29 if (*loc == NULL) { | 21 return ChangeBitToBool(node->InputAt(0), control); |
30 *loc = graph()->NewNode(common()->ExternalConstant(reference)); | 22 case IrOpcode::kChangeBoolToBit: |
| 23 return ChangeBoolToBit(node->InputAt(0)); |
| 24 case IrOpcode::kChangeInt32ToTagged: |
| 25 return ChangeInt32ToTagged(node->InputAt(0), effect, control); |
| 26 case IrOpcode::kChangeTaggedToFloat64: |
| 27 return ChangeTaggedToFloat64(node->InputAt(0), effect, control); |
| 28 default: |
| 29 return NoChange(); |
31 } | 30 } |
32 return *loc; | 31 UNREACHABLE(); |
| 32 return NoChange(); |
33 } | 33 } |
34 | 34 |
35 | 35 |
36 Node* ChangeLoweringBase::HeapConstant(PrintableUnique<HeapObject> value) { | 36 Node* ChangeLowering::HeapNumberValueIndexConstant() { |
37 // TODO(bmeurer): Use common node cache. | 37 STATIC_ASSERT(HeapNumber::kValueOffset % kPointerSize == 0); |
38 return graph()->NewNode(common()->HeapConstant(value)); | 38 const int heap_number_value_offset = |
| 39 ((HeapNumber::kValueOffset / kPointerSize) * (machine()->is64() ? 8 : 4)); |
| 40 return jsgraph()->Int32Constant(heap_number_value_offset - kHeapObjectTag); |
39 } | 41 } |
40 | 42 |
41 | 43 |
42 Node* ChangeLoweringBase::ImmovableHeapConstant(Handle<HeapObject> value) { | 44 Node* ChangeLowering::SmiShiftBitsConstant() { |
43 return HeapConstant( | 45 const int smi_shift_size = (machine()->is64() ? SmiTagging<8>::kSmiShiftSize |
44 PrintableUnique<HeapObject>::CreateImmovable(graph()->zone(), value)); | 46 : SmiTagging<4>::kSmiShiftSize); |
| 47 return jsgraph()->Int32Constant(smi_shift_size + kSmiTagSize); |
45 } | 48 } |
46 | 49 |
47 | 50 |
48 Node* ChangeLoweringBase::Int32Constant(int32_t value) { | 51 Reduction ChangeLowering::ChangeBitToBool(Node* val, Node* control) { |
49 Node** loc = cache()->FindInt32Constant(value); | |
50 if (*loc == NULL) { | |
51 *loc = graph()->NewNode(common()->Int32Constant(value)); | |
52 } | |
53 return *loc; | |
54 } | |
55 | |
56 | |
57 Node* ChangeLoweringBase::NumberConstant(double value) { | |
58 Node** loc = cache()->FindNumberConstant(value); | |
59 if (*loc == NULL) { | |
60 *loc = graph()->NewNode(common()->NumberConstant(value)); | |
61 } | |
62 return *loc; | |
63 } | |
64 | |
65 | |
66 Node* ChangeLoweringBase::CEntryStubConstant() { | |
67 if (!c_entry_stub_constant_.is_set()) { | |
68 c_entry_stub_constant_.set( | |
69 ImmovableHeapConstant(CEntryStub(isolate(), 1).GetCode())); | |
70 } | |
71 return c_entry_stub_constant_.get(); | |
72 } | |
73 | |
74 | |
75 Node* ChangeLoweringBase::TrueConstant() { | |
76 if (!true_constant_.is_set()) { | |
77 true_constant_.set( | |
78 ImmovableHeapConstant(isolate()->factory()->true_value())); | |
79 } | |
80 return true_constant_.get(); | |
81 } | |
82 | |
83 | |
84 Node* ChangeLoweringBase::FalseConstant() { | |
85 if (!false_constant_.is_set()) { | |
86 false_constant_.set( | |
87 ImmovableHeapConstant(isolate()->factory()->false_value())); | |
88 } | |
89 return false_constant_.get(); | |
90 } | |
91 | |
92 | |
93 Reduction ChangeLoweringBase::ChangeBitToBool(Node* val, Node* control) { | |
94 Node* branch = graph()->NewNode(common()->Branch(), val, control); | 52 Node* branch = graph()->NewNode(common()->Branch(), val, control); |
95 | 53 |
96 Node* if_true = graph()->NewNode(common()->IfTrue(), branch); | 54 Node* if_true = graph()->NewNode(common()->IfTrue(), branch); |
97 Node* true_value = TrueConstant(); | 55 Node* true_value = jsgraph()->TrueConstant(); |
98 | 56 |
99 Node* if_false = graph()->NewNode(common()->IfFalse(), branch); | 57 Node* if_false = graph()->NewNode(common()->IfFalse(), branch); |
100 Node* false_value = FalseConstant(); | 58 Node* false_value = jsgraph()->FalseConstant(); |
101 | 59 |
102 Node* merge = graph()->NewNode(common()->Merge(2), if_true, if_false); | 60 Node* merge = graph()->NewNode(common()->Merge(2), if_true, if_false); |
103 Node* phi = | 61 Node* phi = |
104 graph()->NewNode(common()->Phi(2), true_value, false_value, merge); | 62 graph()->NewNode(common()->Phi(2), true_value, false_value, merge); |
105 | 63 |
106 return Replace(phi); | 64 return Replace(phi); |
107 } | 65 } |
108 | 66 |
109 | 67 |
110 template <size_t kPointerSize> | 68 Reduction ChangeLowering::ChangeBoolToBit(Node* val) { |
111 ChangeLowering<kPointerSize>::ChangeLowering(Graph* graph, Linkage* linkage) | 69 return Replace( |
112 : ChangeLoweringBase(graph, linkage, | 70 graph()->NewNode(machine()->WordEqual(), val, jsgraph()->TrueConstant())); |
113 new (graph->zone()) CommonNodeCache(graph->zone())) {} | |
114 | |
115 | |
116 template <size_t kPointerSize> | |
117 Reduction ChangeLowering<kPointerSize>::Reduce(Node* node) { | |
118 Node* control = graph()->start(); | |
119 Node* effect = control; | |
120 switch (node->opcode()) { | |
121 case IrOpcode::kChangeBitToBool: | |
122 return ChangeBitToBool(node->InputAt(0), control); | |
123 case IrOpcode::kChangeBoolToBit: | |
124 return ChangeBoolToBit(node->InputAt(0)); | |
125 case IrOpcode::kChangeInt32ToTagged: | |
126 return ChangeInt32ToTagged(node->InputAt(0), effect, control); | |
127 case IrOpcode::kChangeTaggedToFloat64: | |
128 return ChangeTaggedToFloat64(node->InputAt(0), effect, control); | |
129 default: | |
130 return NoChange(); | |
131 } | |
132 UNREACHABLE(); | |
133 return NoChange(); | |
134 } | 71 } |
135 | 72 |
136 | 73 |
137 template <> | 74 Reduction ChangeLowering::ChangeInt32ToTagged(Node* val, Node* effect, |
138 Reduction ChangeLowering<4>::ChangeBoolToBit(Node* val) { | 75 Node* control) { |
139 return Replace( | 76 if (machine()->is64()) { |
140 graph()->NewNode(machine()->Word32Equal(), val, TrueConstant())); | 77 return Replace( |
141 } | 78 graph()->NewNode(machine()->WordShl(), val, SmiShiftBitsConstant())); |
| 79 } |
142 | 80 |
143 | 81 Node* context = jsgraph()->SmiConstant(0); |
144 template <> | |
145 Reduction ChangeLowering<8>::ChangeBoolToBit(Node* val) { | |
146 return Replace( | |
147 graph()->NewNode(machine()->Word64Equal(), val, TrueConstant())); | |
148 } | |
149 | |
150 | |
151 template <> | |
152 Reduction ChangeLowering<4>::ChangeInt32ToTagged(Node* val, Node* effect, | |
153 Node* control) { | |
154 Node* context = NumberConstant(0); | |
155 | 82 |
156 Node* add = graph()->NewNode(machine()->Int32AddWithOverflow(), val, val); | 83 Node* add = graph()->NewNode(machine()->Int32AddWithOverflow(), val, val); |
157 Node* ovf = graph()->NewNode(common()->Projection(1), add); | 84 Node* ovf = graph()->NewNode(common()->Projection(1), add); |
158 | 85 |
159 Node* branch = graph()->NewNode(common()->Branch(), ovf, control); | 86 Node* branch = graph()->NewNode(common()->Branch(), ovf, control); |
160 | 87 |
161 Node* if_true = graph()->NewNode(common()->IfTrue(), branch); | 88 Node* if_true = graph()->NewNode(common()->IfTrue(), branch); |
162 Node* number = graph()->NewNode(machine()->ChangeInt32ToFloat64(), val); | 89 Node* number = graph()->NewNode(machine()->ChangeInt32ToFloat64(), val); |
163 | 90 |
164 // TODO(bmeurer): Inline allocation if possible. | 91 // TODO(bmeurer): Inline allocation if possible. |
165 const Runtime::Function* fn = | 92 const Runtime::Function* fn = |
166 Runtime::FunctionForId(Runtime::kAllocateHeapNumber); | 93 Runtime::FunctionForId(Runtime::kAllocateHeapNumber); |
167 DCHECK_EQ(0, fn->nargs); | 94 DCHECK_EQ(0, fn->nargs); |
168 CallDescriptor* desc = linkage()->GetRuntimeCallDescriptor( | 95 CallDescriptor* desc = linkage()->GetRuntimeCallDescriptor( |
169 fn->function_id, 0, Operator::kNoProperties); | 96 fn->function_id, 0, Operator::kNoProperties); |
170 Node* heap_number = | 97 Node* heap_number = graph()->NewNode( |
171 graph()->NewNode(common()->Call(desc), CEntryStubConstant(), | 98 common()->Call(desc), jsgraph()->CEntryStubConstant(), |
172 ExternalConstant(ExternalReference(fn, isolate())), | 99 jsgraph()->ExternalConstant(ExternalReference(fn, isolate())), |
173 Int32Constant(0), context, effect, if_true); | 100 jsgraph()->ZeroConstant(), context, effect, if_true); |
174 | 101 |
175 Node* store = graph()->NewNode( | 102 Node* store = graph()->NewNode( |
176 machine()->Store(kMachFloat64, kNoWriteBarrier), heap_number, | 103 machine()->Store(kMachFloat64, kNoWriteBarrier), heap_number, |
177 Int32Constant(HeapNumber::kValueOffset - kHeapObjectTag), number, effect, | 104 HeapNumberValueIndexConstant(), number, effect, heap_number); |
178 heap_number); | |
179 | 105 |
180 Node* if_false = graph()->NewNode(common()->IfFalse(), branch); | 106 Node* if_false = graph()->NewNode(common()->IfFalse(), branch); |
181 Node* smi = graph()->NewNode(common()->Projection(0), add); | 107 Node* smi = graph()->NewNode(common()->Projection(0), add); |
182 | 108 |
183 Node* merge = graph()->NewNode(common()->Merge(2), store, if_false); | 109 Node* merge = graph()->NewNode(common()->Merge(2), store, if_false); |
184 Node* phi = graph()->NewNode(common()->Phi(2), heap_number, smi, merge); | 110 Node* phi = graph()->NewNode(common()->Phi(2), heap_number, smi, merge); |
185 | 111 |
186 return Replace(phi); | 112 return Replace(phi); |
187 } | 113 } |
188 | 114 |
189 | 115 |
190 template <> | 116 Reduction ChangeLowering::ChangeTaggedToFloat64(Node* val, Node* effect, |
191 Reduction ChangeLowering<8>::ChangeInt32ToTagged(Node* val, Node* effect, | 117 Node* control) { |
192 Node* control) { | 118 STATIC_ASSERT(kSmiTagMask == 1); |
193 return Replace(graph()->NewNode( | |
194 machine()->Word64Shl(), val, | |
195 Int32Constant(SmiTagging<8>::kSmiShiftSize + kSmiTagSize))); | |
196 } | |
197 | 119 |
198 | 120 Node* tag = graph()->NewNode(machine()->WordAnd(), val, |
199 template <> | 121 jsgraph()->Int32Constant(kSmiTagMask)); |
200 Reduction ChangeLowering<4>::ChangeTaggedToFloat64(Node* val, Node* effect, | 122 Node* branch = graph()->NewNode(common()->Branch(), tag, control); |
201 Node* control) { | |
202 Node* branch = graph()->NewNode( | |
203 common()->Branch(), | |
204 graph()->NewNode(machine()->Word32And(), val, Int32Constant(kSmiTagMask)), | |
205 control); | |
206 | 123 |
207 Node* if_true = graph()->NewNode(common()->IfTrue(), branch); | 124 Node* if_true = graph()->NewNode(common()->IfTrue(), branch); |
208 Node* load = graph()->NewNode( | 125 Node* load = graph()->NewNode(machine()->Load(kMachFloat64), val, |
209 machine()->Load(kMachFloat64), val, | 126 HeapNumberValueIndexConstant(), if_true); |
210 Int32Constant(HeapNumber::kValueOffset - kHeapObjectTag), if_true); | |
211 | 127 |
212 Node* if_false = graph()->NewNode(common()->IfFalse(), branch); | 128 Node* if_false = graph()->NewNode(common()->IfFalse(), branch); |
| 129 Node* integer = |
| 130 graph()->NewNode(machine()->WordSar(), val, SmiShiftBitsConstant()); |
213 Node* number = graph()->NewNode( | 131 Node* number = graph()->NewNode( |
214 machine()->ChangeInt32ToFloat64(), | 132 machine()->ChangeInt32ToFloat64(), |
215 graph()->NewNode( | 133 machine()->is64() |
216 machine()->Word32Sar(), val, | 134 ? graph()->NewNode(machine()->ConvertInt64ToInt32(), integer) |
217 Int32Constant(SmiTagging<4>::kSmiShiftSize + kSmiTagSize))); | 135 : integer); |
218 | 136 |
219 Node* merge = graph()->NewNode(common()->Merge(2), if_true, if_false); | 137 Node* merge = graph()->NewNode(common()->Merge(2), if_true, if_false); |
220 Node* phi = graph()->NewNode(common()->Phi(2), load, number, merge); | 138 Node* phi = graph()->NewNode(common()->Phi(2), load, number, merge); |
221 | 139 |
222 return Replace(phi); | 140 return Replace(phi); |
223 } | 141 } |
224 | 142 |
225 | 143 |
226 template <> | 144 Isolate* ChangeLowering::isolate() const { return jsgraph()->isolate(); } |
227 Reduction ChangeLowering<8>::ChangeTaggedToFloat64(Node* val, Node* effect, | |
228 Node* control) { | |
229 Node* branch = graph()->NewNode( | |
230 common()->Branch(), | |
231 graph()->NewNode(machine()->Word64And(), val, Int32Constant(kSmiTagMask)), | |
232 control); | |
233 | |
234 Node* if_true = graph()->NewNode(common()->IfTrue(), branch); | |
235 Node* load = graph()->NewNode( | |
236 machine()->Load(kMachFloat64), val, | |
237 Int32Constant(HeapNumber::kValueOffset - kHeapObjectTag), if_true); | |
238 | |
239 Node* if_false = graph()->NewNode(common()->IfFalse(), branch); | |
240 Node* number = graph()->NewNode( | |
241 machine()->ChangeInt32ToFloat64(), | |
242 graph()->NewNode( | |
243 machine()->ConvertInt64ToInt32(), | |
244 graph()->NewNode( | |
245 machine()->Word64Sar(), val, | |
246 Int32Constant(SmiTagging<8>::kSmiShiftSize + kSmiTagSize)))); | |
247 | |
248 Node* merge = graph()->NewNode(common()->Merge(2), if_true, if_false); | |
249 Node* phi = graph()->NewNode(common()->Phi(2), load, number, merge); | |
250 | |
251 return Replace(phi); | |
252 } | |
253 | 145 |
254 | 146 |
255 template class ChangeLowering<4>; | 147 Graph* ChangeLowering::graph() const { return jsgraph()->graph(); } |
256 template class ChangeLowering<8>; | 148 |
| 149 |
| 150 CommonOperatorBuilder* ChangeLowering::common() const { |
| 151 return jsgraph()->common(); |
| 152 } |
257 | 153 |
258 } // namespace compiler | 154 } // namespace compiler |
259 } // namespace internal | 155 } // namespace internal |
260 } // namespace v8 | 156 } // namespace v8 |
OLD | NEW |