instruction-selector-mips64.cc 123 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/base/bits.h"
6
#include "src/base/platform/wrappers.h"
7
#include "src/codegen/machine-type.h"
8
#include "src/compiler/backend/instruction-selector-impl.h"
9
#include "src/compiler/node-matchers.h"
10
#include "src/compiler/node-properties.h"
11 12 13 14 15 16 17 18 19 20 21

namespace v8 {
namespace internal {
namespace compiler {

#define TRACE_UNIMPL() \
  PrintF("UNIMPLEMENTED instr_sel: %s at line %d\n", __FUNCTION__, __LINE__)

#define TRACE() PrintF("instr_sel: %s at line %d\n", __FUNCTION__, __LINE__)

// Adds Mips-specific methods for generating InstructionOperands.
22
class Mips64OperandGenerator final : public OperandGenerator {
23 24 25 26
 public:
  explicit Mips64OperandGenerator(InstructionSelector* selector)
      : OperandGenerator(selector) {}

27
  InstructionOperand UseOperand(Node* node, InstructionCode opcode) {
28 29 30 31 32 33
    if (CanBeImmediate(node, opcode)) {
      return UseImmediate(node);
    }
    return UseRegister(node);
  }

34 35 36 37 38
  // Use the zero register if the node has the immediate value zero, otherwise
  // assign a register.
  InstructionOperand UseRegisterOrImmediateZero(Node* node) {
    if ((IsIntegerConstant(node) && (GetIntegerConstantValue(node) == 0)) ||
        (IsFloatConstant(node) &&
39
         (bit_cast<int64_t>(GetFloatConstantValue(node)) == 0))) {
40 41 42 43 44
      return UseImmediate(node);
    }
    return UseRegister(node);
  }

45 46 47 48 49 50 51
  bool IsIntegerConstant(Node* node) {
    return (node->opcode() == IrOpcode::kInt32Constant) ||
           (node->opcode() == IrOpcode::kInt64Constant);
  }

  int64_t GetIntegerConstantValue(Node* node) {
    if (node->opcode() == IrOpcode::kInt32Constant) {
52
      return OpParameter<int32_t>(node->op());
53
    }
54
    DCHECK_EQ(IrOpcode::kInt64Constant, node->opcode());
55
    return OpParameter<int64_t>(node->op());
56 57
  }

58 59 60 61 62 63 64
  bool IsFloatConstant(Node* node) {
    return (node->opcode() == IrOpcode::kFloat32Constant) ||
           (node->opcode() == IrOpcode::kFloat64Constant);
  }

  double GetFloatConstantValue(Node* node) {
    if (node->opcode() == IrOpcode::kFloat32Constant) {
65
      return OpParameter<float>(node->op());
66 67
    }
    DCHECK_EQ(IrOpcode::kFloat64Constant, node->opcode());
68
    return OpParameter<double>(node->op());
69 70
  }

71 72 73 74 75 76
  bool CanBeImmediate(Node* node, InstructionCode mode) {
    return IsIntegerConstant(node) &&
           CanBeImmediate(GetIntegerConstantValue(node), mode);
  }

  bool CanBeImmediate(int64_t value, InstructionCode opcode) {
77 78 79 80 81 82 83 84 85
    switch (ArchOpcodeField::decode(opcode)) {
      case kMips64Shl:
      case kMips64Sar:
      case kMips64Shr:
        return is_uint5(value);
      case kMips64Dshl:
      case kMips64Dsar:
      case kMips64Dshr:
        return is_uint6(value);
86 87 88 89 90 91
      case kMips64Add:
      case kMips64And32:
      case kMips64And:
      case kMips64Dadd:
      case kMips64Or32:
      case kMips64Or:
92
      case kMips64Tst:
93 94
      case kMips64Xor:
        return is_uint16(value);
95 96 97 98 99 100 101 102 103 104 105 106
      case kMips64Lb:
      case kMips64Lbu:
      case kMips64Sb:
      case kMips64Lh:
      case kMips64Lhu:
      case kMips64Sh:
      case kMips64Lw:
      case kMips64Sw:
      case kMips64Ld:
      case kMips64Sd:
      case kMips64Lwc1:
      case kMips64Swc1:
107 108
      case kMips64Ldc1:
      case kMips64Sdc1:
109
        return is_int32(value);
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
      default:
        return is_int16(value);
    }
  }

 private:
  bool ImmediateFitsAddrMode1Instruction(int32_t imm) const {
    TRACE_UNIMPL();
    return false;
  }
};

static void VisitRR(InstructionSelector* selector, ArchOpcode opcode,
                    Node* node) {
  Mips64OperandGenerator g(selector);
  selector->Emit(opcode, g.DefineAsRegister(node),
                 g.UseRegister(node->InputAt(0)));
}

129 130 131
static void VisitRRI(InstructionSelector* selector, ArchOpcode opcode,
                     Node* node) {
  Mips64OperandGenerator g(selector);
132
  int32_t imm = OpParameter<int32_t>(node->op());
133 134 135 136
  selector->Emit(opcode, g.DefineAsRegister(node),
                 g.UseRegister(node->InputAt(0)), g.UseImmediate(imm));
}

137 138 139 140 141 142 143 144 145 146 147 148 149 150
static void VisitSimdShift(InstructionSelector* selector, ArchOpcode opcode,
                           Node* node) {
  Mips64OperandGenerator g(selector);
  if (g.IsIntegerConstant(node->InputAt(1))) {
    selector->Emit(opcode, g.DefineAsRegister(node),
                   g.UseRegister(node->InputAt(0)),
                   g.UseImmediate(node->InputAt(1)));
  } else {
    selector->Emit(opcode, g.DefineAsRegister(node),
                   g.UseRegister(node->InputAt(0)),
                   g.UseRegister(node->InputAt(1)));
  }
}

151 152 153
static void VisitRRIR(InstructionSelector* selector, ArchOpcode opcode,
                      Node* node) {
  Mips64OperandGenerator g(selector);
154
  int32_t imm = OpParameter<int32_t>(node->op());
155 156 157 158
  selector->Emit(opcode, g.DefineAsRegister(node),
                 g.UseRegister(node->InputAt(0)), g.UseImmediate(imm),
                 g.UseRegister(node->InputAt(1)));
}
159 160 161 162 163 164 165 166 167

static void VisitRRR(InstructionSelector* selector, ArchOpcode opcode,
                     Node* node) {
  Mips64OperandGenerator g(selector);
  selector->Emit(opcode, g.DefineAsRegister(node),
                 g.UseRegister(node->InputAt(0)),
                 g.UseRegister(node->InputAt(1)));
}

168 169 170 171 172 173 174 175
static void VisitUniqueRRR(InstructionSelector* selector, ArchOpcode opcode,
                           Node* node) {
  Mips64OperandGenerator g(selector);
  selector->Emit(opcode, g.DefineAsRegister(node),
                 g.UseUniqueRegister(node->InputAt(0)),
                 g.UseUniqueRegister(node->InputAt(1)));
}

176 177 178 179 180 181
void VisitRRRR(InstructionSelector* selector, ArchOpcode opcode, Node* node) {
  Mips64OperandGenerator g(selector);
  selector->Emit(
      opcode, g.DefineSameAsFirst(node), g.UseRegister(node->InputAt(0)),
      g.UseRegister(node->InputAt(1)), g.UseRegister(node->InputAt(2)));
}
182 183 184 185 186 187 188 189 190

static void VisitRRO(InstructionSelector* selector, ArchOpcode opcode,
                     Node* node) {
  Mips64OperandGenerator g(selector);
  selector->Emit(opcode, g.DefineAsRegister(node),
                 g.UseRegister(node->InputAt(0)),
                 g.UseOperand(node->InputAt(1), opcode));
}

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
struct ExtendingLoadMatcher {
  ExtendingLoadMatcher(Node* node, InstructionSelector* selector)
      : matches_(false), selector_(selector), base_(nullptr), immediate_(0) {
    Initialize(node);
  }

  bool Matches() const { return matches_; }

  Node* base() const {
    DCHECK(Matches());
    return base_;
  }
  int64_t immediate() const {
    DCHECK(Matches());
    return immediate_;
  }
  ArchOpcode opcode() const {
    DCHECK(Matches());
    return opcode_;
  }

 private:
  bool matches_;
  InstructionSelector* selector_;
  Node* base_;
  int64_t immediate_;
  ArchOpcode opcode_;

