machine-operator-reducer.cc 29.7 KB
Newer Older
1 2 3 4 5 6
// 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"

7
#include "src/base/bits.h"
8
#include "src/base/division-by-constant.h"
9
#include "src/codegen.h"
10
#include "src/compiler/diamond.h"
11
#include "src/compiler/generic-node-inl.h"
12
#include "src/compiler/graph.h"
13
#include "src/compiler/js-graph.h"
14 15 16 17 18 19
#include "src/compiler/node-matchers.h"

namespace v8 {
namespace internal {
namespace compiler {

20
MachineOperatorReducer::MachineOperatorReducer(JSGraph* jsgraph)
21
    : jsgraph_(jsgraph) {}
22 23


24
MachineOperatorReducer::~MachineOperatorReducer() {}
25 26


27 28 29 30 31
Node* MachineOperatorReducer::Float32Constant(volatile float value) {
  return graph()->NewNode(common()->Float32Constant(value));
}


32
Node* MachineOperatorReducer::Float64Constant(volatile double value) {
33
  return jsgraph()->Float64Constant(value);
34 35 36
}


37
Node* MachineOperatorReducer::Int32Constant(int32_t value) {
38
  return jsgraph()->Int32Constant(value);
39 40 41
}


42
Node* MachineOperatorReducer::Int64Constant(int64_t value) {
43
  return graph()->NewNode(common()->Int64Constant(value));
44 45 46
}


47 48 49 50 51 52
Node* MachineOperatorReducer::Word32And(Node* lhs, uint32_t rhs) {
  return graph()->NewNode(machine()->Word32And(), lhs, Uint32Constant(rhs));
}


Node* MachineOperatorReducer::Word32Sar(Node* lhs, uint32_t rhs) {
53
  if (rhs == 0) return lhs;
54 55 56 57 58
  return graph()->NewNode(machine()->Word32Sar(), lhs, Uint32Constant(rhs));
}


Node* MachineOperatorReducer::Word32Shr(Node* lhs, uint32_t rhs) {
59
  if (rhs == 0) return lhs;
60 61 62 63
  return graph()->NewNode(machine()->Word32Shr(), lhs, Uint32Constant(rhs));
}


64 65 66 67 68
Node* MachineOperatorReducer::Word32Equal(Node* lhs, Node* rhs) {
  return graph()->NewNode(machine()->Word32Equal(), lhs, rhs);
}


69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
Node* MachineOperatorReducer::Int32Add(Node* lhs, Node* rhs) {
  return graph()->NewNode(machine()->Int32Add(), lhs, rhs);
}


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


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


84 85
Node* MachineOperatorReducer::Int32Div(Node* dividend, int32_t divisor) {
  DCHECK_NE(0, divisor);
86 87 88 89 90 91 92 93 94 95
  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);
  }
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
  return Int32Add(Word32Sar(quotient, mag.shift), Word32Shr(dividend, 31));
}


Node* MachineOperatorReducer::Uint32Div(Node* dividend, uint32_t divisor) {
  DCHECK_LT(0, divisor);
  base::MagicNumbersForDivision<uint32_t> const mag =
      base::UnsignedDivisionByConstant(bit_cast<uint32_t>(divisor));
  Node* quotient = graph()->NewNode(machine()->Uint32MulHigh(), dividend,
                                    Uint32Constant(mag.multiplier));
  if (mag.add) {
    DCHECK_LE(1, mag.shift);
    quotient = Word32Shr(
        Int32Add(Word32Shr(Int32Sub(dividend, quotient), 1), quotient),
        mag.shift - 1);
  } else {
    quotient = Word32Shr(quotient, mag.shift);
113
  }
114
  return quotient;
115 116 117
}


118 119 120
// Perform constant folding and strength reduction on machine operators.
Reduction MachineOperatorReducer::Reduce(Node* node) {
  switch (node->opcode()) {
121 122
    case IrOpcode::kProjection:
      return ReduceProjection(OpParameter<size_t>(node), node->InputAt(0));
123 124 125 126 127 128 129 130
    case IrOpcode::kWord32And: {
      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
        return ReplaceInt32(m.left().Value() & m.right().Value());
      }
      if (m.LeftEqualsRight()) return Replace(m.left().node());  // x & x => x
131 132 133 134 135 136 137 138 139
      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()));
          return Changed(node);
        }
      }
