machine-operator-reducer.cc 62.6 KB
Newer Older
1 2 3 4 5
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/compiler/machine-operator-reducer.h"
6
#include <cmath>
7

8
#include "src/base/bits.h"
9
#include "src/base/division-by-constant.h"
10
#include "src/base/ieee754.h"
11
#include "src/base/overflowing-math.h"
12
#include "src/compiler/diamond.h"
13
#include "src/compiler/graph.h"
14
#include "src/compiler/machine-graph.h"
15
#include "src/compiler/node-matchers.h"
16
#include "src/compiler/node-properties.h"
17
#include "src/numbers/conversions-inl.h"
18 19 20 21 22

namespace v8 {
namespace internal {
namespace compiler {

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
// Some optimizations performed by the MachineOperatorReducer can be applied
// to both Word32 and Word64 operations. Those are implemented in a generic
// way to be reused for different word sizes.
// This class adapts a generic algorithm to Word32 operations.
class Word32Adapter {
 public:
  using IntNBinopMatcher = Int32BinopMatcher;
  using UintNBinopMatcher = Uint32BinopMatcher;
  using intN_t = int32_t;
  // WORD_SIZE refers to the N for which this adapter specializes.
  static constexpr std::size_t WORD_SIZE = 32;

  explicit Word32Adapter(MachineOperatorReducer* reducer) : r_(reducer) {}

  template <typename T>
  static bool IsWordNAnd(const T& x) {
    return x.IsWord32And();
  }
  template <typename T>
  static bool IsWordNShl(const T& x) {
    return x.IsWord32Shl();
  }
  template <typename T>
  static bool IsWordNXor(const T& x) {
    return x.IsWord32Xor();
  }
  template <typename T>
  static bool IsIntNAdd(const T& x) {
    return x.IsInt32Add();
  }
  template <typename T>
  static bool IsIntNMul(const T& x) {
    return x.IsInt32Mul();
  }

  const Operator* IntNAdd(MachineOperatorBuilder* machine) {
    return machine->Int32Add();
  }

  Reduction ReplaceIntN(int32_t value) { return r_->ReplaceInt32(value); }
  Reduction ReduceWordNAnd(Node* node) { return r_->ReduceWord32And(node); }
  Reduction ReduceIntNAdd(Node* node) { return r_->ReduceInt32Add(node); }
  Reduction TryMatchWordNRor(Node* node) { return r_->TryMatchWord32Ror(node); }

  Node* IntNConstant(int32_t value) { return r_->Int32Constant(value); }
  Node* WordNAnd(Node* lhs, Node* rhs) { return r_->Word32And(lhs, rhs); }

 private:
  MachineOperatorReducer* r_;
};

// Some optimizations performed by the MachineOperatorReducer can be applied
// to both Word32 and Word64 operations. Those are implemented in a generic
// way to be reused for different word sizes.
// This class adapts a generic algorithm to Word64 operations.
class Word64Adapter {
 public:
  using IntNBinopMatcher = Int64BinopMatcher;
  using UintNBinopMatcher = Uint64BinopMatcher;
  using intN_t = int64_t;
  // WORD_SIZE refers to the N for which this adapter specializes.
  static constexpr std::size_t WORD_SIZE = 64;

  explicit Word64Adapter(MachineOperatorReducer* reducer) : r_(reducer) {}

  template <typename T>
  static bool IsWordNAnd(const T& x) {
    return x.IsWord64And();
  }
  template <typename T>
  static bool IsWordNShl(const T& x) {
    return x.IsWord64Shl();
  }
  template <typename T>
  static bool IsWordNXor(const T& x) {
    return x.IsWord64Xor();
  }
  template <typename T>
  static bool IsIntNAdd(const T& x) {
    return x.IsInt64Add();
  }
  template <typename T>
  static bool IsIntNMul(const T& x) {
    return x.IsInt64Mul();
  }

  static const Operator* IntNAdd(MachineOperatorBuilder* machine) {
    return machine->Int64Add();
  }

  Reduction ReplaceIntN(int64_t value) { return r_->ReplaceInt64(value); }
  Reduction ReduceWordNAnd(Node* node) { return r_->ReduceWord64And(node); }
  Reduction ReduceIntNAdd(Node* node) { return r_->ReduceInt64Add(node); }
  Reduction TryMatchWordNRor(Node* node) {
    // TODO(nicohartmann@): Add a MachineOperatorReducer::TryMatchWord64Ror.
    return r_->NoChange();
  }

  Node* IntNConstant(int64_t value) { return r_->Int64Constant(value); }
  Node* WordNAnd(Node* lhs, Node* rhs) { return r_->Word64And(lhs, rhs); }