  void Initialize(Node* node) {
    Int64BinopMatcher m(node);
    // When loading a 64-bit value and shifting by 32, we should
    // just load and sign-extend the interesting 4 bytes instead.
    // This happens, for example, when we're loading and untagging SMIs.
    DCHECK(m.IsWord64Sar());
    if (m.left().IsLoad() && m.right().Is(32) &&
        selector_->CanCover(m.node(), m.left().node())) {
227 228
      DCHECK_EQ(selector_->GetEffectLevel(node),
                selector_->GetEffectLevel(m.left().node()));
229 230
      MachineRepresentation rep =
          LoadRepresentationOf(m.left().node()->op()).representation();
231
      DCHECK_EQ(3, ElementSizeLog2Of(rep));
232 233 234 235 236 237 238
      if (rep != MachineRepresentation::kTaggedSigned &&
          rep != MachineRepresentation::kTaggedPointer &&
          rep != MachineRepresentation::kTagged &&
          rep != MachineRepresentation::kWord64) {
        return;
      }

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
      Mips64OperandGenerator g(selector_);
      Node* load = m.left().node();
      Node* offset = load->InputAt(1);
      base_ = load->InputAt(0);
      opcode_ = kMips64Lw;
      if (g.CanBeImmediate(offset, opcode_)) {
#if defined(V8_TARGET_LITTLE_ENDIAN)
        immediate_ = g.GetIntegerConstantValue(offset) + 4;
#elif defined(V8_TARGET_BIG_ENDIAN)
        immediate_ = g.GetIntegerConstantValue(offset);
#endif
        matches_ = g.CanBeImmediate(immediate_, kMips64Lw);
      }
    }
  }
};

256 257
bool TryEmitExtendingLoad(InstructionSelector* selector, Node* node,
                          Node* output_node) {
258 259 260 261 262 263 264 265 266
  ExtendingLoadMatcher m(node, selector);
  Mips64OperandGenerator g(selector);
  if (m.Matches()) {
    InstructionOperand inputs[2];
    inputs[0] = g.UseRegister(m.base());
    InstructionCode opcode =
        m.opcode() | AddressingModeField::encode(kMode_MRI);
    DCHECK(is_int32(m.immediate()));
    inputs[1] = g.TempImmediate(static_cast<int32_t>(m.immediate()));
267
    InstructionOperand outputs[] = {g.DefineAsRegister(output_node)};
268 269 270 271 272 273
    selector->Emit(opcode, arraysize(outputs), outputs, arraysize(inputs),
                   inputs);
    return true;
  }
  return false;
}
274

275 276 277 278 279 280 281 282 283 284 285 286 287
bool TryMatchImmediate(InstructionSelector* selector,
                       InstructionCode* opcode_return, Node* node,
                       size_t* input_count_return, InstructionOperand* inputs) {
  Mips64OperandGenerator g(selector);
  if (g.CanBeImmediate(node, *opcode_return)) {
    *opcode_return |= AddressingModeField::encode(kMode_MRI);
    inputs[0] = g.UseImmediate(node);
    *input_count_return = 1;
    return true;
  }
  return false;
}

288
static void VisitBinop(InstructionSelector* selector, Node* node,
289 290 291
                       InstructionCode opcode, bool has_reverse_opcode,
                       InstructionCode reverse_opcode,
                       FlagsContinuation* cont) {
292 293
  Mips64OperandGenerator g(selector);
  Int32BinopMatcher m(node);
294
  InstructionOperand inputs[2];
295
  size_t input_count = 0;
296
  InstructionOperand outputs[1];
297 298
  size_t output_count = 0;

299 300 301 302
  if (TryMatchImmediate(selector, &opcode, m.right().node(), &input_count,
                        &inputs[1])) {
    inputs[0] = g.UseRegister(m.left().node());
    input_count++;
303 304 305
  } else if (has_reverse_opcode &&
             TryMatchImmediate(selector, &reverse_opcode, m.left().node(),
                               &input_count, &inputs[1])) {
306 307 308 309 310 311 312
    inputs[0] = g.UseRegister(m.right().node());
    opcode = reverse_opcode;
    input_count++;
  } else {
    inputs[input_count++] = g.UseRegister(m.left().node());
    inputs[input_count++] = g.UseOperand(m.right().node(), opcode);
  }
313

314 315 316 317 318 319 320 321
  if (cont->IsDeoptimize()) {
    // If we can deoptimize as a result of the binop, we need to make sure that
    // the deopt inputs are not overwritten by the binop result. One way
    // to achieve that is to declare the output register as same-as-first.
    outputs[output_count++] = g.DefineSameAsFirst(node);
  } else {
    outputs[output_count++] = g.DefineAsRegister(node);
  }
322

323
  DCHECK_NE(0u, input_count);
324
  DCHECK_EQ(1u, output_count);
325 326 327
  DCHECK_GE(arraysize(inputs), input_count);
  DCHECK_GE(arraysize(outputs), output_count);

328 329
  selector->EmitWithContinuation(opcode, output_count, outputs, input_count,
                                 inputs, cont);
330 331
}

332 333 334 335 336 337 338 339 340 341 342
static void VisitBinop(InstructionSelector* selector, Node* node,
                       InstructionCode opcode, bool has_reverse_opcode,
                       InstructionCode reverse_opcode) {
  FlagsContinuation cont;
  VisitBinop(selector, node, opcode, has_reverse_opcode, reverse_opcode, &cont);
}

static void VisitBinop(InstructionSelector* selector, Node* node,
                       InstructionCode opcode, FlagsContinuation* cont) {
  VisitBinop(selector, node, opcode, false, kArchNop, cont);
}
343 344 345

static void VisitBinop(InstructionSelector* selector, Node* node,
                       InstructionCode opcode) {
346
  VisitBinop(selector, node, opcode, false, kArchNop);
347 348
}

349 350 351 352 353 354 355
void InstructionSelector::VisitStackSlot(Node* node) {
  StackSlotRepresentation rep = StackSlotRepresentationOf(node->op());
  int alignment = rep.alignment();
  int slot = frame_->AllocateSpillSlot(rep.size(), alignment);
  OperandGenerator g(this);

  Emit(kArchStackSlot, g.DefineAsRegister(node),
356
       sequence()->AddImmediate(Constant(slot)), 0, nullptr);
357 358
}

359 360 361
void InstructionSelector::VisitAbortCSAAssert(Node* node) {
  Mips64OperandGenerator g(this);
  Emit(kArchAbortCSAAssert, g.NoOutput(), g.UseFixed(node->InputAt(0), a0));
362 363
}

364 365 366 367 368 369 370 371 372 373 374 375 376
void EmitLoad(InstructionSelector* selector, Node* node, InstructionCode opcode,
              Node* output = nullptr) {
  Mips64OperandGenerator g(selector);
  Node* base = node->InputAt(0);
  Node* index = node->InputAt(1);

  if (g.CanBeImmediate(index, opcode)) {
    selector->Emit(opcode | AddressingModeField::encode(kMode_MRI),
                   g.DefineAsRegister(output == nullptr ? node : output),
                   g.UseRegister(base), g.UseImmediate(index));
  } else {
    InstructionOperand addr_reg = g.TempRegister();
    selector->Emit(kMips64Dadd | AddressingModeField::encode(kMode_None),
377
                   addr_reg, g.UseRegister(base), g.UseRegister(index));
378 379 380 381 382 383
    // Emit desired load opcode, using temp addr_reg.
    selector->Emit(opcode | AddressingModeField::encode(kMode_MRI),
                   g.DefineAsRegister(output == nullptr ? node : output),
                   addr_reg, g.TempImmediate(0));
  }
}
384

385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
namespace {
InstructionOperand EmitAddBeforeS128LoadStore(InstructionSelector* selector,
                                              Node* node,
                                              InstructionCode* opcode) {
  Mips64OperandGenerator g(selector);
  Node* base = node->InputAt(0);
  Node* index = node->InputAt(1);
  InstructionOperand addr_reg = g.TempRegister();
  selector->Emit(kMips64Dadd | AddressingModeField::encode(kMode_None),
                 addr_reg, g.UseRegister(base), g.UseRegister(index));
  *opcode |= AddressingModeField::encode(kMode_MRI);
  return addr_reg;
}

}  // namespace

void InstructionSelector::VisitStoreLane(Node* node) {
  StoreLaneParameters params = StoreLaneParametersOf(node->op());
403
  LoadStoreLaneParams f(params.rep, params.laneidx);
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
  InstructionCode opcode = kMips64S128StoreLane;
  opcode |= MiscField::encode(f.sz);

  Mips64OperandGenerator g(this);
  InstructionOperand addr = EmitAddBeforeS128LoadStore(this, node, &opcode);
  InstructionOperand inputs[4] = {
      g.UseRegister(node->InputAt(2)),
      g.UseImmediate(f.laneidx),
      addr,
      g.TempImmediate(0),
  };
  Emit(opcode, 0, nullptr, 4, inputs);
}

void InstructionSelector::VisitLoadLane(Node* node) {
  LoadLaneParameters params = LoadLaneParametersOf(node->op());
420
  LoadStoreLaneParams f(params.rep.representation(), params.laneidx);
421 422 423 424 425 426 427 428 429
  InstructionCode opcode = kMips64S128LoadLane;
  opcode |= MiscField::encode(f.sz);

  Mips64OperandGenerator g(this);
  InstructionOperand addr = EmitAddBeforeS128LoadStore(this, node, &opcode);
  Emit(opcode, g.DefineSameAsFirst(node), g.UseRegister(node->InputAt(2)),
       g.UseImmediate(f.laneidx), addr, g.TempImmediate(0));
}

430 431 432 433 434
void InstructionSelector::VisitLoadTransform(Node* node) {
  LoadTransformParameters params = LoadTransformParametersOf(node->op());

  InstructionCode opcode = kArchNop;
  switch (params.transformation) {
435
    case LoadTransformation::kS128Load8Splat:
436 437
      opcode = kMips64S128LoadSplat;
      opcode |= MiscField::encode(MSASize::MSA_B);
438
      break;
439
    case LoadTransformation::kS128Load16Splat:
440 441
      opcode = kMips64S128LoadSplat;
      opcode |= MiscField::encode(MSASize::MSA_H);
442
      break;
443
    case LoadTransformation::kS128Load32Splat:
444 445
      opcode = kMips64S128LoadSplat;
      opcode |= MiscField::encode(MSASize::MSA_W);
446
      break;
447
    case LoadTransformation::kS128Load64Splat:
448 449
      opcode = kMips64S128LoadSplat;
      opcode |= MiscField::encode(MSASize::MSA_D);
450
      break;
451 452
    case LoadTransformation::kS128Load8x8S:
      opcode = kMips64S128Load8x8S;
453
      break;
454 455
    case LoadTransformation::kS128Load8x8U:
      opcode = kMips64S128Load8x8U;
456
      break;
457 458
    case LoadTransformation::kS128Load16x4S:
      opcode = kMips64S128Load16x4S;
459
      break;
460 461
    case LoadTransformation::kS128Load16x4U:
      opcode = kMips64S128Load16x4U;
462
      break;
463 464
    case LoadTransformation::kS128Load32x2S:
      opcode = kMips64S128Load32x2S;
465
      break;
466 467
    case LoadTransformation::kS128Load32x2U:
      opcode = kMips64S128Load32x2U;
468
      break;
469 470 471 472 473 474
    case LoadTransformation::kS128Load32Zero:
      opcode = kMips64S128Load32Zero;
      break;
    case LoadTransformation::kS128Load64Zero:
      opcode = kMips64S128Load64Zero;
      break;
475 476 477 478 479 480 481
    default:
      UNIMPLEMENTED();
  }

  EmitLoad(this, node, opcode);
}

482
void InstructionSelector::VisitLoad(Node* node) {
483
  LoadRepresentation load_rep = LoadRepresentationOf(node->op());
484

485
  InstructionCode opcode = kArchNop;
486 487
  switch (load_rep.representation()) {
    case MachineRepresentation::kFloat32:
488 489
      opcode = kMips64Lwc1;
      break;
490
    case MachineRepresentation::kFloat64:
491 492
      opcode = kMips64Ldc1;
      break;
493 494 495
    case MachineRepresentation::kBit:  // Fall through.
    case MachineRepresentation::kWord8:
      opcode = load_rep.IsUnsigned() ? kMips64Lbu : kMips64Lb;
496
      break;
497 498
    case MachineRepresentation::kWord16:
      opcode = load_rep.IsUnsigned() ? kMips64Lhu : kMips64Lh;
499
      break;
500
    case MachineRepresentation::kWord32:
501
      opcode = load_rep.IsUnsigned() ? kMips64Lwu : kMips64Lw;
502
      break;
503 504
    case MachineRepresentation::kTaggedSigned:   // Fall through.
    case MachineRepresentation::kTaggedPointer:  // Fall through.
505
    case MachineRepresentation::kTagged:         // Fall through.
506
    case MachineRepresentation::kWord64:
507 508
      opcode = kMips64Ld;
      break;
509 510 511
    case MachineRepresentation::kSimd128:
      opcode = kMips64MsaLd;
      break;
512 513
    case MachineRepresentation::kCompressedPointer:  // Fall through.
    case MachineRepresentation::kCompressed:         // Fall through.
514
    case MachineRepresentation::kMapWord:            // Fall through.
515
    case MachineRepresentation::kNone:
516 517
      UNREACHABLE();
  }
518
  if (node->opcode() == IrOpcode::kPoisonedLoad) {
519
    CHECK_NE(poisoning_level_, PoisoningMitigationLevel::kDontPoison);
520
    opcode |= AccessModeField::encode(kMemoryAccessPoisoned);
521
  }
522

523
  EmitLoad(this, node, opcode);
524 525
}

526 527
void InstructionSelector::VisitPoisonedLoad(Node* node) { VisitLoad(node); }

528 529 530 531
void InstructionSelector::VisitProtectedLoad(Node* node) {
  // TODO(eholk)
  UNIMPLEMENTED();
}
532 533 534 535 536 537 538

void InstructionSelector::VisitStore(Node* node) {
  Mips64OperandGenerator g(this);
  Node* base = node->InputAt(0);
  Node* index = node->InputAt(1);
  Node* value = node->InputAt(2);

539
  StoreRepresentation store_rep = StoreRepresentationOf(node->op());
540
  WriteBarrierKind write_barrier_kind = store_rep.write_barrier_kind();
541
  MachineRepresentation rep = store_rep.representation();
542

543 544 545 546
  if (FLAG_enable_unconditional_write_barriers && CanBeTaggedPointer(rep)) {
    write_barrier_kind = kFullWriteBarrier;
  }

547
  // TODO(mips): I guess this could be done in a better way.
548
  if (write_barrier_kind != kNoWriteBarrier && !FLAG_disable_write_barriers) {
549
    DCHECK(CanBeTaggedPointer(rep));
550 551 552 553
    InstructionOperand inputs[3];
    size_t input_count = 0;
    inputs[input_count++] = g.UseUniqueRegister(base);
    inputs[input_count++] = g.UseUniqueRegister(index);
554
    inputs[input_count++] = g.UseUniqueRegister(value);
555 556
    RecordWriteMode record_write_mode =
        WriteBarrierKindToRecordWriteMode(write_barrier_kind);
557 558 559 560 561
    InstructionOperand temps[] = {g.TempRegister(), g.TempRegister()};
    size_t const temp_count = arraysize(temps);
    InstructionCode code = kArchStoreWithWriteBarrier;
    code |= MiscField::encode(static_cast<int>(record_write_mode));
    Emit(code, 0, nullptr, input_count, inputs, temp_count, temps);
562
  } else {
563
    ArchOpcode opcode;
564
    switch (rep) {
565
      case MachineRepresentation::kFloat32:
566 567
        opcode = kMips64Swc1;
        break;
568
      case MachineRepresentation::kFloat64:
569 570
        opcode = kMips64Sdc1;
        break;
571 572
      case MachineRepresentation::kBit:  // Fall through.
      case MachineRepresentation::kWord8:
573 574
        opcode = kMips64Sb;
        break;
575
      case MachineRepresentation::kWord16:
576 577
        opcode = kMips64Sh;
        break;
578
      case MachineRepresentation::kWord32:
579 580
        opcode = kMips64Sw;
        break;
581 582
      case MachineRepresentation::kTaggedSigned:   // Fall through.
      case MachineRepresentation::kTaggedPointer:  // Fall through.
583
      case MachineRepresentation::kTagged:         // Fall through.
584
      case MachineRepresentation::kWord64:
585 586
        opcode = kMips64Sd;
        break;
587 588 589
      case MachineRepresentation::kSimd128:
        opcode = kMips64MsaSt;
        break;
590 591
      case MachineRepresentation::kCompressedPointer:  // Fall through.
      case MachineRepresentation::kCompressed:         // Fall through.
592
      case MachineRepresentation::kMapWord:            // Fall through.
593
      case MachineRepresentation::kNone:
594 595 596 597 598
        UNREACHABLE();
    }

    if (g.CanBeImmediate(index, opcode)) {
      Emit(opcode | AddressingModeField::encode(kMode_MRI), g.NoOutput(),
599 600
           g.UseRegister(base), g.UseImmediate(index),
           g.UseRegisterOrImmediateZero(value));
601 602 603 604 605 606
    } else {
      InstructionOperand addr_reg = g.TempRegister();
      Emit(kMips64Dadd | AddressingModeField::encode(kMode_None), addr_reg,
           g.UseRegister(index), g.UseRegister(base));
      // Emit desired store opcode, using temp addr_reg.
      Emit(opcode | AddressingModeField::encode(kMode_MRI), g.NoOutput(),
607
           addr_reg, g.TempImmediate(0), g.UseRegisterOrImmediateZero(value));
608
    }
609 610 611
  }
}

612 613 614 615
void InstructionSelector::VisitProtectedStore(Node* node) {
  // TODO(eholk)
  UNIMPLEMENTED();
}
616 617

void InstructionSelector::VisitWord32And(Node* node) {
618 619 620
  Mips64OperandGenerator g(this);
  Int32BinopMatcher m(node);
  if (m.left().IsWord32Shr() && CanCover(node, m.left().node()) &&
621 622
      m.right().HasResolvedValue()) {
    uint32_t mask = m.right().ResolvedValue();
623
    uint32_t mask_width = base::bits::CountPopulation(mask);
624 625 626 627 628 629 630 631
    uint32_t mask_msb = base::bits::CountLeadingZeros32(mask);
    if ((mask_width != 0) && (mask_msb + mask_width == 32)) {
      // The mask must be contiguous, and occupy the least-significant bits.
      DCHECK_EQ(0u, base::bits::CountTrailingZeros32(mask));

      // Select Ext for And(Shr(x, imm), mask) where the mask is in the least
      // significant bits.
      Int32BinopMatcher mleft(m.left().node());
632
      if (mleft.right().HasResolvedValue()) {
633
        // Any shift value can match; int32 shifts use `value % 32`.
634
        uint32_t lsb = mleft.right().ResolvedValue() & 0x1F;
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649

        // Ext cannot extract bits past the register size, however since
        // shifting the original value would have introduced some zeros we can
        // still use Ext with a smaller mask and the remaining bits will be
        // zeros.
        if (lsb + mask_width > 32) mask_width = 32 - lsb;

        Emit(kMips64Ext, g.DefineAsRegister(node),
             g.UseRegister(mleft.left().node()), g.TempImmediate(lsb),
             g.TempImmediate(mask_width));
        return;
      }
      // Other cases fall through to the normal And operation.
    }
  }
650 651
  if (m.right().HasResolvedValue()) {
    uint32_t mask = m.right().ResolvedValue();
652
    uint32_t shift = base::bits::CountPopulation(~mask);
653 654 655 656 657 658 659 660 661 662
    uint32_t msb = base::bits::CountLeadingZeros32(~mask);
    if (shift != 0 && shift != 32 && msb + shift == 32) {
      // Insert zeros for (x >> K) << K => x & ~(2^K - 1) expression reduction
      // and remove constant loading of inverted mask.
      Emit(kMips64Ins, g.DefineSameAsFirst(node),
           g.UseRegister(m.left().node()), g.TempImmediate(0),
           g.TempImmediate(shift));
      return;
    }
  }
663
  VisitBinop(this, node, kMips64And32, true, kMips64And32);
664 665 666
}

void InstructionSelector::VisitWord64And(Node* node) {
667 668 669
  Mips64OperandGenerator g(this);
  Int64BinopMatcher m(node);
  if (m.left().IsWord64Shr() && CanCover(node, m.left().node()) &&
670 671
      m.right().HasResolvedValue()) {
    uint64_t mask = m.right().ResolvedValue();
672
    uint32_t mask_width = base::bits::CountPopulation(mask);
673 674 675 676 677 678 679 680
    uint32_t mask_msb = base::bits::CountLeadingZeros64(mask);
    if ((mask_width != 0) && (mask_msb + mask_width == 64)) {
      // The mask must be contiguous, and occupy the least-significant bits.
      DCHECK_EQ(0u, base::bits::CountTrailingZeros64(mask));

      // Select Dext for And(Shr(x, imm), mask) where the mask is in the least
      // significant bits.
      Int64BinopMatcher mleft(m.left().node());
681
      if (mleft.right().HasResolvedValue()) {
682
        // Any shift value can match; int64 shifts use `value % 64`.
683 684
        uint32_t lsb =
            static_cast<uint32_t>(mleft.right().ResolvedValue() & 0x3F);
685 686 687 688 689 690 691

        // Dext cannot extract bits past the register size, however since
        // shifting the original value would have introduced some zeros we can
        // still use Dext with a smaller mask and the remaining bits will be
        // zeros.
        if (lsb + mask_width > 64) mask_width = 64 - lsb;

692 693 694 695 696 697 698
        if (lsb == 0 && mask_width == 64) {
          Emit(kArchNop, g.DefineSameAsFirst(node), g.Use(mleft.left().node()));
        } else {
          Emit(kMips64Dext, g.DefineAsRegister(node),
               g.UseRegister(mleft.left().node()), g.TempImmediate(lsb),
               g.TempImmediate(static_cast<int32_t>(mask_width)));
        }
699 700 701 702 703
        return;
      }
      // Other cases fall through to the normal And operation.
    }
  }
704 705
  if (m.right().HasResolvedValue()) {
    uint64_t mask = m.right().ResolvedValue();
706
    uint32_t shift = base::bits::CountPopulation(~mask);
707 708 709 710 711 712 713 714 715 716 717
    uint32_t msb = base::bits::CountLeadingZeros64(~mask);
    if (shift != 0 && shift < 32 && msb + shift == 64) {
      // Insert zeros for (x >> K) << K => x & ~(2^K - 1) expression reduction
      // and remove constant loading of inverted mask. Dins cannot insert bits
      // past word size, so shifts smaller than 32 are covered.
      Emit(kMips64Dins, g.DefineSameAsFirst(node),
           g.UseRegister(m.left().node()), g.TempImmediate(0),
           g.TempImmediate(shift));
      return;
    }
  }
718
  VisitBinop(this, node, kMips64And, true, kMips64And);
719 720 721
}

void InstructionSelector::VisitWord32Or(Node* node) {
722
  VisitBinop(this, node, kMips64Or32, true, kMips64Or32);
723 724 725
}

void InstructionSelector::VisitWord64Or(Node* node) {
726
  VisitBinop(this, node, kMips64Or, true, kMips64Or);
727 728 729
}

void InstructionSelector::VisitWord32Xor(Node* node) {
730 731 732 733
  Int32BinopMatcher m(node);
  if (m.left().IsWord32Or() && CanCover(node, m.left().node()) &&
      m.right().Is(-1)) {
    Int32BinopMatcher mleft(m.left().node());
734
    if (!mleft.right().HasResolvedValue()) {
735
      Mips64OperandGenerator g(this);
736
      Emit(kMips64Nor32, g.DefineAsRegister(node),
737 738 739 740 741
           g.UseRegister(mleft.left().node()),
           g.UseRegister(mleft.right().node()));
      return;
    }
  }
742 743 744
  if (m.right().Is(-1)) {
    // Use Nor for bit negation and eliminate constant loading for xori.
    Mips64OperandGenerator g(this);
745
    Emit(kMips64Nor32, g.DefineAsRegister(node), g.UseRegister(m.left().node()),
746 747 748
         g.TempImmediate(0));
    return;
  }
749
  VisitBinop(this, node, kMips64Xor32, true, kMips64Xor32);
750 751 752
}

void InstructionSelector::VisitWord64Xor(Node* node) {
753 754 755 756
  Int64BinopMatcher m(node);
  if (m.left().IsWord64Or() && CanCover(node, m.left().node()) &&
      m.right().Is(-1)) {
    Int64BinopMatcher mleft(m.left().node());
757
    if (!mleft.right().HasResolvedValue()) {
758 759 760 761 762 763 764
      Mips64OperandGenerator g(this);
      Emit(kMips64Nor, g.DefineAsRegister(node),
           g.UseRegister(mleft.left().node()),
           g.UseRegister(mleft.right().node()));
      return;
    }
  }
765 766 767 768 769 770 771
  if (m.right().Is(-1)) {
    // Use Nor for bit negation and eliminate constant loading for xori.
    Mips64OperandGenerator g(this);
    Emit(kMips64Nor, g.DefineAsRegister(node), g.UseRegister(m.left().node()),
         g.TempImmediate(0));
    return;
  }
772
  VisitBinop(this, node, kMips64Xor, true, kMips64Xor);
773 774 775
}

void InstructionSelector::VisitWord32Shl(Node* node) {
776 777 778 779 780 781 782
  Int32BinopMatcher m(node);
  if (m.left().IsWord32And() && CanCover(node, m.left().node()) &&
      m.right().IsInRange(1, 31)) {
    Mips64OperandGenerator g(this);
    Int32BinopMatcher mleft(m.left().node());
    // Match Word32Shl(Word32And(x, mask), imm) to Shl where the mask is
    // contiguous, and the shift immediate non-zero.
783 784
    if (mleft.right().HasResolvedValue()) {
      uint32_t mask = mleft.right().ResolvedValue();
785
      uint32_t mask_width = base::bits::CountPopulation(mask);
786 787
      uint32_t mask_msb = base::bits::CountLeadingZeros32(mask);
      if ((mask_width != 0) && (mask_msb + mask_width == 32)) {
788
        uint32_t shift = m.right().ResolvedValue();
789 790 791 792 793 794 795 796 797 798 799 800 801
        DCHECK_EQ(0u, base::bits::CountTrailingZeros32(mask));
        DCHECK_NE(0u, shift);
        if ((shift + mask_width) >= 32) {
          // If the mask is contiguous and reaches or extends beyond the top
          // bit, only the shift is needed.
          Emit(kMips64Shl, g.DefineAsRegister(node),
               g.UseRegister(mleft.left().node()),
               g.UseImmediate(m.right().node()));
          return;
        }
      }
    }
  }
802 803 804 805
  VisitRRO(this, kMips64Shl, node);
}

void InstructionSelector::VisitWord32Shr(Node* node) {
806
  Int32BinopMatcher m(node);
807 808
  if (m.left().IsWord32And() && m.right().HasResolvedValue()) {
    uint32_t lsb = m.right().ResolvedValue() & 0x1F;
809
    Int32BinopMatcher mleft(m.left().node());
810 811
    if (mleft.right().HasResolvedValue() &&
        mleft.right().ResolvedValue() != 0) {
812 813
      // Select Ext for Shr(And(x, mask), imm) where the result of the mask is
      // shifted into the least-significant bits.
814
      uint32_t mask = (mleft.right().ResolvedValue() >> lsb) << lsb;
815
      unsigned mask_width = base::bits::CountPopulation(mask);
816 817 818 819 820 821 822 823 824 825 826
      unsigned mask_msb = base::bits::CountLeadingZeros32(mask);
      if ((mask_msb + mask_width + lsb) == 32) {
        Mips64OperandGenerator g(this);
        DCHECK_EQ(lsb, base::bits::CountTrailingZeros32(mask));
        Emit(kMips64Ext, g.DefineAsRegister(node),
             g.UseRegister(mleft.left().node()), g.TempImmediate(lsb),
             g.TempImmediate(mask_width));
        return;
      }
    }
  }
827 828 829 830
  VisitRRO(this, kMips64Shr, node);
}

void InstructionSelector::VisitWord32Sar(Node* node) {
831 832 833
  Int32BinopMatcher m(node);
  if (m.left().IsWord32Shl() && CanCover(node, m.left().node())) {
    Int32BinopMatcher mleft(m.left().node());
834
    if (m.right().HasResolvedValue() && mleft.right().HasResolvedValue()) {
835
      Mips64OperandGenerator g(this);
836 837
      uint32_t sar = m.right().ResolvedValue();
      uint32_t shl = mleft.right().ResolvedValue();
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
      if ((sar == shl) && (sar == 16)) {
        Emit(kMips64Seh, g.DefineAsRegister(node),
             g.UseRegister(mleft.left().node()));
        return;
      } else if ((sar == shl) && (sar == 24)) {
        Emit(kMips64Seb, g.DefineAsRegister(node),
             g.UseRegister(mleft.left().node()));
        return;
      } else if ((sar == shl) && (sar == 32)) {
        Emit(kMips64Shl, g.DefineAsRegister(node),
             g.UseRegister(mleft.left().node()), g.TempImmediate(0));
        return;
      }
    }
  }
853 854 855 856
  VisitRRO(this, kMips64Sar, node);
}

void InstructionSelector::VisitWord64Shl(Node* node) {
857 858 859
  Mips64OperandGenerator g(this);
  Int64BinopMatcher m(node);
  if ((m.left().IsChangeInt32ToInt64() || m.left().IsChangeUint32ToUint64()) &&
860
      m.right().IsInRange(32, 63) && CanCover(node, m.left().node())) {
861 862 863 864 865 866 867
    // There's no need to sign/zero-extend to 64-bit if we shift out the upper
    // 32 bits anyway.
    Emit(kMips64Dshl, g.DefineSameAsFirst(node),
         g.UseRegister(m.left().node()->InputAt(0)),
         g.UseImmediate(m.right().node()));
    return;
  }
868 869 870 871 872
  if (m.left().IsWord64And() && CanCover(node, m.left().node()) &&
      m.right().IsInRange(1, 63)) {
    // Match Word64Shl(Word64And(x, mask), imm) to Dshl where the mask is
    // contiguous, and the shift immediate non-zero.
    Int64BinopMatcher mleft(m.left().node());
873 874
    if (mleft.right().HasResolvedValue()) {
      uint64_t mask = mleft.right().ResolvedValue();
875
      uint32_t mask_width = base::bits::CountPopulation(mask);
876 877
      uint32_t mask_msb = base::bits::CountLeadingZeros64(mask);
      if ((mask_width != 0) && (mask_msb + mask_width == 64)) {
878
        uint64_t shift = m.right().ResolvedValue();
879 880 881 882 883 884 885 886 887 888 889 890 891 892
        DCHECK_EQ(0u, base::bits::CountTrailingZeros64(mask));
        DCHECK_NE(0u, shift);

        if ((shift + mask_width) >= 64) {
          // If the mask is contiguous and reaches or extends beyond the top
          // bit, only the shift is needed.
          Emit(kMips64Dshl, g.DefineAsRegister(node),
               g.UseRegister(mleft.left().node()),
               g.UseImmediate(m.right().node()));
          return;
        }
      }
    }
  }
893 894 895 896
  VisitRRO(this, kMips64Dshl, node);
}

void InstructionSelector::VisitWord64Shr(Node* node) {
897
  Int64BinopMatcher m(node);
898 899
  if (m.left().IsWord64And() && m.right().HasResolvedValue()) {
    uint32_t lsb = m.right().ResolvedValue() & 0x3F;
900
    Int64BinopMatcher mleft(m.left().node());
901 902
    if (mleft.right().HasResolvedValue() &&
        mleft.right().ResolvedValue() != 0) {
903 904
      // Select Dext for Shr(And(x, mask), imm) where the result of the mask is
      // shifted into the least-significant bits.
905
      uint64_t mask = (mleft.right().ResolvedValue() >> lsb) << lsb;
906
      unsigned mask_width = base::bits::CountPopulation(mask);
907 908 909 910 911 912 913 914 915 916 917
      unsigned mask_msb = base::bits::CountLeadingZeros64(mask);
      if ((mask_msb + mask_width + lsb) == 64) {
        Mips64OperandGenerator g(this);
        DCHECK_EQ(lsb, base::bits::CountTrailingZeros64(mask));
        Emit(kMips64Dext, g.DefineAsRegister(node),
             g.UseRegister(mleft.left().node()), g.TempImmediate(lsb),
             g.TempImmediate(mask_width));
        return;
      }
    }
  }
918 919 920 921
  VisitRRO(this, kMips64Dshr, node);
}

void InstructionSelector::VisitWord64Sar(Node* node) {
922
  if (TryEmitExtendingLoad(this, node, node)) return;
923 924 925
  VisitRRO(this, kMips64Dsar, node);
}

926 927 928 929
void InstructionSelector::VisitWord32Rol(Node* node) { UNREACHABLE(); }

void InstructionSelector::VisitWord64Rol(Node* node) { UNREACHABLE(); }

930 931 932 933
void InstructionSelector::VisitWord32Ror(Node* node) {
  VisitRRO(this, kMips64Ror, node);
}

934
void InstructionSelector::VisitWord32Clz(Node* node) {
935
  VisitRR(this, kMips64Clz, node);
936 937
}

938 939 940 941
void InstructionSelector::VisitWord32ReverseBits(Node* node) { UNREACHABLE(); }

void InstructionSelector::VisitWord64ReverseBits(Node* node) { UNREACHABLE(); }

942 943 944 945 946
void InstructionSelector::VisitWord64ReverseBytes(Node* node) {
  Mips64OperandGenerator g(this);
  Emit(kMips64ByteSwap64, g.DefineAsRegister(node),
       g.UseRegister(node->InputAt(0)));
}
947

948 949 950 951 952
void InstructionSelector::VisitWord32ReverseBytes(Node* node) {
  Mips64OperandGenerator g(this);
  Emit(kMips64ByteSwap32, g.DefineAsRegister(node),
       g.UseRegister(node->InputAt(0)));
}
953

954 955 956 957
void InstructionSelector::VisitSimd128ReverseBytes(Node* node) {
  UNREACHABLE();
}

958 959 960 961
void InstructionSelector::VisitWord32Ctz(Node* node) {
  Mips64OperandGenerator g(this);
  Emit(kMips64Ctz, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0)));
}
962

963 964 965 966
void InstructionSelector::VisitWord64Ctz(Node* node) {
  Mips64OperandGenerator g(this);
  Emit(kMips64Dctz, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0)));
}
967