140 141 142 143 144 145 146 147 148 149
      break;
    }
    case IrOpcode::kWord32Or: {
      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
150 151 152 153 154 155 156 157 158
      if (m.left().IsWord32Shl() && m.right().IsWord32Shr()) {
        Int32BinopMatcher mleft(m.left().node());
        Int32BinopMatcher mright(m.right().node());
        if (mleft.left().node() == mright.left().node()) {
          // (x << y) | (x >> (32 - y)) => x ror y
          if (mright.right().IsInt32Sub()) {
            Int32BinopMatcher mrightright(mright.right().node());
            if (mrightright.left().Is(32) &&
                mrightright.right().node() == mleft.right().node()) {
159
              node->set_op(machine()->Word32Ror());
160 161 162 163 164 165 166 167
              node->ReplaceInput(0, mleft.left().node());
              node->ReplaceInput(1, mleft.right().node());
              return Changed(node);
            }
          }
          // (x << K) | (x >> (32 - K)) => x ror K
          if (mleft.right().IsInRange(0, 31) &&
              mright.right().Is(32 - mleft.right().Value())) {
168
            node->set_op(machine()->Word32Ror());
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
            node->ReplaceInput(0, mleft.left().node());
            node->ReplaceInput(1, mleft.right().node());
            return Changed(node);
          }
        }
      }
      if (m.left().IsWord32Shr() && m.right().IsWord32Shl()) {
        // (x >> (32 - y)) | (x << y)  => x ror y
        Int32BinopMatcher mleft(m.left().node());
        Int32BinopMatcher mright(m.right().node());
        if (mleft.left().node() == mright.left().node()) {
          if (mleft.right().IsInt32Sub()) {
            Int32BinopMatcher mleftright(mleft.right().node());
            if (mleftright.left().Is(32) &&
                mleftright.right().node() == mright.right().node()) {
184
              node->set_op(machine()->Word32Ror());
185 186 187 188 189 190 191 192
              node->ReplaceInput(0, mright.left().node());
              node->ReplaceInput(1, mright.right().node());
              return Changed(node);
            }
          }
          // (x >> (32 - K)) | (x << K) => x ror K
          if (mright.right().IsInRange(0, 31) &&
              mleft.right().Is(32 - mright.right().Value())) {
193
            node->set_op(machine()->Word32Ror());
194 195 196 197 198 199
            node->ReplaceInput(0, mright.left().node());
            node->ReplaceInput(1, mright.right().node());
            return Changed(node);
          }
        }
      }
200 201 202 203 204 205 206 207 208
      break;
    }
    case IrOpcode::kWord32Xor: {
      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
209 210 211 212 213 214
      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());
        }
      }
215 216 217 218 219 220 221 222
      break;
    }
    case IrOpcode::kWord32Shl: {
      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());
      }
223 224 225 226 227 228 229 230 231 232 233 234 235 236
      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->set_op(machine()->Word32And());
            node->ReplaceInput(0, mleft.left().node());
            node->ReplaceInput(
                1, Uint32Constant(~((1U << m.right().Value()) - 1U)));
            return Changed(node);
          }
        }
      }
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
      break;
    }
    case IrOpcode::kWord32Shr: {
      Uint32BinopMatcher 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());
      }
      break;
    }
    case IrOpcode::kWord32Sar: {
      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());
      }
      break;
    }
255 256 257 258 259 260 261 262 263
    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
        return ReplaceInt32(
            base::bits::RotateRight32(m.left().Value(), m.right().Value()));
      }
      break;
    }
