simd-scalar-lowering.cc 72.9 KB
Newer Older
1 2 3 4 5
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/compiler/simd-scalar-lowering.h"
6

7
#include "src/codegen/machine-type.h"
8 9
#include "src/compiler/diamond.h"
#include "src/compiler/linkage.h"
10
#include "src/compiler/machine-operator.h"
11 12 13
#include "src/compiler/node-matchers.h"
#include "src/compiler/node-properties.h"
#include "src/compiler/node.h"
14
#include "src/compiler/wasm-compiler.h"
15 16 17 18 19

namespace v8 {
namespace internal {
namespace compiler {

20
namespace {
21
static const int kNumLanes64 = 2;
22 23
static const int kNumLanes32 = 4;
static const int kNumLanes16 = 8;
24
static const int kNumLanes8 = 16;
25 26
static const int32_t kMask16 = 0xFFFF;
static const int32_t kMask8 = 0xFF;
27
static const int32_t kShift16 = 16;
28
static const int32_t kShift8 = 24;
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
static const int32_t kShiftMask8 = 0x7;
static const int32_t kShiftMask16 = 0xF;
static const int32_t kShiftMask32 = 0x1F;

// Shift values are taken modulo lane size. This helper calculates the mask
// required for different shift opcodes.
int GetMaskForShift(Node* node) {
  switch (node->opcode()) {
    case IrOpcode::kI8x16Shl:
    case IrOpcode::kI8x16ShrS:
    case IrOpcode::kI8x16ShrU:
      return kShiftMask8;
    case IrOpcode::kI16x8Shl:
    case IrOpcode::kI16x8ShrS:
    case IrOpcode::kI16x8ShrU:
      return kShiftMask16;
    case IrOpcode::kI32x4Shl:
    case IrOpcode::kI32x4ShrS:
    case IrOpcode::kI32x4ShrU:
      return kShiftMask32;
    default:
      UNIMPLEMENTED();
  }
}
}  // anonymous namespace
54

55
SimdScalarLowering::SimdScalarLowering(
56 57 58 59
    MachineGraph* mcgraph, Signature<MachineRepresentation>* signature)
    : mcgraph_(mcgraph),
      state_(mcgraph->graph(), 3),
      stack_(mcgraph_->zone()),
60 61
      replacements_(nullptr),
      signature_(signature),
62 63
      placeholder_(graph()->NewNode(common()->Parameter(-2, "placeholder"),
                                    graph()->start())),
64
      parameter_count_after_lowering_(-1) {
65 66 67
  DCHECK_NOT_NULL(graph());
  DCHECK_NOT_NULL(graph()->end());
  replacements_ = zone()->NewArray<Replacement>(graph()->NodeCount());
68 69
  memset(static_cast<void*>(replacements_), 0,
         sizeof(Replacement) * graph()->NodeCount());
70 71 72 73 74
}

void SimdScalarLowering::LowerGraph() {
  stack_.push_back({graph()->end(), 0});
  state_.Set(graph()->end(), State::kOnStack);
75
  replacements_[graph()->end()->id()].type = SimdType::kInt32x4;
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

  while (!stack_.empty()) {
    NodeState& top = stack_.back();
    if (top.input_index == top.node->InputCount()) {
      // All inputs of top have already been lowered, now lower top.
      stack_.pop_back();
      state_.Set(top.node, State::kVisited);
      LowerNode(top.node);
    } else {
      // Push the next input onto the stack.
      Node* input = top.node->InputAt(top.input_index++);
      if (state_.Get(input) == State::kUnvisited) {
        SetLoweredType(input, top.node);
        if (input->opcode() == IrOpcode::kPhi) {
          // To break cycles with phi nodes we push phis on a separate stack so
          // that they are processed after all other nodes.
          PreparePhiReplacement(input);
          stack_.push_front({input, 0});
94 95 96
        } else if (input->opcode() == IrOpcode::kEffectPhi ||
                   input->opcode() == IrOpcode::kLoop) {
          stack_.push_front({input, 0});
97 98 99 100 101 102 103 104 105
        } else {
          stack_.push_back({input, 0});
        }
        state_.Set(input, State::kOnStack);
      }
    }
  }
}

106 107
#define FOREACH_INT64X2_OPCODE(V) V(I64x2Splat)

108
#define FOREACH_INT32X4_OPCODE(V) \
109 110 111 112 113
  V(I32x4Splat)                   \
  V(I32x4ExtractLane)             \
  V(I32x4ReplaceLane)             \
  V(I32x4SConvertF32x4)           \
  V(I32x4UConvertF32x4)           \
114 115
  V(I32x4SConvertI16x8Low)        \
  V(I32x4SConvertI16x8High)       \
116
  V(I32x4Neg)                     \
117 118
  V(I32x4Shl)                     \
  V(I32x4ShrS)                    \
119
  V(I32x4Add)                     \
120
  V(I32x4AddHoriz)                \
121 122 123 124
  V(I32x4Sub)                     \
  V(I32x4Mul)                     \
  V(I32x4MinS)                    \
  V(I32x4MaxS)                    \
125
  V(I32x4ShrU)                    \
126 127
  V(I32x4MinU)                    \
  V(I32x4MaxU)                    \
128 129 130 131 132 133
  V(I32x4Eq)                      \
  V(I32x4Ne)                      \
  V(I32x4LtS)                     \
  V(I32x4LeS)                     \
  V(I32x4GtS)                     \
  V(I32x4GeS)                     \
134 135
  V(I32x4UConvertI16x8Low)        \
  V(I32x4UConvertI16x8High)       \
136 137 138 139
  V(I32x4LtU)                     \
  V(I32x4LeU)                     \
  V(I32x4GtU)                     \
  V(I32x4GeU)                     \
140
  V(I32x4Abs)                     \
141 142 143
  V(S128And)                      \
  V(S128Or)                       \
  V(S128Xor)                      \
144
  V(S128Not)                      \
145 146 147 148 149
  V(V32x4AnyTrue)                 \
  V(V32x4AllTrue)                 \
  V(V16x8AnyTrue)                 \
  V(V16x8AllTrue)                 \
  V(V8x16AnyTrue)                 \
150 151
  V(V8x16AllTrue)                 \
  V(I32x4BitMask)
152

153 154
#define FOREACH_FLOAT64X2_OPCODE(V) V(F64x2Splat)

155
#define FOREACH_FLOAT32X4_OPCODE(V) \
156 157 158 159 160 161 162
  V(F32x4Splat)                     \
  V(F32x4ExtractLane)               \
  V(F32x4ReplaceLane)               \
  V(F32x4SConvertI32x4)             \
  V(F32x4UConvertI32x4)             \
  V(F32x4Abs)                       \
  V(F32x4Neg)                       \
163
  V(F32x4Sqrt)                      \
164 165
  V(F32x4RecipApprox)               \
  V(F32x4RecipSqrtApprox)           \
166
  V(F32x4Add)                       \
167
  V(F32x4AddHoriz)                  \
168 169
  V(F32x4Sub)                       \
  V(F32x4Mul)                       \
170
  V(F32x4Div)                       \
171 172
  V(F32x4Min)                       \
  V(F32x4Max)
173

174
#define FOREACH_FLOAT32X4_TO_INT32X4OPCODE(V) \
175 176 177 178 179 180
  V(F32x4Eq)                                  \
  V(F32x4Ne)                                  \
  V(F32x4Lt)                                  \
  V(F32x4Le)                                  \
  V(F32x4Gt)                                  \
  V(F32x4Ge)
181

182 183
#define FOREACH_INT16X8_OPCODE(V) \
  V(I16x8Splat)                   \
184 185
  V(I16x8ExtractLaneU)            \
  V(I16x8ExtractLaneS)            \
186
  V(I16x8ReplaceLane)             \
187 188
  V(I16x8SConvertI8x16Low)        \
  V(I16x8SConvertI8x16High)       \
189 190 191
  V(I16x8Neg)                     \
  V(I16x8Shl)                     \
  V(I16x8ShrS)                    \
192
  V(I16x8SConvertI32x4)           \
193 194
  V(I16x8Add)                     \
  V(I16x8AddSaturateS)            \
195
  V(I16x8AddHoriz)                \
196 197 198 199 200
  V(I16x8Sub)                     \
  V(I16x8SubSaturateS)            \
  V(I16x8Mul)                     \
  V(I16x8MinS)                    \
  V(I16x8MaxS)                    \
201 202
  V(I16x8UConvertI8x16Low)        \
  V(I16x8UConvertI8x16High)       \
203
  V(I16x8ShrU)                    \
204
  V(I16x8UConvertI32x4)           \
205 206 207
  V(I16x8AddSaturateU)            \
  V(I16x8SubSaturateU)            \
  V(I16x8MinU)                    \
208 209 210 211 212 213
  V(I16x8MaxU)                    \
  V(I16x8Eq)                      \
  V(I16x8Ne)                      \
  V(I16x8LtS)                     \
  V(I16x8LeS)                     \
  V(I16x8LtU)                     \
214
  V(I16x8LeU)                     \
215
  V(I16x8RoundingAverageU)        \
216 217
  V(I16x8Abs)                     \
  V(I16x8BitMask)
218

219 220
#define FOREACH_INT8X16_OPCODE(V) \
  V(I8x16Splat)                   \
221 222
  V(I8x16ExtractLaneU)            \
  V(I8x16ExtractLaneS)            \
223
  V(I8x16ReplaceLane)             \
224
  V(I8x16SConvertI16x8)           \
225 226 227 228 229 230 231 232 233 234 235
  V(I8x16Neg)                     \
  V(I8x16Shl)                     \
  V(I8x16ShrS)                    \
  V(I8x16Add)                     \
  V(I8x16AddSaturateS)            \
  V(I8x16Sub)                     \
  V(I8x16SubSaturateS)            \
  V(I8x16Mul)                     \
  V(I8x16MinS)                    \
  V(I8x16MaxS)                    \
  V(I8x16ShrU)                    \
236
  V(I8x16UConvertI16x8)           \
237 238 239
  V(I8x16AddSaturateU)            \
  V(I8x16SubSaturateU)            \
  V(I8x16MinU)                    \
240 241 242 243 244 245
  V(I8x16MaxU)                    \
  V(I8x16Eq)                      \
  V(I8x16Ne)                      \
  V(I8x16LtS)                     \
  V(I8x16LeS)                     \
  V(I8x16LtU)                     \
246
  V(I8x16LeU)                     \
247
  V(S8x16Swizzle)                 \
248
  V(S8x16Shuffle)                 \
249
  V(I8x16RoundingAverageU)        \
250 251
  V(I8x16Abs)                     \
  V(I8x16BitMask)
252

253 254
MachineType SimdScalarLowering::MachineTypeFrom(SimdType simdType) {
  switch (simdType) {
255 256
    case SimdType::kFloat64x2:
      return MachineType::Float64();
257 258
    case SimdType::kFloat32x4:
      return MachineType::Float32();
259 260
    case SimdType::kInt64x2:
      return MachineType::Int64();
261 262 263 264 265 266 267 268 269
    case SimdType::kInt32x4:
      return MachineType::Int32();
    case SimdType::kInt16x8:
      return MachineType::Int16();
    case SimdType::kInt8x16:
      return MachineType::Int8();
  }
  return MachineType::None();
}
270

271 272 273
void SimdScalarLowering::SetLoweredType(Node* node, Node* output) {
  switch (node->opcode()) {
#define CASE_STMT(name) case IrOpcode::k##name:
274 275 276 277
    FOREACH_FLOAT64X2_OPCODE(CASE_STMT) {
      replacements_[node->id()].type = SimdType::kFloat64x2;
      break;
    }
278 279 280 281
    FOREACH_INT64X2_OPCODE(CASE_STMT) {
      replacements_[node->id()].type = SimdType::kInt64x2;
      break;
    }
282 283 284 285
    FOREACH_INT32X4_OPCODE(CASE_STMT)
    case IrOpcode::kReturn:
    case IrOpcode::kParameter:
    case IrOpcode::kCall: {
286
      replacements_[node->id()].type = SimdType::kInt32x4;
287 288 289
      break;
    }
      FOREACH_FLOAT32X4_OPCODE(CASE_STMT) {
290
        replacements_[node->id()].type = SimdType::kFloat32x4;
291 292
        break;
      }
293
      FOREACH_FLOAT32X4_TO_INT32X4OPCODE(CASE_STMT) {
294
        replacements_[node->id()].type = SimdType::kInt32x4;
295 296
        break;
      }
297 298 299 300
      FOREACH_INT16X8_OPCODE(CASE_STMT) {
        replacements_[node->id()].type = SimdType::kInt16x8;
        break;
      }
301 302 303 304
      FOREACH_INT8X16_OPCODE(CASE_STMT) {
        replacements_[node->id()].type = SimdType::kInt8x16;
        break;
      }
305 306 307 308 309 310 311 312 313 314 315 316
    case IrOpcode::kLoadTransform: {
      LoadTransformParameters params = LoadTransformParametersOf(node->op());
      switch (params.transformation) {
        case LoadTransformation::kS8x16LoadSplat:
          replacements_[node->id()].type = SimdType::kInt8x16;
          break;
        case LoadTransformation::kS16x8LoadSplat:
          replacements_[node->id()].type = SimdType::kInt16x8;
          break;
        case LoadTransformation::kS32x4LoadSplat:
          replacements_[node->id()].type = SimdType::kInt32x4;
          break;
317 318 319 320 321 322 323 324
        case LoadTransformation::kI16x8Load8x8S:
        case LoadTransformation::kI16x8Load8x8U:
          replacements_[node->id()].type = SimdType::kInt16x8;
          break;
        case LoadTransformation::kI32x4Load16x4S:
        case LoadTransformation::kI32x4Load16x4U:
          replacements_[node->id()].type = SimdType::kInt32x4;
          break;
325 326 327 328 329
        default:
          UNIMPLEMENTED();
      }
      break;
    }
330
    default: {
331
      switch (output->opcode()) {
332
        case IrOpcode::kF32x4SConvertI32x4:
333 334 335
        case IrOpcode::kF32x4UConvertI32x4:
        case IrOpcode::kI16x8SConvertI32x4:
        case IrOpcode::kI16x8UConvertI32x4: {
336
          replacements_[node->id()].type = SimdType::kInt32x4;
337
          break;
338 339
        }
        case IrOpcode::kI8x16SConvertI16x8:
340 341 342 343 344
        case IrOpcode::kI8x16UConvertI16x8:
        case IrOpcode::kI32x4SConvertI16x8Low:
        case IrOpcode::kI32x4SConvertI16x8High:
        case IrOpcode::kI32x4UConvertI16x8Low:
        case IrOpcode::kI32x4UConvertI16x8High: {
345 346
          replacements_[node->id()].type = SimdType::kInt16x8;
          break;
347 348 349 350 351 352 353
        }
        case IrOpcode::kI16x8SConvertI8x16Low:
        case IrOpcode::kI16x8SConvertI8x16High:
        case IrOpcode::kI16x8UConvertI8x16Low:
        case IrOpcode::kI16x8UConvertI8x16High: {
          replacements_[node->id()].type = SimdType::kInt8x16;
          break;
354
        }
355
          FOREACH_FLOAT32X4_TO_INT32X4OPCODE(CASE_STMT)
356 357
        case IrOpcode::kI32x4SConvertF32x4:
        case IrOpcode::kI32x4UConvertF32x4: {
358
          replacements_[node->id()].type = SimdType::kFloat32x4;
359 360
          break;
        }
361 362
        case IrOpcode::kS128Select: {
          replacements_[node->id()].type = SimdType::kInt32x4;
363
          break;
364
        }
365 366 367
        default: {
          replacements_[node->id()].type = replacements_[output->id()].type;
        }
368 369
      }
    }
370
#undef CASE_STMT
371 372 373
  }
}

374
static int GetParameterIndexAfterLoweringSimd128(
375 376 377 378
    Signature<MachineRepresentation>* signature, int old_index) {
  // In function calls, the simd128 types are passed as 4 Int32 types. The
  // parameters are typecast to the types as needed for various operations.
  int result = old_index;
379
  for (int i = 0; i < old_index; ++i) {
380 381 382 383 384 385 386 387 388
    if (signature->GetParam(i) == MachineRepresentation::kSimd128) {
      result += 3;
    }
  }
  return result;
}

int SimdScalarLowering::GetParameterCountAfterLowering() {
  if (parameter_count_after_lowering_ == -1) {
389 390 391
    // GetParameterIndexAfterLoweringSimd128(parameter_count) returns the
    // parameter count after lowering.
    parameter_count_after_lowering_ = GetParameterIndexAfterLoweringSimd128(
392 393 394 395 396
        signature(), static_cast<int>(signature()->parameter_count()));
  }
  return parameter_count_after_lowering_;
}

397
static int GetReturnCountAfterLoweringSimd128(
398 399
    Signature<MachineRepresentation>* signature) {
  int result = static_cast<int>(signature->return_count());
400
  for (int i = 0; i < static_cast<int>(signature->return_count()); ++i) {
401 402 403 404 405 406 407
    if (signature->GetReturn(i) == MachineRepresentation::kSimd128) {
      result += 3;
    }
  }
  return result;
}

408 409
int SimdScalarLowering::NumLanes(SimdType type) {
  int num_lanes = 0;
410
  if (type == SimdType::kFloat64x2 || type == SimdType::kInt64x2) {
411 412
    num_lanes = kNumLanes64;
  } else if (type == SimdType::kFloat32x4 || type == SimdType::kInt32x4) {
413
    num_lanes = kNumLanes32;
414
  } else if (type == SimdType::kInt16x8) {
415
    num_lanes = kNumLanes16;
416
  } else if (type == SimdType::kInt8x16) {
417
    num_lanes = kNumLanes8;
418 419 420 421 422 423
  } else {
    UNREACHABLE();
  }
  return num_lanes;
}

424 425
constexpr int SimdScalarLowering::kLaneOffsets[];

426 427 428 429
void SimdScalarLowering::GetIndexNodes(Node* index, Node** new_indices,
                                       SimdType type) {
  int num_lanes = NumLanes(type);
  int lane_width = kSimd128Size / num_lanes;
430 431
  int laneIndex = kLaneOffsets[0] / lane_width;
  new_indices[laneIndex] = index;
432
  for (int i = 1; i < num_lanes; ++i) {
433 434 435 436 437
    laneIndex = kLaneOffsets[i * lane_width] / lane_width;
    new_indices[laneIndex] = graph()->NewNode(
        machine()->Int32Add(), index,
        graph()->NewNode(
            common()->Int32Constant(static_cast<int>(i) * lane_width)));
438 439 440
  }
}

441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
void SimdScalarLowering::LowerLoadOp(Node* node, SimdType type) {
  MachineRepresentation rep = LoadRepresentationOf(node->op()).representation();
  const Operator* load_op;
  switch (node->opcode()) {
    case IrOpcode::kLoad:
      load_op = machine()->Load(MachineTypeFrom(type));
      break;
    case IrOpcode::kUnalignedLoad:
      load_op = machine()->UnalignedLoad(MachineTypeFrom(type));
      break;
    case IrOpcode::kProtectedLoad:
      load_op = machine()->ProtectedLoad(MachineTypeFrom(type));
      break;
    default:
      UNREACHABLE();
  }
457 458 459
  if (rep == MachineRepresentation::kSimd128) {
    Node* base = node->InputAt(0);
    Node* index = node->InputAt(1);
460 461 462 463
    int num_lanes = NumLanes(type);
    Node** indices = zone()->NewArray<Node*>(num_lanes);
    GetIndexNodes(index, indices, type);
    Node** rep_nodes = zone()->NewArray<Node*>(num_lanes);
464
    rep_nodes[0] = node;
465
    rep_nodes[0]->ReplaceInput(1, indices[0]);
466 467
    NodeProperties::ChangeOp(rep_nodes[0], load_op);
    if (node->InputCount() > 2) {
468
      DCHECK_LT(3, node->InputCount());
469 470
      Node* effect_input = node->InputAt(2);
      Node* control_input = node->InputAt(3);
471 472 473 474 475
      for (int i = num_lanes - 1; i > 0; --i) {
        rep_nodes[i] = graph()->NewNode(load_op, base, indices[i], effect_input,
                                        control_input);
        effect_input = rep_nodes[i];
      }
476 477
      rep_nodes[0]->ReplaceInput(2, rep_nodes[1]);
    } else {
478
      for (int i = 1; i < num_lanes; ++i) {
479 480 481
        rep_nodes[i] = graph()->NewNode(load_op, base, indices[i]);
      }
    }
482
    ReplaceNode(node, rep_nodes, num_lanes);
483 484 485 486 487
  } else {
    DefaultLowering(node);
  }
}

488 489
void SimdScalarLowering::LowerLoadTransformOp(Node* node, SimdType type) {
  LoadTransformParameters params = LoadTransformParametersOf(node->op());
490 491 492 493
  MachineType load_rep = MachineType::None();
  SimdType load_type = type;

  // Load extends have a different machine type for loading.
494
  switch (params.transformation) {
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
    case LoadTransformation::kI16x8Load8x8S:
      load_rep = MachineType::Int8();
      load_type = SimdType::kInt8x16;
      break;
    case LoadTransformation::kI16x8Load8x8U:
      load_rep = MachineType::Uint8();
      load_type = SimdType::kInt8x16;
      break;
    case LoadTransformation::kI32x4Load16x4S:
      load_rep = MachineType::Int16();
      load_type = SimdType::kInt16x8;
      break;
    case LoadTransformation::kI32x4Load16x4U:
      load_rep = MachineType::Uint16();
      load_type = SimdType::kInt16x8;
      break;
511 512 513
    case LoadTransformation::kS8x16LoadSplat:
    case LoadTransformation::kS16x8LoadSplat:
    case LoadTransformation::kS32x4LoadSplat:
514
      load_rep = MachineTypeFrom(type);
515 516
      break;
    default:
517 518
      // Lowering for s64x2 is not implemented since lowering for 64x2
      // operations doesn't work properly yet.
519 520 521
      UNIMPLEMENTED();
  }

522 523
  DCHECK_NE(load_rep, MachineType::None());

524 525 526
  const Operator* load_op;
  switch (params.kind) {
    case LoadKind::kNormal:
527
      load_op = machine()->Load(load_rep);
528 529
      break;
    case LoadKind::kUnaligned:
530
      load_op = machine()->UnalignedLoad(load_rep);
531 532
      break;
    case LoadKind::kProtected:
533
      load_op = machine()->ProtectedLoad(load_rep);
534 535 536 537 538 539 540 541 542
      break;
  }

  Node* base = node->InputAt(0);
  Node* index = node->InputAt(1);
  int num_lanes = NumLanes(type);
  Node** rep_nodes = zone()->NewArray<Node*>(num_lanes);
  Node* effect_input = node->InputAt(2);
  Node* control_input = node->InputAt(3);
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561

  if (type != load_type) {
    // We load a smaller lane size, then extend to a larger lane size. So use
    // the smaller lane size to calculte the index nodes for loads, but only
    // actually load half of those lanes.
    Node** indices = zone()->NewArray<Node*>(num_lanes * 2);
    GetIndexNodes(index, indices, load_type);
    for (int i = num_lanes - 1; i >= 0; --i) {
      rep_nodes[i] = graph()->NewNode(load_op, base, indices[i], effect_input,
                                      control_input);
      effect_input = rep_nodes[i];
    }
  } else {
    // Load splat, load from the same index for every lane.
    for (int i = num_lanes - 1; i >= 0; --i) {
      rep_nodes[i] =
          graph()->NewNode(load_op, base, index, effect_input, control_input);
      effect_input = rep_nodes[i];
    }
562 563 564 565
  }
  ReplaceNode(node, rep_nodes, num_lanes);
}

566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
void SimdScalarLowering::LowerStoreOp(Node* node) {
  // For store operation, use replacement type of its input instead of the
  // one of its effected node.
  DCHECK_LT(2, node->InputCount());
  SimdType rep_type = ReplacementType(node->InputAt(2));
  replacements_[node->id()].type = rep_type;
  const Operator* store_op;
  MachineRepresentation rep;
  switch (node->opcode()) {
    case IrOpcode::kStore: {
      rep = StoreRepresentationOf(node->op()).representation();
      WriteBarrierKind write_barrier_kind =
          StoreRepresentationOf(node->op()).write_barrier_kind();
      store_op = machine()->Store(StoreRepresentation(
          MachineTypeFrom(rep_type).representation(), write_barrier_kind));
      break;
    }
    case IrOpcode::kUnalignedStore: {
      rep = UnalignedStoreRepresentationOf(node->op());
      store_op =
          machine()->UnalignedStore(MachineTypeFrom(rep_type).representation());
      break;
    }
    case IrOpcode::kProtectedStore: {
      rep = StoreRepresentationOf(node->op()).representation();
      store_op =
          machine()->ProtectedStore(MachineTypeFrom(rep_type).representation());
      break;
    }
    default:
      UNREACHABLE();
  }
598 599 600
  if (rep == MachineRepresentation::kSimd128) {
    Node* base = node->InputAt(0);
    Node* index = node->InputAt(1);
601 602 603
    int num_lanes = NumLanes(rep_type);
    Node** indices = zone()->NewArray<Node*>(num_lanes);
    GetIndexNodes(index, indices, rep_type);
604 605
    Node* value = node->InputAt(2);
    DCHECK(HasReplacement(1, value));
606
    Node** rep_nodes = zone()->NewArray<Node*>(num_lanes);
607 608 609
    rep_nodes[0] = node;
    Node** rep_inputs = GetReplacementsWithType(value, rep_type);
    rep_nodes[0]->ReplaceInput(2, rep_inputs[0]);
610
    rep_nodes[0]->ReplaceInput(1, indices[0]);
611 612
    NodeProperties::ChangeOp(node, store_op);
    if (node->InputCount() > 3) {
613
      DCHECK_LT(4, node->InputCount());
614 615
      Node* effect_input = node->InputAt(3);
      Node* control_input = node->InputAt(4);
616 617 618 619 620 621
      for (int i = num_lanes - 1; i > 0; --i) {
        rep_nodes[i] =
            graph()->NewNode(store_op, base, indices[i], rep_inputs[i],
                             effect_input, control_input);
        effect_input = rep_nodes[i];
      }
622 623
      rep_nodes[0]->ReplaceInput(3, rep_nodes[1]);
    } else {
624
      for (int i = 1; i < num_lanes; ++i) {
625 626 627 628
        rep_nodes[i] =
            graph()->NewNode(store_op, base, indices[i], rep_inputs[i]);
      }
    }
629
    ReplaceNode(node, rep_nodes, num_lanes);
630 631 632 633 634
  } else {
    DefaultLowering(node);
  }
}

635
void SimdScalarLowering::LowerBinaryOp(Node* node, SimdType input_rep_type,
636 637
                                       const Operator* op,
                                       bool not_horizontal) {
638
  DCHECK_EQ(2, node->InputCount());
639 640 641 642
  Node** rep_left = GetReplacementsWithType(node->InputAt(0), input_rep_type);
  Node** rep_right = GetReplacementsWithType(node->InputAt(1), input_rep_type);
  int num_lanes = NumLanes(input_rep_type);
  Node** rep_node = zone()->NewArray<Node*>(num_lanes);
643 644 645 646 647 648 649 650 651 652
  if (not_horizontal) {
    for (int i = 0; i < num_lanes; ++i) {
      rep_node[i] = graph()->NewNode(op, rep_left[i], rep_right[i]);
    }
  } else {
    for (int i = 0; i < num_lanes / 2; ++i) {
      rep_node[i] = graph()->NewNode(op, rep_left[i * 2], rep_left[i * 2 + 1]);
      rep_node[i + num_lanes / 2] =
          graph()->NewNode(op, rep_right[i * 2], rep_right[i * 2 + 1]);
    }
653 654 655 656 657 658 659
  }
  ReplaceNode(node, rep_node, num_lanes);
}

void SimdScalarLowering::LowerCompareOp(Node* node, SimdType input_rep_type,
                                        const Operator* op,
                                        bool invert_inputs) {
660
  DCHECK_EQ(2, node->InputCount());
661 662
  Node** rep_left = GetReplacementsWithType(node->InputAt(0), input_rep_type);
  Node** rep_right = GetReplacementsWithType(node->InputAt(1), input_rep_type);
663 664 665
  int num_lanes = NumLanes(input_rep_type);
  Node** rep_node = zone()->NewArray<Node*>(num_lanes);
  for (int i = 0; i < num_lanes; ++i) {
666
    Node* cmp_result = nullptr;
667
    if (invert_inputs) {
668
      cmp_result = graph()->NewNode(op, rep_right[i], rep_left[i]);
669
    } else {
670
      cmp_result = graph()->NewNode(op, rep_left[i], rep_right[i]);
671
    }
672 673
    Diamond d_cmp(graph(), common(),
                  graph()->NewNode(machine()->Word32Equal(), cmp_result,
674
                                   mcgraph_->Int32Constant(0)));
675 676 677 678 679
    MachineRepresentation rep =
        (input_rep_type == SimdType::kFloat32x4)
            ? MachineRepresentation::kWord32
            : MachineTypeFrom(input_rep_type).representation();
    rep_node[i] =
680
        d_cmp.Phi(rep, mcgraph_->Int32Constant(0), mcgraph_->Int32Constant(-1));
681
  }
682 683 684 685 686 687
  ReplaceNode(node, rep_node, num_lanes);
}

Node* SimdScalarLowering::FixUpperBits(Node* input, int32_t shift) {
  return graph()->NewNode(machine()->Word32Sar(),
                          graph()->NewNode(machine()->Word32Shl(), input,
688 689
                                           mcgraph_->Int32Constant(shift)),
                          mcgraph_->Int32Constant(shift));
690 691 692 693
}

void SimdScalarLowering::LowerBinaryOpForSmallInt(Node* node,
                                                  SimdType input_rep_type,
694 695
                                                  const Operator* op,
                                                  bool not_horizontal) {
696
  DCHECK_EQ(2, node->InputCount());
697 698
  DCHECK(input_rep_type == SimdType::kInt16x8 ||
         input_rep_type == SimdType::kInt8x16);
699 700 701 702
  Node** rep_left = GetReplacementsWithType(node->InputAt(0), input_rep_type);
  Node** rep_right = GetReplacementsWithType(node->InputAt(1), input_rep_type);
  int num_lanes = NumLanes(input_rep_type);
  Node** rep_node = zone()->NewArray<Node*>(num_lanes);
703 704
  int32_t shift_val =
      (input_rep_type == SimdType::kInt16x8) ? kShift16 : kShift8;
705 706 707 708 709 710 711 712 713 714 715 716 717 718
  if (not_horizontal) {
    for (int i = 0; i < num_lanes; ++i) {
      rep_node[i] = FixUpperBits(
          graph()->NewNode(op, rep_left[i], rep_right[i]), shift_val);
    }
  } else {
    for (int i = 0; i < num_lanes / 2; ++i) {
      rep_node[i] = FixUpperBits(
          graph()->NewNode(op, rep_left[i * 2], rep_left[i * 2 + 1]),
          shift_val);
      rep_node[i + num_lanes / 2] = FixUpperBits(
          graph()->NewNode(op, rep_right[i * 2], rep_right[i * 2 + 1]),
          shift_val);
    }
719 720 721 722 723 724
  }
  ReplaceNode(node, rep_node, num_lanes);
}

Node* SimdScalarLowering::Mask(Node* input, int32_t mask) {
  return graph()->NewNode(machine()->Word32And(), input,
725
                          mcgraph_->Int32Constant(mask));
726 727 728 729 730 731
}

void SimdScalarLowering::LowerSaturateBinaryOp(Node* node,
                                               SimdType input_rep_type,
                                               const Operator* op,
                                               bool is_signed) {
732
  DCHECK_EQ(2, node->InputCount());
733 734
  DCHECK(input_rep_type == SimdType::kInt16x8 ||
         input_rep_type == SimdType::kInt8x16);
735 736 737 738
  Node** rep_left = GetReplacementsWithType(node->InputAt(0), input_rep_type);
  Node** rep_right = GetReplacementsWithType(node->InputAt(1), input_rep_type);
  int32_t min = 0;
  int32_t max = 0;
739 740 741 742 743 744 745 746 747 748 749 750 751 752
  int32_t mask = 0;
  int32_t shift_val = 0;
  MachineRepresentation phi_rep;
  if (input_rep_type == SimdType::kInt16x8) {
    if (is_signed) {
      min = std::numeric_limits<int16_t>::min();
      max = std::numeric_limits<int16_t>::max();
    } else {
      min = std::numeric_limits<uint16_t>::min();
      max = std::numeric_limits<uint16_t>::max();
    }
    mask = kMask16;
    shift_val = kShift16;
    phi_rep = MachineRepresentation::kWord16;
753
  } else {
754 755 756 757 758 759 760 761 762 763
    if (is_signed) {
      min = std::numeric_limits<int8_t>::min();
      max = std::numeric_limits<int8_t>::max();
    } else {
      min = std::numeric_limits<uint8_t>::min();
      max = std::numeric_limits<uint8_t>::max();
    }
    mask = kMask8;
    shift_val = kShift8;
    phi_rep = MachineRepresentation::kWord8;
764 765 766 767 768
  }
  int num_lanes = NumLanes(input_rep_type);
  Node** rep_node = zone()->NewArray<Node*>(num_lanes);
  for (int i = 0; i < num_lanes; ++i) {
    Node* op_result = nullptr;
769 770
    Node* left = is_signed ? rep_left[i] : Mask(rep_left[i], mask);
    Node* right = is_signed ? rep_right[i] : Mask(rep_right[i], mask);
771 772 773
    op_result = graph()->NewNode(op, left, right);
    Diamond d_min(graph(), common(),
                  graph()->NewNode(machine()->Int32LessThan(), op_result,
774 775
                                   mcgraph_->Int32Constant(min)));
    rep_node[i] = d_min.Phi(phi_rep, mcgraph_->Int32Constant(min), op_result);
776 777
    Diamond d_max(graph(), common(),
                  graph()->NewNode(machine()->Int32LessThan(),
778 779
                                   mcgraph_->Int32Constant(max), rep_node[i]));
    rep_node[i] = d_max.Phi(phi_rep, mcgraph_->Int32Constant(max), rep_node[i]);
780 781
    rep_node[i] =
        is_signed ? rep_node[i] : FixUpperBits(rep_node[i], shift_val);
782 783
  }
  ReplaceNode(node, rep_node, num_lanes);
784 785
}

786 787
void SimdScalarLowering::LowerUnaryOp(Node* node, SimdType input_rep_type,
                                      const Operator* op) {
788
  DCHECK_EQ(1, node->InputCount());
789
  Node** rep = GetReplacementsWithType(node->InputAt(0), input_rep_type);
790 791 792
  int num_lanes = NumLanes(input_rep_type);
  Node** rep_node = zone()->NewArray<Node*>(num_lanes);
  for (int i = 0; i < num_lanes; ++i) {
793 794
    rep_node[i] = graph()->NewNode(op, rep[i]);
  }
795
  ReplaceNode(node, rep_node, num_lanes);
796 797
}

798
void SimdScalarLowering::LowerIntMinMax(Node* node, const Operator* op,
799
                                        bool is_max, SimdType type) {
800
  DCHECK_EQ(2, node->InputCount());
801 802 803 804 805 806 807 808 809
  Node** rep_left = GetReplacementsWithType(node->InputAt(0), type);
  Node** rep_right = GetReplacementsWithType(node->InputAt(1), type);
  int num_lanes = NumLanes(type);
  Node** rep_node = zone()->NewArray<Node*>(num_lanes);
  MachineRepresentation rep = MachineRepresentation::kNone;
  if (type == SimdType::kInt32x4) {
    rep = MachineRepresentation::kWord32;
  } else if (type == SimdType::kInt16x8) {
    rep = MachineRepresentation::kWord16;
810 811
  } else if (type == SimdType::kInt8x16) {
    rep = MachineRepresentation::kWord8;
812 813 814 815
  } else {
    UNREACHABLE();
  }
  for (int i = 0; i < num_lanes; ++i) {
816 817 818
    Diamond d(graph(), common(),
              graph()->NewNode(op, rep_left[i], rep_right[i]));
    if (is_max) {
819
      rep_node[i] = d.Phi(rep, rep_right[i], rep_left[i]);
820
    } else {
821
      rep_node[i] = d.Phi(rep, rep_left[i], rep_right[i]);
822 823
    }
  }
824
  ReplaceNode(node, rep_node, num_lanes);
825 826 827 828 829 830
}

Node* SimdScalarLowering::BuildF64Trunc(Node* input) {
  if (machine()->Float64RoundTruncate().IsSupported()) {
    return graph()->NewNode(machine()->Float64RoundTruncate().op(), input);
  } else {
831
    ExternalReference ref = ExternalReference::wasm_f64_trunc();
832 833 834 835 836
    Node* stack_slot =
        graph()->NewNode(machine()->StackSlot(MachineRepresentation::kFloat64));
    const Operator* store_op = machine()->Store(
        StoreRepresentation(MachineRepresentation::kFloat64, kNoWriteBarrier));
    Node* effect =
837
        graph()->NewNode(store_op, stack_slot, mcgraph_->Int32Constant(0),
838 839 840 841 842 843 844 845 846
                         input, graph()->start(), graph()->start());
    Node* function = graph()->NewNode(common()->ExternalConstant(ref));
    Node** args = zone()->NewArray<Node*>(4);
    args[0] = function;
    args[1] = stack_slot;
    args[2] = effect;
    args[3] = graph()->start();
    Signature<MachineType>::Builder sig_builder(zone(), 0, 1);
    sig_builder.AddParam(MachineType::Pointer());
847
    auto call_descriptor =
848
        Linkage::GetSimplifiedCDescriptor(zone(), sig_builder.Build());
849
    Node* call = graph()->NewNode(common()->Call(call_descriptor), 4, args);
850
    return graph()->NewNode(machine()->Load(LoadRepresentation::Float64()),
851
                            stack_slot, mcgraph_->Int32Constant(0), call,
852 853 854 855 856
                            graph()->start());
  }
}

void SimdScalarLowering::LowerConvertFromFloat(Node* node, bool is_signed) {
857
  DCHECK_EQ(1, node->InputCount());
858 859
  Node** rep = GetReplacementsWithType(node->InputAt(0), SimdType::kFloat32x4);
  Node* rep_node[kNumLanes32];
860 861 862 863
  Node* double_zero = graph()->NewNode(common()->Float64Constant(0.0));
  Node* min = graph()->NewNode(
      common()->Float64Constant(static_cast<double>(is_signed ? kMinInt : 0)));
  Node* max = graph()->NewNode(common()->Float64Constant(
864
      static_cast<double>(is_signed ? kMaxInt : 0xFFFFFFFFu)));
865
  for (int i = 0; i < kNumLanes32; ++i) {
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
    Node* double_rep =
        graph()->NewNode(machine()->ChangeFloat32ToFloat64(), rep[i]);
    Diamond nan_d(graph(), common(), graph()->NewNode(machine()->Float64Equal(),
                                                      double_rep, double_rep));
    Node* temp =
        nan_d.Phi(MachineRepresentation::kFloat64, double_rep, double_zero);
    Diamond min_d(graph(), common(),
                  graph()->NewNode(machine()->Float64LessThan(), temp, min));
    temp = min_d.Phi(MachineRepresentation::kFloat64, min, temp);
    Diamond max_d(graph(), common(),
                  graph()->NewNode(machine()->Float64LessThan(), max, temp));
    temp = max_d.Phi(MachineRepresentation::kFloat64, max, temp);
    Node* trunc = BuildF64Trunc(temp);
    if (is_signed) {
      rep_node[i] = graph()->NewNode(machine()->ChangeFloat64ToInt32(), trunc);
    } else {
      rep_node[i] =
          graph()->NewNode(machine()->TruncateFloat64ToUint32(), trunc);
    }
  }
886
  ReplaceNode(node, rep_node, kNumLanes32);
887 888
}

889 890 891
void SimdScalarLowering::LowerConvertFromInt(Node* node,
                                             SimdType input_rep_type,
                                             SimdType output_rep_type,
892
                                             bool is_signed, int start_index) {
893 894 895
  DCHECK_EQ(1, node->InputCount());
  Node** rep = GetReplacementsWithType(node->InputAt(0), input_rep_type);

896
  int32_t mask = 0;
897 898
  if (input_rep_type == SimdType::kInt16x8) {
    DCHECK_EQ(output_rep_type, SimdType::kInt32x4);
899
    mask = kMask16;
900 901 902
  } else {
    DCHECK_EQ(output_rep_type, SimdType::kInt16x8);
    DCHECK_EQ(input_rep_type, SimdType::kInt8x16);
903
    mask = kMask8;
904 905 906 907 908
  }

  int num_lanes = NumLanes(output_rep_type);
  Node** rep_node = zone()->NewArray<Node*>(num_lanes);
  for (int i = 0; i < num_lanes; ++i) {
909 910
    rep_node[i] =
        is_signed ? rep[i + start_index] : Mask(rep[i + start_index], mask);
911 912 913 914 915
  }

  ReplaceNode(node, rep_node, num_lanes);
}

916 917 918 919 920
void SimdScalarLowering::LowerPack(Node* node, SimdType input_rep_type,
                                   SimdType output_rep_type, bool is_signed) {
  DCHECK_EQ(2, node->InputCount());
  Node** rep_left = GetReplacementsWithType(node->InputAt(0), input_rep_type);
  Node** rep_right = GetReplacementsWithType(node->InputAt(1), input_rep_type);
921
  const Operator* less_op = machine()->Int32LessThan();
922 923 924 925 926 927
  Node* min = nullptr;
  Node* max = nullptr;
  MachineRepresentation phi_rep;
  if (output_rep_type == SimdType::kInt16x8) {
    DCHECK(input_rep_type == SimdType::kInt32x4);
    if (is_signed) {
928 929
      min = mcgraph_->Int32Constant(std::numeric_limits<int16_t>::min());
      max = mcgraph_->Int32Constant(std::numeric_limits<int16_t>::max());
930
    } else {
931
      min = mcgraph_->Int32Constant(std::numeric_limits<uint16_t>::min());
932
      max = mcgraph_->Uint32Constant(std::numeric_limits<uint16_t>::max());
933 934 935 936 937 938
    }
    phi_rep = MachineRepresentation::kWord16;
  } else {
    DCHECK(output_rep_type == SimdType::kInt8x16 &&
           input_rep_type == SimdType::kInt16x8);
    if (is_signed) {
939 940
      min = mcgraph_->Int32Constant(std::numeric_limits<int8_t>::min());
      max = mcgraph_->Int32Constant(std::numeric_limits<int8_t>::max());
941
    } else {
942
      min = mcgraph_->Int32Constant(std::numeric_limits<uint8_t>::min());
943
      max = mcgraph_->Uint32Constant(std::numeric_limits<uint8_t>::max());
944 945 946 947 948 949 950 951 952 953 954
    }
    phi_rep = MachineRepresentation::kWord8;
  }
  int num_lanes = NumLanes(output_rep_type);
  Node** rep_node = zone()->NewArray<Node*>(num_lanes);
  for (int i = 0; i < num_lanes; ++i) {
    Node* input = nullptr;
    if (i < num_lanes / 2)
      input = rep_left[i];
    else
      input = rep_right[i - num_lanes / 2];
955 956
    Diamond d_min(graph(), common(), graph()->NewNode(less_op, input, min));
    input = d_min.Phi(phi_rep, min, input);
957 958 959 960 961 962
    Diamond d_max(graph(), common(), graph()->NewNode(less_op, max, input));
    rep_node[i] = d_max.Phi(phi_rep, max, input);
  }
  ReplaceNode(node, rep_node, num_lanes);
}

963
void SimdScalarLowering::LowerShiftOp(Node* node, SimdType type) {
964 965
  DCHECK_EQ(2, node->InputCount());
  Node* shift_node = Mask(node->InputAt(1), GetMaskForShift(node));
966 967 968 969 970 971
  Node** rep = GetReplacementsWithType(node->InputAt(0), type);
  int num_lanes = NumLanes(type);
  Node** rep_node = zone()->NewArray<Node*>(num_lanes);
  for (int i = 0; i < num_lanes; ++i) {
    rep_node[i] = rep[i];
    switch (node->opcode()) {
972 973 974 975 976
      case IrOpcode::kI8x16ShrU:
        rep_node[i] = Mask(rep_node[i], kMask8);
        rep_node[i] =
            graph()->NewNode(machine()->Word32Shr(), rep_node[i], shift_node);
        break;
977
      case IrOpcode::kI16x8ShrU:
978 979
        rep_node[i] = Mask(rep_node[i], kMask16);
        V8_FALLTHROUGH;
980 981 982 983 984 985 986 987 988 989 990 991 992
      case IrOpcode::kI32x4ShrU:
        rep_node[i] =
            graph()->NewNode(machine()->Word32Shr(), rep_node[i], shift_node);
        break;
      case IrOpcode::kI32x4Shl:
        rep_node[i] =
            graph()->NewNode(machine()->Word32Shl(), rep_node[i], shift_node);
        break;
      case IrOpcode::kI16x8Shl:
        rep_node[i] =
            graph()->NewNode(machine()->Word32Shl(), rep_node[i], shift_node);
        rep_node[i] = FixUpperBits(rep_node[i], kShift16);
        break;
993 994 995 996 997
      case IrOpcode::kI8x16Shl:
        rep_node[i] =
            graph()->NewNode(machine()->Word32Shl(), rep_node[i], shift_node);
        rep_node[i] = FixUpperBits(rep_node[i], kShift8);
        break;
998 999
      case IrOpcode::kI32x4ShrS:
      case IrOpcode::kI16x8ShrS:
1000
      case IrOpcode::kI8x16ShrS:
1001 1002 1003 1004 1005 1006
        rep_node[i] =
            graph()->NewNode(machine()->Word32Sar(), rep_node[i], shift_node);
        break;
      default:
        UNREACHABLE();
    }
1007
  }
1008
  ReplaceNode(node, rep_node, num_lanes);
1009 1010
}

1011 1012
void SimdScalarLowering::LowerNotEqual(Node* node, SimdType input_rep_type,
                                       const Operator* op) {
1013
  DCHECK_EQ(2, node->InputCount());
1014 1015
  Node** rep_left = GetReplacementsWithType(node->InputAt(0), input_rep_type);
  Node** rep_right = GetReplacementsWithType(node->InputAt(1), input_rep_type);
1016 1017 1018
  int num_lanes = NumLanes(input_rep_type);
  Node** rep_node = zone()->NewArray<Node*>(num_lanes);
  for (int i = 0; i < num_lanes; ++i) {
1019 1020
    Diamond d(graph(), common(),
              graph()->NewNode(op, rep_left[i], rep_right[i]));
1021 1022 1023 1024 1025
    MachineRepresentation rep =
        (input_rep_type == SimdType::kFloat32x4)
            ? MachineRepresentation::kWord32
            : MachineTypeFrom(input_rep_type).representation();
    rep_node[i] =
1026
        d.Phi(rep, mcgraph_->Int32Constant(0), mcgraph_->Int32Constant(-1));
1027
  }
1028
  ReplaceNode(node, rep_node, num_lanes);
1029 1030
}

1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
void SimdScalarLowering::LowerBitMaskOp(Node* node, SimdType rep_type,
                                        int msb_index) {
  Node** reps = GetReplacementsWithType(node->InputAt(0), rep_type);
  int num_lanes = NumLanes(rep_type);
  Node** rep_node = zone()->NewArray<Node*>(1);
  Node* result = mcgraph_->Int32Constant(0);
  uint32_t mask = 1 << msb_index;

  for (int i = 0; i < num_lanes; ++i) {
    // Lane i should end up at bit i in the final result.
    // +-----------------------------------------------------------------+
    // |       | msb_index |   (i < msb_index)    |    (i > msb_index)   |
    // +-------+-----------+----------------------+----------------------+
    // | i8x16 |     7     | msb >> (msb_index-i) | msb << (i-msb_index) |
    // | i16x8 |    15     | msb >> (msb_index-i) |         n/a          |
    // | i32x4 |    31     | msb >> (msb_index-i) |         n/a          |
    // +-------+-----------+----------------------+----------------------+
    Node* msb = Mask(reps[i], mask);

    if (i < msb_index) {
      int shift = msb_index - i;
      Node* shifted = graph()->NewNode(machine()->Word32Shr(), msb,
                                       mcgraph_->Int32Constant(shift));
      result = graph()->NewNode(machine()->Word32Or(), shifted, result);
    } else if (i > msb_index) {
      int shift = i - msb_index;
      Node* shifted = graph()->NewNode(machine()->Word32Shl(), msb,
                                       mcgraph_->Int32Constant(shift));
      result = graph()->NewNode(machine()->Word32Or(), shifted, result);
    } else {
      result = graph()->NewNode(machine()->Word32Or(), msb, result);
    }
  }

  rep_node[0] = result;
  ReplaceNode(node, rep_node, 1);
}

1069 1070
void SimdScalarLowering::LowerNode(Node* node) {
  SimdType rep_type = ReplacementType(node);
1071
  int num_lanes = NumLanes(rep_type);
1072 1073 1074 1075
  switch (node->opcode()) {
    case IrOpcode::kStart: {
      int parameter_count = GetParameterCountAfterLowering();
      // Only exchange the node if the parameter count actually changed.
1076
      if (parameter_count != static_cast<int>(signature()->parameter_count())) {
1077 1078 1079 1080 1081 1082 1083 1084
        int delta =
            parameter_count - static_cast<int>(signature()->parameter_count());
        int new_output_count = node->op()->ValueOutputCount() + delta;
        NodeProperties::ChangeOp(node, common()->Start(new_output_count));
      }
      break;
    }
    case IrOpcode::kParameter: {
1085
      DCHECK_EQ(1, node->InputCount());
1086
      int param_count = static_cast<int>(signature()->parameter_count());
1087
      // Only exchange the node if the parameter count actually changed. We do
1088
      // not even have to do the default lowering because the start node,
1089 1090
      // the only input of a parameter node, only changes if the parameter count
      // changes.
1091
      if (GetParameterCountAfterLowering() != param_count) {
1092
        int old_index = ParameterIndexOf(node->op());
1093 1094 1095
        // Parameter index 0 is the instance parameter, we will use old_index to
        // index into the function signature, so we need to decrease it by 1.
        --old_index;
1096 1097
        int new_index =
            GetParameterIndexAfterLoweringSimd128(signature(), old_index);
1098 1099 1100 1101
        // Similarly, the index into function signature needs to account for the
        // instance parameter, so increase it by 1.
        ++new_index;
        NodeProperties::ChangeOp(node, common()->Parameter(new_index));
1102

1103 1104 1105 1106 1107 1108 1109 1110
        if (old_index < 0) {
          break;
        }

        DCHECK(old_index < param_count);

        if (signature()->GetParam(old_index) ==
            MachineRepresentation::kSimd128) {
1111
          Node* new_node[kNumLanes32];
1112
          new_node[0] = node;
1113 1114 1115
          for (int i = 1; i < kNumLanes32; ++i) {
            new_node[i] = graph()->NewNode(common()->Parameter(new_index + i),
                                           graph()->start());
1116
          }
1117
          ReplaceNode(node, new_node, kNumLanes32);
1118 1119 1120 1121
        }
      }
      break;
    }
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
    case IrOpcode::kSimd128ReverseBytes: {
      DCHECK_EQ(1, node->InputCount());
      bool is_float = ReplacementType(node->InputAt(0)) == SimdType::kFloat32x4;
      replacements_[node->id()].type =
          is_float ? SimdType::kFloat32x4 : SimdType::kInt32x4;
      Node** rep = GetReplacementsWithType(
          node->InputAt(0),
          is_float ? SimdType::kFloat32x4 : SimdType::kInt32x4);
      Node* rep_node[kNumLanes32];
      for (int i = 0; i < kNumLanes32; ++i) {
        Node* temp = is_float ? graph()->NewNode(
                                    machine()->BitcastFloat32ToInt32(), rep[i])
                              : rep[i];
        temp = graph()->NewNode(machine()->Word32ReverseBytes(), temp);
        rep_node[kNumLanes32 - 1 - i] =
            is_float
                ? graph()->NewNode(machine()->BitcastInt32ToFloat32(), temp)
                : temp;
      }
      ReplaceNode(node, rep_node, kNumLanes32);
      break;
    }
1144 1145 1146 1147
    case IrOpcode::kLoad:
    case IrOpcode::kUnalignedLoad:
    case IrOpcode::kProtectedLoad: {
      LowerLoadOp(node, rep_type);
1148 1149
      break;
    }
1150 1151 1152 1153
    case IrOpcode::kLoadTransform: {
      LowerLoadTransformOp(node, rep_type);
      break;
    }
1154 1155 1156 1157
    case IrOpcode::kStore:
    case IrOpcode::kUnalignedStore:
    case IrOpcode::kProtectedStore: {
      LowerStoreOp(node);
1158 1159
      break;
    }
1160 1161
    case IrOpcode::kReturn: {
      DefaultLowering(node);
1162
      int new_return_count = GetReturnCountAfterLoweringSimd128(signature());
1163
      if (static_cast<int>(signature()->return_count()) != new_return_count) {
1164 1165 1166 1167 1168
        NodeProperties::ChangeOp(node, common()->Return(new_return_count));
      }
      break;
    }
    case IrOpcode::kCall: {
1169
      // TODO(turbofan): Make wasm code const-correct wrt. CallDescriptor.
1170
      auto call_descriptor =
1171 1172
          const_cast<CallDescriptor*>(CallDescriptorOf(node->op()));
      if (DefaultLowering(node) ||
1173 1174
          (call_descriptor->ReturnCount() == 1 &&
           call_descriptor->GetReturnType(0) == MachineType::Simd128())) {
1175
        // We have to adjust the call descriptor.
1176 1177
        const Operator* op = common()->Call(
            GetI32WasmCallDescriptorForSimd(zone(), call_descriptor));
1178 1179
        NodeProperties::ChangeOp(node, op);
      }
1180 1181
      if (call_descriptor->ReturnCount() == 1 &&
          call_descriptor->GetReturnType(0) == MachineType::Simd128()) {
1182
        // We access the additional return values through projections.
1183 1184
        Node* rep_node[kNumLanes32];
        for (int i = 0; i < kNumLanes32; ++i) {
1185 1186 1187
          rep_node[i] =
              graph()->NewNode(common()->Projection(i), node, graph()->start());
        }
1188
        ReplaceNode(node, rep_node, kNumLanes32);
1189 1190 1191 1192 1193 1194 1195 1196 1197
      }
      break;
    }
    case IrOpcode::kPhi: {
      MachineRepresentation rep = PhiRepresentationOf(node->op());
      if (rep == MachineRepresentation::kSimd128) {
        // The replacement nodes have already been created, we only have to
        // replace placeholder nodes.
        Node** rep_node = GetReplacements(node);
1198
        for (int i = 0; i < node->op()->ValueInputCount(); ++i) {
1199 1200
          Node** rep_input =
              GetReplacementsWithType(node->InputAt(i), rep_type);
1201
          for (int j = 0; j < num_lanes; j++) {
1202 1203 1204 1205 1206 1207 1208 1209
            rep_node[j]->ReplaceInput(i, rep_input[j]);
          }
        }
      } else {
        DefaultLowering(node);
      }
      break;
    }
1210 1211 1212 1213 1214
#define I32X4_BINOP_CASE(opcode, instruction)                \
  case IrOpcode::opcode: {                                   \
    LowerBinaryOp(node, rep_type, machine()->instruction()); \
    break;                                                   \
  }
1215 1216 1217 1218 1219 1220
      I32X4_BINOP_CASE(kI32x4Add, Int32Add)
      I32X4_BINOP_CASE(kI32x4Sub, Int32Sub)
      I32X4_BINOP_CASE(kI32x4Mul, Int32Mul)
      I32X4_BINOP_CASE(kS128And, Word32And)
      I32X4_BINOP_CASE(kS128Or, Word32Or)
      I32X4_BINOP_CASE(kS128Xor, Word32Xor)
1221
#undef I32X4_BINOP_CASE
1222 1223 1224 1225
    case IrOpcode::kI32x4AddHoriz: {
      LowerBinaryOp(node, rep_type, machine()->Int32Add(), false);
      break;
    }
1226 1227 1228 1229
    case IrOpcode::kI16x8AddHoriz: {
      LowerBinaryOpForSmallInt(node, rep_type, machine()->Int32Add(), false);
      break;
    }
1230 1231
    case IrOpcode::kI16x8Add:
    case IrOpcode::kI8x16Add: {
1232 1233 1234
      LowerBinaryOpForSmallInt(node, rep_type, machine()->Int32Add());
      break;
    }
1235 1236
    case IrOpcode::kI16x8Sub:
    case IrOpcode::kI8x16Sub: {
1237 1238 1239
      LowerBinaryOpForSmallInt(node, rep_type, machine()->Int32Sub());
      break;
    }
1240 1241
    case IrOpcode::kI16x8Mul:
    case IrOpcode::kI8x16Mul: {
1242 1243 1244
      LowerBinaryOpForSmallInt(node, rep_type, machine()->Int32Mul());
      break;
    }
1245 1246
    case IrOpcode::kI16x8AddSaturateS:
    case IrOpcode::kI8x16AddSaturateS: {
1247 1248 1249
      LowerSaturateBinaryOp(node, rep_type, machine()->Int32Add(), true);
      break;
    }
1250 1251
    case IrOpcode::kI16x8SubSaturateS:
    case IrOpcode::kI8x16SubSaturateS: {
1252 1253 1254
      LowerSaturateBinaryOp(node, rep_type, machine()->Int32Sub(), true);
      break;
    }
1255 1256
    case IrOpcode::kI16x8AddSaturateU:
    case IrOpcode::kI8x16AddSaturateU: {
1257
      LowerSaturateBinaryOp(node, rep_type, machine()->Int32Add(), false);
1258 1259
      break;
    }
1260 1261
    case IrOpcode::kI16x8SubSaturateU:
    case IrOpcode::kI8x16SubSaturateU: {
1262
      LowerSaturateBinaryOp(node, rep_type, machine()->Int32Sub(), false);
1263 1264
      break;
    }
1265
    case IrOpcode::kI32x4MaxS:
1266 1267
    case IrOpcode::kI16x8MaxS:
    case IrOpcode::kI8x16MaxS: {
1268
      LowerIntMinMax(node, machine()->Int32LessThan(), true, rep_type);
1269 1270
      break;
    }
1271
    case IrOpcode::kI32x4MinS:
1272 1273
    case IrOpcode::kI16x8MinS:
    case IrOpcode::kI8x16MinS: {
1274 1275 1276 1277
      LowerIntMinMax(node, machine()->Int32LessThan(), false, rep_type);
      break;
    }
    case IrOpcode::kI32x4MaxU:
1278 1279
    case IrOpcode::kI16x8MaxU:
    case IrOpcode::kI8x16MaxU: {
1280 1281 1282 1283
      LowerIntMinMax(node, machine()->Uint32LessThan(), true, rep_type);
      break;
    }
    case IrOpcode::kI32x4MinU:
1284 1285
    case IrOpcode::kI16x8MinU:
    case IrOpcode::kI8x16MinU: {
1286
      LowerIntMinMax(node, machine()->Uint32LessThan(), false, rep_type);
1287 1288
      break;
    }
1289
    case IrOpcode::kI32x4Neg:
1290 1291
    case IrOpcode::kI16x8Neg:
    case IrOpcode::kI8x16Neg: {
1292
      DCHECK_EQ(1, node->InputCount());
1293
      Node** rep = GetReplacementsWithType(node->InputAt(0), rep_type);
1294 1295
      int num_lanes = NumLanes(rep_type);
      Node** rep_node = zone()->NewArray<Node*>(num_lanes);
1296
      Node* zero = graph()->NewNode(common()->Int32Constant(0));
1297
      for (int i = 0; i < num_lanes; ++i) {
1298
        rep_node[i] = graph()->NewNode(machine()->Int32Sub(), zero, rep[i]);
1299 1300
        if (node->opcode() == IrOpcode::kI16x8Neg) {
          rep_node[i] = FixUpperBits(rep_node[i], kShift16);
1301
        } else if (node->opcode() == IrOpcode::kI8x16Neg) {
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
          rep_node[i] = FixUpperBits(rep_node[i], kShift8);
        }
      }
      ReplaceNode(node, rep_node, num_lanes);
      break;
    }
    case IrOpcode::kI32x4Abs:
    case IrOpcode::kI16x8Abs:
    case IrOpcode::kI8x16Abs: {
      // From https://stackoverflow.com/a/14194764
      // abs(x) = (x XOR y) - y
      Node** rep = GetReplacementsWithType(node->InputAt(0), rep_type);
      Node** rep_node = zone()->NewArray<Node*>(num_lanes);
      for (int i = 0; i < num_lanes; ++i) {
        // It's fine to shift by 31 even for i8x16 since each node is
        // effectively expanded to 32 bits.
        Node* y = graph()->NewNode(machine()->Word32Sar(), rep[i],
                                   mcgraph_->Int32Constant(31));
        rep_node[i] = graph()->NewNode(
            machine()->Int32Sub(),
            graph()->NewNode(machine()->Word32Xor(), rep[i], y), y);
        if (node->opcode() == IrOpcode::kI16x8Neg) {
          rep_node[i] = FixUpperBits(rep_node[i], kShift16);
        } else if (node->opcode() == IrOpcode::kI8x16Neg) {
1326
          rep_node[i] = FixUpperBits(rep_node[i], kShift8);
1327
        }
1328
      }
1329
      ReplaceNode(node, rep_node, num_lanes);
1330 1331
      break;
    }
1332 1333 1334 1335 1336 1337 1338 1339 1340
    case IrOpcode::kS128Zero: {
      DCHECK_EQ(0, node->InputCount());
      Node* rep_node[kNumLanes32];
      for (int i = 0; i < kNumLanes32; ++i) {
        rep_node[i] = mcgraph_->Int32Constant(0);
      }
      ReplaceNode(node, rep_node, kNumLanes32);
      break;
    }
1341
    case IrOpcode::kS128Not: {
1342
      DCHECK_EQ(1, node->InputCount());
1343
      Node** rep = GetReplacementsWithType(node->InputAt(0), rep_type);
1344
      Node* rep_node[kNumLanes32];
1345
      Node* mask = graph()->NewNode(common()->Int32Constant(0xFFFFFFFF));
1346
      for (int i = 0; i < kNumLanes32; ++i) {
1347 1348
        rep_node[i] = graph()->NewNode(machine()->Word32Xor(), rep[i], mask);
      }
1349
      ReplaceNode(node, rep_node, kNumLanes32);
1350 1351
      break;
    }
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
    case IrOpcode::kS128AndNot: {
      DCHECK_EQ(2, node->InputCount());
      Node** rep_left = GetReplacementsWithType(node->InputAt(0), rep_type);
      Node** rep_right = GetReplacementsWithType(node->InputAt(1), rep_type);
      int num_lanes = NumLanes(rep_type);
      Node** rep_node = zone()->NewArray<Node*>(num_lanes);
      Node* mask = graph()->NewNode(common()->Int32Constant(0xFFFFFFFF));
      for (int i = 0; i < num_lanes; ++i) {
        Node* not_rep_right =
            graph()->NewNode(machine()->Word32Xor(), rep_right[i], mask);
        rep_node[i] = graph()->NewNode(machine()->Word32And(), rep_left[i],
                                       not_rep_right);
      }
      ReplaceNode(node, rep_node, num_lanes);
      break;
    }
1368
    case IrOpcode::kI32x4SConvertF32x4: {
1369 1370 1371
      LowerConvertFromFloat(node, true);
      break;
    }
1372
    case IrOpcode::kI32x4UConvertF32x4: {
1373 1374 1375
      LowerConvertFromFloat(node, false);
      break;
    }
1376 1377 1378 1379 1380
    case IrOpcode::kI32x4SConvertI16x8Low: {
      LowerConvertFromInt(node, SimdType::kInt16x8, SimdType::kInt32x4, true,
                          0);
      break;
    }
1381
    case IrOpcode::kI32x4SConvertI16x8High: {
1382 1383 1384 1385 1386 1387 1388
      LowerConvertFromInt(node, SimdType::kInt16x8, SimdType::kInt32x4, true,
                          4);
      break;
    }
    case IrOpcode::kI32x4UConvertI16x8Low: {
      LowerConvertFromInt(node, SimdType::kInt16x8, SimdType::kInt32x4, false,
                          0);
1389 1390 1391
      break;
    }
    case IrOpcode::kI32x4UConvertI16x8High: {
1392 1393 1394 1395 1396 1397 1398
      LowerConvertFromInt(node, SimdType::kInt16x8, SimdType::kInt32x4, false,
                          4);
      break;
    }
    case IrOpcode::kI16x8SConvertI8x16Low: {
      LowerConvertFromInt(node, SimdType::kInt8x16, SimdType::kInt16x8, true,
                          0);
1399 1400 1401
      break;
    }
    case IrOpcode::kI16x8SConvertI8x16High: {
1402 1403 1404 1405 1406 1407 1408
      LowerConvertFromInt(node, SimdType::kInt8x16, SimdType::kInt16x8, true,
                          8);
      break;
    }
    case IrOpcode::kI16x8UConvertI8x16Low: {
      LowerConvertFromInt(node, SimdType::kInt8x16, SimdType::kInt16x8, false,
                          0);
1409 1410 1411
      break;
    }
    case IrOpcode::kI16x8UConvertI8x16High: {
1412 1413
      LowerConvertFromInt(node, SimdType::kInt8x16, SimdType::kInt16x8, false,
                          8);
1414 1415
      break;
    }
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
    case IrOpcode::kI16x8SConvertI32x4: {
      LowerPack(node, SimdType::kInt32x4, SimdType::kInt16x8, true);
      break;
    }
    case IrOpcode::kI16x8UConvertI32x4: {
      LowerPack(node, SimdType::kInt32x4, SimdType::kInt16x8, false);
      break;
    }
    case IrOpcode::kI8x16SConvertI16x8: {
      LowerPack(node, SimdType::kInt16x8, SimdType::kInt8x16, true);
      break;
    }
    case IrOpcode::kI8x16UConvertI16x8: {
      LowerPack(node, SimdType::kInt16x8, SimdType::kInt8x16, false);
      break;
    }
1432 1433
    case IrOpcode::kI32x4Shl:
    case IrOpcode::kI16x8Shl:
1434
    case IrOpcode::kI8x16Shl:
1435 1436
    case IrOpcode::kI32x4ShrS:
    case IrOpcode::kI16x8ShrS:
1437
    case IrOpcode::kI8x16ShrS:
1438
    case IrOpcode::kI32x4ShrU:
1439 1440
    case IrOpcode::kI16x8ShrU:
    case IrOpcode::kI8x16ShrU: {
1441
      LowerShiftOp(node, rep_type);
1442 1443
      break;
    }
1444 1445 1446 1447
    case IrOpcode::kF32x4AddHoriz: {
      LowerBinaryOp(node, rep_type, machine()->Float32Add(), false);
      break;
    }
1448
#define F32X4_BINOP_CASE(name)                                 \
1449
  case IrOpcode::kF32x4##name: {                               \
1450 1451 1452 1453 1454 1455
    LowerBinaryOp(node, rep_type, machine()->Float32##name()); \
    break;                                                     \
  }
      F32X4_BINOP_CASE(Add)
      F32X4_BINOP_CASE(Sub)
      F32X4_BINOP_CASE(Mul)
1456
      F32X4_BINOP_CASE(Div)
1457 1458 1459
      F32X4_BINOP_CASE(Min)
      F32X4_BINOP_CASE(Max)
#undef F32X4_BINOP_CASE
1460
#define F32X4_UNOP_CASE(name)                                 \
1461
  case IrOpcode::kF32x4##name: {                              \
1462 1463 1464 1465 1466
    LowerUnaryOp(node, rep_type, machine()->Float32##name()); \
    break;                                                    \
  }
      F32X4_UNOP_CASE(Abs)
      F32X4_UNOP_CASE(Neg)
1467
      F32X4_UNOP_CASE(Sqrt)
Ng Zhi An's avatar
Ng Zhi An committed
1468
#undef F32X4_UNOP_CASE
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484
    case IrOpcode::kF32x4RecipApprox:
    case IrOpcode::kF32x4RecipSqrtApprox: {
      DCHECK_EQ(1, node->InputCount());
      Node** rep = GetReplacementsWithType(node->InputAt(0), rep_type);
      Node** rep_node = zone()->NewArray<Node*>(num_lanes);
      Node* float_one = graph()->NewNode(common()->Float32Constant(1.0));
      for (int i = 0; i < num_lanes; ++i) {
        Node* tmp = rep[i];
        if (node->opcode() == IrOpcode::kF32x4RecipSqrtApprox) {
          tmp = graph()->NewNode(machine()->Float32Sqrt(), rep[i]);
        }
        rep_node[i] = graph()->NewNode(machine()->Float32Div(), float_one, tmp);
      }
      ReplaceNode(node, rep_node, num_lanes);
      break;
    }
1485
    case IrOpcode::kF32x4SConvertI32x4: {
1486
      LowerUnaryOp(node, SimdType::kInt32x4, machine()->RoundInt32ToFloat32());
1487 1488
      break;
    }
1489
    case IrOpcode::kF32x4UConvertI32x4: {
1490
      LowerUnaryOp(node, SimdType::kInt32x4, machine()->RoundUint32ToFloat32());
1491 1492
      break;
    }
1493 1494
    case IrOpcode::kF64x2Splat:
    case IrOpcode::kF32x4Splat:
1495
    case IrOpcode::kI64x2Splat:
1496
    case IrOpcode::kI32x4Splat:
1497 1498
    case IrOpcode::kI16x8Splat:
    case IrOpcode::kI8x16Splat: {
1499 1500
      Node** rep_node = zone()->NewArray<Node*>(num_lanes);
      for (int i = 0; i < num_lanes; ++i) {
1501 1502
        if (HasReplacement(0, node->InputAt(0))) {
          rep_node[i] = GetReplacements(node->InputAt(0))[0];
1503
        } else {
1504
          rep_node[i] = node->InputAt(0);
1505
        }
1506
      }
1507
      ReplaceNode(node, rep_node, num_lanes);
1508 1509
      break;
    }
1510
    case IrOpcode::kI32x4ExtractLane:
1511
    case IrOpcode::kF32x4ExtractLane:
1512 1513 1514 1515
    case IrOpcode::kI16x8ExtractLaneU:
    case IrOpcode::kI16x8ExtractLaneS:
    case IrOpcode::kI8x16ExtractLaneU:
    case IrOpcode::kI8x16ExtractLaneS: {
1516
      int32_t lane = OpParameter<int32_t>(node->op());
1517 1518 1519 1520 1521 1522
      Node** rep_node = zone()->NewArray<Node*>(num_lanes);
      rep_node[0] = GetReplacementsWithType(node->InputAt(0), rep_type)[lane];
      for (int i = 1; i < num_lanes; ++i) {
        rep_node[i] = nullptr;
      }
      ReplaceNode(node, rep_node, num_lanes);
1523 1524
      break;
    }
1525
    case IrOpcode::kI32x4ReplaceLane:
1526
    case IrOpcode::kF32x4ReplaceLane:
1527 1528
    case IrOpcode::kI16x8ReplaceLane:
    case IrOpcode::kI8x16ReplaceLane: {
1529 1530
      DCHECK_EQ(2, node->InputCount());
      Node* repNode = node->InputAt(1);
1531
      int32_t lane = OpParameter<int32_t>(node->op());
1532 1533 1534 1535 1536
      Node** old_rep_node = GetReplacementsWithType(node->InputAt(0), rep_type);
      Node** rep_node = zone()->NewArray<Node*>(num_lanes);
      for (int i = 0; i < num_lanes; ++i) {
        rep_node[i] = old_rep_node[i];
      }
1537 1538 1539 1540 1541
      if (HasReplacement(0, repNode)) {
        rep_node[lane] = GetReplacements(repNode)[0];
      } else {
        rep_node[lane] = repNode;
      }
1542
      ReplaceNode(node, rep_node, num_lanes);
1543 1544
      break;
    }
1545 1546 1547 1548
#define COMPARISON_CASE(type, simd_op, lowering_op, invert)                    \
  case IrOpcode::simd_op: {                                                    \
    LowerCompareOp(node, SimdType::k##type, machine()->lowering_op(), invert); \
    break;                                                                     \
1549
  }
1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
      COMPARISON_CASE(Float32x4, kF32x4Eq, Float32Equal, false)
      COMPARISON_CASE(Float32x4, kF32x4Lt, Float32LessThan, false)
      COMPARISON_CASE(Float32x4, kF32x4Le, Float32LessThanOrEqual, false)
      COMPARISON_CASE(Float32x4, kF32x4Gt, Float32LessThan, true)
      COMPARISON_CASE(Float32x4, kF32x4Ge, Float32LessThanOrEqual, true)
      COMPARISON_CASE(Int32x4, kI32x4Eq, Word32Equal, false)
      COMPARISON_CASE(Int32x4, kI32x4LtS, Int32LessThan, false)
      COMPARISON_CASE(Int32x4, kI32x4LeS, Int32LessThanOrEqual, false)
      COMPARISON_CASE(Int32x4, kI32x4GtS, Int32LessThan, true)
      COMPARISON_CASE(Int32x4, kI32x4GeS, Int32LessThanOrEqual, true)
      COMPARISON_CASE(Int32x4, kI32x4LtU, Uint32LessThan, false)
      COMPARISON_CASE(Int32x4, kI32x4LeU, Uint32LessThanOrEqual, false)
      COMPARISON_CASE(Int32x4, kI32x4GtU, Uint32LessThan, true)
      COMPARISON_CASE(Int32x4, kI32x4GeU, Uint32LessThanOrEqual, true)
      COMPARISON_CASE(Int16x8, kI16x8Eq, Word32Equal, false)
      COMPARISON_CASE(Int16x8, kI16x8LtS, Int32LessThan, false)
      COMPARISON_CASE(Int16x8, kI16x8LeS, Int32LessThanOrEqual, false)
      COMPARISON_CASE(Int16x8, kI16x8GtS, Int32LessThan, true)
      COMPARISON_CASE(Int16x8, kI16x8GeS, Int32LessThanOrEqual, true)
      COMPARISON_CASE(Int16x8, kI16x8LtU, Uint32LessThan, false)
      COMPARISON_CASE(Int16x8, kI16x8LeU, Uint32LessThanOrEqual, false)
      COMPARISON_CASE(Int16x8, kI16x8GtU, Uint32LessThan, true)
      COMPARISON_CASE(Int16x8, kI16x8GeU, Uint32LessThanOrEqual, true)
1573 1574 1575 1576 1577 1578 1579 1580 1581
      COMPARISON_CASE(Int8x16, kI8x16Eq, Word32Equal, false)
      COMPARISON_CASE(Int8x16, kI8x16LtS, Int32LessThan, false)
      COMPARISON_CASE(Int8x16, kI8x16LeS, Int32LessThanOrEqual, false)
      COMPARISON_CASE(Int8x16, kI8x16GtS, Int32LessThan, true)
      COMPARISON_CASE(Int8x16, kI8x16GeS, Int32LessThanOrEqual, true)
      COMPARISON_CASE(Int8x16, kI8x16LtU, Uint32LessThan, false)
      COMPARISON_CASE(Int8x16, kI8x16LeU, Uint32LessThanOrEqual, false)
      COMPARISON_CASE(Int8x16, kI8x16GtU, Uint32LessThan, true)
      COMPARISON_CASE(Int8x16, kI8x16GeU, Uint32LessThanOrEqual, true)
1582
#undef COMPARISON_CASE
1583
    case IrOpcode::kF32x4Ne: {
1584
      LowerNotEqual(node, SimdType::kFloat32x4, machine()->Float32Equal());
1585 1586
      break;
    }
1587
    case IrOpcode::kI32x4Ne: {
1588 1589 1590 1591 1592
      LowerNotEqual(node, SimdType::kInt32x4, machine()->Word32Equal());
      break;
    }
    case IrOpcode::kI16x8Ne: {
      LowerNotEqual(node, SimdType::kInt16x8, machine()->Word32Equal());
1593 1594
      break;
    }
1595 1596 1597 1598
    case IrOpcode::kI8x16Ne: {
      LowerNotEqual(node, SimdType::kInt8x16, machine()->Word32Equal());
      break;
    }
1599
    case IrOpcode::kS128Select: {
1600
      DCHECK_EQ(3, node->InputCount());
1601 1602 1603
      DCHECK(ReplacementType(node->InputAt(0)) == SimdType::kInt32x4 ||
             ReplacementType(node->InputAt(0)) == SimdType::kInt16x8 ||
             ReplacementType(node->InputAt(0)) == SimdType::kInt8x16);
1604 1605 1606
      Node** boolean_input = GetReplacements(node->InputAt(0));
      Node** rep_left = GetReplacementsWithType(node->InputAt(1), rep_type);
      Node** rep_right = GetReplacementsWithType(node->InputAt(2), rep_type);
1607 1608
      Node** rep_node = zone()->NewArray<Node*>(num_lanes);
      for (int i = 0; i < num_lanes; ++i) {
1609 1610 1611 1612 1613 1614
        Node* tmp1 =
            graph()->NewNode(machine()->Word32Xor(), rep_left[i], rep_right[i]);
        Node* tmp2 =
            graph()->NewNode(machine()->Word32And(), boolean_input[i], tmp1);
        rep_node[i] =
            graph()->NewNode(machine()->Word32Xor(), rep_right[i], tmp2);
1615
      }
1616
      ReplaceNode(node, rep_node, num_lanes);
1617 1618
      break;
    }
1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
    case IrOpcode::kS8x16Swizzle: {
      DCHECK_EQ(2, node->InputCount());
      Node** rep_left = GetReplacementsWithType(node->InputAt(0), rep_type);
      Node** indices = GetReplacementsWithType(node->InputAt(1), rep_type);
      Node** rep_nodes = zone()->NewArray<Node*>(num_lanes);
      Node* stack_slot = graph()->NewNode(
          machine()->StackSlot(MachineRepresentation::kSimd128));

      // Push all num_lanes values into stack slot.
      const Operator* store_op = machine()->Store(
          StoreRepresentation(MachineRepresentation::kWord8, kNoWriteBarrier));
      Node* effect_input = graph()->start();
      for (int i = num_lanes - 1; i >= 0; i--) {
        // We want all the stores to happen first before any of the loads
        // below, so connect them via effect edge from i-1 to i.
        Node* store =
            graph()->NewNode(store_op, stack_slot, mcgraph_->Int32Constant(i),
                             rep_left[i], effect_input, graph()->start());
        effect_input = store;
      }

      for (int i = num_lanes - 1; i >= 0; i--) {
        // Only select lane when index is < num_lanes, otherwise write 0 to
        // lane. Use Uint32 to take care of negative indices.
        Diamond d(graph(), common(),
                  graph()->NewNode(machine()->Uint32LessThan(), indices[i],
                                   mcgraph_->Int32Constant(num_lanes)));

        Node* load =
            graph()->NewNode(machine()->Load(LoadRepresentation::Uint8()),
                             stack_slot, indices[i], effect_input, d.if_true);

        rep_nodes[i] = d.Phi(MachineRepresentation::kWord8, load,
                             mcgraph_->Int32Constant(0));
      }

      ReplaceNode(node, rep_nodes, num_lanes);
      break;
    }
1658 1659
    case IrOpcode::kS8x16Shuffle: {
      DCHECK_EQ(2, node->InputCount());
1660
      S128ImmediateParameter shuffle = S128ImmediateParameterOf(node->op());
1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
      Node** rep_left = GetReplacementsWithType(node->InputAt(0), rep_type);
      Node** rep_right = GetReplacementsWithType(node->InputAt(1), rep_type);
      Node** rep_node = zone()->NewArray<Node*>(16);
      for (int i = 0; i < 16; i++) {
        int lane = shuffle[i];
        rep_node[i] = lane < 16 ? rep_left[lane] : rep_right[lane - 16];
      }
      ReplaceNode(node, rep_node, 16);
      break;
    }
1671 1672 1673 1674 1675 1676
    case IrOpcode::kV32x4AnyTrue:
    case IrOpcode::kV32x4AllTrue:
    case IrOpcode::kV16x8AnyTrue:
    case IrOpcode::kV16x8AllTrue:
    case IrOpcode::kV8x16AnyTrue:
    case IrOpcode::kV8x16AllTrue: {
1677 1678
      DCHECK_EQ(1, node->InputCount());
      SimdType input_rep_type = ReplacementType(node->InputAt(0));
1679 1680 1681 1682 1683 1684 1685 1686 1687
      Node** rep;
      // If the input is a SIMD float, bitcast it to a SIMD int of the same
      // shape, because the comparisons below use Word32.
      if (input_rep_type == SimdType::kFloat32x4) {
        // TODO(v8:9418): f64x2 lowering is not implemented yet.
        rep = GetReplacementsWithType(node->InputAt(0), SimdType::kInt32x4);
      } else {
        rep = GetReplacements(node->InputAt(0));
      }
1688 1689
      int input_num_lanes = NumLanes(input_rep_type);
      Node** rep_node = zone()->NewArray<Node*>(num_lanes);
1690
      Node* true_node = mcgraph_->Int32Constant(1);
1691
      Node* false_node = mcgraph_->Int32Constant(0);
1692
      Node* tmp_result = false_node;
1693 1694 1695
      if (node->opcode() == IrOpcode::kV32x4AllTrue ||
          node->opcode() == IrOpcode::kV16x8AllTrue ||
          node->opcode() == IrOpcode::kV8x16AllTrue) {
1696 1697 1698 1699 1700 1701
        tmp_result = true_node;
      }
      for (int i = 0; i < input_num_lanes; ++i) {
        Diamond is_false(
            graph(), common(),
            graph()->NewNode(machine()->Word32Equal(), rep[i], false_node));
1702 1703 1704
        if (node->opcode() == IrOpcode::kV32x4AllTrue ||
            node->opcode() == IrOpcode::kV16x8AllTrue ||
            node->opcode() == IrOpcode::kV8x16AllTrue) {
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718
          tmp_result = is_false.Phi(MachineRepresentation::kWord32, false_node,
                                    tmp_result);
        } else {
          tmp_result = is_false.Phi(MachineRepresentation::kWord32, tmp_result,
                                    true_node);
        }
      }
      rep_node[0] = tmp_result;
      for (int i = 1; i < num_lanes; ++i) {
        rep_node[i] = nullptr;
      }
      ReplaceNode(node, rep_node, num_lanes);
      break;
    }
1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730
    case IrOpcode::kI8x16BitMask: {
      LowerBitMaskOp(node, rep_type, 7);
      break;
    }
    case IrOpcode::kI16x8BitMask: {
      LowerBitMaskOp(node, rep_type, 15);
      break;
    }
    case IrOpcode::kI32x4BitMask: {
      LowerBitMaskOp(node, rep_type, 31);
      break;
    }
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750
    case IrOpcode::kI8x16RoundingAverageU:
    case IrOpcode::kI16x8RoundingAverageU: {
      DCHECK_EQ(2, node->InputCount());
      Node** rep_left = GetReplacementsWithType(node->InputAt(0), rep_type);
      Node** rep_right = GetReplacementsWithType(node->InputAt(1), rep_type);
      int num_lanes = NumLanes(rep_type);
      Node** rep_node = zone()->NewArray<Node*>(num_lanes);
      // rounding_average(left, right) = (left + right + 1) >> 1
      for (int i = 0; i < num_lanes; ++i) {
        Node* left_plus_right_plus_one = graph()->NewNode(
            machine()->Int32Add(),
            graph()->NewNode(machine()->Int32Add(), rep_left[i], rep_right[i]),
            mcgraph_->Int32Constant(1));
        rep_node[i] =
            graph()->NewNode(machine()->Word32Shr(), left_plus_right_plus_one,
                             mcgraph_->Int32Constant(1));
      }
      ReplaceNode(node, rep_node, num_lanes);
      break;
    }
1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762
    default: { DefaultLowering(node); }
  }
}

bool SimdScalarLowering::DefaultLowering(Node* node) {
  bool something_changed = false;
  for (int i = NodeProperties::PastValueIndex(node) - 1; i >= 0; i--) {
    Node* input = node->InputAt(i);
    if (HasReplacement(0, input)) {
      something_changed = true;
      node->ReplaceInput(i, GetReplacements(input)[0]);
    }
1763
    if (ReplacementCount(input) > 1 && HasReplacement(1, input)) {
1764
      something_changed = true;
1765
      for (int j = 1; j < ReplacementCount(input); ++j) {
1766 1767 1768 1769 1770 1771 1772
        node->InsertInput(zone(), i + j, GetReplacements(input)[j]);
      }
    }
  }
  return something_changed;
}

1773 1774 1775 1776
void SimdScalarLowering::ReplaceNode(Node* old, Node** new_nodes, int count) {
  replacements_[old->id()].node = zone()->NewArray<Node*>(count);
  for (int i = 0; i < count; ++i) {
    replacements_[old->id()].node[i] = new_nodes[i];
1777
  }
1778
  replacements_[old->id()].num_replacements = count;
1779 1780 1781
}

bool SimdScalarLowering::HasReplacement(size_t index, Node* node) {
1782 1783
  return replacements_[node->id()].node != nullptr &&
         replacements_[node->id()].node[index] != nullptr;
1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
}

SimdScalarLowering::SimdType SimdScalarLowering::ReplacementType(Node* node) {
  return replacements_[node->id()].type;
}

Node** SimdScalarLowering::GetReplacements(Node* node) {
  Node** result = replacements_[node->id()].node;
  DCHECK(result);
  return result;
}

1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821
int SimdScalarLowering::ReplacementCount(Node* node) {
  return replacements_[node->id()].num_replacements;
}

void SimdScalarLowering::Int32ToFloat32(Node** replacements, Node** result) {
  for (int i = 0; i < kNumLanes32; ++i) {
    if (replacements[i] != nullptr) {
      result[i] =
          graph()->NewNode(machine()->BitcastInt32ToFloat32(), replacements[i]);
    } else {
      result[i] = nullptr;
    }
  }
}

void SimdScalarLowering::Float32ToInt32(Node** replacements, Node** result) {
  for (int i = 0; i < kNumLanes32; ++i) {
    if (replacements[i] != nullptr) {
      result[i] =
          graph()->NewNode(machine()->BitcastFloat32ToInt32(), replacements[i]);
    } else {
      result[i] = nullptr;
    }
  }
}

1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874
template <typename T>
void SimdScalarLowering::Int32ToSmallerInt(Node** replacements, Node** result) {
  const int num_ints = sizeof(int32_t) / sizeof(T);
  const int bit_size = sizeof(T) * 8;
  const Operator* sign_extend;
  switch (sizeof(T)) {
    case 1:
      sign_extend = machine()->SignExtendWord8ToInt32();
      break;
    case 2:
      sign_extend = machine()->SignExtendWord16ToInt32();
      break;
    default:
      UNREACHABLE();
  }

  for (int i = 0; i < kNumLanes32; i++) {
    if (replacements[i] != nullptr) {
      for (int j = 0; j < num_ints; j++) {
        result[num_ints * i + j] = graph()->NewNode(
            sign_extend,
            graph()->NewNode(machine()->Word32Sar(), replacements[i],
                             mcgraph_->Int32Constant(j * bit_size)));
      }
    } else {
      for (int j = 0; j < num_ints; j++) {
        result[num_ints * i + j] = nullptr;
      }
    }
  }
}

template <typename T>
void SimdScalarLowering::SmallerIntToInt32(Node** replacements, Node** result) {
  const int num_ints = sizeof(int32_t) / sizeof(T);
  const int bit_size = sizeof(T) * 8;
  const int bit_mask = (1 << bit_size) - 1;

  for (int i = 0; i < kNumLanes32; ++i) {
    result[i] = mcgraph_->Int32Constant(0);
    for (int j = 0; j < num_ints; j++) {
      if (replacements[num_ints * i + j] != nullptr) {
        Node* clean_bits = graph()->NewNode(machine()->Word32And(),
                                            replacements[num_ints * i + j],
                                            mcgraph_->Int32Constant(bit_mask));
        Node* shift = graph()->NewNode(machine()->Word32Shl(), clean_bits,
                                       mcgraph_->Int32Constant(j * bit_size));
        result[i] = graph()->NewNode(machine()->Word32Or(), result[i], shift);
      }
    }
  }
}

1875 1876 1877 1878 1879
Node** SimdScalarLowering::GetReplacementsWithType(Node* node, SimdType type) {
  Node** replacements = GetReplacements(node);
  if (ReplacementType(node) == type) {
    return GetReplacements(node);
  }
1880 1881 1882 1883 1884 1885
  int num_lanes = NumLanes(type);
  Node** result = zone()->NewArray<Node*>(num_lanes);
  if (type == SimdType::kInt32x4) {
    if (ReplacementType(node) == SimdType::kFloat32x4) {
      Float32ToInt32(replacements, result);
    } else if (ReplacementType(node) == SimdType::kInt16x8) {
1886 1887 1888
      SmallerIntToInt32<int16_t>(replacements, result);
    } else if (ReplacementType(node) == SimdType::kInt8x16) {
      SmallerIntToInt32<int8_t>(replacements, result);
1889 1890
    } else {
      UNREACHABLE();
1891
    }
1892 1893 1894 1895 1896
  } else if (type == SimdType::kFloat32x4) {
    if (ReplacementType(node) == SimdType::kInt32x4) {
      Int32ToFloat32(replacements, result);
    } else if (ReplacementType(node) == SimdType::kInt16x8) {
      UNIMPLEMENTED();
1897 1898 1899
    } else if (ReplacementType(node) == SimdType::kInt8x16) {
      SmallerIntToInt32<int8_t>(replacements, result);
      Int32ToFloat32(result, result);
1900 1901 1902 1903
    } else {
      UNREACHABLE();
    }
  } else if (type == SimdType::kInt16x8) {
1904 1905 1906
    if (ReplacementType(node) == SimdType::kInt32x4) {
      Int32ToSmallerInt<int16_t>(replacements, result);
    } else if (ReplacementType(node) == SimdType::kFloat32x4) {
1907 1908 1909
      UNIMPLEMENTED();
    } else {
      UNREACHABLE();
1910
    }
1911 1912 1913 1914 1915 1916
  } else if (type == SimdType::kInt8x16) {
    if (ReplacementType(node) == SimdType::kInt32x4) {
      Int32ToSmallerInt<int8_t>(replacements, result);
    } else {
      UNIMPLEMENTED();
    }
1917 1918
  } else {
    UNREACHABLE();
1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931
  }
  return result;
}

void SimdScalarLowering::PreparePhiReplacement(Node* phi) {
  MachineRepresentation rep = PhiRepresentationOf(phi->op());
  if (rep == MachineRepresentation::kSimd128) {
    // We have to create the replacements for a phi node before we actually
    // lower the phi to break potential cycles in the graph. The replacements of
    // input nodes do not exist yet, so we use a placeholder node to pass the
    // graph verifier.
    int value_count = phi->op()->ValueInputCount();
    SimdType type = ReplacementType(phi);
1932 1933 1934
    int num_lanes = NumLanes(type);
    Node*** inputs_rep = zone()->NewArray<Node**>(num_lanes);
    for (int i = 0; i < num_lanes; ++i) {
1935 1936 1937
      inputs_rep[i] = zone()->NewArray<Node*>(value_count + 1);
      inputs_rep[i][value_count] = NodeProperties::GetControlInput(phi, 0);
    }
1938
    for (int i = 0; i < value_count; ++i) {
1939
      for (int j = 0; j < num_lanes; ++j) {
1940 1941 1942
        inputs_rep[j][i] = placeholder_;
      }
    }
1943 1944
    Node** rep_nodes = zone()->NewArray<Node*>(num_lanes);
    for (int i = 0; i < num_lanes; ++i) {
1945 1946 1947
      rep_nodes[i] = graph()->NewNode(
          common()->Phi(MachineTypeFrom(type).representation(), value_count),
          value_count + 1, inputs_rep[i], false);
1948
    }
1949
    ReplaceNode(phi, rep_nodes, num_lanes);
1950 1951 1952 1953 1954
  }
}
}  // namespace compiler
}  // namespace internal
}  // namespace v8