968 969 970 971 972
void InstructionSelector::VisitWord32Popcnt(Node* node) {
  Mips64OperandGenerator g(this);
  Emit(kMips64Popcnt, g.DefineAsRegister(node),
       g.UseRegister(node->InputAt(0)));
}
973

974 975 976 977 978
void InstructionSelector::VisitWord64Popcnt(Node* node) {
  Mips64OperandGenerator g(this);
  Emit(kMips64Dpopcnt, g.DefineAsRegister(node),
       g.UseRegister(node->InputAt(0)));
}
979

980 981 982 983
void InstructionSelector::VisitWord64Ror(Node* node) {
  VisitRRO(this, kMips64Dror, node);
}

984 985 986 987
void InstructionSelector::VisitWord64Clz(Node* node) {
  VisitRR(this, kMips64Dclz, node);
}

988 989
void InstructionSelector::VisitInt32Add(Node* node) {
  Mips64OperandGenerator g(this);
990 991
  Int32BinopMatcher m(node);

992 993 994 995 996
  if (kArchVariant == kMips64r6) {
    // Select Lsa for (left + (left_of_right << imm)).
    if (m.right().opcode() == IrOpcode::kWord32Shl &&
        CanCover(node, m.left().node()) && CanCover(node, m.right().node())) {
      Int32BinopMatcher mright(m.right().node());
997 998 999
      if (mright.right().HasResolvedValue() && !m.left().HasResolvedValue()) {
        int32_t shift_value =
            static_cast<int32_t>(mright.right().ResolvedValue());
1000 1001 1002 1003 1004 1005 1006 1007
        if (shift_value > 0 && shift_value <= 31) {
          Emit(kMips64Lsa, g.DefineAsRegister(node),
               g.UseRegister(m.left().node()),
               g.UseRegister(mright.left().node()),
               g.TempImmediate(shift_value));
          return;
        }
      }
1008 1009
    }

1010 1011 1012 1013
    // Select Lsa for ((left_of_left << imm) + right).
    if (m.left().opcode() == IrOpcode::kWord32Shl &&
        CanCover(node, m.right().node()) && CanCover(node, m.left().node())) {
      Int32BinopMatcher mleft(m.left().node());
1014 1015 1016
      if (mleft.right().HasResolvedValue() && !m.right().HasResolvedValue()) {
        int32_t shift_value =
            static_cast<int32_t>(mleft.right().ResolvedValue());
1017 1018 1019 1020 1021 1022 1023 1024
        if (shift_value > 0 && shift_value <= 31) {
          Emit(kMips64Lsa, g.DefineAsRegister(node),
               g.UseRegister(m.right().node()),
               g.UseRegister(mleft.left().node()),
               g.TempImmediate(shift_value));
          return;
        }
      }
1025 1026
    }
  }
1027

1028
  VisitBinop(this, node, kMips64Add, true, kMips64Add);
1029 1030 1031 1032
}

void InstructionSelector::VisitInt64Add(Node* node) {
  Mips64OperandGenerator g(this);
1033 1034
  Int64BinopMatcher m(node);

1035 1036 1037 1038 1039
  if (kArchVariant == kMips64r6) {
    // Select Dlsa for (left + (left_of_right << imm)).
    if (m.right().opcode() == IrOpcode::kWord64Shl &&
        CanCover(node, m.left().node()) && CanCover(node, m.right().node())) {
      Int64BinopMatcher mright(m.right().node());
1040 1041 1042
      if (mright.right().HasResolvedValue() && !m.left().HasResolvedValue()) {
        int32_t shift_value =
            static_cast<int32_t>(mright.right().ResolvedValue());
1043 1044 1045 1046 1047 1048 1049 1050
        if (shift_value > 0 && shift_value <= 31) {
          Emit(kMips64Dlsa, g.DefineAsRegister(node),
               g.UseRegister(m.left().node()),
               g.UseRegister(mright.left().node()),
               g.TempImmediate(shift_value));
          return;
        }
      }
1051 1052
    }

1053 1054 1055 1056
    // Select Dlsa for ((left_of_left << imm) + right).
    if (m.left().opcode() == IrOpcode::kWord64Shl &&
        CanCover(node, m.right().node()) && CanCover(node, m.left().node())) {
      Int64BinopMatcher mleft(m.left().node());
1057 1058 1059
      if (mleft.right().HasResolvedValue() && !m.right().HasResolvedValue()) {
        int32_t shift_value =
            static_cast<int32_t>(mleft.right().ResolvedValue());
1060 1061 1062 1063 1064 1065 1066 1067
        if (shift_value > 0 && shift_value <= 31) {
          Emit(kMips64Dlsa, g.DefineAsRegister(node),
               g.UseRegister(m.right().node()),
               g.UseRegister(mleft.left().node()),
               g.TempImmediate(shift_value));
          return;
        }
      }
1068 1069 1070
    }
  }

1071
  VisitBinop(this, node, kMips64Dadd, true, kMips64Dadd);
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
}

void InstructionSelector::VisitInt32Sub(Node* node) {
  VisitBinop(this, node, kMips64Sub);
}

void InstructionSelector::VisitInt64Sub(Node* node) {
  VisitBinop(this, node, kMips64Dsub);
}

void InstructionSelector::VisitInt32Mul(Node* node) {
  Mips64OperandGenerator g(this);
  Int32BinopMatcher m(node);
1085 1086
  if (m.right().HasResolvedValue() && m.right().ResolvedValue() > 0) {
    uint32_t value = static_cast<uint32_t>(m.right().ResolvedValue());
1087
    if (base::bits::IsPowerOfTwo(value)) {
1088 1089
      Emit(kMips64Shl | AddressingModeField::encode(kMode_None),
           g.DefineAsRegister(node), g.UseRegister(m.left().node()),
1090
           g.TempImmediate(base::bits::WhichPowerOfTwo(value)));
1091 1092
      return;
    }
1093 1094
    if (base::bits::IsPowerOfTwo(value - 1) && kArchVariant == kMips64r6 &&
        value - 1 > 0 && value - 1 <= 31) {
1095
      Emit(kMips64Lsa, g.DefineAsRegister(node), g.UseRegister(m.left().node()),
1096
           g.UseRegister(m.left().node()),
1097
           g.TempImmediate(base::bits::WhichPowerOfTwo(value - 1)));
1098 1099
      return;
    }
1100
    if (base::bits::IsPowerOfTwo(value + 1)) {
1101
      InstructionOperand temp = g.TempRegister();
1102 1103
      Emit(kMips64Shl | AddressingModeField::encode(kMode_None), temp,
           g.UseRegister(m.left().node()),
1104
           g.TempImmediate(base::bits::WhichPowerOfTwo(value + 1)));
1105 1106 1107 1108 1109
      Emit(kMips64Sub | AddressingModeField::encode(kMode_None),
           g.DefineAsRegister(node), temp, g.UseRegister(m.left().node()));
      return;
    }
  }
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
  Node* left = node->InputAt(0);
  Node* right = node->InputAt(1);
  if (CanCover(node, left) && CanCover(node, right)) {
    if (left->opcode() == IrOpcode::kWord64Sar &&
        right->opcode() == IrOpcode::kWord64Sar) {
      Int64BinopMatcher leftInput(left), rightInput(right);
      if (leftInput.right().Is(32) && rightInput.right().Is(32)) {
        // Combine untagging shifts with Dmul high.
        Emit(kMips64DMulHigh, g.DefineSameAsFirst(node),
             g.UseRegister(leftInput.left().node()),
             g.UseRegister(rightInput.left().node()));
        return;
      }
    }
  }
1125
  VisitRRR(this, kMips64Mul, node);
1126 1127 1128
}

void InstructionSelector::VisitInt32MulHigh(Node* node) {
1129
  VisitRRR(this, kMips64MulHigh, node);
1130 1131 1132
}

void InstructionSelector::VisitUint32MulHigh(Node* node) {
1133
  VisitRRR(this, kMips64MulHighU, node);
1134 1135 1136 1137 1138 1139
}

void InstructionSelector::VisitInt64Mul(Node* node) {
  Mips64OperandGenerator g(this);
  Int64BinopMatcher m(node);
  // TODO(dusmil): Add optimization for shifts larger than 32.
1140 1141
  if (m.right().HasResolvedValue() && m.right().ResolvedValue() > 0) {
    uint32_t value = static_cast<uint32_t>(m.right().ResolvedValue());
1142
    if (base::bits::IsPowerOfTwo(value)) {
1143 1144
      Emit(kMips64Dshl | AddressingModeField::encode(kMode_None),
           g.DefineAsRegister(node), g.UseRegister(m.left().node()),
1145
           g.TempImmediate(base::bits::WhichPowerOfTwo(value)));
1146 1147
      return;
    }
1148 1149
    if (base::bits::IsPowerOfTwo(value - 1) && kArchVariant == kMips64r6 &&
        value - 1 > 0 && value - 1 <= 31) {
1150 1151 1152
      // Dlsa macro will handle the shifting value out of bound cases.
      Emit(kMips64Dlsa, g.DefineAsRegister(node),
           g.UseRegister(m.left().node()), g.UseRegister(m.left().node()),
1153
           g.TempImmediate(base::bits::WhichPowerOfTwo(value - 1)));
1154 1155
      return;
    }
1156
    if (base::bits::IsPowerOfTwo(value + 1)) {
1157
      InstructionOperand temp = g.TempRegister();
1158 1159
      Emit(kMips64Dshl | AddressingModeField::encode(kMode_None), temp,
           g.UseRegister(m.left().node()),
1160
           g.TempImmediate(base::bits::WhichPowerOfTwo(value + 1)));
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
      Emit(kMips64Dsub | AddressingModeField::encode(kMode_None),
           g.DefineAsRegister(node), temp, g.UseRegister(m.left().node()));
      return;
    }
  }
  Emit(kMips64Dmul, g.DefineAsRegister(node), g.UseRegister(m.left().node()),
       g.UseRegister(m.right().node()));
}

void InstructionSelector::VisitInt32Div(Node* node) {
  Mips64OperandGenerator g(this);
  Int32BinopMatcher m(node);
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
  Node* left = node->InputAt(0);
  Node* right = node->InputAt(1);
  if (CanCover(node, left) && CanCover(node, right)) {
    if (left->opcode() == IrOpcode::kWord64Sar &&
        right->opcode() == IrOpcode::kWord64Sar) {
      Int64BinopMatcher rightInput(right), leftInput(left);
      if (rightInput.right().Is(32) && leftInput.right().Is(32)) {
        // Combine both shifted operands with Ddiv.
        Emit(kMips64Ddiv, g.DefineSameAsFirst(node),
             g.UseRegister(leftInput.left().node()),
             g.UseRegister(rightInput.left().node()));
        return;
      }
    }
  }
1188
  Emit(kMips64Div, g.DefineSameAsFirst(node), g.UseRegister(m.left().node()),
1189 1190 1191 1192 1193 1194
       g.UseRegister(m.right().node()));
}

void InstructionSelector::VisitUint32Div(Node* node) {
  Mips64OperandGenerator g(this);
  Int32BinopMatcher m(node);
1195
  Emit(kMips64DivU, g.DefineSameAsFirst(node), g.UseRegister(m.left().node()),
1196 1197 1198 1199 1200 1201
       g.UseRegister(m.right().node()));
}

void InstructionSelector::VisitInt32Mod(Node* node) {
  Mips64OperandGenerator g(this);
  Int32BinopMatcher m(node);
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
  Node* left = node->InputAt(0);
  Node* right = node->InputAt(1);
  if (CanCover(node, left) && CanCover(node, right)) {
    if (left->opcode() == IrOpcode::kWord64Sar &&
        right->opcode() == IrOpcode::kWord64Sar) {
      Int64BinopMatcher rightInput(right), leftInput(left);
      if (rightInput.right().Is(32) && leftInput.right().Is(32)) {
        // Combine both shifted operands with Dmod.
        Emit(kMips64Dmod, g.DefineSameAsFirst(node),
             g.UseRegister(leftInput.left().node()),
             g.UseRegister(rightInput.left().node()));
        return;
      }
    }
  }
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
  Emit(kMips64Mod, g.DefineAsRegister(node), g.UseRegister(m.left().node()),
       g.UseRegister(m.right().node()));
}

void InstructionSelector::VisitUint32Mod(Node* node) {
  Mips64OperandGenerator g(this);
  Int32BinopMatcher m(node);
  Emit(kMips64ModU, g.DefineAsRegister(node), g.UseRegister(m.left().node()),
       g.UseRegister(m.right().node()));
}

void InstructionSelector::VisitInt64Div(Node* node) {
  Mips64OperandGenerator g(this);
  Int64BinopMatcher m(node);
1231
  Emit(kMips64Ddiv, g.DefineSameAsFirst(node), g.UseRegister(m.left().node()),
1232 1233 1234 1235 1236 1237
       g.UseRegister(m.right().node()));
}

void InstructionSelector::VisitUint64Div(Node* node) {
  Mips64OperandGenerator g(this);
  Int64BinopMatcher m(node);
1238
  Emit(kMips64DdivU, g.DefineSameAsFirst(node), g.UseRegister(m.left().node()),
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
       g.UseRegister(m.right().node()));
}

void InstructionSelector::VisitInt64Mod(Node* node) {
  Mips64OperandGenerator g(this);
  Int64BinopMatcher m(node);
  Emit(kMips64Dmod, g.DefineAsRegister(node), g.UseRegister(m.left().node()),
       g.UseRegister(m.right().node()));
}

void InstructionSelector::VisitUint64Mod(Node* node) {
  Mips64OperandGenerator g(this);
  Int64BinopMatcher m(node);
  Emit(kMips64DmodU, g.DefineAsRegister(node), g.UseRegister(m.left().node()),
       g.UseRegister(m.right().node()));
}

void InstructionSelector::VisitChangeFloat32ToFloat64(Node* node) {
1257
  VisitRR(this, kMips64CvtDS, node);
1258 1259
}

1260 1261 1262 1263
void InstructionSelector::VisitRoundInt32ToFloat32(Node* node) {
  VisitRR(this, kMips64CvtSW, node);
}

1264
void InstructionSelector::VisitRoundUint32ToFloat32(Node* node) {
1265
  VisitRR(this, kMips64CvtSUw, node);
1266 1267
}

1268
void InstructionSelector::VisitChangeInt32ToFloat64(Node* node) {
1269
  VisitRR(this, kMips64CvtDW, node);
1270 1271
}

1272 1273 1274
void InstructionSelector::VisitChangeInt64ToFloat64(Node* node) {
  VisitRR(this, kMips64CvtDL, node);
}
1275 1276

void InstructionSelector::VisitChangeUint32ToFloat64(Node* node) {
1277
  VisitRR(this, kMips64CvtDUw, node);
1278 1279 1280
}

void InstructionSelector::VisitTruncateFloat32ToInt32(Node* node) {
1281 1282 1283 1284 1285 1286 1287
  Mips64OperandGenerator g(this);
  InstructionCode opcode = kMips64TruncWS;
  TruncateKind kind = OpParameter<TruncateKind>(node->op());
  if (kind == TruncateKind::kSetOverflowToMin) {
    opcode |= MiscField::encode(true);
  }
  Emit(opcode, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0)));
1288 1289
}

1290
void InstructionSelector::VisitTruncateFloat32ToUint32(Node* node) {
1291 1292 1293 1294 1295 1296 1297
  Mips64OperandGenerator g(this);
  InstructionCode opcode = kMips64TruncUwS;
  TruncateKind kind = OpParameter<TruncateKind>(node->op());
  if (kind == TruncateKind::kSetOverflowToMin) {
    opcode |= MiscField::encode(true);
  }
  Emit(opcode, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0)));
1298 1299
}