 private:
  MachineOperatorReducer* r_;
};

128 129
MachineOperatorReducer::MachineOperatorReducer(Editor* editor,
                                               MachineGraph* mcgraph,
130
                                               bool allow_signalling_nan)
131 132 133
    : AdvancedReducer(editor),
      mcgraph_(mcgraph),
      allow_signalling_nan_(allow_signalling_nan) {}
134

135
MachineOperatorReducer::~MachineOperatorReducer() = default;
136 137


138 139 140 141
Node* MachineOperatorReducer::Float32Constant(volatile float value) {
  return graph()->NewNode(common()->Float32Constant(value));
}

142
Node* MachineOperatorReducer::Float64Constant(volatile double value) {
143
  return mcgraph()->Float64Constant(value);
144 145
}

146
Node* MachineOperatorReducer::Int32Constant(int32_t value) {
147
  return mcgraph()->Int32Constant(value);
148 149
}

150
Node* MachineOperatorReducer::Int64Constant(int64_t value) {
151
  return graph()->NewNode(common()->Int64Constant(value));
152 153
}

154 155 156 157 158 159 160
Node* MachineOperatorReducer::Float64Mul(Node* lhs, Node* rhs) {
  return graph()->NewNode(machine()->Float64Mul(), lhs, rhs);
}

Node* MachineOperatorReducer::Float64PowHalf(Node* value) {
  value =
      graph()->NewNode(machine()->Float64Add(), Float64Constant(0.0), value);
161 162 163 164 165 166
  Diamond d(graph(), common(),
            graph()->NewNode(machine()->Float64LessThanOrEqual(), value,
                             Float64Constant(-V8_INFINITY)),
            BranchHint::kFalse);
  return d.Phi(MachineRepresentation::kFloat64, Float64Constant(V8_INFINITY),
               graph()->NewNode(machine()->Float64Sqrt(), value));
167
}
168

169 170 171 172
Node* MachineOperatorReducer::Word32And(Node* lhs, Node* rhs) {
  Node* const node = graph()->NewNode(machine()->Word32And(), lhs, rhs);
  Reduction const reduction = ReduceWord32And(node);
  return reduction.Changed() ? reduction.replacement() : node;
173 174 175
}

Node* MachineOperatorReducer::Word32Sar(Node* lhs, uint32_t rhs) {
176
  if (rhs == 0) return lhs;
177 178 179 180
  return graph()->NewNode(machine()->Word32Sar(), lhs, Uint32Constant(rhs));
}

Node* MachineOperatorReducer::Word32Shr(Node* lhs, uint32_t rhs) {
181
  if (rhs == 0) return lhs;
182 183 184
  return graph()->NewNode(machine()->Word32Shr(), lhs, Uint32Constant(rhs));
}

185 186 187 188
Node* MachineOperatorReducer::Word32Equal(Node* lhs, Node* rhs) {
  return graph()->NewNode(machine()->Word32Equal(), lhs, rhs);
}

189 190 191 192 193 194
Node* MachineOperatorReducer::Word64And(Node* lhs, Node* rhs) {
  Node* const node = graph()->NewNode(machine()->Word64And(), lhs, rhs);
  Reduction const reduction = ReduceWord64And(node);
  return reduction.Changed() ? reduction.replacement() : node;
}

195
Node* MachineOperatorReducer::Int32Add(Node* lhs, Node* rhs) {
196 197 198
  Node* const node = graph()->NewNode(machine()->Int32Add(), lhs, rhs);
  Reduction const reduction = ReduceInt32Add(node);
  return reduction.Changed() ? reduction.replacement() : node;
199 200 201
}

Node* MachineOperatorReducer::Int32Sub(Node* lhs, Node* rhs) {
202 203 204
  Node* const node = graph()->NewNode(machine()->Int32Sub(), lhs, rhs);
  Reduction const reduction = ReduceInt32Sub(node);
  return reduction.Changed() ? reduction.replacement() : node;
205 206 207 208 209 210
}

Node* MachineOperatorReducer::Int32Mul(Node* lhs, Node* rhs) {
  return graph()->NewNode(machine()->Int32Mul(), lhs, rhs);
}

211 212
Node* MachineOperatorReducer::Int32Div(Node* dividend, int32_t divisor) {
  DCHECK_NE(0, divisor);
213 214 215 216 217 218 219 220 221 222
  DCHECK_NE(std::numeric_limits<int32_t>::min(), divisor);
  base::MagicNumbersForDivision<uint32_t> const mag =
      base::SignedDivisionByConstant(bit_cast<uint32_t>(divisor));
  Node* quotient = graph()->NewNode(machine()->Int32MulHigh(), dividend,
                                    Uint32Constant(mag.multiplier));
  if (divisor > 0 && bit_cast<int32_t>(mag.multiplier) < 0) {
    quotient = Int32Add(quotient, dividend);
  } else if (divisor < 0 && bit_cast<int32_t>(mag.multiplier) > 0) {
    quotient = Int32Sub(quotient, dividend);
  }
223 224 225 226
  return Int32Add(Word32Sar(quotient, mag.shift), Word32Shr(dividend, 31));
}

Node* MachineOperatorReducer::Uint32Div(Node* dividend, uint32_t divisor) {
227
  DCHECK_LT(0u, divisor);
228 229
  // If the divisor is even, we can avoid using the expensive fixup by shifting
  // the dividend upfront.
230
  unsigned const shift = base::bits::CountTrailingZeros(divisor);
231 232 233
  dividend = Word32Shr(dividend, shift);
  divisor >>= shift;
  // Compute the magic number for the (shifted) divisor.
234
  base::MagicNumbersForDivision<uint32_t> const mag =
235
      base::UnsignedDivisionByConstant(divisor, shift);
236 237 238
  Node* quotient = graph()->NewNode(machine()->Uint32MulHigh(), dividend,
                                    Uint32Constant(mag.multiplier));
  if (mag.add) {
239
    DCHECK_LE(1u, mag.shift);
240 241 242 243 244
    quotient = Word32Shr(
        Int32Add(Word32Shr(Int32Sub(dividend, quotient), 1), quotient),
        mag.shift - 1);
  } else {
    quotient = Word32Shr(quotient, mag.shift);
245
  }
246
  return quotient;
247 248
}

249 250 251
// Perform constant folding and strength reduction on machine operators.
Reduction MachineOperatorReducer::Reduce(Node* node) {
  switch (node->opcode()) {
252
    case IrOpcode::kProjection:
253
      return ReduceProjection(ProjectionIndexOf(node->op()), node->InputAt(0));
254 255
    case IrOpcode::kWord32And:
      return ReduceWord32And(node);
256 257
    case IrOpcode::kWord64And:
      return ReduceWord64And(node);
258 259
    case IrOpcode::kWord32Or:
      return ReduceWord32Or(node);
260 261
    case IrOpcode::kWord64Or:
      return ReduceWord64Or(node);
262 263
    case IrOpcode::kWord32Xor:
      return ReduceWord32Xor(node);
264 265
    case IrOpcode::kWord64Xor:
      return ReduceWord64Xor(node);
266 267
    case IrOpcode::kWord32Shl:
      return ReduceWord32Shl(node);
268 269
    case IrOpcode::kWord64Shl:
      return ReduceWord64Shl(node);
270 271
    case IrOpcode::kWord32Shr:
      return ReduceWord32Shr(node);
272 273
    case IrOpcode::kWord64Shr:
      return ReduceWord64Shr(node);
274 275
    case IrOpcode::kWord32Sar:
      return ReduceWord32Sar(node);
276 277
    case IrOpcode::kWord64Sar:
      return ReduceWord64Sar(node);
278 279 280 281
    case IrOpcode::kWord32Ror: {
      Int32BinopMatcher m(node);
      if (m.right().Is(0)) return Replace(m.left().node());  // x ror 0 => x
      if (m.IsFoldable()) {                                  // K ror K => K
282 283
        return ReplaceInt32(base::bits::RotateRight32(m.left().Value(),
                                                      m.right().Value() & 31));
284 285 286
      }
      break;
    }
287 288 289 290 291 292 293 294 295
    case IrOpcode::kWord32Equal: {
      Int32BinopMatcher m(node);
      if (m.IsFoldable()) {  // K == K => K
        return ReplaceBool(m.left().Value() == m.right().Value());
      }
      if (m.left().IsInt32Sub() && m.right().Is(0)) {  // x - y == 0 => x == y
        Int32BinopMatcher msub(m.left().node());
        node->ReplaceInput(0, msub.left().node());
        node->ReplaceInput(1, msub.right().node());
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
        return Changed(node);
      }
      // TODO(turbofan): fold HeapConstant, ExternalReference, pointer compares
      if (m.LeftEqualsRight()) return ReplaceBool(true);  // x == x => true
      break;
    }
    case IrOpcode::kWord64Equal: {
      Int64BinopMatcher m(node);
      if (m.IsFoldable()) {  // K == K => K
        return ReplaceBool(m.left().Value() == m.right().Value());
      }
      if (m.left().IsInt64Sub() && m.right().Is(0)) {  // x - y == 0 => x == y
        Int64BinopMatcher msub(m.left().node());
        node->ReplaceInput(0, msub.left().node());
        node->ReplaceInput(1, msub.right().node());
311 312 313 314 315 316
        return Changed(node);
      }
      // TODO(turbofan): fold HeapConstant, ExternalReference, pointer compares
      if (m.LeftEqualsRight()) return ReplaceBool(true);  // x == x => true
      break;
    }
317 318
    case IrOpcode::kInt32Add:
      return ReduceInt32Add(node);
319 320
    case IrOpcode::kInt64Add:
      return ReduceInt64Add(node);
321 322
    case IrOpcode::kInt32Sub:
      return ReduceInt32Sub(node);
323 324
    case IrOpcode::kInt64Sub:
      return ReduceInt64Sub(node);
325 326 327 328 329
    case IrOpcode::kInt32Mul: {
      Int32BinopMatcher m(node);
      if (m.right().Is(0)) return Replace(m.right().node());  // x * 0 => 0
      if (m.right().Is(1)) return Replace(m.left().node());   // x * 1 => x
      if (m.IsFoldable()) {                                   // K * K => K
330 331
        return ReplaceInt32(
            base::MulWithWraparound(m.left().Value(), m.right().Value()));
332 333 334 335
      }
      if (m.right().Is(-1)) {  // x * -1 => 0 - x
        node->ReplaceInput(0, Int32Constant(0));
        node->ReplaceInput(1, m.left().node());
336
        NodeProperties::ChangeOp(node, machine()->Int32Sub());
337 338 339
        return Changed(node);
      }
      if (m.right().IsPowerOf2()) {  // x * 2^n => x << n
340 341
        node->ReplaceInput(
            1, Int32Constant(base::bits::WhichPowerOfTwo(m.right().Value())));
342
        NodeProperties::ChangeOp(node, machine()->Word32Shl());
343 344
        Reduction reduction = ReduceWord32Shl(node);
        return reduction.Changed() ? reduction : Changed(node);
345 346 347
      }
      break;
    }
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
    case IrOpcode::kInt32MulWithOverflow: {
      Int32BinopMatcher m(node);
      if (m.right().Is(2)) {
        node->ReplaceInput(1, m.left().node());
        NodeProperties::ChangeOp(node, machine()->Int32AddWithOverflow());
        return Changed(node);
      }
      if (m.right().Is(-1)) {
        node->ReplaceInput(0, Int32Constant(0));
        node->ReplaceInput(1, m.left().node());
        NodeProperties::ChangeOp(node, machine()->Int32SubWithOverflow());
        return Changed(node);
      }
      break;
    }
363 364
    case IrOpcode::kInt64Mul:
      return ReduceInt64Mul(node);
365 366
    case IrOpcode::kInt32Div:
      return ReduceInt32Div(node);
367 368
    case IrOpcode::kUint32Div:
      return ReduceUint32Div(node);
369 370
    case IrOpcode::kInt32Mod:
      return ReduceInt32Mod(node);
371 372
    case IrOpcode::kUint32Mod:
      return ReduceUint32Mod(node);
373 374 375 376 377 378
    case IrOpcode::kInt32LessThan: {
      Int32BinopMatcher m(node);
      if (m.IsFoldable()) {  // K < K => K
        return ReplaceBool(m.left().Value() < m.right().Value());
      }
      if (m.LeftEqualsRight()) return ReplaceBool(false);  // x < x => false
379 380 381 382 383 384 385 386
      if (m.left().IsWord32Or() && m.right().Is(0)) {
        // (x | K) < 0 => true or (K | x) < 0 => true iff K < 0
        Int32BinopMatcher mleftmatcher(m.left().node());
        if (mleftmatcher.left().IsNegative() ||
            mleftmatcher.right().IsNegative()) {
          return ReplaceBool(true);
        }
      }
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
      break;
    }
    case IrOpcode::kInt32LessThanOrEqual: {
      Int32BinopMatcher m(node);
      if (m.IsFoldable()) {  // K <= K => K
        return ReplaceBool(m.left().Value() <= m.right().Value());
      }
      if (m.LeftEqualsRight()) return ReplaceBool(true);  // x <= x => true
      break;
    }
    case IrOpcode::kUint32LessThan: {
      Uint32BinopMatcher m(node);
      if (m.left().Is(kMaxUInt32)) return ReplaceBool(false);  // M < x => false
      if (m.right().Is(0)) return ReplaceBool(false);          // x < 0 => false
      if (m.IsFoldable()) {                                    // K < K => K
        return ReplaceBool(m.left().Value() < m.right().Value());
      }
      if (m.LeftEqualsRight()) return ReplaceBool(false);  // x < x => false
405 406 407
      if (m.left().IsWord32Sar() && m.right().HasValue()) {
        Int32BinopMatcher mleft(m.left().node());
        if (mleft.right().HasValue()) {
408
          // (x >> K) < C => x < (C << K)
409 410
          // when C < (M >> K)
          const uint32_t c = m.right().Value();
411
          const uint32_t k = mleft.right().Value() & 0x1F;
412 413
          if (c < static_cast<uint32_t>(kMaxInt >> k)) {
            node->ReplaceInput(0, mleft.left().node());
414
            node->ReplaceInput(1, Uint32Constant(c << k));
415 416
            return Changed(node);
          }
417
          // TODO(turbofan): else the comparison is always true.
418 419
        }
      }
420 421 422 423 424 425 426 427 428 429 430 431
      break;
    }
    case IrOpcode::kUint32LessThanOrEqual: {
      Uint32BinopMatcher m(node);
      if (m.left().Is(0)) return ReplaceBool(true);            // 0 <= x => true
      if (m.right().Is(kMaxUInt32)) return ReplaceBool(true);  // x <= M => true
      if (m.IsFoldable()) {                                    // K <= K => K
        return ReplaceBool(m.left().Value() <= m.right().Value());
      }
      if (m.LeftEqualsRight()) return ReplaceBool(true);  // x <= x => true
      break;
    }
432 433
    case IrOpcode::kFloat32Sub: {
      Float32BinopMatcher m(node);
434
      if (allow_signalling_nan_ && m.right().Is(0) &&
435
          (std::copysign(1.0, m.right().Value()) > 0)) {
436 437
        return Replace(m.left().node());  // x - 0 => x
      }
438
      if (m.right().IsNaN()) {  // x - NaN => NaN
439 440
        // Do some calculation to make a signalling NaN quiet.
        return ReplaceFloat32(m.right().Value() - m.right().Value());
441 442
      }
      if (m.left().IsNaN()) {  // NaN - x => NaN
443 444
        // Do some calculation to make a signalling NaN quiet.
        return ReplaceFloat32(m.left().Value() - m.left().Value());
445 446 447 448
      }
      if (m.IsFoldable()) {  // L - R => (L - R)
        return ReplaceFloat32(m.left().Value() - m.right().Value());
      }
449
      if (allow_signalling_nan_ && m.left().IsMinusZero()) {
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
        // -0.0 - round_down(-0.0 - R) => round_up(R)
        if (machine()->Float32RoundUp().IsSupported() &&
            m.right().IsFloat32RoundDown()) {
          if (m.right().InputAt(0)->opcode() == IrOpcode::kFloat32Sub) {
            Float32BinopMatcher mright0(m.right().InputAt(0));
            if (mright0.left().IsMinusZero()) {
              return Replace(graph()->NewNode(machine()->Float32RoundUp().op(),
                                              mright0.right().node()));
            }
          }
        }
        // -0.0 - R => -R
        node->RemoveInput(0);
        NodeProperties::ChangeOp(node, machine()->Float32Neg());
        return Changed(node);
      }
      break;
    }
468 469 470 471 472 473 474 475 476
    case IrOpcode::kFloat64Add: {
      Float64BinopMatcher m(node);
      if (m.IsFoldable()) {  // K + K => K
        return ReplaceFloat64(m.left().Value() + m.right().Value());
      }
      break;
    }
    case IrOpcode::kFloat64Sub: {
      Float64BinopMatcher m(node);
477 478 479 480
      if (allow_signalling_nan_ && m.right().Is(0) &&
          (Double(m.right().Value()).Sign() > 0)) {
        return Replace(m.left().node());  // x - 0 => x
      }
481
      if (m.right().IsNaN()) {  // x - NaN => NaN
482 483
        // Do some calculation to make a signalling NaN quiet.
        return ReplaceFloat64(m.right().Value() - m.right().Value());
484 485
      }
      if (m.left().IsNaN()) {  // NaN - x => NaN
486 487
        // Do some calculation to make a signalling NaN quiet.
        return ReplaceFloat64(m.left().Value() - m.left().Value());
488
      }
489
      if (m.IsFoldable()) {  // L - R => (L - R)
490 491
        return ReplaceFloat64(m.left().Value() - m.right().Value());
      }
492
      if (allow_signalling_nan_ && m.left().IsMinusZero()) {
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
        // -0.0 - round_down(-0.0 - R) => round_up(R)
        if (machine()->Float64RoundUp().IsSupported() &&
            m.right().IsFloat64RoundDown()) {
          if (m.right().InputAt(0)->opcode() == IrOpcode::kFloat64Sub) {
            Float64BinopMatcher mright0(m.right().InputAt(0));
            if (mright0.left().IsMinusZero()) {
              return Replace(graph()->NewNode(machine()->Float64RoundUp().op(),
                                              mright0.right().node()));
            }
          }
        }
        // -0.0 - R => -R
        node->RemoveInput(0);
        NodeProperties::ChangeOp(node, machine()->Float64Neg());
        return Changed(node);
      }
509 510 511 512
      break;
    }
    case IrOpcode::kFloat64Mul: {
      Float64BinopMatcher m(node);
513 514
      if (allow_signalling_nan_ && m.right().Is(1))
        return Replace(m.left().node());  // x * 1.0 => x
515 516 517
      if (m.right().Is(-1)) {  // x * -1.0 => -0.0 - x
        node->ReplaceInput(0, Float64Constant(-0.0));
        node->ReplaceInput(1, m.left().node());
518
        NodeProperties::ChangeOp(node, machine()->Float64Sub());
519 520
        return Changed(node);
      }
521
      if (m.right().IsNaN()) {                               // x * NaN => NaN
522 523
        // Do some calculation to make a signalling NaN quiet.
        return ReplaceFloat64(m.right().Value() - m.right().Value());
524 525 526 527
      }
      if (m.IsFoldable()) {  // K * K => K
        return ReplaceFloat64(m.left().Value() * m.right().Value());
      }
528 529 530 531 532
      if (m.right().Is(2)) {  // x * 2.0 => x + x
        node->ReplaceInput(1, m.left().node());
        NodeProperties::ChangeOp(node, machine()->Float64Add());
        return Changed(node);
      }
533
      break;
534 535 536
    }
    case IrOpcode::kFloat64Div: {
      Float64BinopMatcher m(node);
537 538
      if (allow_signalling_nan_ && m.right().Is(1))
        return Replace(m.left().node());  // x / 1.0 => x
539
      // TODO(ahaas): We could do x / 1.0 = x if we knew that x is not an sNaN.
540
      if (m.right().IsNaN()) {                               // x / NaN => NaN
541 542
        // Do some calculation to make a signalling NaN quiet.
        return ReplaceFloat64(m.right().Value() - m.right().Value());
543 544
      }
      if (m.left().IsNaN()) {  // NaN / x => NaN
545 546
        // Do some calculation to make a signalling NaN quiet.
        return ReplaceFloat64(m.left().Value() - m.left().Value());
547 548
      }
      if (m.IsFoldable()) {  // K / K => K
549 550
        return ReplaceFloat64(
            base::Divide(m.left().Value(), m.right().Value()));
551
      }
552
      if (allow_signalling_nan_ && m.right().Is(-1)) {  // x / -1.0 => -x
553 554 555 556 557 558 559 560 561 562 563 564
        node->RemoveInput(1);
        NodeProperties::ChangeOp(node, machine()->Float64Neg());
        return Changed(node);
      }
      if (m.right().IsNormal() && m.right().IsPositiveOrNegativePowerOf2()) {
        // All reciprocals of non-denormal powers of two can be represented
        // exactly, so division by power of two can be reduced to
        // multiplication by reciprocal, with the same result.
        node->ReplaceInput(1, Float64Constant(1.0 / m.right().Value()));
        NodeProperties::ChangeOp(node, machine()->Float64Mul());
        return Changed(node);
      }
565
      break;
566 567 568
    }
    case IrOpcode::kFloat64Mod: {
      Float64BinopMatcher m(node);
569
      if (m.right().Is(0)) {  // x % 0 => NaN
570
        return ReplaceFloat64(std::numeric_limits<double>::quiet_NaN());
571
      }
572 573 574 575 576 577 578
      if (m.right().IsNaN()) {  // x % NaN => NaN
        return Replace(m.right().node());
      }
      if (m.left().IsNaN()) {  // NaN % x => NaN
        return Replace(m.left().node());
      }
      if (m.IsFoldable()) {  // K % K => K
579
        return ReplaceFloat64(Modulo(m.left().Value(), m.right().Value()));
580 581 582
      }
      break;
    }
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
    case IrOpcode::kFloat64Acos: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(base::ieee754::acos(m.Value()));
      break;
    }
    case IrOpcode::kFloat64Acosh: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(base::ieee754::acosh(m.Value()));
      break;
    }
    case IrOpcode::kFloat64Asin: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(base::ieee754::asin(m.Value()));
      break;
    }
    case IrOpcode::kFloat64Asinh: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(base::ieee754::asinh(m.Value()));
      break;
    }
603 604 605 606 607
    case IrOpcode::kFloat64Atan: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(base::ieee754::atan(m.Value()));
      break;
    }
