blob: 4b50ffe61ea46c67bd5ae376de040c4123ec9eb3 [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#include "src/compiler/machine-operator-reducer.h"
6
7#include "src/base/bits.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008#include "src/base/division-by-constant.h"
9#include "src/codegen.h"
10#include "src/compiler/diamond.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011#include "src/compiler/graph.h"
12#include "src/compiler/js-graph.h"
13#include "src/compiler/node-matchers.h"
14
15namespace v8 {
16namespace internal {
17namespace compiler {
18
19MachineOperatorReducer::MachineOperatorReducer(JSGraph* jsgraph)
20 : jsgraph_(jsgraph) {}
21
22
23MachineOperatorReducer::~MachineOperatorReducer() {}
24
25
26Node* MachineOperatorReducer::Float32Constant(volatile float value) {
27 return graph()->NewNode(common()->Float32Constant(value));
28}
29
30
31Node* MachineOperatorReducer::Float64Constant(volatile double value) {
32 return jsgraph()->Float64Constant(value);
33}
34
35
36Node* MachineOperatorReducer::Int32Constant(int32_t value) {
37 return jsgraph()->Int32Constant(value);
38}
39
40
41Node* MachineOperatorReducer::Int64Constant(int64_t value) {
42 return graph()->NewNode(common()->Int64Constant(value));
43}
44
45
Emily Bernierd0a1eb72015-03-24 16:35:39 -040046Node* MachineOperatorReducer::Word32And(Node* lhs, Node* rhs) {
47 Node* const node = graph()->NewNode(machine()->Word32And(), lhs, rhs);
48 Reduction const reduction = ReduceWord32And(node);
49 return reduction.Changed() ? reduction.replacement() : node;
50}
51
52
53Node* MachineOperatorReducer::Word32Sar(Node* lhs, uint32_t rhs) {
54 if (rhs == 0) return lhs;
55 return graph()->NewNode(machine()->Word32Sar(), lhs, Uint32Constant(rhs));
56}
57
58
59Node* MachineOperatorReducer::Word32Shr(Node* lhs, uint32_t rhs) {
60 if (rhs == 0) return lhs;
61 return graph()->NewNode(machine()->Word32Shr(), lhs, Uint32Constant(rhs));
62}
63
64
65Node* MachineOperatorReducer::Word32Equal(Node* lhs, Node* rhs) {
66 return graph()->NewNode(machine()->Word32Equal(), lhs, rhs);
67}
68
69
70Node* MachineOperatorReducer::Int32Add(Node* lhs, Node* rhs) {
71 Node* const node = graph()->NewNode(machine()->Int32Add(), lhs, rhs);
72 Reduction const reduction = ReduceInt32Add(node);
73 return reduction.Changed() ? reduction.replacement() : node;
74}
75
76
77Node* MachineOperatorReducer::Int32Sub(Node* lhs, Node* rhs) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000078 Node* const node = graph()->NewNode(machine()->Int32Sub(), lhs, rhs);
79 Reduction const reduction = ReduceInt32Sub(node);
80 return reduction.Changed() ? reduction.replacement() : node;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040081}
82
83
84Node* MachineOperatorReducer::Int32Mul(Node* lhs, Node* rhs) {
85 return graph()->NewNode(machine()->Int32Mul(), lhs, rhs);
86}
87
88
89Node* MachineOperatorReducer::Int32Div(Node* dividend, int32_t divisor) {
90 DCHECK_NE(0, divisor);
91 DCHECK_NE(std::numeric_limits<int32_t>::min(), divisor);
92 base::MagicNumbersForDivision<uint32_t> const mag =
93 base::SignedDivisionByConstant(bit_cast<uint32_t>(divisor));
94 Node* quotient = graph()->NewNode(machine()->Int32MulHigh(), dividend,
95 Uint32Constant(mag.multiplier));
96 if (divisor > 0 && bit_cast<int32_t>(mag.multiplier) < 0) {
97 quotient = Int32Add(quotient, dividend);
98 } else if (divisor < 0 && bit_cast<int32_t>(mag.multiplier) > 0) {
99 quotient = Int32Sub(quotient, dividend);
100 }
101 return Int32Add(Word32Sar(quotient, mag.shift), Word32Shr(dividend, 31));
102}
103
104
105Node* MachineOperatorReducer::Uint32Div(Node* dividend, uint32_t divisor) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000106 DCHECK_LT(0u, divisor);
107 // If the divisor is even, we can avoid using the expensive fixup by shifting
108 // the dividend upfront.
109 unsigned const shift = base::bits::CountTrailingZeros32(divisor);
110 dividend = Word32Shr(dividend, shift);
111 divisor >>= shift;
112 // Compute the magic number for the (shifted) divisor.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400113 base::MagicNumbersForDivision<uint32_t> const mag =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000114 base::UnsignedDivisionByConstant(divisor, shift);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400115 Node* quotient = graph()->NewNode(machine()->Uint32MulHigh(), dividend,
116 Uint32Constant(mag.multiplier));
117 if (mag.add) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000118 DCHECK_LE(1u, mag.shift);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400119 quotient = Word32Shr(
120 Int32Add(Word32Shr(Int32Sub(dividend, quotient), 1), quotient),
121 mag.shift - 1);
122 } else {
123 quotient = Word32Shr(quotient, mag.shift);
124 }
125 return quotient;
126}
127
128
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000129// Perform constant folding and strength reduction on machine operators.
130Reduction MachineOperatorReducer::Reduce(Node* node) {
131 switch (node->opcode()) {
132 case IrOpcode::kProjection:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000133 return ReduceProjection(ProjectionIndexOf(node->op()), node->InputAt(0));
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400134 case IrOpcode::kWord32And:
135 return ReduceWord32And(node);
136 case IrOpcode::kWord32Or:
137 return ReduceWord32Or(node);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000138 case IrOpcode::kWord32Xor: {
139 Int32BinopMatcher m(node);
140 if (m.right().Is(0)) return Replace(m.left().node()); // x ^ 0 => x
141 if (m.IsFoldable()) { // K ^ K => K
142 return ReplaceInt32(m.left().Value() ^ m.right().Value());
143 }
144 if (m.LeftEqualsRight()) return ReplaceInt32(0); // x ^ x => 0
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400145 if (m.left().IsWord32Xor() && m.right().Is(-1)) {
146 Int32BinopMatcher mleft(m.left().node());
147 if (mleft.right().Is(-1)) { // (x ^ -1) ^ -1 => x
148 return Replace(mleft.left().node());
149 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000150 }
151 break;
152 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400153 case IrOpcode::kWord32Shl:
154 return ReduceWord32Shl(node);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000155 case IrOpcode::kWord32Shr: {
156 Uint32BinopMatcher m(node);
157 if (m.right().Is(0)) return Replace(m.left().node()); // x >>> 0 => x
158 if (m.IsFoldable()) { // K >>> K => K
159 return ReplaceInt32(m.left().Value() >> m.right().Value());
160 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400161 return ReduceWord32Shifts(node);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000162 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000163 case IrOpcode::kWord32Sar:
164 return ReduceWord32Sar(node);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000165 case IrOpcode::kWord32Ror: {
166 Int32BinopMatcher m(node);
167 if (m.right().Is(0)) return Replace(m.left().node()); // x ror 0 => x
168 if (m.IsFoldable()) { // K ror K => K
169 return ReplaceInt32(
170 base::bits::RotateRight32(m.left().Value(), m.right().Value()));
171 }
172 break;
173 }
174 case IrOpcode::kWord32Equal: {
175 Int32BinopMatcher m(node);
176 if (m.IsFoldable()) { // K == K => K
177 return ReplaceBool(m.left().Value() == m.right().Value());
178 }
179 if (m.left().IsInt32Sub() && m.right().Is(0)) { // x - y == 0 => x == y
180 Int32BinopMatcher msub(m.left().node());
181 node->ReplaceInput(0, msub.left().node());
182 node->ReplaceInput(1, msub.right().node());
183 return Changed(node);
184 }
185 // TODO(turbofan): fold HeapConstant, ExternalReference, pointer compares
186 if (m.LeftEqualsRight()) return ReplaceBool(true); // x == x => true
187 break;
188 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400189 case IrOpcode::kWord64Equal: {
190 Int64BinopMatcher m(node);
191 if (m.IsFoldable()) { // K == K => K
192 return ReplaceBool(m.left().Value() == m.right().Value());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000193 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400194 if (m.left().IsInt64Sub() && m.right().Is(0)) { // x - y == 0 => x == y
195 Int64BinopMatcher msub(m.left().node());
196 node->ReplaceInput(0, msub.left().node());
197 node->ReplaceInput(1, msub.right().node());
198 return Changed(node);
199 }
200 // TODO(turbofan): fold HeapConstant, ExternalReference, pointer compares
201 if (m.LeftEqualsRight()) return ReplaceBool(true); // x == x => true
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000202 break;
203 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400204 case IrOpcode::kInt32Add:
205 return ReduceInt32Add(node);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000206 case IrOpcode::kInt32Sub:
207 return ReduceInt32Sub(node);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000208 case IrOpcode::kInt32Mul: {
209 Int32BinopMatcher m(node);
210 if (m.right().Is(0)) return Replace(m.right().node()); // x * 0 => 0
211 if (m.right().Is(1)) return Replace(m.left().node()); // x * 1 => x
212 if (m.IsFoldable()) { // K * K => K
213 return ReplaceInt32(m.left().Value() * m.right().Value());
214 }
215 if (m.right().Is(-1)) { // x * -1 => 0 - x
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000216 node->ReplaceInput(0, Int32Constant(0));
217 node->ReplaceInput(1, m.left().node());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000218 NodeProperties::ChangeOp(node, machine()->Int32Sub());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000219 return Changed(node);
220 }
221 if (m.right().IsPowerOf2()) { // x * 2^n => x << n
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000222 node->ReplaceInput(1, Int32Constant(WhichPowerOf2(m.right().Value())));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000223 NodeProperties::ChangeOp(node, machine()->Word32Shl());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400224 Reduction reduction = ReduceWord32Shl(node);
225 return reduction.Changed() ? reduction : Changed(node);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000226 }
227 break;
228 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400229 case IrOpcode::kInt32Div:
230 return ReduceInt32Div(node);
231 case IrOpcode::kUint32Div:
232 return ReduceUint32Div(node);
233 case IrOpcode::kInt32Mod:
234 return ReduceInt32Mod(node);
235 case IrOpcode::kUint32Mod:
236 return ReduceUint32Mod(node);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000237 case IrOpcode::kInt32LessThan: {
238 Int32BinopMatcher m(node);
239 if (m.IsFoldable()) { // K < K => K
240 return ReplaceBool(m.left().Value() < m.right().Value());
241 }
242 if (m.left().IsInt32Sub() && m.right().Is(0)) { // x - y < 0 => x < y
243 Int32BinopMatcher msub(m.left().node());
244 node->ReplaceInput(0, msub.left().node());
245 node->ReplaceInput(1, msub.right().node());
246 return Changed(node);
247 }
248 if (m.left().Is(0) && m.right().IsInt32Sub()) { // 0 < x - y => y < x
249 Int32BinopMatcher msub(m.right().node());
250 node->ReplaceInput(0, msub.right().node());
251 node->ReplaceInput(1, msub.left().node());
252 return Changed(node);
253 }
254 if (m.LeftEqualsRight()) return ReplaceBool(false); // x < x => false
255 break;
256 }
257 case IrOpcode::kInt32LessThanOrEqual: {
258 Int32BinopMatcher m(node);
259 if (m.IsFoldable()) { // K <= K => K
260 return ReplaceBool(m.left().Value() <= m.right().Value());
261 }
262 if (m.left().IsInt32Sub() && m.right().Is(0)) { // x - y <= 0 => x <= y
263 Int32BinopMatcher msub(m.left().node());
264 node->ReplaceInput(0, msub.left().node());
265 node->ReplaceInput(1, msub.right().node());
266 return Changed(node);
267 }
268 if (m.left().Is(0) && m.right().IsInt32Sub()) { // 0 <= x - y => y <= x
269 Int32BinopMatcher msub(m.right().node());
270 node->ReplaceInput(0, msub.right().node());
271 node->ReplaceInput(1, msub.left().node());
272 return Changed(node);
273 }
274 if (m.LeftEqualsRight()) return ReplaceBool(true); // x <= x => true
275 break;
276 }
277 case IrOpcode::kUint32LessThan: {
278 Uint32BinopMatcher m(node);
279 if (m.left().Is(kMaxUInt32)) return ReplaceBool(false); // M < x => false
280 if (m.right().Is(0)) return ReplaceBool(false); // x < 0 => false
281 if (m.IsFoldable()) { // K < K => K
282 return ReplaceBool(m.left().Value() < m.right().Value());
283 }
284 if (m.LeftEqualsRight()) return ReplaceBool(false); // x < x => false
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400285 if (m.left().IsWord32Sar() && m.right().HasValue()) {
286 Int32BinopMatcher mleft(m.left().node());
287 if (mleft.right().HasValue()) {
288 // (x >> K) < C => x < (C << K)
289 // when C < (M >> K)
290 const uint32_t c = m.right().Value();
291 const uint32_t k = mleft.right().Value() & 0x1f;
292 if (c < static_cast<uint32_t>(kMaxInt >> k)) {
293 node->ReplaceInput(0, mleft.left().node());
294 node->ReplaceInput(1, Uint32Constant(c << k));
295 return Changed(node);
296 }
297 // TODO(turbofan): else the comparison is always true.
298 }
299 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000300 break;
301 }
302 case IrOpcode::kUint32LessThanOrEqual: {
303 Uint32BinopMatcher m(node);
304 if (m.left().Is(0)) return ReplaceBool(true); // 0 <= x => true
305 if (m.right().Is(kMaxUInt32)) return ReplaceBool(true); // x <= M => true
306 if (m.IsFoldable()) { // K <= K => K
307 return ReplaceBool(m.left().Value() <= m.right().Value());
308 }
309 if (m.LeftEqualsRight()) return ReplaceBool(true); // x <= x => true
310 break;
311 }
312 case IrOpcode::kFloat64Add: {
313 Float64BinopMatcher m(node);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400314 if (m.right().IsNaN()) { // x + NaN => NaN
315 return Replace(m.right().node());
316 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000317 if (m.IsFoldable()) { // K + K => K
318 return ReplaceFloat64(m.left().Value() + m.right().Value());
319 }
320 break;
321 }
322 case IrOpcode::kFloat64Sub: {
323 Float64BinopMatcher m(node);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400324 if (m.right().Is(0) && (Double(m.right().Value()).Sign() > 0)) {
325 return Replace(m.left().node()); // x - 0 => x
326 }
327 if (m.right().IsNaN()) { // x - NaN => NaN
328 return Replace(m.right().node());
329 }
330 if (m.left().IsNaN()) { // NaN - x => NaN
331 return Replace(m.left().node());
332 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000333 if (m.IsFoldable()) { // K - K => K
334 return ReplaceFloat64(m.left().Value() - m.right().Value());
335 }
336 break;
337 }
338 case IrOpcode::kFloat64Mul: {
339 Float64BinopMatcher m(node);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400340 if (m.right().Is(-1)) { // x * -1.0 => -0.0 - x
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400341 node->ReplaceInput(0, Float64Constant(-0.0));
342 node->ReplaceInput(1, m.left().node());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000343 NodeProperties::ChangeOp(node, machine()->Float64Sub());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400344 return Changed(node);
345 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000346 if (m.right().Is(1)) return Replace(m.left().node()); // x * 1.0 => x
347 if (m.right().IsNaN()) { // x * NaN => NaN
348 return Replace(m.right().node());
349 }
350 if (m.IsFoldable()) { // K * K => K
351 return ReplaceFloat64(m.left().Value() * m.right().Value());
352 }
353 break;
354 }
355 case IrOpcode::kFloat64Div: {
356 Float64BinopMatcher m(node);
357 if (m.right().Is(1)) return Replace(m.left().node()); // x / 1.0 => x
358 if (m.right().IsNaN()) { // x / NaN => NaN
359 return Replace(m.right().node());
360 }
361 if (m.left().IsNaN()) { // NaN / x => NaN
362 return Replace(m.left().node());
363 }
364 if (m.IsFoldable()) { // K / K => K
365 return ReplaceFloat64(m.left().Value() / m.right().Value());
366 }
367 break;
368 }
369 case IrOpcode::kFloat64Mod: {
370 Float64BinopMatcher m(node);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400371 if (m.right().Is(0)) { // x % 0 => NaN
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000372 return ReplaceFloat64(std::numeric_limits<double>::quiet_NaN());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400373 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000374 if (m.right().IsNaN()) { // x % NaN => NaN
375 return Replace(m.right().node());
376 }
377 if (m.left().IsNaN()) { // NaN % x => NaN
378 return Replace(m.left().node());
379 }
380 if (m.IsFoldable()) { // K % K => K
381 return ReplaceFloat64(modulo(m.left().Value(), m.right().Value()));
382 }
383 break;
384 }
385 case IrOpcode::kChangeFloat32ToFloat64: {
386 Float32Matcher m(node->InputAt(0));
387 if (m.HasValue()) return ReplaceFloat64(m.Value());
388 break;
389 }
390 case IrOpcode::kChangeFloat64ToInt32: {
391 Float64Matcher m(node->InputAt(0));
392 if (m.HasValue()) return ReplaceInt32(FastD2I(m.Value()));
393 if (m.IsChangeInt32ToFloat64()) return Replace(m.node()->InputAt(0));
394 break;
395 }
396 case IrOpcode::kChangeFloat64ToUint32: {
397 Float64Matcher m(node->InputAt(0));
398 if (m.HasValue()) return ReplaceInt32(FastD2UI(m.Value()));
399 if (m.IsChangeUint32ToFloat64()) return Replace(m.node()->InputAt(0));
400 break;
401 }
402 case IrOpcode::kChangeInt32ToFloat64: {
403 Int32Matcher m(node->InputAt(0));
404 if (m.HasValue()) return ReplaceFloat64(FastI2D(m.Value()));
405 break;
406 }
407 case IrOpcode::kChangeInt32ToInt64: {
408 Int32Matcher m(node->InputAt(0));
409 if (m.HasValue()) return ReplaceInt64(m.Value());
410 break;
411 }
412 case IrOpcode::kChangeUint32ToFloat64: {
413 Uint32Matcher m(node->InputAt(0));
414 if (m.HasValue()) return ReplaceFloat64(FastUI2D(m.Value()));
415 break;
416 }
417 case IrOpcode::kChangeUint32ToUint64: {
418 Uint32Matcher m(node->InputAt(0));
419 if (m.HasValue()) return ReplaceInt64(static_cast<uint64_t>(m.Value()));
420 break;
421 }
Ben Murdochc5610432016-08-08 18:44:38 +0100422 case IrOpcode::kTruncateFloat64ToWord32: {
423 Float64Matcher m(node->InputAt(0));
424 if (m.HasValue()) return ReplaceInt32(DoubleToInt32(m.Value()));
425 if (m.IsChangeInt32ToFloat64()) return Replace(m.node()->InputAt(0));
426 return NoChange();
427 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000428 case IrOpcode::kTruncateInt64ToInt32: {
429 Int64Matcher m(node->InputAt(0));
430 if (m.HasValue()) return ReplaceInt32(static_cast<int32_t>(m.Value()));
431 if (m.IsChangeInt32ToInt64()) return Replace(m.node()->InputAt(0));
432 break;
433 }
434 case IrOpcode::kTruncateFloat64ToFloat32: {
435 Float64Matcher m(node->InputAt(0));
436 if (m.HasValue()) return ReplaceFloat32(DoubleToFloat32(m.Value()));
437 if (m.IsChangeFloat32ToFloat64()) return Replace(m.node()->InputAt(0));
438 break;
439 }
Ben Murdochc5610432016-08-08 18:44:38 +0100440 case IrOpcode::kRoundFloat64ToInt32: {
441 Float64Matcher m(node->InputAt(0));
442 if (m.HasValue()) return ReplaceInt32(static_cast<int32_t>(m.Value()));
443 if (m.IsChangeInt32ToFloat64()) return Replace(m.node()->InputAt(0));
444 break;
445 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000446 case IrOpcode::kFloat64InsertLowWord32:
447 return ReduceFloat64InsertLowWord32(node);
448 case IrOpcode::kFloat64InsertHighWord32:
449 return ReduceFloat64InsertHighWord32(node);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400450 case IrOpcode::kStore:
Ben Murdochc5610432016-08-08 18:44:38 +0100451 case IrOpcode::kCheckedStore:
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400452 return ReduceStore(node);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000453 case IrOpcode::kFloat64Equal:
454 case IrOpcode::kFloat64LessThan:
455 case IrOpcode::kFloat64LessThanOrEqual:
456 return ReduceFloat64Compare(node);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400457 default:
458 break;
459 }
460 return NoChange();
461}
462
463
464Reduction MachineOperatorReducer::ReduceInt32Add(Node* node) {
465 DCHECK_EQ(IrOpcode::kInt32Add, node->opcode());
466 Int32BinopMatcher m(node);
467 if (m.right().Is(0)) return Replace(m.left().node()); // x + 0 => x
468 if (m.IsFoldable()) { // K + K => K
469 return ReplaceUint32(bit_cast<uint32_t>(m.left().Value()) +
470 bit_cast<uint32_t>(m.right().Value()));
471 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000472 if (m.left().IsInt32Sub()) {
473 Int32BinopMatcher mleft(m.left().node());
474 if (mleft.left().Is(0)) { // (0 - x) + y => y - x
475 node->ReplaceInput(0, m.right().node());
476 node->ReplaceInput(1, mleft.right().node());
477 NodeProperties::ChangeOp(node, machine()->Int32Sub());
478 Reduction const reduction = ReduceInt32Sub(node);
479 return reduction.Changed() ? reduction : Changed(node);
480 }
481 }
482 if (m.right().IsInt32Sub()) {
483 Int32BinopMatcher mright(m.right().node());
484 if (mright.left().Is(0)) { // y + (0 - x) => y - x
485 node->ReplaceInput(1, mright.right().node());
486 NodeProperties::ChangeOp(node, machine()->Int32Sub());
487 Reduction const reduction = ReduceInt32Sub(node);
488 return reduction.Changed() ? reduction : Changed(node);
489 }
490 }
491 return NoChange();
492}
493
494
495Reduction MachineOperatorReducer::ReduceInt32Sub(Node* node) {
496 DCHECK_EQ(IrOpcode::kInt32Sub, node->opcode());
497 Int32BinopMatcher m(node);
498 if (m.right().Is(0)) return Replace(m.left().node()); // x - 0 => x
499 if (m.IsFoldable()) { // K - K => K
500 return ReplaceInt32(static_cast<uint32_t>(m.left().Value()) -
501 static_cast<uint32_t>(m.right().Value()));
502 }
503 if (m.LeftEqualsRight()) return ReplaceInt32(0); // x - x => 0
504 if (m.right().HasValue()) { // x - K => x + -K
505 node->ReplaceInput(1, Int32Constant(-m.right().Value()));
506 NodeProperties::ChangeOp(node, machine()->Int32Add());
507 Reduction const reduction = ReduceInt32Add(node);
508 return reduction.Changed() ? reduction : Changed(node);
509 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400510 return NoChange();
511}
512
513
514Reduction MachineOperatorReducer::ReduceInt32Div(Node* node) {
515 Int32BinopMatcher m(node);
516 if (m.left().Is(0)) return Replace(m.left().node()); // 0 / x => 0
517 if (m.right().Is(0)) return Replace(m.right().node()); // x / 0 => 0
518 if (m.right().Is(1)) return Replace(m.left().node()); // x / 1 => x
519 if (m.IsFoldable()) { // K / K => K
520 return ReplaceInt32(
521 base::bits::SignedDiv32(m.left().Value(), m.right().Value()));
522 }
523 if (m.LeftEqualsRight()) { // x / x => x != 0
524 Node* const zero = Int32Constant(0);
525 return Replace(Word32Equal(Word32Equal(m.left().node(), zero), zero));
526 }
527 if (m.right().Is(-1)) { // x / -1 => 0 - x
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400528 node->ReplaceInput(0, Int32Constant(0));
529 node->ReplaceInput(1, m.left().node());
530 node->TrimInputCount(2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000531 NodeProperties::ChangeOp(node, machine()->Int32Sub());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400532 return Changed(node);
533 }
534 if (m.right().HasValue()) {
535 int32_t const divisor = m.right().Value();
536 Node* const dividend = m.left().node();
537 Node* quotient = dividend;
538 if (base::bits::IsPowerOfTwo32(Abs(divisor))) {
539 uint32_t const shift = WhichPowerOf2Abs(divisor);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000540 DCHECK_NE(0u, shift);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400541 if (shift > 1) {
542 quotient = Word32Sar(quotient, 31);
543 }
544 quotient = Int32Add(Word32Shr(quotient, 32u - shift), dividend);
545 quotient = Word32Sar(quotient, shift);
546 } else {
547 quotient = Int32Div(quotient, Abs(divisor));
548 }
549 if (divisor < 0) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400550 node->ReplaceInput(0, Int32Constant(0));
551 node->ReplaceInput(1, quotient);
552 node->TrimInputCount(2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000553 NodeProperties::ChangeOp(node, machine()->Int32Sub());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400554 return Changed(node);
555 }
556 return Replace(quotient);
557 }
558 return NoChange();
559}
560
561
562Reduction MachineOperatorReducer::ReduceUint32Div(Node* node) {
563 Uint32BinopMatcher m(node);
564 if (m.left().Is(0)) return Replace(m.left().node()); // 0 / x => 0
565 if (m.right().Is(0)) return Replace(m.right().node()); // x / 0 => 0
566 if (m.right().Is(1)) return Replace(m.left().node()); // x / 1 => x
567 if (m.IsFoldable()) { // K / K => K
568 return ReplaceUint32(
569 base::bits::UnsignedDiv32(m.left().Value(), m.right().Value()));
570 }
571 if (m.LeftEqualsRight()) { // x / x => x != 0
572 Node* const zero = Int32Constant(0);
573 return Replace(Word32Equal(Word32Equal(m.left().node(), zero), zero));
574 }
575 if (m.right().HasValue()) {
576 Node* const dividend = m.left().node();
577 uint32_t const divisor = m.right().Value();
578 if (base::bits::IsPowerOfTwo32(divisor)) { // x / 2^n => x >> n
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400579 node->ReplaceInput(1, Uint32Constant(WhichPowerOf2(m.right().Value())));
580 node->TrimInputCount(2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000581 NodeProperties::ChangeOp(node, machine()->Word32Shr());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400582 return Changed(node);
583 } else {
584 return Replace(Uint32Div(dividend, divisor));
585 }
586 }
587 return NoChange();
588}
589
590
591Reduction MachineOperatorReducer::ReduceInt32Mod(Node* node) {
592 Int32BinopMatcher m(node);
593 if (m.left().Is(0)) return Replace(m.left().node()); // 0 % x => 0
594 if (m.right().Is(0)) return Replace(m.right().node()); // x % 0 => 0
595 if (m.right().Is(1)) return ReplaceInt32(0); // x % 1 => 0
596 if (m.right().Is(-1)) return ReplaceInt32(0); // x % -1 => 0
597 if (m.LeftEqualsRight()) return ReplaceInt32(0); // x % x => 0
598 if (m.IsFoldable()) { // K % K => K
599 return ReplaceInt32(
600 base::bits::SignedMod32(m.left().Value(), m.right().Value()));
601 }
602 if (m.right().HasValue()) {
603 Node* const dividend = m.left().node();
604 int32_t const divisor = Abs(m.right().Value());
605 if (base::bits::IsPowerOfTwo32(divisor)) {
606 uint32_t const mask = divisor - 1;
607 Node* const zero = Int32Constant(0);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400608 node->ReplaceInput(
609 0, graph()->NewNode(machine()->Int32LessThan(), dividend, zero));
610 node->ReplaceInput(
611 1, Int32Sub(zero, Word32And(Int32Sub(zero, dividend), mask)));
612 node->ReplaceInput(2, Word32And(dividend, mask));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000613 NodeProperties::ChangeOp(
614 node,
615 common()->Select(MachineRepresentation::kWord32, BranchHint::kFalse));
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400616 } else {
617 Node* quotient = Int32Div(dividend, divisor);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400618 DCHECK_EQ(dividend, node->InputAt(0));
619 node->ReplaceInput(1, Int32Mul(quotient, Int32Constant(divisor)));
620 node->TrimInputCount(2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000621 NodeProperties::ChangeOp(node, machine()->Int32Sub());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400622 }
623 return Changed(node);
624 }
625 return NoChange();
626}
627
628
629Reduction MachineOperatorReducer::ReduceUint32Mod(Node* node) {
630 Uint32BinopMatcher m(node);
631 if (m.left().Is(0)) return Replace(m.left().node()); // 0 % x => 0
632 if (m.right().Is(0)) return Replace(m.right().node()); // x % 0 => 0
633 if (m.right().Is(1)) return ReplaceUint32(0); // x % 1 => 0
634 if (m.LeftEqualsRight()) return ReplaceInt32(0); // x % x => 0
635 if (m.IsFoldable()) { // K % K => K
636 return ReplaceUint32(
637 base::bits::UnsignedMod32(m.left().Value(), m.right().Value()));
638 }
639 if (m.right().HasValue()) {
640 Node* const dividend = m.left().node();
641 uint32_t const divisor = m.right().Value();
642 if (base::bits::IsPowerOfTwo32(divisor)) { // x % 2^n => x & 2^n-1
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400643 node->ReplaceInput(1, Uint32Constant(m.right().Value() - 1));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000644 node->TrimInputCount(2);
645 NodeProperties::ChangeOp(node, machine()->Word32And());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400646 } else {
647 Node* quotient = Uint32Div(dividend, divisor);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400648 DCHECK_EQ(dividend, node->InputAt(0));
649 node->ReplaceInput(1, Int32Mul(quotient, Uint32Constant(divisor)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000650 node->TrimInputCount(2);
651 NodeProperties::ChangeOp(node, machine()->Int32Sub());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400652 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400653 return Changed(node);
654 }
655 return NoChange();
656}
657
658
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400659Reduction MachineOperatorReducer::ReduceStore(Node* node) {
Ben Murdochc5610432016-08-08 18:44:38 +0100660 NodeMatcher nm(node);
661 MachineRepresentation rep;
662 int value_input;
663 if (nm.IsCheckedStore()) {
664 rep = CheckedStoreRepresentationOf(node->op());
665 value_input = 3;
666 } else {
667 rep = StoreRepresentationOf(node->op()).representation();
668 value_input = 2;
669 }
670
671 Node* const value = node->InputAt(value_input);
672
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400673 switch (value->opcode()) {
674 case IrOpcode::kWord32And: {
675 Uint32BinopMatcher m(value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000676 if (m.right().HasValue() && ((rep == MachineRepresentation::kWord8 &&
677 (m.right().Value() & 0xff) == 0xff) ||
678 (rep == MachineRepresentation::kWord16 &&
679 (m.right().Value() & 0xffff) == 0xffff))) {
Ben Murdochc5610432016-08-08 18:44:38 +0100680 node->ReplaceInput(value_input, m.left().node());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400681 return Changed(node);
682 }
683 break;
684 }
685 case IrOpcode::kWord32Sar: {
686 Int32BinopMatcher m(value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000687 if (m.left().IsWord32Shl() && ((rep == MachineRepresentation::kWord8 &&
688 m.right().IsInRange(1, 24)) ||
689 (rep == MachineRepresentation::kWord16 &&
690 m.right().IsInRange(1, 16)))) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400691 Int32BinopMatcher mleft(m.left().node());
692 if (mleft.right().Is(m.right().Value())) {
Ben Murdochc5610432016-08-08 18:44:38 +0100693 node->ReplaceInput(value_input, mleft.left().node());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400694 return Changed(node);
695 }
696 }
697 break;
698 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000699 default:
700 break;
701 }
702 return NoChange();
703}
704
705
706Reduction MachineOperatorReducer::ReduceProjection(size_t index, Node* node) {
707 switch (node->opcode()) {
708 case IrOpcode::kInt32AddWithOverflow: {
709 DCHECK(index == 0 || index == 1);
710 Int32BinopMatcher m(node);
711 if (m.IsFoldable()) {
712 int32_t val;
713 bool ovf = base::bits::SignedAddOverflow32(m.left().Value(),
714 m.right().Value(), &val);
715 return ReplaceInt32((index == 0) ? val : ovf);
716 }
717 if (m.right().Is(0)) {
718 return (index == 0) ? Replace(m.left().node()) : ReplaceInt32(0);
719 }
720 break;
721 }
722 case IrOpcode::kInt32SubWithOverflow: {
723 DCHECK(index == 0 || index == 1);
724 Int32BinopMatcher m(node);
725 if (m.IsFoldable()) {
726 int32_t val;
727 bool ovf = base::bits::SignedSubOverflow32(m.left().Value(),
728 m.right().Value(), &val);
729 return ReplaceInt32((index == 0) ? val : ovf);
730 }
731 if (m.right().Is(0)) {
732 return (index == 0) ? Replace(m.left().node()) : ReplaceInt32(0);
733 }
734 break;
735 }
736 default:
737 break;
738 }
739 return NoChange();
740}
741
742
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400743Reduction MachineOperatorReducer::ReduceWord32Shifts(Node* node) {
744 DCHECK((node->opcode() == IrOpcode::kWord32Shl) ||
745 (node->opcode() == IrOpcode::kWord32Shr) ||
746 (node->opcode() == IrOpcode::kWord32Sar));
747 if (machine()->Word32ShiftIsSafe()) {
748 // Remove the explicit 'and' with 0x1f if the shift provided by the machine
749 // instruction matches that required by JavaScript.
750 Int32BinopMatcher m(node);
751 if (m.right().IsWord32And()) {
752 Int32BinopMatcher mright(m.right().node());
753 if (mright.right().Is(0x1f)) {
754 node->ReplaceInput(1, mright.left().node());
755 return Changed(node);
756 }
757 }
758 }
759 return NoChange();
760}
761
762
763Reduction MachineOperatorReducer::ReduceWord32Shl(Node* node) {
764 DCHECK_EQ(IrOpcode::kWord32Shl, node->opcode());
765 Int32BinopMatcher m(node);
766 if (m.right().Is(0)) return Replace(m.left().node()); // x << 0 => x
767 if (m.IsFoldable()) { // K << K => K
768 return ReplaceInt32(m.left().Value() << m.right().Value());
769 }
770 if (m.right().IsInRange(1, 31)) {
771 // (x >>> K) << K => x & ~(2^K - 1)
772 // (x >> K) << K => x & ~(2^K - 1)
773 if (m.left().IsWord32Sar() || m.left().IsWord32Shr()) {
774 Int32BinopMatcher mleft(m.left().node());
775 if (mleft.right().Is(m.right().Value())) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400776 node->ReplaceInput(0, mleft.left().node());
777 node->ReplaceInput(1,
778 Uint32Constant(~((1U << m.right().Value()) - 1U)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000779 NodeProperties::ChangeOp(node, machine()->Word32And());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400780 Reduction reduction = ReduceWord32And(node);
781 return reduction.Changed() ? reduction : Changed(node);
782 }
783 }
784 }
785 return ReduceWord32Shifts(node);
786}
787
788
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000789Reduction MachineOperatorReducer::ReduceWord32Sar(Node* node) {
790 Int32BinopMatcher m(node);
791 if (m.right().Is(0)) return Replace(m.left().node()); // x >> 0 => x
792 if (m.IsFoldable()) { // K >> K => K
793 return ReplaceInt32(m.left().Value() >> m.right().Value());
794 }
795 if (m.left().IsWord32Shl()) {
796 Int32BinopMatcher mleft(m.left().node());
797 if (mleft.left().IsComparison()) {
798 if (m.right().Is(31) && mleft.right().Is(31)) {
799 // Comparison << 31 >> 31 => 0 - Comparison
800 node->ReplaceInput(0, Int32Constant(0));
801 node->ReplaceInput(1, mleft.left().node());
802 NodeProperties::ChangeOp(node, machine()->Int32Sub());
803 Reduction const reduction = ReduceInt32Sub(node);
804 return reduction.Changed() ? reduction : Changed(node);
805 }
806 } else if (mleft.left().IsLoad()) {
807 LoadRepresentation const rep =
808 LoadRepresentationOf(mleft.left().node()->op());
809 if (m.right().Is(24) && mleft.right().Is(24) &&
810 rep == MachineType::Int8()) {
811 // Load[kMachInt8] << 24 >> 24 => Load[kMachInt8]
812 return Replace(mleft.left().node());
813 }
814 if (m.right().Is(16) && mleft.right().Is(16) &&
815 rep == MachineType::Int16()) {
816 // Load[kMachInt16] << 16 >> 16 => Load[kMachInt8]
817 return Replace(mleft.left().node());
818 }
819 }
820 }
821 return ReduceWord32Shifts(node);
822}
823
824
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400825Reduction MachineOperatorReducer::ReduceWord32And(Node* node) {
826 DCHECK_EQ(IrOpcode::kWord32And, node->opcode());
827 Int32BinopMatcher m(node);
828 if (m.right().Is(0)) return Replace(m.right().node()); // x & 0 => 0
829 if (m.right().Is(-1)) return Replace(m.left().node()); // x & -1 => x
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000830 if (m.left().IsComparison() && m.right().Is(1)) { // CMP & 1 => CMP
831 return Replace(m.left().node());
832 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400833 if (m.IsFoldable()) { // K & K => K
834 return ReplaceInt32(m.left().Value() & m.right().Value());
835 }
836 if (m.LeftEqualsRight()) return Replace(m.left().node()); // x & x => x
837 if (m.left().IsWord32And() && m.right().HasValue()) {
838 Int32BinopMatcher mleft(m.left().node());
839 if (mleft.right().HasValue()) { // (x & K) & K => x & K
840 node->ReplaceInput(0, mleft.left().node());
841 node->ReplaceInput(
842 1, Int32Constant(m.right().Value() & mleft.right().Value()));
843 Reduction const reduction = ReduceWord32And(node);
844 return reduction.Changed() ? reduction : Changed(node);
845 }
846 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000847 if (m.right().IsNegativePowerOf2()) {
848 int32_t const mask = m.right().Value();
849 if (m.left().IsWord32Shl()) {
850 Uint32BinopMatcher mleft(m.left().node());
851 if (mleft.right().HasValue() &&
852 mleft.right().Value() >= base::bits::CountTrailingZeros32(mask)) {
853 // (x << L) & (-1 << K) => x << L iff K >= L
854 return Replace(mleft.node());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400855 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000856 } else if (m.left().IsInt32Add()) {
857 Int32BinopMatcher mleft(m.left().node());
858 if (mleft.right().HasValue() &&
859 (mleft.right().Value() & mask) == mleft.right().Value()) {
860 // (x + (K << L)) & (-1 << L) => (x & (-1 << L)) + (K << L)
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400861 node->ReplaceInput(0, Word32And(mleft.left().node(), m.right().node()));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000862 node->ReplaceInput(1, mleft.right().node());
863 NodeProperties::ChangeOp(node, machine()->Int32Add());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400864 Reduction const reduction = ReduceInt32Add(node);
865 return reduction.Changed() ? reduction : Changed(node);
866 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000867 if (mleft.left().IsInt32Mul()) {
868 Int32BinopMatcher mleftleft(mleft.left().node());
869 if (mleftleft.right().IsMultipleOf(-mask)) {
870 // (y * (K << L) + x) & (-1 << L) => (x & (-1 << L)) + y * (K << L)
871 node->ReplaceInput(0,
872 Word32And(mleft.right().node(), m.right().node()));
873 node->ReplaceInput(1, mleftleft.node());
874 NodeProperties::ChangeOp(node, machine()->Int32Add());
875 Reduction const reduction = ReduceInt32Add(node);
876 return reduction.Changed() ? reduction : Changed(node);
877 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400878 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000879 if (mleft.right().IsInt32Mul()) {
880 Int32BinopMatcher mleftright(mleft.right().node());
881 if (mleftright.right().IsMultipleOf(-mask)) {
882 // (x + y * (K << L)) & (-1 << L) => (x & (-1 << L)) + y * (K << L)
883 node->ReplaceInput(0,
884 Word32And(mleft.left().node(), m.right().node()));
885 node->ReplaceInput(1, mleftright.node());
886 NodeProperties::ChangeOp(node, machine()->Int32Add());
887 Reduction const reduction = ReduceInt32Add(node);
888 return reduction.Changed() ? reduction : Changed(node);
889 }
890 }
891 if (mleft.left().IsWord32Shl()) {
892 Int32BinopMatcher mleftleft(mleft.left().node());
893 if (mleftleft.right().Is(base::bits::CountTrailingZeros32(mask))) {
894 // (y << L + x) & (-1 << L) => (x & (-1 << L)) + y << L
895 node->ReplaceInput(0,
896 Word32And(mleft.right().node(), m.right().node()));
897 node->ReplaceInput(1, mleftleft.node());
898 NodeProperties::ChangeOp(node, machine()->Int32Add());
899 Reduction const reduction = ReduceInt32Add(node);
900 return reduction.Changed() ? reduction : Changed(node);
901 }
902 }
903 if (mleft.right().IsWord32Shl()) {
904 Int32BinopMatcher mleftright(mleft.right().node());
905 if (mleftright.right().Is(base::bits::CountTrailingZeros32(mask))) {
906 // (x + y << L) & (-1 << L) => (x & (-1 << L)) + y << L
907 node->ReplaceInput(0,
908 Word32And(mleft.left().node(), m.right().node()));
909 node->ReplaceInput(1, mleftright.node());
910 NodeProperties::ChangeOp(node, machine()->Int32Add());
911 Reduction const reduction = ReduceInt32Add(node);
912 return reduction.Changed() ? reduction : Changed(node);
913 }
914 }
915 } else if (m.left().IsInt32Mul()) {
916 Int32BinopMatcher mleft(m.left().node());
917 if (mleft.right().IsMultipleOf(-mask)) {
918 // (x * (K << L)) & (-1 << L) => x * (K << L)
919 return Replace(mleft.node());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400920 }
921 }
922 }
923 return NoChange();
924}
925
926
927Reduction MachineOperatorReducer::ReduceWord32Or(Node* node) {
928 DCHECK_EQ(IrOpcode::kWord32Or, node->opcode());
929 Int32BinopMatcher m(node);
930 if (m.right().Is(0)) return Replace(m.left().node()); // x | 0 => x
931 if (m.right().Is(-1)) return Replace(m.right().node()); // x | -1 => -1
932 if (m.IsFoldable()) { // K | K => K
933 return ReplaceInt32(m.left().Value() | m.right().Value());
934 }
935 if (m.LeftEqualsRight()) return Replace(m.left().node()); // x | x => x
936
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000937 Node* shl = nullptr;
938 Node* shr = nullptr;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400939 // Recognize rotation, we are matching either:
940 // * x << y | x >>> (32 - y) => x ror (32 - y), i.e x rol y
941 // * x << (32 - y) | x >>> y => x ror y
942 // as well as their commuted form.
943 if (m.left().IsWord32Shl() && m.right().IsWord32Shr()) {
944 shl = m.left().node();
945 shr = m.right().node();
946 } else if (m.left().IsWord32Shr() && m.right().IsWord32Shl()) {
947 shl = m.right().node();
948 shr = m.left().node();
949 } else {
950 return NoChange();
951 }
952
953 Int32BinopMatcher mshl(shl);
954 Int32BinopMatcher mshr(shr);
955 if (mshl.left().node() != mshr.left().node()) return NoChange();
956
957 if (mshl.right().HasValue() && mshr.right().HasValue()) {
958 // Case where y is a constant.
959 if (mshl.right().Value() + mshr.right().Value() != 32) return NoChange();
960 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000961 Node* sub = nullptr;
962 Node* y = nullptr;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400963 if (mshl.right().IsInt32Sub()) {
964 sub = mshl.right().node();
965 y = mshr.right().node();
966 } else if (mshr.right().IsInt32Sub()) {
967 sub = mshr.right().node();
968 y = mshl.right().node();
969 } else {
970 return NoChange();
971 }
972
973 Int32BinopMatcher msub(sub);
974 if (!msub.left().Is(32) || msub.right().node() != y) return NoChange();
975 }
976
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400977 node->ReplaceInput(0, mshl.left().node());
978 node->ReplaceInput(1, mshr.right().node());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000979 NodeProperties::ChangeOp(node, machine()->Word32Ror());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400980 return Changed(node);
981}
982
983
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000984Reduction MachineOperatorReducer::ReduceFloat64InsertLowWord32(Node* node) {
985 DCHECK_EQ(IrOpcode::kFloat64InsertLowWord32, node->opcode());
986 Float64Matcher mlhs(node->InputAt(0));
987 Uint32Matcher mrhs(node->InputAt(1));
988 if (mlhs.HasValue() && mrhs.HasValue()) {
989 return ReplaceFloat64(bit_cast<double>(
990 (bit_cast<uint64_t>(mlhs.Value()) & V8_UINT64_C(0xFFFFFFFF00000000)) |
991 mrhs.Value()));
992 }
993 return NoChange();
994}
995
996
997Reduction MachineOperatorReducer::ReduceFloat64InsertHighWord32(Node* node) {
998 DCHECK_EQ(IrOpcode::kFloat64InsertHighWord32, node->opcode());
999 Float64Matcher mlhs(node->InputAt(0));
1000 Uint32Matcher mrhs(node->InputAt(1));
1001 if (mlhs.HasValue() && mrhs.HasValue()) {
1002 return ReplaceFloat64(bit_cast<double>(
1003 (bit_cast<uint64_t>(mlhs.Value()) & V8_UINT64_C(0xFFFFFFFF)) |
1004 (static_cast<uint64_t>(mrhs.Value()) << 32)));
1005 }
1006 return NoChange();
1007}
1008
1009
1010namespace {
1011
1012bool IsFloat64RepresentableAsFloat32(const Float64Matcher& m) {
1013 if (m.HasValue()) {
1014 double v = m.Value();
1015 float fv = static_cast<float>(v);
1016 return static_cast<double>(fv) == v;
1017 }
1018 return false;
1019}
1020
1021} // namespace
1022
1023
1024Reduction MachineOperatorReducer::ReduceFloat64Compare(Node* node) {
1025 DCHECK((IrOpcode::kFloat64Equal == node->opcode()) ||
1026 (IrOpcode::kFloat64LessThan == node->opcode()) ||
1027 (IrOpcode::kFloat64LessThanOrEqual == node->opcode()));
1028 // As all Float32 values have an exact representation in Float64, comparing
1029 // two Float64 values both converted from Float32 is equivalent to comparing
1030 // the original Float32s, so we can ignore the conversions. We can also reduce
1031 // comparisons of converted Float64 values against constants that can be
1032 // represented exactly as Float32.
1033 Float64BinopMatcher m(node);
1034 if ((m.left().IsChangeFloat32ToFloat64() &&
1035 m.right().IsChangeFloat32ToFloat64()) ||
1036 (m.left().IsChangeFloat32ToFloat64() &&
1037 IsFloat64RepresentableAsFloat32(m.right())) ||
1038 (IsFloat64RepresentableAsFloat32(m.left()) &&
1039 m.right().IsChangeFloat32ToFloat64())) {
1040 switch (node->opcode()) {
1041 case IrOpcode::kFloat64Equal:
1042 NodeProperties::ChangeOp(node, machine()->Float32Equal());
1043 break;
1044 case IrOpcode::kFloat64LessThan:
1045 NodeProperties::ChangeOp(node, machine()->Float32LessThan());
1046 break;
1047 case IrOpcode::kFloat64LessThanOrEqual:
1048 NodeProperties::ChangeOp(node, machine()->Float32LessThanOrEqual());
1049 break;
1050 default:
1051 return NoChange();
1052 }
1053 node->ReplaceInput(
1054 0, m.left().HasValue()
1055 ? Float32Constant(static_cast<float>(m.left().Value()))
1056 : m.left().InputAt(0));
1057 node->ReplaceInput(
1058 1, m.right().HasValue()
1059 ? Float32Constant(static_cast<float>(m.right().Value()))
1060 : m.right().InputAt(0));
1061 return Changed(node);
1062 }
1063 return NoChange();
1064}
1065
1066
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001067CommonOperatorBuilder* MachineOperatorReducer::common() const {
1068 return jsgraph()->common();
1069}
1070
1071
1072MachineOperatorBuilder* MachineOperatorReducer::machine() const {
1073 return jsgraph()->machine();
1074}
1075
1076
1077Graph* MachineOperatorReducer::graph() const { return jsgraph()->graph(); }
1078
1079} // namespace compiler
1080} // namespace internal
1081} // namespace v8