machine-operator-reducer.cc 55.4 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/conversions-inl.h"
18 19 20 21 22

namespace v8 {
namespace internal {
namespace compiler {

23 24
MachineOperatorReducer::MachineOperatorReducer(Editor* editor,
                                               MachineGraph* mcgraph,
25
                                               bool allow_signalling_nan)
26 27 28
    : AdvancedReducer(editor),
      mcgraph_(mcgraph),
      allow_signalling_nan_(allow_signalling_nan) {}
29

30
MachineOperatorReducer::~MachineOperatorReducer() = default;
31 32


33 34 35 36 37
Node* MachineOperatorReducer::Float32Constant(volatile float value) {
  return graph()->NewNode(common()->Float32Constant(value));
}


38
Node* MachineOperatorReducer::Float64Constant(volatile double value) {
39
  return mcgraph()->Float64Constant(value);
40 41 42
}


43
Node* MachineOperatorReducer::Int32Constant(int32_t value) {
44
  return mcgraph()->Int32Constant(value);
45 46 47
}


48
Node* MachineOperatorReducer::Int64Constant(int64_t value) {
49
  return graph()->NewNode(common()->Int64Constant(value));
50 51
}

52 53 54 55 56 57 58
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);
59 60 61 62 63 64
  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));
65
}
66

67 68 69 70
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;
71 72 73 74
}


Node* MachineOperatorReducer::Word32Sar(Node* lhs, uint32_t rhs) {
75
  if (rhs == 0) return lhs;
76 77 78 79 80
  return graph()->NewNode(machine()->Word32Sar(), lhs, Uint32Constant(rhs));
}


Node* MachineOperatorReducer::Word32Shr(Node* lhs, uint32_t rhs) {
81
  if (rhs == 0) return lhs;
82 83 84 85
  return graph()->NewNode(machine()->Word32Shr(), lhs, Uint32Constant(rhs));
}


86 87 88 89 90
Node* MachineOperatorReducer::Word32Equal(Node* lhs, Node* rhs) {
  return graph()->NewNode(machine()->Word32Equal(), lhs, rhs);
}


91
Node* MachineOperatorReducer::Int32Add(Node* lhs, Node* rhs) {
92 93 94
  Node* const node = graph()->NewNode(machine()->Int32Add(), lhs, rhs);
  Reduction const reduction = ReduceInt32Add(node);
  return reduction.Changed() ? reduction.replacement() : node;
95 96 97 98
}


Node* MachineOperatorReducer::Int32Sub(Node* lhs, Node* rhs) {
99 100 101
  Node* const node = graph()->NewNode(machine()->Int32Sub(), lhs, rhs);
  Reduction const reduction = ReduceInt32Sub(node);
  return reduction.Changed() ? reduction.replacement() : node;
102 103 104 105 106 107 108 109
}


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


110 111
Node* MachineOperatorReducer::Int32Div(Node* dividend, int32_t divisor) {
  DCHECK_NE(0, divisor);
112 113 114 115 116 117 118 119 120 121
  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);
  }
122 123 124 125 126
  return Int32Add(Word32Sar(quotient, mag.shift), Word32Shr(dividend, 31));
}


Node* MachineOperatorReducer::Uint32Div(Node* dividend, uint32_t divisor) {
127
  DCHECK_LT(0u, divisor);
128 129
  // If the divisor is even, we can avoid using the expensive fixup by shifting
  // the dividend upfront.
130
  unsigned const shift = base::bits::CountTrailingZeros(divisor);
131 132 133
  dividend = Word32Shr(dividend, shift);
  divisor >>= shift;
  // Compute the magic number for the (shifted) divisor.
134
  base::MagicNumbersForDivision<uint32_t> const mag =
135
      base::UnsignedDivisionByConstant(divisor, shift);
136 137 138
  Node* quotient = graph()->NewNode(machine()->Uint32MulHigh(), dividend,
                                    Uint32Constant(mag.multiplier));
  if (mag.add) {
139
    DCHECK_LE(1u, mag.shift);
140 141 142 143 144
    quotient = Word32Shr(
        Int32Add(Word32Shr(Int32Sub(dividend, quotient), 1), quotient),
        mag.shift - 1);
  } else {
    quotient = Word32Shr(quotient, mag.shift);
145
  }
146
  return quotient;
147 148 149
}


150 151 152
// Perform constant folding and strength reduction on machine operators.
Reduction MachineOperatorReducer::Reduce(Node* node) {
  switch (node->opcode()) {
153
    case IrOpcode::kProjection:
154
      return ReduceProjection(ProjectionIndexOf(node->op()), node->InputAt(0));
155 156
    case IrOpcode::kWord32And:
      return ReduceWord32And(node);
157 158
    case IrOpcode::kWord32Or:
      return ReduceWord32Or(node);
159 160
    case IrOpcode::kWord32Xor:
      return ReduceWord32Xor(node);
161 162
    case IrOpcode::kWord32Shl:
      return ReduceWord32Shl(node);
163 164
    case IrOpcode::kWord64Shl:
      return ReduceWord64Shl(node);
165 166
    case IrOpcode::kWord32Shr:
      return ReduceWord32Shr(node);
167 168
    case IrOpcode::kWord64Shr:
      return ReduceWord64Shr(node);
169 170
    case IrOpcode::kWord32Sar:
      return ReduceWord32Sar(node);
171 172
    case IrOpcode::kWord64Sar:
      return ReduceWord64Sar(node);
173 174 175 176
    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
177 178
        return ReplaceInt32(base::bits::RotateRight32(m.left().Value(),
                                                      m.right().Value() & 31));
179 180 181
      }
      break;
    }