608 609 610 611 612
    case IrOpcode::kFloat64Atanh: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(base::ieee754::atanh(m.Value()));
      break;
    }
613 614 615 616 617 618 619 620 621 622 623 624 625 626
    case IrOpcode::kFloat64Atan2: {
      Float64BinopMatcher m(node);
      if (m.right().IsNaN()) {
        return Replace(m.right().node());
      }
      if (m.left().IsNaN()) {
        return Replace(m.left().node());
      }
      if (m.IsFoldable()) {
        return ReplaceFloat64(
            base::ieee754::atan2(m.left().Value(), m.right().Value()));
      }
      break;
    }
627 628 629 630 631
    case IrOpcode::kFloat64Cbrt: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(base::ieee754::cbrt(m.Value()));
      break;
    }
632 633 634 635 636
    case IrOpcode::kFloat64Cos: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(base::ieee754::cos(m.Value()));
      break;
    }
637 638 639 640 641
    case IrOpcode::kFloat64Cosh: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(base::ieee754::cosh(m.Value()));
      break;
    }
642 643 644 645 646
    case IrOpcode::kFloat64Exp: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(base::ieee754::exp(m.Value()));
      break;
    }
647 648 649 650 651
    case IrOpcode::kFloat64Expm1: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(base::ieee754::expm1(m.Value()));
      break;
    }
652 653
    case IrOpcode::kFloat64Log: {
      Float64Matcher m(node->InputAt(0));
654
      if (m.HasValue()) return ReplaceFloat64(base::ieee754::log(m.Value()));
655 656
      break;
    }
657 658 659 660 661
    case IrOpcode::kFloat64Log1p: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(base::ieee754::log1p(m.Value()));
      break;
    }
662 663 664 665 666
    case IrOpcode::kFloat64Log10: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(base::ieee754::log10(m.Value()));
      break;
    }