1300
void InstructionSelector::VisitChangeFloat64ToInt32(Node* node) {
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
  Mips64OperandGenerator g(this);
  Node* value = node->InputAt(0);
  // Match ChangeFloat64ToInt32(Float64Round##OP) to corresponding instruction
  // which does rounding and conversion to integer format.
  if (CanCover(node, value)) {
    switch (value->opcode()) {
      case IrOpcode::kFloat64RoundDown:
        Emit(kMips64FloorWD, g.DefineAsRegister(node),
             g.UseRegister(value->InputAt(0)));
        return;
      case IrOpcode::kFloat64RoundUp:
        Emit(kMips64CeilWD, g.DefineAsRegister(node),
             g.UseRegister(value->InputAt(0)));
        return;
      case IrOpcode::kFloat64RoundTiesEven:
        Emit(kMips64RoundWD, g.DefineAsRegister(node),
             g.UseRegister(value->InputAt(0)));
        return;
      case IrOpcode::kFloat64RoundTruncate:
        Emit(kMips64TruncWD, g.DefineAsRegister(node),
             g.UseRegister(value->InputAt(0)));
        return;
      default:
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
        break;
    }
    if (value->opcode() == IrOpcode::kChangeFloat32ToFloat64) {
      Node* next = value->InputAt(0);
      if (CanCover(value, next)) {
        // Match ChangeFloat64ToInt32(ChangeFloat32ToFloat64(Float64Round##OP))
        switch (next->opcode()) {
          case IrOpcode::kFloat32RoundDown:
            Emit(kMips64FloorWS, g.DefineAsRegister(node),
                 g.UseRegister(next->InputAt(0)));
            return;
          case IrOpcode::kFloat32RoundUp:
            Emit(kMips64CeilWS, g.DefineAsRegister(node),
                 g.UseRegister(next->InputAt(0)));
            return;
          case IrOpcode::kFloat32RoundTiesEven:
            Emit(kMips64RoundWS, g.DefineAsRegister(node),
                 g.UseRegister(next->InputAt(0)));
            return;
          case IrOpcode::kFloat32RoundTruncate:
            Emit(kMips64TruncWS, g.DefineAsRegister(node),
                 g.UseRegister(next->InputAt(0)));
            return;
          default:
            Emit(kMips64TruncWS, g.DefineAsRegister(node),
                 g.UseRegister(value->InputAt(0)));
            return;
        }
      } else {
        // Match float32 -> float64 -> int32 representation change path.
        Emit(kMips64TruncWS, g.DefineAsRegister(node),
             g.UseRegister(value->InputAt(0)));
1356
        return;
1357
      }
1358 1359
    }
  }
1360
  VisitRR(this, kMips64TruncWD, node);
1361 1362
}

1363 1364 1365
void InstructionSelector::VisitChangeFloat64ToInt64(Node* node) {
  VisitRR(this, kMips64TruncLD, node);
}
1366 1367

void InstructionSelector::VisitChangeFloat64ToUint32(Node* node) {
1368
  VisitRR(this, kMips64TruncUwD, node);
1369 1370
}

1371 1372 1373 1374
void InstructionSelector::VisitChangeFloat64ToUint64(Node* node) {
  VisitRR(this, kMips64TruncUlD, node);
}

1375 1376 1377
void InstructionSelector::VisitTruncateFloat64ToUint32(Node* node) {
  VisitRR(this, kMips64TruncUwD, node);
}
1378

1379
void InstructionSelector::VisitTruncateFloat64ToInt64(Node* node) {
1380 1381 1382 1383 1384 1385 1386
  Mips64OperandGenerator g(this);
  InstructionCode opcode = kMips64TruncLD;
  TruncateKind kind = OpParameter<TruncateKind>(node->op());
  if (kind == TruncateKind::kSetOverflowToMin) {
    opcode |= MiscField::encode(true);
  }
  Emit(opcode, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0)));
1387 1388
}

1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
void InstructionSelector::VisitTryTruncateFloat32ToInt64(Node* node) {
  Mips64OperandGenerator g(this);
  InstructionOperand inputs[] = {g.UseRegister(node->InputAt(0))};
  InstructionOperand outputs[2];
  size_t output_count = 0;
  outputs[output_count++] = g.DefineAsRegister(node);

  Node* success_output = NodeProperties::FindProjection(node, 1);
  if (success_output) {
    outputs[output_count++] = g.DefineAsRegister(success_output);
  }

  this->Emit(kMips64TruncLS, output_count, outputs, 1, inputs);
1402 1403
}

1404
void InstructionSelector::VisitTryTruncateFloat64ToInt64(Node* node) {
1405 1406 1407 1408 1409 1410 1411 1412 1413
  Mips64OperandGenerator g(this);
  InstructionOperand inputs[] = {g.UseRegister(node->InputAt(0))};
  InstructionOperand outputs[2];
  size_t output_count = 0;
  outputs[output_count++] = g.DefineAsRegister(node);

  Node* success_output = NodeProperties::FindProjection(node, 1);
  if (success_output) {
    outputs[output_count++] = g.DefineAsRegister(success_output);
1414
  }
1415 1416

  Emit(kMips64TruncLD, output_count, outputs, 1, inputs);
1417 1418
}

1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
void InstructionSelector::VisitTryTruncateFloat32ToUint64(Node* node) {
  Mips64OperandGenerator g(this);
  InstructionOperand inputs[] = {g.UseRegister(node->InputAt(0))};
  InstructionOperand outputs[2];
  size_t output_count = 0;
  outputs[output_count++] = g.DefineAsRegister(node);

  Node* success_output = NodeProperties::FindProjection(node, 1);
  if (success_output) {
    outputs[output_count++] = g.DefineAsRegister(success_output);
  }

  Emit(kMips64TruncUlS, output_count, outputs, 1, inputs);
1432 1433
}

1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
void InstructionSelector::VisitTryTruncateFloat64ToUint64(Node* node) {
  Mips64OperandGenerator g(this);

  InstructionOperand inputs[] = {g.UseRegister(node->InputAt(0))};
  InstructionOperand outputs[2];
  size_t output_count = 0;
  outputs[output_count++] = g.DefineAsRegister(node);

  Node* success_output = NodeProperties::FindProjection(node, 1);
  if (success_output) {
    outputs[output_count++] = g.DefineAsRegister(success_output);
  }

  Emit(kMips64TruncUlD, output_count, outputs, 1, inputs);
1448 1449
}

1450 1451 1452 1453
void InstructionSelector::VisitBitcastWord32ToWord64(Node* node) {
  UNIMPLEMENTED();
}

1454
void InstructionSelector::VisitChangeInt32ToInt64(Node* node) {
1455
  Node* value = node->InputAt(0);
1456 1457
  if ((value->opcode() == IrOpcode::kLoad ||
       value->opcode() == IrOpcode::kLoadImmutable) &&
1458
      CanCover(node, value)) {
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
    // Generate sign-extending load.
    LoadRepresentation load_rep = LoadRepresentationOf(value->op());
    InstructionCode opcode = kArchNop;
    switch (load_rep.representation()) {
      case MachineRepresentation::kBit:  // Fall through.
      case MachineRepresentation::kWord8:
        opcode = load_rep.IsUnsigned() ? kMips64Lbu : kMips64Lb;
        break;
      case MachineRepresentation::kWord16:
        opcode = load_rep.IsUnsigned() ? kMips64Lhu : kMips64Lh;
        break;
      case MachineRepresentation::kWord32:
        opcode = kMips64Lw;
        break;
      default:
        UNREACHABLE();
    }
    EmitLoad(this, value, opcode, node);
  } else {
    Mips64OperandGenerator g(this);
    Emit(kMips64Shl, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0)),
         g.TempImmediate(0));
  }
1482 1483
}

1484 1485 1486
bool InstructionSelector::ZeroExtendsWord32ToWord64NoPhis(Node* node) {
  DCHECK_NE(node->opcode(), IrOpcode::kPhi);
  switch (node->opcode()) {
1487 1488 1489 1490
    // 32-bit operations will write their result in a 64 bit register,
    // clearing the top 32 bits of the destination register.
    case IrOpcode::kUint32Div:
    case IrOpcode::kUint32Mod:
1491 1492
    case IrOpcode::kUint32MulHigh:
      return true;
1493 1494
    case IrOpcode::kLoad:
    case IrOpcode::kLoadImmutable: {
1495
      LoadRepresentation load_rep = LoadRepresentationOf(node->op());
1496 1497 1498 1499 1500
      if (load_rep.IsUnsigned()) {
        switch (load_rep.representation()) {
          case MachineRepresentation::kWord8:
          case MachineRepresentation::kWord16:
          case MachineRepresentation::kWord32:
1501
            return true;
1502
          default:
1503
            return false;
1504 1505
        }
      }
1506
      return false;
1507 1508
    }
    default:
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
      return false;
  }
}

void InstructionSelector::VisitChangeUint32ToUint64(Node* node) {
  Mips64OperandGenerator g(this);
  Node* value = node->InputAt(0);
  if (ZeroExtendsWord32ToWord64(value)) {
    Emit(kArchNop, g.DefineSameAsFirst(node), g.Use(value));
    return;
1519
  }
1520 1521 1522 1523 1524 1525
  Emit(kMips64Dext, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0)),
       g.TempImmediate(0), g.TempImmediate(32));
}

void InstructionSelector::VisitTruncateInt64ToInt32(Node* node) {
  Mips64OperandGenerator g(this);
1526 1527 1528 1529
  Node* value = node->InputAt(0);
  if (CanCover(node, value)) {
    switch (value->opcode()) {
      case IrOpcode::kWord64Sar: {
1530 1531
        if (CanCoverTransitively(node, value, value->InputAt(0)) &&
            TryEmitExtendingLoad(this, value, node)) {
1532
          return;
1533 1534 1535 1536 1537 1538 1539 1540 1541
        } else {
          Int64BinopMatcher m(value);
          if (m.right().IsInRange(32, 63)) {
            // After smi untagging no need for truncate. Combine sequence.
            Emit(kMips64Dsar, g.DefineSameAsFirst(node),
                 g.UseRegister(m.left().node()),
                 g.UseImmediate(m.right().node()));
            return;
          }
1542 1543 1544 1545 1546 1547 1548
        }
        break;
      }
      default:
        break;
    }
  }
1549 1550 1551 1552 1553
  Emit(kMips64Ext, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0)),
       g.TempImmediate(0), g.TempImmediate(32));
}

void InstructionSelector::VisitTruncateFloat64ToFloat32(Node* node) {
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563
  Mips64OperandGenerator g(this);
  Node* value = node->InputAt(0);
  // Match TruncateFloat64ToFloat32(ChangeInt32ToFloat64) to corresponding
  // instruction.
  if (CanCover(node, value) &&
      value->opcode() == IrOpcode::kChangeInt32ToFloat64) {
    Emit(kMips64CvtSW, g.DefineAsRegister(node),
         g.UseRegister(value->InputAt(0)));
    return;
  }
1564 1565 1566
  VisitRR(this, kMips64CvtSD, node);
}

1567 1568
void InstructionSelector::VisitTruncateFloat64ToWord32(Node* node) {
  VisitRR(this, kArchTruncateDoubleToI, node);
1569 1570
}

1571 1572 1573
void InstructionSelector::VisitRoundFloat64ToInt32(Node* node) {
  VisitRR(this, kMips64TruncWD, node);
}
1574

1575 1576 1577 1578
void InstructionSelector::VisitRoundInt64ToFloat32(Node* node) {
  VisitRR(this, kMips64CvtSL, node);
}

1579 1580 1581 1582
void InstructionSelector::VisitRoundInt64ToFloat64(Node* node) {
  VisitRR(this, kMips64CvtDL, node);
}

1583 1584 1585 1586
void InstructionSelector::VisitRoundUint64ToFloat32(Node* node) {
  VisitRR(this, kMips64CvtSUl, node);
}

1587
void InstructionSelector::VisitRoundUint64ToFloat64(Node* node) {
1588
  VisitRR(this, kMips64CvtDUl, node);
1589 1590
}

1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601
void InstructionSelector::VisitBitcastFloat32ToInt32(Node* node) {
  VisitRR(this, kMips64Float64ExtractLowWord32, node);
}

void InstructionSelector::VisitBitcastFloat64ToInt64(Node* node) {
  VisitRR(this, kMips64BitcastDL, node);
}

void InstructionSelector::VisitBitcastInt32ToFloat32(Node* node) {
  Mips64OperandGenerator g(this);
  Emit(kMips64Float64InsertLowWord32, g.DefineAsRegister(node),
1602
       ImmediateOperand(ImmediateOperand::INLINE_INT32, 0),
1603 1604 1605 1606 1607 1608 1609
       g.UseRegister(node->InputAt(0)));
}

void InstructionSelector::VisitBitcastInt64ToFloat64(Node* node) {
  VisitRR(this, kMips64BitcastLD, node);
}

1610
void InstructionSelector::VisitFloat32Add(Node* node) {
1611 1612
  // Optimization with Madd.S(z, x, y) is intentionally removed.
  // See explanation for madd_s in assembler-mips64.cc.
1613
  VisitRRR(this, kMips64AddS, node);
1614 1615 1616
}

void InstructionSelector::VisitFloat64Add(Node* node) {
1617 1618
  // Optimization with Madd.D(z, x, y) is intentionally removed.
  // See explanation for madd_d in assembler-mips64.cc.
1619 1620 1621
  VisitRRR(this, kMips64AddD, node);
}

1622
void InstructionSelector::VisitFloat32Sub(Node* node) {
1623 1624
  // Optimization with Msub.S(z, x, y) is intentionally removed.
  // See explanation for madd_s in assembler-mips64.cc.
1625 1626 1627
  VisitRRR(this, kMips64SubS, node);
}

1628
void InstructionSelector::VisitFloat64Sub(Node* node) {
1629 1630
  // Optimization with Msub.D(z, x, y) is intentionally removed.
  // See explanation for madd_d in assembler-mips64.cc.
1631 1632 1633
  VisitRRR(this, kMips64SubD, node);
}

1634 1635 1636 1637
void InstructionSelector::VisitFloat32Mul(Node* node) {
  VisitRRR(this, kMips64MulS, node);
}

1638 1639 1640 1641
void InstructionSelector::VisitFloat64Mul(Node* node) {
  VisitRRR(this, kMips64MulD, node);
}

1642 1643 1644 1645
void InstructionSelector::VisitFloat32Div(Node* node) {
  VisitRRR(this, kMips64DivS, node);
}

1646 1647 1648 1649 1650 1651 1652
void InstructionSelector::VisitFloat64Div(Node* node) {
  VisitRRR(this, kMips64DivD, node);
}

void InstructionSelector::VisitFloat64Mod(Node* node) {
  Mips64OperandGenerator g(this);
  Emit(kMips64ModD, g.DefineAsFixed(node, f0),
1653 1654
       g.UseFixed(node->InputAt(0), f12), g.UseFixed(node->InputAt(1), f14))
      ->MarkAsCall();
1655 1656
}

1657 1658 1659 1660 1661
void InstructionSelector::VisitFloat32Max(Node* node) {
  Mips64OperandGenerator g(this);
  Emit(kMips64Float32Max, g.DefineAsRegister(node),
       g.UseRegister(node->InputAt(0)), g.UseRegister(node->InputAt(1)));
}
1662

1663 1664
void InstructionSelector::VisitFloat64Max(Node* node) {
  Mips64OperandGenerator g(this);
1665 1666
  Emit(kMips64Float64Max, g.DefineAsRegister(node),
       g.UseRegister(node->InputAt(0)), g.UseRegister(node->InputAt(1)));
1667
}
1668

1669 1670 1671 1672 1673
void InstructionSelector::VisitFloat32Min(Node* node) {
  Mips64OperandGenerator g(this);
  Emit(kMips64Float32Min, g.DefineAsRegister(node),
       g.UseRegister(node->InputAt(0)), g.UseRegister(node->InputAt(1)));
}
1674

1675 1676
void InstructionSelector::VisitFloat64Min(Node* node) {
  Mips64OperandGenerator g(this);
1677 1678
  Emit(kMips64Float64Min, g.DefineAsRegister(node),
       g.UseRegister(node->InputAt(0)), g.UseRegister(node->InputAt(1)));
1679
}
1680

1681 1682 1683
void InstructionSelector::VisitFloat32Abs(Node* node) {
  VisitRR(this, kMips64AbsS, node);
}
1684

1685 1686 1687
void InstructionSelector::VisitFloat64Abs(Node* node) {
  VisitRR(this, kMips64AbsD, node);
}
1688

1689 1690 1691 1692
void InstructionSelector::VisitFloat32Sqrt(Node* node) {
  VisitRR(this, kMips64SqrtS, node);
}

1693
void InstructionSelector::VisitFloat64Sqrt(Node* node) {
1694
  VisitRR(this, kMips64SqrtD, node);
1695 1696
}

1697 1698 1699
void InstructionSelector::VisitFloat32RoundDown(Node* node) {
  VisitRR(this, kMips64Float32RoundDown, node);
}
1700

1701 1702
void InstructionSelector::VisitFloat64RoundDown(Node* node) {
  VisitRR(this, kMips64Float64RoundDown, node);
1703 1704
}

1705 1706 1707
void InstructionSelector::VisitFloat32RoundUp(Node* node) {
  VisitRR(this, kMips64Float32RoundUp, node);
}
1708

1709 1710 1711 1712
void InstructionSelector::VisitFloat64RoundUp(Node* node) {
  VisitRR(this, kMips64Float64RoundUp, node);
}

1713
void InstructionSelector::VisitFloat32RoundTruncate(Node* node) {
1714
  VisitRR(this, kMips64Float32RoundTruncate, node);
1715 1716
}

1717
void InstructionSelector::VisitFloat64RoundTruncate(Node* node) {
1718
  VisitRR(this, kMips64Float64RoundTruncate, node);
1719 1720 1721 1722 1723 1724
}

void InstructionSelector::VisitFloat64RoundTiesAway(Node* node) {
  UNREACHABLE();
}

1725
void InstructionSelector::VisitFloat32RoundTiesEven(Node* node) {
1726
  VisitRR(this, kMips64Float32RoundTiesEven, node);
1727 1728
}

1729 1730 1731 1732
void InstructionSelector::VisitFloat64RoundTiesEven(Node* node) {
  VisitRR(this, kMips64Float64RoundTiesEven, node);
}

1733 1734 1735
void InstructionSelector::VisitFloat32Neg(Node* node) {
  VisitRR(this, kMips64NegS, node);
}
1736

1737 1738 1739
void InstructionSelector::VisitFloat64Neg(Node* node) {
  VisitRR(this, kMips64NegD, node);
}
1740

1741 1742 1743
void InstructionSelector::VisitFloat64Ieee754Binop(Node* node,
                                                   InstructionCode opcode) {
  Mips64OperandGenerator g(this);
1744 1745
  Emit(opcode, g.DefineAsFixed(node, f0), g.UseFixed(node->InputAt(0), f2),
       g.UseFixed(node->InputAt(1), f4))
1746 1747 1748
      ->MarkAsCall();
}

1749 1750 1751 1752 1753 1754 1755
void InstructionSelector::VisitFloat64Ieee754Unop(Node* node,
                                                  InstructionCode opcode) {
  Mips64OperandGenerator g(this);
  Emit(opcode, g.DefineAsFixed(node, f0), g.UseFixed(node->InputAt(0), f12))
      ->MarkAsCall();
}

1756
void InstructionSelector::EmitPrepareArguments(
1757
    ZoneVector<PushParameter>* arguments, const CallDescriptor* call_descriptor,
1758
    Node* node) {
1759
  Mips64OperandGenerator g(this);
1760 1761

  // Prepare for C function call.
1762 1763 1764
  if (call_descriptor->IsCFunctionCall()) {
    Emit(kArchPrepareCallCFunction | MiscField::encode(static_cast<int>(
                                         call_descriptor->ParameterCount())),
1765 1766 1767 1768
         0, nullptr, 0, nullptr);

    // Poke any stack arguments.
    int slot = kCArgSlotCount;
1769
    for (PushParameter input : (*arguments)) {
1770
      Emit(kMips64StoreToStackSlot, g.NoOutput(), g.UseRegister(input.node),
1771
           g.TempImmediate(slot << kSystemPointerSizeLog2));
1772 1773 1774
      ++slot;
    }
  } else {
1775
    int push_count = static_cast<int>(call_descriptor->ParameterSlotCount());
1776
    if (push_count > 0) {
1777 1778 1779 1780 1781 1782 1783
      // Calculate needed space
      int stack_size = 0;
      for (PushParameter input : (*arguments)) {
        if (input.node) {
          stack_size += input.location.GetSizeInPointers();
        }
      }
1784
      Emit(kMips64StackClaim, g.NoOutput(),
1785
           g.TempImmediate(stack_size << kSystemPointerSizeLog2));
1786
    }
1787
    for (size_t n = 0; n < arguments->size(); ++n) {
1788
      PushParameter input = (*arguments)[n];
1789 1790
      if (input.node) {
        Emit(kMips64StoreToStackSlot, g.NoOutput(), g.UseRegister(input.node),
1791
             g.TempImmediate(static_cast<int>(n << kSystemPointerSizeLog2)));
1792
      }
1793
    }
1794
  }
1795 1796
}