182 183 184 185 186 187 188 189 190
    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());
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
        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());
206 207 208 209 210 211
        return Changed(node);
      }
      // TODO(turbofan): fold HeapConstant, ExternalReference, pointer compares
      if (m.LeftEqualsRight()) return ReplaceBool(true);  // x == x => true
      break;
    }
212 213
    case IrOpcode::kInt32Add:
      return ReduceInt32Add(node);
214 215
    case IrOpcode::kInt64Add:
      return ReduceInt64Add(node);
216 217
    case IrOpcode::kInt32Sub:
      return ReduceInt32Sub(node);
218 219
    case IrOpcode::kInt64Sub:
      return ReduceInt64Sub(node);
220 221 222 223 224
    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
225 226
        return ReplaceInt32(
            base::MulWithWraparound(m.left().Value(), m.right().Value()));
227 228 229 230
      }
      if (m.right().Is(-1)) {  // x * -1 => 0 - x
        node->ReplaceInput(0, Int32Constant(0));
        node->ReplaceInput(1, m.left().node());
231
        NodeProperties::ChangeOp(node, machine()->Int32Sub());
232 233 234 235
        return Changed(node);
      }
      if (m.right().IsPowerOf2()) {  // x * 2^n => x << n
        node->ReplaceInput(1, Int32Constant(WhichPowerOf2(m.right().Value())));
236
        NodeProperties::ChangeOp(node, machine()->Word32Shl());
237 238
        Reduction reduction = ReduceWord32Shl(node);
        return reduction.Changed() ? reduction : Changed(node);
239 240 241
      }
      break;
    }
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    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;
    }
257 258
    case IrOpcode::kInt32Div:
      return ReduceInt32Div(node);
259 260
    case IrOpcode::kUint32Div:
      return ReduceUint32Div(node);
261 262
    case IrOpcode::kInt32Mod:
      return ReduceInt32Mod(node);
263 264
    case IrOpcode::kUint32Mod:
      return ReduceUint32Mod(node);
265 266 267 268 269 270
    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
271 272 273 274 275 276 277 278
      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);
        }
      }
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
      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
297 298 299
      if (m.left().IsWord32Sar() && m.right().HasValue()) {
        Int32BinopMatcher mleft(m.left().node());
        if (mleft.right().HasValue()) {
300
          // (x >> K) < C => x < (C << K)
301 302
          // when C < (M >> K)
          const uint32_t c = m.right().Value();
303
          const uint32_t k = mleft.right().Value() & 0x1F;
304 305
          if (c < static_cast<uint32_t>(kMaxInt >> k)) {
            node->ReplaceInput(0, mleft.left().node());
306
            node->ReplaceInput(1, Uint32Constant(c << k));
307 308
            return Changed(node);
          }
309
          // TODO(turbofan): else the comparison is always true.
310 311
        }
      }
312 313 314 315 316 317 318 319 320 321 322 323
      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;
    }
324 325
    case IrOpcode::kFloat32Sub: {
      Float32BinopMatcher m(node);
326
      if (allow_signalling_nan_ && m.right().Is(0) &&
327
          (std::copysign(1.0, m.right().Value()) > 0)) {
328 329
        return Replace(m.left().node());  // x - 0 => x
      }
330
      if (m.right().IsNaN()) {  // x - NaN => NaN
331 332
        // Do some calculation to make a signalling NaN quiet.
        return ReplaceFloat32(m.right().Value() - m.right().Value());
333 334
      }
      if (m.left().IsNaN()) {  // NaN - x => NaN
335 336
        // Do some calculation to make a signalling NaN quiet.
        return ReplaceFloat32(m.left().Value() - m.left().Value());
337 338 339 340
      }
      if (m.IsFoldable()) {  // L - R => (L - R)
        return ReplaceFloat32(m.left().Value() - m.right().Value());
      }
341
      if (allow_signalling_nan_ && m.left().IsMinusZero()) {
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
        // -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;
    }
360 361 362 363 364 365 366 367 368
    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);
369 370 371 372
      if (allow_signalling_nan_ && m.right().Is(0) &&
          (Double(m.right().Value()).Sign() > 0)) {
        return Replace(m.left().node());  // x - 0 => x
      }
373
      if (m.right().IsNaN()) {  // x - NaN => NaN
374 375
        // Do some calculation to make a signalling NaN quiet.
        return ReplaceFloat64(m.right().Value() - m.right().Value());
376 377
      }
      if (m.left().IsNaN()) {  // NaN - x => NaN
378 379
        // Do some calculation to make a signalling NaN quiet.
        return ReplaceFloat64(m.left().Value() - m.left().Value());
380
      }
381
      if (m.IsFoldable()) {  // L - R => (L - R)
382 383
        return ReplaceFloat64(m.left().Value() - m.right().Value());
      }
384
      if (allow_signalling_nan_ && m.left().IsMinusZero()) {
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
        // -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);
      }