667
    case IrOpcode::kFloat64Log2: {
668
      Float64Matcher m(node->InputAt(0));
669 670 671 672 673
      if (m.HasValue()) return ReplaceFloat64(base::ieee754::log2(m.Value()));
      break;
    }
    case IrOpcode::kFloat64Pow: {
      Float64BinopMatcher m(node);
674
      if (m.IsFoldable()) {
675 676
        return ReplaceFloat64(
            base::ieee754::pow(m.left().Value(), m.right().Value()));
677
      } else if (m.right().Is(0.0)) {  // x ** +-0.0 => 1.0
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
        return ReplaceFloat64(1.0);
      } else if (m.right().Is(-2.0)) {  // x ** -2.0 => 1 / (x * x)
        node->ReplaceInput(0, Float64Constant(1.0));
        node->ReplaceInput(1, Float64Mul(m.left().node(), m.left().node()));
        NodeProperties::ChangeOp(node, machine()->Float64Div());
        return Changed(node);
      } else if (m.right().Is(2.0)) {  // x ** 2.0 => x * x
        node->ReplaceInput(1, m.left().node());
        NodeProperties::ChangeOp(node, machine()->Float64Mul());
        return Changed(node);
      } else if (m.right().Is(-0.5)) {
        // x ** 0.5 => 1 / (if x <= -Infinity then Infinity else sqrt(0.0 + x))
        node->ReplaceInput(0, Float64Constant(1.0));
        node->ReplaceInput(1, Float64PowHalf(m.left().node()));
        NodeProperties::ChangeOp(node, machine()->Float64Div());
        return Changed(node);
      } else if (m.right().Is(0.5)) {
        // x ** 0.5 => if x <= -Infinity then Infinity else sqrt(0.0 + x)
        return Replace(Float64PowHalf(m.left().node()));
      }
698 699
      break;
    }
700 701 702 703 704
    case IrOpcode::kFloat64Sin: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(base::ieee754::sin(m.Value()));
      break;
    }
705 706 707 708 709
    case IrOpcode::kFloat64Sinh: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(base::ieee754::sinh(m.Value()));
      break;
    }
710 711 712 713 714
    case IrOpcode::kFloat64Tan: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(base::ieee754::tan(m.Value()));
      break;
    }
715 716 717 718 719
    case IrOpcode::kFloat64Tanh: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(base::ieee754::tanh(m.Value()));
      break;
    }
720 721
    case IrOpcode::kChangeFloat32ToFloat64: {
      Float32Matcher m(node->InputAt(0));
722 723 724 725 726 727 728
      if (m.HasValue()) {
        if (!allow_signalling_nan_ && std::isnan(m.Value())) {
          // Do some calculation to make guarantee the value is a quiet NaN.
          return ReplaceFloat64(m.Value() + m.Value());
        }
        return ReplaceFloat64(m.Value());
      }
729 730
      break;
    }
731 732
    case IrOpcode::kChangeFloat64ToInt32: {
      Float64Matcher m(node->InputAt(0));
733
      if (m.HasValue()) return ReplaceInt32(FastD2IChecked(m.Value()));
734 735 736
      if (m.IsChangeInt32ToFloat64()) return Replace(m.node()->InputAt(0));
      break;
    }
737 738 739
    case IrOpcode::kChangeFloat64ToInt64: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceInt64(static_cast<int64_t>(m.Value()));
740
      if (m.IsChangeInt64ToFloat64()) return Replace(m.node()->InputAt(0));
741 742
      break;
    }
743 744 745 746 747 748 749 750 751 752 753
    case IrOpcode::kChangeFloat64ToUint32: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceInt32(FastD2UI(m.Value()));
      if (m.IsChangeUint32ToFloat64()) return Replace(m.node()->InputAt(0));
      break;
    }
    case IrOpcode::kChangeInt32ToFloat64: {
      Int32Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(FastI2D(m.Value()));
      break;
    }
754 755 756 757 758
    case IrOpcode::kBitcastWord32ToWord64: {
      Int32Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceInt64(m.Value());
      break;
    }
759 760 761 762 763
    case IrOpcode::kChangeInt32ToInt64: {
      Int32Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceInt64(m.Value());
      break;
    }
764 765 766 767 768 769
    case IrOpcode::kChangeInt64ToFloat64: {
      Int64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(static_cast<double>(m.Value()));
      if (m.IsChangeFloat64ToInt64()) return Replace(m.node()->InputAt(0));
      break;
    }
770 771 772 773 774 775 776 777 778 779
    case IrOpcode::kChangeUint32ToFloat64: {
      Uint32Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(FastUI2D(m.Value()));
      break;
    }
    case IrOpcode::kChangeUint32ToUint64: {
      Uint32Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceInt64(static_cast<uint64_t>(m.Value()));
      break;
    }
780 781 782 783 784 785
    case IrOpcode::kTruncateFloat64ToWord32: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceInt32(DoubleToInt32(m.Value()));
      if (m.IsChangeInt32ToFloat64()) return Replace(m.node()->InputAt(0));
      return NoChange();
    }
786 787 788 789 790 791
    case IrOpcode::kTruncateInt64ToInt32: {
      Int64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceInt32(static_cast<int32_t>(m.Value()));
      if (m.IsChangeInt32ToInt64()) return Replace(m.node()->InputAt(0));
      break;
    }
792 793
    case IrOpcode::kTruncateFloat64ToFloat32: {
      Float64Matcher m(node->InputAt(0));
794 795 796 797 798 799 800 801 802
      if (m.HasValue()) {
        if (!allow_signalling_nan_ && std::isnan(m.Value())) {
          // Do some calculation to make guarantee the value is a quiet NaN.
          return ReplaceFloat32(DoubleToFloat32(m.Value() + m.Value()));
        }
        return ReplaceFloat32(DoubleToFloat32(m.Value()));
      }
      if (allow_signalling_nan_ && m.IsChangeFloat32ToFloat64())
        return Replace(m.node()->InputAt(0));
803 804
      break;
    }
805 806
    case IrOpcode::kRoundFloat64ToInt32: {
      Float64Matcher m(node->InputAt(0));
807 808 809
      if (m.HasValue()) {
        return ReplaceInt32(DoubleToInt32(m.Value()));
      }
810 811 812
      if (m.IsChangeInt32ToFloat64()) return Replace(m.node()->InputAt(0));
      break;
    }
813 814 815 816
    case IrOpcode::kFloat64InsertLowWord32:
      return ReduceFloat64InsertLowWord32(node);
    case IrOpcode::kFloat64InsertHighWord32:
      return ReduceFloat64InsertHighWord32(node);
817
    case IrOpcode::kStore:
818
    case IrOpcode::kUnalignedStore:
819
      return ReduceStore(node);
820 821 822 823
    case IrOpcode::kFloat64Equal:
    case IrOpcode::kFloat64LessThan:
    case IrOpcode::kFloat64LessThanOrEqual:
      return ReduceFloat64Compare(node);
824 825
    case IrOpcode::kFloat64RoundDown:
      return ReduceFloat64RoundDown(node);
826
    case IrOpcode::kBitcastTaggedToWord:
827
    case IrOpcode::kBitcastTaggedToWordForTagAndSmiBits: {
828 829 830 831 832 833 834
      NodeMatcher m(node->InputAt(0));
      if (m.IsBitcastWordToTaggedSigned()) {
        RelaxEffectsAndControls(node);
        return Replace(m.InputAt(0));
      }
      break;
    }
835 836 837 838 839
    default:
      break;
  }
  return NoChange();
}
840

841 842 843 844 845
Reduction MachineOperatorReducer::ReduceInt32Add(Node* node) {
  DCHECK_EQ(IrOpcode::kInt32Add, node->opcode());
  Int32BinopMatcher m(node);
  if (m.right().Is(0)) return Replace(m.left().node());  // x + 0 => x
  if (m.IsFoldable()) {                                  // K + K => K
846 847
    return ReplaceInt32(
        base::AddWithWraparound(m.left().Value(), m.right().Value()));
848
  }
849 850 851 852 853
  if (m.left().IsInt32Sub()) {
    Int32BinopMatcher mleft(m.left().node());
    if (mleft.left().Is(0)) {  // (0 - x) + y => y - x
      node->ReplaceInput(0, m.right().node());
      node->ReplaceInput(1, mleft.right().node());
854
      NodeProperties::ChangeOp(node, machine()->Int32Sub());
855 856 857 858 859 860 861 862
      Reduction const reduction = ReduceInt32Sub(node);
      return reduction.Changed() ? reduction : Changed(node);
    }
  }
  if (m.right().IsInt32Sub()) {
    Int32BinopMatcher mright(m.right().node());
    if (mright.left().Is(0)) {  // y + (0 - x) => y - x
      node->ReplaceInput(1, mright.right().node());
863
      NodeProperties::ChangeOp(node, machine()->Int32Sub());
864 865 866 867
      Reduction const reduction = ReduceInt32Sub(node);
      return reduction.Changed() ? reduction : Changed(node);
    }
  }
868 869 870
  // (x + Int32Constant(a)) + Int32Constant(b)) => x + Int32Constant(a + b)
  if (m.right().HasValue() && m.left().IsInt32Add()) {
    Int32BinopMatcher n(m.left().node());
871
    if (n.right().HasValue() && m.OwnsInput(m.left().node())) {
872 873 874 875 876 877
      node->ReplaceInput(1, Int32Constant(base::AddWithWraparound(
                                m.right().Value(), n.right().Value())));
      node->ReplaceInput(0, n.left().node());
      return Changed(node);
    }
  }
878

879 880 881
  return NoChange();
}