1797 1798 1799
void InstructionSelector::EmitPrepareResults(
    ZoneVector<PushParameter>* results, const CallDescriptor* call_descriptor,
    Node* node) {
1800 1801 1802 1803 1804 1805
  Mips64OperandGenerator g(this);

  for (PushParameter output : *results) {
    if (!output.location.IsCallerFrameSlot()) continue;
    // Skip any alignment holes in nodes.
    if (output.node != nullptr) {
1806
      DCHECK(!call_descriptor->IsCFunctionCall());
1807 1808 1809 1810
      if (output.location.GetType() == MachineType::Float32()) {
        MarkAsFloat32(output.node);
      } else if (output.location.GetType() == MachineType::Float64()) {
        MarkAsFloat64(output.node);
1811 1812
      } else if (output.location.GetType() == MachineType::Simd128()) {
        MarkAsSimd128(output.node);
1813
      }
1814 1815
      int offset = call_descriptor->GetOffsetToReturns();
      int reverse_slot = -output.location.GetLocation() - offset;
1816 1817
      Emit(kMips64Peek, g.DefineAsRegister(output.node),
           g.UseImmediate(reverse_slot));
1818 1819
    }
  }
1820
}
1821

1822
bool InstructionSelector::IsTailCallAddressImmediate() { return false; }
1823

1824
void InstructionSelector::VisitUnalignedLoad(Node* node) {
1825
  LoadRepresentation load_rep = LoadRepresentationOf(node->op());
1826 1827 1828 1829
  Mips64OperandGenerator g(this);
  Node* base = node->InputAt(0);
  Node* index = node->InputAt(1);

1830
  ArchOpcode opcode;
1831 1832 1833 1834 1835 1836 1837 1838
  switch (load_rep.representation()) {
    case MachineRepresentation::kFloat32:
      opcode = kMips64Ulwc1;
      break;
    case MachineRepresentation::kFloat64:
      opcode = kMips64Uldc1;
      break;
    case MachineRepresentation::kWord8:
1839 1840
      opcode = load_rep.IsUnsigned() ? kMips64Lbu : kMips64Lb;
      break;
1841 1842 1843 1844 1845 1846
    case MachineRepresentation::kWord16:
      opcode = load_rep.IsUnsigned() ? kMips64Ulhu : kMips64Ulh;
      break;
    case MachineRepresentation::kWord32:
      opcode = load_rep.IsUnsigned() ? kMips64Ulwu : kMips64Ulw;
      break;
1847 1848
    case MachineRepresentation::kTaggedSigned:   // Fall through.
    case MachineRepresentation::kTaggedPointer:  // Fall through.
1849
    case MachineRepresentation::kTagged:         // Fall through.
1850 1851 1852
    case MachineRepresentation::kWord64:
      opcode = kMips64Uld;
      break;
1853 1854 1855
    case MachineRepresentation::kSimd128:
      opcode = kMips64MsaLd;
      break;
1856
    case MachineRepresentation::kBit:                // Fall through.
1857 1858
    case MachineRepresentation::kCompressedPointer:  // Fall through.
    case MachineRepresentation::kCompressed:         // Fall through.
1859
    case MachineRepresentation::kMapWord:            // Fall through.
1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883
    case MachineRepresentation::kNone:
      UNREACHABLE();
  }

  if (g.CanBeImmediate(index, opcode)) {
    Emit(opcode | AddressingModeField::encode(kMode_MRI),
         g.DefineAsRegister(node), g.UseRegister(base), g.UseImmediate(index));
  } else {
    InstructionOperand addr_reg = g.TempRegister();
    Emit(kMips64Dadd | AddressingModeField::encode(kMode_None), addr_reg,
         g.UseRegister(index), g.UseRegister(base));
    // Emit desired load opcode, using temp addr_reg.
    Emit(opcode | AddressingModeField::encode(kMode_MRI),
         g.DefineAsRegister(node), addr_reg, g.TempImmediate(0));
  }
}

void InstructionSelector::VisitUnalignedStore(Node* node) {
  Mips64OperandGenerator g(this);
  Node* base = node->InputAt(0);
  Node* index = node->InputAt(1);
  Node* value = node->InputAt(2);

  UnalignedStoreRepresentation rep = UnalignedStoreRepresentationOf(node->op());
1884
  ArchOpcode opcode;
1885 1886 1887 1888 1889 1890 1891 1892
  switch (rep) {
    case MachineRepresentation::kFloat32:
      opcode = kMips64Uswc1;
      break;
    case MachineRepresentation::kFloat64:
      opcode = kMips64Usdc1;
      break;
    case MachineRepresentation::kWord8:
1893 1894
      opcode = kMips64Sb;
      break;
1895 1896 1897 1898 1899 1900
    case MachineRepresentation::kWord16:
      opcode = kMips64Ush;
      break;
    case MachineRepresentation::kWord32:
      opcode = kMips64Usw;
      break;
1901 1902
    case MachineRepresentation::kTaggedSigned:   // Fall through.
    case MachineRepresentation::kTaggedPointer:  // Fall through.
1903
    case MachineRepresentation::kTagged:         // Fall through.
1904 1905 1906
    case MachineRepresentation::kWord64:
      opcode = kMips64Usd;
      break;
1907 1908 1909
    case MachineRepresentation::kSimd128:
      opcode = kMips64MsaSt;
      break;
1910
    case MachineRepresentation::kBit:                // Fall through.
1911 1912
    case MachineRepresentation::kCompressedPointer:  // Fall through.
    case MachineRepresentation::kCompressed:         // Fall through.
1913
    case MachineRepresentation::kMapWord:            // Fall through.
1914 1915 1916 1917 1918 1919
    case MachineRepresentation::kNone:
      UNREACHABLE();
  }

  if (g.CanBeImmediate(index, opcode)) {
    Emit(opcode | AddressingModeField::encode(kMode_MRI), g.NoOutput(),
1920 1921
         g.UseRegister(base), g.UseImmediate(index),
         g.UseRegisterOrImmediateZero(value));
1922 1923 1924 1925 1926 1927
  } else {
    InstructionOperand addr_reg = g.TempRegister();
    Emit(kMips64Dadd | AddressingModeField::encode(kMode_None), addr_reg,
         g.UseRegister(index), g.UseRegister(base));
    // Emit desired store opcode, using temp addr_reg.
    Emit(opcode | AddressingModeField::encode(kMode_MRI), g.NoOutput(),
1928
         addr_reg, g.TempImmediate(0), g.UseRegisterOrImmediateZero(value));
1929 1930 1931
  }
}