264 265 266 267 268 269 270 271 272
    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());
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
        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());
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
        return Changed(node);
      }
      // TODO(turbofan): fold HeapConstant, ExternalReference, pointer compares
      if (m.LeftEqualsRight()) return ReplaceBool(true);  // x == x => true
      break;
    }
    case IrOpcode::kInt32Add: {
      Int32BinopMatcher m(node);
      if (m.right().Is(0)) return Replace(m.left().node());  // x + 0 => x
      if (m.IsFoldable()) {                                  // K + K => K
        return ReplaceInt32(static_cast<uint32_t>(m.left().Value()) +
                            static_cast<uint32_t>(m.right().Value()));
      }
      break;
    }
    case IrOpcode::kInt32Sub: {
      Int32BinopMatcher m(node);
      if (m.right().Is(0)) return Replace(m.left().node());  // x - 0 => x
      if (m.IsFoldable()) {                                  // K - K => K
        return ReplaceInt32(static_cast<uint32_t>(m.left().Value()) -
                            static_cast<uint32_t>(m.right().Value()));
      }
      if (m.LeftEqualsRight()) return ReplaceInt32(0);  // x - x => 0
      break;
    }
    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
        return ReplaceInt32(m.left().Value() * m.right().Value());
      }
      if (m.right().Is(-1)) {  // x * -1 => 0 - x
321
        node->set_op(machine()->Int32Sub());
322 323 324 325 326
        node->ReplaceInput(0, Int32Constant(0));
        node->ReplaceInput(1, m.left().node());
        return Changed(node);
      }
      if (m.right().IsPowerOf2()) {  // x * 2^n => x << n
327
        node->set_op(machine()->Word32Shl());
328 329 330 331 332
        node->ReplaceInput(1, Int32Constant(WhichPowerOf2(m.right().Value())));
        return Changed(node);
      }
      break;
    }
333 334
    case IrOpcode::kInt32Div:
      return ReduceInt32Div(node);
335 336
    case IrOpcode::kUint32Div:
      return ReduceUint32Div(node);
337 338
    case IrOpcode::kInt32Mod:
      return ReduceInt32Mod(node);
339 340
    case IrOpcode::kUint32Mod:
      return ReduceUint32Mod(node);
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
    case IrOpcode::kInt32LessThan: {
      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());
        return Changed(node);
      }
      if (m.left().Is(0) && m.right().IsInt32Sub()) {  // 0 < x - y => y < x
        Int32BinopMatcher msub(m.right().node());
        node->ReplaceInput(0, msub.right().node());
        node->ReplaceInput(1, msub.left().node());
        return Changed(node);
      }
      if (m.LeftEqualsRight()) return ReplaceBool(false);  // x < x => false
      break;
    }
    case IrOpcode::kInt32LessThanOrEqual: {
      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());
        return Changed(node);
      }
      if (m.left().Is(0) && m.right().IsInt32Sub()) {  // 0 <= x - y => y <= x
        Int32BinopMatcher msub(m.right().node());
        node->ReplaceInput(0, msub.right().node());
        node->ReplaceInput(1, msub.left().node());
        return Changed(node);
      }
      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
389 390 391
      if (m.left().IsWord32Sar() && m.right().HasValue()) {
        Int32BinopMatcher mleft(m.left().node());
        if (mleft.right().HasValue()) {
392
          // (x >> K) < C => x < (C << K)
393 394 395 396 397
          // when C < (M >> K)
          const uint32_t c = m.right().Value();
          const uint32_t k = mleft.right().Value() & 0x1f;
          if (c < static_cast<uint32_t>(kMaxInt >> k)) {
            node->ReplaceInput(0, mleft.left().node());
398
            node->ReplaceInput(1, Uint32Constant(c << k));
399 400
            return Changed(node);
          }
401
          // TODO(turbofan): else the comparison is always true.
402 403
        }
      }