882 883 884 885 886
Reduction MachineOperatorReducer::ReduceInt64Add(Node* node) {
  DCHECK_EQ(IrOpcode::kInt64Add, node->opcode());
  Int64BinopMatcher m(node);
  if (m.right().Is(0)) return Replace(m.left().node());  // x + 0 => 0
  if (m.IsFoldable()) {
887 888
    return ReplaceInt64(
        base::AddWithWraparound(m.left().Value(), m.right().Value()));
889
  }
890 891 892
  // (x + Int64Constant(a)) + Int64Constant(b)) => x + Int64Constant(a + b)
  if (m.right().HasValue() && m.left().IsInt64Add()) {
    Int64BinopMatcher n(m.left().node());
893
    if (n.right().HasValue() && m.OwnsInput(m.left().node())) {
894 895 896 897 898 899
      node->ReplaceInput(1, Int64Constant(base::AddWithWraparound(
                                m.right().Value(), n.right().Value())));
      node->ReplaceInput(0, n.left().node());
      return Changed(node);
    }
  }
900 901
  return NoChange();
}
902

903 904 905 906 907
Reduction MachineOperatorReducer::ReduceInt32Sub(Node* node) {
  DCHECK_EQ(IrOpcode::kInt32Sub, node->opcode());
  Int32BinopMatcher m(node);
  if (m.right().Is(0)) return Replace(m.left().node());  // x - 0 => x
  if (m.IsFoldable()) {                                  // K - K => K
908 909
    return ReplaceInt32(
        base::SubWithWraparound(m.left().Value(), m.right().Value()));
910 911 912
  }
  if (m.LeftEqualsRight()) return ReplaceInt32(0);  // x - x => 0
  if (m.right().HasValue()) {                       // x - K => x + -K
913 914
    node->ReplaceInput(
        1, Int32Constant(base::NegateWithWraparound(m.right().Value())));
915
    NodeProperties::ChangeOp(node, machine()->Int32Add());
916 917 918 919 920 921
    Reduction const reduction = ReduceInt32Add(node);
    return reduction.Changed() ? reduction : Changed(node);
  }
  return NoChange();
}

922 923 924 925 926
Reduction MachineOperatorReducer::ReduceInt64Sub(Node* node) {
  DCHECK_EQ(IrOpcode::kInt64Sub, node->opcode());
  Int64BinopMatcher m(node);
  if (m.right().Is(0)) return Replace(m.left().node());  // x - 0 => x
  if (m.IsFoldable()) {                                  // K - K => K
927 928
    return ReplaceInt64(
        base::SubWithWraparound(m.left().Value(), m.right().Value()));
929 930 931
  }
  if (m.LeftEqualsRight()) return Replace(Int64Constant(0));  // x - x => 0
  if (m.right().HasValue()) {                                 // x - K => x + -K
932 933
    node->ReplaceInput(
        1, Int64Constant(base::NegateWithWraparound(m.right().Value())));
934 935 936 937 938 939
    NodeProperties::ChangeOp(node, machine()->Int64Add());
    Reduction const reduction = ReduceInt64Add(node);
    return reduction.Changed() ? reduction : Changed(node);
  }
  return NoChange();
}
940

941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
Reduction MachineOperatorReducer::ReduceInt64Mul(Node* node) {
  DCHECK_EQ(IrOpcode::kInt64Mul, node->opcode());
  Int64BinopMatcher m(node);
  if (m.right().Is(0)) return Replace(m.right().node());  // x * 0 => 0
  if (m.right().Is(1)) return Replace(m.left().node());   // x * 1 => x
  if (m.IsFoldable()) {                                   // K * K => K
    return ReplaceInt64(
        base::MulWithWraparound(m.left().Value(), m.right().Value()));
  }
  if (m.right().Is(-1)) {  // x * -1 => 0 - x
    node->ReplaceInput(0, Int64Constant(0));
    node->ReplaceInput(1, m.left().node());
    NodeProperties::ChangeOp(node, machine()->Int64Sub());
    return Changed(node);
  }
  if (m.right().IsPowerOf2()) {  // x * 2^n => x << n
    node->ReplaceInput(
        1, Int64Constant(base::bits::WhichPowerOfTwo(m.right().Value())));
    NodeProperties::ChangeOp(node, machine()->Word64Shl());
    Reduction reduction = ReduceWord64Shl(node);
    return reduction.Changed() ? reduction : Changed(node);
  }
  return NoChange();
}

966 967
Reduction MachineOperatorReducer::ReduceInt32Div(Node* node) {
  Int32BinopMatcher m(node);
968
  if (m.left().Is(0)) return Replace(m.left().node());    // 0 / x => 0
969 970
  if (m.right().Is(0)) return Replace(m.right().node());  // x / 0 => 0
  if (m.right().Is(1)) return Replace(m.left().node());   // x / 1 => x
971 972 973 974 975 976 977
  if (m.IsFoldable()) {                                   // K / K => K
    return ReplaceInt32(
        base::bits::SignedDiv32(m.left().Value(), m.right().Value()));
  }
  if (m.LeftEqualsRight()) {  // x / x => x != 0
    Node* const zero = Int32Constant(0);
    return Replace(Word32Equal(Word32Equal(m.left().node(), zero), zero));
978 979 980 981
  }
  if (m.right().Is(-1)) {  // x / -1 => 0 - x
    node->ReplaceInput(0, Int32Constant(0));
    node->ReplaceInput(1, m.left().node());
982
    node->TrimInputCount(2);
983
    NodeProperties::ChangeOp(node, machine()->Int32Sub());
984 985 986 987 988 989
    return Changed(node);
  }
  if (m.right().HasValue()) {
    int32_t const divisor = m.right().Value();
    Node* const dividend = m.left().node();
    Node* quotient = dividend;
990
    if (base::bits::IsPowerOfTwo(Abs(divisor))) {
991
      uint32_t const shift = base::bits::WhichPowerOfTwo(Abs(divisor));
992
      DCHECK_NE(0u, shift);
993 994 995 996 997 998
      if (shift > 1) {
        quotient = Word32Sar(quotient, 31);
      }
      quotient = Int32Add(Word32Shr(quotient, 32u - shift), dividend);
      quotient = Word32Sar(quotient, shift);
    } else {
999
      quotient = Int32Div(quotient, Abs(divisor));
1000 1001 1002 1003
    }
    if (divisor < 0) {
      node->ReplaceInput(0, Int32Constant(0));
      node->ReplaceInput(1, quotient);
1004
      node->TrimInputCount(2);
1005
      NodeProperties::ChangeOp(node, machine()->Int32Sub());
1006 1007 1008 1009 1010 1011 1012
      return Changed(node);
    }
    return Replace(quotient);
  }
  return NoChange();
}

1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
Reduction MachineOperatorReducer::ReduceUint32Div(Node* node) {
  Uint32BinopMatcher m(node);
  if (m.left().Is(0)) return Replace(m.left().node());    // 0 / x => 0
  if (m.right().Is(0)) return Replace(m.right().node());  // x / 0 => 0
  if (m.right().Is(1)) return Replace(m.left().node());   // x / 1 => x
  if (m.IsFoldable()) {                                   // K / K => K
    return ReplaceUint32(
        base::bits::UnsignedDiv32(m.left().Value(), m.right().Value()));
  }
  if (m.LeftEqualsRight()) {  // x / x => x != 0
    Node* const zero = Int32Constant(0);
    return Replace(Word32Equal(Word32Equal(m.left().node(), zero), zero));
  }
1026 1027 1028
  if (m.right().HasValue()) {
    Node* const dividend = m.left().node();
    uint32_t const divisor = m.right().Value();
1029
    if (base::bits::IsPowerOfTwo(divisor)) {  // x / 2^n => x >> n
1030 1031
      node->ReplaceInput(
          1, Uint32Constant(base::bits::WhichPowerOfTwo(m.right().Value())));
1032
      node->TrimInputCount(2);
1033
      NodeProperties::ChangeOp(node, machine()->Word32Shr());
1034 1035 1036 1037
      return Changed(node);
    } else {
      return Replace(Uint32Div(dividend, divisor));
    }
1038 1039 1040 1041
  }
  return NoChange();
}

1042
Reduction MachineOperatorReducer::ReduceInt32Mod(Node* node) {
1043
  Int32BinopMatcher m(node);
1044 1045 1046 1047 1048 1049 1050 1051
  if (m.left().Is(0)) return Replace(m.left().node());    // 0 % x  => 0
  if (m.right().Is(0)) return Replace(m.right().node());  // x % 0  => 0
  if (m.right().Is(1)) return ReplaceInt32(0);            // x % 1  => 0
  if (m.right().Is(-1)) return ReplaceInt32(0);           // x % -1 => 0
  if (m.LeftEqualsRight()) return ReplaceInt32(0);        // x % x  => 0
  if (m.IsFoldable()) {                                   // K % K => K
    return ReplaceInt32(
        base::bits::SignedMod32(m.left().Value(), m.right().Value()));
1052
  }
1053 1054
  if (m.right().HasValue()) {
    Node* const dividend = m.left().node();
1055 1056
    uint32_t const divisor = Abs(m.right().Value());
    if (base::bits::IsPowerOfTwo(divisor)) {
1057 1058
      uint32_t const mask = divisor - 1;
      Node* const zero = Int32Constant(0);
1059 1060 1061 1062 1063 1064 1065
      Diamond d(graph(), common(),
                graph()->NewNode(machine()->Int32LessThan(), dividend, zero),
                BranchHint::kFalse);
      return Replace(
          d.Phi(MachineRepresentation::kWord32,
                Int32Sub(zero, Word32And(Int32Sub(zero, dividend), mask)),
                Word32And(dividend, mask)));
1066
    } else {
1067
      Node* quotient = Int32Div(dividend, divisor);
1068 1069
      DCHECK_EQ(dividend, node->InputAt(0));
      node->ReplaceInput(1, Int32Mul(quotient, Int32Constant(divisor)));
1070
      node->TrimInputCount(2);
1071
      NodeProperties::ChangeOp(node, machine()->Int32Sub());
1072
    }
1073
    return Changed(node);
1074 1075 1076 1077
  }
  return NoChange();
}