1932 1933 1934 1935
namespace {

// Shared routine for multiple compare operations.
static void VisitCompare(InstructionSelector* selector, InstructionCode opcode,
1936
                         InstructionOperand left, InstructionOperand right,
1937
                         FlagsContinuation* cont) {
1938
  selector->EmitWithContinuation(opcode, left, right, cont);
1939 1940
}

1941 1942 1943 1944
// Shared routine for multiple float32 compare operations.
void VisitFloat32Compare(InstructionSelector* selector, Node* node,
                         FlagsContinuation* cont) {
  Mips64OperandGenerator g(selector);
1945 1946 1947 1948 1949 1950 1951 1952
  Float32BinopMatcher m(node);
  InstructionOperand lhs, rhs;

  lhs = m.left().IsZero() ? g.UseImmediate(m.left().node())
                          : g.UseRegister(m.left().node());
  rhs = m.right().IsZero() ? g.UseImmediate(m.right().node())
                           : g.UseRegister(m.right().node());
  VisitCompare(selector, kMips64CmpS, lhs, rhs, cont);
1953 1954 1955
}

// Shared routine for multiple float64 compare operations.
1956 1957 1958
void VisitFloat64Compare(InstructionSelector* selector, Node* node,
                         FlagsContinuation* cont) {
  Mips64OperandGenerator g(selector);
1959 1960 1961 1962 1963 1964 1965 1966
  Float64BinopMatcher m(node);
  InstructionOperand lhs, rhs;

  lhs = m.left().IsZero() ? g.UseImmediate(m.left().node())
                          : g.UseRegister(m.left().node());
  rhs = m.right().IsZero() ? g.UseImmediate(m.right().node())
                           : g.UseRegister(m.right().node());
  VisitCompare(selector, kMips64CmpD, lhs, rhs, cont);
1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977
}

// Shared routine for multiple word compare operations.
void VisitWordCompare(InstructionSelector* selector, Node* node,
                      InstructionCode opcode, FlagsContinuation* cont,
                      bool commutative) {
  Mips64OperandGenerator g(selector);
  Node* left = node->InputAt(0);
  Node* right = node->InputAt(1);

  // Match immediates on left or right side of comparison.
1978
  if (g.CanBeImmediate(right, opcode)) {
1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997
    if (opcode == kMips64Tst) {
      VisitCompare(selector, opcode, g.UseRegister(left), g.UseImmediate(right),
                   cont);
    } else {
      switch (cont->condition()) {
        case kEqual:
        case kNotEqual:
          if (cont->IsSet()) {
            VisitCompare(selector, opcode, g.UseRegister(left),
                         g.UseImmediate(right), cont);
          } else {
            VisitCompare(selector, opcode, g.UseRegister(left),
                         g.UseRegister(right), cont);
          }
          break;
        case kSignedLessThan:
        case kSignedGreaterThanOrEqual:
        case kUnsignedLessThan:
        case kUnsignedGreaterThanOrEqual:
1998 1999
          VisitCompare(selector, opcode, g.UseRegister(left),
                       g.UseImmediate(right), cont);
2000 2001
          break;
        default:
2002 2003
          VisitCompare(selector, opcode, g.UseRegister(left),
                       g.UseRegister(right), cont);
2004
      }
2005
    }
2006
  } else if (g.CanBeImmediate(left, opcode)) {
2007
    if (!commutative) cont->Commute();
2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026
    if (opcode == kMips64Tst) {
      VisitCompare(selector, opcode, g.UseRegister(right), g.UseImmediate(left),
                   cont);
    } else {
      switch (cont->condition()) {
        case kEqual:
        case kNotEqual:
          if (cont->IsSet()) {
            VisitCompare(selector, opcode, g.UseRegister(right),
                         g.UseImmediate(left), cont);
          } else {
            VisitCompare(selector, opcode, g.UseRegister(right),
                         g.UseRegister(left), cont);
          }
          break;
        case kSignedLessThan:
        case kSignedGreaterThanOrEqual:
        case kUnsignedLessThan:
        case kUnsignedGreaterThanOrEqual:
2027 2028
          VisitCompare(selector, opcode, g.UseRegister(right),
                       g.UseImmediate(left), cont);
2029 2030
          break;
        default:
2031 2032
          VisitCompare(selector, opcode, g.UseRegister(right),
                       g.UseRegister(left), cont);
2033
      }
2034
    }
2035 2036 2037 2038 2039 2040
  } else {
    VisitCompare(selector, opcode, g.UseRegister(left), g.UseRegister(right),
                 cont);
  }
}

2041 2042 2043
bool IsNodeUnsigned(Node* n) {
  NodeMatcher m(n);

2044 2045
  if (m.IsLoad() || m.IsUnalignedLoad() || m.IsPoisonedLoad() ||
      m.IsProtectedLoad() || m.IsWord32AtomicLoad() || m.IsWord64AtomicLoad()) {
2046
    LoadRepresentation load_rep = LoadRepresentationOf(n->op());
2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095
    return load_rep.IsUnsigned();
  } else {
    return m.IsUint32Div() || m.IsUint32LessThan() ||
           m.IsUint32LessThanOrEqual() || m.IsUint32Mod() ||
           m.IsUint32MulHigh() || m.IsChangeFloat64ToUint32() ||
           m.IsTruncateFloat64ToUint32() || m.IsTruncateFloat32ToUint32();
  }
}

// Shared routine for multiple word compare operations.
void VisitFullWord32Compare(InstructionSelector* selector, Node* node,
                            InstructionCode opcode, FlagsContinuation* cont) {
  Mips64OperandGenerator g(selector);
  InstructionOperand leftOp = g.TempRegister();
  InstructionOperand rightOp = g.TempRegister();

  selector->Emit(kMips64Dshl, leftOp, g.UseRegister(node->InputAt(0)),
                 g.TempImmediate(32));
  selector->Emit(kMips64Dshl, rightOp, g.UseRegister(node->InputAt(1)),
                 g.TempImmediate(32));

  VisitCompare(selector, opcode, leftOp, rightOp, cont);
}

void VisitOptimizedWord32Compare(InstructionSelector* selector, Node* node,
                                 InstructionCode opcode,
                                 FlagsContinuation* cont) {
  if (FLAG_debug_code) {
    Mips64OperandGenerator g(selector);
    InstructionOperand leftOp = g.TempRegister();
    InstructionOperand rightOp = g.TempRegister();
    InstructionOperand optimizedResult = g.TempRegister();
    InstructionOperand fullResult = g.TempRegister();
    FlagsCondition condition = cont->condition();
    InstructionCode testOpcode = opcode |
                                 FlagsConditionField::encode(condition) |
                                 FlagsModeField::encode(kFlags_set);

    selector->Emit(testOpcode, optimizedResult, g.UseRegister(node->InputAt(0)),
                   g.UseRegister(node->InputAt(1)));

    selector->Emit(kMips64Dshl, leftOp, g.UseRegister(node->InputAt(0)),
                   g.TempImmediate(32));
    selector->Emit(kMips64Dshl, rightOp, g.UseRegister(node->InputAt(1)),
                   g.TempImmediate(32));
    selector->Emit(testOpcode, fullResult, leftOp, rightOp);

    selector->Emit(
        kMips64AssertEqual, g.NoOutput(), optimizedResult, fullResult,
2096 2097
        g.TempImmediate(
            static_cast<int>(AbortReason::kUnsupportedNonPrimitiveCompare)));
2098 2099 2100 2101
  }

  VisitWordCompare(selector, node, opcode, cont, false);
}
2102 2103 2104

void VisitWord32Compare(InstructionSelector* selector, Node* node,
                        FlagsContinuation* cont) {
2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116
  // MIPS64 doesn't support Word32 compare instructions. Instead it relies
  // that the values in registers are correctly sign-extended and uses
  // Word64 comparison instead. This behavior is correct in most cases,
  // but doesn't work when comparing signed with unsigned operands.
  // We could simulate full Word32 compare in all cases but this would
  // create an unnecessary overhead since unsigned integers are rarely
  // used in JavaScript.
  // The solution proposed here tries to match a comparison of signed
  // with unsigned operand, and perform full Word32Compare only
  // in those cases. Unfortunately, the solution is not complete because
  // it might skip cases where Word32 full compare is needed, so
  // basically it is a hack.
2117 2118 2119 2120 2121
  // When call to a host function in simulator, if the function return a
  // int32 value, the simulator do not sign-extended to int64 because in
  // simulator we do not know the function whether return a int32 or int64.
  // so we need do a full word32 compare in this case.
#ifndef USE_SIMULATOR
2122
  if (IsNodeUnsigned(node->InputAt(0)) != IsNodeUnsigned(node->InputAt(1))) {
2123 2124 2125 2126 2127
#else
  if (IsNodeUnsigned(node->InputAt(0)) != IsNodeUnsigned(node->InputAt(1)) ||
      node->InputAt(0)->opcode() == IrOpcode::kCall ||
      node->InputAt(1)->opcode() == IrOpcode::kCall ) {
#endif
2128 2129 2130 2131
    VisitFullWord32Compare(selector, node, kMips64Cmp, cont);
  } else {
    VisitOptimizedWord32Compare(selector, node, kMips64Cmp, cont);
  }
2132 2133 2134 2135 2136 2137 2138
}

void VisitWord64Compare(InstructionSelector* selector, Node* node,
                        FlagsContinuation* cont) {
  VisitWordCompare(selector, node, kMips64Cmp, cont, false);
}

2139 2140
void EmitWordCompareZero(InstructionSelector* selector, Node* value,
                         FlagsContinuation* cont) {
2141
  Mips64OperandGenerator g(selector);
2142 2143
  selector->EmitWithContinuation(kMips64Cmp, g.UseRegister(value),
                                 g.TempImmediate(0), cont);
2144 2145
}

2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258
void VisitAtomicLoad(InstructionSelector* selector, Node* node,
                     ArchOpcode opcode) {
  Mips64OperandGenerator g(selector);
  Node* base = node->InputAt(0);
  Node* index = node->InputAt(1);
  if (g.CanBeImmediate(index, opcode)) {
    selector->Emit(opcode | AddressingModeField::encode(kMode_MRI),
                   g.DefineAsRegister(node), g.UseRegister(base),
                   g.UseImmediate(index));
  } else {
    InstructionOperand addr_reg = g.TempRegister();
    selector->Emit(kMips64Dadd | AddressingModeField::encode(kMode_None),
                   addr_reg, g.UseRegister(index), g.UseRegister(base));
    // Emit desired load opcode, using temp addr_reg.
    selector->Emit(opcode | AddressingModeField::encode(kMode_MRI),
                   g.DefineAsRegister(node), addr_reg, g.TempImmediate(0));
  }
}

void VisitAtomicStore(InstructionSelector* selector, Node* node,
                      ArchOpcode opcode) {
  Mips64OperandGenerator g(selector);
  Node* base = node->InputAt(0);
  Node* index = node->InputAt(1);
  Node* value = node->InputAt(2);

  if (g.CanBeImmediate(index, opcode)) {
    selector->Emit(opcode | AddressingModeField::encode(kMode_MRI),
                   g.NoOutput(), g.UseRegister(base), g.UseImmediate(index),
                   g.UseRegisterOrImmediateZero(value));
  } else {
    InstructionOperand addr_reg = g.TempRegister();
    selector->Emit(kMips64Dadd | AddressingModeField::encode(kMode_None),
                   addr_reg, g.UseRegister(index), g.UseRegister(base));
    // Emit desired store opcode, using temp addr_reg.
    selector->Emit(opcode | AddressingModeField::encode(kMode_MRI),
                   g.NoOutput(), addr_reg, g.TempImmediate(0),
                   g.UseRegisterOrImmediateZero(value));
  }
}

void VisitAtomicExchange(InstructionSelector* selector, Node* node,
                         ArchOpcode opcode) {
  Mips64OperandGenerator g(selector);
  Node* base = node->InputAt(0);
  Node* index = node->InputAt(1);
  Node* value = node->InputAt(2);

  AddressingMode addressing_mode = kMode_MRI;
  InstructionOperand inputs[3];
  size_t input_count = 0;
  inputs[input_count++] = g.UseUniqueRegister(base);
  inputs[input_count++] = g.UseUniqueRegister(index);
  inputs[input_count++] = g.UseUniqueRegister(value);
  InstructionOperand outputs[1];
  outputs[0] = g.UseUniqueRegister(node);
  InstructionOperand temp[3];
  temp[0] = g.TempRegister();
  temp[1] = g.TempRegister();
  temp[2] = g.TempRegister();
  InstructionCode code = opcode | AddressingModeField::encode(addressing_mode);
  selector->Emit(code, 1, outputs, input_count, inputs, 3, temp);
}

void VisitAtomicCompareExchange(InstructionSelector* selector, Node* node,
                                ArchOpcode opcode) {
  Mips64OperandGenerator g(selector);
  Node* base = node->InputAt(0);
  Node* index = node->InputAt(1);
  Node* old_value = node->InputAt(2);
  Node* new_value = node->InputAt(3);

  AddressingMode addressing_mode = kMode_MRI;
  InstructionOperand inputs[4];
  size_t input_count = 0;
  inputs[input_count++] = g.UseUniqueRegister(base);
  inputs[input_count++] = g.UseUniqueRegister(index);
  inputs[input_count++] = g.UseUniqueRegister(old_value);
  inputs[input_count++] = g.UseUniqueRegister(new_value);
  InstructionOperand outputs[1];
  outputs[0] = g.UseUniqueRegister(node);
  InstructionOperand temp[3];
  temp[0] = g.TempRegister();
  temp[1] = g.TempRegister();
  temp[2] = g.TempRegister();
  InstructionCode code = opcode | AddressingModeField::encode(addressing_mode);
  selector->Emit(code, 1, outputs, input_count, inputs, 3, temp);
}

void VisitAtomicBinop(InstructionSelector* selector, Node* node,
                      ArchOpcode opcode) {
  Mips64OperandGenerator g(selector);
  Node* base = node->InputAt(0);
  Node* index = node->InputAt(1);
  Node* value = node->InputAt(2);

  AddressingMode addressing_mode = kMode_MRI;
  InstructionOperand inputs[3];
  size_t input_count = 0;
  inputs[input_count++] = g.UseUniqueRegister(base);
  inputs[input_count++] = g.UseUniqueRegister(index);
  inputs[input_count++] = g.UseUniqueRegister(value);
  InstructionOperand outputs[1];
  outputs[0] = g.UseUniqueRegister(node);
  InstructionOperand temps[4];
  temps[0] = g.TempRegister();
  temps[1] = g.TempRegister();
  temps[2] = g.TempRegister();
  temps[3] = g.TempRegister();
  InstructionCode code = opcode | AddressingModeField::encode(addressing_mode);
  selector->Emit(code, 1, outputs, input_count, inputs, 4, temps);
}

2259
}  // namespace
2260

2261 2262
void InstructionSelector::VisitStackPointerGreaterThan(
    Node* node, FlagsContinuation* cont) {
2263 2264 2265
  StackCheckKind kind = StackCheckKindOf(node->op());
  InstructionCode opcode =
      kArchStackPointerGreaterThan | MiscField::encode(static_cast<int>(kind));
2266 2267

  Mips64OperandGenerator g(this);
2268 2269 2270 2271 2272

  // No outputs.
  InstructionOperand* const outputs = nullptr;
  const int output_count = 0;

2273
  // TempRegister(0) is used to store the comparison result.
2274 2275 2276
  // Applying an offset to this stack check requires a temp register. Offsets
  // are only applied to the first stack check. If applying an offset, we must
  // ensure the input and temp registers do not alias, thus kUniqueRegister.
2277 2278
  InstructionOperand temps[] = {g.TempRegister(), g.TempRegister()};
  const int temp_count = (kind == StackCheckKind::kJSFunctionEntry ? 2 : 1);
2279 2280 2281 2282 2283 2284 2285 2286 2287 2288
  const auto register_mode = (kind == StackCheckKind::kJSFunctionEntry)
                                 ? OperandGenerator::kUniqueRegister
                                 : OperandGenerator::kRegister;

  Node* const value = node->InputAt(0);
  InstructionOperand inputs[] = {g.UseRegisterWithMode(value, register_mode)};
  static constexpr int input_count = arraysize(inputs);

  EmitWithContinuation(opcode, output_count, outputs, input_count, inputs,
                       temp_count, temps, cont);
2289 2290
}

2291
// Shared routine for word comparisons against zero.
2292 2293
void InstructionSelector::VisitWordCompareZero(Node* user, Node* value,
                                               FlagsContinuation* cont) {
2294
  // Try to combine with comparisons against 0 by simply inverting the branch.
2295
  while (CanCover(user, value)) {
2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312
    if (value->opcode() == IrOpcode::kWord32Equal) {
      Int32BinopMatcher m(value);
      if (!m.right().Is(0)) break;
      user = value;
      value = m.left().node();
    } else if (value->opcode() == IrOpcode::kWord64Equal) {
      Int64BinopMatcher m(value);
      if (!m.right().Is(0)) break;
      user = value;
      value = m.left().node();
    } else {
      break;
    }

    cont->Negate();
  }

2313
  if (CanCover(user, value)) {
2314
    switch (value->opcode()) {
2315
      case IrOpcode::kWord32Equal:
2316
        cont->OverwriteAndNegateIfEqual(kEqual);
2317
        return VisitWord32Compare(this, value, cont);
2318 2319
      case IrOpcode::kInt32LessThan:
        cont->OverwriteAndNegateIfEqual(kSignedLessThan);
2320
        return VisitWord32Compare(this, value, cont);
2321 2322
      case IrOpcode::kInt32LessThanOrEqual:
        cont->OverwriteAndNegateIfEqual(kSignedLessThanOrEqual);
2323
        return VisitWord32Compare(this, value, cont);
2324 2325
      case IrOpcode::kUint32LessThan:
        cont->OverwriteAndNegateIfEqual(kUnsignedLessThan);
2326
        return VisitWord32Compare(this, value, cont);
2327 2328
      case IrOpcode::kUint32LessThanOrEqual:
        cont->OverwriteAndNegateIfEqual(kUnsignedLessThanOrEqual);
2329
        return VisitWord32Compare(this, value, cont);
2330
      case IrOpcode::kWord64Equal:
2331
        cont->OverwriteAndNegateIfEqual(kEqual);
2332
        return VisitWord64Compare(this, value, cont);
2333 2334
      case IrOpcode::kInt64LessThan:
        cont->OverwriteAndNegateIfEqual(kSignedLessThan);
2335
        return VisitWord64Compare(this, value, cont);
2336 2337
      case IrOpcode::kInt64LessThanOrEqual:
        cont->OverwriteAndNegateIfEqual(kSignedLessThanOrEqual);
2338
        return VisitWord64Compare(this, value, cont);
2339 2340
      case IrOpcode::kUint64LessThan:
        cont->OverwriteAndNegateIfEqual(kUnsignedLessThan);
2341
        return VisitWord64Compare(this, value, cont);
2342 2343
      case IrOpcode::kUint64LessThanOrEqual:
        cont->OverwriteAndNegateIfEqual(kUnsignedLessThanOrEqual);
2344
        return VisitWord64Compare(this, value, cont);
2345 2346
      case IrOpcode::kFloat32Equal:
        cont->OverwriteAndNegateIfEqual(kEqual);
2347
        return VisitFloat32Compare(this, value, cont);
2348 2349
      case IrOpcode::kFloat32LessThan:
        cont->OverwriteAndNegateIfEqual(kUnsignedLessThan);
2350
        return VisitFloat32Compare(this, value, cont);
2351 2352
      case IrOpcode::kFloat32LessThanOrEqual:
        cont->OverwriteAndNegateIfEqual(kUnsignedLessThanOrEqual);
2353
        return VisitFloat32Compare(this, value, cont);
2354
      case IrOpcode::kFloat64Equal:
2355
        cont->OverwriteAndNegateIfEqual(kEqual);
2356
        return VisitFloat64Compare(this, value, cont);
2357
      case IrOpcode::kFloat64LessThan:
2358
        cont->OverwriteAndNegateIfEqual(kUnsignedLessThan);
2359
        return VisitFloat64Compare(this, value, cont);
2360
      case IrOpcode::kFloat64LessThanOrEqual:
2361
        cont->OverwriteAndNegateIfEqual(kUnsignedLessThanOrEqual);
2362
        return VisitFloat64Compare(this, value, cont);
2363 2364 2365
      case IrOpcode::kProjection:
        // Check if this is the overflow output projection of an
        // <Operation>WithOverflow node.
2366
        if (ProjectionIndexOf(value->op()) == 1u) {
2367 2368
          // We cannot combine the <Operation>WithOverflow with this branch
          // unless the 0th projection (the use of the actual value of the
2369
          // <Operation> is either nullptr, which means there's no use of the
2370 2371
          // actual value, or was already defined, which means it is scheduled
          // *AFTER* this branch).
2372 2373
          Node* const node = value->InputAt(0);
          Node* const result = NodeProperties::FindProjection(node, 0);
2374
          if (result == nullptr || IsDefined(result)) {
2375 2376 2377
            switch (node->opcode()) {
              case IrOpcode::kInt32AddWithOverflow:
                cont->OverwriteAndNegateIfEqual(kOverflow);
2378
                return VisitBinop(this, node, kMips64Dadd, cont);
2379 2380
              case IrOpcode::kInt32SubWithOverflow:
                cont->OverwriteAndNegateIfEqual(kOverflow);
2381
                return VisitBinop(this, node, kMips64Dsub, cont);
2382 2383
              case IrOpcode::kInt32MulWithOverflow:
                cont->OverwriteAndNegateIfEqual(kOverflow);
2384
                return VisitBinop(this, node, kMips64MulOvf, cont);
2385 2386
              case IrOpcode::kInt64AddWithOverflow:
                cont->OverwriteAndNegateIfEqual(kOverflow);
2387
                return VisitBinop(this, node, kMips64DaddOvf, cont);
2388 2389
              case IrOpcode::kInt64SubWithOverflow:
                cont->OverwriteAndNegateIfEqual(kOverflow);
2390
                return VisitBinop(this, node, kMips64DsubOvf, cont);
2391 2392 2393 2394 2395 2396 2397 2398
              default:
                break;
            }
          }
        }
        break;
      case IrOpcode::kWord32And:
      case IrOpcode::kWord64And:
2399
        return VisitWordCompare(this, value, kMips64Tst, cont, true);
2400 2401 2402
      case IrOpcode::kStackPointerGreaterThan:
        cont->OverwriteAndNegateIfEqual(kStackPointerGreaterThanCondition);
        return VisitStackPointerGreaterThan(value, cont);
2403 2404 2405 2406 2407 2408
      default:
        break;
    }
  }

  // Continuation could not be combined with a compare, emit compare against 0.
2409
  EmitWordCompareZero(this, value, cont);
2410
}
2411

2412
void InstructionSelector::VisitSwitch(Node* node, const SwitchInfo& sw) {
2413 2414 2415
  Mips64OperandGenerator g(this);
  InstructionOperand value_operand = g.UseRegister(node->InputAt(0));

2416
  // Emit either ArchTableSwitch or ArchBinarySearchSwitch.
2417 2418
  if (enable_switch_jump_table_ == kEnableSwitchJumpTable) {
    static const size_t kMaxTableSwitchValueRange = 2 << 16;
2419
    size_t table_space_cost = 10 + 2 * sw.value_range();
2420
    size_t table_time_cost = 3;
2421 2422 2423
    size_t lookup_space_cost = 2 + 2 * sw.case_count();
    size_t lookup_time_cost = sw.case_count();
    if (sw.case_count() > 0 &&
2424 2425
        table_space_cost + 3 * table_time_cost <=
            lookup_space_cost + 3 * lookup_time_cost &&
2426 2427
        sw.min_value() > std::numeric_limits<int32_t>::min() &&
        sw.value_range() <= kMaxTableSwitchValueRange) {
2428
      InstructionOperand index_operand = value_operand;
2429
      if (sw.min_value()) {
2430 2431
        index_operand = g.TempRegister();
        Emit(kMips64Sub, index_operand, value_operand,
2432
             g.TempImmediate(sw.min_value()));
2433 2434 2435
      }
      // Generate a table lookup.
      return EmitTableSwitch(sw, index_operand);
2436 2437 2438
    }
  }

2439 2440
  // Generate a tree of conditional jumps.
  return EmitBinarySearchSwitch(sw, value_operand);
2441 2442
}

2443
void InstructionSelector::VisitWord32Equal(Node* const node) {
2444
  FlagsContinuation cont = FlagsContinuation::ForSet(kEqual, node);
2445 2446
  Int32BinopMatcher m(node);
  if (m.right().Is(0)) {
2447
    return VisitWordCompareZero(m.node(), m.left().node(), &cont);
2448 2449 2450 2451 2452 2453
  }

  VisitWord32Compare(this, node, &cont);
}

void InstructionSelector::VisitInt32LessThan(Node* node) {
2454
  FlagsContinuation cont = FlagsContinuation::ForSet(kSignedLessThan, node);
2455 2456 2457 2458
  VisitWord32Compare(this, node, &cont);
}

void InstructionSelector::VisitInt32LessThanOrEqual(Node* node) {
2459 2460
  FlagsContinuation cont =
      FlagsContinuation::ForSet(kSignedLessThanOrEqual, node);
2461 2462 2463 2464
  VisitWord32Compare(this, node, &cont);
}

void InstructionSelector::VisitUint32LessThan(Node* node) {
2465
  FlagsContinuation cont = FlagsContinuation::ForSet(kUnsignedLessThan, node);
2466 2467 2468 2469
  VisitWord32Compare(this, node, &cont);
}

void InstructionSelector::VisitUint32LessThanOrEqual(Node* node) {
2470 2471
  FlagsContinuation cont =
      FlagsContinuation::ForSet(kUnsignedLessThanOrEqual, node);
2472 2473 2474 2475
  VisitWord32Compare(this, node, &cont);
}

void InstructionSelector::VisitInt32AddWithOverflow(Node* node) {
2476
  if (Node* ovf = NodeProperties::FindProjection(node, 1)) {
2477
    FlagsContinuation cont = FlagsContinuation::ForSet(kOverflow, ovf);
2478 2479 2480 2481 2482 2483 2484
    return VisitBinop(this, node, kMips64Dadd, &cont);
  }
  FlagsContinuation cont;
  VisitBinop(this, node, kMips64Dadd, &cont);
}

void InstructionSelector::VisitInt32SubWithOverflow(Node* node) {
2485
  if (Node* ovf = NodeProperties::FindProjection(node, 1)) {
2486
    FlagsContinuation cont = FlagsContinuation::ForSet(kOverflow, ovf);
2487 2488 2489 2490 2491 2492
    return VisitBinop(this, node, kMips64Dsub, &cont);
  }
  FlagsContinuation cont;
  VisitBinop(this, node, kMips64Dsub, &cont);
}

2493 2494 2495 2496 2497 2498 2499 2500
void InstructionSelector::VisitInt32MulWithOverflow(Node* node) {
  if (Node* ovf = NodeProperties::FindProjection(node, 1)) {
    FlagsContinuation cont = FlagsContinuation::ForSet(kOverflow, ovf);
    return VisitBinop(this, node, kMips64MulOvf, &cont);
  }
  FlagsContinuation cont;
  VisitBinop(this, node, kMips64MulOvf, &cont);
}
2501

2502 2503
void InstructionSelector::VisitInt64AddWithOverflow(Node* node) {
  if (Node* ovf = NodeProperties::FindProjection(node, 1)) {
2504
    FlagsContinuation cont = FlagsContinuation::ForSet(kOverflow, ovf);
2505 2506 2507 2508 2509 2510 2511 2512
    return VisitBinop(this, node, kMips64DaddOvf, &cont);
  }
  FlagsContinuation cont;
  VisitBinop(this, node, kMips64DaddOvf, &cont);
}

void InstructionSelector::VisitInt64SubWithOverflow(Node* node) {
  if (Node* ovf = NodeProperties::FindProjection(node, 1)) {
2513
    FlagsContinuation cont = FlagsContinuation::ForSet(kOverflow, ovf);
2514 2515 2516 2517 2518 2519
    return VisitBinop(this, node, kMips64DsubOvf, &cont);
  }
  FlagsContinuation cont;
  VisitBinop(this, node, kMips64DsubOvf, &cont);
}

2520
void InstructionSelector::VisitWord64Equal(Node* const node) {
2521
  FlagsContinuation cont = FlagsContinuation::ForSet(kEqual, node);
2522 2523
  Int64BinopMatcher m(node);
  if (m.right().Is(0)) {
2524
    return VisitWordCompareZero(m.node(), m.left().node(), &cont);
2525 2526 2527 2528 2529 2530
  }

  VisitWord64Compare(this, node, &cont);
}

void InstructionSelector::VisitInt64LessThan(Node* node) {
2531
  FlagsContinuation cont = FlagsContinuation::ForSet(kSignedLessThan, node);
2532 2533 2534 2535
  VisitWord64Compare(this, node, &cont);
}

void InstructionSelector::VisitInt64LessThanOrEqual(Node* node) {
2536 2537
  FlagsContinuation cont =
      FlagsContinuation::ForSet(kSignedLessThanOrEqual, node);
2538 2539 2540 2541
  VisitWord64Compare(this, node, &cont);
}

void InstructionSelector::VisitUint64LessThan(Node* node) {
2542
  FlagsContinuation cont = FlagsContinuation::ForSet(kUnsignedLessThan, node);
2543 2544 2545
  VisitWord64Compare(this, node, &cont);
}

2546
void InstructionSelector::VisitUint64LessThanOrEqual(Node* node) {
2547 2548
  FlagsContinuation cont =
      FlagsContinuation::ForSet(kUnsignedLessThanOrEqual, node);
2549 2550 2551
  VisitWord64Compare(this, node, &cont);
}

2552
void InstructionSelector::VisitFloat32Equal(Node* node) {
2553
  FlagsContinuation cont = FlagsContinuation::ForSet(kEqual, node);
2554 2555 2556 2557
  VisitFloat32Compare(this, node, &cont);
}

void InstructionSelector::VisitFloat32LessThan(Node* node) {
2558
  FlagsContinuation cont = FlagsContinuation::ForSet(kUnsignedLessThan, node);
2559 2560 2561 2562
  VisitFloat32Compare(this, node, &cont);
}

void InstructionSelector::VisitFloat32LessThanOrEqual(Node* node) {
2563 2564
  FlagsContinuation cont =
      FlagsContinuation::ForSet(kUnsignedLessThanOrEqual, node);
2565 2566 2567
  VisitFloat32Compare(this, node, &cont);
}

2568
void InstructionSelector::VisitFloat64Equal(Node* node) {
2569
  FlagsContinuation cont = FlagsContinuation::ForSet(kEqual, node);
2570 2571 2572 2573
  VisitFloat64Compare(this, node, &cont);
}

void InstructionSelector::VisitFloat64LessThan(Node* node) {
2574
  FlagsContinuation cont = FlagsContinuation::ForSet(kUnsignedLessThan, node);
2575 2576 2577 2578
  VisitFloat64Compare(this, node, &cont);
}

void InstructionSelector::VisitFloat64LessThanOrEqual(Node* node) {
2579 2580
  FlagsContinuation cont =
      FlagsContinuation::ForSet(kUnsignedLessThanOrEqual, node);
2581 2582 2583
  VisitFloat64Compare(this, node, &cont);
}

2584
void InstructionSelector::VisitFloat64ExtractLowWord32(Node* node) {
2585
  VisitRR(this, kMips64Float64ExtractLowWord32, node);
2586 2587 2588
}

void InstructionSelector::VisitFloat64ExtractHighWord32(Node* node) {
2589
  VisitRR(this, kMips64Float64ExtractHighWord32, node);
2590 2591
}

2592 2593 2594
void InstructionSelector::VisitFloat64SilenceNaN(Node* node) {
  VisitRR(this, kMips64Float64SilenceNaN, node);
}
2595 2596 2597 2598 2599

void InstructionSelector::VisitFloat64InsertLowWord32(Node* node) {
  Mips64OperandGenerator g(this);
  Node* left = node->InputAt(0);
  Node* right = node->InputAt(1);
2600 2601
  Emit(kMips64Float64InsertLowWord32, g.DefineSameAsFirst(node),
       g.UseRegister(left), g.UseRegister(right));
2602 2603 2604 2605 2606 2607
}

void InstructionSelector::VisitFloat64InsertHighWord32(Node* node) {
  Mips64OperandGenerator g(this);
  Node* left = node->InputAt(0);
  Node* right = node->InputAt(1);
2608 2609
  Emit(kMips64Float64InsertHighWord32, g.DefineSameAsFirst(node),
       g.UseRegister(left), g.UseRegister(right));
2610 2611
}

2612 2613 2614 2615 2616
void InstructionSelector::VisitMemoryBarrier(Node* node) {
  Mips64OperandGenerator g(this);
  Emit(kMips64Sync, g.NoOutput());
}

2617
void InstructionSelector::VisitWord32AtomicLoad(Node* node) {
2618
  LoadRepresentation load_rep = LoadRepresentationOf(node->op());
2619
  ArchOpcode opcode;
2620 2621
  switch (load_rep.representation()) {
    case MachineRepresentation::kWord8:
2622 2623
      opcode =
          load_rep.IsSigned() ? kWord32AtomicLoadInt8 : kWord32AtomicLoadUint8;
2624 2625
      break;
    case MachineRepresentation::kWord16:
2626 2627
      opcode = load_rep.IsSigned() ? kWord32AtomicLoadInt16
                                   : kWord32AtomicLoadUint16;
2628 2629
      break;
    case MachineRepresentation::kWord32:
2630
      opcode = kWord32AtomicLoadWord32;
2631 2632 2633 2634
      break;
    default:
      UNREACHABLE();
  }
2635
  VisitAtomicLoad(this, node, opcode);
2636
}
2637

2638
void InstructionSelector::VisitWord32AtomicStore(Node* node) {
2639
  MachineRepresentation rep = AtomicStoreRepresentationOf(node->op());
2640
  ArchOpcode opcode;
2641 2642
  switch (rep) {
    case MachineRepresentation::kWord8:
2643
      opcode = kWord32AtomicStoreWord8;
2644 2645
      break;
    case MachineRepresentation::kWord16:
2646
      opcode = kWord32AtomicStoreWord16;
2647 2648
      break;
    case MachineRepresentation::kWord32:
2649
      opcode = kWord32AtomicStoreWord32;
2650 2651 2652 2653 2654
      break;
    default:
      UNREACHABLE();
  }

2655 2656 2657 2658 2659
  VisitAtomicStore(this, node, opcode);
}

void InstructionSelector::VisitWord64AtomicLoad(Node* node) {
  LoadRepresentation load_rep = LoadRepresentationOf(node->op());
2660
  ArchOpcode opcode;
2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675
  switch (load_rep.representation()) {
    case MachineRepresentation::kWord8:
      opcode = kMips64Word64AtomicLoadUint8;
      break;
    case MachineRepresentation::kWord16:
      opcode = kMips64Word64AtomicLoadUint16;
      break;
    case MachineRepresentation::kWord32:
      opcode = kMips64Word64AtomicLoadUint32;
      break;
    case MachineRepresentation::kWord64:
      opcode = kMips64Word64AtomicLoadUint64;
      break;
    default:
      UNREACHABLE();
2676
  }
2677 2678 2679 2680 2681
  VisitAtomicLoad(this, node, opcode);
}

void InstructionSelector::VisitWord64AtomicStore(Node* node) {
  MachineRepresentation rep = AtomicStoreRepresentationOf(node->op());
2682
  ArchOpcode opcode;
2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700
  switch (rep) {
    case MachineRepresentation::kWord8:
      opcode = kMips64Word64AtomicStoreWord8;
      break;
    case MachineRepresentation::kWord16:
      opcode = kMips64Word64AtomicStoreWord16;
      break;
    case MachineRepresentation::kWord32:
      opcode = kMips64Word64AtomicStoreWord32;
      break;
    case MachineRepresentation::kWord64:
      opcode = kMips64Word64AtomicStoreWord64;
      break;
    default:
      UNREACHABLE();
  }

  VisitAtomicStore(this, node, opcode);
2701 2702
}

2703
void InstructionSelector::VisitWord32AtomicExchange(Node* node) {
2704
  ArchOpcode opcode;
2705
  MachineType type = AtomicOpType(node->op());
2706
  if (type == MachineType::Int8()) {
2707
    opcode = kWord32AtomicExchangeInt8;
2708
  } else if (type == MachineType::Uint8()) {
2709
    opcode = kWord32AtomicExchangeUint8;
2710
  } else if (type == MachineType::Int16()) {
2711
    opcode = kWord32AtomicExchangeInt16;
2712
  } else if (type == MachineType::Uint16()) {
2713
    opcode = kWord32AtomicExchangeUint16;
2714
  } else if (type == MachineType::Int32() || type == MachineType::Uint32()) {
2715
    opcode = kWord32AtomicExchangeWord32;
2716 2717 2718 2719
  } else {
    UNREACHABLE();
  }

2720 2721 2722 2723
  VisitAtomicExchange(this, node, opcode);
}

void InstructionSelector::VisitWord64AtomicExchange(Node* node) {
2724
  ArchOpcode opcode;
2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737
  MachineType type = AtomicOpType(node->op());
  if (type == MachineType::Uint8()) {
    opcode = kMips64Word64AtomicExchangeUint8;
  } else if (type == MachineType::Uint16()) {
    opcode = kMips64Word64AtomicExchangeUint16;
  } else if (type == MachineType::Uint32()) {
    opcode = kMips64Word64AtomicExchangeUint32;
  } else if (type == MachineType::Uint64()) {
    opcode = kMips64Word64AtomicExchangeUint64;
  } else {
    UNREACHABLE();
  }
  VisitAtomicExchange(this, node, opcode);
2738
}
2739

2740
void InstructionSelector::VisitWord32AtomicCompareExchange(Node* node) {
2741
  ArchOpcode opcode;
2742
  MachineType type = AtomicOpType(node->op());
2743
  if (type == MachineType::Int8()) {
2744
    opcode = kWord32AtomicCompareExchangeInt8;
2745
  } else if (type == MachineType::Uint8()) {
2746
    opcode = kWord32AtomicCompareExchangeUint8;
2747
  } else if (type == MachineType::Int16()) {
2748
    opcode = kWord32AtomicCompareExchangeInt16;
2749
  } else if (type == MachineType::Uint16()) {
2750
    opcode = kWord32AtomicCompareExchangeUint16;
2751
  } else if (type == MachineType::Int32() || type == MachineType::Uint32()) {
2752
    opcode = kWord32AtomicCompareExchangeWord32;
2753 2754 2755 2756
  } else {
    UNREACHABLE();
  }

2757
  VisitAtomicCompareExchange(this, node, opcode);
2758 2759
}

2760
void InstructionSelector::VisitWord64AtomicCompareExchange(Node* node) {
2761
  ArchOpcode opcode;
2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775
  MachineType type = AtomicOpType(node->op());
  if (type == MachineType::Uint8()) {
    opcode = kMips64Word64AtomicCompareExchangeUint8;
  } else if (type == MachineType::Uint16()) {
    opcode = kMips64Word64AtomicCompareExchangeUint16;
  } else if (type == MachineType::Uint32()) {
    opcode = kMips64Word64AtomicCompareExchangeUint32;
  } else if (type == MachineType::Uint64()) {
    opcode = kMips64Word64AtomicCompareExchangeUint64;
  } else {
    UNREACHABLE();
  }
  VisitAtomicCompareExchange(this, node, opcode);
}
2776
void InstructionSelector::VisitWord32AtomicBinaryOperation(
2777 2778
    Node* node, ArchOpcode int8_op, ArchOpcode uint8_op, ArchOpcode int16_op,
    ArchOpcode uint16_op, ArchOpcode word32_op) {
2779
  ArchOpcode opcode;
2780
  MachineType type = AtomicOpType(node->op());
2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793
  if (type == MachineType::Int8()) {
    opcode = int8_op;
  } else if (type == MachineType::Uint8()) {
    opcode = uint8_op;
  } else if (type == MachineType::Int16()) {
    opcode = int16_op;
  } else if (type == MachineType::Uint16()) {
    opcode = uint16_op;
  } else if (type == MachineType::Int32() || type == MachineType::Uint32()) {
    opcode = word32_op;
  } else {
    UNREACHABLE();
  }
2794

2795
  VisitAtomicBinop(this, node, opcode);
2796 2797
}

2798 2799
#define VISIT_ATOMIC_BINOP(op)                                   \
  void InstructionSelector::VisitWord32Atomic##op(Node* node) {  \
2800
    VisitWord32AtomicBinaryOperation(                            \
2801 2802 2803
        node, kWord32Atomic##op##Int8, kWord32Atomic##op##Uint8, \
        kWord32Atomic##op##Int16, kWord32Atomic##op##Uint16,     \
        kWord32Atomic##op##Word32);                              \
2804 2805 2806 2807
  }
VISIT_ATOMIC_BINOP(Add)
VISIT_ATOMIC_BINOP(Sub)
VISIT_ATOMIC_BINOP(And)
2808 2809 2810 2811 2812 2813 2814
VISIT_ATOMIC_BINOP(Or)
VISIT_ATOMIC_BINOP(Xor)
#undef VISIT_ATOMIC_BINOP

void InstructionSelector::VisitWord64AtomicBinaryOperation(
    Node* node, ArchOpcode uint8_op, ArchOpcode uint16_op, ArchOpcode uint32_op,
    ArchOpcode uint64_op) {
2815
  ArchOpcode opcode;
2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839
  MachineType type = AtomicOpType(node->op());
  if (type == MachineType::Uint8()) {
    opcode = uint8_op;
  } else if (type == MachineType::Uint16()) {
    opcode = uint16_op;
  } else if (type == MachineType::Uint32()) {
    opcode = uint32_op;
  } else if (type == MachineType::Uint64()) {
    opcode = uint64_op;
  } else {
    UNREACHABLE();
  }
  VisitAtomicBinop(this, node, opcode);
}

#define VISIT_ATOMIC_BINOP(op)                                                 \
  void InstructionSelector::VisitWord64Atomic##op(Node* node) {                \
    VisitWord64AtomicBinaryOperation(                                          \
        node, kMips64Word64Atomic##op##Uint8, kMips64Word64Atomic##op##Uint16, \
        kMips64Word64Atomic##op##Uint32, kMips64Word64Atomic##op##Uint64);     \
  }
VISIT_ATOMIC_BINOP(Add)
VISIT_ATOMIC_BINOP(Sub)
VISIT_ATOMIC_BINOP(And)
2840 2841 2842
VISIT_ATOMIC_BINOP(Or)
VISIT_ATOMIC_BINOP(Xor)
#undef VISIT_ATOMIC_BINOP
2843

2844 2845 2846 2847 2848 2849 2850 2851
void InstructionSelector::VisitInt32AbsWithOverflow(Node* node) {
  UNREACHABLE();
}

void InstructionSelector::VisitInt64AbsWithOverflow(Node* node) {
  UNREACHABLE();
}

2852
#define SIMD_TYPE_LIST(V) \
2853
  V(F64x2)                \
2854
  V(F32x4)                \
2855
  V(I64x2)                \
2856 2857 2858 2859
  V(I32x4)                \
  V(I16x8)                \
  V(I8x16)

2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884
#define SIMD_UNOP_LIST(V)                                    \
  V(F64x2Abs, kMips64F64x2Abs)                               \
  V(F64x2Neg, kMips64F64x2Neg)                               \
  V(F64x2Sqrt, kMips64F64x2Sqrt)                             \
  V(F64x2Ceil, kMips64F64x2Ceil)                             \
  V(F64x2Floor, kMips64F64x2Floor)                           \
  V(F64x2Trunc, kMips64F64x2Trunc)                           \
  V(F64x2NearestInt, kMips64F64x2NearestInt)                 \
  V(I64x2Neg, kMips64I64x2Neg)                               \
  V(I64x2BitMask, kMips64I64x2BitMask)                       \
  V(F64x2ConvertLowI32x4S, kMips64F64x2ConvertLowI32x4S)     \
  V(F64x2ConvertLowI32x4U, kMips64F64x2ConvertLowI32x4U)     \
  V(F64x2PromoteLowF32x4, kMips64F64x2PromoteLowF32x4)       \
  V(F32x4SConvertI32x4, kMips64F32x4SConvertI32x4)           \
  V(F32x4UConvertI32x4, kMips64F32x4UConvertI32x4)           \
  V(F32x4Abs, kMips64F32x4Abs)                               \
  V(F32x4Neg, kMips64F32x4Neg)                               \
  V(F32x4Sqrt, kMips64F32x4Sqrt)                             \
  V(F32x4RecipApprox, kMips64F32x4RecipApprox)               \
  V(F32x4RecipSqrtApprox, kMips64F32x4RecipSqrtApprox)       \
  V(F32x4Ceil, kMips64F32x4Ceil)                             \
  V(F32x4Floor, kMips64F32x4Floor)                           \
  V(F32x4Trunc, kMips64F32x4Trunc)                           \
  V(F32x4NearestInt, kMips64F32x4NearestInt)                 \
  V(F32x4DemoteF64x2Zero, kMips64F32x4DemoteF64x2Zero)       \
2885
  V(I64x2Abs, kMips64I64x2Abs)                               \
2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912
  V(I64x2SConvertI32x4Low, kMips64I64x2SConvertI32x4Low)     \
  V(I64x2SConvertI32x4High, kMips64I64x2SConvertI32x4High)   \
  V(I64x2UConvertI32x4Low, kMips64I64x2UConvertI32x4Low)     \
  V(I64x2UConvertI32x4High, kMips64I64x2UConvertI32x4High)   \
  V(I32x4SConvertF32x4, kMips64I32x4SConvertF32x4)           \
  V(I32x4UConvertF32x4, kMips64I32x4UConvertF32x4)           \
  V(I32x4Neg, kMips64I32x4Neg)                               \
  V(I32x4SConvertI16x8Low, kMips64I32x4SConvertI16x8Low)     \
  V(I32x4SConvertI16x8High, kMips64I32x4SConvertI16x8High)   \
  V(I32x4UConvertI16x8Low, kMips64I32x4UConvertI16x8Low)     \
  V(I32x4UConvertI16x8High, kMips64I32x4UConvertI16x8High)   \
  V(I32x4Abs, kMips64I32x4Abs)                               \
  V(I32x4BitMask, kMips64I32x4BitMask)                       \
  V(I32x4TruncSatF64x2SZero, kMips64I32x4TruncSatF64x2SZero) \
  V(I32x4TruncSatF64x2UZero, kMips64I32x4TruncSatF64x2UZero) \
  V(I16x8Neg, kMips64I16x8Neg)                               \
  V(I16x8SConvertI8x16Low, kMips64I16x8SConvertI8x16Low)     \
  V(I16x8SConvertI8x16High, kMips64I16x8SConvertI8x16High)   \
  V(I16x8UConvertI8x16Low, kMips64I16x8UConvertI8x16Low)     \
  V(I16x8UConvertI8x16High, kMips64I16x8UConvertI8x16High)   \
  V(I16x8Abs, kMips64I16x8Abs)                               \
  V(I16x8BitMask, kMips64I16x8BitMask)                       \
  V(I8x16Neg, kMips64I8x16Neg)                               \
  V(I8x16Abs, kMips64I8x16Abs)                               \
  V(I8x16Popcnt, kMips64I8x16Popcnt)                         \
  V(I8x16BitMask, kMips64I8x16BitMask)                       \
  V(S128Not, kMips64S128Not)                                 \
2913 2914 2915 2916
  V(I64x2AllTrue, kMips64I64x2AllTrue)                       \
  V(I32x4AllTrue, kMips64I32x4AllTrue)                       \
  V(I16x8AllTrue, kMips64I16x8AllTrue)                       \
  V(I8x16AllTrue, kMips64I8x16AllTrue)                       \
2917
  V(V128AnyTrue, kMips64V128AnyTrue)
2918 2919

#define SIMD_SHIFT_OP_LIST(V) \
2920 2921 2922
  V(I64x2Shl)                 \
  V(I64x2ShrS)                \
  V(I64x2ShrU)                \
2923 2924 2925 2926 2927 2928 2929 2930 2931 2932
  V(I32x4Shl)                 \
  V(I32x4ShrS)                \
  V(I32x4ShrU)                \
  V(I16x8Shl)                 \
  V(I16x8ShrS)                \
  V(I16x8ShrU)                \
  V(I8x16Shl)                 \
  V(I8x16ShrS)                \
  V(I8x16ShrU)

2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943
#define SIMD_BINOP_LIST(V)                               \
  V(F64x2Add, kMips64F64x2Add)                           \
  V(F64x2Sub, kMips64F64x2Sub)                           \
  V(F64x2Mul, kMips64F64x2Mul)                           \
  V(F64x2Div, kMips64F64x2Div)                           \
  V(F64x2Min, kMips64F64x2Min)                           \
  V(F64x2Max, kMips64F64x2Max)                           \
  V(F64x2Eq, kMips64F64x2Eq)                             \
  V(F64x2Ne, kMips64F64x2Ne)                             \
  V(F64x2Lt, kMips64F64x2Lt)                             \
  V(F64x2Le, kMips64F64x2Le)                             \
2944
  V(I64x2Eq, kMips64I64x2Eq)                             \
2945
  V(I64x2Ne, kMips64I64x2Ne)                             \
2946 2947
  V(I64x2Add, kMips64I64x2Add)                           \
  V(I64x2Sub, kMips64I64x2Sub)                           \
2948
  V(I64x2Mul, kMips64I64x2Mul)                           \
2949 2950
  V(I64x2GtS, kMips64I64x2GtS)                           \
  V(I64x2GeS, kMips64I64x2GeS)                           \
2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973
  V(F32x4Add, kMips64F32x4Add)                           \
  V(F32x4Sub, kMips64F32x4Sub)                           \
  V(F32x4Mul, kMips64F32x4Mul)                           \
  V(F32x4Div, kMips64F32x4Div)                           \
  V(F32x4Max, kMips64F32x4Max)                           \
  V(F32x4Min, kMips64F32x4Min)                           \
  V(F32x4Eq, kMips64F32x4Eq)                             \
  V(F32x4Ne, kMips64F32x4Ne)                             \
  V(F32x4Lt, kMips64F32x4Lt)                             \
  V(F32x4Le, kMips64F32x4Le)                             \
  V(I32x4Add, kMips64I32x4Add)                           \
  V(I32x4Sub, kMips64I32x4Sub)                           \
  V(I32x4Mul, kMips64I32x4Mul)                           \
  V(I32x4MaxS, kMips64I32x4MaxS)                         \
  V(I32x4MinS, kMips64I32x4MinS)                         \
  V(I32x4MaxU, kMips64I32x4MaxU)                         \
  V(I32x4MinU, kMips64I32x4MinU)                         \
  V(I32x4Eq, kMips64I32x4Eq)                             \
  V(I32x4Ne, kMips64I32x4Ne)                             \
  V(I32x4GtS, kMips64I32x4GtS)                           \
  V(I32x4GeS, kMips64I32x4GeS)                           \
  V(I32x4GtU, kMips64I32x4GtU)                           \
  V(I32x4GeU, kMips64I32x4GeU)                           \
2974
  V(I32x4DotI16x8S, kMips64I32x4DotI16x8S)               \
2975
  V(I16x8Add, kMips64I16x8Add)                           \
2976 2977
  V(I16x8AddSatS, kMips64I16x8AddSatS)                   \
  V(I16x8AddSatU, kMips64I16x8AddSatU)                   \
2978
  V(I16x8Sub, kMips64I16x8Sub)                           \
2979 2980
  V(I16x8SubSatS, kMips64I16x8SubSatS)                   \
  V(I16x8SubSatU, kMips64I16x8SubSatU)                   \
2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994
  V(I16x8Mul, kMips64I16x8Mul)                           \
  V(I16x8MaxS, kMips64I16x8MaxS)                         \
  V(I16x8MinS, kMips64I16x8MinS)                         \
  V(I16x8MaxU, kMips64I16x8MaxU)                         \
  V(I16x8MinU, kMips64I16x8MinU)                         \
  V(I16x8Eq, kMips64I16x8Eq)                             \
  V(I16x8Ne, kMips64I16x8Ne)                             \
  V(I16x8GtS, kMips64I16x8GtS)                           \
  V(I16x8GeS, kMips64I16x8GeS)                           \
  V(I16x8GtU, kMips64I16x8GtU)                           \
  V(I16x8GeU, kMips64I16x8GeU)                           \
  V(I16x8RoundingAverageU, kMips64I16x8RoundingAverageU) \
  V(I16x8SConvertI32x4, kMips64I16x8SConvertI32x4)       \
  V(I16x8UConvertI32x4, kMips64I16x8UConvertI32x4)       \
2995
  V(I16x8Q15MulRSatS, kMips64I16x8Q15MulRSatS)           \
2996
  V(I8x16Add, kMips64I8x16Add)                           \
2997 2998
  V(I8x16AddSatS, kMips64I8x16AddSatS)                   \
  V(I8x16AddSatU, kMips64I8x16AddSatU)                   \
2999
  V(I8x16Sub, kMips64I8x16Sub)                           \
3000 3001
  V(I8x16SubSatS, kMips64I8x16SubSatS)                   \
  V(I8x16SubSatU, kMips64I8x16SubSatU)                   \
3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016
  V(I8x16MaxS, kMips64I8x16MaxS)                         \
  V(I8x16MinS, kMips64I8x16MinS)                         \
  V(I8x16MaxU, kMips64I8x16MaxU)                         \
  V(I8x16MinU, kMips64I8x16MinU)                         \
  V(I8x16Eq, kMips64I8x16Eq)                             \
  V(I8x16Ne, kMips64I8x16Ne)                             \
  V(I8x16GtS, kMips64I8x16GtS)                           \
  V(I8x16GeS, kMips64I8x16GeS)                           \
  V(I8x16GtU, kMips64I8x16GtU)                           \
  V(I8x16GeU, kMips64I8x16GeU)                           \
  V(I8x16RoundingAverageU, kMips64I8x16RoundingAverageU) \
  V(I8x16SConvertI16x8, kMips64I8x16SConvertI16x8)       \
  V(I8x16UConvertI16x8, kMips64I8x16UConvertI16x8)       \
  V(S128And, kMips64S128And)                             \
  V(S128Or, kMips64S128Or)                               \
3017 3018
  V(S128Xor, kMips64S128Xor)                             \
  V(S128AndNot, kMips64S128AndNot)
3019

3020 3021 3022 3023
void InstructionSelector::VisitS128Const(Node* node) {
  Mips64OperandGenerator g(this);
  static const int kUint32Immediates = kSimd128Size / sizeof(uint32_t);
  uint32_t val[kUint32Immediates];
3024
  memcpy(val, S128ImmediateParameterOf(node->op()).data(), kSimd128Size);
3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039
  // If all bytes are zeros or ones, avoid emitting code for generic constants
  bool all_zeros = !(val[0] || val[1] || val[2] || val[3]);
  bool all_ones = val[0] == UINT32_MAX && val[1] == UINT32_MAX &&
                  val[2] == UINT32_MAX && val[3] == UINT32_MAX;
  InstructionOperand dst = g.DefineAsRegister(node);
  if (all_zeros) {
    Emit(kMips64S128Zero, dst);
  } else if (all_ones) {
    Emit(kMips64S128AllOnes, dst);
  } else {
    Emit(kMips64S128Const, dst, g.UseImmediate(val[0]), g.UseImmediate(val[1]),
         g.UseImmediate(val[2]), g.UseImmediate(val[3]));
  }
}

3040 3041
void InstructionSelector::VisitS128Zero(Node* node) {
  Mips64OperandGenerator g(this);
3042
  Emit(kMips64S128Zero, g.DefineAsRegister(node));
3043
}
3044 3045 3046 3047 3048 3049 3050 3051

#define SIMD_VISIT_SPLAT(Type)                               \
  void InstructionSelector::Visit##Type##Splat(Node* node) { \
    VisitRR(this, kMips64##Type##Splat, node);               \
  }
SIMD_TYPE_LIST(SIMD_VISIT_SPLAT)
#undef SIMD_VISIT_SPLAT

3052 3053 3054 3055 3056 3057
#define SIMD_VISIT_EXTRACT_LANE(Type, Sign)                              \
  void InstructionSelector::Visit##Type##ExtractLane##Sign(Node* node) { \
    VisitRRI(this, kMips64##Type##ExtractLane##Sign, node);              \
  }
SIMD_VISIT_EXTRACT_LANE(F64x2, )
SIMD_VISIT_EXTRACT_LANE(F32x4, )
3058
SIMD_VISIT_EXTRACT_LANE(I64x2, )
3059 3060 3061 3062 3063
SIMD_VISIT_EXTRACT_LANE(I32x4, )
SIMD_VISIT_EXTRACT_LANE(I16x8, U)
SIMD_VISIT_EXTRACT_LANE(I16x8, S)
SIMD_VISIT_EXTRACT_LANE(I8x16, U)
SIMD_VISIT_EXTRACT_LANE(I8x16, S)
3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081
#undef SIMD_VISIT_EXTRACT_LANE

#define SIMD_VISIT_REPLACE_LANE(Type)                              \
  void InstructionSelector::Visit##Type##ReplaceLane(Node* node) { \
    VisitRRIR(this, kMips64##Type##ReplaceLane, node);             \
  }
SIMD_TYPE_LIST(SIMD_VISIT_REPLACE_LANE)
#undef SIMD_VISIT_REPLACE_LANE

#define SIMD_VISIT_UNOP(Name, instruction)            \
  void InstructionSelector::Visit##Name(Node* node) { \
    VisitRR(this, instruction, node);                 \
  }