401 402 403 404
      break;
    }
    case IrOpcode::kFloat64Mul: {
      Float64BinopMatcher m(node);
405 406
      if (allow_signalling_nan_ && m.right().Is(1))
        return Replace(m.left().node());  // x * 1.0 => x
407 408 409
      if (m.right().Is(-1)) {  // x * -1.0 => -0.0 - x
        node->ReplaceInput(0, Float64Constant(-0.0));
        node->ReplaceInput(1, m.left().node());
410
        NodeProperties::ChangeOp(node, machine()->Float64Sub());
411 412
        return Changed(node);
      }
413
      if (m.right().IsNaN()) {                               // x * NaN => NaN
414 415
        // Do some calculation to make a signalling NaN quiet.
        return ReplaceFloat64(m.right().Value() - m.right().Value());
416 417 418 419
      }
      if (m.IsFoldable()) {  // K * K => K
        return ReplaceFloat64(m.left().Value() * m.right().Value());
      }
420 421 422 423 424
      if (m.right().Is(2)) {  // x * 2.0 => x + x
        node->ReplaceInput(1, m.left().node());
        NodeProperties::ChangeOp(node, machine()->Float64Add());
        return Changed(node);
      }
425
      break;
426 427 428
    }
    case IrOpcode::kFloat64Div: {
      Float64BinopMatcher m(node);
429 430
      if (allow_signalling_nan_ && m.right().Is(1))
        return Replace(m.left().node());  // x / 1.0 => x
431
      // TODO(ahaas): We could do x / 1.0 = x if we knew that x is not an sNaN.
432
      if (m.right().IsNaN()) {                               // x / NaN => NaN
433 434
        // Do some calculation to make a signalling NaN quiet.
        return ReplaceFloat64(m.right().Value() - m.right().Value());
435 436
      }
      if (m.left().IsNaN()) {  // NaN / x => NaN
437 438
        // Do some calculation to make a signalling NaN quiet.
        return ReplaceFloat64(m.left().Value() - m.left().Value());
439 440
      }
      if (m.IsFoldable()) {  // K / K => K
441 442
        return ReplaceFloat64(
            base::Divide(m.left().Value(), m.right().Value()));
443
      }
444
      if (allow_signalling_nan_ && m.right().Is(-1)) {  // x / -1.0 => -x
445 446 447 448 449 450 451 452 453 454 455 456
        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);
      }
457
      break;
458 459 460
    }
    case IrOpcode::kFloat64Mod: {
      Float64BinopMatcher m(node);
461
      if (m.right().Is(0)) {  // x % 0 => NaN
462
        return ReplaceFloat64(std::numeric_limits<double>::quiet_NaN());
463
      }
464 465 466 467 468 469 470
      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
471
        return ReplaceFloat64(Modulo(m.left().Value(), m.right().Value()));
472 473 474
      }
      break;
    }
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
    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;
    }
495 496 497 498 499
    case IrOpcode::kFloat64Atan: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(base::ieee754::atan(m.Value()));
      break;
    }
500 501 502 503 504
    case IrOpcode::kFloat64Atanh: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(base::ieee754::atanh(m.Value()));
      break;
    }
505 506 507 508 509 510 511 512 513 514 515 516 517 518
    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;
    }
519 520 521 522 523
    case IrOpcode::kFloat64Cbrt: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(base::ieee754::cbrt(m.Value()));
      break;
    }
524 525 526 527 528
    case IrOpcode::kFloat64Cos: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(base::ieee754::cos(m.Value()));
      break;
    }
529 530 531 532 533
    case IrOpcode::kFloat64Cosh: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(base::ieee754::cosh(m.Value()));
      break;
    }
534 535 536 537 538
    case IrOpcode::kFloat64Exp: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(base::ieee754::exp(m.Value()));
      break;
    }
539 540 541 542 543
    case IrOpcode::kFloat64Expm1: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(base::ieee754::expm1(m.Value()));
      break;
    }
544 545
    case IrOpcode::kFloat64Log: {
      Float64Matcher m(node->InputAt(0));
546
      if (m.HasValue()) return ReplaceFloat64(base::ieee754::log(m.Value()));
547 548
      break;
    }
549 550 551 552 553
    case IrOpcode::kFloat64Log1p: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(base::ieee754::log1p(m.Value()));
      break;
    }
554 555 556 557 558
    case IrOpcode::kFloat64Log10: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(base::ieee754::log10(m.Value()));
      break;
    }
559
    case IrOpcode::kFloat64Log2: {
560
      Float64Matcher m(node->InputAt(0));
561 562 563 564 565
      if (m.HasValue()) return ReplaceFloat64(base::ieee754::log2(m.Value()));
      break;
    }
    case IrOpcode::kFloat64Pow: {
      Float64BinopMatcher m(node);
566
      if (m.IsFoldable()) {
567 568
        return ReplaceFloat64(
            base::ieee754::pow(m.left().Value(), m.right().Value()));
569
      } else if (m.right().Is(0.0)) {  // x ** +-0.0 => 1.0
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
        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()));
      }
590 591
      break;
    }
592 593 594 595 596
    case IrOpcode::kFloat64Sin: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(base::ieee754::sin(m.Value()));
      break;
    }
597 598 599 600 601
    case IrOpcode::kFloat64Sinh: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(base::ieee754::sinh(m.Value()));
      break;
    }
602 603 604 605 606
    case IrOpcode::kFloat64Tan: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(base::ieee754::tan(m.Value()));
      break;
    }
607 608 609 610 611
    case IrOpcode::kFloat64Tanh: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(base::ieee754::tanh(m.Value()));
      break;
    }
612 613
    case IrOpcode::kChangeFloat32ToFloat64: {
      Float32Matcher m(node->InputAt(0));
614 615 616 617 618 619 620
      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());
      }