1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
Reduction MachineOperatorReducer::ReduceUint32Mod(Node* node) {
  Uint32BinopMatcher m(node);
  if (m.left().Is(0)) return Replace(m.left().node());    // 0 % x => 0
  if (m.right().Is(0)) return Replace(m.right().node());  // x % 0 => 0
  if (m.right().Is(1)) return ReplaceUint32(0);           // x % 1 => 0
  if (m.LeftEqualsRight()) return ReplaceInt32(0);        // x % x  => 0
  if (m.IsFoldable()) {                                   // K % K => K
    return ReplaceUint32(
        base::bits::UnsignedMod32(m.left().Value(), m.right().Value()));
  }
1088 1089 1090
  if (m.right().HasValue()) {
    Node* const dividend = m.left().node();
    uint32_t const divisor = m.right().Value();
1091
    if (base::bits::IsPowerOfTwo(divisor)) {  // x % 2^n => x & 2^n-1
1092
      node->ReplaceInput(1, Uint32Constant(m.right().Value() - 1));
1093 1094
      node->TrimInputCount(2);
      NodeProperties::ChangeOp(node, machine()->Word32And());
1095 1096 1097 1098
    } else {
      Node* quotient = Uint32Div(dividend, divisor);
      DCHECK_EQ(dividend, node->InputAt(0));
      node->ReplaceInput(1, Int32Mul(quotient, Uint32Constant(divisor)));
1099 1100
      node->TrimInputCount(2);
      NodeProperties::ChangeOp(node, machine()->Int32Sub());
1101
    }
1102 1103 1104 1105 1106
    return Changed(node);
  }
  return NoChange();
}

1107
Reduction MachineOperatorReducer::ReduceStore(Node* node) {
1108 1109 1110
  NodeMatcher nm(node);
  MachineRepresentation rep;
  int value_input;
1111
  if (nm.IsStore()) {
1112 1113
    rep = StoreRepresentationOf(node->op()).representation();
    value_input = 2;
1114 1115 1116 1117
  } else {
    DCHECK(nm.IsUnalignedStore());
    rep = UnalignedStoreRepresentationOf(node->op());
    value_input = 2;
1118 1119 1120 1121
  }

  Node* const value = node->InputAt(value_input);

1122 1123 1124
  switch (value->opcode()) {
    case IrOpcode::kWord32And: {
      Uint32BinopMatcher m(value);
1125
      if (m.right().HasValue() && ((rep == MachineRepresentation::kWord8 &&
1126
                                    (m.right().Value() & 0xFF) == 0xFF) ||
1127
                                   (rep == MachineRepresentation::kWord16 &&
1128
                                    (m.right().Value() & 0xFFFF) == 0xFFFF))) {
1129
        node->ReplaceInput(value_input, m.left().node());
1130 1131 1132 1133 1134 1135
        return Changed(node);
      }
      break;
    }
    case IrOpcode::kWord32Sar: {
      Int32BinopMatcher m(value);
1136 1137 1138 1139
      if (m.left().IsWord32Shl() && ((rep == MachineRepresentation::kWord8 &&
                                      m.right().IsInRange(1, 24)) ||
                                     (rep == MachineRepresentation::kWord16 &&
                                      m.right().IsInRange(1, 16)))) {
1140 1141
        Int32BinopMatcher mleft(m.left().node());
        if (mleft.right().Is(m.right().Value())) {
1142
          node->ReplaceInput(value_input, mleft.left().node());
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
          return Changed(node);
        }
      }
      break;
    }
    default:
      break;
  }
  return NoChange();
}

1154 1155 1156 1157 1158 1159 1160 1161 1162
Reduction MachineOperatorReducer::ReduceProjection(size_t index, Node* node) {
  switch (node->opcode()) {
    case IrOpcode::kInt32AddWithOverflow: {
      DCHECK(index == 0 || index == 1);
      Int32BinopMatcher m(node);
      if (m.IsFoldable()) {
        int32_t val;
        bool ovf = base::bits::SignedAddOverflow32(m.left().Value(),
                                                   m.right().Value(), &val);
1163
        return ReplaceInt32(index == 0 ? val : ovf);
1164 1165
      }
      if (m.right().Is(0)) {
1166
        return Replace(index == 0 ? m.left().node() : m.right().node());
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
      }
      break;
    }
    case IrOpcode::kInt32SubWithOverflow: {
      DCHECK(index == 0 || index == 1);
      Int32BinopMatcher m(node);
      if (m.IsFoldable()) {
        int32_t val;
        bool ovf = base::bits::SignedSubOverflow32(m.left().Value(),
                                                   m.right().Value(), &val);
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
        return ReplaceInt32(index == 0 ? val : ovf);
      }
      if (m.right().Is(0)) {
        return Replace(index == 0 ? m.left().node() : m.right().node());
      }
      break;
    }
    case IrOpcode::kInt32MulWithOverflow: {
      DCHECK(index == 0 || index == 1);
      Int32BinopMatcher m(node);
      if (m.IsFoldable()) {
        int32_t val;
        bool ovf = base::bits::SignedMulOverflow32(m.left().Value(),
                                                   m.right().Value(), &val);
        return ReplaceInt32(index == 0 ? val : ovf);
1192 1193
      }
      if (m.right().Is(0)) {
1194 1195 1196 1197
        return Replace(m.right().node());
      }
      if (m.right().Is(1)) {
        return index == 0 ? Replace(m.left().node()) : ReplaceInt32(0);
1198 1199 1200 1201 1202 1203 1204 1205 1206
      }
      break;
    }
    default:
      break;
  }
  return NoChange();
}

1207 1208 1209 1210 1211
Reduction MachineOperatorReducer::ReduceWord32Shifts(Node* node) {
  DCHECK((node->opcode() == IrOpcode::kWord32Shl) ||
         (node->opcode() == IrOpcode::kWord32Shr) ||
         (node->opcode() == IrOpcode::kWord32Sar));
  if (machine()->Word32ShiftIsSafe()) {
1212
    // Remove the explicit 'and' with 0x1F if the shift provided by the machine
1213 1214 1215 1216
    // instruction matches that required by JavaScript.
    Int32BinopMatcher m(node);
    if (m.right().IsWord32And()) {
      Int32BinopMatcher mright(m.right().node());
1217
      if (mright.right().Is(0x1F)) {
1218 1219 1220 1221 1222 1223 1224 1225
        node->ReplaceInput(1, mright.left().node());
        return Changed(node);
      }
    }
  }
  return NoChange();
}

1226 1227 1228 1229 1230
Reduction MachineOperatorReducer::ReduceWord32Shl(Node* node) {
  DCHECK_EQ(IrOpcode::kWord32Shl, node->opcode());
  Int32BinopMatcher m(node);
  if (m.right().Is(0)) return Replace(m.left().node());  // x << 0 => x
  if (m.IsFoldable()) {                                  // K << K => K
1231 1232
    return ReplaceInt32(
        base::ShlWithWraparound(m.left().Value(), m.right().Value()));
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
  }
  if (m.right().IsInRange(1, 31)) {
    // (x >>> K) << K => x & ~(2^K - 1)
    // (x >> K) << K => x & ~(2^K - 1)
    if (m.left().IsWord32Sar() || m.left().IsWord32Shr()) {
      Int32BinopMatcher mleft(m.left().node());
      if (mleft.right().Is(m.right().Value())) {
        node->ReplaceInput(0, mleft.left().node());
        node->ReplaceInput(1,
                           Uint32Constant(~((1U << m.right().Value()) - 1U)));
1243
        NodeProperties::ChangeOp(node, machine()->Word32And());
1244 1245 1246 1247 1248 1249 1250 1251
        Reduction reduction = ReduceWord32And(node);
        return reduction.Changed() ? reduction : Changed(node);
      }
    }
  }
  return ReduceWord32Shifts(node);
}

1252 1253 1254 1255 1256
Reduction MachineOperatorReducer::ReduceWord64Shl(Node* node) {
  DCHECK_EQ(IrOpcode::kWord64Shl, node->opcode());
  Int64BinopMatcher m(node);
  if (m.right().Is(0)) return Replace(m.left().node());  // x << 0 => x
  if (m.IsFoldable()) {                                  // K << K => K
1257 1258
    return ReplaceInt64(
        base::ShlWithWraparound(m.left().Value(), m.right().Value()));
1259 1260 1261 1262
  }
  return NoChange();
}

1263 1264 1265 1266
Reduction MachineOperatorReducer::ReduceWord32Shr(Node* node) {
  Uint32BinopMatcher m(node);
  if (m.right().Is(0)) return Replace(m.left().node());  // x >>> 0 => x
  if (m.IsFoldable()) {                                  // K >>> K => K
1267
    return ReplaceInt32(m.left().Value() >> (m.right().Value() & 31));
1268 1269 1270 1271
  }
  if (m.left().IsWord32And() && m.right().HasValue()) {
    Uint32BinopMatcher mleft(m.left().node());
    if (mleft.right().HasValue()) {
1272
      uint32_t shift = m.right().Value() & 31;
1273 1274 1275 1276 1277 1278 1279 1280 1281
      uint32_t mask = mleft.right().Value();
      if ((mask >> shift) == 0) {
        // (m >>> s) == 0 implies ((x & m) >>> s) == 0
        return ReplaceInt32(0);
      }
    }
  }
  return ReduceWord32Shifts(node);
}
1282