404 405 406 407 408 409 410 411 412 413 414 415 416 417
      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;
    }
    case IrOpcode::kFloat64Add: {
      Float64BinopMatcher m(node);
418 419 420
      if (m.right().IsNaN()) {  // x + NaN => NaN
        return Replace(m.right().node());
      }
421 422 423 424 425 426 427
      if (m.IsFoldable()) {  // K + K => K
        return ReplaceFloat64(m.left().Value() + m.right().Value());
      }
      break;
    }
    case IrOpcode::kFloat64Sub: {
      Float64BinopMatcher m(node);
428 429 430 431 432 433 434 435 436
      if (m.right().Is(0) && (Double(m.right().Value()).Sign() > 0)) {
        return Replace(m.left().node());  // x - 0 => x
      }
      if (m.right().IsNaN()) {  // x - NaN => NaN
        return Replace(m.right().node());
      }
      if (m.left().IsNaN()) {  // NaN - x => NaN
        return Replace(m.left().node());
      }
437 438 439 440 441 442 443
      if (m.IsFoldable()) {  // K - K => K
        return ReplaceFloat64(m.left().Value() - m.right().Value());
      }
      break;
    }
    case IrOpcode::kFloat64Mul: {
      Float64BinopMatcher m(node);
444 445 446 447 448 449
      if (m.right().Is(-1)) {  // x * -1.0 => -0.0 - x
        node->set_op(machine()->Float64Sub());
        node->ReplaceInput(0, Float64Constant(-0.0));
        node->ReplaceInput(1, m.left().node());
        return Changed(node);
      }
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
      if (m.right().Is(1)) return Replace(m.left().node());  // x * 1.0 => x
      if (m.right().IsNaN()) {                               // x * NaN => NaN
        return Replace(m.right().node());
      }
      if (m.IsFoldable()) {  // K * K => K
        return ReplaceFloat64(m.left().Value() * m.right().Value());
      }
      break;
    }
    case IrOpcode::kFloat64Div: {
      Float64BinopMatcher m(node);
      if (m.right().Is(1)) return Replace(m.left().node());  // x / 1.0 => x
      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
        return ReplaceFloat64(m.left().Value() / m.right().Value());
      }
      break;
    }
    case IrOpcode::kFloat64Mod: {
      Float64BinopMatcher m(node);
475 476 477
      if (m.right().Is(0)) {  // x % 0 => NaN
        return ReplaceFloat64(base::OS::nan_value());
      }
478 479 480 481 482 483 484 485 486 487 488
      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
        return ReplaceFloat64(modulo(m.left().Value(), m.right().Value()));
      }
      break;
    }
489 490 491 492 493
    case IrOpcode::kChangeFloat32ToFloat64: {
      Float32Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(m.Value());
      break;
    }
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
    case IrOpcode::kChangeFloat64ToInt32: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceInt32(FastD2I(m.Value()));
      if (m.IsChangeInt32ToFloat64()) return Replace(m.node()->InputAt(0));
      break;
    }
    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;
    }
    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;
    }
526 527
    case IrOpcode::kTruncateFloat64ToInt32:
      return ReduceTruncateFloat64ToInt32(node);
528 529 530 531 532 533
    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;
    }
534 535 536 537 538 539
    case IrOpcode::kTruncateFloat64ToFloat32: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat32(DoubleToFloat32(m.Value()));
      if (m.IsChangeFloat32ToFloat64()) return Replace(m.node()->InputAt(0));
      break;
    }
540 541
    case IrOpcode::kStore:
      return ReduceStore(node);
542 543 544 545 546
    default:
      break;
  }
  return NoChange();
}
547 548


549 550
Reduction MachineOperatorReducer::ReduceInt32Div(Node* node) {
  Int32BinopMatcher m(node);
551
  if (m.left().Is(0)) return Replace(m.left().node());    // 0 / x => 0
552 553
  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
554 555 556 557 558 559 560
  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));