621 622
      break;
    }
623 624
    case IrOpcode::kChangeFloat64ToInt32: {
      Float64Matcher m(node->InputAt(0));
625
      if (m.HasValue()) return ReplaceInt32(FastD2IChecked(m.Value()));
626 627 628
      if (m.IsChangeInt32ToFloat64()) return Replace(m.node()->InputAt(0));
      break;
    }
629 630 631
    case IrOpcode::kChangeFloat64ToInt64: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceInt64(static_cast<int64_t>(m.Value()));
632
      if (m.IsChangeInt64ToFloat64()) return Replace(m.node()->InputAt(0));
633 634
      break;
    }
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
    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;
    }
    case IrOpcode::kChangeInt32ToInt64: {
      Int32Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceInt64(m.Value());
      break;
    }
651 652 653 654 655 656
    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;
    }
657 658 659 660 661 662 663 664 665 666
    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;
    }
667 668 669 670 671 672
    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();
    }
673 674 675 676 677 678
    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;
    }
679 680
    case IrOpcode::kTruncateFloat64ToFloat32: {
      Float64Matcher m(node->InputAt(0));
681 682 683 684 685 686 687 688 689
      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));
690 691
      break;
    }
692 693
    case IrOpcode::kRoundFloat64ToInt32: {
      Float64Matcher m(node->InputAt(0));
694 695 696
      if (m.HasValue()) {
        return ReplaceInt32(DoubleToInt32(m.Value()));
      }
697 698 699
      if (m.IsChangeInt32ToFloat64()) return Replace(m.node()->InputAt(0));
      break;
    }
700 701 702 703
    case IrOpcode::kFloat64InsertLowWord32:
      return ReduceFloat64InsertLowWord32(node);
    case IrOpcode::kFloat64InsertHighWord32:
      return ReduceFloat64InsertHighWord32(node);
704
    case IrOpcode::kStore:
705
    case IrOpcode::kUnalignedStore:
706
      return ReduceStore(node);
707 708 709 710
    case IrOpcode::kFloat64Equal:
    case IrOpcode::kFloat64LessThan:
    case IrOpcode::kFloat64LessThanOrEqual:
      return ReduceFloat64Compare(node);
711 712
    case IrOpcode::kFloat64RoundDown:
      return ReduceFloat64RoundDown(node);
713 714 715 716 717 718 719 720
    case IrOpcode::kBitcastTaggedToWord: {
      NodeMatcher m(node->InputAt(0));
      if (m.IsBitcastWordToTaggedSigned()) {
        RelaxEffectsAndControls(node);
        return Replace(m.InputAt(0));
      }
      break;
    }
721 722 723 724 725
    default:
      break;
  }
  return NoChange();
}
726

727 728 729 730 731
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
732 733
    return ReplaceInt32(
        base::AddWithWraparound(m.left().Value(), m.right().Value()));
734
  }
735 736 737 738 739
  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());
740
      NodeProperties::ChangeOp(node, machine()->Int32Sub());
741 742 743 744 745 746 747 748
      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());
749
      NodeProperties::ChangeOp(node, machine()->Int32Sub());
750 751 752 753
      Reduction const reduction = ReduceInt32Sub(node);
      return reduction.Changed() ? reduction : Changed(node);
    }
  }
754 755 756
  return NoChange();
}

757 758 759 760 761
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()) {
762 763
    return ReplaceInt64(
        base::AddWithWraparound(m.left().Value(), m.right().Value()));
764 765 766
  }
  return NoChange();
}
767

768 769 770 771 772
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
773 774
    return ReplaceInt32(
        base::SubWithWraparound(m.left().Value(), m.right().Value()));
775 776 777
  }
  if (m.LeftEqualsRight()) return ReplaceInt32(0);  // x - x => 0
  if (m.right().HasValue()) {                       // x - K => x + -K
778 779
    node->ReplaceInput(
        1, Int32Constant(base::NegateWithWraparound(m.right().Value())));
780
    NodeProperties::ChangeOp(node, machine()->Int32Add());
781 782 783 784 785 786
    Reduction const reduction = ReduceInt32Add(node);
    return reduction.Changed() ? reduction : Changed(node);
  }
  return NoChange();
}

787 788 789 790 791
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
792 793
    return ReplaceInt64(
        base::SubWithWraparound(m.left().Value(), m.right().Value()));
794 795 796
  }
  if (m.LeftEqualsRight()) return Replace(Int64Constant(0));  // x - x => 0
  if (m.right().HasValue()) {                                 // x - K => x + -K
797 798
    node->ReplaceInput(
        1, Int64Constant(base::NegateWithWraparound(m.right().Value())));
799 800 801 802 803 804
    NodeProperties::ChangeOp(node, machine()->Int64Add());
    Reduction const reduction = ReduceInt64Add(node);
    return reduction.Changed() ? reduction : Changed(node);
  }
  return NoChange();
}
805

806 807
Reduction MachineOperatorReducer::ReduceInt32Div(Node* node) {
  Int32BinopMatcher m(node);
808
  if (m.left().Is(0)) return Replace(m.left().node());    // 0 / x => 0
809 810
  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
811 812 813 814 815 816 817
  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));
