deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / src / compiler / machine-operator-reducer.cc
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 #include "src/compiler/machine-operator-reducer.h"
6
7 #include "src/base/bits.h"
8 #include "src/base/division-by-constant.h"
9 #include "src/codegen.h"
10 #include "src/compiler/diamond.h"
11 #include "src/compiler/graph.h"
12 #include "src/compiler/js-graph.h"
13 #include "src/compiler/node-matchers.h"
14
15 namespace v8 {
16 namespace internal {
17 namespace compiler {
18
19 MachineOperatorReducer::MachineOperatorReducer(JSGraph* jsgraph)
20     : jsgraph_(jsgraph) {}
21
22
23 MachineOperatorReducer::~MachineOperatorReducer() {}
24
25
26 Node* MachineOperatorReducer::Float32Constant(volatile float value) {
27   return graph()->NewNode(common()->Float32Constant(value));
28 }
29
30
31 Node* MachineOperatorReducer::Float64Constant(volatile double value) {
32   return jsgraph()->Float64Constant(value);
33 }
34
35
36 Node* MachineOperatorReducer::Int32Constant(int32_t value) {
37   return jsgraph()->Int32Constant(value);
38 }
39
40
41 Node* MachineOperatorReducer::Int64Constant(int64_t value) {
42   return graph()->NewNode(common()->Int64Constant(value));
43 }
44
45
46 Node* 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
53 Node* 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
59 Node* 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
65 Node* MachineOperatorReducer::Word32Equal(Node* lhs, Node* rhs) {
66   return graph()->NewNode(machine()->Word32Equal(), lhs, rhs);
67 }
68
69
70 Node* 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
77 Node* MachineOperatorReducer::Int32Sub(Node* lhs, Node* rhs) {
78   Node* const node = graph()->NewNode(machine()->Int32Sub(), lhs, rhs);
79   Reduction const reduction = ReduceInt32Sub(node);
80   return reduction.Changed() ? reduction.replacement() : node;
81 }
82
83
84 Node* MachineOperatorReducer::Int32Mul(Node* lhs, Node* rhs) {
85   return graph()->NewNode(machine()->Int32Mul(), lhs, rhs);
86 }
87
88
89 Node* 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
105 Node* MachineOperatorReducer::Uint32Div(Node* dividend, uint32_t divisor) {
106   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.
113   base::MagicNumbersForDivision<uint32_t> const mag =
114       base::UnsignedDivisionByConstant(divisor, shift);
115   Node* quotient = graph()->NewNode(machine()->Uint32MulHigh(), dividend,
116                                     Uint32Constant(mag.multiplier));
117   if (mag.add) {
118     DCHECK_LE(1u, mag.shift);
119     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
129 // Perform constant folding and strength reduction on machine operators.
130 Reduction MachineOperatorReducer::Reduce(Node* node) {
131   switch (node->opcode()) {
132     case IrOpcode::kProjection:
133       return ReduceProjection(ProjectionIndexOf(node->op()), node->InputAt(0));
134     case IrOpcode::kWord32And:
135       return ReduceWord32And(node);
136     case IrOpcode::kWord32Or:
137       return ReduceWord32Or(node);
138     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
145       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         }
150       }
151       break;
152     }
153     case IrOpcode::kWord32Shl:
154       return ReduceWord32Shl(node);
155     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       }
161       return ReduceWord32Shifts(node);
162     }
163     case IrOpcode::kWord32Sar:
164       return ReduceWord32Sar(node);
165     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     }
189     case IrOpcode::kWord64Equal: {
190       Int64BinopMatcher m(node);
191       if (m.IsFoldable()) {  // K == K => K
192         return ReplaceBool(m.left().Value() == m.right().Value());
193       }
194       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
202       break;
203     }
204     case IrOpcode::kInt32Add:
205       return ReduceInt32Add(node);
206     case IrOpcode::kInt32Sub:
207       return ReduceInt32Sub(node);
208     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
216         node->set_op(machine()->Int32Sub());
217         node->ReplaceInput(0, Int32Constant(0));
218         node->ReplaceInput(1, m.left().node());
219         return Changed(node);
220       }
221       if (m.right().IsPowerOf2()) {  // x * 2^n => x << n
222         node->set_op(machine()->Word32Shl());
223         node->ReplaceInput(1, Int32Constant(WhichPowerOf2(m.right().Value())));
224         Reduction reduction = ReduceWord32Shl(node);
225         return reduction.Changed() ? reduction : Changed(node);
226       }
227       break;
228     }
229     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);
237     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
285       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       }
300       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);
314       if (m.right().IsNaN()) {  // x + NaN => NaN
315         return Replace(m.right().node());
316       }
317       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);
324       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       }
333       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);
340       if (m.right().Is(-1)) {  // x * -1.0 => -0.0 - x
341         node->set_op(machine()->Float64Sub());
342         node->ReplaceInput(0, Float64Constant(-0.0));
343         node->ReplaceInput(1, m.left().node());
344         return Changed(node);
345       }
346       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);
371       if (m.right().Is(0)) {  // x % 0 => NaN
372         return ReplaceFloat64(std::numeric_limits<double>::quiet_NaN());
373       }
374       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     }
422     case IrOpcode::kTruncateFloat64ToInt32:
423       return ReduceTruncateFloat64ToInt32(node);
424     case IrOpcode::kTruncateInt64ToInt32: {
425       Int64Matcher m(node->InputAt(0));
426       if (m.HasValue()) return ReplaceInt32(static_cast<int32_t>(m.Value()));
427       if (m.IsChangeInt32ToInt64()) return Replace(m.node()->InputAt(0));
428       break;
429     }
430     case IrOpcode::kTruncateFloat64ToFloat32: {
431       Float64Matcher m(node->InputAt(0));
432       if (m.HasValue()) return ReplaceFloat32(DoubleToFloat32(m.Value()));
433       if (m.IsChangeFloat32ToFloat64()) return Replace(m.node()->InputAt(0));
434       break;
435     }
436     case IrOpcode::kFloat64InsertLowWord32:
437       return ReduceFloat64InsertLowWord32(node);
438     case IrOpcode::kFloat64InsertHighWord32:
439       return ReduceFloat64InsertHighWord32(node);
440     case IrOpcode::kStore:
441       return ReduceStore(node);
442     default:
443       break;
444   }
445   return NoChange();
446 }
447
448
449 Reduction MachineOperatorReducer::ReduceInt32Add(Node* node) {
450   DCHECK_EQ(IrOpcode::kInt32Add, node->opcode());
451   Int32BinopMatcher m(node);
452   if (m.right().Is(0)) return Replace(m.left().node());  // x + 0 => x
453   if (m.IsFoldable()) {                                  // K + K => K
454     return ReplaceUint32(bit_cast<uint32_t>(m.left().Value()) +
455                          bit_cast<uint32_t>(m.right().Value()));
456   }
457   if (m.left().IsInt32Sub()) {
458     Int32BinopMatcher mleft(m.left().node());
459     if (mleft.left().Is(0)) {  // (0 - x) + y => y - x
460       node->set_op(machine()->Int32Sub());
461       node->ReplaceInput(0, m.right().node());
462       node->ReplaceInput(1, mleft.right().node());
463       Reduction const reduction = ReduceInt32Sub(node);
464       return reduction.Changed() ? reduction : Changed(node);
465     }
466   }
467   if (m.right().IsInt32Sub()) {
468     Int32BinopMatcher mright(m.right().node());
469     if (mright.left().Is(0)) {  // y + (0 - x) => y - x
470       node->set_op(machine()->Int32Sub());
471       node->ReplaceInput(1, mright.right().node());
472       Reduction const reduction = ReduceInt32Sub(node);
473       return reduction.Changed() ? reduction : Changed(node);
474     }
475   }
476   return NoChange();
477 }
478
479
480 Reduction MachineOperatorReducer::ReduceInt32Sub(Node* node) {
481   DCHECK_EQ(IrOpcode::kInt32Sub, node->opcode());
482   Int32BinopMatcher m(node);
483   if (m.right().Is(0)) return Replace(m.left().node());  // x - 0 => x
484   if (m.IsFoldable()) {                                  // K - K => K
485     return ReplaceInt32(static_cast<uint32_t>(m.left().Value()) -
486                         static_cast<uint32_t>(m.right().Value()));
487   }
488   if (m.LeftEqualsRight()) return ReplaceInt32(0);  // x - x => 0
489   if (m.right().HasValue()) {                       // x - K => x + -K
490     node->set_op(machine()->Int32Add());
491     node->ReplaceInput(1, Int32Constant(-m.right().Value()));
492     Reduction const reduction = ReduceInt32Add(node);
493     return reduction.Changed() ? reduction : Changed(node);
494   }
495   return NoChange();
496 }
497
498
499 Reduction MachineOperatorReducer::ReduceInt32Div(Node* node) {
500   Int32BinopMatcher m(node);
501   if (m.left().Is(0)) return Replace(m.left().node());    // 0 / x => 0
502   if (m.right().Is(0)) return Replace(m.right().node());  // x / 0 => 0
503   if (m.right().Is(1)) return Replace(m.left().node());   // x / 1 => x
504   if (m.IsFoldable()) {                                   // K / K => K
505     return ReplaceInt32(
506         base::bits::SignedDiv32(m.left().Value(), m.right().Value()));
507   }
508   if (m.LeftEqualsRight()) {  // x / x => x != 0
509     Node* const zero = Int32Constant(0);
510     return Replace(Word32Equal(Word32Equal(m.left().node(), zero), zero));
511   }
512   if (m.right().Is(-1)) {  // x / -1 => 0 - x
513     node->set_op(machine()->Int32Sub());
514     node->ReplaceInput(0, Int32Constant(0));
515     node->ReplaceInput(1, m.left().node());
516     node->TrimInputCount(2);
517     return Changed(node);
518   }
519   if (m.right().HasValue()) {
520     int32_t const divisor = m.right().Value();
521     Node* const dividend = m.left().node();
522     Node* quotient = dividend;
523     if (base::bits::IsPowerOfTwo32(Abs(divisor))) {
524       uint32_t const shift = WhichPowerOf2Abs(divisor);
525       DCHECK_NE(0u, shift);
526       if (shift > 1) {
527         quotient = Word32Sar(quotient, 31);
528       }
529       quotient = Int32Add(Word32Shr(quotient, 32u - shift), dividend);
530       quotient = Word32Sar(quotient, shift);
531     } else {
532       quotient = Int32Div(quotient, Abs(divisor));
533     }
534     if (divisor < 0) {
535       node->set_op(machine()->Int32Sub());
536       node->ReplaceInput(0, Int32Constant(0));
537       node->ReplaceInput(1, quotient);
538       node->TrimInputCount(2);
539       return Changed(node);
540     }
541     return Replace(quotient);
542   }
543   return NoChange();
544 }
545
546
547 Reduction MachineOperatorReducer::ReduceUint32Div(Node* node) {
548   Uint32BinopMatcher m(node);
549   if (m.left().Is(0)) return Replace(m.left().node());    // 0 / x => 0
550   if (m.right().Is(0)) return Replace(m.right().node());  // x / 0 => 0
551   if (m.right().Is(1)) return Replace(m.left().node());   // x / 1 => x
552   if (m.IsFoldable()) {                                   // K / K => K
553     return ReplaceUint32(
554         base::bits::UnsignedDiv32(m.left().Value(), m.right().Value()));
555   }
556   if (m.LeftEqualsRight()) {  // x / x => x != 0
557     Node* const zero = Int32Constant(0);
558     return Replace(Word32Equal(Word32Equal(m.left().node(), zero), zero));
559   }
560   if (m.right().HasValue()) {
561     Node* const dividend = m.left().node();
562     uint32_t const divisor = m.right().Value();
563     if (base::bits::IsPowerOfTwo32(divisor)) {  // x / 2^n => x >> n
564       node->set_op(machine()->Word32Shr());
565       node->ReplaceInput(1, Uint32Constant(WhichPowerOf2(m.right().Value())));
566       node->TrimInputCount(2);
567       return Changed(node);
568     } else {
569       return Replace(Uint32Div(dividend, divisor));
570     }
571   }
572   return NoChange();
573 }
574
575
576 Reduction MachineOperatorReducer::ReduceInt32Mod(Node* node) {
577   Int32BinopMatcher m(node);
578   if (m.left().Is(0)) return Replace(m.left().node());    // 0 % x  => 0
579   if (m.right().Is(0)) return Replace(m.right().node());  // x % 0  => 0
580   if (m.right().Is(1)) return ReplaceInt32(0);            // x % 1  => 0
581   if (m.right().Is(-1)) return ReplaceInt32(0);           // x % -1 => 0
582   if (m.LeftEqualsRight()) return ReplaceInt32(0);        // x % x  => 0
583   if (m.IsFoldable()) {                                   // K % K => K
584     return ReplaceInt32(
585         base::bits::SignedMod32(m.left().Value(), m.right().Value()));
586   }
587   if (m.right().HasValue()) {
588     Node* const dividend = m.left().node();
589     int32_t const divisor = Abs(m.right().Value());
590     if (base::bits::IsPowerOfTwo32(divisor)) {
591       uint32_t const mask = divisor - 1;
592       Node* const zero = Int32Constant(0);
593       node->set_op(common()->Select(kMachInt32, BranchHint::kFalse));
594       node->ReplaceInput(
595           0, graph()->NewNode(machine()->Int32LessThan(), dividend, zero));
596       node->ReplaceInput(
597           1, Int32Sub(zero, Word32And(Int32Sub(zero, dividend), mask)));
598       node->ReplaceInput(2, Word32And(dividend, mask));
599     } else {
600       Node* quotient = Int32Div(dividend, divisor);
601       node->set_op(machine()->Int32Sub());
602       DCHECK_EQ(dividend, node->InputAt(0));
603       node->ReplaceInput(1, Int32Mul(quotient, Int32Constant(divisor)));
604       node->TrimInputCount(2);
605     }
606     return Changed(node);
607   }
608   return NoChange();
609 }
610
611
612 Reduction MachineOperatorReducer::ReduceUint32Mod(Node* node) {
613   Uint32BinopMatcher m(node);
614   if (m.left().Is(0)) return Replace(m.left().node());    // 0 % x => 0
615   if (m.right().Is(0)) return Replace(m.right().node());  // x % 0 => 0
616   if (m.right().Is(1)) return ReplaceUint32(0);           // x % 1 => 0
617   if (m.LeftEqualsRight()) return ReplaceInt32(0);        // x % x  => 0
618   if (m.IsFoldable()) {                                   // K % K => K
619     return ReplaceUint32(
620         base::bits::UnsignedMod32(m.left().Value(), m.right().Value()));
621   }
622   if (m.right().HasValue()) {
623     Node* const dividend = m.left().node();
624     uint32_t const divisor = m.right().Value();
625     if (base::bits::IsPowerOfTwo32(divisor)) {  // x % 2^n => x & 2^n-1
626       node->set_op(machine()->Word32And());
627       node->ReplaceInput(1, Uint32Constant(m.right().Value() - 1));
628     } else {
629       Node* quotient = Uint32Div(dividend, divisor);
630       node->set_op(machine()->Int32Sub());
631       DCHECK_EQ(dividend, node->InputAt(0));
632       node->ReplaceInput(1, Int32Mul(quotient, Uint32Constant(divisor)));
633     }
634     node->TrimInputCount(2);
635     return Changed(node);
636   }
637   return NoChange();
638 }
639
640
641 Reduction MachineOperatorReducer::ReduceTruncateFloat64ToInt32(Node* node) {
642   Float64Matcher m(node->InputAt(0));
643   if (m.HasValue()) return ReplaceInt32(DoubleToInt32(m.Value()));
644   if (m.IsChangeInt32ToFloat64()) return Replace(m.node()->InputAt(0));
645   if (m.IsPhi()) {
646     Node* const phi = m.node();
647     DCHECK_EQ(kRepFloat64, RepresentationOf(OpParameter<MachineType>(phi)));
648     if (phi->OwnedBy(node)) {
649       // TruncateFloat64ToInt32(Phi[Float64](x1,...,xn))
650       //   => Phi[Int32](TruncateFloat64ToInt32(x1),
651       //                 ...,
652       //                 TruncateFloat64ToInt32(xn))
653       const int value_input_count = phi->InputCount() - 1;
654       for (int i = 0; i < value_input_count; ++i) {
655         Node* input = graph()->NewNode(machine()->TruncateFloat64ToInt32(),
656                                        phi->InputAt(i));
657         // TODO(bmeurer): Reschedule input for reduction once we have Revisit()
658         // instead of recursing into ReduceTruncateFloat64ToInt32() here.
659         Reduction reduction = ReduceTruncateFloat64ToInt32(input);
660         if (reduction.Changed()) input = reduction.replacement();
661         phi->ReplaceInput(i, input);
662       }
663       phi->set_op(common()->Phi(kMachInt32, value_input_count));
664       return Replace(phi);
665     }
666   }
667   return NoChange();
668 }
669
670
671 Reduction MachineOperatorReducer::ReduceStore(Node* node) {
672   MachineType const rep =
673       RepresentationOf(StoreRepresentationOf(node->op()).machine_type());
674   Node* const value = node->InputAt(2);
675   switch (value->opcode()) {
676     case IrOpcode::kWord32And: {
677       Uint32BinopMatcher m(value);
678       if (m.right().HasValue() &&
679           ((rep == kRepWord8 && (m.right().Value() & 0xff) == 0xff) ||
680            (rep == kRepWord16 && (m.right().Value() & 0xffff) == 0xffff))) {
681         node->ReplaceInput(2, m.left().node());
682         return Changed(node);
683       }
684       break;
685     }
686     case IrOpcode::kWord32Sar: {
687       Int32BinopMatcher m(value);
688       if (m.left().IsWord32Shl() &&
689           ((rep == kRepWord8 && m.right().IsInRange(1, 24)) ||
690            (rep == kRepWord16 && m.right().IsInRange(1, 16)))) {
691         Int32BinopMatcher mleft(m.left().node());
692         if (mleft.right().Is(m.right().Value())) {
693           node->ReplaceInput(2, mleft.left().node());
694           return Changed(node);
695         }
696       }
697       break;
698     }
699     default:
700       break;
701   }
702   return NoChange();
703 }
704
705
706 Reduction 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
743 Reduction 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
763 Reduction 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())) {
776         node->set_op(machine()->Word32And());
777         node->ReplaceInput(0, mleft.left().node());
778         node->ReplaceInput(1,
779                            Uint32Constant(~((1U << m.right().Value()) - 1U)));
780         Reduction reduction = ReduceWord32And(node);
781         return reduction.Changed() ? reduction : Changed(node);
782       }
783     }
784   }
785   return ReduceWord32Shifts(node);
786 }
787
788
789 Reduction 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->set_op(machine()->Int32Sub());
801         node->ReplaceInput(0, Int32Constant(0));
802         node->ReplaceInput(1, mleft.left().node());
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           OpParameter<LoadRepresentation>(mleft.left().node());
809       if (m.right().Is(24) && mleft.right().Is(24) && rep == kMachInt8) {
810         // Load[kMachInt8] << 24 >> 24 => Load[kMachInt8]
811         return Replace(mleft.left().node());
812       }
813       if (m.right().Is(16) && mleft.right().Is(16) && rep == kMachInt16) {
814         // Load[kMachInt16] << 16 >> 16 => Load[kMachInt8]
815         return Replace(mleft.left().node());
816       }
817     }
818   }
819   return ReduceWord32Shifts(node);
820 }
821
822
823 Reduction MachineOperatorReducer::ReduceWord32And(Node* node) {
824   DCHECK_EQ(IrOpcode::kWord32And, node->opcode());
825   Int32BinopMatcher m(node);
826   if (m.right().Is(0)) return Replace(m.right().node());  // x & 0  => 0
827   if (m.right().Is(-1)) return Replace(m.left().node());  // x & -1 => x
828   if (m.left().IsComparison() && m.right().Is(1)) {       // CMP & 1 => CMP
829     return Replace(m.left().node());
830   }
831   if (m.IsFoldable()) {                                   // K & K  => K
832     return ReplaceInt32(m.left().Value() & m.right().Value());
833   }
834   if (m.LeftEqualsRight()) return Replace(m.left().node());  // x & x => x
835   if (m.left().IsWord32And() && m.right().HasValue()) {
836     Int32BinopMatcher mleft(m.left().node());
837     if (mleft.right().HasValue()) {  // (x & K) & K => x & K
838       node->ReplaceInput(0, mleft.left().node());
839       node->ReplaceInput(
840           1, Int32Constant(m.right().Value() & mleft.right().Value()));
841       Reduction const reduction = ReduceWord32And(node);
842       return reduction.Changed() ? reduction : Changed(node);
843     }
844   }
845   if (m.right().IsNegativePowerOf2()) {
846     int32_t const mask = m.right().Value();
847     if (m.left().IsWord32Shl()) {
848       Uint32BinopMatcher mleft(m.left().node());
849       if (mleft.right().HasValue() &&
850           mleft.right().Value() >= base::bits::CountTrailingZeros32(mask)) {
851         // (x << L) & (-1 << K) => x << L iff K >= L
852         return Replace(mleft.node());
853       }
854     } else if (m.left().IsInt32Add()) {
855       Int32BinopMatcher mleft(m.left().node());
856       if (mleft.right().HasValue() &&
857           (mleft.right().Value() & mask) == mleft.right().Value()) {
858         // (x + (K << L)) & (-1 << L) => (x & (-1 << L)) + (K << L)
859         node->set_op(machine()->Int32Add());
860         node->ReplaceInput(0, Word32And(mleft.left().node(), m.right().node()));
861         node->ReplaceInput(1, mleft.right().node());
862         Reduction const reduction = ReduceInt32Add(node);
863         return reduction.Changed() ? reduction : Changed(node);
864       }
865       if (mleft.left().IsInt32Mul()) {
866         Int32BinopMatcher mleftleft(mleft.left().node());
867         if (mleftleft.right().IsMultipleOf(-mask)) {
868           // (y * (K << L) + x) & (-1 << L) => (x & (-1 << L)) + y * (K << L)
869           node->set_op(machine()->Int32Add());
870           node->ReplaceInput(0,
871                              Word32And(mleft.right().node(), m.right().node()));
872           node->ReplaceInput(1, mleftleft.node());
873           Reduction const reduction = ReduceInt32Add(node);
874           return reduction.Changed() ? reduction : Changed(node);
875         }
876       }
877       if (mleft.right().IsInt32Mul()) {
878         Int32BinopMatcher mleftright(mleft.right().node());
879         if (mleftright.right().IsMultipleOf(-mask)) {
880           // (x + y * (K << L)) & (-1 << L) => (x & (-1 << L)) + y * (K << L)
881           node->set_op(machine()->Int32Add());
882           node->ReplaceInput(0,
883                              Word32And(mleft.left().node(), m.right().node()));
884           node->ReplaceInput(1, mleftright.node());
885           Reduction const reduction = ReduceInt32Add(node);
886           return reduction.Changed() ? reduction : Changed(node);
887         }
888       }
889       if (mleft.left().IsWord32Shl()) {
890         Int32BinopMatcher mleftleft(mleft.left().node());
891         if (mleftleft.right().Is(base::bits::CountTrailingZeros32(mask))) {
892           // (y << L + x) & (-1 << L) => (x & (-1 << L)) + y << L
893           node->set_op(machine()->Int32Add());
894           node->ReplaceInput(0,
895                              Word32And(mleft.right().node(), m.right().node()));
896           node->ReplaceInput(1, mleftleft.node());
897           Reduction const reduction = ReduceInt32Add(node);
898           return reduction.Changed() ? reduction : Changed(node);
899         }
900       }
901       if (mleft.right().IsWord32Shl()) {
902         Int32BinopMatcher mleftright(mleft.right().node());
903         if (mleftright.right().Is(base::bits::CountTrailingZeros32(mask))) {
904           // (x + y << L) & (-1 << L) => (x & (-1 << L)) + y << L
905           node->set_op(machine()->Int32Add());
906           node->ReplaceInput(0,
907                              Word32And(mleft.left().node(), m.right().node()));
908           node->ReplaceInput(1, mleftright.node());
909           Reduction const reduction = ReduceInt32Add(node);
910           return reduction.Changed() ? reduction : Changed(node);
911         }
912       }
913     } else if (m.left().IsInt32Mul()) {
914       Int32BinopMatcher mleft(m.left().node());
915       if (mleft.right().IsMultipleOf(-mask)) {
916         // (x * (K << L)) & (-1 << L) => x * (K << L)
917         return Replace(mleft.node());
918       }
919     }
920   }
921   return NoChange();
922 }
923
924
925 Reduction MachineOperatorReducer::ReduceWord32Or(Node* node) {
926   DCHECK_EQ(IrOpcode::kWord32Or, node->opcode());
927   Int32BinopMatcher m(node);
928   if (m.right().Is(0)) return Replace(m.left().node());    // x | 0  => x
929   if (m.right().Is(-1)) return Replace(m.right().node());  // x | -1 => -1
930   if (m.IsFoldable()) {                                    // K | K  => K
931     return ReplaceInt32(m.left().Value() | m.right().Value());
932   }
933   if (m.LeftEqualsRight()) return Replace(m.left().node());  // x | x => x
934
935   Node* shl = NULL;
936   Node* shr = NULL;
937   // Recognize rotation, we are matching either:
938   //  * x << y | x >>> (32 - y) => x ror (32 - y), i.e  x rol y
939   //  * x << (32 - y) | x >>> y => x ror y
940   // as well as their commuted form.
941   if (m.left().IsWord32Shl() && m.right().IsWord32Shr()) {
942     shl = m.left().node();
943     shr = m.right().node();
944   } else if (m.left().IsWord32Shr() && m.right().IsWord32Shl()) {
945     shl = m.right().node();
946     shr = m.left().node();
947   } else {
948     return NoChange();
949   }
950
951   Int32BinopMatcher mshl(shl);
952   Int32BinopMatcher mshr(shr);
953   if (mshl.left().node() != mshr.left().node()) return NoChange();
954
955   if (mshl.right().HasValue() && mshr.right().HasValue()) {
956     // Case where y is a constant.
957     if (mshl.right().Value() + mshr.right().Value() != 32) return NoChange();
958   } else {
959     Node* sub = NULL;
960     Node* y = NULL;
961     if (mshl.right().IsInt32Sub()) {
962       sub = mshl.right().node();
963       y = mshr.right().node();
964     } else if (mshr.right().IsInt32Sub()) {
965       sub = mshr.right().node();
966       y = mshl.right().node();
967     } else {
968       return NoChange();
969     }
970
971     Int32BinopMatcher msub(sub);
972     if (!msub.left().Is(32) || msub.right().node() != y) return NoChange();
973   }
974
975   node->set_op(machine()->Word32Ror());
976   node->ReplaceInput(0, mshl.left().node());
977   node->ReplaceInput(1, mshr.right().node());
978   return Changed(node);
979 }
980
981
982 Reduction MachineOperatorReducer::ReduceFloat64InsertLowWord32(Node* node) {
983   DCHECK_EQ(IrOpcode::kFloat64InsertLowWord32, node->opcode());
984   Float64Matcher mlhs(node->InputAt(0));
985   Uint32Matcher mrhs(node->InputAt(1));
986   if (mlhs.HasValue() && mrhs.HasValue()) {
987     return ReplaceFloat64(bit_cast<double>(
988         (bit_cast<uint64_t>(mlhs.Value()) & V8_UINT64_C(0xFFFFFFFF00000000)) |
989         mrhs.Value()));
990   }
991   return NoChange();
992 }
993
994
995 Reduction MachineOperatorReducer::ReduceFloat64InsertHighWord32(Node* node) {
996   DCHECK_EQ(IrOpcode::kFloat64InsertHighWord32, node->opcode());
997   Float64Matcher mlhs(node->InputAt(0));
998   Uint32Matcher mrhs(node->InputAt(1));
999   if (mlhs.HasValue() && mrhs.HasValue()) {
1000     return ReplaceFloat64(bit_cast<double>(
1001         (bit_cast<uint64_t>(mlhs.Value()) & V8_UINT64_C(0xFFFFFFFF)) |
1002         (static_cast<uint64_t>(mrhs.Value()) << 32)));
1003   }
1004   return NoChange();
1005 }
1006
1007
1008 CommonOperatorBuilder* MachineOperatorReducer::common() const {
1009   return jsgraph()->common();
1010 }
1011
1012
1013 MachineOperatorBuilder* MachineOperatorReducer::machine() const {
1014   return jsgraph()->machine();
1015 }
1016
1017
1018 Graph* MachineOperatorReducer::graph() const { return jsgraph()->graph(); }
1019
1020 }  // namespace compiler
1021 }  // namespace internal
1022 }  // namespace v8