561 562 563 564 565
  }
  if (m.right().Is(-1)) {  // x / -1 => 0 - x
    node->set_op(machine()->Int32Sub());
    node->ReplaceInput(0, Int32Constant(0));
    node->ReplaceInput(1, m.left().node());
566
    node->TrimInputCount(2);
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
    return Changed(node);
  }
  if (m.right().HasValue()) {
    int32_t const divisor = m.right().Value();
    Node* const dividend = m.left().node();
    Node* quotient = dividend;
    if (base::bits::IsPowerOfTwo32(Abs(divisor))) {
      uint32_t const shift = WhichPowerOf2Abs(divisor);
      DCHECK_NE(0, shift);
      if (shift > 1) {
        quotient = Word32Sar(quotient, 31);
      }
      quotient = Int32Add(Word32Shr(quotient, 32u - shift), dividend);
      quotient = Word32Sar(quotient, shift);
    } else {
582
      quotient = Int32Div(quotient, Abs(divisor));
583 584 585 586 587
    }
    if (divisor < 0) {
      node->set_op(machine()->Int32Sub());
      node->ReplaceInput(0, Int32Constant(0));
      node->ReplaceInput(1, quotient);
588
      node->TrimInputCount(2);
589 590 591 592 593 594 595 596
      return Changed(node);
    }
    return Replace(quotient);
  }
  return NoChange();
}


597 598 599 600 601 602 603 604 605 606 607 608 609
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));
  }
610 611 612 613 614 615 616 617 618 619 620
  if (m.right().HasValue()) {
    Node* const dividend = m.left().node();
    uint32_t const divisor = m.right().Value();
    if (base::bits::IsPowerOfTwo32(divisor)) {  // x / 2^n => x >> n
      node->set_op(machine()->Word32Shr());
      node->ReplaceInput(1, Uint32Constant(WhichPowerOf2(m.right().Value())));
      node->TrimInputCount(2);
      return Changed(node);
    } else {
      return Replace(Uint32Div(dividend, divisor));
    }
621 622 623 624 625
  }
  return NoChange();
}


626
Reduction MachineOperatorReducer::ReduceInt32Mod(Node* node) {
627
  Int32BinopMatcher m(node);
628 629 630 631 632 633 634 635
  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()));
636
  }
637 638 639 640 641 642
  if (m.right().HasValue()) {
    Node* const dividend = m.left().node();
    int32_t const divisor = Abs(m.right().Value());
    if (base::bits::IsPowerOfTwo32(divisor)) {
      uint32_t const mask = divisor - 1;
      Node* const zero = Int32Constant(0);
643 644 645 646 647 648
      node->set_op(common()->Select(kMachInt32, BranchHint::kFalse));
      node->ReplaceInput(
          0, graph()->NewNode(machine()->Int32LessThan(), dividend, zero));
      node->ReplaceInput(
          1, Int32Sub(zero, Word32And(Int32Sub(zero, dividend), mask)));
      node->ReplaceInput(2, Word32And(dividend, mask));
649
    } else {
650
      Node* quotient = Int32Div(dividend, divisor);
651 652 653
      node->set_op(machine()->Int32Sub());
      DCHECK_EQ(dividend, node->InputAt(0));
      node->ReplaceInput(1, Int32Mul(quotient, Int32Constant(divisor)));
654
      node->TrimInputCount(2);
655
    }
656
    return Changed(node);
657 658 659 660 661
  }
  return NoChange();
}


662 663 664 665 666 667 668 669 670 671
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()));
  }
672 673 674 675 676 677 678 679 680 681 682 683
  if (m.right().HasValue()) {
    Node* const dividend = m.left().node();
    uint32_t const divisor = m.right().Value();
    if (base::bits::IsPowerOfTwo32(divisor)) {  // x % 2^n => x & 2^n-1
      node->set_op(machine()->Word32And());
      node->ReplaceInput(1, Uint32Constant(m.right().Value() - 1));
    } else {
      Node* quotient = Uint32Div(dividend, divisor);
      node->set_op(machine()->Int32Sub());
      DCHECK_EQ(dividend, node->InputAt(0));
      node->ReplaceInput(1, Int32Mul(quotient, Uint32Constant(divisor)));
    }
684
    node->TrimInputCount(2);
685 686 687 688 689 690
    return Changed(node);
  }
  return NoChange();
}