1283 1284 1285 1286 1287
Reduction MachineOperatorReducer::ReduceWord64Shr(Node* node) {
  DCHECK_EQ(IrOpcode::kWord64Shr, node->opcode());
  Uint64BinopMatcher m(node);
  if (m.right().Is(0)) return Replace(m.left().node());  // x >>> 0 => x
  if (m.IsFoldable()) {                                  // K >> K => K
1288
    return ReplaceInt64(m.left().Value() >> (m.right().Value() & 63));
1289 1290 1291 1292
  }
  return NoChange();
}

1293 1294 1295 1296
Reduction MachineOperatorReducer::ReduceWord32Sar(Node* node) {
  Int32BinopMatcher m(node);
  if (m.right().Is(0)) return Replace(m.left().node());  // x >> 0 => x
  if (m.IsFoldable()) {                                  // K >> K => K
1297
    return ReplaceInt32(m.left().Value() >> (m.right().Value() & 31));
1298 1299 1300 1301 1302 1303 1304 1305
  }
  if (m.left().IsWord32Shl()) {
    Int32BinopMatcher mleft(m.left().node());
    if (mleft.left().IsComparison()) {
      if (m.right().Is(31) && mleft.right().Is(31)) {
        // Comparison << 31 >> 31 => 0 - Comparison
        node->ReplaceInput(0, Int32Constant(0));
        node->ReplaceInput(1, mleft.left().node());
1306
        NodeProperties::ChangeOp(node, machine()->Int32Sub());
1307 1308 1309 1310 1311
        Reduction const reduction = ReduceInt32Sub(node);
        return reduction.Changed() ? reduction : Changed(node);
      }
    } else if (mleft.left().IsLoad()) {
      LoadRepresentation const rep =
1312 1313 1314
          LoadRepresentationOf(mleft.left().node()->op());
      if (m.right().Is(24) && mleft.right().Is(24) &&
          rep == MachineType::Int8()) {
1315 1316 1317
        // Load[kMachInt8] << 24 >> 24 => Load[kMachInt8]
        return Replace(mleft.left().node());
      }
1318 1319
      if (m.right().Is(16) && mleft.right().Is(16) &&
          rep == MachineType::Int16()) {
1320 1321 1322 1323 1324 1325 1326 1327
        // Load[kMachInt16] << 16 >> 16 => Load[kMachInt8]
        return Replace(mleft.left().node());
      }
    }
  }
  return ReduceWord32Shifts(node);
}

1328 1329 1330 1331
Reduction MachineOperatorReducer::ReduceWord64Sar(Node* node) {
  Int64BinopMatcher m(node);
  if (m.right().Is(0)) return Replace(m.left().node());  // x >> 0 => x
  if (m.IsFoldable()) {
1332
    return ReplaceInt64(m.left().Value() >> (m.right().Value() & 63));
1333 1334 1335
  }
  return NoChange();
}
1336

1337 1338 1339 1340 1341 1342
template <typename WordNAdapter>
Reduction MachineOperatorReducer::ReduceWordNAnd(Node* node) {
  using A = WordNAdapter;
  A a(this);

  typename A::IntNBinopMatcher m(node);
1343 1344
  if (m.right().Is(0)) return Replace(m.right().node());  // x & 0  => 0
  if (m.right().Is(-1)) return Replace(m.left().node());  // x & -1 => x
1345 1346 1347
  if (m.left().IsComparison() && m.right().Is(1)) {       // CMP & 1 => CMP
    return Replace(m.left().node());
  }
1348
  if (m.IsFoldable()) {                                   // K & K  => K
1349
    return a.ReplaceIntN(m.left().Value() & m.right().Value());
1350 1351
  }
  if (m.LeftEqualsRight()) return Replace(m.left().node());  // x & x => x
1352 1353
  if (A::IsWordNAnd(m.left()) && m.right().HasValue()) {
    typename A::IntNBinopMatcher mleft(m.left().node());
1354 1355 1356
    if (mleft.right().HasValue()) {  // (x & K) & K => x & K
      node->ReplaceInput(0, mleft.left().node());
      node->ReplaceInput(
1357 1358
          1, a.IntNConstant(m.right().Value() & mleft.right().Value()));
      Reduction const reduction = a.ReduceWordNAnd(node);
1359 1360 1361
      return reduction.Changed() ? reduction : Changed(node);
    }
  }
1362
  if (m.right().IsNegativePowerOf2()) {
1363 1364 1365 1366
    typename A::intN_t const mask = m.right().Value();
    typename A::intN_t const neg_mask = base::NegateWithWraparound(mask);
    if (A::IsWordNShl(m.left())) {
      typename A::UintNBinopMatcher mleft(m.left().node());
1367
      if (mleft.right().HasValue() &&
1368
          (mleft.right().Value() & (A::WORD_SIZE - 1)) >=
1369
              base::bits::CountTrailingZeros(mask)) {
1370
        // (x << L) & (-1 << K) => x << L iff L >= K
1371 1372
        return Replace(mleft.node());
      }
1373 1374
    } else if (A::IsIntNAdd(m.left())) {
      typename A::IntNBinopMatcher mleft(m.left().node());
1375 1376 1377
      if (mleft.right().HasValue() &&
          (mleft.right().Value() & mask) == mleft.right().Value()) {
        // (x + (K << L)) & (-1 << L) => (x & (-1 << L)) + (K << L)
1378 1379
        node->ReplaceInput(0,
                           a.WordNAnd(mleft.left().node(), m.right().node()));
1380
        node->ReplaceInput(1, mleft.right().node());
1381 1382
        NodeProperties::ChangeOp(node, a.IntNAdd(machine()));
        Reduction const reduction = a.ReduceIntNAdd(node);
1383 1384
        return reduction.Changed() ? reduction : Changed(node);
      }
1385 1386
      if (A::IsIntNMul(mleft.left())) {
        typename A::IntNBinopMatcher mleftleft(mleft.left().node());
1387
        if (mleftleft.right().IsMultipleOf(neg_mask)) {
1388
          // (y * (K << L) + x) & (-1 << L) => (x & (-1 << L)) + y * (K << L)
1389 1390
          node->ReplaceInput(
              0, a.WordNAnd(mleft.right().node(), m.right().node()));
1391
          node->ReplaceInput(1, mleftleft.node());
1392 1393
          NodeProperties::ChangeOp(node, a.IntNAdd(machine()));
          Reduction const reduction = a.ReduceIntNAdd(node);
1394 1395
          return reduction.Changed() ? reduction : Changed(node);
        }
1396
      }
1397 1398
      if (A::IsIntNMul(mleft.right())) {
        typename A::IntNBinopMatcher mleftright(mleft.right().node());
1399
        if (mleftright.right().IsMultipleOf(neg_mask)) {
1400 1401
          // (x + y * (K << L)) & (-1 << L) => (x & (-1 << L)) + y * (K << L)
          node->ReplaceInput(0,
1402
                             a.WordNAnd(mleft.left().node(), m.right().node()));
1403
          node->ReplaceInput(1, mleftright.node());
1404 1405
          NodeProperties::ChangeOp(node, a.IntNAdd(machine()));
          Reduction const reduction = a.ReduceIntNAdd(node);
1406 1407 1408
          return reduction.Changed() ? reduction : Changed(node);
        }
      }
1409 1410
      if (A::IsWordNShl(mleft.left())) {
        typename A::IntNBinopMatcher mleftleft(mleft.left().node());
1411
        if (mleftleft.right().Is(base::bits::CountTrailingZeros(mask))) {
1412
          // (y << L + x) & (-1 << L) => (x & (-1 << L)) + y << L
1413 1414
          node->ReplaceInput(
              0, a.WordNAnd(mleft.right().node(), m.right().node()));
1415
          node->ReplaceInput(1, mleftleft.node());
1416 1417
          NodeProperties::ChangeOp(node, a.IntNAdd(machine()));
          Reduction const reduction = a.ReduceIntNAdd(node);
1418 1419 1420
          return reduction.Changed() ? reduction : Changed(node);
        }
      }
1421 1422
      if (A::IsWordNShl(mleft.right())) {
        typename A::IntNBinopMatcher mleftright(mleft.right().node());
1423
        if (mleftright.right().Is(base::bits::CountTrailingZeros(mask))) {
1424 1425
          // (x + y << L) & (-1 << L) => (x & (-1 << L)) + y << L
          node->ReplaceInput(0,
1426
                             a.WordNAnd(mleft.left().node(), m.right().node()));
1427
          node->ReplaceInput(1, mleftright.node());
1428 1429
          NodeProperties::ChangeOp(node, a.IntNAdd(machine()));
          Reduction const reduction = a.ReduceIntNAdd(node);
1430 1431
          return reduction.Changed() ? reduction : Changed(node);
        }
1432
      }
1433 1434
    } else if (A::IsIntNMul(m.left())) {
      typename A::IntNBinopMatcher mleft(m.left().node());
1435
      if (mleft.right().IsMultipleOf(neg_mask)) {
1436 1437 1438
        // (x * (K << L)) & (-1 << L) => x * (K << L)
        return Replace(mleft.node());
      }
1439
    }
1440 1441 1442 1443
  }
  return NoChange();
}

1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
Reduction MachineOperatorReducer::ReduceWord32And(Node* node) {
  DCHECK_EQ(IrOpcode::kWord32And, node->opcode());
  return ReduceWordNAnd<Word32Adapter>(node);
}

Reduction MachineOperatorReducer::ReduceWord64And(Node* node) {
  DCHECK_EQ(IrOpcode::kWord64And, node->opcode());
  return ReduceWordNAnd<Word64Adapter>(node);
}