818 819 820 821
  }
  if (m.right().Is(-1)) {  // x / -1 => 0 - x
    node->ReplaceInput(0, Int32Constant(0));
    node->ReplaceInput(1, m.left().node());
822
    node->TrimInputCount(2);
823
    NodeProperties::ChangeOp(node, machine()->Int32Sub());
824 825 826 827 828 829
    return Changed(node);
  }
  if (m.right().HasValue()) {
    int32_t const divisor = m.right().Value();
    Node* const dividend = m.left().node();
    Node* quotient = dividend;
830 831
    if (base::bits::IsPowerOfTwo(Abs(divisor))) {
      uint32_t const shift = WhichPowerOf2(Abs(divisor));
832
      DCHECK_NE(0u, shift);
833 834 835 836 837 838
      if (shift > 1) {
        quotient = Word32Sar(quotient, 31);
      }
      quotient = Int32Add(Word32Shr(quotient, 32u - shift), dividend);
      quotient = Word32Sar(quotient, shift);
    } else {
839
      quotient = Int32Div(quotient, Abs(divisor));
840 841 842 843
    }
    if (divisor < 0) {
      node->ReplaceInput(0, Int32Constant(0));
      node->ReplaceInput(1, quotient);
844
      node->TrimInputCount(2);
845
      NodeProperties::ChangeOp(node, machine()->Int32Sub());
846 847 848 849 850 851 852 853
      return Changed(node);
    }
    return Replace(quotient);
  }
  return NoChange();
}


854 855 856 857 858 859 860 861 862 863 864 865 866
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));
  }
867 868 869
  if (m.right().HasValue()) {
    Node* const dividend = m.left().node();
    uint32_t const divisor = m.right().Value();
870
    if (base::bits::IsPowerOfTwo(divisor)) {  // x / 2^n => x >> n
871 872
      node->ReplaceInput(1, Uint32Constant(WhichPowerOf2(m.right().Value())));
      node->TrimInputCount(2);
873
      NodeProperties::ChangeOp(node, machine()->Word32Shr());
874 875 876 877
      return Changed(node);
    } else {
      return Replace(Uint32Div(dividend, divisor));
    }
878 879 880 881 882
  }
  return NoChange();
}


883
Reduction MachineOperatorReducer::ReduceInt32Mod(Node* node) {
884
  Int32BinopMatcher m(node);
885 886 887 888 889 890 891 892
  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()));
893
  }
894 895
  if (m.right().HasValue()) {
    Node* const dividend = m.left().node();
896 897
    uint32_t const divisor = Abs(m.right().Value());
    if (base::bits::IsPowerOfTwo(divisor)) {
898 899
      uint32_t const mask = divisor - 1;
      Node* const zero = Int32Constant(0);
900 901 902 903 904 905 906
      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)));
907
    } else {
908
      Node* quotient = Int32Div(dividend, divisor);
909 910
      DCHECK_EQ(dividend, node->InputAt(0));
      node->ReplaceInput(1, Int32Mul(quotient, Int32Constant(divisor)));
911
      node->TrimInputCount(2);
912
      NodeProperties::ChangeOp(node, machine()->Int32Sub());
913
    }
914
    return Changed(node);
915 916 917 918 919
  }
  return NoChange();
}


920 921 922 923 924 925 926 927 928 929
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()));
  }
930 931 932
  if (m.right().HasValue()) {
    Node* const dividend = m.left().node();
    uint32_t const divisor = m.right().Value();
933
    if (base::bits::IsPowerOfTwo(divisor)) {  // x % 2^n => x & 2^n-1
934
      node->ReplaceInput(1, Uint32Constant(m.right().Value() - 1));
935 936
      node->TrimInputCount(2);
      NodeProperties::ChangeOp(node, machine()->Word32And());
937 938 939 940
    } else {
      Node* quotient = Uint32Div(dividend, divisor);
      DCHECK_EQ(dividend, node->InputAt(0));
      node->ReplaceInput(1, Int32Mul(quotient, Uint32Constant(divisor)));
941 942
      node->TrimInputCount(2);
      NodeProperties::ChangeOp(node, machine()->Int32Sub());
943
    }
944 945 946 947 948 949
    return Changed(node);
  }
  return NoChange();
}


950
Reduction MachineOperatorReducer::ReduceStore(Node* node) {
951 952 953
  NodeMatcher nm(node);
  MachineRepresentation rep;
  int value_input;
954
  if (nm.IsStore()) {
955 956
    rep = StoreRepresentationOf(node->op()).representation();
    value_input = 2;
957 958 959 960
  } else {
    DCHECK(nm.IsUnalignedStore());
    rep = UnalignedStoreRepresentationOf(node->op());
    value_input = 2;
961 962 963 964
  }

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

965 966 967
  switch (value->opcode()) {
    case IrOpcode::kWord32And: {
      Uint32BinopMatcher m(value);
968
      if (m.right().HasValue() && ((rep == MachineRepresentation::kWord8 &&
969
                                    (m.right().Value() & 0xFF) == 0xFF) ||
970
                                   (rep == MachineRepresentation::kWord16 &&
971
                                    (m.right().Value() & 0xFFFF) == 0xFFFF))) {
972
        node->ReplaceInput(value_input, m.left().node());
973 974 975 976 977 978
        return Changed(node);
      }
      break;
    }
    case IrOpcode::kWord32Sar: {
      Int32BinopMatcher m(value);
979 980 981 982
      if (m.left().IsWord32Shl() && ((rep == MachineRepresentation::kWord8 &&
                                      m.right().IsInRange(1, 24)) ||
                                     (rep == MachineRepresentation::kWord16 &&
                                      m.right().IsInRange(1, 16)))) {
983 984
        Int32BinopMatcher mleft(m.left().node());
        if (mleft.right().Is(m.right().Value())) {
985
          node->ReplaceInput(value_input, mleft.left().node());
986 987 988 989 990 991 992 993 994 995 996 997
          return Changed(node);
        }
      }
      break;
    }
    default:
      break;
  }
  return NoChange();
}