SIMD_UNOP_LIST(SIMD_VISIT_UNOP)
#undef SIMD_VISIT_UNOP

#define SIMD_VISIT_SHIFT_OP(Name)                     \
  void InstructionSelector::Visit##Name(Node* node) { \
3082
    VisitSimdShift(this, kMips64##Name, node);        \
3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093
  }
SIMD_SHIFT_OP_LIST(SIMD_VISIT_SHIFT_OP)
#undef SIMD_VISIT_SHIFT_OP

#define SIMD_VISIT_BINOP(Name, instruction)           \
  void InstructionSelector::Visit##Name(Node* node) { \
    VisitRRR(this, instruction, node);                \
  }
SIMD_BINOP_LIST(SIMD_VISIT_BINOP)
#undef SIMD_VISIT_BINOP

3094 3095 3096
void InstructionSelector::VisitS128Select(Node* node) {
  VisitRRRR(this, kMips64S128Select, node);
}
3097

3098
#if V8_ENABLE_WEBASSEMBLY
3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156
namespace {

struct ShuffleEntry {
  uint8_t shuffle[kSimd128Size];
  ArchOpcode opcode;
};

static const ShuffleEntry arch_shuffles[] = {
    {{0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23},
     kMips64S32x4InterleaveRight},
    {{8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31},
     kMips64S32x4InterleaveLeft},
    {{0, 1, 2, 3, 8, 9, 10, 11, 16, 17, 18, 19, 24, 25, 26, 27},
     kMips64S32x4PackEven},
    {{4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31},
     kMips64S32x4PackOdd},
    {{0, 1, 2, 3, 16, 17, 18, 19, 8, 9, 10, 11, 24, 25, 26, 27},
     kMips64S32x4InterleaveEven},
    {{4, 5, 6, 7, 20, 21, 22, 23, 12, 13, 14, 15, 28, 29, 30, 31},
     kMips64S32x4InterleaveOdd},

    {{0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23},
     kMips64S16x8InterleaveRight},
    {{8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31},
     kMips64S16x8InterleaveLeft},
    {{0, 1, 4, 5, 8, 9, 12, 13, 16, 17, 20, 21, 24, 25, 28, 29},
     kMips64S16x8PackEven},
    {{2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31},
     kMips64S16x8PackOdd},
    {{0, 1, 16, 17, 4, 5, 20, 21, 8, 9, 24, 25, 12, 13, 28, 29},
     kMips64S16x8InterleaveEven},
    {{2, 3, 18, 19, 6, 7, 22, 23, 10, 11, 26, 27, 14, 15, 30, 31},
     kMips64S16x8InterleaveOdd},
    {{6, 7, 4, 5, 2, 3, 0, 1, 14, 15, 12, 13, 10, 11, 8, 9},
     kMips64S16x4Reverse},
    {{2, 3, 0, 1, 6, 7, 4, 5, 10, 11, 8, 9, 14, 15, 12, 13},
     kMips64S16x2Reverse},

    {{0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23},
     kMips64S8x16InterleaveRight},
    {{8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31},
     kMips64S8x16InterleaveLeft},
    {{0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30},
     kMips64S8x16PackEven},
    {{1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31},
     kMips64S8x16PackOdd},
    {{0, 16, 2, 18, 4, 20, 6, 22, 8, 24, 10, 26, 12, 28, 14, 30},
     kMips64S8x16InterleaveEven},
    {{1, 17, 3, 19, 5, 21, 7, 23, 9, 25, 11, 27, 13, 29, 15, 31},
     kMips64S8x16InterleaveOdd},
    {{7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, 10, 9, 8},
     kMips64S8x8Reverse},
    {{3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12},
     kMips64S8x4Reverse},
    {{1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14},
     kMips64S8x2Reverse}};

bool TryMatchArchShuffle(const uint8_t* shuffle, const ShuffleEntry* table,
3157 3158 3159
                         size_t num_entries, bool is_swizzle,
                         ArchOpcode* opcode) {
  uint8_t mask = is_swizzle ? kSimd128Size - 1 : 2 * kSimd128Size - 1;
3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177
  for (size_t i = 0; i < num_entries; ++i) {
    const ShuffleEntry& entry = table[i];
    int j = 0;
    for (; j < kSimd128Size; ++j) {
      if ((entry.shuffle[j] & mask) != (shuffle[j] & mask)) {
        break;
      }
    }
    if (j == kSimd128Size) {
      *opcode = entry.opcode;
      return true;
    }
  }
  return false;
}

}  // namespace