1454 1455 1456
Reduction MachineOperatorReducer::TryMatchWord32Ror(Node* node) {
  DCHECK(IrOpcode::kWord32Or == node->opcode() ||
         IrOpcode::kWord32Xor == node->opcode());
1457
  Int32BinopMatcher m(node);
1458 1459
  Node* shl = nullptr;
  Node* shr = nullptr;
1460
  // Recognize rotation, we are matching:
1461 1462
  //  * x << y | x >>> (32 - y) => x ror (32 - y), i.e  x rol y
  //  * x << (32 - y) | x >>> y => x ror y
1463 1464
  //  * x << y ^ x >>> (32 - y) => x ror (32 - y), i.e. x rol y
  //  * x << (32 - y) ^ x >>> y => x ror y
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
  // as well as their commuted form.
  if (m.left().IsWord32Shl() && m.right().IsWord32Shr()) {
    shl = m.left().node();
    shr = m.right().node();
  } else if (m.left().IsWord32Shr() && m.right().IsWord32Shl()) {
    shl = m.right().node();
    shr = m.left().node();
  } else {
    return NoChange();
  }

  Int32BinopMatcher mshl(shl);
  Int32BinopMatcher mshr(shr);
  if (mshl.left().node() != mshr.left().node()) return NoChange();

  if (mshl.right().HasValue() && mshr.right().HasValue()) {
    // Case where y is a constant.
    if (mshl.right().Value() + mshr.right().Value() != 32) return NoChange();
  } else {
1484 1485
    Node* sub = nullptr;
    Node* y = nullptr;
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501
    if (mshl.right().IsInt32Sub()) {
      sub = mshl.right().node();
      y = mshr.right().node();
    } else if (mshr.right().IsInt32Sub()) {
      sub = mshr.right().node();
      y = mshl.right().node();
    } else {
      return NoChange();
    }

    Int32BinopMatcher msub(sub);
    if (!msub.left().Is(32) || msub.right().node() != y) return NoChange();
  }

  node->ReplaceInput(0, mshl.left().node());
  node->ReplaceInput(1, mshr.right().node());
1502
  NodeProperties::ChangeOp(node, machine()->Word32Ror());
1503 1504 1505
  return Changed(node);
}

1506 1507 1508 1509 1510 1511
template <typename WordNAdapter>
Reduction MachineOperatorReducer::ReduceWordNOr(Node* node) {
  using A = WordNAdapter;
  A a(this);

  typename A::IntNBinopMatcher m(node);
1512 1513 1514
  if (m.right().Is(0)) return Replace(m.left().node());    // x | 0  => x
  if (m.right().Is(-1)) return Replace(m.right().node());  // x | -1 => -1
  if (m.IsFoldable()) {                                    // K | K  => K
1515
    return a.ReplaceIntN(m.left().Value() | m.right().Value());
1516 1517 1518
  }
  if (m.LeftEqualsRight()) return Replace(m.left().node());  // x | x => x

1519
  return a.TryMatchWordNRor(node);
1520 1521
}

1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537
Reduction MachineOperatorReducer::ReduceWord32Or(Node* node) {
  DCHECK_EQ(IrOpcode::kWord32Or, node->opcode());
  return ReduceWordNOr<Word32Adapter>(node);
}

Reduction MachineOperatorReducer::ReduceWord64Or(Node* node) {
  DCHECK_EQ(IrOpcode::kWord64Or, node->opcode());
  return ReduceWordNOr<Word64Adapter>(node);
}

template <typename WordNAdapter>
Reduction MachineOperatorReducer::ReduceWordNXor(Node* node) {
  using A = WordNAdapter;
  A a(this);

  typename A::IntNBinopMatcher m(node);
1538 1539
  if (m.right().Is(0)) return Replace(m.left().node());  // x ^ 0 => x
  if (m.IsFoldable()) {                                  // K ^ K => K
1540
    return a.ReplaceIntN(m.left().Value() ^ m.right().Value());
1541 1542
  }
  if (m.LeftEqualsRight()) return ReplaceInt32(0);  // x ^ x => 0
1543 1544
  if (A::IsWordNXor(m.left()) && m.right().Is(-1)) {
    typename A::IntNBinopMatcher mleft(m.left().node());
1545 1546 1547 1548 1549
    if (mleft.right().Is(-1)) {  // (x ^ -1) ^ -1 => x
      return Replace(mleft.left().node());
    }
  }

1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
  return a.TryMatchWordNRor(node);
}

Reduction MachineOperatorReducer::ReduceWord32Xor(Node* node) {
  DCHECK_EQ(IrOpcode::kWord32Xor, node->opcode());
  return ReduceWordNXor<Word32Adapter>(node);
}

Reduction MachineOperatorReducer::ReduceWord64Xor(Node* node) {
  DCHECK_EQ(IrOpcode::kWord64Xor, node->opcode());
  return ReduceWordNXor<Word64Adapter>(node);
1561
}
1562

1563 1564 1565 1566 1567 1568
Reduction MachineOperatorReducer::ReduceFloat64InsertLowWord32(Node* node) {
  DCHECK_EQ(IrOpcode::kFloat64InsertLowWord32, node->opcode());
  Float64Matcher mlhs(node->InputAt(0));
  Uint32Matcher mrhs(node->InputAt(1));
  if (mlhs.HasValue() && mrhs.HasValue()) {
    return ReplaceFloat64(bit_cast<double>(
1569
        (bit_cast<uint64_t>(mlhs.Value()) & uint64_t{0xFFFFFFFF00000000}) |
1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580
        mrhs.Value()));
  }
  return NoChange();
}

Reduction MachineOperatorReducer::ReduceFloat64InsertHighWord32(Node* node) {
  DCHECK_EQ(IrOpcode::kFloat64InsertHighWord32, node->opcode());
  Float64Matcher mlhs(node->InputAt(0));
  Uint32Matcher mrhs(node->InputAt(1));
  if (mlhs.HasValue() && mrhs.HasValue()) {
    return ReplaceFloat64(bit_cast<double>(
1581
        (bit_cast<uint64_t>(mlhs.Value()) & uint64_t{0xFFFFFFFF}) |
1582 1583 1584 1585 1586
        (static_cast<uint64_t>(mrhs.Value()) << 32)));
  }
  return NoChange();
}

1587 1588 1589 1590 1591
namespace {

bool IsFloat64RepresentableAsFloat32(const Float64Matcher& m) {
  if (m.HasValue()) {
    double v = m.Value();
1592
    return DoubleToFloat32(v) == v;
1593 1594 1595 1596 1597 1598 1599
  }
  return false;
}

}  // namespace


1600
Reduction MachineOperatorReducer::ReduceFloat64Compare(Node* node) {
1601 1602 1603
  DCHECK(IrOpcode::kFloat64Equal == node->opcode() ||
         IrOpcode::kFloat64LessThan == node->opcode() ||
         IrOpcode::kFloat64LessThanOrEqual == node->opcode());
1604
  Float64BinopMatcher m(node);
1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
  if (m.IsFoldable()) {
    switch (node->opcode()) {
      case IrOpcode::kFloat64Equal:
        return ReplaceBool(m.left().Value() == m.right().Value());
      case IrOpcode::kFloat64LessThan:
        return ReplaceBool(m.left().Value() < m.right().Value());
      case IrOpcode::kFloat64LessThanOrEqual:
        return ReplaceBool(m.left().Value() <= m.right().Value());
      default:
        UNREACHABLE();
    }
  } else if ((m.left().IsChangeFloat32ToFloat64() &&
              m.right().IsChangeFloat32ToFloat64()) ||
             (m.left().IsChangeFloat32ToFloat64() &&
              IsFloat64RepresentableAsFloat32(m.right())) ||
             (IsFloat64RepresentableAsFloat32(m.left()) &&
              m.right().IsChangeFloat32ToFloat64())) {
    // As all Float32 values have an exact representation in Float64, comparing
    // two Float64 values both converted from Float32 is equivalent to comparing
    // the original Float32s, so we can ignore the conversions. We can also
    // reduce comparisons of converted Float64 values against constants that
    // can be represented exactly as Float32.
1627 1628
    switch (node->opcode()) {
      case IrOpcode::kFloat64Equal:
1629
        NodeProperties::ChangeOp(node, machine()->Float32Equal());
1630 1631
        break;
      case IrOpcode::kFloat64LessThan:
1632
        NodeProperties::ChangeOp(node, machine()->Float32LessThan());
1633 1634
        break;
      case IrOpcode::kFloat64LessThanOrEqual:
1635
        NodeProperties::ChangeOp(node, machine()->Float32LessThanOrEqual());
1636 1637
        break;
      default:
1638
        UNREACHABLE();
1639
    }
1640 1641 1642 1643 1644 1645 1646 1647
    node->ReplaceInput(
        0, m.left().HasValue()
               ? Float32Constant(static_cast<float>(m.left().Value()))
               : m.left().InputAt(0));
    node->ReplaceInput(
        1, m.right().HasValue()
               ? Float32Constant(static_cast<float>(m.right().Value()))
               : m.right().InputAt(0));
1648 1649 1650 1651 1652
    return Changed(node);
  }
  return NoChange();
}

1653 1654 1655 1656
Reduction MachineOperatorReducer::ReduceFloat64RoundDown(Node* node) {
  DCHECK_EQ(IrOpcode::kFloat64RoundDown, node->opcode());
  Float64Matcher m(node->InputAt(0));
  if (m.HasValue()) {
1657
    return ReplaceFloat64(std::floor(m.Value()));
1658 1659 1660
  }
  return NoChange();
}
1661

1662
CommonOperatorBuilder* MachineOperatorReducer::common() const {
1663
  return mcgraph()->common();
1664
}
1665

1666
MachineOperatorBuilder* MachineOperatorReducer::machine() const {
1667
  return mcgraph()->machine();
1668 1669
}

1670
Graph* MachineOperatorReducer::graph() const { return mcgraph()->graph(); }
1671 1672 1673 1674

}  // namespace compiler
}  // namespace internal
}  // namespace v8