998 999 1000 1001 1002 1003 1004 1005 1006
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);
1007
        return ReplaceInt32(index == 0 ? val : ovf);
1008 1009
      }
      if (m.right().Is(0)) {
1010
        return Replace(index == 0 ? m.left().node() : m.right().node());
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
      }
      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);
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
        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);
1036 1037
      }
      if (m.right().Is(0)) {
1038 1039 1040 1041
        return Replace(m.right().node());
      }
      if (m.right().Is(1)) {
        return index == 0 ? Replace(m.left().node()) : ReplaceInt32(0);
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
      }
      break;
    }
    default:
      break;
  }
  return NoChange();
}


1052 1053 1054 1055 1056
Reduction MachineOperatorReducer::ReduceWord32Shifts(Node* node) {
  DCHECK((node->opcode() == IrOpcode::kWord32Shl) ||
         (node->opcode() == IrOpcode::kWord32Shr) ||
         (node->opcode() == IrOpcode::kWord32Sar));
  if (machine()->Word32ShiftIsSafe()) {
1057
    // Remove the explicit 'and' with 0x1F if the shift provided by the machine
1058 1059 1060 1061
    // instruction matches that required by JavaScript.
    Int32BinopMatcher m(node);
    if (m.right().IsWord32And()) {
      Int32BinopMatcher mright(m.right().node());
1062
      if (mright.right().Is(0x1F)) {
1063 1064 1065 1066 1067 1068 1069 1070 1071
        node->ReplaceInput(1, mright.left().node());
        return Changed(node);
      }
    }
  }
  return NoChange();
}


1072 1073 1074 1075 1076
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
1077 1078
    return ReplaceInt32(
        base::ShlWithWraparound(m.left().Value(), m.right().Value()));
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
  }
  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)));
1089
        NodeProperties::ChangeOp(node, machine()->Word32And());
1090 1091 1092 1093 1094 1095 1096 1097
        Reduction reduction = ReduceWord32And(node);
        return reduction.Changed() ? reduction : Changed(node);
      }
    }
  }
  return ReduceWord32Shifts(node);
}

1098 1099 1100 1101 1102
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
1103 1104
    return ReplaceInt64(
        base::ShlWithWraparound(m.left().Value(), m.right().Value()));
1105 1106 1107 1108
  }
  return NoChange();
}

1109 1110 1111 1112
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
1113
    return ReplaceInt32(m.left().Value() >> (m.right().Value() & 31));
1114 1115 1116 1117
  }
  if (m.left().IsWord32And() && m.right().HasValue()) {
    Uint32BinopMatcher mleft(m.left().node());
    if (mleft.right().HasValue()) {
1118
      uint32_t shift = m.right().Value() & 31;
1119 1120 1121 1122 1123 1124 1125 1126 1127
      uint32_t mask = mleft.right().Value();
      if ((mask >> shift) == 0) {
        // (m >>> s) == 0 implies ((x & m) >>> s) == 0
        return ReplaceInt32(0);
      }
    }
  }
  return ReduceWord32Shifts(node);
}
1128

1129 1130 1131 1132 1133
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
1134
    return ReplaceInt64(m.left().Value() >> (m.right().Value() & 63));
1135 1136 1137 1138
  }
  return NoChange();
}

1139 1140 1141 1142
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
1143
    return ReplaceInt32(m.left().Value() >> (m.right().Value() & 31));
1144 1145 1146 1147 1148 1149 1150 1151
  }
  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());
1152
        NodeProperties::ChangeOp(node, machine()->Int32Sub());
1153 1154 1155 1156 1157
        Reduction const reduction = ReduceInt32Sub(node);
        return reduction.Changed() ? reduction : Changed(node);
      }
    } else if (mleft.left().IsLoad()) {
      LoadRepresentation const rep =
1158 1159 1160
          LoadRepresentationOf(mleft.left().node()->op());
      if (m.right().Is(24) && mleft.right().Is(24) &&
          rep == MachineType::Int8()) {
1161 1162 1163
        // Load[kMachInt8] << 24 >> 24 => Load[kMachInt8]
        return Replace(mleft.left().node());
      }
1164 1165
      if (m.right().Is(16) && mleft.right().Is(16) &&
          rep == MachineType::Int16()) {
1166 1167 1168 1169 1170 1171 1172 1173
        // Load[kMachInt16] << 16 >> 16 => Load[kMachInt8]
        return Replace(mleft.left().node());
      }
    }
  }
  return ReduceWord32Shifts(node);
}

1174 1175 1176 1177
Reduction MachineOperatorReducer::ReduceWord64Sar(Node* node) {
  Int64BinopMatcher m(node);
  if (m.right().Is(0)) return Replace(m.left().node());  // x >> 0 => x
  if (m.IsFoldable()) {
1178
    return ReplaceInt64(m.left().Value() >> (m.right().Value() & 63));
1179 1180 1181
  }
  return NoChange();
}
1182