3178
void InstructionSelector::VisitI8x16Shuffle(Node* node) {
3179
  uint8_t shuffle[kSimd128Size];
3180 3181
  bool is_swizzle;
  CanonicalizeShuffle(node, shuffle, &is_swizzle);
3182 3183 3184
  uint8_t shuffle32x4[4];
  ArchOpcode opcode;
  if (TryMatchArchShuffle(shuffle, arch_shuffles, arraysize(arch_shuffles),
3185
                          is_swizzle, &opcode)) {
3186 3187 3188
    VisitRRR(this, opcode, node);
    return;
  }
3189 3190
  Node* input0 = node->InputAt(0);
  Node* input1 = node->InputAt(1);
3191 3192
  uint8_t offset;
  Mips64OperandGenerator g(this);
3193
  if (wasm::SimdShuffle::TryMatchConcat(shuffle, &offset)) {
3194 3195
    Emit(kMips64S8x16Concat, g.DefineSameAsFirst(node), g.UseRegister(input1),
         g.UseRegister(input0), g.UseImmediate(offset));
3196 3197
    return;
  }
3198
  if (wasm::SimdShuffle::TryMatch32x4Shuffle(shuffle, shuffle32x4)) {
3199
    Emit(kMips64S32x4Shuffle, g.DefineAsRegister(node), g.UseRegister(input0),
3200 3201
         g.UseRegister(input1),
         g.UseImmediate(wasm::SimdShuffle::Pack4Lanes(shuffle32x4)));
3202 3203
    return;
  }
3204
  Emit(kMips64I8x16Shuffle, g.DefineAsRegister(node), g.UseRegister(input0),
3205 3206 3207 3208 3209
       g.UseRegister(input1),
       g.UseImmediate(wasm::SimdShuffle::Pack4Lanes(shuffle)),
       g.UseImmediate(wasm::SimdShuffle::Pack4Lanes(shuffle + 4)),
       g.UseImmediate(wasm::SimdShuffle::Pack4Lanes(shuffle + 8)),
       g.UseImmediate(wasm::SimdShuffle::Pack4Lanes(shuffle + 12)));
3210
}
3211 3212 3213
#else
void InstructionSelector::VisitI8x16Shuffle(Node* node) { UNREACHABLE(); }
#endif  // V8_ENABLE_WEBASSEMBLY
3214

3215
void InstructionSelector::VisitI8x16Swizzle(Node* node) {
3216 3217 3218 3219
  Mips64OperandGenerator g(this);
  InstructionOperand temps[] = {g.TempSimd128Register()};
  // We don't want input 0 or input 1 to be the same as output, since we will
  // modify output before do the calculation.
3220
  Emit(kMips64I8x16Swizzle, g.DefineAsRegister(node),
3221
       g.UseUniqueRegister(node->InputAt(0)),
3222
       g.UseUniqueRegister(node->InputAt(1)), arraysize(temps), temps);
3223 3224
}

3225
void InstructionSelector::VisitSignExtendWord8ToInt32(Node* node) {
3226 3227
  Mips64OperandGenerator g(this);
  Emit(kMips64Seb, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0)));
3228 3229 3230
}

void InstructionSelector::VisitSignExtendWord16ToInt32(Node* node) {
3231 3232
  Mips64OperandGenerator g(this);
  Emit(kMips64Seh, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0)));
3233 3234 3235
}

void InstructionSelector::VisitSignExtendWord8ToInt64(Node* node) {
3236 3237
  Mips64OperandGenerator g(this);
  Emit(kMips64Seb, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0)));
3238 3239 3240
}

void InstructionSelector::VisitSignExtendWord16ToInt64(Node* node) {
3241 3242
  Mips64OperandGenerator g(this);
  Emit(kMips64Seh, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0)));
3243 3244 3245
}

void InstructionSelector::VisitSignExtendWord32ToInt64(Node* node) {
3246 3247 3248
  Mips64OperandGenerator g(this);
  Emit(kMips64Shl, g.DefineAsRegister(node), g.UseRegister(node->InputAt(0)),
       g.TempImmediate(0));
3249 3250
}

3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266
void InstructionSelector::VisitF32x4Pmin(Node* node) {
  VisitUniqueRRR(this, kMips64F32x4Pmin, node);
}

void InstructionSelector::VisitF32x4Pmax(Node* node) {
  VisitUniqueRRR(this, kMips64F32x4Pmax, node);
}

void InstructionSelector::VisitF64x2Pmin(Node* node) {
  VisitUniqueRRR(this, kMips64F64x2Pmin, node);
}

void InstructionSelector::VisitF64x2Pmax(Node* node) {
  VisitUniqueRRR(this, kMips64F64x2Pmax, node);
}

3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287
#define VISIT_EXT_MUL(OPCODE1, OPCODE2, TYPE)                                  \
  void InstructionSelector::Visit##OPCODE1##ExtMulLow##OPCODE2(Node* node) {   \
    Mips64OperandGenerator g(this);                                            \
    Emit(kMips64ExtMulLow | MiscField::encode(TYPE), g.DefineAsRegister(node), \
         g.UseRegister(node->InputAt(0)), g.UseRegister(node->InputAt(1)));    \
  }                                                                            \
  void InstructionSelector::Visit##OPCODE1##ExtMulHigh##OPCODE2(Node* node) {  \
    Mips64OperandGenerator g(this);                                            \
    Emit(kMips64ExtMulHigh | MiscField::encode(TYPE),                          \
         g.DefineAsRegister(node), g.UseRegister(node->InputAt(0)),            \
         g.UseRegister(node->InputAt(1)));                                     \
  }

VISIT_EXT_MUL(I64x2, I32x4S, MSAS32)
VISIT_EXT_MUL(I64x2, I32x4U, MSAU32)
VISIT_EXT_MUL(I32x4, I16x8S, MSAS16)
VISIT_EXT_MUL(I32x4, I16x8U, MSAU16)
VISIT_EXT_MUL(I16x8, I8x16S, MSAS8)
VISIT_EXT_MUL(I16x8, I8x16U, MSAU8)
#undef VISIT_EXT_MUL

3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299
#define VISIT_EXTADD_PAIRWISE(OPCODE, TYPE)                          \
  void InstructionSelector::Visit##OPCODE(Node* node) {              \
    Mips64OperandGenerator g(this);                                  \
    Emit(kMips64ExtAddPairwise | MiscField::encode(TYPE),            \
         g.DefineAsRegister(node), g.UseRegister(node->InputAt(0))); \
  }
VISIT_EXTADD_PAIRWISE(I16x8ExtAddPairwiseI8x16S, MSAS8)
VISIT_EXTADD_PAIRWISE(I16x8ExtAddPairwiseI8x16U, MSAU8)
VISIT_EXTADD_PAIRWISE(I32x4ExtAddPairwiseI16x8S, MSAS16)
VISIT_EXTADD_PAIRWISE(I32x4ExtAddPairwiseI16x8U, MSAU16)
#undef VISIT_EXTADD_PAIRWISE

3300 3301 3302 3303 3304 3305
void InstructionSelector::AddOutputToSelectContinuation(OperandGenerator* g,
                                                        int first_input_index,
                                                        Node* node) {
  UNREACHABLE();
}

3306 3307 3308
// static
MachineOperatorBuilder::Flags
InstructionSelector::SupportedMachineOperatorFlags() {
3309 3310
  MachineOperatorBuilder::Flags flags = MachineOperatorBuilder::kNoFlags;
  return flags | MachineOperatorBuilder::kWord32Ctz |
3311 3312 3313 3314
         MachineOperatorBuilder::kWord64Ctz |
         MachineOperatorBuilder::kWord32Popcnt |
         MachineOperatorBuilder::kWord64Popcnt |
         MachineOperatorBuilder::kWord32ShiftIsSafe |
3315 3316
         MachineOperatorBuilder::kInt32DivIsSafe |
         MachineOperatorBuilder::kUint32DivIsSafe |
3317
         MachineOperatorBuilder::kFloat64RoundDown |
3318
         MachineOperatorBuilder::kFloat32RoundDown |
3319
         MachineOperatorBuilder::kFloat64RoundUp |
3320
         MachineOperatorBuilder::kFloat32RoundUp |
3321
         MachineOperatorBuilder::kFloat64RoundTruncate |
3322 3323
         MachineOperatorBuilder::kFloat32RoundTruncate |
         MachineOperatorBuilder::kFloat64RoundTiesEven |
3324
         MachineOperatorBuilder::kFloat32RoundTiesEven;
3325 3326
}

3327 3328 3329 3330 3331 3332 3333
// static
MachineOperatorBuilder::AlignmentRequirements
InstructionSelector::AlignmentRequirements() {
  if (kArchVariant == kMips64r6) {
    return MachineOperatorBuilder::AlignmentRequirements::
        FullUnalignedAccessSupport();
  } else {
3334
    DCHECK_EQ(kMips64r2, kArchVariant);
3335 3336 3337 3338 3339
    return MachineOperatorBuilder::AlignmentRequirements::
        NoUnalignedAccessSupport();
  }
}

3340 3341 3342 3343 3344 3345 3346
#undef SIMD_BINOP_LIST
#undef SIMD_SHIFT_OP_LIST
#undef SIMD_UNOP_LIST
#undef SIMD_TYPE_LIST
#undef TRACE_UNIMPL
#undef TRACE

3347 3348 3349
}  // namespace compiler
}  // namespace internal
}  // namespace v8