691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
Reduction MachineOperatorReducer::ReduceTruncateFloat64ToInt32(Node* node) {
  Float64Matcher m(node->InputAt(0));
  if (m.HasValue()) return ReplaceInt32(DoubleToInt32(m.Value()));
  if (m.IsChangeInt32ToFloat64()) return Replace(m.node()->InputAt(0));
  if (m.IsPhi()) {
    Node* const phi = m.node();
    DCHECK_EQ(kRepFloat64, RepresentationOf(OpParameter<MachineType>(phi)));
    if (phi->OwnedBy(node)) {
      // TruncateFloat64ToInt32(Phi[Float64](x1,...,xn))
      //   => Phi[Int32](TruncateFloat64ToInt32(x1),
      //                 ...,
      //                 TruncateFloat64ToInt32(xn))
      const int value_input_count = phi->InputCount() - 1;
      for (int i = 0; i < value_input_count; ++i) {
        Node* input = graph()->NewNode(machine()->TruncateFloat64ToInt32(),
                                       phi->InputAt(i));
        // TODO(bmeurer): Reschedule input for reduction once we have Revisit()
        // instead of recursing into ReduceTruncateFloat64ToInt32() here.
        Reduction reduction = ReduceTruncateFloat64ToInt32(input);
        if (reduction.Changed()) input = reduction.replacement();
        phi->ReplaceInput(i, input);
      }
      phi->set_op(common()->Phi(kMachInt32, value_input_count));
      return Replace(phi);
    }
  }
  return NoChange();
}


721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
Reduction MachineOperatorReducer::ReduceStore(Node* node) {
  MachineType const rep =
      RepresentationOf(StoreRepresentationOf(node->op()).machine_type());
  Node* const value = node->InputAt(2);
  switch (value->opcode()) {
    case IrOpcode::kWord32And: {
      Uint32BinopMatcher m(value);
      if (m.right().HasValue() &&
          ((rep == kRepWord8 && (m.right().Value() & 0xff) == 0xff) ||
           (rep == kRepWord16 && (m.right().Value() & 0xffff) == 0xffff))) {
        node->ReplaceInput(2, m.left().node());
        return Changed(node);
      }
      break;
    }
    case IrOpcode::kWord32Sar: {
      Int32BinopMatcher m(value);
      if (m.left().IsWord32Shl() &&
          ((rep == kRepWord8 && m.right().IsInRange(1, 24)) ||
           (rep == kRepWord16 && m.right().IsInRange(1, 16)))) {
        Int32BinopMatcher mleft(m.left().node());
        if (mleft.right().Is(m.right().Value())) {
          node->ReplaceInput(2, mleft.left().node());
          return Changed(node);
        }
      }
      break;
    }
    default:
      break;
  }
  return NoChange();
}


756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
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);
        return ReplaceInt32((index == 0) ? val : ovf);
      }
      if (m.right().Is(0)) {
        return (index == 0) ? Replace(m.left().node()) : ReplaceInt32(0);
      }
      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);
        return ReplaceInt32((index == 0) ? val : ovf);
      }
      if (m.right().Is(0)) {
        return (index == 0) ? Replace(m.left().node()) : ReplaceInt32(0);
      }
      break;
    }
    default:
      break;
  }
  return NoChange();
}


793 794
CommonOperatorBuilder* MachineOperatorReducer::common() const {
  return jsgraph()->common();
795
}
796 797


798 799 800 801 802
MachineOperatorBuilder* MachineOperatorReducer::machine() const {
  return jsgraph()->machine();
}


803 804 805 806 807
Graph* MachineOperatorReducer::graph() const { return jsgraph()->graph(); }

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