1183 1184 1185 1186 1187
Reduction MachineOperatorReducer::ReduceWord32And(Node* node) {
  DCHECK_EQ(IrOpcode::kWord32And, node->opcode());
  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
1188 1189 1190
  if (m.left().IsComparison() && m.right().Is(1)) {       // CMP & 1 => CMP
    return Replace(m.left().node());
  }
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
  if (m.IsFoldable()) {                                   // K & K  => K
    return ReplaceInt32(m.left().Value() & m.right().Value());
  }
  if (m.LeftEqualsRight()) return Replace(m.left().node());  // x & x => x
  if (m.left().IsWord32And() && m.right().HasValue()) {
    Int32BinopMatcher mleft(m.left().node());
    if (mleft.right().HasValue()) {  // (x & K) & K => x & K
      node->ReplaceInput(0, mleft.left().node());
      node->ReplaceInput(
          1, Int32Constant(m.right().Value() & mleft.right().Value()));
1201
      Reduction const reduction = ReduceWord32And(node);
1202 1203 1204
      return reduction.Changed() ? reduction : Changed(node);
    }
  }
1205 1206
  if (m.right().IsNegativePowerOf2()) {
    int32_t const mask = m.right().Value();
1207
    int32_t const neg_mask = base::NegateWithWraparound(mask);
1208 1209 1210
    if (m.left().IsWord32Shl()) {
      Uint32BinopMatcher mleft(m.left().node());
      if (mleft.right().HasValue() &&
1211
          (mleft.right().Value() & 0x1F) >=
1212
              base::bits::CountTrailingZeros(mask)) {
1213
        // (x << L) & (-1 << K) => x << L iff L >= K
1214 1215 1216 1217 1218 1219 1220
        return Replace(mleft.node());
      }
    } else if (m.left().IsInt32Add()) {
      Int32BinopMatcher mleft(m.left().node());
      if (mleft.right().HasValue() &&
          (mleft.right().Value() & mask) == mleft.right().Value()) {
        // (x + (K << L)) & (-1 << L) => (x & (-1 << L)) + (K << L)
1221
        node->ReplaceInput(0, Word32And(mleft.left().node(), m.right().node()));
1222
        node->ReplaceInput(1, mleft.right().node());
1223
        NodeProperties::ChangeOp(node, machine()->Int32Add());
1224 1225 1226
        Reduction const reduction = ReduceInt32Add(node);
        return reduction.Changed() ? reduction : Changed(node);
      }
1227 1228
      if (mleft.left().IsInt32Mul()) {
        Int32BinopMatcher mleftleft(mleft.left().node());
1229
        if (mleftleft.right().IsMultipleOf(neg_mask)) {
1230 1231 1232 1233
          // (y * (K << L) + x) & (-1 << L) => (x & (-1 << L)) + y * (K << L)
          node->ReplaceInput(0,
                             Word32And(mleft.right().node(), m.right().node()));
          node->ReplaceInput(1, mleftleft.node());
1234
          NodeProperties::ChangeOp(node, machine()->Int32Add());
1235 1236 1237
          Reduction const reduction = ReduceInt32Add(node);
          return reduction.Changed() ? reduction : Changed(node);
        }
1238
      }
1239 1240
      if (mleft.right().IsInt32Mul()) {
        Int32BinopMatcher mleftright(mleft.right().node());
1241
        if (mleftright.right().IsMultipleOf(neg_mask)) {
1242 1243 1244 1245
          // (x + y * (K << L)) & (-1 << L) => (x & (-1 << L)) + y * (K << L)
          node->ReplaceInput(0,
                             Word32And(mleft.left().node(), m.right().node()));
          node->ReplaceInput(1, mleftright.node());
1246
          NodeProperties::ChangeOp(node, machine()->Int32Add());
1247 1248 1249 1250 1251 1252
          Reduction const reduction = ReduceInt32Add(node);
          return reduction.Changed() ? reduction : Changed(node);
        }
      }
      if (mleft.left().IsWord32Shl()) {
        Int32BinopMatcher mleftleft(mleft.left().node());
1253
        if (mleftleft.right().Is(base::bits::CountTrailingZeros(mask))) {
1254 1255 1256 1257
          // (y << L + x) & (-1 << L) => (x & (-1 << L)) + y << L
          node->ReplaceInput(0,
                             Word32And(mleft.right().node(), m.right().node()));
          node->ReplaceInput(1, mleftleft.node());
1258
          NodeProperties::ChangeOp(node, machine()->Int32Add());
1259 1260 1261 1262 1263 1264
          Reduction const reduction = ReduceInt32Add(node);
          return reduction.Changed() ? reduction : Changed(node);
        }
      }
      if (mleft.right().IsWord32Shl()) {
        Int32BinopMatcher mleftright(mleft.right().node());
1265
        if (mleftright.right().Is(base::bits::CountTrailingZeros(mask))) {
1266 1267 1268 1269
          // (x + y << L) & (-1 << L) => (x & (-1 << L)) + y << L
          node->ReplaceInput(0,
                             Word32And(mleft.left().node(), m.right().node()));
          node->ReplaceInput(1, mleftright.node());
1270
          NodeProperties::ChangeOp(node, machine()->Int32Add());
1271 1272 1273
          Reduction const reduction = ReduceInt32Add(node);
          return reduction.Changed() ? reduction : Changed(node);
        }
1274
      }
1275 1276
    } else if (m.left().IsInt32Mul()) {
      Int32BinopMatcher mleft(m.left().node());
1277
      if (mleft.right().IsMultipleOf(neg_mask)) {
1278 1279 1280
        // (x * (K << L)) & (-1 << L) => x * (K << L)
        return Replace(mleft.node());
      }
1281
    }
1282 1283 1284 1285
  }
  return NoChange();
}

1286 1287 1288
Reduction MachineOperatorReducer::TryMatchWord32Ror(Node* node) {
  DCHECK(IrOpcode::kWord32Or == node->opcode() ||
         IrOpcode::kWord32Xor == node->opcode());
1289
  Int32BinopMatcher m(node);
1290 1291
  Node* shl = nullptr;
  Node* shr = nullptr;
1292
  // Recognize rotation, we are matching:
1293 1294
  //  * x << y | x >>> (32 - y) => x ror (32 - y), i.e  x rol y
  //  * x << (32 - y) | x >>> y => x ror y
1295 1296
  //  * x << y ^ x >>> (32 - y) => x ror (32 - y), i.e. x rol y
  //  * x << (32 - y) ^ x >>> y => x ror y
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
  // 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 {
1316 1317
    Node* sub = nullptr;
    Node* y = nullptr;
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
    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());
1334
  NodeProperties::ChangeOp(node, machine()->Word32Ror());
1335 1336 1337
  return Changed(node);
}

1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
Reduction MachineOperatorReducer::ReduceWord32Or(Node* node) {
  DCHECK_EQ(IrOpcode::kWord32Or, node->opcode());
  Int32BinopMatcher m(node);
  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
    return ReplaceInt32(m.left().Value() | m.right().Value());
  }
  if (m.LeftEqualsRight()) return Replace(m.left().node());  // x | x => x

  return TryMatchWord32Ror(node);
}

Reduction MachineOperatorReducer::ReduceWord32Xor(Node* node) {
  DCHECK_EQ(IrOpcode::kWord32Xor, node->opcode());
  Int32BinopMatcher m(node);
  if (m.right().Is(0)) return Replace(m.left().node());  // x ^ 0 => x
  if (m.IsFoldable()) {                                  // K ^ K => K
    return ReplaceInt32(m.left().Value() ^ m.right().Value());
  }
  if (m.LeftEqualsRight()) return ReplaceInt32(0);  // x ^ x => 0
  if (m.left().IsWord32Xor() && m.right().Is(-1)) {
    Int32BinopMatcher mleft(m.left().node());
    if (mleft.right().Is(-1)) {  // (x ^ -1) ^ -1 => x
      return Replace(mleft.left().node());
    }
  }

  return TryMatchWord32Ror(node);
}
1368

1369 1370 1371 1372 1373 1374
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>(
1375
        (bit_cast<uint64_t>(mlhs.Value()) & uint64_t{0xFFFFFFFF00000000}) |
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
        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>(
1388
        (bit_cast<uint64_t>(mlhs.Value()) & uint64_t{0xFFFFFFFF}) |
1389 1390 1391 1392 1393 1394
        (static_cast<uint64_t>(mrhs.Value()) << 32)));
  }
  return NoChange();
}


1395 1396 1397 1398 1399
namespace {

bool IsFloat64RepresentableAsFloat32(const Float64Matcher& m) {
  if (m.HasValue()) {
    double v = m.Value();
1400
    return DoubleToFloat32(v) == v;
1401 1402 1403 1404 1405 1406 1407
  }
  return false;
}

}  // namespace


1408
Reduction MachineOperatorReducer::ReduceFloat64Compare(Node* node) {
1409 1410 1411
  DCHECK(IrOpcode::kFloat64Equal == node->opcode() ||
         IrOpcode::kFloat64LessThan == node->opcode() ||
         IrOpcode::kFloat64LessThanOrEqual == node->opcode());
1412
  Float64BinopMatcher m(node);
1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
  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.
1435 1436
    switch (node->opcode()) {
      case IrOpcode::kFloat64Equal:
1437
        NodeProperties::ChangeOp(node, machine()->Float32Equal());
1438 1439
        break;
      case IrOpcode::kFloat64LessThan:
1440
        NodeProperties::ChangeOp(node, machine()->Float32LessThan());
1441 1442
        break;
      case IrOpcode::kFloat64LessThanOrEqual:
1443
        NodeProperties::ChangeOp(node, machine()->Float32LessThanOrEqual());
1444 1445
        break;
      default:
1446
        UNREACHABLE();
1447
    }
1448 1449 1450 1451 1452 1453 1454 1455
    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));
1456 1457 1458 1459 1460
    return Changed(node);
  }
  return NoChange();
}

1461 1462 1463 1464
Reduction MachineOperatorReducer::ReduceFloat64RoundDown(Node* node) {
  DCHECK_EQ(IrOpcode::kFloat64RoundDown, node->opcode());
  Float64Matcher m(node->InputAt(0));
  if (m.HasValue()) {
1465
    return ReplaceFloat64(std::floor(m.Value()));
1466 1467 1468
  }
  return NoChange();
}
1469

1470
CommonOperatorBuilder* MachineOperatorReducer::common() const {
1471
  return mcgraph()->common();
1472
}
1473 1474


1475
MachineOperatorBuilder* MachineOperatorReducer::machine() const {
1476
  return mcgraph()->machine();
1477 1478
}

1479
Graph* MachineOperatorReducer::graph() const { return mcgraph()->graph(); }
1480 1481 1482 1483

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